sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_ARM) |
| 31 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
| 33 | #include "codegen-inl.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 34 | #include "compiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "debug.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 36 | #include "ic-inl.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 37 | #include "jsregexp.h" |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 38 | #include "jump-target-light-inl.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | #include "parser.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 40 | #include "regexp-macro-assembler.h" |
| 41 | #include "regexp-stack.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 42 | #include "register-allocator-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | #include "runtime.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 44 | #include "scopes.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 45 | #include "virtual-frame-inl.h" |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 46 | #include "virtual-frame-arm-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 48 | namespace v8 { |
| 49 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 51 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 52 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 53 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 54 | Condition cc, |
| 55 | bool never_nan_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 56 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 57 | Register lhs, |
| 58 | Register rhs, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 59 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 60 | Label* slow, |
| 61 | bool strict); |
| 62 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 63 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm, |
| 64 | Register lhs, |
| 65 | Register rhs); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 66 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 67 | Register source, |
| 68 | Register destination, |
| 69 | int known_int); |
| 70 | static bool IsEasyToMultiplyBy(int x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 71 | |
| 72 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 73 | #define __ ACCESS_MASM(masm_) |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 74 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 75 | // ------------------------------------------------------------------------- |
| 76 | // Platform-specific DeferredCode functions. |
| 77 | |
| 78 | void DeferredCode::SaveRegisters() { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 79 | // On ARM you either have a completely spilled frame or you |
| 80 | // handle it yourself, but at the moment there's no automation |
| 81 | // of registers and deferred code. |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | void DeferredCode::RestoreRegisters() { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | // ------------------------------------------------------------------------- |
| 90 | // Platform-specific RuntimeCallHelper functions. |
| 91 | |
| 92 | void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const { |
| 93 | frame_state_->frame()->AssertIsSpilled(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const { |
| 102 | masm->EnterInternalFrame(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { |
| 107 | masm->LeaveInternalFrame(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 108 | } |
| 109 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 110 | |
| 111 | // ------------------------------------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 112 | // CodeGenState implementation. |
| 113 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 114 | CodeGenState::CodeGenState(CodeGenerator* owner) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 115 | : owner_(owner), |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 116 | previous_(owner->state()) { |
| 117 | owner->set_state(this); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 121 | ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner, |
| 122 | JumpTarget* true_target, |
| 123 | JumpTarget* false_target) |
| 124 | : CodeGenState(owner), |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 125 | true_target_(true_target), |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 126 | false_target_(false_target) { |
| 127 | owner->set_state(this); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner, |
| 132 | Slot* slot, |
| 133 | TypeInfo type_info) |
| 134 | : CodeGenState(owner), |
| 135 | slot_(slot) { |
| 136 | owner->set_state(this); |
| 137 | old_type_info_ = owner->set_type_info(slot, type_info); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 141 | CodeGenState::~CodeGenState() { |
| 142 | ASSERT(owner_->state() == this); |
| 143 | owner_->set_state(previous_); |
| 144 | } |
| 145 | |
| 146 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 147 | TypeInfoCodeGenState::~TypeInfoCodeGenState() { |
| 148 | owner()->set_type_info(slot_, old_type_info_); |
| 149 | } |
| 150 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 151 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 152 | // CodeGenerator implementation |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 154 | int CodeGenerator::inlined_write_barrier_size_ = -1; |
| 155 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 156 | CodeGenerator::CodeGenerator(MacroAssembler* masm) |
| 157 | : deferred_(8), |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 158 | masm_(masm), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 159 | info_(NULL), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 160 | frame_(NULL), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 161 | allocator_(NULL), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | cc_reg_(al), |
| 163 | state_(NULL), |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 164 | loop_nesting_(0), |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 165 | type_info_(NULL), |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 166 | function_return_(JumpTarget::BIDIRECTIONAL), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 167 | function_return_is_shadowed_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | // Calling conventions: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 172 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 174 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | // cp: callee's context |
| 176 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 177 | void CodeGenerator::Generate(CompilationInfo* info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 178 | // Record the position for debugging purposes. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 179 | CodeForFunctionPosition(info->function()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 180 | Comment cmnt(masm_, "[ function compiled by virtual frame code generator"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | |
| 182 | // Initialize state. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 183 | info_ = info; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 184 | |
| 185 | int slots = scope()->num_parameters() + scope()->num_stack_slots(); |
| 186 | ScopedVector<TypeInfo> type_info_array(slots); |
| 187 | type_info_ = &type_info_array; |
| 188 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 189 | ASSERT(allocator_ == NULL); |
| 190 | RegisterAllocator register_allocator(this); |
| 191 | allocator_ = ®ister_allocator; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 192 | ASSERT(frame_ == NULL); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 193 | frame_ = new VirtualFrame(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 194 | cc_reg_ = al; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 195 | |
| 196 | // Adjust for function-level loop nesting. |
| 197 | ASSERT_EQ(0, loop_nesting_); |
| 198 | loop_nesting_ = info->loop_nesting(); |
| 199 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 200 | { |
| 201 | CodeGenState state(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 203 | // Entry: |
| 204 | // Stack: receiver, arguments |
| 205 | // lr: return address |
| 206 | // fp: caller's frame pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | // sp: stack pointer |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 208 | // r1: called JS function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | // cp: callee's context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 210 | allocator_->Initialize(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 211 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | #ifdef DEBUG |
| 213 | if (strlen(FLAG_stop_at) > 0 && |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 214 | info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 215 | frame_->SpillAll(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 216 | __ stop("stop-at"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 220 | frame_->Enter(); |
| 221 | // tos: code slot |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 222 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 223 | // Allocate space for locals and initialize them. This also checks |
| 224 | // for stack overflow. |
| 225 | frame_->AllocateStackSlots(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 226 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 227 | frame_->AssertIsSpilled(); |
| 228 | int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS; |
| 229 | if (heap_slots > 0) { |
| 230 | // Allocate local context. |
| 231 | // Get outer context and create a new context based on it. |
| 232 | __ ldr(r0, frame_->Function()); |
| 233 | frame_->EmitPush(r0); |
| 234 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 235 | FastNewContextStub stub(heap_slots); |
| 236 | frame_->CallStub(&stub, 1); |
| 237 | } else { |
| 238 | frame_->CallRuntime(Runtime::kNewContext, 1); |
| 239 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef DEBUG |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 242 | JumpTarget verified_true; |
| 243 | __ cmp(r0, cp); |
| 244 | verified_true.Branch(eq); |
| 245 | __ stop("NewContext: r0 is expected to be the same as cp"); |
| 246 | verified_true.Bind(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 247 | #endif |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 248 | // Update context local. |
| 249 | __ str(cp, frame_->Context()); |
| 250 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 251 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 252 | // TODO(1241774): Improve this code: |
| 253 | // 1) only needed if we have a context |
| 254 | // 2) no need to recompute context ptr every single time |
| 255 | // 3) don't copy parameter operand code from SlotOperand! |
| 256 | { |
| 257 | Comment cmnt2(masm_, "[ copy context parameters into .context"); |
| 258 | // Note that iteration order is relevant here! If we have the same |
| 259 | // parameter twice (e.g., function (x, y, x)), and that parameter |
| 260 | // needs to be copied into the context, it must be the last argument |
| 261 | // passed to the parameter that needs to be copied. This is a rare |
| 262 | // case so we don't check for it, instead we rely on the copying |
| 263 | // order: such a parameter is copied repeatedly into the same |
| 264 | // context location and thus the last value is what is seen inside |
| 265 | // the function. |
| 266 | frame_->AssertIsSpilled(); |
| 267 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 268 | Variable* par = scope()->parameter(i); |
| 269 | Slot* slot = par->slot(); |
| 270 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 271 | ASSERT(!scope()->is_global_scope()); // No params in global scope. |
| 272 | __ ldr(r1, frame_->ParameterAt(i)); |
| 273 | // Loads r2 with context; used below in RecordWrite. |
| 274 | __ str(r1, SlotOperand(slot, r2)); |
| 275 | // Load the offset into r3. |
| 276 | int slot_offset = |
| 277 | FixedArray::kHeaderSize + slot->index() * kPointerSize; |
| 278 | __ RecordWrite(r2, Operand(slot_offset), r3, r1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 281 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 282 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 283 | // Store the arguments object. This must happen after context |
| 284 | // initialization because the arguments object may be stored in |
| 285 | // the context. |
| 286 | if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) { |
| 287 | StoreArgumentsObject(true); |
| 288 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 289 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 290 | // Initialize ThisFunction reference if present. |
| 291 | if (scope()->is_function_scope() && scope()->function() != NULL) { |
| 292 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
| 293 | StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 294 | } |
| 295 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 296 | // Initialize the function return target after the locals are set |
| 297 | // up, because it needs the expected frame height from the frame. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 298 | function_return_.SetExpectedHeight(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 299 | function_return_is_shadowed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 301 | // Generate code to 'execute' declarations and initialize functions |
| 302 | // (source elements). In case of an illegal redeclaration we need to |
| 303 | // handle that instead of processing the declarations. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 304 | if (scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | Comment cmnt(masm_, "[ illegal redeclarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 306 | scope()->VisitIllegalRedeclaration(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | } else { |
| 308 | Comment cmnt(masm_, "[ declarations"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 309 | ProcessDeclarations(scope()->declarations()); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 310 | // Bail out if a stack-overflow exception occurred when processing |
| 311 | // declarations. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 312 | if (HasStackOverflow()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 313 | } |
| 314 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 315 | if (FLAG_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 316 | frame_->CallRuntime(Runtime::kTraceEnter, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 317 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 319 | |
| 320 | // Compile the body of the function in a vanilla state. Don't |
| 321 | // bother compiling all the code if the scope has an illegal |
| 322 | // redeclaration. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 323 | if (!scope()->HasIllegalRedeclaration()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | Comment cmnt(masm_, "[ function body"); |
| 325 | #ifdef DEBUG |
| 326 | bool is_builtin = Bootstrapper::IsActive(); |
| 327 | bool should_trace = |
| 328 | is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 329 | if (should_trace) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 330 | frame_->CallRuntime(Runtime::kDebugTrace, 0); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 331 | // Ignore the return value. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 332 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 333 | #endif |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 334 | VisitStatements(info->function()->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 336 | } |
| 337 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 338 | // Handle the return from the function. |
| 339 | if (has_valid_frame()) { |
| 340 | // If there is a valid frame, control flow can fall off the end of |
| 341 | // the body. In that case there is an implicit return statement. |
| 342 | ASSERT(!function_return_is_shadowed_); |
| 343 | frame_->PrepareForReturn(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 344 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 345 | if (function_return_.is_bound()) { |
| 346 | function_return_.Jump(); |
| 347 | } else { |
| 348 | function_return_.Bind(); |
| 349 | GenerateReturnSequence(); |
| 350 | } |
| 351 | } else if (function_return_.is_linked()) { |
| 352 | // If the return target has dangling jumps to it, then we have not |
| 353 | // yet generated the return sequence. This can happen when (a) |
| 354 | // control does not flow off the end of the body so we did not |
| 355 | // compile an artificial return statement just above, and (b) there |
| 356 | // are return statements in the body but (c) they are all shadowed. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 357 | function_return_.Bind(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 358 | GenerateReturnSequence(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 359 | } |
| 360 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 361 | // Adjust for function-level loop nesting. |
| 362 | ASSERT(loop_nesting_ == info->loop_nesting()); |
| 363 | loop_nesting_ = 0; |
| 364 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | // Code generation state must be reset. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | ASSERT(!has_cc()); |
| 367 | ASSERT(state_ == NULL); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 368 | ASSERT(loop_nesting() == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 369 | ASSERT(!function_return_is_shadowed_); |
| 370 | function_return_.Unuse(); |
| 371 | DeleteFrame(); |
| 372 | |
| 373 | // Process any deferred code using the register allocator. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 374 | if (!HasStackOverflow()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 375 | ProcessDeferred(); |
| 376 | } |
| 377 | |
| 378 | allocator_ = NULL; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 379 | type_info_ = NULL; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | int CodeGenerator::NumberOfSlot(Slot* slot) { |
| 384 | if (slot == NULL) return kInvalidSlotNumber; |
| 385 | switch (slot->type()) { |
| 386 | case Slot::PARAMETER: |
| 387 | return slot->index(); |
| 388 | case Slot::LOCAL: |
| 389 | return slot->index() + scope()->num_parameters(); |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | return kInvalidSlotNumber; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 397 | MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) { |
| 398 | // Currently, this assertion will fail if we try to assign to |
| 399 | // a constant variable that is constant because it is read-only |
| 400 | // (such as the variable referring to a named function expression). |
| 401 | // We need to implement assignments to read-only variables. |
| 402 | // Ideally, we should do this during AST generation (by converting |
| 403 | // such assignments into expression statements); however, in general |
| 404 | // we may not be able to make the decision until past AST generation, |
| 405 | // that is when the entire program is known. |
| 406 | ASSERT(slot != NULL); |
| 407 | int index = slot->index(); |
| 408 | switch (slot->type()) { |
| 409 | case Slot::PARAMETER: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 410 | return frame_->ParameterAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 411 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 412 | case Slot::LOCAL: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 413 | return frame_->LocalAt(index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 414 | |
| 415 | case Slot::CONTEXT: { |
| 416 | // Follow the context chain if necessary. |
| 417 | ASSERT(!tmp.is(cp)); // do not overwrite context register |
| 418 | Register context = cp; |
| 419 | int chain_length = scope()->ContextChainLength(slot->var()->scope()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 420 | for (int i = 0; i < chain_length; i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 421 | // Load the closure. |
| 422 | // (All contexts, even 'with' contexts, have a closure, |
| 423 | // and it is the same for all contexts inside a function. |
| 424 | // There is no need to go to the function context first.) |
| 425 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 426 | // Load the function context (which is the incoming, outer context). |
| 427 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 428 | context = tmp; |
| 429 | } |
| 430 | // We may have a 'with' context now. Get the function context. |
| 431 | // (In fact this mov may never be the needed, since the scope analysis |
| 432 | // may not permit a direct context access in this case and thus we are |
| 433 | // always at a function context. However it is safe to dereference be- |
| 434 | // cause the function context of a function context is itself. Before |
| 435 | // deleting this mov we should try to create a counter-example first, |
| 436 | // though...) |
| 437 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
| 438 | return ContextOperand(tmp, index); |
| 439 | } |
| 440 | |
| 441 | default: |
| 442 | UNREACHABLE(); |
| 443 | return MemOperand(r0, 0); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 448 | MemOperand CodeGenerator::ContextSlotOperandCheckExtensions( |
| 449 | Slot* slot, |
| 450 | Register tmp, |
| 451 | Register tmp2, |
| 452 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 453 | ASSERT(slot->type() == Slot::CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 454 | Register context = cp; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 455 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 456 | for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) { |
| 457 | if (s->num_heap_slots() > 0) { |
| 458 | if (s->calls_eval()) { |
| 459 | // Check that extension is NULL. |
| 460 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 461 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 462 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 463 | } |
| 464 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 465 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 466 | context = tmp; |
| 467 | } |
| 468 | } |
| 469 | // Check that last extension is NULL. |
| 470 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 471 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 472 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 473 | __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 474 | return ContextOperand(tmp, slot->index()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 478 | // Loads a value on TOS. If it is a boolean value, the result may have been |
| 479 | // (partially) translated into branches, or it may have set the condition |
| 480 | // code register. If force_cc is set, the value is forced to set the |
| 481 | // condition code register and no value is pushed. If the condition code |
| 482 | // register was set, has_cc() is true and cc_reg_ contains the condition to |
| 483 | // test for 'true'. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 484 | void CodeGenerator::LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 485 | JumpTarget* true_target, |
| 486 | JumpTarget* false_target, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 487 | bool force_cc) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 488 | ASSERT(!has_cc()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 489 | int original_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 491 | { ConditionCodeGenState new_state(this, true_target, false_target); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 492 | Visit(x); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 493 | |
| 494 | // If we hit a stack overflow, we may not have actually visited |
| 495 | // the expression. In that case, we ensure that we have a |
| 496 | // valid-looking frame state because we will continue to generate |
| 497 | // code as we unwind the C++ stack. |
| 498 | // |
| 499 | // It's possible to have both a stack overflow and a valid frame |
| 500 | // state (eg, a subexpression overflowed, visiting it returned |
| 501 | // with a dummied frame state, and visiting this expression |
| 502 | // returned with a normal-looking state). |
| 503 | if (HasStackOverflow() && |
| 504 | has_valid_frame() && |
| 505 | !has_cc() && |
| 506 | frame_->height() == original_height) { |
| 507 | true_target->Jump(); |
| 508 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 509 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 510 | if (force_cc && frame_ != NULL && !has_cc()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 511 | // Convert the TOS value to a boolean in the condition code register. |
| 512 | ToBoolean(true_target, false_target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | ASSERT(!force_cc || !has_valid_frame() || has_cc()); |
| 515 | ASSERT(!has_valid_frame() || |
| 516 | (has_cc() && frame_->height() == original_height) || |
| 517 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 521 | void CodeGenerator::Load(Expression* expr) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 522 | // We generally assume that we are not in a spilled scope for most |
| 523 | // of the code generator. A failure to ensure this caused issue 815 |
| 524 | // and this assert is designed to catch similar issues. |
| 525 | frame_->AssertIsNotSpilled(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 526 | #ifdef DEBUG |
| 527 | int original_height = frame_->height(); |
| 528 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 529 | JumpTarget true_target; |
| 530 | JumpTarget false_target; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 531 | LoadCondition(expr, &true_target, &false_target, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | |
| 533 | if (has_cc()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 534 | // Convert cc_reg_ into a boolean value. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 535 | JumpTarget loaded; |
| 536 | JumpTarget materialize_true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 537 | materialize_true.Branch(cc_reg_); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 538 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 539 | loaded.Jump(); |
| 540 | materialize_true.Bind(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 541 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 542 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 543 | cc_reg_ = al; |
| 544 | } |
| 545 | |
| 546 | if (true_target.is_linked() || false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 547 | // We have at least one condition value that has been "translated" |
| 548 | // into a branch, thus it needs to be loaded explicitly. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 549 | JumpTarget loaded; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 550 | if (frame_ != NULL) { |
| 551 | loaded.Jump(); // Don't lose the current TOS. |
| 552 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | bool both = true_target.is_linked() && false_target.is_linked(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 554 | // Load "true" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | if (true_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 556 | true_target.Bind(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 557 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 559 | // If both "true" and "false" need to be loaded jump across the code for |
| 560 | // "false". |
| 561 | if (both) { |
| 562 | loaded.Jump(); |
| 563 | } |
| 564 | // Load "false" if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | if (false_target.is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 566 | false_target.Bind(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 567 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 568 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 569 | // A value is loaded on all paths reaching this point. |
| 570 | loaded.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 572 | ASSERT(has_valid_frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | ASSERT(!has_cc()); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 574 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 578 | void CodeGenerator::LoadGlobal() { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 579 | Register reg = frame_->GetTOSRegister(); |
| 580 | __ ldr(reg, GlobalObject()); |
| 581 | frame_->EmitPush(reg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 585 | void CodeGenerator::LoadGlobalReceiver(Register scratch) { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 586 | Register reg = frame_->GetTOSRegister(); |
| 587 | __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 588 | __ ldr(reg, |
| 589 | FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset)); |
| 590 | frame_->EmitPush(reg); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 594 | ArgumentsAllocationMode CodeGenerator::ArgumentsMode() { |
| 595 | if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION; |
| 596 | ASSERT(scope()->arguments_shadow() != NULL); |
| 597 | // We don't want to do lazy arguments allocation for functions that |
| 598 | // have heap-allocated contexts, because it interfers with the |
| 599 | // uninitialized const tracking in the context objects. |
| 600 | return (scope()->num_heap_slots() > 0) |
| 601 | ? EAGER_ARGUMENTS_ALLOCATION |
| 602 | : LAZY_ARGUMENTS_ALLOCATION; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | void CodeGenerator::StoreArgumentsObject(bool initial) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 607 | ArgumentsAllocationMode mode = ArgumentsMode(); |
| 608 | ASSERT(mode != NO_ARGUMENTS_ALLOCATION); |
| 609 | |
| 610 | Comment cmnt(masm_, "[ store arguments object"); |
| 611 | if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) { |
| 612 | // When using lazy arguments allocation, we store the hole value |
| 613 | // as a sentinel indicating that the arguments object hasn't been |
| 614 | // allocated yet. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 615 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 616 | } else { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 617 | frame_->SpillAll(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 618 | ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
| 619 | __ ldr(r2, frame_->Function()); |
| 620 | // The receiver is below the arguments, the return address, and the |
| 621 | // frame pointer on the stack. |
| 622 | const int kReceiverDisplacement = 2 + scope()->num_parameters(); |
| 623 | __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize)); |
| 624 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
| 625 | frame_->Adjust(3); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 626 | __ Push(r2, r1, r0); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 627 | frame_->CallStub(&stub, 3); |
| 628 | frame_->EmitPush(r0); |
| 629 | } |
| 630 | |
| 631 | Variable* arguments = scope()->arguments()->var(); |
| 632 | Variable* shadow = scope()->arguments_shadow()->var(); |
| 633 | ASSERT(arguments != NULL && arguments->slot() != NULL); |
| 634 | ASSERT(shadow != NULL && shadow->slot() != NULL); |
| 635 | JumpTarget done; |
| 636 | if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) { |
| 637 | // We have to skip storing into the arguments slot if it has |
| 638 | // already been written to. This can happen if the a function |
| 639 | // has a local variable named 'arguments'. |
| 640 | LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 641 | Register arguments = frame_->PopToRegister(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 642 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 643 | __ cmp(arguments, ip); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 644 | done.Branch(ne); |
| 645 | } |
| 646 | StoreToSlot(arguments->slot(), NOT_CONST_INIT); |
| 647 | if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind(); |
| 648 | StoreToSlot(shadow->slot(), NOT_CONST_INIT); |
| 649 | } |
| 650 | |
| 651 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 652 | void CodeGenerator::LoadTypeofExpression(Expression* expr) { |
| 653 | // Special handling of identifiers as subexpressions of typeof. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 654 | Variable* variable = expr->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 655 | if (variable != NULL && !variable->is_this() && variable->is_global()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 656 | // For a global variable we build the property reference |
| 657 | // <global>.<variable> and perform a (regular non-contextual) property |
| 658 | // load to make sure we do not get reference errors. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX); |
| 660 | Literal key(variable->name()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 661 | Property property(&global, &key, RelocInfo::kNoPosition); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 662 | Reference ref(this, &property); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 663 | ref.GetValue(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 664 | } else if (variable != NULL && variable->slot() != NULL) { |
| 665 | // For a variable that rewrites to a slot, we signal it is the immediate |
| 666 | // subexpression of a typeof. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 667 | LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 669 | // Anything else can be handled normally. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 670 | Load(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 675 | Reference::Reference(CodeGenerator* cgen, |
| 676 | Expression* expression, |
| 677 | bool persist_after_get) |
| 678 | : cgen_(cgen), |
| 679 | expression_(expression), |
| 680 | type_(ILLEGAL), |
| 681 | persist_after_get_(persist_after_get) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 682 | // We generally assume that we are not in a spilled scope for most |
| 683 | // of the code generator. A failure to ensure this caused issue 815 |
| 684 | // and this assert is designed to catch similar issues. |
| 685 | cgen->frame()->AssertIsNotSpilled(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | cgen->LoadReference(this); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | Reference::~Reference() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 691 | ASSERT(is_unloaded() || is_illegal()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 695 | void CodeGenerator::LoadReference(Reference* ref) { |
| 696 | Comment cmnt(masm_, "[ LoadReference"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 697 | Expression* e = ref->expression(); |
| 698 | Property* property = e->AsProperty(); |
| 699 | Variable* var = e->AsVariableProxy()->AsVariable(); |
| 700 | |
| 701 | if (property != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 702 | // The expression is either a property or a variable proxy that rewrites |
| 703 | // to a property. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 704 | Load(property->obj()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 705 | if (property->key()->IsPropertyName()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 706 | ref->set_type(Reference::NAMED); |
| 707 | } else { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 708 | Load(property->key()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | ref->set_type(Reference::KEYED); |
| 710 | } |
| 711 | } else if (var != NULL) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 712 | // The expression is a variable proxy that does not rewrite to a |
| 713 | // property. Global variables are treated as named property references. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 714 | if (var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | LoadGlobal(); |
| 716 | ref->set_type(Reference::NAMED); |
| 717 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 718 | ASSERT(var->slot() != NULL); |
| 719 | ref->set_type(Reference::SLOT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | } |
| 721 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 722 | // Anything else is a runtime error. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 723 | Load(e); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 724 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 729 | void CodeGenerator::UnloadReference(Reference* ref) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | int size = ref->size(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 731 | ref->set_unloaded(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 732 | if (size == 0) return; |
| 733 | |
| 734 | // Pop a reference from the stack while preserving TOS. |
| 735 | VirtualFrame::RegisterAllocationScope scope(this); |
| 736 | Comment cmnt(masm_, "[ UnloadReference"); |
| 737 | if (size > 0) { |
| 738 | Register tos = frame_->PopToRegister(); |
| 739 | frame_->Drop(size); |
| 740 | frame_->EmitPush(tos); |
| 741 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 745 | // ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given |
| 746 | // register to a boolean in the condition code register. The code |
| 747 | // 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] | 748 | void CodeGenerator::ToBoolean(JumpTarget* true_target, |
| 749 | JumpTarget* false_target) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 750 | // Note: The generated code snippet does not change stack variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 751 | // Only the condition code should be set. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 752 | bool known_smi = frame_->KnownSmiAt(0); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 753 | Register tos = frame_->PopToRegister(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 754 | |
| 755 | // Fast case checks |
| 756 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 757 | // Check if the value is 'false'. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 758 | if (!known_smi) { |
| 759 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 760 | __ cmp(tos, ip); |
| 761 | false_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 762 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 763 | // Check if the value is 'true'. |
| 764 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
| 765 | __ cmp(tos, ip); |
| 766 | true_target->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 767 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 768 | // Check if the value is 'undefined'. |
| 769 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 770 | __ cmp(tos, ip); |
| 771 | false_target->Branch(eq); |
| 772 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 773 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 774 | // Check if the value is a smi. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 775 | __ cmp(tos, Operand(Smi::FromInt(0))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 777 | if (!known_smi) { |
| 778 | false_target->Branch(eq); |
| 779 | __ tst(tos, Operand(kSmiTagMask)); |
| 780 | true_target->Branch(eq); |
| 781 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 782 | // Slow case. |
| 783 | if (CpuFeatures::IsSupported(VFP3)) { |
| 784 | CpuFeatures::Scope scope(VFP3); |
| 785 | // Implements the slow case by using ToBooleanStub. |
| 786 | // The ToBooleanStub takes a single argument, and |
| 787 | // returns a non-zero value for true, or zero for false. |
| 788 | // Both the argument value and the return value use the |
| 789 | // register assigned to tos_ |
| 790 | ToBooleanStub stub(tos); |
| 791 | frame_->CallStub(&stub, 0); |
| 792 | // Convert the result in "tos" to a condition code. |
| 793 | __ cmp(tos, Operand(0)); |
| 794 | } else { |
| 795 | // Implements slow case by calling the runtime. |
| 796 | frame_->EmitPush(tos); |
| 797 | frame_->CallRuntime(Runtime::kToBool, 1); |
| 798 | // Convert the result (r0) to a condition code. |
| 799 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
| 800 | __ cmp(r0, ip); |
| 801 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 802 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 803 | |
| 804 | cc_reg_ = ne; |
| 805 | } |
| 806 | |
| 807 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 808 | void CodeGenerator::GenericBinaryOperation(Token::Value op, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 809 | OverwriteMode overwrite_mode, |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 810 | GenerateInlineSmi inline_smi, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 811 | int constant_rhs) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 812 | // top of virtual frame: y |
| 813 | // 2nd elt. on virtual frame : x |
| 814 | // result : top of virtual frame |
| 815 | |
| 816 | // Stub is entered with a call: 'return address' is in lr. |
| 817 | switch (op) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 818 | case Token::ADD: |
| 819 | case Token::SUB: |
| 820 | if (inline_smi) { |
| 821 | JumpTarget done; |
| 822 | Register rhs = frame_->PopToRegister(); |
| 823 | Register lhs = frame_->PopToRegister(rhs); |
| 824 | Register scratch = VirtualFrame::scratch0(); |
| 825 | __ orr(scratch, rhs, Operand(lhs)); |
| 826 | // Check they are both small and positive. |
| 827 | __ tst(scratch, Operand(kSmiTagMask | 0xc0000000)); |
| 828 | ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 829 | STATIC_ASSERT(kSmiTag == 0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 830 | if (op == Token::ADD) { |
| 831 | __ add(r0, lhs, Operand(rhs), LeaveCC, eq); |
| 832 | } else { |
| 833 | __ sub(r0, lhs, Operand(rhs), LeaveCC, eq); |
| 834 | } |
| 835 | done.Branch(eq); |
| 836 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 837 | frame_->SpillAll(); |
| 838 | frame_->CallStub(&stub, 0); |
| 839 | done.Bind(); |
| 840 | frame_->EmitPush(r0); |
| 841 | break; |
| 842 | } else { |
| 843 | // Fall through! |
| 844 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 845 | case Token::BIT_OR: |
| 846 | case Token::BIT_AND: |
| 847 | case Token::BIT_XOR: |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 848 | if (inline_smi) { |
| 849 | bool rhs_is_smi = frame_->KnownSmiAt(0); |
| 850 | bool lhs_is_smi = frame_->KnownSmiAt(1); |
| 851 | Register rhs = frame_->PopToRegister(); |
| 852 | Register lhs = frame_->PopToRegister(rhs); |
| 853 | Register smi_test_reg; |
| 854 | Condition cond; |
| 855 | if (!rhs_is_smi || !lhs_is_smi) { |
| 856 | if (rhs_is_smi) { |
| 857 | smi_test_reg = lhs; |
| 858 | } else if (lhs_is_smi) { |
| 859 | smi_test_reg = rhs; |
| 860 | } else { |
| 861 | smi_test_reg = VirtualFrame::scratch0(); |
| 862 | __ orr(smi_test_reg, rhs, Operand(lhs)); |
| 863 | } |
| 864 | // Check they are both Smis. |
| 865 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 866 | cond = eq; |
| 867 | } else { |
| 868 | cond = al; |
| 869 | } |
| 870 | ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now. |
| 871 | if (op == Token::BIT_OR) { |
| 872 | __ orr(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 873 | } else if (op == Token::BIT_AND) { |
| 874 | __ and_(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 875 | } else { |
| 876 | ASSERT(op == Token::BIT_XOR); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 877 | STATIC_ASSERT(kSmiTag == 0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 878 | __ eor(r0, lhs, Operand(rhs), LeaveCC, cond); |
| 879 | } |
| 880 | if (cond != al) { |
| 881 | JumpTarget done; |
| 882 | done.Branch(cond); |
| 883 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 884 | frame_->SpillAll(); |
| 885 | frame_->CallStub(&stub, 0); |
| 886 | done.Bind(); |
| 887 | } |
| 888 | frame_->EmitPush(r0); |
| 889 | break; |
| 890 | } else { |
| 891 | // Fall through! |
| 892 | } |
| 893 | case Token::MUL: |
| 894 | case Token::DIV: |
| 895 | case Token::MOD: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 896 | case Token::SHL: |
| 897 | case Token::SHR: |
| 898 | case Token::SAR: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 899 | Register rhs = frame_->PopToRegister(); |
| 900 | Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 901 | GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs); |
| 902 | frame_->SpillAll(); |
| 903 | frame_->CallStub(&stub, 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 904 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | break; |
| 906 | } |
| 907 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 908 | case Token::COMMA: { |
| 909 | Register scratch = frame_->PopToRegister(); |
| 910 | // Simply discard left value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 911 | frame_->Drop(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 912 | frame_->EmitPush(scratch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | break; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 914 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | |
| 916 | default: |
| 917 | // Other cases should have been handled before this point. |
| 918 | UNREACHABLE(); |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 924 | class DeferredInlineSmiOperation: public DeferredCode { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 925 | public: |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 926 | DeferredInlineSmiOperation(Token::Value op, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 927 | int value, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 928 | bool reversed, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 929 | OverwriteMode overwrite_mode, |
| 930 | Register tos) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 931 | : op_(op), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 932 | value_(value), |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 933 | reversed_(reversed), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 934 | overwrite_mode_(overwrite_mode), |
| 935 | tos_register_(tos) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 936 | set_comment("[ DeferredInlinedSmiOperation"); |
| 937 | } |
| 938 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 939 | virtual void Generate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 940 | |
| 941 | private: |
| 942 | Token::Value op_; |
| 943 | int value_; |
| 944 | bool reversed_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 945 | OverwriteMode overwrite_mode_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 946 | Register tos_register_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
| 949 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 950 | |
| 951 | // On entry the non-constant side of the binary operation is in tos_register_ |
| 952 | // and the constant smi side is nowhere. The tos_register_ is not used by the |
| 953 | // virtual frame. On exit the answer is in the tos_register_ and the virtual |
| 954 | // frame is unchanged. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 955 | void DeferredInlineSmiOperation::Generate() { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 956 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 957 | copied_frame.SpillAll(); |
| 958 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 959 | Register lhs = r1; |
| 960 | Register rhs = r0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 961 | switch (op_) { |
| 962 | case Token::ADD: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 963 | // Revert optimistic add. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 964 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 965 | __ sub(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 966 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 967 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 968 | __ sub(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 969 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 970 | } |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | case Token::SUB: { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 975 | // Revert optimistic sub. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 976 | if (reversed_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 977 | __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 978 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 979 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 980 | __ add(r1, tos_register_, Operand(Smi::FromInt(value_))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 981 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 982 | } |
| 983 | break; |
| 984 | } |
| 985 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 986 | // For these operations there is no optimistic operation that needs to be |
| 987 | // reverted. |
| 988 | case Token::MUL: |
| 989 | case Token::MOD: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 990 | case Token::BIT_OR: |
| 991 | case Token::BIT_XOR: |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 992 | case Token::BIT_AND: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 993 | case Token::SHL: |
| 994 | case Token::SHR: |
| 995 | case Token::SAR: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 996 | if (tos_register_.is(r1)) { |
| 997 | __ mov(r0, Operand(Smi::FromInt(value_))); |
| 998 | } else { |
| 999 | ASSERT(tos_register_.is(r0)); |
| 1000 | __ mov(r1, Operand(Smi::FromInt(value_))); |
| 1001 | } |
| 1002 | if (reversed_ == tos_register_.is(r1)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1003 | lhs = r0; |
| 1004 | rhs = r1; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1005 | } |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | default: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1010 | // Other cases should have been handled before this point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1011 | UNREACHABLE(); |
| 1012 | break; |
| 1013 | } |
| 1014 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1015 | GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1016 | __ CallStub(&stub); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1017 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1018 | // The generic stub returns its value in r0, but that's not |
| 1019 | // necessarily what we want. We want whatever the inlined code |
| 1020 | // expected, which is that the answer is in the same register as |
| 1021 | // the operand was. |
| 1022 | __ Move(tos_register_, r0); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1023 | |
| 1024 | // The tos register was not in use for the virtual frame that we |
| 1025 | // came into this function with, so we can merge back to that frame |
| 1026 | // without trashing it. |
| 1027 | copied_frame.MergeTo(frame_state()->frame()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1031 | static bool PopCountLessThanEqual2(unsigned int x) { |
| 1032 | x &= x - 1; |
| 1033 | return (x & (x - 1)) == 0; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | // Returns the index of the lowest bit set. |
| 1038 | static int BitPosition(unsigned x) { |
| 1039 | int bit_posn = 0; |
| 1040 | while ((x & 0xf) == 0) { |
| 1041 | bit_posn += 4; |
| 1042 | x >>= 4; |
| 1043 | } |
| 1044 | while ((x & 1) == 0) { |
| 1045 | bit_posn++; |
| 1046 | x >>= 1; |
| 1047 | } |
| 1048 | return bit_posn; |
| 1049 | } |
| 1050 | |
| 1051 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1052 | void CodeGenerator::SmiOperation(Token::Value op, |
| 1053 | Handle<Object> value, |
| 1054 | bool reversed, |
| 1055 | OverwriteMode mode) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1056 | int int_value = Smi::cast(*value)->value(); |
| 1057 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1058 | bool both_sides_are_smi = frame_->KnownSmiAt(0); |
| 1059 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1060 | bool something_to_inline; |
| 1061 | switch (op) { |
| 1062 | case Token::ADD: |
| 1063 | case Token::SUB: |
| 1064 | case Token::BIT_AND: |
| 1065 | case Token::BIT_OR: |
| 1066 | case Token::BIT_XOR: { |
| 1067 | something_to_inline = true; |
| 1068 | break; |
| 1069 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1070 | case Token::SHL: { |
| 1071 | something_to_inline = (both_sides_are_smi || !reversed); |
| 1072 | break; |
| 1073 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1074 | case Token::SHR: |
| 1075 | case Token::SAR: { |
| 1076 | if (reversed) { |
| 1077 | something_to_inline = false; |
| 1078 | } else { |
| 1079 | something_to_inline = true; |
| 1080 | } |
| 1081 | break; |
| 1082 | } |
| 1083 | case Token::MOD: { |
| 1084 | if (reversed || int_value < 2 || !IsPowerOf2(int_value)) { |
| 1085 | something_to_inline = false; |
| 1086 | } else { |
| 1087 | something_to_inline = true; |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | case Token::MUL: { |
| 1092 | if (!IsEasyToMultiplyBy(int_value)) { |
| 1093 | something_to_inline = false; |
| 1094 | } else { |
| 1095 | something_to_inline = true; |
| 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | default: { |
| 1100 | something_to_inline = false; |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | if (!something_to_inline) { |
| 1106 | if (!reversed) { |
| 1107 | // Push the rhs onto the virtual frame by putting it in a TOS register. |
| 1108 | Register rhs = frame_->GetTOSRegister(); |
| 1109 | __ mov(rhs, Operand(value)); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1110 | frame_->EmitPush(rhs, TypeInfo::Smi()); |
| 1111 | GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1112 | } else { |
| 1113 | // Pop the rhs, then push lhs and rhs in the right order. Only performs |
| 1114 | // at most one pop, the rest takes place in TOS registers. |
| 1115 | Register lhs = frame_->GetTOSRegister(); // Get reg for pushing. |
| 1116 | Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this. |
| 1117 | __ mov(lhs, Operand(value)); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1118 | frame_->EmitPush(lhs, TypeInfo::Smi()); |
| 1119 | TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown(); |
| 1120 | frame_->EmitPush(rhs, t); |
| 1121 | GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1122 | } |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | // We move the top of stack to a register (normally no move is invoved). |
| 1127 | Register tos = frame_->PopToRegister(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1128 | switch (op) { |
| 1129 | case Token::ADD: { |
| 1130 | DeferredCode* deferred = |
| 1131 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1132 | |
| 1133 | __ add(tos, tos, Operand(value), SetCC); |
| 1134 | deferred->Branch(vs); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1135 | if (!both_sides_are_smi) { |
| 1136 | __ tst(tos, Operand(kSmiTagMask)); |
| 1137 | deferred->Branch(ne); |
| 1138 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1139 | deferred->BindExit(); |
| 1140 | frame_->EmitPush(tos); |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | case Token::SUB: { |
| 1145 | DeferredCode* deferred = |
| 1146 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1147 | |
| 1148 | if (reversed) { |
| 1149 | __ rsb(tos, tos, Operand(value), SetCC); |
| 1150 | } else { |
| 1151 | __ sub(tos, tos, Operand(value), SetCC); |
| 1152 | } |
| 1153 | deferred->Branch(vs); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1154 | if (!both_sides_are_smi) { |
| 1155 | __ tst(tos, Operand(kSmiTagMask)); |
| 1156 | deferred->Branch(ne); |
| 1157 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1158 | deferred->BindExit(); |
| 1159 | frame_->EmitPush(tos); |
| 1160 | break; |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | case Token::BIT_OR: |
| 1165 | case Token::BIT_XOR: |
| 1166 | case Token::BIT_AND: { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1167 | if (both_sides_are_smi) { |
| 1168 | switch (op) { |
| 1169 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1170 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1171 | case Token::BIT_AND: __ And(tos, tos, Operand(value)); break; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1172 | default: UNREACHABLE(); |
| 1173 | } |
| 1174 | frame_->EmitPush(tos, TypeInfo::Smi()); |
| 1175 | } else { |
| 1176 | DeferredCode* deferred = |
| 1177 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
| 1178 | __ tst(tos, Operand(kSmiTagMask)); |
| 1179 | deferred->Branch(ne); |
| 1180 | switch (op) { |
| 1181 | case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break; |
| 1182 | case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1183 | case Token::BIT_AND: __ And(tos, tos, Operand(value)); break; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1184 | default: UNREACHABLE(); |
| 1185 | } |
| 1186 | deferred->BindExit(); |
| 1187 | TypeInfo result_type = |
| 1188 | (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32(); |
| 1189 | frame_->EmitPush(tos, result_type); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1190 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | case Token::SHL: |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1195 | if (reversed) { |
| 1196 | ASSERT(both_sides_are_smi); |
| 1197 | int max_shift = 0; |
| 1198 | int max_result = int_value == 0 ? 1 : int_value; |
| 1199 | while (Smi::IsValid(max_result << 1)) { |
| 1200 | max_shift++; |
| 1201 | max_result <<= 1; |
| 1202 | } |
| 1203 | DeferredCode* deferred = |
| 1204 | new DeferredInlineSmiOperation(op, int_value, true, mode, tos); |
| 1205 | // Mask off the last 5 bits of the shift operand (rhs). This is part |
| 1206 | // of the definition of shift in JS and we know we have a Smi so we |
| 1207 | // can safely do this. The masked version gets passed to the |
| 1208 | // deferred code, but that makes no difference. |
| 1209 | __ and_(tos, tos, Operand(Smi::FromInt(0x1f))); |
| 1210 | __ cmp(tos, Operand(Smi::FromInt(max_shift))); |
| 1211 | deferred->Branch(ge); |
| 1212 | Register scratch = VirtualFrame::scratch0(); |
| 1213 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag. |
| 1214 | __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant. |
| 1215 | __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant. |
| 1216 | deferred->BindExit(); |
| 1217 | TypeInfo result = TypeInfo::Integer32(); |
| 1218 | frame_->EmitPush(tos, result); |
| 1219 | break; |
| 1220 | } |
| 1221 | // Fall through! |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1222 | case Token::SHR: |
| 1223 | case Token::SAR: { |
| 1224 | ASSERT(!reversed); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 1225 | TypeInfo result = |
| 1226 | (op == Token::SAR) ? TypeInfo::Integer32() : TypeInfo::Number(); |
| 1227 | if (!reversed) { |
| 1228 | if (op == Token::SHR) { |
| 1229 | if (int_value >= 2) { |
| 1230 | result = TypeInfo::Smi(); |
| 1231 | } else if (int_value >= 1) { |
| 1232 | result = TypeInfo::Integer32(); |
| 1233 | } |
| 1234 | } else { |
| 1235 | if (int_value >= 1) { |
| 1236 | result = TypeInfo::Smi(); |
| 1237 | } |
| 1238 | } |
| 1239 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1240 | Register scratch = VirtualFrame::scratch0(); |
| 1241 | Register scratch2 = VirtualFrame::scratch1(); |
| 1242 | int shift_value = int_value & 0x1f; // least significant 5 bits |
| 1243 | DeferredCode* deferred = |
| 1244 | new DeferredInlineSmiOperation(op, shift_value, false, mode, tos); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1245 | uint32_t problematic_mask = kSmiTagMask; |
| 1246 | // For unsigned shift by zero all negative smis are problematic. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1247 | bool skip_smi_test = both_sides_are_smi; |
| 1248 | if (shift_value == 0 && op == Token::SHR) { |
| 1249 | problematic_mask |= 0x80000000; |
| 1250 | skip_smi_test = false; |
| 1251 | } |
| 1252 | if (!skip_smi_test) { |
| 1253 | __ tst(tos, Operand(problematic_mask)); |
| 1254 | deferred->Branch(ne); // Go slow for problematic input. |
| 1255 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1256 | switch (op) { |
| 1257 | case Token::SHL: { |
| 1258 | if (shift_value != 0) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1259 | int adjusted_shift = shift_value - kSmiTagSize; |
| 1260 | ASSERT(adjusted_shift >= 0); |
| 1261 | if (adjusted_shift != 0) { |
| 1262 | __ mov(scratch, Operand(tos, LSL, adjusted_shift)); |
| 1263 | // Check that the *signed* result fits in a smi. |
| 1264 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
| 1265 | deferred->Branch(mi); |
| 1266 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
| 1267 | } else { |
| 1268 | // Check that the *signed* result fits in a smi. |
| 1269 | __ add(scratch2, tos, Operand(0x40000000), SetCC); |
| 1270 | deferred->Branch(mi); |
| 1271 | __ mov(tos, Operand(tos, LSL, kSmiTagSize)); |
| 1272 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1273 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | case Token::SHR: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1277 | if (shift_value != 0) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1278 | __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag. |
| 1279 | // LSR by immediate 0 means shifting 32 bits. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1280 | __ mov(scratch, Operand(scratch, LSR, shift_value)); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1281 | if (shift_value == 1) { |
| 1282 | // check that the *unsigned* result fits in a smi |
| 1283 | // neither of the two high-order bits can be set: |
| 1284 | // - 0x80000000: high bit would be lost when smi tagging |
| 1285 | // - 0x40000000: this number would convert to negative when |
| 1286 | // smi tagging these two cases can only happen with shifts |
| 1287 | // by 0 or 1 when handed a valid smi |
| 1288 | __ tst(scratch, Operand(0xc0000000)); |
| 1289 | deferred->Branch(ne); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1290 | } else { |
| 1291 | ASSERT(shift_value >= 2); |
| 1292 | result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1293 | } |
| 1294 | __ mov(tos, Operand(scratch, LSL, kSmiTagSize)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1295 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | case Token::SAR: { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1299 | // In the ARM instructions set, ASR by immediate 0 means shifting 32 |
| 1300 | // bits. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1301 | if (shift_value != 0) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1302 | // Do the shift and the tag removal in one operation. If the shift |
| 1303 | // is 31 bits (the highest possible value) then we emit the |
| 1304 | // instruction as a shift by 0 which means shift arithmetically by |
| 1305 | // 32. |
| 1306 | __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f)); |
| 1307 | // Put tag back. |
| 1308 | __ mov(tos, Operand(tos, LSL, kSmiTagSize)); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1309 | // SAR by at least 1 gives a Smi. |
| 1310 | result = TypeInfo::Smi(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1311 | } |
| 1312 | break; |
| 1313 | } |
| 1314 | default: UNREACHABLE(); |
| 1315 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1316 | deferred->BindExit(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1317 | frame_->EmitPush(tos, result); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | case Token::MOD: { |
| 1322 | ASSERT(!reversed); |
| 1323 | ASSERT(int_value >= 2); |
| 1324 | ASSERT(IsPowerOf2(int_value)); |
| 1325 | DeferredCode* deferred = |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1326 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1327 | unsigned mask = (0x80000000u | kSmiTagMask); |
| 1328 | __ tst(tos, Operand(mask)); |
| 1329 | deferred->Branch(ne); // Go to deferred code on non-Smis and negative. |
| 1330 | mask = (int_value << kSmiTagSize) - 1; |
| 1331 | __ and_(tos, tos, Operand(mask)); |
| 1332 | deferred->BindExit(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1333 | // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer. |
| 1334 | frame_->EmitPush( |
| 1335 | tos, |
| 1336 | both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | case Token::MUL: { |
| 1341 | ASSERT(IsEasyToMultiplyBy(int_value)); |
| 1342 | DeferredCode* deferred = |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1343 | new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1344 | unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value; |
| 1345 | max_smi_that_wont_overflow <<= kSmiTagSize; |
| 1346 | unsigned mask = 0x80000000u; |
| 1347 | while ((mask & max_smi_that_wont_overflow) == 0) { |
| 1348 | mask |= mask >> 1; |
| 1349 | } |
| 1350 | mask |= kSmiTagMask; |
| 1351 | // This does a single mask that checks for a too high value in a |
| 1352 | // conservative way and for a non-Smi. It also filters out negative |
| 1353 | // numbers, unfortunately, but since this code is inline we prefer |
| 1354 | // brevity to comprehensiveness. |
| 1355 | __ tst(tos, Operand(mask)); |
| 1356 | deferred->Branch(ne); |
| 1357 | MultiplyByKnownInt(masm_, tos, tos, int_value); |
| 1358 | deferred->BindExit(); |
| 1359 | frame_->EmitPush(tos); |
| 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | default: |
| 1364 | UNREACHABLE(); |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1370 | void CodeGenerator::Comparison(Condition cc, |
| 1371 | Expression* left, |
| 1372 | Expression* right, |
| 1373 | bool strict) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1374 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1375 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1376 | if (left != NULL) Load(left); |
| 1377 | if (right != NULL) Load(right); |
| 1378 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1379 | // sp[0] : y |
| 1380 | // sp[1] : x |
| 1381 | // result : cc register |
| 1382 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1383 | // Strict only makes sense for equality comparisons. |
| 1384 | ASSERT(!strict || cc == eq); |
| 1385 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1386 | Register lhs; |
| 1387 | Register rhs; |
| 1388 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1389 | bool lhs_is_smi; |
| 1390 | bool rhs_is_smi; |
| 1391 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1392 | // We load the top two stack positions into registers chosen by the virtual |
| 1393 | // frame. This should keep the register shuffling to a minimum. |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1394 | // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order. |
| 1395 | if (cc == gt || cc == le) { |
| 1396 | cc = ReverseCondition(cc); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1397 | lhs_is_smi = frame_->KnownSmiAt(0); |
| 1398 | rhs_is_smi = frame_->KnownSmiAt(1); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1399 | lhs = frame_->PopToRegister(); |
| 1400 | rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1401 | } else { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1402 | rhs_is_smi = frame_->KnownSmiAt(0); |
| 1403 | lhs_is_smi = frame_->KnownSmiAt(1); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1404 | rhs = frame_->PopToRegister(); |
| 1405 | lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again! |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1406 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1407 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1408 | bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi); |
| 1409 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1410 | ASSERT(rhs.is(r0) || rhs.is(r1)); |
| 1411 | ASSERT(lhs.is(r0) || lhs.is(r1)); |
| 1412 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1413 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1414 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1415 | if (!both_sides_are_smi) { |
| 1416 | // Now we have the two sides in r0 and r1. We flush any other registers |
| 1417 | // because the stub doesn't know about register allocation. |
| 1418 | frame_->SpillAll(); |
| 1419 | Register scratch = VirtualFrame::scratch0(); |
| 1420 | Register smi_test_reg; |
| 1421 | if (lhs_is_smi) { |
| 1422 | smi_test_reg = rhs; |
| 1423 | } else if (rhs_is_smi) { |
| 1424 | smi_test_reg = lhs; |
| 1425 | } else { |
| 1426 | __ orr(scratch, lhs, Operand(rhs)); |
| 1427 | smi_test_reg = scratch; |
| 1428 | } |
| 1429 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 1430 | JumpTarget smi; |
| 1431 | smi.Branch(eq); |
| 1432 | |
| 1433 | // Perform non-smi comparison by stub. |
| 1434 | // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0. |
| 1435 | // We call with 0 args because there are 0 on the stack. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 1436 | CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 1437 | frame_->CallStub(&stub, 0); |
| 1438 | __ cmp(r0, Operand(0)); |
| 1439 | exit.Jump(); |
| 1440 | |
| 1441 | smi.Bind(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1444 | // Do smi comparisons by pointer comparison. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1445 | __ cmp(lhs, Operand(rhs)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1447 | exit.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1448 | cc_reg_ = cc; |
| 1449 | } |
| 1450 | |
| 1451 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1452 | // Call the function on the stack with the given arguments. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1453 | void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1454 | CallFunctionFlags flags, |
| 1455 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | // Push the arguments ("left-to-right") on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1457 | int arg_count = args->length(); |
| 1458 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1459 | Load(args->at(i)); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1460 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1462 | // Record the position for debugging purposes. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1463 | CodeForSourcePosition(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1465 | // Use the shared code stub to call the function. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1466 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1467 | CallFunctionStub call_function(arg_count, in_loop, flags); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1468 | frame_->CallStub(&call_function, arg_count + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Restore context and pop function from the stack. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1471 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1472 | frame_->Drop(); // discard the TOS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1476 | void CodeGenerator::CallApplyLazy(Expression* applicand, |
| 1477 | Expression* receiver, |
| 1478 | VariableProxy* arguments, |
| 1479 | int position) { |
| 1480 | // An optimized implementation of expressions of the form |
| 1481 | // x.apply(y, arguments). |
| 1482 | // If the arguments object of the scope has not been allocated, |
| 1483 | // and x.apply is Function.prototype.apply, this optimization |
| 1484 | // just copies y and the arguments of the current function on the |
| 1485 | // stack, as receiver and arguments, and calls x. |
| 1486 | // In the implementation comments, we call x the applicand |
| 1487 | // and y the receiver. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1488 | |
| 1489 | ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION); |
| 1490 | ASSERT(arguments->IsArguments()); |
| 1491 | |
| 1492 | // Load applicand.apply onto the stack. This will usually |
| 1493 | // give us a megamorphic load site. Not super, but it works. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1494 | Load(applicand); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1495 | Handle<String> name = Factory::LookupAsciiSymbol("apply"); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1496 | frame_->Dup(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1497 | frame_->CallLoadIC(name, RelocInfo::CODE_TARGET); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1498 | frame_->EmitPush(r0); |
| 1499 | |
| 1500 | // Load the receiver and the existing arguments object onto the |
| 1501 | // expression stack. Avoid allocating the arguments object here. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1502 | Load(receiver); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1503 | LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF); |
| 1504 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1505 | // At this point the top two stack elements are probably in registers |
| 1506 | // since they were just loaded. Ensure they are in regs and get the |
| 1507 | // regs. |
| 1508 | Register receiver_reg = frame_->Peek2(); |
| 1509 | Register arguments_reg = frame_->Peek(); |
| 1510 | |
| 1511 | // From now on the frame is spilled. |
| 1512 | frame_->SpillAll(); |
| 1513 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1514 | // Emit the source position information after having loaded the |
| 1515 | // receiver and the arguments. |
| 1516 | CodeForSourcePosition(position); |
| 1517 | // Contents of the stack at this point: |
| 1518 | // sp[0]: arguments object of the current function or the hole. |
| 1519 | // sp[1]: receiver |
| 1520 | // sp[2]: applicand.apply |
| 1521 | // sp[3]: applicand. |
| 1522 | |
| 1523 | // Check if the arguments object has been lazily allocated |
| 1524 | // already. If so, just use that instead of copying the arguments |
| 1525 | // from the stack. This also deals with cases where a local variable |
| 1526 | // named 'arguments' has been introduced. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1527 | JumpTarget slow; |
| 1528 | Label done; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1529 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1530 | __ cmp(ip, arguments_reg); |
| 1531 | slow.Branch(ne); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1532 | |
| 1533 | Label build_args; |
| 1534 | // Get rid of the arguments object probe. |
| 1535 | frame_->Drop(); |
| 1536 | // Stack now has 3 elements on it. |
| 1537 | // Contents of stack at this point: |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1538 | // sp[0]: receiver - in the receiver_reg register. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1539 | // sp[1]: applicand.apply |
| 1540 | // sp[2]: applicand. |
| 1541 | |
| 1542 | // Check that the receiver really is a JavaScript object. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1543 | __ BranchOnSmi(receiver_reg, &build_args); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1544 | // We allow all JSObjects including JSFunctions. As long as |
| 1545 | // JS_FUNCTION_TYPE is the last instance type and it is right |
| 1546 | // after LAST_JS_OBJECT_TYPE, we do not have to check the upper |
| 1547 | // bound. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 1548 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 1549 | STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1550 | __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1551 | __ b(lt, &build_args); |
| 1552 | |
| 1553 | // Check that applicand.apply is Function.prototype.apply. |
| 1554 | __ ldr(r0, MemOperand(sp, kPointerSize)); |
| 1555 | __ BranchOnSmi(r0, &build_args); |
| 1556 | __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE); |
| 1557 | __ b(ne, &build_args); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1558 | Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1559 | __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeOffset)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1560 | __ cmp(r1, Operand(apply_code)); |
| 1561 | __ b(ne, &build_args); |
| 1562 | |
| 1563 | // Check that applicand is a function. |
| 1564 | __ ldr(r1, MemOperand(sp, 2 * kPointerSize)); |
| 1565 | __ BranchOnSmi(r1, &build_args); |
| 1566 | __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE); |
| 1567 | __ b(ne, &build_args); |
| 1568 | |
| 1569 | // Copy the arguments to this function possibly from the |
| 1570 | // adaptor frame below it. |
| 1571 | Label invoke, adapted; |
| 1572 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 1573 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
| 1574 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 1575 | __ b(eq, &adapted); |
| 1576 | |
| 1577 | // No arguments adaptor frame. Copy fixed number of arguments. |
| 1578 | __ mov(r0, Operand(scope()->num_parameters())); |
| 1579 | for (int i = 0; i < scope()->num_parameters(); i++) { |
| 1580 | __ ldr(r2, frame_->ParameterAt(i)); |
| 1581 | __ push(r2); |
| 1582 | } |
| 1583 | __ jmp(&invoke); |
| 1584 | |
| 1585 | // Arguments adaptor frame present. Copy arguments from there, but |
| 1586 | // avoid copying too many arguments to avoid stack overflows. |
| 1587 | __ bind(&adapted); |
| 1588 | static const uint32_t kArgumentsLimit = 1 * KB; |
| 1589 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 1590 | __ mov(r0, Operand(r0, LSR, kSmiTagSize)); |
| 1591 | __ mov(r3, r0); |
| 1592 | __ cmp(r0, Operand(kArgumentsLimit)); |
| 1593 | __ b(gt, &build_args); |
| 1594 | |
| 1595 | // Loop through the arguments pushing them onto the execution |
| 1596 | // stack. We don't inform the virtual frame of the push, so we don't |
| 1597 | // have to worry about getting rid of the elements from the virtual |
| 1598 | // frame. |
| 1599 | Label loop; |
| 1600 | // r3 is a small non-negative integer, due to the test above. |
| 1601 | __ cmp(r3, Operand(0)); |
| 1602 | __ b(eq, &invoke); |
| 1603 | // Compute the address of the first argument. |
| 1604 | __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2)); |
| 1605 | __ add(r2, r2, Operand(kPointerSize)); |
| 1606 | __ bind(&loop); |
| 1607 | // Post-decrement argument address by kPointerSize on each iteration. |
| 1608 | __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex)); |
| 1609 | __ push(r4); |
| 1610 | __ sub(r3, r3, Operand(1), SetCC); |
| 1611 | __ b(gt, &loop); |
| 1612 | |
| 1613 | // Invoke the function. |
| 1614 | __ bind(&invoke); |
| 1615 | ParameterCount actual(r0); |
| 1616 | __ InvokeFunction(r1, actual, CALL_FUNCTION); |
| 1617 | // Drop applicand.apply and applicand from the stack, and push |
| 1618 | // the result of the function call, but leave the spilled frame |
| 1619 | // unchanged, with 3 elements, so it is correct when we compile the |
| 1620 | // slow-case code. |
| 1621 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 1622 | __ push(r0); |
| 1623 | // Stack now has 1 element: |
| 1624 | // sp[0]: result |
| 1625 | __ jmp(&done); |
| 1626 | |
| 1627 | // Slow-case: Allocate the arguments object since we know it isn't |
| 1628 | // there, and fall-through to the slow-case where we call |
| 1629 | // applicand.apply. |
| 1630 | __ bind(&build_args); |
| 1631 | // Stack now has 3 elements, because we have jumped from where: |
| 1632 | // sp[0]: receiver |
| 1633 | // sp[1]: applicand.apply |
| 1634 | // sp[2]: applicand. |
| 1635 | StoreArgumentsObject(false); |
| 1636 | |
| 1637 | // Stack and frame now have 4 elements. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1638 | slow.Bind(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1639 | |
| 1640 | // Generic computation of x.apply(y, args) with no special optimization. |
| 1641 | // Flip applicand.apply and applicand on the stack, so |
| 1642 | // applicand looks like the receiver of the applicand.apply call. |
| 1643 | // Then process it as a normal function call. |
| 1644 | __ ldr(r0, MemOperand(sp, 3 * kPointerSize)); |
| 1645 | __ ldr(r1, MemOperand(sp, 2 * kPointerSize)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1646 | __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1647 | |
| 1648 | CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS); |
| 1649 | frame_->CallStub(&call_function, 3); |
| 1650 | // The function and its two arguments have been dropped. |
| 1651 | frame_->Drop(); // Drop the receiver as well. |
| 1652 | frame_->EmitPush(r0); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1653 | frame_->SpillAll(); // A spilled frame is also jumping to label done. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1654 | // Stack now has 1 element: |
| 1655 | // sp[0]: result |
| 1656 | __ bind(&done); |
| 1657 | |
| 1658 | // Restore the context register after a call. |
| 1659 | __ ldr(cp, frame_->Context()); |
| 1660 | } |
| 1661 | |
| 1662 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1663 | void CodeGenerator::Branch(bool if_true, JumpTarget* target) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1664 | ASSERT(has_cc()); |
| 1665 | Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1666 | target->Branch(cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1667 | cc_reg_ = al; |
| 1668 | } |
| 1669 | |
| 1670 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1671 | void CodeGenerator::CheckStack() { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1672 | frame_->SpillAll(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1673 | Comment cmnt(masm_, "[ check stack"); |
| 1674 | __ LoadRoot(ip, Heap::kStackLimitRootIndex); |
| 1675 | // Put the lr setup instruction in the delay slot. kInstrSize is added to |
| 1676 | // the implicit 8 byte offset that always applies to operations with pc and |
| 1677 | // gives a return address 12 bytes down. |
| 1678 | masm_->add(lr, pc, Operand(Assembler::kInstrSize)); |
| 1679 | masm_->cmp(sp, Operand(ip)); |
| 1680 | StackCheckStub stub; |
| 1681 | // Call the stub if lower. |
| 1682 | masm_->mov(pc, |
| 1683 | Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()), |
| 1684 | RelocInfo::CODE_TARGET), |
| 1685 | LeaveCC, |
| 1686 | lo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1690 | void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) { |
| 1691 | #ifdef DEBUG |
| 1692 | int original_height = frame_->height(); |
| 1693 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1694 | for (int i = 0; frame_ != NULL && i < statements->length(); i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1695 | Visit(statements->at(i)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1696 | } |
| 1697 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 1698 | } |
| 1699 | |
| 1700 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1701 | void CodeGenerator::VisitBlock(Block* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1702 | #ifdef DEBUG |
| 1703 | int original_height = frame_->height(); |
| 1704 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1705 | Comment cmnt(masm_, "[ Block"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1706 | CodeForStatementPosition(node); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1707 | node->break_target()->SetExpectedHeight(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1708 | VisitStatements(node->statements()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1709 | if (node->break_target()->is_linked()) { |
| 1710 | node->break_target()->Bind(); |
| 1711 | } |
| 1712 | node->break_target()->Unuse(); |
| 1713 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1717 | void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1718 | frame_->EmitPush(cp); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1719 | frame_->EmitPush(Operand(pairs)); |
| 1720 | frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0))); |
| 1721 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1722 | frame_->CallRuntime(Runtime::kDeclareGlobals, 3); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1723 | // The result is discarded. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1727 | void CodeGenerator::VisitDeclaration(Declaration* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1728 | #ifdef DEBUG |
| 1729 | int original_height = frame_->height(); |
| 1730 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1731 | Comment cmnt(masm_, "[ Declaration"); |
| 1732 | Variable* var = node->proxy()->var(); |
| 1733 | ASSERT(var != NULL); // must have been resolved |
| 1734 | Slot* slot = var->slot(); |
| 1735 | |
| 1736 | // If it was not possible to allocate the variable at compile time, |
| 1737 | // we need to "declare" it at runtime to make sure it actually |
| 1738 | // exists in the local context. |
| 1739 | if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 1740 | // Variables with a "LOOKUP" slot were introduced as non-locals |
| 1741 | // during variable resolution and must have mode DYNAMIC. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1742 | ASSERT(var->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1743 | // For now, just do a runtime call. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1744 | frame_->EmitPush(cp); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1745 | frame_->EmitPush(Operand(var->name())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1746 | // Declaration nodes are always declared in only two modes. |
| 1747 | ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST); |
| 1748 | PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1749 | frame_->EmitPush(Operand(Smi::FromInt(attr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1750 | // Push initial value, if any. |
| 1751 | // Note: For variables we must not push an initial value (such as |
| 1752 | // 'undefined') because we may have a (legal) redeclaration and we |
| 1753 | // must not destroy the current value. |
| 1754 | if (node->mode() == Variable::CONST) { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1755 | frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | } else if (node->fun() != NULL) { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1757 | Load(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1758 | } else { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1759 | frame_->EmitPush(Operand(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1760 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1761 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1762 | frame_->CallRuntime(Runtime::kDeclareContextSlot, 4); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1763 | // Ignore the return value (declarations are statements). |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1764 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1765 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | ASSERT(!var->is_global()); |
| 1770 | |
| 1771 | // If we have a function or a constant, we need to initialize the variable. |
| 1772 | Expression* val = NULL; |
| 1773 | if (node->mode() == Variable::CONST) { |
| 1774 | val = new Literal(Factory::the_hole_value()); |
| 1775 | } else { |
| 1776 | val = node->fun(); // NULL if we don't have a function |
| 1777 | } |
| 1778 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1779 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1780 | if (val != NULL) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1781 | WriteBarrierCharacter wb_info = |
| 1782 | val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI; |
| 1783 | if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1784 | // Set initial value. |
| 1785 | Reference target(this, node->proxy()); |
| 1786 | Load(val); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1787 | target.SetValue(NOT_CONST_INIT, wb_info); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 1788 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1789 | // Get rid of the assigned value (declarations are statements). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1790 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1791 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1792 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1796 | void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1797 | #ifdef DEBUG |
| 1798 | int original_height = frame_->height(); |
| 1799 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1800 | Comment cmnt(masm_, "[ ExpressionStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1801 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | Expression* expression = node->expression(); |
| 1803 | expression->MarkAsStatement(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1804 | Load(expression); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1805 | frame_->Drop(); |
| 1806 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1810 | void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1811 | #ifdef DEBUG |
| 1812 | int original_height = frame_->height(); |
| 1813 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | Comment cmnt(masm_, "// EmptyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1815 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1816 | // nothing to do |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1817 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1821 | void CodeGenerator::VisitIfStatement(IfStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1822 | #ifdef DEBUG |
| 1823 | int original_height = frame_->height(); |
| 1824 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1825 | Comment cmnt(masm_, "[ IfStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1826 | // Generate different code depending on which parts of the if statement |
| 1827 | // are present or not. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1828 | bool has_then_stm = node->HasThenStatement(); |
| 1829 | bool has_else_stm = node->HasElseStatement(); |
| 1830 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1831 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1832 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1833 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | if (has_then_stm && has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1835 | Comment cmnt(masm_, "[ IfThenElse"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1836 | JumpTarget then; |
| 1837 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1838 | // if (cond) |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1839 | LoadCondition(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1840 | if (frame_ != NULL) { |
| 1841 | Branch(false, &else_); |
| 1842 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1843 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1844 | if (frame_ != NULL || then.is_linked()) { |
| 1845 | then.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1846 | Visit(node->then_statement()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1847 | } |
| 1848 | if (frame_ != NULL) { |
| 1849 | exit.Jump(); |
| 1850 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1852 | if (else_.is_linked()) { |
| 1853 | else_.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1854 | Visit(node->else_statement()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1855 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1856 | |
| 1857 | } else if (has_then_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1858 | Comment cmnt(masm_, "[ IfThen"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | ASSERT(!has_else_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1860 | JumpTarget then; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1861 | // if (cond) |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1862 | LoadCondition(node->condition(), &then, &exit, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1863 | if (frame_ != NULL) { |
| 1864 | Branch(false, &exit); |
| 1865 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | // then |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1867 | if (frame_ != NULL || then.is_linked()) { |
| 1868 | then.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1869 | Visit(node->then_statement()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1870 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | |
| 1872 | } else if (has_else_stm) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1873 | Comment cmnt(masm_, "[ IfElse"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1874 | ASSERT(!has_then_stm); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1875 | JumpTarget else_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | // if (!cond) |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1877 | LoadCondition(node->condition(), &exit, &else_, true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1878 | if (frame_ != NULL) { |
| 1879 | Branch(true, &exit); |
| 1880 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1881 | // else |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1882 | if (frame_ != NULL || else_.is_linked()) { |
| 1883 | else_.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1884 | Visit(node->else_statement()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1885 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | |
| 1887 | } else { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1888 | Comment cmnt(masm_, "[ If"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | ASSERT(!has_then_stm && !has_else_stm); |
| 1890 | // if (cond) |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1891 | LoadCondition(node->condition(), &exit, &exit, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1892 | if (frame_ != NULL) { |
| 1893 | if (has_cc()) { |
| 1894 | cc_reg_ = al; |
| 1895 | } else { |
| 1896 | frame_->Drop(); |
| 1897 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // end |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1902 | if (exit.is_linked()) { |
| 1903 | exit.Bind(); |
| 1904 | } |
| 1905 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1909 | void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | Comment cmnt(masm_, "[ ContinueStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1911 | CodeForStatementPosition(node); |
| 1912 | node->target()->continue_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1916 | void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | Comment cmnt(masm_, "[ BreakStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1918 | CodeForStatementPosition(node); |
| 1919 | node->target()->break_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1923 | void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1924 | Comment cmnt(masm_, "[ ReturnStatement"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1925 | |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 1926 | CodeForStatementPosition(node); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1927 | Load(node->expression()); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 1928 | frame_->PopToR0(); |
| 1929 | frame_->PrepareForReturn(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1930 | if (function_return_is_shadowed_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1931 | function_return_.Jump(); |
| 1932 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1933 | // Pop the result from the frame and prepare the frame for |
| 1934 | // returning thus making it easier to merge. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1935 | if (function_return_.is_bound()) { |
| 1936 | // If the function return label is already bound we reuse the |
| 1937 | // code by jumping to the return site. |
| 1938 | function_return_.Jump(); |
| 1939 | } else { |
| 1940 | function_return_.Bind(); |
| 1941 | GenerateReturnSequence(); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1945 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1946 | |
| 1947 | void CodeGenerator::GenerateReturnSequence() { |
| 1948 | if (FLAG_trace) { |
| 1949 | // Push the return value on the stack as the parameter. |
| 1950 | // Runtime::TraceExit returns the parameter as it is. |
| 1951 | frame_->EmitPush(r0); |
| 1952 | frame_->CallRuntime(Runtime::kTraceExit, 1); |
| 1953 | } |
| 1954 | |
| 1955 | #ifdef DEBUG |
| 1956 | // Add a label for checking the size of the code used for returning. |
| 1957 | Label check_exit_codesize; |
| 1958 | masm_->bind(&check_exit_codesize); |
| 1959 | #endif |
| 1960 | // Make sure that the constant pool is not emitted inside of the return |
| 1961 | // sequence. |
| 1962 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 1963 | // Tear down the frame which will restore the caller's frame pointer and |
| 1964 | // the link register. |
| 1965 | frame_->Exit(); |
| 1966 | |
| 1967 | // Here we use masm_-> instead of the __ macro to avoid the code coverage |
| 1968 | // tool from instrumenting as we rely on the code size here. |
| 1969 | int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; |
| 1970 | masm_->add(sp, sp, Operand(sp_delta)); |
| 1971 | masm_->Jump(lr); |
| 1972 | DeleteFrame(); |
| 1973 | |
| 1974 | #ifdef DEBUG |
| 1975 | // Check that the size of the code used for returning matches what is |
| 1976 | // expected by the debugger. If the sp_delts above cannot be encoded in |
| 1977 | // the add instruction the add will generate two instructions. |
| 1978 | int return_sequence_length = |
| 1979 | masm_->InstructionsGeneratedSince(&check_exit_codesize); |
| 1980 | CHECK(return_sequence_length == |
| 1981 | Assembler::kJSReturnSequenceInstructions || |
| 1982 | return_sequence_length == |
| 1983 | Assembler::kJSReturnSequenceInstructions + 1); |
| 1984 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1985 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1989 | void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1990 | #ifdef DEBUG |
| 1991 | int original_height = frame_->height(); |
| 1992 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | Comment cmnt(masm_, "[ WithEnterStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1994 | CodeForStatementPosition(node); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1995 | Load(node->expression()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1996 | if (node->is_catch_block()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1997 | frame_->CallRuntime(Runtime::kPushCatchContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1998 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1999 | frame_->CallRuntime(Runtime::kPushContext, 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2000 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2001 | #ifdef DEBUG |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2002 | JumpTarget verified_true; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2003 | __ cmp(r0, cp); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2004 | verified_true.Branch(eq); |
| 2005 | __ stop("PushContext: r0 is expected to be the same as cp"); |
| 2006 | verified_true.Bind(); |
| 2007 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2009 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2010 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2014 | void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2015 | #ifdef DEBUG |
| 2016 | int original_height = frame_->height(); |
| 2017 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2018 | Comment cmnt(masm_, "[ WithExitStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2019 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | // Pop context. |
| 2021 | __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); |
| 2022 | // Update context local. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2023 | __ str(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2024 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2028 | void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2029 | #ifdef DEBUG |
| 2030 | int original_height = frame_->height(); |
| 2031 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | Comment cmnt(masm_, "[ SwitchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2033 | CodeForStatementPosition(node); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2034 | node->break_target()->SetExpectedHeight(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2035 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2036 | Load(node->tag()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2037 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2038 | JumpTarget next_test; |
| 2039 | JumpTarget fall_through; |
| 2040 | JumpTarget default_entry; |
| 2041 | JumpTarget default_exit(JumpTarget::BIDIRECTIONAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2042 | ZoneList<CaseClause*>* cases = node->cases(); |
| 2043 | int length = cases->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2044 | CaseClause* default_clause = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | |
| 2046 | for (int i = 0; i < length; i++) { |
| 2047 | CaseClause* clause = cases->at(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2048 | if (clause->is_default()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2049 | // Remember the default clause and compile it at the end. |
| 2050 | default_clause = clause; |
| 2051 | continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2054 | Comment cmnt(masm_, "[ Case clause"); |
| 2055 | // Compile the test. |
| 2056 | next_test.Bind(); |
| 2057 | next_test.Unuse(); |
| 2058 | // Duplicate TOS. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2059 | frame_->Dup(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2060 | Comparison(eq, NULL, clause->label(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2061 | Branch(false, &next_test); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2062 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2063 | // Before entering the body from the test, remove the switch value from |
| 2064 | // the stack. |
| 2065 | frame_->Drop(); |
| 2066 | |
| 2067 | // Label the body so that fall through is enabled. |
| 2068 | if (i > 0 && cases->at(i - 1)->is_default()) { |
| 2069 | default_exit.Bind(); |
| 2070 | } else { |
| 2071 | fall_through.Bind(); |
| 2072 | fall_through.Unuse(); |
| 2073 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2074 | VisitStatements(clause->statements()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2075 | |
| 2076 | // If control flow can fall through from the body, jump to the next body |
| 2077 | // or the end of the statement. |
| 2078 | if (frame_ != NULL) { |
| 2079 | if (i < length - 1 && cases->at(i + 1)->is_default()) { |
| 2080 | default_entry.Jump(); |
| 2081 | } else { |
| 2082 | fall_through.Jump(); |
| 2083 | } |
| 2084 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2087 | // The final "test" removes the switch value. |
| 2088 | next_test.Bind(); |
| 2089 | frame_->Drop(); |
| 2090 | |
| 2091 | // If there is a default clause, compile it. |
| 2092 | if (default_clause != NULL) { |
| 2093 | Comment cmnt(masm_, "[ Default clause"); |
| 2094 | default_entry.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2095 | VisitStatements(default_clause->statements()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2096 | // If control flow can fall out of the default and there is a case after |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2097 | // it, jump to that case's body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2098 | if (frame_ != NULL && default_exit.is_bound()) { |
| 2099 | default_exit.Jump(); |
| 2100 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2101 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2102 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2103 | if (fall_through.is_linked()) { |
| 2104 | fall_through.Bind(); |
| 2105 | } |
| 2106 | |
| 2107 | if (node->break_target()->is_linked()) { |
| 2108 | node->break_target()->Bind(); |
| 2109 | } |
| 2110 | node->break_target()->Unuse(); |
| 2111 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2115 | void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2116 | #ifdef DEBUG |
| 2117 | int original_height = frame_->height(); |
| 2118 | #endif |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2119 | Comment cmnt(masm_, "[ DoWhileStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2120 | CodeForStatementPosition(node); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2121 | node->break_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2122 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2123 | IncrementLoopNesting(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2124 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2125 | // Label the top of the loop for the backward CFG edge. If the test |
| 2126 | // is always true we can use the continue target, and if the test is |
| 2127 | // always false there is no need. |
| 2128 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2129 | switch (info) { |
| 2130 | case ALWAYS_TRUE: |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2131 | node->continue_target()->SetExpectedHeight(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2132 | node->continue_target()->Bind(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2133 | break; |
| 2134 | case ALWAYS_FALSE: |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2135 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2136 | break; |
| 2137 | case DONT_KNOW: |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2138 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2139 | body.Bind(); |
| 2140 | break; |
| 2141 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2142 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2143 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2144 | Visit(node->body()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2145 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2146 | // Compile the test. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2147 | switch (info) { |
| 2148 | case ALWAYS_TRUE: |
| 2149 | // If control can fall off the end of the body, jump back to the |
| 2150 | // top. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2151 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2152 | node->continue_target()->Jump(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2153 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2154 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2155 | case ALWAYS_FALSE: |
| 2156 | // If we have a continue in the body, we only have to bind its |
| 2157 | // jump target. |
| 2158 | if (node->continue_target()->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2159 | node->continue_target()->Bind(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2160 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | case DONT_KNOW: |
| 2163 | // We have to compile the test expression if it can be reached by |
| 2164 | // control flow falling out of the body or via continue. |
| 2165 | if (node->continue_target()->is_linked()) { |
| 2166 | node->continue_target()->Bind(); |
| 2167 | } |
| 2168 | if (has_valid_frame()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2169 | Comment cmnt(masm_, "[ DoWhileCondition"); |
| 2170 | CodeForDoWhileConditionPosition(node); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2171 | LoadCondition(node->cond(), &body, node->break_target(), true); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2172 | if (has_valid_frame()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2173 | // A invalid frame here indicates that control did not |
| 2174 | // fall out of the test expression. |
| 2175 | Branch(true, &body); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2178 | break; |
| 2179 | } |
| 2180 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2181 | if (node->break_target()->is_linked()) { |
| 2182 | node->break_target()->Bind(); |
| 2183 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2184 | DecrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2185 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | void CodeGenerator::VisitWhileStatement(WhileStatement* node) { |
| 2190 | #ifdef DEBUG |
| 2191 | int original_height = frame_->height(); |
| 2192 | #endif |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2193 | Comment cmnt(masm_, "[ WhileStatement"); |
| 2194 | CodeForStatementPosition(node); |
| 2195 | |
| 2196 | // If the test is never true and has no side effects there is no need |
| 2197 | // to compile the test or body. |
| 2198 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2199 | if (info == ALWAYS_FALSE) return; |
| 2200 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2201 | node->break_target()->SetExpectedHeight(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2202 | IncrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2203 | |
| 2204 | // Label the top of the loop with the continue target for the backward |
| 2205 | // CFG edge. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2206 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2207 | node->continue_target()->Bind(); |
| 2208 | |
| 2209 | if (info == DONT_KNOW) { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2210 | JumpTarget body(JumpTarget::BIDIRECTIONAL); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2211 | LoadCondition(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2212 | if (has_valid_frame()) { |
| 2213 | // A NULL frame indicates that control did not fall out of the |
| 2214 | // test expression. |
| 2215 | Branch(false, node->break_target()); |
| 2216 | } |
| 2217 | if (has_valid_frame() || body.is_linked()) { |
| 2218 | body.Bind(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | if (has_valid_frame()) { |
| 2223 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2224 | Visit(node->body()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2225 | |
| 2226 | // If control flow can fall out of the body, jump back to the top. |
| 2227 | if (has_valid_frame()) { |
| 2228 | node->continue_target()->Jump(); |
| 2229 | } |
| 2230 | } |
| 2231 | if (node->break_target()->is_linked()) { |
| 2232 | node->break_target()->Bind(); |
| 2233 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2234 | DecrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2235 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
| 2236 | } |
| 2237 | |
| 2238 | |
| 2239 | void CodeGenerator::VisitForStatement(ForStatement* node) { |
| 2240 | #ifdef DEBUG |
| 2241 | int original_height = frame_->height(); |
| 2242 | #endif |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2243 | Comment cmnt(masm_, "[ ForStatement"); |
| 2244 | CodeForStatementPosition(node); |
| 2245 | if (node->init() != NULL) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2246 | Visit(node->init()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | // If the test is never true there is no need to compile the test or |
| 2250 | // body. |
| 2251 | ConditionAnalysis info = AnalyzeCondition(node->cond()); |
| 2252 | if (info == ALWAYS_FALSE) return; |
| 2253 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2254 | node->break_target()->SetExpectedHeight(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2255 | IncrementLoopNesting(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2256 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2257 | // We know that the loop index is a smi if it is not modified in the |
| 2258 | // loop body and it is checked against a constant limit in the loop |
| 2259 | // condition. In this case, we reset the static type information of the |
| 2260 | // loop index to smi before compiling the body, the update expression, and |
| 2261 | // the bottom check of the loop condition. |
| 2262 | TypeInfoCodeGenState type_info_scope(this, |
| 2263 | node->is_fast_smi_loop() ? |
| 2264 | node->loop_variable()->slot() : |
| 2265 | NULL, |
| 2266 | TypeInfo::Smi()); |
| 2267 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2268 | // If there is no update statement, label the top of the loop with the |
| 2269 | // continue target, otherwise with the loop target. |
| 2270 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2271 | if (node->next() == NULL) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2272 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2273 | node->continue_target()->Bind(); |
| 2274 | } else { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2275 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2276 | loop.Bind(); |
| 2277 | } |
| 2278 | |
| 2279 | // If the test is always true, there is no need to compile it. |
| 2280 | if (info == DONT_KNOW) { |
| 2281 | JumpTarget body; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2282 | LoadCondition(node->cond(), &body, node->break_target(), true); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2283 | if (has_valid_frame()) { |
| 2284 | Branch(false, node->break_target()); |
| 2285 | } |
| 2286 | if (has_valid_frame() || body.is_linked()) { |
| 2287 | body.Bind(); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | if (has_valid_frame()) { |
| 2292 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2293 | Visit(node->body()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (node->next() == NULL) { |
| 2296 | // If there is no update statement and control flow can fall out |
| 2297 | // of the loop, jump directly to the continue label. |
| 2298 | if (has_valid_frame()) { |
| 2299 | node->continue_target()->Jump(); |
| 2300 | } |
| 2301 | } else { |
| 2302 | // If there is an update statement and control flow can reach it |
| 2303 | // via falling out of the body of the loop or continuing, we |
| 2304 | // compile the update statement. |
| 2305 | if (node->continue_target()->is_linked()) { |
| 2306 | node->continue_target()->Bind(); |
| 2307 | } |
| 2308 | if (has_valid_frame()) { |
| 2309 | // Record source position of the statement as this code which is |
| 2310 | // after the code for the body actually belongs to the loop |
| 2311 | // statement and not the body. |
| 2312 | CodeForStatementPosition(node); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2313 | Visit(node->next()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2314 | loop.Jump(); |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | if (node->break_target()->is_linked()) { |
| 2319 | node->break_target()->Bind(); |
| 2320 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2321 | DecrementLoopNesting(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2322 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2326 | void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2327 | #ifdef DEBUG |
| 2328 | int original_height = frame_->height(); |
| 2329 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2330 | Comment cmnt(masm_, "[ ForInStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2331 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2332 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2333 | JumpTarget primitive; |
| 2334 | JumpTarget jsobject; |
| 2335 | JumpTarget fixed_array; |
| 2336 | JumpTarget entry(JumpTarget::BIDIRECTIONAL); |
| 2337 | JumpTarget end_del_check; |
| 2338 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Get the object to enumerate over (converted to JSObject). |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2341 | Load(node->enumerable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2342 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2343 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2344 | // Both SpiderMonkey and kjs ignore null and undefined in contrast |
| 2345 | // to the specification. 12.6.4 mandates a call to ToObject. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2346 | frame_->EmitPop(r0); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2347 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 2348 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2349 | exit.Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2350 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2351 | __ cmp(r0, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2352 | exit.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2353 | |
| 2354 | // Stack layout in body: |
| 2355 | // [iteration counter (Smi)] |
| 2356 | // [length of array] |
| 2357 | // [FixedArray] |
| 2358 | // [Map or 0] |
| 2359 | // [Object] |
| 2360 | |
| 2361 | // Check if enumerable is already a JSObject |
| 2362 | __ tst(r0, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2363 | primitive.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2364 | __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2365 | jsobject.Branch(hs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2366 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2367 | primitive.Bind(); |
| 2368 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2369 | frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2370 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2371 | jsobject.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2372 | // Get the set of properties (as a FixedArray or Map). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2373 | // r0: value to be iterated over |
| 2374 | frame_->EmitPush(r0); // Push the object being iterated over. |
| 2375 | |
| 2376 | // Check cache validity in generated code. This is a fast case for |
| 2377 | // the JSObject::IsSimpleEnum cache validity checks. If we cannot |
| 2378 | // guarantee cache validity, call the runtime system to check cache |
| 2379 | // validity or get the property names in a fixed array. |
| 2380 | JumpTarget call_runtime; |
| 2381 | JumpTarget loop(JumpTarget::BIDIRECTIONAL); |
| 2382 | JumpTarget check_prototype; |
| 2383 | JumpTarget use_cache; |
| 2384 | __ mov(r1, Operand(r0)); |
| 2385 | loop.Bind(); |
| 2386 | // Check that there are no elements. |
| 2387 | __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset)); |
| 2388 | __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex); |
| 2389 | __ cmp(r2, r4); |
| 2390 | call_runtime.Branch(ne); |
| 2391 | // Check that instance descriptors are not empty so that we can |
| 2392 | // check for an enum cache. Leave the map in r3 for the subsequent |
| 2393 | // prototype load. |
| 2394 | __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2395 | __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset)); |
| 2396 | __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex); |
| 2397 | __ cmp(r2, ip); |
| 2398 | call_runtime.Branch(eq); |
| 2399 | // Check that there in an enum cache in the non-empty instance |
| 2400 | // descriptors. This is the case if the next enumeration index |
| 2401 | // field does not contain a smi. |
| 2402 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset)); |
| 2403 | __ tst(r2, Operand(kSmiTagMask)); |
| 2404 | call_runtime.Branch(eq); |
| 2405 | // For all objects but the receiver, check that the cache is empty. |
| 2406 | // r4: empty fixed array root. |
| 2407 | __ cmp(r1, r0); |
| 2408 | check_prototype.Branch(eq); |
| 2409 | __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2410 | __ cmp(r2, r4); |
| 2411 | call_runtime.Branch(ne); |
| 2412 | check_prototype.Bind(); |
| 2413 | // Load the prototype from the map and loop if non-null. |
| 2414 | __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 2415 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 2416 | __ cmp(r1, ip); |
| 2417 | loop.Branch(ne); |
| 2418 | // The enum cache is valid. Load the map of the object being |
| 2419 | // iterated over and use the cache for the iteration. |
| 2420 | __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 2421 | use_cache.Jump(); |
| 2422 | |
| 2423 | call_runtime.Bind(); |
| 2424 | // Call the runtime to get the property names for the object. |
| 2425 | frame_->EmitPush(r0); // push the object (slot 4) for the runtime call |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2426 | frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2427 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2428 | // If we got a map from the runtime call, we can do a fast |
| 2429 | // modification check. Otherwise, we got a fixed array, and we have |
| 2430 | // to do a slow check. |
| 2431 | // r0: map or fixed array (result from call to |
| 2432 | // Runtime::kGetPropertyNamesFast) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2433 | __ mov(r2, Operand(r0)); |
| 2434 | __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2435 | __ LoadRoot(ip, Heap::kMetaMapRootIndex); |
| 2436 | __ cmp(r1, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2437 | fixed_array.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2439 | use_cache.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2440 | // Get enum cache |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2441 | // r0: map (either the result from a call to |
| 2442 | // Runtime::kGetPropertyNamesFast or has been fetched directly from |
| 2443 | // the object) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2444 | __ mov(r1, Operand(r0)); |
| 2445 | __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset)); |
| 2446 | __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset)); |
| 2447 | __ ldr(r2, |
| 2448 | FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset)); |
| 2449 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2450 | frame_->EmitPush(r0); // map |
| 2451 | frame_->EmitPush(r2); // enum cache bridge cache |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2452 | __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2453 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2454 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2455 | frame_->EmitPush(r0); |
| 2456 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2457 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2458 | fixed_array.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2459 | __ mov(r1, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2460 | frame_->EmitPush(r1); // insert 0 in place of Map |
| 2461 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2462 | |
| 2463 | // 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] | 2464 | __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2465 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2466 | __ mov(r0, Operand(Smi::FromInt(0))); // init index |
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 | |
| 2469 | // Condition. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2470 | entry.Bind(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2471 | // sp[0] : index |
| 2472 | // sp[1] : array/enum cache length |
| 2473 | // sp[2] : array or enum cache |
| 2474 | // sp[3] : 0 or map |
| 2475 | // sp[4] : enumerable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2476 | // Grab the current frame's height for the break and continue |
| 2477 | // targets only after all the state is pushed on the frame. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2478 | node->break_target()->SetExpectedHeight(); |
| 2479 | node->continue_target()->SetExpectedHeight(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2480 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2481 | // Load the current count to r0, load the length to r1. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 2482 | __ Ldrd(r0, r1, frame_->ElementAt(0)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2483 | __ cmp(r0, r1); // compare to the array length |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2484 | node->break_target()->Branch(hs); |
| 2485 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2486 | // Get the i'th entry of the array. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2487 | __ ldr(r2, frame_->ElementAt(2)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2488 | __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 2489 | __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 2490 | |
| 2491 | // Get Map or 0. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2492 | __ ldr(r2, frame_->ElementAt(3)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2493 | // Check if this (still) matches the map of the enumerable. |
| 2494 | // If not, we have to filter the key. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2495 | __ ldr(r1, frame_->ElementAt(4)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2496 | __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 2497 | __ cmp(r1, Operand(r2)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2498 | end_del_check.Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2499 | |
| 2500 | // 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] | 2501 | __ ldr(r0, frame_->ElementAt(4)); // push enumerable |
| 2502 | frame_->EmitPush(r0); |
| 2503 | frame_->EmitPush(r3); // push entry |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 2504 | frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2505 | __ mov(r3, Operand(r0), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2506 | // If the property has been removed while iterating, we just skip it. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2507 | node->continue_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2508 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2509 | end_del_check.Bind(); |
| 2510 | // Store the entry in the 'each' expression and take another spin in the |
| 2511 | // loop. r3: i'th entry of the enum cache (or string there of) |
| 2512 | frame_->EmitPush(r3); // push entry |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2513 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2514 | Reference each(this, node->each()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2515 | if (!each.is_illegal()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2516 | if (each.size() > 0) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2517 | // Loading a reference may leave the frame in an unspilled state. |
| 2518 | frame_->SpillAll(); // Sync stack to memory. |
| 2519 | // Get the value (under the reference on the stack) from memory. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2520 | __ ldr(r0, frame_->ElementAt(each.size())); |
| 2521 | frame_->EmitPush(r0); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2522 | each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2523 | frame_->Drop(2); // The result of the set and the extra pushed value. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2524 | } else { |
| 2525 | // If the reference was to a slot we rely on the convenient property |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2526 | // that it doesn't matter whether a value (eg, ebx pushed above) is |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2527 | // right on top of or right underneath a zero-sized reference. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2528 | each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2529 | frame_->Drop(1); // Drop the result of the set operation. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2530 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2531 | } |
| 2532 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2533 | // Body. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2534 | CheckStack(); // TODO(1222600): ignore if body contains calls. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2535 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2536 | Visit(node->body()); |
| 2537 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2538 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2539 | // Next. Reestablish a spilled frame in case we are coming here via |
| 2540 | // a continue in the body. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2541 | node->continue_target()->Bind(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2542 | frame_->SpillAll(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2543 | frame_->EmitPop(r0); |
| 2544 | __ add(r0, r0, Operand(Smi::FromInt(1))); |
| 2545 | frame_->EmitPush(r0); |
| 2546 | entry.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2547 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2548 | // Cleanup. No need to spill because VirtualFrame::Drop is safe for |
| 2549 | // any frame. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2550 | node->break_target()->Bind(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2551 | frame_->Drop(5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Exit. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2554 | exit.Bind(); |
| 2555 | node->continue_target()->Unuse(); |
| 2556 | node->break_target()->Unuse(); |
| 2557 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2561 | void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2562 | #ifdef DEBUG |
| 2563 | int original_height = frame_->height(); |
| 2564 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2565 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2566 | Comment cmnt(masm_, "[ TryCatchStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2567 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2568 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2569 | JumpTarget try_block; |
| 2570 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2571 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2572 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2573 | // --- Catch block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2574 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Store the caught exception in the catch variable. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2577 | Variable* catch_var = node->catch_var()->var(); |
| 2578 | ASSERT(catch_var != NULL && catch_var->slot() != NULL); |
| 2579 | StoreToSlot(catch_var->slot(), NOT_CONST_INIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2580 | |
| 2581 | // Remove the exception from the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2582 | frame_->Drop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2583 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2584 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2585 | VisitStatements(node->catch_block()->statements()); |
| 2586 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2587 | if (frame_ != NULL) { |
| 2588 | exit.Jump(); |
| 2589 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2590 | |
| 2591 | |
| 2592 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2593 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2594 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2595 | frame_->PushTryHandler(TRY_CATCH_HANDLER); |
| 2596 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2597 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2598 | // Shadow the labels for all escapes from the try block, including |
| 2599 | // returns. During shadowing, the original label is hidden as the |
| 2600 | // LabelShadow and operations on the original actually affect the |
| 2601 | // shadowing label. |
| 2602 | // |
| 2603 | // We should probably try to unify the escaping labels and the return |
| 2604 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2605 | int nof_escapes = node->escaping_targets()->length(); |
| 2606 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2607 | |
| 2608 | // Add the shadow target for the function return. |
| 2609 | static const int kReturnShadowIndex = 0; |
| 2610 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2611 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2612 | function_return_is_shadowed_ = true; |
| 2613 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2614 | |
| 2615 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2616 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2617 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | // Generate code for the statements in the try block. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2621 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2622 | VisitStatements(node->try_block()->statements()); |
| 2623 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Stop the introduced shadowing and count the number of required unlinks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2626 | // After shadowing stops, the original labels are unshadowed and the |
| 2627 | // LabelShadows represent the formerly shadowing labels. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2628 | bool has_unlinks = false; |
| 2629 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2630 | shadows[i]->StopShadowing(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2631 | has_unlinks = has_unlinks || shadows[i]->is_linked(); |
| 2632 | } |
| 2633 | function_return_is_shadowed_ = function_return_was_shadowed; |
| 2634 | |
| 2635 | // Get an external reference to the handler address. |
| 2636 | ExternalReference handler_address(Top::k_handler_address); |
| 2637 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2638 | // If we can fall off the end of the try block, unlink from try chain. |
| 2639 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2640 | // The next handler address is on top of the frame. Unlink from |
| 2641 | // the handler list and drop the rest of this handler from the |
| 2642 | // frame. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 2643 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2644 | frame_->EmitPop(r1); // r0 can contain the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2645 | __ mov(r3, Operand(handler_address)); |
| 2646 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2647 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2648 | if (has_unlinks) { |
| 2649 | exit.Jump(); |
| 2650 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2653 | // Generate unlink code for the (formerly) shadowing labels that have been |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2654 | // jumped to. Deallocate each shadow target. |
| 2655 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2656 | if (shadows[i]->is_linked()) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2657 | // Unlink from try chain; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2658 | shadows[i]->Bind(); |
| 2659 | // Because we can be jumping here (to spilled code) from unspilled |
| 2660 | // code, we need to reestablish a spilled frame at this block. |
| 2661 | frame_->SpillAll(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2662 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2663 | // Reload sp from the top handler, because some statements that we |
| 2664 | // 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] | 2665 | __ mov(r3, Operand(handler_address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2667 | frame_->Forget(frame_->height() - handler_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2668 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 2669 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2670 | frame_->EmitPop(r1); // r0 can contain the return value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 2672 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2673 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2674 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
| 2675 | frame_->PrepareForReturn(); |
| 2676 | } |
| 2677 | shadows[i]->other_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2681 | exit.Bind(); |
| 2682 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2686 | void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2687 | #ifdef DEBUG |
| 2688 | int original_height = frame_->height(); |
| 2689 | #endif |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2690 | VirtualFrame::SpilledScope spilled_scope(frame_); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2691 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2692 | CodeForStatementPosition(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2693 | |
| 2694 | // State: Used to keep track of reason for entering the finally |
| 2695 | // block. Should probably be extended to hold information for |
| 2696 | // break/continue from within the try block. |
| 2697 | enum { FALLING, THROWING, JUMPING }; |
| 2698 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2699 | JumpTarget try_block; |
| 2700 | JumpTarget finally_block; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2701 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2702 | try_block.Call(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2703 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2704 | frame_->EmitPush(r0); // save exception object on the stack |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2705 | // In case of thrown exceptions, this is where we continue. |
| 2706 | __ mov(r2, Operand(Smi::FromInt(THROWING))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2707 | finally_block.Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2708 | |
| 2709 | // --- Try block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2710 | try_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2711 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2712 | frame_->PushTryHandler(TRY_FINALLY_HANDLER); |
| 2713 | int handler_height = frame_->height(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2714 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2715 | // Shadow the labels for all escapes from the try block, including |
| 2716 | // returns. Shadowing hides the original label as the LabelShadow and |
| 2717 | // operations on the original actually affect the shadowing label. |
| 2718 | // |
| 2719 | // We should probably try to unify the escaping labels and the return |
| 2720 | // label. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2721 | int nof_escapes = node->escaping_targets()->length(); |
| 2722 | List<ShadowTarget*> shadows(1 + nof_escapes); |
| 2723 | |
| 2724 | // Add the shadow target for the function return. |
| 2725 | static const int kReturnShadowIndex = 0; |
| 2726 | shadows.Add(new ShadowTarget(&function_return_)); |
| 2727 | bool function_return_was_shadowed = function_return_is_shadowed_; |
| 2728 | function_return_is_shadowed_ = true; |
| 2729 | ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_); |
| 2730 | |
| 2731 | // Add the remaining shadow targets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2732 | for (int i = 0; i < nof_escapes; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2733 | shadows.Add(new ShadowTarget(node->escaping_targets()->at(i))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | // Generate code for the statements in the try block. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2737 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2738 | VisitStatements(node->try_block()->statements()); |
| 2739 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2740 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2741 | // Stop the introduced shadowing and count the number of required unlinks. |
| 2742 | // After shadowing stops, the original labels are unshadowed and the |
| 2743 | // LabelShadows represent the formerly shadowing labels. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2744 | int nof_unlinks = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2745 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2746 | shadows[i]->StopShadowing(); |
| 2747 | if (shadows[i]->is_linked()) nof_unlinks++; |
| 2748 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2749 | function_return_is_shadowed_ = function_return_was_shadowed; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2750 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2751 | // Get an external reference to the handler address. |
| 2752 | ExternalReference handler_address(Top::k_handler_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2753 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2754 | // If we can fall off the end of the try block, unlink from the try |
| 2755 | // chain and set the state on the frame to FALLING. |
| 2756 | if (has_valid_frame()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2757 | // The next handler address is on top of the frame. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 2758 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2759 | frame_->EmitPop(r1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2760 | __ mov(r3, Operand(handler_address)); |
| 2761 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2762 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2763 | |
| 2764 | // Fake a top of stack value (unneeded when FALLING) and set the |
| 2765 | // state in r2, then jump around the unlink blocks if any. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2766 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2767 | frame_->EmitPush(r0); |
| 2768 | __ mov(r2, Operand(Smi::FromInt(FALLING))); |
| 2769 | if (nof_unlinks > 0) { |
| 2770 | finally_block.Jump(); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | // Generate code to unlink and set the state for the (formerly) |
| 2775 | // shadowing targets that have been jumped to. |
| 2776 | for (int i = 0; i < shadows.length(); i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2777 | if (shadows[i]->is_linked()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2778 | // If we have come from the shadowed return, the return value is |
| 2779 | // in (a non-refcounted reference to) r0. We must preserve it |
| 2780 | // until it is pushed. |
| 2781 | // |
| 2782 | // Because we can be jumping here (to spilled code) from |
| 2783 | // unspilled code, we need to reestablish a spilled frame at |
| 2784 | // this block. |
| 2785 | shadows[i]->Bind(); |
| 2786 | frame_->SpillAll(); |
| 2787 | |
| 2788 | // Reload sp from the top handler, because some statements that |
| 2789 | // we break from (eg, for...in) may have left stuff on the |
| 2790 | // stack. |
| 2791 | __ mov(r3, Operand(handler_address)); |
| 2792 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2793 | frame_->Forget(frame_->height() - handler_height); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2794 | |
| 2795 | // Unlink this handler and drop it from the frame. The next |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2796 | // handler address is currently on top of the frame. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 2797 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2798 | frame_->EmitPop(r1); |
| 2799 | __ str(r1, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 2800 | frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2801 | |
| 2802 | if (i == kReturnShadowIndex) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2803 | // If this label shadowed the function return, materialize the |
| 2804 | // return value on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2805 | frame_->EmitPush(r0); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2806 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2807 | // Fake TOS for targets that shadowed breaks and continues. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 2808 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2809 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2810 | } |
| 2811 | __ mov(r2, Operand(Smi::FromInt(JUMPING + i))); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2812 | if (--nof_unlinks > 0) { |
| 2813 | // If this is not the last unlink block, jump around the next. |
| 2814 | finally_block.Jump(); |
| 2815 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2819 | // --- Finally block --- |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2820 | finally_block.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2821 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2822 | // Push the state on the stack. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2823 | frame_->EmitPush(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2824 | |
| 2825 | // We keep two elements on the stack - the (possibly faked) result |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2826 | // and the state - while evaluating the finally block. |
| 2827 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2828 | // Generate code for the statements in the finally block. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 2829 | { VirtualFrame::RegisterAllocationScope scope(this); |
| 2830 | VisitStatements(node->finally_block()->statements()); |
| 2831 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2832 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2833 | if (has_valid_frame()) { |
| 2834 | // Restore state and return value or faked TOS. |
| 2835 | frame_->EmitPop(r2); |
| 2836 | frame_->EmitPop(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2839 | // Generate code to jump to the right destination for all used |
| 2840 | // formerly shadowing targets. Deallocate each shadow target. |
| 2841 | for (int i = 0; i < shadows.length(); i++) { |
| 2842 | if (has_valid_frame() && shadows[i]->is_bound()) { |
| 2843 | JumpTarget* original = shadows[i]->other_target(); |
| 2844 | __ cmp(r2, Operand(Smi::FromInt(JUMPING + i))); |
| 2845 | if (!function_return_is_shadowed_ && i == kReturnShadowIndex) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2846 | JumpTarget skip; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2847 | skip.Branch(ne); |
| 2848 | frame_->PrepareForReturn(); |
| 2849 | original->Jump(); |
| 2850 | skip.Bind(); |
| 2851 | } else { |
| 2852 | original->Branch(eq); |
| 2853 | } |
| 2854 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2855 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2856 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2857 | if (has_valid_frame()) { |
| 2858 | // Check if we need to rethrow the exception. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2859 | JumpTarget exit; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2860 | __ cmp(r2, Operand(Smi::FromInt(THROWING))); |
| 2861 | exit.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2862 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2863 | // Rethrow exception. |
| 2864 | frame_->EmitPush(r0); |
| 2865 | frame_->CallRuntime(Runtime::kReThrow, 1); |
| 2866 | |
| 2867 | // Done. |
| 2868 | exit.Bind(); |
| 2869 | } |
| 2870 | ASSERT(!has_valid_frame() || frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2874 | void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2875 | #ifdef DEBUG |
| 2876 | int original_height = frame_->height(); |
| 2877 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2878 | Comment cmnt(masm_, "[ DebuggerStatament"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2879 | CodeForStatementPosition(node); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2880 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2881 | frame_->DebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2882 | #endif |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2883 | // Ignore the return value. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2884 | ASSERT(frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2888 | void CodeGenerator::InstantiateFunction( |
| 2889 | Handle<SharedFunctionInfo> function_info) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2890 | // Use the fast case closure allocation code that allocates in new |
| 2891 | // space for nested functions that don't need literals cloning. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2892 | if (scope()->is_function_scope() && function_info->num_literals() == 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2893 | FastNewClosureStub stub; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2894 | frame_->EmitPush(Operand(function_info)); |
| 2895 | frame_->SpillAll(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2896 | frame_->CallStub(&stub, 1); |
| 2897 | frame_->EmitPush(r0); |
| 2898 | } else { |
| 2899 | // Create a new closure. |
| 2900 | frame_->EmitPush(cp); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2901 | frame_->EmitPush(Operand(function_info)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2902 | frame_->CallRuntime(Runtime::kNewClosure, 2); |
| 2903 | frame_->EmitPush(r0); |
| 2904 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2908 | void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2909 | #ifdef DEBUG |
| 2910 | int original_height = frame_->height(); |
| 2911 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2912 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 2913 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2914 | // Build the function info and instantiate it. |
| 2915 | Handle<SharedFunctionInfo> function_info = |
| 2916 | Compiler::BuildFunctionInfo(node, script(), this); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 2917 | // Check for stack-overflow exception. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2918 | if (HasStackOverflow()) { |
| 2919 | ASSERT(frame_->height() == original_height); |
| 2920 | return; |
| 2921 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2922 | InstantiateFunction(function_info); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 2923 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2927 | void CodeGenerator::VisitSharedFunctionInfoLiteral( |
| 2928 | SharedFunctionInfoLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2929 | #ifdef DEBUG |
| 2930 | int original_height = frame_->height(); |
| 2931 | #endif |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 2932 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 2933 | InstantiateFunction(node->shared_function_info()); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 2934 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2938 | void CodeGenerator::VisitConditional(Conditional* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2939 | #ifdef DEBUG |
| 2940 | int original_height = frame_->height(); |
| 2941 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2942 | Comment cmnt(masm_, "[ Conditional"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2943 | JumpTarget then; |
| 2944 | JumpTarget else_; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2945 | LoadCondition(node->condition(), &then, &else_, true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2946 | if (has_valid_frame()) { |
| 2947 | Branch(false, &else_); |
| 2948 | } |
| 2949 | if (has_valid_frame() || then.is_linked()) { |
| 2950 | then.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2951 | Load(node->then_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2952 | } |
| 2953 | if (else_.is_linked()) { |
| 2954 | JumpTarget exit; |
| 2955 | if (has_valid_frame()) exit.Jump(); |
| 2956 | else_.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2957 | Load(node->else_expression()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2958 | if (exit.is_linked()) exit.Bind(); |
| 2959 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 2960 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2964 | void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) { |
| 2965 | if (slot->type() == Slot::LOOKUP) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2966 | ASSERT(slot->var()->is_dynamic()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2967 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 2968 | // JumpTargets do not yet support merging frames so the frame must be |
| 2969 | // spilled when jumping to these targets. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2970 | JumpTarget slow; |
| 2971 | JumpTarget done; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2972 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 2973 | // Generate fast case for loading from slots that correspond to |
| 2974 | // local/global variables or arguments unless they are shadowed by |
| 2975 | // eval-introduced bindings. |
| 2976 | EmitDynamicLoadFromSlotFastCase(slot, |
| 2977 | typeof_state, |
| 2978 | &slow, |
| 2979 | &done); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2980 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2981 | slow.Bind(); |
| 2982 | frame_->EmitPush(cp); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2983 | frame_->EmitPush(Operand(slot->var()->name())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2984 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2985 | if (typeof_state == INSIDE_TYPEOF) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2986 | frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2987 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2988 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2989 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 2990 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2991 | done.Bind(); |
| 2992 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2993 | |
| 2994 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2995 | Register scratch = VirtualFrame::scratch0(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2996 | TypeInfo info = type_info(slot); |
| 2997 | frame_->EmitPush(SlotOperand(slot, scratch), info); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2998 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2999 | if (slot->var()->mode() == Variable::CONST) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3000 | // Const slots may contain 'the hole' value (the constant hasn't been |
| 3001 | // initialized yet) which needs to be converted into the 'undefined' |
| 3002 | // value. |
| 3003 | Comment cmnt(masm_, "[ Unhole const"); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3004 | Register tos = frame_->PopToRegister(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3005 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3006 | __ cmp(tos, ip); |
| 3007 | __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq); |
| 3008 | frame_->EmitPush(tos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3014 | void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot, |
| 3015 | TypeofState state) { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3016 | VirtualFrame::RegisterAllocationScope scope(this); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3017 | LoadFromSlot(slot, state); |
| 3018 | |
| 3019 | // Bail out quickly if we're not using lazy arguments allocation. |
| 3020 | if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return; |
| 3021 | |
| 3022 | // ... or if the slot isn't a non-parameter arguments slot. |
| 3023 | if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return; |
| 3024 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3025 | // Load the loaded value from the stack into a register but leave it on the |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3026 | // stack. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3027 | Register tos = frame_->Peek(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3028 | |
| 3029 | // If the loaded value is the sentinel that indicates that we |
| 3030 | // haven't loaded the arguments object yet, we need to do it now. |
| 3031 | JumpTarget exit; |
| 3032 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3033 | __ cmp(tos, ip); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3034 | exit.Branch(ne); |
| 3035 | frame_->Drop(); |
| 3036 | StoreArgumentsObject(false); |
| 3037 | exit.Bind(); |
| 3038 | } |
| 3039 | |
| 3040 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3041 | void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) { |
| 3042 | ASSERT(slot != NULL); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3043 | VirtualFrame::RegisterAllocationScope scope(this); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3044 | if (slot->type() == Slot::LOOKUP) { |
| 3045 | ASSERT(slot->var()->is_dynamic()); |
| 3046 | |
| 3047 | // For now, just do a runtime call. |
| 3048 | frame_->EmitPush(cp); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3049 | frame_->EmitPush(Operand(slot->var()->name())); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3050 | |
| 3051 | if (init_state == CONST_INIT) { |
| 3052 | // Same as the case for a normal store, but ignores attribute |
| 3053 | // (e.g. READ_ONLY) of context slot so that we can initialize |
| 3054 | // const properties (introduced via eval("const foo = (some |
| 3055 | // expr);")). Also, uses the current function context instead of |
| 3056 | // the top context. |
| 3057 | // |
| 3058 | // Note that we must declare the foo upon entry of eval(), via a |
| 3059 | // context slot declaration, but we cannot initialize it at the |
| 3060 | // same time, because the const declaration may be at the end of |
| 3061 | // the eval code (sigh...) and the const variable may have been |
| 3062 | // used before (where its value is 'undefined'). Thus, we can only |
| 3063 | // do the initialization when we actually encounter the expression |
| 3064 | // and when the expression operands are defined and valid, and |
| 3065 | // thus we need the split into 2 operations: declaration of the |
| 3066 | // context slot followed by initialization. |
| 3067 | frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3); |
| 3068 | } else { |
| 3069 | frame_->CallRuntime(Runtime::kStoreContextSlot, 3); |
| 3070 | } |
| 3071 | // Storing a variable must keep the (new) value on the expression |
| 3072 | // stack. This is necessary for compiling assignment expressions. |
| 3073 | frame_->EmitPush(r0); |
| 3074 | |
| 3075 | } else { |
| 3076 | ASSERT(!slot->var()->is_dynamic()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3077 | Register scratch = VirtualFrame::scratch0(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3078 | Register scratch2 = VirtualFrame::scratch1(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3079 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3080 | // The frame must be spilled when branching to this target. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3081 | JumpTarget exit; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3082 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3083 | if (init_state == CONST_INIT) { |
| 3084 | ASSERT(slot->var()->mode() == Variable::CONST); |
| 3085 | // Only the first const initialization must be executed (the slot |
| 3086 | // still contains 'the hole' value). When the assignment is |
| 3087 | // executed, the code is identical to a normal store (see below). |
| 3088 | Comment cmnt(masm_, "[ Init const"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3089 | __ ldr(scratch, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3090 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3091 | __ cmp(scratch, ip); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3092 | exit.Branch(ne); |
| 3093 | } |
| 3094 | |
| 3095 | // We must execute the store. Storing a variable must keep the |
| 3096 | // (new) value on the stack. This is necessary for compiling |
| 3097 | // assignment expressions. |
| 3098 | // |
| 3099 | // Note: We will reach here even with slot->var()->mode() == |
| 3100 | // Variable::CONST because of const declarations which will |
| 3101 | // initialize consts to 'the hole' value and by doing so, end up |
| 3102 | // calling this code. r2 may be loaded with context; used below in |
| 3103 | // RecordWrite. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3104 | Register tos = frame_->Peek(); |
| 3105 | __ str(tos, SlotOperand(slot, scratch)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3106 | if (slot->type() == Slot::CONTEXT) { |
| 3107 | // Skip write barrier if the written value is a smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3108 | __ tst(tos, Operand(kSmiTagMask)); |
| 3109 | // We don't use tos any more after here. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3110 | exit.Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3111 | // scratch is loaded with context when calling SlotOperand above. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3112 | int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3113 | // We need an extra register. Until we have a way to do that in the |
| 3114 | // virtual frame we will cheat and ask for a free TOS register. |
| 3115 | Register scratch3 = frame_->GetTOSRegister(); |
| 3116 | __ RecordWrite(scratch, Operand(offset), scratch2, scratch3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3117 | } |
| 3118 | // If we definitely did not jump over the assignment, we do not need |
| 3119 | // to bind the exit label. Doing so can defeat peephole |
| 3120 | // optimization. |
| 3121 | if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) { |
| 3122 | exit.Bind(); |
| 3123 | } |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3128 | void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 3129 | TypeofState typeof_state, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3130 | JumpTarget* slow) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3131 | // Check that no extension objects have been created by calls to |
| 3132 | // eval from the current scope to the global scope. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 3133 | Register tmp = frame_->scratch0(); |
| 3134 | Register tmp2 = frame_->scratch1(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3135 | Register context = cp; |
| 3136 | Scope* s = scope(); |
| 3137 | while (s != NULL) { |
| 3138 | if (s->num_heap_slots() > 0) { |
| 3139 | if (s->calls_eval()) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 3140 | frame_->SpillAll(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3141 | // Check that extension is NULL. |
| 3142 | __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX)); |
| 3143 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3144 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3145 | } |
| 3146 | // Load next context in chain. |
| 3147 | __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX)); |
| 3148 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 3149 | context = tmp; |
| 3150 | } |
| 3151 | // If no outer scope calls eval, we do not need to check more |
| 3152 | // context extensions. |
| 3153 | if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; |
| 3154 | s = s->outer_scope(); |
| 3155 | } |
| 3156 | |
| 3157 | if (s->is_eval_scope()) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 3158 | frame_->SpillAll(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3159 | Label next, fast; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3160 | __ Move(tmp, context); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3161 | __ bind(&next); |
| 3162 | // Terminate at global context. |
| 3163 | __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3164 | __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex); |
| 3165 | __ cmp(tmp2, ip); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3166 | __ b(eq, &fast); |
| 3167 | // Check that extension is NULL. |
| 3168 | __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX)); |
| 3169 | __ tst(tmp2, tmp2); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3170 | slow->Branch(ne); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3171 | // Load next context in chain. |
| 3172 | __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX)); |
| 3173 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset)); |
| 3174 | __ b(&next); |
| 3175 | __ bind(&fast); |
| 3176 | } |
| 3177 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3178 | // Load the global object. |
| 3179 | LoadGlobal(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3180 | // Setup the name register and call load IC. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3181 | frame_->CallLoadIC(slot->var()->name(), |
| 3182 | typeof_state == INSIDE_TYPEOF |
| 3183 | ? RelocInfo::CODE_TARGET |
| 3184 | : RelocInfo::CODE_TARGET_CONTEXT); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 3188 | void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot, |
| 3189 | TypeofState typeof_state, |
| 3190 | JumpTarget* slow, |
| 3191 | JumpTarget* done) { |
| 3192 | // Generate fast-case code for variables that might be shadowed by |
| 3193 | // eval-introduced variables. Eval is used a lot without |
| 3194 | // introducing variables. In those cases, we do not want to |
| 3195 | // perform a runtime call for all variables in the scope |
| 3196 | // containing the eval. |
| 3197 | if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) { |
| 3198 | LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow); |
| 3199 | frame_->SpillAll(); |
| 3200 | done->Jump(); |
| 3201 | |
| 3202 | } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) { |
| 3203 | frame_->SpillAll(); |
| 3204 | Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot(); |
| 3205 | Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite(); |
| 3206 | if (potential_slot != NULL) { |
| 3207 | // Generate fast case for locals that rewrite to slots. |
| 3208 | __ ldr(r0, |
| 3209 | ContextSlotOperandCheckExtensions(potential_slot, |
| 3210 | r1, |
| 3211 | r2, |
| 3212 | slow)); |
| 3213 | if (potential_slot->var()->mode() == Variable::CONST) { |
| 3214 | __ LoadRoot(ip, Heap::kTheHoleValueRootIndex); |
| 3215 | __ cmp(r0, ip); |
| 3216 | __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq); |
| 3217 | } |
| 3218 | done->Jump(); |
| 3219 | } else if (rewrite != NULL) { |
| 3220 | // Generate fast case for argument loads. |
| 3221 | Property* property = rewrite->AsProperty(); |
| 3222 | if (property != NULL) { |
| 3223 | VariableProxy* obj_proxy = property->obj()->AsVariableProxy(); |
| 3224 | Literal* key_literal = property->key()->AsLiteral(); |
| 3225 | if (obj_proxy != NULL && |
| 3226 | key_literal != NULL && |
| 3227 | obj_proxy->IsArguments() && |
| 3228 | key_literal->handle()->IsSmi()) { |
| 3229 | // Load arguments object if there are no eval-introduced |
| 3230 | // variables. Then load the argument from the arguments |
| 3231 | // object using keyed load. |
| 3232 | __ ldr(r0, |
| 3233 | ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(), |
| 3234 | r1, |
| 3235 | r2, |
| 3236 | slow)); |
| 3237 | frame_->EmitPush(r0); |
| 3238 | __ mov(r1, Operand(key_literal->handle())); |
| 3239 | frame_->EmitPush(r1); |
| 3240 | EmitKeyedLoad(); |
| 3241 | done->Jump(); |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3249 | void CodeGenerator::VisitSlot(Slot* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3250 | #ifdef DEBUG |
| 3251 | int original_height = frame_->height(); |
| 3252 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3253 | Comment cmnt(masm_, "[ Slot"); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 3254 | LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3255 | ASSERT_EQ(original_height + 1, frame_->height()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3256 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3257 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3258 | |
| 3259 | void CodeGenerator::VisitVariableProxy(VariableProxy* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3260 | #ifdef DEBUG |
| 3261 | int original_height = frame_->height(); |
| 3262 | #endif |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3263 | Comment cmnt(masm_, "[ VariableProxy"); |
| 3264 | |
| 3265 | Variable* var = node->var(); |
| 3266 | Expression* expr = var->rewrite(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3267 | if (expr != NULL) { |
| 3268 | Visit(expr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3269 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3270 | ASSERT(var->is_global()); |
| 3271 | Reference ref(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3272 | ref.GetValue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3273 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3274 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3278 | void CodeGenerator::VisitLiteral(Literal* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3279 | #ifdef DEBUG |
| 3280 | int original_height = frame_->height(); |
| 3281 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3282 | Comment cmnt(masm_, "[ Literal"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3283 | Register reg = frame_->GetTOSRegister(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3284 | bool is_smi = node->handle()->IsSmi(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3285 | __ mov(reg, Operand(node->handle())); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3286 | frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown()); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3287 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3291 | void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3292 | #ifdef DEBUG |
| 3293 | int original_height = frame_->height(); |
| 3294 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3295 | Comment cmnt(masm_, "[ RexExp Literal"); |
| 3296 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3297 | Register tmp = VirtualFrame::scratch0(); |
| 3298 | // Free up a TOS register that can be used to push the literal. |
| 3299 | Register literal = frame_->GetTOSRegister(); |
| 3300 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3301 | // Retrieve the literal array and check the allocated entry. |
| 3302 | |
| 3303 | // Load the function of this activation. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3304 | __ ldr(tmp, frame_->Function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3305 | |
| 3306 | // Load the literals array of the function. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3307 | __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3308 | |
| 3309 | // Load the literal at the ast saved index. |
| 3310 | int literal_offset = |
| 3311 | FixedArray::kHeaderSize + node->literal_index() * kPointerSize; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3312 | __ ldr(literal, FieldMemOperand(tmp, literal_offset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3313 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3314 | JumpTarget materialized; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 3315 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3316 | __ cmp(literal, ip); |
| 3317 | // This branch locks the virtual frame at the done label to match the |
| 3318 | // one we have here, where the literal register is not on the stack and |
| 3319 | // nothing is spilled. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3320 | materialized.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3321 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3322 | // 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] | 3323 | // the literal. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3324 | // literal array (0) |
| 3325 | frame_->EmitPush(tmp); |
| 3326 | // literal index (1) |
| 3327 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
| 3328 | // RegExp pattern (2) |
| 3329 | frame_->EmitPush(Operand(node->pattern())); |
| 3330 | // RegExp flags (3) |
| 3331 | frame_->EmitPush(Operand(node->flags())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3332 | frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3333 | __ Move(literal, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3334 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3335 | materialized.Bind(); |
| 3336 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3337 | frame_->EmitPush(literal); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 3338 | int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize; |
| 3339 | frame_->EmitPush(Operand(Smi::FromInt(size))); |
| 3340 | frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1); |
| 3341 | // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime. |
| 3342 | // r0 is newly allocated space. |
| 3343 | |
| 3344 | // Reuse literal variable with (possibly) a new register, still holding |
| 3345 | // the materialized boilerplate. |
| 3346 | literal = frame_->PopToRegister(r0); |
| 3347 | |
| 3348 | __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize); |
| 3349 | |
| 3350 | // Push the clone. |
| 3351 | frame_->EmitPush(r0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3352 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3356 | void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3357 | #ifdef DEBUG |
| 3358 | int original_height = frame_->height(); |
| 3359 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3360 | Comment cmnt(masm_, "[ ObjectLiteral"); |
| 3361 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3362 | Register literal = frame_->GetTOSRegister(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3363 | // Load the function of this activation. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3364 | __ ldr(literal, frame_->Function()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3365 | // Literal array. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3366 | __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset)); |
| 3367 | frame_->EmitPush(literal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3368 | // Literal index. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3369 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3370 | // Constant properties. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3371 | frame_->EmitPush(Operand(node->constant_properties())); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3372 | // Should the object literal have fast elements? |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3373 | frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0))); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3374 | if (node->depth() > 1) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3375 | frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3376 | } else { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3377 | frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3378 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3379 | frame_->EmitPush(r0); // save the result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3380 | for (int i = 0; i < node->properties()->length(); i++) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3381 | // At the start of each iteration, the top of stack contains |
| 3382 | // the newly created object literal. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3383 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 3384 | Literal* key = property->key(); |
| 3385 | Expression* value = property->value(); |
| 3386 | switch (property->kind()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3387 | case ObjectLiteral::Property::CONSTANT: |
| 3388 | break; |
| 3389 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 3390 | if (CompileTimeValue::IsCompileTimeValue(property->value())) break; |
| 3391 | // else fall through |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3392 | case ObjectLiteral::Property::COMPUTED: |
| 3393 | if (key->handle()->IsSymbol()) { |
| 3394 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3395 | Load(value); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3396 | frame_->PopToR0(); |
| 3397 | // Fetch the object literal. |
| 3398 | frame_->SpillAllButCopyTOSToR1(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3399 | __ mov(r2, Operand(key->handle())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3400 | frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0); |
| 3401 | break; |
| 3402 | } |
| 3403 | // else fall through |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3404 | case ObjectLiteral::Property::PROTOTYPE: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3405 | frame_->Dup(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3406 | Load(key); |
| 3407 | Load(value); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3408 | frame_->CallRuntime(Runtime::kSetProperty, 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3409 | break; |
| 3410 | } |
| 3411 | case ObjectLiteral::Property::SETTER: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3412 | frame_->Dup(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3413 | Load(key); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3414 | frame_->EmitPush(Operand(Smi::FromInt(1))); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3415 | Load(value); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3416 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | } |
| 3419 | case ObjectLiteral::Property::GETTER: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3420 | frame_->Dup(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3421 | Load(key); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3422 | frame_->EmitPush(Operand(Smi::FromInt(0))); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3423 | Load(value); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3424 | frame_->CallRuntime(Runtime::kDefineAccessor, 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3429 | ASSERT_EQ(original_height + 1, frame_->height()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3433 | void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3434 | #ifdef DEBUG |
| 3435 | int original_height = frame_->height(); |
| 3436 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3437 | Comment cmnt(masm_, "[ ArrayLiteral"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3438 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3439 | Register tos = frame_->GetTOSRegister(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3440 | // Load the function of this activation. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3441 | __ ldr(tos, frame_->Function()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3442 | // Load the literals array of the function. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3443 | __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset)); |
| 3444 | frame_->EmitPush(tos); |
| 3445 | frame_->EmitPush(Operand(Smi::FromInt(node->literal_index()))); |
| 3446 | frame_->EmitPush(Operand(node->constant_elements())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3447 | int length = node->values()->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3448 | if (node->depth() > 1) { |
| 3449 | frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3450 | } else if (length > FastCloneShallowArrayStub::kMaximumLength) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3451 | frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3452 | } else { |
| 3453 | FastCloneShallowArrayStub stub(length); |
| 3454 | frame_->CallStub(&stub, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3455 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3456 | frame_->EmitPush(r0); // save the result |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3457 | // r0: created object literal |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3458 | |
| 3459 | // Generate code to set the elements in the array that are not |
| 3460 | // literals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3461 | for (int i = 0; i < node->values()->length(); i++) { |
| 3462 | Expression* value = node->values()->at(i); |
| 3463 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3464 | // If value is a literal the property value is already set in the |
| 3465 | // boilerplate object. |
| 3466 | if (value->AsLiteral() != NULL) continue; |
| 3467 | // If value is a materialized literal the property value is already set |
| 3468 | // in the boilerplate object if it is simple. |
| 3469 | if (CompileTimeValue::IsCompileTimeValue(value)) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3470 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3471 | // The property must be set by generated code. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3472 | Load(value); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3473 | frame_->PopToR0(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3474 | // Fetch the object literal. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3475 | frame_->SpillAllButCopyTOSToR1(); |
| 3476 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3477 | // Get the elements array. |
| 3478 | __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3479 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3480 | // Write to the indexed properties array. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 3481 | int offset = i * kPointerSize + FixedArray::kHeaderSize; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3482 | __ str(r0, FieldMemOperand(r1, offset)); |
| 3483 | |
| 3484 | // Update the write barrier for the array address. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 3485 | __ RecordWrite(r1, Operand(offset), r3, r2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3486 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3487 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3491 | void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3492 | #ifdef DEBUG |
| 3493 | int original_height = frame_->height(); |
| 3494 | #endif |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3495 | // Call runtime routine to allocate the catch extension object and |
| 3496 | // assign the exception value to the catch variable. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3497 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3498 | Load(node->key()); |
| 3499 | Load(node->value()); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 3500 | frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 3501 | frame_->EmitPush(r0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3502 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3503 | } |
| 3504 | |
| 3505 | |
| 3506 | void CodeGenerator::EmitSlotAssignment(Assignment* node) { |
| 3507 | #ifdef DEBUG |
| 3508 | int original_height = frame_->height(); |
| 3509 | #endif |
| 3510 | Comment cmnt(masm(), "[ Variable Assignment"); |
| 3511 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3512 | ASSERT(var != NULL); |
| 3513 | Slot* slot = var->slot(); |
| 3514 | ASSERT(slot != NULL); |
| 3515 | |
| 3516 | // Evaluate the right-hand side. |
| 3517 | if (node->is_compound()) { |
| 3518 | // For a compound assignment the right-hand side is a binary operation |
| 3519 | // between the current property value and the actual right-hand side. |
| 3520 | LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF); |
| 3521 | |
| 3522 | // Perform the binary operation. |
| 3523 | Literal* literal = node->value()->AsLiteral(); |
| 3524 | bool overwrite_value = |
| 3525 | (node->value()->AsBinaryOperation() != NULL && |
| 3526 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3527 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3528 | SmiOperation(node->binary_op(), |
| 3529 | literal->handle(), |
| 3530 | false, |
| 3531 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3532 | } else { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3533 | GenerateInlineSmi inline_smi = |
| 3534 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3535 | if (literal != NULL) { |
| 3536 | ASSERT(!literal->handle()->IsSmi()); |
| 3537 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3538 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3539 | Load(node->value()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3540 | GenericBinaryOperation(node->binary_op(), |
| 3541 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3542 | inline_smi); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3543 | } |
| 3544 | } else { |
| 3545 | Load(node->value()); |
| 3546 | } |
| 3547 | |
| 3548 | // Perform the assignment. |
| 3549 | if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) { |
| 3550 | CodeForSourcePosition(node->position()); |
| 3551 | StoreToSlot(slot, |
| 3552 | node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT); |
| 3553 | } |
| 3554 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3555 | } |
| 3556 | |
| 3557 | |
| 3558 | void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) { |
| 3559 | #ifdef DEBUG |
| 3560 | int original_height = frame_->height(); |
| 3561 | #endif |
| 3562 | Comment cmnt(masm(), "[ Named Property Assignment"); |
| 3563 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3564 | Property* prop = node->target()->AsProperty(); |
| 3565 | ASSERT(var == NULL || (prop == NULL && var->is_global())); |
| 3566 | |
| 3567 | // Initialize name and evaluate the receiver sub-expression if necessary. If |
| 3568 | // the receiver is trivial it is not placed on the stack at this point, but |
| 3569 | // loaded whenever actually needed. |
| 3570 | Handle<String> name; |
| 3571 | bool is_trivial_receiver = false; |
| 3572 | if (var != NULL) { |
| 3573 | name = var->name(); |
| 3574 | } else { |
| 3575 | Literal* lit = prop->key()->AsLiteral(); |
| 3576 | ASSERT_NOT_NULL(lit); |
| 3577 | name = Handle<String>::cast(lit->handle()); |
| 3578 | // Do not materialize the receiver on the frame if it is trivial. |
| 3579 | is_trivial_receiver = prop->obj()->IsTrivial(); |
| 3580 | if (!is_trivial_receiver) Load(prop->obj()); |
| 3581 | } |
| 3582 | |
| 3583 | // Change to slow case in the beginning of an initialization block to |
| 3584 | // avoid the quadratic behavior of repeatedly adding fast properties. |
| 3585 | if (node->starts_initialization_block()) { |
| 3586 | // Initialization block consists of assignments of the form expr.x = ..., so |
| 3587 | // this will never be an assignment to a variable, so there must be a |
| 3588 | // receiver object. |
| 3589 | ASSERT_EQ(NULL, var); |
| 3590 | if (is_trivial_receiver) { |
| 3591 | Load(prop->obj()); |
| 3592 | } else { |
| 3593 | frame_->Dup(); |
| 3594 | } |
| 3595 | frame_->CallRuntime(Runtime::kToSlowProperties, 1); |
| 3596 | } |
| 3597 | |
| 3598 | // Change to fast case at the end of an initialization block. To prepare for |
| 3599 | // that add an extra copy of the receiver to the frame, so that it can be |
| 3600 | // converted back to fast case after the assignment. |
| 3601 | if (node->ends_initialization_block() && !is_trivial_receiver) { |
| 3602 | frame_->Dup(); |
| 3603 | } |
| 3604 | |
| 3605 | // Stack layout: |
| 3606 | // [tos] : receiver (only materialized if non-trivial) |
| 3607 | // [tos+1] : receiver if at the end of an initialization block |
| 3608 | |
| 3609 | // Evaluate the right-hand side. |
| 3610 | if (node->is_compound()) { |
| 3611 | // For a compound assignment the right-hand side is a binary operation |
| 3612 | // between the current property value and the actual right-hand side. |
| 3613 | if (is_trivial_receiver) { |
| 3614 | Load(prop->obj()); |
| 3615 | } else if (var != NULL) { |
| 3616 | LoadGlobal(); |
| 3617 | } else { |
| 3618 | frame_->Dup(); |
| 3619 | } |
| 3620 | EmitNamedLoad(name, var != NULL); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3621 | |
| 3622 | // Perform the binary operation. |
| 3623 | Literal* literal = node->value()->AsLiteral(); |
| 3624 | bool overwrite_value = |
| 3625 | (node->value()->AsBinaryOperation() != NULL && |
| 3626 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3627 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3628 | SmiOperation(node->binary_op(), |
| 3629 | literal->handle(), |
| 3630 | false, |
| 3631 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3632 | } else { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3633 | GenerateInlineSmi inline_smi = |
| 3634 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3635 | if (literal != NULL) { |
| 3636 | ASSERT(!literal->handle()->IsSmi()); |
| 3637 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3638 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3639 | Load(node->value()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3640 | GenericBinaryOperation(node->binary_op(), |
| 3641 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3642 | inline_smi); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } else { |
| 3645 | // For non-compound assignment just load the right-hand side. |
| 3646 | Load(node->value()); |
| 3647 | } |
| 3648 | |
| 3649 | // Stack layout: |
| 3650 | // [tos] : value |
| 3651 | // [tos+1] : receiver (only materialized if non-trivial) |
| 3652 | // [tos+2] : receiver if at the end of an initialization block |
| 3653 | |
| 3654 | // Perform the assignment. It is safe to ignore constants here. |
| 3655 | ASSERT(var == NULL || var->mode() != Variable::CONST); |
| 3656 | ASSERT_NE(Token::INIT_CONST, node->op()); |
| 3657 | if (is_trivial_receiver) { |
| 3658 | // Load the receiver and swap with the value. |
| 3659 | Load(prop->obj()); |
| 3660 | Register t0 = frame_->PopToRegister(); |
| 3661 | Register t1 = frame_->PopToRegister(t0); |
| 3662 | frame_->EmitPush(t0); |
| 3663 | frame_->EmitPush(t1); |
| 3664 | } |
| 3665 | CodeForSourcePosition(node->position()); |
| 3666 | bool is_contextual = (var != NULL); |
| 3667 | EmitNamedStore(name, is_contextual); |
| 3668 | frame_->EmitPush(r0); |
| 3669 | |
| 3670 | // Change to fast case at the end of an initialization block. |
| 3671 | if (node->ends_initialization_block()) { |
| 3672 | ASSERT_EQ(NULL, var); |
| 3673 | // The argument to the runtime call is the receiver. |
| 3674 | if (is_trivial_receiver) { |
| 3675 | Load(prop->obj()); |
| 3676 | } else { |
| 3677 | // A copy of the receiver is below the value of the assignment. Swap |
| 3678 | // the receiver and the value of the assignment expression. |
| 3679 | Register t0 = frame_->PopToRegister(); |
| 3680 | Register t1 = frame_->PopToRegister(t0); |
| 3681 | frame_->EmitPush(t0); |
| 3682 | frame_->EmitPush(t1); |
| 3683 | } |
| 3684 | frame_->CallRuntime(Runtime::kToFastProperties, 1); |
| 3685 | } |
| 3686 | |
| 3687 | // Stack layout: |
| 3688 | // [tos] : result |
| 3689 | |
| 3690 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) { |
| 3695 | #ifdef DEBUG |
| 3696 | int original_height = frame_->height(); |
| 3697 | #endif |
| 3698 | Comment cmnt(masm_, "[ Keyed Property Assignment"); |
| 3699 | Property* prop = node->target()->AsProperty(); |
| 3700 | ASSERT_NOT_NULL(prop); |
| 3701 | |
| 3702 | // Evaluate the receiver subexpression. |
| 3703 | Load(prop->obj()); |
| 3704 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 3705 | WriteBarrierCharacter wb_info; |
| 3706 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3707 | // Change to slow case in the beginning of an initialization block to |
| 3708 | // avoid the quadratic behavior of repeatedly adding fast properties. |
| 3709 | if (node->starts_initialization_block()) { |
| 3710 | frame_->Dup(); |
| 3711 | frame_->CallRuntime(Runtime::kToSlowProperties, 1); |
| 3712 | } |
| 3713 | |
| 3714 | // Change to fast case at the end of an initialization block. To prepare for |
| 3715 | // that add an extra copy of the receiver to the frame, so that it can be |
| 3716 | // converted back to fast case after the assignment. |
| 3717 | if (node->ends_initialization_block()) { |
| 3718 | frame_->Dup(); |
| 3719 | } |
| 3720 | |
| 3721 | // Evaluate the key subexpression. |
| 3722 | Load(prop->key()); |
| 3723 | |
| 3724 | // Stack layout: |
| 3725 | // [tos] : key |
| 3726 | // [tos+1] : receiver |
| 3727 | // [tos+2] : receiver if at the end of an initialization block |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 3728 | // |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3729 | // Evaluate the right-hand side. |
| 3730 | if (node->is_compound()) { |
| 3731 | // For a compound assignment the right-hand side is a binary operation |
| 3732 | // between the current property value and the actual right-hand side. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 3733 | // Duplicate receiver and key for loading the current property value. |
| 3734 | frame_->Dup2(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3735 | EmitKeyedLoad(); |
| 3736 | frame_->EmitPush(r0); |
| 3737 | |
| 3738 | // Perform the binary operation. |
| 3739 | Literal* literal = node->value()->AsLiteral(); |
| 3740 | bool overwrite_value = |
| 3741 | (node->value()->AsBinaryOperation() != NULL && |
| 3742 | node->value()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 3743 | if (literal != NULL && literal->handle()->IsSmi()) { |
| 3744 | SmiOperation(node->binary_op(), |
| 3745 | literal->handle(), |
| 3746 | false, |
| 3747 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 3748 | } else { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3749 | GenerateInlineSmi inline_smi = |
| 3750 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 3751 | if (literal != NULL) { |
| 3752 | ASSERT(!literal->handle()->IsSmi()); |
| 3753 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 3754 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3755 | Load(node->value()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3756 | GenericBinaryOperation(node->binary_op(), |
| 3757 | overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE, |
| 3758 | inline_smi); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3759 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 3760 | wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3761 | } else { |
| 3762 | // For non-compound assignment just load the right-hand side. |
| 3763 | Load(node->value()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 3764 | wb_info = node->value()->AsLiteral() != NULL ? |
| 3765 | NEVER_NEWSPACE : |
| 3766 | (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | // Stack layout: |
| 3770 | // [tos] : value |
| 3771 | // [tos+1] : key |
| 3772 | // [tos+2] : receiver |
| 3773 | // [tos+3] : receiver if at the end of an initialization block |
| 3774 | |
| 3775 | // Perform the assignment. It is safe to ignore constants here. |
| 3776 | ASSERT(node->op() != Token::INIT_CONST); |
| 3777 | CodeForSourcePosition(node->position()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 3778 | EmitKeyedStore(prop->key()->type(), wb_info); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3779 | frame_->EmitPush(r0); |
| 3780 | |
| 3781 | // Stack layout: |
| 3782 | // [tos] : result |
| 3783 | // [tos+1] : receiver if at the end of an initialization block |
| 3784 | |
| 3785 | // Change to fast case at the end of an initialization block. |
| 3786 | if (node->ends_initialization_block()) { |
| 3787 | // The argument to the runtime call is the extra copy of the receiver, |
| 3788 | // which is below the value of the assignment. Swap the receiver and |
| 3789 | // the value of the assignment expression. |
| 3790 | Register t0 = frame_->PopToRegister(); |
| 3791 | Register t1 = frame_->PopToRegister(t0); |
| 3792 | frame_->EmitPush(t1); |
| 3793 | frame_->EmitPush(t0); |
| 3794 | frame_->CallRuntime(Runtime::kToFastProperties, 1); |
| 3795 | } |
| 3796 | |
| 3797 | // Stack layout: |
| 3798 | // [tos] : result |
| 3799 | |
| 3800 | ASSERT_EQ(original_height + 1, frame_->height()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3804 | void CodeGenerator::VisitAssignment(Assignment* node) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3805 | VirtualFrame::RegisterAllocationScope scope(this); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3806 | #ifdef DEBUG |
| 3807 | int original_height = frame_->height(); |
| 3808 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3809 | Comment cmnt(masm_, "[ Assignment"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3810 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3811 | Variable* var = node->target()->AsVariableProxy()->AsVariable(); |
| 3812 | Property* prop = node->target()->AsProperty(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3813 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3814 | if (var != NULL && !var->is_global()) { |
| 3815 | EmitSlotAssignment(node); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 3816 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3817 | } else if ((prop != NULL && prop->key()->IsPropertyName()) || |
| 3818 | (var != NULL && var->is_global())) { |
| 3819 | // Properties whose keys are property names and global variables are |
| 3820 | // treated as named property references. We do not need to consider |
| 3821 | // global 'this' because it is not a valid left-hand side. |
| 3822 | EmitNamedPropertyAssignment(node); |
| 3823 | |
| 3824 | } else if (prop != NULL) { |
| 3825 | // Other properties (including rewritten parameters for a function that |
| 3826 | // uses arguments) are keyed property assignments. |
| 3827 | EmitKeyedPropertyAssignment(node); |
| 3828 | |
| 3829 | } else { |
| 3830 | // Invalid left-hand side. |
| 3831 | Load(node->target()); |
| 3832 | frame_->CallRuntime(Runtime::kThrowReferenceError, 1); |
| 3833 | // The runtime call doesn't actually return but the code generator will |
| 3834 | // still generate code and expects a certain frame height. |
| 3835 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3836 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3837 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3841 | void CodeGenerator::VisitThrow(Throw* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3842 | #ifdef DEBUG |
| 3843 | int original_height = frame_->height(); |
| 3844 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3845 | Comment cmnt(masm_, "[ Throw"); |
| 3846 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3847 | Load(node->exception()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3848 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3849 | frame_->CallRuntime(Runtime::kThrow, 1); |
| 3850 | frame_->EmitPush(r0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3851 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3855 | void CodeGenerator::VisitProperty(Property* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3856 | #ifdef DEBUG |
| 3857 | int original_height = frame_->height(); |
| 3858 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3859 | Comment cmnt(masm_, "[ Property"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3860 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3861 | { Reference property(this, node); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 3862 | property.GetValue(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3863 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 3864 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3868 | void CodeGenerator::VisitCall(Call* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3869 | #ifdef DEBUG |
| 3870 | int original_height = frame_->height(); |
| 3871 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3872 | Comment cmnt(masm_, "[ Call"); |
| 3873 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3874 | Expression* function = node->expression(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3875 | ZoneList<Expression*>* args = node->arguments(); |
| 3876 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3877 | // Standard function call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3878 | // Check if the function is a variable or a property. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3879 | Variable* var = function->AsVariableProxy()->AsVariable(); |
| 3880 | Property* property = function->AsProperty(); |
| 3881 | |
| 3882 | // ------------------------------------------------------------------------ |
| 3883 | // Fast-case: Use inline caching. |
| 3884 | // --- |
| 3885 | // According to ECMA-262, section 11.2.3, page 44, the function to call |
| 3886 | // must be resolved after the arguments have been evaluated. The IC code |
| 3887 | // automatically handles this by loading the arguments before the function |
| 3888 | // is resolved in cache misses (this also holds for megamorphic calls). |
| 3889 | // ------------------------------------------------------------------------ |
| 3890 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3891 | if (var != NULL && var->is_possibly_eval()) { |
| 3892 | // ---------------------------------- |
| 3893 | // JavaScript example: 'eval(arg)' // eval is not known to be shadowed |
| 3894 | // ---------------------------------- |
| 3895 | |
| 3896 | // In a call to eval, we first call %ResolvePossiblyDirectEval to |
| 3897 | // resolve the function we need to call and the receiver of the |
| 3898 | // call. Then we call the resolved function using the given |
| 3899 | // arguments. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3900 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3901 | // Prepare stack for call to resolved function. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3902 | Load(function); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3903 | |
| 3904 | // Allocate a frame slot for the receiver. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3905 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3906 | |
| 3907 | // Load the arguments. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3908 | int arg_count = args->length(); |
| 3909 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3910 | Load(args->at(i)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 3913 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 3914 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3915 | // If we know that eval can only be shadowed by eval-introduced |
| 3916 | // variables we attempt to load the global eval function directly |
| 3917 | // in generated code. If we succeed, there is no need to perform a |
| 3918 | // context lookup in the runtime system. |
| 3919 | JumpTarget done; |
| 3920 | if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) { |
| 3921 | ASSERT(var->slot()->type() == Slot::LOOKUP); |
| 3922 | JumpTarget slow; |
| 3923 | // Prepare the stack for the call to |
| 3924 | // ResolvePossiblyDirectEvalNoLookup by pushing the loaded |
| 3925 | // function, the first argument to the eval call and the |
| 3926 | // receiver. |
| 3927 | LoadFromGlobalSlotCheckExtensions(var->slot(), |
| 3928 | NOT_INSIDE_TYPEOF, |
| 3929 | &slow); |
| 3930 | frame_->EmitPush(r0); |
| 3931 | if (arg_count > 0) { |
| 3932 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3933 | frame_->EmitPush(r1); |
| 3934 | } else { |
| 3935 | frame_->EmitPush(r2); |
| 3936 | } |
| 3937 | __ ldr(r1, frame_->Receiver()); |
| 3938 | frame_->EmitPush(r1); |
| 3939 | |
| 3940 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3); |
| 3941 | |
| 3942 | done.Jump(); |
| 3943 | slow.Bind(); |
| 3944 | } |
| 3945 | |
| 3946 | // Prepare the stack for the call to ResolvePossiblyDirectEval by |
| 3947 | // pushing the loaded function, the first argument to the eval |
| 3948 | // call and the receiver. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3949 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize)); |
| 3950 | frame_->EmitPush(r1); |
| 3951 | if (arg_count > 0) { |
| 3952 | __ ldr(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3953 | frame_->EmitPush(r1); |
| 3954 | } else { |
| 3955 | frame_->EmitPush(r2); |
| 3956 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3957 | __ ldr(r1, frame_->Receiver()); |
| 3958 | frame_->EmitPush(r1); |
| 3959 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3960 | // Resolve the call. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3961 | frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3962 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 3963 | // If we generated fast-case code bind the jump-target where fast |
| 3964 | // and slow case merge. |
| 3965 | if (done.is_linked()) done.Bind(); |
| 3966 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3967 | // Touch up stack with the right values for the function and the receiver. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3968 | __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3969 | __ str(r1, MemOperand(sp, arg_count * kPointerSize)); |
| 3970 | |
| 3971 | // Call the function. |
| 3972 | CodeForSourcePosition(node->position()); |
| 3973 | |
| 3974 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3975 | CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3976 | frame_->CallStub(&call_function, arg_count + 1); |
| 3977 | |
| 3978 | __ ldr(cp, frame_->Context()); |
| 3979 | // Remove the function from the stack. |
| 3980 | frame_->Drop(); |
| 3981 | frame_->EmitPush(r0); |
| 3982 | |
| 3983 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | // ---------------------------------- |
| 3985 | // JavaScript example: 'foo(1, 2, 3)' // foo is global |
| 3986 | // ---------------------------------- |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 3987 | // Pass the global object as the receiver and let the IC stub |
| 3988 | // patch the stack to use the global proxy as 'this' in the |
| 3989 | // invoked function. |
| 3990 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | |
| 3992 | // Load the arguments. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3993 | int arg_count = args->length(); |
| 3994 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3995 | Load(args->at(i)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 3996 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3997 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3998 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3999 | // Setup the name register and call the IC initialization code. |
| 4000 | __ mov(r2, Operand(var->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4001 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4002 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4003 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4004 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT, |
| 4005 | arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 4006 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4007 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4008 | |
| 4009 | } else if (var != NULL && var->slot() != NULL && |
| 4010 | var->slot()->type() == Slot::LOOKUP) { |
| 4011 | // ---------------------------------- |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4012 | // JavaScript examples: |
| 4013 | // |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4014 | // with (obj) foo(1, 2, 3) // foo may be in obj. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4015 | // |
| 4016 | // function f() {}; |
| 4017 | // function g() { |
| 4018 | // eval(...); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4019 | // f(); // f could be in extension object. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4020 | // } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | // ---------------------------------- |
| 4022 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4023 | JumpTarget slow, done; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4024 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4025 | // Generate fast case for loading functions from slots that |
| 4026 | // correspond to local/global variables or arguments unless they |
| 4027 | // are shadowed by eval-introduced bindings. |
| 4028 | EmitDynamicLoadFromSlotFastCase(var->slot(), |
| 4029 | NOT_INSIDE_TYPEOF, |
| 4030 | &slow, |
| 4031 | &done); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4032 | |
| 4033 | slow.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4034 | // Load the function |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4035 | frame_->EmitPush(cp); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4036 | frame_->EmitPush(Operand(var->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4037 | frame_->CallRuntime(Runtime::kLoadContextSlot, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4038 | // r0: slot value; r1: receiver |
| 4039 | |
| 4040 | // Load the receiver. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4041 | frame_->EmitPush(r0); // function |
| 4042 | frame_->EmitPush(r1); // receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4043 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4044 | // If fast case code has been generated, emit code to push the |
| 4045 | // function and receiver and have the slow path jump around this |
| 4046 | // code. |
| 4047 | if (done.is_linked()) { |
| 4048 | JumpTarget call; |
| 4049 | call.Jump(); |
| 4050 | done.Bind(); |
| 4051 | frame_->EmitPush(r0); // function |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4052 | LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 4053 | call.Bind(); |
| 4054 | } |
| 4055 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4056 | // Call the function. At this point, everything is spilled but the |
| 4057 | // function and receiver are in r0 and r1. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4058 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4059 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4060 | |
| 4061 | } else if (property != NULL) { |
| 4062 | // Check if the key is a literal string. |
| 4063 | Literal* literal = property->key()->AsLiteral(); |
| 4064 | |
| 4065 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 4066 | // ------------------------------------------------------------------ |
| 4067 | // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)' |
| 4068 | // ------------------------------------------------------------------ |
| 4069 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4070 | Handle<String> name = Handle<String>::cast(literal->handle()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4071 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4072 | if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION && |
| 4073 | name->IsEqualTo(CStrVector("apply")) && |
| 4074 | args->length() == 2 && |
| 4075 | args->at(1)->AsVariableProxy() != NULL && |
| 4076 | args->at(1)->AsVariableProxy()->IsArguments()) { |
| 4077 | // Use the optimized Function.prototype.apply that avoids |
| 4078 | // allocating lazily allocated arguments objects. |
| 4079 | CallApplyLazy(property->obj(), |
| 4080 | args->at(0), |
| 4081 | args->at(1)->AsVariableProxy(), |
| 4082 | node->position()); |
| 4083 | |
| 4084 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4085 | Load(property->obj()); // Receiver. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4086 | // Load the arguments. |
| 4087 | int arg_count = args->length(); |
| 4088 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4089 | Load(args->at(i)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4092 | VirtualFrame::SpilledScope spilled_scope(frame_); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 4093 | // Set the name register and call the IC initialization code. |
| 4094 | __ mov(r2, Operand(name)); |
| 4095 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4096 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
| 4097 | CodeForSourcePosition(node->position()); |
| 4098 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 4099 | __ ldr(cp, frame_->Context()); |
| 4100 | frame_->EmitPush(r0); |
| 4101 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4102 | |
| 4103 | } else { |
| 4104 | // ------------------------------------------- |
| 4105 | // JavaScript example: 'array[index](1, 2, 3)' |
| 4106 | // ------------------------------------------- |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4107 | Load(property->obj()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4108 | if (property->is_synthetic()) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4109 | Load(property->key()); |
| 4110 | EmitKeyedLoad(); |
| 4111 | // Put the function below the receiver. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4112 | // Use the global receiver. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 4113 | frame_->EmitPush(r0); // Function. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4114 | LoadGlobalReceiver(VirtualFrame::scratch0()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4115 | // Call the function. |
| 4116 | CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position()); |
| 4117 | frame_->EmitPush(r0); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4118 | } else { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4119 | // Load the arguments. |
| 4120 | int arg_count = args->length(); |
| 4121 | for (int i = 0; i < arg_count; i++) { |
| 4122 | Load(args->at(i)); |
| 4123 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4124 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4125 | // Set the name register and call the IC initialization code. |
| 4126 | Load(property->key()); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4127 | frame_->SpillAll(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4128 | frame_->EmitPop(r2); // Function name. |
| 4129 | |
| 4130 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 4131 | Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop); |
| 4132 | CodeForSourcePosition(node->position()); |
| 4133 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
| 4134 | __ ldr(cp, frame_->Context()); |
| 4135 | frame_->EmitPush(r0); |
| 4136 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | } else { |
| 4140 | // ---------------------------------- |
| 4141 | // JavaScript example: 'foo(1, 2, 3)' // foo is not global |
| 4142 | // ---------------------------------- |
| 4143 | |
| 4144 | // Load the function. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4145 | Load(function); |
| 4146 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 4147 | // Pass the global proxy as the receiver. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4148 | LoadGlobalReceiver(VirtualFrame::scratch0()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 4149 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4150 | // Call the function. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4151 | CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4152 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4153 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 4154 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4158 | void CodeGenerator::VisitCallNew(CallNew* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4159 | #ifdef DEBUG |
| 4160 | int original_height = frame_->height(); |
| 4161 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4162 | Comment cmnt(masm_, "[ CallNew"); |
| 4163 | |
| 4164 | // According to ECMA-262, section 11.2.2, page 44, the function |
| 4165 | // expression in new calls must be evaluated before the |
| 4166 | // arguments. This is different from ordinary calls, where the |
| 4167 | // actual function to call is resolved after the arguments have been |
| 4168 | // evaluated. |
| 4169 | |
| 4170 | // Compute function to call and use the global object as the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 4171 | // receiver. There is no need to use the global proxy here because |
| 4172 | // it will always be replaced with a newly allocated object. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4173 | Load(node->expression()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 4174 | LoadGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | |
| 4176 | // Push the arguments ("left-to-right") on the stack. |
| 4177 | ZoneList<Expression*>* args = node->arguments(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4178 | int arg_count = args->length(); |
| 4179 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4180 | Load(args->at(i)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4181 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4182 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4183 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 4184 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4185 | // r0: the number of arguments. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4186 | __ mov(r0, Operand(arg_count)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4187 | // Load the function into r1 as per calling convention. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 4188 | __ ldr(r1, frame_->ElementAt(arg_count + 1)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4189 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4190 | // Call the construct call builtin that handles allocation and |
| 4191 | // constructor invocation. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4192 | CodeForSourcePosition(node->position()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4193 | Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4194 | frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4195 | |
| 4196 | // 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] | 4197 | __ str(r0, frame_->Top()); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 4198 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4202 | void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4203 | Register scratch = VirtualFrame::scratch0(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4204 | JumpTarget null, function, leave, non_function_constructor; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4205 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4206 | // Load the object into register. |
| 4207 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4208 | Load(args->at(0)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4209 | Register tos = frame_->PopToRegister(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4210 | |
| 4211 | // If the object is a smi, we return null. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4212 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4213 | null.Branch(eq); |
| 4214 | |
| 4215 | // Check that the object is a JS object but take special care of JS |
| 4216 | // functions to make sure they have 'Function' as their class. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4217 | __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4218 | null.Branch(lt); |
| 4219 | |
| 4220 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 4221 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 4222 | // LAST_JS_OBJECT_TYPE. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 4223 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 4224 | STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4225 | __ cmp(scratch, Operand(JS_FUNCTION_TYPE)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4226 | function.Branch(eq); |
| 4227 | |
| 4228 | // Check if the constructor in the map is a function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4229 | __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset)); |
| 4230 | __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4231 | non_function_constructor.Branch(ne); |
| 4232 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4233 | // The tos register now contains the constructor function. Grab the |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4234 | // instance class name from there. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4235 | __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset)); |
| 4236 | __ ldr(tos, |
| 4237 | FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset)); |
| 4238 | frame_->EmitPush(tos); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4239 | leave.Jump(); |
| 4240 | |
| 4241 | // Functions have class 'Function'. |
| 4242 | function.Bind(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4243 | __ mov(tos, Operand(Factory::function_class_symbol())); |
| 4244 | frame_->EmitPush(tos); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4245 | leave.Jump(); |
| 4246 | |
| 4247 | // Objects with a non-function constructor have class 'Object'. |
| 4248 | non_function_constructor.Bind(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4249 | __ mov(tos, Operand(Factory::Object_symbol())); |
| 4250 | frame_->EmitPush(tos); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4251 | leave.Jump(); |
| 4252 | |
| 4253 | // Non-JS objects have class null. |
| 4254 | null.Bind(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4255 | __ LoadRoot(tos, Heap::kNullValueRootIndex); |
| 4256 | frame_->EmitPush(tos); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4257 | |
| 4258 | // All done. |
| 4259 | leave.Bind(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4263 | void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4264 | Register scratch = VirtualFrame::scratch0(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4265 | JumpTarget leave; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4266 | |
| 4267 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4268 | Load(args->at(0)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4269 | Register tos = frame_->PopToRegister(); // tos contains object. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 4270 | // if (object->IsSmi()) return the object. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4271 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4272 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4273 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4274 | __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4275 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4276 | // Load the value. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4277 | __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4278 | leave.Bind(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4279 | frame_->EmitPush(tos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4283 | void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4284 | Register scratch1 = VirtualFrame::scratch0(); |
| 4285 | Register scratch2 = VirtualFrame::scratch1(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4286 | JumpTarget leave; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4287 | |
| 4288 | ASSERT(args->length() == 2); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4289 | Load(args->at(0)); // Load the object. |
| 4290 | Load(args->at(1)); // Load the value. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4291 | Register value = frame_->PopToRegister(); |
| 4292 | Register object = frame_->PopToRegister(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4293 | // if (object->IsSmi()) return object. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4294 | __ tst(object, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4295 | leave.Branch(eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4296 | // It is a heap object - get map. If (!object->IsJSValue()) return the object. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4297 | __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4298 | leave.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4299 | // Store the value. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4300 | __ str(value, FieldMemOperand(object, JSValue::kValueOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4301 | // Update the write barrier. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4302 | __ RecordWrite(object, |
| 4303 | Operand(JSValue::kValueOffset - kHeapObjectTag), |
| 4304 | scratch1, |
| 4305 | scratch2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4306 | // Leave. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4307 | leave.Bind(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4308 | frame_->EmitPush(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4312 | void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4313 | ASSERT(args->length() == 1); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4314 | Load(args->at(0)); |
| 4315 | Register reg = frame_->PopToRegister(); |
| 4316 | __ tst(reg, Operand(kSmiTagMask)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4317 | cc_reg_ = eq; |
| 4318 | } |
| 4319 | |
| 4320 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4321 | void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) { |
| 4322 | // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc. |
| 4323 | ASSERT_EQ(args->length(), 3); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 4324 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4325 | if (ShouldGenerateLog(args->at(0))) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4326 | Load(args->at(1)); |
| 4327 | Load(args->at(2)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4328 | frame_->CallRuntime(Runtime::kLog, 2); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4329 | } |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 4330 | #endif |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4331 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4335 | void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) { |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 4336 | ASSERT(args->length() == 1); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4337 | Load(args->at(0)); |
| 4338 | Register reg = frame_->PopToRegister(); |
| 4339 | __ tst(reg, Operand(kSmiTagMask | 0x80000000u)); |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 4340 | cc_reg_ = eq; |
| 4341 | } |
| 4342 | |
| 4343 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4344 | // Generates the Math.pow method. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4345 | void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) { |
| 4346 | ASSERT(args->length() == 2); |
| 4347 | Load(args->at(0)); |
| 4348 | Load(args->at(1)); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 4351 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 4352 | frame_->EmitPush(r0); |
| 4353 | } else { |
| 4354 | CpuFeatures::Scope scope(VFP3); |
| 4355 | JumpTarget runtime, done; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4356 | Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4357 | |
| 4358 | Register scratch1 = VirtualFrame::scratch0(); |
| 4359 | Register scratch2 = VirtualFrame::scratch1(); |
| 4360 | |
| 4361 | // Get base and exponent to registers. |
| 4362 | Register exponent = frame_->PopToRegister(); |
| 4363 | Register base = frame_->PopToRegister(exponent); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4364 | Register heap_number_map = no_reg; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4365 | |
| 4366 | // Set the frame for the runtime jump target. The code below jumps to the |
| 4367 | // jump target label so the frame needs to be established before that. |
| 4368 | ASSERT(runtime.entry_frame() == NULL); |
| 4369 | runtime.set_entry_frame(frame_); |
| 4370 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4371 | __ BranchOnNotSmi(exponent, &exponent_nonsmi); |
| 4372 | __ BranchOnNotSmi(base, &base_nonsmi); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4373 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4374 | heap_number_map = r6; |
| 4375 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4376 | |
| 4377 | // Exponent is a smi and base is a smi. Get the smi value into vfp register |
| 4378 | // d1. |
| 4379 | __ SmiToDoubleVFPRegister(base, d1, scratch1, s0); |
| 4380 | __ b(&powi); |
| 4381 | |
| 4382 | __ bind(&base_nonsmi); |
| 4383 | // Exponent is smi and base is non smi. Get the double value from the base |
| 4384 | // into vfp register d1. |
| 4385 | __ ObjectToDoubleVFPRegister(base, d1, |
| 4386 | scratch1, scratch2, heap_number_map, s0, |
| 4387 | runtime.entry_label()); |
| 4388 | |
| 4389 | __ bind(&powi); |
| 4390 | |
| 4391 | // Load 1.0 into d0. |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4392 | __ vmov(d0, 1.0); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4393 | |
| 4394 | // Get the absolute untagged value of the exponent and use that for the |
| 4395 | // calculation. |
| 4396 | __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC); |
| 4397 | __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative. |
| 4398 | __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative. |
| 4399 | |
| 4400 | // Run through all the bits in the exponent. The result is calculated in d0 |
| 4401 | // and d1 holds base^(bit^2). |
| 4402 | Label more_bits; |
| 4403 | __ bind(&more_bits); |
| 4404 | __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC); |
| 4405 | __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set. |
| 4406 | __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done. |
| 4407 | __ b(ne, &more_bits); |
| 4408 | |
| 4409 | // If exponent is positive we are done. |
| 4410 | __ cmp(exponent, Operand(0)); |
| 4411 | __ b(ge, &allocate_return); |
| 4412 | |
| 4413 | // If exponent is negative result is 1/result (d2 already holds 1.0 in that |
| 4414 | // case). However if d0 has reached infinity this will not provide the |
| 4415 | // correct result, so call runtime if that is the case. |
| 4416 | __ mov(scratch2, Operand(0x7FF00000)); |
| 4417 | __ mov(scratch1, Operand(0)); |
| 4418 | __ vmov(d1, scratch1, scratch2); // Load infinity into d1. |
| 4419 | __ vcmp(d0, d1); |
| 4420 | __ vmrs(pc); |
| 4421 | runtime.Branch(eq); // d0 reached infinity. |
| 4422 | __ vdiv(d0, d2, d0); |
| 4423 | __ b(&allocate_return); |
| 4424 | |
| 4425 | __ bind(&exponent_nonsmi); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4426 | // Special handling of raising to the power of -0.5 and 0.5. First check |
| 4427 | // that the value is a heap number and that the lower bits (which for both |
| 4428 | // values are zero). |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4429 | heap_number_map = r6; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4430 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4431 | __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset)); |
| 4432 | __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset)); |
| 4433 | __ cmp(scratch1, heap_number_map); |
| 4434 | runtime.Branch(ne); |
| 4435 | __ tst(scratch2, scratch2); |
| 4436 | runtime.Branch(ne); |
| 4437 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4438 | // Load the higher bits (which contains the floating point exponent). |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4439 | __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset)); |
| 4440 | |
| 4441 | // Compare exponent with -0.5. |
| 4442 | __ cmp(scratch1, Operand(0xbfe00000)); |
| 4443 | __ b(ne, ¬_minus_half); |
| 4444 | |
| 4445 | // Get the double value from the base into vfp register d0. |
| 4446 | __ ObjectToDoubleVFPRegister(base, d0, |
| 4447 | scratch1, scratch2, heap_number_map, s0, |
| 4448 | runtime.entry_label(), |
| 4449 | AVOID_NANS_AND_INFINITIES); |
| 4450 | |
| 4451 | // Load 1.0 into d2. |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4452 | __ vmov(d2, 1.0); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4453 | |
| 4454 | // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x). |
| 4455 | __ vdiv(d0, d2, d0); |
| 4456 | __ vsqrt(d0, d0); |
| 4457 | |
| 4458 | __ b(&allocate_return); |
| 4459 | |
| 4460 | __ bind(¬_minus_half); |
| 4461 | // Compare exponent with 0.5. |
| 4462 | __ cmp(scratch1, Operand(0x3fe00000)); |
| 4463 | runtime.Branch(ne); |
| 4464 | |
| 4465 | // Get the double value from the base into vfp register d0. |
| 4466 | __ ObjectToDoubleVFPRegister(base, d0, |
| 4467 | scratch1, scratch2, heap_number_map, s0, |
| 4468 | runtime.entry_label(), |
| 4469 | AVOID_NANS_AND_INFINITIES); |
| 4470 | __ vsqrt(d0, d0); |
| 4471 | |
| 4472 | __ bind(&allocate_return); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 4473 | Register scratch3 = r5; |
| 4474 | __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2, |
| 4475 | heap_number_map, runtime.entry_label()); |
| 4476 | __ mov(base, scratch3); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4477 | done.Jump(); |
| 4478 | |
| 4479 | runtime.Bind(); |
| 4480 | |
| 4481 | // Push back the arguments again for the runtime call. |
| 4482 | frame_->EmitPush(base); |
| 4483 | frame_->EmitPush(exponent); |
| 4484 | frame_->CallRuntime(Runtime::kMath_pow, 2); |
| 4485 | __ Move(base, r0); |
| 4486 | |
| 4487 | done.Bind(); |
| 4488 | frame_->EmitPush(base); |
| 4489 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4493 | // Generates the Math.sqrt method. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4494 | void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) { |
| 4495 | ASSERT(args->length() == 1); |
| 4496 | Load(args->at(0)); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4497 | |
| 4498 | if (!CpuFeatures::IsSupported(VFP3)) { |
| 4499 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 4500 | frame_->EmitPush(r0); |
| 4501 | } else { |
| 4502 | CpuFeatures::Scope scope(VFP3); |
| 4503 | JumpTarget runtime, done; |
| 4504 | |
| 4505 | Register scratch1 = VirtualFrame::scratch0(); |
| 4506 | Register scratch2 = VirtualFrame::scratch1(); |
| 4507 | |
| 4508 | // Get the value from the frame. |
| 4509 | Register tos = frame_->PopToRegister(); |
| 4510 | |
| 4511 | // Set the frame for the runtime jump target. The code below jumps to the |
| 4512 | // jump target label so the frame needs to be established before that. |
| 4513 | ASSERT(runtime.entry_frame() == NULL); |
| 4514 | runtime.set_entry_frame(frame_); |
| 4515 | |
| 4516 | Register heap_number_map = r6; |
| 4517 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 4518 | |
| 4519 | // Get the double value from the heap number into vfp register d0. |
| 4520 | __ ObjectToDoubleVFPRegister(tos, d0, |
| 4521 | scratch1, scratch2, heap_number_map, s0, |
| 4522 | runtime.entry_label()); |
| 4523 | |
| 4524 | // Calculate the square root of d0 and place result in a heap number object. |
| 4525 | __ vsqrt(d0, d0); |
| 4526 | __ AllocateHeapNumberWithValue( |
| 4527 | tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label()); |
| 4528 | done.Jump(); |
| 4529 | |
| 4530 | runtime.Bind(); |
| 4531 | // Push back the argument again for the runtime call. |
| 4532 | frame_->EmitPush(tos); |
| 4533 | frame_->CallRuntime(Runtime::kMath_sqrt, 1); |
| 4534 | __ Move(tos, r0); |
| 4535 | |
| 4536 | done.Bind(); |
| 4537 | frame_->EmitPush(tos); |
| 4538 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4542 | class DeferredStringCharCodeAt : public DeferredCode { |
| 4543 | public: |
| 4544 | DeferredStringCharCodeAt(Register object, |
| 4545 | Register index, |
| 4546 | Register scratch, |
| 4547 | Register result) |
| 4548 | : result_(result), |
| 4549 | char_code_at_generator_(object, |
| 4550 | index, |
| 4551 | scratch, |
| 4552 | result, |
| 4553 | &need_conversion_, |
| 4554 | &need_conversion_, |
| 4555 | &index_out_of_range_, |
| 4556 | STRING_INDEX_IS_NUMBER) {} |
| 4557 | |
| 4558 | StringCharCodeAtGenerator* fast_case_generator() { |
| 4559 | return &char_code_at_generator_; |
| 4560 | } |
| 4561 | |
| 4562 | virtual void Generate() { |
| 4563 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4564 | char_code_at_generator_.GenerateSlow(masm(), call_helper); |
| 4565 | |
| 4566 | __ bind(&need_conversion_); |
| 4567 | // Move the undefined value into the result register, which will |
| 4568 | // trigger conversion. |
| 4569 | __ LoadRoot(result_, Heap::kUndefinedValueRootIndex); |
| 4570 | __ jmp(exit_label()); |
| 4571 | |
| 4572 | __ bind(&index_out_of_range_); |
| 4573 | // When the index is out of range, the spec requires us to return |
| 4574 | // NaN. |
| 4575 | __ LoadRoot(result_, Heap::kNanValueRootIndex); |
| 4576 | __ jmp(exit_label()); |
| 4577 | } |
| 4578 | |
| 4579 | private: |
| 4580 | Register result_; |
| 4581 | |
| 4582 | Label need_conversion_; |
| 4583 | Label index_out_of_range_; |
| 4584 | |
| 4585 | StringCharCodeAtGenerator char_code_at_generator_; |
| 4586 | }; |
| 4587 | |
| 4588 | |
| 4589 | // This generates code that performs a String.prototype.charCodeAt() call |
| 4590 | // or returns a smi in order to trigger conversion. |
| 4591 | void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4592 | Comment(masm_, "[ GenerateStringCharCodeAt"); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4593 | ASSERT(args->length() == 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4594 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4595 | Load(args->at(0)); |
| 4596 | Load(args->at(1)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4597 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4598 | Register index = frame_->PopToRegister(); |
| 4599 | Register object = frame_->PopToRegister(index); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4600 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4601 | // We need two extra registers. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4602 | Register scratch = VirtualFrame::scratch0(); |
| 4603 | Register result = VirtualFrame::scratch1(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4604 | |
| 4605 | DeferredStringCharCodeAt* deferred = |
| 4606 | new DeferredStringCharCodeAt(object, |
| 4607 | index, |
| 4608 | scratch, |
| 4609 | result); |
| 4610 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4611 | deferred->BindExit(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4612 | frame_->EmitPush(result); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4616 | class DeferredStringCharFromCode : public DeferredCode { |
| 4617 | public: |
| 4618 | DeferredStringCharFromCode(Register code, |
| 4619 | Register result) |
| 4620 | : char_from_code_generator_(code, result) {} |
| 4621 | |
| 4622 | StringCharFromCodeGenerator* fast_case_generator() { |
| 4623 | return &char_from_code_generator_; |
| 4624 | } |
| 4625 | |
| 4626 | virtual void Generate() { |
| 4627 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4628 | char_from_code_generator_.GenerateSlow(masm(), call_helper); |
| 4629 | } |
| 4630 | |
| 4631 | private: |
| 4632 | StringCharFromCodeGenerator char_from_code_generator_; |
| 4633 | }; |
| 4634 | |
| 4635 | |
| 4636 | // Generates code for creating a one-char string from a char code. |
| 4637 | void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4638 | Comment(masm_, "[ GenerateStringCharFromCode"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4639 | ASSERT(args->length() == 1); |
| 4640 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4641 | Load(args->at(0)); |
| 4642 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4643 | Register result = frame_->GetTOSRegister(); |
| 4644 | Register code = frame_->PopToRegister(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4645 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4646 | DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode( |
| 4647 | code, result); |
| 4648 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4649 | deferred->BindExit(); |
| 4650 | frame_->EmitPush(result); |
| 4651 | } |
| 4652 | |
| 4653 | |
| 4654 | class DeferredStringCharAt : public DeferredCode { |
| 4655 | public: |
| 4656 | DeferredStringCharAt(Register object, |
| 4657 | Register index, |
| 4658 | Register scratch1, |
| 4659 | Register scratch2, |
| 4660 | Register result) |
| 4661 | : result_(result), |
| 4662 | char_at_generator_(object, |
| 4663 | index, |
| 4664 | scratch1, |
| 4665 | scratch2, |
| 4666 | result, |
| 4667 | &need_conversion_, |
| 4668 | &need_conversion_, |
| 4669 | &index_out_of_range_, |
| 4670 | STRING_INDEX_IS_NUMBER) {} |
| 4671 | |
| 4672 | StringCharAtGenerator* fast_case_generator() { |
| 4673 | return &char_at_generator_; |
| 4674 | } |
| 4675 | |
| 4676 | virtual void Generate() { |
| 4677 | VirtualFrameRuntimeCallHelper call_helper(frame_state()); |
| 4678 | char_at_generator_.GenerateSlow(masm(), call_helper); |
| 4679 | |
| 4680 | __ bind(&need_conversion_); |
| 4681 | // Move smi zero into the result register, which will trigger |
| 4682 | // conversion. |
| 4683 | __ mov(result_, Operand(Smi::FromInt(0))); |
| 4684 | __ jmp(exit_label()); |
| 4685 | |
| 4686 | __ bind(&index_out_of_range_); |
| 4687 | // When the index is out of range, the spec requires us to return |
| 4688 | // the empty string. |
| 4689 | __ LoadRoot(result_, Heap::kEmptyStringRootIndex); |
| 4690 | __ jmp(exit_label()); |
| 4691 | } |
| 4692 | |
| 4693 | private: |
| 4694 | Register result_; |
| 4695 | |
| 4696 | Label need_conversion_; |
| 4697 | Label index_out_of_range_; |
| 4698 | |
| 4699 | StringCharAtGenerator char_at_generator_; |
| 4700 | }; |
| 4701 | |
| 4702 | |
| 4703 | // This generates code that performs a String.prototype.charAt() call |
| 4704 | // or returns a smi in order to trigger conversion. |
| 4705 | void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4706 | Comment(masm_, "[ GenerateStringCharAt"); |
| 4707 | ASSERT(args->length() == 2); |
| 4708 | |
| 4709 | Load(args->at(0)); |
| 4710 | Load(args->at(1)); |
| 4711 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4712 | Register index = frame_->PopToRegister(); |
| 4713 | Register object = frame_->PopToRegister(index); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4714 | |
| 4715 | // We need three extra registers. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4716 | Register scratch1 = VirtualFrame::scratch0(); |
| 4717 | Register scratch2 = VirtualFrame::scratch1(); |
| 4718 | // Use r6 without notifying the virtual frame. |
| 4719 | Register result = r6; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4720 | |
| 4721 | DeferredStringCharAt* deferred = |
| 4722 | new DeferredStringCharAt(object, |
| 4723 | index, |
| 4724 | scratch1, |
| 4725 | scratch2, |
| 4726 | result); |
| 4727 | deferred->fast_case_generator()->GenerateFast(masm_); |
| 4728 | deferred->BindExit(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 4729 | frame_->EmitPush(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4733 | void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4734 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4735 | Load(args->at(0)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4736 | JumpTarget answer; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4737 | // We need the CC bits to come out as not_equal in the case where the |
| 4738 | // object is a smi. This can't be done with the usual test opcode so |
| 4739 | // we use XOR to get the right CC bits. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4740 | Register possible_array = frame_->PopToRegister(); |
| 4741 | Register scratch = VirtualFrame::scratch0(); |
| 4742 | __ and_(scratch, possible_array, Operand(kSmiTagMask)); |
| 4743 | __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4744 | answer.Branch(ne); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4745 | // It is a heap object - get the map. Check if the object is a JS array. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4746 | __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 4747 | answer.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4748 | cc_reg_ = eq; |
| 4749 | } |
| 4750 | |
| 4751 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4752 | void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4753 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4754 | Load(args->at(0)); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4755 | JumpTarget answer; |
| 4756 | // We need the CC bits to come out as not_equal in the case where the |
| 4757 | // object is a smi. This can't be done with the usual test opcode so |
| 4758 | // we use XOR to get the right CC bits. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4759 | Register possible_regexp = frame_->PopToRegister(); |
| 4760 | Register scratch = VirtualFrame::scratch0(); |
| 4761 | __ and_(scratch, possible_regexp, Operand(kSmiTagMask)); |
| 4762 | __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4763 | answer.Branch(ne); |
| 4764 | // It is a heap object - get the map. Check if the object is a regexp. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4765 | __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4766 | answer.Bind(); |
| 4767 | cc_reg_ = eq; |
| 4768 | } |
| 4769 | |
| 4770 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4771 | void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) { |
| 4772 | // This generates a fast version of: |
| 4773 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp') |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4774 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4775 | Load(args->at(0)); |
| 4776 | Register possible_object = frame_->PopToRegister(); |
| 4777 | __ tst(possible_object, Operand(kSmiTagMask)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4778 | false_target()->Branch(eq); |
| 4779 | |
| 4780 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4781 | __ cmp(possible_object, ip); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4782 | true_target()->Branch(eq); |
| 4783 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4784 | Register map_reg = VirtualFrame::scratch0(); |
| 4785 | __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4786 | // Undetectable objects behave like undefined when tested with typeof. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4787 | __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 4788 | __ tst(possible_object, Operand(1 << Map::kIsUndetectable)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4789 | false_target()->Branch(ne); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4790 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4791 | __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 4792 | __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4793 | false_target()->Branch(lt); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4794 | __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4795 | cc_reg_ = le; |
| 4796 | } |
| 4797 | |
| 4798 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 4799 | void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) { |
| 4800 | // This generates a fast version of: |
| 4801 | // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' || |
| 4802 | // typeof(arg) == function). |
| 4803 | // It includes undetectable objects (as opposed to IsObject). |
| 4804 | ASSERT(args->length() == 1); |
| 4805 | Load(args->at(0)); |
| 4806 | Register value = frame_->PopToRegister(); |
| 4807 | __ tst(value, Operand(kSmiTagMask)); |
| 4808 | false_target()->Branch(eq); |
| 4809 | // Check that this is an object. |
| 4810 | __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset)); |
| 4811 | __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset)); |
| 4812 | __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE)); |
| 4813 | cc_reg_ = ge; |
| 4814 | } |
| 4815 | |
| 4816 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 4817 | // Deferred code to check whether the String JavaScript object is safe for using |
| 4818 | // default value of. This code is called after the bit caching this information |
| 4819 | // in the map has been checked with the map for the object in the map_result_ |
| 4820 | // register. On return the register map_result_ contains 1 for true and 0 for |
| 4821 | // false. |
| 4822 | class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode { |
| 4823 | public: |
| 4824 | DeferredIsStringWrapperSafeForDefaultValueOf(Register object, |
| 4825 | Register map_result, |
| 4826 | Register scratch1, |
| 4827 | Register scratch2) |
| 4828 | : object_(object), |
| 4829 | map_result_(map_result), |
| 4830 | scratch1_(scratch1), |
| 4831 | scratch2_(scratch2) { } |
| 4832 | |
| 4833 | virtual void Generate() { |
| 4834 | Label false_result; |
| 4835 | |
| 4836 | // Check that map is loaded as expected. |
| 4837 | if (FLAG_debug_code) { |
| 4838 | __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 4839 | __ cmp(map_result_, ip); |
| 4840 | __ Assert(eq, "Map not in expected register"); |
| 4841 | } |
| 4842 | |
| 4843 | // Check for fast case object. Generate false result for slow case object. |
| 4844 | __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset)); |
| 4845 | __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset)); |
| 4846 | __ LoadRoot(ip, Heap::kHashTableMapRootIndex); |
| 4847 | __ cmp(scratch1_, ip); |
| 4848 | __ b(eq, &false_result); |
| 4849 | |
| 4850 | // Look for valueOf symbol in the descriptor array, and indicate false if |
| 4851 | // found. The type is not checked, so if it is a transition it is a false |
| 4852 | // negative. |
| 4853 | __ ldr(map_result_, |
| 4854 | FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset)); |
| 4855 | __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset)); |
| 4856 | // map_result_: descriptor array |
| 4857 | // scratch2_: length of descriptor array |
| 4858 | // Calculate the end of the descriptor array. |
| 4859 | STATIC_ASSERT(kSmiTag == 0); |
| 4860 | STATIC_ASSERT(kSmiTagSize == 1); |
| 4861 | STATIC_ASSERT(kPointerSize == 4); |
| 4862 | __ add(scratch1_, |
| 4863 | map_result_, |
| 4864 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 4865 | __ add(scratch1_, |
| 4866 | scratch1_, |
| 4867 | Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 4868 | |
| 4869 | // Calculate location of the first key name. |
| 4870 | __ add(map_result_, |
| 4871 | map_result_, |
| 4872 | Operand(FixedArray::kHeaderSize - kHeapObjectTag + |
| 4873 | DescriptorArray::kFirstIndex * kPointerSize)); |
| 4874 | // Loop through all the keys in the descriptor array. If one of these is the |
| 4875 | // symbol valueOf the result is false. |
| 4876 | Label entry, loop; |
| 4877 | // The use of ip to store the valueOf symbol asumes that it is not otherwise |
| 4878 | // used in the loop below. |
| 4879 | __ mov(ip, Operand(Factory::value_of_symbol())); |
| 4880 | __ jmp(&entry); |
| 4881 | __ bind(&loop); |
| 4882 | __ ldr(scratch2_, MemOperand(map_result_, 0)); |
| 4883 | __ cmp(scratch2_, ip); |
| 4884 | __ b(eq, &false_result); |
| 4885 | __ add(map_result_, map_result_, Operand(kPointerSize)); |
| 4886 | __ bind(&entry); |
| 4887 | __ cmp(map_result_, Operand(scratch1_)); |
| 4888 | __ b(ne, &loop); |
| 4889 | |
| 4890 | // Reload map as register map_result_ was used as temporary above. |
| 4891 | __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 4892 | |
| 4893 | // If a valueOf property is not found on the object check that it's |
| 4894 | // prototype is the un-modified String prototype. If not result is false. |
| 4895 | __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset)); |
| 4896 | __ tst(scratch1_, Operand(kSmiTagMask)); |
| 4897 | __ b(eq, &false_result); |
| 4898 | __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset)); |
| 4899 | __ ldr(scratch2_, |
| 4900 | CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 4901 | __ ldr(scratch2_, |
| 4902 | FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset)); |
| 4903 | __ ldr(scratch2_, |
| 4904 | CodeGenerator::ContextOperand( |
| 4905 | scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX)); |
| 4906 | __ cmp(scratch1_, scratch2_); |
| 4907 | __ b(ne, &false_result); |
| 4908 | |
| 4909 | // Set the bit in the map to indicate that it has been checked safe for |
| 4910 | // default valueOf and set true result. |
| 4911 | __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset)); |
| 4912 | __ orr(scratch1_, |
| 4913 | scratch1_, |
| 4914 | Operand(1 << Map::kStringWrapperSafeForDefaultValueOf)); |
| 4915 | __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset)); |
| 4916 | __ mov(map_result_, Operand(1)); |
| 4917 | __ jmp(exit_label()); |
| 4918 | __ bind(&false_result); |
| 4919 | // Set false result. |
| 4920 | __ mov(map_result_, Operand(0)); |
| 4921 | } |
| 4922 | |
| 4923 | private: |
| 4924 | Register object_; |
| 4925 | Register map_result_; |
| 4926 | Register scratch1_; |
| 4927 | Register scratch2_; |
| 4928 | }; |
| 4929 | |
| 4930 | |
| 4931 | void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf( |
| 4932 | ZoneList<Expression*>* args) { |
| 4933 | ASSERT(args->length() == 1); |
| 4934 | Load(args->at(0)); |
| 4935 | Register obj = frame_->PopToRegister(); // Pop the string wrapper. |
| 4936 | if (FLAG_debug_code) { |
| 4937 | __ AbortIfSmi(obj); |
| 4938 | } |
| 4939 | |
| 4940 | // Check whether this map has already been checked to be safe for default |
| 4941 | // valueOf. |
| 4942 | Register map_result = VirtualFrame::scratch0(); |
| 4943 | __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset)); |
| 4944 | __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset)); |
| 4945 | __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf)); |
| 4946 | true_target()->Branch(ne); |
| 4947 | |
| 4948 | // We need an additional two scratch registers for the deferred code. |
| 4949 | Register scratch1 = VirtualFrame::scratch1(); |
| 4950 | // Use r6 without notifying the virtual frame. |
| 4951 | Register scratch2 = r6; |
| 4952 | |
| 4953 | DeferredIsStringWrapperSafeForDefaultValueOf* deferred = |
| 4954 | new DeferredIsStringWrapperSafeForDefaultValueOf( |
| 4955 | obj, map_result, scratch1, scratch2); |
| 4956 | deferred->Branch(eq); |
| 4957 | deferred->BindExit(); |
| 4958 | __ tst(map_result, Operand(map_result)); |
| 4959 | cc_reg_ = ne; |
| 4960 | } |
| 4961 | |
| 4962 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4963 | void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) { |
| 4964 | // This generates a fast version of: |
| 4965 | // (%_ClassOf(arg) === 'Function') |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4966 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4967 | Load(args->at(0)); |
| 4968 | Register possible_function = frame_->PopToRegister(); |
| 4969 | __ tst(possible_function, Operand(kSmiTagMask)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4970 | false_target()->Branch(eq); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4971 | Register map_reg = VirtualFrame::scratch0(); |
| 4972 | Register scratch = VirtualFrame::scratch1(); |
| 4973 | __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4974 | cc_reg_ = eq; |
| 4975 | } |
| 4976 | |
| 4977 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4978 | void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4979 | ASSERT(args->length() == 1); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4980 | Load(args->at(0)); |
| 4981 | Register possible_undetectable = frame_->PopToRegister(); |
| 4982 | __ tst(possible_undetectable, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4983 | false_target()->Branch(eq); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4984 | Register scratch = VirtualFrame::scratch0(); |
| 4985 | __ ldr(scratch, |
| 4986 | FieldMemOperand(possible_undetectable, HeapObject::kMapOffset)); |
| 4987 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset)); |
| 4988 | __ tst(scratch, Operand(1 << Map::kIsUndetectable)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4989 | cc_reg_ = ne; |
| 4990 | } |
| 4991 | |
| 4992 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4993 | void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4994 | ASSERT(args->length() == 0); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4995 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4996 | Register scratch0 = VirtualFrame::scratch0(); |
| 4997 | Register scratch1 = VirtualFrame::scratch1(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4998 | // Get the frame pointer for the calling frame. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 4999 | __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 5000 | |
| 5001 | // Skip the arguments adaptor frame if it exists. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5002 | __ ldr(scratch1, |
| 5003 | MemOperand(scratch0, StandardFrameConstants::kContextOffset)); |
| 5004 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 5005 | __ ldr(scratch0, |
| 5006 | MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 5007 | |
| 5008 | // Check the marker in the calling frame. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5009 | __ ldr(scratch1, |
| 5010 | MemOperand(scratch0, StandardFrameConstants::kMarkerOffset)); |
| 5011 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT))); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 5012 | cc_reg_ = eq; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5016 | void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5017 | ASSERT(args->length() == 0); |
| 5018 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5019 | Register tos = frame_->GetTOSRegister(); |
| 5020 | Register scratch0 = VirtualFrame::scratch0(); |
| 5021 | Register scratch1 = VirtualFrame::scratch1(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5022 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5023 | // Check if the calling frame is an arguments adaptor frame. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5024 | __ ldr(scratch0, |
| 5025 | MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 5026 | __ ldr(scratch1, |
| 5027 | MemOperand(scratch0, StandardFrameConstants::kContextOffset)); |
| 5028 | __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 5029 | |
| 5030 | // Get the number of formal parameters. |
| 5031 | __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5032 | |
| 5033 | // Arguments adaptor case: Read the arguments length from the |
| 5034 | // adaptor frame. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5035 | __ ldr(tos, |
| 5036 | MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset), |
| 5037 | eq); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5038 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5039 | frame_->EmitPush(tos); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
| 5042 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 5043 | void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5044 | ASSERT(args->length() == 1); |
| 5045 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 5046 | // Satisfy contract with ArgumentsAccessStub: |
| 5047 | // Load the key into r1 and the formal parameters count into r0. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5048 | Load(args->at(0)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5049 | frame_->PopToR1(); |
| 5050 | frame_->SpillAll(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5051 | __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters()))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5052 | |
| 5053 | // Call the shared stub to get to arguments[key]. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5054 | ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5055 | frame_->CallStub(&stub, 0); |
| 5056 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5060 | void CodeGenerator::GenerateRandomHeapNumber( |
| 5061 | ZoneList<Expression*>* args) { |
| 5062 | VirtualFrame::SpilledScope spilled_scope(frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5063 | ASSERT(args->length() == 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5064 | |
| 5065 | Label slow_allocate_heapnumber; |
| 5066 | Label heapnumber_allocated; |
| 5067 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5068 | __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex); |
| 5069 | __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5070 | __ jmp(&heapnumber_allocated); |
| 5071 | |
| 5072 | __ bind(&slow_allocate_heapnumber); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 5073 | // Allocate a heap number. |
| 5074 | __ CallRuntime(Runtime::kNumberAlloc, 0); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 5075 | __ mov(r4, Operand(r0)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5076 | |
| 5077 | __ bind(&heapnumber_allocated); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 5078 | |
| 5079 | // Convert 32 random bits in r0 to 0.(32 random bits) in a double |
| 5080 | // by computing: |
| 5081 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 5082 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5083 | __ PrepareCallCFunction(0, r1); |
| 5084 | __ CallCFunction(ExternalReference::random_uint32_function(), 0); |
| 5085 | |
| 5086 | CpuFeatures::Scope scope(VFP3); |
| 5087 | // 0x41300000 is the top half of 1.0 x 2^20 as a double. |
| 5088 | // Create this constant using mov/orr to avoid PC relative load. |
| 5089 | __ mov(r1, Operand(0x41000000)); |
| 5090 | __ orr(r1, r1, Operand(0x300000)); |
| 5091 | // Move 0x41300000xxxxxxxx (x = random bits) to VFP. |
| 5092 | __ vmov(d7, r0, r1); |
| 5093 | // Move 0x4130000000000000 to VFP. |
| 5094 | __ mov(r0, Operand(0)); |
| 5095 | __ vmov(d8, r0, r1); |
| 5096 | // Subtract and store the result in the heap number. |
| 5097 | __ vsub(d7, d7, d8); |
| 5098 | __ sub(r0, r4, Operand(kHeapObjectTag)); |
| 5099 | __ vstr(d7, r0, HeapNumber::kValueOffset); |
| 5100 | frame_->EmitPush(r4); |
| 5101 | } else { |
| 5102 | __ mov(r0, Operand(r4)); |
| 5103 | __ PrepareCallCFunction(1, r1); |
| 5104 | __ CallCFunction( |
| 5105 | ExternalReference::fill_heap_number_with_random_function(), 1); |
| 5106 | frame_->EmitPush(r0); |
| 5107 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5111 | void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) { |
| 5112 | ASSERT_EQ(2, args->length()); |
| 5113 | |
| 5114 | Load(args->at(0)); |
| 5115 | Load(args->at(1)); |
| 5116 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5117 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5118 | frame_->SpillAll(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5119 | frame_->CallStub(&stub, 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5120 | frame_->EmitPush(r0); |
| 5121 | } |
| 5122 | |
| 5123 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5124 | void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) { |
| 5125 | ASSERT_EQ(3, args->length()); |
| 5126 | |
| 5127 | Load(args->at(0)); |
| 5128 | Load(args->at(1)); |
| 5129 | Load(args->at(2)); |
| 5130 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5131 | SubStringStub stub; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5132 | frame_->SpillAll(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5133 | frame_->CallStub(&stub, 3); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5134 | frame_->EmitPush(r0); |
| 5135 | } |
| 5136 | |
| 5137 | |
| 5138 | void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) { |
| 5139 | ASSERT_EQ(2, args->length()); |
| 5140 | |
| 5141 | Load(args->at(0)); |
| 5142 | Load(args->at(1)); |
| 5143 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5144 | StringCompareStub stub; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5145 | frame_->SpillAll(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5146 | frame_->CallStub(&stub, 2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5147 | frame_->EmitPush(r0); |
| 5148 | } |
| 5149 | |
| 5150 | |
| 5151 | void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) { |
| 5152 | ASSERT_EQ(4, args->length()); |
| 5153 | |
| 5154 | Load(args->at(0)); |
| 5155 | Load(args->at(1)); |
| 5156 | Load(args->at(2)); |
| 5157 | Load(args->at(3)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5158 | RegExpExecStub stub; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5159 | frame_->SpillAll(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5160 | frame_->CallStub(&stub, 4); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 5161 | frame_->EmitPush(r0); |
| 5162 | } |
| 5163 | |
| 5164 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 5165 | void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) { |
| 5166 | // No stub. This code only occurs a few times in regexp.js. |
| 5167 | const int kMaxInlineLength = 100; |
| 5168 | ASSERT_EQ(3, args->length()); |
| 5169 | Load(args->at(0)); // Size of array, smi. |
| 5170 | Load(args->at(1)); // "index" property value. |
| 5171 | Load(args->at(2)); // "input" property value. |
| 5172 | { |
| 5173 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5174 | Label slowcase; |
| 5175 | Label done; |
| 5176 | __ ldr(r1, MemOperand(sp, kPointerSize * 2)); |
| 5177 | STATIC_ASSERT(kSmiTag == 0); |
| 5178 | STATIC_ASSERT(kSmiTagSize == 1); |
| 5179 | __ tst(r1, Operand(kSmiTagMask)); |
| 5180 | __ b(ne, &slowcase); |
| 5181 | __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength))); |
| 5182 | __ b(hi, &slowcase); |
| 5183 | // Smi-tagging is equivalent to multiplying by 2. |
| 5184 | // Allocate RegExpResult followed by FixedArray with size in ebx. |
| 5185 | // JSArray: [Map][empty properties][Elements][Length-smi][index][input] |
| 5186 | // Elements: [Map][Length][..elements..] |
| 5187 | // Size of JSArray with two in-object properties and the header of a |
| 5188 | // FixedArray. |
| 5189 | int objects_size = |
| 5190 | (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize; |
| 5191 | __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize)); |
| 5192 | __ add(r2, r5, Operand(objects_size)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 5193 | __ AllocateInNewSpace( |
| 5194 | r2, // In: Size, in words. |
| 5195 | r0, // Out: Start of allocation (tagged). |
| 5196 | r3, // Scratch register. |
| 5197 | r4, // Scratch register. |
| 5198 | &slowcase, |
| 5199 | static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 5200 | // r0: Start of allocated area, object-tagged. |
| 5201 | // r1: Number of elements in array, as smi. |
| 5202 | // r5: Number of elements, untagged. |
| 5203 | |
| 5204 | // Set JSArray map to global.regexp_result_map(). |
| 5205 | // Set empty properties FixedArray. |
| 5206 | // Set elements to point to FixedArray allocated right after the JSArray. |
| 5207 | // Interleave operations for better latency. |
| 5208 | __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 5209 | __ add(r3, r0, Operand(JSRegExpResult::kSize)); |
| 5210 | __ mov(r4, Operand(Factory::empty_fixed_array())); |
| 5211 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 5212 | __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 5213 | __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX)); |
| 5214 | __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 5215 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 5216 | |
| 5217 | // Set input, index and length fields from arguments. |
| 5218 | __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit())); |
| 5219 | __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset)); |
| 5220 | __ add(sp, sp, Operand(kPointerSize)); |
| 5221 | __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset)); |
| 5222 | __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset)); |
| 5223 | |
| 5224 | // Fill out the elements FixedArray. |
| 5225 | // r0: JSArray, tagged. |
| 5226 | // r3: FixedArray, tagged. |
| 5227 | // r5: Number of elements in array, untagged. |
| 5228 | |
| 5229 | // Set map. |
| 5230 | __ mov(r2, Operand(Factory::fixed_array_map())); |
| 5231 | __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset)); |
| 5232 | // Set FixedArray length. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5233 | __ mov(r6, Operand(r5, LSL, kSmiTagSize)); |
| 5234 | __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 5235 | // Fill contents of fixed-array with the-hole. |
| 5236 | __ mov(r2, Operand(Factory::the_hole_value())); |
| 5237 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5238 | // Fill fixed array elements with hole. |
| 5239 | // r0: JSArray, tagged. |
| 5240 | // r2: the hole. |
| 5241 | // r3: Start of elements in FixedArray. |
| 5242 | // r5: Number of elements to fill. |
| 5243 | Label loop; |
| 5244 | __ tst(r5, Operand(r5)); |
| 5245 | __ bind(&loop); |
| 5246 | __ b(le, &done); // Jump if r1 is negative or zero. |
| 5247 | __ sub(r5, r5, Operand(1), SetCC); |
| 5248 | __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2)); |
| 5249 | __ jmp(&loop); |
| 5250 | |
| 5251 | __ bind(&slowcase); |
| 5252 | __ CallRuntime(Runtime::kRegExpConstructResult, 3); |
| 5253 | |
| 5254 | __ bind(&done); |
| 5255 | } |
| 5256 | frame_->Forget(3); |
| 5257 | frame_->EmitPush(r0); |
| 5258 | } |
| 5259 | |
| 5260 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5261 | class DeferredSearchCache: public DeferredCode { |
| 5262 | public: |
| 5263 | DeferredSearchCache(Register dst, Register cache, Register key) |
| 5264 | : dst_(dst), cache_(cache), key_(key) { |
| 5265 | set_comment("[ DeferredSearchCache"); |
| 5266 | } |
| 5267 | |
| 5268 | virtual void Generate(); |
| 5269 | |
| 5270 | private: |
| 5271 | Register dst_, cache_, key_; |
| 5272 | }; |
| 5273 | |
| 5274 | |
| 5275 | void DeferredSearchCache::Generate() { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 5276 | __ Push(cache_, key_); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5277 | __ CallRuntime(Runtime::kGetFromCache, 2); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5278 | __ Move(dst_, r0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | |
| 5282 | void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) { |
| 5283 | ASSERT_EQ(2, args->length()); |
| 5284 | |
| 5285 | ASSERT_NE(NULL, args->at(0)->AsLiteral()); |
| 5286 | int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value(); |
| 5287 | |
| 5288 | Handle<FixedArray> jsfunction_result_caches( |
| 5289 | Top::global_context()->jsfunction_result_caches()); |
| 5290 | if (jsfunction_result_caches->length() <= cache_id) { |
| 5291 | __ Abort("Attempt to use undefined cache."); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5292 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5293 | return; |
| 5294 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5295 | |
| 5296 | Load(args->at(1)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5297 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5298 | frame_->PopToR1(); |
| 5299 | frame_->SpillAll(); |
| 5300 | Register key = r1; // Just poped to r1 |
| 5301 | Register result = r0; // Free, as frame has just been spilled. |
| 5302 | Register scratch1 = VirtualFrame::scratch0(); |
| 5303 | Register scratch2 = VirtualFrame::scratch1(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5304 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5305 | __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX)); |
| 5306 | __ ldr(scratch1, |
| 5307 | FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset)); |
| 5308 | __ ldr(scratch1, |
| 5309 | ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX)); |
| 5310 | __ ldr(scratch1, |
| 5311 | FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id))); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5312 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5313 | DeferredSearchCache* deferred = |
| 5314 | new DeferredSearchCache(result, scratch1, key); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5315 | |
| 5316 | const int kFingerOffset = |
| 5317 | FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 5318 | STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5319 | __ ldr(result, FieldMemOperand(scratch1, kFingerOffset)); |
| 5320 | // result now holds finger offset as a smi. |
| 5321 | __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5322 | // scratch2 now points to the start of fixed array elements. |
| 5323 | __ ldr(result, |
| 5324 | MemOperand( |
| 5325 | scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex)); |
| 5326 | // Note side effect of PreIndex: scratch2 now points to the key of the pair. |
| 5327 | __ cmp(key, result); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5328 | deferred->Branch(ne); |
| 5329 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5330 | __ ldr(result, MemOperand(scratch2, kPointerSize)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5331 | |
| 5332 | deferred->BindExit(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 5333 | frame_->EmitPush(result); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5337 | void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) { |
| 5338 | ASSERT_EQ(args->length(), 1); |
| 5339 | |
| 5340 | // Load the argument on the stack and jump to the runtime. |
| 5341 | Load(args->at(0)); |
| 5342 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5343 | NumberToStringStub stub; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5344 | frame_->SpillAll(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5345 | frame_->CallStub(&stub, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5346 | frame_->EmitPush(r0); |
| 5347 | } |
| 5348 | |
| 5349 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 5350 | class DeferredSwapElements: public DeferredCode { |
| 5351 | public: |
| 5352 | DeferredSwapElements(Register object, Register index1, Register index2) |
| 5353 | : object_(object), index1_(index1), index2_(index2) { |
| 5354 | set_comment("[ DeferredSwapElements"); |
| 5355 | } |
| 5356 | |
| 5357 | virtual void Generate(); |
| 5358 | |
| 5359 | private: |
| 5360 | Register object_, index1_, index2_; |
| 5361 | }; |
| 5362 | |
| 5363 | |
| 5364 | void DeferredSwapElements::Generate() { |
| 5365 | __ push(object_); |
| 5366 | __ push(index1_); |
| 5367 | __ push(index2_); |
| 5368 | __ CallRuntime(Runtime::kSwapElements, 3); |
| 5369 | } |
| 5370 | |
| 5371 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 5372 | void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) { |
| 5373 | Comment cmnt(masm_, "[ GenerateSwapElements"); |
| 5374 | |
| 5375 | ASSERT_EQ(3, args->length()); |
| 5376 | |
| 5377 | Load(args->at(0)); |
| 5378 | Load(args->at(1)); |
| 5379 | Load(args->at(2)); |
| 5380 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5381 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5382 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 5383 | Register index2 = r2; |
| 5384 | Register index1 = r1; |
| 5385 | Register object = r0; |
| 5386 | Register tmp1 = r3; |
| 5387 | Register tmp2 = r4; |
| 5388 | |
| 5389 | frame_->EmitPop(index2); |
| 5390 | frame_->EmitPop(index1); |
| 5391 | frame_->EmitPop(object); |
| 5392 | |
| 5393 | DeferredSwapElements* deferred = |
| 5394 | new DeferredSwapElements(object, index1, index2); |
| 5395 | |
| 5396 | // Fetch the map and check if array is in fast case. |
| 5397 | // Check that object doesn't require security checks and |
| 5398 | // has no indexed interceptor. |
| 5399 | __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE); |
| 5400 | deferred->Branch(lt); |
| 5401 | __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset)); |
| 5402 | __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask)); |
| 5403 | deferred->Branch(nz); |
| 5404 | |
| 5405 | // Check the object's elements are in fast case. |
| 5406 | __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset)); |
| 5407 | __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset)); |
| 5408 | __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex); |
| 5409 | __ cmp(tmp2, ip); |
| 5410 | deferred->Branch(ne); |
| 5411 | |
| 5412 | // Smi-tagging is equivalent to multiplying by 2. |
| 5413 | STATIC_ASSERT(kSmiTag == 0); |
| 5414 | STATIC_ASSERT(kSmiTagSize == 1); |
| 5415 | |
| 5416 | // Check that both indices are smis. |
| 5417 | __ mov(tmp2, index1); |
| 5418 | __ orr(tmp2, tmp2, index2); |
| 5419 | __ tst(tmp2, Operand(kSmiTagMask)); |
| 5420 | deferred->Branch(nz); |
| 5421 | |
| 5422 | // Bring the offsets into the fixed array in tmp1 into index1 and |
| 5423 | // index2. |
| 5424 | __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 5425 | __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5426 | __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 5427 | |
| 5428 | // Swap elements. |
| 5429 | Register tmp3 = object; |
| 5430 | object = no_reg; |
| 5431 | __ ldr(tmp3, MemOperand(tmp1, index1)); |
| 5432 | __ ldr(tmp2, MemOperand(tmp1, index2)); |
| 5433 | __ str(tmp3, MemOperand(tmp1, index2)); |
| 5434 | __ str(tmp2, MemOperand(tmp1, index1)); |
| 5435 | |
| 5436 | Label done; |
| 5437 | __ InNewSpace(tmp1, tmp2, eq, &done); |
| 5438 | // Possible optimization: do a check that both values are Smis |
| 5439 | // (or them and test against Smi mask.) |
| 5440 | |
| 5441 | __ mov(tmp2, tmp1); |
| 5442 | RecordWriteStub recordWrite1(tmp1, index1, tmp3); |
| 5443 | __ CallStub(&recordWrite1); |
| 5444 | |
| 5445 | RecordWriteStub recordWrite2(tmp2, index2, tmp3); |
| 5446 | __ CallStub(&recordWrite2); |
| 5447 | |
| 5448 | __ bind(&done); |
| 5449 | |
| 5450 | deferred->BindExit(); |
| 5451 | __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex); |
| 5452 | frame_->EmitPush(tmp1); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5456 | void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) { |
| 5457 | Comment cmnt(masm_, "[ GenerateCallFunction"); |
| 5458 | |
| 5459 | ASSERT(args->length() >= 2); |
| 5460 | |
| 5461 | int n_args = args->length() - 2; // for receiver and function. |
| 5462 | Load(args->at(0)); // receiver |
| 5463 | for (int i = 0; i < n_args; i++) { |
| 5464 | Load(args->at(i + 1)); |
| 5465 | } |
| 5466 | Load(args->at(n_args + 1)); // function |
| 5467 | frame_->CallJSFunction(n_args); |
| 5468 | frame_->EmitPush(r0); |
| 5469 | } |
| 5470 | |
| 5471 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5472 | void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) { |
| 5473 | ASSERT_EQ(args->length(), 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5474 | Load(args->at(0)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 5475 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5476 | TranscendentalCacheStub stub(TranscendentalCache::SIN); |
| 5477 | frame_->SpillAllButCopyTOSToR0(); |
| 5478 | frame_->CallStub(&stub, 1); |
| 5479 | } else { |
| 5480 | frame_->CallRuntime(Runtime::kMath_sin, 1); |
| 5481 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5482 | frame_->EmitPush(r0); |
| 5483 | } |
| 5484 | |
| 5485 | |
| 5486 | void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) { |
| 5487 | ASSERT_EQ(args->length(), 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5488 | Load(args->at(0)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 5489 | if (CpuFeatures::IsSupported(VFP3)) { |
| 5490 | TranscendentalCacheStub stub(TranscendentalCache::COS); |
| 5491 | frame_->SpillAllButCopyTOSToR0(); |
| 5492 | frame_->CallStub(&stub, 1); |
| 5493 | } else { |
| 5494 | frame_->CallRuntime(Runtime::kMath_cos, 1); |
| 5495 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5496 | frame_->EmitPush(r0); |
| 5497 | } |
| 5498 | |
| 5499 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5500 | void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 5501 | ASSERT(args->length() == 2); |
| 5502 | |
| 5503 | // Load the two objects into registers and perform the comparison. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5504 | Load(args->at(0)); |
| 5505 | Load(args->at(1)); |
| 5506 | Register lhs = frame_->PopToRegister(); |
| 5507 | Register rhs = frame_->PopToRegister(lhs); |
| 5508 | __ cmp(lhs, rhs); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 5509 | cc_reg_ = eq; |
| 5510 | } |
| 5511 | |
| 5512 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 5513 | void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) { |
| 5514 | ASSERT(args->length() == 2); |
| 5515 | |
| 5516 | // Load the two objects into registers and perform the comparison. |
| 5517 | Load(args->at(0)); |
| 5518 | Load(args->at(1)); |
| 5519 | Register right = frame_->PopToRegister(); |
| 5520 | Register left = frame_->PopToRegister(right); |
| 5521 | Register tmp = frame_->scratch0(); |
| 5522 | Register tmp2 = frame_->scratch1(); |
| 5523 | |
| 5524 | // Jumps to done must have the eq flag set if the test is successful |
| 5525 | // and clear if the test has failed. |
| 5526 | Label done; |
| 5527 | |
| 5528 | // Fail if either is a non-HeapObject. |
| 5529 | __ cmp(left, Operand(right)); |
| 5530 | __ b(eq, &done); |
| 5531 | __ and_(tmp, left, Operand(right)); |
| 5532 | __ eor(tmp, tmp, Operand(kSmiTagMask)); |
| 5533 | __ tst(tmp, Operand(kSmiTagMask)); |
| 5534 | __ b(ne, &done); |
| 5535 | __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset)); |
| 5536 | __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset)); |
| 5537 | __ cmp(tmp2, Operand(JS_REGEXP_TYPE)); |
| 5538 | __ b(ne, &done); |
| 5539 | __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset)); |
| 5540 | __ cmp(tmp, Operand(tmp2)); |
| 5541 | __ b(ne, &done); |
| 5542 | __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset)); |
| 5543 | __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset)); |
| 5544 | __ cmp(tmp, tmp2); |
| 5545 | __ bind(&done); |
| 5546 | cc_reg_ = eq; |
| 5547 | } |
| 5548 | |
| 5549 | |
| 5550 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5551 | void CodeGenerator::VisitCallRuntime(CallRuntime* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5552 | #ifdef DEBUG |
| 5553 | int original_height = frame_->height(); |
| 5554 | #endif |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5555 | if (CheckForInlineRuntimeCall(node)) { |
| 5556 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 5557 | (!has_cc() && frame_->height() == original_height + 1)); |
| 5558 | return; |
| 5559 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5560 | |
| 5561 | ZoneList<Expression*>* args = node->arguments(); |
| 5562 | Comment cmnt(masm_, "[ CallRuntime"); |
| 5563 | Runtime::Function* function = node->function(); |
| 5564 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5565 | if (function == NULL) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5566 | // Prepare stack for calling JS runtime function. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5567 | // Push the builtins object found in the current global object. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5568 | Register scratch = VirtualFrame::scratch0(); |
| 5569 | __ ldr(scratch, GlobalObject()); |
| 5570 | Register builtins = frame_->GetTOSRegister(); |
| 5571 | __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset)); |
| 5572 | frame_->EmitPush(builtins); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5573 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5574 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5575 | // Push the arguments ("left-to-right"). |
| 5576 | int arg_count = args->length(); |
| 5577 | for (int i = 0; i < arg_count; i++) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5578 | Load(args->at(i)); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5579 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5580 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5581 | VirtualFrame::SpilledScope spilled_scope(frame_); |
| 5582 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5583 | if (function == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5584 | // Call the JS runtime function. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5585 | __ mov(r2, Operand(node->name())); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5586 | InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP; |
| 5587 | Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5588 | frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5589 | __ ldr(cp, frame_->Context()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5590 | frame_->EmitPush(r0); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5591 | } else { |
| 5592 | // Call the C runtime function. |
| 5593 | frame_->CallRuntime(function, arg_count); |
| 5594 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5595 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 5596 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5600 | void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5601 | #ifdef DEBUG |
| 5602 | int original_height = frame_->height(); |
| 5603 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5604 | Comment cmnt(masm_, "[ UnaryOperation"); |
| 5605 | |
| 5606 | Token::Value op = node->op(); |
| 5607 | |
| 5608 | if (op == Token::NOT) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5609 | LoadCondition(node->expression(), false_target(), true_target(), true); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 5610 | // LoadCondition may (and usually does) leave a test and branch to |
| 5611 | // be emitted by the caller. In that case, negate the condition. |
| 5612 | if (has_cc()) cc_reg_ = NegateCondition(cc_reg_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5613 | |
| 5614 | } else if (op == Token::DELETE) { |
| 5615 | Property* property = node->expression()->AsProperty(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5616 | Variable* variable = node->expression()->AsVariableProxy()->AsVariable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5617 | if (property != NULL) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5618 | Load(property->obj()); |
| 5619 | Load(property->key()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5620 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5621 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5622 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5623 | } else if (variable != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5624 | Slot* slot = variable->slot(); |
| 5625 | if (variable->is_global()) { |
| 5626 | LoadGlobal(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5627 | frame_->EmitPush(Operand(variable->name())); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5628 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5629 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5630 | |
| 5631 | } else if (slot != NULL && slot->type() == Slot::LOOKUP) { |
| 5632 | // lookup the context holding the named variable |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5633 | frame_->EmitPush(cp); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5634 | frame_->EmitPush(Operand(variable->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5635 | frame_->CallRuntime(Runtime::kLookupContext, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5636 | // r0: context |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5637 | frame_->EmitPush(r0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5638 | frame_->EmitPush(Operand(variable->name())); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5639 | frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5640 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5641 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5642 | } else { |
| 5643 | // Default: Result of deleting non-global, not dynamically |
| 5644 | // introduced variables is false. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5645 | frame_->EmitPushRoot(Heap::kFalseValueRootIndex); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5646 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5647 | |
| 5648 | } else { |
| 5649 | // Default: Result of deleting expressions is true. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5650 | Load(node->expression()); // may have side-effects |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5651 | frame_->Drop(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5652 | frame_->EmitPushRoot(Heap::kTrueValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
| 5655 | } else if (op == Token::TYPEOF) { |
| 5656 | // Special case for loading the typeof expression; see comment on |
| 5657 | // LoadTypeofExpression(). |
| 5658 | LoadTypeofExpression(node->expression()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5659 | frame_->CallRuntime(Runtime::kTypeof, 1); |
| 5660 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5661 | |
| 5662 | } else { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 5663 | bool can_overwrite = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5664 | (node->expression()->AsBinaryOperation() != NULL && |
| 5665 | node->expression()->AsBinaryOperation()->ResultOverwriteAllowed()); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 5666 | UnaryOverwriteMode overwrite = |
| 5667 | can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE; |
| 5668 | |
| 5669 | bool no_negative_zero = node->expression()->no_negative_zero(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5670 | Load(node->expression()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5671 | switch (op) { |
| 5672 | case Token::NOT: |
| 5673 | case Token::DELETE: |
| 5674 | case Token::TYPEOF: |
| 5675 | UNREACHABLE(); // handled above |
| 5676 | break; |
| 5677 | |
| 5678 | case Token::SUB: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5679 | frame_->PopToR0(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 5680 | GenericUnaryOpStub stub( |
| 5681 | Token::SUB, |
| 5682 | overwrite, |
| 5683 | no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5684 | frame_->CallStub(&stub, 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5685 | frame_->EmitPush(r0); // r0 has result |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5686 | break; |
| 5687 | } |
| 5688 | |
| 5689 | case Token::BIT_NOT: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5690 | Register tos = frame_->PopToRegister(); |
| 5691 | JumpTarget not_smi_label; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5692 | JumpTarget continue_label; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5693 | // Smi check. |
| 5694 | __ tst(tos, Operand(kSmiTagMask)); |
| 5695 | not_smi_label.Branch(ne); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5696 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5697 | __ mvn(tos, Operand(tos)); |
| 5698 | __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag. |
| 5699 | frame_->EmitPush(tos); |
| 5700 | // The fast case is the first to jump to the continue label, so it gets |
| 5701 | // to decide the virtual frame layout. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5702 | continue_label.Jump(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5703 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5704 | not_smi_label.Bind(); |
| 5705 | frame_->SpillAll(); |
| 5706 | __ Move(r0, tos); |
| 5707 | GenericUnaryOpStub stub(Token::BIT_NOT, overwrite); |
| 5708 | frame_->CallStub(&stub, 0); |
| 5709 | frame_->EmitPush(r0); |
| 5710 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5711 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5712 | break; |
| 5713 | } |
| 5714 | |
| 5715 | case Token::VOID: |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5716 | frame_->Drop(); |
| 5717 | frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5718 | break; |
| 5719 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5720 | case Token::ADD: { |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5721 | Register tos = frame_->Peek(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5722 | // Smi check. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5723 | JumpTarget continue_label; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5724 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5725 | continue_label.Branch(eq); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5726 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5727 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5728 | frame_->EmitPush(r0); |
| 5729 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5730 | continue_label.Bind(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5731 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5732 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5733 | default: |
| 5734 | UNREACHABLE(); |
| 5735 | } |
| 5736 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5737 | ASSERT(!has_valid_frame() || |
| 5738 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5739 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5743 | void CodeGenerator::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5744 | #ifdef DEBUG |
| 5745 | int original_height = frame_->height(); |
| 5746 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5747 | Comment cmnt(masm_, "[ CountOperation"); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 5748 | VirtualFrame::RegisterAllocationScope scope(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5749 | |
| 5750 | bool is_postfix = node->is_postfix(); |
| 5751 | bool is_increment = node->op() == Token::INC; |
| 5752 | |
| 5753 | Variable* var = node->expression()->AsVariableProxy()->AsVariable(); |
| 5754 | bool is_const = (var != NULL && var->mode() == Variable::CONST); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5755 | bool is_slot = (var != NULL && var->mode() == Variable::VAR); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5756 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5757 | if (!is_const && is_slot && type_info(var->slot()).IsSmi()) { |
| 5758 | // The type info declares that this variable is always a Smi. That |
| 5759 | // means it is a Smi both before and after the increment/decrement. |
| 5760 | // Lets make use of that to make a very minimal count. |
| 5761 | Reference target(this, node->expression(), !is_const); |
| 5762 | ASSERT(!target.is_illegal()); |
| 5763 | target.GetValue(); // Pushes the value. |
| 5764 | Register value = frame_->PopToRegister(); |
| 5765 | if (is_postfix) frame_->EmitPush(value); |
| 5766 | if (is_increment) { |
| 5767 | __ add(value, value, Operand(Smi::FromInt(1))); |
| 5768 | } else { |
| 5769 | __ sub(value, value, Operand(Smi::FromInt(1))); |
| 5770 | } |
| 5771 | frame_->EmitPush(value); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5772 | target.SetValue(NOT_CONST_INIT, LIKELY_SMI); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5773 | if (is_postfix) frame_->Pop(); |
| 5774 | ASSERT_EQ(original_height + 1, frame_->height()); |
| 5775 | return; |
| 5776 | } |
| 5777 | |
| 5778 | // If it's a postfix expression and its result is not ignored and the |
| 5779 | // reference is non-trivial, then push a placeholder on the stack now |
| 5780 | // to hold the result of the expression. |
| 5781 | bool placeholder_pushed = false; |
| 5782 | if (!is_slot && is_postfix) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5783 | frame_->EmitPush(Operand(Smi::FromInt(0))); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5784 | placeholder_pushed = true; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5785 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5786 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5787 | // A constant reference is not saved to, so a constant reference is not a |
| 5788 | // compound assignment reference. |
| 5789 | { Reference target(this, node->expression(), !is_const); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5790 | if (target.is_illegal()) { |
| 5791 | // Spoof the virtual frame to have the expected height (one higher |
| 5792 | // than on entry). |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5793 | if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0))); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 5794 | ASSERT_EQ(original_height + 1, frame_->height()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5795 | return; |
| 5796 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5797 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5798 | // This pushes 0, 1 or 2 words on the object to be used later when updating |
| 5799 | // the target. It also pushes the current value of the target. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5800 | target.GetValue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5801 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5802 | JumpTarget slow; |
| 5803 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5804 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5805 | Register value = frame_->PopToRegister(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5806 | |
| 5807 | // Postfix: Store the old value as the result. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5808 | if (placeholder_pushed) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5809 | frame_->SetElementAt(value, target.size()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5810 | } else if (is_postfix) { |
| 5811 | frame_->EmitPush(value); |
| 5812 | __ mov(VirtualFrame::scratch0(), value); |
| 5813 | value = VirtualFrame::scratch0(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 5814 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5815 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5816 | // Check for smi operand. |
| 5817 | __ tst(value, Operand(kSmiTagMask)); |
| 5818 | slow.Branch(ne); |
| 5819 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5820 | // Perform optimistic increment/decrement. |
| 5821 | if (is_increment) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5822 | __ add(value, value, Operand(Smi::FromInt(1)), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5823 | } else { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5824 | __ sub(value, value, Operand(Smi::FromInt(1)), SetCC); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | // If the increment/decrement didn't overflow, we're done. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5828 | exit.Branch(vc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5829 | |
| 5830 | // Revert optimistic increment/decrement. |
| 5831 | if (is_increment) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5832 | __ sub(value, value, Operand(Smi::FromInt(1))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5833 | } else { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5834 | __ add(value, value, Operand(Smi::FromInt(1))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5837 | // Slow case: Convert to number. At this point the |
| 5838 | // value to be incremented is in the value register.. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5839 | slow.Bind(); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5840 | |
| 5841 | // Convert the operand to a number. |
| 5842 | frame_->EmitPush(value); |
| 5843 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5844 | { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5845 | VirtualFrame::SpilledScope spilled(frame_); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5846 | frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5847 | |
| 5848 | if (is_postfix) { |
| 5849 | // Postfix: store to result (on the stack). |
| 5850 | __ str(r0, frame_->ElementAt(target.size())); |
| 5851 | } |
| 5852 | |
| 5853 | // Compute the new value. |
| 5854 | frame_->EmitPush(r0); |
| 5855 | frame_->EmitPush(Operand(Smi::FromInt(1))); |
| 5856 | if (is_increment) { |
| 5857 | frame_->CallRuntime(Runtime::kNumberAdd, 2); |
| 5858 | } else { |
| 5859 | frame_->CallRuntime(Runtime::kNumberSub, 2); |
| 5860 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5863 | __ Move(value, r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5864 | // Store the new value in the target if not const. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5865 | // At this point the answer is in the value register. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5866 | exit.Bind(); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5867 | frame_->EmitPush(value); |
| 5868 | // Set the target with the result, leaving the result on |
| 5869 | // top of the stack. Removes the target from the stack if |
| 5870 | // it has a non-zero size. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5871 | if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
| 5874 | // Postfix: Discard the new value and use the old. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 5875 | if (is_postfix) frame_->Pop(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 5876 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
| 5879 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5880 | void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5881 | // According to ECMA-262 section 11.11, page 58, the binary logical |
| 5882 | // operators must yield the result of one of the two expressions |
| 5883 | // before any ToBoolean() conversions. This means that the value |
| 5884 | // produced by a && or || operator is not necessarily a boolean. |
| 5885 | |
| 5886 | // NOTE: If the left hand side produces a materialized value (not in |
| 5887 | // the CC register), we force the right hand side to do the |
| 5888 | // same. This is necessary because we may have to branch to the exit |
| 5889 | // after evaluating the left hand side (due to the shortcut |
| 5890 | // semantics), but the compiler must (statically) know if the result |
| 5891 | // of compiling the binary operation is materialized or not. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5892 | if (node->op() == Token::AND) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5893 | JumpTarget is_true; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5894 | LoadCondition(node->left(), &is_true, false_target(), false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5895 | if (has_valid_frame() && !has_cc()) { |
| 5896 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5897 | JumpTarget pop_and_continue; |
| 5898 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5899 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5900 | frame_->Dup(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5901 | // Avoid popping the result if it converts to 'false' using the |
| 5902 | // standard ToBoolean() conversion as described in ECMA-262, |
| 5903 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5904 | ToBoolean(&pop_and_continue, &exit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5905 | Branch(false, &exit); |
| 5906 | |
| 5907 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5908 | pop_and_continue.Bind(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5909 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5910 | |
| 5911 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5912 | is_true.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5913 | Load(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5914 | |
| 5915 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5916 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5917 | } else if (has_cc() || is_true.is_linked()) { |
| 5918 | // The left-hand side is either (a) partially compiled to |
| 5919 | // control flow with a final branch left to emit or (b) fully |
| 5920 | // compiled to control flow and possibly true. |
| 5921 | if (has_cc()) { |
| 5922 | Branch(false, false_target()); |
| 5923 | } |
| 5924 | is_true.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5925 | LoadCondition(node->right(), true_target(), false_target(), false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5926 | } else { |
| 5927 | // Nothing to do. |
| 5928 | ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5931 | } else { |
| 5932 | ASSERT(node->op() == Token::OR); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5933 | JumpTarget is_false; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5934 | LoadCondition(node->left(), true_target(), &is_false, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5935 | if (has_valid_frame() && !has_cc()) { |
| 5936 | // The left-hand side result is on top of the virtual frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 5937 | JumpTarget pop_and_continue; |
| 5938 | JumpTarget exit; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5939 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5940 | frame_->Dup(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5941 | // Avoid popping the result if it converts to 'true' using the |
| 5942 | // standard ToBoolean() conversion as described in ECMA-262, |
| 5943 | // section 9.2, page 30. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 5944 | ToBoolean(&exit, &pop_and_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5945 | Branch(true, &exit); |
| 5946 | |
| 5947 | // Pop the result of evaluating the first part. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5948 | pop_and_continue.Bind(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5949 | frame_->Pop(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5950 | |
| 5951 | // Evaluate right side expression. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5952 | is_false.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5953 | Load(node->right()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5954 | |
| 5955 | // Exit (always with a materialized value). |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 5956 | exit.Bind(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5957 | } else if (has_cc() || is_false.is_linked()) { |
| 5958 | // The left-hand side is either (a) partially compiled to |
| 5959 | // control flow with a final branch left to emit or (b) fully |
| 5960 | // compiled to control flow and possibly false. |
| 5961 | if (has_cc()) { |
| 5962 | Branch(true, true_target()); |
| 5963 | } |
| 5964 | is_false.Bind(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5965 | LoadCondition(node->right(), true_target(), false_target(), false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5966 | } else { |
| 5967 | // Nothing to do. |
| 5968 | ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5969 | } |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5970 | } |
| 5971 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5972 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5973 | |
| 5974 | void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) { |
| 5975 | #ifdef DEBUG |
| 5976 | int original_height = frame_->height(); |
| 5977 | #endif |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5978 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 5979 | |
| 5980 | if (node->op() == Token::AND || node->op() == Token::OR) { |
| 5981 | GenerateLogicalBooleanOperation(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5982 | } else { |
| 5983 | // Optimize for the case where (at least) one of the expressions |
| 5984 | // is a literal small integer. |
| 5985 | Literal* lliteral = node->left()->AsLiteral(); |
| 5986 | Literal* rliteral = node->right()->AsLiteral(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5987 | // NOTE: The code below assumes that the slow cases (calls to runtime) |
| 5988 | // never return a constant/immutable object. |
| 5989 | bool overwrite_left = |
| 5990 | (node->left()->AsBinaryOperation() != NULL && |
| 5991 | node->left()->AsBinaryOperation()->ResultOverwriteAllowed()); |
| 5992 | bool overwrite_right = |
| 5993 | (node->right()->AsBinaryOperation() != NULL && |
| 5994 | node->right()->AsBinaryOperation()->ResultOverwriteAllowed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5995 | |
| 5996 | if (rliteral != NULL && rliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5997 | VirtualFrame::RegisterAllocationScope scope(this); |
| 5998 | Load(node->left()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 5999 | if (frame_->KnownSmiAt(0)) overwrite_left = false; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6000 | SmiOperation(node->op(), |
| 6001 | rliteral->handle(), |
| 6002 | false, |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6003 | overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6004 | } else if (lliteral != NULL && lliteral->handle()->IsSmi()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6005 | VirtualFrame::RegisterAllocationScope scope(this); |
| 6006 | Load(node->right()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6007 | if (frame_->KnownSmiAt(0)) overwrite_right = false; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6008 | SmiOperation(node->op(), |
| 6009 | lliteral->handle(), |
| 6010 | true, |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6011 | overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6012 | } else { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6013 | GenerateInlineSmi inline_smi = |
| 6014 | loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI; |
| 6015 | if (lliteral != NULL) { |
| 6016 | ASSERT(!lliteral->handle()->IsSmi()); |
| 6017 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 6018 | } |
| 6019 | if (rliteral != NULL) { |
| 6020 | ASSERT(!rliteral->handle()->IsSmi()); |
| 6021 | inline_smi = DONT_GENERATE_INLINE_SMI; |
| 6022 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6023 | VirtualFrame::RegisterAllocationScope scope(this); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6024 | OverwriteMode overwrite_mode = NO_OVERWRITE; |
| 6025 | if (overwrite_left) { |
| 6026 | overwrite_mode = OVERWRITE_LEFT; |
| 6027 | } else if (overwrite_right) { |
| 6028 | overwrite_mode = OVERWRITE_RIGHT; |
| 6029 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6030 | Load(node->left()); |
| 6031 | Load(node->right()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6032 | GenericBinaryOperation(node->op(), overwrite_mode, inline_smi); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6033 | } |
| 6034 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 6035 | ASSERT(!has_valid_frame() || |
| 6036 | (has_cc() && frame_->height() == original_height) || |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6037 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6041 | void CodeGenerator::VisitThisFunction(ThisFunction* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6042 | #ifdef DEBUG |
| 6043 | int original_height = frame_->height(); |
| 6044 | #endif |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6045 | frame_->EmitPush(MemOperand(frame_->Function())); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6046 | ASSERT_EQ(original_height + 1, frame_->height()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6050 | void CodeGenerator::VisitCompareOperation(CompareOperation* node) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6051 | #ifdef DEBUG |
| 6052 | int original_height = frame_->height(); |
| 6053 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6054 | Comment cmnt(masm_, "[ CompareOperation"); |
| 6055 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6056 | VirtualFrame::RegisterAllocationScope nonspilled_scope(this); |
| 6057 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6058 | // Get the expressions from the node. |
| 6059 | Expression* left = node->left(); |
| 6060 | Expression* right = node->right(); |
| 6061 | Token::Value op = node->op(); |
| 6062 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6063 | // To make null checks efficient, we check if either left or right is the |
| 6064 | // literal 'null'. If so, we optimize the code by inlining a null check |
| 6065 | // instead of calling the (very) general runtime routine for checking |
| 6066 | // equality. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6067 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6068 | bool left_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6069 | left->AsLiteral() != NULL && left->AsLiteral()->IsNull(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6070 | bool right_is_null = |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6071 | right->AsLiteral() != NULL && right->AsLiteral()->IsNull(); |
| 6072 | // The 'null' value can only be equal to 'null' or 'undefined'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6073 | if (left_is_null || right_is_null) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6074 | Load(left_is_null ? right : left); |
| 6075 | Register tos = frame_->PopToRegister(); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6076 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6077 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6078 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6079 | // The 'null' value is only equal to 'undefined' if using non-strict |
| 6080 | // comparisons. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6081 | if (op != Token::EQ_STRICT) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6082 | true_target()->Branch(eq); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6083 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6084 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6085 | __ cmp(tos, Operand(ip)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6086 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6087 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6088 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6089 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6090 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6091 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6092 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 6093 | __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6094 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 6095 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6098 | cc_reg_ = eq; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6099 | ASSERT(has_cc() && frame_->height() == original_height); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6100 | return; |
| 6101 | } |
| 6102 | } |
| 6103 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6104 | // To make typeof testing for natives implemented in JavaScript really |
| 6105 | // efficient, we generate special code for expressions of the form: |
| 6106 | // 'typeof <expression> == <string>'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6107 | UnaryOperation* operation = left->AsUnaryOperation(); |
| 6108 | if ((op == Token::EQ || op == Token::EQ_STRICT) && |
| 6109 | (operation != NULL && operation->op() == Token::TYPEOF) && |
| 6110 | (right->AsLiteral() != NULL && |
| 6111 | right->AsLiteral()->handle()->IsString())) { |
| 6112 | Handle<String> check(String::cast(*right->AsLiteral()->handle())); |
| 6113 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6114 | // Load the operand, move it to a register. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6115 | LoadTypeofExpression(operation->expression()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6116 | Register tos = frame_->PopToRegister(); |
| 6117 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6118 | Register scratch = VirtualFrame::scratch0(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6119 | |
| 6120 | if (check->Equals(Heap::number_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6121 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6122 | true_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6123 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6124 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6125 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6126 | cc_reg_ = eq; |
| 6127 | |
| 6128 | } else if (check->Equals(Heap::string_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6129 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6130 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6131 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6132 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6133 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6134 | // It can be an undetectable string object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6135 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6136 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 6137 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6138 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6139 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6140 | __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset)); |
| 6141 | __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6142 | cc_reg_ = lt; |
| 6143 | |
| 6144 | } else if (check->Equals(Heap::boolean_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6145 | __ LoadRoot(ip, Heap::kTrueValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6146 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6147 | true_target()->Branch(eq); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6148 | __ LoadRoot(ip, Heap::kFalseValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6149 | __ cmp(tos, ip); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6150 | cc_reg_ = eq; |
| 6151 | |
| 6152 | } else if (check->Equals(Heap::undefined_symbol())) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6153 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6154 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6155 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6156 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6157 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6158 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6159 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6160 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6161 | __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset)); |
| 6162 | __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset)); |
| 6163 | __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable)); |
| 6164 | __ cmp(scratch, Operand(1 << Map::kIsUndetectable)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6165 | |
| 6166 | cc_reg_ = eq; |
| 6167 | |
| 6168 | } else if (check->Equals(Heap::function_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6169 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6170 | false_target()->Branch(eq); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6171 | Register map_reg = scratch; |
| 6172 | __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6173 | true_target()->Branch(eq); |
| 6174 | // Regular expressions are callable so typeof == 'function'. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6175 | __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6176 | cc_reg_ = eq; |
| 6177 | |
| 6178 | } else if (check->Equals(Heap::object_symbol())) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6179 | __ tst(tos, Operand(kSmiTagMask)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6180 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6181 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 6182 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6183 | __ cmp(tos, ip); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6184 | true_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6185 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6186 | Register map_reg = scratch; |
| 6187 | __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6188 | false_target()->Branch(eq); |
| 6189 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6190 | // It can be an undetectable object. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6191 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset)); |
| 6192 | __ and_(tos, tos, Operand(1 << Map::kIsUndetectable)); |
| 6193 | __ cmp(tos, Operand(1 << Map::kIsUndetectable)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6194 | false_target()->Branch(eq); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6195 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6196 | __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset)); |
| 6197 | __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6198 | false_target()->Branch(lt); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6199 | __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6200 | cc_reg_ = le; |
| 6201 | |
| 6202 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6203 | // Uncommon case: typeof testing against a string literal that is |
| 6204 | // never returned from the typeof operator. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6205 | false_target()->Jump(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6206 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6207 | ASSERT(!has_valid_frame() || |
| 6208 | (has_cc() && frame_->height() == original_height)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6209 | return; |
| 6210 | } |
| 6211 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6212 | switch (op) { |
| 6213 | case Token::EQ: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6214 | Comparison(eq, left, right, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6215 | break; |
| 6216 | |
| 6217 | case Token::LT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6218 | Comparison(lt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6219 | break; |
| 6220 | |
| 6221 | case Token::GT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6222 | Comparison(gt, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6223 | break; |
| 6224 | |
| 6225 | case Token::LTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6226 | Comparison(le, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6227 | break; |
| 6228 | |
| 6229 | case Token::GTE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6230 | Comparison(ge, left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6231 | break; |
| 6232 | |
| 6233 | case Token::EQ_STRICT: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 6234 | Comparison(eq, left, right, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6235 | break; |
| 6236 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6237 | case Token::IN: { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6238 | Load(left); |
| 6239 | Load(right); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 6240 | frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 6241 | frame_->EmitPush(r0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6242 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6243 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6244 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6245 | case Token::INSTANCEOF: { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6246 | Load(left); |
| 6247 | Load(right); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6248 | InstanceofStub stub; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 6249 | frame_->CallStub(&stub, 2); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 6250 | // At this point if instanceof succeeded then r0 == 0. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 6251 | __ tst(r0, Operand(r0)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6252 | cc_reg_ = eq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6253 | break; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6254 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6255 | |
| 6256 | default: |
| 6257 | UNREACHABLE(); |
| 6258 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6259 | ASSERT((has_cc() && frame_->height() == original_height) || |
| 6260 | (!has_cc() && frame_->height() == original_height + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6264 | class DeferredReferenceGetNamedValue: public DeferredCode { |
| 6265 | public: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6266 | explicit DeferredReferenceGetNamedValue(Register receiver, |
| 6267 | Handle<String> name) |
| 6268 | : receiver_(receiver), name_(name) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6269 | set_comment("[ DeferredReferenceGetNamedValue"); |
| 6270 | } |
| 6271 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6272 | virtual void Generate(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6273 | |
| 6274 | private: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6275 | Register receiver_; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6276 | Handle<String> name_; |
| 6277 | }; |
| 6278 | |
| 6279 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6280 | // Convention for this is that on entry the receiver is in a register that |
| 6281 | // is not used by the stack. On exit the answer is found in that same |
| 6282 | // register and the stack has the same height. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6283 | void DeferredReferenceGetNamedValue::Generate() { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6284 | #ifdef DEBUG |
| 6285 | int expected_height = frame_state()->frame()->height(); |
| 6286 | #endif |
| 6287 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6288 | copied_frame.SpillAll(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6289 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6290 | Register scratch1 = VirtualFrame::scratch0(); |
| 6291 | Register scratch2 = VirtualFrame::scratch1(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6292 | ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6293 | __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2); |
| 6294 | __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2); |
| 6295 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6296 | // Ensure receiver in r0 and name in r2 to match load ic calling convention. |
| 6297 | __ Move(r0, receiver_); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6298 | __ mov(r2, Operand(name_)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6299 | |
| 6300 | // The rest of the instructions in the deferred code must be together. |
| 6301 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6302 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 6303 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6304 | // The call must be followed by a nop(1) instruction to indicate that the |
| 6305 | // in-object has been inlined. |
| 6306 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6307 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6308 | // At this point the answer is in r0. We move it to the expected register |
| 6309 | // if necessary. |
| 6310 | __ Move(receiver_, r0); |
| 6311 | |
| 6312 | // Now go back to the frame that we entered with. This will not overwrite |
| 6313 | // the receiver register since that register was not in use when we came |
| 6314 | // in. The instructions emitted by this merge are skipped over by the |
| 6315 | // inline load patching mechanism when looking for the branch instruction |
| 6316 | // that tells it where the code to patch is. |
| 6317 | copied_frame.MergeTo(frame_state()->frame()); |
| 6318 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6319 | // Block the constant pool for one more instruction after leaving this |
| 6320 | // constant pool block scope to include the branch instruction ending the |
| 6321 | // deferred code. |
| 6322 | __ BlockConstPoolFor(1); |
| 6323 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6324 | ASSERT_EQ(expected_height, frame_state()->frame()->height()); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6328 | class DeferredReferenceGetKeyedValue: public DeferredCode { |
| 6329 | public: |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6330 | DeferredReferenceGetKeyedValue(Register key, Register receiver) |
| 6331 | : key_(key), receiver_(receiver) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6332 | set_comment("[ DeferredReferenceGetKeyedValue"); |
| 6333 | } |
| 6334 | |
| 6335 | virtual void Generate(); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6336 | |
| 6337 | private: |
| 6338 | Register key_; |
| 6339 | Register receiver_; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6340 | }; |
| 6341 | |
| 6342 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6343 | // Takes key and register in r0 and r1 or vice versa. Returns result |
| 6344 | // in r0. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6345 | void DeferredReferenceGetKeyedValue::Generate() { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6346 | ASSERT((key_.is(r0) && receiver_.is(r1)) || |
| 6347 | (key_.is(r1) && receiver_.is(r0))); |
| 6348 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6349 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6350 | copied_frame.SpillAll(); |
| 6351 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6352 | Register scratch1 = VirtualFrame::scratch0(); |
| 6353 | Register scratch2 = VirtualFrame::scratch1(); |
| 6354 | __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2); |
| 6355 | __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2); |
| 6356 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6357 | // Ensure key in r0 and receiver in r1 to match keyed load ic calling |
| 6358 | // convention. |
| 6359 | if (key_.is(r1)) { |
| 6360 | __ Swap(r0, r1, ip); |
| 6361 | } |
| 6362 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6363 | // The rest of the instructions in the deferred code must be together. |
| 6364 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 6365 | // Call keyed load IC. It has the arguments key and receiver in r0 and r1. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6366 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 6367 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6368 | // The call must be followed by a nop instruction to indicate that the |
| 6369 | // keyed load has been inlined. |
| 6370 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6371 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6372 | // Now go back to the frame that we entered with. This will not overwrite |
| 6373 | // the receiver or key registers since they were not in use when we came |
| 6374 | // in. The instructions emitted by this merge are skipped over by the |
| 6375 | // inline load patching mechanism when looking for the branch instruction |
| 6376 | // that tells it where the code to patch is. |
| 6377 | copied_frame.MergeTo(frame_state()->frame()); |
| 6378 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6379 | // Block the constant pool for one more instruction after leaving this |
| 6380 | // constant pool block scope to include the branch instruction ending the |
| 6381 | // deferred code. |
| 6382 | __ BlockConstPoolFor(1); |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | |
| 6387 | class DeferredReferenceSetKeyedValue: public DeferredCode { |
| 6388 | public: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6389 | DeferredReferenceSetKeyedValue(Register value, |
| 6390 | Register key, |
| 6391 | Register receiver) |
| 6392 | : value_(value), key_(key), receiver_(receiver) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6393 | set_comment("[ DeferredReferenceSetKeyedValue"); |
| 6394 | } |
| 6395 | |
| 6396 | virtual void Generate(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6397 | |
| 6398 | private: |
| 6399 | Register value_; |
| 6400 | Register key_; |
| 6401 | Register receiver_; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6402 | }; |
| 6403 | |
| 6404 | |
| 6405 | void DeferredReferenceSetKeyedValue::Generate() { |
| 6406 | Register scratch1 = VirtualFrame::scratch0(); |
| 6407 | Register scratch2 = VirtualFrame::scratch1(); |
| 6408 | __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2); |
| 6409 | __ IncrementCounter( |
| 6410 | &Counters::keyed_store_inline_miss, 1, scratch1, scratch2); |
| 6411 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6412 | // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic |
| 6413 | // calling convention. |
| 6414 | if (value_.is(r1)) { |
| 6415 | __ Swap(r0, r1, ip); |
| 6416 | } |
| 6417 | ASSERT(receiver_.is(r2)); |
| 6418 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6419 | // The rest of the instructions in the deferred code must be together. |
| 6420 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6421 | // Call keyed store IC. It has the arguments value, key and receiver in r0, |
| 6422 | // r1 and r2. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6423 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); |
| 6424 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6425 | // The call must be followed by a nop instruction to indicate that the |
| 6426 | // keyed store has been inlined. |
| 6427 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6428 | |
| 6429 | // Block the constant pool for one more instruction after leaving this |
| 6430 | // constant pool block scope to include the branch instruction ending the |
| 6431 | // deferred code. |
| 6432 | __ BlockConstPoolFor(1); |
| 6433 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 6437 | class DeferredReferenceSetNamedValue: public DeferredCode { |
| 6438 | public: |
| 6439 | DeferredReferenceSetNamedValue(Register value, |
| 6440 | Register receiver, |
| 6441 | Handle<String> name) |
| 6442 | : value_(value), receiver_(receiver), name_(name) { |
| 6443 | set_comment("[ DeferredReferenceSetNamedValue"); |
| 6444 | } |
| 6445 | |
| 6446 | virtual void Generate(); |
| 6447 | |
| 6448 | private: |
| 6449 | Register value_; |
| 6450 | Register receiver_; |
| 6451 | Handle<String> name_; |
| 6452 | }; |
| 6453 | |
| 6454 | |
| 6455 | // Takes value in r0, receiver in r1 and returns the result (the |
| 6456 | // value) in r0. |
| 6457 | void DeferredReferenceSetNamedValue::Generate() { |
| 6458 | // Record the entry frame and spill. |
| 6459 | VirtualFrame copied_frame(*frame_state()->frame()); |
| 6460 | copied_frame.SpillAll(); |
| 6461 | |
| 6462 | // Ensure value in r0, receiver in r1 to match store ic calling |
| 6463 | // convention. |
| 6464 | ASSERT(value_.is(r0) && receiver_.is(r1)); |
| 6465 | __ mov(r2, Operand(name_)); |
| 6466 | |
| 6467 | // The rest of the instructions in the deferred code must be together. |
| 6468 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6469 | // Call keyed store IC. It has the arguments value, key and receiver in r0, |
| 6470 | // r1 and r2. |
| 6471 | Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize)); |
| 6472 | __ Call(ic, RelocInfo::CODE_TARGET); |
| 6473 | // The call must be followed by a nop instruction to indicate that the |
| 6474 | // named store has been inlined. |
| 6475 | __ nop(PROPERTY_ACCESS_INLINED); |
| 6476 | |
| 6477 | // Go back to the frame we entered with. The instructions |
| 6478 | // generated by this merge are skipped over by the inline store |
| 6479 | // patching mechanism when looking for the branch instruction that |
| 6480 | // tells it where the code to patch is. |
| 6481 | copied_frame.MergeTo(frame_state()->frame()); |
| 6482 | |
| 6483 | // Block the constant pool for one more instruction after leaving this |
| 6484 | // constant pool block scope to include the branch instruction ending the |
| 6485 | // deferred code. |
| 6486 | __ BlockConstPoolFor(1); |
| 6487 | } |
| 6488 | } |
| 6489 | |
| 6490 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6491 | // Consumes the top of stack (the receiver) and pushes the result instead. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6492 | void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) { |
| 6493 | if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) { |
| 6494 | Comment cmnt(masm(), "[ Load from named Property"); |
| 6495 | // Setup the name register and call load IC. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6496 | frame_->CallLoadIC(name, |
| 6497 | is_contextual |
| 6498 | ? RelocInfo::CODE_TARGET_CONTEXT |
| 6499 | : RelocInfo::CODE_TARGET); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6500 | frame_->EmitPush(r0); // Push answer. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6501 | } else { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6502 | // Inline the in-object property case. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6503 | Comment cmnt(masm(), "[ Inlined named property load"); |
| 6504 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6505 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6506 | // having it in the instruction stream below where patching will occur. |
| 6507 | __ IncrementCounter(&Counters::named_load_inline, 1, |
| 6508 | frame_->scratch0(), frame_->scratch1()); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6509 | |
| 6510 | // The following instructions are the inlined load of an in-object property. |
| 6511 | // Parts of this code is patched, so the exact instructions generated needs |
| 6512 | // to be fixed. Therefore the instruction pool is blocked when generating |
| 6513 | // this code |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6514 | |
| 6515 | // Load the receiver from the stack. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6516 | Register receiver = frame_->PopToRegister(); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6517 | |
| 6518 | DeferredReferenceGetNamedValue* deferred = |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6519 | new DeferredReferenceGetNamedValue(receiver, name); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6520 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6521 | #ifdef DEBUG |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6522 | int kInlinedNamedLoadInstructions = 7; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6523 | Label check_inlined_codesize; |
| 6524 | masm_->bind(&check_inlined_codesize); |
| 6525 | #endif |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6526 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6527 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6528 | // Check that the receiver is a heap object. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6529 | __ tst(receiver, Operand(kSmiTagMask)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6530 | deferred->Branch(eq); |
| 6531 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6532 | Register scratch = VirtualFrame::scratch0(); |
| 6533 | Register scratch2 = VirtualFrame::scratch1(); |
| 6534 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6535 | // Check the map. The null map used below is patched by the inline cache |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6536 | // code. Therefore we can't use a LoadRoot call. |
| 6537 | __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 6538 | __ mov(scratch2, Operand(Factory::null_value())); |
| 6539 | __ cmp(scratch, scratch2); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6540 | deferred->Branch(ne); |
| 6541 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6542 | // Initially use an invalid index. The index will be patched by the |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6543 | // inline cache code. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6544 | __ ldr(receiver, MemOperand(receiver, 0)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6545 | |
| 6546 | // Make sure that the expected number of instructions are generated. |
| 6547 | ASSERT_EQ(kInlinedNamedLoadInstructions, |
| 6548 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6551 | deferred->BindExit(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6552 | // At this point the receiver register has the result, either from the |
| 6553 | // deferred code or from the inlined code. |
| 6554 | frame_->EmitPush(receiver); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6559 | void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) { |
| 6560 | #ifdef DEBUG |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 6561 | int expected_height = frame()->height() - (is_contextual ? 1 : 2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6562 | #endif |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6563 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 6564 | Result result; |
| 6565 | if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) { |
| 6566 | frame()->CallStoreIC(name, is_contextual); |
| 6567 | } else { |
| 6568 | // Inline the in-object property case. |
| 6569 | JumpTarget slow, done; |
| 6570 | |
| 6571 | // Get the value and receiver from the stack. |
| 6572 | frame()->PopToR0(); |
| 6573 | Register value = r0; |
| 6574 | frame()->PopToR1(); |
| 6575 | Register receiver = r1; |
| 6576 | |
| 6577 | DeferredReferenceSetNamedValue* deferred = |
| 6578 | new DeferredReferenceSetNamedValue(value, receiver, name); |
| 6579 | |
| 6580 | // Check that the receiver is a heap object. |
| 6581 | __ tst(receiver, Operand(kSmiTagMask)); |
| 6582 | deferred->Branch(eq); |
| 6583 | |
| 6584 | // The following instructions are the part of the inlined |
| 6585 | // in-object property store code which can be patched. Therefore |
| 6586 | // the exact number of instructions generated must be fixed, so |
| 6587 | // the constant pool is blocked while generating this code. |
| 6588 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6589 | Register scratch0 = VirtualFrame::scratch0(); |
| 6590 | Register scratch1 = VirtualFrame::scratch1(); |
| 6591 | |
| 6592 | // Check the map. Initially use an invalid map to force a |
| 6593 | // failure. The map check will be patched in the runtime system. |
| 6594 | __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
| 6595 | |
| 6596 | #ifdef DEBUG |
| 6597 | Label check_inlined_codesize; |
| 6598 | masm_->bind(&check_inlined_codesize); |
| 6599 | #endif |
| 6600 | __ mov(scratch0, Operand(Factory::null_value())); |
| 6601 | __ cmp(scratch0, scratch1); |
| 6602 | deferred->Branch(ne); |
| 6603 | |
| 6604 | int offset = 0; |
| 6605 | __ str(value, MemOperand(receiver, offset)); |
| 6606 | |
| 6607 | // Update the write barrier and record its size. We do not use |
| 6608 | // the RecordWrite macro here because we want the offset |
| 6609 | // addition instruction first to make it easy to patch. |
| 6610 | Label record_write_start, record_write_done; |
| 6611 | __ bind(&record_write_start); |
| 6612 | // Add offset into the object. |
| 6613 | __ add(scratch0, receiver, Operand(offset)); |
| 6614 | // Test that the object is not in the new space. We cannot set |
| 6615 | // region marks for new space pages. |
| 6616 | __ InNewSpace(receiver, scratch1, eq, &record_write_done); |
| 6617 | // Record the actual write. |
| 6618 | __ RecordWriteHelper(receiver, scratch0, scratch1); |
| 6619 | __ bind(&record_write_done); |
| 6620 | // Clobber all input registers when running with the debug-code flag |
| 6621 | // turned on to provoke errors. |
| 6622 | if (FLAG_debug_code) { |
| 6623 | __ mov(receiver, Operand(BitCast<int32_t>(kZapValue))); |
| 6624 | __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue))); |
| 6625 | __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue))); |
| 6626 | } |
| 6627 | // Check that this is the first inlined write barrier or that |
| 6628 | // this inlined write barrier has the same size as all the other |
| 6629 | // inlined write barriers. |
| 6630 | ASSERT((inlined_write_barrier_size_ == -1) || |
| 6631 | (inlined_write_barrier_size_ == |
| 6632 | masm()->InstructionsGeneratedSince(&record_write_start))); |
| 6633 | inlined_write_barrier_size_ = |
| 6634 | masm()->InstructionsGeneratedSince(&record_write_start); |
| 6635 | |
| 6636 | // Make sure that the expected number of instructions are generated. |
| 6637 | ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(), |
| 6638 | masm()->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6639 | } |
| 6640 | deferred->BindExit(); |
| 6641 | } |
| 6642 | ASSERT_EQ(expected_height, frame()->height()); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6646 | void CodeGenerator::EmitKeyedLoad() { |
| 6647 | if (loop_nesting() == 0) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6648 | Comment cmnt(masm_, "[ Load from keyed property"); |
| 6649 | frame_->CallKeyedLoadIC(); |
| 6650 | } else { |
| 6651 | // Inline the keyed load. |
| 6652 | Comment cmnt(masm_, "[ Inlined load from keyed property"); |
| 6653 | |
| 6654 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6655 | // having it in the instruction stream below where patching will occur. |
| 6656 | __ IncrementCounter(&Counters::keyed_load_inline, 1, |
| 6657 | frame_->scratch0(), frame_->scratch1()); |
| 6658 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6659 | // Load the key and receiver from the stack. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6660 | bool key_is_known_smi = frame_->KnownSmiAt(0); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6661 | Register key = frame_->PopToRegister(); |
| 6662 | Register receiver = frame_->PopToRegister(key); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6663 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6664 | // The deferred code expects key and receiver in registers. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6665 | DeferredReferenceGetKeyedValue* deferred = |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6666 | new DeferredReferenceGetKeyedValue(key, receiver); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6667 | |
| 6668 | // Check that the receiver is a heap object. |
| 6669 | __ tst(receiver, Operand(kSmiTagMask)); |
| 6670 | deferred->Branch(eq); |
| 6671 | |
| 6672 | // The following instructions are the part of the inlined load keyed |
| 6673 | // property code which can be patched. Therefore the exact number of |
| 6674 | // instructions generated need to be fixed, so the constant pool is blocked |
| 6675 | // while generating this code. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6676 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
| 6677 | Register scratch1 = VirtualFrame::scratch0(); |
| 6678 | Register scratch2 = VirtualFrame::scratch1(); |
| 6679 | // Check the map. The null map used below is patched by the inline cache |
| 6680 | // code. |
| 6681 | __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset)); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6682 | |
| 6683 | // Check that the key is a smi. |
| 6684 | if (!key_is_known_smi) { |
| 6685 | __ tst(key, Operand(kSmiTagMask)); |
| 6686 | deferred->Branch(ne); |
| 6687 | } |
| 6688 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6689 | #ifdef DEBUG |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6690 | Label check_inlined_codesize; |
| 6691 | masm_->bind(&check_inlined_codesize); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6692 | #endif |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6693 | __ mov(scratch2, Operand(Factory::null_value())); |
| 6694 | __ cmp(scratch1, scratch2); |
| 6695 | deferred->Branch(ne); |
| 6696 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6697 | // Get the elements array from the receiver and check that it |
| 6698 | // is not a dictionary. |
| 6699 | __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6700 | if (FLAG_debug_code) { |
| 6701 | __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset)); |
| 6702 | __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex); |
| 6703 | __ cmp(scratch2, ip); |
| 6704 | __ Assert(eq, "JSObject with fast elements map has slow elements"); |
| 6705 | } |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6706 | |
| 6707 | // Check that key is within bounds. Use unsigned comparison to handle |
| 6708 | // negative keys. |
| 6709 | __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6710 | __ cmp(scratch2, key); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6711 | deferred->Branch(ls); // Unsigned less equal. |
| 6712 | |
| 6713 | // Load and check that the result is not the hole (key is a smi). |
| 6714 | __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex); |
| 6715 | __ add(scratch1, |
| 6716 | scratch1, |
| 6717 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6718 | __ ldr(scratch1, |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6719 | MemOperand(scratch1, key, LSL, |
| 6720 | kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize))); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6721 | __ cmp(scratch1, scratch2); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6722 | deferred->Branch(eq); |
| 6723 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 6724 | __ mov(r0, scratch1); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6725 | // Make sure that the expected number of instructions are generated. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6726 | ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(), |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6727 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6728 | } |
| 6729 | |
| 6730 | deferred->BindExit(); |
| 6731 | } |
| 6732 | } |
| 6733 | |
| 6734 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6735 | void CodeGenerator::EmitKeyedStore(StaticType* key_type, |
| 6736 | WriteBarrierCharacter wb_info) { |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6737 | // Generate inlined version of the keyed store if the code is in a loop |
| 6738 | // and the key is likely to be a smi. |
| 6739 | if (loop_nesting() > 0 && key_type->IsLikelySmi()) { |
| 6740 | // Inline the keyed store. |
| 6741 | Comment cmnt(masm_, "[ Inlined store to keyed property"); |
| 6742 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6743 | Register scratch1 = VirtualFrame::scratch0(); |
| 6744 | Register scratch2 = VirtualFrame::scratch1(); |
| 6745 | Register scratch3 = r3; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6746 | |
| 6747 | // Counter will be decremented in the deferred code. Placed here to avoid |
| 6748 | // having it in the instruction stream below where patching will occur. |
| 6749 | __ IncrementCounter(&Counters::keyed_store_inline, 1, |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6750 | scratch1, scratch2); |
| 6751 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6752 | |
| 6753 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6754 | // Load the value, key and receiver from the stack. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6755 | bool value_is_harmless = frame_->KnownSmiAt(0); |
| 6756 | if (wb_info == NEVER_NEWSPACE) value_is_harmless = true; |
| 6757 | bool key_is_smi = frame_->KnownSmiAt(1); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6758 | Register value = frame_->PopToRegister(); |
| 6759 | Register key = frame_->PopToRegister(value); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 6760 | VirtualFrame::SpilledScope spilled(frame_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6761 | Register receiver = r2; |
| 6762 | frame_->EmitPop(receiver); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6763 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6764 | #ifdef DEBUG |
| 6765 | bool we_remembered_the_write_barrier = value_is_harmless; |
| 6766 | #endif |
| 6767 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6768 | // The deferred code expects value, key and receiver in registers. |
| 6769 | DeferredReferenceSetKeyedValue* deferred = |
| 6770 | new DeferredReferenceSetKeyedValue(value, key, receiver); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6771 | |
| 6772 | // Check that the value is a smi. As this inlined code does not set the |
| 6773 | // write barrier it is only possible to store smi values. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6774 | if (!value_is_harmless) { |
| 6775 | // If the value is not likely to be a Smi then let's test the fixed array |
| 6776 | // for new space instead. See below. |
| 6777 | if (wb_info == LIKELY_SMI) { |
| 6778 | __ tst(value, Operand(kSmiTagMask)); |
| 6779 | deferred->Branch(ne); |
| 6780 | #ifdef DEBUG |
| 6781 | we_remembered_the_write_barrier = true; |
| 6782 | #endif |
| 6783 | } |
| 6784 | } |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6785 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6786 | if (!key_is_smi) { |
| 6787 | // Check that the key is a smi. |
| 6788 | __ tst(key, Operand(kSmiTagMask)); |
| 6789 | deferred->Branch(ne); |
| 6790 | } |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6791 | |
| 6792 | // Check that the receiver is a heap object. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6793 | __ tst(receiver, Operand(kSmiTagMask)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6794 | deferred->Branch(eq); |
| 6795 | |
| 6796 | // Check that the receiver is a JSArray. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6797 | __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6798 | deferred->Branch(ne); |
| 6799 | |
| 6800 | // Check that the key is within bounds. Both the key and the length of |
| 6801 | // the JSArray are smis. Use unsigned comparison to handle negative keys. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6802 | __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset)); |
| 6803 | __ cmp(scratch1, key); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6804 | deferred->Branch(ls); // Unsigned less equal. |
| 6805 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6806 | // Get the elements array from the receiver. |
| 6807 | __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset)); |
| 6808 | if (!value_is_harmless && wb_info != LIKELY_SMI) { |
| 6809 | Label ok; |
| 6810 | __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask())); |
| 6811 | __ cmp(scratch2, Operand(ExternalReference::new_space_start())); |
| 6812 | __ tst(value, Operand(kSmiTagMask), ne); |
| 6813 | deferred->Branch(ne); |
| 6814 | #ifdef DEBUG |
| 6815 | we_remembered_the_write_barrier = true; |
| 6816 | #endif |
| 6817 | } |
| 6818 | // Check that the elements array is not a dictionary. |
| 6819 | __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6820 | // The following instructions are the part of the inlined store keyed |
| 6821 | // property code which can be patched. Therefore the exact number of |
| 6822 | // instructions generated need to be fixed, so the constant pool is blocked |
| 6823 | // while generating this code. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6824 | { Assembler::BlockConstPoolScope block_const_pool(masm_); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6825 | #ifdef DEBUG |
| 6826 | Label check_inlined_codesize; |
| 6827 | masm_->bind(&check_inlined_codesize); |
| 6828 | #endif |
| 6829 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6830 | // Read the fixed array map from the constant pool (not from the root |
| 6831 | // array) so that the value can be patched. When debugging, we patch this |
| 6832 | // comparison to always fail so that we will hit the IC call in the |
| 6833 | // deferred code which will allow the debugger to break for fast case |
| 6834 | // stores. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6835 | __ mov(scratch3, Operand(Factory::fixed_array_map())); |
| 6836 | __ cmp(scratch2, scratch3); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6837 | deferred->Branch(ne); |
| 6838 | |
| 6839 | // Store the value. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6840 | __ add(scratch1, scratch1, |
| 6841 | Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 6842 | __ str(value, |
| 6843 | MemOperand(scratch1, key, LSL, |
| 6844 | kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize))); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6845 | |
| 6846 | // Make sure that the expected number of instructions are generated. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6847 | ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch, |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6848 | masm_->InstructionsGeneratedSince(&check_inlined_codesize)); |
| 6849 | } |
| 6850 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6851 | ASSERT(we_remembered_the_write_barrier); |
| 6852 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6853 | deferred->BindExit(); |
| 6854 | } else { |
| 6855 | frame()->CallKeyedStoreIC(); |
| 6856 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6860 | #ifdef DEBUG |
| 6861 | bool CodeGenerator::HasValidEntryRegisters() { return true; } |
| 6862 | #endif |
| 6863 | |
| 6864 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6865 | #undef __ |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6866 | #define __ ACCESS_MASM(masm) |
| 6867 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6868 | Handle<String> Reference::GetName() { |
| 6869 | ASSERT(type_ == NAMED); |
| 6870 | Property* property = expression_->AsProperty(); |
| 6871 | if (property == NULL) { |
| 6872 | // Global variable reference treated as a named property reference. |
| 6873 | VariableProxy* proxy = expression_->AsVariableProxy(); |
| 6874 | ASSERT(proxy->AsVariable() != NULL); |
| 6875 | ASSERT(proxy->AsVariable()->is_global()); |
| 6876 | return proxy->name(); |
| 6877 | } else { |
| 6878 | Literal* raw_name = property->key()->AsLiteral(); |
| 6879 | ASSERT(raw_name != NULL); |
| 6880 | return Handle<String>(String::cast(*raw_name->handle())); |
| 6881 | } |
| 6882 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6883 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6884 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6885 | void Reference::DupIfPersist() { |
| 6886 | if (persist_after_get_) { |
| 6887 | switch (type_) { |
| 6888 | case KEYED: |
| 6889 | cgen_->frame()->Dup2(); |
| 6890 | break; |
| 6891 | case NAMED: |
| 6892 | cgen_->frame()->Dup(); |
| 6893 | // Fall through. |
| 6894 | case UNLOADED: |
| 6895 | case ILLEGAL: |
| 6896 | case SLOT: |
| 6897 | // Do nothing. |
| 6898 | ; |
| 6899 | } |
| 6900 | } else { |
| 6901 | set_unloaded(); |
| 6902 | } |
| 6903 | } |
| 6904 | |
| 6905 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6906 | void Reference::GetValue() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6907 | ASSERT(cgen_->HasValidEntryRegisters()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6908 | ASSERT(!is_illegal()); |
| 6909 | ASSERT(!cgen_->has_cc()); |
| 6910 | MacroAssembler* masm = cgen_->masm(); |
| 6911 | Property* property = expression_->AsProperty(); |
| 6912 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 6913 | cgen_->CodeForSourcePosition(property->position()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
| 6916 | switch (type_) { |
| 6917 | case SLOT: { |
| 6918 | Comment cmnt(masm, "[ Load from Slot"); |
| 6919 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
| 6920 | ASSERT(slot != NULL); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6921 | DupIfPersist(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6922 | cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6923 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6926 | case NAMED: { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6927 | Variable* var = expression_->AsVariableProxy()->AsVariable(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 6928 | bool is_global = var != NULL; |
| 6929 | ASSERT(!is_global || var->is_global()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6930 | Handle<String> name = GetName(); |
| 6931 | DupIfPersist(); |
| 6932 | cgen_->EmitNamedLoad(name, is_global); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6933 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6936 | case KEYED: { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6937 | ASSERT(property != NULL); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6938 | DupIfPersist(); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 6939 | cgen_->EmitKeyedLoad(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6940 | cgen_->frame()->EmitPush(r0); |
| 6941 | break; |
| 6942 | } |
| 6943 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6944 | default: |
| 6945 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6946 | } |
| 6947 | } |
| 6948 | |
| 6949 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6950 | void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6951 | ASSERT(!is_illegal()); |
| 6952 | ASSERT(!cgen_->has_cc()); |
| 6953 | MacroAssembler* masm = cgen_->masm(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 6954 | VirtualFrame* frame = cgen_->frame(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6955 | Property* property = expression_->AsProperty(); |
| 6956 | if (property != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 6957 | cgen_->CodeForSourcePosition(property->position()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6958 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6959 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6960 | switch (type_) { |
| 6961 | case SLOT: { |
| 6962 | Comment cmnt(masm, "[ Store to Slot"); |
| 6963 | Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6964 | cgen_->StoreToSlot(slot, init_state); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6965 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6966 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6967 | } |
| 6968 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6969 | case NAMED: { |
| 6970 | Comment cmnt(masm, "[ Store to named Property"); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 6971 | cgen_->EmitNamedStore(GetName(), false); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 6972 | frame->EmitPush(r0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 6973 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6974 | break; |
| 6975 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6976 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6977 | case KEYED: { |
| 6978 | Comment cmnt(masm, "[ Store to keyed Property"); |
| 6979 | Property* property = expression_->AsProperty(); |
| 6980 | ASSERT(property != NULL); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 6981 | cgen_->CodeForSourcePosition(property->position()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6982 | cgen_->EmitKeyedStore(property->key()->type(), wb_info); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 6983 | frame->EmitPush(r0); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6984 | set_unloaded(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6985 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6986 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6987 | |
| 6988 | default: |
| 6989 | UNREACHABLE(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 6990 | } |
| 6991 | } |
| 6992 | |
| 6993 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6994 | void FastNewClosureStub::Generate(MacroAssembler* masm) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6995 | // Create a new closure from the given function info in new |
| 6996 | // space. Set the context to the current context in cp. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6997 | Label gc; |
| 6998 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6999 | // Pop the function info from the stack. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7000 | __ pop(r3); |
| 7001 | |
| 7002 | // Attempt to allocate new JSFunction in new space. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 7003 | __ AllocateInNewSpace(JSFunction::kSize, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7004 | r0, |
| 7005 | r1, |
| 7006 | r2, |
| 7007 | &gc, |
| 7008 | TAG_OBJECT); |
| 7009 | |
| 7010 | // Compute the function map in the current global context and set that |
| 7011 | // as the map of the allocated object. |
| 7012 | __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7013 | __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset)); |
| 7014 | __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX))); |
| 7015 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 7016 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7017 | // Initialize the rest of the function. We don't have to update the |
| 7018 | // write barrier because the allocated object is in new space. |
| 7019 | __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex); |
| 7020 | __ LoadRoot(r2, Heap::kTheHoleValueRootIndex); |
| 7021 | __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset)); |
| 7022 | __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 7023 | __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset)); |
| 7024 | __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset)); |
| 7025 | __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset)); |
| 7026 | __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7027 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7028 | // Initialize the code pointer in the function to be the one |
| 7029 | // found in the shared function info object. |
| 7030 | __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset)); |
| 7031 | __ str(r3, FieldMemOperand(r0, JSFunction::kCodeOffset)); |
| 7032 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7033 | // Return result. The argument function info has been popped already. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7034 | __ Ret(); |
| 7035 | |
| 7036 | // Create a new closure through the slower runtime call. |
| 7037 | __ bind(&gc); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7038 | __ Push(cp, r3); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7039 | __ TailCallRuntime(Runtime::kNewClosure, 2, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | |
| 7043 | void FastNewContextStub::Generate(MacroAssembler* masm) { |
| 7044 | // Try to allocate the context in new space. |
| 7045 | Label gc; |
| 7046 | int length = slots_ + Context::MIN_CONTEXT_SLOTS; |
| 7047 | |
| 7048 | // Attempt to allocate the context in new space. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 7049 | __ AllocateInNewSpace(FixedArray::SizeFor(length), |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7050 | r0, |
| 7051 | r1, |
| 7052 | r2, |
| 7053 | &gc, |
| 7054 | TAG_OBJECT); |
| 7055 | |
| 7056 | // Load the function from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7057 | __ ldr(r3, MemOperand(sp, 0)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7058 | |
| 7059 | // Setup the object header. |
| 7060 | __ LoadRoot(r2, Heap::kContextMapRootIndex); |
| 7061 | __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7062 | __ mov(r2, Operand(Smi::FromInt(length))); |
| 7063 | __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7064 | |
| 7065 | // Setup the fixed slots. |
| 7066 | __ mov(r1, Operand(Smi::FromInt(0))); |
| 7067 | __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 7068 | __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 7069 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
| 7070 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX))); |
| 7071 | |
| 7072 | // Copy the global object from the surrounding context. |
| 7073 | __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7074 | __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 7075 | |
| 7076 | // Initialize the rest of the slots to undefined. |
| 7077 | __ LoadRoot(r1, Heap::kUndefinedValueRootIndex); |
| 7078 | for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) { |
| 7079 | __ str(r1, MemOperand(r0, Context::SlotOffset(i))); |
| 7080 | } |
| 7081 | |
| 7082 | // Remove the on-stack argument and return. |
| 7083 | __ mov(cp, r0); |
| 7084 | __ pop(); |
| 7085 | __ Ret(); |
| 7086 | |
| 7087 | // Need to collect. Call into runtime system. |
| 7088 | __ bind(&gc); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7089 | __ TailCallRuntime(Runtime::kNewContext, 1, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
| 7092 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7093 | void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) { |
| 7094 | // Stack layout on entry: |
| 7095 | // |
| 7096 | // [sp]: constant elements. |
| 7097 | // [sp + kPointerSize]: literal index. |
| 7098 | // [sp + (2 * kPointerSize)]: literals array. |
| 7099 | |
| 7100 | // All sizes here are multiples of kPointerSize. |
| 7101 | int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0; |
| 7102 | int size = JSArray::kSize + elements_size; |
| 7103 | |
| 7104 | // Load boilerplate object into r3 and check if we need to create a |
| 7105 | // boilerplate. |
| 7106 | Label slow_case; |
| 7107 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 7108 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 7109 | __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 7110 | __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 7111 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 7112 | __ cmp(r3, ip); |
| 7113 | __ b(eq, &slow_case); |
| 7114 | |
| 7115 | // Allocate both the JS array and the elements array in one big |
| 7116 | // allocation. This avoids multiple limit checks. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 7117 | __ AllocateInNewSpace(size, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7118 | r0, |
| 7119 | r1, |
| 7120 | r2, |
| 7121 | &slow_case, |
| 7122 | TAG_OBJECT); |
| 7123 | |
| 7124 | // Copy the JS array part. |
| 7125 | for (int i = 0; i < JSArray::kSize; i += kPointerSize) { |
| 7126 | if ((i != JSArray::kElementsOffset) || (length_ == 0)) { |
| 7127 | __ ldr(r1, FieldMemOperand(r3, i)); |
| 7128 | __ str(r1, FieldMemOperand(r0, i)); |
| 7129 | } |
| 7130 | } |
| 7131 | |
| 7132 | if (length_ > 0) { |
| 7133 | // Get hold of the elements array of the boilerplate and setup the |
| 7134 | // elements pointer in the resulting object. |
| 7135 | __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset)); |
| 7136 | __ add(r2, r0, Operand(JSArray::kSize)); |
| 7137 | __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 7138 | |
| 7139 | // Copy the elements array. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 7140 | __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | // Return and remove the on-stack parameters. |
| 7144 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 7145 | __ Ret(); |
| 7146 | |
| 7147 | __ bind(&slow_case); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7148 | __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
| 7151 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7152 | // Takes a Smi and converts to an IEEE 64 bit floating point value in two |
| 7153 | // registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and |
| 7154 | // 52 fraction bits (20 in the first word, 32 in the second). Zeros is a |
| 7155 | // scratch register. Destroys the source register. No GC occurs during this |
| 7156 | // stub so you don't have to set up the frame. |
| 7157 | class ConvertToDoubleStub : public CodeStub { |
| 7158 | public: |
| 7159 | ConvertToDoubleStub(Register result_reg_1, |
| 7160 | Register result_reg_2, |
| 7161 | Register source_reg, |
| 7162 | Register scratch_reg) |
| 7163 | : result1_(result_reg_1), |
| 7164 | result2_(result_reg_2), |
| 7165 | source_(source_reg), |
| 7166 | zeros_(scratch_reg) { } |
| 7167 | |
| 7168 | private: |
| 7169 | Register result1_; |
| 7170 | Register result2_; |
| 7171 | Register source_; |
| 7172 | Register zeros_; |
| 7173 | |
| 7174 | // Minor key encoding in 16 bits. |
| 7175 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 7176 | class OpBits: public BitField<Token::Value, 2, 14> {}; |
| 7177 | |
| 7178 | Major MajorKey() { return ConvertToDouble; } |
| 7179 | int MinorKey() { |
| 7180 | // Encode the parameters in a unique 16 bit value. |
| 7181 | return result1_.code() + |
| 7182 | (result2_.code() << 4) + |
| 7183 | (source_.code() << 8) + |
| 7184 | (zeros_.code() << 12); |
| 7185 | } |
| 7186 | |
| 7187 | void Generate(MacroAssembler* masm); |
| 7188 | |
| 7189 | const char* GetName() { return "ConvertToDoubleStub"; } |
| 7190 | |
| 7191 | #ifdef DEBUG |
| 7192 | void Print() { PrintF("ConvertToDoubleStub\n"); } |
| 7193 | #endif |
| 7194 | }; |
| 7195 | |
| 7196 | |
| 7197 | void ConvertToDoubleStub::Generate(MacroAssembler* masm) { |
| 7198 | #ifndef BIG_ENDIAN_FLOATING_POINT |
| 7199 | Register exponent = result1_; |
| 7200 | Register mantissa = result2_; |
| 7201 | #else |
| 7202 | Register exponent = result2_; |
| 7203 | Register mantissa = result1_; |
| 7204 | #endif |
| 7205 | Label not_special; |
| 7206 | // Convert from Smi to integer. |
| 7207 | __ mov(source_, Operand(source_, ASR, kSmiTagSize)); |
| 7208 | // Move sign bit from source to destination. This works because the sign bit |
| 7209 | // in the exponent word of the double has the same position and polarity as |
| 7210 | // the 2's complement sign bit in a Smi. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7211 | STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7212 | __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC); |
| 7213 | // Subtract from 0 if source was negative. |
| 7214 | __ rsb(source_, source_, Operand(0), LeaveCC, ne); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7215 | |
| 7216 | // We have -1, 0 or 1, which we treat specially. Register source_ contains |
| 7217 | // absolute value: it is either equal to 1 (special case of -1 and 1), |
| 7218 | // greater than 1 (not a special case) or less than 1 (special case of 0). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7219 | __ cmp(source_, Operand(1)); |
| 7220 | __ b(gt, ¬_special); |
| 7221 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7222 | // For 1 or -1 we need to or in the 0 exponent (biased to 1023). |
| 7223 | static const uint32_t exponent_word_for_1 = |
| 7224 | HeapNumber::kExponentBias << HeapNumber::kExponentShift; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7225 | __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7226 | // 1, 0 and -1 all have 0 for the second word. |
| 7227 | __ mov(mantissa, Operand(0)); |
| 7228 | __ Ret(); |
| 7229 | |
| 7230 | __ bind(¬_special); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7231 | // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7232 | // Gets the wrong answer for 0, but we already checked for that case above. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 7233 | __ CountLeadingZeros(zeros_, source_, mantissa); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7234 | // Compute exponent and or it into the exponent register. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7235 | // We use mantissa as a scratch register here. Use a fudge factor to |
| 7236 | // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts |
| 7237 | // that fit in the ARM's constant field. |
| 7238 | int fudge = 0x400; |
| 7239 | __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge)); |
| 7240 | __ add(mantissa, mantissa, Operand(fudge)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7241 | __ orr(exponent, |
| 7242 | exponent, |
| 7243 | Operand(mantissa, LSL, HeapNumber::kExponentShift)); |
| 7244 | // Shift up the source chopping the top bit off. |
| 7245 | __ add(zeros_, zeros_, Operand(1)); |
| 7246 | // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0. |
| 7247 | __ mov(source_, Operand(source_, LSL, zeros_)); |
| 7248 | // Compute lower part of fraction (last 12 bits). |
| 7249 | __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord)); |
| 7250 | // And the top (top 20 bits). |
| 7251 | __ orr(exponent, |
| 7252 | exponent, |
| 7253 | Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord)); |
| 7254 | __ Ret(); |
| 7255 | } |
| 7256 | |
| 7257 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7258 | // See comment for class. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7259 | void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7260 | Label max_negative_int; |
| 7261 | // the_int_ has the answer which is a signed int32 but not a Smi. |
| 7262 | // We test for the special value that has a different exponent. This test |
| 7263 | // has the neat side effect of setting the flags according to the sign. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7264 | STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 7265 | __ cmp(the_int_, Operand(0x80000000u)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 7266 | __ b(eq, &max_negative_int); |
| 7267 | // Set up the correct exponent in scratch_. All non-Smi int32s have the same. |
| 7268 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). |
| 7269 | uint32_t non_smi_exponent = |
| 7270 | (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift; |
| 7271 | __ mov(scratch_, Operand(non_smi_exponent)); |
| 7272 | // Set the sign bit in scratch_ if the value was negative. |
| 7273 | __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs); |
| 7274 | // Subtract from 0 if the value was negative. |
| 7275 | __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs); |
| 7276 | // We should be masking the implict first digit of the mantissa away here, |
| 7277 | // but it just ends up combining harmlessly with the last digit of the |
| 7278 | // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get |
| 7279 | // the most significant 1 to hit the last bit of the 12 bit sign and exponent. |
| 7280 | ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0); |
| 7281 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 7282 | __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance)); |
| 7283 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 7284 | HeapNumber::kExponentOffset)); |
| 7285 | __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance)); |
| 7286 | __ str(scratch_, FieldMemOperand(the_heap_number_, |
| 7287 | HeapNumber::kMantissaOffset)); |
| 7288 | __ Ret(); |
| 7289 | |
| 7290 | __ bind(&max_negative_int); |
| 7291 | // The max negative int32 is stored as a positive number in the mantissa of |
| 7292 | // a double because it uses a sign bit instead of using two's complement. |
| 7293 | // The actual mantissa bits stored are all 0 because the implicit most |
| 7294 | // significant 1 bit is not stored. |
| 7295 | non_smi_exponent += 1 << HeapNumber::kExponentShift; |
| 7296 | __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent)); |
| 7297 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset)); |
| 7298 | __ mov(ip, Operand(0)); |
| 7299 | __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset)); |
| 7300 | __ Ret(); |
| 7301 | } |
| 7302 | |
| 7303 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7304 | // Handle the case where the lhs and rhs are the same object. |
| 7305 | // Equality is almost reflexive (everything but NaN), so this is a test |
| 7306 | // for "identity and not NaN". |
| 7307 | static void EmitIdenticalObjectComparison(MacroAssembler* masm, |
| 7308 | Label* slow, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7309 | Condition cc, |
| 7310 | bool never_nan_nan) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7311 | Label not_identical; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7312 | Label heap_number, return_equal; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 7313 | __ cmp(r0, r1); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7314 | __ b(ne, ¬_identical); |
| 7315 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7316 | // The two objects are identical. If we know that one of them isn't NaN then |
| 7317 | // we now know they test equal. |
| 7318 | if (cc != eq || !never_nan_nan) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7319 | // Test for NaN. Sadly, we can't just compare to Factory::nan_value(), |
| 7320 | // so we do the second best thing - test it ourselves. |
| 7321 | // They are both equal and they are not both Smis so both of them are not |
| 7322 | // Smis. If it's not a heap number, then return equal. |
| 7323 | if (cc == lt || cc == gt) { |
| 7324 | __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7325 | __ b(ge, slow); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7326 | } else { |
| 7327 | __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE); |
| 7328 | __ b(eq, &heap_number); |
| 7329 | // Comparing JS objects with <=, >= is complicated. |
| 7330 | if (cc != eq) { |
| 7331 | __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE)); |
| 7332 | __ b(ge, slow); |
| 7333 | // Normally here we fall through to return_equal, but undefined is |
| 7334 | // special: (undefined == undefined) == true, but |
| 7335 | // (undefined <= undefined) == false! See ECMAScript 11.8.5. |
| 7336 | if (cc == le || cc == ge) { |
| 7337 | __ cmp(r4, Operand(ODDBALL_TYPE)); |
| 7338 | __ b(ne, &return_equal); |
| 7339 | __ LoadRoot(r2, Heap::kUndefinedValueRootIndex); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 7340 | __ cmp(r0, r2); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7341 | __ b(ne, &return_equal); |
| 7342 | if (cc == le) { |
| 7343 | // undefined <= undefined should fail. |
| 7344 | __ mov(r0, Operand(GREATER)); |
| 7345 | } else { |
| 7346 | // undefined >= undefined should fail. |
| 7347 | __ mov(r0, Operand(LESS)); |
| 7348 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7349 | __ Ret(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7350 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7351 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7352 | } |
| 7353 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7354 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7355 | __ bind(&return_equal); |
| 7356 | if (cc == lt) { |
| 7357 | __ mov(r0, Operand(GREATER)); // Things aren't less than themselves. |
| 7358 | } else if (cc == gt) { |
| 7359 | __ mov(r0, Operand(LESS)); // Things aren't greater than themselves. |
| 7360 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7361 | __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7362 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7363 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7364 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7365 | if (cc != eq || !never_nan_nan) { |
| 7366 | // For less and greater we don't have to check for NaN since the result of |
| 7367 | // x < x is false regardless. For the others here is some code to check |
| 7368 | // for NaN. |
| 7369 | if (cc != lt && cc != gt) { |
| 7370 | __ bind(&heap_number); |
| 7371 | // It is a heap number, so return non-equal if it's NaN and equal if it's |
| 7372 | // not NaN. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7373 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7374 | // The representation of NaN values has all exponent bits (52..62) set, |
| 7375 | // and not all mantissa bits (0..51) clear. |
| 7376 | // Read top bits of double representation (second word of value). |
| 7377 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 7378 | // Test that exponent bits are all set. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7379 | __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits); |
| 7380 | // NaNs have all-one exponents so they sign extend to -1. |
| 7381 | __ cmp(r3, Operand(-1)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7382 | __ b(ne, &return_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7383 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7384 | // Shift out flag and all exponent bits, retaining only mantissa. |
| 7385 | __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord)); |
| 7386 | // Or with all low-bits of mantissa. |
| 7387 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 7388 | __ orr(r0, r3, Operand(r2), SetCC); |
| 7389 | // For equal we already have the right value in r0: Return zero (equal) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7390 | // if all bits in mantissa are zero (it's an Infinity) and non-zero if |
| 7391 | // not (it's a NaN). For <= and >= we need to load r0 with the failing |
| 7392 | // value if it's a NaN. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7393 | if (cc != eq) { |
| 7394 | // All-zero means Infinity means equal. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7395 | __ Ret(eq); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7396 | if (cc == le) { |
| 7397 | __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail. |
| 7398 | } else { |
| 7399 | __ mov(r0, Operand(LESS)); // NaN >= NaN should fail. |
| 7400 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7401 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7402 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7403 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7404 | // No fall through here. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7405 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7406 | |
| 7407 | __ bind(¬_identical); |
| 7408 | } |
| 7409 | |
| 7410 | |
| 7411 | // See comment at call site. |
| 7412 | static void EmitSmiNonsmiComparison(MacroAssembler* masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7413 | Register lhs, |
| 7414 | Register rhs, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7415 | Label* lhs_not_nan, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7416 | Label* slow, |
| 7417 | bool strict) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7418 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7419 | (lhs.is(r1) && rhs.is(r0))); |
| 7420 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7421 | Label rhs_is_smi; |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7422 | __ tst(rhs, Operand(kSmiTagMask)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7423 | __ b(eq, &rhs_is_smi); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7424 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7425 | // Lhs is a Smi. Check whether the rhs is a heap number. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7426 | __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7427 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7428 | // If rhs is not a number and lhs is a Smi then strict equality cannot |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7429 | // succeed. Return non-equal |
| 7430 | // If rhs is r0 then there is already a non zero value in it. |
| 7431 | if (!rhs.is(r0)) { |
| 7432 | __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne); |
| 7433 | } |
| 7434 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7435 | } else { |
| 7436 | // Smi compared non-strictly with a non-Smi non-heap-number. Call |
| 7437 | // the runtime. |
| 7438 | __ b(ne, slow); |
| 7439 | } |
| 7440 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7441 | // Lhs is a smi, rhs is a number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7442 | if (CpuFeatures::IsSupported(VFP3)) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7443 | // Convert lhs to a double in d7. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7444 | CpuFeatures::Scope scope(VFP3); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7445 | __ SmiToDoubleVFPRegister(lhs, d7, r7, s15); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7446 | // Load the double from rhs, tagged HeapNumber r0, to d6. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7447 | __ sub(r7, rhs, Operand(kHeapObjectTag)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7448 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7449 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7450 | __ push(lr); |
| 7451 | // Convert lhs to a double in r2, r3. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7452 | __ mov(r7, Operand(lhs)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7453 | ConvertToDoubleStub stub1(r3, r2, r7, r6); |
| 7454 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7455 | // Load rhs to a double in r0, r1. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7456 | __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7457 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7460 | // We now have both loaded as doubles but we can skip the lhs nan check |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7461 | // since it's a smi. |
| 7462 | __ jmp(lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7463 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7464 | __ bind(&rhs_is_smi); |
| 7465 | // Rhs is a smi. Check whether the non-smi lhs is a heap number. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7466 | __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7467 | if (strict) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7468 | // If lhs is not a number and rhs is a smi then strict equality cannot |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7469 | // succeed. Return non-equal. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7470 | // If lhs is r0 then there is already a non zero value in it. |
| 7471 | if (!lhs.is(r0)) { |
| 7472 | __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne); |
| 7473 | } |
| 7474 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7475 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7476 | // Smi compared non-strictly with a non-smi non-heap-number. Call |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7477 | // the runtime. |
| 7478 | __ b(ne, slow); |
| 7479 | } |
| 7480 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7481 | // Rhs is a smi, lhs is a heap number. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7482 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7483 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7484 | // Load the double from lhs, tagged HeapNumber r1, to d7. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7485 | __ sub(r7, lhs, Operand(kHeapObjectTag)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7486 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7487 | // Convert rhs to a double in d6 . |
| 7488 | __ SmiToDoubleVFPRegister(rhs, d6, r7, s13); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7489 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7490 | __ push(lr); |
| 7491 | // Load lhs to a double in r2, r3. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7492 | __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7493 | // Convert rhs to a double in r0, r1. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7494 | __ mov(r7, Operand(rhs)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7495 | ConvertToDoubleStub stub2(r1, r0, r7, r6); |
| 7496 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7497 | __ pop(lr); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7498 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7499 | // Fall through to both_loaded_as_doubles. |
| 7500 | } |
| 7501 | |
| 7502 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7503 | void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7504 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7505 | Register rhs_exponent = exp_first ? r0 : r1; |
| 7506 | Register lhs_exponent = exp_first ? r2 : r3; |
| 7507 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 7508 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7509 | Label one_is_nan, neither_is_nan; |
| 7510 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7511 | __ Sbfx(r4, |
| 7512 | lhs_exponent, |
| 7513 | HeapNumber::kExponentShift, |
| 7514 | HeapNumber::kExponentBits); |
| 7515 | // NaNs have all-one exponents so they sign extend to -1. |
| 7516 | __ cmp(r4, Operand(-1)); |
| 7517 | __ b(ne, lhs_not_nan); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7518 | __ mov(r4, |
| 7519 | Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 7520 | SetCC); |
| 7521 | __ b(ne, &one_is_nan); |
| 7522 | __ cmp(lhs_mantissa, Operand(0)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7523 | __ b(ne, &one_is_nan); |
| 7524 | |
| 7525 | __ bind(lhs_not_nan); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7526 | __ Sbfx(r4, |
| 7527 | rhs_exponent, |
| 7528 | HeapNumber::kExponentShift, |
| 7529 | HeapNumber::kExponentBits); |
| 7530 | // NaNs have all-one exponents so they sign extend to -1. |
| 7531 | __ cmp(r4, Operand(-1)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7532 | __ b(ne, &neither_is_nan); |
| 7533 | __ mov(r4, |
| 7534 | Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord), |
| 7535 | SetCC); |
| 7536 | __ b(ne, &one_is_nan); |
| 7537 | __ cmp(rhs_mantissa, Operand(0)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7538 | __ b(eq, &neither_is_nan); |
| 7539 | |
| 7540 | __ bind(&one_is_nan); |
| 7541 | // NaN comparisons always fail. |
| 7542 | // Load whatever we need in r0 to make the comparison fail. |
| 7543 | if (cc == lt || cc == le) { |
| 7544 | __ mov(r0, Operand(GREATER)); |
| 7545 | } else { |
| 7546 | __ mov(r0, Operand(LESS)); |
| 7547 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7548 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7549 | |
| 7550 | __ bind(&neither_is_nan); |
| 7551 | } |
| 7552 | |
| 7553 | |
| 7554 | // See comment at call site. |
| 7555 | static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) { |
| 7556 | bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7557 | Register rhs_exponent = exp_first ? r0 : r1; |
| 7558 | Register lhs_exponent = exp_first ? r2 : r3; |
| 7559 | Register rhs_mantissa = exp_first ? r1 : r0; |
| 7560 | Register lhs_mantissa = exp_first ? r3 : r2; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7561 | |
| 7562 | // r0, r1, r2, r3 have the two doubles. Neither is a NaN. |
| 7563 | if (cc == eq) { |
| 7564 | // Doubles are not equal unless they have the same bit pattern. |
| 7565 | // Exception: 0 and -0. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7566 | __ cmp(rhs_mantissa, Operand(lhs_mantissa)); |
| 7567 | __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7568 | // Return non-zero if the numbers are unequal. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7569 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7570 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7571 | __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7572 | // If exponents are equal then return 0. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7573 | __ Ret(eq); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7574 | |
| 7575 | // Exponents are unequal. The only way we can return that the numbers |
| 7576 | // are equal is if one is -0 and the other is 0. We already dealt |
| 7577 | // with the case where both are -0 or both are 0. |
| 7578 | // We start by seeing if the mantissas (that are equal) or the bottom |
| 7579 | // 31 bits of the rhs exponent are non-zero. If so we return not |
| 7580 | // equal. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7581 | __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7582 | __ mov(r0, Operand(r4), LeaveCC, ne); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7583 | __ Ret(ne); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7584 | // Now they are equal if and only if the lhs exponent is zero in its |
| 7585 | // low 31 bits. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7586 | __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize)); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7587 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7588 | } else { |
| 7589 | // Call a native function to do a comparison between two non-NaNs. |
| 7590 | // Call C routine that may not cause GC or other trouble. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 7591 | __ push(lr); |
| 7592 | __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments. |
| 7593 | __ CallCFunction(ExternalReference::compare_doubles(), 4); |
| 7594 | __ pop(pc); // Return. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7595 | } |
| 7596 | } |
| 7597 | |
| 7598 | |
| 7599 | // See comment at call site. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7600 | static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm, |
| 7601 | Register lhs, |
| 7602 | Register rhs) { |
| 7603 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7604 | (lhs.is(r1) && rhs.is(r0))); |
| 7605 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7606 | // If either operand is a JSObject or an oddball value, then they are |
| 7607 | // not equal since their pointers are different. |
| 7608 | // There is no test for undetectability in strict equality. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7609 | STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7610 | Label first_non_object; |
| 7611 | // Get the type of the first operand into r2 and compare it with |
| 7612 | // FIRST_JS_OBJECT_TYPE. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7613 | __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7614 | __ b(lt, &first_non_object); |
| 7615 | |
| 7616 | // Return non-zero (r0 is not zero) |
| 7617 | Label return_not_equal; |
| 7618 | __ bind(&return_not_equal); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7619 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7620 | |
| 7621 | __ bind(&first_non_object); |
| 7622 | // Check for oddballs: true, false, null, undefined. |
| 7623 | __ cmp(r2, Operand(ODDBALL_TYPE)); |
| 7624 | __ b(eq, &return_not_equal); |
| 7625 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7626 | __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7627 | __ b(ge, &return_not_equal); |
| 7628 | |
| 7629 | // Check for oddballs: true, false, null, undefined. |
| 7630 | __ cmp(r3, Operand(ODDBALL_TYPE)); |
| 7631 | __ b(eq, &return_not_equal); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7632 | |
| 7633 | // Now that we have the types we might as well check for symbol-symbol. |
| 7634 | // Ensure that no non-strings have the symbol bit set. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7635 | STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask); |
| 7636 | STATIC_ASSERT(kSymbolTag != 0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7637 | __ and_(r2, r2, Operand(r3)); |
| 7638 | __ tst(r2, Operand(kIsSymbolMask)); |
| 7639 | __ b(ne, &return_not_equal); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | |
| 7643 | // See comment at call site. |
| 7644 | static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7645 | Register lhs, |
| 7646 | Register rhs, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7647 | Label* both_loaded_as_doubles, |
| 7648 | Label* not_heap_numbers, |
| 7649 | Label* slow) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7650 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7651 | (lhs.is(r1) && rhs.is(r0))); |
| 7652 | |
| 7653 | __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7654 | __ b(ne, not_heap_numbers); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7655 | __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7656 | __ cmp(r2, r3); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7657 | __ b(ne, slow); // First was a heap number, second wasn't. Go slow case. |
| 7658 | |
| 7659 | // Both are heap numbers. Load them up then jump to the code we have |
| 7660 | // for that. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7661 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7662 | CpuFeatures::Scope scope(VFP3); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7663 | __ sub(r7, rhs, Operand(kHeapObjectTag)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7664 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7665 | __ sub(r7, lhs, Operand(kHeapObjectTag)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7666 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
| 7667 | } else { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7668 | __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset)); |
| 7669 | __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7670 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7671 | __ jmp(both_loaded_as_doubles); |
| 7672 | } |
| 7673 | |
| 7674 | |
| 7675 | // Fast negative check for symbol-to-symbol equality. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7676 | static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7677 | Register lhs, |
| 7678 | Register rhs, |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7679 | Label* possible_strings, |
| 7680 | Label* not_both_strings) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7681 | ASSERT((lhs.is(r0) && rhs.is(r1)) || |
| 7682 | (lhs.is(r1) && rhs.is(r0))); |
| 7683 | |
| 7684 | // r2 is object type of rhs. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7685 | // Ensure that no non-strings have the symbol bit set. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7686 | Label object_test; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7687 | STATIC_ASSERT(kSymbolTag != 0); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7688 | __ tst(r2, Operand(kIsNotStringMask)); |
| 7689 | __ b(ne, &object_test); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7690 | __ tst(r2, Operand(kIsSymbolMask)); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7691 | __ b(eq, possible_strings); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7692 | __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7693 | __ b(ge, not_both_strings); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7694 | __ tst(r3, Operand(kIsSymbolMask)); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7695 | __ b(eq, possible_strings); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7696 | |
| 7697 | // Both are symbols. We already checked they weren't the same pointer |
| 7698 | // so they are not equal. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7699 | __ mov(r0, Operand(NOT_EQUAL)); |
| 7700 | __ Ret(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7701 | |
| 7702 | __ bind(&object_test); |
| 7703 | __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE)); |
| 7704 | __ b(lt, not_both_strings); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7705 | __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7706 | __ b(lt, not_both_strings); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7707 | // If both objects are undetectable, they are equal. Otherwise, they |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7708 | // are not equal, since they are different objects and an object is not |
| 7709 | // equal to undefined. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7710 | __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset)); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7711 | __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset)); |
| 7712 | __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset)); |
| 7713 | __ and_(r0, r2, Operand(r3)); |
| 7714 | __ and_(r0, r0, Operand(1 << Map::kIsUndetectable)); |
| 7715 | __ eor(r0, r0, Operand(1 << Map::kIsUndetectable)); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7716 | __ Ret(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7717 | } |
| 7718 | |
| 7719 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7720 | void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 7721 | Register object, |
| 7722 | Register result, |
| 7723 | Register scratch1, |
| 7724 | Register scratch2, |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7725 | Register scratch3, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7726 | bool object_is_smi, |
| 7727 | Label* not_found) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7728 | // Use of registers. Register result is used as a temporary. |
| 7729 | Register number_string_cache = result; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7730 | Register mask = scratch3; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7731 | |
| 7732 | // Load the number string cache. |
| 7733 | __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex); |
| 7734 | |
| 7735 | // Make the hash mask from the length of the number string cache. It |
| 7736 | // contains two elements (number and string) for each cache entry. |
| 7737 | __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7738 | // Divide length by two (length is a smi). |
| 7739 | __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7740 | __ sub(mask, mask, Operand(1)); // Make mask. |
| 7741 | |
| 7742 | // Calculate the entry in the number string cache. The hash value in the |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7743 | // number string cache for smis is just the smi value, and the hash for |
| 7744 | // doubles is the xor of the upper and lower words. See |
| 7745 | // Heap::GetNumberStringCache. |
| 7746 | Label is_smi; |
| 7747 | Label load_result_from_cache; |
| 7748 | if (!object_is_smi) { |
| 7749 | __ BranchOnSmi(object, &is_smi); |
| 7750 | if (CpuFeatures::IsSupported(VFP3)) { |
| 7751 | CpuFeatures::Scope scope(VFP3); |
| 7752 | __ CheckMap(object, |
| 7753 | scratch1, |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 7754 | Heap::kHeapNumberMapRootIndex, |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7755 | not_found, |
| 7756 | true); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7757 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7758 | STATIC_ASSERT(8 == kDoubleSize); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7759 | __ add(scratch1, |
| 7760 | object, |
| 7761 | Operand(HeapNumber::kValueOffset - kHeapObjectTag)); |
| 7762 | __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit()); |
| 7763 | __ eor(scratch1, scratch1, Operand(scratch2)); |
| 7764 | __ and_(scratch1, scratch1, Operand(mask)); |
| 7765 | |
| 7766 | // Calculate address of entry in string cache: each entry consists |
| 7767 | // of two pointer sized fields. |
| 7768 | __ add(scratch1, |
| 7769 | number_string_cache, |
| 7770 | Operand(scratch1, LSL, kPointerSizeLog2 + 1)); |
| 7771 | |
| 7772 | Register probe = mask; |
| 7773 | __ ldr(probe, |
| 7774 | FieldMemOperand(scratch1, FixedArray::kHeaderSize)); |
| 7775 | __ BranchOnSmi(probe, not_found); |
| 7776 | __ sub(scratch2, object, Operand(kHeapObjectTag)); |
| 7777 | __ vldr(d0, scratch2, HeapNumber::kValueOffset); |
| 7778 | __ sub(probe, probe, Operand(kHeapObjectTag)); |
| 7779 | __ vldr(d1, probe, HeapNumber::kValueOffset); |
| 7780 | __ vcmp(d0, d1); |
| 7781 | __ vmrs(pc); |
| 7782 | __ b(ne, not_found); // The cache did not contain this value. |
| 7783 | __ b(&load_result_from_cache); |
| 7784 | } else { |
| 7785 | __ b(not_found); |
| 7786 | } |
| 7787 | } |
| 7788 | |
| 7789 | __ bind(&is_smi); |
| 7790 | Register scratch = scratch1; |
| 7791 | __ and_(scratch, mask, Operand(object, ASR, 1)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7792 | // Calculate address of entry in string cache: each entry consists |
| 7793 | // of two pointer sized fields. |
| 7794 | __ add(scratch, |
| 7795 | number_string_cache, |
| 7796 | Operand(scratch, LSL, kPointerSizeLog2 + 1)); |
| 7797 | |
| 7798 | // Check if the entry is the smi we are looking for. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7799 | Register probe = mask; |
| 7800 | __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize)); |
| 7801 | __ cmp(object, probe); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7802 | __ b(ne, not_found); |
| 7803 | |
| 7804 | // Get the result from the cache. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7805 | __ bind(&load_result_from_cache); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7806 | __ ldr(result, |
| 7807 | FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7808 | __ IncrementCounter(&Counters::number_to_string_native, |
| 7809 | 1, |
| 7810 | scratch1, |
| 7811 | scratch2); |
| 7812 | } |
| 7813 | |
| 7814 | |
| 7815 | void NumberToStringStub::Generate(MacroAssembler* masm) { |
| 7816 | Label runtime; |
| 7817 | |
| 7818 | __ ldr(r1, MemOperand(sp, 0)); |
| 7819 | |
| 7820 | // Generate code to lookup number in the number string cache. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7821 | GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7822 | __ add(sp, sp, Operand(1 * kPointerSize)); |
| 7823 | __ Ret(); |
| 7824 | |
| 7825 | __ bind(&runtime); |
| 7826 | // Handle number to string in the runtime system if not found in the cache. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 7827 | __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
| 7830 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 7831 | void RecordWriteStub::Generate(MacroAssembler* masm) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7832 | __ add(offset_, object_, Operand(offset_)); |
| 7833 | __ RecordWriteHelper(object_, offset_, scratch_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 7834 | __ Ret(); |
| 7835 | } |
| 7836 | |
| 7837 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7838 | // On entry lhs_ and rhs_ are the values to be compared. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7839 | // On exit r0 is 0, positive or negative to indicate the result of |
| 7840 | // the comparison. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7841 | void CompareStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7842 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 7843 | (lhs_.is(r1) && rhs_.is(r0))); |
| 7844 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7845 | Label slow; // Call builtin. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7846 | Label not_smis, both_loaded_as_doubles, lhs_not_nan; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7847 | |
| 7848 | // NOTICE! This code is only reached after a smi-fast-case check, so |
| 7849 | // it is certain that at least one operand isn't a smi. |
| 7850 | |
| 7851 | // Handle the case where the objects are identical. Either returns the answer |
| 7852 | // or goes to slow. Only falls through if the objects were not identical. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7853 | EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7854 | |
| 7855 | // If either is a Smi (we know that not both are), then they can only |
| 7856 | // be strictly equal if the other is a HeapNumber. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7857 | STATIC_ASSERT(kSmiTag == 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7858 | ASSERT_EQ(0, Smi::FromInt(0)); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7859 | __ and_(r2, lhs_, Operand(rhs_)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7860 | __ tst(r2, Operand(kSmiTagMask)); |
| 7861 | __ b(ne, ¬_smis); |
| 7862 | // One operand is a smi. EmitSmiNonsmiComparison generates code that can: |
| 7863 | // 1) Return the answer. |
| 7864 | // 2) Go to slow. |
| 7865 | // 3) Fall through to both_loaded_as_doubles. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7866 | // 4) Jump to lhs_not_nan. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7867 | // In cases 3 and 4 we have found out we were dealing with a number-number |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7868 | // comparison. If VFP3 is supported the double values of the numbers have |
| 7869 | // been loaded into d7 and d6. Otherwise, the double values have been loaded |
| 7870 | // into r0, r1, r2, and r3. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7871 | EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7872 | |
| 7873 | __ bind(&both_loaded_as_doubles); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7874 | // The arguments have been converted to doubles and stored in d6 and d7, if |
| 7875 | // VFP3 is supported, or in r0, r1, r2, and r3. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7876 | if (CpuFeatures::IsSupported(VFP3)) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7877 | __ bind(&lhs_not_nan); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7878 | CpuFeatures::Scope scope(VFP3); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7879 | Label no_nan; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7880 | // ARMv7 VFP3 instructions to implement double precision comparison. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7881 | __ vcmp(d7, d6); |
| 7882 | __ vmrs(pc); // Move vector status bits to normal status bits. |
| 7883 | Label nan; |
| 7884 | __ b(vs, &nan); |
| 7885 | __ mov(r0, Operand(EQUAL), LeaveCC, eq); |
| 7886 | __ mov(r0, Operand(LESS), LeaveCC, lt); |
| 7887 | __ mov(r0, Operand(GREATER), LeaveCC, gt); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7888 | __ Ret(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7889 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7890 | __ bind(&nan); |
| 7891 | // If one of the sides was a NaN then the v flag is set. Load r0 with |
| 7892 | // whatever it takes to make the comparison fail, since comparisons with NaN |
| 7893 | // always fail. |
| 7894 | if (cc_ == lt || cc_ == le) { |
| 7895 | __ mov(r0, Operand(GREATER)); |
| 7896 | } else { |
| 7897 | __ mov(r0, Operand(LESS)); |
| 7898 | } |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7899 | __ Ret(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7900 | } else { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7901 | // Checks for NaN in the doubles we have loaded. Can return the answer or |
| 7902 | // fall through if neither is a NaN. Also binds lhs_not_nan. |
| 7903 | EmitNanCheck(masm, &lhs_not_nan, cc_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7904 | // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the |
| 7905 | // answer. Never falls through. |
| 7906 | EmitTwoNonNanDoubleComparison(masm, cc_); |
| 7907 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7908 | |
| 7909 | __ bind(¬_smis); |
| 7910 | // At this point we know we are dealing with two different objects, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7911 | // and neither of them is a Smi. The objects are in rhs_ and lhs_. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7912 | if (strict_) { |
| 7913 | // This returns non-equal for some object types, or falls through if it |
| 7914 | // was not lucky. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7915 | EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | Label check_for_symbols; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7919 | Label flat_string_check; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7920 | // Check for heap-number-heap-number comparison. Can jump to slow case, |
| 7921 | // or load both doubles into r0, r1, r2, r3 and jump to the code that handles |
| 7922 | // that case. If the inputs are not doubles then jumps to check_for_symbols. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7923 | // In this case r2 will contain the type of rhs_. Never falls through. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7924 | EmitCheckForTwoHeapNumbers(masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7925 | lhs_, |
| 7926 | rhs_, |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7927 | &both_loaded_as_doubles, |
| 7928 | &check_for_symbols, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7929 | &flat_string_check); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7930 | |
| 7931 | __ bind(&check_for_symbols); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7932 | // In the strict case the EmitStrictTwoHeapObjectCompare already took care of |
| 7933 | // symbols. |
| 7934 | if (cc_ == eq && !strict_) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 7935 | // Returns an answer for two symbols or two detectable objects. |
| 7936 | // Otherwise jumps to string case or not both strings case. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7937 | // Assumes that r2 is the type of rhs_ on entry. |
| 7938 | EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7941 | // Check for both being sequential ASCII strings, and inline if that is the |
| 7942 | // case. |
| 7943 | __ bind(&flat_string_check); |
| 7944 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7945 | __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7946 | |
| 7947 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 7948 | StringCompareStub::GenerateCompareFlatAsciiStrings(masm, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7949 | lhs_, |
| 7950 | rhs_, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7951 | r2, |
| 7952 | r3, |
| 7953 | r4, |
| 7954 | r5); |
| 7955 | // Never falls through to here. |
| 7956 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7957 | __ bind(&slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7958 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7959 | __ Push(lhs_, rhs_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7960 | // Figure out which native to call and setup the arguments. |
| 7961 | Builtins::JavaScript native; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7962 | if (cc_ == eq) { |
| 7963 | native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS; |
| 7964 | } else { |
| 7965 | native = Builtins::COMPARE; |
| 7966 | int ncr; // NaN compare result |
| 7967 | if (cc_ == lt || cc_ == le) { |
| 7968 | ncr = GREATER; |
| 7969 | } else { |
| 7970 | ASSERT(cc_ == gt || cc_ == ge); // remaining cases |
| 7971 | ncr = LESS; |
| 7972 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7973 | __ mov(r0, Operand(Smi::FromInt(ncr))); |
| 7974 | __ push(r0); |
| 7975 | } |
| 7976 | |
| 7977 | // Call the native; it returns -1 (less), 0 (equal), or 1 (greater) |
| 7978 | // tagged as a small integer. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7979 | __ InvokeBuiltin(native, JUMP_JS); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame^] | 7983 | // This stub does not handle the inlined cases (Smis, Booleans, undefined). |
| 7984 | // The stub returns zero for false, and a non-zero value for true. |
| 7985 | void ToBooleanStub::Generate(MacroAssembler* masm) { |
| 7986 | Label false_result; |
| 7987 | Label not_heap_number; |
| 7988 | Register scratch0 = VirtualFrame::scratch0(); |
| 7989 | |
| 7990 | // HeapNumber => false iff +0, -0, or NaN. |
| 7991 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 7992 | __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex); |
| 7993 | __ cmp(scratch0, ip); |
| 7994 | __ b(¬_heap_number, ne); |
| 7995 | |
| 7996 | __ sub(ip, tos_, Operand(kHeapObjectTag)); |
| 7997 | __ vldr(d1, ip, HeapNumber::kValueOffset); |
| 7998 | __ vcmp(d1, 0.0); |
| 7999 | __ vmrs(pc); |
| 8000 | // "tos_" is a register, and contains a non zero value by default. |
| 8001 | // Hence we only need to overwrite "tos_" with zero to return false for |
| 8002 | // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true. |
| 8003 | __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO |
| 8004 | __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN |
| 8005 | __ Ret(); |
| 8006 | |
| 8007 | __ bind(¬_heap_number); |
| 8008 | |
| 8009 | // Check if the value is 'null'. |
| 8010 | // 'null' => false. |
| 8011 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 8012 | __ cmp(tos_, ip); |
| 8013 | __ b(&false_result, eq); |
| 8014 | |
| 8015 | // It can be an undetectable object. |
| 8016 | // Undetectable => false. |
| 8017 | __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8018 | __ ldrb(scratch0, FieldMemOperand(ip, Map::kBitFieldOffset)); |
| 8019 | __ and_(scratch0, scratch0, Operand(1 << Map::kIsUndetectable)); |
| 8020 | __ cmp(scratch0, Operand(1 << Map::kIsUndetectable)); |
| 8021 | __ b(&false_result, eq); |
| 8022 | |
| 8023 | // JavaScript object => true. |
| 8024 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8025 | __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset)); |
| 8026 | __ cmp(scratch0, Operand(FIRST_JS_OBJECT_TYPE)); |
| 8027 | // "tos_" is a register and contains a non-zero value. |
| 8028 | // Hence we implicitly return true if the greater than |
| 8029 | // condition is satisfied. |
| 8030 | __ Ret(gt); |
| 8031 | |
| 8032 | // Check for string |
| 8033 | __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset)); |
| 8034 | __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset)); |
| 8035 | __ cmp(scratch0, Operand(FIRST_NONSTRING_TYPE)); |
| 8036 | // "tos_" is a register and contains a non-zero value. |
| 8037 | // Hence we implicitly return true if the greater than |
| 8038 | // condition is satisfied. |
| 8039 | __ Ret(gt); |
| 8040 | |
| 8041 | // String value => false iff empty, i.e., length is zero |
| 8042 | __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset)); |
| 8043 | // If length is zero, "tos_" contains zero ==> false. |
| 8044 | // If length is not zero, "tos_" contains a non-zero value ==> true. |
| 8045 | __ Ret(); |
| 8046 | |
| 8047 | // Return 0 in "tos_" for false . |
| 8048 | __ bind(&false_result); |
| 8049 | __ mov(tos_, Operand(0)); |
| 8050 | __ Ret(); |
| 8051 | } |
| 8052 | |
| 8053 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 8054 | // We fall into this code if the operands were Smis, but the result was |
| 8055 | // not (eg. overflow). We branch into this code (to the not_smi label) if |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8056 | // the operands were not both Smi. The operands are in r0 and r1. In order |
| 8057 | // to call the C-implemented binary fp operation routines we need to end up |
| 8058 | // with the double precision floating point operands in r0 and r1 (for the |
| 8059 | // value in r1) and r2 and r3 (for the value in r0). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8060 | void GenericBinaryOpStub::HandleBinaryOpSlowCases( |
| 8061 | MacroAssembler* masm, |
| 8062 | Label* not_smi, |
| 8063 | Register lhs, |
| 8064 | Register rhs, |
| 8065 | const Builtins::JavaScript& builtin) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 8066 | Label slow, slow_reverse, do_the_call; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8067 | bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8068 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 8069 | ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8070 | Register heap_number_map = r6; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8071 | |
| 8072 | if (ShouldGenerateSmiCode()) { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8073 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8074 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8075 | // Smi-smi case (overflow). |
| 8076 | // Since both are Smis there is no heap number to overwrite, so allocate. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8077 | // The new heap number is in r5. r3 and r7 are scratch. |
| 8078 | __ AllocateHeapNumber( |
| 8079 | r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8080 | |
| 8081 | // If we have floating point hardware, inline ADD, SUB, MUL, and DIV, |
| 8082 | // using registers d7 and d6 for the double values. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8083 | if (CpuFeatures::IsSupported(VFP3)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8084 | CpuFeatures::Scope scope(VFP3); |
| 8085 | __ mov(r7, Operand(rhs, ASR, kSmiTagSize)); |
| 8086 | __ vmov(s15, r7); |
| 8087 | __ vcvt_f64_s32(d7, s15); |
| 8088 | __ mov(r7, Operand(lhs, ASR, kSmiTagSize)); |
| 8089 | __ vmov(s13, r7); |
| 8090 | __ vcvt_f64_s32(d6, s13); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8091 | if (!use_fp_registers) { |
| 8092 | __ vmov(r2, r3, d7); |
| 8093 | __ vmov(r0, r1, d6); |
| 8094 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8095 | } else { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8096 | // Write Smi from rhs to r3 and r2 in double format. r9 is scratch. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8097 | __ mov(r7, Operand(rhs)); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8098 | ConvertToDoubleStub stub1(r3, r2, r7, r9); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8099 | __ push(lr); |
| 8100 | __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8101 | // Write Smi from lhs to r1 and r0 in double format. r9 is scratch. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8102 | __ mov(r7, Operand(lhs)); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8103 | ConvertToDoubleStub stub2(r1, r0, r7, r9); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8104 | __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET); |
| 8105 | __ pop(lr); |
| 8106 | } |
| 8107 | __ jmp(&do_the_call); // Tail call. No return. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8108 | } |
| 8109 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8110 | // We branch here if at least one of r0 and r1 is not a Smi. |
| 8111 | __ bind(not_smi); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8112 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8113 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 8114 | // After this point we have the left hand side in r1 and the right hand side |
| 8115 | // in r0. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8116 | if (lhs.is(r0)) { |
| 8117 | __ Swap(r0, r1, ip); |
| 8118 | } |
| 8119 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8120 | // The type transition also calculates the answer. |
| 8121 | bool generate_code_to_calculate_answer = true; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 8122 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8123 | if (ShouldGenerateFPCode()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8124 | if (runtime_operands_type_ == BinaryOpIC::DEFAULT) { |
| 8125 | switch (op_) { |
| 8126 | case Token::ADD: |
| 8127 | case Token::SUB: |
| 8128 | case Token::MUL: |
| 8129 | case Token::DIV: |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8130 | GenerateTypeTransition(masm); // Tail call. |
| 8131 | generate_code_to_calculate_answer = false; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8132 | break; |
| 8133 | |
| 8134 | default: |
| 8135 | break; |
| 8136 | } |
| 8137 | } |
| 8138 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8139 | if (generate_code_to_calculate_answer) { |
| 8140 | Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1; |
| 8141 | if (mode_ == NO_OVERWRITE) { |
| 8142 | // In the case where there is no chance of an overwritable float we may |
| 8143 | // as well do the allocation immediately while r0 and r1 are untouched. |
| 8144 | __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8145 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8146 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8147 | // Move r0 to a double in r2-r3. |
| 8148 | __ tst(r0, Operand(kSmiTagMask)); |
| 8149 | __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number. |
| 8150 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 8151 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8152 | __ cmp(r4, heap_number_map); |
| 8153 | __ b(ne, &slow); |
| 8154 | if (mode_ == OVERWRITE_RIGHT) { |
| 8155 | __ mov(r5, Operand(r0)); // Overwrite this heap number. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8156 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8157 | if (use_fp_registers) { |
| 8158 | CpuFeatures::Scope scope(VFP3); |
| 8159 | // Load the double from tagged HeapNumber r0 to d7. |
| 8160 | __ sub(r7, r0, Operand(kHeapObjectTag)); |
| 8161 | __ vldr(d7, r7, HeapNumber::kValueOffset); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8162 | } else { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8163 | // Calling convention says that second double is in r2 and r3. |
| 8164 | __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8165 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8166 | __ jmp(&finished_loading_r0); |
| 8167 | __ bind(&r0_is_smi); |
| 8168 | if (mode_ == OVERWRITE_RIGHT) { |
| 8169 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 8170 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
| 8171 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8172 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8173 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8174 | CpuFeatures::Scope scope(VFP3); |
| 8175 | // Convert smi in r0 to double in d7. |
| 8176 | __ mov(r7, Operand(r0, ASR, kSmiTagSize)); |
| 8177 | __ vmov(s15, r7); |
| 8178 | __ vcvt_f64_s32(d7, s15); |
| 8179 | if (!use_fp_registers) { |
| 8180 | __ vmov(r2, r3, d7); |
| 8181 | } |
| 8182 | } else { |
| 8183 | // Write Smi from r0 to r3 and r2 in double format. |
| 8184 | __ mov(r7, Operand(r0)); |
| 8185 | ConvertToDoubleStub stub3(r3, r2, r7, r4); |
| 8186 | __ push(lr); |
| 8187 | __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET); |
| 8188 | __ pop(lr); |
| 8189 | } |
| 8190 | |
| 8191 | // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis. |
| 8192 | // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC. |
| 8193 | Label r1_is_not_smi; |
| 8194 | if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) { |
| 8195 | __ tst(r1, Operand(kSmiTagMask)); |
| 8196 | __ b(ne, &r1_is_not_smi); |
| 8197 | GenerateTypeTransition(masm); // Tail call. |
| 8198 | } |
| 8199 | |
| 8200 | __ bind(&finished_loading_r0); |
| 8201 | |
| 8202 | // Move r1 to a double in r0-r1. |
| 8203 | __ tst(r1, Operand(kSmiTagMask)); |
| 8204 | __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number. |
| 8205 | __ bind(&r1_is_not_smi); |
| 8206 | __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset)); |
| 8207 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8208 | __ cmp(r4, heap_number_map); |
| 8209 | __ b(ne, &slow); |
| 8210 | if (mode_ == OVERWRITE_LEFT) { |
| 8211 | __ mov(r5, Operand(r1)); // Overwrite this heap number. |
| 8212 | } |
| 8213 | if (use_fp_registers) { |
| 8214 | CpuFeatures::Scope scope(VFP3); |
| 8215 | // Load the double from tagged HeapNumber r1 to d6. |
| 8216 | __ sub(r7, r1, Operand(kHeapObjectTag)); |
| 8217 | __ vldr(d6, r7, HeapNumber::kValueOffset); |
| 8218 | } else { |
| 8219 | // Calling convention says that first double is in r0 and r1. |
| 8220 | __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset)); |
| 8221 | } |
| 8222 | __ jmp(&finished_loading_r1); |
| 8223 | __ bind(&r1_is_smi); |
| 8224 | if (mode_ == OVERWRITE_LEFT) { |
| 8225 | // We can't overwrite a Smi so get address of new heap number into r5. |
| 8226 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
| 8227 | } |
| 8228 | |
| 8229 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8230 | CpuFeatures::Scope scope(VFP3); |
| 8231 | // Convert smi in r1 to double in d6. |
| 8232 | __ mov(r7, Operand(r1, ASR, kSmiTagSize)); |
| 8233 | __ vmov(s13, r7); |
| 8234 | __ vcvt_f64_s32(d6, s13); |
| 8235 | if (!use_fp_registers) { |
| 8236 | __ vmov(r0, r1, d6); |
| 8237 | } |
| 8238 | } else { |
| 8239 | // Write Smi from r1 to r1 and r0 in double format. |
| 8240 | __ mov(r7, Operand(r1)); |
| 8241 | ConvertToDoubleStub stub4(r1, r0, r7, r9); |
| 8242 | __ push(lr); |
| 8243 | __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET); |
| 8244 | __ pop(lr); |
| 8245 | } |
| 8246 | |
| 8247 | __ bind(&finished_loading_r1); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8248 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 8249 | |
| 8250 | if (generate_code_to_calculate_answer || do_the_call.is_linked()) { |
| 8251 | __ bind(&do_the_call); |
| 8252 | // If we are inlining the operation using VFP3 instructions for |
| 8253 | // add, subtract, multiply, or divide, the arguments are in d6 and d7. |
| 8254 | if (use_fp_registers) { |
| 8255 | CpuFeatures::Scope scope(VFP3); |
| 8256 | // ARMv7 VFP3 instructions to implement |
| 8257 | // double precision, add, subtract, multiply, divide. |
| 8258 | |
| 8259 | if (Token::MUL == op_) { |
| 8260 | __ vmul(d5, d6, d7); |
| 8261 | } else if (Token::DIV == op_) { |
| 8262 | __ vdiv(d5, d6, d7); |
| 8263 | } else if (Token::ADD == op_) { |
| 8264 | __ vadd(d5, d6, d7); |
| 8265 | } else if (Token::SUB == op_) { |
| 8266 | __ vsub(d5, d6, d7); |
| 8267 | } else { |
| 8268 | UNREACHABLE(); |
| 8269 | } |
| 8270 | __ sub(r0, r5, Operand(kHeapObjectTag)); |
| 8271 | __ vstr(d5, r0, HeapNumber::kValueOffset); |
| 8272 | __ add(r0, r0, Operand(kHeapObjectTag)); |
| 8273 | __ mov(pc, lr); |
| 8274 | } else { |
| 8275 | // If we did not inline the operation, then the arguments are in: |
| 8276 | // r0: Left value (least significant part of mantissa). |
| 8277 | // r1: Left value (sign, exponent, top of mantissa). |
| 8278 | // r2: Right value (least significant part of mantissa). |
| 8279 | // r3: Right value (sign, exponent, top of mantissa). |
| 8280 | // r5: Address of heap number for result. |
| 8281 | |
| 8282 | __ push(lr); // For later. |
| 8283 | __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments. |
| 8284 | // Call C routine that may not cause GC or other trouble. r5 is callee |
| 8285 | // save. |
| 8286 | __ CallCFunction(ExternalReference::double_fp_operation(op_), 4); |
| 8287 | // Store answer in the overwritable heap number. |
| 8288 | #if !defined(USE_ARM_EABI) |
| 8289 | // Double returned in fp coprocessor register 0 and 1, encoded as |
| 8290 | // register cr8. Offsets must be divisible by 4 for coprocessor so we |
| 8291 | // need to substract the tag from r5. |
| 8292 | __ sub(r4, r5, Operand(kHeapObjectTag)); |
| 8293 | __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset)); |
| 8294 | #else |
| 8295 | // Double returned in registers 0 and 1. |
| 8296 | __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset)); |
| 8297 | #endif |
| 8298 | __ mov(r0, Operand(r5)); |
| 8299 | // And we are done. |
| 8300 | __ pop(pc); |
| 8301 | } |
| 8302 | } |
| 8303 | } |
| 8304 | |
| 8305 | if (!generate_code_to_calculate_answer && |
| 8306 | !slow_reverse.is_linked() && |
| 8307 | !slow.is_linked()) { |
| 8308 | return; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8309 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 8310 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 8311 | if (lhs.is(r0)) { |
| 8312 | __ b(&slow); |
| 8313 | __ bind(&slow_reverse); |
| 8314 | __ Swap(r0, r1, ip); |
| 8315 | } |
| 8316 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8317 | heap_number_map = no_reg; // Don't use this any more from here on. |
| 8318 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8319 | // We jump to here if something goes wrong (one param is not a number of any |
| 8320 | // sort or new-space allocation fails). |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8321 | __ bind(&slow); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8322 | |
| 8323 | // Push arguments to the stack |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 8324 | __ Push(r1, r0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8325 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8326 | if (Token::ADD == op_) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8327 | // Test for string arguments before calling runtime. |
| 8328 | // r1 : first argument |
| 8329 | // r0 : second argument |
| 8330 | // sp[0] : second argument |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8331 | // sp[4] : first argument |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8332 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 8333 | Label not_strings, not_string1, string1, string1_smi2; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8334 | __ tst(r1, Operand(kSmiTagMask)); |
| 8335 | __ b(eq, ¬_string1); |
| 8336 | __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE); |
| 8337 | __ b(ge, ¬_string1); |
| 8338 | |
| 8339 | // First argument is a a string, test second. |
| 8340 | __ tst(r0, Operand(kSmiTagMask)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 8341 | __ b(eq, &string1_smi2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8342 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 8343 | __ b(ge, &string1); |
| 8344 | |
| 8345 | // First and second argument are strings. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 8346 | StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB); |
| 8347 | __ TailCallStub(&string_add_stub); |
| 8348 | |
| 8349 | __ bind(&string1_smi2); |
| 8350 | // First argument is a string, second is a smi. Try to lookup the number |
| 8351 | // string for the smi in the number string cache. |
| 8352 | NumberToStringStub::GenerateLookupNumberStringCache( |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 8353 | masm, r0, r2, r4, r5, r6, true, &string1); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 8354 | |
| 8355 | // Replace second argument on stack and tailcall string add stub to make |
| 8356 | // the result. |
| 8357 | __ str(r2, MemOperand(sp, 0)); |
| 8358 | __ TailCallStub(&string_add_stub); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8359 | |
| 8360 | // Only first argument is a string. |
| 8361 | __ bind(&string1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8362 | __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS); |
| 8363 | |
| 8364 | // First argument was not a string, test second. |
| 8365 | __ bind(¬_string1); |
| 8366 | __ tst(r0, Operand(kSmiTagMask)); |
| 8367 | __ b(eq, ¬_strings); |
| 8368 | __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE); |
| 8369 | __ b(ge, ¬_strings); |
| 8370 | |
| 8371 | // Only second argument is a string. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8372 | __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS); |
| 8373 | |
| 8374 | __ bind(¬_strings); |
| 8375 | } |
| 8376 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8377 | __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8378 | } |
| 8379 | |
| 8380 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8381 | // Tries to get a signed int32 out of a double precision floating point heap |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8382 | // number. Rounds towards 0. Fastest for doubles that are in the ranges |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8383 | // -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds |
| 8384 | // almost to the range of signed int32 values that are not Smis. Jumps to the |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8385 | // label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0 |
| 8386 | // (excluding the endpoints). |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8387 | static void GetInt32(MacroAssembler* masm, |
| 8388 | Register source, |
| 8389 | Register dest, |
| 8390 | Register scratch, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8391 | Register scratch2, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8392 | Label* slow) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8393 | Label right_exponent, done; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8394 | // Get exponent word. |
| 8395 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset)); |
| 8396 | // Get exponent alone in scratch2. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 8397 | __ Ubfx(scratch2, |
| 8398 | scratch, |
| 8399 | HeapNumber::kExponentShift, |
| 8400 | HeapNumber::kExponentBits); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8401 | // Load dest with zero. We use this either for the final shift or |
| 8402 | // for the answer. |
| 8403 | __ mov(dest, Operand(0)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8404 | // Check whether the exponent matches a 32 bit signed int that is not a Smi. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8405 | // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is |
| 8406 | // the exponent that we are fastest at and also the highest exponent we can |
| 8407 | // handle here. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 8408 | const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30; |
| 8409 | // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we |
| 8410 | // split it up to avoid a constant pool entry. You can't do that in general |
| 8411 | // for cmp because of the overflow flag, but we know the exponent is in the |
| 8412 | // range 0-2047 so there is no overflow. |
| 8413 | int fudge_factor = 0x400; |
| 8414 | __ sub(scratch2, scratch2, Operand(fudge_factor)); |
| 8415 | __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8416 | // If we have a match of the int32-but-not-Smi exponent then skip some logic. |
| 8417 | __ b(eq, &right_exponent); |
| 8418 | // If the exponent is higher than that then go to slow case. This catches |
| 8419 | // numbers that don't fit in a signed int32, infinities and NaNs. |
| 8420 | __ b(gt, slow); |
| 8421 | |
| 8422 | // We know the exponent is smaller than 30 (biased). If it is less than |
| 8423 | // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie |
| 8424 | // it rounds to zero. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 8425 | const uint32_t zero_exponent = HeapNumber::kExponentBias + 0; |
| 8426 | __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8427 | // Dest already has a Smi zero. |
| 8428 | __ b(lt, &done); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8429 | if (!CpuFeatures::IsSupported(VFP3)) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 8430 | // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to |
| 8431 | // get how much to shift down. |
| 8432 | __ rsb(dest, scratch2, Operand(30)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8433 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8434 | __ bind(&right_exponent); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8435 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8436 | CpuFeatures::Scope scope(VFP3); |
| 8437 | // ARMv7 VFP3 instructions implementing double precision to integer |
| 8438 | // conversion using round to zero. |
| 8439 | __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 8440 | __ vmov(d7, scratch2, scratch); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8441 | __ vcvt_s32_f64(s15, d7); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 8442 | __ vmov(dest, s15); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8443 | } else { |
| 8444 | // Get the top bits of the mantissa. |
| 8445 | __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask)); |
| 8446 | // Put back the implicit 1. |
| 8447 | __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift)); |
| 8448 | // Shift up the mantissa bits to take up the space the exponent used to |
| 8449 | // take. We just orred in the implicit bit so that took care of one and |
| 8450 | // we want to leave the sign bit 0 so we subtract 2 bits from the shift |
| 8451 | // distance. |
| 8452 | const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2; |
| 8453 | __ mov(scratch2, Operand(scratch2, LSL, shift_distance)); |
| 8454 | // Put sign in zero flag. |
| 8455 | __ tst(scratch, Operand(HeapNumber::kSignMask)); |
| 8456 | // Get the second half of the double. For some exponents we don't |
| 8457 | // actually need this because the bits get shifted out again, but |
| 8458 | // it's probably slower to test than just to do it. |
| 8459 | __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset)); |
| 8460 | // Shift down 22 bits to get the last 10 bits. |
| 8461 | __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance)); |
| 8462 | // Move down according to the exponent. |
| 8463 | __ mov(dest, Operand(scratch, LSR, dest)); |
| 8464 | // Fix sign if sign bit was set. |
| 8465 | __ rsb(dest, dest, Operand(0), LeaveCC, ne); |
| 8466 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8467 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8468 | } |
| 8469 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8470 | // For bitwise ops where the inputs are not both Smis we here try to determine |
| 8471 | // whether both inputs are either Smis or at least heap numbers that can be |
| 8472 | // represented by a 32 bit signed value. We truncate towards zero as required |
| 8473 | // by the ES spec. If this is the case we do the bitwise op and see if the |
| 8474 | // result is a Smi. If so, great, otherwise we try to find a heap number to |
| 8475 | // write the answer into (either by allocating or by overwriting). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8476 | // On entry the operands are in lhs and rhs. On exit the answer is in r0. |
| 8477 | void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm, |
| 8478 | Register lhs, |
| 8479 | Register rhs) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8480 | Label slow, result_not_a_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8481 | Label rhs_is_smi, lhs_is_smi; |
| 8482 | Label done_checking_rhs, done_checking_lhs; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8483 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8484 | Register heap_number_map = r6; |
| 8485 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 8486 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8487 | __ tst(lhs, Operand(kSmiTagMask)); |
| 8488 | __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8489 | __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset)); |
| 8490 | __ cmp(r4, heap_number_map); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8491 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8492 | GetInt32(masm, lhs, r3, r5, r4, &slow); |
| 8493 | __ jmp(&done_checking_lhs); |
| 8494 | __ bind(&lhs_is_smi); |
| 8495 | __ mov(r3, Operand(lhs, ASR, 1)); |
| 8496 | __ bind(&done_checking_lhs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8497 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8498 | __ tst(rhs, Operand(kSmiTagMask)); |
| 8499 | __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8500 | __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset)); |
| 8501 | __ cmp(r4, heap_number_map); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8502 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8503 | GetInt32(masm, rhs, r2, r5, r4, &slow); |
| 8504 | __ jmp(&done_checking_rhs); |
| 8505 | __ bind(&rhs_is_smi); |
| 8506 | __ mov(r2, Operand(rhs, ASR, 1)); |
| 8507 | __ bind(&done_checking_rhs); |
| 8508 | |
| 8509 | ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8510 | |
| 8511 | // r0 and r1: Original operands (Smi or heap numbers). |
| 8512 | // r2 and r3: Signed int32 operands. |
| 8513 | switch (op_) { |
| 8514 | case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break; |
| 8515 | case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break; |
| 8516 | case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break; |
| 8517 | case Token::SAR: |
| 8518 | // Use only the 5 least significant bits of the shift count. |
| 8519 | __ and_(r2, r2, Operand(0x1f)); |
| 8520 | __ mov(r2, Operand(r3, ASR, r2)); |
| 8521 | break; |
| 8522 | case Token::SHR: |
| 8523 | // Use only the 5 least significant bits of the shift count. |
| 8524 | __ and_(r2, r2, Operand(0x1f)); |
| 8525 | __ mov(r2, Operand(r3, LSR, r2), SetCC); |
| 8526 | // SHR is special because it is required to produce a positive answer. |
| 8527 | // The code below for writing into heap numbers isn't capable of writing |
| 8528 | // the register as an unsigned int so we go to slow case if we hit this |
| 8529 | // case. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 8530 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8531 | __ b(mi, &result_not_a_smi); |
| 8532 | } else { |
| 8533 | __ b(mi, &slow); |
| 8534 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8535 | break; |
| 8536 | case Token::SHL: |
| 8537 | // Use only the 5 least significant bits of the shift count. |
| 8538 | __ and_(r2, r2, Operand(0x1f)); |
| 8539 | __ mov(r2, Operand(r3, LSL, r2)); |
| 8540 | break; |
| 8541 | default: UNREACHABLE(); |
| 8542 | } |
| 8543 | // check that the *signed* result fits in a smi |
| 8544 | __ add(r3, r2, Operand(0x40000000), SetCC); |
| 8545 | __ b(mi, &result_not_a_smi); |
| 8546 | __ mov(r0, Operand(r2, LSL, kSmiTagSize)); |
| 8547 | __ Ret(); |
| 8548 | |
| 8549 | Label have_to_allocate, got_a_heap_number; |
| 8550 | __ bind(&result_not_a_smi); |
| 8551 | switch (mode_) { |
| 8552 | case OVERWRITE_RIGHT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8553 | __ tst(rhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8554 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8555 | __ mov(r5, Operand(rhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8556 | break; |
| 8557 | } |
| 8558 | case OVERWRITE_LEFT: { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8559 | __ tst(lhs, Operand(kSmiTagMask)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8560 | __ b(eq, &have_to_allocate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8561 | __ mov(r5, Operand(lhs)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8562 | break; |
| 8563 | } |
| 8564 | case NO_OVERWRITE: { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8565 | // Get a new heap number in r5. r4 and r7 are scratch. |
| 8566 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8567 | } |
| 8568 | default: break; |
| 8569 | } |
| 8570 | __ bind(&got_a_heap_number); |
| 8571 | // r2: Answer as signed int32. |
| 8572 | // r5: Heap number to write answer into. |
| 8573 | |
| 8574 | // Nothing can go wrong now, so move the heap number to r0, which is the |
| 8575 | // result. |
| 8576 | __ mov(r0, Operand(r5)); |
| 8577 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 8578 | if (CpuFeatures::IsSupported(VFP3)) { |
| 8579 | // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. |
| 8580 | CpuFeatures::Scope scope(VFP3); |
| 8581 | __ vmov(s0, r2); |
| 8582 | if (op_ == Token::SHR) { |
| 8583 | __ vcvt_f64_u32(d0, s0); |
| 8584 | } else { |
| 8585 | __ vcvt_f64_s32(d0, s0); |
| 8586 | } |
| 8587 | __ sub(r3, r0, Operand(kHeapObjectTag)); |
| 8588 | __ vstr(d0, r3, HeapNumber::kValueOffset); |
| 8589 | __ Ret(); |
| 8590 | } else { |
| 8591 | // Tail call that writes the int32 in r2 to the heap number in r0, using |
| 8592 | // r3 as scratch. r0 is preserved and returned. |
| 8593 | WriteInt32ToHeapNumberStub stub(r2, r0, r3); |
| 8594 | __ TailCallStub(&stub); |
| 8595 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8596 | |
| 8597 | if (mode_ != NO_OVERWRITE) { |
| 8598 | __ bind(&have_to_allocate); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8599 | // Get a new heap number in r5. r4 and r7 are scratch. |
| 8600 | __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8601 | __ jmp(&got_a_heap_number); |
| 8602 | } |
| 8603 | |
| 8604 | // If all else failed then we go to the runtime system. |
| 8605 | __ bind(&slow); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 8606 | __ Push(lhs, rhs); // Restore stack. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8607 | switch (op_) { |
| 8608 | case Token::BIT_OR: |
| 8609 | __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS); |
| 8610 | break; |
| 8611 | case Token::BIT_AND: |
| 8612 | __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS); |
| 8613 | break; |
| 8614 | case Token::BIT_XOR: |
| 8615 | __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS); |
| 8616 | break; |
| 8617 | case Token::SAR: |
| 8618 | __ InvokeBuiltin(Builtins::SAR, JUMP_JS); |
| 8619 | break; |
| 8620 | case Token::SHR: |
| 8621 | __ InvokeBuiltin(Builtins::SHR, JUMP_JS); |
| 8622 | break; |
| 8623 | case Token::SHL: |
| 8624 | __ InvokeBuiltin(Builtins::SHL, JUMP_JS); |
| 8625 | break; |
| 8626 | default: |
| 8627 | UNREACHABLE(); |
| 8628 | } |
| 8629 | } |
| 8630 | |
| 8631 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 8632 | // Can we multiply by x with max two shifts and an add. |
| 8633 | // This answers yes to all integers from 2 to 10. |
| 8634 | static bool IsEasyToMultiplyBy(int x) { |
| 8635 | if (x < 2) return false; // Avoid special cases. |
| 8636 | if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows. |
| 8637 | if (IsPowerOf2(x)) return true; // Simple shift. |
| 8638 | if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift. |
| 8639 | if (IsPowerOf2(x + 1)) return true; // Patterns like 11111. |
| 8640 | return false; |
| 8641 | } |
| 8642 | |
| 8643 | |
| 8644 | // Can multiply by anything that IsEasyToMultiplyBy returns true for. |
| 8645 | // Source and destination may be the same register. This routine does |
| 8646 | // not set carry and overflow the way a mul instruction would. |
| 8647 | static void MultiplyByKnownInt(MacroAssembler* masm, |
| 8648 | Register source, |
| 8649 | Register destination, |
| 8650 | int known_int) { |
| 8651 | if (IsPowerOf2(known_int)) { |
| 8652 | __ mov(destination, Operand(source, LSL, BitPosition(known_int))); |
| 8653 | } else if (PopCountLessThanEqual2(known_int)) { |
| 8654 | int first_bit = BitPosition(known_int); |
| 8655 | int second_bit = BitPosition(known_int ^ (1 << first_bit)); |
| 8656 | __ add(destination, source, Operand(source, LSL, second_bit - first_bit)); |
| 8657 | if (first_bit != 0) { |
| 8658 | __ mov(destination, Operand(destination, LSL, first_bit)); |
| 8659 | } |
| 8660 | } else { |
| 8661 | ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111. |
| 8662 | int the_bit = BitPosition(known_int + 1); |
| 8663 | __ rsb(destination, source, Operand(source, LSL, the_bit)); |
| 8664 | } |
| 8665 | } |
| 8666 | |
| 8667 | |
| 8668 | // This function (as opposed to MultiplyByKnownInt) takes the known int in a |
| 8669 | // a register for the cases where it doesn't know a good trick, and may deliver |
| 8670 | // a result that needs shifting. |
| 8671 | static void MultiplyByKnownInt2( |
| 8672 | MacroAssembler* masm, |
| 8673 | Register result, |
| 8674 | Register source, |
| 8675 | Register known_int_register, // Smi tagged. |
| 8676 | int known_int, |
| 8677 | int* required_shift) { // Including Smi tag shift |
| 8678 | switch (known_int) { |
| 8679 | case 3: |
| 8680 | __ add(result, source, Operand(source, LSL, 1)); |
| 8681 | *required_shift = 1; |
| 8682 | break; |
| 8683 | case 5: |
| 8684 | __ add(result, source, Operand(source, LSL, 2)); |
| 8685 | *required_shift = 1; |
| 8686 | break; |
| 8687 | case 6: |
| 8688 | __ add(result, source, Operand(source, LSL, 1)); |
| 8689 | *required_shift = 2; |
| 8690 | break; |
| 8691 | case 7: |
| 8692 | __ rsb(result, source, Operand(source, LSL, 3)); |
| 8693 | *required_shift = 1; |
| 8694 | break; |
| 8695 | case 9: |
| 8696 | __ add(result, source, Operand(source, LSL, 3)); |
| 8697 | *required_shift = 1; |
| 8698 | break; |
| 8699 | case 10: |
| 8700 | __ add(result, source, Operand(source, LSL, 2)); |
| 8701 | *required_shift = 2; |
| 8702 | break; |
| 8703 | default: |
| 8704 | ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient. |
| 8705 | __ mul(result, source, known_int_register); |
| 8706 | *required_shift = 0; |
| 8707 | } |
| 8708 | } |
| 8709 | |
| 8710 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 8711 | // This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3 |
| 8712 | // trick. See http://en.wikipedia.org/wiki/Divisibility_rule |
| 8713 | // Takes the sum of the digits base (mask + 1) repeatedly until we have a |
| 8714 | // number from 0 to mask. On exit the 'eq' condition flags are set if the |
| 8715 | // answer is exactly the mask. |
| 8716 | void IntegerModStub::DigitSum(MacroAssembler* masm, |
| 8717 | Register lhs, |
| 8718 | int mask, |
| 8719 | int shift, |
| 8720 | Label* entry) { |
| 8721 | ASSERT(mask > 0); |
| 8722 | ASSERT(mask <= 0xff); // This ensures we don't need ip to use it. |
| 8723 | Label loop; |
| 8724 | __ bind(&loop); |
| 8725 | __ and_(ip, lhs, Operand(mask)); |
| 8726 | __ add(lhs, ip, Operand(lhs, LSR, shift)); |
| 8727 | __ bind(entry); |
| 8728 | __ cmp(lhs, Operand(mask)); |
| 8729 | __ b(gt, &loop); |
| 8730 | } |
| 8731 | |
| 8732 | |
| 8733 | void IntegerModStub::DigitSum(MacroAssembler* masm, |
| 8734 | Register lhs, |
| 8735 | Register scratch, |
| 8736 | int mask, |
| 8737 | int shift1, |
| 8738 | int shift2, |
| 8739 | Label* entry) { |
| 8740 | ASSERT(mask > 0); |
| 8741 | ASSERT(mask <= 0xff); // This ensures we don't need ip to use it. |
| 8742 | Label loop; |
| 8743 | __ bind(&loop); |
| 8744 | __ bic(scratch, lhs, Operand(mask)); |
| 8745 | __ and_(ip, lhs, Operand(mask)); |
| 8746 | __ add(lhs, ip, Operand(lhs, LSR, shift1)); |
| 8747 | __ add(lhs, lhs, Operand(scratch, LSR, shift2)); |
| 8748 | __ bind(entry); |
| 8749 | __ cmp(lhs, Operand(mask)); |
| 8750 | __ b(gt, &loop); |
| 8751 | } |
| 8752 | |
| 8753 | |
| 8754 | // Splits the number into two halves (bottom half has shift bits). The top |
| 8755 | // half is subtracted from the bottom half. If the result is negative then |
| 8756 | // rhs is added. |
| 8757 | void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm, |
| 8758 | Register lhs, |
| 8759 | int shift, |
| 8760 | int rhs) { |
| 8761 | int mask = (1 << shift) - 1; |
| 8762 | __ and_(ip, lhs, Operand(mask)); |
| 8763 | __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC); |
| 8764 | __ add(lhs, lhs, Operand(rhs), LeaveCC, mi); |
| 8765 | } |
| 8766 | |
| 8767 | |
| 8768 | void IntegerModStub::ModReduce(MacroAssembler* masm, |
| 8769 | Register lhs, |
| 8770 | int max, |
| 8771 | int denominator) { |
| 8772 | int limit = denominator; |
| 8773 | while (limit * 2 <= max) limit *= 2; |
| 8774 | while (limit >= denominator) { |
| 8775 | __ cmp(lhs, Operand(limit)); |
| 8776 | __ sub(lhs, lhs, Operand(limit), LeaveCC, ge); |
| 8777 | limit >>= 1; |
| 8778 | } |
| 8779 | } |
| 8780 | |
| 8781 | |
| 8782 | void IntegerModStub::ModAnswer(MacroAssembler* masm, |
| 8783 | Register result, |
| 8784 | Register shift_distance, |
| 8785 | Register mask_bits, |
| 8786 | Register sum_of_digits) { |
| 8787 | __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance)); |
| 8788 | __ Ret(); |
| 8789 | } |
| 8790 | |
| 8791 | |
| 8792 | // See comment for class. |
| 8793 | void IntegerModStub::Generate(MacroAssembler* masm) { |
| 8794 | __ mov(lhs_, Operand(lhs_, LSR, shift_distance_)); |
| 8795 | __ bic(odd_number_, odd_number_, Operand(1)); |
| 8796 | __ mov(odd_number_, Operand(odd_number_, LSL, 1)); |
| 8797 | // We now have (odd_number_ - 1) * 2 in the register. |
| 8798 | // Build a switch out of branches instead of data because it avoids |
| 8799 | // having to teach the assembler about intra-code-object pointers |
| 8800 | // that are not in relative branch instructions. |
| 8801 | Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19; |
| 8802 | Label mod21, mod23, mod25; |
| 8803 | { Assembler::BlockConstPoolScope block_const_pool(masm); |
| 8804 | __ add(pc, pc, Operand(odd_number_)); |
| 8805 | // When you read pc it is always 8 ahead, but when you write it you always |
| 8806 | // write the actual value. So we put in two nops to take up the slack. |
| 8807 | __ nop(); |
| 8808 | __ nop(); |
| 8809 | __ b(&mod3); |
| 8810 | __ b(&mod5); |
| 8811 | __ b(&mod7); |
| 8812 | __ b(&mod9); |
| 8813 | __ b(&mod11); |
| 8814 | __ b(&mod13); |
| 8815 | __ b(&mod15); |
| 8816 | __ b(&mod17); |
| 8817 | __ b(&mod19); |
| 8818 | __ b(&mod21); |
| 8819 | __ b(&mod23); |
| 8820 | __ b(&mod25); |
| 8821 | } |
| 8822 | |
| 8823 | // For each denominator we find a multiple that is almost only ones |
| 8824 | // when expressed in binary. Then we do the sum-of-digits trick for |
| 8825 | // that number. If the multiple is not 1 then we have to do a little |
| 8826 | // more work afterwards to get the answer into the 0-denominator-1 |
| 8827 | // range. |
| 8828 | DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11. |
| 8829 | __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq); |
| 8830 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8831 | |
| 8832 | DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111. |
| 8833 | ModGetInRangeBySubtraction(masm, lhs_, 2, 5); |
| 8834 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8835 | |
| 8836 | DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111. |
| 8837 | __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq); |
| 8838 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8839 | |
| 8840 | DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111. |
| 8841 | ModGetInRangeBySubtraction(masm, lhs_, 3, 9); |
| 8842 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8843 | |
| 8844 | DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111. |
| 8845 | ModReduce(masm, lhs_, 0x3f, 11); |
| 8846 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8847 | |
| 8848 | DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111. |
| 8849 | ModReduce(masm, lhs_, 0xff, 13); |
| 8850 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8851 | |
| 8852 | DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111. |
| 8853 | __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq); |
| 8854 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8855 | |
| 8856 | DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111. |
| 8857 | ModGetInRangeBySubtraction(masm, lhs_, 4, 17); |
| 8858 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8859 | |
| 8860 | DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111. |
| 8861 | ModReduce(masm, lhs_, 0xff, 19); |
| 8862 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8863 | |
| 8864 | DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111. |
| 8865 | ModReduce(masm, lhs_, 0x3f, 21); |
| 8866 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8867 | |
| 8868 | DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101. |
| 8869 | ModReduce(masm, lhs_, 0xff, 23); |
| 8870 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8871 | |
| 8872 | DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101. |
| 8873 | ModReduce(masm, lhs_, 0x7f, 25); |
| 8874 | ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_); |
| 8875 | } |
| 8876 | |
| 8877 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 8878 | const char* GenericBinaryOpStub::GetName() { |
| 8879 | if (name_ != NULL) return name_; |
| 8880 | const int len = 100; |
| 8881 | name_ = Bootstrapper::AllocateAutoDeletedArray(len); |
| 8882 | if (name_ == NULL) return "OOM"; |
| 8883 | const char* op_name = Token::Name(op_); |
| 8884 | const char* overwrite_name; |
| 8885 | switch (mode_) { |
| 8886 | case NO_OVERWRITE: overwrite_name = "Alloc"; break; |
| 8887 | case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; |
| 8888 | case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; |
| 8889 | default: overwrite_name = "UnknownOverwrite"; break; |
| 8890 | } |
| 8891 | |
| 8892 | OS::SNPrintF(Vector<char>(name_, len), |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8893 | "GenericBinaryOpStub_%s_%s%s_%s", |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 8894 | op_name, |
| 8895 | overwrite_name, |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 8896 | specialized_on_rhs_ ? "_ConstantRhs" : "", |
| 8897 | BinaryOpIC::GetName(runtime_operands_type_)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 8898 | return name_; |
| 8899 | } |
| 8900 | |
| 8901 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8902 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8903 | void GenericBinaryOpStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8904 | // lhs_ : x |
| 8905 | // rhs_ : y |
| 8906 | // r0 : result |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8907 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8908 | Register result = r0; |
| 8909 | Register lhs = lhs_; |
| 8910 | Register rhs = rhs_; |
| 8911 | |
| 8912 | // This code can't cope with other register allocations yet. |
| 8913 | ASSERT(result.is(r0) && |
| 8914 | ((lhs.is(r0) && rhs.is(r1)) || |
| 8915 | (lhs.is(r1) && rhs.is(r0)))); |
| 8916 | |
| 8917 | Register smi_test_reg = VirtualFrame::scratch0(); |
| 8918 | Register scratch = VirtualFrame::scratch1(); |
| 8919 | |
| 8920 | // All ops need to know whether we are dealing with two Smis. Set up |
| 8921 | // smi_test_reg to tell us that. |
| 8922 | if (ShouldGenerateSmiCode()) { |
| 8923 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 8924 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8925 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8926 | switch (op_) { |
| 8927 | case Token::ADD: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8928 | Label not_smi; |
| 8929 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8930 | if (ShouldGenerateSmiCode()) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8931 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8932 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8933 | __ b(ne, ¬_smi); |
| 8934 | __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically. |
| 8935 | // Return if no overflow. |
| 8936 | __ Ret(vc); |
| 8937 | __ sub(r0, r0, Operand(r1)); // Revert optimistic add. |
| 8938 | } |
| 8939 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::ADD); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8940 | break; |
| 8941 | } |
| 8942 | |
| 8943 | case Token::SUB: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8944 | Label not_smi; |
| 8945 | // Fast path. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8946 | if (ShouldGenerateSmiCode()) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8947 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8948 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8949 | __ b(ne, ¬_smi); |
| 8950 | if (lhs.is(r1)) { |
| 8951 | __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically. |
| 8952 | // Return if no overflow. |
| 8953 | __ Ret(vc); |
| 8954 | __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract. |
| 8955 | } else { |
| 8956 | __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically. |
| 8957 | // Return if no overflow. |
| 8958 | __ Ret(vc); |
| 8959 | __ add(r0, r0, Operand(r1)); // Revert optimistic subtract. |
| 8960 | } |
| 8961 | } |
| 8962 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::SUB); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8963 | break; |
| 8964 | } |
| 8965 | |
| 8966 | case Token::MUL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8967 | Label not_smi, slow; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8968 | if (ShouldGenerateSmiCode()) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8969 | STATIC_ASSERT(kSmiTag == 0); // adjust code below |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 8970 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
| 8971 | Register scratch2 = smi_test_reg; |
| 8972 | smi_test_reg = no_reg; |
| 8973 | __ b(ne, ¬_smi); |
| 8974 | // Remove tag from one operand (but keep sign), so that result is Smi. |
| 8975 | __ mov(ip, Operand(rhs, ASR, kSmiTagSize)); |
| 8976 | // Do multiplication |
| 8977 | // scratch = lower 32 bits of ip * lhs. |
| 8978 | __ smull(scratch, scratch2, lhs, ip); |
| 8979 | // Go slow on overflows (overflow bit is not set). |
| 8980 | __ mov(ip, Operand(scratch, ASR, 31)); |
| 8981 | // No overflow if higher 33 bits are identical. |
| 8982 | __ cmp(ip, Operand(scratch2)); |
| 8983 | __ b(ne, &slow); |
| 8984 | // Go slow on zero result to handle -0. |
| 8985 | __ tst(scratch, Operand(scratch)); |
| 8986 | __ mov(result, Operand(scratch), LeaveCC, ne); |
| 8987 | __ Ret(ne); |
| 8988 | // We need -0 if we were multiplying a negative number with 0 to get 0. |
| 8989 | // We know one of them was zero. |
| 8990 | __ add(scratch2, rhs, Operand(lhs), SetCC); |
| 8991 | __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl); |
| 8992 | __ Ret(pl); // Return Smi 0 if the non-zero one was positive. |
| 8993 | // Slow case. We fall through here if we multiplied a negative number |
| 8994 | // with 0, because that would mean we should produce -0. |
| 8995 | __ bind(&slow); |
| 8996 | } |
| 8997 | HandleBinaryOpSlowCases(masm, ¬_smi, lhs, rhs, Builtins::MUL); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 8998 | break; |
| 8999 | } |
| 9000 | |
| 9001 | case Token::DIV: |
| 9002 | case Token::MOD: { |
| 9003 | Label not_smi; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9004 | if (ShouldGenerateSmiCode() && specialized_on_rhs_) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9005 | Label lhs_is_unsuitable; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9006 | __ BranchOnNotSmi(lhs, ¬_smi); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9007 | if (IsPowerOf2(constant_rhs_)) { |
| 9008 | if (op_ == Token::MOD) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9009 | __ and_(rhs, |
| 9010 | lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9011 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)), |
| 9012 | SetCC); |
| 9013 | // We now have the answer, but if the input was negative we also |
| 9014 | // have the sign bit. Our work is done if the result is |
| 9015 | // positive or zero: |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9016 | if (!rhs.is(r0)) { |
| 9017 | __ mov(r0, rhs, LeaveCC, pl); |
| 9018 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9019 | __ Ret(pl); |
| 9020 | // A mod of a negative left hand side must return a negative number. |
| 9021 | // Unfortunately if the answer is 0 then we must return -0. And we |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9022 | // already optimistically trashed rhs so we may need to restore it. |
| 9023 | __ eor(rhs, rhs, Operand(0x80000000u), SetCC); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9024 | // Next two instructions are conditional on the answer being -0. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9025 | __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9026 | __ b(eq, &lhs_is_unsuitable); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9027 | // We need to subtract the dividend. Eg. -3 % 4 == -3. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9028 | __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9029 | } else { |
| 9030 | ASSERT(op_ == Token::DIV); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9031 | __ tst(lhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9032 | Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1))); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9033 | __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9034 | int shift = 0; |
| 9035 | int d = constant_rhs_; |
| 9036 | while ((d & 1) == 0) { |
| 9037 | d >>= 1; |
| 9038 | shift++; |
| 9039 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9040 | __ mov(r0, Operand(lhs, LSR, shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9041 | __ bic(r0, r0, Operand(kSmiTagMask)); |
| 9042 | } |
| 9043 | } else { |
| 9044 | // Not a power of 2. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9045 | __ tst(lhs, Operand(0x80000000u)); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9046 | __ b(ne, &lhs_is_unsuitable); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9047 | // Find a fixed point reciprocal of the divisor so we can divide by |
| 9048 | // multiplying. |
| 9049 | double divisor = 1.0 / constant_rhs_; |
| 9050 | int shift = 32; |
| 9051 | double scale = 4294967296.0; // 1 << 32. |
| 9052 | uint32_t mul; |
| 9053 | // Maximise the precision of the fixed point reciprocal. |
| 9054 | while (true) { |
| 9055 | mul = static_cast<uint32_t>(scale * divisor); |
| 9056 | if (mul >= 0x7fffffff) break; |
| 9057 | scale *= 2.0; |
| 9058 | shift++; |
| 9059 | } |
| 9060 | mul++; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9061 | Register scratch2 = smi_test_reg; |
| 9062 | smi_test_reg = no_reg; |
| 9063 | __ mov(scratch2, Operand(mul)); |
| 9064 | __ umull(scratch, scratch2, scratch2, lhs); |
| 9065 | __ mov(scratch2, Operand(scratch2, LSR, shift - 31)); |
| 9066 | // scratch2 is lhs / rhs. scratch2 is not Smi tagged. |
| 9067 | // rhs is still the known rhs. rhs is Smi tagged. |
| 9068 | // lhs is still the unkown lhs. lhs is Smi tagged. |
| 9069 | int required_scratch_shift = 0; // Including the Smi tag shift of 1. |
| 9070 | // scratch = scratch2 * rhs. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9071 | MultiplyByKnownInt2(masm, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9072 | scratch, |
| 9073 | scratch2, |
| 9074 | rhs, |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9075 | constant_rhs_, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9076 | &required_scratch_shift); |
| 9077 | // scratch << required_scratch_shift is now the Smi tagged rhs * |
| 9078 | // (lhs / rhs) where / indicates integer division. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9079 | if (op_ == Token::DIV) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9080 | __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift)); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9081 | __ b(ne, &lhs_is_unsuitable); // There was a remainder. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9082 | __ mov(result, Operand(scratch2, LSL, kSmiTagSize)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9083 | } else { |
| 9084 | ASSERT(op_ == Token::MOD); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9085 | __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift)); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9086 | } |
| 9087 | } |
| 9088 | __ Ret(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9089 | __ bind(&lhs_is_unsuitable); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9090 | } else if (op_ == Token::MOD && |
| 9091 | runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS && |
| 9092 | runtime_operands_type_ != BinaryOpIC::STRINGS) { |
| 9093 | // Do generate a bit of smi code for modulus even though the default for |
| 9094 | // modulus is not to do it, but as the ARM processor has no coprocessor |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9095 | // support for modulus checking for smis makes sense. We can handle |
| 9096 | // 1 to 25 times any power of 2. This covers over half the numbers from |
| 9097 | // 1 to 100 including all of the first 25. (Actually the constants < 10 |
| 9098 | // are handled above by reciprocal multiplication. We only get here for |
| 9099 | // those cases if the right hand side is not a constant or for cases |
| 9100 | // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod |
| 9101 | // stub.) |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9102 | Label slow; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9103 | Label not_power_of_2; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9104 | ASSERT(!ShouldGenerateSmiCode()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9105 | STATIC_ASSERT(kSmiTag == 0); // Adjust code below. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9106 | // Check for two positive smis. |
| 9107 | __ orr(smi_test_reg, lhs, Operand(rhs)); |
| 9108 | __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask)); |
| 9109 | __ b(ne, &slow); |
| 9110 | // Check that rhs is a power of two and not zero. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9111 | Register mask_bits = r3; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9112 | __ sub(scratch, rhs, Operand(1), SetCC); |
| 9113 | __ b(mi, &slow); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9114 | __ and_(mask_bits, rhs, Operand(scratch), SetCC); |
| 9115 | __ b(ne, ¬_power_of_2); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9116 | // Calculate power of two modulus. |
| 9117 | __ and_(result, lhs, Operand(scratch)); |
| 9118 | __ Ret(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 9119 | |
| 9120 | __ bind(¬_power_of_2); |
| 9121 | __ eor(scratch, scratch, Operand(mask_bits)); |
| 9122 | // At least two bits are set in the modulus. The high one(s) are in |
| 9123 | // mask_bits and the low one is scratch + 1. |
| 9124 | __ and_(mask_bits, scratch, Operand(lhs)); |
| 9125 | Register shift_distance = scratch; |
| 9126 | scratch = no_reg; |
| 9127 | |
| 9128 | // The rhs consists of a power of 2 multiplied by some odd number. |
| 9129 | // The power-of-2 part we handle by putting the corresponding bits |
| 9130 | // from the lhs in the mask_bits register, and the power in the |
| 9131 | // shift_distance register. Shift distance is never 0 due to Smi |
| 9132 | // tagging. |
| 9133 | __ CountLeadingZeros(r4, shift_distance, shift_distance); |
| 9134 | __ rsb(shift_distance, r4, Operand(32)); |
| 9135 | |
| 9136 | // Now we need to find out what the odd number is. The last bit is |
| 9137 | // always 1. |
| 9138 | Register odd_number = r4; |
| 9139 | __ mov(odd_number, Operand(rhs, LSR, shift_distance)); |
| 9140 | __ cmp(odd_number, Operand(25)); |
| 9141 | __ b(gt, &slow); |
| 9142 | |
| 9143 | IntegerModStub stub( |
| 9144 | result, shift_distance, odd_number, mask_bits, lhs, r5); |
| 9145 | __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call. |
| 9146 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9147 | __ bind(&slow); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9148 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9149 | HandleBinaryOpSlowCases( |
| 9150 | masm, |
| 9151 | ¬_smi, |
| 9152 | lhs, |
| 9153 | rhs, |
| 9154 | op_ == Token::MOD ? Builtins::MOD : Builtins::DIV); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9155 | break; |
| 9156 | } |
| 9157 | |
| 9158 | case Token::BIT_OR: |
| 9159 | case Token::BIT_AND: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9160 | case Token::BIT_XOR: |
| 9161 | case Token::SAR: |
| 9162 | case Token::SHR: |
| 9163 | case Token::SHL: { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9164 | Label slow; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9165 | STATIC_ASSERT(kSmiTag == 0); // adjust code below |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9166 | __ tst(smi_test_reg, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9167 | __ b(ne, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9168 | Register scratch2 = smi_test_reg; |
| 9169 | smi_test_reg = no_reg; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9170 | switch (op_) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9171 | case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break; |
| 9172 | case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break; |
| 9173 | case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9174 | case Token::SAR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9175 | // Remove tags from right operand. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9176 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9177 | __ mov(result, Operand(lhs, ASR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9178 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9179 | __ bic(result, result, Operand(kSmiTagMask)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9180 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9181 | case Token::SHR: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9182 | // Remove tags from operands. We can't do this on a 31 bit number |
| 9183 | // because then the 0s get shifted into bit 30 instead of bit 31. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9184 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 9185 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9186 | __ mov(scratch, Operand(scratch, LSR, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9187 | // Unsigned shift is not allowed to produce a negative number, so |
| 9188 | // check the sign bit and the sign bit after Smi tagging. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9189 | __ tst(scratch, Operand(0xc0000000)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9190 | __ b(ne, &slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9191 | // Smi tag result. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9192 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9193 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9194 | case Token::SHL: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9195 | // Remove tags from operands. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9196 | __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x |
| 9197 | __ GetLeastBitsFromSmi(scratch2, rhs, 5); |
| 9198 | __ mov(scratch, Operand(scratch, LSL, scratch2)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9199 | // Check that the signed result fits in a Smi. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9200 | __ add(scratch2, scratch, Operand(0x40000000), SetCC); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9201 | __ b(mi, &slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9202 | __ mov(result, Operand(scratch, LSL, kSmiTagSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9203 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9204 | default: UNREACHABLE(); |
| 9205 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9206 | __ Ret(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9207 | __ bind(&slow); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9208 | HandleNonSmiBitwiseOp(masm, lhs, rhs); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9209 | break; |
| 9210 | } |
| 9211 | |
| 9212 | default: UNREACHABLE(); |
| 9213 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9214 | // This code should be unreachable. |
| 9215 | __ stop("Unreachable"); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9216 | |
| 9217 | // Generate an unreachable reference to the DEFAULT stub so that it can be |
| 9218 | // found at the end of this stub when clearing ICs at GC. |
| 9219 | // TODO(kaznacheev): Check performance impact and get rid of this. |
| 9220 | if (runtime_operands_type_ != BinaryOpIC::DEFAULT) { |
| 9221 | GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT); |
| 9222 | __ CallStub(&uninit); |
| 9223 | } |
| 9224 | } |
| 9225 | |
| 9226 | |
| 9227 | void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) { |
| 9228 | Label get_result; |
| 9229 | |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 9230 | __ Push(r1, r0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9231 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9232 | __ mov(r2, Operand(Smi::FromInt(MinorKey()))); |
| 9233 | __ mov(r1, Operand(Smi::FromInt(op_))); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9234 | __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_))); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9235 | __ Push(r2, r1, r0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9236 | |
| 9237 | __ TailCallExternalReference( |
| 9238 | ExternalReference(IC_Utility(IC::kBinaryOp_Patch)), |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9239 | 5, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9240 | 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9241 | } |
| 9242 | |
| 9243 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9244 | Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 9245 | GenericBinaryOpStub stub(key, type_info); |
| 9246 | return stub.GetCode(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9247 | } |
| 9248 | |
| 9249 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9250 | void TranscendentalCacheStub::Generate(MacroAssembler* masm) { |
| 9251 | // Argument is a number and is on stack and in r0. |
| 9252 | Label runtime_call; |
| 9253 | Label input_not_smi; |
| 9254 | Label loaded; |
| 9255 | |
| 9256 | if (CpuFeatures::IsSupported(VFP3)) { |
| 9257 | // Load argument and check if it is a smi. |
| 9258 | __ BranchOnNotSmi(r0, &input_not_smi); |
| 9259 | |
| 9260 | CpuFeatures::Scope scope(VFP3); |
| 9261 | // Input is a smi. Convert to double and load the low and high words |
| 9262 | // of the double into r2, r3. |
| 9263 | __ IntegerToDoubleConversionWithVFP3(r0, r3, r2); |
| 9264 | __ b(&loaded); |
| 9265 | |
| 9266 | __ bind(&input_not_smi); |
| 9267 | // Check if input is a HeapNumber. |
| 9268 | __ CheckMap(r0, |
| 9269 | r1, |
| 9270 | Heap::kHeapNumberMapRootIndex, |
| 9271 | &runtime_call, |
| 9272 | true); |
| 9273 | // Input is a HeapNumber. Load it to a double register and store the |
| 9274 | // low and high words into r2, r3. |
| 9275 | __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset)); |
| 9276 | |
| 9277 | __ bind(&loaded); |
| 9278 | // r2 = low 32 bits of double value |
| 9279 | // r3 = high 32 bits of double value |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 9280 | // Compute hash (the shifts are arithmetic): |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9281 | // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1); |
| 9282 | __ eor(r1, r2, Operand(r3)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 9283 | __ eor(r1, r1, Operand(r1, ASR, 16)); |
| 9284 | __ eor(r1, r1, Operand(r1, ASR, 8)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9285 | ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 9286 | __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9287 | |
| 9288 | // r2 = low 32 bits of double value. |
| 9289 | // r3 = high 32 bits of double value. |
| 9290 | // r1 = TranscendentalCache::hash(double value). |
| 9291 | __ mov(r0, |
| 9292 | Operand(ExternalReference::transcendental_cache_array_address())); |
| 9293 | // r0 points to cache array. |
| 9294 | __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0]))); |
| 9295 | // r0 points to the cache for the type type_. |
| 9296 | // If NULL, the cache hasn't been initialized yet, so go through runtime. |
| 9297 | __ cmp(r0, Operand(0)); |
| 9298 | __ b(eq, &runtime_call); |
| 9299 | |
| 9300 | #ifdef DEBUG |
| 9301 | // Check that the layout of cache elements match expectations. |
| 9302 | { TranscendentalCache::Element test_elem[2]; |
| 9303 | char* elem_start = reinterpret_cast<char*>(&test_elem[0]); |
| 9304 | char* elem2_start = reinterpret_cast<char*>(&test_elem[1]); |
| 9305 | char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0])); |
| 9306 | char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1])); |
| 9307 | char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output)); |
| 9308 | CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer. |
| 9309 | CHECK_EQ(0, elem_in0 - elem_start); |
| 9310 | CHECK_EQ(kIntSize, elem_in1 - elem_start); |
| 9311 | CHECK_EQ(2 * kIntSize, elem_out - elem_start); |
| 9312 | } |
| 9313 | #endif |
| 9314 | |
| 9315 | // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12]. |
| 9316 | __ add(r1, r1, Operand(r1, LSL, 1)); |
| 9317 | __ add(r0, r0, Operand(r1, LSL, 2)); |
| 9318 | // Check if cache matches: Double value is stored in uint32_t[2] array. |
| 9319 | __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit()); |
| 9320 | __ cmp(r2, r4); |
| 9321 | __ b(ne, &runtime_call); |
| 9322 | __ cmp(r3, r5); |
| 9323 | __ b(ne, &runtime_call); |
| 9324 | // Cache hit. Load result, pop argument and return. |
| 9325 | __ mov(r0, Operand(r6)); |
| 9326 | __ pop(); |
| 9327 | __ Ret(); |
| 9328 | } |
| 9329 | |
| 9330 | __ bind(&runtime_call); |
| 9331 | __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1); |
| 9332 | } |
| 9333 | |
| 9334 | |
| 9335 | Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() { |
| 9336 | switch (type_) { |
| 9337 | // Add more cases when necessary. |
| 9338 | case TranscendentalCache::SIN: return Runtime::kMath_sin; |
| 9339 | case TranscendentalCache::COS: return Runtime::kMath_cos; |
| 9340 | default: |
| 9341 | UNIMPLEMENTED(); |
| 9342 | return Runtime::kAbort; |
| 9343 | } |
| 9344 | } |
| 9345 | |
| 9346 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9347 | void StackCheckStub::Generate(MacroAssembler* masm) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 9348 | // Do tail-call to runtime routine. Runtime routines expect at least one |
| 9349 | // argument, so give it a Smi. |
| 9350 | __ mov(r0, Operand(Smi::FromInt(0))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9351 | __ push(r0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9352 | __ TailCallRuntime(Runtime::kStackGuard, 1, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9353 | |
| 9354 | __ StubReturn(1); |
| 9355 | } |
| 9356 | |
| 9357 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9358 | void GenericUnaryOpStub::Generate(MacroAssembler* masm) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9359 | Label slow, done; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9360 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9361 | Register heap_number_map = r6; |
| 9362 | __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9363 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9364 | if (op_ == Token::SUB) { |
| 9365 | // Check whether the value is a smi. |
| 9366 | Label try_float; |
| 9367 | __ tst(r0, Operand(kSmiTagMask)); |
| 9368 | __ b(ne, &try_float); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9369 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9370 | // Go slow case if the value of the expression is zero |
| 9371 | // to make sure that we switch between 0 and -0. |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9372 | if (negative_zero_ == kStrictNegativeZero) { |
| 9373 | // If we have to check for zero, then we can check for the max negative |
| 9374 | // smi while we are at it. |
| 9375 | __ bic(ip, r0, Operand(0x80000000), SetCC); |
| 9376 | __ b(eq, &slow); |
| 9377 | __ rsb(r0, r0, Operand(0)); |
| 9378 | __ StubReturn(1); |
| 9379 | } else { |
| 9380 | // The value of the expression is a smi and 0 is OK for -0. Try |
| 9381 | // optimistic subtraction '0 - value'. |
| 9382 | __ rsb(r0, r0, Operand(0), SetCC); |
| 9383 | __ StubReturn(1, vc); |
| 9384 | // We don't have to reverse the optimistic neg since the only case |
| 9385 | // where we fall through is the minimum negative Smi, which is the case |
| 9386 | // where the neg leaves the register unchanged. |
| 9387 | __ jmp(&slow); // Go slow on max negative Smi. |
| 9388 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9389 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9390 | __ bind(&try_float); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9391 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 9392 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9393 | __ cmp(r1, heap_number_map); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9394 | __ b(ne, &slow); |
| 9395 | // r0 is a heap number. Get a new heap number in r1. |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9396 | if (overwrite_ == UNARY_OVERWRITE) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9397 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9398 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 9399 | __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9400 | } else { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9401 | __ AllocateHeapNumber(r1, r2, r3, r6, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9402 | __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset)); |
| 9403 | __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset)); |
| 9404 | __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset)); |
| 9405 | __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign. |
| 9406 | __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset)); |
| 9407 | __ mov(r0, Operand(r1)); |
| 9408 | } |
| 9409 | } else if (op_ == Token::BIT_NOT) { |
| 9410 | // Check if the operand is a heap number. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9411 | __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 9412 | __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex); |
| 9413 | __ cmp(r1, heap_number_map); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9414 | __ b(ne, &slow); |
| 9415 | |
| 9416 | // Convert the heap number is r0 to an untagged integer in r1. |
| 9417 | GetInt32(masm, r0, r1, r2, r3, &slow); |
| 9418 | |
| 9419 | // Do the bitwise operation (move negated) and check if the result |
| 9420 | // fits in a smi. |
| 9421 | Label try_float; |
| 9422 | __ mvn(r1, Operand(r1)); |
| 9423 | __ add(r2, r1, Operand(0x40000000), SetCC); |
| 9424 | __ b(mi, &try_float); |
| 9425 | __ mov(r0, Operand(r1, LSL, kSmiTagSize)); |
| 9426 | __ b(&done); |
| 9427 | |
| 9428 | __ bind(&try_float); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 9429 | if (!overwrite_ == UNARY_OVERWRITE) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9430 | // Allocate a fresh heap number, but don't overwrite r0 until |
| 9431 | // we're sure we can do it without going through the slow case |
| 9432 | // that needs the value in r0. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 9433 | __ AllocateHeapNumber(r2, r3, r4, r6, &slow); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9434 | __ mov(r0, Operand(r2)); |
| 9435 | } |
| 9436 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 9437 | if (CpuFeatures::IsSupported(VFP3)) { |
| 9438 | // Convert the int32 in r1 to the heap number in r0. r2 is corrupted. |
| 9439 | CpuFeatures::Scope scope(VFP3); |
| 9440 | __ vmov(s0, r1); |
| 9441 | __ vcvt_f64_s32(d0, s0); |
| 9442 | __ sub(r2, r0, Operand(kHeapObjectTag)); |
| 9443 | __ vstr(d0, r2, HeapNumber::kValueOffset); |
| 9444 | } else { |
| 9445 | // WriteInt32ToHeapNumberStub does not trigger GC, so we do not |
| 9446 | // have to set up a frame. |
| 9447 | WriteInt32ToHeapNumberStub stub(r1, r0, r2); |
| 9448 | __ push(lr); |
| 9449 | __ Call(stub.GetCode(), RelocInfo::CODE_TARGET); |
| 9450 | __ pop(lr); |
| 9451 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9452 | } else { |
| 9453 | UNIMPLEMENTED(); |
| 9454 | } |
| 9455 | |
| 9456 | __ bind(&done); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9457 | __ StubReturn(1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9458 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9459 | // Handle the slow case by jumping to the JavaScript builtin. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9460 | __ bind(&slow); |
| 9461 | __ push(r0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9462 | switch (op_) { |
| 9463 | case Token::SUB: |
| 9464 | __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS); |
| 9465 | break; |
| 9466 | case Token::BIT_NOT: |
| 9467 | __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS); |
| 9468 | break; |
| 9469 | default: |
| 9470 | UNREACHABLE(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9471 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 9472 | } |
| 9473 | |
| 9474 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9475 | void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9476 | // r0 holds the exception. |
| 9477 | |
| 9478 | // Adjust this code if not the case. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9479 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9480 | |
| 9481 | // Drop the sp to the top of the handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9482 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
| 9483 | __ ldr(sp, MemOperand(r3)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9484 | |
| 9485 | // Restore the next handler and frame pointer, discard handler state. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9486 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9487 | __ pop(r2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9488 | __ str(r2, MemOperand(r3)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9489 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9490 | __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state. |
| 9491 | |
| 9492 | // Before returning we restore the context from the frame pointer if |
| 9493 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 9494 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9495 | __ cmp(fp, Operand(0)); |
| 9496 | // Set cp to NULL if fp is NULL. |
| 9497 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 9498 | // Restore cp otherwise. |
| 9499 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9500 | #ifdef DEBUG |
| 9501 | if (FLAG_debug_code) { |
| 9502 | __ mov(lr, Operand(pc)); |
| 9503 | } |
| 9504 | #endif |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9505 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9506 | __ pop(pc); |
| 9507 | } |
| 9508 | |
| 9509 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9510 | void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm, |
| 9511 | UncatchableExceptionType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9512 | // Adjust this code if not the case. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9513 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9514 | |
| 9515 | // Drop sp to the top stack handler. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9516 | __ mov(r3, Operand(ExternalReference(Top::k_handler_address))); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9517 | __ ldr(sp, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9518 | |
| 9519 | // Unwind the handlers until the ENTRY handler is found. |
| 9520 | Label loop, done; |
| 9521 | __ bind(&loop); |
| 9522 | // Load the type of the current stack handler. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9523 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 9524 | __ ldr(r2, MemOperand(sp, kStateOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9525 | __ cmp(r2, Operand(StackHandler::ENTRY)); |
| 9526 | __ b(eq, &done); |
| 9527 | // Fetch the next handler in the list. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9528 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 9529 | __ ldr(sp, MemOperand(sp, kNextOffset)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9530 | __ jmp(&loop); |
| 9531 | __ bind(&done); |
| 9532 | |
| 9533 | // Set the top handler address to next handler past the current ENTRY handler. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9534 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9535 | __ pop(r2); |
| 9536 | __ str(r2, MemOperand(r3)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9537 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9538 | if (type == OUT_OF_MEMORY) { |
| 9539 | // Set external caught exception to false. |
| 9540 | ExternalReference external_caught(Top::k_external_caught_exception_address); |
| 9541 | __ mov(r0, Operand(false)); |
| 9542 | __ mov(r2, Operand(external_caught)); |
| 9543 | __ str(r0, MemOperand(r2)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9544 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9545 | // Set pending exception and r0 to out of memory exception. |
| 9546 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 9547 | __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 9548 | __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 9549 | __ str(r0, MemOperand(r2)); |
| 9550 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9551 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9552 | // Stack layout at this point. See also StackHandlerConstants. |
| 9553 | // sp -> state (ENTRY) |
| 9554 | // fp |
| 9555 | // lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9556 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9557 | // Discard handler state (r2 is not used) and restore frame pointer. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9558 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9559 | __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state. |
| 9560 | // Before returning we restore the context from the frame pointer if |
| 9561 | // not NULL. The frame pointer is NULL in the exception handler of a |
| 9562 | // JS entry frame. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9563 | __ cmp(fp, Operand(0)); |
| 9564 | // Set cp to NULL if fp is NULL. |
| 9565 | __ mov(cp, Operand(0), LeaveCC, eq); |
| 9566 | // Restore cp otherwise. |
| 9567 | __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9568 | #ifdef DEBUG |
| 9569 | if (FLAG_debug_code) { |
| 9570 | __ mov(lr, Operand(pc)); |
| 9571 | } |
| 9572 | #endif |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9573 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9574 | __ pop(pc); |
| 9575 | } |
| 9576 | |
| 9577 | |
| 9578 | void CEntryStub::GenerateCore(MacroAssembler* masm, |
| 9579 | Label* throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9580 | Label* throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9581 | Label* throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9582 | bool do_gc, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9583 | bool always_allocate, |
| 9584 | int frame_alignment_skew) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9585 | // r0: result parameter for PerformGC, if any |
| 9586 | // r4: number of arguments including receiver (C callee-saved) |
| 9587 | // r5: pointer to builtin function (C callee-saved) |
| 9588 | // r6: pointer to the first argument (C callee-saved) |
| 9589 | |
| 9590 | if (do_gc) { |
| 9591 | // Passing r0. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9592 | __ PrepareCallCFunction(1, r1); |
| 9593 | __ CallCFunction(ExternalReference::perform_gc_function(), 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9594 | } |
| 9595 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9596 | ExternalReference scope_depth = |
| 9597 | ExternalReference::heap_always_allocate_scope_depth(); |
| 9598 | if (always_allocate) { |
| 9599 | __ mov(r0, Operand(scope_depth)); |
| 9600 | __ ldr(r1, MemOperand(r0)); |
| 9601 | __ add(r1, r1, Operand(1)); |
| 9602 | __ str(r1, MemOperand(r0)); |
| 9603 | } |
| 9604 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9605 | // Call C built-in. |
| 9606 | // r0 = argc, r1 = argv |
| 9607 | __ mov(r0, Operand(r4)); |
| 9608 | __ mov(r1, Operand(r6)); |
| 9609 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9610 | int frame_alignment = MacroAssembler::ActivationFrameAlignment(); |
| 9611 | int frame_alignment_mask = frame_alignment - 1; |
| 9612 | #if defined(V8_HOST_ARCH_ARM) |
| 9613 | if (FLAG_debug_code) { |
| 9614 | if (frame_alignment > kPointerSize) { |
| 9615 | Label alignment_as_expected; |
| 9616 | ASSERT(IsPowerOf2(frame_alignment)); |
| 9617 | __ sub(r2, sp, Operand(frame_alignment_skew)); |
| 9618 | __ tst(r2, Operand(frame_alignment_mask)); |
| 9619 | __ b(eq, &alignment_as_expected); |
| 9620 | // Don't use Check here, as it will call Runtime_Abort re-entering here. |
| 9621 | __ stop("Unexpected alignment"); |
| 9622 | __ bind(&alignment_as_expected); |
| 9623 | } |
| 9624 | } |
| 9625 | #endif |
| 9626 | |
| 9627 | // Just before the call (jump) below lr is pushed, so the actual alignment is |
| 9628 | // adding one to the current skew. |
| 9629 | int alignment_before_call = |
| 9630 | (frame_alignment_skew + kPointerSize) & frame_alignment_mask; |
| 9631 | if (alignment_before_call > 0) { |
| 9632 | // Push until the alignment before the call is met. |
| 9633 | __ mov(r2, Operand(0)); |
| 9634 | for (int i = alignment_before_call; |
| 9635 | (i & frame_alignment_mask) != 0; |
| 9636 | i += kPointerSize) { |
| 9637 | __ push(r2); |
| 9638 | } |
| 9639 | } |
| 9640 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9641 | // TODO(1242173): To let the GC traverse the return address of the exit |
| 9642 | // frames, we need to know where the return address is. Right now, |
| 9643 | // we push it on the stack to be able to find it again, but we never |
| 9644 | // restore from it in case of changes, which makes it impossible to |
| 9645 | // support moving the C entry code stub. This should be fixed, but currently |
| 9646 | // this is OK because the CEntryStub gets generated so early in the V8 boot |
| 9647 | // sequence that it is not moving ever. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9648 | masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4 |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9649 | masm->push(lr); |
| 9650 | masm->Jump(r5); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9651 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9652 | // Restore sp back to before aligning the stack. |
| 9653 | if (alignment_before_call > 0) { |
| 9654 | __ add(sp, sp, Operand(alignment_before_call)); |
| 9655 | } |
| 9656 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9657 | if (always_allocate) { |
| 9658 | // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1 |
| 9659 | // though (contain the result). |
| 9660 | __ mov(r2, Operand(scope_depth)); |
| 9661 | __ ldr(r3, MemOperand(r2)); |
| 9662 | __ sub(r3, r3, Operand(1)); |
| 9663 | __ str(r3, MemOperand(r2)); |
| 9664 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9665 | |
| 9666 | // check for failure result |
| 9667 | Label failure_returned; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9668 | STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9669 | // Lower 2 bits of r2 are 0 iff r0 has failure tag. |
| 9670 | __ add(r2, r0, Operand(1)); |
| 9671 | __ tst(r2, Operand(kFailureTagMask)); |
| 9672 | __ b(eq, &failure_returned); |
| 9673 | |
| 9674 | // Exit C frame and return. |
| 9675 | // r0:r1: result |
| 9676 | // sp: stack pointer |
| 9677 | // fp: frame pointer |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9678 | __ LeaveExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9679 | |
| 9680 | // check if we should retry or throw exception |
| 9681 | Label retry; |
| 9682 | __ bind(&failure_returned); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 9683 | STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9684 | __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize)); |
| 9685 | __ b(eq, &retry); |
| 9686 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9687 | // Special handling of out of memory exceptions. |
| 9688 | Failure* out_of_memory = Failure::OutOfMemoryException(); |
| 9689 | __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory))); |
| 9690 | __ b(eq, throw_out_of_memory_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9691 | |
| 9692 | // Retrieve the pending exception and clear the variable. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9693 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9694 | __ ldr(r3, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9695 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9696 | __ ldr(r0, MemOperand(ip)); |
| 9697 | __ str(r3, MemOperand(ip)); |
| 9698 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9699 | // Special handling of termination exceptions which are uncatchable |
| 9700 | // by javascript code. |
| 9701 | __ cmp(r0, Operand(Factory::termination_exception())); |
| 9702 | __ b(eq, throw_termination_exception); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9703 | |
| 9704 | // Handle normal exception. |
| 9705 | __ jmp(throw_normal_exception); |
| 9706 | |
| 9707 | __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying |
| 9708 | } |
| 9709 | |
| 9710 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9711 | void CEntryStub::Generate(MacroAssembler* masm) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9712 | // Called from JavaScript; parameters are on stack as if calling JS function |
| 9713 | // r0: number of arguments including receiver |
| 9714 | // r1: pointer to builtin function |
| 9715 | // fp: frame pointer (restored after C call) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9716 | // sp: stack pointer (restored as callee's sp after C call) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9717 | // cp: current context (C callee-saved) |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9718 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9719 | // Result returned in r0 or r0+r1 by default. |
| 9720 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9721 | // NOTE: Invocations of builtins may return failure objects |
| 9722 | // instead of a proper result. The builtin entry handles |
| 9723 | // this by performing a garbage collection and retrying the |
| 9724 | // builtin once. |
| 9725 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9726 | // Enter the exit frame that transitions from JavaScript to C++. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9727 | __ EnterExitFrame(mode_); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9728 | |
| 9729 | // r4: number of arguments (C callee-saved) |
| 9730 | // r5: pointer to builtin function (C callee-saved) |
| 9731 | // r6: pointer to first argument (C callee-saved) |
| 9732 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9733 | Label throw_normal_exception; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9734 | Label throw_termination_exception; |
| 9735 | Label throw_out_of_memory_exception; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9736 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9737 | // Call into the runtime system. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9738 | GenerateCore(masm, |
| 9739 | &throw_normal_exception, |
| 9740 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9741 | &throw_out_of_memory_exception, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9742 | false, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9743 | false, |
| 9744 | -kPointerSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9745 | |
| 9746 | // Do space-specific GC and retry runtime call. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9747 | GenerateCore(masm, |
| 9748 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9749 | &throw_termination_exception, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9750 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9751 | true, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9752 | false, |
| 9753 | 0); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9754 | |
| 9755 | // Do full GC and retry runtime call one final time. |
| 9756 | Failure* failure = Failure::InternalError(); |
| 9757 | __ mov(r0, Operand(reinterpret_cast<int32_t>(failure))); |
| 9758 | GenerateCore(masm, |
| 9759 | &throw_normal_exception, |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9760 | &throw_termination_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9761 | &throw_out_of_memory_exception, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9762 | true, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 9763 | true, |
| 9764 | kPointerSize); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9765 | |
| 9766 | __ bind(&throw_out_of_memory_exception); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 9767 | GenerateThrowUncatchable(masm, OUT_OF_MEMORY); |
| 9768 | |
| 9769 | __ bind(&throw_termination_exception); |
| 9770 | GenerateThrowUncatchable(masm, TERMINATION); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9771 | |
| 9772 | __ bind(&throw_normal_exception); |
| 9773 | GenerateThrowTOS(masm); |
| 9774 | } |
| 9775 | |
| 9776 | |
| 9777 | void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) { |
| 9778 | // r0: code entry |
| 9779 | // r1: function |
| 9780 | // r2: receiver |
| 9781 | // r3: argc |
| 9782 | // [sp+0]: argv |
| 9783 | |
| 9784 | Label invoke, exit; |
| 9785 | |
| 9786 | // Called from C, so do not pop argc and args on exit (preserve sp) |
| 9787 | // No need to save register-passed args |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9788 | // Save callee-saved registers (incl. cp and fp), sp, and lr |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9789 | __ stm(db_w, sp, kCalleeSaved | lr.bit()); |
| 9790 | |
| 9791 | // Get address of argv, see stm above. |
| 9792 | // r0: code entry |
| 9793 | // r1: function |
| 9794 | // r2: receiver |
| 9795 | // r3: argc |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9796 | __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9797 | |
| 9798 | // Push a frame with special values setup to mark it as an entry frame. |
| 9799 | // r0: code entry |
| 9800 | // r1: function |
| 9801 | // r2: receiver |
| 9802 | // r3: argc |
| 9803 | // r4: argv |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9804 | __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9805 | int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY; |
| 9806 | __ mov(r7, Operand(Smi::FromInt(marker))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9807 | __ mov(r6, Operand(Smi::FromInt(marker))); |
| 9808 | __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 9809 | __ ldr(r5, MemOperand(r5)); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 9810 | __ Push(r8, r7, r6, r5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9811 | |
| 9812 | // Setup frame pointer for the frame to be pushed. |
| 9813 | __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 9814 | |
| 9815 | // Call a faked try-block that does the invoke. |
| 9816 | __ bl(&invoke); |
| 9817 | |
| 9818 | // Caught exception: Store result (exception) in the pending |
| 9819 | // exception field in the JSEnv and return a failure sentinel. |
| 9820 | // Coming in here the fp will be invalid because the PushTryHandler below |
| 9821 | // 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] | 9822 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9823 | __ str(r0, MemOperand(ip)); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 9824 | __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception()))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9825 | __ b(&exit); |
| 9826 | |
| 9827 | // Invoke: Link this frame into the handler chain. |
| 9828 | __ bind(&invoke); |
| 9829 | // Must preserve r0-r4, r5-r7 are available. |
| 9830 | __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9831 | // If an exception not caught by another handler occurs, this handler |
| 9832 | // returns control to the code after the bl(&invoke) above, which |
| 9833 | // restores all kCalleeSaved registers (including cp and fp) to their |
| 9834 | // saved values before returning a failure to C. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9835 | |
| 9836 | // Clear any pending exceptions. |
| 9837 | __ mov(ip, Operand(ExternalReference::the_hole_value_location())); |
| 9838 | __ ldr(r5, MemOperand(ip)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9839 | __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address))); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9840 | __ str(r5, MemOperand(ip)); |
| 9841 | |
| 9842 | // Invoke the function by calling through JS entry trampoline builtin. |
| 9843 | // Notice that we cannot store a reference to the trampoline code directly in |
| 9844 | // this stub, because runtime stubs are not traversed when doing GC. |
| 9845 | |
| 9846 | // Expected registers by Builtins::JSEntryTrampoline |
| 9847 | // r0: code entry |
| 9848 | // r1: function |
| 9849 | // r2: receiver |
| 9850 | // r3: argc |
| 9851 | // r4: argv |
| 9852 | if (is_construct) { |
| 9853 | ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline); |
| 9854 | __ mov(ip, Operand(construct_entry)); |
| 9855 | } else { |
| 9856 | ExternalReference entry(Builtins::JSEntryTrampoline); |
| 9857 | __ mov(ip, Operand(entry)); |
| 9858 | } |
| 9859 | __ ldr(ip, MemOperand(ip)); // deref address |
| 9860 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9861 | // Branch and link to JSEntryTrampoline. We don't use the double underscore |
| 9862 | // macro for the add instruction because we don't want the coverage tool |
| 9863 | // inserting instructions here after we read the pc. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9864 | __ mov(lr, Operand(pc)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9865 | masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9866 | |
| 9867 | // Unlink this frame from the handler chain. When reading the |
| 9868 | // address of the next handler, there is no need to use the address |
| 9869 | // displacement since the current stack pointer (sp) points directly |
| 9870 | // to the stack handler. |
| 9871 | __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset)); |
| 9872 | __ mov(ip, Operand(ExternalReference(Top::k_handler_address))); |
| 9873 | __ str(r3, MemOperand(ip)); |
| 9874 | // No need to restore registers |
| 9875 | __ add(sp, sp, Operand(StackHandlerConstants::kSize)); |
| 9876 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9877 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9878 | __ bind(&exit); // r0 holds result |
| 9879 | // Restore the top frame descriptors from the stack. |
| 9880 | __ pop(r3); |
| 9881 | __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address))); |
| 9882 | __ str(r3, MemOperand(ip)); |
| 9883 | |
| 9884 | // Reset the stack to the callee saved registers. |
| 9885 | __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset)); |
| 9886 | |
| 9887 | // Restore callee-saved registers and return. |
| 9888 | #ifdef DEBUG |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9889 | if (FLAG_debug_code) { |
| 9890 | __ mov(lr, Operand(pc)); |
| 9891 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9892 | #endif |
| 9893 | __ ldm(ia_w, sp, kCalleeSaved | pc.bit()); |
| 9894 | } |
| 9895 | |
| 9896 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9897 | // This stub performs an instanceof, calling the builtin function if |
| 9898 | // necessary. Uses r1 for the object, r0 for the function that it may |
| 9899 | // be an instance of (these are fetched from the stack). |
| 9900 | void InstanceofStub::Generate(MacroAssembler* masm) { |
| 9901 | // Get the object - slow case for smis (we may need to throw an exception |
| 9902 | // depending on the rhs). |
| 9903 | Label slow, loop, is_instance, is_not_instance; |
| 9904 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); |
| 9905 | __ BranchOnSmi(r0, &slow); |
| 9906 | |
| 9907 | // Check that the left hand is a JS object and put map in r3. |
| 9908 | __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE); |
| 9909 | __ b(lt, &slow); |
| 9910 | __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE)); |
| 9911 | __ b(gt, &slow); |
| 9912 | |
| 9913 | // Get the prototype of the function (r4 is result, r2 is scratch). |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9914 | __ ldr(r1, MemOperand(sp, 0)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 9915 | // r1 is function, r3 is map. |
| 9916 | |
| 9917 | // Look up the function and the map in the instanceof cache. |
| 9918 | Label miss; |
| 9919 | __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex); |
| 9920 | __ cmp(r1, ip); |
| 9921 | __ b(ne, &miss); |
| 9922 | __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex); |
| 9923 | __ cmp(r3, ip); |
| 9924 | __ b(ne, &miss); |
| 9925 | __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
| 9926 | __ pop(); |
| 9927 | __ pop(); |
| 9928 | __ mov(pc, Operand(lr)); |
| 9929 | |
| 9930 | __ bind(&miss); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9931 | __ TryGetFunctionPrototype(r1, r4, r2, &slow); |
| 9932 | |
| 9933 | // Check that the function prototype is a JS object. |
| 9934 | __ BranchOnSmi(r4, &slow); |
| 9935 | __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE); |
| 9936 | __ b(lt, &slow); |
| 9937 | __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE)); |
| 9938 | __ b(gt, &slow); |
| 9939 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 9940 | __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex); |
| 9941 | __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex); |
| 9942 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9943 | // Register mapping: r3 is object map and r4 is function prototype. |
| 9944 | // Get prototype of object into r2. |
| 9945 | __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset)); |
| 9946 | |
| 9947 | // Loop through the prototype chain looking for the function prototype. |
| 9948 | __ bind(&loop); |
| 9949 | __ cmp(r2, Operand(r4)); |
| 9950 | __ b(eq, &is_instance); |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 9951 | __ LoadRoot(ip, Heap::kNullValueRootIndex); |
| 9952 | __ cmp(r2, ip); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9953 | __ b(eq, &is_not_instance); |
| 9954 | __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset)); |
| 9955 | __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset)); |
| 9956 | __ jmp(&loop); |
| 9957 | |
| 9958 | __ bind(&is_instance); |
| 9959 | __ mov(r0, Operand(Smi::FromInt(0))); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 9960 | __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9961 | __ pop(); |
| 9962 | __ pop(); |
| 9963 | __ mov(pc, Operand(lr)); // Return. |
| 9964 | |
| 9965 | __ bind(&is_not_instance); |
| 9966 | __ mov(r0, Operand(Smi::FromInt(1))); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 9967 | __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9968 | __ pop(); |
| 9969 | __ pop(); |
| 9970 | __ mov(pc, Operand(lr)); // Return. |
| 9971 | |
| 9972 | // Slow-case. Tail call builtin. |
| 9973 | __ bind(&slow); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9974 | __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS); |
| 9975 | } |
| 9976 | |
| 9977 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9978 | void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) { |
| 9979 | // The displacement is the offset of the last parameter (if any) |
| 9980 | // relative to the frame pointer. |
| 9981 | static const int kDisplacement = |
| 9982 | StandardFrameConstants::kCallerSPOffset - kPointerSize; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9983 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9984 | // Check that the key is a smi. |
| 9985 | Label slow; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9986 | __ BranchOnNotSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9987 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9988 | // Check if the calling frame is an arguments adaptor frame. |
| 9989 | Label adaptor; |
| 9990 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 9991 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9992 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9993 | __ b(eq, &adaptor); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 9994 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9995 | // Check index against formal parameters count limit passed in |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9996 | // through register r0. Use unsigned comparison to get negative |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9997 | // check for free. |
| 9998 | __ cmp(r1, r0); |
| 9999 | __ b(cs, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10000 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10001 | // Read the argument from the stack and return it. |
| 10002 | __ sub(r3, r0, r1); |
| 10003 | __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10004 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10005 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10006 | |
| 10007 | // Arguments adaptor case: Check index against actual arguments |
| 10008 | // limit found in the arguments adaptor frame. Use unsigned |
| 10009 | // comparison to get negative check for free. |
| 10010 | __ bind(&adaptor); |
| 10011 | __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 10012 | __ cmp(r1, r0); |
| 10013 | __ b(cs, &slow); |
| 10014 | |
| 10015 | // Read the argument from the adaptor frame and return it. |
| 10016 | __ sub(r3, r0, r1); |
| 10017 | __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10018 | __ ldr(r0, MemOperand(r3, kDisplacement)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10019 | __ Jump(lr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10020 | |
| 10021 | // Slow-case: Handle non-smi or out-of-bounds access to arguments |
| 10022 | // by calling the runtime system. |
| 10023 | __ bind(&slow); |
| 10024 | __ push(r1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 10025 | __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10026 | } |
| 10027 | |
| 10028 | |
| 10029 | void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10030 | // sp[0] : number of parameters |
| 10031 | // sp[4] : receiver displacement |
| 10032 | // sp[8] : function |
| 10033 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10034 | // Check if the calling frame is an arguments adaptor frame. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10035 | Label adaptor_frame, try_allocate, runtime; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10036 | __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset)); |
| 10037 | __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset)); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 10038 | __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10039 | __ b(eq, &adaptor_frame); |
| 10040 | |
| 10041 | // Get the length from the frame. |
| 10042 | __ ldr(r1, MemOperand(sp, 0)); |
| 10043 | __ b(&try_allocate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10044 | |
| 10045 | // Patch the arguments.length and the parameters pointer. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10046 | __ bind(&adaptor_frame); |
| 10047 | __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 10048 | __ str(r1, MemOperand(sp, 0)); |
| 10049 | __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10050 | __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset)); |
| 10051 | __ str(r3, MemOperand(sp, 1 * kPointerSize)); |
| 10052 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10053 | // Try the new space allocation. Start out with computing the size |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 10054 | // of the arguments object and the elements array in words. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10055 | Label add_arguments_object; |
| 10056 | __ bind(&try_allocate); |
| 10057 | __ cmp(r1, Operand(0)); |
| 10058 | __ b(eq, &add_arguments_object); |
| 10059 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); |
| 10060 | __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize)); |
| 10061 | __ bind(&add_arguments_object); |
| 10062 | __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize)); |
| 10063 | |
| 10064 | // Do the allocation of both objects in one go. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 10065 | __ AllocateInNewSpace( |
| 10066 | r1, |
| 10067 | r0, |
| 10068 | r2, |
| 10069 | r3, |
| 10070 | &runtime, |
| 10071 | static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10072 | |
| 10073 | // Get the arguments boilerplate from the current (global) context. |
| 10074 | int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX); |
| 10075 | __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 10076 | __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset)); |
| 10077 | __ ldr(r4, MemOperand(r4, offset)); |
| 10078 | |
| 10079 | // Copy the JS object part. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 10080 | __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10081 | |
| 10082 | // Setup the callee in-object property. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10083 | STATIC_ASSERT(Heap::arguments_callee_index == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10084 | __ ldr(r3, MemOperand(sp, 2 * kPointerSize)); |
| 10085 | __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize)); |
| 10086 | |
| 10087 | // Get the length (smi tagged) and set that as an in-object property too. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10088 | STATIC_ASSERT(Heap::arguments_length_index == 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10089 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); |
| 10090 | __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize)); |
| 10091 | |
| 10092 | // If there are no actual arguments, we're done. |
| 10093 | Label done; |
| 10094 | __ cmp(r1, Operand(0)); |
| 10095 | __ b(eq, &done); |
| 10096 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10097 | // Get the parameters pointer from the stack. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10098 | __ ldr(r2, MemOperand(sp, 1 * kPointerSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10099 | |
| 10100 | // Setup the elements pointer in the allocated arguments object and |
| 10101 | // initialize the header in the elements fixed array. |
| 10102 | __ add(r4, r0, Operand(Heap::kArgumentsObjectSize)); |
| 10103 | __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset)); |
| 10104 | __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex); |
| 10105 | __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset)); |
| 10106 | __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10107 | __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10108 | |
| 10109 | // Copy the fixed array slots. |
| 10110 | Label loop; |
| 10111 | // Setup r4 to point to the first array slot. |
| 10112 | __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag)); |
| 10113 | __ bind(&loop); |
| 10114 | // Pre-decrement r2 with kPointerSize on each iteration. |
| 10115 | // Pre-decrement in order to skip receiver. |
| 10116 | __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex)); |
| 10117 | // Post-increment r4 with kPointerSize on each iteration. |
| 10118 | __ str(r3, MemOperand(r4, kPointerSize, PostIndex)); |
| 10119 | __ sub(r1, r1, Operand(1)); |
| 10120 | __ cmp(r1, Operand(0)); |
| 10121 | __ b(ne, &loop); |
| 10122 | |
| 10123 | // Return and remove the on-stack parameters. |
| 10124 | __ bind(&done); |
| 10125 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 10126 | __ Ret(); |
| 10127 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10128 | // Do the runtime call to allocate the arguments object. |
| 10129 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 10130 | __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10131 | } |
| 10132 | |
| 10133 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10134 | void RegExpExecStub::Generate(MacroAssembler* masm) { |
| 10135 | // Just jump directly to runtime if native RegExp is not selected at compile |
| 10136 | // time or if regexp entry in generated code is turned off runtime switch or |
| 10137 | // at compilation. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 10138 | #ifdef V8_INTERPRETED_REGEXP |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10139 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 10140 | #else // V8_INTERPRETED_REGEXP |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10141 | if (!FLAG_regexp_entry_native) { |
| 10142 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
| 10143 | return; |
| 10144 | } |
| 10145 | |
| 10146 | // Stack frame on entry. |
| 10147 | // sp[0]: last_match_info (expected JSArray) |
| 10148 | // sp[4]: previous index |
| 10149 | // sp[8]: subject string |
| 10150 | // sp[12]: JSRegExp object |
| 10151 | |
| 10152 | static const int kLastMatchInfoOffset = 0 * kPointerSize; |
| 10153 | static const int kPreviousIndexOffset = 1 * kPointerSize; |
| 10154 | static const int kSubjectOffset = 2 * kPointerSize; |
| 10155 | static const int kJSRegExpOffset = 3 * kPointerSize; |
| 10156 | |
| 10157 | Label runtime, invoke_regexp; |
| 10158 | |
| 10159 | // Allocation of registers for this function. These are in callee save |
| 10160 | // registers and will be preserved by the call to the native RegExp code, as |
| 10161 | // this code is called using the normal C calling convention. When calling |
| 10162 | // directly from generated code the native RegExp code will not do a GC and |
| 10163 | // therefore the content of these registers are safe to use after the call. |
| 10164 | Register subject = r4; |
| 10165 | Register regexp_data = r5; |
| 10166 | Register last_match_info_elements = r6; |
| 10167 | |
| 10168 | // Ensure that a RegExp stack is allocated. |
| 10169 | ExternalReference address_of_regexp_stack_memory_address = |
| 10170 | ExternalReference::address_of_regexp_stack_memory_address(); |
| 10171 | ExternalReference address_of_regexp_stack_memory_size = |
| 10172 | ExternalReference::address_of_regexp_stack_memory_size(); |
| 10173 | __ mov(r0, Operand(address_of_regexp_stack_memory_size)); |
| 10174 | __ ldr(r0, MemOperand(r0, 0)); |
| 10175 | __ tst(r0, Operand(r0)); |
| 10176 | __ b(eq, &runtime); |
| 10177 | |
| 10178 | // Check that the first argument is a JSRegExp object. |
| 10179 | __ ldr(r0, MemOperand(sp, kJSRegExpOffset)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10180 | STATIC_ASSERT(kSmiTag == 0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10181 | __ tst(r0, Operand(kSmiTagMask)); |
| 10182 | __ b(eq, &runtime); |
| 10183 | __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE); |
| 10184 | __ b(ne, &runtime); |
| 10185 | |
| 10186 | // Check that the RegExp has been compiled (data contains a fixed array). |
| 10187 | __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset)); |
| 10188 | if (FLAG_debug_code) { |
| 10189 | __ tst(regexp_data, Operand(kSmiTagMask)); |
| 10190 | __ Check(nz, "Unexpected type for RegExp data, FixedArray expected"); |
| 10191 | __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE); |
| 10192 | __ Check(eq, "Unexpected type for RegExp data, FixedArray expected"); |
| 10193 | } |
| 10194 | |
| 10195 | // regexp_data: RegExp data (FixedArray) |
| 10196 | // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP. |
| 10197 | __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset)); |
| 10198 | __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP))); |
| 10199 | __ b(ne, &runtime); |
| 10200 | |
| 10201 | // regexp_data: RegExp data (FixedArray) |
| 10202 | // Check that the number of captures fit in the static offsets vector buffer. |
| 10203 | __ ldr(r2, |
| 10204 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 10205 | // Calculate number of capture registers (number_of_captures + 1) * 2. This |
| 10206 | // uses the asumption that smis are 2 * their untagged value. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10207 | STATIC_ASSERT(kSmiTag == 0); |
| 10208 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10209 | __ add(r2, r2, Operand(2)); // r2 was a smi. |
| 10210 | // Check that the static offsets vector buffer is large enough. |
| 10211 | __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize)); |
| 10212 | __ b(hi, &runtime); |
| 10213 | |
| 10214 | // r2: Number of capture registers |
| 10215 | // regexp_data: RegExp data (FixedArray) |
| 10216 | // Check that the second argument is a string. |
| 10217 | __ ldr(subject, MemOperand(sp, kSubjectOffset)); |
| 10218 | __ tst(subject, Operand(kSmiTagMask)); |
| 10219 | __ b(eq, &runtime); |
| 10220 | Condition is_string = masm->IsObjectStringType(subject, r0); |
| 10221 | __ b(NegateCondition(is_string), &runtime); |
| 10222 | // Get the length of the string to r3. |
| 10223 | __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset)); |
| 10224 | |
| 10225 | // r2: Number of capture registers |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10226 | // r3: Length of subject string as a smi |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10227 | // subject: Subject string |
| 10228 | // regexp_data: RegExp data (FixedArray) |
| 10229 | // Check that the third argument is a positive smi less than the subject |
| 10230 | // string length. A negative value will be greater (unsigned comparison). |
| 10231 | __ ldr(r0, MemOperand(sp, kPreviousIndexOffset)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10232 | __ tst(r0, Operand(kSmiTagMask)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 10233 | __ b(ne, &runtime); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10234 | __ cmp(r3, Operand(r0)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 10235 | __ b(ls, &runtime); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10236 | |
| 10237 | // r2: Number of capture registers |
| 10238 | // subject: Subject string |
| 10239 | // regexp_data: RegExp data (FixedArray) |
| 10240 | // Check that the fourth object is a JSArray object. |
| 10241 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 10242 | __ tst(r0, Operand(kSmiTagMask)); |
| 10243 | __ b(eq, &runtime); |
| 10244 | __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE); |
| 10245 | __ b(ne, &runtime); |
| 10246 | // Check that the JSArray is in fast case. |
| 10247 | __ ldr(last_match_info_elements, |
| 10248 | FieldMemOperand(r0, JSArray::kElementsOffset)); |
| 10249 | __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset)); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 10250 | __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 10251 | __ cmp(r0, ip); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10252 | __ b(ne, &runtime); |
| 10253 | // Check that the last match info has space for the capture registers and the |
| 10254 | // additional information. |
| 10255 | __ ldr(r0, |
| 10256 | FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset)); |
| 10257 | __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10258 | __ cmp(r2, Operand(r0, ASR, kSmiTagSize)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10259 | __ b(gt, &runtime); |
| 10260 | |
| 10261 | // subject: Subject string |
| 10262 | // regexp_data: RegExp data (FixedArray) |
| 10263 | // Check the representation and encoding of the subject string. |
| 10264 | Label seq_string; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10265 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 10266 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 10267 | // First check for flat string. |
| 10268 | __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10269 | STATIC_ASSERT((kStringTag | kSeqStringTag) == 0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10270 | __ b(eq, &seq_string); |
| 10271 | |
| 10272 | // subject: Subject string |
| 10273 | // regexp_data: RegExp data (FixedArray) |
| 10274 | // Check for flat cons string. |
| 10275 | // A flat cons string is a cons string where the second part is the empty |
| 10276 | // string. In that case the subject string is just the first part of the cons |
| 10277 | // string. Also in this case the first part of the cons string is known to be |
| 10278 | // a sequential string or an external string. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10279 | STATIC_ASSERT(kExternalStringTag !=0); |
| 10280 | STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 10281 | __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag)); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10282 | __ b(ne, &runtime); |
| 10283 | __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset)); |
| 10284 | __ LoadRoot(r1, Heap::kEmptyStringRootIndex); |
| 10285 | __ cmp(r0, r1); |
| 10286 | __ b(ne, &runtime); |
| 10287 | __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset)); |
| 10288 | __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset)); |
| 10289 | __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 10290 | // Is first part a flat string? |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10291 | STATIC_ASSERT(kSeqStringTag == 0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10292 | __ tst(r0, Operand(kStringRepresentationMask)); |
| 10293 | __ b(nz, &runtime); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10294 | |
| 10295 | __ bind(&seq_string); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10296 | // subject: Subject string |
| 10297 | // regexp_data: RegExp data (FixedArray) |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 10298 | // r0: Instance type of subject string |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10299 | STATIC_ASSERT(4 == kAsciiStringTag); |
| 10300 | STATIC_ASSERT(kTwoByteStringTag == 0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10301 | // Find the code object based on the assumptions above. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 10302 | __ and_(r0, r0, Operand(kStringEncodingMask)); |
| 10303 | __ mov(r3, Operand(r0, ASR, 2), SetCC); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10304 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne); |
| 10305 | __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq); |
| 10306 | |
| 10307 | // Check that the irregexp code has been generated for the actual string |
| 10308 | // encoding. If it has, the field contains a code object otherwise it contains |
| 10309 | // the hole. |
| 10310 | __ CompareObjectType(r7, r0, r0, CODE_TYPE); |
| 10311 | __ b(ne, &runtime); |
| 10312 | |
| 10313 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 10314 | // r7: code |
| 10315 | // subject: Subject string |
| 10316 | // regexp_data: RegExp data (FixedArray) |
| 10317 | // Load used arguments before starting to push arguments for call to native |
| 10318 | // RegExp code to avoid handling changing stack height. |
| 10319 | __ ldr(r1, MemOperand(sp, kPreviousIndexOffset)); |
| 10320 | __ mov(r1, Operand(r1, ASR, kSmiTagSize)); |
| 10321 | |
| 10322 | // r1: previous index |
| 10323 | // r3: encoding of subject string (1 if ascii, 0 if two_byte); |
| 10324 | // r7: code |
| 10325 | // subject: Subject string |
| 10326 | // regexp_data: RegExp data (FixedArray) |
| 10327 | // All checks done. Now push arguments for native regexp code. |
| 10328 | __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2); |
| 10329 | |
| 10330 | static const int kRegExpExecuteArguments = 7; |
| 10331 | __ push(lr); |
| 10332 | __ PrepareCallCFunction(kRegExpExecuteArguments, r0); |
| 10333 | |
| 10334 | // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript. |
| 10335 | __ mov(r0, Operand(1)); |
| 10336 | __ str(r0, MemOperand(sp, 2 * kPointerSize)); |
| 10337 | |
| 10338 | // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area. |
| 10339 | __ mov(r0, Operand(address_of_regexp_stack_memory_address)); |
| 10340 | __ ldr(r0, MemOperand(r0, 0)); |
| 10341 | __ mov(r2, Operand(address_of_regexp_stack_memory_size)); |
| 10342 | __ ldr(r2, MemOperand(r2, 0)); |
| 10343 | __ add(r0, r0, Operand(r2)); |
| 10344 | __ str(r0, MemOperand(sp, 1 * kPointerSize)); |
| 10345 | |
| 10346 | // Argument 5 (sp[0]): static offsets vector buffer. |
| 10347 | __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector())); |
| 10348 | __ str(r0, MemOperand(sp, 0 * kPointerSize)); |
| 10349 | |
| 10350 | // For arguments 4 and 3 get string length, calculate start of string data and |
| 10351 | // calculate the shift of the index (0 for ASCII and 1 for two byte). |
| 10352 | __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10353 | __ mov(r0, Operand(r0, ASR, kSmiTagSize)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10354 | STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10355 | __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 10356 | __ eor(r3, r3, Operand(1)); |
| 10357 | // Argument 4 (r3): End of string data |
| 10358 | // Argument 3 (r2): Start of string data |
| 10359 | __ add(r2, r9, Operand(r1, LSL, r3)); |
| 10360 | __ add(r3, r9, Operand(r0, LSL, r3)); |
| 10361 | |
| 10362 | // Argument 2 (r1): Previous index. |
| 10363 | // Already there |
| 10364 | |
| 10365 | // Argument 1 (r0): Subject string. |
| 10366 | __ mov(r0, subject); |
| 10367 | |
| 10368 | // Locate the code entry and call it. |
| 10369 | __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 10370 | __ CallCFunction(r7, kRegExpExecuteArguments); |
| 10371 | __ pop(lr); |
| 10372 | |
| 10373 | // r0: result |
| 10374 | // subject: subject string (callee saved) |
| 10375 | // regexp_data: RegExp data (callee saved) |
| 10376 | // last_match_info_elements: Last match info elements (callee saved) |
| 10377 | |
| 10378 | // Check the result. |
| 10379 | Label success; |
| 10380 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS)); |
| 10381 | __ b(eq, &success); |
| 10382 | Label failure; |
| 10383 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE)); |
| 10384 | __ b(eq, &failure); |
| 10385 | __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION)); |
| 10386 | // If not exception it can only be retry. Handle that in the runtime system. |
| 10387 | __ b(ne, &runtime); |
| 10388 | // Result must now be exception. If there is no pending exception already a |
| 10389 | // stack overflow (on the backtrack stack) was detected in RegExp code but |
| 10390 | // haven't created the exception yet. Handle that in the runtime system. |
| 10391 | // TODO(592): Rerunning the RegExp to get the stack overflow exception. |
| 10392 | __ mov(r0, Operand(ExternalReference::the_hole_value_location())); |
| 10393 | __ ldr(r0, MemOperand(r0, 0)); |
| 10394 | __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address))); |
| 10395 | __ ldr(r1, MemOperand(r1, 0)); |
| 10396 | __ cmp(r0, r1); |
| 10397 | __ b(eq, &runtime); |
| 10398 | __ bind(&failure); |
| 10399 | // For failure and exception return null. |
| 10400 | __ mov(r0, Operand(Factory::null_value())); |
| 10401 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 10402 | __ Ret(); |
| 10403 | |
| 10404 | // Process the result from the native regexp code. |
| 10405 | __ bind(&success); |
| 10406 | __ ldr(r1, |
| 10407 | FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset)); |
| 10408 | // Calculate number of capture registers (number_of_captures + 1) * 2. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10409 | STATIC_ASSERT(kSmiTag == 0); |
| 10410 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10411 | __ add(r1, r1, Operand(2)); // r1 was a smi. |
| 10412 | |
| 10413 | // r1: number of capture registers |
| 10414 | // r4: subject string |
| 10415 | // Store the capture count. |
| 10416 | __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi. |
| 10417 | __ str(r2, FieldMemOperand(last_match_info_elements, |
| 10418 | RegExpImpl::kLastCaptureCountOffset)); |
| 10419 | // Store last subject and last input. |
| 10420 | __ mov(r3, last_match_info_elements); // Moved up to reduce latency. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10421 | __ str(subject, |
| 10422 | FieldMemOperand(last_match_info_elements, |
| 10423 | RegExpImpl::kLastSubjectOffset)); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10424 | __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10425 | __ str(subject, |
| 10426 | FieldMemOperand(last_match_info_elements, |
| 10427 | RegExpImpl::kLastInputOffset)); |
| 10428 | __ mov(r3, last_match_info_elements); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10429 | __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10430 | |
| 10431 | // Get the static offsets vector filled by the native regexp code. |
| 10432 | ExternalReference address_of_static_offsets_vector = |
| 10433 | ExternalReference::address_of_static_offsets_vector(); |
| 10434 | __ mov(r2, Operand(address_of_static_offsets_vector)); |
| 10435 | |
| 10436 | // r1: number of capture registers |
| 10437 | // r2: offsets vector |
| 10438 | Label next_capture, done; |
| 10439 | // Capture register counter starts from number of capture registers and |
| 10440 | // counts down until wraping after zero. |
| 10441 | __ add(r0, |
| 10442 | last_match_info_elements, |
| 10443 | Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag)); |
| 10444 | __ bind(&next_capture); |
| 10445 | __ sub(r1, r1, Operand(1), SetCC); |
| 10446 | __ b(mi, &done); |
| 10447 | // Read the value from the static offsets vector buffer. |
| 10448 | __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex)); |
| 10449 | // Store the smi value in the last match info. |
| 10450 | __ mov(r3, Operand(r3, LSL, kSmiTagSize)); |
| 10451 | __ str(r3, MemOperand(r0, kPointerSize, PostIndex)); |
| 10452 | __ jmp(&next_capture); |
| 10453 | __ bind(&done); |
| 10454 | |
| 10455 | // Return last match info. |
| 10456 | __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset)); |
| 10457 | __ add(sp, sp, Operand(4 * kPointerSize)); |
| 10458 | __ Ret(); |
| 10459 | |
| 10460 | // Do the runtime call to execute the regexp. |
| 10461 | __ bind(&runtime); |
| 10462 | __ TailCallRuntime(Runtime::kRegExpExec, 4, 1); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 10463 | #endif // V8_INTERPRETED_REGEXP |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
| 10466 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10467 | void CallFunctionStub::Generate(MacroAssembler* masm) { |
| 10468 | Label slow; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10469 | |
| 10470 | // If the receiver might be a value (string, number or boolean) check for this |
| 10471 | // and box it if it is. |
| 10472 | if (ReceiverMightBeValue()) { |
| 10473 | // Get the receiver from the stack. |
| 10474 | // function, receiver [, arguments] |
| 10475 | Label receiver_is_value, receiver_is_js_object; |
| 10476 | __ ldr(r1, MemOperand(sp, argc_ * kPointerSize)); |
| 10477 | |
| 10478 | // Check if receiver is a smi (which is a number value). |
| 10479 | __ BranchOnSmi(r1, &receiver_is_value); |
| 10480 | |
| 10481 | // Check if the receiver is a valid JS object. |
| 10482 | __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE); |
| 10483 | __ b(ge, &receiver_is_js_object); |
| 10484 | |
| 10485 | // Call the runtime to box the value. |
| 10486 | __ bind(&receiver_is_value); |
| 10487 | __ EnterInternalFrame(); |
| 10488 | __ push(r1); |
| 10489 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS); |
| 10490 | __ LeaveInternalFrame(); |
| 10491 | __ str(r0, MemOperand(sp, argc_ * kPointerSize)); |
| 10492 | |
| 10493 | __ bind(&receiver_is_js_object); |
| 10494 | } |
| 10495 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10496 | // Get the function to call from the stack. |
| 10497 | // function, receiver [, arguments] |
| 10498 | __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize)); |
| 10499 | |
| 10500 | // Check that the function is really a JavaScript function. |
| 10501 | // r1: pushed function (to be verified) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10502 | __ BranchOnSmi(r1, &slow); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10503 | // Get the map of the function object. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10504 | __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10505 | __ b(ne, &slow); |
| 10506 | |
| 10507 | // Fast-case: Invoke the function now. |
| 10508 | // r1: pushed function |
| 10509 | ParameterCount actual(argc_); |
| 10510 | __ InvokeFunction(r1, actual, JUMP_FUNCTION); |
| 10511 | |
| 10512 | // Slow-case: Non-function called. |
| 10513 | __ bind(&slow); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10514 | // CALL_NON_FUNCTION expects the non-function callee as receiver (instead |
| 10515 | // of the original receiver from the call site). |
| 10516 | __ str(r1, MemOperand(sp, argc_ * kPointerSize)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10517 | __ mov(r0, Operand(argc_)); // Setup the number of arguments. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10518 | __ mov(r2, Operand(0)); |
| 10519 | __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION); |
| 10520 | __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)), |
| 10521 | RelocInfo::CODE_TARGET); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 10522 | } |
| 10523 | |
| 10524 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10525 | // Unfortunately you have to run without snapshots to see most of these |
| 10526 | // names in the profile since most compare stubs end up in the snapshot. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 10527 | const char* CompareStub::GetName() { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10528 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 10529 | (lhs_.is(r1) && rhs_.is(r0))); |
| 10530 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10531 | if (name_ != NULL) return name_; |
| 10532 | const int kMaxNameLength = 100; |
| 10533 | name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength); |
| 10534 | if (name_ == NULL) return "OOM"; |
| 10535 | |
| 10536 | const char* cc_name; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 10537 | switch (cc_) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10538 | case lt: cc_name = "LT"; break; |
| 10539 | case gt: cc_name = "GT"; break; |
| 10540 | case le: cc_name = "LE"; break; |
| 10541 | case ge: cc_name = "GE"; break; |
| 10542 | case eq: cc_name = "EQ"; break; |
| 10543 | case ne: cc_name = "NE"; break; |
| 10544 | default: cc_name = "UnknownCondition"; break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 10545 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10546 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10547 | const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1"; |
| 10548 | const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1"; |
| 10549 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10550 | const char* strict_name = ""; |
| 10551 | if (strict_ && (cc_ == eq || cc_ == ne)) { |
| 10552 | strict_name = "_STRICT"; |
| 10553 | } |
| 10554 | |
| 10555 | const char* never_nan_nan_name = ""; |
| 10556 | if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) { |
| 10557 | never_nan_nan_name = "_NO_NAN"; |
| 10558 | } |
| 10559 | |
| 10560 | const char* include_number_compare_name = ""; |
| 10561 | if (!include_number_compare_) { |
| 10562 | include_number_compare_name = "_NO_NUMBER"; |
| 10563 | } |
| 10564 | |
| 10565 | OS::SNPrintF(Vector<char>(name_, kMaxNameLength), |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10566 | "CompareStub_%s%s%s%s%s%s", |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10567 | cc_name, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10568 | lhs_name, |
| 10569 | rhs_name, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10570 | strict_name, |
| 10571 | never_nan_nan_name, |
| 10572 | include_number_compare_name); |
| 10573 | return name_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 10574 | } |
| 10575 | |
| 10576 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10577 | int CompareStub::MinorKey() { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10578 | // Encode the three parameters in a unique 16 bit value. To avoid duplicate |
| 10579 | // stubs the never NaN NaN condition is only taken into account if the |
| 10580 | // condition is equals. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10581 | ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12)); |
| 10582 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 10583 | (lhs_.is(r1) && rhs_.is(r0))); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10584 | return ConditionField::encode(static_cast<unsigned>(cc_) >> 28) |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10585 | | RegisterField::encode(lhs_.is(r0)) |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10586 | | StrictField::encode(strict_) |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 10587 | | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false) |
| 10588 | | IncludeNumberCompareField::encode(include_number_compare_); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10589 | } |
| 10590 | |
| 10591 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10592 | // StringCharCodeAtGenerator |
| 10593 | |
| 10594 | void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) { |
| 10595 | Label flat_string; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10596 | Label ascii_string; |
| 10597 | Label got_char_code; |
| 10598 | |
| 10599 | // If the receiver is a smi trigger the non-string case. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10600 | __ BranchOnSmi(object_, receiver_not_string_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10601 | |
| 10602 | // Fetch the instance type of the receiver into result register. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10603 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10604 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10605 | // If the receiver is not a string trigger the non-string case. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10606 | __ tst(result_, Operand(kIsNotStringMask)); |
| 10607 | __ b(ne, receiver_not_string_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10608 | |
| 10609 | // If the index is non-smi trigger the non-smi case. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10610 | __ BranchOnNotSmi(index_, &index_not_smi_); |
| 10611 | |
| 10612 | // Put smi-tagged index into scratch register. |
| 10613 | __ mov(scratch_, index_); |
| 10614 | __ bind(&got_smi_index_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10615 | |
| 10616 | // Check for index out of range. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10617 | __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset)); |
| 10618 | __ cmp(ip, Operand(scratch_)); |
| 10619 | __ b(ls, index_out_of_range_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10620 | |
| 10621 | // We need special handling for non-flat strings. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10622 | STATIC_ASSERT(kSeqStringTag == 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10623 | __ tst(result_, Operand(kStringRepresentationMask)); |
| 10624 | __ b(eq, &flat_string); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10625 | |
| 10626 | // Handle non-flat strings. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10627 | __ tst(result_, Operand(kIsConsStringMask)); |
| 10628 | __ b(eq, &call_runtime_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10629 | |
| 10630 | // ConsString. |
| 10631 | // Check whether the right hand side is the empty string (i.e. if |
| 10632 | // this is really a flat string in a cons string). If that is not |
| 10633 | // the case we would rather go to the runtime system now to flatten |
| 10634 | // the string. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10635 | __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset)); |
| 10636 | __ LoadRoot(ip, Heap::kEmptyStringRootIndex); |
| 10637 | __ cmp(result_, Operand(ip)); |
| 10638 | __ b(ne, &call_runtime_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10639 | // Get the first of the two strings and load its instance type. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10640 | __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset)); |
| 10641 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10642 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
| 10643 | // If the first cons component is also non-flat, then go to runtime. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10644 | STATIC_ASSERT(kSeqStringTag == 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10645 | __ tst(result_, Operand(kStringRepresentationMask)); |
| 10646 | __ b(nz, &call_runtime_); |
| 10647 | |
| 10648 | // Check for 1-byte or 2-byte string. |
| 10649 | __ bind(&flat_string); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10650 | STATIC_ASSERT(kAsciiStringTag != 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10651 | __ tst(result_, Operand(kStringEncodingMask)); |
| 10652 | __ b(nz, &ascii_string); |
| 10653 | |
| 10654 | // 2-byte string. |
| 10655 | // Load the 2-byte character code into the result register. We can |
| 10656 | // add without shifting since the smi tag size is the log2 of the |
| 10657 | // number of bytes in a two-byte character. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10658 | STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10659 | __ add(scratch_, object_, Operand(scratch_)); |
| 10660 | __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize)); |
| 10661 | __ jmp(&got_char_code); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10662 | |
| 10663 | // ASCII string. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10664 | // Load the byte into the result register. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10665 | __ bind(&ascii_string); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10666 | __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize)); |
| 10667 | __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10668 | |
| 10669 | __ bind(&got_char_code); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10670 | __ mov(result_, Operand(result_, LSL, kSmiTagSize)); |
| 10671 | __ bind(&exit_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10672 | } |
| 10673 | |
| 10674 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10675 | void StringCharCodeAtGenerator::GenerateSlow( |
| 10676 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10677 | __ Abort("Unexpected fallthrough to CharCodeAt slow case"); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10678 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10679 | // Index is not a smi. |
| 10680 | __ bind(&index_not_smi_); |
| 10681 | // If index is a heap number, try converting it to an integer. |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10682 | __ CheckMap(index_, |
| 10683 | scratch_, |
| 10684 | Heap::kHeapNumberMapRootIndex, |
| 10685 | index_not_number_, |
| 10686 | true); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10687 | call_helper.BeforeCall(masm); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 10688 | __ Push(object_, index_); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10689 | __ push(index_); // Consumed by runtime conversion function. |
| 10690 | if (index_flags_ == STRING_INDEX_IS_NUMBER) { |
| 10691 | __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1); |
| 10692 | } else { |
| 10693 | ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX); |
| 10694 | // NumberToSmi discards numbers that are not exact integers. |
| 10695 | __ CallRuntime(Runtime::kNumberToSmi, 1); |
| 10696 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 10697 | // Save the conversion result before the pop instructions below |
| 10698 | // have a chance to overwrite it. |
| 10699 | __ Move(scratch_, r0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10700 | __ pop(index_); |
| 10701 | __ pop(object_); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 10702 | // Reload the instance type. |
| 10703 | __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset)); |
| 10704 | __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10705 | call_helper.AfterCall(masm); |
| 10706 | // If index is still not a smi, it must be out of range. |
| 10707 | __ BranchOnNotSmi(scratch_, index_out_of_range_); |
| 10708 | // Otherwise, return to the fast path. |
| 10709 | __ jmp(&got_smi_index_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10710 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10711 | // Call runtime. We get here when the receiver is a string and the |
| 10712 | // index is a number, but the code of getting the actual character |
| 10713 | // is too complex (e.g., when the string needs to be flattened). |
| 10714 | __ bind(&call_runtime_); |
| 10715 | call_helper.BeforeCall(masm); |
| 10716 | __ Push(object_, index_); |
| 10717 | __ CallRuntime(Runtime::kStringCharCodeAt, 2); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 10718 | __ Move(result_, r0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10719 | call_helper.AfterCall(masm); |
| 10720 | __ jmp(&exit_); |
| 10721 | |
| 10722 | __ Abort("Unexpected fallthrough from CharCodeAt slow case"); |
| 10723 | } |
| 10724 | |
| 10725 | |
| 10726 | // ------------------------------------------------------------------------- |
| 10727 | // StringCharFromCodeGenerator |
| 10728 | |
| 10729 | void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10730 | // Fast case of Heap::LookupSingleCharacterStringFromCode. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10731 | STATIC_ASSERT(kSmiTag == 0); |
| 10732 | STATIC_ASSERT(kSmiShiftSize == 0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10733 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10734 | __ tst(code_, |
| 10735 | Operand(kSmiTagMask | |
| 10736 | ((~String::kMaxAsciiCharCode) << kSmiTagSize))); |
| 10737 | __ b(nz, &slow_case_); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10738 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10739 | __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex); |
| 10740 | // At this point code register contains smi tagged ascii char code. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10741 | STATIC_ASSERT(kSmiTag == 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10742 | __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize)); |
| 10743 | __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize)); |
| 10744 | __ LoadRoot(ip, Heap::kUndefinedValueRootIndex); |
| 10745 | __ cmp(result_, Operand(ip)); |
| 10746 | __ b(eq, &slow_case_); |
| 10747 | __ bind(&exit_); |
| 10748 | } |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10749 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10750 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10751 | void StringCharFromCodeGenerator::GenerateSlow( |
| 10752 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10753 | __ Abort("Unexpected fallthrough to CharFromCode slow case"); |
| 10754 | |
| 10755 | __ bind(&slow_case_); |
| 10756 | call_helper.BeforeCall(masm); |
| 10757 | __ push(code_); |
| 10758 | __ CallRuntime(Runtime::kCharFromCode, 1); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 10759 | __ Move(result_, r0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10760 | call_helper.AfterCall(masm); |
| 10761 | __ jmp(&exit_); |
| 10762 | |
| 10763 | __ Abort("Unexpected fallthrough from CharFromCode slow case"); |
| 10764 | } |
| 10765 | |
| 10766 | |
| 10767 | // ------------------------------------------------------------------------- |
| 10768 | // StringCharAtGenerator |
| 10769 | |
| 10770 | void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) { |
| 10771 | char_code_at_generator_.GenerateFast(masm); |
| 10772 | char_from_code_generator_.GenerateFast(masm); |
| 10773 | } |
| 10774 | |
| 10775 | |
| 10776 | void StringCharAtGenerator::GenerateSlow( |
| 10777 | MacroAssembler* masm, const RuntimeCallHelper& call_helper) { |
| 10778 | char_code_at_generator_.GenerateSlow(masm, call_helper); |
| 10779 | char_from_code_generator_.GenerateSlow(masm, call_helper); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
| 10782 | |
| 10783 | void StringHelper::GenerateCopyCharacters(MacroAssembler* masm, |
| 10784 | Register dest, |
| 10785 | Register src, |
| 10786 | Register count, |
| 10787 | Register scratch, |
| 10788 | bool ascii) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10789 | Label loop; |
| 10790 | Label done; |
| 10791 | // This loop just copies one character at a time, as it is only used for very |
| 10792 | // short strings. |
| 10793 | if (!ascii) { |
| 10794 | __ add(count, count, Operand(count), SetCC); |
| 10795 | } else { |
| 10796 | __ cmp(count, Operand(0)); |
| 10797 | } |
| 10798 | __ b(eq, &done); |
| 10799 | |
| 10800 | __ bind(&loop); |
| 10801 | __ ldrb(scratch, MemOperand(src, 1, PostIndex)); |
| 10802 | // Perform sub between load and dependent store to get the load time to |
| 10803 | // complete. |
| 10804 | __ sub(count, count, Operand(1), SetCC); |
| 10805 | __ strb(scratch, MemOperand(dest, 1, PostIndex)); |
| 10806 | // last iteration. |
| 10807 | __ b(gt, &loop); |
| 10808 | |
| 10809 | __ bind(&done); |
| 10810 | } |
| 10811 | |
| 10812 | |
| 10813 | enum CopyCharactersFlags { |
| 10814 | COPY_ASCII = 1, |
| 10815 | DEST_ALWAYS_ALIGNED = 2 |
| 10816 | }; |
| 10817 | |
| 10818 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10819 | void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm, |
| 10820 | Register dest, |
| 10821 | Register src, |
| 10822 | Register count, |
| 10823 | Register scratch1, |
| 10824 | Register scratch2, |
| 10825 | Register scratch3, |
| 10826 | Register scratch4, |
| 10827 | Register scratch5, |
| 10828 | int flags) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10829 | bool ascii = (flags & COPY_ASCII) != 0; |
| 10830 | bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0; |
| 10831 | |
| 10832 | if (dest_always_aligned && FLAG_debug_code) { |
| 10833 | // Check that destination is actually word aligned if the flag says |
| 10834 | // that it is. |
| 10835 | __ tst(dest, Operand(kPointerAlignmentMask)); |
| 10836 | __ Check(eq, "Destination of copy not aligned."); |
| 10837 | } |
| 10838 | |
| 10839 | const int kReadAlignment = 4; |
| 10840 | const int kReadAlignmentMask = kReadAlignment - 1; |
| 10841 | // Ensure that reading an entire aligned word containing the last character |
| 10842 | // of a string will not read outside the allocated area (because we pad up |
| 10843 | // to kObjectAlignment). |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 10844 | STATIC_ASSERT(kObjectAlignment >= kReadAlignment); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10845 | // Assumes word reads and writes are little endian. |
| 10846 | // Nothing to do for zero characters. |
| 10847 | Label done; |
| 10848 | if (!ascii) { |
| 10849 | __ add(count, count, Operand(count), SetCC); |
| 10850 | } else { |
| 10851 | __ cmp(count, Operand(0)); |
| 10852 | } |
| 10853 | __ b(eq, &done); |
| 10854 | |
| 10855 | // Assume that you cannot read (or write) unaligned. |
| 10856 | Label byte_loop; |
| 10857 | // Must copy at least eight bytes, otherwise just do it one byte at a time. |
| 10858 | __ cmp(count, Operand(8)); |
| 10859 | __ add(count, dest, Operand(count)); |
| 10860 | Register limit = count; // Read until src equals this. |
| 10861 | __ b(lt, &byte_loop); |
| 10862 | |
| 10863 | if (!dest_always_aligned) { |
| 10864 | // Align dest by byte copying. Copies between zero and three bytes. |
| 10865 | __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC); |
| 10866 | Label dest_aligned; |
| 10867 | __ b(eq, &dest_aligned); |
| 10868 | __ cmp(scratch4, Operand(2)); |
| 10869 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex)); |
| 10870 | __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le); |
| 10871 | __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt); |
| 10872 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10873 | __ strb(scratch2, MemOperand(dest, 1, PostIndex), le); |
| 10874 | __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt); |
| 10875 | __ bind(&dest_aligned); |
| 10876 | } |
| 10877 | |
| 10878 | Label simple_loop; |
| 10879 | |
| 10880 | __ sub(scratch4, dest, Operand(src)); |
| 10881 | __ and_(scratch4, scratch4, Operand(0x03), SetCC); |
| 10882 | __ b(eq, &simple_loop); |
| 10883 | // Shift register is number of bits in a source word that |
| 10884 | // must be combined with bits in the next source word in order |
| 10885 | // to create a destination word. |
| 10886 | |
| 10887 | // Complex loop for src/dst that are not aligned the same way. |
| 10888 | { |
| 10889 | Label loop; |
| 10890 | __ mov(scratch4, Operand(scratch4, LSL, 3)); |
| 10891 | Register left_shift = scratch4; |
| 10892 | __ and_(src, src, Operand(~3)); // Round down to load previous word. |
| 10893 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 10894 | // Store the "shift" most significant bits of scratch in the least |
| 10895 | // signficant bits (i.e., shift down by (32-shift)). |
| 10896 | __ rsb(scratch2, left_shift, Operand(32)); |
| 10897 | Register right_shift = scratch2; |
| 10898 | __ mov(scratch1, Operand(scratch1, LSR, right_shift)); |
| 10899 | |
| 10900 | __ bind(&loop); |
| 10901 | __ ldr(scratch3, MemOperand(src, 4, PostIndex)); |
| 10902 | __ sub(scratch5, limit, Operand(dest)); |
| 10903 | __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift)); |
| 10904 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 10905 | __ mov(scratch1, Operand(scratch3, LSR, right_shift)); |
| 10906 | // Loop if four or more bytes left to copy. |
| 10907 | // Compare to eight, because we did the subtract before increasing dst. |
| 10908 | __ sub(scratch5, scratch5, Operand(8), SetCC); |
| 10909 | __ b(ge, &loop); |
| 10910 | } |
| 10911 | // There is now between zero and three bytes left to copy (negative that |
| 10912 | // number is in scratch5), and between one and three bytes already read into |
| 10913 | // scratch1 (eight times that number in scratch4). We may have read past |
| 10914 | // the end of the string, but because objects are aligned, we have not read |
| 10915 | // past the end of the object. |
| 10916 | // Find the minimum of remaining characters to move and preloaded characters |
| 10917 | // and write those as bytes. |
| 10918 | __ add(scratch5, scratch5, Operand(4), SetCC); |
| 10919 | __ b(eq, &done); |
| 10920 | __ cmp(scratch4, Operand(scratch5, LSL, 3), ne); |
| 10921 | // Move minimum of bytes read and bytes left to copy to scratch4. |
| 10922 | __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt); |
| 10923 | // Between one and three (value in scratch5) characters already read into |
| 10924 | // scratch ready to write. |
| 10925 | __ cmp(scratch5, Operand(2)); |
| 10926 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10927 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge); |
| 10928 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge); |
| 10929 | __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt); |
| 10930 | __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt); |
| 10931 | // Copy any remaining bytes. |
| 10932 | __ b(&byte_loop); |
| 10933 | |
| 10934 | // Simple loop. |
| 10935 | // Copy words from src to dst, until less than four bytes left. |
| 10936 | // Both src and dest are word aligned. |
| 10937 | __ bind(&simple_loop); |
| 10938 | { |
| 10939 | Label loop; |
| 10940 | __ bind(&loop); |
| 10941 | __ ldr(scratch1, MemOperand(src, 4, PostIndex)); |
| 10942 | __ sub(scratch3, limit, Operand(dest)); |
| 10943 | __ str(scratch1, MemOperand(dest, 4, PostIndex)); |
| 10944 | // Compare to 8, not 4, because we do the substraction before increasing |
| 10945 | // dest. |
| 10946 | __ cmp(scratch3, Operand(8)); |
| 10947 | __ b(ge, &loop); |
| 10948 | } |
| 10949 | |
| 10950 | // Copy bytes from src to dst until dst hits limit. |
| 10951 | __ bind(&byte_loop); |
| 10952 | __ cmp(dest, Operand(limit)); |
| 10953 | __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt); |
| 10954 | __ b(ge, &done); |
| 10955 | __ strb(scratch1, MemOperand(dest, 1, PostIndex)); |
| 10956 | __ b(&byte_loop); |
| 10957 | |
| 10958 | __ bind(&done); |
| 10959 | } |
| 10960 | |
| 10961 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10962 | void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 10963 | Register c1, |
| 10964 | Register c2, |
| 10965 | Register scratch1, |
| 10966 | Register scratch2, |
| 10967 | Register scratch3, |
| 10968 | Register scratch4, |
| 10969 | Register scratch5, |
| 10970 | Label* not_found) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 10971 | // Register scratch3 is the general scratch register in this function. |
| 10972 | Register scratch = scratch3; |
| 10973 | |
| 10974 | // Make sure that both characters are not digits as such strings has a |
| 10975 | // different hash algorithm. Don't try to look for these in the symbol table. |
| 10976 | Label not_array_index; |
| 10977 | __ sub(scratch, c1, Operand(static_cast<int>('0'))); |
| 10978 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 10979 | __ b(hi, ¬_array_index); |
| 10980 | __ sub(scratch, c2, Operand(static_cast<int>('0'))); |
| 10981 | __ cmp(scratch, Operand(static_cast<int>('9' - '0'))); |
| 10982 | |
| 10983 | // If check failed combine both characters into single halfword. |
| 10984 | // This is required by the contract of the method: code at the |
| 10985 | // not_found branch expects this combination in c1 register |
| 10986 | __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls); |
| 10987 | __ b(ls, not_found); |
| 10988 | |
| 10989 | __ bind(¬_array_index); |
| 10990 | // Calculate the two character string hash. |
| 10991 | Register hash = scratch1; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10992 | StringHelper::GenerateHashInit(masm, hash, c1); |
| 10993 | StringHelper::GenerateHashAddCharacter(masm, hash, c2); |
| 10994 | StringHelper::GenerateHashGetHash(masm, hash); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 10995 | |
| 10996 | // Collect the two characters in a register. |
| 10997 | Register chars = c1; |
| 10998 | __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte)); |
| 10999 | |
| 11000 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 11001 | // hash: hash of two character string. |
| 11002 | |
| 11003 | // Load symbol table |
| 11004 | // Load address of first element of the symbol table. |
| 11005 | Register symbol_table = c2; |
| 11006 | __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex); |
| 11007 | |
| 11008 | // Load undefined value |
| 11009 | Register undefined = scratch4; |
| 11010 | __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex); |
| 11011 | |
| 11012 | // Calculate capacity mask from the symbol table capacity. |
| 11013 | Register mask = scratch2; |
| 11014 | __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset)); |
| 11015 | __ mov(mask, Operand(mask, ASR, 1)); |
| 11016 | __ sub(mask, mask, Operand(1)); |
| 11017 | |
| 11018 | // Calculate untagged address of the first element of the symbol table. |
| 11019 | Register first_symbol_table_element = symbol_table; |
| 11020 | __ add(first_symbol_table_element, symbol_table, |
| 11021 | Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag)); |
| 11022 | |
| 11023 | // Registers |
| 11024 | // chars: two character string, char 1 in byte 0 and char 2 in byte 1. |
| 11025 | // hash: hash of two character string |
| 11026 | // mask: capacity mask |
| 11027 | // first_symbol_table_element: address of the first element of |
| 11028 | // the symbol table |
| 11029 | // scratch: - |
| 11030 | |
| 11031 | // Perform a number of probes in the symbol table. |
| 11032 | static const int kProbes = 4; |
| 11033 | Label found_in_symbol_table; |
| 11034 | Label next_probe[kProbes]; |
| 11035 | for (int i = 0; i < kProbes; i++) { |
| 11036 | Register candidate = scratch5; // Scratch register contains candidate. |
| 11037 | |
| 11038 | // Calculate entry in symbol table. |
| 11039 | if (i > 0) { |
| 11040 | __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i))); |
| 11041 | } else { |
| 11042 | __ mov(candidate, hash); |
| 11043 | } |
| 11044 | |
| 11045 | __ and_(candidate, candidate, Operand(mask)); |
| 11046 | |
| 11047 | // Load the entry from the symble table. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11048 | STATIC_ASSERT(SymbolTable::kEntrySize == 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11049 | __ ldr(candidate, |
| 11050 | MemOperand(first_symbol_table_element, |
| 11051 | candidate, |
| 11052 | LSL, |
| 11053 | kPointerSizeLog2)); |
| 11054 | |
| 11055 | // If entry is undefined no string with this hash can be found. |
| 11056 | __ cmp(candidate, undefined); |
| 11057 | __ b(eq, not_found); |
| 11058 | |
| 11059 | // If length is not 2 the string is not a candidate. |
| 11060 | __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11061 | __ cmp(scratch, Operand(Smi::FromInt(2))); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11062 | __ b(ne, &next_probe[i]); |
| 11063 | |
| 11064 | // Check that the candidate is a non-external ascii string. |
| 11065 | __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset)); |
| 11066 | __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset)); |
| 11067 | __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, |
| 11068 | &next_probe[i]); |
| 11069 | |
| 11070 | // Check if the two characters match. |
| 11071 | // Assumes that word load is little endian. |
| 11072 | __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize)); |
| 11073 | __ cmp(chars, scratch); |
| 11074 | __ b(eq, &found_in_symbol_table); |
| 11075 | __ bind(&next_probe[i]); |
| 11076 | } |
| 11077 | |
| 11078 | // No matching 2 character string found by probing. |
| 11079 | __ jmp(not_found); |
| 11080 | |
| 11081 | // Scratch register contains result when we fall through to here. |
| 11082 | Register result = scratch; |
| 11083 | __ bind(&found_in_symbol_table); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11084 | __ Move(r0, result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11085 | } |
| 11086 | |
| 11087 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11088 | void StringHelper::GenerateHashInit(MacroAssembler* masm, |
| 11089 | Register hash, |
| 11090 | Register character) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11091 | // hash = character + (character << 10); |
| 11092 | __ add(hash, character, Operand(character, LSL, 10)); |
| 11093 | // hash ^= hash >> 6; |
| 11094 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 11095 | } |
| 11096 | |
| 11097 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11098 | void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm, |
| 11099 | Register hash, |
| 11100 | Register character) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11101 | // hash += character; |
| 11102 | __ add(hash, hash, Operand(character)); |
| 11103 | // hash += hash << 10; |
| 11104 | __ add(hash, hash, Operand(hash, LSL, 10)); |
| 11105 | // hash ^= hash >> 6; |
| 11106 | __ eor(hash, hash, Operand(hash, ASR, 6)); |
| 11107 | } |
| 11108 | |
| 11109 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11110 | void StringHelper::GenerateHashGetHash(MacroAssembler* masm, |
| 11111 | Register hash) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11112 | // hash += hash << 3; |
| 11113 | __ add(hash, hash, Operand(hash, LSL, 3)); |
| 11114 | // hash ^= hash >> 11; |
| 11115 | __ eor(hash, hash, Operand(hash, ASR, 11)); |
| 11116 | // hash += hash << 15; |
| 11117 | __ add(hash, hash, Operand(hash, LSL, 15), SetCC); |
| 11118 | |
| 11119 | // if (hash == 0) hash = 27; |
| 11120 | __ mov(hash, Operand(27), LeaveCC, nz); |
| 11121 | } |
| 11122 | |
| 11123 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11124 | void SubStringStub::Generate(MacroAssembler* masm) { |
| 11125 | Label runtime; |
| 11126 | |
| 11127 | // Stack frame on entry. |
| 11128 | // lr: return address |
| 11129 | // sp[0]: to |
| 11130 | // sp[4]: from |
| 11131 | // sp[8]: string |
| 11132 | |
| 11133 | // This stub is called from the native-call %_SubString(...), so |
| 11134 | // nothing can be assumed about the arguments. It is tested that: |
| 11135 | // "string" is a sequential string, |
| 11136 | // both "from" and "to" are smis, and |
| 11137 | // 0 <= from <= to <= string.length. |
| 11138 | // If any of these assumptions fail, we call the runtime system. |
| 11139 | |
| 11140 | static const int kToOffset = 0 * kPointerSize; |
| 11141 | static const int kFromOffset = 1 * kPointerSize; |
| 11142 | static const int kStringOffset = 2 * kPointerSize; |
| 11143 | |
| 11144 | |
| 11145 | // Check bounds and smi-ness. |
| 11146 | __ ldr(r7, MemOperand(sp, kToOffset)); |
| 11147 | __ ldr(r6, MemOperand(sp, kFromOffset)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11148 | STATIC_ASSERT(kSmiTag == 0); |
| 11149 | STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11150 | // I.e., arithmetic shift right by one un-smi-tags. |
| 11151 | __ mov(r2, Operand(r7, ASR, 1), SetCC); |
| 11152 | __ mov(r3, Operand(r6, ASR, 1), SetCC, cc); |
| 11153 | // If either r2 or r6 had the smi tag bit set, then carry is set now. |
| 11154 | __ b(cs, &runtime); // Either "from" or "to" is not a smi. |
| 11155 | __ b(mi, &runtime); // From is negative. |
| 11156 | |
| 11157 | __ sub(r2, r2, Operand(r3), SetCC); |
| 11158 | __ b(mi, &runtime); // Fail if from > to. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11159 | // Special handling of sub-strings of length 1 and 2. One character strings |
| 11160 | // are handled in the runtime system (looked up in the single character |
| 11161 | // cache). Two character strings are looked for in the symbol cache. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11162 | __ cmp(r2, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11163 | __ b(lt, &runtime); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11164 | |
| 11165 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11166 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11167 | // r6: from (smi) |
| 11168 | // r7: to (smi) |
| 11169 | |
| 11170 | // Make sure first argument is a sequential (or flat) string. |
| 11171 | __ ldr(r5, MemOperand(sp, kStringOffset)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11172 | STATIC_ASSERT(kSmiTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11173 | __ tst(r5, Operand(kSmiTagMask)); |
| 11174 | __ b(eq, &runtime); |
| 11175 | Condition is_string = masm->IsObjectStringType(r5, r1); |
| 11176 | __ b(NegateCondition(is_string), &runtime); |
| 11177 | |
| 11178 | // r1: instance type |
| 11179 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11180 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11181 | // r5: string |
| 11182 | // r6: from (smi) |
| 11183 | // r7: to (smi) |
| 11184 | Label seq_string; |
| 11185 | __ and_(r4, r1, Operand(kStringRepresentationMask)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11186 | STATIC_ASSERT(kSeqStringTag < kConsStringTag); |
| 11187 | STATIC_ASSERT(kConsStringTag < kExternalStringTag); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11188 | __ cmp(r4, Operand(kConsStringTag)); |
| 11189 | __ b(gt, &runtime); // External strings go to runtime. |
| 11190 | __ b(lt, &seq_string); // Sequential strings are handled directly. |
| 11191 | |
| 11192 | // Cons string. Try to recurse (once) on the first substring. |
| 11193 | // (This adds a little more generality than necessary to handle flattened |
| 11194 | // cons strings, but not much). |
| 11195 | __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset)); |
| 11196 | __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset)); |
| 11197 | __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11198 | __ tst(r1, Operand(kStringRepresentationMask)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11199 | STATIC_ASSERT(kSeqStringTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11200 | __ b(ne, &runtime); // Cons and External strings go to runtime. |
| 11201 | |
| 11202 | // Definitly a sequential string. |
| 11203 | __ bind(&seq_string); |
| 11204 | |
| 11205 | // r1: instance type. |
| 11206 | // r2: length |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11207 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11208 | // r5: string |
| 11209 | // r6: from (smi) |
| 11210 | // r7: to (smi) |
| 11211 | __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11212 | __ cmp(r4, Operand(r7)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11213 | __ b(lt, &runtime); // Fail if to > length. |
| 11214 | |
| 11215 | // r1: instance type. |
| 11216 | // r2: result string length. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11217 | // r3: from index (untaged smi) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11218 | // r5: string. |
| 11219 | // r6: from offset (smi) |
| 11220 | // Check for flat ascii string. |
| 11221 | Label non_ascii_flat; |
| 11222 | __ tst(r1, Operand(kStringEncodingMask)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11223 | STATIC_ASSERT(kTwoByteStringTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11224 | __ b(eq, &non_ascii_flat); |
| 11225 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11226 | Label result_longer_than_two; |
| 11227 | __ cmp(r2, Operand(2)); |
| 11228 | __ b(gt, &result_longer_than_two); |
| 11229 | |
| 11230 | // Sub string of length 2 requested. |
| 11231 | // Get the two characters forming the sub string. |
| 11232 | __ add(r5, r5, Operand(r3)); |
| 11233 | __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize)); |
| 11234 | __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1)); |
| 11235 | |
| 11236 | // Try to lookup two character string in symbol table. |
| 11237 | Label make_two_character_string; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11238 | StringHelper::GenerateTwoCharacterSymbolTableProbe( |
| 11239 | masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11240 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11241 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11242 | __ Ret(); |
| 11243 | |
| 11244 | // r2: result string length. |
| 11245 | // r3: two characters combined into halfword in little endian byte order. |
| 11246 | __ bind(&make_two_character_string); |
| 11247 | __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime); |
| 11248 | __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11249 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11250 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11251 | __ Ret(); |
| 11252 | |
| 11253 | __ bind(&result_longer_than_two); |
| 11254 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11255 | // Allocate the result. |
| 11256 | __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime); |
| 11257 | |
| 11258 | // r0: result string. |
| 11259 | // r2: result string length. |
| 11260 | // r5: string. |
| 11261 | // r6: from offset (smi) |
| 11262 | // Locate first character of result. |
| 11263 | __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11264 | // Locate 'from' character of string. |
| 11265 | __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11266 | __ add(r5, r5, Operand(r6, ASR, 1)); |
| 11267 | |
| 11268 | // r0: result string. |
| 11269 | // r1: first character of result string. |
| 11270 | // r2: result string length. |
| 11271 | // r5: first character of sub string to copy. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11272 | STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11273 | StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 11274 | COPY_ASCII | DEST_ALWAYS_ALIGNED); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11275 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11276 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11277 | __ Ret(); |
| 11278 | |
| 11279 | __ bind(&non_ascii_flat); |
| 11280 | // r2: result string length. |
| 11281 | // r5: string. |
| 11282 | // r6: from offset (smi) |
| 11283 | // Check for flat two byte string. |
| 11284 | |
| 11285 | // Allocate the result. |
| 11286 | __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime); |
| 11287 | |
| 11288 | // r0: result string. |
| 11289 | // r2: result string length. |
| 11290 | // r5: string. |
| 11291 | // Locate first character of result. |
| 11292 | __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11293 | // Locate 'from' character of string. |
| 11294 | __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11295 | // As "from" is a smi it is 2 times the value which matches the size of a two |
| 11296 | // byte character. |
| 11297 | __ add(r5, r5, Operand(r6)); |
| 11298 | |
| 11299 | // r0: result string. |
| 11300 | // r1: first character of result. |
| 11301 | // r2: result length. |
| 11302 | // r5: first character of string to copy. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11303 | STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11304 | StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9, |
| 11305 | DEST_ALWAYS_ALIGNED); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11306 | __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4); |
| 11307 | __ add(sp, sp, Operand(3 * kPointerSize)); |
| 11308 | __ Ret(); |
| 11309 | |
| 11310 | // Just jump to runtime to create the sub string. |
| 11311 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11312 | __ TailCallRuntime(Runtime::kSubString, 3, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11313 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11314 | |
| 11315 | |
| 11316 | void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 11317 | Register left, |
| 11318 | Register right, |
| 11319 | Register scratch1, |
| 11320 | Register scratch2, |
| 11321 | Register scratch3, |
| 11322 | Register scratch4) { |
| 11323 | Label compare_lengths; |
| 11324 | // Find minimum length and length difference. |
| 11325 | __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset)); |
| 11326 | __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset)); |
| 11327 | __ sub(scratch3, scratch1, Operand(scratch2), SetCC); |
| 11328 | Register length_delta = scratch3; |
| 11329 | __ mov(scratch1, scratch2, LeaveCC, gt); |
| 11330 | Register min_length = scratch1; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11331 | STATIC_ASSERT(kSmiTag == 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11332 | __ tst(min_length, Operand(min_length)); |
| 11333 | __ b(eq, &compare_lengths); |
| 11334 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11335 | // Untag smi. |
| 11336 | __ mov(min_length, Operand(min_length, ASR, kSmiTagSize)); |
| 11337 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11338 | // Setup registers so that we only need to increment one register |
| 11339 | // in the loop. |
| 11340 | __ add(scratch2, min_length, |
| 11341 | Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11342 | __ add(left, left, Operand(scratch2)); |
| 11343 | __ add(right, right, Operand(scratch2)); |
| 11344 | // Registers left and right points to the min_length character of strings. |
| 11345 | __ rsb(min_length, min_length, Operand(-1)); |
| 11346 | Register index = min_length; |
| 11347 | // Index starts at -min_length. |
| 11348 | |
| 11349 | { |
| 11350 | // Compare loop. |
| 11351 | Label loop; |
| 11352 | __ bind(&loop); |
| 11353 | // Compare characters. |
| 11354 | __ add(index, index, Operand(1), SetCC); |
| 11355 | __ ldrb(scratch2, MemOperand(left, index), ne); |
| 11356 | __ ldrb(scratch4, MemOperand(right, index), ne); |
| 11357 | // Skip to compare lengths with eq condition true. |
| 11358 | __ b(eq, &compare_lengths); |
| 11359 | __ cmp(scratch2, scratch4); |
| 11360 | __ b(eq, &loop); |
| 11361 | // Fallthrough with eq condition false. |
| 11362 | } |
| 11363 | // Compare lengths - strings up to min-length are equal. |
| 11364 | __ bind(&compare_lengths); |
| 11365 | ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0)); |
| 11366 | // Use zero length_delta as result. |
| 11367 | __ mov(r0, Operand(length_delta), SetCC, eq); |
| 11368 | // Fall through to here if characters compare not-equal. |
| 11369 | __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt); |
| 11370 | __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt); |
| 11371 | __ Ret(); |
| 11372 | } |
| 11373 | |
| 11374 | |
| 11375 | void StringCompareStub::Generate(MacroAssembler* masm) { |
| 11376 | Label runtime; |
| 11377 | |
| 11378 | // Stack frame on entry. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11379 | // sp[0]: right string |
| 11380 | // sp[4]: left string |
| 11381 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left |
| 11382 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11383 | |
| 11384 | Label not_same; |
| 11385 | __ cmp(r0, r1); |
| 11386 | __ b(ne, ¬_same); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11387 | STATIC_ASSERT(EQUAL == 0); |
| 11388 | STATIC_ASSERT(kSmiTag == 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11389 | __ mov(r0, Operand(Smi::FromInt(EQUAL))); |
| 11390 | __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2); |
| 11391 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11392 | __ Ret(); |
| 11393 | |
| 11394 | __ bind(¬_same); |
| 11395 | |
| 11396 | // Check that both objects are sequential ascii strings. |
| 11397 | __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime); |
| 11398 | |
| 11399 | // Compare flat ascii strings natively. Remove arguments from stack first. |
| 11400 | __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3); |
| 11401 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11402 | GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5); |
| 11403 | |
| 11404 | // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater) |
| 11405 | // tagged as a small integer. |
| 11406 | __ bind(&runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11407 | __ TailCallRuntime(Runtime::kStringCompare, 2, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11408 | } |
| 11409 | |
| 11410 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11411 | void StringAddStub::Generate(MacroAssembler* masm) { |
| 11412 | Label string_add_runtime; |
| 11413 | // Stack on entry: |
| 11414 | // sp[0]: second argument. |
| 11415 | // sp[4]: first argument. |
| 11416 | |
| 11417 | // Load the two arguments. |
| 11418 | __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument. |
| 11419 | __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument. |
| 11420 | |
| 11421 | // Make sure that both arguments are strings if not known in advance. |
| 11422 | if (string_check_) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11423 | STATIC_ASSERT(kSmiTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11424 | __ JumpIfEitherSmi(r0, r1, &string_add_runtime); |
| 11425 | // Load instance types. |
| 11426 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11427 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11428 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11429 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11430 | STATIC_ASSERT(kStringTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11431 | // If either is not a string, go to runtime. |
| 11432 | __ tst(r4, Operand(kIsNotStringMask)); |
| 11433 | __ tst(r5, Operand(kIsNotStringMask), eq); |
| 11434 | __ b(ne, &string_add_runtime); |
| 11435 | } |
| 11436 | |
| 11437 | // Both arguments are strings. |
| 11438 | // r0: first string |
| 11439 | // r1: second string |
| 11440 | // r4: first string instance type (if string_check_) |
| 11441 | // r5: second string instance type (if string_check_) |
| 11442 | { |
| 11443 | Label strings_not_empty; |
| 11444 | // Check if either of the strings are empty. In that case return the other. |
| 11445 | __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset)); |
| 11446 | __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11447 | STATIC_ASSERT(kSmiTag == 0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11448 | __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11449 | __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11450 | STATIC_ASSERT(kSmiTag == 0); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11451 | // Else test if second string is empty. |
| 11452 | __ cmp(r3, Operand(Smi::FromInt(0)), ne); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11453 | __ b(ne, &strings_not_empty); // If either string was empty, return r0. |
| 11454 | |
| 11455 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11456 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11457 | __ Ret(); |
| 11458 | |
| 11459 | __ bind(&strings_not_empty); |
| 11460 | } |
| 11461 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11462 | __ mov(r2, Operand(r2, ASR, kSmiTagSize)); |
| 11463 | __ mov(r3, Operand(r3, ASR, kSmiTagSize)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11464 | // Both strings are non-empty. |
| 11465 | // r0: first string |
| 11466 | // r1: second string |
| 11467 | // r2: length of first string |
| 11468 | // r3: length of second string |
| 11469 | // r4: first string instance type (if string_check_) |
| 11470 | // r5: second string instance type (if string_check_) |
| 11471 | // Look at the length of the result of adding the two strings. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11472 | Label string_add_flat_result, longer_than_two; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11473 | // Adding two lengths can't overflow. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11474 | STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11475 | __ add(r6, r2, Operand(r3)); |
| 11476 | // Use the runtime system when adding two one character strings, as it |
| 11477 | // contains optimizations for this specific case using the symbol table. |
| 11478 | __ cmp(r6, Operand(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11479 | __ b(ne, &longer_than_two); |
| 11480 | |
| 11481 | // Check that both strings are non-external ascii strings. |
| 11482 | if (!string_check_) { |
| 11483 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11484 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11485 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11486 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11487 | } |
| 11488 | __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7, |
| 11489 | &string_add_runtime); |
| 11490 | |
| 11491 | // Get the two characters forming the sub string. |
| 11492 | __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11493 | __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize)); |
| 11494 | |
| 11495 | // Try to lookup two character string in symbol table. If it is not found |
| 11496 | // just allocate a new one. |
| 11497 | Label make_two_character_string; |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11498 | StringHelper::GenerateTwoCharacterSymbolTableProbe( |
| 11499 | masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11500 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11501 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11502 | __ Ret(); |
| 11503 | |
| 11504 | __ bind(&make_two_character_string); |
| 11505 | // Resulting string has length 2 and first chars of two strings |
| 11506 | // are combined into single halfword in r2 register. |
| 11507 | // So we can fill resulting string without two loops by a single |
| 11508 | // halfword store instruction (which assumes that processor is |
| 11509 | // in a little endian mode) |
| 11510 | __ mov(r6, Operand(2)); |
| 11511 | __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime); |
| 11512 | __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize)); |
| 11513 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11514 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11515 | __ Ret(); |
| 11516 | |
| 11517 | __ bind(&longer_than_two); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11518 | // Check if resulting string will be flat. |
| 11519 | __ cmp(r6, Operand(String::kMinNonFlatLength)); |
| 11520 | __ b(lt, &string_add_flat_result); |
| 11521 | // Handle exceptionally long strings in the runtime system. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11522 | STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11523 | ASSERT(IsPowerOf2(String::kMaxLength + 1)); |
| 11524 | // kMaxLength + 1 is representable as shifted literal, kMaxLength is not. |
| 11525 | __ cmp(r6, Operand(String::kMaxLength + 1)); |
| 11526 | __ b(hs, &string_add_runtime); |
| 11527 | |
| 11528 | // If result is not supposed to be flat, allocate a cons string object. |
| 11529 | // If both strings are ascii the result is an ascii cons string. |
| 11530 | if (!string_check_) { |
| 11531 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11532 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11533 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11534 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11535 | } |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11536 | Label non_ascii, allocated, ascii_data; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11537 | STATIC_ASSERT(kTwoByteStringTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11538 | __ tst(r4, Operand(kStringEncodingMask)); |
| 11539 | __ tst(r5, Operand(kStringEncodingMask), ne); |
| 11540 | __ b(eq, &non_ascii); |
| 11541 | |
| 11542 | // Allocate an ASCII cons string. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11543 | __ bind(&ascii_data); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11544 | __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime); |
| 11545 | __ bind(&allocated); |
| 11546 | // Fill the fields of the cons string. |
| 11547 | __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset)); |
| 11548 | __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset)); |
| 11549 | __ mov(r0, Operand(r7)); |
| 11550 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11551 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11552 | __ Ret(); |
| 11553 | |
| 11554 | __ bind(&non_ascii); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11555 | // At least one of the strings is two-byte. Check whether it happens |
| 11556 | // to contain only ascii characters. |
| 11557 | // r4: first instance type. |
| 11558 | // r5: second instance type. |
| 11559 | __ tst(r4, Operand(kAsciiDataHintMask)); |
| 11560 | __ tst(r5, Operand(kAsciiDataHintMask), ne); |
| 11561 | __ b(ne, &ascii_data); |
| 11562 | __ eor(r4, r4, Operand(r5)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11563 | STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11564 | __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag)); |
| 11565 | __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag)); |
| 11566 | __ b(eq, &ascii_data); |
| 11567 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11568 | // Allocate a two byte cons string. |
| 11569 | __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime); |
| 11570 | __ jmp(&allocated); |
| 11571 | |
| 11572 | // Handle creating a flat result. First check that both strings are |
| 11573 | // sequential and that they have the same encoding. |
| 11574 | // r0: first string |
| 11575 | // r1: second string |
| 11576 | // r2: length of first string |
| 11577 | // r3: length of second string |
| 11578 | // r4: first string instance type (if string_check_) |
| 11579 | // r5: second string instance type (if string_check_) |
| 11580 | // r6: sum of lengths. |
| 11581 | __ bind(&string_add_flat_result); |
| 11582 | if (!string_check_) { |
| 11583 | __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset)); |
| 11584 | __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset)); |
| 11585 | __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset)); |
| 11586 | __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset)); |
| 11587 | } |
| 11588 | // Check that both strings are sequential. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 11589 | STATIC_ASSERT(kSeqStringTag == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11590 | __ tst(r4, Operand(kStringRepresentationMask)); |
| 11591 | __ tst(r5, Operand(kStringRepresentationMask), eq); |
| 11592 | __ b(ne, &string_add_runtime); |
| 11593 | // Now check if both strings have the same encoding (ASCII/Two-byte). |
| 11594 | // r0: first string. |
| 11595 | // r1: second string. |
| 11596 | // r2: length of first string. |
| 11597 | // r3: length of second string. |
| 11598 | // r6: sum of lengths.. |
| 11599 | Label non_ascii_string_add_flat_result; |
| 11600 | ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test. |
| 11601 | __ eor(r7, r4, Operand(r5)); |
| 11602 | __ tst(r7, Operand(kStringEncodingMask)); |
| 11603 | __ b(ne, &string_add_runtime); |
| 11604 | // And see if it's ASCII or two-byte. |
| 11605 | __ tst(r4, Operand(kStringEncodingMask)); |
| 11606 | __ b(eq, &non_ascii_string_add_flat_result); |
| 11607 | |
| 11608 | // Both strings are sequential ASCII strings. We also know that they are |
| 11609 | // short (since the sum of the lengths is less than kMinNonFlatLength). |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11610 | // r6: length of resulting flat string |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11611 | __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 11612 | // Locate first character of result. |
| 11613 | __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11614 | // Locate first character of first argument. |
| 11615 | __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11616 | // r0: first character of first string. |
| 11617 | // r1: second string. |
| 11618 | // r2: length of first string. |
| 11619 | // r3: length of second string. |
| 11620 | // r6: first character of result. |
| 11621 | // r7: result string. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11622 | StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11623 | |
| 11624 | // Load second argument and locate first character. |
| 11625 | __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag)); |
| 11626 | // r1: first character of second string. |
| 11627 | // r3: length of second string. |
| 11628 | // r6: next character of result. |
| 11629 | // r7: result string. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11630 | StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11631 | __ mov(r0, Operand(r7)); |
| 11632 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11633 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11634 | __ Ret(); |
| 11635 | |
| 11636 | __ bind(&non_ascii_string_add_flat_result); |
| 11637 | // Both strings are sequential two byte strings. |
| 11638 | // r0: first string. |
| 11639 | // r1: second string. |
| 11640 | // r2: length of first string. |
| 11641 | // r3: length of second string. |
| 11642 | // r6: sum of length of strings. |
| 11643 | __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime); |
| 11644 | // r0: first string. |
| 11645 | // r1: second string. |
| 11646 | // r2: length of first string. |
| 11647 | // r3: length of second string. |
| 11648 | // r7: result string. |
| 11649 | |
| 11650 | // Locate first character of result. |
| 11651 | __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11652 | // Locate first character of first argument. |
| 11653 | __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11654 | |
| 11655 | // r0: first character of first string. |
| 11656 | // r1: second string. |
| 11657 | // r2: length of first string. |
| 11658 | // r3: length of second string. |
| 11659 | // r6: first character of result. |
| 11660 | // r7: result string. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11661 | StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11662 | |
| 11663 | // Locate first character of second argument. |
| 11664 | __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag)); |
| 11665 | |
| 11666 | // r1: first character of second string. |
| 11667 | // r3: length of second string. |
| 11668 | // r6: next character of result (after copy of first string). |
| 11669 | // r7: result string. |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11670 | StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11671 | |
| 11672 | __ mov(r0, Operand(r7)); |
| 11673 | __ IncrementCounter(&Counters::string_add_native, 1, r2, r3); |
| 11674 | __ add(sp, sp, Operand(2 * kPointerSize)); |
| 11675 | __ Ret(); |
| 11676 | |
| 11677 | // Just jump to runtime to add the two strings. |
| 11678 | __ bind(&string_add_runtime); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11679 | __ TailCallRuntime(Runtime::kStringAdd, 2, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11680 | } |
| 11681 | |
| 11682 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 11683 | #undef __ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11684 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11685 | } } // namespace v8::internal |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 11686 | |
| 11687 | #endif // V8_TARGET_ARCH_ARM |