ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
| 32 | #include "debug.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "scopes.h" |
| 34 | #include "runtime.h" |
| 35 | |
| 36 | namespace v8 { namespace internal { |
| 37 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 38 | #define __ masm_-> |
| 39 | |
| 40 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 41 | // CodeGenState implementation. |
| 42 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 43 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 44 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 45 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 46 | true_target_(NULL), |
| 47 | false_target_(NULL), |
| 48 | previous_(NULL) { |
| 49 | owner_->set_state(this); |
| 50 | } |
| 51 | |
| 52 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 53 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 54 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 55 | JumpTarget* true_target, |
| 56 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 57 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 58 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 59 | true_target_(true_target), |
| 60 | false_target_(false_target), |
| 61 | previous_(owner->state()) { |
| 62 | owner_->set_state(this); |
| 63 | } |
| 64 | |
| 65 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 66 | CodeGenState::~CodeGenState() { |
| 67 | ASSERT(owner_->state() == this); |
| 68 | owner_->set_state(previous_); |
| 69 | } |
| 70 | |
| 71 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 72 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 73 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 75 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 76 | bool is_eval) |
| 77 | : is_eval_(is_eval), |
| 78 | script_(script), |
| 79 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 81 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 82 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 83 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 84 | cc_reg_(al), |
| 85 | state_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 86 | function_return_is_shadowed_(false), |
| 87 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 92 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 94 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | // cp: callee's context |
| 96 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 97 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | ZoneList<Statement*>* body = fun->body(); |
| 99 | |
| 100 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 101 | ASSERT(scope_ == NULL); |
| 102 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 103 | ASSERT(allocator_ == NULL); |
| 104 | RegisterAllocator register_allocator(this); |
| 105 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 106 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 107 | frame_ = new VirtualFrame(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 108 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 109 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 110 | { |
| 111 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 113 | // Entry: |
| 114 | // Stack: receiver, arguments |
| 115 | // lr: return address |
| 116 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 118 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 120 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 121 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | // tos: code slot |
| 123 | #ifdef DEBUG |
| 124 | if (strlen(FLAG_stop_at) > 0 && |
| 125 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 126 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 127 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | } |
| 129 | #endif |
| 130 | |
| 131 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 132 | frame_->AllocateStackSlots(scope_->num_stack_slots()); |
| 133 | // Initialize the function return target after the locals are set |
| 134 | // up, because it needs the expected frame height from the frame. |
| 135 | function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 136 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 138 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 139 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | // Allocate local context. |
| 141 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 142 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 143 | frame_->EmitPush(r0); |
| 144 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 145 | |
| 146 | if (kDebug) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 147 | JumpTarget verified_true(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 148 | __ cmp(r0, Operand(cp)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 149 | verified_true.Branch(eq); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 150 | __ stop("NewContext: r0 is expected to be the same as cp"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 151 | verified_true.Bind(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 152 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 154 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | } |
| 156 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 157 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | // 1) only needed if we have a context |
| 159 | // 2) no need to recompute context ptr every single time |
| 160 | // 3) don't copy parameter operand code from SlotOperand! |
| 161 | { |
| 162 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 163 | |
| 164 | // Note that iteration order is relevant here! If we have the same |
| 165 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 166 | // needs to be copied into the context, it must be the last argument |
| 167 | // passed to the parameter that needs to be copied. This is a rare |
| 168 | // case so we don't check for it, instead we rely on the copying |
| 169 | // order: such a parameter is copied repeatedly into the same |
| 170 | // context location and thus the last value is what is seen inside |
| 171 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 172 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 173 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | Slot* slot = par->slot(); |
| 175 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 176 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 177 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | // Loads r2 with context; used below in RecordWrite. |
| 179 | __ str(r1, SlotOperand(slot, r2)); |
| 180 | // Load the offset into r3. |
| 181 | int slot_offset = |
| 182 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 183 | __ mov(r3, Operand(slot_offset)); |
| 184 | __ RecordWrite(r2, r3, r1); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 189 | // Store the arguments object. This must happen after context |
| 190 | // initialization because the arguments object may be stored in the |
| 191 | // context. |
| 192 | if (scope_->arguments() != NULL) { |
| 193 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 195 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 196 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 197 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 198 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 199 | // The receiver is below the arguments, the return address, |
| 200 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 201 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 203 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 204 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 205 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 206 | frame_->CallStub(&stub, 3); |
| 207 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 208 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 209 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 210 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 212 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | } |
| 214 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 215 | // Generate code to 'execute' declarations and initialize functions |
| 216 | // (source elements). In case of an illegal redeclaration we need to |
| 217 | // handle that instead of processing the declarations. |
| 218 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 220 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | } else { |
| 222 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 223 | ProcessDeclarations(scope_->declarations()); |
| 224 | // Bail out if a stack-overflow exception occurred when processing |
| 225 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 226 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | } |
| 228 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 229 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 230 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 231 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 232 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | CheckStack(); |
| 234 | |
| 235 | // Compile the body of the function in a vanilla state. Don't |
| 236 | // bother compiling all the code if the scope has an illegal |
| 237 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 238 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | Comment cmnt(masm_, "[ function body"); |
| 240 | #ifdef DEBUG |
| 241 | bool is_builtin = Bootstrapper::IsActive(); |
| 242 | bool should_trace = |
| 243 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 244 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 245 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 246 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 247 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 249 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | } |
| 252 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 253 | // Generate the return sequence if necessary. |
| 254 | if (frame_ != NULL || function_return_.is_linked()) { |
| 255 | // exit |
| 256 | // r0: result |
| 257 | // sp: stack pointer |
| 258 | // fp: frame pointer |
| 259 | // pp: parameter pointer |
| 260 | // cp: callee's context |
| 261 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 262 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 263 | function_return_.Bind(); |
| 264 | if (FLAG_trace) { |
| 265 | // Push the return value on the stack as the parameter. |
| 266 | // Runtime::TraceExit returns the parameter as it is. |
| 267 | frame_->EmitPush(r0); |
| 268 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 269 | } |
| 270 | |
| 271 | // Tear down the frame which will restore the caller's frame pointer and |
| 272 | // the link register. |
| 273 | frame_->Exit(); |
| 274 | |
| 275 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
| 276 | __ mov(pc, lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 277 | } |
| 278 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 280 | ASSERT(!has_cc()); |
| 281 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 282 | ASSERT(!function_return_is_shadowed_); |
| 283 | function_return_.Unuse(); |
| 284 | DeleteFrame(); |
| 285 | |
| 286 | // Process any deferred code using the register allocator. |
| 287 | if (HasStackOverflow()) { |
| 288 | ClearDeferred(); |
| 289 | } else { |
| 290 | ProcessDeferred(); |
| 291 | } |
| 292 | |
| 293 | allocator_ = NULL; |
| 294 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 298 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 299 | // Currently, this assertion will fail if we try to assign to |
| 300 | // a constant variable that is constant because it is read-only |
| 301 | // (such as the variable referring to a named function expression). |
| 302 | // We need to implement assignments to read-only variables. |
| 303 | // Ideally, we should do this during AST generation (by converting |
| 304 | // such assignments into expression statements); however, in general |
| 305 | // we may not be able to make the decision until past AST generation, |
| 306 | // that is when the entire program is known. |
| 307 | ASSERT(slot != NULL); |
| 308 | int index = slot->index(); |
| 309 | switch (slot->type()) { |
| 310 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 311 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 312 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 313 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 314 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 315 | |
| 316 | case Slot::CONTEXT: { |
| 317 | // Follow the context chain if necessary. |
| 318 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 319 | Register context = cp; |
| 320 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 321 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 322 | // Load the closure. |
| 323 | // (All contexts, even 'with' contexts, have a closure, |
| 324 | // and it is the same for all contexts inside a function. |
| 325 | // There is no need to go to the function context first.) |
| 326 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 327 | // Load the function context (which is the incoming, outer context). |
| 328 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 329 | context = tmp; |
| 330 | } |
| 331 | // We may have a 'with' context now. Get the function context. |
| 332 | // (In fact this mov may never be the needed, since the scope analysis |
| 333 | // may not permit a direct context access in this case and thus we are |
| 334 | // always at a function context. However it is safe to dereference be- |
| 335 | // cause the function context of a function context is itself. Before |
| 336 | // deleting this mov we should try to create a counter-example first, |
| 337 | // though...) |
| 338 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 339 | return ContextOperand(tmp, index); |
| 340 | } |
| 341 | |
| 342 | default: |
| 343 | UNREACHABLE(); |
| 344 | return MemOperand(r0, 0); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 349 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 350 | Slot* slot, |
| 351 | Register tmp, |
| 352 | Register tmp2, |
| 353 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 354 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 355 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 356 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 357 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 358 | if (s->num_heap_slots() > 0) { |
| 359 | if (s->calls_eval()) { |
| 360 | // Check that extension is NULL. |
| 361 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 362 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 363 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 364 | } |
| 365 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 366 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 367 | context = tmp; |
| 368 | } |
| 369 | } |
| 370 | // Check that last extension is NULL. |
| 371 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 372 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 373 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 374 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 375 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 379 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 380 | // (partially) translated into branches, or it may have set the condition |
| 381 | // code register. If force_cc is set, the value is forced to set the |
| 382 | // condition code register and no value is pushed. If the condition code |
| 383 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 384 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 385 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 386 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 387 | JumpTarget* true_target, |
| 388 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 389 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 390 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 391 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 392 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 394 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 395 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 396 | |
| 397 | // If we hit a stack overflow, we may not have actually visited |
| 398 | // the expression. In that case, we ensure that we have a |
| 399 | // valid-looking frame state because we will continue to generate |
| 400 | // code as we unwind the C++ stack. |
| 401 | // |
| 402 | // It's possible to have both a stack overflow and a valid frame |
| 403 | // state (eg, a subexpression overflowed, visiting it returned |
| 404 | // with a dummied frame state, and visiting this expression |
| 405 | // returned with a normal-looking state). |
| 406 | if (HasStackOverflow() && |
| 407 | has_valid_frame() && |
| 408 | !has_cc() && |
| 409 | frame_->height() == original_height) { |
| 410 | true_target->Jump(); |
| 411 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 412 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 413 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 414 | // Convert the TOS value to a boolean in the condition code register. |
| 415 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 417 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 418 | ASSERT(!has_valid_frame() || |
| 419 | (has_cc() && frame_->height() == original_height) || |
| 420 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 424 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 425 | #ifdef DEBUG |
| 426 | int original_height = frame_->height(); |
| 427 | #endif |
| 428 | ASSERT(!in_spilled_code()); |
| 429 | JumpTarget true_target(this); |
| 430 | JumpTarget false_target(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 431 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | |
| 433 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 434 | // Convert cc_reg_ into a boolean value. |
| 435 | JumpTarget loaded(this); |
| 436 | JumpTarget materialize_true(this); |
| 437 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 438 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 439 | frame_->EmitPush(r0); |
| 440 | loaded.Jump(); |
| 441 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 442 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 443 | frame_->EmitPush(r0); |
| 444 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | cc_reg_ = al; |
| 446 | } |
| 447 | |
| 448 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 449 | // We have at least one condition value that has been "translated" |
| 450 | // into a branch, thus it needs to be loaded explicitly. |
| 451 | JumpTarget loaded(this); |
| 452 | if (frame_ != NULL) { |
| 453 | loaded.Jump(); // Don't lose the current TOS. |
| 454 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 456 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 457 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 458 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 459 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 460 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 462 | // If both "true" and "false" need to be loaded jump across the code for |
| 463 | // "false". |
| 464 | if (both) { |
| 465 | loaded.Jump(); |
| 466 | } |
| 467 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 469 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 470 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 471 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 473 | // A value is loaded on all paths reaching this point. |
| 474 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 476 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 477 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 478 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 482 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 483 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 484 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 485 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 489 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 490 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 491 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 492 | __ ldr(scratch, |
| 493 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 494 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | // 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] | 499 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 500 | // variables w/o reference errors elsewhere. |
| 501 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 502 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 504 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 505 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 506 | // the variable as if through '<global>.<variable>' to make sure we |
| 507 | // do not get reference errors. |
| 508 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 509 | Literal key(variable->name()); |
| 510 | // TODO(1241834): Fetch the position from the variable instead of using |
| 511 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 512 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 513 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 515 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 520 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 522 | cgen->LoadReference(this); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | Reference::~Reference() { |
| 527 | cgen_->UnloadReference(this); |
| 528 | } |
| 529 | |
| 530 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 531 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 532 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 533 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | Expression* e = ref->expression(); |
| 535 | Property* property = e->AsProperty(); |
| 536 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 537 | |
| 538 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 539 | // The expression is either a property or a variable proxy that rewrites |
| 540 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 541 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 542 | // We use a named reference if the key is a literal symbol, unless it is |
| 543 | // a string that can be legally parsed as an integer. This is because |
| 544 | // otherwise we will not get into the slow case code that handles [] on |
| 545 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | Literal* literal = property->key()->AsLiteral(); |
| 547 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 548 | if (literal != NULL && |
| 549 | literal->handle()->IsSymbol() && |
| 550 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 551 | ref->set_type(Reference::NAMED); |
| 552 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 553 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | ref->set_type(Reference::KEYED); |
| 555 | } |
| 556 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 557 | // The expression is a variable proxy that does not rewrite to a |
| 558 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | LoadGlobal(); |
| 561 | ref->set_type(Reference::NAMED); |
| 562 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 563 | ASSERT(var->slot() != NULL); |
| 564 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | } |
| 566 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 567 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 568 | LoadAndSpill(e); |
| 569 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 574 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 575 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 576 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 578 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 579 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 580 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 581 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 582 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 588 | // register to a boolean in the condition code register. The code |
| 589 | // 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^] | 590 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 591 | JumpTarget* false_target) { |
| 592 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 593 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 594 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 595 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 596 | |
| 597 | // Fast case checks |
| 598 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 599 | // Check if the value is 'false'. |
| 600 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 601 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 603 | // Check if the value is 'true'. |
| 604 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 605 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 606 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 607 | // Check if the value is 'undefined'. |
| 608 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 609 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 611 | // Check if the value is a smi. |
| 612 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 613 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 614 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 615 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | |
| 617 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 618 | frame_->EmitPush(r0); |
| 619 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 620 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | |
| 623 | cc_reg_ = ne; |
| 624 | } |
| 625 | |
| 626 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | class GetPropertyStub : public CodeStub { |
| 628 | public: |
| 629 | GetPropertyStub() { } |
| 630 | |
| 631 | private: |
| 632 | Major MajorKey() { return GetProperty; } |
| 633 | int MinorKey() { return 0; } |
| 634 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | }; |
| 636 | |
| 637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | class SetPropertyStub : public CodeStub { |
| 639 | public: |
| 640 | SetPropertyStub() { } |
| 641 | |
| 642 | private: |
| 643 | Major MajorKey() { return SetProperty; } |
| 644 | int MinorKey() { return 0; } |
| 645 | void Generate(MacroAssembler* masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 649 | class GenericBinaryOpStub : public CodeStub { |
| 650 | public: |
| 651 | explicit GenericBinaryOpStub(Token::Value op) : op_(op) { } |
| 652 | |
| 653 | private: |
| 654 | Token::Value op_; |
| 655 | |
| 656 | Major MajorKey() { return GenericBinaryOp; } |
| 657 | int MinorKey() { return static_cast<int>(op_); } |
| 658 | void Generate(MacroAssembler* masm); |
| 659 | |
| 660 | const char* GetName() { |
| 661 | switch (op_) { |
| 662 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 663 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 664 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 665 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 666 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 667 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 668 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 669 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 670 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 671 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 672 | default: return "GenericBinaryOpStub"; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | #ifdef DEBUG |
| 677 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 678 | #endif |
| 679 | }; |
| 680 | |
| 681 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | class InvokeBuiltinStub : public CodeStub { |
| 683 | public: |
| 684 | enum Kind { Inc, Dec, ToNumber }; |
| 685 | InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { } |
| 686 | |
| 687 | private: |
| 688 | Kind kind_; |
| 689 | int argc_; |
| 690 | |
| 691 | Major MajorKey() { return InvokeBuiltin; } |
| 692 | int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); } |
| 693 | void Generate(MacroAssembler* masm); |
| 694 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | #ifdef DEBUG |
| 696 | void Print() { |
| 697 | PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n", |
| 698 | static_cast<int>(kind_), |
| 699 | argc_); |
| 700 | } |
| 701 | #endif |
| 702 | }; |
| 703 | |
| 704 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 705 | void CodeGenerator::GenericBinaryOperation(Token::Value op) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 706 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 707 | // sp[0] : y |
| 708 | // sp[1] : x |
| 709 | // result : r0 |
| 710 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 711 | // Stub is entered with a call: 'return address' is in lr. |
| 712 | switch (op) { |
| 713 | case Token::ADD: // fall through. |
| 714 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 715 | case Token::MUL: |
| 716 | case Token::BIT_OR: |
| 717 | case Token::BIT_AND: |
| 718 | case Token::BIT_XOR: |
| 719 | case Token::SHL: |
| 720 | case Token::SHR: |
| 721 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 722 | frame_->EmitPop(r0); // r0 : y |
| 723 | frame_->EmitPop(r1); // r1 : x |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 724 | GenericBinaryOpStub stub(op); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 725 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | |
| 729 | case Token::DIV: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 730 | Result arg_count = allocator_->Allocate(r0); |
| 731 | ASSERT(arg_count.is_valid()); |
| 732 | __ mov(arg_count.reg(), Operand(1)); |
| 733 | frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 734 | break; |
| 735 | } |
| 736 | |
| 737 | case Token::MOD: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 738 | Result arg_count = allocator_->Allocate(r0); |
| 739 | ASSERT(arg_count.is_valid()); |
| 740 | __ mov(arg_count.reg(), Operand(1)); |
| 741 | frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 746 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 747 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 748 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 749 | break; |
| 750 | |
| 751 | default: |
| 752 | // Other cases should have been handled before this point. |
| 753 | UNREACHABLE(); |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 759 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 760 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 761 | DeferredInlineSmiOperation(CodeGenerator* generator, |
| 762 | Token::Value op, |
| 763 | int value, |
| 764 | bool reversed) |
| 765 | : DeferredCode(generator), |
| 766 | op_(op), |
| 767 | value_(value), |
| 768 | reversed_(reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 769 | set_comment("[ DeferredInlinedSmiOperation"); |
| 770 | } |
| 771 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 772 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 773 | |
| 774 | private: |
| 775 | Token::Value op_; |
| 776 | int value_; |
| 777 | bool reversed_; |
| 778 | }; |
| 779 | |
| 780 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 781 | void DeferredInlineSmiOperation::Generate() { |
| 782 | enter()->Bind(); |
| 783 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 784 | |
| 785 | switch (op_) { |
| 786 | case Token::ADD: { |
| 787 | if (reversed_) { |
| 788 | // revert optimistic add |
| 789 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 790 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 791 | } else { |
| 792 | // revert optimistic add |
| 793 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 794 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 795 | } |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | case Token::SUB: { |
| 800 | if (reversed_) { |
| 801 | // revert optimistic sub |
| 802 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 803 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 804 | } else { |
| 805 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 806 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 807 | } |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | case Token::BIT_OR: |
| 812 | case Token::BIT_XOR: |
| 813 | case Token::BIT_AND: { |
| 814 | if (reversed_) { |
| 815 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 816 | } else { |
| 817 | __ mov(r1, Operand(r0)); |
| 818 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | case Token::SHL: |
| 824 | case Token::SHR: |
| 825 | case Token::SAR: { |
| 826 | if (!reversed_) { |
| 827 | __ mov(r1, Operand(r0)); |
| 828 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 829 | } else { |
| 830 | UNREACHABLE(); // should have been handled in SmiOperation |
| 831 | } |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | default: |
| 836 | // other cases should have been handled before this point. |
| 837 | UNREACHABLE(); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | GenericBinaryOpStub igostub(op_); |
| 842 | Result arg0 = generator()->allocator()->Allocate(r0); |
| 843 | ASSERT(arg0.is_valid()); |
| 844 | Result arg1 = generator()->allocator()->Allocate(r1); |
| 845 | ASSERT(arg1.is_valid()); |
| 846 | generator()->frame()->CallStub(&igostub, &arg0, &arg1, 0); |
| 847 | exit_.Jump(); |
| 848 | } |
| 849 | |
| 850 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 851 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 852 | Handle<Object> value, |
| 853 | bool reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 854 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 856 | // some possible smi operations (like + and -) when (at least) one |
| 857 | // of the operands is a literal smi. With this optimization, the |
| 858 | // performance of the system is increased by ~15%, and the generated |
| 859 | // code size is increased by ~1% (measured on a combination of |
| 860 | // different benchmarks). |
| 861 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 862 | // sp[0] : operand |
| 863 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 864 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 865 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 866 | JumpTarget exit(this); |
| 867 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 868 | |
| 869 | switch (op) { |
| 870 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 871 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 872 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 873 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 874 | __ add(r0, r0, Operand(value), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 875 | deferred->enter()->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 877 | deferred->enter()->Branch(ne); |
| 878 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | |
| 882 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 883 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 884 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 885 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | if (!reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 887 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 889 | __ rsb(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 890 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 891 | deferred->enter()->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 892 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 893 | deferred->enter()->Branch(ne); |
| 894 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 895 | break; |
| 896 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 898 | case Token::BIT_OR: |
| 899 | case Token::BIT_XOR: |
| 900 | case Token::BIT_AND: { |
| 901 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 902 | new DeferredInlineSmiOperation(this, op, int_value, reversed); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 904 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 905 | switch (op) { |
| 906 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 907 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 908 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 909 | default: UNREACHABLE(); |
| 910 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 911 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 912 | break; |
| 913 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 915 | case Token::SHL: |
| 916 | case Token::SHR: |
| 917 | case Token::SAR: { |
| 918 | if (reversed) { |
| 919 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 920 | frame_->EmitPush(ip); |
| 921 | frame_->EmitPush(r0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 922 | GenericBinaryOperation(op); |
| 923 | |
| 924 | } else { |
| 925 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 926 | DeferredCode* deferred = |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 927 | new DeferredInlineSmiOperation(this, op, shift_value, false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 928 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 929 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 930 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 931 | switch (op) { |
| 932 | case Token::SHL: { |
| 933 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 934 | // check that the *unsigned* result fits in a smi |
| 935 | __ add(r3, r2, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 936 | deferred->enter()->Branch(mi); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 937 | break; |
| 938 | } |
| 939 | case Token::SHR: { |
| 940 | // LSR by immediate 0 means shifting 32 bits. |
| 941 | if (shift_value != 0) { |
| 942 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 943 | } |
| 944 | // check that the *unsigned* result fits in a smi |
| 945 | // neither of the two high-order bits can be set: |
| 946 | // - 0x80000000: high bit would be lost when smi tagging |
| 947 | // - 0x40000000: this number would convert to negative when |
| 948 | // smi tagging these two cases can only happen with shifts |
| 949 | // by 0 or 1 when handed a valid smi |
| 950 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 951 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 952 | break; |
| 953 | } |
| 954 | case Token::SAR: { |
| 955 | if (shift_value != 0) { |
| 956 | // ASR by immediate 0 means shifting 32 bits. |
| 957 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 958 | } |
| 959 | break; |
| 960 | } |
| 961 | default: UNREACHABLE(); |
| 962 | } |
| 963 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 964 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 965 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 966 | break; |
| 967 | } |
| 968 | |
| 969 | default: |
| 970 | if (!reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 971 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 972 | __ mov(r0, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 973 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | } else { |
| 975 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 976 | frame_->EmitPush(ip); |
| 977 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 978 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 979 | GenericBinaryOperation(op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 980 | break; |
| 981 | } |
| 982 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 983 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 987 | void CodeGenerator::Comparison(Condition cc, bool strict) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 988 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 989 | // sp[0] : y |
| 990 | // sp[1] : x |
| 991 | // result : cc register |
| 992 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 993 | // Strict only makes sense for equality comparisons. |
| 994 | ASSERT(!strict || cc == eq); |
| 995 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 996 | JumpTarget exit(this); |
| 997 | JumpTarget smi(this); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 998 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 999 | if (cc == gt || cc == le) { |
| 1000 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1001 | frame_->EmitPop(r1); |
| 1002 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1003 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1004 | frame_->EmitPop(r0); |
| 1005 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1006 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1007 | __ orr(r2, r0, Operand(r1)); |
| 1008 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1009 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Perform non-smi comparison by runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1012 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Figure out which native to call and setup the arguments. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1015 | Builtins::JavaScript native; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1016 | int arg_count = 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1017 | if (cc == eq) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1018 | native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1020 | native = Builtins::COMPARE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1021 | int ncr; // NaN compare result |
| 1022 | if (cc == lt || cc == le) { |
| 1023 | ncr = GREATER; |
| 1024 | } else { |
| 1025 | ASSERT(cc == gt || cc == ge); // remaining cases |
| 1026 | ncr = LESS; |
| 1027 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1028 | frame_->EmitPush(r0); |
| 1029 | arg_count++; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1030 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 1034 | // tagged as a small integer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1035 | frame_->EmitPush(r0); |
| 1036 | Result arg_count_register = allocator_->Allocate(r0); |
| 1037 | ASSERT(arg_count_register.is_valid()); |
| 1038 | __ mov(arg_count_register.reg(), Operand(arg_count)); |
| 1039 | Result result = frame_->InvokeBuiltin(native, |
| 1040 | CALL_JS, |
| 1041 | &arg_count_register, |
| 1042 | arg_count + 1); |
| 1043 | __ cmp(result.reg(), Operand(0)); |
| 1044 | result.Unuse(); |
| 1045 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | |
| 1047 | // test smi equality by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1048 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1049 | __ cmp(r1, Operand(r0)); |
| 1050 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1051 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | cc_reg_ = cc; |
| 1053 | } |
| 1054 | |
| 1055 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1056 | class CallFunctionStub: public CodeStub { |
| 1057 | public: |
| 1058 | explicit CallFunctionStub(int argc) : argc_(argc) {} |
| 1059 | |
| 1060 | void Generate(MacroAssembler* masm); |
| 1061 | |
| 1062 | private: |
| 1063 | int argc_; |
| 1064 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1065 | #if defined(DEBUG) |
| 1066 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1067 | #endif // defined(DEBUG) |
| 1068 | |
| 1069 | Major MajorKey() { return CallFunction; } |
| 1070 | int MinorKey() { return argc_; } |
| 1071 | }; |
| 1072 | |
| 1073 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1074 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1075 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | int position) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1077 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1078 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1079 | int arg_count = args->length(); |
| 1080 | for (int i = 0; i < arg_count; i++) { |
| 1081 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1082 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1083 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1084 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1085 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1086 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1087 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1088 | CallFunctionStub call_function(arg_count); |
| 1089 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1092 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1093 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1097 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
| 1098 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | ASSERT(has_cc()); |
| 1100 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1101 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | cc_reg_ = al; |
| 1103 | } |
| 1104 | |
| 1105 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1106 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1107 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1108 | if (FLAG_check_stack) { |
| 1109 | Comment cmnt(masm_, "[ check stack"); |
| 1110 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1111 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1116 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1117 | #ifdef DEBUG |
| 1118 | int original_height = frame_->height(); |
| 1119 | #endif |
| 1120 | VirtualFrame::SpilledScope spilled_scope(this); |
| 1121 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1122 | VisitAndSpill(statements->at(i)); |
| 1123 | } |
| 1124 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1125 | } |
| 1126 | |
| 1127 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1128 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1129 | #ifdef DEBUG |
| 1130 | int original_height = frame_->height(); |
| 1131 | #endif |
| 1132 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1134 | CodeForStatementPosition(node); |
| 1135 | node->break_target()->Initialize(this); |
| 1136 | VisitStatementsAndSpill(node->statements()); |
| 1137 | if (node->break_target()->is_linked()) { |
| 1138 | node->break_target()->Bind(); |
| 1139 | } |
| 1140 | node->break_target()->Unuse(); |
| 1141 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1145 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1146 | VirtualFrame::SpilledScope spilled_scope(this); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1147 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1148 | frame_->EmitPush(r0); |
| 1149 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1150 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1151 | frame_->EmitPush(r0); |
| 1152 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1153 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1157 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1158 | #ifdef DEBUG |
| 1159 | int original_height = frame_->height(); |
| 1160 | #endif |
| 1161 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1162 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1163 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | Variable* var = node->proxy()->var(); |
| 1165 | ASSERT(var != NULL); // must have been resolved |
| 1166 | Slot* slot = var->slot(); |
| 1167 | |
| 1168 | // If it was not possible to allocate the variable at compile time, |
| 1169 | // we need to "declare" it at runtime to make sure it actually |
| 1170 | // exists in the local context. |
| 1171 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1172 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1173 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1174 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1175 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1176 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1177 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1178 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1179 | // Declaration nodes are always declared in only two modes. |
| 1180 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1181 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1182 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1183 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1184 | // Push initial value, if any. |
| 1185 | // Note: For variables we must not push an initial value (such as |
| 1186 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1187 | // must not destroy the current value. |
| 1188 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1189 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1190 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1192 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1194 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1195 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1196 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1197 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1198 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1199 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | ASSERT(!var->is_global()); |
| 1204 | |
| 1205 | // If we have a function or a constant, we need to initialize the variable. |
| 1206 | Expression* val = NULL; |
| 1207 | if (node->mode() == Variable::CONST) { |
| 1208 | val = new Literal(Factory::the_hole_value()); |
| 1209 | } else { |
| 1210 | val = node->fun(); // NULL if we don't have a function |
| 1211 | } |
| 1212 | |
| 1213 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1214 | { |
| 1215 | // Set initial value. |
| 1216 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1217 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1218 | target.SetValue(NOT_CONST_INIT); |
| 1219 | // The reference is removed from the stack (preserving TOS) when |
| 1220 | // it goes out of scope. |
| 1221 | } |
| 1222 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1223 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1224 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1225 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1229 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1230 | #ifdef DEBUG |
| 1231 | int original_height = frame_->height(); |
| 1232 | #endif |
| 1233 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1235 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1236 | Expression* expression = node->expression(); |
| 1237 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1238 | LoadAndSpill(expression); |
| 1239 | frame_->Drop(); |
| 1240 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1244 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1245 | #ifdef DEBUG |
| 1246 | int original_height = frame_->height(); |
| 1247 | #endif |
| 1248 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1250 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1252 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1256 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1257 | #ifdef DEBUG |
| 1258 | int original_height = frame_->height(); |
| 1259 | #endif |
| 1260 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1261 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1262 | // Generate different code depending on which parts of the if statement |
| 1263 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1264 | bool has_then_stm = node->HasThenStatement(); |
| 1265 | bool has_else_stm = node->HasElseStatement(); |
| 1266 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1267 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1269 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1271 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1272 | JumpTarget then(this); |
| 1273 | JumpTarget else_(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1274 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1275 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1276 | &then, &else_, true); |
| 1277 | if (frame_ != NULL) { |
| 1278 | Branch(false, &else_); |
| 1279 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1281 | if (frame_ != NULL || then.is_linked()) { |
| 1282 | then.Bind(); |
| 1283 | VisitAndSpill(node->then_statement()); |
| 1284 | } |
| 1285 | if (frame_ != NULL) { |
| 1286 | exit.Jump(); |
| 1287 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1288 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1289 | if (else_.is_linked()) { |
| 1290 | else_.Bind(); |
| 1291 | VisitAndSpill(node->else_statement()); |
| 1292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | |
| 1294 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1295 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1296 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1297 | JumpTarget then(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1298 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1299 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1300 | &then, &exit, true); |
| 1301 | if (frame_ != NULL) { |
| 1302 | Branch(false, &exit); |
| 1303 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1305 | if (frame_ != NULL || then.is_linked()) { |
| 1306 | then.Bind(); |
| 1307 | VisitAndSpill(node->then_statement()); |
| 1308 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1309 | |
| 1310 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1311 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1312 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1313 | JumpTarget else_(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1315 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1316 | &exit, &else_, true); |
| 1317 | if (frame_ != NULL) { |
| 1318 | Branch(true, &exit); |
| 1319 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1320 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1321 | if (frame_ != NULL || else_.is_linked()) { |
| 1322 | else_.Bind(); |
| 1323 | VisitAndSpill(node->else_statement()); |
| 1324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1325 | |
| 1326 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1327 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1328 | ASSERT(!has_then_stm && !has_else_stm); |
| 1329 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1330 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1331 | &exit, &exit, false); |
| 1332 | if (frame_ != NULL) { |
| 1333 | if (has_cc()) { |
| 1334 | cc_reg_ = al; |
| 1335 | } else { |
| 1336 | frame_->Drop(); |
| 1337 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1342 | if (exit.is_linked()) { |
| 1343 | exit.Bind(); |
| 1344 | } |
| 1345 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1349 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1350 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1351 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1352 | CodeForStatementPosition(node); |
| 1353 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1357 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1358 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1359 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1360 | CodeForStatementPosition(node); |
| 1361 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1365 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1366 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1367 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1368 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1369 | if (function_return_is_shadowed_) { |
| 1370 | CodeForStatementPosition(node); |
| 1371 | LoadAndSpill(node->expression()); |
| 1372 | frame_->EmitPop(r0); |
| 1373 | function_return_.Jump(); |
| 1374 | } else { |
| 1375 | // Load the returned value. |
| 1376 | CodeForStatementPosition(node); |
| 1377 | LoadAndSpill(node->expression()); |
| 1378 | |
| 1379 | // Pop the result from the frame and prepare the frame for |
| 1380 | // returning thus making it easier to merge. |
| 1381 | frame_->EmitPop(r0); |
| 1382 | frame_->PrepareForReturn(); |
| 1383 | |
| 1384 | function_return_.Jump(); |
| 1385 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1389 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1390 | #ifdef DEBUG |
| 1391 | int original_height = frame_->height(); |
| 1392 | #endif |
| 1393 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1394 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1395 | CodeForStatementPosition(node); |
| 1396 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1397 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1398 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1399 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1400 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1401 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1402 | if (kDebug) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1403 | JumpTarget verified_true(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1404 | __ cmp(r0, Operand(cp)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1405 | verified_true.Branch(eq); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1406 | __ stop("PushContext: r0 is expected to be the same as cp"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1407 | verified_true.Bind(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1408 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1409 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1410 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1411 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1415 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1416 | #ifdef DEBUG |
| 1417 | int original_height = frame_->height(); |
| 1418 | #endif |
| 1419 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1421 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | // Pop context. |
| 1423 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1424 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1425 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1426 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1430 | int CodeGenerator::FastCaseSwitchMaxOverheadFactor() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1431 | return kFastSwitchMaxOverheadFactor; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1434 | int CodeGenerator::FastCaseSwitchMinCaseCount() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1435 | return kFastSwitchMinCaseCount; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1439 | void CodeGenerator::GenerateFastCaseSwitchJumpTable( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1440 | SwitchStatement* node, |
| 1441 | int min_index, |
| 1442 | int range, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1443 | Label* default_label, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1444 | Vector<Label*> case_targets, |
| 1445 | Vector<Label> case_labels) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1446 | VirtualFrame::SpilledScope spilled_scope(this); |
| 1447 | JumpTarget setup_default(this); |
| 1448 | JumpTarget is_smi(this); |
| 1449 | |
| 1450 | // A non-null default label pointer indicates a default case among |
| 1451 | // the case labels. Otherwise we use the break target as a |
| 1452 | // "default" for failure to hit the jump table. |
| 1453 | JumpTarget* default_target = |
| 1454 | (default_label == NULL) ? node->break_target() : &setup_default; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1455 | |
| 1456 | ASSERT(kSmiTag == 0 && kSmiTagSize <= 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1457 | frame_->EmitPop(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1458 | |
| 1459 | // Test for a Smi value in a HeapNumber. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1460 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1461 | is_smi.Branch(eq); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1462 | __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag)); |
| 1463 | __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag)); |
| 1464 | __ cmp(r1, Operand(HEAP_NUMBER_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1465 | default_target->Branch(ne); |
| 1466 | frame_->EmitPush(r0); |
| 1467 | frame_->CallRuntime(Runtime::kNumberToSmi, 1); |
| 1468 | is_smi.Bind(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1469 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1470 | if (min_index != 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1471 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1472 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1473 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1474 | if (Smi::IsValid(-min_index)) { |
| 1475 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1476 | } else { |
| 1477 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1478 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1479 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1480 | } else { |
| 1481 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1482 | } |
| 1483 | } |
| 1484 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1485 | default_target->Branch(ne); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1486 | __ cmp(r0, Operand(Smi::FromInt(range))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1487 | default_target->Branch(ge); |
| 1488 | VirtualFrame* start_frame = new VirtualFrame(frame_); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1489 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1490 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1491 | GenerateFastCaseSwitchCases(node, case_labels, start_frame); |
| 1492 | |
| 1493 | // If there was a default case among the case labels, we need to |
| 1494 | // emit code to jump to it from the default target used for failure |
| 1495 | // to hit the jump table. |
| 1496 | if (default_label != NULL) { |
| 1497 | if (has_valid_frame()) { |
| 1498 | node->break_target()->Jump(); |
| 1499 | } |
| 1500 | setup_default.Bind(); |
| 1501 | frame_->MergeTo(start_frame); |
| 1502 | __ b(default_label); |
| 1503 | DeleteFrame(); |
| 1504 | } |
| 1505 | if (node->break_target()->is_linked()) { |
| 1506 | node->break_target()->Bind(); |
| 1507 | } |
| 1508 | |
| 1509 | delete start_frame; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1513 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1514 | #ifdef DEBUG |
| 1515 | int original_height = frame_->height(); |
| 1516 | #endif |
| 1517 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1518 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1519 | CodeForStatementPosition(node); |
| 1520 | node->break_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1521 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1522 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1523 | if (TryGenerateFastCaseSwitchStatement(node)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1524 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1525 | return; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1528 | JumpTarget next_test(this); |
| 1529 | JumpTarget fall_through(this); |
| 1530 | JumpTarget default_entry(this); |
| 1531 | JumpTarget default_exit(this, JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1532 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1533 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1534 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1535 | |
| 1536 | for (int i = 0; i < length; i++) { |
| 1537 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1538 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1539 | // Remember the default clause and compile it at the end. |
| 1540 | default_clause = clause; |
| 1541 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1544 | Comment cmnt(masm_, "[ Case clause"); |
| 1545 | // Compile the test. |
| 1546 | next_test.Bind(); |
| 1547 | next_test.Unuse(); |
| 1548 | // Duplicate TOS. |
| 1549 | __ ldr(r0, frame_->Top()); |
| 1550 | frame_->EmitPush(r0); |
| 1551 | LoadAndSpill(clause->label()); |
| 1552 | Comparison(eq, true); |
| 1553 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1554 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1555 | // Before entering the body from the test, remove the switch value from |
| 1556 | // the stack. |
| 1557 | frame_->Drop(); |
| 1558 | |
| 1559 | // Label the body so that fall through is enabled. |
| 1560 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1561 | default_exit.Bind(); |
| 1562 | } else { |
| 1563 | fall_through.Bind(); |
| 1564 | fall_through.Unuse(); |
| 1565 | } |
| 1566 | VisitStatementsAndSpill(clause->statements()); |
| 1567 | |
| 1568 | // If control flow can fall through from the body, jump to the next body |
| 1569 | // or the end of the statement. |
| 1570 | if (frame_ != NULL) { |
| 1571 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1572 | default_entry.Jump(); |
| 1573 | } else { |
| 1574 | fall_through.Jump(); |
| 1575 | } |
| 1576 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1579 | // The final "test" removes the switch value. |
| 1580 | next_test.Bind(); |
| 1581 | frame_->Drop(); |
| 1582 | |
| 1583 | // If there is a default clause, compile it. |
| 1584 | if (default_clause != NULL) { |
| 1585 | Comment cmnt(masm_, "[ Default clause"); |
| 1586 | default_entry.Bind(); |
| 1587 | VisitStatementsAndSpill(default_clause->statements()); |
| 1588 | // If control flow can fall out of the default and there is a case after |
| 1589 | // it, jup to that case's body. |
| 1590 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1591 | default_exit.Jump(); |
| 1592 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1593 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1595 | if (fall_through.is_linked()) { |
| 1596 | fall_through.Bind(); |
| 1597 | } |
| 1598 | |
| 1599 | if (node->break_target()->is_linked()) { |
| 1600 | node->break_target()->Bind(); |
| 1601 | } |
| 1602 | node->break_target()->Unuse(); |
| 1603 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1607 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1608 | #ifdef DEBUG |
| 1609 | int original_height = frame_->height(); |
| 1610 | #endif |
| 1611 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1612 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1613 | CodeForStatementPosition(node); |
| 1614 | node->break_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1615 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1616 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1617 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1618 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1619 | if (node->cond() == NULL) { |
| 1620 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1621 | info = ALWAYS_TRUE; |
| 1622 | } else { |
| 1623 | Literal* lit = node->cond()->AsLiteral(); |
| 1624 | if (lit != NULL) { |
| 1625 | if (lit->IsTrue()) { |
| 1626 | info = ALWAYS_TRUE; |
| 1627 | } else if (lit->IsFalse()) { |
| 1628 | info = ALWAYS_FALSE; |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1633 | switch (node->type()) { |
| 1634 | case LoopStatement::DO_LOOP: { |
| 1635 | JumpTarget body(this, JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1636 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1637 | // Label the top of the loop for the backward CFG edge. If the test |
| 1638 | // is always true we can use the continue target, and if the test is |
| 1639 | // always false there is no need. |
| 1640 | if (info == ALWAYS_TRUE) { |
| 1641 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1642 | node->continue_target()->Bind(); |
| 1643 | } else if (info == ALWAYS_FALSE) { |
| 1644 | node->continue_target()->Initialize(this); |
| 1645 | } else { |
| 1646 | ASSERT(info == DONT_KNOW); |
| 1647 | node->continue_target()->Initialize(this); |
| 1648 | body.Bind(); |
| 1649 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1650 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1651 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1652 | VisitAndSpill(node->body()); |
| 1653 | |
| 1654 | // Compile the test. |
| 1655 | if (info == ALWAYS_TRUE) { |
| 1656 | if (has_valid_frame()) { |
| 1657 | // If control can fall off the end of the body, jump back to the |
| 1658 | // top. |
| 1659 | node->continue_target()->Jump(); |
| 1660 | } |
| 1661 | } else if (info == ALWAYS_FALSE) { |
| 1662 | // If we have a continue in the body, we only have to bind its jump |
| 1663 | // target. |
| 1664 | if (node->continue_target()->is_linked()) { |
| 1665 | node->continue_target()->Bind(); |
| 1666 | } |
| 1667 | } else { |
| 1668 | ASSERT(info == DONT_KNOW); |
| 1669 | // We have to compile the test expression if it can be reached by |
| 1670 | // control flow falling out of the body or via continue. |
| 1671 | if (node->continue_target()->is_linked()) { |
| 1672 | node->continue_target()->Bind(); |
| 1673 | } |
| 1674 | if (has_valid_frame()) { |
| 1675 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1676 | &body, node->break_target(), true); |
| 1677 | if (has_valid_frame()) { |
| 1678 | // A invalid frame here indicates that control did not |
| 1679 | // fall out of the test expression. |
| 1680 | Branch(true, &body); |
| 1681 | } |
| 1682 | } |
| 1683 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1684 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1685 | } |
| 1686 | |
| 1687 | case LoopStatement::WHILE_LOOP: { |
| 1688 | // If the test is never true and has no side effects there is no need |
| 1689 | // to compile the test or body. |
| 1690 | if (info == ALWAYS_FALSE) break; |
| 1691 | |
| 1692 | // Label the top of the loop with the continue target for the backward |
| 1693 | // CFG edge. |
| 1694 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1695 | node->continue_target()->Bind(); |
| 1696 | |
| 1697 | if (info == DONT_KNOW) { |
| 1698 | JumpTarget body(this); |
| 1699 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1700 | &body, node->break_target(), true); |
| 1701 | if (has_valid_frame()) { |
| 1702 | // A NULL frame indicates that control did not fall out of the |
| 1703 | // test expression. |
| 1704 | Branch(false, node->break_target()); |
| 1705 | } |
| 1706 | if (has_valid_frame() || body.is_linked()) { |
| 1707 | body.Bind(); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | if (has_valid_frame()) { |
| 1712 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1713 | VisitAndSpill(node->body()); |
| 1714 | |
| 1715 | // If control flow can fall out of the body, jump back to the top. |
| 1716 | if (has_valid_frame()) { |
| 1717 | node->continue_target()->Jump(); |
| 1718 | } |
| 1719 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1720 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1721 | } |
| 1722 | |
| 1723 | case LoopStatement::FOR_LOOP: { |
| 1724 | JumpTarget loop(this, JumpTarget::BIDIRECTIONAL); |
| 1725 | |
| 1726 | if (node->init() != NULL) { |
| 1727 | VisitAndSpill(node->init()); |
| 1728 | } |
| 1729 | |
| 1730 | // There is no need to compile the test or body. |
| 1731 | if (info == ALWAYS_FALSE) break; |
| 1732 | |
| 1733 | // If there is no update statement, label the top of the loop with the |
| 1734 | // continue target, otherwise with the loop target. |
| 1735 | if (node->next() == NULL) { |
| 1736 | node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 1737 | node->continue_target()->Bind(); |
| 1738 | } else { |
| 1739 | node->continue_target()->Initialize(this); |
| 1740 | loop.Bind(); |
| 1741 | } |
| 1742 | |
| 1743 | // If the test is always true, there is no need to compile it. |
| 1744 | if (info == DONT_KNOW) { |
| 1745 | JumpTarget body(this); |
| 1746 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1747 | &body, node->break_target(), true); |
| 1748 | if (has_valid_frame()) { |
| 1749 | Branch(false, node->break_target()); |
| 1750 | } |
| 1751 | if (has_valid_frame() || body.is_linked()) { |
| 1752 | body.Bind(); |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | if (has_valid_frame()) { |
| 1757 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1758 | VisitAndSpill(node->body()); |
| 1759 | |
| 1760 | if (node->next() == NULL) { |
| 1761 | // If there is no update statement and control flow can fall out |
| 1762 | // of the loop, jump directly to the continue label. |
| 1763 | if (has_valid_frame()) { |
| 1764 | node->continue_target()->Jump(); |
| 1765 | } |
| 1766 | } else { |
| 1767 | // If there is an update statement and control flow can reach it |
| 1768 | // via falling out of the body of the loop or continuing, we |
| 1769 | // compile the update statement. |
| 1770 | if (node->continue_target()->is_linked()) { |
| 1771 | node->continue_target()->Bind(); |
| 1772 | } |
| 1773 | if (has_valid_frame()) { |
| 1774 | // Record source position of the statement as this code which is |
| 1775 | // after the code for the body actually belongs to the loop |
| 1776 | // statement and not the body. |
| 1777 | CodeForStatementPosition(node); |
| 1778 | VisitAndSpill(node->next()); |
| 1779 | loop.Jump(); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1783 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1784 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1787 | if (node->break_target()->is_linked()) { |
| 1788 | node->break_target()->Bind(); |
| 1789 | } |
| 1790 | node->continue_target()->Unuse(); |
| 1791 | node->break_target()->Unuse(); |
| 1792 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1796 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1797 | #ifdef DEBUG |
| 1798 | int original_height = frame_->height(); |
| 1799 | #endif |
| 1800 | ASSERT(!in_spilled_code()); |
| 1801 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1803 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1804 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1805 | JumpTarget primitive(this); |
| 1806 | JumpTarget jsobject(this); |
| 1807 | JumpTarget fixed_array(this); |
| 1808 | JumpTarget entry(this, JumpTarget::BIDIRECTIONAL); |
| 1809 | JumpTarget end_del_check(this); |
| 1810 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1811 | |
| 1812 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1813 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1816 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1817 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1818 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1819 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1820 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1821 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1822 | |
| 1823 | // Stack layout in body: |
| 1824 | // [iteration counter (Smi)] |
| 1825 | // [length of array] |
| 1826 | // [FixedArray] |
| 1827 | // [Map or 0] |
| 1828 | // [Object] |
| 1829 | |
| 1830 | // Check if enumerable is already a JSObject |
| 1831 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1832 | primitive.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1834 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 1835 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1836 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1837 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1838 | primitive.Bind(); |
| 1839 | frame_->EmitPush(r0); |
| 1840 | Result arg_count = allocator_->Allocate(r0); |
| 1841 | ASSERT(arg_count.is_valid()); |
| 1842 | __ mov(arg_count.reg(), Operand(0)); |
| 1843 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1844 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1845 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1847 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1848 | frame_->EmitPush(r0); |
| 1849 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | |
| 1851 | // If we got a Map, we can do a fast modification check. |
| 1852 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1853 | __ mov(r2, Operand(r0)); |
| 1854 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1855 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1856 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Get enum cache |
| 1859 | __ mov(r1, Operand(r0)); |
| 1860 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1861 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1862 | __ ldr(r2, |
| 1863 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1864 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1865 | frame_->EmitPush(r0); // map |
| 1866 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1867 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1869 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1870 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1871 | frame_->EmitPush(r0); |
| 1872 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1873 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1874 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1875 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1876 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1877 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1878 | |
| 1879 | // 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] | 1880 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1881 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1882 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1883 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1884 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1885 | |
| 1886 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1887 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1888 | // sp[0] : index |
| 1889 | // sp[1] : array/enum cache length |
| 1890 | // sp[2] : array or enum cache |
| 1891 | // sp[3] : 0 or map |
| 1892 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1893 | // Grab the current frame's height for the break and continue |
| 1894 | // targets only after all the state is pushed on the frame. |
| 1895 | node->break_target()->Initialize(this); |
| 1896 | node->continue_target()->Initialize(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1897 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1898 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1899 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1900 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1901 | node->break_target()->Branch(hs); |
| 1902 | |
| 1903 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1904 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1906 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1908 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1909 | |
| 1910 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1911 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | // Check if this (still) matches the map of the enumerable. |
| 1913 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1914 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1915 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1916 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1917 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1918 | |
| 1919 | // 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^] | 1920 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1921 | frame_->EmitPush(r0); |
| 1922 | frame_->EmitPush(r3); // push entry |
| 1923 | Result arg_count_register = allocator_->Allocate(r0); |
| 1924 | ASSERT(arg_count_register.is_valid()); |
| 1925 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1926 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1927 | CALL_JS, |
| 1928 | &arg_count_register, |
| 1929 | 2); |
| 1930 | __ mov(r3, Operand(result.reg())); |
| 1931 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1932 | |
| 1933 | // If the property has been removed while iterating, we just skip it. |
| 1934 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1935 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1937 | end_del_check.Bind(); |
| 1938 | // Store the entry in the 'each' expression and take another spin in the |
| 1939 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1940 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1941 | { Reference each(this, node->each()); |
| 1942 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1943 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1944 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1945 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1946 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1947 | // If the reference was to a slot we rely on the convenient property |
| 1948 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1949 | // right on top of or right underneath a zero-sized reference. |
| 1950 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1951 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1952 | // It's safe to pop the value lying on top of the reference before |
| 1953 | // unloading the reference itself (which preserves the top of stack, |
| 1954 | // ie, now the topmost value of the non-zero sized reference), since |
| 1955 | // we will discard the top of stack after unloading the reference |
| 1956 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1957 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1958 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1961 | // Discard the i'th entry pushed above or else the remainder of the |
| 1962 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1963 | frame_->Drop(); |
| 1964 | |
| 1965 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1966 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1967 | VisitAndSpill(node->body()); |
| 1968 | |
| 1969 | // Next. |
| 1970 | node->continue_target()->Bind(); |
| 1971 | frame_->EmitPop(r0); |
| 1972 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1973 | frame_->EmitPush(r0); |
| 1974 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Cleanup. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1977 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1978 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1981 | exit.Bind(); |
| 1982 | node->continue_target()->Unuse(); |
| 1983 | node->break_target()->Unuse(); |
| 1984 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1988 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1989 | #ifdef DEBUG |
| 1990 | int original_height = frame_->height(); |
| 1991 | #endif |
| 1992 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1994 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1996 | JumpTarget try_block(this); |
| 1997 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 1999 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2000 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2001 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Store the caught exception in the catch variable. |
| 2004 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2005 | ASSERT(ref.is_slot()); |
| 2006 | // Here we make use of the convenient property that it doesn't matter |
| 2007 | // whether a value is immediately on top of or underneath a zero-sized |
| 2008 | // reference. |
| 2009 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2013 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2014 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2015 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2016 | if (frame_ != NULL) { |
| 2017 | exit.Jump(); |
| 2018 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2019 | |
| 2020 | |
| 2021 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2022 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2023 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2024 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2025 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2027 | // Shadow the labels for all escapes from the try block, including |
| 2028 | // returns. During shadowing, the original label is hidden as the |
| 2029 | // LabelShadow and operations on the original actually affect the |
| 2030 | // shadowing label. |
| 2031 | // |
| 2032 | // We should probably try to unify the escaping labels and the return |
| 2033 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2034 | int nof_escapes = node->escaping_targets()->length(); |
| 2035 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2036 | |
| 2037 | // Add the shadow target for the function return. |
| 2038 | static const int kReturnShadowIndex = 0; |
| 2039 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2040 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2041 | function_return_is_shadowed_ = true; |
| 2042 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2043 | |
| 2044 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2046 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2050 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2053 | // After shadowing stops, the original labels are unshadowed and the |
| 2054 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2055 | bool has_unlinks = false; |
| 2056 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2058 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2059 | } |
| 2060 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2061 | |
| 2062 | // Get an external reference to the handler address. |
| 2063 | ExternalReference handler_address(Top::k_handler_address); |
| 2064 | |
| 2065 | // The next handler address is at kNextIndex in the stack. |
| 2066 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2067 | // If we can fall off the end of the try block, unlink from try chain. |
| 2068 | if (has_valid_frame()) { |
| 2069 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2070 | __ mov(r3, Operand(handler_address)); |
| 2071 | __ str(r1, MemOperand(r3)); |
| 2072 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2073 | if (has_unlinks) { |
| 2074 | exit.Jump(); |
| 2075 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2078 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2079 | // jumped to. Deallocate each shadow target. |
| 2080 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2081 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2082 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2083 | shadows[i]->Bind(); |
| 2084 | // Because we can be jumping here (to spilled code) from unspilled |
| 2085 | // code, we need to reestablish a spilled frame at this block. |
| 2086 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2087 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2088 | // Reload sp from the top handler, because some statements that we |
| 2089 | // 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^] | 2090 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2091 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2092 | // The stack pointer was restored to just below the code slot |
| 2093 | // (the topmost slot) in the handler. |
| 2094 | frame_->Forget(frame_->height() - handler_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2095 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2096 | // kNextIndex is off by one because the code slot has already |
| 2097 | // been dropped. |
| 2098 | __ ldr(r1, frame_->ElementAt(kNextIndex - 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2100 | // The code slot has already been dropped from the handler. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2101 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2102 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2103 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2104 | frame_->PrepareForReturn(); |
| 2105 | } |
| 2106 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2108 | delete shadows[i]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2111 | exit.Bind(); |
| 2112 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2116 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2117 | #ifdef DEBUG |
| 2118 | int original_height = frame_->height(); |
| 2119 | #endif |
| 2120 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2121 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2122 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2123 | |
| 2124 | // State: Used to keep track of reason for entering the finally |
| 2125 | // block. Should probably be extended to hold information for |
| 2126 | // break/continue from within the try block. |
| 2127 | enum { FALLING, THROWING, JUMPING }; |
| 2128 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2129 | JumpTarget try_block(this); |
| 2130 | JumpTarget finally_block(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2131 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2132 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2133 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2134 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | // In case of thrown exceptions, this is where we continue. |
| 2136 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2137 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | |
| 2139 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2140 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2142 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2143 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2144 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2145 | // Shadow the labels for all escapes from the try block, including |
| 2146 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2147 | // operations on the original actually affect the shadowing label. |
| 2148 | // |
| 2149 | // We should probably try to unify the escaping labels and the return |
| 2150 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2151 | int nof_escapes = node->escaping_targets()->length(); |
| 2152 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2153 | |
| 2154 | // Add the shadow target for the function return. |
| 2155 | static const int kReturnShadowIndex = 0; |
| 2156 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2157 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2158 | function_return_is_shadowed_ = true; |
| 2159 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2160 | |
| 2161 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2162 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2163 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2167 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2168 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2169 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2170 | // After shadowing stops, the original labels are unshadowed and the |
| 2171 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2173 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | shadows[i]->StopShadowing(); |
| 2175 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2176 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2177 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2179 | // Get an external reference to the handler address. |
| 2180 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2181 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2182 | // The next handler address is at kNextIndex in the stack. |
| 2183 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2184 | // If we can fall off the end of the try block, unlink from the try |
| 2185 | // chain and set the state on the frame to FALLING. |
| 2186 | if (has_valid_frame()) { |
| 2187 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2188 | __ mov(r3, Operand(handler_address)); |
| 2189 | __ str(r1, MemOperand(r3)); |
| 2190 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2191 | |
| 2192 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2193 | // state in r2, then jump around the unlink blocks if any. |
| 2194 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2195 | frame_->EmitPush(r0); |
| 2196 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2197 | if (nof_unlinks > 0) { |
| 2198 | finally_block.Jump(); |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | // Generate code to unlink and set the state for the (formerly) |
| 2203 | // shadowing targets that have been jumped to. |
| 2204 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2205 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2206 | // If we have come from the shadowed return, the return value is |
| 2207 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2208 | // until it is pushed. |
| 2209 | // |
| 2210 | // Because we can be jumping here (to spilled code) from |
| 2211 | // unspilled code, we need to reestablish a spilled frame at |
| 2212 | // this block. |
| 2213 | shadows[i]->Bind(); |
| 2214 | frame_->SpillAll(); |
| 2215 | |
| 2216 | // Reload sp from the top handler, because some statements that |
| 2217 | // we break from (eg, for...in) may have left stuff on the |
| 2218 | // stack. |
| 2219 | __ mov(r3, Operand(handler_address)); |
| 2220 | __ ldr(sp, MemOperand(r3)); |
| 2221 | // The stack pointer was restored to the address slot in the handler. |
| 2222 | ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize); |
| 2223 | frame_->Forget(frame_->height() - handler_height + 1); |
| 2224 | |
| 2225 | // Unlink this handler and drop it from the frame. The next |
| 2226 | // handler address is now on top of the frame. |
| 2227 | frame_->EmitPop(r1); |
| 2228 | __ str(r1, MemOperand(r3)); |
| 2229 | // The top (code) and the second (handler) slot have both been |
| 2230 | // dropped already. |
| 2231 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2); |
| 2232 | |
| 2233 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2234 | // If this label shadowed the function return, materialize the |
| 2235 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2236 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2237 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2238 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2239 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2240 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2241 | } |
| 2242 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2243 | if (--nof_unlinks > 0) { |
| 2244 | // If this is not the last unlink block, jump around the next. |
| 2245 | finally_block.Jump(); |
| 2246 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2250 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2251 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2253 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2254 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2255 | |
| 2256 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2257 | // and the state - while evaluating the finally block. |
| 2258 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2259 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2260 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2261 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2262 | if (has_valid_frame()) { |
| 2263 | // Restore state and return value or faked TOS. |
| 2264 | frame_->EmitPop(r2); |
| 2265 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2268 | // Generate code to jump to the right destination for all used |
| 2269 | // formerly shadowing targets. Deallocate each shadow target. |
| 2270 | for (int i = 0; i < shadows.length(); i++) { |
| 2271 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2272 | JumpTarget* original = shadows[i]->other_target(); |
| 2273 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2274 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2275 | JumpTarget skip(this); |
| 2276 | skip.Branch(ne); |
| 2277 | frame_->PrepareForReturn(); |
| 2278 | original->Jump(); |
| 2279 | skip.Bind(); |
| 2280 | } else { |
| 2281 | original->Branch(eq); |
| 2282 | } |
| 2283 | } |
| 2284 | delete shadows[i]; |
| 2285 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2286 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2287 | if (has_valid_frame()) { |
| 2288 | // Check if we need to rethrow the exception. |
| 2289 | JumpTarget exit(this); |
| 2290 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2291 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2292 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2293 | // Rethrow exception. |
| 2294 | frame_->EmitPush(r0); |
| 2295 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2296 | |
| 2297 | // Done. |
| 2298 | exit.Bind(); |
| 2299 | } |
| 2300 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2304 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2305 | #ifdef DEBUG |
| 2306 | int original_height = frame_->height(); |
| 2307 | #endif |
| 2308 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2309 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2310 | CodeForStatementPosition(node); |
| 2311 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2312 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2313 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2317 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2318 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | ASSERT(boilerplate->IsBoilerplate()); |
| 2320 | |
| 2321 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2322 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2323 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2324 | |
| 2325 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2326 | frame_->EmitPush(cp); |
| 2327 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2328 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2332 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2333 | #ifdef DEBUG |
| 2334 | int original_height = frame_->height(); |
| 2335 | #endif |
| 2336 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2337 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2338 | |
| 2339 | // Build the function boilerplate and instantiate it. |
| 2340 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2341 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2342 | if (HasStackOverflow()) { |
| 2343 | ASSERT(frame_->height() == original_height); |
| 2344 | return; |
| 2345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2346 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2347 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2351 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2352 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2353 | #ifdef DEBUG |
| 2354 | int original_height = frame_->height(); |
| 2355 | #endif |
| 2356 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2357 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2358 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2359 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2363 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2364 | #ifdef DEBUG |
| 2365 | int original_height = frame_->height(); |
| 2366 | #endif |
| 2367 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2368 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2369 | JumpTarget then(this); |
| 2370 | JumpTarget else_(this); |
| 2371 | JumpTarget exit(this); |
| 2372 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2373 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2374 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2375 | then.Bind(); |
| 2376 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2377 | exit.Jump(); |
| 2378 | else_.Bind(); |
| 2379 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2380 | exit.Bind(); |
| 2381 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2385 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2386 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2387 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2388 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2389 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2390 | JumpTarget slow(this); |
| 2391 | JumpTarget done(this); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2392 | |
| 2393 | // Generate fast-case code for variables that might be shadowed by |
| 2394 | // eval-introduced variables. Eval is used a lot without |
| 2395 | // introducing variables. In those cases, we do not want to |
| 2396 | // perform a runtime call for all variables in the scope |
| 2397 | // containing the eval. |
| 2398 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2399 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2400 | // If there was no control flow to slow, we can exit early. |
| 2401 | if (!slow.is_linked()) { |
| 2402 | frame_->EmitPush(r0); |
| 2403 | return; |
| 2404 | } |
| 2405 | |
| 2406 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2407 | |
| 2408 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2409 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2410 | // Only generate the fast case for locals that rewrite to slots. |
| 2411 | // This rules out argument loads. |
| 2412 | if (potential_slot != NULL) { |
| 2413 | __ ldr(r0, |
| 2414 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2415 | r1, |
| 2416 | r2, |
| 2417 | &slow)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2418 | // There is always control flow to slow from |
| 2419 | // ContextSlotOperandCheckExtensions. |
| 2420 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2424 | slow.Bind(); |
| 2425 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2426 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2427 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2428 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2429 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2430 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2431 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2432 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2433 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2434 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2435 | done.Bind(); |
| 2436 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2437 | |
| 2438 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2439 | // Note: We would like to keep the assert below, but it fires because of |
| 2440 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2441 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2442 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2443 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2444 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2445 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2446 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2447 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2448 | // initialized yet) which needs to be converted into the 'undefined' |
| 2449 | // value. |
| 2450 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2451 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2452 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2453 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2454 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2460 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2461 | TypeofState typeof_state, |
| 2462 | Register tmp, |
| 2463 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2464 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2465 | // Check that no extension objects have been created by calls to |
| 2466 | // eval from the current scope to the global scope. |
| 2467 | Register context = cp; |
| 2468 | Scope* s = scope(); |
| 2469 | while (s != NULL) { |
| 2470 | if (s->num_heap_slots() > 0) { |
| 2471 | if (s->calls_eval()) { |
| 2472 | // Check that extension is NULL. |
| 2473 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2474 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2475 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2476 | } |
| 2477 | // Load next context in chain. |
| 2478 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2479 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2480 | context = tmp; |
| 2481 | } |
| 2482 | // If no outer scope calls eval, we do not need to check more |
| 2483 | // context extensions. |
| 2484 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2485 | s = s->outer_scope(); |
| 2486 | } |
| 2487 | |
| 2488 | if (s->is_eval_scope()) { |
| 2489 | Label next, fast; |
| 2490 | if (!context.is(tmp)) __ mov(tmp, Operand(context)); |
| 2491 | __ bind(&next); |
| 2492 | // Terminate at global context. |
| 2493 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2494 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2495 | __ b(eq, &fast); |
| 2496 | // Check that extension is NULL. |
| 2497 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2498 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2499 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2500 | // Load next context in chain. |
| 2501 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2502 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2503 | __ b(&next); |
| 2504 | __ bind(&fast); |
| 2505 | } |
| 2506 | |
| 2507 | // All extension objects were empty and it is safe to use a global |
| 2508 | // load IC call. |
| 2509 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2510 | // Load the global object. |
| 2511 | LoadGlobal(); |
| 2512 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2513 | Result name = allocator_->Allocate(r2); |
| 2514 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2515 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2516 | // Call IC stub. |
| 2517 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2518 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2519 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2520 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2523 | // Drop the global object. The result is in r0. |
| 2524 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2528 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2529 | #ifdef DEBUG |
| 2530 | int original_height = frame_->height(); |
| 2531 | #endif |
| 2532 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2533 | Comment cmnt(masm_, "[ Slot"); |
| 2534 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2535 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2536 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2537 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2538 | |
| 2539 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2540 | #ifdef DEBUG |
| 2541 | int original_height = frame_->height(); |
| 2542 | #endif |
| 2543 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2544 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2545 | |
| 2546 | Variable* var = node->var(); |
| 2547 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2548 | if (expr != NULL) { |
| 2549 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2550 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2551 | ASSERT(var->is_global()); |
| 2552 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2553 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2554 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2555 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2559 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2560 | #ifdef DEBUG |
| 2561 | int original_height = frame_->height(); |
| 2562 | #endif |
| 2563 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2564 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2565 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2566 | frame_->EmitPush(r0); |
| 2567 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2571 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2572 | #ifdef DEBUG |
| 2573 | int original_height = frame_->height(); |
| 2574 | #endif |
| 2575 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2576 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2577 | |
| 2578 | // Retrieve the literal array and check the allocated entry. |
| 2579 | |
| 2580 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2581 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2582 | |
| 2583 | // Load the literals array of the function. |
| 2584 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2585 | |
| 2586 | // Load the literal at the ast saved index. |
| 2587 | int literal_offset = |
| 2588 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2589 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2590 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2591 | JumpTarget done(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2592 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2593 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2594 | |
| 2595 | // If the entry is undefined we call the runtime system to computed |
| 2596 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2597 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2598 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2599 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2600 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2601 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2602 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2603 | frame_->EmitPush(r0); |
| 2604 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2605 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2606 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2607 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2608 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2609 | frame_->EmitPush(r2); |
| 2610 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | |
| 2614 | // This deferred code stub will be used for creating the boilerplate |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2615 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | // Each created boilerplate is stored in the JSFunction and they are |
| 2617 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2618 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2619 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2620 | DeferredObjectLiteral(CodeGenerator* generator, ObjectLiteral* node) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2621 | : DeferredCode(generator), node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2622 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2623 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2624 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2625 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2626 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2627 | private: |
| 2628 | ObjectLiteral* node_; |
| 2629 | }; |
| 2630 | |
| 2631 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2632 | void DeferredObjectLiteral::Generate() { |
| 2633 | // Argument is passed in r1. |
| 2634 | enter()->Bind(); |
| 2635 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 2636 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | // If the entry is undefined we call the runtime system to computed |
| 2638 | // the literal. |
| 2639 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2640 | VirtualFrame* frame = generator()->frame(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2641 | // Literal array (0). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2642 | frame->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2643 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2644 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2645 | frame->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2646 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2647 | __ mov(r0, Operand(node_->constant_properties())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2648 | frame->EmitPush(r0); |
| 2649 | Result boilerplate = |
| 2650 | frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2651 | __ mov(r2, Operand(boilerplate.reg())); |
| 2652 | // Result is returned in r2. |
| 2653 | exit_.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2657 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2658 | #ifdef DEBUG |
| 2659 | int original_height = frame_->height(); |
| 2660 | #endif |
| 2661 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2662 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2663 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2664 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(this, node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | |
| 2666 | // Retrieve the literal array and check the allocated entry. |
| 2667 | |
| 2668 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2669 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Load the literals array of the function. |
| 2672 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2673 | |
| 2674 | // Load the literal at the ast saved index. |
| 2675 | int literal_offset = |
| 2676 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2677 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2678 | |
| 2679 | // Check whether we need to materialize the object literal boilerplate. |
| 2680 | // If so, jump to the deferred code. |
| 2681 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2682 | deferred->enter()->Branch(eq); |
| 2683 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | |
| 2685 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2686 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2687 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2688 | // Clone the boilerplate object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2689 | frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1); |
| 2690 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2691 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2692 | |
| 2693 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2694 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2695 | Literal* key = property->key(); |
| 2696 | Expression* value = property->value(); |
| 2697 | switch (property->kind()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2698 | case ObjectLiteral::Property::CONSTANT: |
| 2699 | break; |
| 2700 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2701 | if (property->value()->AsMaterializedLiteral()->is_simple()) break; |
| 2702 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2703 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2704 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2705 | frame_->EmitPush(r0); // dup the result |
| 2706 | LoadAndSpill(key); |
| 2707 | LoadAndSpill(value); |
| 2708 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2709 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2710 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2711 | break; |
| 2712 | } |
| 2713 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2714 | frame_->EmitPush(r0); |
| 2715 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2716 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2717 | frame_->EmitPush(r0); |
| 2718 | LoadAndSpill(value); |
| 2719 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2720 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | } |
| 2723 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2724 | frame_->EmitPush(r0); |
| 2725 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2726 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2727 | frame_->EmitPush(r0); |
| 2728 | LoadAndSpill(value); |
| 2729 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2730 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | } |
| 2733 | } |
| 2734 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2735 | ASSERT(frame_->height() == original_height + 1); |
| 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | // This deferred code stub will be used for creating the boilerplate |
| 2740 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2741 | // Each created boilerplate is stored in the JSFunction and they are |
| 2742 | // therefore context dependent. |
| 2743 | class DeferredArrayLiteral: public DeferredCode { |
| 2744 | public: |
| 2745 | DeferredArrayLiteral(CodeGenerator* generator, ArrayLiteral* node) |
| 2746 | : DeferredCode(generator), node_(node) { |
| 2747 | set_comment("[ DeferredArrayLiteral"); |
| 2748 | } |
| 2749 | |
| 2750 | virtual void Generate(); |
| 2751 | |
| 2752 | private: |
| 2753 | ArrayLiteral* node_; |
| 2754 | }; |
| 2755 | |
| 2756 | |
| 2757 | void DeferredArrayLiteral::Generate() { |
| 2758 | // Argument is passed in r1. |
| 2759 | enter()->Bind(); |
| 2760 | VirtualFrame::SpilledScope spilled_scope(generator()); |
| 2761 | |
| 2762 | // If the entry is undefined we call the runtime system to computed |
| 2763 | // the literal. |
| 2764 | |
| 2765 | VirtualFrame* frame = generator()->frame(); |
| 2766 | // Literal array (0). |
| 2767 | frame->EmitPush(r1); |
| 2768 | // Literal index (1). |
| 2769 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
| 2770 | frame->EmitPush(r0); |
| 2771 | // Constant properties (2). |
| 2772 | __ mov(r0, Operand(node_->literals())); |
| 2773 | frame->EmitPush(r0); |
| 2774 | Result boilerplate = |
| 2775 | frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2776 | __ mov(r2, Operand(boilerplate.reg())); |
| 2777 | // Result is returned in r2. |
| 2778 | exit_.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2782 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2783 | #ifdef DEBUG |
| 2784 | int original_height = frame_->height(); |
| 2785 | #endif |
| 2786 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2787 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2788 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2789 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2790 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2791 | // Retrieve the literal array and check the allocated entry. |
| 2792 | |
| 2793 | // Load the function of this activation. |
| 2794 | __ ldr(r1, frame_->Function()); |
| 2795 | |
| 2796 | // Load the literals array of the function. |
| 2797 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2798 | |
| 2799 | // Load the literal at the ast saved index. |
| 2800 | int literal_offset = |
| 2801 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2802 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2803 | |
| 2804 | // Check whether we need to materialize the object literal boilerplate. |
| 2805 | // If so, jump to the deferred code. |
| 2806 | __ cmp(r2, Operand(Factory::undefined_value())); |
| 2807 | deferred->enter()->Branch(eq); |
| 2808 | deferred->BindExit(); |
| 2809 | |
| 2810 | // Push the object literal boilerplate. |
| 2811 | frame_->EmitPush(r2); |
| 2812 | |
| 2813 | // Clone the boilerplate object. |
| 2814 | frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1); |
| 2815 | frame_->EmitPush(r0); // save the result |
| 2816 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2817 | |
| 2818 | // Generate code to set the elements in the array that are not |
| 2819 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2820 | for (int i = 0; i < node->values()->length(); i++) { |
| 2821 | Expression* value = node->values()->at(i); |
| 2822 | |
| 2823 | // If value is literal the property value is already |
| 2824 | // set in the boilerplate object. |
| 2825 | if (value->AsLiteral() == NULL) { |
| 2826 | // The property must be set by generated code. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2827 | LoadAndSpill(value); |
| 2828 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2829 | |
| 2830 | // Fetch the object literal |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2831 | __ ldr(r1, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2832 | // Get the elements array. |
| 2833 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2834 | |
| 2835 | // Write to the indexed properties array. |
| 2836 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2837 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2838 | |
| 2839 | // Update the write barrier for the array address. |
| 2840 | __ mov(r3, Operand(offset)); |
| 2841 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2844 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2848 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2849 | #ifdef DEBUG |
| 2850 | int original_height = frame_->height(); |
| 2851 | #endif |
| 2852 | ASSERT(!in_spilled_code()); |
| 2853 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2854 | // Call runtime routine to allocate the catch extension object and |
| 2855 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2856 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2857 | LoadAndSpill(node->key()); |
| 2858 | LoadAndSpill(node->value()); |
| 2859 | Result result = |
| 2860 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2861 | frame_->EmitPush(result.reg()); |
| 2862 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2866 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2867 | #ifdef DEBUG |
| 2868 | int original_height = frame_->height(); |
| 2869 | #endif |
| 2870 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2871 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2872 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2873 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2874 | { Reference target(this, node->target()); |
| 2875 | if (target.is_illegal()) { |
| 2876 | // Fool the virtual frame into thinking that we left the assignment's |
| 2877 | // value on the frame. |
| 2878 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2879 | frame_->EmitPush(r0); |
| 2880 | ASSERT(frame_->height() == original_height + 1); |
| 2881 | return; |
| 2882 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2883 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2884 | if (node->op() == Token::ASSIGN || |
| 2885 | node->op() == Token::INIT_VAR || |
| 2886 | node->op() == Token::INIT_CONST) { |
| 2887 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2888 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2889 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2890 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2891 | Literal* literal = node->value()->AsLiteral(); |
| 2892 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 2893 | SmiOperation(node->binary_op(), literal->handle(), false); |
| 2894 | frame_->EmitPush(r0); |
| 2895 | |
| 2896 | } else { |
| 2897 | LoadAndSpill(node->value()); |
| 2898 | GenericBinaryOperation(node->binary_op()); |
| 2899 | frame_->EmitPush(r0); |
| 2900 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2901 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2902 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2903 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2904 | if (var != NULL && |
| 2905 | (var->mode() == Variable::CONST) && |
| 2906 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2907 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2908 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2909 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2910 | CodeForSourcePosition(node->position()); |
| 2911 | if (node->op() == Token::INIT_CONST) { |
| 2912 | // Dynamic constant initializations must use the function context |
| 2913 | // and initialize the actual constant declared. Dynamic variable |
| 2914 | // initializations are simply assignments and use SetValue. |
| 2915 | target.SetValue(CONST_INIT); |
| 2916 | } else { |
| 2917 | target.SetValue(NOT_CONST_INIT); |
| 2918 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2919 | } |
| 2920 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2921 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2925 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2926 | #ifdef DEBUG |
| 2927 | int original_height = frame_->height(); |
| 2928 | #endif |
| 2929 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2930 | Comment cmnt(masm_, "[ Throw"); |
| 2931 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2932 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2933 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2934 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 2935 | frame_->EmitPush(r0); |
| 2936 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2940 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2941 | #ifdef DEBUG |
| 2942 | int original_height = frame_->height(); |
| 2943 | #endif |
| 2944 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2945 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2946 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2947 | { Reference property(this, node); |
| 2948 | property.GetValueAndSpill(typeof_state()); |
| 2949 | } |
| 2950 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2954 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2955 | #ifdef DEBUG |
| 2956 | int original_height = frame_->height(); |
| 2957 | #endif |
| 2958 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2959 | Comment cmnt(masm_, "[ Call"); |
| 2960 | |
| 2961 | ZoneList<Expression*>* args = node->arguments(); |
| 2962 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2963 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2964 | // Standard function call. |
| 2965 | |
| 2966 | // Check if the function is a variable or a property. |
| 2967 | Expression* function = node->expression(); |
| 2968 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 2969 | Property* property = function->AsProperty(); |
| 2970 | |
| 2971 | // ------------------------------------------------------------------------ |
| 2972 | // Fast-case: Use inline caching. |
| 2973 | // --- |
| 2974 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 2975 | // must be resolved after the arguments have been evaluated. The IC code |
| 2976 | // automatically handles this by loading the arguments before the function |
| 2977 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 2978 | // ------------------------------------------------------------------------ |
| 2979 | |
| 2980 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 2981 | // ---------------------------------- |
| 2982 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 2983 | // ---------------------------------- |
| 2984 | |
| 2985 | // 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] | 2986 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2987 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2988 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2989 | // Pass the global object as the receiver and let the IC stub |
| 2990 | // patch the stack to use the global proxy as 'this' in the |
| 2991 | // invoked function. |
| 2992 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2993 | |
| 2994 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 2995 | int arg_count = args->length(); |
| 2996 | for (int i = 0; i < arg_count; i++) { |
| 2997 | LoadAndSpill(args->at(i)); |
| 2998 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2999 | |
| 3000 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3001 | Handle<Code> stub = ComputeCallInitialize(arg_count); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3002 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3003 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3004 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3005 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3006 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3007 | frame_->Drop(); |
| 3008 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3009 | |
| 3010 | } else if (var != NULL && var->slot() != NULL && |
| 3011 | var->slot()->type() == Slot::LOOKUP) { |
| 3012 | // ---------------------------------- |
| 3013 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3014 | // ---------------------------------- |
| 3015 | |
| 3016 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3017 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3018 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3019 | frame_->EmitPush(r0); |
| 3020 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3021 | // r0: slot value; r1: receiver |
| 3022 | |
| 3023 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3024 | frame_->EmitPush(r0); // function |
| 3025 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Call the function. |
| 3028 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3029 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3030 | |
| 3031 | } else if (property != NULL) { |
| 3032 | // Check if the key is a literal string. |
| 3033 | Literal* literal = property->key()->AsLiteral(); |
| 3034 | |
| 3035 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3036 | // ------------------------------------------------------------------ |
| 3037 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3038 | // ------------------------------------------------------------------ |
| 3039 | |
| 3040 | // 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] | 3041 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3042 | frame_->EmitPush(r0); |
| 3043 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3044 | |
| 3045 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3046 | int arg_count = args->length(); |
| 3047 | for (int i = 0; i < arg_count; i++) { |
| 3048 | LoadAndSpill(args->at(i)); |
| 3049 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3050 | |
| 3051 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3052 | Handle<Code> stub = ComputeCallInitialize(arg_count); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3053 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3054 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3055 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3056 | |
| 3057 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3058 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3059 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3060 | 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] | 3061 | |
| 3062 | } else { |
| 3063 | // ------------------------------------------- |
| 3064 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3065 | // ------------------------------------------- |
| 3066 | |
| 3067 | // Load the function to call from the property through a reference. |
| 3068 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3069 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3070 | |
| 3071 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3072 | if (property->is_synthetic()) { |
| 3073 | LoadGlobalReceiver(r0); |
| 3074 | } else { |
| 3075 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3076 | frame_->EmitPush(r0); |
| 3077 | } |
| 3078 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3079 | // Call the function. |
| 3080 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3081 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | } else { |
| 3085 | // ---------------------------------- |
| 3086 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3087 | // ---------------------------------- |
| 3088 | |
| 3089 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3090 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3091 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3092 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3093 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3094 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3095 | // Call the function. |
| 3096 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3097 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3099 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3103 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3104 | #ifdef DEBUG |
| 3105 | int original_height = frame_->height(); |
| 3106 | #endif |
| 3107 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3108 | Comment cmnt(masm_, "[ CallEval"); |
| 3109 | |
| 3110 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3111 | // the function we need to call and the receiver of the call. |
| 3112 | // Then we call the resolved function using the given arguments. |
| 3113 | |
| 3114 | ZoneList<Expression*>* args = node->arguments(); |
| 3115 | Expression* function = node->expression(); |
| 3116 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3117 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3118 | |
| 3119 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3120 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3121 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3122 | frame_->EmitPush(r2); // Slot for receiver |
| 3123 | int arg_count = args->length(); |
| 3124 | for (int i = 0; i < arg_count; i++) { |
| 3125 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3129 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3130 | frame_->EmitPush(r1); |
| 3131 | if (arg_count > 0) { |
| 3132 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3133 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3134 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3135 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3139 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3140 | |
| 3141 | // Touch up stack with the right values for the function and the receiver. |
| 3142 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3143 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3144 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3145 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3148 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3149 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3150 | CallFunctionStub call_function(arg_count); |
| 3151 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3152 | |
| 3153 | __ ldr(cp, frame_->Context()); |
| 3154 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3155 | frame_->Drop(); |
| 3156 | frame_->EmitPush(r0); |
| 3157 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3161 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3162 | #ifdef DEBUG |
| 3163 | int original_height = frame_->height(); |
| 3164 | #endif |
| 3165 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3166 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3167 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3168 | |
| 3169 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3170 | // expression in new calls must be evaluated before the |
| 3171 | // arguments. This is different from ordinary calls, where the |
| 3172 | // actual function to call is resolved after the arguments have been |
| 3173 | // evaluated. |
| 3174 | |
| 3175 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3176 | // receiver. There is no need to use the global proxy here because |
| 3177 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3178 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3179 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3180 | |
| 3181 | // Push the arguments ("left-to-right") on the stack. |
| 3182 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3183 | int arg_count = args->length(); |
| 3184 | for (int i = 0; i < arg_count; i++) { |
| 3185 | LoadAndSpill(args->at(i)); |
| 3186 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3187 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3188 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3189 | Result num_args = allocator_->Allocate(r0); |
| 3190 | ASSERT(num_args.is_valid()); |
| 3191 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3192 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3193 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3194 | Result function = allocator_->Allocate(r1); |
| 3195 | ASSERT(function.is_valid()); |
| 3196 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3197 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3198 | // Call the construct call builtin that handles allocation and |
| 3199 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3200 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3201 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3202 | Result result = frame_->CallCodeObject(ic, |
| 3203 | RelocInfo::CONSTRUCT_CALL, |
| 3204 | &num_args, |
| 3205 | &function, |
| 3206 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3207 | |
| 3208 | // 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] | 3209 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3210 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3214 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3215 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3216 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3217 | JumpTarget leave(this); |
| 3218 | LoadAndSpill(args->at(0)); |
| 3219 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3220 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3221 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3222 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3223 | // It is a heap object - get map. |
| 3224 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3225 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3226 | // if (!object->IsJSValue()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3227 | __ cmp(r1, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3228 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3229 | // Load the value. |
| 3230 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3231 | leave.Bind(); |
| 3232 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3236 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3237 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3238 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3239 | JumpTarget leave(this); |
| 3240 | LoadAndSpill(args->at(0)); // Load the object. |
| 3241 | LoadAndSpill(args->at(1)); // Load the value. |
| 3242 | frame_->EmitPop(r0); // r0 contains value |
| 3243 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3244 | // if (object->IsSmi()) return object. |
| 3245 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3246 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3247 | // It is a heap object - get map. |
| 3248 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3249 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3250 | // if (!object->IsJSValue()) return object. |
| 3251 | __ cmp(r2, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3252 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3253 | // Store the value. |
| 3254 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3255 | // Update the write barrier. |
| 3256 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3257 | __ RecordWrite(r1, r2, r3); |
| 3258 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3259 | leave.Bind(); |
| 3260 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3264 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3265 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3266 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3267 | LoadAndSpill(args->at(0)); |
| 3268 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3269 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3270 | cc_reg_ = eq; |
| 3271 | } |
| 3272 | |
| 3273 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3274 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3275 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3276 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3277 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3278 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3279 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3280 | LoadAndSpill(args->at(1)); |
| 3281 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3282 | __ CallRuntime(Runtime::kLog, 2); |
| 3283 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3284 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3285 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3286 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3290 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3291 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3292 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3293 | LoadAndSpill(args->at(0)); |
| 3294 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3295 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3296 | cc_reg_ = eq; |
| 3297 | } |
| 3298 | |
| 3299 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3300 | // This should generate code that performs a charCodeAt() call or returns |
| 3301 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3302 | // 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] | 3303 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3304 | VirtualFrame::SpilledScope spilled_scope(this); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3305 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3306 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3307 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3311 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3312 | VirtualFrame::SpilledScope spilled_scope(this); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3313 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3314 | LoadAndSpill(args->at(0)); |
| 3315 | JumpTarget answer(this); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3316 | // We need the CC bits to come out as not_equal in the case where the |
| 3317 | // object is a smi. This can't be done with the usual test opcode so |
| 3318 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3319 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3320 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3321 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3322 | answer.Branch(ne); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3323 | // It is a heap object - get the map. |
| 3324 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3325 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3326 | // Check if the object is a JS array or not. |
| 3327 | __ cmp(r1, Operand(JS_ARRAY_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3328 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3329 | cc_reg_ = eq; |
| 3330 | } |
| 3331 | |
| 3332 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3333 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3334 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3335 | ASSERT(args->length() == 0); |
| 3336 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3337 | // Seed the result with the formal parameters count, which will be used |
| 3338 | // 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] | 3339 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3340 | |
| 3341 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3342 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3343 | frame_->CallStub(&stub, 0); |
| 3344 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3348 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3349 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3350 | ASSERT(args->length() == 1); |
| 3351 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3352 | // Satisfy contract with ArgumentsAccessStub: |
| 3353 | // 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^] | 3354 | LoadAndSpill(args->at(0)); |
| 3355 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3356 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3357 | |
| 3358 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3359 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3360 | frame_->CallStub(&stub, 0); |
| 3361 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3365 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3366 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3367 | ASSERT(args->length() == 2); |
| 3368 | |
| 3369 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3370 | LoadAndSpill(args->at(0)); |
| 3371 | LoadAndSpill(args->at(1)); |
| 3372 | frame_->EmitPop(r0); |
| 3373 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3374 | __ cmp(r0, Operand(r1)); |
| 3375 | cc_reg_ = eq; |
| 3376 | } |
| 3377 | |
| 3378 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3379 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3380 | #ifdef DEBUG |
| 3381 | int original_height = frame_->height(); |
| 3382 | #endif |
| 3383 | VirtualFrame::SpilledScope spilled_scope(this); |
| 3384 | if (CheckForInlineRuntimeCall(node)) { |
| 3385 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3386 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3387 | return; |
| 3388 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3389 | |
| 3390 | ZoneList<Expression*>* args = node->arguments(); |
| 3391 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3392 | Runtime::Function* function = node->function(); |
| 3393 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3394 | if (function != NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3395 | // Push the arguments ("left-to-right"). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3396 | int arg_count = args->length(); |
| 3397 | for (int i = 0; i < arg_count; i++) { |
| 3398 | LoadAndSpill(args->at(i)); |
| 3399 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3400 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3401 | // Call the C runtime function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3402 | frame_->CallRuntime(function, arg_count); |
| 3403 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3404 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3405 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3406 | // Prepare stack for calling JS runtime function. |
| 3407 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3408 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3409 | // Push the builtins object found in the current global object. |
| 3410 | __ ldr(r1, GlobalObject()); |
| 3411 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3412 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3413 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3414 | int arg_count = args->length(); |
| 3415 | for (int i = 0; i < arg_count; i++) { |
| 3416 | LoadAndSpill(args->at(i)); |
| 3417 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3418 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3419 | // Call the JS runtime function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3420 | Handle<Code> stub = ComputeCallInitialize(args->length()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3421 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3422 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3423 | frame_->Drop(); |
| 3424 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3425 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3426 | ASSERT(frame_->height() == original_height + 1); |
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::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3431 | #ifdef DEBUG |
| 3432 | int original_height = frame_->height(); |
| 3433 | #endif |
| 3434 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3435 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3436 | |
| 3437 | Token::Value op = node->op(); |
| 3438 | |
| 3439 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3440 | LoadConditionAndSpill(node->expression(), |
| 3441 | NOT_INSIDE_TYPEOF, |
| 3442 | false_target(), |
| 3443 | true_target(), |
| 3444 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3445 | cc_reg_ = NegateCondition(cc_reg_); |
| 3446 | |
| 3447 | } else if (op == Token::DELETE) { |
| 3448 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3449 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3451 | LoadAndSpill(property->obj()); |
| 3452 | LoadAndSpill(property->key()); |
| 3453 | Result arg_count = allocator_->Allocate(r0); |
| 3454 | ASSERT(arg_count.is_valid()); |
| 3455 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3456 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3457 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3458 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3459 | Slot* slot = variable->slot(); |
| 3460 | if (variable->is_global()) { |
| 3461 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3462 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3463 | frame_->EmitPush(r0); |
| 3464 | Result arg_count = allocator_->Allocate(r0); |
| 3465 | ASSERT(arg_count.is_valid()); |
| 3466 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3467 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3468 | |
| 3469 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3470 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3471 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3472 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3473 | frame_->EmitPush(r0); |
| 3474 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3475 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3476 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3477 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3478 | frame_->EmitPush(r0); |
| 3479 | Result arg_count = allocator_->Allocate(r0); |
| 3480 | ASSERT(arg_count.is_valid()); |
| 3481 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3482 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3483 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3484 | } else { |
| 3485 | // Default: Result of deleting non-global, not dynamically |
| 3486 | // introduced variables is false. |
| 3487 | __ mov(r0, Operand(Factory::false_value())); |
| 3488 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3489 | |
| 3490 | } else { |
| 3491 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3492 | LoadAndSpill(node->expression()); // may have side-effects |
| 3493 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3494 | __ mov(r0, Operand(Factory::true_value())); |
| 3495 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3496 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3497 | |
| 3498 | } else if (op == Token::TYPEOF) { |
| 3499 | // Special case for loading the typeof expression; see comment on |
| 3500 | // LoadTypeofExpression(). |
| 3501 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3502 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3503 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3504 | |
| 3505 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3506 | LoadAndSpill(node->expression()); |
| 3507 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3508 | switch (op) { |
| 3509 | case Token::NOT: |
| 3510 | case Token::DELETE: |
| 3511 | case Token::TYPEOF: |
| 3512 | UNREACHABLE(); // handled above |
| 3513 | break; |
| 3514 | |
| 3515 | case Token::SUB: { |
| 3516 | UnarySubStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3517 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | } |
| 3520 | |
| 3521 | case Token::BIT_NOT: { |
| 3522 | // smi check |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3523 | JumpTarget smi_label(this); |
| 3524 | JumpTarget continue_label(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3525 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3526 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3527 | |
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(0)); // not counting receiver |
| 3532 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3534 | continue_label.Jump(); |
| 3535 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3536 | __ mvn(r0, Operand(r0)); |
| 3537 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3538 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3539 | break; |
| 3540 | } |
| 3541 | |
| 3542 | case Token::VOID: |
| 3543 | // since the stack top is cached in r0, popping and then |
| 3544 | // pushing a value can be done by just writing to r0. |
| 3545 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3546 | break; |
| 3547 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3548 | case Token::ADD: { |
| 3549 | // Smi check. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3550 | JumpTarget continue_label(this); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3551 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3552 | continue_label.Branch(eq); |
| 3553 | frame_->EmitPush(r0); |
| 3554 | Result arg_count = allocator_->Allocate(r0); |
| 3555 | ASSERT(arg_count.is_valid()); |
| 3556 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3557 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3558 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3559 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3560 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3561 | default: |
| 3562 | UNREACHABLE(); |
| 3563 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3564 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3565 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3566 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3567 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3571 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3572 | #ifdef DEBUG |
| 3573 | int original_height = frame_->height(); |
| 3574 | #endif |
| 3575 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3576 | Comment cmnt(masm_, "[ CountOperation"); |
| 3577 | |
| 3578 | bool is_postfix = node->is_postfix(); |
| 3579 | bool is_increment = node->op() == Token::INC; |
| 3580 | |
| 3581 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3582 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3583 | |
| 3584 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3585 | if (is_postfix) { |
| 3586 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3587 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3588 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | |
| 3590 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3591 | if (target.is_illegal()) { |
| 3592 | // Spoof the virtual frame to have the expected height (one higher |
| 3593 | // than on entry). |
| 3594 | if (!is_postfix) { |
| 3595 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3596 | frame_->EmitPush(r0); |
| 3597 | } |
| 3598 | ASSERT(frame_->height() == original_height + 1); |
| 3599 | return; |
| 3600 | } |
| 3601 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3602 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3603 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3604 | JumpTarget slow(this); |
| 3605 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3606 | |
| 3607 | // Load the value (1) into register r1. |
| 3608 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3609 | |
| 3610 | // Check for smi operand. |
| 3611 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3612 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3613 | |
| 3614 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3615 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3616 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3617 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Perform optimistic increment/decrement. |
| 3620 | if (is_increment) { |
| 3621 | __ add(r0, r0, Operand(r1), SetCC); |
| 3622 | } else { |
| 3623 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3624 | } |
| 3625 | |
| 3626 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3627 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3628 | |
| 3629 | // Revert optimistic increment/decrement. |
| 3630 | if (is_increment) { |
| 3631 | __ sub(r0, r0, Operand(r1)); |
| 3632 | } else { |
| 3633 | __ add(r0, r0, Operand(r1)); |
| 3634 | } |
| 3635 | |
| 3636 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3637 | slow.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3638 | |
| 3639 | // Postfix: Convert the operand to a number and store it as the result. |
| 3640 | if (is_postfix) { |
| 3641 | InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3642 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | // Store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3644 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | // Compute the new value by calling the right JavaScript native. |
| 3648 | if (is_increment) { |
| 3649 | InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3650 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3651 | } else { |
| 3652 | InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3653 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3657 | exit.Bind(); |
| 3658 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3659 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3663 | if (is_postfix) frame_->EmitPop(r0); |
| 3664 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3668 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3669 | #ifdef DEBUG |
| 3670 | int original_height = frame_->height(); |
| 3671 | #endif |
| 3672 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3673 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3674 | Token::Value op = node->op(); |
| 3675 | |
| 3676 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3677 | // operators must yield the result of one of the two expressions |
| 3678 | // before any ToBoolean() conversions. This means that the value |
| 3679 | // produced by a && or || operator is not necessarily a boolean. |
| 3680 | |
| 3681 | // NOTE: If the left hand side produces a materialized value (not in |
| 3682 | // the CC register), we force the right hand side to do the |
| 3683 | // same. This is necessary because we may have to branch to the exit |
| 3684 | // after evaluating the left hand side (due to the shortcut |
| 3685 | // semantics), but the compiler must (statically) know if the result |
| 3686 | // of compiling the binary operation is materialized or not. |
| 3687 | |
| 3688 | if (op == Token::AND) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3689 | JumpTarget is_true(this); |
| 3690 | LoadConditionAndSpill(node->left(), |
| 3691 | NOT_INSIDE_TYPEOF, |
| 3692 | &is_true, |
| 3693 | false_target(), |
| 3694 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3695 | if (has_cc()) { |
| 3696 | Branch(false, false_target()); |
| 3697 | |
| 3698 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3699 | is_true.Bind(); |
| 3700 | LoadConditionAndSpill(node->right(), |
| 3701 | NOT_INSIDE_TYPEOF, |
| 3702 | true_target(), |
| 3703 | false_target(), |
| 3704 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3705 | |
| 3706 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3707 | JumpTarget pop_and_continue(this); |
| 3708 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3709 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3710 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3711 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3712 | // Avoid popping the result if it converts to 'false' using the |
| 3713 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3714 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3715 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3716 | Branch(false, &exit); |
| 3717 | |
| 3718 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3719 | pop_and_continue.Bind(); |
| 3720 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3721 | |
| 3722 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3723 | is_true.Bind(); |
| 3724 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3725 | |
| 3726 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3727 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | } else if (op == Token::OR) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3731 | JumpTarget is_false(this); |
| 3732 | LoadConditionAndSpill(node->left(), |
| 3733 | NOT_INSIDE_TYPEOF, |
| 3734 | true_target(), |
| 3735 | &is_false, |
| 3736 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3737 | if (has_cc()) { |
| 3738 | Branch(true, true_target()); |
| 3739 | |
| 3740 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3741 | is_false.Bind(); |
| 3742 | LoadConditionAndSpill(node->right(), |
| 3743 | NOT_INSIDE_TYPEOF, |
| 3744 | true_target(), |
| 3745 | false_target(), |
| 3746 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | |
| 3748 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3749 | JumpTarget pop_and_continue(this); |
| 3750 | JumpTarget exit(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3751 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3752 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3753 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3754 | // Avoid popping the result if it converts to 'true' using the |
| 3755 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3756 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3757 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3758 | Branch(true, &exit); |
| 3759 | |
| 3760 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3761 | pop_and_continue.Bind(); |
| 3762 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3763 | |
| 3764 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3765 | is_false.Bind(); |
| 3766 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3767 | |
| 3768 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3769 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | } else { |
| 3773 | // Optimize for the case where (at least) one of the expressions |
| 3774 | // is a literal small integer. |
| 3775 | Literal* lliteral = node->left()->AsLiteral(); |
| 3776 | Literal* rliteral = node->right()->AsLiteral(); |
| 3777 | |
| 3778 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3779 | LoadAndSpill(node->left()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3780 | SmiOperation(node->op(), rliteral->handle(), false); |
| 3781 | |
| 3782 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3783 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3784 | SmiOperation(node->op(), lliteral->handle(), true); |
| 3785 | |
| 3786 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3787 | LoadAndSpill(node->left()); |
| 3788 | LoadAndSpill(node->right()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3789 | GenericBinaryOperation(node->op()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3790 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3791 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3792 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3793 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3794 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3798 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3799 | #ifdef DEBUG |
| 3800 | int original_height = frame_->height(); |
| 3801 | #endif |
| 3802 | VirtualFrame::SpilledScope spilled_scope(this); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3803 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3804 | frame_->EmitPush(r0); |
| 3805 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3809 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3810 | #ifdef DEBUG |
| 3811 | int original_height = frame_->height(); |
| 3812 | #endif |
| 3813 | VirtualFrame::SpilledScope spilled_scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3814 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3815 | |
| 3816 | // Get the expressions from the node. |
| 3817 | Expression* left = node->left(); |
| 3818 | Expression* right = node->right(); |
| 3819 | Token::Value op = node->op(); |
| 3820 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3821 | // To make null checks efficient, we check if either left or right is the |
| 3822 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3823 | // instead of calling the (very) general runtime routine for checking |
| 3824 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3825 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3826 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3827 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3828 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3829 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3830 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3831 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3832 | LoadAndSpill(left_is_null ? right : left); |
| 3833 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3834 | __ cmp(r0, Operand(Factory::null_value())); |
| 3835 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3836 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3837 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3838 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3839 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3840 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3841 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3842 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3843 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3844 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3845 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3846 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3847 | // It can be an undetectable object. |
| 3848 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3849 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3850 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3851 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3854 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3855 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3856 | return; |
| 3857 | } |
| 3858 | } |
| 3859 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3860 | // To make typeof testing for natives implemented in JavaScript really |
| 3861 | // efficient, we generate special code for expressions of the form: |
| 3862 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3863 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3864 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3865 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3866 | (right->AsLiteral() != NULL && |
| 3867 | right->AsLiteral()->handle()->IsString())) { |
| 3868 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3869 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3870 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3872 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3873 | |
| 3874 | if (check->Equals(Heap::number_symbol())) { |
| 3875 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3876 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3877 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3878 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3879 | cc_reg_ = eq; |
| 3880 | |
| 3881 | } else if (check->Equals(Heap::string_symbol())) { |
| 3882 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3883 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3884 | |
| 3885 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3886 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3887 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3888 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3889 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3890 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3891 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3892 | |
| 3893 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3894 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3895 | cc_reg_ = lt; |
| 3896 | |
| 3897 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 3898 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3899 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3900 | __ cmp(r1, Operand(Factory::false_value())); |
| 3901 | cc_reg_ = eq; |
| 3902 | |
| 3903 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 3904 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3905 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3906 | |
| 3907 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3908 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3909 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3910 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3911 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3912 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3913 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3914 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 3915 | |
| 3916 | cc_reg_ = eq; |
| 3917 | |
| 3918 | } else if (check->Equals(Heap::function_symbol())) { |
| 3919 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3920 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3921 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3922 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3923 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 3924 | cc_reg_ = eq; |
| 3925 | |
| 3926 | } else if (check->Equals(Heap::object_symbol())) { |
| 3927 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3928 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3929 | |
| 3930 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3931 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3932 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3933 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3934 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3935 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 3936 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 3937 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3938 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3939 | |
| 3940 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3941 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3942 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3943 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 3944 | cc_reg_ = le; |
| 3945 | |
| 3946 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3947 | // Uncommon case: typeof testing against a string literal that is |
| 3948 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3949 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3950 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3951 | ASSERT(!has_valid_frame() || |
| 3952 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3953 | return; |
| 3954 | } |
| 3955 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3956 | LoadAndSpill(left); |
| 3957 | LoadAndSpill(right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3958 | switch (op) { |
| 3959 | case Token::EQ: |
| 3960 | Comparison(eq, false); |
| 3961 | break; |
| 3962 | |
| 3963 | case Token::LT: |
| 3964 | Comparison(lt); |
| 3965 | break; |
| 3966 | |
| 3967 | case Token::GT: |
| 3968 | Comparison(gt); |
| 3969 | break; |
| 3970 | |
| 3971 | case Token::LTE: |
| 3972 | Comparison(le); |
| 3973 | break; |
| 3974 | |
| 3975 | case Token::GTE: |
| 3976 | Comparison(ge); |
| 3977 | break; |
| 3978 | |
| 3979 | case Token::EQ_STRICT: |
| 3980 | Comparison(eq, true); |
| 3981 | break; |
| 3982 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3983 | case Token::IN: { |
| 3984 | Result arg_count = allocator_->Allocate(r0); |
| 3985 | ASSERT(arg_count.is_valid()); |
| 3986 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3987 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 3988 | CALL_JS, |
| 3989 | &arg_count, |
| 3990 | 2); |
| 3991 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3992 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3993 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3994 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 3995 | case Token::INSTANCEOF: { |
| 3996 | Result arg_count = allocator_->Allocate(r0); |
| 3997 | ASSERT(arg_count.is_valid()); |
| 3998 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3999 | Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF, |
| 4000 | CALL_JS, |
| 4001 | &arg_count, |
| 4002 | 2); |
| 4003 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4004 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4005 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4006 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4007 | |
| 4008 | default: |
| 4009 | UNREACHABLE(); |
| 4010 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4011 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4012 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4016 | #ifdef DEBUG |
| 4017 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4018 | #endif |
| 4019 | |
| 4020 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | #undef __ |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4022 | #define __ masm-> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4023 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4024 | Handle<String> Reference::GetName() { |
| 4025 | ASSERT(type_ == NAMED); |
| 4026 | Property* property = expression_->AsProperty(); |
| 4027 | if (property == NULL) { |
| 4028 | // Global variable reference treated as a named property reference. |
| 4029 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4030 | ASSERT(proxy->AsVariable() != NULL); |
| 4031 | ASSERT(proxy->AsVariable()->is_global()); |
| 4032 | return proxy->name(); |
| 4033 | } else { |
| 4034 | Literal* raw_name = property->key()->AsLiteral(); |
| 4035 | ASSERT(raw_name != NULL); |
| 4036 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4037 | } |
| 4038 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4039 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4040 | |
| 4041 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4042 | ASSERT(!cgen_->in_spilled_code()); |
| 4043 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4044 | ASSERT(!is_illegal()); |
| 4045 | ASSERT(!cgen_->has_cc()); |
| 4046 | MacroAssembler* masm = cgen_->masm(); |
| 4047 | Property* property = expression_->AsProperty(); |
| 4048 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4049 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | switch (type_) { |
| 4053 | case SLOT: { |
| 4054 | Comment cmnt(masm, "[ Load from Slot"); |
| 4055 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4056 | ASSERT(slot != NULL); |
| 4057 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4058 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4061 | case NAMED: { |
| 4062 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4063 | // distinction between expressions in a typeof and not in a typeof. If |
| 4064 | // there is a chance that reference errors can be thrown below, we |
| 4065 | // must distinguish between the two kinds of loads (typeof expression |
| 4066 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4067 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4068 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4069 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4070 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4071 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4072 | // Setup the name register. |
| 4073 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4074 | ASSERT(name_reg.is_valid()); |
| 4075 | __ mov(name_reg.reg(), Operand(name)); |
| 4076 | ASSERT(var == NULL || var->is_global()); |
| 4077 | RelocInfo::Mode rmode = (var == NULL) |
| 4078 | ? RelocInfo::CODE_TARGET |
| 4079 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4080 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4081 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4082 | break; |
| 4083 | } |
| 4084 | |
| 4085 | case KEYED: { |
| 4086 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4087 | // 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] | 4088 | |
| 4089 | // TODO(181): Implement inlined version of array indexing once |
| 4090 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4091 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4092 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4093 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4094 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4095 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4096 | ASSERT(var == NULL || var->is_global()); |
| 4097 | RelocInfo::Mode rmode = (var == NULL) |
| 4098 | ? RelocInfo::CODE_TARGET |
| 4099 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4100 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4101 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4102 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | default: |
| 4106 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4111 | void Reference::SetValue(InitState init_state) { |
| 4112 | ASSERT(!is_illegal()); |
| 4113 | ASSERT(!cgen_->has_cc()); |
| 4114 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4115 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4116 | Property* property = expression_->AsProperty(); |
| 4117 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4118 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4119 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4120 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4121 | switch (type_) { |
| 4122 | case SLOT: { |
| 4123 | Comment cmnt(masm, "[ Store to Slot"); |
| 4124 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4125 | ASSERT(slot != NULL); |
| 4126 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4127 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4128 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4129 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4130 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4131 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4132 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4133 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4134 | if (init_state == CONST_INIT) { |
| 4135 | // Same as the case for a normal store, but ignores attribute |
| 4136 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4137 | // const properties (introduced via eval("const foo = (some |
| 4138 | // expr);")). Also, uses the current function context instead of |
| 4139 | // the top context. |
| 4140 | // |
| 4141 | // Note that we must declare the foo upon entry of eval(), via a |
| 4142 | // context slot declaration, but we cannot initialize it at the |
| 4143 | // same time, because the const declaration may be at the end of |
| 4144 | // the eval code (sigh...) and the const variable may have been |
| 4145 | // used before (where its value is 'undefined'). Thus, we can only |
| 4146 | // do the initialization when we actually encounter the expression |
| 4147 | // and when the expression operands are defined and valid, and |
| 4148 | // thus we need the split into 2 operations: declaration of the |
| 4149 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4150 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4151 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4152 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4153 | } |
| 4154 | // Storing a variable must keep the (new) value on the expression |
| 4155 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4156 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4157 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4158 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4159 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4160 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4161 | JumpTarget exit(cgen_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4162 | if (init_state == CONST_INIT) { |
| 4163 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4164 | // Only the first const initialization must be executed (the slot |
| 4165 | // still contains 'the hole' value). When the assignment is |
| 4166 | // executed, the code is identical to a normal store (see below). |
| 4167 | Comment cmnt(masm, "[ Init const"); |
| 4168 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4169 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4170 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4171 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4172 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4173 | // We must execute the store. Storing a variable must keep the |
| 4174 | // (new) value on the stack. This is necessary for compiling |
| 4175 | // assignment expressions. |
| 4176 | // |
| 4177 | // Note: We will reach here even with slot->var()->mode() == |
| 4178 | // Variable::CONST because of const declarations which will |
| 4179 | // initialize consts to 'the hole' value and by doing so, end up |
| 4180 | // calling this code. r2 may be loaded with context; used below in |
| 4181 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4182 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4183 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4184 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4185 | if (slot->type() == Slot::CONTEXT) { |
| 4186 | // Skip write barrier if the written value is a smi. |
| 4187 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4188 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4189 | // r2 is loaded with context when calling SlotOperand above. |
| 4190 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4191 | __ mov(r3, Operand(offset)); |
| 4192 | __ RecordWrite(r2, r3, r1); |
| 4193 | } |
| 4194 | // If we definitely did not jump over the assignment, we do not need |
| 4195 | // to bind the exit label. Doing so can defeat peephole |
| 4196 | // optimization. |
| 4197 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4198 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4199 | } |
| 4200 | } |
| 4201 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4204 | case NAMED: { |
| 4205 | Comment cmnt(masm, "[ Store to named Property"); |
| 4206 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4207 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4208 | Handle<String> name(GetName()); |
| 4209 | |
| 4210 | Result value = cgen_->allocator()->Allocate(r0); |
| 4211 | ASSERT(value.is_valid()); |
| 4212 | frame->EmitPop(value.reg()); |
| 4213 | |
| 4214 | // Setup the name register. |
| 4215 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4216 | ASSERT(property_name.is_valid()); |
| 4217 | __ mov(property_name.reg(), Operand(name)); |
| 4218 | Result answer = frame->CallCodeObject(ic, |
| 4219 | RelocInfo::CODE_TARGET, |
| 4220 | &value, |
| 4221 | &property_name, |
| 4222 | 0); |
| 4223 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | break; |
| 4225 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4226 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4227 | case KEYED: { |
| 4228 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4229 | Property* property = expression_->AsProperty(); |
| 4230 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4231 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4232 | |
| 4233 | // Call IC code. |
| 4234 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4235 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 4236 | Result value = cgen_->allocator()->Allocate(r0); |
| 4237 | ASSERT(value.is_valid()); |
| 4238 | frame->EmitPop(value.reg()); // value |
| 4239 | Result result = |
| 4240 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4241 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4242 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4243 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | |
| 4245 | default: |
| 4246 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | |
| 4251 | void GetPropertyStub::Generate(MacroAssembler* masm) { |
| 4252 | // sp[0]: key |
| 4253 | // sp[1]: receiver |
| 4254 | Label slow, fast; |
| 4255 | // Get the key and receiver object from the stack. |
| 4256 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 4257 | // Check that the key is a smi. |
| 4258 | __ tst(r0, Operand(kSmiTagMask)); |
| 4259 | __ b(ne, &slow); |
| 4260 | __ mov(r0, Operand(r0, ASR, kSmiTagSize)); |
| 4261 | // Check that the object isn't a smi. |
| 4262 | __ tst(r1, Operand(kSmiTagMask)); |
| 4263 | __ b(eq, &slow); |
| 4264 | |
| 4265 | // Check that the object is some kind of JS object EXCEPT JS Value type. |
| 4266 | // In the case that the object is a value-wrapper object, |
| 4267 | // we enter the runtime system to make sure that indexing into string |
| 4268 | // objects work as intended. |
| 4269 | ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE); |
| 4270 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4271 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4272 | __ cmp(r2, Operand(JS_OBJECT_TYPE)); |
| 4273 | __ b(lt, &slow); |
| 4274 | |
| 4275 | // Get the elements array of the object. |
| 4276 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 4277 | // Check that the object is in fast mode (not dictionary). |
| 4278 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4279 | __ cmp(r3, Operand(Factory::hash_table_map())); |
| 4280 | __ b(eq, &slow); |
| 4281 | // Check that the key (index) is within bounds. |
| 4282 | __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset)); |
| 4283 | __ cmp(r0, Operand(r3)); |
| 4284 | __ b(lo, &fast); |
| 4285 | |
| 4286 | // Slow case: Push extra copies of the arguments (2). |
| 4287 | __ bind(&slow); |
| 4288 | __ ldm(ia, sp, r0.bit() | r1.bit()); |
| 4289 | __ stm(db_w, sp, r0.bit() | r1.bit()); |
| 4290 | // Do tail-call to runtime routine. |
| 4291 | __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2); |
| 4292 | |
| 4293 | // Fast case: Do the load. |
| 4294 | __ bind(&fast); |
| 4295 | __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4296 | __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2)); |
| 4297 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 4298 | // In case the loaded value is the_hole we have to consult GetProperty |
| 4299 | // to ensure the prototype chain is searched. |
| 4300 | __ b(eq, &slow); |
| 4301 | |
| 4302 | __ StubReturn(1); |
| 4303 | } |
| 4304 | |
| 4305 | |
| 4306 | void SetPropertyStub::Generate(MacroAssembler* masm) { |
| 4307 | // r0 : value |
| 4308 | // sp[0] : key |
| 4309 | // sp[1] : receiver |
| 4310 | |
| 4311 | Label slow, fast, array, extra, exit; |
| 4312 | // Get the key and the object from the stack. |
| 4313 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver |
| 4314 | // Check that the key is a smi. |
| 4315 | __ tst(r1, Operand(kSmiTagMask)); |
| 4316 | __ b(ne, &slow); |
| 4317 | // Check that the object isn't a smi. |
| 4318 | __ tst(r3, Operand(kSmiTagMask)); |
| 4319 | __ b(eq, &slow); |
| 4320 | // Get the type of the object from its map. |
| 4321 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4322 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4323 | // Check if the object is a JS array or not. |
| 4324 | __ cmp(r2, Operand(JS_ARRAY_TYPE)); |
| 4325 | __ b(eq, &array); |
| 4326 | // Check that the object is some kind of JS object. |
| 4327 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4328 | __ b(lt, &slow); |
| 4329 | |
| 4330 | |
| 4331 | // Object case: Check key against length in the elements array. |
| 4332 | __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 4333 | // Check that the object is in fast mode (not dictionary). |
| 4334 | __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 4335 | __ cmp(r2, Operand(Factory::hash_table_map())); |
| 4336 | __ b(eq, &slow); |
| 4337 | // Untag the key (for checking against untagged length in the fixed array). |
| 4338 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 4339 | // Compute address to store into and check array bounds. |
| 4340 | __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4341 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2)); |
| 4342 | __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset)); |
| 4343 | __ cmp(r1, Operand(ip)); |
| 4344 | __ b(lo, &fast); |
| 4345 | |
| 4346 | |
| 4347 | // Slow case: Push extra copies of the arguments (3). |
| 4348 | __ bind(&slow); |
| 4349 | __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object |
| 4350 | __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit()); |
| 4351 | // Do tail-call to runtime routine. |
| 4352 | __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3); |
| 4353 | |
| 4354 | |
| 4355 | // Extra capacity case: Check if there is extra capacity to |
| 4356 | // perform the store and update the length. Used for adding one |
| 4357 | // element to the array by writing to array[array.length]. |
| 4358 | // r0 == value, r1 == key, r2 == elements, r3 == object |
| 4359 | __ bind(&extra); |
| 4360 | __ b(ne, &slow); // do not leave holes in the array |
| 4361 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag |
| 4362 | __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset)); |
| 4363 | __ cmp(r1, Operand(ip)); |
| 4364 | __ b(hs, &slow); |
| 4365 | __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag |
| 4366 | __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment |
| 4367 | __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 4368 | __ mov(r3, Operand(r2)); |
| 4369 | // NOTE: Computing the address to store into must take the fact |
| 4370 | // that the key has been incremented into account. |
| 4371 | int displacement = Array::kHeaderSize - kHeapObjectTag - |
| 4372 | ((1 << kSmiTagSize) * 2); |
| 4373 | __ add(r2, r2, Operand(displacement)); |
| 4374 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4375 | __ b(&fast); |
| 4376 | |
| 4377 | |
| 4378 | // Array case: Get the length and the elements array from the JS |
| 4379 | // array. Check that the array is in fast mode; if it is the |
| 4380 | // length is always a smi. |
| 4381 | // r0 == value, r3 == object |
| 4382 | __ bind(&array); |
| 4383 | __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset)); |
| 4384 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 4385 | __ cmp(r1, Operand(Factory::hash_table_map())); |
| 4386 | __ b(eq, &slow); |
| 4387 | |
| 4388 | // Check the key against the length in the array, compute the |
| 4389 | // address to store into and fall through to fast case. |
| 4390 | __ ldr(r1, MemOperand(sp)); |
| 4391 | // r0 == value, r1 == key, r2 == elements, r3 == object. |
| 4392 | __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset)); |
| 4393 | __ cmp(r1, Operand(ip)); |
| 4394 | __ b(hs, &extra); |
| 4395 | __ mov(r3, Operand(r2)); |
| 4396 | __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag)); |
| 4397 | __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4398 | |
| 4399 | |
| 4400 | // Fast case: Do the store. |
| 4401 | // r0 == value, r2 == address to store into, r3 == elements |
| 4402 | __ bind(&fast); |
| 4403 | __ str(r0, MemOperand(r2)); |
| 4404 | // Skip write barrier if the written value is a smi. |
| 4405 | __ tst(r0, Operand(kSmiTagMask)); |
| 4406 | __ b(eq, &exit); |
| 4407 | // Update write barrier for the elements array address. |
| 4408 | __ sub(r1, r2, Operand(r3)); |
| 4409 | __ RecordWrite(r3, r1, r2); |
| 4410 | __ bind(&exit); |
| 4411 | __ StubReturn(1); |
| 4412 | } |
| 4413 | |
| 4414 | |
| 4415 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 4416 | // r1 : x |
| 4417 | // r0 : y |
| 4418 | // result : r0 |
| 4419 | |
| 4420 | switch (op_) { |
| 4421 | case Token::ADD: { |
| 4422 | Label slow, exit; |
| 4423 | // fast path |
| 4424 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4425 | __ add(r0, r1, Operand(r0), SetCC); // add y optimistically |
| 4426 | // go slow-path in case of overflow |
| 4427 | __ b(vs, &slow); |
| 4428 | // go slow-path in case of non-smi operands |
| 4429 | ASSERT(kSmiTag == 0); // adjust code below |
| 4430 | __ tst(r2, Operand(kSmiTagMask)); |
| 4431 | __ b(eq, &exit); |
| 4432 | // slow path |
| 4433 | __ bind(&slow); |
| 4434 | __ sub(r0, r0, Operand(r1)); // revert optimistic add |
| 4435 | __ push(r1); |
| 4436 | __ push(r0); |
| 4437 | __ mov(r0, Operand(1)); // set number of arguments |
| 4438 | __ InvokeBuiltin(Builtins::ADD, JUMP_JS); |
| 4439 | // done |
| 4440 | __ bind(&exit); |
| 4441 | break; |
| 4442 | } |
| 4443 | |
| 4444 | case Token::SUB: { |
| 4445 | Label slow, exit; |
| 4446 | // fast path |
| 4447 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4448 | __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically |
| 4449 | // go slow-path in case of overflow |
| 4450 | __ b(vs, &slow); |
| 4451 | // go slow-path in case of non-smi operands |
| 4452 | ASSERT(kSmiTag == 0); // adjust code below |
| 4453 | __ tst(r2, Operand(kSmiTagMask)); |
| 4454 | __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result |
| 4455 | __ b(eq, &exit); |
| 4456 | // slow path |
| 4457 | __ bind(&slow); |
| 4458 | __ push(r1); |
| 4459 | __ push(r0); |
| 4460 | __ mov(r0, Operand(1)); // set number of arguments |
| 4461 | __ InvokeBuiltin(Builtins::SUB, JUMP_JS); |
| 4462 | // done |
| 4463 | __ bind(&exit); |
| 4464 | break; |
| 4465 | } |
| 4466 | |
| 4467 | case Token::MUL: { |
| 4468 | Label slow, exit; |
| 4469 | // tag check |
| 4470 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4471 | ASSERT(kSmiTag == 0); // adjust code below |
| 4472 | __ tst(r2, Operand(kSmiTagMask)); |
| 4473 | __ b(ne, &slow); |
| 4474 | // remove tag from one operand (but keep sign), so that result is smi |
| 4475 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
| 4476 | // do multiplication |
| 4477 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1 |
| 4478 | // go slow on overflows (overflow bit is not set) |
| 4479 | __ mov(ip, Operand(r3, ASR, 31)); |
| 4480 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 4481 | __ b(ne, &slow); |
| 4482 | // go slow on zero result to handle -0 |
| 4483 | __ tst(r3, Operand(r3)); |
| 4484 | __ mov(r0, Operand(r3), LeaveCC, ne); |
| 4485 | __ b(ne, &exit); |
| 4486 | // slow case |
| 4487 | __ bind(&slow); |
| 4488 | __ push(r1); |
| 4489 | __ push(r0); |
| 4490 | __ mov(r0, Operand(1)); // set number of arguments |
| 4491 | __ InvokeBuiltin(Builtins::MUL, JUMP_JS); |
| 4492 | // done |
| 4493 | __ bind(&exit); |
| 4494 | break; |
| 4495 | } |
| 4496 | |
| 4497 | case Token::BIT_OR: |
| 4498 | case Token::BIT_AND: |
| 4499 | case Token::BIT_XOR: { |
| 4500 | Label slow, exit; |
| 4501 | // tag check |
| 4502 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4503 | ASSERT(kSmiTag == 0); // adjust code below |
| 4504 | __ tst(r2, Operand(kSmiTagMask)); |
| 4505 | __ b(ne, &slow); |
| 4506 | switch (op_) { |
| 4507 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 4508 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 4509 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 4510 | default: UNREACHABLE(); |
| 4511 | } |
| 4512 | __ b(&exit); |
| 4513 | __ bind(&slow); |
| 4514 | __ push(r1); // restore stack |
| 4515 | __ push(r0); |
| 4516 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4517 | switch (op_) { |
| 4518 | case Token::BIT_OR: |
| 4519 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 4520 | break; |
| 4521 | case Token::BIT_AND: |
| 4522 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 4523 | break; |
| 4524 | case Token::BIT_XOR: |
| 4525 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 4526 | break; |
| 4527 | default: |
| 4528 | UNREACHABLE(); |
| 4529 | } |
| 4530 | __ bind(&exit); |
| 4531 | break; |
| 4532 | } |
| 4533 | |
| 4534 | case Token::SHL: |
| 4535 | case Token::SHR: |
| 4536 | case Token::SAR: { |
| 4537 | Label slow, exit; |
| 4538 | // tag check |
| 4539 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4540 | ASSERT(kSmiTag == 0); // adjust code below |
| 4541 | __ tst(r2, Operand(kSmiTagMask)); |
| 4542 | __ b(ne, &slow); |
| 4543 | // remove tags from operands (but keep sign) |
| 4544 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 4545 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 4546 | // use only the 5 least significant bits of the shift count |
| 4547 | __ and_(r2, r2, Operand(0x1f)); |
| 4548 | // perform operation |
| 4549 | switch (op_) { |
| 4550 | case Token::SAR: |
| 4551 | __ mov(r3, Operand(r3, ASR, r2)); |
| 4552 | // no checks of result necessary |
| 4553 | break; |
| 4554 | |
| 4555 | case Token::SHR: |
| 4556 | __ mov(r3, Operand(r3, LSR, r2)); |
| 4557 | // check that the *unsigned* result fits in a smi |
| 4558 | // neither of the two high-order bits can be set: |
| 4559 | // - 0x80000000: high bit would be lost when smi tagging |
| 4560 | // - 0x40000000: this number would convert to negative when |
| 4561 | // smi tagging these two cases can only happen with shifts |
| 4562 | // by 0 or 1 when handed a valid smi |
| 4563 | __ and_(r2, r3, Operand(0xc0000000), SetCC); |
| 4564 | __ b(ne, &slow); |
| 4565 | break; |
| 4566 | |
| 4567 | case Token::SHL: |
| 4568 | __ mov(r3, Operand(r3, LSL, r2)); |
| 4569 | // check that the *signed* result fits in a smi |
| 4570 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 4571 | __ b(mi, &slow); |
| 4572 | break; |
| 4573 | |
| 4574 | default: UNREACHABLE(); |
| 4575 | } |
| 4576 | // tag result and store it in r0 |
| 4577 | ASSERT(kSmiTag == 0); // adjust code below |
| 4578 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
| 4579 | __ b(&exit); |
| 4580 | // slow case |
| 4581 | __ bind(&slow); |
| 4582 | __ push(r1); // restore stack |
| 4583 | __ push(r0); |
| 4584 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4585 | switch (op_) { |
| 4586 | case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break; |
| 4587 | case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break; |
| 4588 | case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break; |
| 4589 | default: UNREACHABLE(); |
| 4590 | } |
| 4591 | __ bind(&exit); |
| 4592 | break; |
| 4593 | } |
| 4594 | |
| 4595 | default: UNREACHABLE(); |
| 4596 | } |
| 4597 | __ Ret(); |
| 4598 | } |
| 4599 | |
| 4600 | |
| 4601 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 4602 | Label within_limit; |
| 4603 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 4604 | __ ldr(ip, MemOperand(ip)); |
| 4605 | __ cmp(sp, Operand(ip)); |
| 4606 | __ b(hs, &within_limit); |
| 4607 | // Do tail-call to runtime routine. |
| 4608 | __ push(r0); |
| 4609 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 4610 | __ bind(&within_limit); |
| 4611 | |
| 4612 | __ StubReturn(1); |
| 4613 | } |
| 4614 | |
| 4615 | |
| 4616 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 4617 | Label undo; |
| 4618 | Label slow; |
| 4619 | Label done; |
| 4620 | |
| 4621 | // Enter runtime system if the value is not a smi. |
| 4622 | __ tst(r0, Operand(kSmiTagMask)); |
| 4623 | __ b(ne, &slow); |
| 4624 | |
| 4625 | // Enter runtime system if the value of the expression is zero |
| 4626 | // to make sure that we switch between 0 and -0. |
| 4627 | __ cmp(r0, Operand(0)); |
| 4628 | __ b(eq, &slow); |
| 4629 | |
| 4630 | // The value of the expression is a smi that is not zero. Try |
| 4631 | // optimistic subtraction '0 - value'. |
| 4632 | __ rsb(r1, r0, Operand(0), SetCC); |
| 4633 | __ b(vs, &slow); |
| 4634 | |
| 4635 | // If result is a smi we are done. |
| 4636 | __ tst(r1, Operand(kSmiTagMask)); |
| 4637 | __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result |
| 4638 | __ b(eq, &done); |
| 4639 | |
| 4640 | // Enter runtime system. |
| 4641 | __ bind(&slow); |
| 4642 | __ push(r0); |
| 4643 | __ mov(r0, Operand(0)); // set number of arguments |
| 4644 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 4645 | |
| 4646 | __ bind(&done); |
| 4647 | __ StubReturn(1); |
| 4648 | } |
| 4649 | |
| 4650 | |
| 4651 | void InvokeBuiltinStub::Generate(MacroAssembler* masm) { |
| 4652 | __ push(r0); |
| 4653 | __ mov(r0, Operand(0)); // set number of arguments |
| 4654 | switch (kind_) { |
| 4655 | case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break; |
| 4656 | case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break; |
| 4657 | case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break; |
| 4658 | default: UNREACHABLE(); |
| 4659 | } |
| 4660 | __ StubReturn(argc_); |
| 4661 | } |
| 4662 | |
| 4663 | |
| 4664 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 4665 | // r0 holds exception |
| 4666 | ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code |
| 4667 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4668 | __ ldr(sp, MemOperand(r3)); |
| 4669 | __ pop(r2); // pop next in chain |
| 4670 | __ str(r2, MemOperand(r3)); |
| 4671 | // restore parameter- and frame-pointer and pop state. |
| 4672 | __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit()); |
| 4673 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4674 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4675 | __ cmp(fp, Operand(0)); |
| 4676 | // Set cp to NULL if fp is NULL. |
| 4677 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4678 | // Restore cp otherwise. |
| 4679 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 4680 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 4681 | __ pop(pc); |
| 4682 | } |
| 4683 | |
| 4684 | |
| 4685 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
| 4686 | // Fetch top stack handler. |
| 4687 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4688 | __ ldr(r3, MemOperand(r3)); |
| 4689 | |
| 4690 | // Unwind the handlers until the ENTRY handler is found. |
| 4691 | Label loop, done; |
| 4692 | __ bind(&loop); |
| 4693 | // Load the type of the current stack handler. |
| 4694 | const int kStateOffset = StackHandlerConstants::kAddressDisplacement + |
| 4695 | StackHandlerConstants::kStateOffset; |
| 4696 | __ ldr(r2, MemOperand(r3, kStateOffset)); |
| 4697 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 4698 | __ b(eq, &done); |
| 4699 | // Fetch the next handler in the list. |
| 4700 | const int kNextOffset = StackHandlerConstants::kAddressDisplacement + |
| 4701 | StackHandlerConstants::kNextOffset; |
| 4702 | __ ldr(r3, MemOperand(r3, kNextOffset)); |
| 4703 | __ jmp(&loop); |
| 4704 | __ bind(&done); |
| 4705 | |
| 4706 | // Set the top handler address to next handler past the current ENTRY handler. |
| 4707 | __ ldr(r0, MemOperand(r3, kNextOffset)); |
| 4708 | __ mov(r2, Operand(ExternalReference(Top::k_handler_address))); |
| 4709 | __ str(r0, MemOperand(r2)); |
| 4710 | |
| 4711 | // Set external caught exception to false. |
| 4712 | __ mov(r0, Operand(false)); |
| 4713 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 4714 | __ mov(r2, Operand(external_caught)); |
| 4715 | __ str(r0, MemOperand(r2)); |
| 4716 | |
| 4717 | // Set pending exception and r0 to out of memory exception. |
| 4718 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4719 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4720 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 4721 | __ str(r0, MemOperand(r2)); |
| 4722 | |
| 4723 | // Restore the stack to the address of the ENTRY handler |
| 4724 | __ mov(sp, Operand(r3)); |
| 4725 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4726 | // Stack layout at this point. See also PushTryHandler |
| 4727 | // r3, sp -> next handler |
| 4728 | // state (ENTRY) |
| 4729 | // pp |
| 4730 | // fp |
| 4731 | // lr |
| 4732 | |
| 4733 | // Discard ENTRY state (r2 is not used), and restore parameter- |
| 4734 | // and frame-pointer and pop state. |
| 4735 | __ 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] | 4736 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4737 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4738 | __ cmp(fp, Operand(0)); |
| 4739 | // Set cp to NULL if fp is NULL. |
| 4740 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4741 | // Restore cp otherwise. |
| 4742 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
| 4743 | if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 4744 | __ pop(pc); |
| 4745 | } |
| 4746 | |
| 4747 | |
| 4748 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 4749 | Label* throw_normal_exception, |
| 4750 | Label* throw_out_of_memory_exception, |
| 4751 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4752 | bool do_gc, |
| 4753 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4754 | // r0: result parameter for PerformGC, if any |
| 4755 | // r4: number of arguments including receiver (C callee-saved) |
| 4756 | // r5: pointer to builtin function (C callee-saved) |
| 4757 | // r6: pointer to the first argument (C callee-saved) |
| 4758 | |
| 4759 | if (do_gc) { |
| 4760 | // Passing r0. |
| 4761 | __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY); |
| 4762 | } |
| 4763 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4764 | ExternalReference scope_depth = |
| 4765 | ExternalReference::heap_always_allocate_scope_depth(); |
| 4766 | if (always_allocate) { |
| 4767 | __ mov(r0, Operand(scope_depth)); |
| 4768 | __ ldr(r1, MemOperand(r0)); |
| 4769 | __ add(r1, r1, Operand(1)); |
| 4770 | __ str(r1, MemOperand(r0)); |
| 4771 | } |
| 4772 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4773 | // Call C built-in. |
| 4774 | // r0 = argc, r1 = argv |
| 4775 | __ mov(r0, Operand(r4)); |
| 4776 | __ mov(r1, Operand(r6)); |
| 4777 | |
| 4778 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 4779 | // frames, we need to know where the return address is. Right now, |
| 4780 | // we push it on the stack to be able to find it again, but we never |
| 4781 | // restore from it in case of changes, which makes it impossible to |
| 4782 | // support moving the C entry code stub. This should be fixed, but currently |
| 4783 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 4784 | // sequence that it is not moving ever. |
| 4785 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 4786 | __ push(lr); |
| 4787 | #if !defined(__arm__) |
| 4788 | // Notify the simulator of the transition to C code. |
| 4789 | __ swi(assembler::arm::call_rt_r5); |
| 4790 | #else /* !defined(__arm__) */ |
| 4791 | __ mov(pc, Operand(r5)); |
| 4792 | #endif /* !defined(__arm__) */ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4793 | |
| 4794 | if (always_allocate) { |
| 4795 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 4796 | // though (contain the result). |
| 4797 | __ mov(r2, Operand(scope_depth)); |
| 4798 | __ ldr(r3, MemOperand(r2)); |
| 4799 | __ sub(r3, r3, Operand(1)); |
| 4800 | __ str(r3, MemOperand(r2)); |
| 4801 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4802 | |
| 4803 | // check for failure result |
| 4804 | Label failure_returned; |
| 4805 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 4806 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 4807 | __ add(r2, r0, Operand(1)); |
| 4808 | __ tst(r2, Operand(kFailureTagMask)); |
| 4809 | __ b(eq, &failure_returned); |
| 4810 | |
| 4811 | // Exit C frame and return. |
| 4812 | // r0:r1: result |
| 4813 | // sp: stack pointer |
| 4814 | // fp: frame pointer |
| 4815 | // pp: caller's parameter pointer pp (restored as C callee-saved) |
| 4816 | __ LeaveExitFrame(frame_type); |
| 4817 | |
| 4818 | // check if we should retry or throw exception |
| 4819 | Label retry; |
| 4820 | __ bind(&failure_returned); |
| 4821 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 4822 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 4823 | __ b(eq, &retry); |
| 4824 | |
| 4825 | Label continue_exception; |
| 4826 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 4827 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 4828 | __ b(ne, &continue_exception); |
| 4829 | |
| 4830 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4831 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4832 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4833 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4834 | __ ldr(r0, MemOperand(ip)); |
| 4835 | __ str(r3, MemOperand(ip)); |
| 4836 | |
| 4837 | __ bind(&continue_exception); |
| 4838 | // Special handling of out of memory exception. |
| 4839 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4840 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4841 | __ b(eq, throw_out_of_memory_exception); |
| 4842 | |
| 4843 | // Handle normal exception. |
| 4844 | __ jmp(throw_normal_exception); |
| 4845 | |
| 4846 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 4847 | } |
| 4848 | |
| 4849 | |
| 4850 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 4851 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 4852 | // r0: number of arguments including receiver |
| 4853 | // r1: pointer to builtin function |
| 4854 | // fp: frame pointer (restored after C call) |
| 4855 | // sp: stack pointer (restored as callee's pp after C call) |
| 4856 | // cp: current context (C callee-saved) |
| 4857 | // pp: caller's parameter pointer pp (C callee-saved) |
| 4858 | |
| 4859 | // NOTE: Invocations of builtins may return failure objects |
| 4860 | // instead of a proper result. The builtin entry handles |
| 4861 | // this by performing a garbage collection and retrying the |
| 4862 | // builtin once. |
| 4863 | |
| 4864 | StackFrame::Type frame_type = is_debug_break |
| 4865 | ? StackFrame::EXIT_DEBUG |
| 4866 | : StackFrame::EXIT; |
| 4867 | |
| 4868 | // Enter the exit frame that transitions from JavaScript to C++. |
| 4869 | __ EnterExitFrame(frame_type); |
| 4870 | |
| 4871 | // r4: number of arguments (C callee-saved) |
| 4872 | // r5: pointer to builtin function (C callee-saved) |
| 4873 | // r6: pointer to first argument (C callee-saved) |
| 4874 | |
| 4875 | Label throw_out_of_memory_exception; |
| 4876 | Label throw_normal_exception; |
| 4877 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4878 | // Call into the runtime system. Collect garbage before the call if |
| 4879 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4880 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4881 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4882 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 4883 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4884 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4885 | &throw_out_of_memory_exception, |
| 4886 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4887 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4888 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4889 | |
| 4890 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4891 | GenerateCore(masm, |
| 4892 | &throw_normal_exception, |
| 4893 | &throw_out_of_memory_exception, |
| 4894 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4895 | true, |
| 4896 | false); |
| 4897 | |
| 4898 | // Do full GC and retry runtime call one final time. |
| 4899 | Failure* failure = Failure::InternalError(); |
| 4900 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 4901 | GenerateCore(masm, |
| 4902 | &throw_normal_exception, |
| 4903 | &throw_out_of_memory_exception, |
| 4904 | frame_type, |
| 4905 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4906 | true); |
| 4907 | |
| 4908 | __ bind(&throw_out_of_memory_exception); |
| 4909 | GenerateThrowOutOfMemory(masm); |
| 4910 | // control flow for generated will not return. |
| 4911 | |
| 4912 | __ bind(&throw_normal_exception); |
| 4913 | GenerateThrowTOS(masm); |
| 4914 | } |
| 4915 | |
| 4916 | |
| 4917 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 4918 | // r0: code entry |
| 4919 | // r1: function |
| 4920 | // r2: receiver |
| 4921 | // r3: argc |
| 4922 | // [sp+0]: argv |
| 4923 | |
| 4924 | Label invoke, exit; |
| 4925 | |
| 4926 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 4927 | // No need to save register-passed args |
| 4928 | // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr |
| 4929 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 4930 | |
| 4931 | // Get address of argv, see stm above. |
| 4932 | // r0: code entry |
| 4933 | // r1: function |
| 4934 | // r2: receiver |
| 4935 | // r3: argc |
| 4936 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 4937 | __ ldr(r4, MemOperand(r4)); // argv |
| 4938 | |
| 4939 | // Push a frame with special values setup to mark it as an entry frame. |
| 4940 | // r0: code entry |
| 4941 | // r1: function |
| 4942 | // r2: receiver |
| 4943 | // r3: argc |
| 4944 | // r4: argv |
| 4945 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 4946 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 4947 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 4948 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 4949 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 4950 | __ ldr(r5, MemOperand(r5)); |
| 4951 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 4952 | |
| 4953 | // Setup frame pointer for the frame to be pushed. |
| 4954 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 4955 | |
| 4956 | // Call a faked try-block that does the invoke. |
| 4957 | __ bl(&invoke); |
| 4958 | |
| 4959 | // Caught exception: Store result (exception) in the pending |
| 4960 | // exception field in the JSEnv and return a failure sentinel. |
| 4961 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 4962 | // 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] | 4963 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4964 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4965 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4966 | __ b(&exit); |
| 4967 | |
| 4968 | // Invoke: Link this frame into the handler chain. |
| 4969 | __ bind(&invoke); |
| 4970 | // Must preserve r0-r4, r5-r7 are available. |
| 4971 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 4972 | // If an exception not caught by another handler occurs, this handler returns |
| 4973 | // control to the code after the bl(&invoke) above, which restores all |
| 4974 | // kCalleeSaved registers (including cp, pp and fp) to their saved values |
| 4975 | // before returning a failure to C. |
| 4976 | |
| 4977 | // Clear any pending exceptions. |
| 4978 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 4979 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4980 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4981 | __ str(r5, MemOperand(ip)); |
| 4982 | |
| 4983 | // Invoke the function by calling through JS entry trampoline builtin. |
| 4984 | // Notice that we cannot store a reference to the trampoline code directly in |
| 4985 | // this stub, because runtime stubs are not traversed when doing GC. |
| 4986 | |
| 4987 | // Expected registers by Builtins::JSEntryTrampoline |
| 4988 | // r0: code entry |
| 4989 | // r1: function |
| 4990 | // r2: receiver |
| 4991 | // r3: argc |
| 4992 | // r4: argv |
| 4993 | if (is_construct) { |
| 4994 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 4995 | __ mov(ip, Operand(construct_entry)); |
| 4996 | } else { |
| 4997 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 4998 | __ mov(ip, Operand(entry)); |
| 4999 | } |
| 5000 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5001 | |
| 5002 | // Branch and link to JSEntryTrampoline |
| 5003 | __ mov(lr, Operand(pc)); |
| 5004 | __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 5005 | |
| 5006 | // Unlink this frame from the handler chain. When reading the |
| 5007 | // address of the next handler, there is no need to use the address |
| 5008 | // displacement since the current stack pointer (sp) points directly |
| 5009 | // to the stack handler. |
| 5010 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5011 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5012 | __ str(r3, MemOperand(ip)); |
| 5013 | // No need to restore registers |
| 5014 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5015 | |
| 5016 | __ bind(&exit); // r0 holds result |
| 5017 | // Restore the top frame descriptors from the stack. |
| 5018 | __ pop(r3); |
| 5019 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5020 | __ str(r3, MemOperand(ip)); |
| 5021 | |
| 5022 | // Reset the stack to the callee saved registers. |
| 5023 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5024 | |
| 5025 | // Restore callee-saved registers and return. |
| 5026 | #ifdef DEBUG |
| 5027 | if (FLAG_debug_code) __ mov(lr, Operand(pc)); |
| 5028 | #endif |
| 5029 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5030 | } |
| 5031 | |
| 5032 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5033 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5034 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5035 | Label adaptor; |
| 5036 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5037 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5038 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5039 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5040 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5041 | // Nothing to do: The formal number of parameters has already been |
| 5042 | // passed in register r0 by calling function. Just return it. |
| 5043 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5044 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5045 | // Arguments adaptor case: Read the arguments length from the |
| 5046 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5047 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5048 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5049 | __ mov(pc, lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5053 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5054 | // The displacement is the offset of the last parameter (if any) |
| 5055 | // relative to the frame pointer. |
| 5056 | static const int kDisplacement = |
| 5057 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5058 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5059 | // Check that the key is a smi. |
| 5060 | Label slow; |
| 5061 | __ tst(r1, Operand(kSmiTagMask)); |
| 5062 | __ b(ne, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5063 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5064 | // Check if the calling frame is an arguments adaptor frame. |
| 5065 | Label adaptor; |
| 5066 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5067 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5068 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5069 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5070 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5071 | // Check index against formal parameters count limit passed in |
| 5072 | // through register eax. Use unsigned comparison to get negative |
| 5073 | // check for free. |
| 5074 | __ cmp(r1, r0); |
| 5075 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5076 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5077 | // Read the argument from the stack and return it. |
| 5078 | __ sub(r3, r0, r1); |
| 5079 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5080 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 5081 | __ mov(pc, lr); |
| 5082 | |
| 5083 | // Arguments adaptor case: Check index against actual arguments |
| 5084 | // limit found in the arguments adaptor frame. Use unsigned |
| 5085 | // comparison to get negative check for free. |
| 5086 | __ bind(&adaptor); |
| 5087 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5088 | __ cmp(r1, r0); |
| 5089 | __ b(cs, &slow); |
| 5090 | |
| 5091 | // Read the argument from the adaptor frame and return it. |
| 5092 | __ sub(r3, r0, r1); |
| 5093 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5094 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
| 5095 | __ mov(pc, lr); |
| 5096 | |
| 5097 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5098 | // by calling the runtime system. |
| 5099 | __ bind(&slow); |
| 5100 | __ push(r1); |
| 5101 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5102 | } |
| 5103 | |
| 5104 | |
| 5105 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 5106 | // Check if the calling frame is an arguments adaptor frame. |
| 5107 | Label runtime; |
| 5108 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5109 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5110 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5111 | __ b(ne, &runtime); |
| 5112 | |
| 5113 | // Patch the arguments.length and the parameters pointer. |
| 5114 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5115 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 5116 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5117 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 5118 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 5119 | |
| 5120 | // Do the runtime call to allocate the arguments object. |
| 5121 | __ bind(&runtime); |
| 5122 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | |
| 5126 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 5127 | Label slow; |
| 5128 | // Get the function to call from the stack. |
| 5129 | // function, receiver [, arguments] |
| 5130 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 5131 | |
| 5132 | // Check that the function is really a JavaScript function. |
| 5133 | // r1: pushed function (to be verified) |
| 5134 | __ tst(r1, Operand(kSmiTagMask)); |
| 5135 | __ b(eq, &slow); |
| 5136 | // Get the map of the function object. |
| 5137 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5138 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 5139 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 5140 | __ b(ne, &slow); |
| 5141 | |
| 5142 | // Fast-case: Invoke the function now. |
| 5143 | // r1: pushed function |
| 5144 | ParameterCount actual(argc_); |
| 5145 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 5146 | |
| 5147 | // Slow-case: Non-function called. |
| 5148 | __ bind(&slow); |
| 5149 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5150 | __ mov(r2, Operand(0)); |
| 5151 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 5152 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 5153 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | |
| 5157 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5158 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5159 | } } // namespace v8::internal |