ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
| 32 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 33 | #include "parser.h" |
| 34 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 36 | #include "scopes.h" |
| 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { namespace internal { |
| 40 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 41 | #define __ masm_-> |
| 42 | |
| 43 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 44 | // CodeGenState implementation. |
| 45 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 46 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 47 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 48 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 49 | true_target_(NULL), |
| 50 | false_target_(NULL), |
| 51 | previous_(NULL) { |
| 52 | owner_->set_state(this); |
| 53 | } |
| 54 | |
| 55 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 56 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 57 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 58 | JumpTarget* true_target, |
| 59 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 60 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 61 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 62 | true_target_(true_target), |
| 63 | false_target_(false_target), |
| 64 | previous_(owner->state()) { |
| 65 | owner_->set_state(this); |
| 66 | } |
| 67 | |
| 68 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 69 | CodeGenState::~CodeGenState() { |
| 70 | ASSERT(owner_->state() == this); |
| 71 | owner_->set_state(previous_); |
| 72 | } |
| 73 | |
| 74 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 75 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 76 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 78 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 79 | bool is_eval) |
| 80 | : is_eval_(is_eval), |
| 81 | script_(script), |
| 82 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 84 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 85 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 86 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | cc_reg_(al), |
| 88 | state_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 89 | function_return_is_shadowed_(false), |
| 90 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 95 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 97 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | // cp: callee's context |
| 99 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 100 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | ZoneList<Statement*>* body = fun->body(); |
| 102 | |
| 103 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 104 | ASSERT(scope_ == NULL); |
| 105 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 106 | ASSERT(allocator_ == NULL); |
| 107 | RegisterAllocator register_allocator(this); |
| 108 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 109 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 110 | frame_ = new VirtualFrame(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 111 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 112 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 113 | { |
| 114 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 116 | // Entry: |
| 117 | // Stack: receiver, arguments |
| 118 | // lr: return address |
| 119 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 121 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 123 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 124 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | // tos: code slot |
| 126 | #ifdef DEBUG |
| 127 | if (strlen(FLAG_stop_at) > 0 && |
| 128 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 129 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 130 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | } |
| 132 | #endif |
| 133 | |
| 134 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 135 | frame_->AllocateStackSlots(scope_->num_stack_slots()); |
| 136 | // Initialize the function return target after the locals are set |
| 137 | // up, because it needs the expected frame height from the frame. |
| 138 | function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 139 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 142 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | // Allocate local context. |
| 144 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 145 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 146 | frame_->EmitPush(r0); |
| 147 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 148 | |
| 149 | if (kDebug) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 150 | JumpTarget verified_true(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 151 | __ cmp(r0, Operand(cp)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 152 | verified_true.Branch(eq); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 153 | __ stop("NewContext: r0 is expected to be the same as cp"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 154 | verified_true.Bind(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 155 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 157 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | } |
| 159 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 160 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | // 1) only needed if we have a context |
| 162 | // 2) no need to recompute context ptr every single time |
| 163 | // 3) don't copy parameter operand code from SlotOperand! |
| 164 | { |
| 165 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 166 | |
| 167 | // Note that iteration order is relevant here! If we have the same |
| 168 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 169 | // needs to be copied into the context, it must be the last argument |
| 170 | // passed to the parameter that needs to be copied. This is a rare |
| 171 | // case so we don't check for it, instead we rely on the copying |
| 172 | // order: such a parameter is copied repeatedly into the same |
| 173 | // context location and thus the last value is what is seen inside |
| 174 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 175 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 176 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | Slot* slot = par->slot(); |
| 178 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 179 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 180 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | // Loads r2 with context; used below in RecordWrite. |
| 182 | __ str(r1, SlotOperand(slot, r2)); |
| 183 | // Load the offset into r3. |
| 184 | int slot_offset = |
| 185 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 186 | __ mov(r3, Operand(slot_offset)); |
| 187 | __ RecordWrite(r2, r3, r1); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 192 | // Store the arguments object. This must happen after context |
| 193 | // initialization because the arguments object may be stored in the |
| 194 | // context. |
| 195 | if (scope_->arguments() != NULL) { |
| 196 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 198 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 199 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 200 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 201 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | // The receiver is below the arguments, the return address, |
| 203 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 204 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 205 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 206 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 207 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 208 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 209 | frame_->CallStub(&stub, 3); |
| 210 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 211 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 212 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 213 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 215 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | } |
| 217 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 218 | // Generate code to 'execute' declarations and initialize functions |
| 219 | // (source elements). In case of an illegal redeclaration we need to |
| 220 | // handle that instead of processing the declarations. |
| 221 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 223 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | } else { |
| 225 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 226 | ProcessDeclarations(scope_->declarations()); |
| 227 | // Bail out if a stack-overflow exception occurred when processing |
| 228 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 229 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 232 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 233 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 234 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 235 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 236 | CheckStack(); |
| 237 | |
| 238 | // Compile the body of the function in a vanilla state. Don't |
| 239 | // bother compiling all the code if the scope has an illegal |
| 240 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 241 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | Comment cmnt(masm_, "[ function body"); |
| 243 | #ifdef DEBUG |
| 244 | bool is_builtin = Bootstrapper::IsActive(); |
| 245 | bool should_trace = |
| 246 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 247 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 248 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 249 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 252 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | } |
| 255 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 256 | // Generate the return sequence if necessary. |
| 257 | if (frame_ != NULL || function_return_.is_linked()) { |
| 258 | // exit |
| 259 | // r0: result |
| 260 | // sp: stack pointer |
| 261 | // fp: frame pointer |
| 262 | // pp: parameter pointer |
| 263 | // cp: callee's context |
| 264 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 265 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 266 | function_return_.Bind(); |
| 267 | if (FLAG_trace) { |
| 268 | // Push the return value on the stack as the parameter. |
| 269 | // Runtime::TraceExit returns the parameter as it is. |
| 270 | frame_->EmitPush(r0); |
| 271 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 272 | } |
| 273 | |
| 274 | // Tear down the frame which will restore the caller's frame pointer and |
| 275 | // the link register. |
| 276 | frame_->Exit(); |
| 277 | |
| 278 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
| 279 | __ mov(pc, lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 280 | } |
| 281 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | ASSERT(!has_cc()); |
| 284 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 285 | ASSERT(!function_return_is_shadowed_); |
| 286 | function_return_.Unuse(); |
| 287 | DeleteFrame(); |
| 288 | |
| 289 | // Process any deferred code using the register allocator. |
| 290 | if (HasStackOverflow()) { |
| 291 | ClearDeferred(); |
| 292 | } else { |
| 293 | ProcessDeferred(); |
| 294 | } |
| 295 | |
| 296 | allocator_ = NULL; |
| 297 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 301 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 302 | // Currently, this assertion will fail if we try to assign to |
| 303 | // a constant variable that is constant because it is read-only |
| 304 | // (such as the variable referring to a named function expression). |
| 305 | // We need to implement assignments to read-only variables. |
| 306 | // Ideally, we should do this during AST generation (by converting |
| 307 | // such assignments into expression statements); however, in general |
| 308 | // we may not be able to make the decision until past AST generation, |
| 309 | // that is when the entire program is known. |
| 310 | ASSERT(slot != NULL); |
| 311 | int index = slot->index(); |
| 312 | switch (slot->type()) { |
| 313 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 315 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 316 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 317 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 318 | |
| 319 | case Slot::CONTEXT: { |
| 320 | // Follow the context chain if necessary. |
| 321 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 322 | Register context = cp; |
| 323 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 324 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 325 | // Load the closure. |
| 326 | // (All contexts, even 'with' contexts, have a closure, |
| 327 | // and it is the same for all contexts inside a function. |
| 328 | // There is no need to go to the function context first.) |
| 329 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 330 | // Load the function context (which is the incoming, outer context). |
| 331 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 332 | context = tmp; |
| 333 | } |
| 334 | // We may have a 'with' context now. Get the function context. |
| 335 | // (In fact this mov may never be the needed, since the scope analysis |
| 336 | // may not permit a direct context access in this case and thus we are |
| 337 | // always at a function context. However it is safe to dereference be- |
| 338 | // cause the function context of a function context is itself. Before |
| 339 | // deleting this mov we should try to create a counter-example first, |
| 340 | // though...) |
| 341 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 342 | return ContextOperand(tmp, index); |
| 343 | } |
| 344 | |
| 345 | default: |
| 346 | UNREACHABLE(); |
| 347 | return MemOperand(r0, 0); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 352 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 353 | Slot* slot, |
| 354 | Register tmp, |
| 355 | Register tmp2, |
| 356 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 357 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 358 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 359 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 360 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 361 | if (s->num_heap_slots() > 0) { |
| 362 | if (s->calls_eval()) { |
| 363 | // Check that extension is NULL. |
| 364 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 365 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 366 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 367 | } |
| 368 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 369 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 370 | context = tmp; |
| 371 | } |
| 372 | } |
| 373 | // Check that last extension is NULL. |
| 374 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 375 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 376 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 377 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 378 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 382 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 383 | TypeofState typeof_state, |
| 384 | JumpTarget* true_target, |
| 385 | JumpTarget* false_target, |
| 386 | bool force_control) { |
| 387 | ASSERT(in_spilled_code()); |
| 388 | set_in_spilled_code(false); |
| 389 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 390 | force_control); |
| 391 | if (frame_ != NULL) { |
| 392 | frame_->SpillAll(); |
| 393 | } |
| 394 | set_in_spilled_code(true); |
| 395 | } |
| 396 | |
| 397 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 398 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 399 | // (partially) translated into branches, or it may have set the condition |
| 400 | // code register. If force_cc is set, the value is forced to set the |
| 401 | // condition code register and no value is pushed. If the condition code |
| 402 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 403 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 404 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 405 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 406 | JumpTarget* true_target, |
| 407 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 408 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 410 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 411 | int original_height = frame_->height(); |
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); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | |
| 416 | // If we hit a stack overflow, we may not have actually visited |
| 417 | // the expression. In that case, we ensure that we have a |
| 418 | // valid-looking frame state because we will continue to generate |
| 419 | // code as we unwind the C++ stack. |
| 420 | // |
| 421 | // It's possible to have both a stack overflow and a valid frame |
| 422 | // state (eg, a subexpression overflowed, visiting it returned |
| 423 | // with a dummied frame state, and visiting this expression |
| 424 | // returned with a normal-looking state). |
| 425 | if (HasStackOverflow() && |
| 426 | has_valid_frame() && |
| 427 | !has_cc() && |
| 428 | frame_->height() == original_height) { |
| 429 | true_target->Jump(); |
| 430 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 431 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 433 | // Convert the TOS value to a boolean in the condition code register. |
| 434 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 436 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 437 | ASSERT(!has_valid_frame() || |
| 438 | (has_cc() && frame_->height() == original_height) || |
| 439 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 443 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 444 | TypeofState typeof_state) { |
| 445 | ASSERT(in_spilled_code()); |
| 446 | set_in_spilled_code(false); |
| 447 | Load(expression, typeof_state); |
| 448 | frame_->SpillAll(); |
| 449 | set_in_spilled_code(true); |
| 450 | } |
| 451 | |
| 452 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 453 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | #ifdef DEBUG |
| 455 | int original_height = frame_->height(); |
| 456 | #endif |
| 457 | ASSERT(!in_spilled_code()); |
| 458 | JumpTarget true_target(this); |
| 459 | JumpTarget false_target(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 460 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | |
| 462 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | // Convert cc_reg_ into a boolean value. |
| 464 | JumpTarget loaded(this); |
| 465 | JumpTarget materialize_true(this); |
| 466 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 467 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 468 | frame_->EmitPush(r0); |
| 469 | loaded.Jump(); |
| 470 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 471 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 472 | frame_->EmitPush(r0); |
| 473 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | cc_reg_ = al; |
| 475 | } |
| 476 | |
| 477 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 478 | // We have at least one condition value that has been "translated" |
| 479 | // into a branch, thus it needs to be loaded explicitly. |
| 480 | JumpTarget loaded(this); |
| 481 | if (frame_ != NULL) { |
| 482 | loaded.Jump(); // Don't lose the current TOS. |
| 483 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 485 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 487 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 488 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 489 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | // If both "true" and "false" need to be loaded jump across the code for |
| 492 | // "false". |
| 493 | if (both) { |
| 494 | loaded.Jump(); |
| 495 | } |
| 496 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 498 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 499 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 500 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 502 | // A value is loaded on all paths reaching this point. |
| 503 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 507 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 511 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 512 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 513 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 518 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 519 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 520 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 521 | __ ldr(scratch, |
| 522 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | // 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] | 528 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 529 | // variables w/o reference errors elsewhere. |
| 530 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 531 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 533 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 534 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 535 | // the variable as if through '<global>.<variable>' to make sure we |
| 536 | // do not get reference errors. |
| 537 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 538 | Literal key(variable->name()); |
| 539 | // TODO(1241834): Fetch the position from the variable instead of using |
| 540 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 541 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 549 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 551 | cgen->LoadReference(this); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | Reference::~Reference() { |
| 556 | cgen_->UnloadReference(this); |
| 557 | } |
| 558 | |
| 559 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 560 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 561 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 562 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | Expression* e = ref->expression(); |
| 564 | Property* property = e->AsProperty(); |
| 565 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 566 | |
| 567 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 568 | // The expression is either a property or a variable proxy that rewrites |
| 569 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 571 | // We use a named reference if the key is a literal symbol, unless it is |
| 572 | // a string that can be legally parsed as an integer. This is because |
| 573 | // otherwise we will not get into the slow case code that handles [] on |
| 574 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | Literal* literal = property->key()->AsLiteral(); |
| 576 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | if (literal != NULL && |
| 578 | literal->handle()->IsSymbol() && |
| 579 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | ref->set_type(Reference::NAMED); |
| 581 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 582 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | ref->set_type(Reference::KEYED); |
| 584 | } |
| 585 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 586 | // The expression is a variable proxy that does not rewrite to a |
| 587 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | LoadGlobal(); |
| 590 | ref->set_type(Reference::NAMED); |
| 591 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 592 | ASSERT(var->slot() != NULL); |
| 593 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 594 | } |
| 595 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 596 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 597 | LoadAndSpill(e); |
| 598 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 603 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 604 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 605 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 606 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 608 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 609 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 610 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 611 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 617 | // register to a boolean in the condition code register. The code |
| 618 | // may jump to 'false_target' in case the register converts to 'false'. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 619 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 620 | JumpTarget* false_target) { |
| 621 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 622 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 624 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | |
| 626 | // Fast case checks |
| 627 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 628 | // Check if the value is 'false'. |
| 629 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 630 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 632 | // Check if the value is 'true'. |
| 633 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 634 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 636 | // Check if the value is 'undefined'. |
| 637 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 638 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 640 | // Check if the value is a smi. |
| 641 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 642 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 643 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 644 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | |
| 646 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 647 | frame_->EmitPush(r0); |
| 648 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 649 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | |
| 652 | cc_reg_ = ne; |
| 653 | } |
| 654 | |
| 655 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | class GetPropertyStub : public CodeStub { |
| 657 | public: |
| 658 | GetPropertyStub() { } |
| 659 | |
| 660 | private: |
| 661 | Major MajorKey() { return GetProperty; } |
| 662 | int MinorKey() { return 0; } |
| 663 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | class SetPropertyStub : public CodeStub { |
| 668 | public: |
| 669 | SetPropertyStub() { } |
| 670 | |
| 671 | private: |
| 672 | Major MajorKey() { return SetProperty; } |
| 673 | int MinorKey() { return 0; } |
| 674 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 678 | class GenericBinaryOpStub : public CodeStub { |
| 679 | public: |
| 680 | explicit GenericBinaryOpStub(Token::Value op) : op_(op) { } |
| 681 | |
| 682 | private: |
| 683 | Token::Value op_; |
| 684 | |
| 685 | Major MajorKey() { return GenericBinaryOp; } |
| 686 | int MinorKey() { return static_cast<int>(op_); } |
| 687 | void Generate(MacroAssembler* masm); |
| 688 | |
| 689 | const char* GetName() { |
| 690 | switch (op_) { |
| 691 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 692 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 693 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 694 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 695 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 696 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 697 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 698 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 699 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 700 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 701 | default: return "GenericBinaryOpStub"; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | #ifdef DEBUG |
| 706 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 707 | #endif |
| 708 | }; |
| 709 | |
| 710 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | class InvokeBuiltinStub : public CodeStub { |
| 712 | public: |
| 713 | enum Kind { Inc, Dec, ToNumber }; |
| 714 | InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { } |
| 715 | |
| 716 | private: |
| 717 | Kind kind_; |
| 718 | int argc_; |
| 719 | |
| 720 | Major MajorKey() { return InvokeBuiltin; } |
| 721 | int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); } |
| 722 | void Generate(MacroAssembler* masm); |
| 723 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | #ifdef DEBUG |
| 725 | void Print() { |
| 726 | PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n", |
| 727 | static_cast<int>(kind_), |
| 728 | argc_); |
| 729 | } |
| 730 | #endif |
| 731 | }; |
| 732 | |
| 733 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 734 | void CodeGenerator::GenericBinaryOperation(Token::Value op) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 735 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 736 | // sp[0] : y |
| 737 | // sp[1] : x |
| 738 | // result : r0 |
| 739 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 740 | // Stub is entered with a call: 'return address' is in lr. |
| 741 | switch (op) { |
| 742 | case Token::ADD: // fall through. |
| 743 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 744 | case Token::MUL: |
| 745 | case Token::BIT_OR: |
| 746 | case Token::BIT_AND: |
| 747 | case Token::BIT_XOR: |
| 748 | case Token::SHL: |
| 749 | case Token::SHR: |
| 750 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 751 | frame_->EmitPop(r0); // r0 : y |
| 752 | frame_->EmitPop(r1); // r1 : x |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 753 | GenericBinaryOpStub stub(op); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 754 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | |
| 758 | case Token::DIV: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 759 | Result arg_count = allocator_->Allocate(r0); |
| 760 | ASSERT(arg_count.is_valid()); |
| 761 | __ mov(arg_count.reg(), Operand(1)); |
| 762 | frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | |
| 766 | case Token::MOD: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 767 | Result arg_count = allocator_->Allocate(r0); |
| 768 | ASSERT(arg_count.is_valid()); |
| 769 | __ mov(arg_count.reg(), Operand(1)); |
| 770 | frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 775 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 777 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 778 | break; |
| 779 | |
| 780 | default: |
| 781 | // Other cases should have been handled before this point. |
| 782 | UNREACHABLE(); |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 788 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 789 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 790 | DeferredInlineSmiOperation(CodeGenerator* generator, |
| 791 | Token::Value op, |
| 792 | int value, |
| 793 | bool reversed) |
| 794 | : DeferredCode(generator), |
| 795 | op_(op), |
| 796 | value_(value), |
| 797 | reversed_(reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 798 | set_comment("[ DeferredInlinedSmiOperation"); |
| 799 | } |
| 800 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 801 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 802 | |
| 803 | private: |
| 804 | Token::Value op_; |
| 805 | int value_; |
| 806 | bool reversed_; |
| 807 | }; |
| 808 | |
| 809 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 810 | void DeferredInlineSmiOperation::Generate() { |
| 811 | enter()->Bind(); |
| 812 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 813 | |
| 814 | switch (op_) { |
| 815 | case Token::ADD: { |
| 816 | if (reversed_) { |
| 817 | // revert optimistic add |
| 818 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 819 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 820 | } else { |
| 821 | // revert optimistic add |
| 822 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 823 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 824 | } |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | case Token::SUB: { |
| 829 | if (reversed_) { |
| 830 | // revert optimistic sub |
| 831 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 832 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 833 | } else { |
| 834 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 835 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 836 | } |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | case Token::BIT_OR: |
| 841 | case Token::BIT_XOR: |
| 842 | case Token::BIT_AND: { |
| 843 | if (reversed_) { |
| 844 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 845 | } else { |
| 846 | __ mov(r1, Operand(r0)); |
| 847 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 848 | } |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | case Token::SHL: |
| 853 | case Token::SHR: |
| 854 | case Token::SAR: { |
| 855 | if (!reversed_) { |
| 856 | __ mov(r1, Operand(r0)); |
| 857 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 858 | } else { |
| 859 | UNREACHABLE(); // should have been handled in SmiOperation |
| 860 | } |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | default: |
| 865 | // other cases should have been handled before this point. |
| 866 | UNREACHABLE(); |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | GenericBinaryOpStub igostub(op_); |
| 871 | Result arg0 = generator()->allocator()->Allocate(r0); |
| 872 | ASSERT(arg0.is_valid()); |
| 873 | Result arg1 = generator()->allocator()->Allocate(r1); |
| 874 | ASSERT(arg1.is_valid()); |
| 875 | generator()->frame()->CallStub(&igostub, &arg0, &arg1, 0); |
| 876 | exit_.Jump(); |
| 877 | } |
| 878 | |
| 879 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 880 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 881 | Handle<Object> value, |
| 882 | bool reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 883 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 885 | // some possible smi operations (like + and -) when (at least) one |
| 886 | // of the operands is a literal smi. With this optimization, the |
| 887 | // performance of the system is increased by ~15%, and the generated |
| 888 | // code size is increased by ~1% (measured on a combination of |
| 889 | // different benchmarks). |
| 890 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 891 | // sp[0] : operand |
| 892 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 893 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 894 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 895 | JumpTarget exit(this); |
| 896 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | |
| 898 | switch (op) { |
| 899 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 900 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 901 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | __ add(r0, r0, Operand(value), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 904 | deferred->enter()->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 906 | deferred->enter()->Branch(ne); |
| 907 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 908 | break; |
| 909 | } |
| 910 | |
| 911 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 912 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 913 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | if (!reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 916 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 917 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 918 | __ rsb(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 919 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 920 | deferred->enter()->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 921 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 922 | deferred->enter()->Branch(ne); |
| 923 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 924 | break; |
| 925 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 927 | case Token::BIT_OR: |
| 928 | case Token::BIT_XOR: |
| 929 | case Token::BIT_AND: { |
| 930 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 931 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 932 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 933 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 934 | switch (op) { |
| 935 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 936 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 937 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 938 | default: UNREACHABLE(); |
| 939 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 940 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 941 | break; |
| 942 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 943 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 944 | case Token::SHL: |
| 945 | case Token::SHR: |
| 946 | case Token::SAR: { |
| 947 | if (reversed) { |
| 948 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 949 | frame_->EmitPush(ip); |
| 950 | frame_->EmitPush(r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 951 | GenericBinaryOperation(op); |
| 952 | |
| 953 | } else { |
| 954 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 955 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 956 | new DeferredInlineSmiOperation(this, op, shift_value, false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 957 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 958 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 959 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 960 | switch (op) { |
| 961 | case Token::SHL: { |
| 962 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 963 | // check that the *unsigned* result fits in a smi |
| 964 | __ add(r3, r2, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 965 | deferred->enter()->Branch(mi); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 966 | break; |
| 967 | } |
| 968 | case Token::SHR: { |
| 969 | // LSR by immediate 0 means shifting 32 bits. |
| 970 | if (shift_value != 0) { |
| 971 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 972 | } |
| 973 | // check that the *unsigned* result fits in a smi |
| 974 | // neither of the two high-order bits can be set: |
| 975 | // - 0x80000000: high bit would be lost when smi tagging |
| 976 | // - 0x40000000: this number would convert to negative when |
| 977 | // smi tagging these two cases can only happen with shifts |
| 978 | // by 0 or 1 when handed a valid smi |
| 979 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 980 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 981 | break; |
| 982 | } |
| 983 | case Token::SAR: { |
| 984 | if (shift_value != 0) { |
| 985 | // ASR by immediate 0 means shifting 32 bits. |
| 986 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 987 | } |
| 988 | break; |
| 989 | } |
| 990 | default: UNREACHABLE(); |
| 991 | } |
| 992 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 993 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 994 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 995 | break; |
| 996 | } |
| 997 | |
| 998 | default: |
| 999 | if (!reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1000 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1001 | __ mov(r0, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1002 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1003 | } else { |
| 1004 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1005 | frame_->EmitPush(ip); |
| 1006 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1007 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1008 | GenericBinaryOperation(op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1012 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1016 | void CodeGenerator::Comparison(Condition cc, bool strict) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1017 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1018 | // sp[0] : y |
| 1019 | // sp[1] : x |
| 1020 | // result : cc register |
| 1021 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1022 | // Strict only makes sense for equality comparisons. |
| 1023 | ASSERT(!strict || cc == eq); |
| 1024 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1025 | JumpTarget exit(this); |
| 1026 | JumpTarget smi(this); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1027 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1028 | if (cc == gt || cc == le) { |
| 1029 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1030 | frame_->EmitPop(r1); |
| 1031 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1032 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1033 | frame_->EmitPop(r0); |
| 1034 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1035 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | __ orr(r2, r0, Operand(r1)); |
| 1037 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1038 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Perform non-smi comparison by runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1041 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Figure out which native to call and setup the arguments. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1044 | Builtins::JavaScript native; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1045 | int arg_count = 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | if (cc == eq) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1047 | native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1048 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1049 | native = Builtins::COMPARE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | int ncr; // NaN compare result |
| 1051 | if (cc == lt || cc == le) { |
| 1052 | ncr = GREATER; |
| 1053 | } else { |
| 1054 | ASSERT(cc == gt || cc == ge); // remaining cases |
| 1055 | ncr = LESS; |
| 1056 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1057 | frame_->EmitPush(r0); |
| 1058 | arg_count++; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1059 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 1063 | // tagged as a small integer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1064 | frame_->EmitPush(r0); |
| 1065 | Result arg_count_register = allocator_->Allocate(r0); |
| 1066 | ASSERT(arg_count_register.is_valid()); |
| 1067 | __ mov(arg_count_register.reg(), Operand(arg_count)); |
| 1068 | Result result = frame_->InvokeBuiltin(native, |
| 1069 | CALL_JS, |
| 1070 | &arg_count_register, |
| 1071 | arg_count + 1); |
| 1072 | __ cmp(result.reg(), Operand(0)); |
| 1073 | result.Unuse(); |
| 1074 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1075 | |
| 1076 | // test smi equality by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1077 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1078 | __ cmp(r1, Operand(r0)); |
| 1079 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1080 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1081 | cc_reg_ = cc; |
| 1082 | } |
| 1083 | |
| 1084 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1085 | class CallFunctionStub: public CodeStub { |
| 1086 | public: |
| 1087 | explicit CallFunctionStub(int argc) : argc_(argc) {} |
| 1088 | |
| 1089 | void Generate(MacroAssembler* masm); |
| 1090 | |
| 1091 | private: |
| 1092 | int argc_; |
| 1093 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1094 | #if defined(DEBUG) |
| 1095 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1096 | #endif // defined(DEBUG) |
| 1097 | |
| 1098 | Major MajorKey() { return CallFunction; } |
| 1099 | int MinorKey() { return argc_; } |
| 1100 | }; |
| 1101 | |
| 1102 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1103 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1104 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1105 | int position) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1106 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1108 | int arg_count = args->length(); |
| 1109 | for (int i = 0; i < arg_count; i++) { |
| 1110 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1111 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1113 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1114 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1116 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1117 | CallFunctionStub call_function(arg_count); |
| 1118 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1121 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1122 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1126 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
| 1127 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1128 | ASSERT(has_cc()); |
| 1129 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1130 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | cc_reg_ = al; |
| 1132 | } |
| 1133 | |
| 1134 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1135 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1136 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | if (FLAG_check_stack) { |
| 1138 | Comment cmnt(masm_, "[ check stack"); |
| 1139 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1140 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 1145 | void CodeGenerator::VisitAndSpill(Statement* statement) { |
| 1146 | ASSERT(in_spilled_code()); |
| 1147 | set_in_spilled_code(false); |
| 1148 | Visit(statement); |
| 1149 | if (frame_ != NULL) { |
| 1150 | frame_->SpillAll(); |
| 1151 | } |
| 1152 | set_in_spilled_code(true); |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) { |
| 1157 | ASSERT(in_spilled_code()); |
| 1158 | set_in_spilled_code(false); |
| 1159 | VisitStatements(statements); |
| 1160 | if (frame_ != NULL) { |
| 1161 | frame_->SpillAll(); |
| 1162 | } |
| 1163 | set_in_spilled_code(true); |
| 1164 | } |
| 1165 | |
| 1166 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1167 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1168 | #ifdef DEBUG |
| 1169 | int original_height = frame_->height(); |
| 1170 | #endif |
| 1171 | VirtualFrame::SpilledScope spilled_scope(this); |
| 1172 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1173 | VisitAndSpill(statements->at(i)); |
| 1174 | } |
| 1175 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1176 | } |
| 1177 | |
| 1178 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1179 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1180 | #ifdef DEBUG |
| 1181 | int original_height = frame_->height(); |
| 1182 | #endif |
| 1183 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1184 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1185 | CodeForStatementPosition(node); |
| 1186 | node->break_target()->Initialize(this); |
| 1187 | VisitStatementsAndSpill(node->statements()); |
| 1188 | if (node->break_target()->is_linked()) { |
| 1189 | node->break_target()->Bind(); |
| 1190 | } |
| 1191 | node->break_target()->Unuse(); |
| 1192 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1196 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1197 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1198 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1199 | frame_->EmitPush(r0); |
| 1200 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1201 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1202 | frame_->EmitPush(r0); |
| 1203 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1204 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1208 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1209 | #ifdef DEBUG |
| 1210 | int original_height = frame_->height(); |
| 1211 | #endif |
| 1212 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1214 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1215 | Variable* var = node->proxy()->var(); |
| 1216 | ASSERT(var != NULL); // must have been resolved |
| 1217 | Slot* slot = var->slot(); |
| 1218 | |
| 1219 | // If it was not possible to allocate the variable at compile time, |
| 1220 | // we need to "declare" it at runtime to make sure it actually |
| 1221 | // exists in the local context. |
| 1222 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1223 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1224 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1225 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1227 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1228 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1229 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | // Declaration nodes are always declared in only two modes. |
| 1231 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1232 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1233 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1234 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | // Push initial value, if any. |
| 1236 | // Note: For variables we must not push an initial value (such as |
| 1237 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1238 | // must not destroy the current value. |
| 1239 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1240 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1241 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1242 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1243 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1245 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1246 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1248 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1249 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1250 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | ASSERT(!var->is_global()); |
| 1255 | |
| 1256 | // If we have a function or a constant, we need to initialize the variable. |
| 1257 | Expression* val = NULL; |
| 1258 | if (node->mode() == Variable::CONST) { |
| 1259 | val = new Literal(Factory::the_hole_value()); |
| 1260 | } else { |
| 1261 | val = node->fun(); // NULL if we don't have a function |
| 1262 | } |
| 1263 | |
| 1264 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1265 | { |
| 1266 | // Set initial value. |
| 1267 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1268 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1269 | target.SetValue(NOT_CONST_INIT); |
| 1270 | // The reference is removed from the stack (preserving TOS) when |
| 1271 | // it goes out of scope. |
| 1272 | } |
| 1273 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1274 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1275 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1276 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1280 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1281 | #ifdef DEBUG |
| 1282 | int original_height = frame_->height(); |
| 1283 | #endif |
| 1284 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1286 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | Expression* expression = node->expression(); |
| 1288 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1289 | LoadAndSpill(expression); |
| 1290 | frame_->Drop(); |
| 1291 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1295 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1296 | #ifdef DEBUG |
| 1297 | int original_height = frame_->height(); |
| 1298 | #endif |
| 1299 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1300 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1301 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1302 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1303 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1307 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1308 | #ifdef DEBUG |
| 1309 | int original_height = frame_->height(); |
| 1310 | #endif |
| 1311 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1313 | // Generate different code depending on which parts of the if statement |
| 1314 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1315 | bool has_then_stm = node->HasThenStatement(); |
| 1316 | bool has_else_stm = node->HasElseStatement(); |
| 1317 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1318 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1320 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1321 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1322 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1323 | JumpTarget then(this); |
| 1324 | JumpTarget else_(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1325 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1326 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1327 | &then, &else_, true); |
| 1328 | if (frame_ != NULL) { |
| 1329 | Branch(false, &else_); |
| 1330 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1332 | if (frame_ != NULL || then.is_linked()) { |
| 1333 | then.Bind(); |
| 1334 | VisitAndSpill(node->then_statement()); |
| 1335 | } |
| 1336 | if (frame_ != NULL) { |
| 1337 | exit.Jump(); |
| 1338 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1340 | if (else_.is_linked()) { |
| 1341 | else_.Bind(); |
| 1342 | VisitAndSpill(node->else_statement()); |
| 1343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | |
| 1345 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1346 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1347 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1348 | JumpTarget then(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1350 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1351 | &then, &exit, true); |
| 1352 | if (frame_ != NULL) { |
| 1353 | Branch(false, &exit); |
| 1354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1355 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1356 | if (frame_ != NULL || then.is_linked()) { |
| 1357 | then.Bind(); |
| 1358 | VisitAndSpill(node->then_statement()); |
| 1359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1360 | |
| 1361 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1362 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1364 | JumpTarget else_(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1365 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1366 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1367 | &exit, &else_, true); |
| 1368 | if (frame_ != NULL) { |
| 1369 | Branch(true, &exit); |
| 1370 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1371 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1372 | if (frame_ != NULL || else_.is_linked()) { |
| 1373 | else_.Bind(); |
| 1374 | VisitAndSpill(node->else_statement()); |
| 1375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | |
| 1377 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1378 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1379 | ASSERT(!has_then_stm && !has_else_stm); |
| 1380 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1381 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1382 | &exit, &exit, false); |
| 1383 | if (frame_ != NULL) { |
| 1384 | if (has_cc()) { |
| 1385 | cc_reg_ = al; |
| 1386 | } else { |
| 1387 | frame_->Drop(); |
| 1388 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1393 | if (exit.is_linked()) { |
| 1394 | exit.Bind(); |
| 1395 | } |
| 1396 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1400 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1401 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1402 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1403 | CodeForStatementPosition(node); |
| 1404 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1408 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1409 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1410 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1411 | CodeForStatementPosition(node); |
| 1412 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1416 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1417 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1418 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1419 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1420 | if (function_return_is_shadowed_) { |
| 1421 | CodeForStatementPosition(node); |
| 1422 | LoadAndSpill(node->expression()); |
| 1423 | frame_->EmitPop(r0); |
| 1424 | function_return_.Jump(); |
| 1425 | } else { |
| 1426 | // Load the returned value. |
| 1427 | CodeForStatementPosition(node); |
| 1428 | LoadAndSpill(node->expression()); |
| 1429 | |
| 1430 | // Pop the result from the frame and prepare the frame for |
| 1431 | // returning thus making it easier to merge. |
| 1432 | frame_->EmitPop(r0); |
| 1433 | frame_->PrepareForReturn(); |
| 1434 | |
| 1435 | function_return_.Jump(); |
| 1436 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1440 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1441 | #ifdef DEBUG |
| 1442 | int original_height = frame_->height(); |
| 1443 | #endif |
| 1444 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1445 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1446 | CodeForStatementPosition(node); |
| 1447 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1448 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1449 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1450 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1451 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1452 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1453 | if (kDebug) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1454 | JumpTarget verified_true(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1455 | __ cmp(r0, Operand(cp)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1456 | verified_true.Branch(eq); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1457 | __ stop("PushContext: r0 is expected to be the same as cp"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1458 | verified_true.Bind(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1459 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1460 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1461 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1462 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1466 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1467 | #ifdef DEBUG |
| 1468 | int original_height = frame_->height(); |
| 1469 | #endif |
| 1470 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1472 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | // Pop context. |
| 1474 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1475 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1476 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1477 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1481 | int CodeGenerator::FastCaseSwitchMaxOverheadFactor() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1482 | return kFastSwitchMaxOverheadFactor; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1485 | int CodeGenerator::FastCaseSwitchMinCaseCount() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1486 | return kFastSwitchMinCaseCount; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1490 | void CodeGenerator::GenerateFastCaseSwitchJumpTable( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1491 | SwitchStatement* node, |
| 1492 | int min_index, |
| 1493 | int range, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1494 | Label* default_label, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1495 | Vector<Label*> case_targets, |
| 1496 | Vector<Label> case_labels) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1497 | VirtualFrame::SpilledScope spilled_scope(this); |
| 1498 | JumpTarget setup_default(this); |
| 1499 | JumpTarget is_smi(this); |
| 1500 | |
| 1501 | // A non-null default label pointer indicates a default case among |
| 1502 | // the case labels. Otherwise we use the break target as a |
| 1503 | // "default" for failure to hit the jump table. |
| 1504 | JumpTarget* default_target = |
| 1505 | (default_label == NULL) ? node->break_target() : &setup_default; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1506 | |
| 1507 | ASSERT(kSmiTag == 0 && kSmiTagSize <= 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1508 | frame_->EmitPop(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Test for a Smi value in a HeapNumber. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1511 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1512 | is_smi.Branch(eq); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1513 | __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag)); |
| 1514 | __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag)); |
| 1515 | __ cmp(r1, Operand(HEAP_NUMBER_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1516 | default_target->Branch(ne); |
| 1517 | frame_->EmitPush(r0); |
| 1518 | frame_->CallRuntime(Runtime::kNumberToSmi, 1); |
| 1519 | is_smi.Bind(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1520 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1521 | if (min_index != 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1522 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1523 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1524 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1525 | if (Smi::IsValid(-min_index)) { |
| 1526 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1527 | } else { |
| 1528 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1529 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1530 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1531 | } else { |
| 1532 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1533 | } |
| 1534 | } |
| 1535 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1536 | default_target->Branch(ne); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1537 | __ cmp(r0, Operand(Smi::FromInt(range))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1538 | default_target->Branch(ge); |
| 1539 | VirtualFrame* start_frame = new VirtualFrame(frame_); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1540 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1541 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1542 | GenerateFastCaseSwitchCases(node, case_labels, start_frame); |
| 1543 | |
| 1544 | // If there was a default case among the case labels, we need to |
| 1545 | // emit code to jump to it from the default target used for failure |
| 1546 | // to hit the jump table. |
| 1547 | if (default_label != NULL) { |
| 1548 | if (has_valid_frame()) { |
| 1549 | node->break_target()->Jump(); |
| 1550 | } |
| 1551 | setup_default.Bind(); |
| 1552 | frame_->MergeTo(start_frame); |
| 1553 | __ b(default_label); |
| 1554 | DeleteFrame(); |
| 1555 | } |
| 1556 | if (node->break_target()->is_linked()) { |
| 1557 | node->break_target()->Bind(); |
| 1558 | } |
| 1559 | |
| 1560 | delete start_frame; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1564 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1565 | #ifdef DEBUG |
| 1566 | int original_height = frame_->height(); |
| 1567 | #endif |
| 1568 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1569 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1570 | CodeForStatementPosition(node); |
| 1571 | node->break_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1572 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1573 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1574 | if (TryGenerateFastCaseSwitchStatement(node)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1575 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1576 | return; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1579 | JumpTarget next_test(this); |
| 1580 | JumpTarget fall_through(this); |
| 1581 | JumpTarget default_entry(this); |
| 1582 | JumpTarget default_exit(this, JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1583 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1584 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1585 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1586 | |
| 1587 | for (int i = 0; i < length; i++) { |
| 1588 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1589 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1590 | // Remember the default clause and compile it at the end. |
| 1591 | default_clause = clause; |
| 1592 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1595 | Comment cmnt(masm_, "[ Case clause"); |
| 1596 | // Compile the test. |
| 1597 | next_test.Bind(); |
| 1598 | next_test.Unuse(); |
| 1599 | // Duplicate TOS. |
| 1600 | __ ldr(r0, frame_->Top()); |
| 1601 | frame_->EmitPush(r0); |
| 1602 | LoadAndSpill(clause->label()); |
| 1603 | Comparison(eq, true); |
| 1604 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1605 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1606 | // Before entering the body from the test, remove the switch value from |
| 1607 | // the stack. |
| 1608 | frame_->Drop(); |
| 1609 | |
| 1610 | // Label the body so that fall through is enabled. |
| 1611 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1612 | default_exit.Bind(); |
| 1613 | } else { |
| 1614 | fall_through.Bind(); |
| 1615 | fall_through.Unuse(); |
| 1616 | } |
| 1617 | VisitStatementsAndSpill(clause->statements()); |
| 1618 | |
| 1619 | // If control flow can fall through from the body, jump to the next body |
| 1620 | // or the end of the statement. |
| 1621 | if (frame_ != NULL) { |
| 1622 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1623 | default_entry.Jump(); |
| 1624 | } else { |
| 1625 | fall_through.Jump(); |
| 1626 | } |
| 1627 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1630 | // The final "test" removes the switch value. |
| 1631 | next_test.Bind(); |
| 1632 | frame_->Drop(); |
| 1633 | |
| 1634 | // If there is a default clause, compile it. |
| 1635 | if (default_clause != NULL) { |
| 1636 | Comment cmnt(masm_, "[ Default clause"); |
| 1637 | default_entry.Bind(); |
| 1638 | VisitStatementsAndSpill(default_clause->statements()); |
| 1639 | // If control flow can fall out of the default and there is a case after |
| 1640 | // it, jup to that case's body. |
| 1641 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1642 | default_exit.Jump(); |
| 1643 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1644 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1645 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1646 | if (fall_through.is_linked()) { |
| 1647 | fall_through.Bind(); |
| 1648 | } |
| 1649 | |
| 1650 | if (node->break_target()->is_linked()) { |
| 1651 | node->break_target()->Bind(); |
| 1652 | } |
| 1653 | node->break_target()->Unuse(); |
| 1654 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1658 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1659 | #ifdef DEBUG |
| 1660 | int original_height = frame_->height(); |
| 1661 | #endif |
| 1662 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1663 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1664 | CodeForStatementPosition(node); |
| 1665 | node->break_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1666 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1667 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1668 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1669 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1670 | if (node->cond() == NULL) { |
| 1671 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1672 | info = ALWAYS_TRUE; |
| 1673 | } else { |
| 1674 | Literal* lit = node->cond()->AsLiteral(); |
| 1675 | if (lit != NULL) { |
| 1676 | if (lit->IsTrue()) { |
| 1677 | info = ALWAYS_TRUE; |
| 1678 | } else if (lit->IsFalse()) { |
| 1679 | info = ALWAYS_FALSE; |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1684 | switch (node->type()) { |
| 1685 | case LoopStatement::DO_LOOP: { |
| 1686 | JumpTarget body(this, JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1687 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1688 | // Label the top of the loop for the backward CFG edge. If the test |
| 1689 | // is always true we can use the continue target, and if the test is |
| 1690 | // always false there is no need. |
| 1691 | if (info == ALWAYS_TRUE) { |
| 1692 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1693 | node->continue_target()->Bind(); |
| 1694 | } else if (info == ALWAYS_FALSE) { |
| 1695 | node->continue_target()->Initialize(this); |
| 1696 | } else { |
| 1697 | ASSERT(info == DONT_KNOW); |
| 1698 | node->continue_target()->Initialize(this); |
| 1699 | body.Bind(); |
| 1700 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1701 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1702 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1703 | VisitAndSpill(node->body()); |
| 1704 | |
| 1705 | // Compile the test. |
| 1706 | if (info == ALWAYS_TRUE) { |
| 1707 | if (has_valid_frame()) { |
| 1708 | // If control can fall off the end of the body, jump back to the |
| 1709 | // top. |
| 1710 | node->continue_target()->Jump(); |
| 1711 | } |
| 1712 | } else if (info == ALWAYS_FALSE) { |
| 1713 | // If we have a continue in the body, we only have to bind its jump |
| 1714 | // target. |
| 1715 | if (node->continue_target()->is_linked()) { |
| 1716 | node->continue_target()->Bind(); |
| 1717 | } |
| 1718 | } else { |
| 1719 | ASSERT(info == DONT_KNOW); |
| 1720 | // We have to compile the test expression if it can be reached by |
| 1721 | // control flow falling out of the body or via continue. |
| 1722 | if (node->continue_target()->is_linked()) { |
| 1723 | node->continue_target()->Bind(); |
| 1724 | } |
| 1725 | if (has_valid_frame()) { |
| 1726 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1727 | &body, node->break_target(), true); |
| 1728 | if (has_valid_frame()) { |
| 1729 | // A invalid frame here indicates that control did not |
| 1730 | // fall out of the test expression. |
| 1731 | Branch(true, &body); |
| 1732 | } |
| 1733 | } |
| 1734 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1735 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | case LoopStatement::WHILE_LOOP: { |
| 1739 | // If the test is never true and has no side effects there is no need |
| 1740 | // to compile the test or body. |
| 1741 | if (info == ALWAYS_FALSE) break; |
| 1742 | |
| 1743 | // Label the top of the loop with the continue target for the backward |
| 1744 | // CFG edge. |
| 1745 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1746 | node->continue_target()->Bind(); |
| 1747 | |
| 1748 | if (info == DONT_KNOW) { |
| 1749 | JumpTarget body(this); |
| 1750 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1751 | &body, node->break_target(), true); |
| 1752 | if (has_valid_frame()) { |
| 1753 | // A NULL frame indicates that control did not fall out of the |
| 1754 | // test expression. |
| 1755 | Branch(false, node->break_target()); |
| 1756 | } |
| 1757 | if (has_valid_frame() || body.is_linked()) { |
| 1758 | body.Bind(); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | if (has_valid_frame()) { |
| 1763 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1764 | VisitAndSpill(node->body()); |
| 1765 | |
| 1766 | // If control flow can fall out of the body, jump back to the top. |
| 1767 | if (has_valid_frame()) { |
| 1768 | node->continue_target()->Jump(); |
| 1769 | } |
| 1770 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1771 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | case LoopStatement::FOR_LOOP: { |
| 1775 | JumpTarget loop(this, JumpTarget::BIDIRECTIONAL); |
| 1776 | |
| 1777 | if (node->init() != NULL) { |
| 1778 | VisitAndSpill(node->init()); |
| 1779 | } |
| 1780 | |
| 1781 | // There is no need to compile the test or body. |
| 1782 | if (info == ALWAYS_FALSE) break; |
| 1783 | |
| 1784 | // If there is no update statement, label the top of the loop with the |
| 1785 | // continue target, otherwise with the loop target. |
| 1786 | if (node->next() == NULL) { |
| 1787 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1788 | node->continue_target()->Bind(); |
| 1789 | } else { |
| 1790 | node->continue_target()->Initialize(this); |
| 1791 | loop.Bind(); |
| 1792 | } |
| 1793 | |
| 1794 | // If the test is always true, there is no need to compile it. |
| 1795 | if (info == DONT_KNOW) { |
| 1796 | JumpTarget body(this); |
| 1797 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1798 | &body, node->break_target(), true); |
| 1799 | if (has_valid_frame()) { |
| 1800 | Branch(false, node->break_target()); |
| 1801 | } |
| 1802 | if (has_valid_frame() || body.is_linked()) { |
| 1803 | body.Bind(); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | if (has_valid_frame()) { |
| 1808 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1809 | VisitAndSpill(node->body()); |
| 1810 | |
| 1811 | if (node->next() == NULL) { |
| 1812 | // If there is no update statement and control flow can fall out |
| 1813 | // of the loop, jump directly to the continue label. |
| 1814 | if (has_valid_frame()) { |
| 1815 | node->continue_target()->Jump(); |
| 1816 | } |
| 1817 | } else { |
| 1818 | // If there is an update statement and control flow can reach it |
| 1819 | // via falling out of the body of the loop or continuing, we |
| 1820 | // compile the update statement. |
| 1821 | if (node->continue_target()->is_linked()) { |
| 1822 | node->continue_target()->Bind(); |
| 1823 | } |
| 1824 | if (has_valid_frame()) { |
| 1825 | // Record source position of the statement as this code which is |
| 1826 | // after the code for the body actually belongs to the loop |
| 1827 | // statement and not the body. |
| 1828 | CodeForStatementPosition(node); |
| 1829 | VisitAndSpill(node->next()); |
| 1830 | loop.Jump(); |
| 1831 | } |
| 1832 | } |
| 1833 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1835 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1838 | if (node->break_target()->is_linked()) { |
| 1839 | node->break_target()->Bind(); |
| 1840 | } |
| 1841 | node->continue_target()->Unuse(); |
| 1842 | node->break_target()->Unuse(); |
| 1843 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1847 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1848 | #ifdef DEBUG |
| 1849 | int original_height = frame_->height(); |
| 1850 | #endif |
| 1851 | ASSERT(!in_spilled_code()); |
| 1852 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1854 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1855 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1856 | JumpTarget primitive(this); |
| 1857 | JumpTarget jsobject(this); |
| 1858 | JumpTarget fixed_array(this); |
| 1859 | JumpTarget entry(this, JumpTarget::BIDIRECTIONAL); |
| 1860 | JumpTarget end_del_check(this); |
| 1861 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1862 | |
| 1863 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1864 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1865 | |
| 1866 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1867 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1868 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1870 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1872 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Stack layout in body: |
| 1875 | // [iteration counter (Smi)] |
| 1876 | // [length of array] |
| 1877 | // [FixedArray] |
| 1878 | // [Map or 0] |
| 1879 | // [Object] |
| 1880 | |
| 1881 | // Check if enumerable is already a JSObject |
| 1882 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1883 | primitive.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1885 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 1886 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1887 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1889 | primitive.Bind(); |
| 1890 | frame_->EmitPush(r0); |
| 1891 | Result arg_count = allocator_->Allocate(r0); |
| 1892 | ASSERT(arg_count.is_valid()); |
| 1893 | __ mov(arg_count.reg(), Operand(0)); |
| 1894 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1896 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1897 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1898 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1899 | frame_->EmitPush(r0); |
| 1900 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1901 | |
| 1902 | // If we got a Map, we can do a fast modification check. |
| 1903 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1904 | __ mov(r2, Operand(r0)); |
| 1905 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1906 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1907 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Get enum cache |
| 1910 | __ mov(r1, Operand(r0)); |
| 1911 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1912 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1913 | __ ldr(r2, |
| 1914 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1915 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1916 | frame_->EmitPush(r0); // map |
| 1917 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1918 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1920 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1921 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1922 | frame_->EmitPush(r0); |
| 1923 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1926 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1927 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1928 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | |
| 1930 | // 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] | 1931 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1932 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1933 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1934 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1935 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | |
| 1937 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1938 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1939 | // sp[0] : index |
| 1940 | // sp[1] : array/enum cache length |
| 1941 | // sp[2] : array or enum cache |
| 1942 | // sp[3] : 0 or map |
| 1943 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1944 | // Grab the current frame's height for the break and continue |
| 1945 | // targets only after all the state is pushed on the frame. |
| 1946 | node->break_target()->Initialize(this); |
| 1947 | node->continue_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1948 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1949 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1950 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1951 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1952 | node->break_target()->Branch(hs); |
| 1953 | |
| 1954 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1955 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1956 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1957 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1958 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1959 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1960 | |
| 1961 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1962 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1963 | // Check if this (still) matches the map of the enumerable. |
| 1964 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1965 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1966 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1967 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1968 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Convert the entry to a string (or null if it isn't a property anymore). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1971 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1972 | frame_->EmitPush(r0); |
| 1973 | frame_->EmitPush(r3); // push entry |
| 1974 | Result arg_count_register = allocator_->Allocate(r0); |
| 1975 | ASSERT(arg_count_register.is_valid()); |
| 1976 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1977 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1978 | CALL_JS, |
| 1979 | &arg_count_register, |
| 1980 | 2); |
| 1981 | __ mov(r3, Operand(result.reg())); |
| 1982 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | |
| 1984 | // If the property has been removed while iterating, we just skip it. |
| 1985 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1986 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1987 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1988 | end_del_check.Bind(); |
| 1989 | // Store the entry in the 'each' expression and take another spin in the |
| 1990 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1991 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1992 | { Reference each(this, node->each()); |
| 1993 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1994 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1996 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1997 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1998 | // If the reference was to a slot we rely on the convenient property |
| 1999 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 2000 | // right on top of or right underneath a zero-sized reference. |
| 2001 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2002 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2003 | // It's safe to pop the value lying on top of the reference before |
| 2004 | // unloading the reference itself (which preserves the top of stack, |
| 2005 | // ie, now the topmost value of the non-zero sized reference), since |
| 2006 | // we will discard the top of stack after unloading the reference |
| 2007 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2008 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2009 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2012 | // Discard the i'th entry pushed above or else the remainder of the |
| 2013 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2014 | frame_->Drop(); |
| 2015 | |
| 2016 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2017 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2018 | VisitAndSpill(node->body()); |
| 2019 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2020 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2021 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2022 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2023 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2024 | frame_->EmitPop(r0); |
| 2025 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2026 | frame_->EmitPush(r0); |
| 2027 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2028 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2029 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2030 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2031 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2032 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2033 | |
| 2034 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2035 | exit.Bind(); |
| 2036 | node->continue_target()->Unuse(); |
| 2037 | node->break_target()->Unuse(); |
| 2038 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2042 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2043 | #ifdef DEBUG |
| 2044 | int original_height = frame_->height(); |
| 2045 | #endif |
| 2046 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2048 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2050 | JumpTarget try_block(this); |
| 2051 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2052 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2053 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2054 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2055 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2056 | |
| 2057 | // Store the caught exception in the catch variable. |
| 2058 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2059 | ASSERT(ref.is_slot()); |
| 2060 | // Here we make use of the convenient property that it doesn't matter |
| 2061 | // whether a value is immediately on top of or underneath a zero-sized |
| 2062 | // reference. |
| 2063 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2067 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2070 | if (frame_ != NULL) { |
| 2071 | exit.Jump(); |
| 2072 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2073 | |
| 2074 | |
| 2075 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2076 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2078 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2079 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2081 | // Shadow the labels for all escapes from the try block, including |
| 2082 | // returns. During shadowing, the original label is hidden as the |
| 2083 | // LabelShadow and operations on the original actually affect the |
| 2084 | // shadowing label. |
| 2085 | // |
| 2086 | // We should probably try to unify the escaping labels and the return |
| 2087 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2088 | int nof_escapes = node->escaping_targets()->length(); |
| 2089 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2090 | |
| 2091 | // Add the shadow target for the function return. |
| 2092 | static const int kReturnShadowIndex = 0; |
| 2093 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2094 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2095 | function_return_is_shadowed_ = true; |
| 2096 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2097 | |
| 2098 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2100 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2104 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2105 | |
| 2106 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2107 | // After shadowing stops, the original labels are unshadowed and the |
| 2108 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2109 | bool has_unlinks = false; |
| 2110 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2112 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2113 | } |
| 2114 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2115 | |
| 2116 | // Get an external reference to the handler address. |
| 2117 | ExternalReference handler_address(Top::k_handler_address); |
| 2118 | |
| 2119 | // The next handler address is at kNextIndex in the stack. |
| 2120 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2121 | // If we can fall off the end of the try block, unlink from try chain. |
| 2122 | if (has_valid_frame()) { |
| 2123 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2124 | __ mov(r3, Operand(handler_address)); |
| 2125 | __ str(r1, MemOperand(r3)); |
| 2126 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2127 | if (has_unlinks) { |
| 2128 | exit.Jump(); |
| 2129 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2132 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2133 | // jumped to. Deallocate each shadow target. |
| 2134 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2136 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2137 | shadows[i]->Bind(); |
| 2138 | // Because we can be jumping here (to spilled code) from unspilled |
| 2139 | // code, we need to reestablish a spilled frame at this block. |
| 2140 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2142 | // Reload sp from the top handler, because some statements that we |
| 2143 | // break from (eg, for...in) may have left stuff on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2144 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2145 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2146 | // The stack pointer was restored to just below the code slot |
| 2147 | // (the topmost slot) in the handler. |
| 2148 | frame_->Forget(frame_->height() - handler_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2149 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2150 | // kNextIndex is off by one because the code slot has already |
| 2151 | // been dropped. |
| 2152 | __ ldr(r1, frame_->ElementAt(kNextIndex - 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2153 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2154 | // The code slot has already been dropped from the handler. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2155 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2157 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2158 | frame_->PrepareForReturn(); |
| 2159 | } |
| 2160 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2161 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2162 | delete shadows[i]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2165 | exit.Bind(); |
| 2166 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2170 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2171 | #ifdef DEBUG |
| 2172 | int original_height = frame_->height(); |
| 2173 | #endif |
| 2174 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2177 | |
| 2178 | // State: Used to keep track of reason for entering the finally |
| 2179 | // block. Should probably be extended to hold information for |
| 2180 | // break/continue from within the try block. |
| 2181 | enum { FALLING, THROWING, JUMPING }; |
| 2182 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2183 | JumpTarget try_block(this); |
| 2184 | JumpTarget finally_block(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2185 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2186 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2187 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2188 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2189 | // In case of thrown exceptions, this is where we continue. |
| 2190 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2191 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2192 | |
| 2193 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2194 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2196 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2197 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2198 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2199 | // Shadow the labels for all escapes from the try block, including |
| 2200 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2201 | // operations on the original actually affect the shadowing label. |
| 2202 | // |
| 2203 | // We should probably try to unify the escaping labels and the return |
| 2204 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2205 | int nof_escapes = node->escaping_targets()->length(); |
| 2206 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2207 | |
| 2208 | // Add the shadow target for the function return. |
| 2209 | static const int kReturnShadowIndex = 0; |
| 2210 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2211 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2212 | function_return_is_shadowed_ = true; |
| 2213 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2214 | |
| 2215 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2216 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2217 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2221 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2222 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2223 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2224 | // After shadowing stops, the original labels are unshadowed and the |
| 2225 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2226 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2227 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2228 | shadows[i]->StopShadowing(); |
| 2229 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2230 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2231 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2232 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2233 | // Get an external reference to the handler address. |
| 2234 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2235 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2236 | // The next handler address is at kNextIndex in the stack. |
| 2237 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2238 | // If we can fall off the end of the try block, unlink from the try |
| 2239 | // chain and set the state on the frame to FALLING. |
| 2240 | if (has_valid_frame()) { |
| 2241 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2242 | __ mov(r3, Operand(handler_address)); |
| 2243 | __ str(r1, MemOperand(r3)); |
| 2244 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2245 | |
| 2246 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2247 | // state in r2, then jump around the unlink blocks if any. |
| 2248 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2249 | frame_->EmitPush(r0); |
| 2250 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2251 | if (nof_unlinks > 0) { |
| 2252 | finally_block.Jump(); |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | // Generate code to unlink and set the state for the (formerly) |
| 2257 | // shadowing targets that have been jumped to. |
| 2258 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2259 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2260 | // If we have come from the shadowed return, the return value is |
| 2261 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2262 | // until it is pushed. |
| 2263 | // |
| 2264 | // Because we can be jumping here (to spilled code) from |
| 2265 | // unspilled code, we need to reestablish a spilled frame at |
| 2266 | // this block. |
| 2267 | shadows[i]->Bind(); |
| 2268 | frame_->SpillAll(); |
| 2269 | |
| 2270 | // Reload sp from the top handler, because some statements that |
| 2271 | // we break from (eg, for...in) may have left stuff on the |
| 2272 | // stack. |
| 2273 | __ mov(r3, Operand(handler_address)); |
| 2274 | __ ldr(sp, MemOperand(r3)); |
| 2275 | // The stack pointer was restored to the address slot in the handler. |
| 2276 | ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize); |
| 2277 | frame_->Forget(frame_->height() - handler_height + 1); |
| 2278 | |
| 2279 | // Unlink this handler and drop it from the frame. The next |
| 2280 | // handler address is now on top of the frame. |
| 2281 | frame_->EmitPop(r1); |
| 2282 | __ str(r1, MemOperand(r3)); |
| 2283 | // The top (code) and the second (handler) slot have both been |
| 2284 | // dropped already. |
| 2285 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2); |
| 2286 | |
| 2287 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2288 | // If this label shadowed the function return, materialize the |
| 2289 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2290 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2291 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2292 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2293 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2294 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2295 | } |
| 2296 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2297 | if (--nof_unlinks > 0) { |
| 2298 | // If this is not the last unlink block, jump around the next. |
| 2299 | finally_block.Jump(); |
| 2300 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2301 | } |
| 2302 | } |
| 2303 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2304 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2305 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2306 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2307 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2308 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2309 | |
| 2310 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2311 | // and the state - while evaluating the finally block. |
| 2312 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2313 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2314 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2315 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2316 | if (has_valid_frame()) { |
| 2317 | // Restore state and return value or faked TOS. |
| 2318 | frame_->EmitPop(r2); |
| 2319 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2322 | // Generate code to jump to the right destination for all used |
| 2323 | // formerly shadowing targets. Deallocate each shadow target. |
| 2324 | for (int i = 0; i < shadows.length(); i++) { |
| 2325 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2326 | JumpTarget* original = shadows[i]->other_target(); |
| 2327 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2328 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2329 | JumpTarget skip(this); |
| 2330 | skip.Branch(ne); |
| 2331 | frame_->PrepareForReturn(); |
| 2332 | original->Jump(); |
| 2333 | skip.Bind(); |
| 2334 | } else { |
| 2335 | original->Branch(eq); |
| 2336 | } |
| 2337 | } |
| 2338 | delete shadows[i]; |
| 2339 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2340 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2341 | if (has_valid_frame()) { |
| 2342 | // Check if we need to rethrow the exception. |
| 2343 | JumpTarget exit(this); |
| 2344 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2345 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2346 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2347 | // Rethrow exception. |
| 2348 | frame_->EmitPush(r0); |
| 2349 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2350 | |
| 2351 | // Done. |
| 2352 | exit.Bind(); |
| 2353 | } |
| 2354 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2358 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2359 | #ifdef DEBUG |
| 2360 | int original_height = frame_->height(); |
| 2361 | #endif |
| 2362 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2363 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2364 | CodeForStatementPosition(node); |
| 2365 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2366 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2367 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2371 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2372 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2373 | ASSERT(boilerplate->IsBoilerplate()); |
| 2374 | |
| 2375 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2376 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2377 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2378 | |
| 2379 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2380 | frame_->EmitPush(cp); |
| 2381 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2382 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2386 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2387 | #ifdef DEBUG |
| 2388 | int original_height = frame_->height(); |
| 2389 | #endif |
| 2390 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2391 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2392 | |
| 2393 | // Build the function boilerplate and instantiate it. |
| 2394 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2395 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2396 | if (HasStackOverflow()) { |
| 2397 | ASSERT(frame_->height() == original_height); |
| 2398 | return; |
| 2399 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2400 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2401 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2405 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2406 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2407 | #ifdef DEBUG |
| 2408 | int original_height = frame_->height(); |
| 2409 | #endif |
| 2410 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2411 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2412 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2413 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2417 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2418 | #ifdef DEBUG |
| 2419 | int original_height = frame_->height(); |
| 2420 | #endif |
| 2421 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2422 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2423 | JumpTarget then(this); |
| 2424 | JumpTarget else_(this); |
| 2425 | JumpTarget exit(this); |
| 2426 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2427 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2428 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2429 | then.Bind(); |
| 2430 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2431 | exit.Jump(); |
| 2432 | else_.Bind(); |
| 2433 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2434 | exit.Bind(); |
| 2435 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2439 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2440 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2441 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2442 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2443 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2444 | JumpTarget slow(this); |
| 2445 | JumpTarget done(this); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2446 | |
| 2447 | // Generate fast-case code for variables that might be shadowed by |
| 2448 | // eval-introduced variables. Eval is used a lot without |
| 2449 | // introducing variables. In those cases, we do not want to |
| 2450 | // perform a runtime call for all variables in the scope |
| 2451 | // containing the eval. |
| 2452 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2453 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2454 | // If there was no control flow to slow, we can exit early. |
| 2455 | if (!slow.is_linked()) { |
| 2456 | frame_->EmitPush(r0); |
| 2457 | return; |
| 2458 | } |
| 2459 | |
| 2460 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2461 | |
| 2462 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2463 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2464 | // Only generate the fast case for locals that rewrite to slots. |
| 2465 | // This rules out argument loads. |
| 2466 | if (potential_slot != NULL) { |
| 2467 | __ ldr(r0, |
| 2468 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2469 | r1, |
| 2470 | r2, |
| 2471 | &slow)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2472 | // There is always control flow to slow from |
| 2473 | // ContextSlotOperandCheckExtensions. |
| 2474 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2478 | slow.Bind(); |
| 2479 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2480 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2481 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2482 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2483 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2484 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2485 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2486 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2487 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2488 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2489 | done.Bind(); |
| 2490 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2491 | |
| 2492 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2493 | // Note: We would like to keep the assert below, but it fires because of |
| 2494 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2495 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2496 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2497 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2498 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2499 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2500 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2501 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2502 | // initialized yet) which needs to be converted into the 'undefined' |
| 2503 | // value. |
| 2504 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2505 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2506 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2507 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2508 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2514 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2515 | TypeofState typeof_state, |
| 2516 | Register tmp, |
| 2517 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2518 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2519 | // Check that no extension objects have been created by calls to |
| 2520 | // eval from the current scope to the global scope. |
| 2521 | Register context = cp; |
| 2522 | Scope* s = scope(); |
| 2523 | while (s != NULL) { |
| 2524 | if (s->num_heap_slots() > 0) { |
| 2525 | if (s->calls_eval()) { |
| 2526 | // Check that extension is NULL. |
| 2527 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2528 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2529 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2530 | } |
| 2531 | // Load next context in chain. |
| 2532 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2533 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2534 | context = tmp; |
| 2535 | } |
| 2536 | // If no outer scope calls eval, we do not need to check more |
| 2537 | // context extensions. |
| 2538 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2539 | s = s->outer_scope(); |
| 2540 | } |
| 2541 | |
| 2542 | if (s->is_eval_scope()) { |
| 2543 | Label next, fast; |
| 2544 | if (!context.is(tmp)) __ mov(tmp, Operand(context)); |
| 2545 | __ bind(&next); |
| 2546 | // Terminate at global context. |
| 2547 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2548 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2549 | __ b(eq, &fast); |
| 2550 | // Check that extension is NULL. |
| 2551 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2552 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2553 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2554 | // Load next context in chain. |
| 2555 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2556 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2557 | __ b(&next); |
| 2558 | __ bind(&fast); |
| 2559 | } |
| 2560 | |
| 2561 | // All extension objects were empty and it is safe to use a global |
| 2562 | // load IC call. |
| 2563 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2564 | // Load the global object. |
| 2565 | LoadGlobal(); |
| 2566 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2567 | Result name = allocator_->Allocate(r2); |
| 2568 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2569 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2570 | // Call IC stub. |
| 2571 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2572 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2573 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2574 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2577 | // Drop the global object. The result is in r0. |
| 2578 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2582 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2583 | #ifdef DEBUG |
| 2584 | int original_height = frame_->height(); |
| 2585 | #endif |
| 2586 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2587 | Comment cmnt(masm_, "[ Slot"); |
| 2588 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2589 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2590 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2591 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2592 | |
| 2593 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2594 | #ifdef DEBUG |
| 2595 | int original_height = frame_->height(); |
| 2596 | #endif |
| 2597 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2598 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2599 | |
| 2600 | Variable* var = node->var(); |
| 2601 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2602 | if (expr != NULL) { |
| 2603 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2604 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2605 | ASSERT(var->is_global()); |
| 2606 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2607 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2608 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2609 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2613 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2614 | #ifdef DEBUG |
| 2615 | int original_height = frame_->height(); |
| 2616 | #endif |
| 2617 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2619 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2620 | frame_->EmitPush(r0); |
| 2621 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2625 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2626 | #ifdef DEBUG |
| 2627 | int original_height = frame_->height(); |
| 2628 | #endif |
| 2629 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2630 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2631 | |
| 2632 | // Retrieve the literal array and check the allocated entry. |
| 2633 | |
| 2634 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2635 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2636 | |
| 2637 | // Load the literals array of the function. |
| 2638 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2639 | |
| 2640 | // Load the literal at the ast saved index. |
| 2641 | int literal_offset = |
| 2642 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2643 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2644 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2645 | JumpTarget done(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2646 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2647 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2648 | |
| 2649 | // If the entry is undefined we call the runtime system to computed |
| 2650 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2651 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2652 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2653 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2654 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2655 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2656 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2657 | frame_->EmitPush(r0); |
| 2658 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2659 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2660 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2661 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2662 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2663 | frame_->EmitPush(r2); |
| 2664 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | |
| 2668 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2669 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2670 | // Each created boilerplate is stored in the JSFunction and they are |
| 2671 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2672 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2673 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2674 | DeferredObjectLiteral(CodeGenerator* generator, ObjectLiteral* node) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2675 | : DeferredCode(generator), node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2676 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2677 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2678 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2679 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2680 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2681 | private: |
| 2682 | ObjectLiteral* node_; |
| 2683 | }; |
| 2684 | |
| 2685 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2686 | void DeferredObjectLiteral::Generate() { |
| 2687 | // Argument is passed in r1. |
| 2688 | enter()->Bind(); |
| 2689 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 2690 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2691 | // If the entry is undefined we call the runtime system to compute |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2692 | // the literal. |
| 2693 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2694 | VirtualFrame* frame = generator()->frame(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2695 | // Literal array (0). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2696 | frame->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2697 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2698 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2699 | frame->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2700 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2701 | __ mov(r0, Operand(node_->constant_properties())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2702 | frame->EmitPush(r0); |
| 2703 | Result boilerplate = |
| 2704 | frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2705 | __ mov(r2, Operand(boilerplate.reg())); |
| 2706 | // Result is returned in r2. |
| 2707 | exit_.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2711 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2712 | #ifdef DEBUG |
| 2713 | int original_height = frame_->height(); |
| 2714 | #endif |
| 2715 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2716 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2717 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2718 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(this, node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2719 | |
| 2720 | // Retrieve the literal array and check the allocated entry. |
| 2721 | |
| 2722 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2723 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2724 | |
| 2725 | // Load the literals array of the function. |
| 2726 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2727 | |
| 2728 | // Load the literal at the ast saved index. |
| 2729 | int literal_offset = |
| 2730 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2731 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2732 | |
| 2733 | // Check whether we need to materialize the object literal boilerplate. |
| 2734 | // If so, jump to the deferred code. |
| 2735 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2736 | deferred->enter()->Branch(eq); |
| 2737 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2738 | |
| 2739 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2740 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2741 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2742 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2743 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2744 | if (node->depth() == 1) { |
| 2745 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2746 | } |
| 2747 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2748 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2749 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2750 | |
| 2751 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2752 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2753 | Literal* key = property->key(); |
| 2754 | Expression* value = property->value(); |
| 2755 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2756 | case ObjectLiteral::Property::CONSTANT: |
| 2757 | break; |
| 2758 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2759 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2760 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2761 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2762 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2763 | frame_->EmitPush(r0); // dup the result |
| 2764 | LoadAndSpill(key); |
| 2765 | LoadAndSpill(value); |
| 2766 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2767 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2768 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2769 | break; |
| 2770 | } |
| 2771 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2772 | frame_->EmitPush(r0); |
| 2773 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2774 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2775 | frame_->EmitPush(r0); |
| 2776 | LoadAndSpill(value); |
| 2777 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2778 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2779 | break; |
| 2780 | } |
| 2781 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2782 | frame_->EmitPush(r0); |
| 2783 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2784 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2785 | frame_->EmitPush(r0); |
| 2786 | LoadAndSpill(value); |
| 2787 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2788 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2789 | break; |
| 2790 | } |
| 2791 | } |
| 2792 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2793 | ASSERT(frame_->height() == original_height + 1); |
| 2794 | } |
| 2795 | |
| 2796 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2797 | // This deferred code stub will be used for creating the boilerplate |
| 2798 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2799 | // Each created boilerplate is stored in the JSFunction and they are |
| 2800 | // therefore context dependent. |
| 2801 | class DeferredArrayLiteral: public DeferredCode { |
| 2802 | public: |
| 2803 | DeferredArrayLiteral(CodeGenerator* generator, ArrayLiteral* node) |
| 2804 | : DeferredCode(generator), node_(node) { |
| 2805 | set_comment("[ DeferredArrayLiteral"); |
| 2806 | } |
| 2807 | |
| 2808 | virtual void Generate(); |
| 2809 | |
| 2810 | private: |
| 2811 | ArrayLiteral* node_; |
| 2812 | }; |
| 2813 | |
| 2814 | |
| 2815 | void DeferredArrayLiteral::Generate() { |
| 2816 | // Argument is passed in r1. |
| 2817 | enter()->Bind(); |
| 2818 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 2819 | |
| 2820 | // If the entry is undefined we call the runtime system to computed |
| 2821 | // the literal. |
| 2822 | |
| 2823 | VirtualFrame* frame = generator()->frame(); |
| 2824 | // Literal array (0). |
| 2825 | frame->EmitPush(r1); |
| 2826 | // Literal index (1). |
| 2827 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
| 2828 | frame->EmitPush(r0); |
| 2829 | // Constant properties (2). |
| 2830 | __ mov(r0, Operand(node_->literals())); |
| 2831 | frame->EmitPush(r0); |
| 2832 | Result boilerplate = |
| 2833 | frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2834 | __ mov(r2, Operand(boilerplate.reg())); |
| 2835 | // Result is returned in r2. |
| 2836 | exit_.Jump(); |
| 2837 | } |
| 2838 | |
| 2839 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2840 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2841 | #ifdef DEBUG |
| 2842 | int original_height = frame_->height(); |
| 2843 | #endif |
| 2844 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2845 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2846 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2847 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2848 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2849 | // Retrieve the literal array and check the allocated entry. |
| 2850 | |
| 2851 | // Load the function of this activation. |
| 2852 | __ ldr(r1, frame_->Function()); |
| 2853 | |
| 2854 | // Load the literals array of the function. |
| 2855 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2856 | |
| 2857 | // Load the literal at the ast saved index. |
| 2858 | int literal_offset = |
| 2859 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2860 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2861 | |
| 2862 | // Check whether we need to materialize the object literal boilerplate. |
| 2863 | // If so, jump to the deferred code. |
| 2864 | __ cmp(r2, Operand(Factory::undefined_value())); |
| 2865 | deferred->enter()->Branch(eq); |
| 2866 | deferred->BindExit(); |
| 2867 | |
| 2868 | // Push the object literal boilerplate. |
| 2869 | frame_->EmitPush(r2); |
| 2870 | |
| 2871 | // Clone the boilerplate object. |
| 2872 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2873 | if (node->depth() == 1) { |
| 2874 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2875 | } |
| 2876 | frame_->CallRuntime(clone_function_id, 1); |
| 2877 | frame_->EmitPush(r0); // save the result |
| 2878 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2879 | |
| 2880 | // Generate code to set the elements in the array that are not |
| 2881 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2882 | for (int i = 0; i < node->values()->length(); i++) { |
| 2883 | Expression* value = node->values()->at(i); |
| 2884 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2885 | // If value is a literal the property value is already set in the |
| 2886 | // boilerplate object. |
| 2887 | if (value->AsLiteral() != NULL) continue; |
| 2888 | // If value is a materialized literal the property value is already set |
| 2889 | // in the boilerplate object if it is simple. |
| 2890 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2891 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2892 | // The property must be set by generated code. |
| 2893 | LoadAndSpill(value); |
| 2894 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2895 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2896 | // Fetch the object literal. |
| 2897 | __ ldr(r1, frame_->Top()); |
| 2898 | // Get the elements array. |
| 2899 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2900 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 2901 | // Write to the indexed properties array. |
| 2902 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2903 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2904 | |
| 2905 | // Update the write barrier for the array address. |
| 2906 | __ mov(r3, Operand(offset)); |
| 2907 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2908 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2909 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2913 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2914 | #ifdef DEBUG |
| 2915 | int original_height = frame_->height(); |
| 2916 | #endif |
| 2917 | ASSERT(!in_spilled_code()); |
| 2918 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2919 | // Call runtime routine to allocate the catch extension object and |
| 2920 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2921 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2922 | LoadAndSpill(node->key()); |
| 2923 | LoadAndSpill(node->value()); |
| 2924 | Result result = |
| 2925 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2926 | frame_->EmitPush(result.reg()); |
| 2927 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2931 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2932 | #ifdef DEBUG |
| 2933 | int original_height = frame_->height(); |
| 2934 | #endif |
| 2935 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2936 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2937 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2938 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2939 | { Reference target(this, node->target()); |
| 2940 | if (target.is_illegal()) { |
| 2941 | // Fool the virtual frame into thinking that we left the assignment's |
| 2942 | // value on the frame. |
| 2943 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2944 | frame_->EmitPush(r0); |
| 2945 | ASSERT(frame_->height() == original_height + 1); |
| 2946 | return; |
| 2947 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2948 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2949 | if (node->op() == Token::ASSIGN || |
| 2950 | node->op() == Token::INIT_VAR || |
| 2951 | node->op() == Token::INIT_CONST) { |
| 2952 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2953 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2954 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2955 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2956 | Literal* literal = node->value()->AsLiteral(); |
| 2957 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 2958 | SmiOperation(node->binary_op(), literal->handle(), false); |
| 2959 | frame_->EmitPush(r0); |
| 2960 | |
| 2961 | } else { |
| 2962 | LoadAndSpill(node->value()); |
| 2963 | GenericBinaryOperation(node->binary_op()); |
| 2964 | frame_->EmitPush(r0); |
| 2965 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2966 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2968 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2969 | if (var != NULL && |
| 2970 | (var->mode() == Variable::CONST) && |
| 2971 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2972 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2973 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2974 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2975 | CodeForSourcePosition(node->position()); |
| 2976 | if (node->op() == Token::INIT_CONST) { |
| 2977 | // Dynamic constant initializations must use the function context |
| 2978 | // and initialize the actual constant declared. Dynamic variable |
| 2979 | // initializations are simply assignments and use SetValue. |
| 2980 | target.SetValue(CONST_INIT); |
| 2981 | } else { |
| 2982 | target.SetValue(NOT_CONST_INIT); |
| 2983 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2984 | } |
| 2985 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2986 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2990 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2991 | #ifdef DEBUG |
| 2992 | int original_height = frame_->height(); |
| 2993 | #endif |
| 2994 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2995 | Comment cmnt(masm_, "[ Throw"); |
| 2996 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2997 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2998 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2999 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3000 | frame_->EmitPush(r0); |
| 3001 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3005 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3006 | #ifdef DEBUG |
| 3007 | int original_height = frame_->height(); |
| 3008 | #endif |
| 3009 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3010 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3012 | { Reference property(this, node); |
| 3013 | property.GetValueAndSpill(typeof_state()); |
| 3014 | } |
| 3015 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3019 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3020 | #ifdef DEBUG |
| 3021 | int original_height = frame_->height(); |
| 3022 | #endif |
| 3023 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3024 | Comment cmnt(masm_, "[ Call"); |
| 3025 | |
| 3026 | ZoneList<Expression*>* args = node->arguments(); |
| 3027 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3028 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3029 | // Standard function call. |
| 3030 | |
| 3031 | // Check if the function is a variable or a property. |
| 3032 | Expression* function = node->expression(); |
| 3033 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3034 | Property* property = function->AsProperty(); |
| 3035 | |
| 3036 | // ------------------------------------------------------------------------ |
| 3037 | // Fast-case: Use inline caching. |
| 3038 | // --- |
| 3039 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3040 | // must be resolved after the arguments have been evaluated. The IC code |
| 3041 | // automatically handles this by loading the arguments before the function |
| 3042 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3043 | // ------------------------------------------------------------------------ |
| 3044 | |
| 3045 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 3046 | // ---------------------------------- |
| 3047 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3048 | // ---------------------------------- |
| 3049 | |
| 3050 | // 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] | 3051 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3052 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3053 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3054 | // Pass the global object as the receiver and let the IC stub |
| 3055 | // patch the stack to use the global proxy as 'this' in the |
| 3056 | // invoked function. |
| 3057 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3058 | |
| 3059 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3060 | int arg_count = args->length(); |
| 3061 | for (int i = 0; i < arg_count; i++) { |
| 3062 | LoadAndSpill(args->at(i)); |
| 3063 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3064 | |
| 3065 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3066 | Handle<Code> stub = ComputeCallInitialize(arg_count); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3067 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3068 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3069 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3070 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3072 | frame_->Drop(); |
| 3073 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3074 | |
| 3075 | } else if (var != NULL && var->slot() != NULL && |
| 3076 | var->slot()->type() == Slot::LOOKUP) { |
| 3077 | // ---------------------------------- |
| 3078 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3079 | // ---------------------------------- |
| 3080 | |
| 3081 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3082 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3083 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3084 | frame_->EmitPush(r0); |
| 3085 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3086 | // r0: slot value; r1: receiver |
| 3087 | |
| 3088 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3089 | frame_->EmitPush(r0); // function |
| 3090 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3091 | |
| 3092 | // Call the function. |
| 3093 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3094 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3095 | |
| 3096 | } else if (property != NULL) { |
| 3097 | // Check if the key is a literal string. |
| 3098 | Literal* literal = property->key()->AsLiteral(); |
| 3099 | |
| 3100 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3101 | // ------------------------------------------------------------------ |
| 3102 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3103 | // ------------------------------------------------------------------ |
| 3104 | |
| 3105 | // 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] | 3106 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3107 | frame_->EmitPush(r0); |
| 3108 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | |
| 3110 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3111 | int arg_count = args->length(); |
| 3112 | for (int i = 0; i < arg_count; i++) { |
| 3113 | LoadAndSpill(args->at(i)); |
| 3114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3115 | |
| 3116 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3117 | Handle<Code> stub = ComputeCallInitialize(arg_count); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3118 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3119 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3120 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | |
| 3122 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3123 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3124 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3125 | frame_->EmitPush(r0); // push after get rid of function from the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3126 | |
| 3127 | } else { |
| 3128 | // ------------------------------------------- |
| 3129 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3130 | // ------------------------------------------- |
| 3131 | |
| 3132 | // Load the function to call from the property through a reference. |
| 3133 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3134 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3135 | |
| 3136 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3137 | if (property->is_synthetic()) { |
| 3138 | LoadGlobalReceiver(r0); |
| 3139 | } else { |
| 3140 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3141 | frame_->EmitPush(r0); |
| 3142 | } |
| 3143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3144 | // Call the function. |
| 3145 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3146 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | } else { |
| 3150 | // ---------------------------------- |
| 3151 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3152 | // ---------------------------------- |
| 3153 | |
| 3154 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3155 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3156 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3157 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3158 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3159 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3160 | // Call the function. |
| 3161 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3162 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3163 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3164 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3168 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3169 | #ifdef DEBUG |
| 3170 | int original_height = frame_->height(); |
| 3171 | #endif |
| 3172 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3173 | Comment cmnt(masm_, "[ CallEval"); |
| 3174 | |
| 3175 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3176 | // the function we need to call and the receiver of the call. |
| 3177 | // Then we call the resolved function using the given arguments. |
| 3178 | |
| 3179 | ZoneList<Expression*>* args = node->arguments(); |
| 3180 | Expression* function = node->expression(); |
| 3181 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3182 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3183 | |
| 3184 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3185 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3186 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3187 | frame_->EmitPush(r2); // Slot for receiver |
| 3188 | int arg_count = args->length(); |
| 3189 | for (int i = 0; i < arg_count; i++) { |
| 3190 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3194 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3195 | frame_->EmitPush(r1); |
| 3196 | if (arg_count > 0) { |
| 3197 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3198 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3199 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3200 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3204 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Touch up stack with the right values for the function and the receiver. |
| 3207 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3208 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3209 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3210 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3211 | |
| 3212 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3213 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3214 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3215 | CallFunctionStub call_function(arg_count); |
| 3216 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3217 | |
| 3218 | __ ldr(cp, frame_->Context()); |
| 3219 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3220 | frame_->Drop(); |
| 3221 | frame_->EmitPush(r0); |
| 3222 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3226 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3227 | #ifdef DEBUG |
| 3228 | int original_height = frame_->height(); |
| 3229 | #endif |
| 3230 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3231 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3232 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3233 | |
| 3234 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3235 | // expression in new calls must be evaluated before the |
| 3236 | // arguments. This is different from ordinary calls, where the |
| 3237 | // actual function to call is resolved after the arguments have been |
| 3238 | // evaluated. |
| 3239 | |
| 3240 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3241 | // receiver. There is no need to use the global proxy here because |
| 3242 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3243 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3244 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Push the arguments ("left-to-right") on the stack. |
| 3247 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3248 | int arg_count = args->length(); |
| 3249 | for (int i = 0; i < arg_count; i++) { |
| 3250 | LoadAndSpill(args->at(i)); |
| 3251 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3252 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3253 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3254 | Result num_args = allocator_->Allocate(r0); |
| 3255 | ASSERT(num_args.is_valid()); |
| 3256 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3257 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3258 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3259 | Result function = allocator_->Allocate(r1); |
| 3260 | ASSERT(function.is_valid()); |
| 3261 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3262 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3263 | // Call the construct call builtin that handles allocation and |
| 3264 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3265 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3266 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3267 | Result result = frame_->CallCodeObject(ic, |
| 3268 | RelocInfo::CONSTRUCT_CALL, |
| 3269 | &num_args, |
| 3270 | &function, |
| 3271 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3272 | |
| 3273 | // 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] | 3274 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3275 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3279 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3280 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3281 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3282 | JumpTarget leave(this); |
| 3283 | LoadAndSpill(args->at(0)); |
| 3284 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3285 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3286 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3287 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | // It is a heap object - get map. |
| 3289 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3290 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3291 | // if (!object->IsJSValue()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3292 | __ cmp(r1, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3293 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | // Load the value. |
| 3295 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3296 | leave.Bind(); |
| 3297 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3301 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3302 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3303 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3304 | JumpTarget leave(this); |
| 3305 | LoadAndSpill(args->at(0)); // Load the object. |
| 3306 | LoadAndSpill(args->at(1)); // Load the value. |
| 3307 | frame_->EmitPop(r0); // r0 contains value |
| 3308 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3309 | // if (object->IsSmi()) return object. |
| 3310 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3311 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3312 | // It is a heap object - get map. |
| 3313 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3314 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3315 | // if (!object->IsJSValue()) return object. |
| 3316 | __ cmp(r2, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3317 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3318 | // Store the value. |
| 3319 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3320 | // Update the write barrier. |
| 3321 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3322 | __ RecordWrite(r1, r2, r3); |
| 3323 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3324 | leave.Bind(); |
| 3325 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3329 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3330 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3331 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3332 | LoadAndSpill(args->at(0)); |
| 3333 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3334 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3335 | cc_reg_ = eq; |
| 3336 | } |
| 3337 | |
| 3338 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3339 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3340 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3341 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3342 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3343 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3344 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3345 | LoadAndSpill(args->at(1)); |
| 3346 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3347 | __ CallRuntime(Runtime::kLog, 2); |
| 3348 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3349 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3350 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3351 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3355 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3356 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3357 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3358 | LoadAndSpill(args->at(0)); |
| 3359 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3360 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3361 | cc_reg_ = eq; |
| 3362 | } |
| 3363 | |
| 3364 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3365 | // This should generate code that performs a charCodeAt() call or returns |
| 3366 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3367 | // 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] | 3368 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3369 | VirtualFrame::SpilledScope spilled_scope(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3370 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3371 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3372 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3376 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3377 | VirtualFrame::SpilledScope spilled_scope(this); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3378 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3379 | LoadAndSpill(args->at(0)); |
| 3380 | JumpTarget answer(this); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3381 | // We need the CC bits to come out as not_equal in the case where the |
| 3382 | // object is a smi. This can't be done with the usual test opcode so |
| 3383 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3384 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3385 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3386 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3387 | answer.Branch(ne); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3388 | // It is a heap object - get the map. |
| 3389 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3390 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3391 | // Check if the object is a JS array or not. |
| 3392 | __ cmp(r1, Operand(JS_ARRAY_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3393 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3394 | cc_reg_ = eq; |
| 3395 | } |
| 3396 | |
| 3397 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3398 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3399 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3400 | ASSERT(args->length() == 0); |
| 3401 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3402 | // Seed the result with the formal parameters count, which will be used |
| 3403 | // 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] | 3404 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3405 | |
| 3406 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3407 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3408 | frame_->CallStub(&stub, 0); |
| 3409 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3413 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3414 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3415 | ASSERT(args->length() == 1); |
| 3416 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3417 | // Satisfy contract with ArgumentsAccessStub: |
| 3418 | // Load the key into r1 and the formal parameters count into r0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3419 | LoadAndSpill(args->at(0)); |
| 3420 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3421 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3422 | |
| 3423 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3424 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3425 | frame_->CallStub(&stub, 0); |
| 3426 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3430 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3431 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3432 | ASSERT(args->length() == 2); |
| 3433 | |
| 3434 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3435 | LoadAndSpill(args->at(0)); |
| 3436 | LoadAndSpill(args->at(1)); |
| 3437 | frame_->EmitPop(r0); |
| 3438 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3439 | __ cmp(r0, Operand(r1)); |
| 3440 | cc_reg_ = eq; |
| 3441 | } |
| 3442 | |
| 3443 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3444 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3445 | #ifdef DEBUG |
| 3446 | int original_height = frame_->height(); |
| 3447 | #endif |
| 3448 | VirtualFrame::SpilledScope spilled_scope(this); |
| 3449 | if (CheckForInlineRuntimeCall(node)) { |
| 3450 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3451 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3452 | return; |
| 3453 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3454 | |
| 3455 | ZoneList<Expression*>* args = node->arguments(); |
| 3456 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3457 | Runtime::Function* function = node->function(); |
| 3458 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3459 | if (function != NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3460 | // Push the arguments ("left-to-right"). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3461 | int arg_count = args->length(); |
| 3462 | for (int i = 0; i < arg_count; i++) { |
| 3463 | LoadAndSpill(args->at(i)); |
| 3464 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3465 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3466 | // Call the C runtime function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3467 | frame_->CallRuntime(function, arg_count); |
| 3468 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3469 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3470 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3471 | // Prepare stack for calling JS runtime function. |
| 3472 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3473 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3474 | // Push the builtins object found in the current global object. |
| 3475 | __ ldr(r1, GlobalObject()); |
| 3476 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3477 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3478 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3479 | int arg_count = args->length(); |
| 3480 | for (int i = 0; i < arg_count; i++) { |
| 3481 | LoadAndSpill(args->at(i)); |
| 3482 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3483 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3484 | // Call the JS runtime function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3485 | Handle<Code> stub = ComputeCallInitialize(args->length()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3486 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3487 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3488 | frame_->Drop(); |
| 3489 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3490 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3491 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3495 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3496 | #ifdef DEBUG |
| 3497 | int original_height = frame_->height(); |
| 3498 | #endif |
| 3499 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3500 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3501 | |
| 3502 | Token::Value op = node->op(); |
| 3503 | |
| 3504 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3505 | LoadConditionAndSpill(node->expression(), |
| 3506 | NOT_INSIDE_TYPEOF, |
| 3507 | false_target(), |
| 3508 | true_target(), |
| 3509 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3510 | cc_reg_ = NegateCondition(cc_reg_); |
| 3511 | |
| 3512 | } else if (op == Token::DELETE) { |
| 3513 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3514 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3515 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3516 | LoadAndSpill(property->obj()); |
| 3517 | LoadAndSpill(property->key()); |
| 3518 | Result arg_count = allocator_->Allocate(r0); |
| 3519 | ASSERT(arg_count.is_valid()); |
| 3520 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3521 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3522 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3523 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3524 | Slot* slot = variable->slot(); |
| 3525 | if (variable->is_global()) { |
| 3526 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3527 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3528 | frame_->EmitPush(r0); |
| 3529 | Result arg_count = allocator_->Allocate(r0); |
| 3530 | ASSERT(arg_count.is_valid()); |
| 3531 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3532 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | |
| 3534 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3535 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3536 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3537 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3538 | frame_->EmitPush(r0); |
| 3539 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3540 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3541 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3542 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3543 | frame_->EmitPush(r0); |
| 3544 | Result arg_count = allocator_->Allocate(r0); |
| 3545 | ASSERT(arg_count.is_valid()); |
| 3546 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3547 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3548 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3549 | } else { |
| 3550 | // Default: Result of deleting non-global, not dynamically |
| 3551 | // introduced variables is false. |
| 3552 | __ mov(r0, Operand(Factory::false_value())); |
| 3553 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3554 | |
| 3555 | } else { |
| 3556 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3557 | LoadAndSpill(node->expression()); // may have side-effects |
| 3558 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3559 | __ mov(r0, Operand(Factory::true_value())); |
| 3560 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3561 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3562 | |
| 3563 | } else if (op == Token::TYPEOF) { |
| 3564 | // Special case for loading the typeof expression; see comment on |
| 3565 | // LoadTypeofExpression(). |
| 3566 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3567 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3568 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3569 | |
| 3570 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3571 | LoadAndSpill(node->expression()); |
| 3572 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3573 | switch (op) { |
| 3574 | case Token::NOT: |
| 3575 | case Token::DELETE: |
| 3576 | case Token::TYPEOF: |
| 3577 | UNREACHABLE(); // handled above |
| 3578 | break; |
| 3579 | |
| 3580 | case Token::SUB: { |
| 3581 | UnarySubStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3582 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3583 | break; |
| 3584 | } |
| 3585 | |
| 3586 | case Token::BIT_NOT: { |
| 3587 | // smi check |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3588 | JumpTarget smi_label(this); |
| 3589 | JumpTarget continue_label(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3590 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3591 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3592 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3593 | frame_->EmitPush(r0); |
| 3594 | Result arg_count = allocator_->Allocate(r0); |
| 3595 | ASSERT(arg_count.is_valid()); |
| 3596 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3597 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3598 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3599 | continue_label.Jump(); |
| 3600 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3601 | __ mvn(r0, Operand(r0)); |
| 3602 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3603 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | } |
| 3606 | |
| 3607 | case Token::VOID: |
| 3608 | // since the stack top is cached in r0, popping and then |
| 3609 | // pushing a value can be done by just writing to r0. |
| 3610 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3611 | break; |
| 3612 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3613 | case Token::ADD: { |
| 3614 | // Smi check. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3615 | JumpTarget continue_label(this); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3616 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3617 | continue_label.Branch(eq); |
| 3618 | frame_->EmitPush(r0); |
| 3619 | Result arg_count = allocator_->Allocate(r0); |
| 3620 | ASSERT(arg_count.is_valid()); |
| 3621 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3622 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3623 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3624 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | default: |
| 3627 | UNREACHABLE(); |
| 3628 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3629 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3630 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3631 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3632 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3636 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3637 | #ifdef DEBUG |
| 3638 | int original_height = frame_->height(); |
| 3639 | #endif |
| 3640 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3641 | Comment cmnt(masm_, "[ CountOperation"); |
| 3642 | |
| 3643 | bool is_postfix = node->is_postfix(); |
| 3644 | bool is_increment = node->op() == Token::INC; |
| 3645 | |
| 3646 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3647 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3648 | |
| 3649 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3650 | if (is_postfix) { |
| 3651 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3652 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3653 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3654 | |
| 3655 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3656 | if (target.is_illegal()) { |
| 3657 | // Spoof the virtual frame to have the expected height (one higher |
| 3658 | // than on entry). |
| 3659 | if (!is_postfix) { |
| 3660 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3661 | frame_->EmitPush(r0); |
| 3662 | } |
| 3663 | ASSERT(frame_->height() == original_height + 1); |
| 3664 | return; |
| 3665 | } |
| 3666 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3667 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3668 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3669 | JumpTarget slow(this); |
| 3670 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3671 | |
| 3672 | // Load the value (1) into register r1. |
| 3673 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3674 | |
| 3675 | // Check for smi operand. |
| 3676 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3677 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3678 | |
| 3679 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3680 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3681 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3682 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3683 | |
| 3684 | // Perform optimistic increment/decrement. |
| 3685 | if (is_increment) { |
| 3686 | __ add(r0, r0, Operand(r1), SetCC); |
| 3687 | } else { |
| 3688 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3689 | } |
| 3690 | |
| 3691 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3692 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3693 | |
| 3694 | // Revert optimistic increment/decrement. |
| 3695 | if (is_increment) { |
| 3696 | __ sub(r0, r0, Operand(r1)); |
| 3697 | } else { |
| 3698 | __ add(r0, r0, Operand(r1)); |
| 3699 | } |
| 3700 | |
| 3701 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3702 | slow.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3703 | |
| 3704 | // Postfix: Convert the operand to a number and store it as the result. |
| 3705 | if (is_postfix) { |
| 3706 | InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3707 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3708 | // Store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3709 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | // Compute the new value by calling the right JavaScript native. |
| 3713 | if (is_increment) { |
| 3714 | InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3715 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3716 | } else { |
| 3717 | InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3718 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3722 | exit.Bind(); |
| 3723 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3724 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3728 | if (is_postfix) frame_->EmitPop(r0); |
| 3729 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3733 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3734 | #ifdef DEBUG |
| 3735 | int original_height = frame_->height(); |
| 3736 | #endif |
| 3737 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3738 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3739 | Token::Value op = node->op(); |
| 3740 | |
| 3741 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3742 | // operators must yield the result of one of the two expressions |
| 3743 | // before any ToBoolean() conversions. This means that the value |
| 3744 | // produced by a && or || operator is not necessarily a boolean. |
| 3745 | |
| 3746 | // NOTE: If the left hand side produces a materialized value (not in |
| 3747 | // the CC register), we force the right hand side to do the |
| 3748 | // same. This is necessary because we may have to branch to the exit |
| 3749 | // after evaluating the left hand side (due to the shortcut |
| 3750 | // semantics), but the compiler must (statically) know if the result |
| 3751 | // of compiling the binary operation is materialized or not. |
| 3752 | |
| 3753 | if (op == Token::AND) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3754 | JumpTarget is_true(this); |
| 3755 | LoadConditionAndSpill(node->left(), |
| 3756 | NOT_INSIDE_TYPEOF, |
| 3757 | &is_true, |
| 3758 | false_target(), |
| 3759 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3760 | if (has_cc()) { |
| 3761 | Branch(false, false_target()); |
| 3762 | |
| 3763 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3764 | is_true.Bind(); |
| 3765 | LoadConditionAndSpill(node->right(), |
| 3766 | NOT_INSIDE_TYPEOF, |
| 3767 | true_target(), |
| 3768 | false_target(), |
| 3769 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3770 | |
| 3771 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3772 | JumpTarget pop_and_continue(this); |
| 3773 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3774 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3775 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3776 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3777 | // Avoid popping the result if it converts to 'false' using the |
| 3778 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3779 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3780 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3781 | Branch(false, &exit); |
| 3782 | |
| 3783 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3784 | pop_and_continue.Bind(); |
| 3785 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3786 | |
| 3787 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3788 | is_true.Bind(); |
| 3789 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3790 | |
| 3791 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3792 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | } else if (op == Token::OR) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3796 | JumpTarget is_false(this); |
| 3797 | LoadConditionAndSpill(node->left(), |
| 3798 | NOT_INSIDE_TYPEOF, |
| 3799 | true_target(), |
| 3800 | &is_false, |
| 3801 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3802 | if (has_cc()) { |
| 3803 | Branch(true, true_target()); |
| 3804 | |
| 3805 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | is_false.Bind(); |
| 3807 | LoadConditionAndSpill(node->right(), |
| 3808 | NOT_INSIDE_TYPEOF, |
| 3809 | true_target(), |
| 3810 | false_target(), |
| 3811 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3812 | |
| 3813 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3814 | JumpTarget pop_and_continue(this); |
| 3815 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3816 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3817 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3818 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3819 | // Avoid popping the result if it converts to 'true' using the |
| 3820 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3821 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3822 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3823 | Branch(true, &exit); |
| 3824 | |
| 3825 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3826 | pop_and_continue.Bind(); |
| 3827 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3828 | |
| 3829 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3830 | is_false.Bind(); |
| 3831 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3832 | |
| 3833 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3834 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | } else { |
| 3838 | // Optimize for the case where (at least) one of the expressions |
| 3839 | // is a literal small integer. |
| 3840 | Literal* lliteral = node->left()->AsLiteral(); |
| 3841 | Literal* rliteral = node->right()->AsLiteral(); |
| 3842 | |
| 3843 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3844 | LoadAndSpill(node->left()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3845 | SmiOperation(node->op(), rliteral->handle(), false); |
| 3846 | |
| 3847 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3848 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3849 | SmiOperation(node->op(), lliteral->handle(), true); |
| 3850 | |
| 3851 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3852 | LoadAndSpill(node->left()); |
| 3853 | LoadAndSpill(node->right()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3854 | GenericBinaryOperation(node->op()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3855 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3856 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3857 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3858 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3859 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3863 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3864 | #ifdef DEBUG |
| 3865 | int original_height = frame_->height(); |
| 3866 | #endif |
| 3867 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3868 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3869 | frame_->EmitPush(r0); |
| 3870 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3874 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3875 | #ifdef DEBUG |
| 3876 | int original_height = frame_->height(); |
| 3877 | #endif |
| 3878 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3879 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3880 | |
| 3881 | // Get the expressions from the node. |
| 3882 | Expression* left = node->left(); |
| 3883 | Expression* right = node->right(); |
| 3884 | Token::Value op = node->op(); |
| 3885 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3886 | // To make null checks efficient, we check if either left or right is the |
| 3887 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3888 | // instead of calling the (very) general runtime routine for checking |
| 3889 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3890 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3891 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3892 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3893 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3894 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3895 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3896 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3897 | LoadAndSpill(left_is_null ? right : left); |
| 3898 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3899 | __ cmp(r0, Operand(Factory::null_value())); |
| 3900 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3901 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3902 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3903 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3904 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3905 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3906 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3907 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3908 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3909 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3910 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3912 | // It can be an undetectable object. |
| 3913 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3914 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3915 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3916 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3919 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3920 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3921 | return; |
| 3922 | } |
| 3923 | } |
| 3924 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3925 | // To make typeof testing for natives implemented in JavaScript really |
| 3926 | // efficient, we generate special code for expressions of the form: |
| 3927 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3928 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3929 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3930 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3931 | (right->AsLiteral() != NULL && |
| 3932 | right->AsLiteral()->handle()->IsString())) { |
| 3933 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3934 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3935 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3936 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3937 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3938 | |
| 3939 | if (check->Equals(Heap::number_symbol())) { |
| 3940 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3941 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3942 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3943 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3944 | cc_reg_ = eq; |
| 3945 | |
| 3946 | } else if (check->Equals(Heap::string_symbol())) { |
| 3947 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3948 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | |
| 3950 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3951 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3952 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3954 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3955 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3956 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3957 | |
| 3958 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3959 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3960 | cc_reg_ = lt; |
| 3961 | |
| 3962 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3963 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3964 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3965 | __ cmp(r1, Operand(Factory::false_value())); |
| 3966 | cc_reg_ = eq; |
| 3967 | |
| 3968 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3969 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3970 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | |
| 3972 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3973 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3974 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3975 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3977 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3978 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3979 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3980 | |
| 3981 | cc_reg_ = eq; |
| 3982 | |
| 3983 | } else if (check->Equals(Heap::function_symbol())) { |
| 3984 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3985 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3986 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3987 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3988 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3989 | cc_reg_ = eq; |
| 3990 | |
| 3991 | } else if (check->Equals(Heap::object_symbol())) { |
| 3992 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3993 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3994 | |
| 3995 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3996 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3997 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3998 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3999 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4000 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4001 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4002 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4003 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4004 | |
| 4005 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4006 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4007 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4008 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4009 | cc_reg_ = le; |
| 4010 | |
| 4011 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4012 | // Uncommon case: typeof testing against a string literal that is |
| 4013 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4014 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4015 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4016 | ASSERT(!has_valid_frame() || |
| 4017 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | return; |
| 4019 | } |
| 4020 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4021 | LoadAndSpill(left); |
| 4022 | LoadAndSpill(right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4023 | switch (op) { |
| 4024 | case Token::EQ: |
| 4025 | Comparison(eq, false); |
| 4026 | break; |
| 4027 | |
| 4028 | case Token::LT: |
| 4029 | Comparison(lt); |
| 4030 | break; |
| 4031 | |
| 4032 | case Token::GT: |
| 4033 | Comparison(gt); |
| 4034 | break; |
| 4035 | |
| 4036 | case Token::LTE: |
| 4037 | Comparison(le); |
| 4038 | break; |
| 4039 | |
| 4040 | case Token::GTE: |
| 4041 | Comparison(ge); |
| 4042 | break; |
| 4043 | |
| 4044 | case Token::EQ_STRICT: |
| 4045 | Comparison(eq, true); |
| 4046 | break; |
| 4047 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4048 | case Token::IN: { |
| 4049 | Result arg_count = allocator_->Allocate(r0); |
| 4050 | ASSERT(arg_count.is_valid()); |
| 4051 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4052 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4053 | CALL_JS, |
| 4054 | &arg_count, |
| 4055 | 2); |
| 4056 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4057 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4058 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4060 | case Token::INSTANCEOF: { |
| 4061 | Result arg_count = allocator_->Allocate(r0); |
| 4062 | ASSERT(arg_count.is_valid()); |
| 4063 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4064 | Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF, |
| 4065 | CALL_JS, |
| 4066 | &arg_count, |
| 4067 | 2); |
| 4068 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4069 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4070 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4071 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4072 | |
| 4073 | default: |
| 4074 | UNREACHABLE(); |
| 4075 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4076 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4077 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4081 | #ifdef DEBUG |
| 4082 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4083 | #endif |
| 4084 | |
| 4085 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4086 | #undef __ |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4087 | #define __ masm-> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4089 | Handle<String> Reference::GetName() { |
| 4090 | ASSERT(type_ == NAMED); |
| 4091 | Property* property = expression_->AsProperty(); |
| 4092 | if (property == NULL) { |
| 4093 | // Global variable reference treated as a named property reference. |
| 4094 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4095 | ASSERT(proxy->AsVariable() != NULL); |
| 4096 | ASSERT(proxy->AsVariable()->is_global()); |
| 4097 | return proxy->name(); |
| 4098 | } else { |
| 4099 | Literal* raw_name = property->key()->AsLiteral(); |
| 4100 | ASSERT(raw_name != NULL); |
| 4101 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4102 | } |
| 4103 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4104 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4105 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame^] | 4106 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4107 | ASSERT(cgen_->in_spilled_code()); |
| 4108 | cgen_->set_in_spilled_code(false); |
| 4109 | GetValue(typeof_state); |
| 4110 | cgen_->frame()->SpillAll(); |
| 4111 | cgen_->set_in_spilled_code(true); |
| 4112 | } |
| 4113 | |
| 4114 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4115 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4116 | ASSERT(!cgen_->in_spilled_code()); |
| 4117 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4118 | ASSERT(!is_illegal()); |
| 4119 | ASSERT(!cgen_->has_cc()); |
| 4120 | MacroAssembler* masm = cgen_->masm(); |
| 4121 | Property* property = expression_->AsProperty(); |
| 4122 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4123 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | switch (type_) { |
| 4127 | case SLOT: { |
| 4128 | Comment cmnt(masm, "[ Load from Slot"); |
| 4129 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4130 | ASSERT(slot != NULL); |
| 4131 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4132 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4135 | case NAMED: { |
| 4136 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4137 | // distinction between expressions in a typeof and not in a typeof. If |
| 4138 | // there is a chance that reference errors can be thrown below, we |
| 4139 | // must distinguish between the two kinds of loads (typeof expression |
| 4140 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4141 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4142 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4143 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4144 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4145 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4146 | // Setup the name register. |
| 4147 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4148 | ASSERT(name_reg.is_valid()); |
| 4149 | __ mov(name_reg.reg(), Operand(name)); |
| 4150 | ASSERT(var == NULL || var->is_global()); |
| 4151 | RelocInfo::Mode rmode = (var == NULL) |
| 4152 | ? RelocInfo::CODE_TARGET |
| 4153 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4154 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4155 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4156 | break; |
| 4157 | } |
| 4158 | |
| 4159 | case KEYED: { |
| 4160 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4161 | // 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] | 4162 | |
| 4163 | // TODO(181): Implement inlined version of array indexing once |
| 4164 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4165 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4166 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4167 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4168 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4169 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4170 | ASSERT(var == NULL || var->is_global()); |
| 4171 | RelocInfo::Mode rmode = (var == NULL) |
| 4172 | ? RelocInfo::CODE_TARGET |
| 4173 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4174 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4175 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4176 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | default: |
| 4180 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4185 | void Reference::SetValue(InitState init_state) { |
| 4186 | ASSERT(!is_illegal()); |
| 4187 | ASSERT(!cgen_->has_cc()); |
| 4188 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4189 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4190 | Property* property = expression_->AsProperty(); |
| 4191 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4192 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4193 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4194 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4195 | switch (type_) { |
| 4196 | case SLOT: { |
| 4197 | Comment cmnt(masm, "[ Store to Slot"); |
| 4198 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4199 | ASSERT(slot != NULL); |
| 4200 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4201 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4202 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4203 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4204 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4205 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4206 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4207 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4208 | if (init_state == CONST_INIT) { |
| 4209 | // Same as the case for a normal store, but ignores attribute |
| 4210 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4211 | // const properties (introduced via eval("const foo = (some |
| 4212 | // expr);")). Also, uses the current function context instead of |
| 4213 | // the top context. |
| 4214 | // |
| 4215 | // Note that we must declare the foo upon entry of eval(), via a |
| 4216 | // context slot declaration, but we cannot initialize it at the |
| 4217 | // same time, because the const declaration may be at the end of |
| 4218 | // the eval code (sigh...) and the const variable may have been |
| 4219 | // used before (where its value is 'undefined'). Thus, we can only |
| 4220 | // do the initialization when we actually encounter the expression |
| 4221 | // and when the expression operands are defined and valid, and |
| 4222 | // thus we need the split into 2 operations: declaration of the |
| 4223 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4224 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4225 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4226 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4227 | } |
| 4228 | // Storing a variable must keep the (new) value on the expression |
| 4229 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4230 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4231 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4232 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4233 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4234 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4235 | JumpTarget exit(cgen_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4236 | if (init_state == CONST_INIT) { |
| 4237 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4238 | // Only the first const initialization must be executed (the slot |
| 4239 | // still contains 'the hole' value). When the assignment is |
| 4240 | // executed, the code is identical to a normal store (see below). |
| 4241 | Comment cmnt(masm, "[ Init const"); |
| 4242 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4243 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4244 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4245 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4246 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4247 | // We must execute the store. Storing a variable must keep the |
| 4248 | // (new) value on the stack. This is necessary for compiling |
| 4249 | // assignment expressions. |
| 4250 | // |
| 4251 | // Note: We will reach here even with slot->var()->mode() == |
| 4252 | // Variable::CONST because of const declarations which will |
| 4253 | // initialize consts to 'the hole' value and by doing so, end up |
| 4254 | // calling this code. r2 may be loaded with context; used below in |
| 4255 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4256 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4257 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4258 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4259 | if (slot->type() == Slot::CONTEXT) { |
| 4260 | // Skip write barrier if the written value is a smi. |
| 4261 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4262 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4263 | // r2 is loaded with context when calling SlotOperand above. |
| 4264 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4265 | __ mov(r3, Operand(offset)); |
| 4266 | __ RecordWrite(r2, r3, r1); |
| 4267 | } |
| 4268 | // If we definitely did not jump over the assignment, we do not need |
| 4269 | // to bind the exit label. Doing so can defeat peephole |
| 4270 | // optimization. |
| 4271 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4272 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
| 4275 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4278 | case NAMED: { |
| 4279 | Comment cmnt(masm, "[ Store to named Property"); |
| 4280 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4281 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4282 | Handle<String> name(GetName()); |
| 4283 | |
| 4284 | Result value = cgen_->allocator()->Allocate(r0); |
| 4285 | ASSERT(value.is_valid()); |
| 4286 | frame->EmitPop(value.reg()); |
| 4287 | |
| 4288 | // Setup the name register. |
| 4289 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4290 | ASSERT(property_name.is_valid()); |
| 4291 | __ mov(property_name.reg(), Operand(name)); |
| 4292 | Result answer = frame->CallCodeObject(ic, |
| 4293 | RelocInfo::CODE_TARGET, |
| 4294 | &value, |
| 4295 | &property_name, |
| 4296 | 0); |
| 4297 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4298 | break; |
| 4299 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4300 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4301 | case KEYED: { |
| 4302 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4303 | Property* property = expression_->AsProperty(); |
| 4304 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4305 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Call IC code. |
| 4308 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4309 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4310 | Result value = cgen_->allocator()->Allocate(r0); |
| 4311 | ASSERT(value.is_valid()); |
| 4312 | frame->EmitPop(value.reg()); // value |
| 4313 | Result result = |
| 4314 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4315 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4316 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4317 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4318 | |
| 4319 | default: |
| 4320 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | |
| 4325 | void GetPropertyStub::Generate(MacroAssembler* masm) { |
| 4326 | // sp[0]: key |
| 4327 | // sp[1]: receiver |
| 4328 | Label slow, fast; |
| 4329 | // Get the key and receiver object from the stack. |
| 4330 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 4331 | // Check that the key is a smi. |
| 4332 | __ tst(r0, Operand(kSmiTagMask)); |
| 4333 | __ b(ne, &slow); |
| 4334 | __ mov(r0, Operand(r0, ASR, kSmiTagSize)); |
| 4335 | // Check that the object isn't a smi. |
| 4336 | __ tst(r1, Operand(kSmiTagMask)); |
| 4337 | __ b(eq, &slow); |
| 4338 | |
| 4339 | // Check that the object is some kind of JS object EXCEPT JS Value type. |
| 4340 | // In the case that the object is a value-wrapper object, |
| 4341 | // we enter the runtime system to make sure that indexing into string |
| 4342 | // objects work as intended. |
| 4343 | ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE); |
| 4344 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4345 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4346 | __ cmp(r2, Operand(JS_OBJECT_TYPE)); |
| 4347 | __ b(lt, &slow); |
| 4348 | |
| 4349 | // Get the elements array of the object. |
| 4350 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 4351 | // Check that the object is in fast mode (not dictionary). |
| 4352 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4353 | __ cmp(r3, Operand(Factory::hash_table_map())); |
| 4354 | __ b(eq, &slow); |
| 4355 | // Check that the key (index) is within bounds. |
| 4356 | __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset)); |
| 4357 | __ cmp(r0, Operand(r3)); |
| 4358 | __ b(lo, &fast); |
| 4359 | |
| 4360 | // Slow case: Push extra copies of the arguments (2). |
| 4361 | __ bind(&slow); |
| 4362 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 4363 | __ stm(db_w, sp, r0.bit() | r1.bit()); |
| 4364 | // Do tail-call to runtime routine. |
| 4365 | __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2); |
| 4366 | |
| 4367 | // Fast case: Do the load. |
| 4368 | __ bind(&fast); |
| 4369 | __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4370 | __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2)); |
| 4371 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 4372 | // In case the loaded value is the_hole we have to consult GetProperty |
| 4373 | // to ensure the prototype chain is searched. |
| 4374 | __ b(eq, &slow); |
| 4375 | |
| 4376 | __ StubReturn(1); |
| 4377 | } |
| 4378 | |
| 4379 | |
| 4380 | void SetPropertyStub::Generate(MacroAssembler* masm) { |
| 4381 | // r0 : value |
| 4382 | // sp[0] : key |
| 4383 | // sp[1] : receiver |
| 4384 | |
| 4385 | Label slow, fast, array, extra, exit; |
| 4386 | // Get the key and the object from the stack. |
| 4387 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver |
| 4388 | // Check that the key is a smi. |
| 4389 | __ tst(r1, Operand(kSmiTagMask)); |
| 4390 | __ b(ne, &slow); |
| 4391 | // Check that the object isn't a smi. |
| 4392 | __ tst(r3, Operand(kSmiTagMask)); |
| 4393 | __ b(eq, &slow); |
| 4394 | // Get the type of the object from its map. |
| 4395 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4396 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4397 | // Check if the object is a JS array or not. |
| 4398 | __ cmp(r2, Operand(JS_ARRAY_TYPE)); |
| 4399 | __ b(eq, &array); |
| 4400 | // Check that the object is some kind of JS object. |
| 4401 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4402 | __ b(lt, &slow); |
| 4403 | |
| 4404 | |
| 4405 | // Object case: Check key against length in the elements array. |
| 4406 | __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 4407 | // Check that the object is in fast mode (not dictionary). |
| 4408 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4409 | __ cmp(r2, Operand(Factory::hash_table_map())); |
| 4410 | __ b(eq, &slow); |
| 4411 | // Untag the key (for checking against untagged length in the fixed array). |
| 4412 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 4413 | // Compute address to store into and check array bounds. |
| 4414 | __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4415 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2)); |
| 4416 | __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset)); |
| 4417 | __ cmp(r1, Operand(ip)); |
| 4418 | __ b(lo, &fast); |
| 4419 | |
| 4420 | |
| 4421 | // Slow case: Push extra copies of the arguments (3). |
| 4422 | __ bind(&slow); |
| 4423 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object |
| 4424 | __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit()); |
| 4425 | // Do tail-call to runtime routine. |
| 4426 | __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3); |
| 4427 | |
| 4428 | |
| 4429 | // Extra capacity case: Check if there is extra capacity to |
| 4430 | // perform the store and update the length. Used for adding one |
| 4431 | // element to the array by writing to array[array.length]. |
| 4432 | // r0 == value, r1 == key, r2 == elements, r3 == object |
| 4433 | __ bind(&extra); |
| 4434 | __ b(ne, &slow); // do not leave holes in the array |
| 4435 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag |
| 4436 | __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset)); |
| 4437 | __ cmp(r1, Operand(ip)); |
| 4438 | __ b(hs, &slow); |
| 4439 | __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag |
| 4440 | __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment |
| 4441 | __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 4442 | __ mov(r3, Operand(r2)); |
| 4443 | // NOTE: Computing the address to store into must take the fact |
| 4444 | // that the key has been incremented into account. |
| 4445 | int displacement = Array::kHeaderSize - kHeapObjectTag - |
| 4446 | ((1 << kSmiTagSize) * 2); |
| 4447 | __ add(r2, r2, Operand(displacement)); |
| 4448 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4449 | __ b(&fast); |
| 4450 | |
| 4451 | |
| 4452 | // Array case: Get the length and the elements array from the JS |
| 4453 | // array. Check that the array is in fast mode; if it is the |
| 4454 | // length is always a smi. |
| 4455 | // r0 == value, r3 == object |
| 4456 | __ bind(&array); |
| 4457 | __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 4458 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 4459 | __ cmp(r1, Operand(Factory::hash_table_map())); |
| 4460 | __ b(eq, &slow); |
| 4461 | |
| 4462 | // Check the key against the length in the array, compute the |
| 4463 | // address to store into and fall through to fast case. |
| 4464 | __ ldr(r1, MemOperand(sp)); |
| 4465 | // r0 == value, r1 == key, r2 == elements, r3 == object. |
| 4466 | __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 4467 | __ cmp(r1, Operand(ip)); |
| 4468 | __ b(hs, &extra); |
| 4469 | __ mov(r3, Operand(r2)); |
| 4470 | __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4471 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4472 | |
| 4473 | |
| 4474 | // Fast case: Do the store. |
| 4475 | // r0 == value, r2 == address to store into, r3 == elements |
| 4476 | __ bind(&fast); |
| 4477 | __ str(r0, MemOperand(r2)); |
| 4478 | // Skip write barrier if the written value is a smi. |
| 4479 | __ tst(r0, Operand(kSmiTagMask)); |
| 4480 | __ b(eq, &exit); |
| 4481 | // Update write barrier for the elements array address. |
| 4482 | __ sub(r1, r2, Operand(r3)); |
| 4483 | __ RecordWrite(r3, r1, r2); |
| 4484 | __ bind(&exit); |
| 4485 | __ StubReturn(1); |
| 4486 | } |
| 4487 | |
| 4488 | |
| 4489 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 4490 | // r1 : x |
| 4491 | // r0 : y |
| 4492 | // result : r0 |
| 4493 | |
| 4494 | switch (op_) { |
| 4495 | case Token::ADD: { |
| 4496 | Label slow, exit; |
| 4497 | // fast path |
| 4498 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4499 | __ add(r0, r1, Operand(r0), SetCC); // add y optimistically |
| 4500 | // go slow-path in case of overflow |
| 4501 | __ b(vs, &slow); |
| 4502 | // go slow-path in case of non-smi operands |
| 4503 | ASSERT(kSmiTag == 0); // adjust code below |
| 4504 | __ tst(r2, Operand(kSmiTagMask)); |
| 4505 | __ b(eq, &exit); |
| 4506 | // slow path |
| 4507 | __ bind(&slow); |
| 4508 | __ sub(r0, r0, Operand(r1)); // revert optimistic add |
| 4509 | __ push(r1); |
| 4510 | __ push(r0); |
| 4511 | __ mov(r0, Operand(1)); // set number of arguments |
| 4512 | __ InvokeBuiltin(Builtins::ADD, JUMP_JS); |
| 4513 | // done |
| 4514 | __ bind(&exit); |
| 4515 | break; |
| 4516 | } |
| 4517 | |
| 4518 | case Token::SUB: { |
| 4519 | Label slow, exit; |
| 4520 | // fast path |
| 4521 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4522 | __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically |
| 4523 | // go slow-path in case of overflow |
| 4524 | __ b(vs, &slow); |
| 4525 | // go slow-path in case of non-smi operands |
| 4526 | ASSERT(kSmiTag == 0); // adjust code below |
| 4527 | __ tst(r2, Operand(kSmiTagMask)); |
| 4528 | __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result |
| 4529 | __ b(eq, &exit); |
| 4530 | // slow path |
| 4531 | __ bind(&slow); |
| 4532 | __ push(r1); |
| 4533 | __ push(r0); |
| 4534 | __ mov(r0, Operand(1)); // set number of arguments |
| 4535 | __ InvokeBuiltin(Builtins::SUB, JUMP_JS); |
| 4536 | // done |
| 4537 | __ bind(&exit); |
| 4538 | break; |
| 4539 | } |
| 4540 | |
| 4541 | case Token::MUL: { |
| 4542 | Label slow, exit; |
| 4543 | // tag check |
| 4544 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4545 | ASSERT(kSmiTag == 0); // adjust code below |
| 4546 | __ tst(r2, Operand(kSmiTagMask)); |
| 4547 | __ b(ne, &slow); |
| 4548 | // remove tag from one operand (but keep sign), so that result is smi |
| 4549 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
| 4550 | // do multiplication |
| 4551 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1 |
| 4552 | // go slow on overflows (overflow bit is not set) |
| 4553 | __ mov(ip, Operand(r3, ASR, 31)); |
| 4554 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 4555 | __ b(ne, &slow); |
| 4556 | // go slow on zero result to handle -0 |
| 4557 | __ tst(r3, Operand(r3)); |
| 4558 | __ mov(r0, Operand(r3), LeaveCC, ne); |
| 4559 | __ b(ne, &exit); |
| 4560 | // slow case |
| 4561 | __ bind(&slow); |
| 4562 | __ push(r1); |
| 4563 | __ push(r0); |
| 4564 | __ mov(r0, Operand(1)); // set number of arguments |
| 4565 | __ InvokeBuiltin(Builtins::MUL, JUMP_JS); |
| 4566 | // done |
| 4567 | __ bind(&exit); |
| 4568 | break; |
| 4569 | } |
| 4570 | |
| 4571 | case Token::BIT_OR: |
| 4572 | case Token::BIT_AND: |
| 4573 | case Token::BIT_XOR: { |
| 4574 | Label slow, exit; |
| 4575 | // tag check |
| 4576 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4577 | ASSERT(kSmiTag == 0); // adjust code below |
| 4578 | __ tst(r2, Operand(kSmiTagMask)); |
| 4579 | __ b(ne, &slow); |
| 4580 | switch (op_) { |
| 4581 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 4582 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 4583 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 4584 | default: UNREACHABLE(); |
| 4585 | } |
| 4586 | __ b(&exit); |
| 4587 | __ bind(&slow); |
| 4588 | __ push(r1); // restore stack |
| 4589 | __ push(r0); |
| 4590 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4591 | switch (op_) { |
| 4592 | case Token::BIT_OR: |
| 4593 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 4594 | break; |
| 4595 | case Token::BIT_AND: |
| 4596 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 4597 | break; |
| 4598 | case Token::BIT_XOR: |
| 4599 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 4600 | break; |
| 4601 | default: |
| 4602 | UNREACHABLE(); |
| 4603 | } |
| 4604 | __ bind(&exit); |
| 4605 | break; |
| 4606 | } |
| 4607 | |
| 4608 | case Token::SHL: |
| 4609 | case Token::SHR: |
| 4610 | case Token::SAR: { |
| 4611 | Label slow, exit; |
| 4612 | // tag check |
| 4613 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4614 | ASSERT(kSmiTag == 0); // adjust code below |
| 4615 | __ tst(r2, Operand(kSmiTagMask)); |
| 4616 | __ b(ne, &slow); |
| 4617 | // remove tags from operands (but keep sign) |
| 4618 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 4619 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 4620 | // use only the 5 least significant bits of the shift count |
| 4621 | __ and_(r2, r2, Operand(0x1f)); |
| 4622 | // perform operation |
| 4623 | switch (op_) { |
| 4624 | case Token::SAR: |
| 4625 | __ mov(r3, Operand(r3, ASR, r2)); |
| 4626 | // no checks of result necessary |
| 4627 | break; |
| 4628 | |
| 4629 | case Token::SHR: |
| 4630 | __ mov(r3, Operand(r3, LSR, r2)); |
| 4631 | // check that the *unsigned* result fits in a smi |
| 4632 | // neither of the two high-order bits can be set: |
| 4633 | // - 0x80000000: high bit would be lost when smi tagging |
| 4634 | // - 0x40000000: this number would convert to negative when |
| 4635 | // smi tagging these two cases can only happen with shifts |
| 4636 | // by 0 or 1 when handed a valid smi |
| 4637 | __ and_(r2, r3, Operand(0xc0000000), SetCC); |
| 4638 | __ b(ne, &slow); |
| 4639 | break; |
| 4640 | |
| 4641 | case Token::SHL: |
| 4642 | __ mov(r3, Operand(r3, LSL, r2)); |
| 4643 | // check that the *signed* result fits in a smi |
| 4644 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 4645 | __ b(mi, &slow); |
| 4646 | break; |
| 4647 | |
| 4648 | default: UNREACHABLE(); |
| 4649 | } |
| 4650 | // tag result and store it in r0 |
| 4651 | ASSERT(kSmiTag == 0); // adjust code below |
| 4652 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
| 4653 | __ b(&exit); |
| 4654 | // slow case |
| 4655 | __ bind(&slow); |
| 4656 | __ push(r1); // restore stack |
| 4657 | __ push(r0); |
| 4658 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4659 | switch (op_) { |
| 4660 | case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break; |
| 4661 | case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break; |
| 4662 | case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break; |
| 4663 | default: UNREACHABLE(); |
| 4664 | } |
| 4665 | __ bind(&exit); |
| 4666 | break; |
| 4667 | } |
| 4668 | |
| 4669 | default: UNREACHABLE(); |
| 4670 | } |
| 4671 | __ Ret(); |
| 4672 | } |
| 4673 | |
| 4674 | |
| 4675 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 4676 | Label within_limit; |
| 4677 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 4678 | __ ldr(ip, MemOperand(ip)); |
| 4679 | __ cmp(sp, Operand(ip)); |
| 4680 | __ b(hs, &within_limit); |
| 4681 | // Do tail-call to runtime routine. |
| 4682 | __ push(r0); |
| 4683 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 4684 | __ bind(&within_limit); |
| 4685 | |
| 4686 | __ StubReturn(1); |
| 4687 | } |
| 4688 | |
| 4689 | |
| 4690 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 4691 | Label undo; |
| 4692 | Label slow; |
| 4693 | Label done; |
| 4694 | |
| 4695 | // Enter runtime system if the value is not a smi. |
| 4696 | __ tst(r0, Operand(kSmiTagMask)); |
| 4697 | __ b(ne, &slow); |
| 4698 | |
| 4699 | // Enter runtime system if the value of the expression is zero |
| 4700 | // to make sure that we switch between 0 and -0. |
| 4701 | __ cmp(r0, Operand(0)); |
| 4702 | __ b(eq, &slow); |
| 4703 | |
| 4704 | // The value of the expression is a smi that is not zero. Try |
| 4705 | // optimistic subtraction '0 - value'. |
| 4706 | __ rsb(r1, r0, Operand(0), SetCC); |
| 4707 | __ b(vs, &slow); |
| 4708 | |
| 4709 | // If result is a smi we are done. |
| 4710 | __ tst(r1, Operand(kSmiTagMask)); |
| 4711 | __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result |
| 4712 | __ b(eq, &done); |
| 4713 | |
| 4714 | // Enter runtime system. |
| 4715 | __ bind(&slow); |
| 4716 | __ push(r0); |
| 4717 | __ mov(r0, Operand(0)); // set number of arguments |
| 4718 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 4719 | |
| 4720 | __ bind(&done); |
| 4721 | __ StubReturn(1); |
| 4722 | } |
| 4723 | |
| 4724 | |
| 4725 | void InvokeBuiltinStub::Generate(MacroAssembler* masm) { |
| 4726 | __ push(r0); |
| 4727 | __ mov(r0, Operand(0)); // set number of arguments |
| 4728 | switch (kind_) { |
| 4729 | case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break; |
| 4730 | case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break; |
| 4731 | case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break; |
| 4732 | default: UNREACHABLE(); |
| 4733 | } |
| 4734 | __ StubReturn(argc_); |
| 4735 | } |
| 4736 | |
| 4737 | |
| 4738 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 4739 | // r0 holds exception |
| 4740 | ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code |
| 4741 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4742 | __ ldr(sp, MemOperand(r3)); |
| 4743 | __ pop(r2); // pop next in chain |
| 4744 | __ str(r2, MemOperand(r3)); |
| 4745 | // restore parameter- and frame-pointer and pop state. |
| 4746 | __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit()); |
| 4747 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4748 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4749 | __ cmp(fp, Operand(0)); |
| 4750 | // Set cp to NULL if fp is NULL. |
| 4751 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4752 | // Restore cp otherwise. |
| 4753 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 4754 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 4755 | __ pop(pc); |
| 4756 | } |
| 4757 | |
| 4758 | |
| 4759 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
| 4760 | // Fetch top stack handler. |
| 4761 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4762 | __ ldr(r3, MemOperand(r3)); |
| 4763 | |
| 4764 | // Unwind the handlers until the ENTRY handler is found. |
| 4765 | Label loop, done; |
| 4766 | __ bind(&loop); |
| 4767 | // Load the type of the current stack handler. |
| 4768 | const int kStateOffset = StackHandlerConstants::kAddressDisplacement + |
| 4769 | StackHandlerConstants::kStateOffset; |
| 4770 | __ ldr(r2, MemOperand(r3, kStateOffset)); |
| 4771 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 4772 | __ b(eq, &done); |
| 4773 | // Fetch the next handler in the list. |
| 4774 | const int kNextOffset = StackHandlerConstants::kAddressDisplacement + |
| 4775 | StackHandlerConstants::kNextOffset; |
| 4776 | __ ldr(r3, MemOperand(r3, kNextOffset)); |
| 4777 | __ jmp(&loop); |
| 4778 | __ bind(&done); |
| 4779 | |
| 4780 | // Set the top handler address to next handler past the current ENTRY handler. |
| 4781 | __ ldr(r0, MemOperand(r3, kNextOffset)); |
| 4782 | __ mov(r2, Operand(ExternalReference(Top::k_handler_address))); |
| 4783 | __ str(r0, MemOperand(r2)); |
| 4784 | |
| 4785 | // Set external caught exception to false. |
| 4786 | __ mov(r0, Operand(false)); |
| 4787 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 4788 | __ mov(r2, Operand(external_caught)); |
| 4789 | __ str(r0, MemOperand(r2)); |
| 4790 | |
| 4791 | // Set pending exception and r0 to out of memory exception. |
| 4792 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4793 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4794 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 4795 | __ str(r0, MemOperand(r2)); |
| 4796 | |
| 4797 | // Restore the stack to the address of the ENTRY handler |
| 4798 | __ mov(sp, Operand(r3)); |
| 4799 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4800 | // Stack layout at this point. See also PushTryHandler |
| 4801 | // r3, sp -> next handler |
| 4802 | // state (ENTRY) |
| 4803 | // pp |
| 4804 | // fp |
| 4805 | // lr |
| 4806 | |
| 4807 | // Discard ENTRY state (r2 is not used), and restore parameter- |
| 4808 | // and frame-pointer and pop state. |
| 4809 | __ 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] | 4810 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4811 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4812 | __ cmp(fp, Operand(0)); |
| 4813 | // Set cp to NULL if fp is NULL. |
| 4814 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4815 | // Restore cp otherwise. |
| 4816 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 4817 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 4818 | __ pop(pc); |
| 4819 | } |
| 4820 | |
| 4821 | |
| 4822 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 4823 | Label* throw_normal_exception, |
| 4824 | Label* throw_out_of_memory_exception, |
| 4825 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4826 | bool do_gc, |
| 4827 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4828 | // r0: result parameter for PerformGC, if any |
| 4829 | // r4: number of arguments including receiver (C callee-saved) |
| 4830 | // r5: pointer to builtin function (C callee-saved) |
| 4831 | // r6: pointer to the first argument (C callee-saved) |
| 4832 | |
| 4833 | if (do_gc) { |
| 4834 | // Passing r0. |
| 4835 | __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY); |
| 4836 | } |
| 4837 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4838 | ExternalReference scope_depth = |
| 4839 | ExternalReference::heap_always_allocate_scope_depth(); |
| 4840 | if (always_allocate) { |
| 4841 | __ mov(r0, Operand(scope_depth)); |
| 4842 | __ ldr(r1, MemOperand(r0)); |
| 4843 | __ add(r1, r1, Operand(1)); |
| 4844 | __ str(r1, MemOperand(r0)); |
| 4845 | } |
| 4846 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4847 | // Call C built-in. |
| 4848 | // r0 = argc, r1 = argv |
| 4849 | __ mov(r0, Operand(r4)); |
| 4850 | __ mov(r1, Operand(r6)); |
| 4851 | |
| 4852 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 4853 | // frames, we need to know where the return address is. Right now, |
| 4854 | // we push it on the stack to be able to find it again, but we never |
| 4855 | // restore from it in case of changes, which makes it impossible to |
| 4856 | // support moving the C entry code stub. This should be fixed, but currently |
| 4857 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 4858 | // sequence that it is not moving ever. |
| 4859 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 4860 | __ push(lr); |
| 4861 | #if !defined(__arm__) |
| 4862 | // Notify the simulator of the transition to C code. |
| 4863 | __ swi(assembler::arm::call_rt_r5); |
| 4864 | #else /* !defined(__arm__) */ |
| 4865 | __ mov(pc, Operand(r5)); |
| 4866 | #endif /* !defined(__arm__) */ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4867 | |
| 4868 | if (always_allocate) { |
| 4869 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 4870 | // though (contain the result). |
| 4871 | __ mov(r2, Operand(scope_depth)); |
| 4872 | __ ldr(r3, MemOperand(r2)); |
| 4873 | __ sub(r3, r3, Operand(1)); |
| 4874 | __ str(r3, MemOperand(r2)); |
| 4875 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4876 | |
| 4877 | // check for failure result |
| 4878 | Label failure_returned; |
| 4879 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 4880 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 4881 | __ add(r2, r0, Operand(1)); |
| 4882 | __ tst(r2, Operand(kFailureTagMask)); |
| 4883 | __ b(eq, &failure_returned); |
| 4884 | |
| 4885 | // Exit C frame and return. |
| 4886 | // r0:r1: result |
| 4887 | // sp: stack pointer |
| 4888 | // fp: frame pointer |
| 4889 | // pp: caller's parameter pointer pp (restored as C callee-saved) |
| 4890 | __ LeaveExitFrame(frame_type); |
| 4891 | |
| 4892 | // check if we should retry or throw exception |
| 4893 | Label retry; |
| 4894 | __ bind(&failure_returned); |
| 4895 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 4896 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 4897 | __ b(eq, &retry); |
| 4898 | |
| 4899 | Label continue_exception; |
| 4900 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 4901 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 4902 | __ b(ne, &continue_exception); |
| 4903 | |
| 4904 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4905 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4906 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4907 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4908 | __ ldr(r0, MemOperand(ip)); |
| 4909 | __ str(r3, MemOperand(ip)); |
| 4910 | |
| 4911 | __ bind(&continue_exception); |
| 4912 | // Special handling of out of memory exception. |
| 4913 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4914 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4915 | __ b(eq, throw_out_of_memory_exception); |
| 4916 | |
| 4917 | // Handle normal exception. |
| 4918 | __ jmp(throw_normal_exception); |
| 4919 | |
| 4920 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 4921 | } |
| 4922 | |
| 4923 | |
| 4924 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 4925 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 4926 | // r0: number of arguments including receiver |
| 4927 | // r1: pointer to builtin function |
| 4928 | // fp: frame pointer (restored after C call) |
| 4929 | // sp: stack pointer (restored as callee's pp after C call) |
| 4930 | // cp: current context (C callee-saved) |
| 4931 | // pp: caller's parameter pointer pp (C callee-saved) |
| 4932 | |
| 4933 | // NOTE: Invocations of builtins may return failure objects |
| 4934 | // instead of a proper result. The builtin entry handles |
| 4935 | // this by performing a garbage collection and retrying the |
| 4936 | // builtin once. |
| 4937 | |
| 4938 | StackFrame::Type frame_type = is_debug_break |
| 4939 | ? StackFrame::EXIT_DEBUG |
| 4940 | : StackFrame::EXIT; |
| 4941 | |
| 4942 | // Enter the exit frame that transitions from JavaScript to C++. |
| 4943 | __ EnterExitFrame(frame_type); |
| 4944 | |
| 4945 | // r4: number of arguments (C callee-saved) |
| 4946 | // r5: pointer to builtin function (C callee-saved) |
| 4947 | // r6: pointer to first argument (C callee-saved) |
| 4948 | |
| 4949 | Label throw_out_of_memory_exception; |
| 4950 | Label throw_normal_exception; |
| 4951 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4952 | // Call into the runtime system. Collect garbage before the call if |
| 4953 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4954 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4955 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4956 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 4957 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4958 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4959 | &throw_out_of_memory_exception, |
| 4960 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4961 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4962 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4963 | |
| 4964 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4965 | GenerateCore(masm, |
| 4966 | &throw_normal_exception, |
| 4967 | &throw_out_of_memory_exception, |
| 4968 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4969 | true, |
| 4970 | false); |
| 4971 | |
| 4972 | // Do full GC and retry runtime call one final time. |
| 4973 | Failure* failure = Failure::InternalError(); |
| 4974 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 4975 | GenerateCore(masm, |
| 4976 | &throw_normal_exception, |
| 4977 | &throw_out_of_memory_exception, |
| 4978 | frame_type, |
| 4979 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4980 | true); |
| 4981 | |
| 4982 | __ bind(&throw_out_of_memory_exception); |
| 4983 | GenerateThrowOutOfMemory(masm); |
| 4984 | // control flow for generated will not return. |
| 4985 | |
| 4986 | __ bind(&throw_normal_exception); |
| 4987 | GenerateThrowTOS(masm); |
| 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 4992 | // r0: code entry |
| 4993 | // r1: function |
| 4994 | // r2: receiver |
| 4995 | // r3: argc |
| 4996 | // [sp+0]: argv |
| 4997 | |
| 4998 | Label invoke, exit; |
| 4999 | |
| 5000 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 5001 | // No need to save register-passed args |
| 5002 | // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr |
| 5003 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 5004 | |
| 5005 | // Get address of argv, see stm above. |
| 5006 | // r0: code entry |
| 5007 | // r1: function |
| 5008 | // r2: receiver |
| 5009 | // r3: argc |
| 5010 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 5011 | __ ldr(r4, MemOperand(r4)); // argv |
| 5012 | |
| 5013 | // Push a frame with special values setup to mark it as an entry frame. |
| 5014 | // r0: code entry |
| 5015 | // r1: function |
| 5016 | // r2: receiver |
| 5017 | // r3: argc |
| 5018 | // r4: argv |
| 5019 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5020 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 5021 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 5022 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5023 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5024 | __ ldr(r5, MemOperand(r5)); |
| 5025 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5026 | |
| 5027 | // Setup frame pointer for the frame to be pushed. |
| 5028 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5029 | |
| 5030 | // Call a faked try-block that does the invoke. |
| 5031 | __ bl(&invoke); |
| 5032 | |
| 5033 | // Caught exception: Store result (exception) in the pending |
| 5034 | // exception field in the JSEnv and return a failure sentinel. |
| 5035 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 5036 | // 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] | 5037 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5038 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5039 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5040 | __ b(&exit); |
| 5041 | |
| 5042 | // Invoke: Link this frame into the handler chain. |
| 5043 | __ bind(&invoke); |
| 5044 | // Must preserve r0-r4, r5-r7 are available. |
| 5045 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 5046 | // If an exception not caught by another handler occurs, this handler returns |
| 5047 | // control to the code after the bl(&invoke) above, which restores all |
| 5048 | // kCalleeSaved registers (including cp, pp and fp) to their saved values |
| 5049 | // before returning a failure to C. |
| 5050 | |
| 5051 | // Clear any pending exceptions. |
| 5052 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5053 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5054 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5055 | __ str(r5, MemOperand(ip)); |
| 5056 | |
| 5057 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5058 | // Notice that we cannot store a reference to the trampoline code directly in |
| 5059 | // this stub, because runtime stubs are not traversed when doing GC. |
| 5060 | |
| 5061 | // Expected registers by Builtins::JSEntryTrampoline |
| 5062 | // r0: code entry |
| 5063 | // r1: function |
| 5064 | // r2: receiver |
| 5065 | // r3: argc |
| 5066 | // r4: argv |
| 5067 | if (is_construct) { |
| 5068 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 5069 | __ mov(ip, Operand(construct_entry)); |
| 5070 | } else { |
| 5071 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 5072 | __ mov(ip, Operand(entry)); |
| 5073 | } |
| 5074 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5075 | |
| 5076 | // Branch and link to JSEntryTrampoline |
| 5077 | __ mov(lr, Operand(pc)); |
| 5078 | __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 5079 | |
| 5080 | // Unlink this frame from the handler chain. When reading the |
| 5081 | // address of the next handler, there is no need to use the address |
| 5082 | // displacement since the current stack pointer (sp) points directly |
| 5083 | // to the stack handler. |
| 5084 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5085 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5086 | __ str(r3, MemOperand(ip)); |
| 5087 | // No need to restore registers |
| 5088 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5089 | |
| 5090 | __ bind(&exit); // r0 holds result |
| 5091 | // Restore the top frame descriptors from the stack. |
| 5092 | __ pop(r3); |
| 5093 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5094 | __ str(r3, MemOperand(ip)); |
| 5095 | |
| 5096 | // Reset the stack to the callee saved registers. |
| 5097 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5098 | |
| 5099 | // Restore callee-saved registers and return. |
| 5100 | #ifdef DEBUG |
| 5101 | if (FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 5102 | #endif |
| 5103 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5104 | } |
| 5105 | |
| 5106 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5107 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5108 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5109 | Label adaptor; |
| 5110 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5111 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5112 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5113 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5114 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5115 | // Nothing to do: The formal number of parameters has already been |
| 5116 | // passed in register r0 by calling function. Just return it. |
| 5117 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5118 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5119 | // Arguments adaptor case: Read the arguments length from the |
| 5120 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5121 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5122 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5123 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5127 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5128 | // The displacement is the offset of the last parameter (if any) |
| 5129 | // relative to the frame pointer. |
| 5130 | static const int kDisplacement = |
| 5131 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5132 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5133 | // Check that the key is a smi. |
| 5134 | Label slow; |
| 5135 | __ tst(r1, Operand(kSmiTagMask)); |
| 5136 | __ b(ne, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5137 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5138 | // Check if the calling frame is an arguments adaptor frame. |
| 5139 | Label adaptor; |
| 5140 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5141 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5142 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5143 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5144 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5145 | // Check index against formal parameters count limit passed in |
| 5146 | // through register eax. Use unsigned comparison to get negative |
| 5147 | // check for free. |
| 5148 | __ cmp(r1, r0); |
| 5149 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5150 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5151 | // Read the argument from the stack and return it. |
| 5152 | __ sub(r3, r0, r1); |
| 5153 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5154 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 5155 | __ mov(pc, lr); |
| 5156 | |
| 5157 | // Arguments adaptor case: Check index against actual arguments |
| 5158 | // limit found in the arguments adaptor frame. Use unsigned |
| 5159 | // comparison to get negative check for free. |
| 5160 | __ bind(&adaptor); |
| 5161 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5162 | __ cmp(r1, r0); |
| 5163 | __ b(cs, &slow); |
| 5164 | |
| 5165 | // Read the argument from the adaptor frame and return it. |
| 5166 | __ sub(r3, r0, r1); |
| 5167 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5168 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 5169 | __ mov(pc, lr); |
| 5170 | |
| 5171 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5172 | // by calling the runtime system. |
| 5173 | __ bind(&slow); |
| 5174 | __ push(r1); |
| 5175 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5176 | } |
| 5177 | |
| 5178 | |
| 5179 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 5180 | // Check if the calling frame is an arguments adaptor frame. |
| 5181 | Label runtime; |
| 5182 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5183 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5184 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5185 | __ b(ne, &runtime); |
| 5186 | |
| 5187 | // Patch the arguments.length and the parameters pointer. |
| 5188 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5189 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 5190 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5191 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 5192 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 5193 | |
| 5194 | // Do the runtime call to allocate the arguments object. |
| 5195 | __ bind(&runtime); |
| 5196 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | |
| 5200 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 5201 | Label slow; |
| 5202 | // Get the function to call from the stack. |
| 5203 | // function, receiver [, arguments] |
| 5204 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 5205 | |
| 5206 | // Check that the function is really a JavaScript function. |
| 5207 | // r1: pushed function (to be verified) |
| 5208 | __ tst(r1, Operand(kSmiTagMask)); |
| 5209 | __ b(eq, &slow); |
| 5210 | // Get the map of the function object. |
| 5211 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5212 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 5213 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 5214 | __ b(ne, &slow); |
| 5215 | |
| 5216 | // Fast-case: Invoke the function now. |
| 5217 | // r1: pushed function |
| 5218 | ParameterCount actual(argc_); |
| 5219 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 5220 | |
| 5221 | // Slow-case: Non-function called. |
| 5222 | __ bind(&slow); |
| 5223 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5224 | __ mov(r2, Operand(0)); |
| 5225 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 5226 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 5227 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | |
| 5231 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5232 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5233 | } } // namespace v8::internal |