code-stubs.h revision 1e0659c275bb392c045087af4f6b0d7565cb3d77
1// Copyright 2011 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28#ifndef V8_CODE_STUBS_H_ 29#define V8_CODE_STUBS_H_ 30 31#include "globals.h" 32 33namespace v8 { 34namespace internal { 35 36// List of code stubs used on all platforms. The order in this list is important 37// as only the stubs up to and including Instanceof allows nested stub calls. 38#define CODE_STUB_LIST_ALL_PLATFORMS(V) \ 39 V(CallFunction) \ 40 V(GenericBinaryOp) \ 41 V(TypeRecordingBinaryOp) \ 42 V(StringAdd) \ 43 V(StringCharAt) \ 44 V(SubString) \ 45 V(StringCompare) \ 46 V(SmiOp) \ 47 V(Compare) \ 48 V(CompareIC) \ 49 V(MathPow) \ 50 V(TranscendentalCache) \ 51 V(Instanceof) \ 52 V(ConvertToDouble) \ 53 V(WriteInt32ToHeapNumber) \ 54 V(IntegerMod) \ 55 V(StackCheck) \ 56 V(FastNewClosure) \ 57 V(FastNewContext) \ 58 V(FastCloneShallowArray) \ 59 V(GenericUnaryOp) \ 60 V(RevertToNumber) \ 61 V(ToBoolean) \ 62 V(ToNumber) \ 63 V(CounterOp) \ 64 V(ArgumentsAccess) \ 65 V(RegExpExec) \ 66 V(RegExpConstructResult) \ 67 V(NumberToString) \ 68 V(CEntry) \ 69 V(JSEntry) \ 70 V(DebuggerStatement) 71 72// List of code stubs only used on ARM platforms. 73#ifdef V8_TARGET_ARCH_ARM 74#define CODE_STUB_LIST_ARM(V) \ 75 V(GetProperty) \ 76 V(SetProperty) \ 77 V(InvokeBuiltin) \ 78 V(RegExpCEntry) \ 79 V(DirectCEntry) 80#else 81#define CODE_STUB_LIST_ARM(V) 82#endif 83 84// Combined list of code stubs. 85#define CODE_STUB_LIST(V) \ 86 CODE_STUB_LIST_ALL_PLATFORMS(V) \ 87 CODE_STUB_LIST_ARM(V) 88 89// Types of uncatchable exceptions. 90enum UncatchableExceptionType { OUT_OF_MEMORY, TERMINATION }; 91 92// Mode to overwrite BinaryExpression values. 93enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT }; 94enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE }; 95 96 97// Stub is base classes of all stubs. 98class CodeStub BASE_EMBEDDED { 99 public: 100 enum Major { 101#define DEF_ENUM(name) name, 102 CODE_STUB_LIST(DEF_ENUM) 103#undef DEF_ENUM 104 NoCache, // marker for stubs that do custom caching 105 NUMBER_OF_IDS 106 }; 107 108 // Retrieve the code for the stub. Generate the code if needed. 109 Handle<Code> GetCode(); 110 111 // Retrieve the code for the stub if already generated. Do not 112 // generate the code if not already generated and instead return a 113 // retry after GC Failure object. 114 MUST_USE_RESULT MaybeObject* TryGetCode(); 115 116 static Major MajorKeyFromKey(uint32_t key) { 117 return static_cast<Major>(MajorKeyBits::decode(key)); 118 } 119 static int MinorKeyFromKey(uint32_t key) { 120 return MinorKeyBits::decode(key); 121 } 122 123 // Gets the major key from a code object that is a code stub or binary op IC. 124 static Major GetMajorKey(Code* code_stub) { 125 return static_cast<Major>(code_stub->major_key()); 126 } 127 128 static const char* MajorName(Major major_key, bool allow_unknown_keys); 129 130 virtual ~CodeStub() {} 131 132 protected: 133 static const int kMajorBits = 6; 134 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits; 135 136 private: 137 // Lookup the code in the (possibly custom) cache. 138 bool FindCodeInCache(Code** code_out); 139 140 // Nonvirtual wrapper around the stub-specific Generate function. Call 141 // this function to set up the macro assembler and generate the code. 142 void GenerateCode(MacroAssembler* masm); 143 144 // Generates the assembler code for the stub. 145 virtual void Generate(MacroAssembler* masm) = 0; 146 147 // Perform bookkeeping required after code generation when stub code is 148 // initially generated. 149 void RecordCodeGeneration(Code* code, MacroAssembler* masm); 150 151 // Finish the code object after it has been generated. 152 virtual void FinishCode(Code* code) { } 153 154 // Returns information for computing the number key. 155 virtual Major MajorKey() = 0; 156 virtual int MinorKey() = 0; 157 158 // The CallFunctionStub needs to override this so it can encode whether a 159 // lazily generated function should be fully optimized or not. 160 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; } 161 162 // GenericBinaryOpStub needs to override this. 163 virtual int GetCodeKind(); 164 165 // GenericBinaryOpStub needs to override this. 166 virtual InlineCacheState GetICState() { 167 return UNINITIALIZED; 168 } 169 170 // Returns a name for logging/debugging purposes. 171 virtual const char* GetName() { return MajorName(MajorKey(), false); } 172 173#ifdef DEBUG 174 virtual void Print() { PrintF("%s\n", GetName()); } 175#endif 176 177 // Computes the key based on major and minor. 178 uint32_t GetKey() { 179 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS); 180 return MinorKeyBits::encode(MinorKey()) | 181 MajorKeyBits::encode(MajorKey()); 182 } 183 184 bool AllowsStubCalls() { return MajorKey() <= Instanceof; } 185 186 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {}; 187 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {}; 188 189 friend class BreakPointIterator; 190}; 191 192 193// Helper interface to prepare to/restore after making runtime calls. 194class RuntimeCallHelper { 195 public: 196 virtual ~RuntimeCallHelper() {} 197 198 virtual void BeforeCall(MacroAssembler* masm) const = 0; 199 200 virtual void AfterCall(MacroAssembler* masm) const = 0; 201 202 protected: 203 RuntimeCallHelper() {} 204 205 private: 206 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper); 207}; 208 209} } // namespace v8::internal 210 211#if V8_TARGET_ARCH_IA32 212#include "ia32/code-stubs-ia32.h" 213#elif V8_TARGET_ARCH_X64 214#include "x64/code-stubs-x64.h" 215#elif V8_TARGET_ARCH_ARM 216#include "arm/code-stubs-arm.h" 217#elif V8_TARGET_ARCH_MIPS 218#include "mips/code-stubs-mips.h" 219#else 220#error Unsupported target architecture. 221#endif 222 223namespace v8 { 224namespace internal { 225 226 227// RuntimeCallHelper implementation used in stubs: enters/leaves a 228// newly created internal frame before/after the runtime call. 229class StubRuntimeCallHelper : public RuntimeCallHelper { 230 public: 231 StubRuntimeCallHelper() {} 232 233 virtual void BeforeCall(MacroAssembler* masm) const; 234 235 virtual void AfterCall(MacroAssembler* masm) const; 236}; 237 238 239// Trivial RuntimeCallHelper implementation. 240class NopRuntimeCallHelper : public RuntimeCallHelper { 241 public: 242 NopRuntimeCallHelper() {} 243 244 virtual void BeforeCall(MacroAssembler* masm) const {} 245 246 virtual void AfterCall(MacroAssembler* masm) const {} 247}; 248 249 250class StackCheckStub : public CodeStub { 251 public: 252 StackCheckStub() { } 253 254 void Generate(MacroAssembler* masm); 255 256 private: 257 258 const char* GetName() { return "StackCheckStub"; } 259 260 Major MajorKey() { return StackCheck; } 261 int MinorKey() { return 0; } 262}; 263 264 265class ToNumberStub: public CodeStub { 266 public: 267 ToNumberStub() { } 268 269 void Generate(MacroAssembler* masm); 270 271 private: 272 Major MajorKey() { return ToNumber; } 273 int MinorKey() { return 0; } 274 const char* GetName() { return "ToNumberStub"; } 275}; 276 277 278class FastNewClosureStub : public CodeStub { 279 public: 280 void Generate(MacroAssembler* masm); 281 282 private: 283 const char* GetName() { return "FastNewClosureStub"; } 284 Major MajorKey() { return FastNewClosure; } 285 int MinorKey() { return 0; } 286}; 287 288 289class FastNewContextStub : public CodeStub { 290 public: 291 static const int kMaximumSlots = 64; 292 293 explicit FastNewContextStub(int slots) : slots_(slots) { 294 ASSERT(slots_ > 0 && slots <= kMaximumSlots); 295 } 296 297 void Generate(MacroAssembler* masm); 298 299 private: 300 int slots_; 301 302 const char* GetName() { return "FastNewContextStub"; } 303 Major MajorKey() { return FastNewContext; } 304 int MinorKey() { return slots_; } 305}; 306 307 308class FastCloneShallowArrayStub : public CodeStub { 309 public: 310 // Maximum length of copied elements array. 311 static const int kMaximumClonedLength = 8; 312 313 enum Mode { 314 CLONE_ELEMENTS, 315 COPY_ON_WRITE_ELEMENTS 316 }; 317 318 FastCloneShallowArrayStub(Mode mode, int length) 319 : mode_(mode), 320 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) { 321 ASSERT(length_ >= 0); 322 ASSERT(length_ <= kMaximumClonedLength); 323 } 324 325 void Generate(MacroAssembler* masm); 326 327 private: 328 Mode mode_; 329 int length_; 330 331 const char* GetName() { return "FastCloneShallowArrayStub"; } 332 Major MajorKey() { return FastCloneShallowArray; } 333 int MinorKey() { 334 ASSERT(mode_ == 0 || mode_ == 1); 335 return (length_ << 1) | mode_; 336 } 337}; 338 339 340class InstanceofStub: public CodeStub { 341 public: 342 enum Flags { 343 kNoFlags = 0, 344 kArgsInRegisters = 1 << 0, 345 kCallSiteInlineCheck = 1 << 1, 346 kReturnTrueFalseObject = 1 << 2 347 }; 348 349 explicit InstanceofStub(Flags flags) : flags_(flags), name_(NULL) { } 350 351 static Register left(); 352 static Register right(); 353 354 void Generate(MacroAssembler* masm); 355 356 private: 357 Major MajorKey() { return Instanceof; } 358 int MinorKey() { return static_cast<int>(flags_); } 359 360 bool HasArgsInRegisters() const { 361 return (flags_ & kArgsInRegisters) != 0; 362 } 363 364 bool HasCallSiteInlineCheck() const { 365 return (flags_ & kCallSiteInlineCheck) != 0; 366 } 367 368 bool ReturnTrueFalseObject() const { 369 return (flags_ & kReturnTrueFalseObject) != 0; 370 } 371 372 const char* GetName(); 373 374 Flags flags_; 375 char* name_; 376}; 377 378 379enum NegativeZeroHandling { 380 kStrictNegativeZero, 381 kIgnoreNegativeZero 382}; 383 384 385enum UnaryOpFlags { 386 NO_UNARY_FLAGS = 0, 387 NO_UNARY_SMI_CODE_IN_STUB = 1 << 0 388}; 389 390 391class GenericUnaryOpStub : public CodeStub { 392 public: 393 GenericUnaryOpStub(Token::Value op, 394 UnaryOverwriteMode overwrite, 395 UnaryOpFlags flags, 396 NegativeZeroHandling negative_zero = kStrictNegativeZero) 397 : op_(op), 398 overwrite_(overwrite), 399 include_smi_code_((flags & NO_UNARY_SMI_CODE_IN_STUB) == 0), 400 negative_zero_(negative_zero) { } 401 402 private: 403 Token::Value op_; 404 UnaryOverwriteMode overwrite_; 405 bool include_smi_code_; 406 NegativeZeroHandling negative_zero_; 407 408 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {}; 409 class IncludeSmiCodeField: public BitField<bool, 1, 1> {}; 410 class NegativeZeroField: public BitField<NegativeZeroHandling, 2, 1> {}; 411 class OpField: public BitField<Token::Value, 3, kMinorBits - 3> {}; 412 413 Major MajorKey() { return GenericUnaryOp; } 414 int MinorKey() { 415 return OpField::encode(op_) | 416 OverwriteField::encode(overwrite_) | 417 IncludeSmiCodeField::encode(include_smi_code_) | 418 NegativeZeroField::encode(negative_zero_); 419 } 420 421 void Generate(MacroAssembler* masm); 422 423 const char* GetName(); 424}; 425 426 427class MathPowStub: public CodeStub { 428 public: 429 MathPowStub() {} 430 virtual void Generate(MacroAssembler* masm); 431 432 private: 433 virtual CodeStub::Major MajorKey() { return MathPow; } 434 virtual int MinorKey() { return 0; } 435 436 const char* GetName() { return "MathPowStub"; } 437}; 438 439 440class StringCharAtStub: public CodeStub { 441 public: 442 StringCharAtStub() {} 443 444 private: 445 Major MajorKey() { return StringCharAt; } 446 int MinorKey() { return 0; } 447 448 void Generate(MacroAssembler* masm); 449}; 450 451 452class ICCompareStub: public CodeStub { 453 public: 454 ICCompareStub(Token::Value op, CompareIC::State state) 455 : op_(op), state_(state) { 456 ASSERT(Token::IsCompareOp(op)); 457 } 458 459 virtual void Generate(MacroAssembler* masm); 460 461 private: 462 class OpField: public BitField<int, 0, 3> { }; 463 class StateField: public BitField<int, 3, 5> { }; 464 465 virtual void FinishCode(Code* code) { code->set_compare_state(state_); } 466 467 virtual CodeStub::Major MajorKey() { return CompareIC; } 468 virtual int MinorKey(); 469 470 virtual int GetCodeKind() { return Code::COMPARE_IC; } 471 472 void GenerateSmis(MacroAssembler* masm); 473 void GenerateHeapNumbers(MacroAssembler* masm); 474 void GenerateObjects(MacroAssembler* masm); 475 void GenerateMiss(MacroAssembler* masm); 476 477 bool strict() const { return op_ == Token::EQ_STRICT; } 478 Condition GetCondition() const { return CompareIC::ComputeCondition(op_); } 479 480 Token::Value op_; 481 CompareIC::State state_; 482}; 483 484 485// Flags that control the compare stub code generation. 486enum CompareFlags { 487 NO_COMPARE_FLAGS = 0, 488 NO_SMI_COMPARE_IN_STUB = 1 << 0, 489 NO_NUMBER_COMPARE_IN_STUB = 1 << 1, 490 CANT_BOTH_BE_NAN = 1 << 2 491}; 492 493 494enum NaNInformation { 495 kBothCouldBeNaN, 496 kCantBothBeNaN 497}; 498 499 500class CompareStub: public CodeStub { 501 public: 502 CompareStub(Condition cc, 503 bool strict, 504 CompareFlags flags, 505 Register lhs, 506 Register rhs) : 507 cc_(cc), 508 strict_(strict), 509 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0), 510 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0), 511 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0), 512 lhs_(lhs), 513 rhs_(rhs), 514 name_(NULL) { } 515 516 CompareStub(Condition cc, 517 bool strict, 518 CompareFlags flags) : 519 cc_(cc), 520 strict_(strict), 521 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0), 522 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0), 523 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0), 524 lhs_(no_reg), 525 rhs_(no_reg), 526 name_(NULL) { } 527 528 void Generate(MacroAssembler* masm); 529 530 private: 531 Condition cc_; 532 bool strict_; 533 // Only used for 'equal' comparisons. Tells the stub that we already know 534 // that at least one side of the comparison is not NaN. This allows the 535 // stub to use object identity in the positive case. We ignore it when 536 // generating the minor key for other comparisons to avoid creating more 537 // stubs. 538 bool never_nan_nan_; 539 // Do generate the number comparison code in the stub. Stubs without number 540 // comparison code is used when the number comparison has been inlined, and 541 // the stub will be called if one of the operands is not a number. 542 bool include_number_compare_; 543 544 // Generate the comparison code for two smi operands in the stub. 545 bool include_smi_compare_; 546 547 // Register holding the left hand side of the comparison if the stub gives 548 // a choice, no_reg otherwise. 549 550 Register lhs_; 551 // Register holding the right hand side of the comparison if the stub gives 552 // a choice, no_reg otherwise. 553 Register rhs_; 554 555 // Encoding of the minor key in 16 bits. 556 class StrictField: public BitField<bool, 0, 1> {}; 557 class NeverNanNanField: public BitField<bool, 1, 1> {}; 558 class IncludeNumberCompareField: public BitField<bool, 2, 1> {}; 559 class IncludeSmiCompareField: public BitField<bool, 3, 1> {}; 560 class RegisterField: public BitField<bool, 4, 1> {}; 561 class ConditionField: public BitField<int, 5, 11> {}; 562 563 Major MajorKey() { return Compare; } 564 565 int MinorKey(); 566 567 virtual int GetCodeKind() { return Code::COMPARE_IC; } 568 virtual void FinishCode(Code* code) { 569 code->set_compare_state(CompareIC::GENERIC); 570 } 571 572 // Branch to the label if the given object isn't a symbol. 573 void BranchIfNonSymbol(MacroAssembler* masm, 574 Label* label, 575 Register object, 576 Register scratch); 577 578 // Unfortunately you have to run without snapshots to see most of these 579 // names in the profile since most compare stubs end up in the snapshot. 580 char* name_; 581 const char* GetName(); 582#ifdef DEBUG 583 void Print() { 584 PrintF("CompareStub (minor %d) (cc %d), (strict %s), " 585 "(never_nan_nan %s), (smi_compare %s) (number_compare %s) ", 586 MinorKey(), 587 static_cast<int>(cc_), 588 strict_ ? "true" : "false", 589 never_nan_nan_ ? "true" : "false", 590 include_smi_compare_ ? "inluded" : "not included", 591 include_number_compare_ ? "included" : "not included"); 592 593 if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) { 594 PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code()); 595 } else { 596 PrintF("\n"); 597 } 598 } 599#endif 600}; 601 602 603class CEntryStub : public CodeStub { 604 public: 605 explicit CEntryStub(int result_size) 606 : result_size_(result_size), save_doubles_(false) { } 607 608 void Generate(MacroAssembler* masm); 609 void SaveDoubles() { save_doubles_ = true; } 610 611 private: 612 void GenerateCore(MacroAssembler* masm, 613 Label* throw_normal_exception, 614 Label* throw_termination_exception, 615 Label* throw_out_of_memory_exception, 616 bool do_gc, 617 bool always_allocate_scope); 618 void GenerateThrowTOS(MacroAssembler* masm); 619 void GenerateThrowUncatchable(MacroAssembler* masm, 620 UncatchableExceptionType type); 621 622 // Number of pointers/values returned. 623 const int result_size_; 624 bool save_doubles_; 625 626 Major MajorKey() { return CEntry; } 627 int MinorKey(); 628 629 const char* GetName() { return "CEntryStub"; } 630}; 631 632 633class JSEntryStub : public CodeStub { 634 public: 635 JSEntryStub() { } 636 637 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); } 638 639 protected: 640 void GenerateBody(MacroAssembler* masm, bool is_construct); 641 642 private: 643 Major MajorKey() { return JSEntry; } 644 int MinorKey() { return 0; } 645 646 const char* GetName() { return "JSEntryStub"; } 647}; 648 649 650class JSConstructEntryStub : public JSEntryStub { 651 public: 652 JSConstructEntryStub() { } 653 654 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); } 655 656 private: 657 int MinorKey() { return 1; } 658 659 const char* GetName() { return "JSConstructEntryStub"; } 660}; 661 662 663class ArgumentsAccessStub: public CodeStub { 664 public: 665 enum Type { 666 READ_ELEMENT, 667 NEW_OBJECT 668 }; 669 670 explicit ArgumentsAccessStub(Type type) : type_(type) { } 671 672 private: 673 Type type_; 674 675 Major MajorKey() { return ArgumentsAccess; } 676 int MinorKey() { return type_; } 677 678 void Generate(MacroAssembler* masm); 679 void GenerateReadElement(MacroAssembler* masm); 680 void GenerateNewObject(MacroAssembler* masm); 681 682 const char* GetName() { return "ArgumentsAccessStub"; } 683 684#ifdef DEBUG 685 void Print() { 686 PrintF("ArgumentsAccessStub (type %d)\n", type_); 687 } 688#endif 689}; 690 691 692class RegExpExecStub: public CodeStub { 693 public: 694 RegExpExecStub() { } 695 696 private: 697 Major MajorKey() { return RegExpExec; } 698 int MinorKey() { return 0; } 699 700 void Generate(MacroAssembler* masm); 701 702 const char* GetName() { return "RegExpExecStub"; } 703 704#ifdef DEBUG 705 void Print() { 706 PrintF("RegExpExecStub\n"); 707 } 708#endif 709}; 710 711 712class RegExpConstructResultStub: public CodeStub { 713 public: 714 RegExpConstructResultStub() { } 715 716 private: 717 Major MajorKey() { return RegExpConstructResult; } 718 int MinorKey() { return 0; } 719 720 void Generate(MacroAssembler* masm); 721 722 const char* GetName() { return "RegExpConstructResultStub"; } 723 724#ifdef DEBUG 725 void Print() { 726 PrintF("RegExpConstructResultStub\n"); 727 } 728#endif 729}; 730 731 732class CallFunctionStub: public CodeStub { 733 public: 734 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags) 735 : argc_(argc), in_loop_(in_loop), flags_(flags) { } 736 737 void Generate(MacroAssembler* masm); 738 739 static int ExtractArgcFromMinorKey(int minor_key) { 740 return ArgcBits::decode(minor_key); 741 } 742 743 private: 744 int argc_; 745 InLoopFlag in_loop_; 746 CallFunctionFlags flags_; 747 748#ifdef DEBUG 749 void Print() { 750 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n", 751 argc_, 752 static_cast<int>(in_loop_), 753 static_cast<int>(flags_)); 754 } 755#endif 756 757 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>. 758 class InLoopBits: public BitField<InLoopFlag, 0, 1> {}; 759 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {}; 760 class ArgcBits: public BitField<int, 2, 32 - 2> {}; 761 762 Major MajorKey() { return CallFunction; } 763 int MinorKey() { 764 // Encode the parameters in a unique 32 bit value. 765 return InLoopBits::encode(in_loop_) 766 | FlagBits::encode(flags_) 767 | ArgcBits::encode(argc_); 768 } 769 770 InLoopFlag InLoop() { return in_loop_; } 771 bool ReceiverMightBeValue() { 772 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0; 773 } 774}; 775 776 777enum StringIndexFlags { 778 // Accepts smis or heap numbers. 779 STRING_INDEX_IS_NUMBER, 780 781 // Accepts smis or heap numbers that are valid array indices 782 // (ECMA-262 15.4). Invalid indices are reported as being out of 783 // range. 784 STRING_INDEX_IS_ARRAY_INDEX 785}; 786 787 788// Generates code implementing String.prototype.charCodeAt. 789// 790// Only supports the case when the receiver is a string and the index 791// is a number (smi or heap number) that is a valid index into the 792// string. Additional index constraints are specified by the 793// flags. Otherwise, bails out to the provided labels. 794// 795// Register usage: |object| may be changed to another string in a way 796// that doesn't affect charCodeAt/charAt semantics, |index| is 797// preserved, |scratch| and |result| are clobbered. 798class StringCharCodeAtGenerator { 799 public: 800 StringCharCodeAtGenerator(Register object, 801 Register index, 802 Register scratch, 803 Register result, 804 Label* receiver_not_string, 805 Label* index_not_number, 806 Label* index_out_of_range, 807 StringIndexFlags index_flags) 808 : object_(object), 809 index_(index), 810 scratch_(scratch), 811 result_(result), 812 receiver_not_string_(receiver_not_string), 813 index_not_number_(index_not_number), 814 index_out_of_range_(index_out_of_range), 815 index_flags_(index_flags) { 816 ASSERT(!scratch_.is(object_)); 817 ASSERT(!scratch_.is(index_)); 818 ASSERT(!scratch_.is(result_)); 819 ASSERT(!result_.is(object_)); 820 ASSERT(!result_.is(index_)); 821 } 822 823 // Generates the fast case code. On the fallthrough path |result| 824 // register contains the result. 825 void GenerateFast(MacroAssembler* masm); 826 827 // Generates the slow case code. Must not be naturally 828 // reachable. Expected to be put after a ret instruction (e.g., in 829 // deferred code). Always jumps back to the fast case. 830 void GenerateSlow(MacroAssembler* masm, 831 const RuntimeCallHelper& call_helper); 832 833 private: 834 Register object_; 835 Register index_; 836 Register scratch_; 837 Register result_; 838 839 Label* receiver_not_string_; 840 Label* index_not_number_; 841 Label* index_out_of_range_; 842 843 StringIndexFlags index_flags_; 844 845 Label call_runtime_; 846 Label index_not_smi_; 847 Label got_smi_index_; 848 Label exit_; 849 850 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator); 851}; 852 853 854// Generates code for creating a one-char string from a char code. 855class StringCharFromCodeGenerator { 856 public: 857 StringCharFromCodeGenerator(Register code, 858 Register result) 859 : code_(code), 860 result_(result) { 861 ASSERT(!code_.is(result_)); 862 } 863 864 // Generates the fast case code. On the fallthrough path |result| 865 // register contains the result. 866 void GenerateFast(MacroAssembler* masm); 867 868 // Generates the slow case code. Must not be naturally 869 // reachable. Expected to be put after a ret instruction (e.g., in 870 // deferred code). Always jumps back to the fast case. 871 void GenerateSlow(MacroAssembler* masm, 872 const RuntimeCallHelper& call_helper); 873 874 private: 875 Register code_; 876 Register result_; 877 878 Label slow_case_; 879 Label exit_; 880 881 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator); 882}; 883 884 885// Generates code implementing String.prototype.charAt. 886// 887// Only supports the case when the receiver is a string and the index 888// is a number (smi or heap number) that is a valid index into the 889// string. Additional index constraints are specified by the 890// flags. Otherwise, bails out to the provided labels. 891// 892// Register usage: |object| may be changed to another string in a way 893// that doesn't affect charCodeAt/charAt semantics, |index| is 894// preserved, |scratch1|, |scratch2|, and |result| are clobbered. 895class StringCharAtGenerator { 896 public: 897 StringCharAtGenerator(Register object, 898 Register index, 899 Register scratch1, 900 Register scratch2, 901 Register result, 902 Label* receiver_not_string, 903 Label* index_not_number, 904 Label* index_out_of_range, 905 StringIndexFlags index_flags) 906 : char_code_at_generator_(object, 907 index, 908 scratch1, 909 scratch2, 910 receiver_not_string, 911 index_not_number, 912 index_out_of_range, 913 index_flags), 914 char_from_code_generator_(scratch2, result) {} 915 916 // Generates the fast case code. On the fallthrough path |result| 917 // register contains the result. 918 void GenerateFast(MacroAssembler* masm); 919 920 // Generates the slow case code. Must not be naturally 921 // reachable. Expected to be put after a ret instruction (e.g., in 922 // deferred code). Always jumps back to the fast case. 923 void GenerateSlow(MacroAssembler* masm, 924 const RuntimeCallHelper& call_helper); 925 926 private: 927 StringCharCodeAtGenerator char_code_at_generator_; 928 StringCharFromCodeGenerator char_from_code_generator_; 929 930 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator); 931}; 932 933 934class AllowStubCallsScope { 935 public: 936 AllowStubCallsScope(MacroAssembler* masm, bool allow) 937 : masm_(masm), previous_allow_(masm->allow_stub_calls()) { 938 masm_->set_allow_stub_calls(allow); 939 } 940 ~AllowStubCallsScope() { 941 masm_->set_allow_stub_calls(previous_allow_); 942 } 943 944 private: 945 MacroAssembler* masm_; 946 bool previous_allow_; 947 948 DISALLOW_COPY_AND_ASSIGN(AllowStubCallsScope); 949}; 950 951} } // namespace v8::internal 952 953#endif // V8_CODE_STUBS_H_ 954