code-stubs.h revision 3100271588b61cbc1dc472a3f2f105d2eed8497f
1// Copyright 2006-2008 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 31namespace v8 { 32namespace internal { 33 34// List of code stubs used on all platforms. The order in this list is important 35// as only the stubs up to and including RecordWrite allows nested stub calls. 36#define CODE_STUB_LIST_ALL_PLATFORMS(V) \ 37 V(CallFunction) \ 38 V(GenericBinaryOp) \ 39 V(StringAdd) \ 40 V(SubString) \ 41 V(StringCompare) \ 42 V(SmiOp) \ 43 V(Compare) \ 44 V(RecordWrite) \ 45 V(ConvertToDouble) \ 46 V(WriteInt32ToHeapNumber) \ 47 V(StackCheck) \ 48 V(FastNewClosure) \ 49 V(FastNewContext) \ 50 V(FastCloneShallowArray) \ 51 V(GenericUnaryOp) \ 52 V(RevertToNumber) \ 53 V(ToBoolean) \ 54 V(Instanceof) \ 55 V(CounterOp) \ 56 V(ArgumentsAccess) \ 57 V(RegExpExec) \ 58 V(CEntry) \ 59 V(JSEntry) \ 60 V(DebuggerStatement) 61 62// List of code stubs only used on ARM platforms. 63#ifdef V8_TARGET_ARCH_ARM 64#define CODE_STUB_LIST_ARM(V) \ 65 V(GetProperty) \ 66 V(SetProperty) \ 67 V(InvokeBuiltin) \ 68 V(RegExpCEntry) 69#else 70#define CODE_STUB_LIST_ARM(V) 71#endif 72 73// Combined list of code stubs. 74#define CODE_STUB_LIST(V) \ 75 CODE_STUB_LIST_ALL_PLATFORMS(V) \ 76 CODE_STUB_LIST_ARM(V) 77 78// Stub is base classes of all stubs. 79class CodeStub BASE_EMBEDDED { 80 public: 81 enum Major { 82#define DEF_ENUM(name) name, 83 CODE_STUB_LIST(DEF_ENUM) 84#undef DEF_ENUM 85 NoCache, // marker for stubs that do custom caching 86 NUMBER_OF_IDS 87 }; 88 89 // Retrieve the code for the stub. Generate the code if needed. 90 Handle<Code> GetCode(); 91 92 // Retrieve the code for the stub if already generated. Do not 93 // generate the code if not already generated and instead return a 94 // retry after GC Failure object. 95 Object* TryGetCode(); 96 97 static Major MajorKeyFromKey(uint32_t key) { 98 return static_cast<Major>(MajorKeyBits::decode(key)); 99 }; 100 static int MinorKeyFromKey(uint32_t key) { 101 return MinorKeyBits::decode(key); 102 }; 103 static const char* MajorName(Major major_key, bool allow_unknown_keys); 104 105 virtual ~CodeStub() {} 106 107 // Override these methods to provide a custom caching mechanism for 108 // an individual type of code stub. 109 virtual bool GetCustomCache(Code** code_out) { return false; } 110 virtual void SetCustomCache(Code* value) { } 111 virtual bool has_custom_cache() { return false; } 112 113 protected: 114 static const int kMajorBits = 5; 115 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits; 116 117 private: 118 // Lookup the code in the (possibly custom) cache. 119 bool FindCodeInCache(Code** code_out); 120 121 // Nonvirtual wrapper around the stub-specific Generate function. Call 122 // this function to set up the macro assembler and generate the code. 123 void GenerateCode(MacroAssembler* masm); 124 125 // Generates the assembler code for the stub. 126 virtual void Generate(MacroAssembler* masm) = 0; 127 128 // Perform bookkeeping required after code generation when stub code is 129 // initially generated. 130 void RecordCodeGeneration(Code* code, MacroAssembler* masm); 131 132 // Returns information for computing the number key. 133 virtual Major MajorKey() = 0; 134 virtual int MinorKey() = 0; 135 136 // The CallFunctionStub needs to override this so it can encode whether a 137 // lazily generated function should be fully optimized or not. 138 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; } 139 140 // Returns a name for logging/debugging purposes. 141 virtual const char* GetName() { return MajorName(MajorKey(), false); } 142 143#ifdef DEBUG 144 virtual void Print() { PrintF("%s\n", GetName()); } 145#endif 146 147 // Computes the key based on major and minor. 148 uint32_t GetKey() { 149 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS); 150 return MinorKeyBits::encode(MinorKey()) | 151 MajorKeyBits::encode(MajorKey()); 152 } 153 154 bool AllowsStubCalls() { return MajorKey() <= RecordWrite; } 155 156 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {}; 157 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {}; 158 159 friend class BreakPointIterator; 160}; 161 162} } // namespace v8::internal 163 164#endif // V8_CODE_STUBS_H_ 165