ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "codegen-inl.h" |
| 32 | #include "debug.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 33 | #include "parser.h" |
| 34 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 36 | #include "scopes.h" |
| 37 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 42 | #define __ ACCESS_MASM(masm_) |
| 43 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 44 | |
| 45 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 46 | // CodeGenState implementation. |
| 47 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 48 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 49 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 50 | typeof_state_(NOT_INSIDE_TYPEOF), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 51 | true_target_(NULL), |
| 52 | false_target_(NULL), |
| 53 | previous_(NULL) { |
| 54 | owner_->set_state(this); |
| 55 | } |
| 56 | |
| 57 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 58 | CodeGenState::CodeGenState(CodeGenerator* owner, |
| 59 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 60 | JumpTarget* true_target, |
| 61 | JumpTarget* false_target) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 62 | : owner_(owner), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 63 | typeof_state_(typeof_state), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 64 | true_target_(true_target), |
| 65 | false_target_(false_target), |
| 66 | previous_(owner->state()) { |
| 67 | owner_->set_state(this); |
| 68 | } |
| 69 | |
| 70 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 71 | CodeGenState::~CodeGenState() { |
| 72 | ASSERT(owner_->state() == this); |
| 73 | owner_->set_state(previous_); |
| 74 | } |
| 75 | |
| 76 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 77 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 78 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 80 | CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
| 81 | bool is_eval) |
| 82 | : is_eval_(is_eval), |
| 83 | script_(script), |
| 84 | deferred_(8), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | masm_(new MacroAssembler(NULL, buffer_size)), |
| 86 | scope_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 87 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 88 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | cc_reg_(al), |
| 90 | state_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 91 | function_return_is_shadowed_(false), |
| 92 | in_spilled_code_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 97 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 99 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | // cp: callee's context |
| 101 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 102 | void CodeGenerator::GenCode(FunctionLiteral* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | ZoneList<Statement*>* body = fun->body(); |
| 104 | |
| 105 | // Initialize state. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 106 | ASSERT(scope_ == NULL); |
| 107 | scope_ = fun->scope(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 108 | ASSERT(allocator_ == NULL); |
| 109 | RegisterAllocator register_allocator(this); |
| 110 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 111 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 112 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 113 | cc_reg_ = al; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 114 | set_in_spilled_code(false); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 115 | { |
| 116 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 118 | // Entry: |
| 119 | // Stack: receiver, arguments |
| 120 | // lr: return address |
| 121 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 123 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 125 | allocator_->Initialize(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 126 | frame_->Enter(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | // tos: code slot |
| 128 | #ifdef DEBUG |
| 129 | if (strlen(FLAG_stop_at) > 0 && |
| 130 | fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 131 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 132 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | #endif |
| 135 | |
| 136 | // Allocate space for locals and initialize them. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 137 | frame_->AllocateStackSlots(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 138 | // Initialize the function return target after the locals are set |
| 139 | // up, because it needs the expected frame height from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 140 | function_return_.set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 141 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 143 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 144 | if (scope_->num_heap_slots() > 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | // Allocate local context. |
| 146 | // Get outer context and create a new context based on it. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 147 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 148 | frame_->EmitPush(r0); |
| 149 | frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 150 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 151 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 152 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 153 | __ cmp(r0, Operand(cp)); |
| 154 | verified_true.Branch(eq); |
| 155 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 156 | verified_true.Bind(); |
| 157 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 159 | __ str(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 162 | // TODO(1241774): Improve this code: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 163 | // 1) only needed if we have a context |
| 164 | // 2) no need to recompute context ptr every single time |
| 165 | // 3) don't copy parameter operand code from SlotOperand! |
| 166 | { |
| 167 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 168 | |
| 169 | // Note that iteration order is relevant here! If we have the same |
| 170 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 171 | // needs to be copied into the context, it must be the last argument |
| 172 | // passed to the parameter that needs to be copied. This is a rare |
| 173 | // case so we don't check for it, instead we rely on the copying |
| 174 | // order: such a parameter is copied repeatedly into the same |
| 175 | // context location and thus the last value is what is seen inside |
| 176 | // the function. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 177 | for (int i = 0; i < scope_->num_parameters(); i++) { |
| 178 | Variable* par = scope_->parameter(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | Slot* slot = par->slot(); |
| 180 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 181 | ASSERT(!scope_->is_global_scope()); // no parameters in global scope |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 182 | __ ldr(r1, frame_->ParameterAt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | // Loads r2 with context; used below in RecordWrite. |
| 184 | __ str(r1, SlotOperand(slot, r2)); |
| 185 | // Load the offset into r3. |
| 186 | int slot_offset = |
| 187 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 188 | __ mov(r3, Operand(slot_offset)); |
| 189 | __ RecordWrite(r2, r3, r1); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 194 | // Store the arguments object. This must happen after context |
| 195 | // initialization because the arguments object may be stored in the |
| 196 | // context. |
| 197 | if (scope_->arguments() != NULL) { |
| 198 | ASSERT(scope_->arguments_shadow() != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | Comment cmnt(masm_, "[ allocate arguments object"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 200 | { Reference shadow_ref(this, scope_->arguments_shadow()); |
| 201 | { Reference arguments_ref(this, scope_->arguments()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 203 | __ ldr(r2, frame_->Function()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 204 | // The receiver is below the arguments, the return address, |
| 205 | // and the frame pointer on the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 206 | const int kReceiverDisplacement = 2 + scope_->num_parameters(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 207 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 208 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 209 | frame_->Adjust(3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 210 | __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 211 | frame_->CallStub(&stub, 3); |
| 212 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 213 | arguments_ref.SetValue(NOT_CONST_INIT); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 214 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 215 | shadow_ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 217 | frame_->Drop(); // Value is no longer needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | } |
| 219 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 220 | // Generate code to 'execute' declarations and initialize functions |
| 221 | // (source elements). In case of an illegal redeclaration we need to |
| 222 | // handle that instead of processing the declarations. |
| 223 | if (scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 225 | scope_->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | } else { |
| 227 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 228 | ProcessDeclarations(scope_->declarations()); |
| 229 | // Bail out if a stack-overflow exception occurred when processing |
| 230 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 231 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | } |
| 233 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 234 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 235 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 236 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 237 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 238 | CheckStack(); |
| 239 | |
| 240 | // Compile the body of the function in a vanilla state. Don't |
| 241 | // bother compiling all the code if the scope has an illegal |
| 242 | // redeclaration. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 243 | if (!scope_->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | Comment cmnt(masm_, "[ function body"); |
| 245 | #ifdef DEBUG |
| 246 | bool is_builtin = Bootstrapper::IsActive(); |
| 247 | bool should_trace = |
| 248 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 249 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 250 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 251 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 252 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | VisitStatementsAndSpill(body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | } |
| 257 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 258 | // Generate the return sequence if necessary. |
| 259 | if (frame_ != NULL || function_return_.is_linked()) { |
| 260 | // exit |
| 261 | // r0: result |
| 262 | // sp: stack pointer |
| 263 | // fp: frame pointer |
| 264 | // pp: parameter pointer |
| 265 | // cp: callee's context |
| 266 | __ mov(r0, Operand(Factory::undefined_value())); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 267 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 268 | function_return_.Bind(); |
| 269 | if (FLAG_trace) { |
| 270 | // Push the return value on the stack as the parameter. |
| 271 | // Runtime::TraceExit returns the parameter as it is. |
| 272 | frame_->EmitPush(r0); |
| 273 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 274 | } |
| 275 | |
| 276 | // Tear down the frame which will restore the caller's frame pointer and |
| 277 | // the link register. |
| 278 | frame_->Exit(); |
| 279 | |
| 280 | __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 281 | __ Jump(lr); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 282 | } |
| 283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 285 | ASSERT(!has_cc()); |
| 286 | ASSERT(state_ == NULL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 287 | ASSERT(!function_return_is_shadowed_); |
| 288 | function_return_.Unuse(); |
| 289 | DeleteFrame(); |
| 290 | |
| 291 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 292 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 293 | ProcessDeferred(); |
| 294 | } |
| 295 | |
| 296 | allocator_ = NULL; |
| 297 | scope_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 301 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 302 | // Currently, this assertion will fail if we try to assign to |
| 303 | // a constant variable that is constant because it is read-only |
| 304 | // (such as the variable referring to a named function expression). |
| 305 | // We need to implement assignments to read-only variables. |
| 306 | // Ideally, we should do this during AST generation (by converting |
| 307 | // such assignments into expression statements); however, in general |
| 308 | // we may not be able to make the decision until past AST generation, |
| 309 | // that is when the entire program is known. |
| 310 | ASSERT(slot != NULL); |
| 311 | int index = slot->index(); |
| 312 | switch (slot->type()) { |
| 313 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 315 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 316 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 317 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 318 | |
| 319 | case Slot::CONTEXT: { |
| 320 | // Follow the context chain if necessary. |
| 321 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 322 | Register context = cp; |
| 323 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 324 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 325 | // Load the closure. |
| 326 | // (All contexts, even 'with' contexts, have a closure, |
| 327 | // and it is the same for all contexts inside a function. |
| 328 | // There is no need to go to the function context first.) |
| 329 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 330 | // Load the function context (which is the incoming, outer context). |
| 331 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 332 | context = tmp; |
| 333 | } |
| 334 | // We may have a 'with' context now. Get the function context. |
| 335 | // (In fact this mov may never be the needed, since the scope analysis |
| 336 | // may not permit a direct context access in this case and thus we are |
| 337 | // always at a function context. However it is safe to dereference be- |
| 338 | // cause the function context of a function context is itself. Before |
| 339 | // deleting this mov we should try to create a counter-example first, |
| 340 | // though...) |
| 341 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 342 | return ContextOperand(tmp, index); |
| 343 | } |
| 344 | |
| 345 | default: |
| 346 | UNREACHABLE(); |
| 347 | return MemOperand(r0, 0); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 352 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 353 | Slot* slot, |
| 354 | Register tmp, |
| 355 | Register tmp2, |
| 356 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 357 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 358 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 359 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 360 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 361 | if (s->num_heap_slots() > 0) { |
| 362 | if (s->calls_eval()) { |
| 363 | // Check that extension is NULL. |
| 364 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 365 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 366 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 367 | } |
| 368 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 369 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 370 | context = tmp; |
| 371 | } |
| 372 | } |
| 373 | // Check that last extension is NULL. |
| 374 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 375 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 376 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 377 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 378 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 382 | void CodeGenerator::LoadConditionAndSpill(Expression* expression, |
| 383 | TypeofState typeof_state, |
| 384 | JumpTarget* true_target, |
| 385 | JumpTarget* false_target, |
| 386 | bool force_control) { |
| 387 | ASSERT(in_spilled_code()); |
| 388 | set_in_spilled_code(false); |
| 389 | LoadCondition(expression, typeof_state, true_target, false_target, |
| 390 | force_control); |
| 391 | if (frame_ != NULL) { |
| 392 | frame_->SpillAll(); |
| 393 | } |
| 394 | set_in_spilled_code(true); |
| 395 | } |
| 396 | |
| 397 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 398 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 399 | // (partially) translated into branches, or it may have set the condition |
| 400 | // code register. If force_cc is set, the value is forced to set the |
| 401 | // condition code register and no value is pushed. If the condition code |
| 402 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 403 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 404 | void CodeGenerator::LoadCondition(Expression* x, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 405 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 406 | JumpTarget* true_target, |
| 407 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 408 | bool force_cc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | ASSERT(!in_spilled_code()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 410 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 411 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 413 | { CodeGenState new_state(this, typeof_state, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 414 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | |
| 416 | // If we hit a stack overflow, we may not have actually visited |
| 417 | // the expression. In that case, we ensure that we have a |
| 418 | // valid-looking frame state because we will continue to generate |
| 419 | // code as we unwind the C++ stack. |
| 420 | // |
| 421 | // It's possible to have both a stack overflow and a valid frame |
| 422 | // state (eg, a subexpression overflowed, visiting it returned |
| 423 | // with a dummied frame state, and visiting this expression |
| 424 | // returned with a normal-looking state). |
| 425 | if (HasStackOverflow() && |
| 426 | has_valid_frame() && |
| 427 | !has_cc() && |
| 428 | frame_->height() == original_height) { |
| 429 | true_target->Jump(); |
| 430 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 431 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 433 | // Convert the TOS value to a boolean in the condition code register. |
| 434 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 436 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 437 | ASSERT(!has_valid_frame() || |
| 438 | (has_cc() && frame_->height() == original_height) || |
| 439 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 443 | void CodeGenerator::LoadAndSpill(Expression* expression, |
| 444 | TypeofState typeof_state) { |
| 445 | ASSERT(in_spilled_code()); |
| 446 | set_in_spilled_code(false); |
| 447 | Load(expression, typeof_state); |
| 448 | frame_->SpillAll(); |
| 449 | set_in_spilled_code(true); |
| 450 | } |
| 451 | |
| 452 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 453 | void CodeGenerator::Load(Expression* x, TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | #ifdef DEBUG |
| 455 | int original_height = frame_->height(); |
| 456 | #endif |
| 457 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 458 | JumpTarget true_target; |
| 459 | JumpTarget false_target; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 460 | LoadCondition(x, typeof_state, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | |
| 462 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 464 | JumpTarget loaded; |
| 465 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 466 | materialize_true.Branch(cc_reg_); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 467 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 468 | frame_->EmitPush(r0); |
| 469 | loaded.Jump(); |
| 470 | materialize_true.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 471 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 472 | frame_->EmitPush(r0); |
| 473 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | cc_reg_ = al; |
| 475 | } |
| 476 | |
| 477 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 478 | // We have at least one condition value that has been "translated" |
| 479 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 480 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 481 | if (frame_ != NULL) { |
| 482 | loaded.Jump(); // Don't lose the current TOS. |
| 483 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 484 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 485 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 487 | true_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 488 | __ mov(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 489 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | // If both "true" and "false" need to be loaded jump across the code for |
| 492 | // "false". |
| 493 | if (both) { |
| 494 | loaded.Jump(); |
| 495 | } |
| 496 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 498 | false_target.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 499 | __ mov(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 500 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 502 | // A value is loaded on all paths reaching this point. |
| 503 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 505 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 507 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 511 | void CodeGenerator::LoadGlobal() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 512 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 513 | __ ldr(r0, GlobalObject()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 518 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 519 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 520 | __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 521 | __ ldr(scratch, |
| 522 | FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | frame_->EmitPush(scratch); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | // TODO(1241834): Get rid of this function in favor of just using Load, now |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 528 | // that we have the INSIDE_TYPEOF typeof state. => Need to handle global |
| 529 | // variables w/o reference errors elsewhere. |
| 530 | void CodeGenerator::LoadTypeofExpression(Expression* x) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 531 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | Variable* variable = x->AsVariableProxy()->AsVariable(); |
| 533 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
| 534 | // NOTE: This is somewhat nasty. We force the compiler to load |
| 535 | // the variable as if through '<global>.<variable>' to make sure we |
| 536 | // do not get reference errors. |
| 537 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 538 | Literal key(variable->name()); |
| 539 | // TODO(1241834): Fetch the position from the variable instead of using |
| 540 | // no position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 541 | Property property(&global, &key, RelocInfo::kNoPosition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | LoadAndSpill(&property); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 544 | LoadAndSpill(x, INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 549 | Reference::Reference(CodeGenerator* cgen, Expression* expression) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | : cgen_(cgen), expression_(expression), type_(ILLEGAL) { |
| 551 | cgen->LoadReference(this); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | Reference::~Reference() { |
| 556 | cgen_->UnloadReference(this); |
| 557 | } |
| 558 | |
| 559 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 560 | void CodeGenerator::LoadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 561 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 562 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | Expression* e = ref->expression(); |
| 564 | Property* property = e->AsProperty(); |
| 565 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 566 | |
| 567 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 568 | // The expression is either a property or a variable proxy that rewrites |
| 569 | // to a property. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 570 | LoadAndSpill(property->obj()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 571 | // We use a named reference if the key is a literal symbol, unless it is |
| 572 | // a string that can be legally parsed as an integer. This is because |
| 573 | // otherwise we will not get into the slow case code that handles [] on |
| 574 | // String objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | Literal* literal = property->key()->AsLiteral(); |
| 576 | uint32_t dummy; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 577 | if (literal != NULL && |
| 578 | literal->handle()->IsSymbol() && |
| 579 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | ref->set_type(Reference::NAMED); |
| 581 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 582 | LoadAndSpill(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | ref->set_type(Reference::KEYED); |
| 584 | } |
| 585 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 586 | // The expression is a variable proxy that does not rewrite to a |
| 587 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | LoadGlobal(); |
| 590 | ref->set_type(Reference::NAMED); |
| 591 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 592 | ASSERT(var->slot() != NULL); |
| 593 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 594 | } |
| 595 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 596 | // Anything else is a runtime error. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 597 | LoadAndSpill(e); |
| 598 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 603 | void CodeGenerator::UnloadReference(Reference* ref) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 604 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 605 | // Pop a reference from the stack while preserving TOS. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 606 | Comment cmnt(masm_, "[ UnloadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | int size = ref->size(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 608 | if (size > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 609 | frame_->EmitPop(r0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 610 | frame_->Drop(size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 611 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 617 | // register to a boolean in the condition code register. The code |
| 618 | // may jump to 'false_target' in case the register converts to 'false'. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 619 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 620 | JumpTarget* false_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 621 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 622 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | // Only the condition code should be set. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 624 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | |
| 626 | // Fast case checks |
| 627 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 628 | // Check if the value is 'false'. |
| 629 | __ cmp(r0, Operand(Factory::false_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 630 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 632 | // Check if the value is 'true'. |
| 633 | __ cmp(r0, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 634 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 636 | // Check if the value is 'undefined'. |
| 637 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 638 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 639 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 640 | // Check if the value is a smi. |
| 641 | __ cmp(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 642 | false_target->Branch(eq); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 643 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 644 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | |
| 646 | // Slow case: call the runtime. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 647 | frame_->EmitPush(r0); |
| 648 | frame_->CallRuntime(Runtime::kToBool, 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 649 | // Convert the result (r0) to a condition code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | __ cmp(r0, Operand(Factory::false_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 651 | |
| 652 | cc_reg_ = ne; |
| 653 | } |
| 654 | |
| 655 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 656 | class GenericBinaryOpStub : public CodeStub { |
| 657 | public: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 658 | GenericBinaryOpStub(Token::Value op, |
| 659 | OverwriteMode mode) |
| 660 | : op_(op), mode_(mode) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 661 | |
| 662 | private: |
| 663 | Token::Value op_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 664 | OverwriteMode mode_; |
| 665 | |
| 666 | // Minor key encoding in 16 bits. |
| 667 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 668 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 669 | |
| 670 | Major MajorKey() { return GenericBinaryOp; } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 671 | int MinorKey() { |
| 672 | // Encode the parameters in a unique 16 bit value. |
| 673 | return OpBits::encode(op_) |
| 674 | | ModeBits::encode(mode_); |
| 675 | } |
| 676 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 677 | void Generate(MacroAssembler* masm); |
| 678 | |
| 679 | const char* GetName() { |
| 680 | switch (op_) { |
| 681 | case Token::ADD: return "GenericBinaryOpStub_ADD"; |
| 682 | case Token::SUB: return "GenericBinaryOpStub_SUB"; |
| 683 | case Token::MUL: return "GenericBinaryOpStub_MUL"; |
| 684 | case Token::DIV: return "GenericBinaryOpStub_DIV"; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 685 | case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR"; |
| 686 | case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND"; |
| 687 | case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR"; |
| 688 | case Token::SAR: return "GenericBinaryOpStub_SAR"; |
| 689 | case Token::SHL: return "GenericBinaryOpStub_SHL"; |
| 690 | case Token::SHR: return "GenericBinaryOpStub_SHR"; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 691 | default: return "GenericBinaryOpStub"; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | #ifdef DEBUG |
| 696 | void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); } |
| 697 | #endif |
| 698 | }; |
| 699 | |
| 700 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 701 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
| 702 | OverwriteMode overwrite_mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 703 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 704 | // sp[0] : y |
| 705 | // sp[1] : x |
| 706 | // result : r0 |
| 707 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 708 | // Stub is entered with a call: 'return address' is in lr. |
| 709 | switch (op) { |
| 710 | case Token::ADD: // fall through. |
| 711 | case Token::SUB: // fall through. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 712 | case Token::MUL: |
| 713 | case Token::BIT_OR: |
| 714 | case Token::BIT_AND: |
| 715 | case Token::BIT_XOR: |
| 716 | case Token::SHL: |
| 717 | case Token::SHR: |
| 718 | case Token::SAR: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 719 | frame_->EmitPop(r0); // r0 : y |
| 720 | frame_->EmitPop(r1); // r1 : x |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 721 | GenericBinaryOpStub stub(op, overwrite_mode); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 722 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | |
| 726 | case Token::DIV: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 727 | Result arg_count = allocator_->Allocate(r0); |
| 728 | ASSERT(arg_count.is_valid()); |
| 729 | __ mov(arg_count.reg(), Operand(1)); |
| 730 | frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | break; |
| 732 | } |
| 733 | |
| 734 | case Token::MOD: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 735 | Result arg_count = allocator_->Allocate(r0); |
| 736 | ASSERT(arg_count.is_valid()); |
| 737 | __ mov(arg_count.reg(), Operand(1)); |
| 738 | frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | break; |
| 740 | } |
| 741 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 742 | case Token::COMMA: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 743 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 744 | // simply discard left value |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 745 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 746 | break; |
| 747 | |
| 748 | default: |
| 749 | // Other cases should have been handled before this point. |
| 750 | UNREACHABLE(); |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 756 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 757 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 758 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 759 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 760 | bool reversed, |
| 761 | OverwriteMode overwrite_mode) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 762 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 763 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 764 | reversed_(reversed), |
| 765 | overwrite_mode_(overwrite_mode) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 766 | set_comment("[ DeferredInlinedSmiOperation"); |
| 767 | } |
| 768 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 769 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 770 | |
| 771 | private: |
| 772 | Token::Value op_; |
| 773 | int value_; |
| 774 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 775 | OverwriteMode overwrite_mode_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
| 778 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 779 | #undef __ |
| 780 | #define __ ACCESS_MASM(masm) |
| 781 | |
| 782 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 783 | void DeferredInlineSmiOperation::Generate() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 784 | MacroAssembler* masm = cgen()->masm(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 785 | enter()->Bind(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 786 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 787 | |
| 788 | switch (op_) { |
| 789 | case Token::ADD: { |
| 790 | if (reversed_) { |
| 791 | // revert optimistic add |
| 792 | __ sub(r0, r0, Operand(Smi::FromInt(value_))); |
| 793 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 794 | } else { |
| 795 | // revert optimistic add |
| 796 | __ sub(r1, r0, Operand(Smi::FromInt(value_))); |
| 797 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 798 | } |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | case Token::SUB: { |
| 803 | if (reversed_) { |
| 804 | // revert optimistic sub |
| 805 | __ rsb(r0, r0, Operand(Smi::FromInt(value_))); |
| 806 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 807 | } else { |
| 808 | __ add(r1, r0, Operand(Smi::FromInt(value_))); |
| 809 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 810 | } |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | case Token::BIT_OR: |
| 815 | case Token::BIT_XOR: |
| 816 | case Token::BIT_AND: { |
| 817 | if (reversed_) { |
| 818 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 819 | } else { |
| 820 | __ mov(r1, Operand(r0)); |
| 821 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 822 | } |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | case Token::SHL: |
| 827 | case Token::SHR: |
| 828 | case Token::SAR: { |
| 829 | if (!reversed_) { |
| 830 | __ mov(r1, Operand(r0)); |
| 831 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 832 | } else { |
| 833 | UNREACHABLE(); // should have been handled in SmiOperation |
| 834 | } |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | default: |
| 839 | // other cases should have been handled before this point. |
| 840 | UNREACHABLE(); |
| 841 | break; |
| 842 | } |
| 843 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 844 | GenericBinaryOpStub igostub(op_, overwrite_mode_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 845 | Result arg0 = cgen()->allocator()->Allocate(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 846 | ASSERT(arg0.is_valid()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 847 | Result arg1 = cgen()->allocator()->Allocate(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 848 | ASSERT(arg1.is_valid()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 849 | cgen()->frame()->CallStub(&igostub, &arg0, &arg1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 850 | exit_.Jump(); |
| 851 | } |
| 852 | |
| 853 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 854 | #undef __ |
| 855 | #define __ ACCESS_MASM(masm_) |
| 856 | |
| 857 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 858 | void CodeGenerator::SmiOperation(Token::Value op, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 859 | Handle<Object> value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 860 | bool reversed, |
| 861 | OverwriteMode mode) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 862 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | // NOTE: This is an attempt to inline (a bit) more of the code for |
| 864 | // some possible smi operations (like + and -) when (at least) one |
| 865 | // of the operands is a literal smi. With this optimization, the |
| 866 | // performance of the system is increased by ~15%, and the generated |
| 867 | // code size is increased by ~1% (measured on a combination of |
| 868 | // different benchmarks). |
| 869 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 870 | // sp[0] : operand |
| 871 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 872 | int int_value = Smi::cast(*value)->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 873 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 874 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 875 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | |
| 877 | switch (op) { |
| 878 | case Token::ADD: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 879 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 880 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 881 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 882 | __ add(r0, r0, Operand(value), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 883 | deferred->enter()->Branch(vs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 885 | deferred->enter()->Branch(ne); |
| 886 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | |
| 890 | case Token::SUB: { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 891 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 892 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 893 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 894 | if (!reversed) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 895 | __ sub(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 896 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 897 | __ rsb(r0, r0, Operand(value), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 899 | deferred->enter()->Branch(vs); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 900 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 901 | deferred->enter()->Branch(ne); |
| 902 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 903 | break; |
| 904 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | case Token::BIT_OR: |
| 907 | case Token::BIT_XOR: |
| 908 | case Token::BIT_AND: { |
| 909 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 910 | new DeferredInlineSmiOperation(op, int_value, reversed, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 911 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 912 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 913 | switch (op) { |
| 914 | case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break; |
| 915 | case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break; |
| 916 | case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break; |
| 917 | default: UNREACHABLE(); |
| 918 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 919 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 920 | break; |
| 921 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 922 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 923 | case Token::SHL: |
| 924 | case Token::SHR: |
| 925 | case Token::SAR: { |
| 926 | if (reversed) { |
| 927 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 928 | frame_->EmitPush(ip); |
| 929 | frame_->EmitPush(r0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 930 | GenericBinaryOperation(op, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 931 | |
| 932 | } else { |
| 933 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 934 | DeferredCode* deferred = |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 935 | new DeferredInlineSmiOperation(op, shift_value, false, mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 936 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 937 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 938 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags |
| 939 | switch (op) { |
| 940 | case Token::SHL: { |
| 941 | __ mov(r2, Operand(r2, LSL, shift_value)); |
| 942 | // check that the *unsigned* result fits in a smi |
| 943 | __ add(r3, r2, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 944 | deferred->enter()->Branch(mi); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 945 | break; |
| 946 | } |
| 947 | case Token::SHR: { |
| 948 | // LSR by immediate 0 means shifting 32 bits. |
| 949 | if (shift_value != 0) { |
| 950 | __ mov(r2, Operand(r2, LSR, shift_value)); |
| 951 | } |
| 952 | // check that the *unsigned* result fits in a smi |
| 953 | // neither of the two high-order bits can be set: |
| 954 | // - 0x80000000: high bit would be lost when smi tagging |
| 955 | // - 0x40000000: this number would convert to negative when |
| 956 | // smi tagging these two cases can only happen with shifts |
| 957 | // by 0 or 1 when handed a valid smi |
| 958 | __ and_(r3, r2, Operand(0xc0000000), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 959 | deferred->enter()->Branch(ne); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 960 | break; |
| 961 | } |
| 962 | case Token::SAR: { |
| 963 | if (shift_value != 0) { |
| 964 | // ASR by immediate 0 means shifting 32 bits. |
| 965 | __ mov(r2, Operand(r2, ASR, shift_value)); |
| 966 | } |
| 967 | break; |
| 968 | } |
| 969 | default: UNREACHABLE(); |
| 970 | } |
| 971 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 972 | deferred->BindExit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 973 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | |
| 977 | default: |
| 978 | if (!reversed) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 979 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 980 | __ mov(r0, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 981 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 982 | } else { |
| 983 | __ mov(ip, Operand(value)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 984 | frame_->EmitPush(ip); |
| 985 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 986 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 987 | GenericBinaryOperation(op, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 991 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 995 | void CodeGenerator::Comparison(Condition cc, bool strict) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 996 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 997 | // sp[0] : y |
| 998 | // sp[1] : x |
| 999 | // result : cc register |
| 1000 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | // Strict only makes sense for equality comparisons. |
| 1002 | ASSERT(!strict || cc == eq); |
| 1003 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1004 | JumpTarget exit; |
| 1005 | JumpTarget smi; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1006 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1007 | if (cc == gt || cc == le) { |
| 1008 | cc = ReverseCondition(cc); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1009 | frame_->EmitPop(r1); |
| 1010 | frame_->EmitPop(r0); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1011 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1012 | frame_->EmitPop(r0); |
| 1013 | frame_->EmitPop(r1); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1014 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1015 | __ orr(r2, r0, Operand(r1)); |
| 1016 | __ tst(r2, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1017 | smi.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Perform non-smi comparison by runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1020 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Figure out which native to call and setup the arguments. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1023 | Builtins::JavaScript native; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1024 | int arg_count = 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | if (cc == eq) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1026 | native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1027 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1028 | native = Builtins::COMPARE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1029 | int ncr; // NaN compare result |
| 1030 | if (cc == lt || cc == le) { |
| 1031 | ncr = GREATER; |
| 1032 | } else { |
| 1033 | ASSERT(cc == gt || cc == ge); // remaining cases |
| 1034 | ncr = LESS; |
| 1035 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1036 | frame_->EmitPush(r0); |
| 1037 | arg_count++; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1038 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 1042 | // tagged as a small integer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1043 | frame_->EmitPush(r0); |
| 1044 | Result arg_count_register = allocator_->Allocate(r0); |
| 1045 | ASSERT(arg_count_register.is_valid()); |
| 1046 | __ mov(arg_count_register.reg(), Operand(arg_count)); |
| 1047 | Result result = frame_->InvokeBuiltin(native, |
| 1048 | CALL_JS, |
| 1049 | &arg_count_register, |
| 1050 | arg_count + 1); |
| 1051 | __ cmp(result.reg(), Operand(0)); |
| 1052 | result.Unuse(); |
| 1053 | exit.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | |
| 1055 | // test smi equality by pointer comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1056 | smi.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1057 | __ cmp(r1, Operand(r0)); |
| 1058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1059 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1060 | cc_reg_ = cc; |
| 1061 | } |
| 1062 | |
| 1063 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1064 | class CallFunctionStub: public CodeStub { |
| 1065 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1066 | CallFunctionStub(int argc, InLoopFlag in_loop) |
| 1067 | : argc_(argc), in_loop_(in_loop) {} |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1068 | |
| 1069 | void Generate(MacroAssembler* masm); |
| 1070 | |
| 1071 | private: |
| 1072 | int argc_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1073 | InLoopFlag in_loop_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1074 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1075 | #if defined(DEBUG) |
| 1076 | void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); } |
| 1077 | #endif // defined(DEBUG) |
| 1078 | |
| 1079 | Major MajorKey() { return CallFunction; } |
| 1080 | int MinorKey() { return argc_; } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1081 | InLoopFlag InLoop() { return in_loop_; } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1082 | }; |
| 1083 | |
| 1084 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1085 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1086 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | int position) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1088 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1090 | int arg_count = args->length(); |
| 1091 | for (int i = 0; i < arg_count; i++) { |
| 1092 | LoadAndSpill(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1093 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1094 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1095 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1096 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1098 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1099 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 1100 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1101 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1102 | |
| 1103 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1104 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1105 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1109 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1110 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | ASSERT(has_cc()); |
| 1112 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1113 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1114 | cc_reg_ = al; |
| 1115 | } |
| 1116 | |
| 1117 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1118 | void CodeGenerator::CheckStack() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1119 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1120 | if (FLAG_check_stack) { |
| 1121 | Comment cmnt(masm_, "[ check stack"); |
| 1122 | StackCheckStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1123 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1128 | void CodeGenerator::VisitAndSpill(Statement* statement) { |
| 1129 | ASSERT(in_spilled_code()); |
| 1130 | set_in_spilled_code(false); |
| 1131 | Visit(statement); |
| 1132 | if (frame_ != NULL) { |
| 1133 | frame_->SpillAll(); |
| 1134 | } |
| 1135 | set_in_spilled_code(true); |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) { |
| 1140 | ASSERT(in_spilled_code()); |
| 1141 | set_in_spilled_code(false); |
| 1142 | VisitStatements(statements); |
| 1143 | if (frame_ != NULL) { |
| 1144 | frame_->SpillAll(); |
| 1145 | } |
| 1146 | set_in_spilled_code(true); |
| 1147 | } |
| 1148 | |
| 1149 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1150 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1151 | #ifdef DEBUG |
| 1152 | int original_height = frame_->height(); |
| 1153 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1154 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1155 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
| 1156 | VisitAndSpill(statements->at(i)); |
| 1157 | } |
| 1158 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1159 | } |
| 1160 | |
| 1161 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1162 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1163 | #ifdef DEBUG |
| 1164 | int original_height = frame_->height(); |
| 1165 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1166 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1167 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1168 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1169 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1170 | VisitStatementsAndSpill(node->statements()); |
| 1171 | if (node->break_target()->is_linked()) { |
| 1172 | node->break_target()->Bind(); |
| 1173 | } |
| 1174 | node->break_target()->Unuse(); |
| 1175 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1179 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1180 | VirtualFrame::SpilledScope spilled_scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1181 | __ mov(r0, Operand(pairs)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1182 | frame_->EmitPush(r0); |
| 1183 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1184 | __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1185 | frame_->EmitPush(r0); |
| 1186 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1187 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1191 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1192 | #ifdef DEBUG |
| 1193 | int original_height = frame_->height(); |
| 1194 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1195 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1196 | Comment cmnt(masm_, "[ Declaration"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1197 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | Variable* var = node->proxy()->var(); |
| 1199 | ASSERT(var != NULL); // must have been resolved |
| 1200 | Slot* slot = var->slot(); |
| 1201 | |
| 1202 | // If it was not possible to allocate the variable at compile time, |
| 1203 | // we need to "declare" it at runtime to make sure it actually |
| 1204 | // exists in the local context. |
| 1205 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1206 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1207 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1208 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1210 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1211 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1212 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | // Declaration nodes are always declared in only two modes. |
| 1214 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1215 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1216 | __ mov(r0, Operand(Smi::FromInt(attr))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1217 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1218 | // Push initial value, if any. |
| 1219 | // Note: For variables we must not push an initial value (such as |
| 1220 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1221 | // must not destroy the current value. |
| 1222 | if (node->mode() == Variable::CONST) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1223 | __ mov(r0, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1224 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | } else if (node->fun() != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1226 | LoadAndSpill(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1227 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1228 | __ mov(r0, Operand(0)); // no initial value! |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1229 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1231 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1232 | // Ignore the return value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1233 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | ASSERT(!var->is_global()); |
| 1238 | |
| 1239 | // If we have a function or a constant, we need to initialize the variable. |
| 1240 | Expression* val = NULL; |
| 1241 | if (node->mode() == Variable::CONST) { |
| 1242 | val = new Literal(Factory::the_hole_value()); |
| 1243 | } else { |
| 1244 | val = node->fun(); // NULL if we don't have a function |
| 1245 | } |
| 1246 | |
| 1247 | if (val != NULL) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1248 | { |
| 1249 | // Set initial value. |
| 1250 | Reference target(this, node->proxy()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1251 | LoadAndSpill(val); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1252 | target.SetValue(NOT_CONST_INIT); |
| 1253 | // The reference is removed from the stack (preserving TOS) when |
| 1254 | // it goes out of scope. |
| 1255 | } |
| 1256 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1257 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1258 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1259 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1263 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1264 | #ifdef DEBUG |
| 1265 | int original_height = frame_->height(); |
| 1266 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1267 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1269 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | Expression* expression = node->expression(); |
| 1271 | expression->MarkAsStatement(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1272 | LoadAndSpill(expression); |
| 1273 | frame_->Drop(); |
| 1274 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1278 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1279 | #ifdef DEBUG |
| 1280 | int original_height = frame_->height(); |
| 1281 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1282 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1284 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1286 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1290 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1291 | #ifdef DEBUG |
| 1292 | int original_height = frame_->height(); |
| 1293 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1294 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1296 | // Generate different code depending on which parts of the if statement |
| 1297 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1298 | bool has_then_stm = node->HasThenStatement(); |
| 1299 | bool has_else_stm = node->HasElseStatement(); |
| 1300 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1301 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1302 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1303 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1305 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1306 | JumpTarget then; |
| 1307 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1309 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1310 | &then, &else_, true); |
| 1311 | if (frame_ != NULL) { |
| 1312 | Branch(false, &else_); |
| 1313 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1315 | if (frame_ != NULL || then.is_linked()) { |
| 1316 | then.Bind(); |
| 1317 | VisitAndSpill(node->then_statement()); |
| 1318 | } |
| 1319 | if (frame_ != NULL) { |
| 1320 | exit.Jump(); |
| 1321 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1323 | if (else_.is_linked()) { |
| 1324 | else_.Bind(); |
| 1325 | VisitAndSpill(node->else_statement()); |
| 1326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1327 | |
| 1328 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1329 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1330 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1331 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1333 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1334 | &then, &exit, true); |
| 1335 | if (frame_ != NULL) { |
| 1336 | Branch(false, &exit); |
| 1337 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1339 | if (frame_ != NULL || then.is_linked()) { |
| 1340 | then.Bind(); |
| 1341 | VisitAndSpill(node->then_statement()); |
| 1342 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | |
| 1344 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1345 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1347 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1348 | // if (!cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1349 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1350 | &exit, &else_, true); |
| 1351 | if (frame_ != NULL) { |
| 1352 | Branch(true, &exit); |
| 1353 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1355 | if (frame_ != NULL || else_.is_linked()) { |
| 1356 | else_.Bind(); |
| 1357 | VisitAndSpill(node->else_statement()); |
| 1358 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1359 | |
| 1360 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1361 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1362 | ASSERT(!has_then_stm && !has_else_stm); |
| 1363 | // if (cond) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1364 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 1365 | &exit, &exit, false); |
| 1366 | if (frame_ != NULL) { |
| 1367 | if (has_cc()) { |
| 1368 | cc_reg_ = al; |
| 1369 | } else { |
| 1370 | frame_->Drop(); |
| 1371 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1376 | if (exit.is_linked()) { |
| 1377 | exit.Bind(); |
| 1378 | } |
| 1379 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1383 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1384 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1385 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1386 | CodeForStatementPosition(node); |
| 1387 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1391 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1392 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1394 | CodeForStatementPosition(node); |
| 1395 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1399 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1400 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1401 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1402 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1403 | if (function_return_is_shadowed_) { |
| 1404 | CodeForStatementPosition(node); |
| 1405 | LoadAndSpill(node->expression()); |
| 1406 | frame_->EmitPop(r0); |
| 1407 | function_return_.Jump(); |
| 1408 | } else { |
| 1409 | // Load the returned value. |
| 1410 | CodeForStatementPosition(node); |
| 1411 | LoadAndSpill(node->expression()); |
| 1412 | |
| 1413 | // Pop the result from the frame and prepare the frame for |
| 1414 | // returning thus making it easier to merge. |
| 1415 | frame_->EmitPop(r0); |
| 1416 | frame_->PrepareForReturn(); |
| 1417 | |
| 1418 | function_return_.Jump(); |
| 1419 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1423 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1424 | #ifdef DEBUG |
| 1425 | int original_height = frame_->height(); |
| 1426 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1427 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1428 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1429 | CodeForStatementPosition(node); |
| 1430 | LoadAndSpill(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1431 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1432 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1433 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1434 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1435 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1436 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1437 | JumpTarget verified_true; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1438 | __ cmp(r0, Operand(cp)); |
| 1439 | verified_true.Branch(eq); |
| 1440 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 1441 | verified_true.Bind(); |
| 1442 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1443 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1444 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1445 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1449 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1450 | #ifdef DEBUG |
| 1451 | int original_height = frame_->height(); |
| 1452 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1453 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1455 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | // Pop context. |
| 1457 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 1458 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1459 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1460 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1464 | int CodeGenerator::FastCaseSwitchMaxOverheadFactor() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1465 | return kFastSwitchMaxOverheadFactor; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1468 | int CodeGenerator::FastCaseSwitchMinCaseCount() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1469 | return kFastSwitchMinCaseCount; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1473 | void CodeGenerator::GenerateFastCaseSwitchJumpTable( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1474 | SwitchStatement* node, |
| 1475 | int min_index, |
| 1476 | int range, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1477 | Label* default_label, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1478 | Vector<Label*> case_targets, |
| 1479 | Vector<Label> case_labels) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1480 | VirtualFrame::SpilledScope spilled_scope; |
| 1481 | JumpTarget setup_default; |
| 1482 | JumpTarget is_smi; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1483 | |
| 1484 | // A non-null default label pointer indicates a default case among |
| 1485 | // the case labels. Otherwise we use the break target as a |
| 1486 | // "default" for failure to hit the jump table. |
| 1487 | JumpTarget* default_target = |
| 1488 | (default_label == NULL) ? node->break_target() : &setup_default; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1489 | |
| 1490 | ASSERT(kSmiTag == 0 && kSmiTagSize <= 2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1491 | frame_->EmitPop(r0); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Test for a Smi value in a HeapNumber. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1494 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1495 | is_smi.Branch(eq); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1496 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1497 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1498 | __ cmp(r1, Operand(HEAP_NUMBER_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1499 | default_target->Branch(ne); |
| 1500 | frame_->EmitPush(r0); |
| 1501 | frame_->CallRuntime(Runtime::kNumberToSmi, 1); |
| 1502 | is_smi.Bind(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1503 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1504 | if (min_index != 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1505 | // Small positive numbers can be immediate operands. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1506 | if (min_index < 0) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1507 | // If min_index is Smi::kMinValue, -min_index is not a Smi. |
| 1508 | if (Smi::IsValid(-min_index)) { |
| 1509 | __ add(r0, r0, Operand(Smi::FromInt(-min_index))); |
| 1510 | } else { |
| 1511 | __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1))); |
| 1512 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 1513 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1514 | } else { |
| 1515 | __ sub(r0, r0, Operand(Smi::FromInt(min_index))); |
| 1516 | } |
| 1517 | } |
| 1518 | __ tst(r0, Operand(0x80000000 | kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1519 | default_target->Branch(ne); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1520 | __ cmp(r0, Operand(Smi::FromInt(range))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1521 | default_target->Branch(ge); |
| 1522 | VirtualFrame* start_frame = new VirtualFrame(frame_); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1523 | __ SmiJumpTable(r0, case_targets); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1524 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1525 | GenerateFastCaseSwitchCases(node, case_labels, start_frame); |
| 1526 | |
| 1527 | // If there was a default case among the case labels, we need to |
| 1528 | // emit code to jump to it from the default target used for failure |
| 1529 | // to hit the jump table. |
| 1530 | if (default_label != NULL) { |
| 1531 | if (has_valid_frame()) { |
| 1532 | node->break_target()->Jump(); |
| 1533 | } |
| 1534 | setup_default.Bind(); |
| 1535 | frame_->MergeTo(start_frame); |
| 1536 | __ b(default_label); |
| 1537 | DeleteFrame(); |
| 1538 | } |
| 1539 | if (node->break_target()->is_linked()) { |
| 1540 | node->break_target()->Bind(); |
| 1541 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1545 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1546 | #ifdef DEBUG |
| 1547 | int original_height = frame_->height(); |
| 1548 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1549 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1550 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1551 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1552 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1553 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1554 | LoadAndSpill(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1555 | if (TryGenerateFastCaseSwitchStatement(node)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1556 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1557 | return; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1560 | JumpTarget next_test; |
| 1561 | JumpTarget fall_through; |
| 1562 | JumpTarget default_entry; |
| 1563 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1564 | ZoneList<CaseClause*>* cases = node->cases(); |
| 1565 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1566 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1567 | |
| 1568 | for (int i = 0; i < length; i++) { |
| 1569 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1570 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1571 | // Remember the default clause and compile it at the end. |
| 1572 | default_clause = clause; |
| 1573 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1576 | Comment cmnt(masm_, "[ Case clause"); |
| 1577 | // Compile the test. |
| 1578 | next_test.Bind(); |
| 1579 | next_test.Unuse(); |
| 1580 | // Duplicate TOS. |
| 1581 | __ ldr(r0, frame_->Top()); |
| 1582 | frame_->EmitPush(r0); |
| 1583 | LoadAndSpill(clause->label()); |
| 1584 | Comparison(eq, true); |
| 1585 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1586 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1587 | // Before entering the body from the test, remove the switch value from |
| 1588 | // the stack. |
| 1589 | frame_->Drop(); |
| 1590 | |
| 1591 | // Label the body so that fall through is enabled. |
| 1592 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 1593 | default_exit.Bind(); |
| 1594 | } else { |
| 1595 | fall_through.Bind(); |
| 1596 | fall_through.Unuse(); |
| 1597 | } |
| 1598 | VisitStatementsAndSpill(clause->statements()); |
| 1599 | |
| 1600 | // If control flow can fall through from the body, jump to the next body |
| 1601 | // or the end of the statement. |
| 1602 | if (frame_ != NULL) { |
| 1603 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 1604 | default_entry.Jump(); |
| 1605 | } else { |
| 1606 | fall_through.Jump(); |
| 1607 | } |
| 1608 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1611 | // The final "test" removes the switch value. |
| 1612 | next_test.Bind(); |
| 1613 | frame_->Drop(); |
| 1614 | |
| 1615 | // If there is a default clause, compile it. |
| 1616 | if (default_clause != NULL) { |
| 1617 | Comment cmnt(masm_, "[ Default clause"); |
| 1618 | default_entry.Bind(); |
| 1619 | VisitStatementsAndSpill(default_clause->statements()); |
| 1620 | // If control flow can fall out of the default and there is a case after |
| 1621 | // it, jup to that case's body. |
| 1622 | if (frame_ != NULL && default_exit.is_bound()) { |
| 1623 | default_exit.Jump(); |
| 1624 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1627 | if (fall_through.is_linked()) { |
| 1628 | fall_through.Bind(); |
| 1629 | } |
| 1630 | |
| 1631 | if (node->break_target()->is_linked()) { |
| 1632 | node->break_target()->Bind(); |
| 1633 | } |
| 1634 | node->break_target()->Unuse(); |
| 1635 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1639 | void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1640 | #ifdef DEBUG |
| 1641 | int original_height = frame_->height(); |
| 1642 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1643 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1644 | Comment cmnt(masm_, "[ LoopStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1645 | CodeForStatementPosition(node); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1646 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1647 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1648 | // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
| 1649 | // known result for the test expression, with no side effects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1650 | enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
| 1651 | if (node->cond() == NULL) { |
| 1652 | ASSERT(node->type() == LoopStatement::FOR_LOOP); |
| 1653 | info = ALWAYS_TRUE; |
| 1654 | } else { |
| 1655 | Literal* lit = node->cond()->AsLiteral(); |
| 1656 | if (lit != NULL) { |
| 1657 | if (lit->IsTrue()) { |
| 1658 | info = ALWAYS_TRUE; |
| 1659 | } else if (lit->IsFalse()) { |
| 1660 | info = ALWAYS_FALSE; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1665 | switch (node->type()) { |
| 1666 | case LoopStatement::DO_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1667 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1668 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1669 | // Label the top of the loop for the backward CFG edge. If the test |
| 1670 | // is always true we can use the continue target, and if the test is |
| 1671 | // always false there is no need. |
| 1672 | if (info == ALWAYS_TRUE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1673 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1674 | node->continue_target()->Bind(); |
| 1675 | } else if (info == ALWAYS_FALSE) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1676 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1677 | } else { |
| 1678 | ASSERT(info == DONT_KNOW); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1679 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1680 | body.Bind(); |
| 1681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1682 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1683 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1684 | VisitAndSpill(node->body()); |
| 1685 | |
| 1686 | // Compile the test. |
| 1687 | if (info == ALWAYS_TRUE) { |
| 1688 | if (has_valid_frame()) { |
| 1689 | // If control can fall off the end of the body, jump back to the |
| 1690 | // top. |
| 1691 | node->continue_target()->Jump(); |
| 1692 | } |
| 1693 | } else if (info == ALWAYS_FALSE) { |
| 1694 | // If we have a continue in the body, we only have to bind its jump |
| 1695 | // target. |
| 1696 | if (node->continue_target()->is_linked()) { |
| 1697 | node->continue_target()->Bind(); |
| 1698 | } |
| 1699 | } else { |
| 1700 | ASSERT(info == DONT_KNOW); |
| 1701 | // We have to compile the test expression if it can be reached by |
| 1702 | // control flow falling out of the body or via continue. |
| 1703 | if (node->continue_target()->is_linked()) { |
| 1704 | node->continue_target()->Bind(); |
| 1705 | } |
| 1706 | if (has_valid_frame()) { |
| 1707 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1708 | &body, node->break_target(), true); |
| 1709 | if (has_valid_frame()) { |
| 1710 | // A invalid frame here indicates that control did not |
| 1711 | // fall out of the test expression. |
| 1712 | Branch(true, &body); |
| 1713 | } |
| 1714 | } |
| 1715 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1716 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | case LoopStatement::WHILE_LOOP: { |
| 1720 | // If the test is never true and has no side effects there is no need |
| 1721 | // to compile the test or body. |
| 1722 | if (info == ALWAYS_FALSE) break; |
| 1723 | |
| 1724 | // Label the top of the loop with the continue target for the backward |
| 1725 | // CFG edge. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1726 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1727 | node->continue_target()->Bind(); |
| 1728 | |
| 1729 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1730 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1731 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1732 | &body, node->break_target(), true); |
| 1733 | if (has_valid_frame()) { |
| 1734 | // A NULL frame indicates that control did not fall out of the |
| 1735 | // test expression. |
| 1736 | Branch(false, node->break_target()); |
| 1737 | } |
| 1738 | if (has_valid_frame() || body.is_linked()) { |
| 1739 | body.Bind(); |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | if (has_valid_frame()) { |
| 1744 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1745 | VisitAndSpill(node->body()); |
| 1746 | |
| 1747 | // If control flow can fall out of the body, jump back to the top. |
| 1748 | if (has_valid_frame()) { |
| 1749 | node->continue_target()->Jump(); |
| 1750 | } |
| 1751 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | case LoopStatement::FOR_LOOP: { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1756 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1757 | |
| 1758 | if (node->init() != NULL) { |
| 1759 | VisitAndSpill(node->init()); |
| 1760 | } |
| 1761 | |
| 1762 | // There is no need to compile the test or body. |
| 1763 | if (info == ALWAYS_FALSE) break; |
| 1764 | |
| 1765 | // If there is no update statement, label the top of the loop with the |
| 1766 | // continue target, otherwise with the loop target. |
| 1767 | if (node->next() == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1768 | node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1769 | node->continue_target()->Bind(); |
| 1770 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1771 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1772 | loop.Bind(); |
| 1773 | } |
| 1774 | |
| 1775 | // If the test is always true, there is no need to compile it. |
| 1776 | if (info == DONT_KNOW) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1777 | JumpTarget body; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1778 | LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF, |
| 1779 | &body, node->break_target(), true); |
| 1780 | if (has_valid_frame()) { |
| 1781 | Branch(false, node->break_target()); |
| 1782 | } |
| 1783 | if (has_valid_frame() || body.is_linked()) { |
| 1784 | body.Bind(); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | if (has_valid_frame()) { |
| 1789 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
| 1790 | VisitAndSpill(node->body()); |
| 1791 | |
| 1792 | if (node->next() == NULL) { |
| 1793 | // If there is no update statement and control flow can fall out |
| 1794 | // of the loop, jump directly to the continue label. |
| 1795 | if (has_valid_frame()) { |
| 1796 | node->continue_target()->Jump(); |
| 1797 | } |
| 1798 | } else { |
| 1799 | // If there is an update statement and control flow can reach it |
| 1800 | // via falling out of the body of the loop or continuing, we |
| 1801 | // compile the update statement. |
| 1802 | if (node->continue_target()->is_linked()) { |
| 1803 | node->continue_target()->Bind(); |
| 1804 | } |
| 1805 | if (has_valid_frame()) { |
| 1806 | // Record source position of the statement as this code which is |
| 1807 | // after the code for the body actually belongs to the loop |
| 1808 | // statement and not the body. |
| 1809 | CodeForStatementPosition(node); |
| 1810 | VisitAndSpill(node->next()); |
| 1811 | loop.Jump(); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1815 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1816 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1819 | if (node->break_target()->is_linked()) { |
| 1820 | node->break_target()->Bind(); |
| 1821 | } |
| 1822 | node->continue_target()->Unuse(); |
| 1823 | node->break_target()->Unuse(); |
| 1824 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1828 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1829 | #ifdef DEBUG |
| 1830 | int original_height = frame_->height(); |
| 1831 | #endif |
| 1832 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1833 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1835 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1836 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1837 | JumpTarget primitive; |
| 1838 | JumpTarget jsobject; |
| 1839 | JumpTarget fixed_array; |
| 1840 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 1841 | JumpTarget end_del_check; |
| 1842 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Get the object to enumerate over (converted to JSObject). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1845 | LoadAndSpill(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | |
| 1847 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 1848 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1849 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1851 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1852 | __ cmp(r0, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1853 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1854 | |
| 1855 | // Stack layout in body: |
| 1856 | // [iteration counter (Smi)] |
| 1857 | // [length of array] |
| 1858 | // [FixedArray] |
| 1859 | // [Map or 0] |
| 1860 | // [Object] |
| 1861 | |
| 1862 | // Check if enumerable is already a JSObject |
| 1863 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1864 | primitive.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1865 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 1866 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 1867 | __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1868 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1870 | primitive.Bind(); |
| 1871 | frame_->EmitPush(r0); |
| 1872 | Result arg_count = allocator_->Allocate(r0); |
| 1873 | ASSERT(arg_count.is_valid()); |
| 1874 | __ mov(arg_count.reg(), Operand(0)); |
| 1875 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1877 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1878 | // Get the set of properties (as a FixedArray or Map). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1879 | frame_->EmitPush(r0); // duplicate the object being enumerated |
| 1880 | frame_->EmitPush(r0); |
| 1881 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1882 | |
| 1883 | // If we got a Map, we can do a fast modification check. |
| 1884 | // Otherwise, we got a FixedArray, and we have to do a slow check. |
| 1885 | __ mov(r2, Operand(r0)); |
| 1886 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 1887 | __ cmp(r1, Operand(Factory::meta_map())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1888 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Get enum cache |
| 1891 | __ mov(r1, Operand(r0)); |
| 1892 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 1893 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 1894 | __ ldr(r2, |
| 1895 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 1896 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1897 | frame_->EmitPush(r0); // map |
| 1898 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1899 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1900 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1901 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1902 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1903 | frame_->EmitPush(r0); |
| 1904 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1906 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1907 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1908 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 1909 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | |
| 1911 | // 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] | 1912 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1913 | __ mov(r0, Operand(r0, LSL, kSmiTagSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1914 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1915 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1916 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | |
| 1918 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1919 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1920 | // sp[0] : index |
| 1921 | // sp[1] : array/enum cache length |
| 1922 | // sp[2] : array or enum cache |
| 1923 | // sp[3] : 0 or map |
| 1924 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1925 | // Grab the current frame's height for the break and continue |
| 1926 | // targets only after all the state is pushed on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1927 | node->break_target()->set_direction(JumpTarget::FORWARD_ONLY); |
| 1928 | node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1930 | __ ldr(r0, frame_->ElementAt(0)); // load the current count |
| 1931 | __ ldr(r1, frame_->ElementAt(1)); // load the length |
| 1932 | __ cmp(r0, Operand(r1)); // compare to the array length |
| 1933 | node->break_target()->Branch(hs); |
| 1934 | |
| 1935 | __ ldr(r0, frame_->ElementAt(0)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1936 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1937 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1938 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 1940 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 1941 | |
| 1942 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1943 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1944 | // Check if this (still) matches the map of the enumerable. |
| 1945 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1946 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1947 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 1948 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1949 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | |
| 1951 | // 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] | 1952 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 1953 | frame_->EmitPush(r0); |
| 1954 | frame_->EmitPush(r3); // push entry |
| 1955 | Result arg_count_register = allocator_->Allocate(r0); |
| 1956 | ASSERT(arg_count_register.is_valid()); |
| 1957 | __ mov(arg_count_register.reg(), Operand(1)); |
| 1958 | Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY, |
| 1959 | CALL_JS, |
| 1960 | &arg_count_register, |
| 1961 | 2); |
| 1962 | __ mov(r3, Operand(result.reg())); |
| 1963 | result.Unuse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1964 | |
| 1965 | // If the property has been removed while iterating, we just skip it. |
| 1966 | __ cmp(r3, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1967 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1968 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1969 | end_del_check.Bind(); |
| 1970 | // Store the entry in the 'each' expression and take another spin in the |
| 1971 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 1972 | frame_->EmitPush(r3); // push entry |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1973 | { Reference each(this, node->each()); |
| 1974 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1975 | if (each.size() > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1976 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 1977 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1978 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1979 | // If the reference was to a slot we rely on the convenient property |
| 1980 | // that it doesn't matter whether a value (eg, r3 pushed above) is |
| 1981 | // right on top of or right underneath a zero-sized reference. |
| 1982 | each.SetValue(NOT_CONST_INIT); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1983 | if (each.size() > 0) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1984 | // It's safe to pop the value lying on top of the reference before |
| 1985 | // unloading the reference itself (which preserves the top of stack, |
| 1986 | // ie, now the topmost value of the non-zero sized reference), since |
| 1987 | // we will discard the top of stack after unloading the reference |
| 1988 | // anyway. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1989 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1990 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1993 | // Discard the i'th entry pushed above or else the remainder of the |
| 1994 | // reference, whichever is currently on top of the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | frame_->Drop(); |
| 1996 | |
| 1997 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | VisitAndSpill(node->body()); |
| 2000 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2001 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2002 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2003 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2004 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2005 | frame_->EmitPop(r0); |
| 2006 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2007 | frame_->EmitPush(r0); |
| 2008 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2009 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2010 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2011 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2012 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2013 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2014 | |
| 2015 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2016 | exit.Bind(); |
| 2017 | node->continue_target()->Unuse(); |
| 2018 | node->break_target()->Unuse(); |
| 2019 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2023 | void CodeGenerator::VisitTryCatch(TryCatch* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2024 | #ifdef DEBUG |
| 2025 | int original_height = frame_->height(); |
| 2026 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2027 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2028 | Comment cmnt(masm_, "[ TryCatch"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2029 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2030 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2031 | JumpTarget try_block; |
| 2032 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2033 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2034 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2035 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2036 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Store the caught exception in the catch variable. |
| 2039 | { Reference ref(this, node->catch_var()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2040 | ASSERT(ref.is_slot()); |
| 2041 | // Here we make use of the convenient property that it doesn't matter |
| 2042 | // whether a value is immediately on top of or underneath a zero-sized |
| 2043 | // reference. |
| 2044 | ref.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2048 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2050 | VisitStatementsAndSpill(node->catch_block()->statements()); |
| 2051 | if (frame_ != NULL) { |
| 2052 | exit.Jump(); |
| 2053 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2054 | |
| 2055 | |
| 2056 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2057 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2059 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2060 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2061 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2062 | // Shadow the labels for all escapes from the try block, including |
| 2063 | // returns. During shadowing, the original label is hidden as the |
| 2064 | // LabelShadow and operations on the original actually affect the |
| 2065 | // shadowing label. |
| 2066 | // |
| 2067 | // We should probably try to unify the escaping labels and the return |
| 2068 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2069 | int nof_escapes = node->escaping_targets()->length(); |
| 2070 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2071 | |
| 2072 | // Add the shadow target for the function return. |
| 2073 | static const int kReturnShadowIndex = 0; |
| 2074 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2075 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2076 | function_return_is_shadowed_ = true; |
| 2077 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2078 | |
| 2079 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2080 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2081 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2085 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2088 | // After shadowing stops, the original labels are unshadowed and the |
| 2089 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2090 | bool has_unlinks = false; |
| 2091 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2092 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2093 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2094 | } |
| 2095 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2096 | |
| 2097 | // Get an external reference to the handler address. |
| 2098 | ExternalReference handler_address(Top::k_handler_address); |
| 2099 | |
| 2100 | // The next handler address is at kNextIndex in the stack. |
| 2101 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2102 | // If we can fall off the end of the try block, unlink from try chain. |
| 2103 | if (has_valid_frame()) { |
| 2104 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2105 | __ mov(r3, Operand(handler_address)); |
| 2106 | __ str(r1, MemOperand(r3)); |
| 2107 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2108 | if (has_unlinks) { |
| 2109 | exit.Jump(); |
| 2110 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2113 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2114 | // jumped to. Deallocate each shadow target. |
| 2115 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2116 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2117 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2118 | shadows[i]->Bind(); |
| 2119 | // Because we can be jumping here (to spilled code) from unspilled |
| 2120 | // code, we need to reestablish a spilled frame at this block. |
| 2121 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2123 | // Reload sp from the top handler, because some statements that we |
| 2124 | // 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] | 2125 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2127 | // The stack pointer was restored to just below the code slot |
| 2128 | // (the topmost slot) in the handler. |
| 2129 | frame_->Forget(frame_->height() - handler_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2131 | // kNextIndex is off by one because the code slot has already |
| 2132 | // been dropped. |
| 2133 | __ ldr(r1, frame_->ElementAt(kNextIndex - 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2135 | // The code slot has already been dropped from the handler. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2136 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2137 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2138 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2139 | frame_->PrepareForReturn(); |
| 2140 | } |
| 2141 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2145 | exit.Bind(); |
| 2146 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2150 | void CodeGenerator::VisitTryFinally(TryFinally* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2151 | #ifdef DEBUG |
| 2152 | int original_height = frame_->height(); |
| 2153 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2154 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2155 | Comment cmnt(masm_, "[ TryFinally"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2156 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2157 | |
| 2158 | // State: Used to keep track of reason for entering the finally |
| 2159 | // block. Should probably be extended to hold information for |
| 2160 | // break/continue from within the try block. |
| 2161 | enum { FALLING, THROWING, JUMPING }; |
| 2162 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2163 | JumpTarget try_block; |
| 2164 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2165 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2166 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2167 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2168 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2169 | // In case of thrown exceptions, this is where we continue. |
| 2170 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2171 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2172 | |
| 2173 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2174 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2175 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2177 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2179 | // Shadow the labels for all escapes from the try block, including |
| 2180 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2181 | // operations on the original actually affect the shadowing label. |
| 2182 | // |
| 2183 | // We should probably try to unify the escaping labels and the return |
| 2184 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2185 | int nof_escapes = node->escaping_targets()->length(); |
| 2186 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2187 | |
| 2188 | // Add the shadow target for the function return. |
| 2189 | static const int kReturnShadowIndex = 0; |
| 2190 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2191 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2192 | function_return_is_shadowed_ = true; |
| 2193 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2194 | |
| 2195 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2196 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2197 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | // Generate code for the statements in the try block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2201 | VisitStatementsAndSpill(node->try_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2202 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2203 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2204 | // After shadowing stops, the original labels are unshadowed and the |
| 2205 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2207 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2208 | shadows[i]->StopShadowing(); |
| 2209 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2210 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2211 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2212 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2213 | // Get an external reference to the handler address. |
| 2214 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2215 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2216 | // The next handler address is at kNextIndex in the stack. |
| 2217 | const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize; |
| 2218 | // If we can fall off the end of the try block, unlink from the try |
| 2219 | // chain and set the state on the frame to FALLING. |
| 2220 | if (has_valid_frame()) { |
| 2221 | __ ldr(r1, frame_->ElementAt(kNextIndex)); |
| 2222 | __ mov(r3, Operand(handler_address)); |
| 2223 | __ str(r1, MemOperand(r3)); |
| 2224 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize); |
| 2225 | |
| 2226 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2227 | // state in r2, then jump around the unlink blocks if any. |
| 2228 | __ mov(r0, Operand(Factory::undefined_value())); |
| 2229 | frame_->EmitPush(r0); |
| 2230 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2231 | if (nof_unlinks > 0) { |
| 2232 | finally_block.Jump(); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | // Generate code to unlink and set the state for the (formerly) |
| 2237 | // shadowing targets that have been jumped to. |
| 2238 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2239 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2240 | // If we have come from the shadowed return, the return value is |
| 2241 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2242 | // until it is pushed. |
| 2243 | // |
| 2244 | // Because we can be jumping here (to spilled code) from |
| 2245 | // unspilled code, we need to reestablish a spilled frame at |
| 2246 | // this block. |
| 2247 | shadows[i]->Bind(); |
| 2248 | frame_->SpillAll(); |
| 2249 | |
| 2250 | // Reload sp from the top handler, because some statements that |
| 2251 | // we break from (eg, for...in) may have left stuff on the |
| 2252 | // stack. |
| 2253 | __ mov(r3, Operand(handler_address)); |
| 2254 | __ ldr(sp, MemOperand(r3)); |
| 2255 | // The stack pointer was restored to the address slot in the handler. |
| 2256 | ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize); |
| 2257 | frame_->Forget(frame_->height() - handler_height + 1); |
| 2258 | |
| 2259 | // Unlink this handler and drop it from the frame. The next |
| 2260 | // handler address is now on top of the frame. |
| 2261 | frame_->EmitPop(r1); |
| 2262 | __ str(r1, MemOperand(r3)); |
| 2263 | // The top (code) and the second (handler) slot have both been |
| 2264 | // dropped already. |
| 2265 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2); |
| 2266 | |
| 2267 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2268 | // If this label shadowed the function return, materialize the |
| 2269 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2270 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2271 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2272 | // Fake TOS for targets that shadowed breaks and continues. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2273 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2274 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2275 | } |
| 2276 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2277 | if (--nof_unlinks > 0) { |
| 2278 | // If this is not the last unlink block, jump around the next. |
| 2279 | finally_block.Jump(); |
| 2280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2284 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2285 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2286 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2287 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2288 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2289 | |
| 2290 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2291 | // and the state - while evaluating the finally block. |
| 2292 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2293 | // Generate code for the statements in the finally block. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2294 | VisitStatementsAndSpill(node->finally_block()->statements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2295 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2296 | if (has_valid_frame()) { |
| 2297 | // Restore state and return value or faked TOS. |
| 2298 | frame_->EmitPop(r2); |
| 2299 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2302 | // Generate code to jump to the right destination for all used |
| 2303 | // formerly shadowing targets. Deallocate each shadow target. |
| 2304 | for (int i = 0; i < shadows.length(); i++) { |
| 2305 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2306 | JumpTarget* original = shadows[i]->other_target(); |
| 2307 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2308 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2309 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2310 | skip.Branch(ne); |
| 2311 | frame_->PrepareForReturn(); |
| 2312 | original->Jump(); |
| 2313 | skip.Bind(); |
| 2314 | } else { |
| 2315 | original->Branch(eq); |
| 2316 | } |
| 2317 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2319 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2320 | if (has_valid_frame()) { |
| 2321 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2322 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2323 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2324 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2325 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2326 | // Rethrow exception. |
| 2327 | frame_->EmitPush(r0); |
| 2328 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2329 | |
| 2330 | // Done. |
| 2331 | exit.Bind(); |
| 2332 | } |
| 2333 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2337 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2338 | #ifdef DEBUG |
| 2339 | int original_height = frame_->height(); |
| 2340 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2341 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2342 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2343 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2344 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2345 | frame_->CallRuntime(Runtime::kDebugBreak, 0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2346 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2347 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2348 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2352 | void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2353 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2354 | ASSERT(boilerplate->IsBoilerplate()); |
| 2355 | |
| 2356 | // Push the boilerplate on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2357 | __ mov(r0, Operand(boilerplate)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2358 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2359 | |
| 2360 | // Create a new closure. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2361 | frame_->EmitPush(cp); |
| 2362 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2363 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2367 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2368 | #ifdef DEBUG |
| 2369 | int original_height = frame_->height(); |
| 2370 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2371 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2372 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2373 | |
| 2374 | // Build the function boilerplate and instantiate it. |
| 2375 | Handle<JSFunction> boilerplate = BuildBoilerplate(node); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2376 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2377 | if (HasStackOverflow()) { |
| 2378 | ASSERT(frame_->height() == original_height); |
| 2379 | return; |
| 2380 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2381 | InstantiateBoilerplate(boilerplate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2382 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2386 | void CodeGenerator::VisitFunctionBoilerplateLiteral( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | FunctionBoilerplateLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2388 | #ifdef DEBUG |
| 2389 | int original_height = frame_->height(); |
| 2390 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2391 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2392 | Comment cmnt(masm_, "[ FunctionBoilerplateLiteral"); |
| 2393 | InstantiateBoilerplate(node->boilerplate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2394 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2398 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2399 | #ifdef DEBUG |
| 2400 | int original_height = frame_->height(); |
| 2401 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2402 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2403 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2404 | JumpTarget then; |
| 2405 | JumpTarget else_; |
| 2406 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2407 | LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF, |
| 2408 | &then, &else_, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2409 | Branch(false, &else_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2410 | then.Bind(); |
| 2411 | LoadAndSpill(node->then_expression(), typeof_state()); |
| 2412 | exit.Jump(); |
| 2413 | else_.Bind(); |
| 2414 | LoadAndSpill(node->else_expression(), typeof_state()); |
| 2415 | exit.Bind(); |
| 2416 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2420 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2421 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2422 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2423 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2424 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2425 | JumpTarget slow; |
| 2426 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2427 | |
| 2428 | // Generate fast-case code for variables that might be shadowed by |
| 2429 | // eval-introduced variables. Eval is used a lot without |
| 2430 | // introducing variables. In those cases, we do not want to |
| 2431 | // perform a runtime call for all variables in the scope |
| 2432 | // containing the eval. |
| 2433 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 2434 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2435 | // If there was no control flow to slow, we can exit early. |
| 2436 | if (!slow.is_linked()) { |
| 2437 | frame_->EmitPush(r0); |
| 2438 | return; |
| 2439 | } |
| 2440 | |
| 2441 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2442 | |
| 2443 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 2444 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 2445 | // Only generate the fast case for locals that rewrite to slots. |
| 2446 | // This rules out argument loads. |
| 2447 | if (potential_slot != NULL) { |
| 2448 | __ ldr(r0, |
| 2449 | ContextSlotOperandCheckExtensions(potential_slot, |
| 2450 | r1, |
| 2451 | r2, |
| 2452 | &slow)); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2453 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 2454 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2455 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
| 2456 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2457 | // There is always control flow to slow from |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 2458 | // ContextSlotOperandCheckExtensions so we have to jump around |
| 2459 | // it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2460 | done.Jump(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2464 | slow.Bind(); |
| 2465 | frame_->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2466 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2467 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2468 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2469 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2470 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2471 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2472 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2473 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2474 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2475 | done.Bind(); |
| 2476 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2477 | |
| 2478 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2479 | // Note: We would like to keep the assert below, but it fires because of |
| 2480 | // some nasty code in LoadTypeofExpression() which should be removed... |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2481 | // ASSERT(!slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2482 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2483 | // Special handling for locals allocated in registers. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2484 | __ ldr(r0, SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2485 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2486 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2487 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 2488 | // initialized yet) which needs to be converted into the 'undefined' |
| 2489 | // value. |
| 2490 | Comment cmnt(masm_, "[ Unhole const"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2491 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2492 | __ cmp(r0, Operand(Factory::the_hole_value())); |
| 2493 | __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2494 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2500 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 2501 | TypeofState typeof_state, |
| 2502 | Register tmp, |
| 2503 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2504 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2505 | // Check that no extension objects have been created by calls to |
| 2506 | // eval from the current scope to the global scope. |
| 2507 | Register context = cp; |
| 2508 | Scope* s = scope(); |
| 2509 | while (s != NULL) { |
| 2510 | if (s->num_heap_slots() > 0) { |
| 2511 | if (s->calls_eval()) { |
| 2512 | // Check that extension is NULL. |
| 2513 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 2514 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2515 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2516 | } |
| 2517 | // Load next context in chain. |
| 2518 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 2519 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2520 | context = tmp; |
| 2521 | } |
| 2522 | // If no outer scope calls eval, we do not need to check more |
| 2523 | // context extensions. |
| 2524 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 2525 | s = s->outer_scope(); |
| 2526 | } |
| 2527 | |
| 2528 | if (s->is_eval_scope()) { |
| 2529 | Label next, fast; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2530 | if (!context.is(tmp)) { |
| 2531 | __ mov(tmp, Operand(context)); |
| 2532 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2533 | __ bind(&next); |
| 2534 | // Terminate at global context. |
| 2535 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
| 2536 | __ cmp(tmp2, Operand(Factory::global_context_map())); |
| 2537 | __ b(eq, &fast); |
| 2538 | // Check that extension is NULL. |
| 2539 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 2540 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2541 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2542 | // Load next context in chain. |
| 2543 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 2544 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 2545 | __ b(&next); |
| 2546 | __ bind(&fast); |
| 2547 | } |
| 2548 | |
| 2549 | // All extension objects were empty and it is safe to use a global |
| 2550 | // load IC call. |
| 2551 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2552 | // Load the global object. |
| 2553 | LoadGlobal(); |
| 2554 | // Setup the name register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2555 | Result name = allocator_->Allocate(r2); |
| 2556 | ASSERT(name.is_valid()); // We are in spilled code. |
| 2557 | __ mov(name.reg(), Operand(slot->var()->name())); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2558 | // Call IC stub. |
| 2559 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2560 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2561 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2562 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2565 | // Drop the global object. The result is in r0. |
| 2566 | frame_->Drop(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2570 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2571 | #ifdef DEBUG |
| 2572 | int original_height = frame_->height(); |
| 2573 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2574 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2575 | Comment cmnt(masm_, "[ Slot"); |
| 2576 | LoadFromSlot(node, typeof_state()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2577 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2578 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2579 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2580 | |
| 2581 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2582 | #ifdef DEBUG |
| 2583 | int original_height = frame_->height(); |
| 2584 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2585 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2586 | Comment cmnt(masm_, "[ VariableProxy"); |
| 2587 | |
| 2588 | Variable* var = node->var(); |
| 2589 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2590 | if (expr != NULL) { |
| 2591 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2592 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2593 | ASSERT(var->is_global()); |
| 2594 | Reference ref(this, node); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | ref.GetValueAndSpill(typeof_state()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2596 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2597 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2601 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2602 | #ifdef DEBUG |
| 2603 | int original_height = frame_->height(); |
| 2604 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2605 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2606 | Comment cmnt(masm_, "[ Literal"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2607 | __ mov(r0, Operand(node->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2608 | frame_->EmitPush(r0); |
| 2609 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2613 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2614 | #ifdef DEBUG |
| 2615 | int original_height = frame_->height(); |
| 2616 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2617 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 2619 | |
| 2620 | // Retrieve the literal array and check the allocated entry. |
| 2621 | |
| 2622 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2623 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Load the literals array of the function. |
| 2626 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2627 | |
| 2628 | // Load the literal at the ast saved index. |
| 2629 | int literal_offset = |
| 2630 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2631 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2632 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2633 | JumpTarget done; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2634 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2635 | done.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2636 | |
| 2637 | // If the entry is undefined we call the runtime system to computed |
| 2638 | // the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2639 | frame_->EmitPush(r1); // literal array (0) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2640 | __ mov(r0, Operand(Smi::FromInt(node->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2641 | frame_->EmitPush(r0); // literal index (1) |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2642 | __ mov(r0, Operand(node->pattern())); // RegExp pattern (2) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2643 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2644 | __ mov(r0, Operand(node->flags())); // RegExp flags (3) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2645 | frame_->EmitPush(r0); |
| 2646 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2647 | __ mov(r2, Operand(r0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2648 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2649 | done.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2650 | // Push the literal. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2651 | frame_->EmitPush(r2); |
| 2652 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | |
| 2656 | // This deferred code stub will be used for creating the boilerplate |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2657 | // by calling Runtime_CreateObjectLiteralBoilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2658 | // Each created boilerplate is stored in the JSFunction and they are |
| 2659 | // therefore context dependent. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2660 | class DeferredObjectLiteral: public DeferredCode { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2661 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2662 | explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2663 | set_comment("[ DeferredObjectLiteral"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2664 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2665 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | virtual void Generate(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2667 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | private: |
| 2669 | ObjectLiteral* node_; |
| 2670 | }; |
| 2671 | |
| 2672 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2673 | #undef __ |
| 2674 | #define __ ACCESS_MASM(masm) |
| 2675 | |
| 2676 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2677 | void DeferredObjectLiteral::Generate() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2678 | MacroAssembler* masm = cgen()->masm(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2679 | // Argument is passed in r1. |
| 2680 | enter()->Bind(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2681 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2682 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2683 | // If the entry is undefined we call the runtime system to compute |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | // the literal. |
| 2685 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2686 | VirtualFrame* frame = cgen()->frame(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2687 | // Literal array (0). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2688 | frame->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2689 | // Literal index (1). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2690 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2691 | frame->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2692 | // Constant properties (2). |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2693 | __ mov(r0, Operand(node_->constant_properties())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2694 | frame->EmitPush(r0); |
| 2695 | Result boilerplate = |
| 2696 | frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3); |
| 2697 | __ mov(r2, Operand(boilerplate.reg())); |
| 2698 | // Result is returned in r2. |
| 2699 | exit_.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2703 | #undef __ |
| 2704 | #define __ ACCESS_MASM(masm_) |
| 2705 | |
| 2706 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2707 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2708 | #ifdef DEBUG |
| 2709 | int original_height = frame_->height(); |
| 2710 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2711 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2712 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 2713 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2714 | DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Retrieve the literal array and check the allocated entry. |
| 2717 | |
| 2718 | // Load the function of this activation. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2719 | __ ldr(r1, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2720 | |
| 2721 | // Load the literals array of the function. |
| 2722 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2723 | |
| 2724 | // Load the literal at the ast saved index. |
| 2725 | int literal_offset = |
| 2726 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2727 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2728 | |
| 2729 | // Check whether we need to materialize the object literal boilerplate. |
| 2730 | // If so, jump to the deferred code. |
| 2731 | __ cmp(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2732 | deferred->enter()->Branch(eq); |
| 2733 | deferred->BindExit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2734 | |
| 2735 | // Push the object literal boilerplate. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2736 | frame_->EmitPush(r2); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2737 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2738 | // Clone the boilerplate object. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2739 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2740 | if (node->depth() == 1) { |
| 2741 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2742 | } |
| 2743 | frame_->CallRuntime(clone_function_id, 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2744 | frame_->EmitPush(r0); // save the result |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2745 | // r0: cloned object literal |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2746 | |
| 2747 | for (int i = 0; i < node->properties()->length(); i++) { |
| 2748 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 2749 | Literal* key = property->key(); |
| 2750 | Expression* value = property->value(); |
| 2751 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2752 | case ObjectLiteral::Property::CONSTANT: |
| 2753 | break; |
| 2754 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 2755 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 2756 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2757 | case ObjectLiteral::Property::COMPUTED: // fall through |
| 2758 | case ObjectLiteral::Property::PROTOTYPE: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2759 | frame_->EmitPush(r0); // dup the result |
| 2760 | LoadAndSpill(key); |
| 2761 | LoadAndSpill(value); |
| 2762 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2763 | // restore r0 |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2764 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | case ObjectLiteral::Property::SETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2768 | frame_->EmitPush(r0); |
| 2769 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2770 | __ mov(r0, Operand(Smi::FromInt(1))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2771 | frame_->EmitPush(r0); |
| 2772 | LoadAndSpill(value); |
| 2773 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2774 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2775 | break; |
| 2776 | } |
| 2777 | case ObjectLiteral::Property::GETTER: { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2778 | frame_->EmitPush(r0); |
| 2779 | LoadAndSpill(key); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2780 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2781 | frame_->EmitPush(r0); |
| 2782 | LoadAndSpill(value); |
| 2783 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2784 | __ ldr(r0, frame_->Top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2785 | break; |
| 2786 | } |
| 2787 | } |
| 2788 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2789 | ASSERT(frame_->height() == original_height + 1); |
| 2790 | } |
| 2791 | |
| 2792 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2793 | // This deferred code stub will be used for creating the boilerplate |
| 2794 | // by calling Runtime_CreateArrayLiteralBoilerplate. |
| 2795 | // Each created boilerplate is stored in the JSFunction and they are |
| 2796 | // therefore context dependent. |
| 2797 | class DeferredArrayLiteral: public DeferredCode { |
| 2798 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2799 | explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2800 | set_comment("[ DeferredArrayLiteral"); |
| 2801 | } |
| 2802 | |
| 2803 | virtual void Generate(); |
| 2804 | |
| 2805 | private: |
| 2806 | ArrayLiteral* node_; |
| 2807 | }; |
| 2808 | |
| 2809 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2810 | #undef __ |
| 2811 | #define __ ACCESS_MASM(masm) |
| 2812 | |
| 2813 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2814 | void DeferredArrayLiteral::Generate() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2815 | MacroAssembler* masm = cgen()->masm(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2816 | // Argument is passed in r1. |
| 2817 | enter()->Bind(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2818 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2819 | |
| 2820 | // If the entry is undefined we call the runtime system to computed |
| 2821 | // the literal. |
| 2822 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2823 | VirtualFrame* frame = cgen()->frame(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2824 | // Literal array (0). |
| 2825 | frame->EmitPush(r1); |
| 2826 | // Literal index (1). |
| 2827 | __ mov(r0, Operand(Smi::FromInt(node_->literal_index()))); |
| 2828 | frame->EmitPush(r0); |
| 2829 | // Constant properties (2). |
| 2830 | __ mov(r0, Operand(node_->literals())); |
| 2831 | frame->EmitPush(r0); |
| 2832 | Result boilerplate = |
| 2833 | frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3); |
| 2834 | __ mov(r2, Operand(boilerplate.reg())); |
| 2835 | // Result is returned in r2. |
| 2836 | exit_.Jump(); |
| 2837 | } |
| 2838 | |
| 2839 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2840 | #undef __ |
| 2841 | #define __ ACCESS_MASM(masm_) |
| 2842 | |
| 2843 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2844 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2845 | #ifdef DEBUG |
| 2846 | int original_height = frame_->height(); |
| 2847 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2848 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2849 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2850 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame^] | 2851 | DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2852 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2853 | // Retrieve the literal array and check the allocated entry. |
| 2854 | |
| 2855 | // Load the function of this activation. |
| 2856 | __ ldr(r1, frame_->Function()); |
| 2857 | |
| 2858 | // Load the literals array of the function. |
| 2859 | __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset)); |
| 2860 | |
| 2861 | // Load the literal at the ast saved index. |
| 2862 | int literal_offset = |
| 2863 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
| 2864 | __ ldr(r2, FieldMemOperand(r1, literal_offset)); |
| 2865 | |
| 2866 | // Check whether we need to materialize the object literal boilerplate. |
| 2867 | // If so, jump to the deferred code. |
| 2868 | __ cmp(r2, Operand(Factory::undefined_value())); |
| 2869 | deferred->enter()->Branch(eq); |
| 2870 | deferred->BindExit(); |
| 2871 | |
| 2872 | // Push the object literal boilerplate. |
| 2873 | frame_->EmitPush(r2); |
| 2874 | |
| 2875 | // Clone the boilerplate object. |
| 2876 | Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate; |
| 2877 | if (node->depth() == 1) { |
| 2878 | clone_function_id = Runtime::kCloneShallowLiteralBoilerplate; |
| 2879 | } |
| 2880 | frame_->CallRuntime(clone_function_id, 1); |
| 2881 | frame_->EmitPush(r0); // save the result |
| 2882 | // r0: cloned object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2883 | |
| 2884 | // Generate code to set the elements in the array that are not |
| 2885 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2886 | for (int i = 0; i < node->values()->length(); i++) { |
| 2887 | Expression* value = node->values()->at(i); |
| 2888 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2889 | // If value is a literal the property value is already set in the |
| 2890 | // boilerplate object. |
| 2891 | if (value->AsLiteral() != NULL) continue; |
| 2892 | // If value is a materialized literal the property value is already set |
| 2893 | // in the boilerplate object if it is simple. |
| 2894 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2895 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2896 | // The property must be set by generated code. |
| 2897 | LoadAndSpill(value); |
| 2898 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2899 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2900 | // Fetch the object literal. |
| 2901 | __ ldr(r1, frame_->Top()); |
| 2902 | // Get the elements array. |
| 2903 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2904 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2905 | // Write to the indexed properties array. |
| 2906 | int offset = i * kPointerSize + Array::kHeaderSize; |
| 2907 | __ str(r0, FieldMemOperand(r1, offset)); |
| 2908 | |
| 2909 | // Update the write barrier for the array address. |
| 2910 | __ mov(r3, Operand(offset)); |
| 2911 | __ RecordWrite(r1, r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2912 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2913 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2917 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2918 | #ifdef DEBUG |
| 2919 | int original_height = frame_->height(); |
| 2920 | #endif |
| 2921 | ASSERT(!in_spilled_code()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2922 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2923 | // Call runtime routine to allocate the catch extension object and |
| 2924 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2925 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 2926 | LoadAndSpill(node->key()); |
| 2927 | LoadAndSpill(node->value()); |
| 2928 | Result result = |
| 2929 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 2930 | frame_->EmitPush(result.reg()); |
| 2931 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2935 | void CodeGenerator::VisitAssignment(Assignment* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2936 | #ifdef DEBUG |
| 2937 | int original_height = frame_->height(); |
| 2938 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2939 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2940 | Comment cmnt(masm_, "[ Assignment"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2941 | CodeForStatementPosition(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2942 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2943 | { Reference target(this, node->target()); |
| 2944 | if (target.is_illegal()) { |
| 2945 | // Fool the virtual frame into thinking that we left the assignment's |
| 2946 | // value on the frame. |
| 2947 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 2948 | frame_->EmitPush(r0); |
| 2949 | ASSERT(frame_->height() == original_height + 1); |
| 2950 | return; |
| 2951 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2952 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2953 | if (node->op() == Token::ASSIGN || |
| 2954 | node->op() == Token::INIT_VAR || |
| 2955 | node->op() == Token::INIT_CONST) { |
| 2956 | LoadAndSpill(node->value()); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2957 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2958 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2959 | // +=, *= and similar binary assignments. |
| 2960 | // Get the old value of the lhs. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2961 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 2962 | Literal* literal = node->value()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2963 | bool overwrite = |
| 2964 | (node->value()->AsBinaryOperation() != NULL && |
| 2965 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2966 | if (literal != NULL && literal->handle()->IsSmi()) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2967 | SmiOperation(node->binary_op(), |
| 2968 | literal->handle(), |
| 2969 | false, |
| 2970 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2971 | frame_->EmitPush(r0); |
| 2972 | |
| 2973 | } else { |
| 2974 | LoadAndSpill(node->value()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2975 | GenericBinaryOperation(node->binary_op(), |
| 2976 | overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2977 | frame_->EmitPush(r0); |
| 2978 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2979 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2980 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2981 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 2982 | if (var != NULL && |
| 2983 | (var->mode() == Variable::CONST) && |
| 2984 | node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) { |
| 2985 | // Assignment ignored - leave the value on the stack. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2986 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2987 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2988 | CodeForSourcePosition(node->position()); |
| 2989 | if (node->op() == Token::INIT_CONST) { |
| 2990 | // Dynamic constant initializations must use the function context |
| 2991 | // and initialize the actual constant declared. Dynamic variable |
| 2992 | // initializations are simply assignments and use SetValue. |
| 2993 | target.SetValue(CONST_INIT); |
| 2994 | } else { |
| 2995 | target.SetValue(NOT_CONST_INIT); |
| 2996 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2999 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3003 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3004 | #ifdef DEBUG |
| 3005 | int original_height = frame_->height(); |
| 3006 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3007 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3008 | Comment cmnt(masm_, "[ Throw"); |
| 3009 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3010 | LoadAndSpill(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3011 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3012 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3013 | frame_->EmitPush(r0); |
| 3014 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3018 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3019 | #ifdef DEBUG |
| 3020 | int original_height = frame_->height(); |
| 3021 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3022 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3023 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3024 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3025 | { Reference property(this, node); |
| 3026 | property.GetValueAndSpill(typeof_state()); |
| 3027 | } |
| 3028 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3032 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3033 | #ifdef DEBUG |
| 3034 | int original_height = frame_->height(); |
| 3035 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3036 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3037 | Comment cmnt(masm_, "[ Call"); |
| 3038 | |
| 3039 | ZoneList<Expression*>* args = node->arguments(); |
| 3040 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3041 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3042 | // Standard function call. |
| 3043 | |
| 3044 | // Check if the function is a variable or a property. |
| 3045 | Expression* function = node->expression(); |
| 3046 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3047 | Property* property = function->AsProperty(); |
| 3048 | |
| 3049 | // ------------------------------------------------------------------------ |
| 3050 | // Fast-case: Use inline caching. |
| 3051 | // --- |
| 3052 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3053 | // must be resolved after the arguments have been evaluated. The IC code |
| 3054 | // automatically handles this by loading the arguments before the function |
| 3055 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3056 | // ------------------------------------------------------------------------ |
| 3057 | |
| 3058 | if (var != NULL && !var->is_this() && var->is_global()) { |
| 3059 | // ---------------------------------- |
| 3060 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3061 | // ---------------------------------- |
| 3062 | |
| 3063 | // 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] | 3064 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3065 | frame_->EmitPush(r0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3066 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3067 | // Pass the global object as the receiver and let the IC stub |
| 3068 | // patch the stack to use the global proxy as 'this' in the |
| 3069 | // invoked function. |
| 3070 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | |
| 3072 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3073 | int arg_count = args->length(); |
| 3074 | for (int i = 0; i < arg_count; i++) { |
| 3075 | LoadAndSpill(args->at(i)); |
| 3076 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3077 | |
| 3078 | // Setup the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3079 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3080 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3081 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3082 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 3083 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3084 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3085 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3086 | frame_->Drop(); |
| 3087 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3088 | |
| 3089 | } else if (var != NULL && var->slot() != NULL && |
| 3090 | var->slot()->type() == Slot::LOOKUP) { |
| 3091 | // ---------------------------------- |
| 3092 | // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj |
| 3093 | // ---------------------------------- |
| 3094 | |
| 3095 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3096 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3097 | __ mov(r0, Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3098 | frame_->EmitPush(r0); |
| 3099 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3100 | // r0: slot value; r1: receiver |
| 3101 | |
| 3102 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3103 | frame_->EmitPush(r0); // function |
| 3104 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3105 | |
| 3106 | // Call the function. |
| 3107 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3108 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | |
| 3110 | } else if (property != NULL) { |
| 3111 | // Check if the key is a literal string. |
| 3112 | Literal* literal = property->key()->AsLiteral(); |
| 3113 | |
| 3114 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 3115 | // ------------------------------------------------------------------ |
| 3116 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 3117 | // ------------------------------------------------------------------ |
| 3118 | |
| 3119 | // 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] | 3120 | __ mov(r0, Operand(literal->handle())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3121 | frame_->EmitPush(r0); |
| 3122 | LoadAndSpill(property->obj()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3123 | |
| 3124 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3125 | int arg_count = args->length(); |
| 3126 | for (int i = 0; i < arg_count; i++) { |
| 3127 | LoadAndSpill(args->at(i)); |
| 3128 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3129 | |
| 3130 | // Set the receiver register and call the IC initialization code. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3131 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3132 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3133 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3134 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3135 | __ ldr(cp, frame_->Context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3136 | |
| 3137 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3138 | frame_->Drop(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3139 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3140 | 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] | 3141 | |
| 3142 | } else { |
| 3143 | // ------------------------------------------- |
| 3144 | // JavaScript example: 'array[index](1, 2, 3)' |
| 3145 | // ------------------------------------------- |
| 3146 | |
| 3147 | // Load the function to call from the property through a reference. |
| 3148 | Reference ref(this, property); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3149 | ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3150 | |
| 3151 | // Pass receiver to called function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3152 | if (property->is_synthetic()) { |
| 3153 | LoadGlobalReceiver(r0); |
| 3154 | } else { |
| 3155 | __ ldr(r0, frame_->ElementAt(ref.size())); |
| 3156 | frame_->EmitPush(r0); |
| 3157 | } |
| 3158 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3159 | // Call the function. |
| 3160 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3161 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | } else { |
| 3165 | // ---------------------------------- |
| 3166 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 3167 | // ---------------------------------- |
| 3168 | |
| 3169 | // Load the function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3170 | LoadAndSpill(function); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3171 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3172 | // Pass the global proxy as the receiver. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3173 | LoadGlobalReceiver(r0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3175 | // Call the function. |
| 3176 | CallWithArguments(args, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3177 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3178 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3179 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3183 | void CodeGenerator::VisitCallEval(CallEval* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3184 | #ifdef DEBUG |
| 3185 | int original_height = frame_->height(); |
| 3186 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3187 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3188 | Comment cmnt(masm_, "[ CallEval"); |
| 3189 | |
| 3190 | // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve |
| 3191 | // the function we need to call and the receiver of the call. |
| 3192 | // Then we call the resolved function using the given arguments. |
| 3193 | |
| 3194 | ZoneList<Expression*>* args = node->arguments(); |
| 3195 | Expression* function = node->expression(); |
| 3196 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3197 | CodeForStatementPosition(node); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3198 | |
| 3199 | // Prepare stack for call to resolved function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3200 | LoadAndSpill(function); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3201 | __ mov(r2, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3202 | frame_->EmitPush(r2); // Slot for receiver |
| 3203 | int arg_count = args->length(); |
| 3204 | for (int i = 0; i < arg_count; i++) { |
| 3205 | LoadAndSpill(args->at(i)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | // Prepare stack for call to ResolvePossiblyDirectEval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3209 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3210 | frame_->EmitPush(r1); |
| 3211 | if (arg_count > 0) { |
| 3212 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3213 | frame_->EmitPush(r1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3214 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3215 | frame_->EmitPush(r2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | // Resolve the call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3219 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3220 | |
| 3221 | // Touch up stack with the right values for the function and the receiver. |
| 3222 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3223 | __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3224 | __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3225 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3226 | |
| 3227 | // Call the function. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3228 | CodeForSourcePosition(node->position()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3229 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3230 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3231 | CallFunctionStub call_function(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3232 | frame_->CallStub(&call_function, arg_count + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3233 | |
| 3234 | __ ldr(cp, frame_->Context()); |
| 3235 | // Remove the function from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3236 | frame_->Drop(); |
| 3237 | frame_->EmitPush(r0); |
| 3238 | ASSERT(frame_->height() == original_height + 1); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3242 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3243 | #ifdef DEBUG |
| 3244 | int original_height = frame_->height(); |
| 3245 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3246 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3247 | Comment cmnt(masm_, "[ CallNew"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3248 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | |
| 3250 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 3251 | // expression in new calls must be evaluated before the |
| 3252 | // arguments. This is different from ordinary calls, where the |
| 3253 | // actual function to call is resolved after the arguments have been |
| 3254 | // evaluated. |
| 3255 | |
| 3256 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3257 | // receiver. There is no need to use the global proxy here because |
| 3258 | // it will always be replaced with a newly allocated object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3259 | LoadAndSpill(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3260 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3261 | |
| 3262 | // Push the arguments ("left-to-right") on the stack. |
| 3263 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3264 | int arg_count = args->length(); |
| 3265 | for (int i = 0; i < arg_count; i++) { |
| 3266 | LoadAndSpill(args->at(i)); |
| 3267 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3268 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3269 | // r0: the number of arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3270 | Result num_args = allocator_->Allocate(r0); |
| 3271 | ASSERT(num_args.is_valid()); |
| 3272 | __ mov(num_args.reg(), Operand(arg_count)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3273 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3274 | // Load the function into r1 as per calling convention. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3275 | Result function = allocator_->Allocate(r1); |
| 3276 | ASSERT(function.is_valid()); |
| 3277 | __ ldr(function.reg(), frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3278 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3279 | // Call the construct call builtin that handles allocation and |
| 3280 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3281 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3282 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
| 3283 | Result result = frame_->CallCodeObject(ic, |
| 3284 | RelocInfo::CONSTRUCT_CALL, |
| 3285 | &num_args, |
| 3286 | &function, |
| 3287 | arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3288 | |
| 3289 | // 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] | 3290 | __ str(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3291 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3295 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3296 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3297 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3298 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3299 | LoadAndSpill(args->at(0)); |
| 3300 | frame_->EmitPop(r0); // r0 contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3301 | // if (object->IsSmi()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3302 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3303 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3304 | // It is a heap object - get map. |
| 3305 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3306 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3307 | // if (!object->IsJSValue()) return the object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3308 | __ cmp(r1, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3309 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3310 | // Load the value. |
| 3311 | __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3312 | leave.Bind(); |
| 3313 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3317 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3318 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3319 | ASSERT(args->length() == 2); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3320 | JumpTarget leave; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3321 | LoadAndSpill(args->at(0)); // Load the object. |
| 3322 | LoadAndSpill(args->at(1)); // Load the value. |
| 3323 | frame_->EmitPop(r0); // r0 contains value |
| 3324 | frame_->EmitPop(r1); // r1 contains object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3325 | // if (object->IsSmi()) return object. |
| 3326 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3327 | leave.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3328 | // It is a heap object - get map. |
| 3329 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3330 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 3331 | // if (!object->IsJSValue()) return object. |
| 3332 | __ cmp(r2, Operand(JS_VALUE_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3333 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3334 | // Store the value. |
| 3335 | __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset)); |
| 3336 | // Update the write barrier. |
| 3337 | __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag)); |
| 3338 | __ RecordWrite(r1, r2, r3); |
| 3339 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3340 | leave.Bind(); |
| 3341 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3345 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3346 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3347 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3348 | LoadAndSpill(args->at(0)); |
| 3349 | frame_->EmitPop(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3350 | __ tst(r0, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3351 | cc_reg_ = eq; |
| 3352 | } |
| 3353 | |
| 3354 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3355 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3356 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3357 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 3358 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3359 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3360 | if (ShouldGenerateLog(args->at(0))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3361 | LoadAndSpill(args->at(1)); |
| 3362 | LoadAndSpill(args->at(2)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3363 | __ CallRuntime(Runtime::kLog, 2); |
| 3364 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 3365 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3366 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3367 | frame_->EmitPush(r0); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3371 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3372 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3373 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3374 | LoadAndSpill(args->at(0)); |
| 3375 | frame_->EmitPop(r0); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 3376 | __ tst(r0, Operand(kSmiTagMask | 0x80000000)); |
| 3377 | cc_reg_ = eq; |
| 3378 | } |
| 3379 | |
| 3380 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3381 | // This should generate code that performs a charCodeAt() call or returns |
| 3382 | // undefined in order to trigger the slow case, Runtime_StringCharCodeAt. |
| 3383 | // 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] | 3384 | void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3385 | VirtualFrame::SpilledScope spilled_scope; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3386 | ASSERT(args->length() == 2); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3387 | __ mov(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3388 | frame_->EmitPush(r0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3392 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3393 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3394 | ASSERT(args->length() == 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3395 | LoadAndSpill(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3396 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3397 | // We need the CC bits to come out as not_equal in the case where the |
| 3398 | // object is a smi. This can't be done with the usual test opcode so |
| 3399 | // we use XOR to get the right CC bits. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3400 | frame_->EmitPop(r0); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3401 | __ and_(r1, r0, Operand(kSmiTagMask)); |
| 3402 | __ eor(r1, r1, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3403 | answer.Branch(ne); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3404 | // It is a heap object - get the map. |
| 3405 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3406 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3407 | // Check if the object is a JS array or not. |
| 3408 | __ cmp(r1, Operand(JS_ARRAY_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3409 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3410 | cc_reg_ = eq; |
| 3411 | } |
| 3412 | |
| 3413 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3414 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3415 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3416 | ASSERT(args->length() == 0); |
| 3417 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3418 | // Seed the result with the formal parameters count, which will be used |
| 3419 | // 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] | 3420 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
| 3421 | |
| 3422 | // Call the shared stub to get to the arguments.length. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3423 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3424 | frame_->CallStub(&stub, 0); |
| 3425 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3429 | void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3430 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3431 | ASSERT(args->length() == 1); |
| 3432 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3433 | // Satisfy contract with ArgumentsAccessStub: |
| 3434 | // 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] | 3435 | LoadAndSpill(args->at(0)); |
| 3436 | frame_->EmitPop(r1); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3437 | __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3438 | |
| 3439 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3440 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3441 | frame_->CallStub(&stub, 0); |
| 3442 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3446 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3447 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3448 | ASSERT(args->length() == 2); |
| 3449 | |
| 3450 | // Load the two objects into registers and perform the comparison. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3451 | LoadAndSpill(args->at(0)); |
| 3452 | LoadAndSpill(args->at(1)); |
| 3453 | frame_->EmitPop(r0); |
| 3454 | frame_->EmitPop(r1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3455 | __ cmp(r0, Operand(r1)); |
| 3456 | cc_reg_ = eq; |
| 3457 | } |
| 3458 | |
| 3459 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3460 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3461 | #ifdef DEBUG |
| 3462 | int original_height = frame_->height(); |
| 3463 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3464 | VirtualFrame::SpilledScope spilled_scope; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3465 | if (CheckForInlineRuntimeCall(node)) { |
| 3466 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3467 | (!has_cc() && frame_->height() == original_height + 1)); |
| 3468 | return; |
| 3469 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3470 | |
| 3471 | ZoneList<Expression*>* args = node->arguments(); |
| 3472 | Comment cmnt(masm_, "[ CallRuntime"); |
| 3473 | Runtime::Function* function = node->function(); |
| 3474 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3475 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3476 | // Prepare stack for calling JS runtime function. |
| 3477 | __ mov(r0, Operand(node->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3478 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3479 | // Push the builtins object found in the current global object. |
| 3480 | __ ldr(r1, GlobalObject()); |
| 3481 | __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3482 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3483 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3484 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3485 | // Push the arguments ("left-to-right"). |
| 3486 | int arg_count = args->length(); |
| 3487 | for (int i = 0; i < arg_count; i++) { |
| 3488 | LoadAndSpill(args->at(i)); |
| 3489 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3490 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3491 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3492 | // Call the JS runtime function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3493 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 3494 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3495 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3496 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3497 | frame_->Drop(); |
| 3498 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3499 | } else { |
| 3500 | // Call the C runtime function. |
| 3501 | frame_->CallRuntime(function, arg_count); |
| 3502 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3503 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3504 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3508 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3509 | #ifdef DEBUG |
| 3510 | int original_height = frame_->height(); |
| 3511 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3512 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3513 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 3514 | |
| 3515 | Token::Value op = node->op(); |
| 3516 | |
| 3517 | if (op == Token::NOT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3518 | LoadConditionAndSpill(node->expression(), |
| 3519 | NOT_INSIDE_TYPEOF, |
| 3520 | false_target(), |
| 3521 | true_target(), |
| 3522 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3523 | cc_reg_ = NegateCondition(cc_reg_); |
| 3524 | |
| 3525 | } else if (op == Token::DELETE) { |
| 3526 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3527 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3528 | if (property != NULL) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3529 | LoadAndSpill(property->obj()); |
| 3530 | LoadAndSpill(property->key()); |
| 3531 | Result arg_count = allocator_->Allocate(r0); |
| 3532 | ASSERT(arg_count.is_valid()); |
| 3533 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3534 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3535 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3536 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3537 | Slot* slot = variable->slot(); |
| 3538 | if (variable->is_global()) { |
| 3539 | LoadGlobal(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3540 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3541 | frame_->EmitPush(r0); |
| 3542 | Result arg_count = allocator_->Allocate(r0); |
| 3543 | ASSERT(arg_count.is_valid()); |
| 3544 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3545 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3546 | |
| 3547 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 3548 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3549 | frame_->EmitPush(cp); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3550 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3551 | frame_->EmitPush(r0); |
| 3552 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3553 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3554 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3555 | __ mov(r0, Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3556 | frame_->EmitPush(r0); |
| 3557 | Result arg_count = allocator_->Allocate(r0); |
| 3558 | ASSERT(arg_count.is_valid()); |
| 3559 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 3560 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3561 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3562 | } else { |
| 3563 | // Default: Result of deleting non-global, not dynamically |
| 3564 | // introduced variables is false. |
| 3565 | __ mov(r0, Operand(Factory::false_value())); |
| 3566 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3567 | |
| 3568 | } else { |
| 3569 | // Default: Result of deleting expressions is true. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3570 | LoadAndSpill(node->expression()); // may have side-effects |
| 3571 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3572 | __ mov(r0, Operand(Factory::true_value())); |
| 3573 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3574 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3575 | |
| 3576 | } else if (op == Token::TYPEOF) { |
| 3577 | // Special case for loading the typeof expression; see comment on |
| 3578 | // LoadTypeofExpression(). |
| 3579 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3580 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 3581 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3582 | |
| 3583 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3584 | LoadAndSpill(node->expression()); |
| 3585 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3586 | switch (op) { |
| 3587 | case Token::NOT: |
| 3588 | case Token::DELETE: |
| 3589 | case Token::TYPEOF: |
| 3590 | UNREACHABLE(); // handled above |
| 3591 | break; |
| 3592 | |
| 3593 | case Token::SUB: { |
| 3594 | UnarySubStub stub; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3595 | frame_->CallStub(&stub, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | case Token::BIT_NOT: { |
| 3600 | // smi check |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3601 | JumpTarget smi_label; |
| 3602 | JumpTarget continue_label; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3603 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3604 | smi_label.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3605 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3606 | frame_->EmitPush(r0); |
| 3607 | Result arg_count = allocator_->Allocate(r0); |
| 3608 | ASSERT(arg_count.is_valid()); |
| 3609 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3610 | frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3611 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3612 | continue_label.Jump(); |
| 3613 | smi_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3614 | __ mvn(r0, Operand(r0)); |
| 3615 | __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3616 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | break; |
| 3618 | } |
| 3619 | |
| 3620 | case Token::VOID: |
| 3621 | // since the stack top is cached in r0, popping and then |
| 3622 | // pushing a value can be done by just writing to r0. |
| 3623 | __ mov(r0, Operand(Factory::undefined_value())); |
| 3624 | break; |
| 3625 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3626 | case Token::ADD: { |
| 3627 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3628 | JumpTarget continue_label; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3629 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3630 | continue_label.Branch(eq); |
| 3631 | frame_->EmitPush(r0); |
| 3632 | Result arg_count = allocator_->Allocate(r0); |
| 3633 | ASSERT(arg_count.is_valid()); |
| 3634 | __ mov(arg_count.reg(), Operand(0)); // not counting receiver |
| 3635 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3636 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3637 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3638 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3639 | default: |
| 3640 | UNREACHABLE(); |
| 3641 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3642 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3643 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3644 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3645 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3649 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3650 | #ifdef DEBUG |
| 3651 | int original_height = frame_->height(); |
| 3652 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3653 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3654 | Comment cmnt(masm_, "[ CountOperation"); |
| 3655 | |
| 3656 | bool is_postfix = node->is_postfix(); |
| 3657 | bool is_increment = node->op() == Token::INC; |
| 3658 | |
| 3659 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 3660 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
| 3661 | |
| 3662 | // Postfix: Make room for the result. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3663 | if (is_postfix) { |
| 3664 | __ mov(r0, Operand(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3665 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3666 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3667 | |
| 3668 | { Reference target(this, node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3669 | if (target.is_illegal()) { |
| 3670 | // Spoof the virtual frame to have the expected height (one higher |
| 3671 | // than on entry). |
| 3672 | if (!is_postfix) { |
| 3673 | __ mov(r0, Operand(Smi::FromInt(0))); |
| 3674 | frame_->EmitPush(r0); |
| 3675 | } |
| 3676 | ASSERT(frame_->height() == original_height + 1); |
| 3677 | return; |
| 3678 | } |
| 3679 | target.GetValueAndSpill(NOT_INSIDE_TYPEOF); |
| 3680 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3681 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3682 | JumpTarget slow; |
| 3683 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3684 | |
| 3685 | // Load the value (1) into register r1. |
| 3686 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3687 | |
| 3688 | // Check for smi operand. |
| 3689 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3690 | slow.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3691 | |
| 3692 | // Postfix: Store the old value as the result. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3693 | if (is_postfix) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3694 | __ str(r0, frame_->ElementAt(target.size())); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3695 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3696 | |
| 3697 | // Perform optimistic increment/decrement. |
| 3698 | if (is_increment) { |
| 3699 | __ add(r0, r0, Operand(r1), SetCC); |
| 3700 | } else { |
| 3701 | __ sub(r0, r0, Operand(r1), SetCC); |
| 3702 | } |
| 3703 | |
| 3704 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3705 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3706 | |
| 3707 | // Revert optimistic increment/decrement. |
| 3708 | if (is_increment) { |
| 3709 | __ sub(r0, r0, Operand(r1)); |
| 3710 | } else { |
| 3711 | __ add(r0, r0, Operand(r1)); |
| 3712 | } |
| 3713 | |
| 3714 | // Slow case: Convert to number. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3715 | slow.Bind(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3716 | { |
| 3717 | // Convert the operand to a number. |
| 3718 | frame_->EmitPush(r0); |
| 3719 | Result arg_count = allocator_->Allocate(r0); |
| 3720 | ASSERT(arg_count.is_valid()); |
| 3721 | __ mov(arg_count.reg(), Operand(0)); |
| 3722 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1); |
| 3723 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3724 | if (is_postfix) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3725 | // Postfix: store to result (on the stack). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3726 | __ str(r0, frame_->ElementAt(target.size())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3729 | // Compute the new value. |
| 3730 | __ mov(r1, Operand(Smi::FromInt(1))); |
| 3731 | frame_->EmitPush(r0); |
| 3732 | frame_->EmitPush(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3733 | if (is_increment) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3734 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3735 | } else { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3736 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | // Store the new value in the target if not const. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3740 | exit.Bind(); |
| 3741 | frame_->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3742 | if (!is_const) target.SetValue(NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | // Postfix: Discard the new value and use the old. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3746 | if (is_postfix) frame_->EmitPop(r0); |
| 3747 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3751 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3752 | #ifdef DEBUG |
| 3753 | int original_height = frame_->height(); |
| 3754 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3755 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3756 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 3757 | Token::Value op = node->op(); |
| 3758 | |
| 3759 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 3760 | // operators must yield the result of one of the two expressions |
| 3761 | // before any ToBoolean() conversions. This means that the value |
| 3762 | // produced by a && or || operator is not necessarily a boolean. |
| 3763 | |
| 3764 | // NOTE: If the left hand side produces a materialized value (not in |
| 3765 | // the CC register), we force the right hand side to do the |
| 3766 | // same. This is necessary because we may have to branch to the exit |
| 3767 | // after evaluating the left hand side (due to the shortcut |
| 3768 | // semantics), but the compiler must (statically) know if the result |
| 3769 | // of compiling the binary operation is materialized or not. |
| 3770 | |
| 3771 | if (op == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3772 | JumpTarget is_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3773 | LoadConditionAndSpill(node->left(), |
| 3774 | NOT_INSIDE_TYPEOF, |
| 3775 | &is_true, |
| 3776 | false_target(), |
| 3777 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3778 | if (has_cc()) { |
| 3779 | Branch(false, false_target()); |
| 3780 | |
| 3781 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3782 | is_true.Bind(); |
| 3783 | LoadConditionAndSpill(node->right(), |
| 3784 | NOT_INSIDE_TYPEOF, |
| 3785 | true_target(), |
| 3786 | false_target(), |
| 3787 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | |
| 3789 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3790 | JumpTarget pop_and_continue; |
| 3791 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3792 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3793 | __ ldr(r0, frame_->Top()); // dup the stack top |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3794 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | // Avoid popping the result if it converts to 'false' using the |
| 3796 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3797 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3798 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3799 | Branch(false, &exit); |
| 3800 | |
| 3801 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3802 | pop_and_continue.Bind(); |
| 3803 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3804 | |
| 3805 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | is_true.Bind(); |
| 3807 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3810 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | } else if (op == Token::OR) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3814 | JumpTarget is_false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3815 | LoadConditionAndSpill(node->left(), |
| 3816 | NOT_INSIDE_TYPEOF, |
| 3817 | true_target(), |
| 3818 | &is_false, |
| 3819 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3820 | if (has_cc()) { |
| 3821 | Branch(true, true_target()); |
| 3822 | |
| 3823 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3824 | is_false.Bind(); |
| 3825 | LoadConditionAndSpill(node->right(), |
| 3826 | NOT_INSIDE_TYPEOF, |
| 3827 | true_target(), |
| 3828 | false_target(), |
| 3829 | false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3830 | |
| 3831 | } else { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3832 | JumpTarget pop_and_continue; |
| 3833 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3834 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3835 | __ ldr(r0, frame_->Top()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3836 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3837 | // Avoid popping the result if it converts to 'true' using the |
| 3838 | // standard ToBoolean() conversion as described in ECMA-262, |
| 3839 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3840 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3841 | Branch(true, &exit); |
| 3842 | |
| 3843 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3844 | pop_and_continue.Bind(); |
| 3845 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3846 | |
| 3847 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3848 | is_false.Bind(); |
| 3849 | LoadAndSpill(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3850 | |
| 3851 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3852 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | } else { |
| 3856 | // Optimize for the case where (at least) one of the expressions |
| 3857 | // is a literal small integer. |
| 3858 | Literal* lliteral = node->left()->AsLiteral(); |
| 3859 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3860 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 3861 | // never return a constant/immutable object. |
| 3862 | bool overwrite_left = |
| 3863 | (node->left()->AsBinaryOperation() != NULL && |
| 3864 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3865 | bool overwrite_right = |
| 3866 | (node->right()->AsBinaryOperation() != NULL && |
| 3867 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3868 | |
| 3869 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3870 | LoadAndSpill(node->left()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3871 | SmiOperation(node->op(), |
| 3872 | rliteral->handle(), |
| 3873 | false, |
| 3874 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3875 | |
| 3876 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3877 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3878 | SmiOperation(node->op(), |
| 3879 | lliteral->handle(), |
| 3880 | true, |
| 3881 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3882 | |
| 3883 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3884 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 3885 | if (overwrite_left) { |
| 3886 | overwrite_mode = OVERWRITE_LEFT; |
| 3887 | } else if (overwrite_right) { |
| 3888 | overwrite_mode = OVERWRITE_RIGHT; |
| 3889 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3890 | LoadAndSpill(node->left()); |
| 3891 | LoadAndSpill(node->right()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3892 | GenericBinaryOperation(node->op(), overwrite_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3893 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3894 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3895 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3896 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 3897 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3901 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3902 | #ifdef DEBUG |
| 3903 | int original_height = frame_->height(); |
| 3904 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3905 | VirtualFrame::SpilledScope spilled_scope; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3906 | __ ldr(r0, frame_->Function()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3907 | frame_->EmitPush(r0); |
| 3908 | ASSERT(frame_->height() == original_height + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3912 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3913 | #ifdef DEBUG |
| 3914 | int original_height = frame_->height(); |
| 3915 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3916 | VirtualFrame::SpilledScope spilled_scope; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3917 | Comment cmnt(masm_, "[ CompareOperation"); |
| 3918 | |
| 3919 | // Get the expressions from the node. |
| 3920 | Expression* left = node->left(); |
| 3921 | Expression* right = node->right(); |
| 3922 | Token::Value op = node->op(); |
| 3923 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3924 | // To make null checks efficient, we check if either left or right is the |
| 3925 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 3926 | // instead of calling the (very) general runtime routine for checking |
| 3927 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3928 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3929 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3930 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3931 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3932 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 3933 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3934 | if (left_is_null || right_is_null) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3935 | LoadAndSpill(left_is_null ? right : left); |
| 3936 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3937 | __ cmp(r0, Operand(Factory::null_value())); |
| 3938 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3939 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 3940 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3941 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3942 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3943 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3944 | __ cmp(r0, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3945 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3946 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3947 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3948 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3949 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3950 | // It can be an undetectable object. |
| 3951 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 3952 | __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset)); |
| 3953 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 3954 | __ cmp(r0, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3957 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3958 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3959 | return; |
| 3960 | } |
| 3961 | } |
| 3962 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3963 | // To make typeof testing for natives implemented in JavaScript really |
| 3964 | // efficient, we generate special code for expressions of the form: |
| 3965 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3966 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 3967 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 3968 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 3969 | (right->AsLiteral() != NULL && |
| 3970 | right->AsLiteral()->handle()->IsString())) { |
| 3971 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 3972 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3973 | // Load the operand, move it to register r1. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3974 | LoadTypeofExpression(operation->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3975 | frame_->EmitPop(r1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | |
| 3977 | if (check->Equals(Heap::number_symbol())) { |
| 3978 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3979 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3980 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3981 | __ cmp(r1, Operand(Factory::heap_number_map())); |
| 3982 | cc_reg_ = eq; |
| 3983 | |
| 3984 | } else if (check->Equals(Heap::string_symbol())) { |
| 3985 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3986 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3987 | |
| 3988 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 3989 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 3990 | // It can be an undetectable string object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 3992 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 3993 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3994 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3995 | |
| 3996 | __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 3997 | __ cmp(r2, Operand(FIRST_NONSTRING_TYPE)); |
| 3998 | cc_reg_ = lt; |
| 3999 | |
| 4000 | } else if (check->Equals(Heap::boolean_symbol())) { |
| 4001 | __ cmp(r1, Operand(Factory::true_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4002 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4003 | __ cmp(r1, Operand(Factory::false_value())); |
| 4004 | cc_reg_ = eq; |
| 4005 | |
| 4006 | } else if (check->Equals(Heap::undefined_symbol())) { |
| 4007 | __ cmp(r1, Operand(Factory::undefined_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4008 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4009 | |
| 4010 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4011 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4013 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4015 | __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset)); |
| 4016 | __ and_(r2, r2, Operand(1 << Map::kIsUndetectable)); |
| 4017 | __ cmp(r2, Operand(1 << Map::kIsUndetectable)); |
| 4018 | |
| 4019 | cc_reg_ = eq; |
| 4020 | |
| 4021 | } else if (check->Equals(Heap::function_symbol())) { |
| 4022 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4023 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4024 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4025 | __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset)); |
| 4026 | __ cmp(r1, Operand(JS_FUNCTION_TYPE)); |
| 4027 | cc_reg_ = eq; |
| 4028 | |
| 4029 | } else if (check->Equals(Heap::object_symbol())) { |
| 4030 | __ tst(r1, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4031 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | |
| 4033 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4034 | __ cmp(r1, Operand(Factory::null_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4035 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4036 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4037 | // It can be an undetectable object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4038 | __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 4039 | __ and_(r1, r1, Operand(1 << Map::kIsUndetectable)); |
| 4040 | __ cmp(r1, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4041 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4042 | |
| 4043 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4044 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4045 | false_target()->Branch(lt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 4047 | cc_reg_ = le; |
| 4048 | |
| 4049 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4050 | // Uncommon case: typeof testing against a string literal that is |
| 4051 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4052 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4053 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4054 | ASSERT(!has_valid_frame() || |
| 4055 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4056 | return; |
| 4057 | } |
| 4058 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4059 | LoadAndSpill(left); |
| 4060 | LoadAndSpill(right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4061 | switch (op) { |
| 4062 | case Token::EQ: |
| 4063 | Comparison(eq, false); |
| 4064 | break; |
| 4065 | |
| 4066 | case Token::LT: |
| 4067 | Comparison(lt); |
| 4068 | break; |
| 4069 | |
| 4070 | case Token::GT: |
| 4071 | Comparison(gt); |
| 4072 | break; |
| 4073 | |
| 4074 | case Token::LTE: |
| 4075 | Comparison(le); |
| 4076 | break; |
| 4077 | |
| 4078 | case Token::GTE: |
| 4079 | Comparison(ge); |
| 4080 | break; |
| 4081 | |
| 4082 | case Token::EQ_STRICT: |
| 4083 | Comparison(eq, true); |
| 4084 | break; |
| 4085 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4086 | case Token::IN: { |
| 4087 | Result arg_count = allocator_->Allocate(r0); |
| 4088 | ASSERT(arg_count.is_valid()); |
| 4089 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4090 | Result result = frame_->InvokeBuiltin(Builtins::IN, |
| 4091 | CALL_JS, |
| 4092 | &arg_count, |
| 4093 | 2); |
| 4094 | frame_->EmitPush(result.reg()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4095 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4096 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4097 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4098 | case Token::INSTANCEOF: { |
| 4099 | Result arg_count = allocator_->Allocate(r0); |
| 4100 | ASSERT(arg_count.is_valid()); |
| 4101 | __ mov(arg_count.reg(), Operand(1)); // not counting receiver |
| 4102 | Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF, |
| 4103 | CALL_JS, |
| 4104 | &arg_count, |
| 4105 | 2); |
| 4106 | __ tst(result.reg(), Operand(result.reg())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4107 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4108 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4109 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | |
| 4111 | default: |
| 4112 | UNREACHABLE(); |
| 4113 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4114 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 4115 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4119 | #ifdef DEBUG |
| 4120 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 4121 | #endif |
| 4122 | |
| 4123 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4124 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4125 | #define __ ACCESS_MASM(masm) |
| 4126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4127 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4128 | Handle<String> Reference::GetName() { |
| 4129 | ASSERT(type_ == NAMED); |
| 4130 | Property* property = expression_->AsProperty(); |
| 4131 | if (property == NULL) { |
| 4132 | // Global variable reference treated as a named property reference. |
| 4133 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 4134 | ASSERT(proxy->AsVariable() != NULL); |
| 4135 | ASSERT(proxy->AsVariable()->is_global()); |
| 4136 | return proxy->name(); |
| 4137 | } else { |
| 4138 | Literal* raw_name = property->key()->AsLiteral(); |
| 4139 | ASSERT(raw_name != NULL); |
| 4140 | return Handle<String>(String::cast(*raw_name->handle())); |
| 4141 | } |
| 4142 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4143 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4144 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4145 | void Reference::GetValueAndSpill(TypeofState typeof_state) { |
| 4146 | ASSERT(cgen_->in_spilled_code()); |
| 4147 | cgen_->set_in_spilled_code(false); |
| 4148 | GetValue(typeof_state); |
| 4149 | cgen_->frame()->SpillAll(); |
| 4150 | cgen_->set_in_spilled_code(true); |
| 4151 | } |
| 4152 | |
| 4153 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4154 | void Reference::GetValue(TypeofState typeof_state) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4155 | ASSERT(!cgen_->in_spilled_code()); |
| 4156 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4157 | ASSERT(!is_illegal()); |
| 4158 | ASSERT(!cgen_->has_cc()); |
| 4159 | MacroAssembler* masm = cgen_->masm(); |
| 4160 | Property* property = expression_->AsProperty(); |
| 4161 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4162 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | switch (type_) { |
| 4166 | case SLOT: { |
| 4167 | Comment cmnt(masm, "[ Load from Slot"); |
| 4168 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4169 | ASSERT(slot != NULL); |
| 4170 | cgen_->LoadFromSlot(slot, typeof_state); |
| 4171 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4174 | case NAMED: { |
| 4175 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4176 | // distinction between expressions in a typeof and not in a typeof. If |
| 4177 | // there is a chance that reference errors can be thrown below, we |
| 4178 | // must distinguish between the two kinds of loads (typeof expression |
| 4179 | // loads must not throw a reference error). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4180 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4181 | Comment cmnt(masm, "[ Load from named Property"); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4182 | Handle<String> name(GetName()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4183 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4184 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 4185 | // Setup the name register. |
| 4186 | Result name_reg = cgen_->allocator()->Allocate(r2); |
| 4187 | ASSERT(name_reg.is_valid()); |
| 4188 | __ mov(name_reg.reg(), Operand(name)); |
| 4189 | ASSERT(var == NULL || var->is_global()); |
| 4190 | RelocInfo::Mode rmode = (var == NULL) |
| 4191 | ? RelocInfo::CODE_TARGET |
| 4192 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4193 | Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0); |
| 4194 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4195 | break; |
| 4196 | } |
| 4197 | |
| 4198 | case KEYED: { |
| 4199 | // TODO(1241834): Make sure that this it is safe to ignore the |
| 4200 | // 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] | 4201 | |
| 4202 | // TODO(181): Implement inlined version of array indexing once |
| 4203 | // loop nesting is properly tracked on ARM. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4204 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4205 | Comment cmnt(masm, "[ Load from keyed Property"); |
| 4206 | ASSERT(property != NULL); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4207 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4208 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4209 | ASSERT(var == NULL || var->is_global()); |
| 4210 | RelocInfo::Mode rmode = (var == NULL) |
| 4211 | ? RelocInfo::CODE_TARGET |
| 4212 | : RelocInfo::CODE_TARGET_CONTEXT; |
| 4213 | Result answer = frame->CallCodeObject(ic, rmode, 0); |
| 4214 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4215 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | default: |
| 4219 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4224 | void Reference::SetValue(InitState init_state) { |
| 4225 | ASSERT(!is_illegal()); |
| 4226 | ASSERT(!cgen_->has_cc()); |
| 4227 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4228 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4229 | Property* property = expression_->AsProperty(); |
| 4230 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4231 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4232 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4233 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4234 | switch (type_) { |
| 4235 | case SLOT: { |
| 4236 | Comment cmnt(masm, "[ Store to Slot"); |
| 4237 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 4238 | ASSERT(slot != NULL); |
| 4239 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4240 | ASSERT(slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4241 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4242 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4243 | frame->EmitPush(cp); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4244 | __ mov(r0, Operand(slot->var()->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4245 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4246 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4247 | if (init_state == CONST_INIT) { |
| 4248 | // Same as the case for a normal store, but ignores attribute |
| 4249 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 4250 | // const properties (introduced via eval("const foo = (some |
| 4251 | // expr);")). Also, uses the current function context instead of |
| 4252 | // the top context. |
| 4253 | // |
| 4254 | // Note that we must declare the foo upon entry of eval(), via a |
| 4255 | // context slot declaration, but we cannot initialize it at the |
| 4256 | // same time, because the const declaration may be at the end of |
| 4257 | // the eval code (sigh...) and the const variable may have been |
| 4258 | // used before (where its value is 'undefined'). Thus, we can only |
| 4259 | // do the initialization when we actually encounter the expression |
| 4260 | // and when the expression operands are defined and valid, and |
| 4261 | // thus we need the split into 2 operations: declaration of the |
| 4262 | // context slot followed by initialization. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4263 | frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4264 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4265 | frame->CallRuntime(Runtime::kStoreContextSlot, 3); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4266 | } |
| 4267 | // Storing a variable must keep the (new) value on the expression |
| 4268 | // stack. This is necessary for compiling assignment expressions. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4269 | frame->EmitPush(r0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4270 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4271 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 4272 | ASSERT(!slot->var()->is_dynamic()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4273 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4274 | JumpTarget exit; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4275 | if (init_state == CONST_INIT) { |
| 4276 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 4277 | // Only the first const initialization must be executed (the slot |
| 4278 | // still contains 'the hole' value). When the assignment is |
| 4279 | // executed, the code is identical to a normal store (see below). |
| 4280 | Comment cmnt(masm, "[ Init const"); |
| 4281 | __ ldr(r2, cgen_->SlotOperand(slot, r2)); |
| 4282 | __ cmp(r2, Operand(Factory::the_hole_value())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4283 | exit.Branch(ne); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4284 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4285 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4286 | // We must execute the store. Storing a variable must keep the |
| 4287 | // (new) value on the stack. This is necessary for compiling |
| 4288 | // assignment expressions. |
| 4289 | // |
| 4290 | // Note: We will reach here even with slot->var()->mode() == |
| 4291 | // Variable::CONST because of const declarations which will |
| 4292 | // initialize consts to 'the hole' value and by doing so, end up |
| 4293 | // calling this code. r2 may be loaded with context; used below in |
| 4294 | // RecordWrite. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4295 | frame->EmitPop(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4296 | __ str(r0, cgen_->SlotOperand(slot, r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4297 | frame->EmitPush(r0); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4298 | if (slot->type() == Slot::CONTEXT) { |
| 4299 | // Skip write barrier if the written value is a smi. |
| 4300 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4301 | exit.Branch(eq); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4302 | // r2 is loaded with context when calling SlotOperand above. |
| 4303 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 4304 | __ mov(r3, Operand(offset)); |
| 4305 | __ RecordWrite(r2, r3, r1); |
| 4306 | } |
| 4307 | // If we definitely did not jump over the assignment, we do not need |
| 4308 | // to bind the exit label. Doing so can defeat peephole |
| 4309 | // optimization. |
| 4310 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4311 | exit.Bind(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4312 | } |
| 4313 | } |
| 4314 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4317 | case NAMED: { |
| 4318 | Comment cmnt(masm, "[ Store to named Property"); |
| 4319 | // Call the appropriate IC code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4320 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4321 | Handle<String> name(GetName()); |
| 4322 | |
| 4323 | Result value = cgen_->allocator()->Allocate(r0); |
| 4324 | ASSERT(value.is_valid()); |
| 4325 | frame->EmitPop(value.reg()); |
| 4326 | |
| 4327 | // Setup the name register. |
| 4328 | Result property_name = cgen_->allocator()->Allocate(r2); |
| 4329 | ASSERT(property_name.is_valid()); |
| 4330 | __ mov(property_name.reg(), Operand(name)); |
| 4331 | Result answer = frame->CallCodeObject(ic, |
| 4332 | RelocInfo::CODE_TARGET, |
| 4333 | &value, |
| 4334 | &property_name, |
| 4335 | 0); |
| 4336 | frame->EmitPush(answer.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4337 | break; |
| 4338 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4339 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4340 | case KEYED: { |
| 4341 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 4342 | Property* property = expression_->AsProperty(); |
| 4343 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4344 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4345 | |
| 4346 | // Call IC code. |
| 4347 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 4348 | // TODO(1222589): Make the IC grab the values from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4349 | Result value = cgen_->allocator()->Allocate(r0); |
| 4350 | ASSERT(value.is_valid()); |
| 4351 | frame->EmitPop(value.reg()); // value |
| 4352 | Result result = |
| 4353 | frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0); |
| 4354 | frame->EmitPush(result.reg()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4355 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4356 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4357 | |
| 4358 | default: |
| 4359 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4364 | static void AllocateHeapNumber( |
| 4365 | MacroAssembler* masm, |
| 4366 | Label* need_gc, // Jump here if young space is full. |
| 4367 | Register result_reg, // The tagged address of the new heap number. |
| 4368 | Register allocation_top_addr_reg, // A scratch register. |
| 4369 | Register scratch2) { // Another scratch register. |
| 4370 | ExternalReference allocation_top = |
| 4371 | ExternalReference::new_space_allocation_top_address(); |
| 4372 | ExternalReference allocation_limit = |
| 4373 | ExternalReference::new_space_allocation_limit_address(); |
| 4374 | |
| 4375 | // allocat := the address of the allocation top variable. |
| 4376 | __ mov(allocation_top_addr_reg, Operand(allocation_top)); |
| 4377 | // result_reg := the old allocation top. |
| 4378 | __ ldr(result_reg, MemOperand(allocation_top_addr_reg)); |
| 4379 | // scratch2 := the address of the allocation limit. |
| 4380 | __ mov(scratch2, Operand(allocation_limit)); |
| 4381 | // scratch2 := the allocation limit. |
| 4382 | __ ldr(scratch2, MemOperand(scratch2)); |
| 4383 | // result_reg := the new allocation top. |
| 4384 | __ add(result_reg, result_reg, Operand(HeapNumber::kSize)); |
| 4385 | // Compare new new allocation top and limit. |
| 4386 | __ cmp(result_reg, Operand(scratch2)); |
| 4387 | // Branch if out of space in young generation. |
| 4388 | __ b(hi, need_gc); |
| 4389 | // Store new allocation top. |
| 4390 | __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top |
| 4391 | // Tag and adjust back to start of new object. |
| 4392 | __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag)); |
| 4393 | // Get heap number map into scratch2. |
| 4394 | __ mov(scratch2, Operand(Factory::heap_number_map())); |
| 4395 | // Store heap number map in new object. |
| 4396 | __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset)); |
| 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | // We fall into this code if the operands were Smis, but the result was |
| 4401 | // not (eg. overflow). We branch into this code (to the not_smi label) if |
| 4402 | // the operands were not both Smi. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4403 | static void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 4404 | Label* not_smi, |
| 4405 | const Builtins::JavaScript& builtin, |
| 4406 | Token::Value operation, |
| 4407 | int swi_number, |
| 4408 | OverwriteMode mode) { |
| 4409 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4410 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4411 | __ push(r1); |
| 4412 | __ push(r0); |
| 4413 | __ mov(r0, Operand(1)); // Set number of arguments. |
| 4414 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4415 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4416 | __ bind(not_smi); |
| 4417 | __ tst(r0, Operand(kSmiTagMask)); |
| 4418 | __ b(eq, &slow); // We can't handle a Smi-double combination yet. |
| 4419 | __ tst(r1, Operand(kSmiTagMask)); |
| 4420 | __ b(eq, &slow); // We can't handle a Smi-double combination yet. |
| 4421 | // Get map of r0 into r2. |
| 4422 | __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 4423 | // Get type of r0 into r3. |
| 4424 | __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 4425 | __ cmp(r3, Operand(HEAP_NUMBER_TYPE)); |
| 4426 | __ b(ne, &slow); |
| 4427 | // Get type of r1 into r3. |
| 4428 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 4429 | // Check they are both the same map (heap number map). |
| 4430 | __ cmp(r2, r3); |
| 4431 | __ b(ne, &slow); |
| 4432 | // Both are doubles. |
| 4433 | // Calling convention says that second double is in r2 and r3. |
| 4434 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 4435 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize)); |
| 4436 | |
| 4437 | if (mode == NO_OVERWRITE) { |
| 4438 | // Get address of new heap number into r5. |
| 4439 | AllocateHeapNumber(masm, &slow, r5, r6, r7); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4440 | __ push(lr); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4441 | __ push(r5); |
| 4442 | } else if (mode == OVERWRITE_LEFT) { |
| 4443 | __ push(lr); |
| 4444 | __ push(r1); |
| 4445 | } else { |
| 4446 | ASSERT(mode == OVERWRITE_RIGHT); |
| 4447 | __ push(lr); |
| 4448 | __ push(r0); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4449 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 4450 | // Calling convention says that first double is in r0 and r1. |
| 4451 | __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 4452 | __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize)); |
| 4453 | // Call C routine that may not cause GC or other trouble. |
| 4454 | __ mov(r5, Operand(ExternalReference::double_fp_operation(operation))); |
| 4455 | #if !defined(__arm__) |
| 4456 | // Notify the simulator that we are calling an add routine in C. |
| 4457 | __ swi(swi_number); |
| 4458 | #else |
| 4459 | // Actually call the add routine written in C. |
| 4460 | __ Call(r5); |
| 4461 | #endif |
| 4462 | // Store answer in the overwritable heap number. |
| 4463 | __ pop(r4); |
| 4464 | #if !defined(__ARM_EABI__) && defined(__arm__) |
| 4465 | // Double returned in fp coprocessor register 0 and 1, encoded as register |
| 4466 | // cr8. Offsets must be divisible by 4 for coprocessor so we need to |
| 4467 | // substract the tag from r4. |
| 4468 | __ sub(r5, r4, Operand(kHeapObjectTag)); |
| 4469 | __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset)); |
| 4470 | #else |
| 4471 | // Double returned in fp coprocessor register 0 and 1. |
| 4472 | __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset)); |
| 4473 | __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize)); |
| 4474 | #endif |
| 4475 | __ mov(r0, Operand(r4)); |
| 4476 | // And we are done. |
| 4477 | __ pop(pc); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | |
| 4481 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
| 4482 | // r1 : x |
| 4483 | // r0 : y |
| 4484 | // result : r0 |
| 4485 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4486 | // All ops need to know whether we are dealing with two Smis. Set up r2 to |
| 4487 | // tell us that. |
| 4488 | __ orr(r2, r1, Operand(r0)); // r2 = x | y; |
| 4489 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4490 | switch (op_) { |
| 4491 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4492 | Label not_smi; |
| 4493 | // Fast path. |
| 4494 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4495 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4496 | __ b(ne, ¬_smi); |
| 4497 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 4498 | // Return if no overflow. |
| 4499 | __ Ret(vc); |
| 4500 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 4501 | |
| 4502 | HandleBinaryOpSlowCases(masm, |
| 4503 | ¬_smi, |
| 4504 | Builtins::ADD, |
| 4505 | Token::ADD, |
| 4506 | assembler::arm::simulator_fp_add, |
| 4507 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4508 | break; |
| 4509 | } |
| 4510 | |
| 4511 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4512 | Label not_smi; |
| 4513 | // Fast path. |
| 4514 | ASSERT(kSmiTag == 0); // Adjust code below. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4515 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4516 | __ b(ne, ¬_smi); |
| 4517 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 4518 | // Return if no overflow. |
| 4519 | __ Ret(vc); |
| 4520 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 4521 | |
| 4522 | HandleBinaryOpSlowCases(masm, |
| 4523 | ¬_smi, |
| 4524 | Builtins::SUB, |
| 4525 | Token::SUB, |
| 4526 | assembler::arm::simulator_fp_sub, |
| 4527 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4528 | break; |
| 4529 | } |
| 4530 | |
| 4531 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4532 | Label not_smi, slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4533 | ASSERT(kSmiTag == 0); // adjust code below |
| 4534 | __ tst(r2, Operand(kSmiTagMask)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4535 | __ b(ne, ¬_smi); |
| 4536 | // Remove tag from one operand (but keep sign), so that result is Smi. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4537 | __ mov(ip, Operand(r0, ASR, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4538 | // Do multiplication |
| 4539 | __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1. |
| 4540 | // Go slow on overflows (overflow bit is not set). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4541 | __ mov(ip, Operand(r3, ASR, 31)); |
| 4542 | __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical |
| 4543 | __ b(ne, &slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4544 | // Go slow on zero result to handle -0. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4545 | __ tst(r3, Operand(r3)); |
| 4546 | __ mov(r0, Operand(r3), LeaveCC, ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4547 | __ Ret(ne); |
| 4548 | // Slow case. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4549 | __ bind(&slow); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4550 | |
| 4551 | HandleBinaryOpSlowCases(masm, |
| 4552 | ¬_smi, |
| 4553 | Builtins::MUL, |
| 4554 | Token::MUL, |
| 4555 | assembler::arm::simulator_fp_mul, |
| 4556 | mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4557 | break; |
| 4558 | } |
| 4559 | |
| 4560 | case Token::BIT_OR: |
| 4561 | case Token::BIT_AND: |
| 4562 | case Token::BIT_XOR: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4563 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4564 | ASSERT(kSmiTag == 0); // adjust code below |
| 4565 | __ tst(r2, Operand(kSmiTagMask)); |
| 4566 | __ b(ne, &slow); |
| 4567 | switch (op_) { |
| 4568 | case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break; |
| 4569 | case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break; |
| 4570 | case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break; |
| 4571 | default: UNREACHABLE(); |
| 4572 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4573 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4574 | __ bind(&slow); |
| 4575 | __ push(r1); // restore stack |
| 4576 | __ push(r0); |
| 4577 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4578 | switch (op_) { |
| 4579 | case Token::BIT_OR: |
| 4580 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 4581 | break; |
| 4582 | case Token::BIT_AND: |
| 4583 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 4584 | break; |
| 4585 | case Token::BIT_XOR: |
| 4586 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 4587 | break; |
| 4588 | default: |
| 4589 | UNREACHABLE(); |
| 4590 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4591 | break; |
| 4592 | } |
| 4593 | |
| 4594 | case Token::SHL: |
| 4595 | case Token::SHR: |
| 4596 | case Token::SAR: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4597 | Label slow; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4598 | ASSERT(kSmiTag == 0); // adjust code below |
| 4599 | __ tst(r2, Operand(kSmiTagMask)); |
| 4600 | __ b(ne, &slow); |
| 4601 | // remove tags from operands (but keep sign) |
| 4602 | __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x |
| 4603 | __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y |
| 4604 | // use only the 5 least significant bits of the shift count |
| 4605 | __ and_(r2, r2, Operand(0x1f)); |
| 4606 | // perform operation |
| 4607 | switch (op_) { |
| 4608 | case Token::SAR: |
| 4609 | __ mov(r3, Operand(r3, ASR, r2)); |
| 4610 | // no checks of result necessary |
| 4611 | break; |
| 4612 | |
| 4613 | case Token::SHR: |
| 4614 | __ mov(r3, Operand(r3, LSR, r2)); |
| 4615 | // check that the *unsigned* result fits in a smi |
| 4616 | // neither of the two high-order bits can be set: |
| 4617 | // - 0x80000000: high bit would be lost when smi tagging |
| 4618 | // - 0x40000000: this number would convert to negative when |
| 4619 | // smi tagging these two cases can only happen with shifts |
| 4620 | // by 0 or 1 when handed a valid smi |
| 4621 | __ and_(r2, r3, Operand(0xc0000000), SetCC); |
| 4622 | __ b(ne, &slow); |
| 4623 | break; |
| 4624 | |
| 4625 | case Token::SHL: |
| 4626 | __ mov(r3, Operand(r3, LSL, r2)); |
| 4627 | // check that the *signed* result fits in a smi |
| 4628 | __ add(r2, r3, Operand(0x40000000), SetCC); |
| 4629 | __ b(mi, &slow); |
| 4630 | break; |
| 4631 | |
| 4632 | default: UNREACHABLE(); |
| 4633 | } |
| 4634 | // tag result and store it in r0 |
| 4635 | ASSERT(kSmiTag == 0); // adjust code below |
| 4636 | __ mov(r0, Operand(r3, LSL, kSmiTagSize)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4637 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4638 | // slow case |
| 4639 | __ bind(&slow); |
| 4640 | __ push(r1); // restore stack |
| 4641 | __ push(r0); |
| 4642 | __ mov(r0, Operand(1)); // 1 argument (not counting receiver). |
| 4643 | switch (op_) { |
| 4644 | case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break; |
| 4645 | case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break; |
| 4646 | case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break; |
| 4647 | default: UNREACHABLE(); |
| 4648 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4649 | break; |
| 4650 | } |
| 4651 | |
| 4652 | default: UNREACHABLE(); |
| 4653 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4654 | // This code should be unreachable. |
| 4655 | __ stop("Unreachable"); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | |
| 4659 | void StackCheckStub::Generate(MacroAssembler* masm) { |
| 4660 | Label within_limit; |
| 4661 | __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit())); |
| 4662 | __ ldr(ip, MemOperand(ip)); |
| 4663 | __ cmp(sp, Operand(ip)); |
| 4664 | __ b(hs, &within_limit); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 4665 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 4666 | // argument, so give it a Smi. |
| 4667 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4668 | __ push(r0); |
| 4669 | __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1); |
| 4670 | __ bind(&within_limit); |
| 4671 | |
| 4672 | __ StubReturn(1); |
| 4673 | } |
| 4674 | |
| 4675 | |
| 4676 | void UnarySubStub::Generate(MacroAssembler* masm) { |
| 4677 | Label undo; |
| 4678 | Label slow; |
| 4679 | Label done; |
| 4680 | |
| 4681 | // Enter runtime system if the value is not a smi. |
| 4682 | __ tst(r0, Operand(kSmiTagMask)); |
| 4683 | __ b(ne, &slow); |
| 4684 | |
| 4685 | // Enter runtime system if the value of the expression is zero |
| 4686 | // to make sure that we switch between 0 and -0. |
| 4687 | __ cmp(r0, Operand(0)); |
| 4688 | __ b(eq, &slow); |
| 4689 | |
| 4690 | // The value of the expression is a smi that is not zero. Try |
| 4691 | // optimistic subtraction '0 - value'. |
| 4692 | __ rsb(r1, r0, Operand(0), SetCC); |
| 4693 | __ b(vs, &slow); |
| 4694 | |
| 4695 | // If result is a smi we are done. |
| 4696 | __ tst(r1, Operand(kSmiTagMask)); |
| 4697 | __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result |
| 4698 | __ b(eq, &done); |
| 4699 | |
| 4700 | // Enter runtime system. |
| 4701 | __ bind(&slow); |
| 4702 | __ push(r0); |
| 4703 | __ mov(r0, Operand(0)); // set number of arguments |
| 4704 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 4705 | |
| 4706 | __ bind(&done); |
| 4707 | __ StubReturn(1); |
| 4708 | } |
| 4709 | |
| 4710 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4711 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
| 4712 | // r0 holds exception |
| 4713 | ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code |
| 4714 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4715 | __ ldr(sp, MemOperand(r3)); |
| 4716 | __ pop(r2); // pop next in chain |
| 4717 | __ str(r2, MemOperand(r3)); |
| 4718 | // restore parameter- and frame-pointer and pop state. |
| 4719 | __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit()); |
| 4720 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4721 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4722 | __ cmp(fp, Operand(0)); |
| 4723 | // Set cp to NULL if fp is NULL. |
| 4724 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4725 | // Restore cp otherwise. |
| 4726 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4727 | #ifdef DEBUG |
| 4728 | if (FLAG_debug_code) { |
| 4729 | __ mov(lr, Operand(pc)); |
| 4730 | } |
| 4731 | #endif |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4732 | __ pop(pc); |
| 4733 | } |
| 4734 | |
| 4735 | |
| 4736 | void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) { |
| 4737 | // Fetch top stack handler. |
| 4738 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 4739 | __ ldr(r3, MemOperand(r3)); |
| 4740 | |
| 4741 | // Unwind the handlers until the ENTRY handler is found. |
| 4742 | Label loop, done; |
| 4743 | __ bind(&loop); |
| 4744 | // Load the type of the current stack handler. |
| 4745 | const int kStateOffset = StackHandlerConstants::kAddressDisplacement + |
| 4746 | StackHandlerConstants::kStateOffset; |
| 4747 | __ ldr(r2, MemOperand(r3, kStateOffset)); |
| 4748 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 4749 | __ b(eq, &done); |
| 4750 | // Fetch the next handler in the list. |
| 4751 | const int kNextOffset = StackHandlerConstants::kAddressDisplacement + |
| 4752 | StackHandlerConstants::kNextOffset; |
| 4753 | __ ldr(r3, MemOperand(r3, kNextOffset)); |
| 4754 | __ jmp(&loop); |
| 4755 | __ bind(&done); |
| 4756 | |
| 4757 | // Set the top handler address to next handler past the current ENTRY handler. |
| 4758 | __ ldr(r0, MemOperand(r3, kNextOffset)); |
| 4759 | __ mov(r2, Operand(ExternalReference(Top::k_handler_address))); |
| 4760 | __ str(r0, MemOperand(r2)); |
| 4761 | |
| 4762 | // Set external caught exception to false. |
| 4763 | __ mov(r0, Operand(false)); |
| 4764 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 4765 | __ mov(r2, Operand(external_caught)); |
| 4766 | __ str(r0, MemOperand(r2)); |
| 4767 | |
| 4768 | // Set pending exception and r0 to out of memory exception. |
| 4769 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4770 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4771 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 4772 | __ str(r0, MemOperand(r2)); |
| 4773 | |
| 4774 | // Restore the stack to the address of the ENTRY handler |
| 4775 | __ mov(sp, Operand(r3)); |
| 4776 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4777 | // Stack layout at this point. See also PushTryHandler |
| 4778 | // r3, sp -> next handler |
| 4779 | // state (ENTRY) |
| 4780 | // pp |
| 4781 | // fp |
| 4782 | // lr |
| 4783 | |
| 4784 | // Discard ENTRY state (r2 is not used), and restore parameter- |
| 4785 | // and frame-pointer and pop state. |
| 4786 | __ 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] | 4787 | // Before returning we restore the context from the frame pointer if not NULL. |
| 4788 | // The frame pointer is NULL in the exception handler of a JS entry frame. |
| 4789 | __ cmp(fp, Operand(0)); |
| 4790 | // Set cp to NULL if fp is NULL. |
| 4791 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 4792 | // Restore cp otherwise. |
| 4793 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4794 | #ifdef DEBUG |
| 4795 | if (FLAG_debug_code) { |
| 4796 | __ mov(lr, Operand(pc)); |
| 4797 | } |
| 4798 | #endif |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4799 | __ pop(pc); |
| 4800 | } |
| 4801 | |
| 4802 | |
| 4803 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 4804 | Label* throw_normal_exception, |
| 4805 | Label* throw_out_of_memory_exception, |
| 4806 | StackFrame::Type frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4807 | bool do_gc, |
| 4808 | bool always_allocate) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4809 | // r0: result parameter for PerformGC, if any |
| 4810 | // r4: number of arguments including receiver (C callee-saved) |
| 4811 | // r5: pointer to builtin function (C callee-saved) |
| 4812 | // r6: pointer to the first argument (C callee-saved) |
| 4813 | |
| 4814 | if (do_gc) { |
| 4815 | // Passing r0. |
| 4816 | __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY); |
| 4817 | } |
| 4818 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4819 | ExternalReference scope_depth = |
| 4820 | ExternalReference::heap_always_allocate_scope_depth(); |
| 4821 | if (always_allocate) { |
| 4822 | __ mov(r0, Operand(scope_depth)); |
| 4823 | __ ldr(r1, MemOperand(r0)); |
| 4824 | __ add(r1, r1, Operand(1)); |
| 4825 | __ str(r1, MemOperand(r0)); |
| 4826 | } |
| 4827 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4828 | // Call C built-in. |
| 4829 | // r0 = argc, r1 = argv |
| 4830 | __ mov(r0, Operand(r4)); |
| 4831 | __ mov(r1, Operand(r6)); |
| 4832 | |
| 4833 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 4834 | // frames, we need to know where the return address is. Right now, |
| 4835 | // we push it on the stack to be able to find it again, but we never |
| 4836 | // restore from it in case of changes, which makes it impossible to |
| 4837 | // support moving the C entry code stub. This should be fixed, but currently |
| 4838 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 4839 | // sequence that it is not moving ever. |
| 4840 | __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4 |
| 4841 | __ push(lr); |
| 4842 | #if !defined(__arm__) |
| 4843 | // Notify the simulator of the transition to C code. |
| 4844 | __ swi(assembler::arm::call_rt_r5); |
| 4845 | #else /* !defined(__arm__) */ |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4846 | __ Jump(r5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4847 | #endif /* !defined(__arm__) */ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4848 | |
| 4849 | if (always_allocate) { |
| 4850 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 4851 | // though (contain the result). |
| 4852 | __ mov(r2, Operand(scope_depth)); |
| 4853 | __ ldr(r3, MemOperand(r2)); |
| 4854 | __ sub(r3, r3, Operand(1)); |
| 4855 | __ str(r3, MemOperand(r2)); |
| 4856 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4857 | |
| 4858 | // check for failure result |
| 4859 | Label failure_returned; |
| 4860 | ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
| 4861 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 4862 | __ add(r2, r0, Operand(1)); |
| 4863 | __ tst(r2, Operand(kFailureTagMask)); |
| 4864 | __ b(eq, &failure_returned); |
| 4865 | |
| 4866 | // Exit C frame and return. |
| 4867 | // r0:r1: result |
| 4868 | // sp: stack pointer |
| 4869 | // fp: frame pointer |
| 4870 | // pp: caller's parameter pointer pp (restored as C callee-saved) |
| 4871 | __ LeaveExitFrame(frame_type); |
| 4872 | |
| 4873 | // check if we should retry or throw exception |
| 4874 | Label retry; |
| 4875 | __ bind(&failure_returned); |
| 4876 | ASSERT(Failure::RETRY_AFTER_GC == 0); |
| 4877 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 4878 | __ b(eq, &retry); |
| 4879 | |
| 4880 | Label continue_exception; |
| 4881 | // If the returned failure is EXCEPTION then promote Top::pending_exception(). |
| 4882 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
| 4883 | __ b(ne, &continue_exception); |
| 4884 | |
| 4885 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4886 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4887 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4888 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4889 | __ ldr(r0, MemOperand(ip)); |
| 4890 | __ str(r3, MemOperand(ip)); |
| 4891 | |
| 4892 | __ bind(&continue_exception); |
| 4893 | // Special handling of out of memory exception. |
| 4894 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 4895 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 4896 | __ b(eq, throw_out_of_memory_exception); |
| 4897 | |
| 4898 | // Handle normal exception. |
| 4899 | __ jmp(throw_normal_exception); |
| 4900 | |
| 4901 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 4902 | } |
| 4903 | |
| 4904 | |
| 4905 | void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) { |
| 4906 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 4907 | // r0: number of arguments including receiver |
| 4908 | // r1: pointer to builtin function |
| 4909 | // fp: frame pointer (restored after C call) |
| 4910 | // sp: stack pointer (restored as callee's pp after C call) |
| 4911 | // cp: current context (C callee-saved) |
| 4912 | // pp: caller's parameter pointer pp (C callee-saved) |
| 4913 | |
| 4914 | // NOTE: Invocations of builtins may return failure objects |
| 4915 | // instead of a proper result. The builtin entry handles |
| 4916 | // this by performing a garbage collection and retrying the |
| 4917 | // builtin once. |
| 4918 | |
| 4919 | StackFrame::Type frame_type = is_debug_break |
| 4920 | ? StackFrame::EXIT_DEBUG |
| 4921 | : StackFrame::EXIT; |
| 4922 | |
| 4923 | // Enter the exit frame that transitions from JavaScript to C++. |
| 4924 | __ EnterExitFrame(frame_type); |
| 4925 | |
| 4926 | // r4: number of arguments (C callee-saved) |
| 4927 | // r5: pointer to builtin function (C callee-saved) |
| 4928 | // r6: pointer to first argument (C callee-saved) |
| 4929 | |
| 4930 | Label throw_out_of_memory_exception; |
| 4931 | Label throw_normal_exception; |
| 4932 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4933 | // Call into the runtime system. Collect garbage before the call if |
| 4934 | // running with --gc-greedy set. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4935 | if (FLAG_gc_greedy) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4936 | Failure* failure = Failure::RetryAfterGC(0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4937 | __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure))); |
| 4938 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4939 | GenerateCore(masm, &throw_normal_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4940 | &throw_out_of_memory_exception, |
| 4941 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4942 | FLAG_gc_greedy, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4943 | false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4944 | |
| 4945 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4946 | GenerateCore(masm, |
| 4947 | &throw_normal_exception, |
| 4948 | &throw_out_of_memory_exception, |
| 4949 | frame_type, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 4950 | true, |
| 4951 | false); |
| 4952 | |
| 4953 | // Do full GC and retry runtime call one final time. |
| 4954 | Failure* failure = Failure::InternalError(); |
| 4955 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 4956 | GenerateCore(masm, |
| 4957 | &throw_normal_exception, |
| 4958 | &throw_out_of_memory_exception, |
| 4959 | frame_type, |
| 4960 | true, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4961 | true); |
| 4962 | |
| 4963 | __ bind(&throw_out_of_memory_exception); |
| 4964 | GenerateThrowOutOfMemory(masm); |
| 4965 | // control flow for generated will not return. |
| 4966 | |
| 4967 | __ bind(&throw_normal_exception); |
| 4968 | GenerateThrowTOS(masm); |
| 4969 | } |
| 4970 | |
| 4971 | |
| 4972 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 4973 | // r0: code entry |
| 4974 | // r1: function |
| 4975 | // r2: receiver |
| 4976 | // r3: argc |
| 4977 | // [sp+0]: argv |
| 4978 | |
| 4979 | Label invoke, exit; |
| 4980 | |
| 4981 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 4982 | // No need to save register-passed args |
| 4983 | // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr |
| 4984 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 4985 | |
| 4986 | // Get address of argv, see stm above. |
| 4987 | // r0: code entry |
| 4988 | // r1: function |
| 4989 | // r2: receiver |
| 4990 | // r3: argc |
| 4991 | __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize)); |
| 4992 | __ ldr(r4, MemOperand(r4)); // argv |
| 4993 | |
| 4994 | // Push a frame with special values setup to mark it as an entry frame. |
| 4995 | // r0: code entry |
| 4996 | // r1: function |
| 4997 | // r2: receiver |
| 4998 | // r3: argc |
| 4999 | // r4: argv |
| 5000 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 5001 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
| 5002 | __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL)); |
| 5003 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 5004 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5005 | __ ldr(r5, MemOperand(r5)); |
| 5006 | __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit()); |
| 5007 | |
| 5008 | // Setup frame pointer for the frame to be pushed. |
| 5009 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5010 | |
| 5011 | // Call a faked try-block that does the invoke. |
| 5012 | __ bl(&invoke); |
| 5013 | |
| 5014 | // Caught exception: Store result (exception) in the pending |
| 5015 | // exception field in the JSEnv and return a failure sentinel. |
| 5016 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 5017 | // 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] | 5018 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5019 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5020 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5021 | __ b(&exit); |
| 5022 | |
| 5023 | // Invoke: Link this frame into the handler chain. |
| 5024 | __ bind(&invoke); |
| 5025 | // Must preserve r0-r4, r5-r7 are available. |
| 5026 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
| 5027 | // If an exception not caught by another handler occurs, this handler returns |
| 5028 | // control to the code after the bl(&invoke) above, which restores all |
| 5029 | // kCalleeSaved registers (including cp, pp and fp) to their saved values |
| 5030 | // before returning a failure to C. |
| 5031 | |
| 5032 | // Clear any pending exceptions. |
| 5033 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 5034 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5035 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5036 | __ str(r5, MemOperand(ip)); |
| 5037 | |
| 5038 | // Invoke the function by calling through JS entry trampoline builtin. |
| 5039 | // Notice that we cannot store a reference to the trampoline code directly in |
| 5040 | // this stub, because runtime stubs are not traversed when doing GC. |
| 5041 | |
| 5042 | // Expected registers by Builtins::JSEntryTrampoline |
| 5043 | // r0: code entry |
| 5044 | // r1: function |
| 5045 | // r2: receiver |
| 5046 | // r3: argc |
| 5047 | // r4: argv |
| 5048 | if (is_construct) { |
| 5049 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 5050 | __ mov(ip, Operand(construct_entry)); |
| 5051 | } else { |
| 5052 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 5053 | __ mov(ip, Operand(entry)); |
| 5054 | } |
| 5055 | __ ldr(ip, MemOperand(ip)); // deref address |
| 5056 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5057 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 5058 | // macro for the add instruction because we don't want the coverage tool |
| 5059 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5060 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5061 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5062 | |
| 5063 | // Unlink this frame from the handler chain. When reading the |
| 5064 | // address of the next handler, there is no need to use the address |
| 5065 | // displacement since the current stack pointer (sp) points directly |
| 5066 | // to the stack handler. |
| 5067 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 5068 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 5069 | __ str(r3, MemOperand(ip)); |
| 5070 | // No need to restore registers |
| 5071 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 5072 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5073 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5074 | __ bind(&exit); // r0 holds result |
| 5075 | // Restore the top frame descriptors from the stack. |
| 5076 | __ pop(r3); |
| 5077 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 5078 | __ str(r3, MemOperand(ip)); |
| 5079 | |
| 5080 | // Reset the stack to the callee saved registers. |
| 5081 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 5082 | |
| 5083 | // Restore callee-saved registers and return. |
| 5084 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5085 | if (FLAG_debug_code) { |
| 5086 | __ mov(lr, Operand(pc)); |
| 5087 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5088 | #endif |
| 5089 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 5090 | } |
| 5091 | |
| 5092 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5093 | void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5094 | // Check if the calling frame is an arguments adaptor frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5095 | Label adaptor; |
| 5096 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5097 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5098 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5099 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5100 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5101 | // Nothing to do: The formal number of parameters has already been |
| 5102 | // passed in register r0 by calling function. Just return it. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5103 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5104 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5105 | // Arguments adaptor case: Read the arguments length from the |
| 5106 | // adaptor frame and return it. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5107 | __ bind(&adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5108 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5109 | __ Jump(lr); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5113 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 5114 | // The displacement is the offset of the last parameter (if any) |
| 5115 | // relative to the frame pointer. |
| 5116 | static const int kDisplacement = |
| 5117 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5118 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5119 | // Check that the key is a smi. |
| 5120 | Label slow; |
| 5121 | __ tst(r1, Operand(kSmiTagMask)); |
| 5122 | __ b(ne, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5123 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5124 | // Check if the calling frame is an arguments adaptor frame. |
| 5125 | Label adaptor; |
| 5126 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5127 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5128 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5129 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5130 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5131 | // Check index against formal parameters count limit passed in |
| 5132 | // through register eax. Use unsigned comparison to get negative |
| 5133 | // check for free. |
| 5134 | __ cmp(r1, r0); |
| 5135 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5136 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5137 | // Read the argument from the stack and return it. |
| 5138 | __ sub(r3, r0, r1); |
| 5139 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5140 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5141 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Arguments adaptor case: Check index against actual arguments |
| 5144 | // limit found in the arguments adaptor frame. Use unsigned |
| 5145 | // comparison to get negative check for free. |
| 5146 | __ bind(&adaptor); |
| 5147 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5148 | __ cmp(r1, r0); |
| 5149 | __ b(cs, &slow); |
| 5150 | |
| 5151 | // Read the argument from the adaptor frame and return it. |
| 5152 | __ sub(r3, r0, r1); |
| 5153 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5154 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 5155 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5156 | |
| 5157 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 5158 | // by calling the runtime system. |
| 5159 | __ bind(&slow); |
| 5160 | __ push(r1); |
| 5161 | __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1); |
| 5162 | } |
| 5163 | |
| 5164 | |
| 5165 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
| 5166 | // Check if the calling frame is an arguments adaptor frame. |
| 5167 | Label runtime; |
| 5168 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5169 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 5170 | __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL)); |
| 5171 | __ b(ne, &runtime); |
| 5172 | |
| 5173 | // Patch the arguments.length and the parameters pointer. |
| 5174 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 5175 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 5176 | __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5177 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 5178 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 5179 | |
| 5180 | // Do the runtime call to allocate the arguments object. |
| 5181 | __ bind(&runtime); |
| 5182 | __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | |
| 5186 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 5187 | Label slow; |
| 5188 | // Get the function to call from the stack. |
| 5189 | // function, receiver [, arguments] |
| 5190 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 5191 | |
| 5192 | // Check that the function is really a JavaScript function. |
| 5193 | // r1: pushed function (to be verified) |
| 5194 | __ tst(r1, Operand(kSmiTagMask)); |
| 5195 | __ b(eq, &slow); |
| 5196 | // Get the map of the function object. |
| 5197 | __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 5198 | __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset)); |
| 5199 | __ cmp(r2, Operand(JS_FUNCTION_TYPE)); |
| 5200 | __ b(ne, &slow); |
| 5201 | |
| 5202 | // Fast-case: Invoke the function now. |
| 5203 | // r1: pushed function |
| 5204 | ParameterCount actual(argc_); |
| 5205 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 5206 | |
| 5207 | // Slow-case: Non-function called. |
| 5208 | __ bind(&slow); |
| 5209 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5210 | __ mov(r2, Operand(0)); |
| 5211 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 5212 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 5213 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | |
| 5217 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5219 | } } // namespace v8::internal |