sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +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 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 28 | #include "v8.h" |
| 29 | |
| 30 | #if defined(V8_TARGET_ARCH_IA32) |
| 31 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "ia32/lithium-codegen-ia32.h" |
| 33 | #include "code-stubs.h" |
| 34 | #include "stub-cache.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 40 | // When invoking builtins, we need to record the safepoint in the middle of |
| 41 | // the invoke instruction sequence generated by the macro assembler. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | class SafepointGenerator : public PostCallGenerator { |
| 43 | public: |
| 44 | SafepointGenerator(LCodeGen* codegen, |
| 45 | LPointerMap* pointers, |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 46 | int deoptimization_index, |
| 47 | bool ensure_reloc_space = false) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 48 | : codegen_(codegen), |
| 49 | pointers_(pointers), |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 50 | deoptimization_index_(deoptimization_index), |
| 51 | ensure_reloc_space_(ensure_reloc_space) { } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 52 | virtual ~SafepointGenerator() { } |
| 53 | |
| 54 | virtual void Generate() { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 55 | // Ensure that we have enough space in the reloc info to patch |
| 56 | // this with calls when doing deoptimization. |
| 57 | if (ensure_reloc_space_) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 58 | codegen_->EnsureRelocSpaceForDeoptimization(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 59 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 60 | codegen_->RecordSafepoint(pointers_, deoptimization_index_); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | LCodeGen* codegen_; |
| 65 | LPointerMap* pointers_; |
| 66 | int deoptimization_index_; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 67 | bool ensure_reloc_space_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | |
| 71 | #define __ masm()-> |
| 72 | |
| 73 | bool LCodeGen::GenerateCode() { |
| 74 | HPhase phase("Code generation", chunk()); |
| 75 | ASSERT(is_unused()); |
| 76 | status_ = GENERATING; |
| 77 | CpuFeatures::Scope scope(SSE2); |
| 78 | return GeneratePrologue() && |
| 79 | GenerateBody() && |
| 80 | GenerateDeferredCode() && |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 81 | GenerateRelocPadding() && |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 82 | GenerateSafepointTable(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void LCodeGen::FinishCode(Handle<Code> code) { |
| 87 | ASSERT(is_done()); |
| 88 | code->set_stack_slots(StackSlotCount()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 89 | code->set_safepoint_table_offset(safepoints_.GetCodeOffset()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 90 | PopulateDeoptimizationData(code); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void LCodeGen::Abort(const char* format, ...) { |
| 95 | if (FLAG_trace_bailout) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 96 | SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString()); |
| 97 | PrintF("Aborting LCodeGen in @\"%s\": ", *name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 98 | va_list arguments; |
| 99 | va_start(arguments, format); |
| 100 | OS::VPrint(format, arguments); |
| 101 | va_end(arguments); |
| 102 | PrintF("\n"); |
| 103 | } |
| 104 | status_ = ABORTED; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void LCodeGen::Comment(const char* format, ...) { |
| 109 | if (!FLAG_code_comments) return; |
| 110 | char buffer[4 * KB]; |
| 111 | StringBuilder builder(buffer, ARRAY_SIZE(buffer)); |
| 112 | va_list arguments; |
| 113 | va_start(arguments, format); |
| 114 | builder.AddFormattedList(format, arguments); |
| 115 | va_end(arguments); |
| 116 | |
| 117 | // Copy the string before recording it in the assembler to avoid |
| 118 | // issues when the stack allocated buffer goes out of scope. |
| 119 | size_t length = builder.position(); |
| 120 | Vector<char> copy = Vector<char>::New(length + 1); |
| 121 | memcpy(copy.start(), builder.Finalize(), copy.length()); |
| 122 | masm()->RecordComment(copy.start()); |
| 123 | } |
| 124 | |
| 125 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 126 | bool LCodeGen::GenerateRelocPadding() { |
| 127 | int reloc_size = masm()->relocation_writer_size(); |
| 128 | while (reloc_size < deoptimization_reloc_size.min_size) { |
| 129 | __ RecordComment(RelocInfo::kFillerCommentString, true); |
ricow@chromium.org | 2233451 | 2011-02-25 07:28:50 +0000 | [diff] [blame] | 130 | reloc_size += RelocInfo::kMinRelocCommentSize; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 131 | } |
| 132 | return !is_aborted(); |
| 133 | } |
| 134 | |
| 135 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 136 | bool LCodeGen::GeneratePrologue() { |
| 137 | ASSERT(is_generating()); |
| 138 | |
| 139 | #ifdef DEBUG |
| 140 | if (strlen(FLAG_stop_at) > 0 && |
| 141 | info_->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
| 142 | __ int3(); |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | __ push(ebp); // Caller's frame pointer. |
| 147 | __ mov(ebp, esp); |
| 148 | __ push(esi); // Callee's context. |
| 149 | __ push(edi); // Callee's JS function. |
| 150 | |
| 151 | // Reserve space for the stack slots needed by the code. |
| 152 | int slots = StackSlotCount(); |
| 153 | if (slots > 0) { |
| 154 | if (FLAG_debug_code) { |
| 155 | __ mov(Operand(eax), Immediate(slots)); |
| 156 | Label loop; |
| 157 | __ bind(&loop); |
| 158 | __ push(Immediate(kSlotsZapValue)); |
| 159 | __ dec(eax); |
| 160 | __ j(not_zero, &loop); |
| 161 | } else { |
| 162 | __ sub(Operand(esp), Immediate(slots * kPointerSize)); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 163 | #ifdef _MSC_VER |
| 164 | // On windows, you may not access the stack more than one page below |
| 165 | // the most recently mapped page. To make the allocated area randomly |
| 166 | // accessible, we write to each page in turn (the value is irrelevant). |
| 167 | const int kPageSize = 4 * KB; |
| 168 | for (int offset = slots * kPointerSize - kPageSize; |
| 169 | offset > 0; |
| 170 | offset -= kPageSize) { |
| 171 | __ mov(Operand(esp, offset), eax); |
| 172 | } |
| 173 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 177 | // Possibly allocate a local context. |
| 178 | int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS; |
| 179 | if (heap_slots > 0) { |
| 180 | Comment(";;; Allocate local context"); |
| 181 | // Argument to NewContext is the function, which is still in edi. |
| 182 | __ push(edi); |
| 183 | if (heap_slots <= FastNewContextStub::kMaximumSlots) { |
| 184 | FastNewContextStub stub(heap_slots); |
| 185 | __ CallStub(&stub); |
| 186 | } else { |
| 187 | __ CallRuntime(Runtime::kNewContext, 1); |
| 188 | } |
| 189 | RecordSafepoint(Safepoint::kNoDeoptimizationIndex); |
| 190 | // Context is returned in both eax and esi. It replaces the context |
| 191 | // passed to us. It's saved in the stack and kept live in esi. |
| 192 | __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi); |
| 193 | |
| 194 | // Copy parameters into context if necessary. |
| 195 | int num_parameters = scope()->num_parameters(); |
| 196 | for (int i = 0; i < num_parameters; i++) { |
| 197 | Slot* slot = scope()->parameter(i)->AsSlot(); |
| 198 | if (slot != NULL && slot->type() == Slot::CONTEXT) { |
| 199 | int parameter_offset = StandardFrameConstants::kCallerSPOffset + |
| 200 | (num_parameters - 1 - i) * kPointerSize; |
| 201 | // Load parameter from stack. |
| 202 | __ mov(eax, Operand(ebp, parameter_offset)); |
| 203 | // Store it in the context. |
| 204 | int context_offset = Context::SlotOffset(slot->index()); |
| 205 | __ mov(Operand(esi, context_offset), eax); |
| 206 | // Update the write barrier. This clobbers all involved |
| 207 | // registers, so we have to use a third register to avoid |
| 208 | // clobbering esi. |
| 209 | __ mov(ecx, esi); |
| 210 | __ RecordWrite(ecx, context_offset, eax, ebx); |
| 211 | } |
| 212 | } |
| 213 | Comment(";;; End allocate local context"); |
| 214 | } |
| 215 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 216 | // Trace the call. |
| 217 | if (FLAG_trace) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 218 | // We have not executed any compiled code yet, so esi still holds the |
| 219 | // incoming context. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 220 | __ CallRuntime(Runtime::kTraceEnter, 0); |
| 221 | } |
| 222 | return !is_aborted(); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | bool LCodeGen::GenerateBody() { |
| 227 | ASSERT(is_generating()); |
| 228 | bool emit_instructions = true; |
| 229 | for (current_instruction_ = 0; |
| 230 | !is_aborted() && current_instruction_ < instructions_->length(); |
| 231 | current_instruction_++) { |
| 232 | LInstruction* instr = instructions_->at(current_instruction_); |
| 233 | if (instr->IsLabel()) { |
| 234 | LLabel* label = LLabel::cast(instr); |
| 235 | emit_instructions = !label->HasReplacement(); |
| 236 | } |
| 237 | |
| 238 | if (emit_instructions) { |
| 239 | Comment(";;; @%d: %s.", current_instruction_, instr->Mnemonic()); |
| 240 | instr->CompileToNative(this); |
| 241 | } |
| 242 | } |
| 243 | return !is_aborted(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | LInstruction* LCodeGen::GetNextInstruction() { |
| 248 | if (current_instruction_ < instructions_->length() - 1) { |
| 249 | return instructions_->at(current_instruction_ + 1); |
| 250 | } else { |
| 251 | return NULL; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | bool LCodeGen::GenerateDeferredCode() { |
| 257 | ASSERT(is_generating()); |
| 258 | for (int i = 0; !is_aborted() && i < deferred_.length(); i++) { |
| 259 | LDeferredCode* code = deferred_[i]; |
| 260 | __ bind(code->entry()); |
| 261 | code->Generate(); |
| 262 | __ jmp(code->exit()); |
| 263 | } |
| 264 | |
| 265 | // Deferred code is the last part of the instruction sequence. Mark |
| 266 | // the generated code as done unless we bailed out. |
| 267 | if (!is_aborted()) status_ = DONE; |
| 268 | return !is_aborted(); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | bool LCodeGen::GenerateSafepointTable() { |
| 273 | ASSERT(is_done()); |
| 274 | safepoints_.Emit(masm(), StackSlotCount()); |
| 275 | return !is_aborted(); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | Register LCodeGen::ToRegister(int index) const { |
| 280 | return Register::FromAllocationIndex(index); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | XMMRegister LCodeGen::ToDoubleRegister(int index) const { |
| 285 | return XMMRegister::FromAllocationIndex(index); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | Register LCodeGen::ToRegister(LOperand* op) const { |
| 290 | ASSERT(op->IsRegister()); |
| 291 | return ToRegister(op->index()); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const { |
| 296 | ASSERT(op->IsDoubleRegister()); |
| 297 | return ToDoubleRegister(op->index()); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | int LCodeGen::ToInteger32(LConstantOperand* op) const { |
| 302 | Handle<Object> value = chunk_->LookupLiteral(op); |
| 303 | ASSERT(chunk_->LookupLiteralRepresentation(op).IsInteger32()); |
| 304 | ASSERT(static_cast<double>(static_cast<int32_t>(value->Number())) == |
| 305 | value->Number()); |
| 306 | return static_cast<int32_t>(value->Number()); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | Immediate LCodeGen::ToImmediate(LOperand* op) { |
| 311 | LConstantOperand* const_op = LConstantOperand::cast(op); |
| 312 | Handle<Object> literal = chunk_->LookupLiteral(const_op); |
| 313 | Representation r = chunk_->LookupLiteralRepresentation(const_op); |
| 314 | if (r.IsInteger32()) { |
| 315 | ASSERT(literal->IsNumber()); |
| 316 | return Immediate(static_cast<int32_t>(literal->Number())); |
| 317 | } else if (r.IsDouble()) { |
| 318 | Abort("unsupported double immediate"); |
| 319 | } |
| 320 | ASSERT(r.IsTagged()); |
| 321 | return Immediate(literal); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | Operand LCodeGen::ToOperand(LOperand* op) const { |
| 326 | if (op->IsRegister()) return Operand(ToRegister(op)); |
| 327 | if (op->IsDoubleRegister()) return Operand(ToDoubleRegister(op)); |
| 328 | ASSERT(op->IsStackSlot() || op->IsDoubleStackSlot()); |
| 329 | int index = op->index(); |
| 330 | if (index >= 0) { |
| 331 | // Local or spill slot. Skip the frame pointer, function, and |
| 332 | // context in the fixed part of the frame. |
| 333 | return Operand(ebp, -(index + 3) * kPointerSize); |
| 334 | } else { |
| 335 | // Incoming parameter. Skip the return address. |
| 336 | return Operand(ebp, -(index - 1) * kPointerSize); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 341 | Operand LCodeGen::HighOperand(LOperand* op) { |
| 342 | ASSERT(op->IsDoubleStackSlot()); |
| 343 | int index = op->index(); |
| 344 | int offset = (index >= 0) ? index + 3 : index - 1; |
| 345 | return Operand(ebp, -offset * kPointerSize); |
| 346 | } |
| 347 | |
| 348 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 349 | void LCodeGen::WriteTranslation(LEnvironment* environment, |
| 350 | Translation* translation) { |
| 351 | if (environment == NULL) return; |
| 352 | |
| 353 | // The translation includes one command per value in the environment. |
| 354 | int translation_size = environment->values()->length(); |
| 355 | // The output frame height does not include the parameters. |
| 356 | int height = translation_size - environment->parameter_count(); |
| 357 | |
| 358 | WriteTranslation(environment->outer(), translation); |
| 359 | int closure_id = DefineDeoptimizationLiteral(environment->closure()); |
| 360 | translation->BeginFrame(environment->ast_id(), closure_id, height); |
| 361 | for (int i = 0; i < translation_size; ++i) { |
| 362 | LOperand* value = environment->values()->at(i); |
| 363 | // spilled_registers_ and spilled_double_registers_ are either |
| 364 | // both NULL or both set. |
| 365 | if (environment->spilled_registers() != NULL && value != NULL) { |
| 366 | if (value->IsRegister() && |
| 367 | environment->spilled_registers()[value->index()] != NULL) { |
| 368 | translation->MarkDuplicate(); |
| 369 | AddToTranslation(translation, |
| 370 | environment->spilled_registers()[value->index()], |
| 371 | environment->HasTaggedValueAt(i)); |
| 372 | } else if ( |
| 373 | value->IsDoubleRegister() && |
| 374 | environment->spilled_double_registers()[value->index()] != NULL) { |
| 375 | translation->MarkDuplicate(); |
| 376 | AddToTranslation( |
| 377 | translation, |
| 378 | environment->spilled_double_registers()[value->index()], |
| 379 | false); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | AddToTranslation(translation, value, environment->HasTaggedValueAt(i)); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 388 | void LCodeGen::EnsureRelocSpaceForDeoptimization() { |
| 389 | // Since we patch the reloc info with RUNTIME_ENTRY calls every patch |
| 390 | // site will take up 2 bytes + any pc-jumps. |
| 391 | // We are conservative and always reserver 6 bytes in case where a |
| 392 | // simple pc-jump is not enough. |
| 393 | uint32_t pc_delta = |
| 394 | masm()->pc_offset() - deoptimization_reloc_size.last_pc_offset; |
| 395 | if (is_uintn(pc_delta, 6)) { |
| 396 | deoptimization_reloc_size.min_size += 2; |
| 397 | } else { |
| 398 | deoptimization_reloc_size.min_size += 6; |
| 399 | } |
| 400 | deoptimization_reloc_size.last_pc_offset = masm()->pc_offset(); |
| 401 | } |
| 402 | |
| 403 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 404 | void LCodeGen::AddToTranslation(Translation* translation, |
| 405 | LOperand* op, |
| 406 | bool is_tagged) { |
| 407 | if (op == NULL) { |
| 408 | // TODO(twuerthinger): Introduce marker operands to indicate that this value |
| 409 | // is not present and must be reconstructed from the deoptimizer. Currently |
| 410 | // this is only used for the arguments object. |
| 411 | translation->StoreArgumentsObject(); |
| 412 | } else if (op->IsStackSlot()) { |
| 413 | if (is_tagged) { |
| 414 | translation->StoreStackSlot(op->index()); |
| 415 | } else { |
| 416 | translation->StoreInt32StackSlot(op->index()); |
| 417 | } |
| 418 | } else if (op->IsDoubleStackSlot()) { |
| 419 | translation->StoreDoubleStackSlot(op->index()); |
| 420 | } else if (op->IsArgument()) { |
| 421 | ASSERT(is_tagged); |
| 422 | int src_index = StackSlotCount() + op->index(); |
| 423 | translation->StoreStackSlot(src_index); |
| 424 | } else if (op->IsRegister()) { |
| 425 | Register reg = ToRegister(op); |
| 426 | if (is_tagged) { |
| 427 | translation->StoreRegister(reg); |
| 428 | } else { |
| 429 | translation->StoreInt32Register(reg); |
| 430 | } |
| 431 | } else if (op->IsDoubleRegister()) { |
| 432 | XMMRegister reg = ToDoubleRegister(op); |
| 433 | translation->StoreDoubleRegister(reg); |
| 434 | } else if (op->IsConstantOperand()) { |
| 435 | Handle<Object> literal = chunk()->LookupLiteral(LConstantOperand::cast(op)); |
| 436 | int src_index = DefineDeoptimizationLiteral(literal); |
| 437 | translation->StoreLiteral(src_index); |
| 438 | } else { |
| 439 | UNREACHABLE(); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | void LCodeGen::CallCode(Handle<Code> code, |
| 445 | RelocInfo::Mode mode, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 446 | LInstruction* instr, |
| 447 | bool adjusted) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 448 | ASSERT(instr != NULL); |
| 449 | LPointerMap* pointers = instr->pointer_map(); |
| 450 | RecordPosition(pointers->position()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 451 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 452 | if (!adjusted) { |
| 453 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 454 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 455 | __ call(code, mode); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 456 | |
| 457 | EnsureRelocSpaceForDeoptimization(); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 458 | RegisterLazyDeoptimization(instr); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 459 | |
| 460 | // Signal that we don't inline smi code before these stubs in the |
| 461 | // optimizing code generator. |
| 462 | if (code->kind() == Code::TYPE_RECORDING_BINARY_OP_IC || |
| 463 | code->kind() == Code::COMPARE_IC) { |
| 464 | __ nop(); |
| 465 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 469 | void LCodeGen::CallRuntime(Runtime::Function* fun, |
| 470 | int argc, |
| 471 | LInstruction* instr, |
| 472 | bool adjusted) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 473 | ASSERT(instr != NULL); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 474 | ASSERT(instr->HasPointerMap()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 475 | LPointerMap* pointers = instr->pointer_map(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 476 | RecordPosition(pointers->position()); |
| 477 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 478 | if (!adjusted) { |
| 479 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 480 | } |
| 481 | __ CallRuntime(fun, argc); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 482 | RegisterLazyDeoptimization(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
| 486 | void LCodeGen::RegisterLazyDeoptimization(LInstruction* instr) { |
| 487 | // Create the environment to bailout to. If the call has side effects |
| 488 | // execution has to continue after the call otherwise execution can continue |
| 489 | // from a previous bailout point repeating the call. |
fschneider@chromium.org | 1df6b47 | 2011-01-26 08:23:03 +0000 | [diff] [blame] | 490 | LEnvironment* deoptimization_environment; |
| 491 | if (instr->HasDeoptimizationEnvironment()) { |
| 492 | deoptimization_environment = instr->deoptimization_environment(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 493 | } else { |
fschneider@chromium.org | 1df6b47 | 2011-01-26 08:23:03 +0000 | [diff] [blame] | 494 | deoptimization_environment = instr->environment(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 495 | } |
| 496 | |
fschneider@chromium.org | 1df6b47 | 2011-01-26 08:23:03 +0000 | [diff] [blame] | 497 | RegisterEnvironmentForDeoptimization(deoptimization_environment); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 498 | RecordSafepoint(instr->pointer_map(), |
fschneider@chromium.org | 1df6b47 | 2011-01-26 08:23:03 +0000 | [diff] [blame] | 499 | deoptimization_environment->deoptimization_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
| 503 | void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment) { |
| 504 | if (!environment->HasBeenRegistered()) { |
| 505 | // Physical stack frame layout: |
| 506 | // -x ............. -4 0 ..................................... y |
| 507 | // [incoming arguments] [spill slots] [pushed outgoing arguments] |
| 508 | |
| 509 | // Layout of the environment: |
| 510 | // 0 ..................................................... size-1 |
| 511 | // [parameters] [locals] [expression stack including arguments] |
| 512 | |
| 513 | // Layout of the translation: |
| 514 | // 0 ........................................................ size - 1 + 4 |
| 515 | // [expression stack including arguments] [locals] [4 words] [parameters] |
| 516 | // |>------------ translation_size ------------<| |
| 517 | |
| 518 | int frame_count = 0; |
| 519 | for (LEnvironment* e = environment; e != NULL; e = e->outer()) { |
| 520 | ++frame_count; |
| 521 | } |
| 522 | Translation translation(&translations_, frame_count); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 523 | WriteTranslation(environment, &translation); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 524 | int deoptimization_index = deoptimizations_.length(); |
| 525 | environment->Register(deoptimization_index, translation.index()); |
| 526 | deoptimizations_.Add(environment); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | void LCodeGen::DeoptimizeIf(Condition cc, LEnvironment* environment) { |
| 532 | RegisterEnvironmentForDeoptimization(environment); |
| 533 | ASSERT(environment->HasBeenRegistered()); |
| 534 | int id = environment->deoptimization_index(); |
| 535 | Address entry = Deoptimizer::GetDeoptimizationEntry(id, Deoptimizer::EAGER); |
| 536 | ASSERT(entry != NULL); |
| 537 | if (entry == NULL) { |
| 538 | Abort("bailout was not prepared"); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | if (FLAG_deopt_every_n_times != 0) { |
| 543 | Handle<SharedFunctionInfo> shared(info_->shared_info()); |
| 544 | Label no_deopt; |
| 545 | __ pushfd(); |
| 546 | __ push(eax); |
| 547 | __ push(ebx); |
| 548 | __ mov(ebx, shared); |
| 549 | __ mov(eax, FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset)); |
| 550 | __ sub(Operand(eax), Immediate(Smi::FromInt(1))); |
| 551 | __ j(not_zero, &no_deopt); |
| 552 | if (FLAG_trap_on_deopt) __ int3(); |
| 553 | __ mov(eax, Immediate(Smi::FromInt(FLAG_deopt_every_n_times))); |
| 554 | __ mov(FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset), eax); |
| 555 | __ pop(ebx); |
| 556 | __ pop(eax); |
| 557 | __ popfd(); |
| 558 | __ jmp(entry, RelocInfo::RUNTIME_ENTRY); |
| 559 | |
| 560 | __ bind(&no_deopt); |
| 561 | __ mov(FieldOperand(ebx, SharedFunctionInfo::kDeoptCounterOffset), eax); |
| 562 | __ pop(ebx); |
| 563 | __ pop(eax); |
| 564 | __ popfd(); |
| 565 | } |
| 566 | |
| 567 | if (cc == no_condition) { |
| 568 | if (FLAG_trap_on_deopt) __ int3(); |
| 569 | __ jmp(entry, RelocInfo::RUNTIME_ENTRY); |
| 570 | } else { |
| 571 | if (FLAG_trap_on_deopt) { |
| 572 | NearLabel done; |
| 573 | __ j(NegateCondition(cc), &done); |
| 574 | __ int3(); |
| 575 | __ jmp(entry, RelocInfo::RUNTIME_ENTRY); |
| 576 | __ bind(&done); |
| 577 | } else { |
| 578 | __ j(cc, entry, RelocInfo::RUNTIME_ENTRY, not_taken); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) { |
| 585 | int length = deoptimizations_.length(); |
| 586 | if (length == 0) return; |
| 587 | ASSERT(FLAG_deopt); |
| 588 | Handle<DeoptimizationInputData> data = |
| 589 | Factory::NewDeoptimizationInputData(length, TENURED); |
| 590 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 591 | Handle<ByteArray> translations = translations_.CreateByteArray(); |
| 592 | data->SetTranslationByteArray(*translations); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 593 | data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_)); |
| 594 | |
| 595 | Handle<FixedArray> literals = |
| 596 | Factory::NewFixedArray(deoptimization_literals_.length(), TENURED); |
| 597 | for (int i = 0; i < deoptimization_literals_.length(); i++) { |
| 598 | literals->set(i, *deoptimization_literals_[i]); |
| 599 | } |
| 600 | data->SetLiteralArray(*literals); |
| 601 | |
| 602 | data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id())); |
| 603 | data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_)); |
| 604 | |
| 605 | // Populate the deoptimization entries. |
| 606 | for (int i = 0; i < length; i++) { |
| 607 | LEnvironment* env = deoptimizations_[i]; |
| 608 | data->SetAstId(i, Smi::FromInt(env->ast_id())); |
| 609 | data->SetTranslationIndex(i, Smi::FromInt(env->translation_index())); |
| 610 | data->SetArgumentsStackHeight(i, |
| 611 | Smi::FromInt(env->arguments_stack_height())); |
| 612 | } |
| 613 | code->set_deoptimization_data(*data); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) { |
| 618 | int result = deoptimization_literals_.length(); |
| 619 | for (int i = 0; i < deoptimization_literals_.length(); ++i) { |
| 620 | if (deoptimization_literals_[i].is_identical_to(literal)) return i; |
| 621 | } |
| 622 | deoptimization_literals_.Add(literal); |
| 623 | return result; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() { |
| 628 | ASSERT(deoptimization_literals_.length() == 0); |
| 629 | |
| 630 | const ZoneList<Handle<JSFunction> >* inlined_closures = |
| 631 | chunk()->inlined_closures(); |
| 632 | |
| 633 | for (int i = 0, length = inlined_closures->length(); |
| 634 | i < length; |
| 635 | i++) { |
| 636 | DefineDeoptimizationLiteral(inlined_closures->at(i)); |
| 637 | } |
| 638 | |
| 639 | inlined_function_count_ = deoptimization_literals_.length(); |
| 640 | } |
| 641 | |
| 642 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 643 | void LCodeGen::RecordSafepoint( |
| 644 | LPointerMap* pointers, |
| 645 | Safepoint::Kind kind, |
| 646 | int arguments, |
| 647 | int deoptimization_index) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 648 | const ZoneList<LOperand*>* operands = pointers->operands(); |
| 649 | Safepoint safepoint = safepoints_.DefineSafepoint(masm(), |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 650 | kind, arguments, deoptimization_index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 651 | for (int i = 0; i < operands->length(); i++) { |
| 652 | LOperand* pointer = operands->at(i); |
| 653 | if (pointer->IsStackSlot()) { |
| 654 | safepoint.DefinePointerSlot(pointer->index()); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 655 | } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) { |
| 656 | safepoint.DefinePointerRegister(ToRegister(pointer)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
| 662 | void LCodeGen::RecordSafepoint(LPointerMap* pointers, |
| 663 | int deoptimization_index) { |
| 664 | RecordSafepoint(pointers, Safepoint::kSimple, 0, deoptimization_index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 668 | void LCodeGen::RecordSafepoint(int deoptimization_index) { |
| 669 | LPointerMap empty_pointers(RelocInfo::kNoPosition); |
| 670 | RecordSafepoint(&empty_pointers, deoptimization_index); |
| 671 | } |
| 672 | |
| 673 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 674 | void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers, |
| 675 | int arguments, |
| 676 | int deoptimization_index) { |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 677 | RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, |
| 678 | deoptimization_index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | |
| 682 | void LCodeGen::RecordPosition(int position) { |
| 683 | if (!FLAG_debug_info || position == RelocInfo::kNoPosition) return; |
| 684 | masm()->positions_recorder()->RecordPosition(position); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | void LCodeGen::DoLabel(LLabel* label) { |
| 689 | if (label->is_loop_header()) { |
| 690 | Comment(";;; B%d - LOOP entry", label->block_id()); |
| 691 | } else { |
| 692 | Comment(";;; B%d", label->block_id()); |
| 693 | } |
| 694 | __ bind(label->label()); |
| 695 | current_block_ = label->block_id(); |
| 696 | LCodeGen::DoGap(label); |
| 697 | } |
| 698 | |
| 699 | |
| 700 | void LCodeGen::DoParallelMove(LParallelMove* move) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 701 | resolver_.Resolve(move); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
| 705 | void LCodeGen::DoGap(LGap* gap) { |
| 706 | for (int i = LGap::FIRST_INNER_POSITION; |
| 707 | i <= LGap::LAST_INNER_POSITION; |
| 708 | i++) { |
| 709 | LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i); |
| 710 | LParallelMove* move = gap->GetParallelMove(inner_pos); |
| 711 | if (move != NULL) DoParallelMove(move); |
| 712 | } |
| 713 | |
| 714 | LInstruction* next = GetNextInstruction(); |
| 715 | if (next != NULL && next->IsLazyBailout()) { |
| 716 | int pc = masm()->pc_offset(); |
| 717 | safepoints_.SetPcAfterGap(pc); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | |
| 722 | void LCodeGen::DoParameter(LParameter* instr) { |
| 723 | // Nothing to do. |
| 724 | } |
| 725 | |
| 726 | |
| 727 | void LCodeGen::DoCallStub(LCallStub* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 728 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 729 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 730 | switch (instr->hydrogen()->major_key()) { |
| 731 | case CodeStub::RegExpConstructResult: { |
| 732 | RegExpConstructResultStub stub; |
| 733 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 734 | break; |
| 735 | } |
| 736 | case CodeStub::RegExpExec: { |
| 737 | RegExpExecStub stub; |
| 738 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 739 | break; |
| 740 | } |
| 741 | case CodeStub::SubString: { |
| 742 | SubStringStub stub; |
| 743 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 744 | break; |
| 745 | } |
| 746 | case CodeStub::StringCharAt: { |
| 747 | StringCharAtStub stub; |
| 748 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 749 | break; |
| 750 | } |
| 751 | case CodeStub::MathPow: { |
| 752 | MathPowStub stub; |
| 753 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 754 | break; |
| 755 | } |
| 756 | case CodeStub::NumberToString: { |
| 757 | NumberToStringStub stub; |
| 758 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 759 | break; |
| 760 | } |
| 761 | case CodeStub::StringAdd: { |
| 762 | StringAddStub stub(NO_STRING_ADD_FLAGS); |
| 763 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 764 | break; |
| 765 | } |
| 766 | case CodeStub::StringCompare: { |
| 767 | StringCompareStub stub; |
| 768 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 769 | break; |
| 770 | } |
| 771 | case CodeStub::TranscendentalCache: { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 772 | TranscendentalCacheStub stub(instr->transcendental_type(), |
| 773 | TranscendentalCacheStub::TAGGED); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 774 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 775 | break; |
| 776 | } |
| 777 | default: |
| 778 | UNREACHABLE(); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { |
| 784 | // Nothing to do. |
| 785 | } |
| 786 | |
| 787 | |
| 788 | void LCodeGen::DoModI(LModI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 789 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 790 | ASSERT(ToRegister(instr->result()).is(edx)); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 791 | ASSERT(ToRegister(instr->InputAt(0)).is(eax)); |
| 792 | ASSERT(!ToRegister(instr->InputAt(1)).is(eax)); |
| 793 | ASSERT(!ToRegister(instr->InputAt(1)).is(edx)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 794 | |
| 795 | Register right_reg = ToRegister(right); |
| 796 | |
| 797 | // Check for x % 0. |
| 798 | if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { |
| 799 | __ test(right_reg, ToOperand(right)); |
| 800 | DeoptimizeIf(zero, instr->environment()); |
| 801 | } |
| 802 | |
| 803 | // Sign extend to edx. |
| 804 | __ cdq(); |
| 805 | |
| 806 | // Check for (0 % -x) that will produce negative zero. |
| 807 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 808 | NearLabel positive_left; |
| 809 | NearLabel done; |
| 810 | __ test(eax, Operand(eax)); |
| 811 | __ j(not_sign, &positive_left); |
| 812 | __ idiv(right_reg); |
| 813 | |
| 814 | // Test the remainder for 0, because then the result would be -0. |
| 815 | __ test(edx, Operand(edx)); |
| 816 | __ j(not_zero, &done); |
| 817 | |
| 818 | DeoptimizeIf(no_condition, instr->environment()); |
| 819 | __ bind(&positive_left); |
| 820 | __ idiv(right_reg); |
| 821 | __ bind(&done); |
| 822 | } else { |
| 823 | __ idiv(right_reg); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | |
| 828 | void LCodeGen::DoDivI(LDivI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 829 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 830 | ASSERT(ToRegister(instr->result()).is(eax)); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 831 | ASSERT(ToRegister(instr->InputAt(0)).is(eax)); |
| 832 | ASSERT(!ToRegister(instr->InputAt(1)).is(eax)); |
| 833 | ASSERT(!ToRegister(instr->InputAt(1)).is(edx)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 834 | |
| 835 | Register left_reg = eax; |
| 836 | |
| 837 | // Check for x / 0. |
| 838 | Register right_reg = ToRegister(right); |
| 839 | if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { |
| 840 | __ test(right_reg, ToOperand(right)); |
| 841 | DeoptimizeIf(zero, instr->environment()); |
| 842 | } |
| 843 | |
| 844 | // Check for (0 / -x) that will produce negative zero. |
| 845 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 846 | NearLabel left_not_zero; |
| 847 | __ test(left_reg, Operand(left_reg)); |
| 848 | __ j(not_zero, &left_not_zero); |
| 849 | __ test(right_reg, ToOperand(right)); |
| 850 | DeoptimizeIf(sign, instr->environment()); |
| 851 | __ bind(&left_not_zero); |
| 852 | } |
| 853 | |
| 854 | // Check for (-kMinInt / -1). |
| 855 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 856 | NearLabel left_not_min_int; |
| 857 | __ cmp(left_reg, kMinInt); |
| 858 | __ j(not_zero, &left_not_min_int); |
| 859 | __ cmp(right_reg, -1); |
| 860 | DeoptimizeIf(zero, instr->environment()); |
| 861 | __ bind(&left_not_min_int); |
| 862 | } |
| 863 | |
| 864 | // Sign extend to edx. |
| 865 | __ cdq(); |
| 866 | __ idiv(right_reg); |
| 867 | |
| 868 | // Deoptimize if remainder is not 0. |
| 869 | __ test(edx, Operand(edx)); |
| 870 | DeoptimizeIf(not_zero, instr->environment()); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | void LCodeGen::DoMulI(LMulI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 875 | Register left = ToRegister(instr->InputAt(0)); |
| 876 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 877 | |
| 878 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 879 | __ mov(ToRegister(instr->TempAt(0)), left); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (right->IsConstantOperand()) { |
| 883 | __ imul(left, left, ToInteger32(LConstantOperand::cast(right))); |
| 884 | } else { |
| 885 | __ imul(left, ToOperand(right)); |
| 886 | } |
| 887 | |
| 888 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 889 | DeoptimizeIf(overflow, instr->environment()); |
| 890 | } |
| 891 | |
| 892 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 893 | // Bail out if the result is supposed to be negative zero. |
| 894 | NearLabel done; |
| 895 | __ test(left, Operand(left)); |
| 896 | __ j(not_zero, &done); |
| 897 | if (right->IsConstantOperand()) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 898 | if (ToInteger32(LConstantOperand::cast(right)) <= 0) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 899 | DeoptimizeIf(no_condition, instr->environment()); |
| 900 | } |
| 901 | } else { |
| 902 | // Test the non-zero operand for negative sign. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 903 | __ or_(ToRegister(instr->TempAt(0)), ToOperand(right)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 904 | DeoptimizeIf(sign, instr->environment()); |
| 905 | } |
| 906 | __ bind(&done); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | |
| 911 | void LCodeGen::DoBitI(LBitI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 912 | LOperand* left = instr->InputAt(0); |
| 913 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 914 | ASSERT(left->Equals(instr->result())); |
| 915 | ASSERT(left->IsRegister()); |
| 916 | |
| 917 | if (right->IsConstantOperand()) { |
| 918 | int right_operand = ToInteger32(LConstantOperand::cast(right)); |
| 919 | switch (instr->op()) { |
| 920 | case Token::BIT_AND: |
| 921 | __ and_(ToRegister(left), right_operand); |
| 922 | break; |
| 923 | case Token::BIT_OR: |
| 924 | __ or_(ToRegister(left), right_operand); |
| 925 | break; |
| 926 | case Token::BIT_XOR: |
| 927 | __ xor_(ToRegister(left), right_operand); |
| 928 | break; |
| 929 | default: |
| 930 | UNREACHABLE(); |
| 931 | break; |
| 932 | } |
| 933 | } else { |
| 934 | switch (instr->op()) { |
| 935 | case Token::BIT_AND: |
| 936 | __ and_(ToRegister(left), ToOperand(right)); |
| 937 | break; |
| 938 | case Token::BIT_OR: |
| 939 | __ or_(ToRegister(left), ToOperand(right)); |
| 940 | break; |
| 941 | case Token::BIT_XOR: |
| 942 | __ xor_(ToRegister(left), ToOperand(right)); |
| 943 | break; |
| 944 | default: |
| 945 | UNREACHABLE(); |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | |
| 952 | void LCodeGen::DoShiftI(LShiftI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 953 | LOperand* left = instr->InputAt(0); |
| 954 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 955 | ASSERT(left->Equals(instr->result())); |
| 956 | ASSERT(left->IsRegister()); |
| 957 | if (right->IsRegister()) { |
| 958 | ASSERT(ToRegister(right).is(ecx)); |
| 959 | |
| 960 | switch (instr->op()) { |
| 961 | case Token::SAR: |
| 962 | __ sar_cl(ToRegister(left)); |
| 963 | break; |
| 964 | case Token::SHR: |
| 965 | __ shr_cl(ToRegister(left)); |
| 966 | if (instr->can_deopt()) { |
| 967 | __ test(ToRegister(left), Immediate(0x80000000)); |
| 968 | DeoptimizeIf(not_zero, instr->environment()); |
| 969 | } |
| 970 | break; |
| 971 | case Token::SHL: |
| 972 | __ shl_cl(ToRegister(left)); |
| 973 | break; |
| 974 | default: |
| 975 | UNREACHABLE(); |
| 976 | break; |
| 977 | } |
| 978 | } else { |
| 979 | int value = ToInteger32(LConstantOperand::cast(right)); |
| 980 | uint8_t shift_count = static_cast<uint8_t>(value & 0x1F); |
| 981 | switch (instr->op()) { |
| 982 | case Token::SAR: |
| 983 | if (shift_count != 0) { |
| 984 | __ sar(ToRegister(left), shift_count); |
| 985 | } |
| 986 | break; |
| 987 | case Token::SHR: |
| 988 | if (shift_count == 0 && instr->can_deopt()) { |
| 989 | __ test(ToRegister(left), Immediate(0x80000000)); |
| 990 | DeoptimizeIf(not_zero, instr->environment()); |
| 991 | } else { |
| 992 | __ shr(ToRegister(left), shift_count); |
| 993 | } |
| 994 | break; |
| 995 | case Token::SHL: |
| 996 | if (shift_count != 0) { |
| 997 | __ shl(ToRegister(left), shift_count); |
| 998 | } |
| 999 | break; |
| 1000 | default: |
| 1001 | UNREACHABLE(); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | void LCodeGen::DoSubI(LSubI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1009 | LOperand* left = instr->InputAt(0); |
| 1010 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1011 | ASSERT(left->Equals(instr->result())); |
| 1012 | |
| 1013 | if (right->IsConstantOperand()) { |
| 1014 | __ sub(ToOperand(left), ToImmediate(right)); |
| 1015 | } else { |
| 1016 | __ sub(ToRegister(left), ToOperand(right)); |
| 1017 | } |
| 1018 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 1019 | DeoptimizeIf(overflow, instr->environment()); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | void LCodeGen::DoConstantI(LConstantI* instr) { |
| 1025 | ASSERT(instr->result()->IsRegister()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1026 | __ Set(ToRegister(instr->result()), Immediate(instr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | void LCodeGen::DoConstantD(LConstantD* instr) { |
| 1031 | ASSERT(instr->result()->IsDoubleRegister()); |
| 1032 | XMMRegister res = ToDoubleRegister(instr->result()); |
| 1033 | double v = instr->value(); |
| 1034 | // Use xor to produce +0.0 in a fast and compact way, but avoid to |
| 1035 | // do so if the constant is -0.0. |
| 1036 | if (BitCast<uint64_t, double>(v) == 0) { |
| 1037 | __ xorpd(res, res); |
| 1038 | } else { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1039 | Register temp = ToRegister(instr->TempAt(0)); |
| 1040 | uint64_t int_val = BitCast<uint64_t, double>(v); |
| 1041 | int32_t lower = static_cast<int32_t>(int_val); |
| 1042 | int32_t upper = static_cast<int32_t>(int_val >> (kBitsPerInt)); |
| 1043 | if (CpuFeatures::IsSupported(SSE4_1)) { |
| 1044 | CpuFeatures::Scope scope(SSE4_1); |
| 1045 | if (lower != 0) { |
| 1046 | __ Set(temp, Immediate(lower)); |
| 1047 | __ movd(res, Operand(temp)); |
| 1048 | __ Set(temp, Immediate(upper)); |
| 1049 | __ pinsrd(res, Operand(temp), 1); |
| 1050 | } else { |
| 1051 | __ xorpd(res, res); |
| 1052 | __ Set(temp, Immediate(upper)); |
| 1053 | __ pinsrd(res, Operand(temp), 1); |
| 1054 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1055 | } else { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1056 | __ Set(temp, Immediate(upper)); |
| 1057 | __ movd(res, Operand(temp)); |
| 1058 | __ psllq(res, 32); |
| 1059 | if (lower != 0) { |
| 1060 | __ Set(temp, Immediate(lower)); |
| 1061 | __ movd(xmm0, Operand(temp)); |
| 1062 | __ por(res, xmm0); |
| 1063 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | void LCodeGen::DoConstantT(LConstantT* instr) { |
| 1070 | ASSERT(instr->result()->IsRegister()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1071 | __ Set(ToRegister(instr->result()), Immediate(instr->value())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1075 | void LCodeGen::DoJSArrayLength(LJSArrayLength* instr) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1076 | Register result = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1077 | Register array = ToRegister(instr->InputAt(0)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1078 | __ mov(result, FieldOperand(array, JSArray::kLengthOffset)); |
| 1079 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1080 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1081 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1082 | void LCodeGen::DoFixedArrayLength(LFixedArrayLength* instr) { |
| 1083 | Register result = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1084 | Register array = ToRegister(instr->InputAt(0)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 1085 | __ mov(result, FieldOperand(array, FixedArray::kLengthOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1089 | void LCodeGen::DoPixelArrayLength(LPixelArrayLength* instr) { |
| 1090 | Register result = ToRegister(instr->result()); |
| 1091 | Register array = ToRegister(instr->InputAt(0)); |
| 1092 | __ mov(result, FieldOperand(array, PixelArray::kLengthOffset)); |
| 1093 | } |
| 1094 | |
| 1095 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1096 | void LCodeGen::DoValueOf(LValueOf* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1097 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1098 | Register result = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1099 | Register map = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1100 | ASSERT(input.is(result)); |
| 1101 | NearLabel done; |
| 1102 | // If the object is a smi return the object. |
| 1103 | __ test(input, Immediate(kSmiTagMask)); |
| 1104 | __ j(zero, &done); |
| 1105 | |
| 1106 | // If the object is not a value type, return the object. |
| 1107 | __ CmpObjectType(input, JS_VALUE_TYPE, map); |
| 1108 | __ j(not_equal, &done); |
| 1109 | __ mov(result, FieldOperand(input, JSValue::kValueOffset)); |
| 1110 | |
| 1111 | __ bind(&done); |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | void LCodeGen::DoBitNotI(LBitNotI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1116 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1117 | ASSERT(input->Equals(instr->result())); |
| 1118 | __ not_(ToRegister(input)); |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | void LCodeGen::DoThrow(LThrow* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1123 | __ push(ToOperand(instr->InputAt(0))); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1124 | CallRuntime(Runtime::kThrow, 1, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (FLAG_debug_code) { |
| 1127 | Comment("Unreachable code."); |
| 1128 | __ int3(); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | void LCodeGen::DoAddI(LAddI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1134 | LOperand* left = instr->InputAt(0); |
| 1135 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1136 | ASSERT(left->Equals(instr->result())); |
| 1137 | |
| 1138 | if (right->IsConstantOperand()) { |
| 1139 | __ add(ToOperand(left), ToImmediate(right)); |
| 1140 | } else { |
| 1141 | __ add(ToRegister(left), ToOperand(right)); |
| 1142 | } |
| 1143 | |
| 1144 | if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { |
| 1145 | DeoptimizeIf(overflow, instr->environment()); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | void LCodeGen::DoArithmeticD(LArithmeticD* instr) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1151 | XMMRegister left = ToDoubleRegister(instr->InputAt(0)); |
| 1152 | XMMRegister right = ToDoubleRegister(instr->InputAt(1)); |
| 1153 | XMMRegister result = ToDoubleRegister(instr->result()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1154 | // Modulo uses a fixed result register. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1155 | ASSERT(instr->op() == Token::MOD || left.is(result)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1156 | switch (instr->op()) { |
| 1157 | case Token::ADD: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1158 | __ addsd(left, right); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | case Token::SUB: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1161 | __ subsd(left, right); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | case Token::MUL: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1164 | __ mulsd(left, right); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | case Token::DIV: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1167 | __ divsd(left, right); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1168 | break; |
| 1169 | case Token::MOD: { |
| 1170 | // Pass two doubles as arguments on the stack. |
| 1171 | __ PrepareCallCFunction(4, eax); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1172 | __ movdbl(Operand(esp, 0 * kDoubleSize), left); |
| 1173 | __ movdbl(Operand(esp, 1 * kDoubleSize), right); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1174 | __ CallCFunction(ExternalReference::double_fp_operation(Token::MOD), 4); |
| 1175 | |
| 1176 | // Return value is in st(0) on ia32. |
| 1177 | // Store it into the (fixed) result register. |
| 1178 | __ sub(Operand(esp), Immediate(kDoubleSize)); |
| 1179 | __ fstp_d(Operand(esp, 0)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1180 | __ movdbl(result, Operand(esp, 0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1181 | __ add(Operand(esp), Immediate(kDoubleSize)); |
| 1182 | break; |
| 1183 | } |
| 1184 | default: |
| 1185 | UNREACHABLE(); |
| 1186 | break; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | void LCodeGen::DoArithmeticT(LArithmeticT* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1192 | ASSERT(ToRegister(instr->InputAt(0)).is(edx)); |
| 1193 | ASSERT(ToRegister(instr->InputAt(1)).is(eax)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1194 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 1195 | |
| 1196 | TypeRecordingBinaryOpStub stub(instr->op(), NO_OVERWRITE); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1197 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | int LCodeGen::GetNextEmittedBlock(int block) { |
| 1202 | for (int i = block + 1; i < graph()->blocks()->length(); ++i) { |
| 1203 | LLabel* label = chunk_->GetLabel(i); |
| 1204 | if (!label->HasReplacement()) return i; |
| 1205 | } |
| 1206 | return -1; |
| 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | void LCodeGen::EmitBranch(int left_block, int right_block, Condition cc) { |
| 1211 | int next_block = GetNextEmittedBlock(current_block_); |
| 1212 | right_block = chunk_->LookupDestination(right_block); |
| 1213 | left_block = chunk_->LookupDestination(left_block); |
| 1214 | |
| 1215 | if (right_block == left_block) { |
| 1216 | EmitGoto(left_block); |
| 1217 | } else if (left_block == next_block) { |
| 1218 | __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block)); |
| 1219 | } else if (right_block == next_block) { |
| 1220 | __ j(cc, chunk_->GetAssemblyLabel(left_block)); |
| 1221 | } else { |
| 1222 | __ j(cc, chunk_->GetAssemblyLabel(left_block)); |
| 1223 | __ jmp(chunk_->GetAssemblyLabel(right_block)); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | void LCodeGen::DoBranch(LBranch* instr) { |
| 1229 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1230 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1231 | |
| 1232 | Representation r = instr->hydrogen()->representation(); |
| 1233 | if (r.IsInteger32()) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1234 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1235 | __ test(reg, Operand(reg)); |
| 1236 | EmitBranch(true_block, false_block, not_zero); |
| 1237 | } else if (r.IsDouble()) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1238 | XMMRegister reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1239 | __ xorpd(xmm0, xmm0); |
| 1240 | __ ucomisd(reg, xmm0); |
| 1241 | EmitBranch(true_block, false_block, not_equal); |
| 1242 | } else { |
| 1243 | ASSERT(r.IsTagged()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1244 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1245 | if (instr->hydrogen()->type().IsBoolean()) { |
| 1246 | __ cmp(reg, Factory::true_value()); |
| 1247 | EmitBranch(true_block, false_block, equal); |
| 1248 | } else { |
| 1249 | Label* true_label = chunk_->GetAssemblyLabel(true_block); |
| 1250 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 1251 | |
| 1252 | __ cmp(reg, Factory::undefined_value()); |
| 1253 | __ j(equal, false_label); |
| 1254 | __ cmp(reg, Factory::true_value()); |
| 1255 | __ j(equal, true_label); |
| 1256 | __ cmp(reg, Factory::false_value()); |
| 1257 | __ j(equal, false_label); |
| 1258 | __ test(reg, Operand(reg)); |
| 1259 | __ j(equal, false_label); |
| 1260 | __ test(reg, Immediate(kSmiTagMask)); |
| 1261 | __ j(zero, true_label); |
| 1262 | |
| 1263 | // Test for double values. Zero is false. |
| 1264 | NearLabel call_stub; |
| 1265 | __ cmp(FieldOperand(reg, HeapObject::kMapOffset), |
| 1266 | Factory::heap_number_map()); |
| 1267 | __ j(not_equal, &call_stub); |
| 1268 | __ fldz(); |
| 1269 | __ fld_d(FieldOperand(reg, HeapNumber::kValueOffset)); |
| 1270 | __ FCmp(); |
| 1271 | __ j(zero, false_label); |
| 1272 | __ jmp(true_label); |
| 1273 | |
| 1274 | // The conversion stub doesn't cause garbage collections so it's |
| 1275 | // safe to not record a safepoint after the call. |
| 1276 | __ bind(&call_stub); |
| 1277 | ToBooleanStub stub; |
| 1278 | __ pushad(); |
| 1279 | __ push(reg); |
| 1280 | __ CallStub(&stub); |
| 1281 | __ test(eax, Operand(eax)); |
| 1282 | __ popad(); |
| 1283 | EmitBranch(true_block, false_block, not_zero); |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | void LCodeGen::EmitGoto(int block, LDeferredCode* deferred_stack_check) { |
| 1290 | block = chunk_->LookupDestination(block); |
| 1291 | int next_block = GetNextEmittedBlock(current_block_); |
| 1292 | if (block != next_block) { |
| 1293 | // Perform stack overflow check if this goto needs it before jumping. |
| 1294 | if (deferred_stack_check != NULL) { |
| 1295 | ExternalReference stack_limit = |
| 1296 | ExternalReference::address_of_stack_limit(); |
| 1297 | __ cmp(esp, Operand::StaticVariable(stack_limit)); |
| 1298 | __ j(above_equal, chunk_->GetAssemblyLabel(block)); |
| 1299 | __ jmp(deferred_stack_check->entry()); |
| 1300 | deferred_stack_check->SetExit(chunk_->GetAssemblyLabel(block)); |
| 1301 | } else { |
| 1302 | __ jmp(chunk_->GetAssemblyLabel(block)); |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | void LCodeGen::DoDeferredStackCheck(LGoto* instr) { |
| 1309 | __ pushad(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1310 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1311 | __ CallRuntimeSaveDoubles(Runtime::kStackGuard); |
| 1312 | RecordSafepointWithRegisters( |
| 1313 | instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex); |
| 1314 | __ popad(); |
| 1315 | } |
| 1316 | |
| 1317 | void LCodeGen::DoGoto(LGoto* instr) { |
| 1318 | class DeferredStackCheck: public LDeferredCode { |
| 1319 | public: |
| 1320 | DeferredStackCheck(LCodeGen* codegen, LGoto* instr) |
| 1321 | : LDeferredCode(codegen), instr_(instr) { } |
| 1322 | virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); } |
| 1323 | private: |
| 1324 | LGoto* instr_; |
| 1325 | }; |
| 1326 | |
| 1327 | DeferredStackCheck* deferred = NULL; |
| 1328 | if (instr->include_stack_check()) { |
| 1329 | deferred = new DeferredStackCheck(this, instr); |
| 1330 | } |
| 1331 | EmitGoto(instr->block_id(), deferred); |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) { |
| 1336 | Condition cond = no_condition; |
| 1337 | switch (op) { |
| 1338 | case Token::EQ: |
| 1339 | case Token::EQ_STRICT: |
| 1340 | cond = equal; |
| 1341 | break; |
| 1342 | case Token::LT: |
| 1343 | cond = is_unsigned ? below : less; |
| 1344 | break; |
| 1345 | case Token::GT: |
| 1346 | cond = is_unsigned ? above : greater; |
| 1347 | break; |
| 1348 | case Token::LTE: |
| 1349 | cond = is_unsigned ? below_equal : less_equal; |
| 1350 | break; |
| 1351 | case Token::GTE: |
| 1352 | cond = is_unsigned ? above_equal : greater_equal; |
| 1353 | break; |
| 1354 | case Token::IN: |
| 1355 | case Token::INSTANCEOF: |
| 1356 | default: |
| 1357 | UNREACHABLE(); |
| 1358 | } |
| 1359 | return cond; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | void LCodeGen::EmitCmpI(LOperand* left, LOperand* right) { |
| 1364 | if (right->IsConstantOperand()) { |
| 1365 | __ cmp(ToOperand(left), ToImmediate(right)); |
| 1366 | } else { |
| 1367 | __ cmp(ToRegister(left), ToOperand(right)); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | void LCodeGen::DoCmpID(LCmpID* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1373 | LOperand* left = instr->InputAt(0); |
| 1374 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1375 | LOperand* result = instr->result(); |
| 1376 | |
| 1377 | NearLabel unordered; |
| 1378 | if (instr->is_double()) { |
| 1379 | // Don't base result on EFLAGS when a NaN is involved. Instead |
| 1380 | // jump to the unordered case, which produces a false value. |
| 1381 | __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right)); |
| 1382 | __ j(parity_even, &unordered, not_taken); |
| 1383 | } else { |
| 1384 | EmitCmpI(left, right); |
| 1385 | } |
| 1386 | |
| 1387 | NearLabel done; |
| 1388 | Condition cc = TokenToCondition(instr->op(), instr->is_double()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1389 | __ mov(ToRegister(result), Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1390 | __ j(cc, &done); |
| 1391 | |
| 1392 | __ bind(&unordered); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1393 | __ mov(ToRegister(result), Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1394 | __ bind(&done); |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | void LCodeGen::DoCmpIDAndBranch(LCmpIDAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1399 | LOperand* left = instr->InputAt(0); |
| 1400 | LOperand* right = instr->InputAt(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1401 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1402 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1403 | |
| 1404 | if (instr->is_double()) { |
| 1405 | // Don't base result on EFLAGS when a NaN is involved. Instead |
| 1406 | // jump to the false block. |
| 1407 | __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right)); |
| 1408 | __ j(parity_even, chunk_->GetAssemblyLabel(false_block)); |
| 1409 | } else { |
| 1410 | EmitCmpI(left, right); |
| 1411 | } |
| 1412 | |
| 1413 | Condition cc = TokenToCondition(instr->op(), instr->is_double()); |
| 1414 | EmitBranch(true_block, false_block, cc); |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | void LCodeGen::DoCmpJSObjectEq(LCmpJSObjectEq* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1419 | Register left = ToRegister(instr->InputAt(0)); |
| 1420 | Register right = ToRegister(instr->InputAt(1)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1421 | Register result = ToRegister(instr->result()); |
| 1422 | |
| 1423 | __ cmp(left, Operand(right)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1424 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1425 | NearLabel done; |
| 1426 | __ j(equal, &done); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1427 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1428 | __ bind(&done); |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | void LCodeGen::DoCmpJSObjectEqAndBranch(LCmpJSObjectEqAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1433 | Register left = ToRegister(instr->InputAt(0)); |
| 1434 | Register right = ToRegister(instr->InputAt(1)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1435 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1436 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1437 | |
| 1438 | __ cmp(left, Operand(right)); |
| 1439 | EmitBranch(true_block, false_block, equal); |
| 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | void LCodeGen::DoIsNull(LIsNull* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1444 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1445 | Register result = ToRegister(instr->result()); |
| 1446 | |
| 1447 | // TODO(fsc): If the expression is known to be a smi, then it's |
| 1448 | // definitely not null. Materialize false. |
| 1449 | |
| 1450 | __ cmp(reg, Factory::null_value()); |
| 1451 | if (instr->is_strict()) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1452 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1453 | NearLabel done; |
| 1454 | __ j(equal, &done); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1455 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1456 | __ bind(&done); |
| 1457 | } else { |
| 1458 | NearLabel true_value, false_value, done; |
| 1459 | __ j(equal, &true_value); |
| 1460 | __ cmp(reg, Factory::undefined_value()); |
| 1461 | __ j(equal, &true_value); |
| 1462 | __ test(reg, Immediate(kSmiTagMask)); |
| 1463 | __ j(zero, &false_value); |
| 1464 | // Check for undetectable objects by looking in the bit field in |
| 1465 | // the map. The object has already been smi checked. |
| 1466 | Register scratch = result; |
| 1467 | __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset)); |
| 1468 | __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset)); |
| 1469 | __ test(scratch, Immediate(1 << Map::kIsUndetectable)); |
| 1470 | __ j(not_zero, &true_value); |
| 1471 | __ bind(&false_value); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1472 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1473 | __ jmp(&done); |
| 1474 | __ bind(&true_value); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1475 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1476 | __ bind(&done); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | void LCodeGen::DoIsNullAndBranch(LIsNullAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1482 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1483 | |
| 1484 | // TODO(fsc): If the expression is known to be a smi, then it's |
| 1485 | // definitely not null. Jump to the false block. |
| 1486 | |
| 1487 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1488 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1489 | |
| 1490 | __ cmp(reg, Factory::null_value()); |
| 1491 | if (instr->is_strict()) { |
| 1492 | EmitBranch(true_block, false_block, equal); |
| 1493 | } else { |
| 1494 | Label* true_label = chunk_->GetAssemblyLabel(true_block); |
| 1495 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 1496 | __ j(equal, true_label); |
| 1497 | __ cmp(reg, Factory::undefined_value()); |
| 1498 | __ j(equal, true_label); |
| 1499 | __ test(reg, Immediate(kSmiTagMask)); |
| 1500 | __ j(zero, false_label); |
| 1501 | // Check for undetectable objects by looking in the bit field in |
| 1502 | // the map. The object has already been smi checked. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1503 | Register scratch = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1504 | __ mov(scratch, FieldOperand(reg, HeapObject::kMapOffset)); |
| 1505 | __ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset)); |
| 1506 | __ test(scratch, Immediate(1 << Map::kIsUndetectable)); |
| 1507 | EmitBranch(true_block, false_block, not_zero); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1512 | Condition LCodeGen::EmitIsObject(Register input, |
| 1513 | Register temp1, |
| 1514 | Register temp2, |
| 1515 | Label* is_not_object, |
| 1516 | Label* is_object) { |
| 1517 | ASSERT(!input.is(temp1)); |
| 1518 | ASSERT(!input.is(temp2)); |
| 1519 | ASSERT(!temp1.is(temp2)); |
| 1520 | |
| 1521 | __ test(input, Immediate(kSmiTagMask)); |
| 1522 | __ j(equal, is_not_object); |
| 1523 | |
| 1524 | __ cmp(input, Factory::null_value()); |
| 1525 | __ j(equal, is_object); |
| 1526 | |
| 1527 | __ mov(temp1, FieldOperand(input, HeapObject::kMapOffset)); |
| 1528 | // Undetectable objects behave like undefined. |
| 1529 | __ movzx_b(temp2, FieldOperand(temp1, Map::kBitFieldOffset)); |
| 1530 | __ test(temp2, Immediate(1 << Map::kIsUndetectable)); |
| 1531 | __ j(not_zero, is_not_object); |
| 1532 | |
| 1533 | __ movzx_b(temp2, FieldOperand(temp1, Map::kInstanceTypeOffset)); |
| 1534 | __ cmp(temp2, FIRST_JS_OBJECT_TYPE); |
| 1535 | __ j(below, is_not_object); |
| 1536 | __ cmp(temp2, LAST_JS_OBJECT_TYPE); |
| 1537 | return below_equal; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | void LCodeGen::DoIsObject(LIsObject* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1542 | Register reg = ToRegister(instr->InputAt(0)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1543 | Register result = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1544 | Register temp = ToRegister(instr->TempAt(0)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1545 | Label is_false, is_true, done; |
| 1546 | |
| 1547 | Condition true_cond = EmitIsObject(reg, result, temp, &is_false, &is_true); |
| 1548 | __ j(true_cond, &is_true); |
| 1549 | |
| 1550 | __ bind(&is_false); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1551 | __ mov(result, Factory::false_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1552 | __ jmp(&done); |
| 1553 | |
| 1554 | __ bind(&is_true); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1555 | __ mov(result, Factory::true_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1556 | |
| 1557 | __ bind(&done); |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1562 | Register reg = ToRegister(instr->InputAt(0)); |
| 1563 | Register temp = ToRegister(instr->TempAt(0)); |
| 1564 | Register temp2 = ToRegister(instr->TempAt(1)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1565 | |
| 1566 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1567 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1568 | Label* true_label = chunk_->GetAssemblyLabel(true_block); |
| 1569 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 1570 | |
| 1571 | Condition true_cond = EmitIsObject(reg, temp, temp2, false_label, true_label); |
| 1572 | |
| 1573 | EmitBranch(true_block, false_block, true_cond); |
| 1574 | } |
| 1575 | |
| 1576 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1577 | void LCodeGen::DoIsSmi(LIsSmi* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1578 | Operand input = ToOperand(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1579 | Register result = ToRegister(instr->result()); |
| 1580 | |
| 1581 | ASSERT(instr->hydrogen()->value()->representation().IsTagged()); |
| 1582 | __ test(input, Immediate(kSmiTagMask)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1583 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1584 | NearLabel done; |
| 1585 | __ j(zero, &done); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1586 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1587 | __ bind(&done); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1592 | Operand input = ToOperand(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1593 | |
| 1594 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1595 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1596 | |
| 1597 | __ test(input, Immediate(kSmiTagMask)); |
| 1598 | EmitBranch(true_block, false_block, zero); |
| 1599 | } |
| 1600 | |
| 1601 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1602 | static InstanceType TestType(HHasInstanceType* instr) { |
| 1603 | InstanceType from = instr->from(); |
| 1604 | InstanceType to = instr->to(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1605 | if (from == FIRST_TYPE) return to; |
| 1606 | ASSERT(from == to || to == LAST_TYPE); |
| 1607 | return from; |
| 1608 | } |
| 1609 | |
| 1610 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1611 | static Condition BranchCondition(HHasInstanceType* instr) { |
| 1612 | InstanceType from = instr->from(); |
| 1613 | InstanceType to = instr->to(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1614 | if (from == to) return equal; |
| 1615 | if (to == LAST_TYPE) return above_equal; |
| 1616 | if (from == FIRST_TYPE) return below_equal; |
| 1617 | UNREACHABLE(); |
| 1618 | return equal; |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | void LCodeGen::DoHasInstanceType(LHasInstanceType* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1623 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1624 | Register result = ToRegister(instr->result()); |
| 1625 | |
| 1626 | ASSERT(instr->hydrogen()->value()->representation().IsTagged()); |
| 1627 | __ test(input, Immediate(kSmiTagMask)); |
| 1628 | NearLabel done, is_false; |
| 1629 | __ j(zero, &is_false); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1630 | __ CmpObjectType(input, TestType(instr->hydrogen()), result); |
| 1631 | __ j(NegateCondition(BranchCondition(instr->hydrogen())), &is_false); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1632 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1633 | __ jmp(&done); |
| 1634 | __ bind(&is_false); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1635 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1636 | __ bind(&done); |
| 1637 | } |
| 1638 | |
| 1639 | |
| 1640 | void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1641 | Register input = ToRegister(instr->InputAt(0)); |
| 1642 | Register temp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1643 | |
| 1644 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1645 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1646 | |
| 1647 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 1648 | |
| 1649 | __ test(input, Immediate(kSmiTagMask)); |
| 1650 | __ j(zero, false_label); |
| 1651 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1652 | __ CmpObjectType(input, TestType(instr->hydrogen()), temp); |
| 1653 | EmitBranch(true_block, false_block, BranchCondition(instr->hydrogen())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1657 | void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) { |
| 1658 | Register input = ToRegister(instr->InputAt(0)); |
| 1659 | Register result = ToRegister(instr->result()); |
| 1660 | |
| 1661 | if (FLAG_debug_code) { |
| 1662 | __ AbortIfNotString(input); |
| 1663 | } |
| 1664 | |
| 1665 | __ mov(result, FieldOperand(input, String::kHashFieldOffset)); |
| 1666 | __ IndexFromHash(result, result); |
| 1667 | } |
| 1668 | |
| 1669 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1670 | void LCodeGen::DoHasCachedArrayIndex(LHasCachedArrayIndex* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1671 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1672 | Register result = ToRegister(instr->result()); |
| 1673 | |
| 1674 | ASSERT(instr->hydrogen()->value()->representation().IsTagged()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1675 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1676 | __ test(FieldOperand(input, String::kHashFieldOffset), |
| 1677 | Immediate(String::kContainsCachedArrayIndexMask)); |
| 1678 | NearLabel done; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1679 | __ j(zero, &done); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1680 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1681 | __ bind(&done); |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | void LCodeGen::DoHasCachedArrayIndexAndBranch( |
| 1686 | LHasCachedArrayIndexAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1687 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1688 | |
| 1689 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1690 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1691 | |
| 1692 | __ test(FieldOperand(input, String::kHashFieldOffset), |
| 1693 | Immediate(String::kContainsCachedArrayIndexMask)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 1694 | EmitBranch(true_block, false_block, equal); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | // Branches to a label or falls through with the answer in the z flag. Trashes |
| 1699 | // the temp registers, but not the input. Only input and temp2 may alias. |
| 1700 | void LCodeGen::EmitClassOfTest(Label* is_true, |
| 1701 | Label* is_false, |
| 1702 | Handle<String>class_name, |
| 1703 | Register input, |
| 1704 | Register temp, |
| 1705 | Register temp2) { |
| 1706 | ASSERT(!input.is(temp)); |
| 1707 | ASSERT(!temp.is(temp2)); // But input and temp2 may be the same register. |
| 1708 | __ test(input, Immediate(kSmiTagMask)); |
| 1709 | __ j(zero, is_false); |
| 1710 | __ CmpObjectType(input, FIRST_JS_OBJECT_TYPE, temp); |
| 1711 | __ j(below, is_false); |
| 1712 | |
| 1713 | // Map is now in temp. |
| 1714 | // Functions have class 'Function'. |
| 1715 | __ CmpInstanceType(temp, JS_FUNCTION_TYPE); |
| 1716 | if (class_name->IsEqualTo(CStrVector("Function"))) { |
| 1717 | __ j(equal, is_true); |
| 1718 | } else { |
| 1719 | __ j(equal, is_false); |
| 1720 | } |
| 1721 | |
| 1722 | // Check if the constructor in the map is a function. |
| 1723 | __ mov(temp, FieldOperand(temp, Map::kConstructorOffset)); |
| 1724 | |
| 1725 | // As long as JS_FUNCTION_TYPE is the last instance type and it is |
| 1726 | // right after LAST_JS_OBJECT_TYPE, we can avoid checking for |
| 1727 | // LAST_JS_OBJECT_TYPE. |
| 1728 | ASSERT(LAST_TYPE == JS_FUNCTION_TYPE); |
| 1729 | ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1); |
| 1730 | |
| 1731 | // Objects with a non-function constructor have class 'Object'. |
| 1732 | __ CmpObjectType(temp, JS_FUNCTION_TYPE, temp2); |
| 1733 | if (class_name->IsEqualTo(CStrVector("Object"))) { |
| 1734 | __ j(not_equal, is_true); |
| 1735 | } else { |
| 1736 | __ j(not_equal, is_false); |
| 1737 | } |
| 1738 | |
| 1739 | // temp now contains the constructor function. Grab the |
| 1740 | // instance class name from there. |
| 1741 | __ mov(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset)); |
| 1742 | __ mov(temp, FieldOperand(temp, |
| 1743 | SharedFunctionInfo::kInstanceClassNameOffset)); |
| 1744 | // The class name we are testing against is a symbol because it's a literal. |
| 1745 | // The name in the constructor is a symbol because of the way the context is |
| 1746 | // booted. This routine isn't expected to work for random API-created |
| 1747 | // classes and it doesn't have to because you can't access it with natives |
| 1748 | // syntax. Since both sides are symbols it is sufficient to use an identity |
| 1749 | // comparison. |
| 1750 | __ cmp(temp, class_name); |
| 1751 | // End with the answer in the z flag. |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | void LCodeGen::DoClassOfTest(LClassOfTest* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1756 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1757 | Register result = ToRegister(instr->result()); |
| 1758 | ASSERT(input.is(result)); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1759 | Register temp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1760 | Handle<String> class_name = instr->hydrogen()->class_name(); |
| 1761 | NearLabel done; |
| 1762 | Label is_true, is_false; |
| 1763 | |
| 1764 | EmitClassOfTest(&is_true, &is_false, class_name, input, temp, input); |
| 1765 | |
| 1766 | __ j(not_equal, &is_false); |
| 1767 | |
| 1768 | __ bind(&is_true); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1769 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1770 | __ jmp(&done); |
| 1771 | |
| 1772 | __ bind(&is_false); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1773 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1774 | __ bind(&done); |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1779 | Register input = ToRegister(instr->InputAt(0)); |
| 1780 | Register temp = ToRegister(instr->TempAt(0)); |
| 1781 | Register temp2 = ToRegister(instr->TempAt(1)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1782 | if (input.is(temp)) { |
| 1783 | // Swap. |
| 1784 | Register swapper = temp; |
| 1785 | temp = temp2; |
| 1786 | temp2 = swapper; |
| 1787 | } |
| 1788 | Handle<String> class_name = instr->hydrogen()->class_name(); |
| 1789 | |
| 1790 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1791 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1792 | |
| 1793 | Label* true_label = chunk_->GetAssemblyLabel(true_block); |
| 1794 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 1795 | |
| 1796 | EmitClassOfTest(true_label, false_label, class_name, input, temp, temp2); |
| 1797 | |
| 1798 | EmitBranch(true_block, false_block, equal); |
| 1799 | } |
| 1800 | |
| 1801 | |
| 1802 | void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1803 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1804 | int true_block = instr->true_block_id(); |
| 1805 | int false_block = instr->false_block_id(); |
| 1806 | |
| 1807 | __ cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map()); |
| 1808 | EmitBranch(true_block, false_block, equal); |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | void LCodeGen::DoInstanceOf(LInstanceOf* instr) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1813 | // Object and function are in fixed registers defined by the stub. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1814 | ASSERT(ToRegister(instr->context()).is(esi)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1815 | InstanceofStub stub(InstanceofStub::kArgsInRegisters); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1816 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 1817 | |
| 1818 | NearLabel true_value, done; |
| 1819 | __ test(eax, Operand(eax)); |
| 1820 | __ j(zero, &true_value); |
| 1821 | __ mov(ToRegister(instr->result()), Factory::false_value()); |
| 1822 | __ jmp(&done); |
| 1823 | __ bind(&true_value); |
| 1824 | __ mov(ToRegister(instr->result()), Factory::true_value()); |
| 1825 | __ bind(&done); |
| 1826 | } |
| 1827 | |
| 1828 | |
| 1829 | void LCodeGen::DoInstanceOfAndBranch(LInstanceOfAndBranch* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1830 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1831 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1832 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1833 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1834 | InstanceofStub stub(InstanceofStub::kArgsInRegisters); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1835 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 1836 | __ test(eax, Operand(eax)); |
| 1837 | EmitBranch(true_block, false_block, zero); |
| 1838 | } |
| 1839 | |
| 1840 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1841 | void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) { |
| 1842 | class DeferredInstanceOfKnownGlobal: public LDeferredCode { |
| 1843 | public: |
| 1844 | DeferredInstanceOfKnownGlobal(LCodeGen* codegen, |
| 1845 | LInstanceOfKnownGlobal* instr) |
| 1846 | : LDeferredCode(codegen), instr_(instr) { } |
| 1847 | virtual void Generate() { |
| 1848 | codegen()->DoDeferredLInstanceOfKnownGlobal(instr_, &map_check_); |
| 1849 | } |
| 1850 | |
| 1851 | Label* map_check() { return &map_check_; } |
| 1852 | |
| 1853 | private: |
| 1854 | LInstanceOfKnownGlobal* instr_; |
| 1855 | Label map_check_; |
| 1856 | }; |
| 1857 | |
| 1858 | DeferredInstanceOfKnownGlobal* deferred; |
| 1859 | deferred = new DeferredInstanceOfKnownGlobal(this, instr); |
| 1860 | |
| 1861 | Label done, false_result; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1862 | Register object = ToRegister(instr->InputAt(0)); |
| 1863 | Register temp = ToRegister(instr->TempAt(0)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1864 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 1865 | // A Smi is not an instance of anything. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1866 | __ test(object, Immediate(kSmiTagMask)); |
| 1867 | __ j(zero, &false_result, not_taken); |
| 1868 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 1869 | // This is the inlined call site instanceof cache. The two occurences of the |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1870 | // hole value will be patched to the last map/result pair generated by the |
| 1871 | // instanceof stub. |
| 1872 | NearLabel cache_miss; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1873 | Register map = ToRegister(instr->TempAt(0)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1874 | __ mov(map, FieldOperand(object, HeapObject::kMapOffset)); |
| 1875 | __ bind(deferred->map_check()); // Label for calculating code patching. |
| 1876 | __ cmp(map, Factory::the_hole_value()); // Patched to cached map. |
| 1877 | __ j(not_equal, &cache_miss, not_taken); |
| 1878 | __ mov(eax, Factory::the_hole_value()); // Patched to either true or false. |
| 1879 | __ jmp(&done); |
| 1880 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 1881 | // The inlined call site cache did not match. Check for null and string |
| 1882 | // before calling the deferred code. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1883 | __ bind(&cache_miss); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 1884 | // Null is not an instance of anything. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1885 | __ cmp(object, Factory::null_value()); |
| 1886 | __ j(equal, &false_result); |
| 1887 | |
| 1888 | // String values are not instances of anything. |
| 1889 | Condition is_string = masm_->IsObjectStringType(object, temp, temp); |
| 1890 | __ j(is_string, &false_result); |
| 1891 | |
| 1892 | // Go to the deferred code. |
| 1893 | __ jmp(deferred->entry()); |
| 1894 | |
| 1895 | __ bind(&false_result); |
| 1896 | __ mov(ToRegister(instr->result()), Factory::false_value()); |
| 1897 | |
| 1898 | // Here result has either true or false. Deferred code also produces true or |
| 1899 | // false object. |
| 1900 | __ bind(deferred->exit()); |
| 1901 | __ bind(&done); |
| 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | void LCodeGen::DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr, |
| 1906 | Label* map_check) { |
| 1907 | __ PushSafepointRegisters(); |
| 1908 | |
| 1909 | InstanceofStub::Flags flags = InstanceofStub::kNoFlags; |
| 1910 | flags = static_cast<InstanceofStub::Flags>( |
| 1911 | flags | InstanceofStub::kArgsInRegisters); |
| 1912 | flags = static_cast<InstanceofStub::Flags>( |
| 1913 | flags | InstanceofStub::kCallSiteInlineCheck); |
| 1914 | flags = static_cast<InstanceofStub::Flags>( |
| 1915 | flags | InstanceofStub::kReturnTrueFalseObject); |
| 1916 | InstanceofStub stub(flags); |
| 1917 | |
| 1918 | // Get the temp register reserved by the instruction. This needs to be edi as |
| 1919 | // its slot of the pushing of safepoint registers is used to communicate the |
| 1920 | // offset to the location of the map check. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1921 | Register temp = ToRegister(instr->TempAt(0)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1922 | ASSERT(temp.is(edi)); |
| 1923 | __ mov(InstanceofStub::right(), Immediate(instr->function())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1924 | static const int kAdditionalDelta = 16; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1925 | int delta = masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1926 | __ mov(temp, Immediate(delta)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1927 | __ StoreToSafepointRegisterSlot(temp, temp); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1928 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1929 | // Put the result value into the eax slot and restore all registers. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1930 | __ StoreToSafepointRegisterSlot(eax, eax); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1931 | __ PopSafepointRegisters(); |
| 1932 | } |
| 1933 | |
| 1934 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1935 | static Condition ComputeCompareCondition(Token::Value op) { |
| 1936 | switch (op) { |
| 1937 | case Token::EQ_STRICT: |
| 1938 | case Token::EQ: |
| 1939 | return equal; |
| 1940 | case Token::LT: |
| 1941 | return less; |
| 1942 | case Token::GT: |
| 1943 | return greater; |
| 1944 | case Token::LTE: |
| 1945 | return less_equal; |
| 1946 | case Token::GTE: |
| 1947 | return greater_equal; |
| 1948 | default: |
| 1949 | UNREACHABLE(); |
| 1950 | return no_condition; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | |
| 1955 | void LCodeGen::DoCmpT(LCmpT* instr) { |
| 1956 | Token::Value op = instr->op(); |
| 1957 | |
| 1958 | Handle<Code> ic = CompareIC::GetUninitialized(op); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1959 | CallCode(ic, RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1960 | |
| 1961 | Condition condition = ComputeCompareCondition(op); |
| 1962 | if (op == Token::GT || op == Token::LTE) { |
| 1963 | condition = ReverseCondition(condition); |
| 1964 | } |
| 1965 | NearLabel true_value, done; |
| 1966 | __ test(eax, Operand(eax)); |
| 1967 | __ j(condition, &true_value); |
| 1968 | __ mov(ToRegister(instr->result()), Factory::false_value()); |
| 1969 | __ jmp(&done); |
| 1970 | __ bind(&true_value); |
| 1971 | __ mov(ToRegister(instr->result()), Factory::true_value()); |
| 1972 | __ bind(&done); |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | void LCodeGen::DoCmpTAndBranch(LCmpTAndBranch* instr) { |
| 1977 | Token::Value op = instr->op(); |
| 1978 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 1979 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 1980 | |
| 1981 | Handle<Code> ic = CompareIC::GetUninitialized(op); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1982 | CallCode(ic, RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1983 | |
| 1984 | // The compare stub expects compare condition and the input operands |
| 1985 | // reversed for GT and LTE. |
| 1986 | Condition condition = ComputeCompareCondition(op); |
| 1987 | if (op == Token::GT || op == Token::LTE) { |
| 1988 | condition = ReverseCondition(condition); |
| 1989 | } |
| 1990 | __ test(eax, Operand(eax)); |
| 1991 | EmitBranch(true_block, false_block, condition); |
| 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | void LCodeGen::DoReturn(LReturn* instr) { |
| 1996 | if (FLAG_trace) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1997 | // Preserve the return value on the stack and rely on the runtime call |
| 1998 | // to return the value in the same register. We're leaving the code |
| 1999 | // managed by the register allocator and tearing down the frame, it's |
| 2000 | // safe to write to the context register. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2001 | __ push(eax); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2002 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2003 | __ CallRuntime(Runtime::kTraceExit, 1); |
| 2004 | } |
| 2005 | __ mov(esp, ebp); |
| 2006 | __ pop(ebp); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2007 | __ Ret((ParameterCount() + 1) * kPointerSize, ecx); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | |
| 2011 | void LCodeGen::DoLoadGlobal(LLoadGlobal* instr) { |
| 2012 | Register result = ToRegister(instr->result()); |
| 2013 | __ mov(result, Operand::Cell(instr->hydrogen()->cell())); |
| 2014 | if (instr->hydrogen()->check_hole_value()) { |
| 2015 | __ cmp(result, Factory::the_hole_value()); |
| 2016 | DeoptimizeIf(equal, instr->environment()); |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | void LCodeGen::DoStoreGlobal(LStoreGlobal* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2022 | Register value = ToRegister(instr->InputAt(0)); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 2023 | Operand cell_operand = Operand::Cell(instr->hydrogen()->cell()); |
| 2024 | |
| 2025 | // If the cell we are storing to contains the hole it could have |
| 2026 | // been deleted from the property dictionary. In that case, we need |
| 2027 | // to update the property details in the property dictionary to mark |
| 2028 | // it as no longer deleted. We deoptimize in that case. |
| 2029 | if (instr->hydrogen()->check_hole_value()) { |
| 2030 | __ cmp(cell_operand, Factory::the_hole_value()); |
| 2031 | DeoptimizeIf(equal, instr->environment()); |
| 2032 | } |
| 2033 | |
| 2034 | // Store the value. |
| 2035 | __ mov(cell_operand, value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2039 | void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2040 | Register context = ToRegister(instr->context()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2041 | Register result = ToRegister(instr->result()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2042 | __ mov(result, ContextOperand(context, instr->slot_index())); |
| 2043 | } |
| 2044 | |
| 2045 | |
| 2046 | void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) { |
| 2047 | Register context = ToRegister(instr->context()); |
| 2048 | Register value = ToRegister(instr->value()); |
| 2049 | __ mov(ContextOperand(context, instr->slot_index()), value); |
| 2050 | if (instr->needs_write_barrier()) { |
| 2051 | Register temp = ToRegister(instr->TempAt(0)); |
| 2052 | int offset = Context::SlotOffset(instr->slot_index()); |
| 2053 | __ RecordWrite(context, offset, value, temp); |
| 2054 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2058 | void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2059 | Register object = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2060 | Register result = ToRegister(instr->result()); |
| 2061 | if (instr->hydrogen()->is_in_object()) { |
| 2062 | __ mov(result, FieldOperand(object, instr->hydrogen()->offset())); |
| 2063 | } else { |
| 2064 | __ mov(result, FieldOperand(object, JSObject::kPropertiesOffset)); |
| 2065 | __ mov(result, FieldOperand(result, instr->hydrogen()->offset())); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | |
| 2070 | void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2071 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2072 | ASSERT(ToRegister(instr->object()).is(eax)); |
| 2073 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2074 | |
| 2075 | __ mov(ecx, instr->name()); |
| 2076 | Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 2077 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2078 | } |
| 2079 | |
| 2080 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 2081 | void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) { |
| 2082 | Register function = ToRegister(instr->function()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2083 | Register temp = ToRegister(instr->TempAt(0)); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 2084 | Register result = ToRegister(instr->result()); |
| 2085 | |
| 2086 | // Check that the function really is a function. |
| 2087 | __ CmpObjectType(function, JS_FUNCTION_TYPE, result); |
| 2088 | DeoptimizeIf(not_equal, instr->environment()); |
| 2089 | |
| 2090 | // Check whether the function has an instance prototype. |
| 2091 | NearLabel non_instance; |
| 2092 | __ test_b(FieldOperand(result, Map::kBitFieldOffset), |
| 2093 | 1 << Map::kHasNonInstancePrototype); |
| 2094 | __ j(not_zero, &non_instance); |
| 2095 | |
| 2096 | // Get the prototype or initial map from the function. |
| 2097 | __ mov(result, |
| 2098 | FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
| 2099 | |
| 2100 | // Check that the function has a prototype or an initial map. |
| 2101 | __ cmp(Operand(result), Immediate(Factory::the_hole_value())); |
| 2102 | DeoptimizeIf(equal, instr->environment()); |
| 2103 | |
| 2104 | // If the function does not have an initial map, we're done. |
| 2105 | NearLabel done; |
| 2106 | __ CmpObjectType(result, MAP_TYPE, temp); |
| 2107 | __ j(not_equal, &done); |
| 2108 | |
| 2109 | // Get the prototype from the initial map. |
| 2110 | __ mov(result, FieldOperand(result, Map::kPrototypeOffset)); |
| 2111 | __ jmp(&done); |
| 2112 | |
| 2113 | // Non-instance prototype: Fetch prototype from constructor field |
| 2114 | // in the function's map. |
| 2115 | __ bind(&non_instance); |
| 2116 | __ mov(result, FieldOperand(result, Map::kConstructorOffset)); |
| 2117 | |
| 2118 | // All done. |
| 2119 | __ bind(&done); |
| 2120 | } |
| 2121 | |
| 2122 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2123 | void LCodeGen::DoLoadElements(LLoadElements* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2124 | Register result = ToRegister(instr->result()); |
| 2125 | Register input = ToRegister(instr->InputAt(0)); |
| 2126 | __ mov(result, FieldOperand(input, JSObject::kElementsOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2127 | if (FLAG_debug_code) { |
| 2128 | NearLabel done; |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2129 | __ cmp(FieldOperand(result, HeapObject::kMapOffset), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2130 | Immediate(Factory::fixed_array_map())); |
| 2131 | __ j(equal, &done); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2132 | __ cmp(FieldOperand(result, HeapObject::kMapOffset), |
| 2133 | Immediate(Factory::pixel_array_map())); |
| 2134 | __ j(equal, &done); |
| 2135 | __ cmp(FieldOperand(result, HeapObject::kMapOffset), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2136 | Immediate(Factory::fixed_cow_array_map())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2137 | __ Check(equal, "Check for fast elements or pixel array failed."); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2138 | __ bind(&done); |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2143 | void LCodeGen::DoLoadPixelArrayExternalPointer( |
| 2144 | LLoadPixelArrayExternalPointer* instr) { |
| 2145 | Register result = ToRegister(instr->result()); |
| 2146 | Register input = ToRegister(instr->InputAt(0)); |
| 2147 | __ mov(result, FieldOperand(input, PixelArray::kExternalPointerOffset)); |
| 2148 | } |
| 2149 | |
| 2150 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2151 | void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) { |
| 2152 | Register arguments = ToRegister(instr->arguments()); |
| 2153 | Register length = ToRegister(instr->length()); |
| 2154 | Operand index = ToOperand(instr->index()); |
| 2155 | Register result = ToRegister(instr->result()); |
| 2156 | |
| 2157 | __ sub(length, index); |
| 2158 | DeoptimizeIf(below_equal, instr->environment()); |
| 2159 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2160 | // There are two words between the frame pointer and the last argument. |
| 2161 | // Subtracting from length accounts for one of them add one more. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2162 | __ mov(result, Operand(arguments, length, times_4, kPointerSize)); |
| 2163 | } |
| 2164 | |
| 2165 | |
| 2166 | void LCodeGen::DoLoadKeyedFastElement(LLoadKeyedFastElement* instr) { |
| 2167 | Register elements = ToRegister(instr->elements()); |
| 2168 | Register key = ToRegister(instr->key()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2169 | Register result = ToRegister(instr->result()); |
| 2170 | ASSERT(result.is(elements)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2171 | |
| 2172 | // Load the result. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2173 | __ mov(result, FieldOperand(elements, |
| 2174 | key, |
| 2175 | times_pointer_size, |
| 2176 | FixedArray::kHeaderSize)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2177 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2178 | // Check for the hole value. |
| 2179 | __ cmp(result, Factory::the_hole_value()); |
| 2180 | DeoptimizeIf(equal, instr->environment()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2184 | void LCodeGen::DoLoadPixelArrayElement(LLoadPixelArrayElement* instr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2185 | Register external_pointer = ToRegister(instr->external_pointer()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2186 | Register key = ToRegister(instr->key()); |
| 2187 | Register result = ToRegister(instr->result()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2188 | ASSERT(result.is(external_pointer)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2189 | |
| 2190 | // Load the result. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2191 | __ movzx_b(result, Operand(external_pointer, key, times_1, 0)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2195 | void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2196 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2197 | ASSERT(ToRegister(instr->object()).is(edx)); |
| 2198 | ASSERT(ToRegister(instr->key()).is(eax)); |
| 2199 | |
| 2200 | Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize)); |
| 2201 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2202 | } |
| 2203 | |
| 2204 | |
| 2205 | void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) { |
| 2206 | Register result = ToRegister(instr->result()); |
| 2207 | |
| 2208 | // Check for arguments adapter frame. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2209 | NearLabel done, adapted; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2210 | __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset)); |
| 2211 | __ mov(result, Operand(result, StandardFrameConstants::kContextOffset)); |
| 2212 | __ cmp(Operand(result), |
| 2213 | Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 2214 | __ j(equal, &adapted); |
| 2215 | |
| 2216 | // No arguments adaptor frame. |
| 2217 | __ mov(result, Operand(ebp)); |
| 2218 | __ jmp(&done); |
| 2219 | |
| 2220 | // Arguments adaptor frame present. |
| 2221 | __ bind(&adapted); |
| 2222 | __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset)); |
| 2223 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2224 | // Result is the frame pointer for the frame if not adapted and for the real |
| 2225 | // frame below the adaptor frame if adapted. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2226 | __ bind(&done); |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2231 | Operand elem = ToOperand(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2232 | Register result = ToRegister(instr->result()); |
| 2233 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2234 | NearLabel done; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2235 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2236 | // If no arguments adaptor frame the number of arguments is fixed. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2237 | __ cmp(ebp, elem); |
| 2238 | __ mov(result, Immediate(scope()->num_parameters())); |
| 2239 | __ j(equal, &done); |
| 2240 | |
| 2241 | // Arguments adaptor frame present. Get argument length from there. |
| 2242 | __ mov(result, Operand(ebp, StandardFrameConstants::kCallerFPOffset)); |
| 2243 | __ mov(result, Operand(result, |
| 2244 | ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 2245 | __ SmiUntag(result); |
| 2246 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2247 | // Argument length is in result register. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2248 | __ bind(&done); |
| 2249 | } |
| 2250 | |
| 2251 | |
| 2252 | void LCodeGen::DoApplyArguments(LApplyArguments* instr) { |
| 2253 | Register receiver = ToRegister(instr->receiver()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2254 | Register function = ToRegister(instr->function()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2255 | Register length = ToRegister(instr->length()); |
| 2256 | Register elements = ToRegister(instr->elements()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2257 | Register scratch = ToRegister(instr->TempAt(0)); |
| 2258 | ASSERT(receiver.is(eax)); // Used for parameter count. |
| 2259 | ASSERT(function.is(edi)); // Required by InvokeFunction. |
| 2260 | ASSERT(ToRegister(instr->result()).is(eax)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2261 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2262 | // If the receiver is null or undefined, we have to pass the global object |
| 2263 | // as a receiver. |
| 2264 | NearLabel global_object, receiver_ok; |
| 2265 | __ cmp(receiver, Factory::null_value()); |
| 2266 | __ j(equal, &global_object); |
| 2267 | __ cmp(receiver, Factory::undefined_value()); |
| 2268 | __ j(equal, &global_object); |
| 2269 | |
| 2270 | // The receiver should be a JS object. |
| 2271 | __ test(receiver, Immediate(kSmiTagMask)); |
| 2272 | DeoptimizeIf(equal, instr->environment()); |
| 2273 | __ CmpObjectType(receiver, FIRST_JS_OBJECT_TYPE, scratch); |
| 2274 | DeoptimizeIf(below, instr->environment()); |
| 2275 | __ jmp(&receiver_ok); |
| 2276 | |
| 2277 | __ bind(&global_object); |
| 2278 | // TODO(kmillikin): We have a hydrogen value for the global object. See |
| 2279 | // if it's better to use it than to explicitly fetch it from the context |
| 2280 | // here. |
| 2281 | __ mov(receiver, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 2282 | __ mov(receiver, ContextOperand(receiver, Context::GLOBAL_INDEX)); |
| 2283 | __ bind(&receiver_ok); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2284 | |
| 2285 | // Copy the arguments to this function possibly from the |
| 2286 | // adaptor frame below it. |
| 2287 | const uint32_t kArgumentsLimit = 1 * KB; |
| 2288 | __ cmp(length, kArgumentsLimit); |
| 2289 | DeoptimizeIf(above, instr->environment()); |
| 2290 | |
| 2291 | __ push(receiver); |
| 2292 | __ mov(receiver, length); |
| 2293 | |
| 2294 | // Loop through the arguments pushing them onto the execution |
| 2295 | // stack. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2296 | NearLabel invoke, loop; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2297 | // length is a small non-negative integer, due to the test above. |
| 2298 | __ test(length, Operand(length)); |
| 2299 | __ j(zero, &invoke); |
| 2300 | __ bind(&loop); |
| 2301 | __ push(Operand(elements, length, times_pointer_size, 1 * kPointerSize)); |
| 2302 | __ dec(length); |
| 2303 | __ j(not_zero, &loop); |
| 2304 | |
| 2305 | // Invoke the function. |
| 2306 | __ bind(&invoke); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2307 | ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment()); |
| 2308 | LPointerMap* pointers = instr->pointer_map(); |
| 2309 | LEnvironment* env = instr->deoptimization_environment(); |
| 2310 | RecordPosition(pointers->position()); |
| 2311 | RegisterEnvironmentForDeoptimization(env); |
| 2312 | SafepointGenerator safepoint_generator(this, |
| 2313 | pointers, |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 2314 | env->deoptimization_index(), |
| 2315 | true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2316 | v8::internal::ParameterCount actual(eax); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2317 | __ InvokeFunction(function, actual, CALL_FUNCTION, &safepoint_generator); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | void LCodeGen::DoPushArgument(LPushArgument* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2322 | LOperand* argument = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2323 | if (argument->IsConstantOperand()) { |
| 2324 | __ push(ToImmediate(argument)); |
| 2325 | } else { |
| 2326 | __ push(ToOperand(argument)); |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2331 | void LCodeGen::DoContext(LContext* instr) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2332 | Register result = ToRegister(instr->result()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2333 | __ mov(result, Operand(ebp, StandardFrameConstants::kContextOffset)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | void LCodeGen::DoOuterContext(LOuterContext* instr) { |
| 2338 | Register context = ToRegister(instr->context()); |
| 2339 | Register result = ToRegister(instr->result()); |
| 2340 | __ mov(result, Operand(context, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 2341 | __ mov(result, FieldOperand(result, JSFunction::kContextOffset)); |
| 2342 | } |
| 2343 | |
| 2344 | |
| 2345 | void LCodeGen::DoGlobalObject(LGlobalObject* instr) { |
| 2346 | Register context = ToRegister(instr->context()); |
| 2347 | Register result = ToRegister(instr->result()); |
| 2348 | __ mov(result, Operand(context, Context::SlotOffset(Context::GLOBAL_INDEX))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | |
| 2352 | void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2353 | Register global = ToRegister(instr->global()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2354 | Register result = ToRegister(instr->result()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2355 | __ mov(result, FieldOperand(global, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | void LCodeGen::CallKnownFunction(Handle<JSFunction> function, |
| 2360 | int arity, |
| 2361 | LInstruction* instr) { |
| 2362 | // Change context if needed. |
| 2363 | bool change_context = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 2364 | (info()->closure()->context() != function->context()) || |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2365 | scope()->contains_with() || |
| 2366 | (scope()->num_heap_slots() > 0); |
| 2367 | if (change_context) { |
| 2368 | __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2369 | } else { |
| 2370 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | // Set eax to arguments count if adaption is not needed. Assumes that eax |
| 2374 | // is available to write to at this point. |
| 2375 | if (!function->NeedsArgumentsAdaption()) { |
| 2376 | __ mov(eax, arity); |
| 2377 | } |
| 2378 | |
| 2379 | LPointerMap* pointers = instr->pointer_map(); |
| 2380 | RecordPosition(pointers->position()); |
| 2381 | |
| 2382 | // Invoke function. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 2383 | if (*function == *info()->closure()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2384 | __ CallSelf(); |
| 2385 | } else { |
| 2386 | __ call(FieldOperand(edi, JSFunction::kCodeEntryOffset)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2387 | EnsureRelocSpaceForDeoptimization(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | // Setup deoptimization. |
| 2391 | RegisterLazyDeoptimization(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | |
| 2395 | void LCodeGen::DoCallConstantFunction(LCallConstantFunction* instr) { |
| 2396 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2397 | __ mov(edi, instr->function()); |
| 2398 | CallKnownFunction(instr->function(), instr->arity(), instr); |
| 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2403 | Register input_reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2404 | __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 2405 | Factory::heap_number_map()); |
| 2406 | DeoptimizeIf(not_equal, instr->environment()); |
| 2407 | |
| 2408 | Label done; |
| 2409 | Register tmp = input_reg.is(eax) ? ecx : eax; |
| 2410 | Register tmp2 = tmp.is(ecx) ? edx : input_reg.is(ecx) ? edx : ecx; |
| 2411 | |
| 2412 | // Preserve the value of all registers. |
| 2413 | __ PushSafepointRegisters(); |
| 2414 | |
| 2415 | Label negative; |
| 2416 | __ mov(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2417 | // Check the sign of the argument. If the argument is positive, just |
| 2418 | // return it. We do not need to patch the stack since |input| and |
| 2419 | // |result| are the same register and |input| will be restored |
| 2420 | // unchanged by popping safepoint registers. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2421 | __ test(tmp, Immediate(HeapNumber::kSignMask)); |
| 2422 | __ j(not_zero, &negative); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2423 | __ jmp(&done); |
| 2424 | |
| 2425 | __ bind(&negative); |
| 2426 | |
| 2427 | Label allocated, slow; |
| 2428 | __ AllocateHeapNumber(tmp, tmp2, no_reg, &slow); |
| 2429 | __ jmp(&allocated); |
| 2430 | |
| 2431 | // Slow case: Call the runtime system to do the number allocation. |
| 2432 | __ bind(&slow); |
| 2433 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2434 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2435 | __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber); |
| 2436 | RecordSafepointWithRegisters( |
| 2437 | instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex); |
| 2438 | // Set the pointer to the new heap number in tmp. |
| 2439 | if (!tmp.is(eax)) __ mov(tmp, eax); |
| 2440 | |
| 2441 | // Restore input_reg after call to runtime. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2442 | __ LoadFromSafepointRegisterSlot(input_reg, input_reg); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2443 | |
| 2444 | __ bind(&allocated); |
| 2445 | __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kExponentOffset)); |
| 2446 | __ and_(tmp2, ~HeapNumber::kSignMask); |
| 2447 | __ mov(FieldOperand(tmp, HeapNumber::kExponentOffset), tmp2); |
| 2448 | __ mov(tmp2, FieldOperand(input_reg, HeapNumber::kMantissaOffset)); |
| 2449 | __ mov(FieldOperand(tmp, HeapNumber::kMantissaOffset), tmp2); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2450 | __ StoreToSafepointRegisterSlot(input_reg, tmp); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2451 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2452 | __ bind(&done); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2453 | __ PopSafepointRegisters(); |
| 2454 | } |
| 2455 | |
| 2456 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2457 | void LCodeGen::EmitIntegerMathAbs(LUnaryMathOperation* instr) { |
| 2458 | Register input_reg = ToRegister(instr->InputAt(0)); |
| 2459 | __ test(input_reg, Operand(input_reg)); |
| 2460 | Label is_positive; |
| 2461 | __ j(not_sign, &is_positive); |
| 2462 | __ neg(input_reg); |
| 2463 | __ test(input_reg, Operand(input_reg)); |
| 2464 | DeoptimizeIf(negative, instr->environment()); |
| 2465 | __ bind(&is_positive); |
| 2466 | } |
| 2467 | |
| 2468 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2469 | void LCodeGen::DoMathAbs(LUnaryMathOperation* instr) { |
| 2470 | // Class for deferred case. |
| 2471 | class DeferredMathAbsTaggedHeapNumber: public LDeferredCode { |
| 2472 | public: |
| 2473 | DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, |
| 2474 | LUnaryMathOperation* instr) |
| 2475 | : LDeferredCode(codegen), instr_(instr) { } |
| 2476 | virtual void Generate() { |
| 2477 | codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_); |
| 2478 | } |
| 2479 | private: |
| 2480 | LUnaryMathOperation* instr_; |
| 2481 | }; |
| 2482 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2483 | ASSERT(instr->InputAt(0)->Equals(instr->result())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2484 | Representation r = instr->hydrogen()->value()->representation(); |
| 2485 | |
| 2486 | if (r.IsDouble()) { |
| 2487 | XMMRegister scratch = xmm0; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2488 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2489 | __ pxor(scratch, scratch); |
| 2490 | __ subsd(scratch, input_reg); |
| 2491 | __ pand(input_reg, scratch); |
| 2492 | } else if (r.IsInteger32()) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2493 | EmitIntegerMathAbs(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2494 | } else { // Tagged case. |
| 2495 | DeferredMathAbsTaggedHeapNumber* deferred = |
| 2496 | new DeferredMathAbsTaggedHeapNumber(this, instr); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2497 | Register input_reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2498 | // Smi check. |
| 2499 | __ test(input_reg, Immediate(kSmiTagMask)); |
| 2500 | __ j(not_zero, deferred->entry()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2501 | EmitIntegerMathAbs(instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2502 | __ bind(deferred->exit()); |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) { |
| 2508 | XMMRegister xmm_scratch = xmm0; |
| 2509 | Register output_reg = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2510 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2511 | __ xorpd(xmm_scratch, xmm_scratch); // Zero the register. |
| 2512 | __ ucomisd(input_reg, xmm_scratch); |
| 2513 | |
| 2514 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 2515 | DeoptimizeIf(below_equal, instr->environment()); |
| 2516 | } else { |
| 2517 | DeoptimizeIf(below, instr->environment()); |
| 2518 | } |
| 2519 | |
| 2520 | // Use truncating instruction (OK because input is positive). |
| 2521 | __ cvttsd2si(output_reg, Operand(input_reg)); |
| 2522 | |
| 2523 | // Overflow is signalled with minint. |
| 2524 | __ cmp(output_reg, 0x80000000u); |
| 2525 | DeoptimizeIf(equal, instr->environment()); |
| 2526 | } |
| 2527 | |
| 2528 | |
| 2529 | void LCodeGen::DoMathRound(LUnaryMathOperation* instr) { |
| 2530 | XMMRegister xmm_scratch = xmm0; |
| 2531 | Register output_reg = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2532 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2533 | |
| 2534 | // xmm_scratch = 0.5 |
| 2535 | ExternalReference one_half = ExternalReference::address_of_one_half(); |
| 2536 | __ movdbl(xmm_scratch, Operand::StaticVariable(one_half)); |
| 2537 | |
| 2538 | // input = input + 0.5 |
| 2539 | __ addsd(input_reg, xmm_scratch); |
| 2540 | |
| 2541 | // We need to return -0 for the input range [-0.5, 0[, otherwise |
| 2542 | // compute Math.floor(value + 0.5). |
| 2543 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 2544 | __ ucomisd(input_reg, xmm_scratch); |
| 2545 | DeoptimizeIf(below_equal, instr->environment()); |
| 2546 | } else { |
| 2547 | // If we don't need to bailout on -0, we check only bailout |
| 2548 | // on negative inputs. |
| 2549 | __ xorpd(xmm_scratch, xmm_scratch); // Zero the register. |
| 2550 | __ ucomisd(input_reg, xmm_scratch); |
| 2551 | DeoptimizeIf(below, instr->environment()); |
| 2552 | } |
| 2553 | |
| 2554 | // Compute Math.floor(value + 0.5). |
| 2555 | // Use truncating instruction (OK because input is positive). |
| 2556 | __ cvttsd2si(output_reg, Operand(input_reg)); |
| 2557 | |
| 2558 | // Overflow is signalled with minint. |
| 2559 | __ cmp(output_reg, 0x80000000u); |
| 2560 | DeoptimizeIf(equal, instr->environment()); |
| 2561 | } |
| 2562 | |
| 2563 | |
| 2564 | void LCodeGen::DoMathSqrt(LUnaryMathOperation* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2565 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2566 | ASSERT(ToDoubleRegister(instr->result()).is(input_reg)); |
| 2567 | __ sqrtsd(input_reg, input_reg); |
| 2568 | } |
| 2569 | |
| 2570 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2571 | void LCodeGen::DoMathPowHalf(LUnaryMathOperation* instr) { |
| 2572 | XMMRegister xmm_scratch = xmm0; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2573 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2574 | ASSERT(ToDoubleRegister(instr->result()).is(input_reg)); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2575 | __ xorpd(xmm_scratch, xmm_scratch); |
| 2576 | __ addsd(input_reg, xmm_scratch); // Convert -0 to +0. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2577 | __ sqrtsd(input_reg, input_reg); |
| 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | void LCodeGen::DoPower(LPower* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2582 | LOperand* left = instr->InputAt(0); |
| 2583 | LOperand* right = instr->InputAt(1); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2584 | DoubleRegister result_reg = ToDoubleRegister(instr->result()); |
| 2585 | Representation exponent_type = instr->hydrogen()->right()->representation(); |
| 2586 | if (exponent_type.IsDouble()) { |
| 2587 | // It is safe to use ebx directly since the instruction is marked |
| 2588 | // as a call. |
| 2589 | __ PrepareCallCFunction(4, ebx); |
| 2590 | __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left)); |
| 2591 | __ movdbl(Operand(esp, 1 * kDoubleSize), ToDoubleRegister(right)); |
| 2592 | __ CallCFunction(ExternalReference::power_double_double_function(), 4); |
| 2593 | } else if (exponent_type.IsInteger32()) { |
| 2594 | // It is safe to use ebx directly since the instruction is marked |
| 2595 | // as a call. |
| 2596 | ASSERT(!ToRegister(right).is(ebx)); |
| 2597 | __ PrepareCallCFunction(4, ebx); |
| 2598 | __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left)); |
| 2599 | __ mov(Operand(esp, 1 * kDoubleSize), ToRegister(right)); |
| 2600 | __ CallCFunction(ExternalReference::power_double_int_function(), 4); |
| 2601 | } else { |
| 2602 | ASSERT(exponent_type.IsTagged()); |
| 2603 | CpuFeatures::Scope scope(SSE2); |
| 2604 | Register right_reg = ToRegister(right); |
| 2605 | |
| 2606 | Label non_smi, call; |
| 2607 | __ test(right_reg, Immediate(kSmiTagMask)); |
| 2608 | __ j(not_zero, &non_smi); |
| 2609 | __ SmiUntag(right_reg); |
| 2610 | __ cvtsi2sd(result_reg, Operand(right_reg)); |
| 2611 | __ jmp(&call); |
| 2612 | |
| 2613 | __ bind(&non_smi); |
| 2614 | // It is safe to use ebx directly since the instruction is marked |
| 2615 | // as a call. |
| 2616 | ASSERT(!right_reg.is(ebx)); |
| 2617 | __ CmpObjectType(right_reg, HEAP_NUMBER_TYPE , ebx); |
| 2618 | DeoptimizeIf(not_equal, instr->environment()); |
| 2619 | __ movdbl(result_reg, FieldOperand(right_reg, HeapNumber::kValueOffset)); |
| 2620 | |
| 2621 | __ bind(&call); |
| 2622 | __ PrepareCallCFunction(4, ebx); |
| 2623 | __ movdbl(Operand(esp, 0 * kDoubleSize), ToDoubleRegister(left)); |
| 2624 | __ movdbl(Operand(esp, 1 * kDoubleSize), result_reg); |
| 2625 | __ CallCFunction(ExternalReference::power_double_double_function(), 4); |
| 2626 | } |
| 2627 | |
| 2628 | // Return value is in st(0) on ia32. |
| 2629 | // Store it into the (fixed) result register. |
| 2630 | __ sub(Operand(esp), Immediate(kDoubleSize)); |
| 2631 | __ fstp_d(Operand(esp, 0)); |
| 2632 | __ movdbl(result_reg, Operand(esp, 0)); |
| 2633 | __ add(Operand(esp), Immediate(kDoubleSize)); |
| 2634 | } |
| 2635 | |
| 2636 | |
| 2637 | void LCodeGen::DoMathLog(LUnaryMathOperation* instr) { |
| 2638 | ASSERT(ToDoubleRegister(instr->result()).is(xmm1)); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2639 | TranscendentalCacheStub stub(TranscendentalCache::LOG, |
| 2640 | TranscendentalCacheStub::UNTAGGED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2641 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | |
| 2645 | void LCodeGen::DoMathCos(LUnaryMathOperation* instr) { |
| 2646 | ASSERT(ToDoubleRegister(instr->result()).is(xmm1)); |
| 2647 | TranscendentalCacheStub stub(TranscendentalCache::COS, |
| 2648 | TranscendentalCacheStub::UNTAGGED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2649 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | |
| 2653 | void LCodeGen::DoMathSin(LUnaryMathOperation* instr) { |
| 2654 | ASSERT(ToDoubleRegister(instr->result()).is(xmm1)); |
| 2655 | TranscendentalCacheStub stub(TranscendentalCache::SIN, |
| 2656 | TranscendentalCacheStub::UNTAGGED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2657 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2661 | void LCodeGen::DoUnaryMathOperation(LUnaryMathOperation* instr) { |
| 2662 | switch (instr->op()) { |
| 2663 | case kMathAbs: |
| 2664 | DoMathAbs(instr); |
| 2665 | break; |
| 2666 | case kMathFloor: |
| 2667 | DoMathFloor(instr); |
| 2668 | break; |
| 2669 | case kMathRound: |
| 2670 | DoMathRound(instr); |
| 2671 | break; |
| 2672 | case kMathSqrt: |
| 2673 | DoMathSqrt(instr); |
| 2674 | break; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2675 | case kMathPowHalf: |
| 2676 | DoMathPowHalf(instr); |
| 2677 | break; |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2678 | case kMathCos: |
| 2679 | DoMathCos(instr); |
| 2680 | break; |
| 2681 | case kMathSin: |
| 2682 | DoMathSin(instr); |
| 2683 | break; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2684 | case kMathLog: |
| 2685 | DoMathLog(instr); |
| 2686 | break; |
| 2687 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2688 | default: |
| 2689 | UNREACHABLE(); |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | void LCodeGen::DoCallKeyed(LCallKeyed* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2695 | ASSERT(ToRegister(instr->context()).is(esi)); |
| 2696 | ASSERT(ToRegister(instr->key()).is(ecx)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2697 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2698 | |
| 2699 | int arity = instr->arity(); |
| 2700 | Handle<Code> ic = StubCache::ComputeKeyedCallInitialize(arity, NOT_IN_LOOP); |
| 2701 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | |
| 2705 | void LCodeGen::DoCallNamed(LCallNamed* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2706 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2707 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2708 | |
| 2709 | int arity = instr->arity(); |
| 2710 | Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP); |
| 2711 | __ mov(ecx, instr->name()); |
| 2712 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | void LCodeGen::DoCallFunction(LCallFunction* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2717 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2718 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2719 | |
| 2720 | int arity = instr->arity(); |
| 2721 | CallFunctionStub stub(arity, NOT_IN_LOOP, RECEIVER_MIGHT_BE_VALUE); |
| 2722 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr); |
| 2723 | __ Drop(1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | void LCodeGen::DoCallGlobal(LCallGlobal* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2728 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2729 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2730 | |
| 2731 | int arity = instr->arity(); |
| 2732 | Handle<Code> ic = StubCache::ComputeCallInitialize(arity, NOT_IN_LOOP); |
| 2733 | __ mov(ecx, instr->name()); |
| 2734 | CallCode(ic, RelocInfo::CODE_TARGET_CONTEXT, instr); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | |
| 2738 | void LCodeGen::DoCallKnownGlobal(LCallKnownGlobal* instr) { |
| 2739 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2740 | __ mov(edi, instr->target()); |
| 2741 | CallKnownFunction(instr->target(), instr->arity(), instr); |
| 2742 | } |
| 2743 | |
| 2744 | |
| 2745 | void LCodeGen::DoCallNew(LCallNew* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2746 | ASSERT(ToRegister(instr->context()).is(esi)); |
| 2747 | ASSERT(ToRegister(instr->constructor()).is(edi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2748 | ASSERT(ToRegister(instr->result()).is(eax)); |
| 2749 | |
| 2750 | Handle<Code> builtin(Builtins::builtin(Builtins::JSConstructCall)); |
| 2751 | __ Set(eax, Immediate(instr->arity())); |
| 2752 | CallCode(builtin, RelocInfo::CONSTRUCT_CALL, instr); |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | void LCodeGen::DoCallRuntime(LCallRuntime* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2757 | CallRuntime(instr->function(), instr->arity(), instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | |
| 2761 | void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) { |
| 2762 | Register object = ToRegister(instr->object()); |
| 2763 | Register value = ToRegister(instr->value()); |
| 2764 | int offset = instr->offset(); |
| 2765 | |
| 2766 | if (!instr->transition().is_null()) { |
| 2767 | __ mov(FieldOperand(object, HeapObject::kMapOffset), instr->transition()); |
| 2768 | } |
| 2769 | |
| 2770 | // Do the store. |
| 2771 | if (instr->is_in_object()) { |
| 2772 | __ mov(FieldOperand(object, offset), value); |
| 2773 | if (instr->needs_write_barrier()) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2774 | Register temp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2775 | // Update the write barrier for the object for in-object properties. |
| 2776 | __ RecordWrite(object, offset, value, temp); |
| 2777 | } |
| 2778 | } else { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 2779 | Register temp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2780 | __ mov(temp, FieldOperand(object, JSObject::kPropertiesOffset)); |
| 2781 | __ mov(FieldOperand(temp, offset), value); |
| 2782 | if (instr->needs_write_barrier()) { |
| 2783 | // Update the write barrier for the properties array. |
| 2784 | // object is used as a scratch register. |
| 2785 | __ RecordWrite(temp, offset, value, object); |
| 2786 | } |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | |
| 2791 | void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2792 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2793 | ASSERT(ToRegister(instr->object()).is(edx)); |
| 2794 | ASSERT(ToRegister(instr->value()).is(eax)); |
| 2795 | |
| 2796 | __ mov(ecx, instr->name()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2797 | Handle<Code> ic(Builtins::builtin( |
| 2798 | info_->is_strict() ? Builtins::StoreIC_Initialize_Strict |
| 2799 | : Builtins::StoreIC_Initialize)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2800 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2801 | } |
| 2802 | |
| 2803 | |
| 2804 | void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) { |
| 2805 | __ cmp(ToRegister(instr->index()), ToOperand(instr->length())); |
| 2806 | DeoptimizeIf(above_equal, instr->environment()); |
| 2807 | } |
| 2808 | |
| 2809 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2810 | void LCodeGen::DoStorePixelArrayElement(LStorePixelArrayElement* instr) { |
| 2811 | Register external_pointer = ToRegister(instr->external_pointer()); |
| 2812 | Register key = ToRegister(instr->key()); |
| 2813 | Register value = ToRegister(instr->value()); |
| 2814 | ASSERT(ToRegister(instr->TempAt(0)).is(eax)); |
| 2815 | |
| 2816 | __ mov(eax, value); |
| 2817 | { // Clamp the value to [0..255]. |
| 2818 | NearLabel done; |
| 2819 | __ test(eax, Immediate(0xFFFFFF00)); |
| 2820 | __ j(zero, &done); |
| 2821 | __ setcc(negative, eax); // 1 if negative, 0 if positive. |
| 2822 | __ dec_b(eax); // 0 if negative, 255 if positive. |
| 2823 | __ bind(&done); |
| 2824 | } |
| 2825 | __ mov_b(Operand(external_pointer, key, times_1, 0), eax); |
| 2826 | } |
| 2827 | |
| 2828 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2829 | void LCodeGen::DoStoreKeyedFastElement(LStoreKeyedFastElement* instr) { |
| 2830 | Register value = ToRegister(instr->value()); |
| 2831 | Register elements = ToRegister(instr->object()); |
| 2832 | Register key = instr->key()->IsRegister() ? ToRegister(instr->key()) : no_reg; |
| 2833 | |
| 2834 | // Do the store. |
| 2835 | if (instr->key()->IsConstantOperand()) { |
| 2836 | ASSERT(!instr->hydrogen()->NeedsWriteBarrier()); |
| 2837 | LConstantOperand* const_operand = LConstantOperand::cast(instr->key()); |
| 2838 | int offset = |
| 2839 | ToInteger32(const_operand) * kPointerSize + FixedArray::kHeaderSize; |
| 2840 | __ mov(FieldOperand(elements, offset), value); |
| 2841 | } else { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2842 | __ mov(FieldOperand(elements, |
| 2843 | key, |
| 2844 | times_pointer_size, |
| 2845 | FixedArray::kHeaderSize), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2846 | value); |
| 2847 | } |
| 2848 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2849 | if (instr->hydrogen()->NeedsWriteBarrier()) { |
| 2850 | // Compute address of modified element and store it into key register. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2851 | __ lea(key, |
| 2852 | FieldOperand(elements, |
| 2853 | key, |
| 2854 | times_pointer_size, |
| 2855 | FixedArray::kHeaderSize)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2856 | __ RecordWrite(elements, key, value); |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | |
| 2861 | void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2862 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2863 | ASSERT(ToRegister(instr->object()).is(edx)); |
| 2864 | ASSERT(ToRegister(instr->key()).is(ecx)); |
| 2865 | ASSERT(ToRegister(instr->value()).is(eax)); |
| 2866 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2867 | Handle<Code> ic(Builtins::builtin( |
| 2868 | info_->is_strict() ? Builtins::KeyedStoreIC_Initialize_Strict |
| 2869 | : Builtins::KeyedStoreIC_Initialize)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2870 | CallCode(ic, RelocInfo::CODE_TARGET, instr); |
| 2871 | } |
| 2872 | |
| 2873 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2874 | void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) { |
| 2875 | class DeferredStringCharCodeAt: public LDeferredCode { |
| 2876 | public: |
| 2877 | DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr) |
| 2878 | : LDeferredCode(codegen), instr_(instr) { } |
| 2879 | virtual void Generate() { codegen()->DoDeferredStringCharCodeAt(instr_); } |
| 2880 | private: |
| 2881 | LStringCharCodeAt* instr_; |
| 2882 | }; |
| 2883 | |
| 2884 | Register string = ToRegister(instr->string()); |
| 2885 | Register index = no_reg; |
| 2886 | int const_index = -1; |
| 2887 | if (instr->index()->IsConstantOperand()) { |
| 2888 | const_index = ToInteger32(LConstantOperand::cast(instr->index())); |
| 2889 | STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue); |
| 2890 | if (!Smi::IsValid(const_index)) { |
| 2891 | // Guaranteed to be out of bounds because of the assert above. |
| 2892 | // So the bounds check that must dominate this instruction must |
| 2893 | // have deoptimized already. |
| 2894 | if (FLAG_debug_code) { |
| 2895 | __ Abort("StringCharCodeAt: out of bounds index."); |
| 2896 | } |
| 2897 | // No code needs to be generated. |
| 2898 | return; |
| 2899 | } |
| 2900 | } else { |
| 2901 | index = ToRegister(instr->index()); |
| 2902 | } |
| 2903 | Register result = ToRegister(instr->result()); |
| 2904 | |
| 2905 | DeferredStringCharCodeAt* deferred = |
| 2906 | new DeferredStringCharCodeAt(this, instr); |
| 2907 | |
| 2908 | NearLabel flat_string, ascii_string, done; |
| 2909 | |
| 2910 | // Fetch the instance type of the receiver into result register. |
| 2911 | __ mov(result, FieldOperand(string, HeapObject::kMapOffset)); |
| 2912 | __ movzx_b(result, FieldOperand(result, Map::kInstanceTypeOffset)); |
| 2913 | |
| 2914 | // We need special handling for non-flat strings. |
| 2915 | STATIC_ASSERT(kSeqStringTag == 0); |
| 2916 | __ test(result, Immediate(kStringRepresentationMask)); |
| 2917 | __ j(zero, &flat_string); |
| 2918 | |
| 2919 | // Handle non-flat strings. |
| 2920 | __ test(result, Immediate(kIsConsStringMask)); |
| 2921 | __ j(zero, deferred->entry()); |
| 2922 | |
| 2923 | // ConsString. |
| 2924 | // Check whether the right hand side is the empty string (i.e. if |
| 2925 | // this is really a flat string in a cons string). If that is not |
| 2926 | // the case we would rather go to the runtime system now to flatten |
| 2927 | // the string. |
| 2928 | __ cmp(FieldOperand(string, ConsString::kSecondOffset), |
| 2929 | Immediate(Factory::empty_string())); |
| 2930 | __ j(not_equal, deferred->entry()); |
| 2931 | // Get the first of the two strings and load its instance type. |
| 2932 | __ mov(string, FieldOperand(string, ConsString::kFirstOffset)); |
| 2933 | __ mov(result, FieldOperand(string, HeapObject::kMapOffset)); |
| 2934 | __ movzx_b(result, FieldOperand(result, Map::kInstanceTypeOffset)); |
| 2935 | // If the first cons component is also non-flat, then go to runtime. |
| 2936 | STATIC_ASSERT(kSeqStringTag == 0); |
| 2937 | __ test(result, Immediate(kStringRepresentationMask)); |
| 2938 | __ j(not_zero, deferred->entry()); |
| 2939 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2940 | // Check for ASCII or two-byte string. |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2941 | __ bind(&flat_string); |
| 2942 | STATIC_ASSERT(kAsciiStringTag != 0); |
| 2943 | __ test(result, Immediate(kStringEncodingMask)); |
| 2944 | __ j(not_zero, &ascii_string); |
| 2945 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2946 | // Two-byte string. |
| 2947 | // Load the two-byte character code into the result register. |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2948 | STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1); |
| 2949 | if (instr->index()->IsConstantOperand()) { |
| 2950 | __ movzx_w(result, |
| 2951 | FieldOperand(string, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 2952 | SeqTwoByteString::kHeaderSize + |
| 2953 | (kUC16Size * const_index))); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 2954 | } else { |
| 2955 | __ movzx_w(result, FieldOperand(string, |
| 2956 | index, |
| 2957 | times_2, |
| 2958 | SeqTwoByteString::kHeaderSize)); |
| 2959 | } |
| 2960 | __ jmp(&done); |
| 2961 | |
| 2962 | // ASCII string. |
| 2963 | // Load the byte into the result register. |
| 2964 | __ bind(&ascii_string); |
| 2965 | if (instr->index()->IsConstantOperand()) { |
| 2966 | __ movzx_b(result, FieldOperand(string, |
| 2967 | SeqAsciiString::kHeaderSize + const_index)); |
| 2968 | } else { |
| 2969 | __ movzx_b(result, FieldOperand(string, |
| 2970 | index, |
| 2971 | times_1, |
| 2972 | SeqAsciiString::kHeaderSize)); |
| 2973 | } |
| 2974 | __ bind(&done); |
| 2975 | __ bind(deferred->exit()); |
| 2976 | } |
| 2977 | |
| 2978 | |
| 2979 | void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) { |
| 2980 | Register string = ToRegister(instr->string()); |
| 2981 | Register result = ToRegister(instr->result()); |
| 2982 | |
| 2983 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 2984 | // result register contain a valid pointer because it is already |
| 2985 | // contained in the register pointer map. |
| 2986 | __ Set(result, Immediate(0)); |
| 2987 | |
| 2988 | __ PushSafepointRegisters(); |
| 2989 | __ push(string); |
| 2990 | // Push the index as a smi. This is safe because of the checks in |
| 2991 | // DoStringCharCodeAt above. |
| 2992 | STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue); |
| 2993 | if (instr->index()->IsConstantOperand()) { |
| 2994 | int const_index = ToInteger32(LConstantOperand::cast(instr->index())); |
| 2995 | __ push(Immediate(Smi::FromInt(const_index))); |
| 2996 | } else { |
| 2997 | Register index = ToRegister(instr->index()); |
| 2998 | __ SmiTag(index); |
| 2999 | __ push(index); |
| 3000 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3001 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3002 | __ CallRuntimeSaveDoubles(Runtime::kStringCharCodeAt); |
| 3003 | RecordSafepointWithRegisters( |
| 3004 | instr->pointer_map(), 2, Safepoint::kNoDeoptimizationIndex); |
| 3005 | if (FLAG_debug_code) { |
| 3006 | __ AbortIfNotSmi(eax); |
| 3007 | } |
| 3008 | __ SmiUntag(eax); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3009 | __ StoreToSafepointRegisterSlot(result, eax); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3010 | __ PopSafepointRegisters(); |
| 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | void LCodeGen::DoStringLength(LStringLength* instr) { |
| 3015 | Register string = ToRegister(instr->string()); |
| 3016 | Register result = ToRegister(instr->result()); |
| 3017 | __ mov(result, FieldOperand(string, String::kLengthOffset)); |
| 3018 | } |
| 3019 | |
| 3020 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3021 | void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3022 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3023 | ASSERT(input->IsRegister() || input->IsStackSlot()); |
| 3024 | LOperand* output = instr->result(); |
| 3025 | ASSERT(output->IsDoubleRegister()); |
| 3026 | __ cvtsi2sd(ToDoubleRegister(output), ToOperand(input)); |
| 3027 | } |
| 3028 | |
| 3029 | |
| 3030 | void LCodeGen::DoNumberTagI(LNumberTagI* instr) { |
| 3031 | class DeferredNumberTagI: public LDeferredCode { |
| 3032 | public: |
| 3033 | DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr) |
| 3034 | : LDeferredCode(codegen), instr_(instr) { } |
| 3035 | virtual void Generate() { codegen()->DoDeferredNumberTagI(instr_); } |
| 3036 | private: |
| 3037 | LNumberTagI* instr_; |
| 3038 | }; |
| 3039 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3040 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3041 | ASSERT(input->IsRegister() && input->Equals(instr->result())); |
| 3042 | Register reg = ToRegister(input); |
| 3043 | |
| 3044 | DeferredNumberTagI* deferred = new DeferredNumberTagI(this, instr); |
| 3045 | __ SmiTag(reg); |
| 3046 | __ j(overflow, deferred->entry()); |
| 3047 | __ bind(deferred->exit()); |
| 3048 | } |
| 3049 | |
| 3050 | |
| 3051 | void LCodeGen::DoDeferredNumberTagI(LNumberTagI* instr) { |
| 3052 | Label slow; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3053 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3054 | Register tmp = reg.is(eax) ? ecx : eax; |
| 3055 | |
| 3056 | // Preserve the value of all registers. |
| 3057 | __ PushSafepointRegisters(); |
| 3058 | |
| 3059 | // There was overflow, so bits 30 and 31 of the original integer |
| 3060 | // disagree. Try to allocate a heap number in new space and store |
| 3061 | // the value in there. If that fails, call the runtime system. |
| 3062 | NearLabel done; |
| 3063 | __ SmiUntag(reg); |
| 3064 | __ xor_(reg, 0x80000000); |
| 3065 | __ cvtsi2sd(xmm0, Operand(reg)); |
| 3066 | if (FLAG_inline_new) { |
| 3067 | __ AllocateHeapNumber(reg, tmp, no_reg, &slow); |
| 3068 | __ jmp(&done); |
| 3069 | } |
| 3070 | |
| 3071 | // Slow case: Call the runtime system to do the number allocation. |
| 3072 | __ bind(&slow); |
| 3073 | |
| 3074 | // TODO(3095996): Put a valid pointer value in the stack slot where the result |
| 3075 | // register is stored, as this register is in the pointer map, but contains an |
| 3076 | // integer value. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3077 | __ StoreToSafepointRegisterSlot(reg, Immediate(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3078 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3079 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3080 | __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber); |
| 3081 | RecordSafepointWithRegisters( |
| 3082 | instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex); |
| 3083 | if (!reg.is(eax)) __ mov(reg, eax); |
| 3084 | |
| 3085 | // Done. Put the value in xmm0 into the value of the allocated heap |
| 3086 | // number. |
| 3087 | __ bind(&done); |
| 3088 | __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), xmm0); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3089 | __ StoreToSafepointRegisterSlot(reg, reg); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3090 | __ PopSafepointRegisters(); |
| 3091 | } |
| 3092 | |
| 3093 | |
| 3094 | void LCodeGen::DoNumberTagD(LNumberTagD* instr) { |
| 3095 | class DeferredNumberTagD: public LDeferredCode { |
| 3096 | public: |
| 3097 | DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr) |
| 3098 | : LDeferredCode(codegen), instr_(instr) { } |
| 3099 | virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); } |
| 3100 | private: |
| 3101 | LNumberTagD* instr_; |
| 3102 | }; |
| 3103 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3104 | XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3105 | Register reg = ToRegister(instr->result()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3106 | Register tmp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3107 | |
| 3108 | DeferredNumberTagD* deferred = new DeferredNumberTagD(this, instr); |
| 3109 | if (FLAG_inline_new) { |
| 3110 | __ AllocateHeapNumber(reg, tmp, no_reg, deferred->entry()); |
| 3111 | } else { |
| 3112 | __ jmp(deferred->entry()); |
| 3113 | } |
| 3114 | __ bind(deferred->exit()); |
| 3115 | __ movdbl(FieldOperand(reg, HeapNumber::kValueOffset), input_reg); |
| 3116 | } |
| 3117 | |
| 3118 | |
| 3119 | void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) { |
| 3120 | // TODO(3095996): Get rid of this. For now, we need to make the |
| 3121 | // result register contain a valid pointer because it is already |
| 3122 | // contained in the register pointer map. |
| 3123 | Register reg = ToRegister(instr->result()); |
| 3124 | __ Set(reg, Immediate(0)); |
| 3125 | |
| 3126 | __ PushSafepointRegisters(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3127 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3128 | __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber); |
| 3129 | RecordSafepointWithRegisters( |
| 3130 | instr->pointer_map(), 0, Safepoint::kNoDeoptimizationIndex); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3131 | __ StoreToSafepointRegisterSlot(reg, eax); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3132 | __ PopSafepointRegisters(); |
| 3133 | } |
| 3134 | |
| 3135 | |
| 3136 | void LCodeGen::DoSmiTag(LSmiTag* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3137 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3138 | ASSERT(input->IsRegister() && input->Equals(instr->result())); |
| 3139 | ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow)); |
| 3140 | __ SmiTag(ToRegister(input)); |
| 3141 | } |
| 3142 | |
| 3143 | |
| 3144 | void LCodeGen::DoSmiUntag(LSmiUntag* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3145 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3146 | ASSERT(input->IsRegister() && input->Equals(instr->result())); |
| 3147 | if (instr->needs_check()) { |
| 3148 | __ test(ToRegister(input), Immediate(kSmiTagMask)); |
| 3149 | DeoptimizeIf(not_zero, instr->environment()); |
| 3150 | } |
| 3151 | __ SmiUntag(ToRegister(input)); |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | void LCodeGen::EmitNumberUntagD(Register input_reg, |
| 3156 | XMMRegister result_reg, |
| 3157 | LEnvironment* env) { |
| 3158 | NearLabel load_smi, heap_number, done; |
| 3159 | |
| 3160 | // Smi check. |
| 3161 | __ test(input_reg, Immediate(kSmiTagMask)); |
| 3162 | __ j(zero, &load_smi, not_taken); |
| 3163 | |
| 3164 | // Heap number map check. |
| 3165 | __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 3166 | Factory::heap_number_map()); |
| 3167 | __ j(equal, &heap_number); |
| 3168 | |
| 3169 | __ cmp(input_reg, Factory::undefined_value()); |
| 3170 | DeoptimizeIf(not_equal, env); |
| 3171 | |
| 3172 | // Convert undefined to NaN. |
| 3173 | __ push(input_reg); |
| 3174 | __ mov(input_reg, Factory::nan_value()); |
| 3175 | __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3176 | __ pop(input_reg); |
| 3177 | __ jmp(&done); |
| 3178 | |
| 3179 | // Heap number to XMM conversion. |
| 3180 | __ bind(&heap_number); |
| 3181 | __ movdbl(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3182 | __ jmp(&done); |
| 3183 | |
| 3184 | // Smi to XMM conversion |
| 3185 | __ bind(&load_smi); |
| 3186 | __ SmiUntag(input_reg); // Untag smi before converting to float. |
| 3187 | __ cvtsi2sd(result_reg, Operand(input_reg)); |
| 3188 | __ SmiTag(input_reg); // Retag smi. |
| 3189 | __ bind(&done); |
| 3190 | } |
| 3191 | |
| 3192 | |
| 3193 | class DeferredTaggedToI: public LDeferredCode { |
| 3194 | public: |
| 3195 | DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr) |
| 3196 | : LDeferredCode(codegen), instr_(instr) { } |
| 3197 | virtual void Generate() { codegen()->DoDeferredTaggedToI(instr_); } |
| 3198 | private: |
| 3199 | LTaggedToI* instr_; |
| 3200 | }; |
| 3201 | |
| 3202 | |
| 3203 | void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) { |
| 3204 | NearLabel done, heap_number; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3205 | Register input_reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3206 | |
| 3207 | // Heap number map check. |
| 3208 | __ cmp(FieldOperand(input_reg, HeapObject::kMapOffset), |
| 3209 | Factory::heap_number_map()); |
| 3210 | |
| 3211 | if (instr->truncating()) { |
| 3212 | __ j(equal, &heap_number); |
| 3213 | // Check for undefined. Undefined is converted to zero for truncating |
| 3214 | // conversions. |
| 3215 | __ cmp(input_reg, Factory::undefined_value()); |
| 3216 | DeoptimizeIf(not_equal, instr->environment()); |
| 3217 | __ mov(input_reg, 0); |
| 3218 | __ jmp(&done); |
| 3219 | |
| 3220 | __ bind(&heap_number); |
| 3221 | if (CpuFeatures::IsSupported(SSE3)) { |
| 3222 | CpuFeatures::Scope scope(SSE3); |
| 3223 | NearLabel convert; |
| 3224 | // Use more powerful conversion when sse3 is available. |
| 3225 | // Load x87 register with heap number. |
| 3226 | __ fld_d(FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3227 | // Get exponent alone and check for too-big exponent. |
| 3228 | __ mov(input_reg, FieldOperand(input_reg, HeapNumber::kExponentOffset)); |
| 3229 | __ and_(input_reg, HeapNumber::kExponentMask); |
| 3230 | const uint32_t kTooBigExponent = |
| 3231 | (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift; |
| 3232 | __ cmp(Operand(input_reg), Immediate(kTooBigExponent)); |
| 3233 | __ j(less, &convert); |
| 3234 | // Pop FPU stack before deoptimizing. |
| 3235 | __ ffree(0); |
| 3236 | __ fincstp(); |
| 3237 | DeoptimizeIf(no_condition, instr->environment()); |
| 3238 | |
| 3239 | // Reserve space for 64 bit answer. |
| 3240 | __ bind(&convert); |
| 3241 | __ sub(Operand(esp), Immediate(kDoubleSize)); |
| 3242 | // Do conversion, which cannot fail because we checked the exponent. |
| 3243 | __ fisttp_d(Operand(esp, 0)); |
| 3244 | __ mov(input_reg, Operand(esp, 0)); // Low word of answer is the result. |
| 3245 | __ add(Operand(esp), Immediate(kDoubleSize)); |
| 3246 | } else { |
| 3247 | NearLabel deopt; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3248 | XMMRegister xmm_temp = ToDoubleRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3249 | __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3250 | __ cvttsd2si(input_reg, Operand(xmm0)); |
| 3251 | __ cmp(input_reg, 0x80000000u); |
| 3252 | __ j(not_equal, &done); |
| 3253 | // Check if the input was 0x8000000 (kMinInt). |
| 3254 | // If no, then we got an overflow and we deoptimize. |
| 3255 | ExternalReference min_int = ExternalReference::address_of_min_int(); |
| 3256 | __ movdbl(xmm_temp, Operand::StaticVariable(min_int)); |
| 3257 | __ ucomisd(xmm_temp, xmm0); |
| 3258 | DeoptimizeIf(not_equal, instr->environment()); |
| 3259 | DeoptimizeIf(parity_even, instr->environment()); // NaN. |
| 3260 | } |
| 3261 | } else { |
| 3262 | // Deoptimize if we don't have a heap number. |
| 3263 | DeoptimizeIf(not_equal, instr->environment()); |
| 3264 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3265 | XMMRegister xmm_temp = ToDoubleRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3266 | __ movdbl(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset)); |
| 3267 | __ cvttsd2si(input_reg, Operand(xmm0)); |
| 3268 | __ cvtsi2sd(xmm_temp, Operand(input_reg)); |
| 3269 | __ ucomisd(xmm0, xmm_temp); |
| 3270 | DeoptimizeIf(not_equal, instr->environment()); |
| 3271 | DeoptimizeIf(parity_even, instr->environment()); // NaN. |
| 3272 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 3273 | __ test(input_reg, Operand(input_reg)); |
| 3274 | __ j(not_zero, &done); |
| 3275 | __ movmskpd(input_reg, xmm0); |
| 3276 | __ and_(input_reg, 1); |
| 3277 | DeoptimizeIf(not_zero, instr->environment()); |
| 3278 | } |
| 3279 | } |
| 3280 | __ bind(&done); |
| 3281 | } |
| 3282 | |
| 3283 | |
| 3284 | void LCodeGen::DoTaggedToI(LTaggedToI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3285 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3286 | ASSERT(input->IsRegister()); |
| 3287 | ASSERT(input->Equals(instr->result())); |
| 3288 | |
| 3289 | Register input_reg = ToRegister(input); |
| 3290 | |
| 3291 | DeferredTaggedToI* deferred = new DeferredTaggedToI(this, instr); |
| 3292 | |
| 3293 | // Smi check. |
| 3294 | __ test(input_reg, Immediate(kSmiTagMask)); |
| 3295 | __ j(not_zero, deferred->entry()); |
| 3296 | |
| 3297 | // Smi to int32 conversion |
| 3298 | __ SmiUntag(input_reg); // Untag smi. |
| 3299 | |
| 3300 | __ bind(deferred->exit()); |
| 3301 | } |
| 3302 | |
| 3303 | |
| 3304 | void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3305 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3306 | ASSERT(input->IsRegister()); |
| 3307 | LOperand* result = instr->result(); |
| 3308 | ASSERT(result->IsDoubleRegister()); |
| 3309 | |
| 3310 | Register input_reg = ToRegister(input); |
| 3311 | XMMRegister result_reg = ToDoubleRegister(result); |
| 3312 | |
| 3313 | EmitNumberUntagD(input_reg, result_reg, instr->environment()); |
| 3314 | } |
| 3315 | |
| 3316 | |
| 3317 | void LCodeGen::DoDoubleToI(LDoubleToI* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3318 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3319 | ASSERT(input->IsDoubleRegister()); |
| 3320 | LOperand* result = instr->result(); |
| 3321 | ASSERT(result->IsRegister()); |
| 3322 | |
| 3323 | XMMRegister input_reg = ToDoubleRegister(input); |
| 3324 | Register result_reg = ToRegister(result); |
| 3325 | |
| 3326 | if (instr->truncating()) { |
| 3327 | // Performs a truncating conversion of a floating point number as used by |
| 3328 | // the JS bitwise operations. |
| 3329 | __ cvttsd2si(result_reg, Operand(input_reg)); |
| 3330 | __ cmp(result_reg, 0x80000000u); |
| 3331 | if (CpuFeatures::IsSupported(SSE3)) { |
| 3332 | // This will deoptimize if the exponent of the input in out of range. |
| 3333 | CpuFeatures::Scope scope(SSE3); |
| 3334 | NearLabel convert, done; |
| 3335 | __ j(not_equal, &done); |
| 3336 | __ sub(Operand(esp), Immediate(kDoubleSize)); |
| 3337 | __ movdbl(Operand(esp, 0), input_reg); |
| 3338 | // Get exponent alone and check for too-big exponent. |
| 3339 | __ mov(result_reg, Operand(esp, sizeof(int32_t))); |
| 3340 | __ and_(result_reg, HeapNumber::kExponentMask); |
| 3341 | const uint32_t kTooBigExponent = |
| 3342 | (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift; |
| 3343 | __ cmp(Operand(result_reg), Immediate(kTooBigExponent)); |
| 3344 | __ j(less, &convert); |
| 3345 | __ add(Operand(esp), Immediate(kDoubleSize)); |
| 3346 | DeoptimizeIf(no_condition, instr->environment()); |
| 3347 | __ bind(&convert); |
| 3348 | // Do conversion, which cannot fail because we checked the exponent. |
| 3349 | __ fld_d(Operand(esp, 0)); |
| 3350 | __ fisttp_d(Operand(esp, 0)); |
| 3351 | __ mov(result_reg, Operand(esp, 0)); // Low word of answer is the result. |
| 3352 | __ add(Operand(esp), Immediate(kDoubleSize)); |
| 3353 | __ bind(&done); |
| 3354 | } else { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3355 | NearLabel done; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3356 | Register temp_reg = ToRegister(instr->TempAt(0)); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3357 | XMMRegister xmm_scratch = xmm0; |
| 3358 | |
| 3359 | // If cvttsd2si succeeded, we're done. Otherwise, we attempt |
| 3360 | // manual conversion. |
| 3361 | __ j(not_equal, &done); |
| 3362 | |
| 3363 | // Get high 32 bits of the input in result_reg and temp_reg. |
| 3364 | __ pshufd(xmm_scratch, input_reg, 1); |
| 3365 | __ movd(Operand(temp_reg), xmm_scratch); |
| 3366 | __ mov(result_reg, temp_reg); |
| 3367 | |
| 3368 | // Prepare negation mask in temp_reg. |
| 3369 | __ sar(temp_reg, kBitsPerInt - 1); |
| 3370 | |
| 3371 | // Extract the exponent from result_reg and subtract adjusted |
| 3372 | // bias from it. The adjustment is selected in a way such that |
| 3373 | // when the difference is zero, the answer is in the low 32 bits |
| 3374 | // of the input, otherwise a shift has to be performed. |
| 3375 | __ shr(result_reg, HeapNumber::kExponentShift); |
| 3376 | __ and_(result_reg, |
| 3377 | HeapNumber::kExponentMask >> HeapNumber::kExponentShift); |
| 3378 | __ sub(Operand(result_reg), |
| 3379 | Immediate(HeapNumber::kExponentBias + |
| 3380 | HeapNumber::kExponentBits + |
| 3381 | HeapNumber::kMantissaBits)); |
| 3382 | // Don't handle big (> kMantissaBits + kExponentBits == 63) or |
| 3383 | // special exponents. |
| 3384 | DeoptimizeIf(greater, instr->environment()); |
| 3385 | |
| 3386 | // Zero out the sign and the exponent in the input (by shifting |
| 3387 | // it to the left) and restore the implicit mantissa bit, |
| 3388 | // i.e. convert the input to unsigned int64 shifted left by |
| 3389 | // kExponentBits. |
| 3390 | ExternalReference minus_zero = ExternalReference::address_of_minus_zero(); |
| 3391 | // Minus zero has the most significant bit set and the other |
| 3392 | // bits cleared. |
| 3393 | __ movdbl(xmm_scratch, Operand::StaticVariable(minus_zero)); |
| 3394 | __ psllq(input_reg, HeapNumber::kExponentBits); |
| 3395 | __ por(input_reg, xmm_scratch); |
| 3396 | |
| 3397 | // Get the amount to shift the input right in xmm_scratch. |
| 3398 | __ neg(result_reg); |
| 3399 | __ movd(xmm_scratch, Operand(result_reg)); |
| 3400 | |
| 3401 | // Shift the input right and extract low 32 bits. |
| 3402 | __ psrlq(input_reg, xmm_scratch); |
| 3403 | __ movd(Operand(result_reg), input_reg); |
| 3404 | |
| 3405 | // Use the prepared mask in temp_reg to negate the result if necessary. |
| 3406 | __ xor_(result_reg, Operand(temp_reg)); |
| 3407 | __ sub(result_reg, Operand(temp_reg)); |
| 3408 | __ bind(&done); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3409 | } |
| 3410 | } else { |
| 3411 | NearLabel done; |
| 3412 | __ cvttsd2si(result_reg, Operand(input_reg)); |
| 3413 | __ cvtsi2sd(xmm0, Operand(result_reg)); |
| 3414 | __ ucomisd(xmm0, input_reg); |
| 3415 | DeoptimizeIf(not_equal, instr->environment()); |
| 3416 | DeoptimizeIf(parity_even, instr->environment()); // NaN. |
| 3417 | if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { |
| 3418 | // The integer converted back is equal to the original. We |
| 3419 | // only have to test if we got -0 as an input. |
| 3420 | __ test(result_reg, Operand(result_reg)); |
| 3421 | __ j(not_zero, &done); |
| 3422 | __ movmskpd(result_reg, input_reg); |
| 3423 | // Bit 0 contains the sign of the double in input_reg. |
| 3424 | // If input was positive, we are ok and return 0, otherwise |
| 3425 | // deoptimize. |
| 3426 | __ and_(result_reg, 1); |
| 3427 | DeoptimizeIf(not_zero, instr->environment()); |
| 3428 | } |
| 3429 | __ bind(&done); |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | |
| 3434 | void LCodeGen::DoCheckSmi(LCheckSmi* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3435 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3436 | ASSERT(input->IsRegister()); |
| 3437 | __ test(ToRegister(input), Immediate(kSmiTagMask)); |
| 3438 | DeoptimizeIf(instr->condition(), instr->environment()); |
| 3439 | } |
| 3440 | |
| 3441 | |
| 3442 | void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3443 | Register input = ToRegister(instr->InputAt(0)); |
| 3444 | Register temp = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3445 | InstanceType first = instr->hydrogen()->first(); |
| 3446 | InstanceType last = instr->hydrogen()->last(); |
| 3447 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3448 | __ mov(temp, FieldOperand(input, HeapObject::kMapOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3449 | |
| 3450 | // If there is only one type in the interval check for equality. |
| 3451 | if (first == last) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3452 | __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset), |
| 3453 | static_cast<int8_t>(first)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3454 | DeoptimizeIf(not_equal, instr->environment()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3455 | } else if (first == FIRST_STRING_TYPE && last == LAST_STRING_TYPE) { |
| 3456 | // String has a dedicated bit in instance type. |
| 3457 | __ test_b(FieldOperand(temp, Map::kInstanceTypeOffset), kIsNotStringMask); |
| 3458 | DeoptimizeIf(not_zero, instr->environment()); |
| 3459 | } else { |
| 3460 | __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset), |
| 3461 | static_cast<int8_t>(first)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3462 | DeoptimizeIf(below, instr->environment()); |
| 3463 | // Omit check for the last type. |
| 3464 | if (last != LAST_TYPE) { |
| 3465 | __ cmpb(FieldOperand(temp, Map::kInstanceTypeOffset), |
| 3466 | static_cast<int8_t>(last)); |
| 3467 | DeoptimizeIf(above, instr->environment()); |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | |
| 3473 | void LCodeGen::DoCheckFunction(LCheckFunction* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3474 | ASSERT(instr->InputAt(0)->IsRegister()); |
| 3475 | Register reg = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3476 | __ cmp(reg, instr->hydrogen()->target()); |
| 3477 | DeoptimizeIf(not_equal, instr->environment()); |
| 3478 | } |
| 3479 | |
| 3480 | |
| 3481 | void LCodeGen::DoCheckMap(LCheckMap* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3482 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3483 | ASSERT(input->IsRegister()); |
| 3484 | Register reg = ToRegister(input); |
| 3485 | __ cmp(FieldOperand(reg, HeapObject::kMapOffset), |
| 3486 | instr->hydrogen()->map()); |
| 3487 | DeoptimizeIf(not_equal, instr->environment()); |
| 3488 | } |
| 3489 | |
| 3490 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3491 | void LCodeGen::LoadHeapObject(Register result, Handle<HeapObject> object) { |
| 3492 | if (Heap::InNewSpace(*object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3493 | Handle<JSGlobalPropertyCell> cell = |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3494 | Factory::NewJSGlobalPropertyCell(object); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3495 | __ mov(result, Operand::Cell(cell)); |
| 3496 | } else { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3497 | __ mov(result, object); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | |
| 3502 | void LCodeGen::DoCheckPrototypeMaps(LCheckPrototypeMaps* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3503 | Register reg = ToRegister(instr->TempAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3504 | |
| 3505 | Handle<JSObject> holder = instr->holder(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3506 | Handle<JSObject> current_prototype = instr->prototype(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3507 | |
| 3508 | // Load prototype object. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3509 | LoadHeapObject(reg, current_prototype); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3510 | |
| 3511 | // Check prototype maps up to the holder. |
| 3512 | while (!current_prototype.is_identical_to(holder)) { |
| 3513 | __ cmp(FieldOperand(reg, HeapObject::kMapOffset), |
| 3514 | Handle<Map>(current_prototype->map())); |
| 3515 | DeoptimizeIf(not_equal, instr->environment()); |
| 3516 | current_prototype = |
| 3517 | Handle<JSObject>(JSObject::cast(current_prototype->GetPrototype())); |
| 3518 | // Load next prototype object. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3519 | LoadHeapObject(reg, current_prototype); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | // Check the holder map. |
| 3523 | __ cmp(FieldOperand(reg, HeapObject::kMapOffset), |
| 3524 | Handle<Map>(current_prototype->map())); |
| 3525 | DeoptimizeIf(not_equal, instr->environment()); |
| 3526 | } |
| 3527 | |
| 3528 | |
| 3529 | void LCodeGen::DoArrayLiteral(LArrayLiteral* instr) { |
| 3530 | // Setup the parameters to the stub/runtime call. |
| 3531 | __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
| 3532 | __ push(FieldOperand(eax, JSFunction::kLiteralsOffset)); |
| 3533 | __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index()))); |
| 3534 | __ push(Immediate(instr->hydrogen()->constant_elements())); |
| 3535 | |
| 3536 | // Pick the right runtime function or stub to call. |
| 3537 | int length = instr->hydrogen()->length(); |
| 3538 | if (instr->hydrogen()->IsCopyOnWrite()) { |
| 3539 | ASSERT(instr->hydrogen()->depth() == 1); |
| 3540 | FastCloneShallowArrayStub::Mode mode = |
| 3541 | FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS; |
| 3542 | FastCloneShallowArrayStub stub(mode, length); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3543 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3544 | } else if (instr->hydrogen()->depth() > 1) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3545 | CallRuntime(Runtime::kCreateArrayLiteral, 3, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3546 | } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3547 | CallRuntime(Runtime::kCreateArrayLiteralShallow, 3, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3548 | } else { |
| 3549 | FastCloneShallowArrayStub::Mode mode = |
| 3550 | FastCloneShallowArrayStub::CLONE_ELEMENTS; |
| 3551 | FastCloneShallowArrayStub stub(mode, length); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3552 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | |
| 3557 | void LCodeGen::DoObjectLiteral(LObjectLiteral* instr) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3558 | ASSERT(ToRegister(instr->context()).is(esi)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3559 | // Setup the parameters to the stub/runtime call. |
| 3560 | __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
| 3561 | __ push(FieldOperand(eax, JSFunction::kLiteralsOffset)); |
| 3562 | __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index()))); |
| 3563 | __ push(Immediate(instr->hydrogen()->constant_properties())); |
| 3564 | __ push(Immediate(Smi::FromInt(instr->hydrogen()->fast_elements() ? 1 : 0))); |
| 3565 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 3566 | // Pick the right runtime function to call. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3567 | if (instr->hydrogen()->depth() > 1) { |
| 3568 | CallRuntime(Runtime::kCreateObjectLiteral, 4, instr); |
| 3569 | } else { |
| 3570 | CallRuntime(Runtime::kCreateObjectLiteralShallow, 4, instr); |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) { |
| 3576 | NearLabel materialized; |
| 3577 | // Registers will be used as follows: |
| 3578 | // edi = JS function. |
| 3579 | // ecx = literals array. |
| 3580 | // ebx = regexp literal. |
| 3581 | // eax = regexp literal clone. |
| 3582 | __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
| 3583 | __ mov(ecx, FieldOperand(edi, JSFunction::kLiteralsOffset)); |
| 3584 | int literal_offset = FixedArray::kHeaderSize + |
| 3585 | instr->hydrogen()->literal_index() * kPointerSize; |
| 3586 | __ mov(ebx, FieldOperand(ecx, literal_offset)); |
| 3587 | __ cmp(ebx, Factory::undefined_value()); |
| 3588 | __ j(not_equal, &materialized); |
| 3589 | |
| 3590 | // Create regexp literal using runtime function |
| 3591 | // Result will be in eax. |
| 3592 | __ push(ecx); |
| 3593 | __ push(Immediate(Smi::FromInt(instr->hydrogen()->literal_index()))); |
| 3594 | __ push(Immediate(instr->hydrogen()->pattern())); |
| 3595 | __ push(Immediate(instr->hydrogen()->flags())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3596 | CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3597 | __ mov(ebx, eax); |
| 3598 | |
| 3599 | __ bind(&materialized); |
| 3600 | int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize; |
| 3601 | Label allocated, runtime_allocate; |
| 3602 | __ AllocateInNewSpace(size, eax, ecx, edx, &runtime_allocate, TAG_OBJECT); |
| 3603 | __ jmp(&allocated); |
| 3604 | |
| 3605 | __ bind(&runtime_allocate); |
| 3606 | __ push(ebx); |
| 3607 | __ push(Immediate(Smi::FromInt(size))); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3608 | CallRuntime(Runtime::kAllocateInNewSpace, 1, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3609 | __ pop(ebx); |
| 3610 | |
| 3611 | __ bind(&allocated); |
| 3612 | // Copy the content into the newly allocated memory. |
| 3613 | // (Unroll copy loop once for better throughput). |
| 3614 | for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) { |
| 3615 | __ mov(edx, FieldOperand(ebx, i)); |
| 3616 | __ mov(ecx, FieldOperand(ebx, i + kPointerSize)); |
| 3617 | __ mov(FieldOperand(eax, i), edx); |
| 3618 | __ mov(FieldOperand(eax, i + kPointerSize), ecx); |
| 3619 | } |
| 3620 | if ((size % (2 * kPointerSize)) != 0) { |
| 3621 | __ mov(edx, FieldOperand(ebx, size - kPointerSize)); |
| 3622 | __ mov(FieldOperand(eax, size - kPointerSize), edx); |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | |
| 3627 | void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) { |
| 3628 | // Use the fast case closure allocation code that allocates in new |
| 3629 | // space for nested functions that don't need literals cloning. |
| 3630 | Handle<SharedFunctionInfo> shared_info = instr->shared_info(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3631 | bool pretenure = instr->hydrogen()->pretenure(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3632 | if (shared_info->num_literals() == 0 && !pretenure) { |
| 3633 | FastNewClosureStub stub; |
| 3634 | __ push(Immediate(shared_info)); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3635 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3636 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3637 | __ push(Operand(ebp, StandardFrameConstants::kContextOffset)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3638 | __ push(Immediate(shared_info)); |
| 3639 | __ push(Immediate(pretenure |
| 3640 | ? Factory::true_value() |
| 3641 | : Factory::false_value())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3642 | CallRuntime(Runtime::kNewClosure, 3, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | |
| 3647 | void LCodeGen::DoTypeof(LTypeof* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3648 | LOperand* input = instr->InputAt(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3649 | if (input->IsConstantOperand()) { |
| 3650 | __ push(ToImmediate(input)); |
| 3651 | } else { |
| 3652 | __ push(ToOperand(input)); |
| 3653 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3654 | CallRuntime(Runtime::kTypeof, 1, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | |
| 3658 | void LCodeGen::DoTypeofIs(LTypeofIs* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3659 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3660 | Register result = ToRegister(instr->result()); |
| 3661 | Label true_label; |
| 3662 | Label false_label; |
| 3663 | NearLabel done; |
| 3664 | |
| 3665 | Condition final_branch_condition = EmitTypeofIs(&true_label, |
| 3666 | &false_label, |
| 3667 | input, |
| 3668 | instr->type_literal()); |
| 3669 | __ j(final_branch_condition, &true_label); |
| 3670 | __ bind(&false_label); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3671 | __ mov(result, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3672 | __ jmp(&done); |
| 3673 | |
| 3674 | __ bind(&true_label); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3675 | __ mov(result, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3676 | |
| 3677 | __ bind(&done); |
| 3678 | } |
| 3679 | |
| 3680 | |
| 3681 | void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 3682 | Register input = ToRegister(instr->InputAt(0)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3683 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 3684 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 3685 | Label* true_label = chunk_->GetAssemblyLabel(true_block); |
| 3686 | Label* false_label = chunk_->GetAssemblyLabel(false_block); |
| 3687 | |
| 3688 | Condition final_branch_condition = EmitTypeofIs(true_label, |
| 3689 | false_label, |
| 3690 | input, |
| 3691 | instr->type_literal()); |
| 3692 | |
| 3693 | EmitBranch(true_block, false_block, final_branch_condition); |
| 3694 | } |
| 3695 | |
| 3696 | |
| 3697 | Condition LCodeGen::EmitTypeofIs(Label* true_label, |
| 3698 | Label* false_label, |
| 3699 | Register input, |
| 3700 | Handle<String> type_name) { |
| 3701 | Condition final_branch_condition = no_condition; |
| 3702 | if (type_name->Equals(Heap::number_symbol())) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3703 | __ JumpIfSmi(input, true_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3704 | __ cmp(FieldOperand(input, HeapObject::kMapOffset), |
| 3705 | Factory::heap_number_map()); |
| 3706 | final_branch_condition = equal; |
| 3707 | |
| 3708 | } else if (type_name->Equals(Heap::string_symbol())) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3709 | __ JumpIfSmi(input, false_label); |
| 3710 | __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input); |
| 3711 | __ j(above_equal, false_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3712 | __ test_b(FieldOperand(input, Map::kBitFieldOffset), |
| 3713 | 1 << Map::kIsUndetectable); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3714 | final_branch_condition = zero; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3715 | |
| 3716 | } else if (type_name->Equals(Heap::boolean_symbol())) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3717 | __ cmp(input, Factory::true_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3718 | __ j(equal, true_label); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 3719 | __ cmp(input, Factory::false_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3720 | final_branch_condition = equal; |
| 3721 | |
| 3722 | } else if (type_name->Equals(Heap::undefined_symbol())) { |
| 3723 | __ cmp(input, Factory::undefined_value()); |
| 3724 | __ j(equal, true_label); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3725 | __ JumpIfSmi(input, false_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3726 | // Check for undetectable objects => true. |
| 3727 | __ mov(input, FieldOperand(input, HeapObject::kMapOffset)); |
| 3728 | __ test_b(FieldOperand(input, Map::kBitFieldOffset), |
| 3729 | 1 << Map::kIsUndetectable); |
| 3730 | final_branch_condition = not_zero; |
| 3731 | |
| 3732 | } else if (type_name->Equals(Heap::function_symbol())) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3733 | __ JumpIfSmi(input, false_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3734 | __ CmpObjectType(input, JS_FUNCTION_TYPE, input); |
| 3735 | __ j(equal, true_label); |
| 3736 | // Regular expressions => 'function' (they are callable). |
| 3737 | __ CmpInstanceType(input, JS_REGEXP_TYPE); |
| 3738 | final_branch_condition = equal; |
| 3739 | |
| 3740 | } else if (type_name->Equals(Heap::object_symbol())) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3741 | __ JumpIfSmi(input, false_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3742 | __ cmp(input, Factory::null_value()); |
| 3743 | __ j(equal, true_label); |
| 3744 | // Regular expressions => 'function', not 'object'. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3745 | __ CmpObjectType(input, FIRST_JS_OBJECT_TYPE, input); |
| 3746 | __ j(below, false_label); |
| 3747 | __ CmpInstanceType(input, FIRST_FUNCTION_CLASS_TYPE); |
| 3748 | __ j(above_equal, false_label); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3749 | // Check for undetectable objects => false. |
| 3750 | __ test_b(FieldOperand(input, Map::kBitFieldOffset), |
| 3751 | 1 << Map::kIsUndetectable); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame^] | 3752 | final_branch_condition = zero; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3753 | |
| 3754 | } else { |
| 3755 | final_branch_condition = not_equal; |
| 3756 | __ jmp(false_label); |
| 3757 | // A dead branch instruction will be generated after this point. |
| 3758 | } |
| 3759 | |
| 3760 | return final_branch_condition; |
| 3761 | } |
| 3762 | |
| 3763 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3764 | void LCodeGen::DoIsConstructCall(LIsConstructCall* instr) { |
| 3765 | Register result = ToRegister(instr->result()); |
| 3766 | NearLabel true_label; |
| 3767 | NearLabel false_label; |
| 3768 | NearLabel done; |
| 3769 | |
| 3770 | EmitIsConstructCall(result); |
| 3771 | __ j(equal, &true_label); |
| 3772 | |
| 3773 | __ mov(result, Factory::false_value()); |
| 3774 | __ jmp(&done); |
| 3775 | |
| 3776 | __ bind(&true_label); |
| 3777 | __ mov(result, Factory::true_value()); |
| 3778 | |
| 3779 | __ bind(&done); |
| 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) { |
| 3784 | Register temp = ToRegister(instr->TempAt(0)); |
| 3785 | int true_block = chunk_->LookupDestination(instr->true_block_id()); |
| 3786 | int false_block = chunk_->LookupDestination(instr->false_block_id()); |
| 3787 | |
| 3788 | EmitIsConstructCall(temp); |
| 3789 | EmitBranch(true_block, false_block, equal); |
| 3790 | } |
| 3791 | |
| 3792 | |
| 3793 | void LCodeGen::EmitIsConstructCall(Register temp) { |
| 3794 | // Get the frame pointer for the calling frame. |
| 3795 | __ mov(temp, Operand(ebp, StandardFrameConstants::kCallerFPOffset)); |
| 3796 | |
| 3797 | // Skip the arguments adaptor frame if it exists. |
| 3798 | NearLabel check_frame_marker; |
| 3799 | __ cmp(Operand(temp, StandardFrameConstants::kContextOffset), |
| 3800 | Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 3801 | __ j(not_equal, &check_frame_marker); |
| 3802 | __ mov(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset)); |
| 3803 | |
| 3804 | // Check the marker in the calling frame. |
| 3805 | __ bind(&check_frame_marker); |
| 3806 | __ cmp(Operand(temp, StandardFrameConstants::kMarkerOffset), |
| 3807 | Immediate(Smi::FromInt(StackFrame::CONSTRUCT))); |
| 3808 | } |
| 3809 | |
| 3810 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3811 | void LCodeGen::DoLazyBailout(LLazyBailout* instr) { |
| 3812 | // No code for lazy bailout instruction. Used to capture environment after a |
| 3813 | // call for populating the safepoint data with deoptimization data. |
| 3814 | } |
| 3815 | |
| 3816 | |
| 3817 | void LCodeGen::DoDeoptimize(LDeoptimize* instr) { |
| 3818 | DeoptimizeIf(no_condition, instr->environment()); |
| 3819 | } |
| 3820 | |
| 3821 | |
| 3822 | void LCodeGen::DoDeleteProperty(LDeleteProperty* instr) { |
| 3823 | LOperand* obj = instr->object(); |
| 3824 | LOperand* key = instr->key(); |
| 3825 | __ push(ToOperand(obj)); |
| 3826 | if (key->IsConstantOperand()) { |
| 3827 | __ push(ToImmediate(key)); |
| 3828 | } else { |
| 3829 | __ push(ToOperand(key)); |
| 3830 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 3831 | ASSERT(instr->HasPointerMap() && instr->HasDeoptimizationEnvironment()); |
| 3832 | LPointerMap* pointers = instr->pointer_map(); |
| 3833 | LEnvironment* env = instr->deoptimization_environment(); |
| 3834 | RecordPosition(pointers->position()); |
| 3835 | RegisterEnvironmentForDeoptimization(env); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3836 | // Create safepoint generator that will also ensure enough space in the |
| 3837 | // reloc info for patching in deoptimization (since this is invoking a |
| 3838 | // builtin) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3839 | SafepointGenerator safepoint_generator(this, |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 3840 | pointers, |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3841 | env->deoptimization_index(), |
| 3842 | true); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3843 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 3844 | __ push(Immediate(Smi::FromInt(strict_mode_flag()))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3845 | __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION, &safepoint_generator); |
| 3846 | } |
| 3847 | |
| 3848 | |
| 3849 | void LCodeGen::DoStackCheck(LStackCheck* instr) { |
| 3850 | // Perform stack overflow check. |
| 3851 | NearLabel done; |
| 3852 | ExternalReference stack_limit = ExternalReference::address_of_stack_limit(); |
| 3853 | __ cmp(esp, Operand::StaticVariable(stack_limit)); |
| 3854 | __ j(above_equal, &done); |
| 3855 | |
| 3856 | StackCheckStub stub; |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3857 | CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr, false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3858 | __ bind(&done); |
| 3859 | } |
| 3860 | |
| 3861 | |
| 3862 | void LCodeGen::DoOsrEntry(LOsrEntry* instr) { |
| 3863 | // This is a pseudo-instruction that ensures that the environment here is |
| 3864 | // properly registered for deoptimization and records the assembler's PC |
| 3865 | // offset. |
| 3866 | LEnvironment* environment = instr->environment(); |
| 3867 | environment->SetSpilledRegisters(instr->SpilledRegisterArray(), |
| 3868 | instr->SpilledDoubleRegisterArray()); |
| 3869 | |
| 3870 | // If the environment were already registered, we would have no way of |
| 3871 | // backpatching it with the spill slot operands. |
| 3872 | ASSERT(!environment->HasBeenRegistered()); |
| 3873 | RegisterEnvironmentForDeoptimization(environment); |
| 3874 | ASSERT(osr_pc_offset_ == -1); |
| 3875 | osr_pc_offset_ = masm()->pc_offset(); |
| 3876 | } |
| 3877 | |
| 3878 | |
| 3879 | #undef __ |
| 3880 | |
| 3881 | } } // namespace v8::internal |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 3882 | |
| 3883 | #endif // V8_TARGET_ARCH_IA32 |