code-stubs.cc 23.1 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

#include "bootstrapper.h"
#include "code-stubs.h"
32
#include "cpu-profiler.h"
danno@chromium.org's avatar
danno@chromium.org committed
33
#include "stub-cache.h"
34
#include "factory.h"
35
#include "gdb-jit.h"
36 37
#include "macro-assembler.h"

38 39
namespace v8 {
namespace internal {
40

41 42 43 44

CodeStubInterfaceDescriptor::CodeStubInterfaceDescriptor()
    : register_param_count_(-1),
      stack_parameter_count_(NULL),
45
      hint_stack_parameter_count_(-1),
46 47 48
      function_mode_(NOT_JS_FUNCTION_STUB_MODE),
      register_params_(NULL),
      deoptimization_handler_(NULL),
49
      miss_handler_(),
50
      has_miss_handler_(false) { }
51 52


53 54 55
bool CodeStub::FindCodeInCache(Code** code_out, Isolate* isolate) {
  UnseededNumberDictionary* stubs = isolate->heap()->code_stubs();
  int index = stubs->FindEntry(GetKey());
56
  if (index != UnseededNumberDictionary::kNotFound) {
57
    *code_out = Code::cast(stubs->ValueAt(index));
58
    return true;
59
  }
60 61
  return false;
}
62

63

64
SmartArrayPointer<const char> CodeStub::GetName() {
65 66 67 68 69 70 71 72 73
  char buffer[100];
  NoAllocationStringAllocator allocator(buffer,
                                        static_cast<unsigned>(sizeof(buffer)));
  StringStream stream(&allocator);
  PrintName(&stream);
  return stream.ToCString();
}


74
void CodeStub::RecordCodeGeneration(Code* code, Isolate* isolate) {
75
  SmartArrayPointer<const char> name = GetName();
76 77
  PROFILE(isolate, CodeCreateEvent(Logger::STUB_TAG, code, *name));
  GDBJIT(AddCode(GDBJITInterface::STUB, *name, code));
78 79
  Counters* counters = isolate->counters();
  counters->total_stubs_code_size()->Increment(code->instruction_size());
80 81 82
}


83
Code::Kind CodeStub::GetCodeKind() const {
84 85 86 87
  return Code::STUB;
}


88 89 90 91 92 93 94 95
Handle<Code> CodeStub::GetCodeCopyFromTemplate(Isolate* isolate) {
  Handle<Code> ic = GetCode(isolate);
  ic = isolate->factory()->CopyCode(ic);
  RecordCodeGeneration(*ic, isolate);
  return ic;
}


96
Handle<Code> PlatformCodeStub::GenerateCode(Isolate* isolate) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  Factory* factory = isolate->factory();

  // Generate the new code.
  MacroAssembler masm(isolate, NULL, 256);

  {
    // Update the static counter each time a new code stub is generated.
    isolate->counters()->code_stubs()->Increment();

    // Nested stubs are not allowed for leaves.
    AllowStubCallsScope allow_scope(&masm, false);

    // Generate the code for the stub.
    masm.set_generating_stub(true);
    NoCurrentFrameScope scope(&masm);
    Generate(&masm);
  }

  // Create the code object.
  CodeDesc desc;
  masm.GetCode(&desc);

  // Copy the generated code into a heap object.
  Code::Flags flags = Code::ComputeFlags(
121
      GetCodeKind(),
122 123
      GetICState(),
      GetExtraICState(),
124 125
      GetStubType(),
      GetStubFlags());
126 127 128 129 130 131
  Handle<Code> new_object = factory->NewCode(
      desc, flags, masm.CodeObject(), NeedsImmovableCode());
  return new_object;
}


132
Handle<Code> CodeStub::GetCode(Isolate* isolate) {
133 134
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
135
  Code* code;
136
  if (UseSpecialCache()
137 138
      ? FindCodeInSpecialCache(&code, isolate)
      : FindCodeInCache(&code, isolate)) {
139 140 141 142 143
    ASSERT(IsPregenerated() == code->is_pregenerated());
    return Handle<Code>(code);
  }

  {
144
    HandleScope scope(isolate);
145

146
    Handle<Code> new_object = GenerateCode(isolate);
147
    new_object->set_major_key(MajorKey());
148
    FinishCode(new_object);
149
    RecordCodeGeneration(*new_object, isolate);
150 151 152 153 154 155 156

#ifdef ENABLE_DISASSEMBLER
    if (FLAG_print_code_stubs) {
      new_object->Disassemble(*GetName());
      PrintF("\n");
    }
#endif
157

158 159 160 161
    if (UseSpecialCache()) {
      AddToSpecialCache(new_object);
    } else {
      // Update the dictionary and the root in Heap.
162
      Handle<UnseededNumberDictionary> dict =
163
          factory->DictionaryAtNumberPut(
164
              Handle<UnseededNumberDictionary>(heap->code_stubs()),
165 166 167 168
              GetKey(),
              new_object);
      heap->public_set_code_stubs(*dict);
    }
169 170 171
    code = *new_object;
  }

172
  Activate(code);
173 174 175
  ASSERT(!NeedsImmovableCode() ||
         heap->lo_space()->Contains(code) ||
         heap->code_space()->FirstPage()->Contains(code->address()));
176
  return Handle<Code>(code, isolate);
177 178 179
}


180 181
const char* CodeStub::MajorName(CodeStub::Major major_key,
                                bool allow_unknown_keys) {
182
  switch (major_key) {
183
#define DEF_CASE(name) case name: return #name "Stub";
184
    CODE_STUB_LIST(DEF_CASE)
185
#undef DEF_CASE
186
    default:
187 188 189
      if (!allow_unknown_keys) {
        UNREACHABLE();
      }
190 191 192 193
      return NULL;
  }
}

194

195 196 197 198
void CodeStub::PrintBaseName(StringStream* stream) {
  stream->Add("%s", MajorName(MajorKey(), false));
}

199

200
void CodeStub::PrintName(StringStream* stream) {
201 202 203 204 205
  PrintBaseName(stream);
  PrintState(stream);
}


206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
void BinaryOpStub::Generate(MacroAssembler* masm) {
  // Explicitly allow generation of nested stubs. It is safe here because
  // generation code does not use any raw pointers.
  AllowStubCallsScope allow_stub_calls(masm, true);

  BinaryOpIC::TypeInfo operands_type = Max(left_type_, right_type_);
  if (left_type_ == BinaryOpIC::ODDBALL && right_type_ == BinaryOpIC::ODDBALL) {
    // The OddballStub handles a number and an oddball, not two oddballs.
    operands_type = BinaryOpIC::GENERIC;
  }
  switch (operands_type) {
    case BinaryOpIC::UNINITIALIZED:
      GenerateTypeTransition(masm);
      break;
    case BinaryOpIC::SMI:
      GenerateSmiStub(masm);
      break;
    case BinaryOpIC::INT32:
      GenerateInt32Stub(masm);
      break;
226 227
    case BinaryOpIC::NUMBER:
      GenerateNumberStub(masm);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      break;
    case BinaryOpIC::ODDBALL:
      GenerateOddballStub(masm);
      break;
    case BinaryOpIC::STRING:
      GenerateStringStub(masm);
      break;
    case BinaryOpIC::GENERIC:
      GenerateGeneric(masm);
      break;
    default:
      UNREACHABLE();
  }
}


#define __ ACCESS_MASM(masm)


void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
  switch (op_) {
    case Token::ADD:
250
      __ InvokeBuiltin(Builtins::ADD, CALL_FUNCTION);
251 252
      break;
    case Token::SUB:
253
      __ InvokeBuiltin(Builtins::SUB, CALL_FUNCTION);
254 255
      break;
    case Token::MUL:
256
      __ InvokeBuiltin(Builtins::MUL, CALL_FUNCTION);
257 258
      break;
    case Token::DIV:
259
      __ InvokeBuiltin(Builtins::DIV, CALL_FUNCTION);
260 261
      break;
    case Token::MOD:
262
      __ InvokeBuiltin(Builtins::MOD, CALL_FUNCTION);
263 264
      break;
    case Token::BIT_OR:
265
      __ InvokeBuiltin(Builtins::BIT_OR, CALL_FUNCTION);
266 267
      break;
    case Token::BIT_AND:
268
      __ InvokeBuiltin(Builtins::BIT_AND, CALL_FUNCTION);
269 270
      break;
    case Token::BIT_XOR:
271
      __ InvokeBuiltin(Builtins::BIT_XOR, CALL_FUNCTION);
272 273
      break;
    case Token::SAR:
274
      __ InvokeBuiltin(Builtins::SAR, CALL_FUNCTION);
275 276
      break;
    case Token::SHR:
277
      __ InvokeBuiltin(Builtins::SHR, CALL_FUNCTION);
278 279
      break;
    case Token::SHL:
280
      __ InvokeBuiltin(Builtins::SHL, CALL_FUNCTION);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
      break;
    default:
      UNREACHABLE();
  }
}


#undef __


void BinaryOpStub::PrintName(StringStream* stream) {
  const char* op_name = Token::Name(op_);
  const char* overwrite_name;
  switch (mode_) {
    case NO_OVERWRITE: overwrite_name = "Alloc"; break;
    case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
    case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
    default: overwrite_name = "UnknownOverwrite"; break;
  }
  stream->Add("BinaryOpStub_%s_%s_%s+%s",
              op_name,
              overwrite_name,
              BinaryOpIC::GetName(left_type_),
              BinaryOpIC::GetName(right_type_));
}


void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
  ASSERT(left_type_ == BinaryOpIC::STRING || right_type_ == BinaryOpIC::STRING);
  ASSERT(op_ == Token::ADD);
  if (left_type_ == BinaryOpIC::STRING && right_type_ == BinaryOpIC::STRING) {
    GenerateBothStringStub(masm);
    return;
  }
  // Try to add arguments as strings, otherwise, transition to the generic
  // BinaryOpIC type.
  GenerateAddStrings(masm);
  GenerateTypeTransition(masm);
}


322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
InlineCacheState ICCompareStub::GetICState() {
  CompareIC::State state = Max(left_, right_);
  switch (state) {
    case CompareIC::UNINITIALIZED:
      return ::v8::internal::UNINITIALIZED;
    case CompareIC::SMI:
    case CompareIC::NUMBER:
    case CompareIC::INTERNALIZED_STRING:
    case CompareIC::STRING:
    case CompareIC::UNIQUE_NAME:
    case CompareIC::OBJECT:
    case CompareIC::KNOWN_OBJECT:
      return MONOMORPHIC;
    case CompareIC::GENERIC:
      return ::v8::internal::GENERIC;
  }
  UNREACHABLE();
  return ::v8::internal::UNINITIALIZED;
}


343 344 345 346 347
void ICCompareStub::AddToSpecialCache(Handle<Code> new_object) {
  ASSERT(*known_map_ != NULL);
  Isolate* isolate = new_object->GetIsolate();
  Factory* factory = isolate->factory();
  return Map::UpdateCodeCache(known_map_,
348
                              strict() ?
349 350
                                  factory->strict_compare_ic_string() :
                                  factory->compare_ic_string(),
351 352 353 354
                              new_object);
}


355
bool ICCompareStub::FindCodeInSpecialCache(Code** code_out, Isolate* isolate) {
356 357
  Factory* factory = isolate->factory();
  Code::Flags flags = Code::ComputeFlags(
358
      GetCodeKind(),
359
      UNINITIALIZED);
360
  ASSERT(op_ == Token::EQ || op_ == Token::EQ_STRICT);
361 362 363 364 365 366 367
  Handle<Object> probe(
      known_map_->FindInCodeCache(
        strict() ?
            *factory->strict_compare_ic_string() :
            *factory->compare_ic_string(),
        flags),
      isolate);
368 369
  if (probe->IsCode()) {
    *code_out = Code::cast(*probe);
370 371 372 373 374 375
#ifdef DEBUG
    Token::Value cached_op;
    ICCompareStub::DecodeMinorKey((*code_out)->stub_info(), NULL, NULL, NULL,
                                  &cached_op);
    ASSERT(op_ == cached_op);
#endif
376 377 378 379 380 381
    return true;
  }
  return false;
}


382
int ICCompareStub::MinorKey() {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  return OpField::encode(op_ - Token::EQ) |
         LeftStateField::encode(left_) |
         RightStateField::encode(right_) |
         HandlerStateField::encode(state_);
}


void ICCompareStub::DecodeMinorKey(int minor_key,
                                   CompareIC::State* left_state,
                                   CompareIC::State* right_state,
                                   CompareIC::State* handler_state,
                                   Token::Value* op) {
  if (left_state) {
    *left_state =
        static_cast<CompareIC::State>(LeftStateField::decode(minor_key));
  }
  if (right_state) {
    *right_state =
        static_cast<CompareIC::State>(RightStateField::decode(minor_key));
  }
  if (handler_state) {
    *handler_state =
        static_cast<CompareIC::State>(HandlerStateField::decode(minor_key));
  }
  if (op) {
    *op = static_cast<Token::Value>(OpField::decode(minor_key) + Token::EQ);
  }
410 411 412 413 414 415 416 417
}


void ICCompareStub::Generate(MacroAssembler* masm) {
  switch (state_) {
    case CompareIC::UNINITIALIZED:
      GenerateMiss(masm);
      break;
418
    case CompareIC::SMI:
419 420
      GenerateSmis(masm);
      break;
421 422
    case CompareIC::NUMBER:
      GenerateNumbers(masm);
423
      break;
424
    case CompareIC::STRING:
425 426
      GenerateStrings(masm);
      break;
427 428
    case CompareIC::INTERNALIZED_STRING:
      GenerateInternalizedStrings(masm);
429
      break;
430 431 432
    case CompareIC::UNIQUE_NAME:
      GenerateUniqueNames(masm);
      break;
433
    case CompareIC::OBJECT:
434 435
      GenerateObjects(masm);
      break;
436
    case CompareIC::KNOWN_OBJECT:
437 438 439
      ASSERT(*known_map_ != NULL);
      GenerateKnownObjects(masm);
      break;
440 441 442
    case CompareIC::GENERIC:
      GenerateGeneric(masm);
      break;
443 444 445 446
  }
}


447
void CompareNilICStub::UpdateStatus(Handle<Object> object) {
448
  ASSERT(!state_.Contains(GENERIC));
449
  State old_state(state_);
450
  if (object->IsNull()) {
451
    state_.Add(NULL_TYPE);
452
  } else if (object->IsUndefined()) {
453
    state_.Add(UNDEFINED);
454 455 456
  } else if (object->IsUndetectableObject() ||
             object->IsOddball() ||
             !object->IsHeapObject()) {
457 458
    state_.RemoveAll();
    state_.Add(GENERIC);
459
  } else if (IsMonomorphic()) {
460 461
    state_.RemoveAll();
    state_.Add(GENERIC);
462
  } else {
463
    state_.Add(MONOMORPHIC_MAP);
464
  }
465
  TraceTransition(old_state, state_);
466 467 468
}


469 470
template<class StateType>
void HydrogenCodeStub::TraceTransition(StateType from, StateType to) {
471 472 473
  // Note: Although a no-op transition is semantically OK, it is hinting at a
  // bug somewhere in our state transition machinery.
  ASSERT(from != to);
474 475 476 477 478 479
  #ifdef DEBUG
  if (!FLAG_trace_ic) return;
  char buffer[100];
  NoAllocationStringAllocator allocator(buffer,
                                        static_cast<unsigned>(sizeof(buffer)));
  StringStream stream(&allocator);
480 481 482 483
  stream.Add("[");
  PrintBaseName(&stream);
  stream.Add(": ");
  from.Print(&stream);
484 485 486 487 488 489 490
  stream.Add("=>");
  to.Print(&stream);
  stream.Add("]\n");
  stream.OutputToStdOut();
  #endif
}

491

492 493 494 495 496
void CompareNilICStub::PrintBaseName(StringStream* stream) {
  CodeStub::PrintBaseName(stream);
  stream->Add((nil_value_ == kNullValue) ? "(NullValue)":
                                           "(UndefinedValue)");
}
497

498

499
void CompareNilICStub::PrintState(StringStream* stream) {
500
  state_.Print(stream);
501 502 503
}


504
void CompareNilICStub::State::Print(StringStream* stream) const {
505 506 507 508 509 510
  stream->Add("(");
  SimpleListPrinter printer(stream);
  if (IsEmpty()) printer.Add("None");
  if (Contains(UNDEFINED)) printer.Add("Undefined");
  if (Contains(NULL_TYPE)) printer.Add("Null");
  if (Contains(MONOMORPHIC_MAP)) printer.Add("MonomorphicMap");
511
  if (Contains(GENERIC)) printer.Add("Generic");
512
  stream->Add(")");
513 514 515
}


516
Handle<Type> CompareNilICStub::GetType(
517 518
    Isolate* isolate,
    Handle<Map> map) {
519
  if (state_.Contains(CompareNilICStub::GENERIC)) {
520 521 522 523
    return handle(Type::Any(), isolate);
  }

  Handle<Type> result(Type::None(), isolate);
524
  if (state_.Contains(CompareNilICStub::UNDEFINED)) {
525 526 527
    result = handle(Type::Union(result, handle(Type::Undefined(), isolate)),
                    isolate);
  }
528
  if (state_.Contains(CompareNilICStub::NULL_TYPE)) {
529 530 531
    result = handle(Type::Union(result, handle(Type::Null(), isolate)),
                    isolate);
  }
532
  if (state_.Contains(CompareNilICStub::MONOMORPHIC_MAP)) {
533 534 535 536 537 538 539 540
    Type* type = map.is_null() ? Type::Detectable() : Type::Class(map);
    result = handle(Type::Union(result, handle(type, isolate)), isolate);
  }

  return result;
}


541 542 543 544 545 546 547 548 549 550
Handle<Type> CompareNilICStub::GetInputType(
    Isolate* isolate,
    Handle<Map> map) {
  Handle<Type> output_type = GetType(isolate, map);
  Handle<Type> nil_type = handle(nil_value_ == kNullValue
      ? Type::Null() : Type::Undefined(), isolate);
  return handle(Type::Union(output_type, nil_type), isolate);
}


551
void InstanceofStub::PrintName(StringStream* stream) {
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  const char* args = "";
  if (HasArgsInRegisters()) {
    args = "_REGS";
  }

  const char* inline_check = "";
  if (HasCallSiteInlineCheck()) {
    inline_check = "_INLINE";
  }

  const char* return_true_false_object = "";
  if (ReturnTrueFalseObject()) {
    return_true_false_object = "_TRUEFALSE";
  }

567 568 569 570
  stream->Add("InstanceofStub%s%s%s",
              args,
              inline_check,
              return_true_false_object);
571 572 573
}


574 575 576 577 578 579 580 581
void JSEntryStub::FinishCode(Handle<Code> code) {
  Handle<FixedArray> handler_table =
      code->GetIsolate()->factory()->NewFixedArray(1, TENURED);
  handler_table->set(0, Smi::FromInt(handler_offset_));
  code->set_handler_table(*handler_table);
}


582 583
void KeyedLoadDictionaryElementStub::Generate(MacroAssembler* masm) {
  KeyedLoadStubCompiler::GenerateLoadDictionaryElement(masm);
danno@chromium.org's avatar
danno@chromium.org committed
584 585 586
}


587 588 589 590 591 592
void CreateAllocationSiteStub::GenerateAheadOfTime(Isolate* isolate) {
  CreateAllocationSiteStub stub;
  stub.GetCode(isolate)->set_is_pregenerated(true);
}


593 594
void KeyedStoreElementStub::Generate(MacroAssembler* masm) {
  switch (elements_kind_) {
595
    case FAST_ELEMENTS:
596 597
    case FAST_HOLEY_ELEMENTS:
    case FAST_SMI_ELEMENTS:
598
    case FAST_HOLEY_SMI_ELEMENTS:
599
    case FAST_DOUBLE_ELEMENTS:
600
    case FAST_HOLEY_DOUBLE_ELEMENTS:
601 602 603 604 605 606 607 608 609
    case EXTERNAL_BYTE_ELEMENTS:
    case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
    case EXTERNAL_SHORT_ELEMENTS:
    case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
    case EXTERNAL_INT_ELEMENTS:
    case EXTERNAL_UNSIGNED_INT_ELEMENTS:
    case EXTERNAL_FLOAT_ELEMENTS:
    case EXTERNAL_DOUBLE_ELEMENTS:
    case EXTERNAL_PIXEL_ELEMENTS:
610
      UNREACHABLE();
611
      break;
612
    case DICTIONARY_ELEMENTS:
613 614
      KeyedStoreStubCompiler::GenerateStoreDictionaryElement(masm);
      break;
615
    case NON_STRICT_ARGUMENTS_ELEMENTS:
616 617 618
      UNREACHABLE();
      break;
  }
danno@chromium.org's avatar
danno@chromium.org committed
619 620 621
}


622
void ArgumentsAccessStub::PrintName(StringStream* stream) {
623
  stream->Add("ArgumentsAccessStub_");
624
  switch (type_) {
625 626 627 628
    case READ_ELEMENT: stream->Add("ReadElement"); break;
    case NEW_NON_STRICT_FAST: stream->Add("NewNonStrictFast"); break;
    case NEW_NON_STRICT_SLOW: stream->Add("NewNonStrictSlow"); break;
    case NEW_STRICT: stream->Add("NewStrict"); break;
629 630 631 632 633
  }
}


void CallFunctionStub::PrintName(StringStream* stream) {
634 635 636
  stream->Add("CallFunctionStub_Args%d", argc_);
  if (ReceiverMightBeImplicit()) stream->Add("_Implicit");
  if (RecordCallTarget()) stream->Add("_Recording");
637 638
}

639

640 641 642 643 644 645
void CallConstructStub::PrintName(StringStream* stream) {
  stream->Add("CallConstructStub");
  if (RecordCallTarget()) stream->Add("_Recording");
}


646
bool ToBooleanStub::UpdateStatus(Handle<Object> object) {
647
  Types old_types(types_);
648 649
  bool to_boolean_value = types_.UpdateStatus(object);
  TraceTransition(old_types, types_);
650 651 652 653
  return to_boolean_value;
}


654
void ToBooleanStub::PrintState(StringStream* stream) {
655 656 657 658
  types_.Print(stream);
}


659
void ToBooleanStub::Types::Print(StringStream* stream) const {
660 661 662 663 664 665 666 667 668 669 670 671
  stream->Add("(");
  SimpleListPrinter printer(stream);
  if (IsEmpty()) printer.Add("None");
  if (Contains(UNDEFINED)) printer.Add("Undefined");
  if (Contains(BOOLEAN)) printer.Add("Bool");
  if (Contains(NULL_TYPE)) printer.Add("Null");
  if (Contains(SMI)) printer.Add("Smi");
  if (Contains(SPEC_OBJECT)) printer.Add("SpecObject");
  if (Contains(STRING)) printer.Add("String");
  if (Contains(SYMBOL)) printer.Add("Symbol");
  if (Contains(HEAP_NUMBER)) printer.Add("HeapNumber");
  stream->Add(")");
672 673 674
}


675
bool ToBooleanStub::Types::UpdateStatus(Handle<Object> object) {
676 677 678 679 680 681 682 683 684 685 686 687 688 689
  if (object->IsUndefined()) {
    Add(UNDEFINED);
    return false;
  } else if (object->IsBoolean()) {
    Add(BOOLEAN);
    return object->IsTrue();
  } else if (object->IsNull()) {
    Add(NULL_TYPE);
    return false;
  } else if (object->IsSmi()) {
    Add(SMI);
    return Smi::cast(*object)->value() != 0;
  } else if (object->IsSpecObject()) {
    Add(SPEC_OBJECT);
690
    return !object->IsUndetectableObject();
691 692
  } else if (object->IsString()) {
    Add(STRING);
693 694
    return !object->IsUndetectableObject() &&
        String::cast(*object)->length() != 0;
695 696 697
  } else if (object->IsSymbol()) {
    Add(SYMBOL);
    return true;
698
  } else if (object->IsHeapNumber()) {
699
    ASSERT(!object->IsUndetectableObject());
700 701
    Add(HEAP_NUMBER);
    double value = HeapNumber::cast(*object)->value();
702
    return value != 0 && !std::isnan(value);
703
  } else {
704 705 706
    // We should never see an internal object at runtime here!
    UNREACHABLE();
    return true;
707 708 709 710
  }
}


711 712 713
bool ToBooleanStub::Types::NeedsMap() const {
  return Contains(ToBooleanStub::SPEC_OBJECT)
      || Contains(ToBooleanStub::STRING)
714
      || Contains(ToBooleanStub::SYMBOL)
715 716 717 718 719 720 721
      || Contains(ToBooleanStub::HEAP_NUMBER);
}


bool ToBooleanStub::Types::CanBeUndetectable() const {
  return Contains(ToBooleanStub::SPEC_OBJECT)
      || Contains(ToBooleanStub::STRING);
722 723 724
}


725
void StubFailureTrampolineStub::GenerateAheadOfTime(Isolate* isolate) {
726 727 728 729
  StubFailureTrampolineStub stub1(NOT_JS_FUNCTION_STUB_MODE);
  StubFailureTrampolineStub stub2(JS_FUNCTION_STUB_MODE);
  stub1.GetCode(isolate)->set_is_pregenerated(true);
  stub2.GetCode(isolate)->set_is_pregenerated(true);
730 731 732
}


733 734
void ProfileEntryHookStub::EntryHookTrampoline(intptr_t function,
                                               intptr_t stack_pointer) {
735 736 737
  FunctionEntryHook entry_hook = Isolate::Current()->function_entry_hook();
  ASSERT(entry_hook != NULL);
  entry_hook(function, stack_pointer);
738 739 740
}


741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
static void InstallDescriptor(Isolate* isolate, HydrogenCodeStub* stub) {
  int major_key = stub->MajorKey();
  CodeStubInterfaceDescriptor* descriptor =
      isolate->code_stub_interface_descriptor(major_key);
  if (!descriptor->initialized()) {
    stub->InitializeInterfaceDescriptor(isolate, descriptor);
  }
}


void ArrayConstructorStubBase::InstallDescriptors(Isolate* isolate) {
  ArrayNoArgumentConstructorStub stub1(GetInitialFastElementsKind());
  InstallDescriptor(isolate, &stub1);
  ArraySingleArgumentConstructorStub stub2(GetInitialFastElementsKind());
  InstallDescriptor(isolate, &stub2);
  ArrayNArgumentsConstructorStub stub3(GetInitialFastElementsKind());
  InstallDescriptor(isolate, &stub3);
}


761 762 763 764 765 766
void FastNewClosureStub::InstallDescriptors(Isolate* isolate) {
  FastNewClosureStub stub(STRICT_MODE, false);
  InstallDescriptor(isolate, &stub);
}


767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
ArrayConstructorStub::ArrayConstructorStub(Isolate* isolate)
    : argument_count_(ANY) {
  ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
}


ArrayConstructorStub::ArrayConstructorStub(Isolate* isolate,
                                           int argument_count) {
  if (argument_count == 0) {
    argument_count_ = NONE;
  } else if (argument_count == 1) {
    argument_count_ = ONE;
  } else if (argument_count >= 2) {
    argument_count_ = MORE_THAN_ONE;
  } else {
    UNREACHABLE();
  }
  ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
}


788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
void InternalArrayConstructorStubBase::InstallDescriptors(Isolate* isolate) {
  InternalArrayNoArgumentConstructorStub stub1(FAST_ELEMENTS);
  InstallDescriptor(isolate, &stub1);
  InternalArraySingleArgumentConstructorStub stub2(FAST_ELEMENTS);
  InstallDescriptor(isolate, &stub2);
  InternalArrayNArgumentsConstructorStub stub3(FAST_ELEMENTS);
  InstallDescriptor(isolate, &stub3);
}

InternalArrayConstructorStub::InternalArrayConstructorStub(
    Isolate* isolate) {
  InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
}


803
} }  // namespace v8::internal