blob: d5189c27246a9b8cd0425303f29c0c66c9f4aa16 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Steve Blockd0582a62009-12-15 09:54:21 +000034// 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) \
Leon Clarkee46be812010-01-19 14:06:41 +000040 V(SubString) \
41 V(StringCompare) \
Steve Blockd0582a62009-12-15 09:54:21 +000042 V(SmiOp) \
43 V(Compare) \
44 V(RecordWrite) \
45 V(ConvertToDouble) \
46 V(WriteInt32ToHeapNumber) \
47 V(StackCheck) \
Leon Clarkee46be812010-01-19 14:06:41 +000048 V(FastNewClosure) \
49 V(FastNewContext) \
50 V(FastCloneShallowArray) \
Andrei Popescu402d9372010-02-26 13:31:12 +000051 V(TranscendentalCache) \
Leon Clarkee46be812010-01-19 14:06:41 +000052 V(GenericUnaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000053 V(RevertToNumber) \
54 V(ToBoolean) \
55 V(Instanceof) \
56 V(CounterOp) \
57 V(ArgumentsAccess) \
Leon Clarkee46be812010-01-19 14:06:41 +000058 V(RegExpExec) \
Andrei Popescu402d9372010-02-26 13:31:12 +000059 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000060 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000061 V(JSEntry) \
62 V(DebuggerStatement)
Steve Blockd0582a62009-12-15 09:54:21 +000063
64// List of code stubs only used on ARM platforms.
65#ifdef V8_TARGET_ARCH_ARM
66#define CODE_STUB_LIST_ARM(V) \
67 V(GetProperty) \
68 V(SetProperty) \
69 V(InvokeBuiltin) \
70 V(RegExpCEntry)
71#else
72#define CODE_STUB_LIST_ARM(V)
73#endif
74
75// Combined list of code stubs.
76#define CODE_STUB_LIST(V) \
77 CODE_STUB_LIST_ALL_PLATFORMS(V) \
78 CODE_STUB_LIST_ARM(V)
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80// Stub is base classes of all stubs.
81class CodeStub BASE_EMBEDDED {
82 public:
83 enum Major {
Steve Blockd0582a62009-12-15 09:54:21 +000084#define DEF_ENUM(name) name,
85 CODE_STUB_LIST(DEF_ENUM)
86#undef DEF_ENUM
87 NoCache, // marker for stubs that do custom caching
Steve Blocka7e24c12009-10-30 11:49:00 +000088 NUMBER_OF_IDS
89 };
90
91 // Retrieve the code for the stub. Generate the code if needed.
92 Handle<Code> GetCode();
93
Leon Clarkee46be812010-01-19 14:06:41 +000094 // Retrieve the code for the stub if already generated. Do not
95 // generate the code if not already generated and instead return a
96 // retry after GC Failure object.
97 Object* TryGetCode();
98
Steve Blocka7e24c12009-10-30 11:49:00 +000099 static Major MajorKeyFromKey(uint32_t key) {
100 return static_cast<Major>(MajorKeyBits::decode(key));
101 };
102 static int MinorKeyFromKey(uint32_t key) {
103 return MinorKeyBits::decode(key);
104 };
Andrei Popescu31002712010-02-23 13:46:05 +0000105 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
107 virtual ~CodeStub() {}
108
Steve Blockd0582a62009-12-15 09:54:21 +0000109 // Override these methods to provide a custom caching mechanism for
110 // an individual type of code stub.
111 virtual bool GetCustomCache(Code** code_out) { return false; }
112 virtual void SetCustomCache(Code* value) { }
113 virtual bool has_custom_cache() { return false; }
114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 protected:
116 static const int kMajorBits = 5;
117 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
118
119 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000120 // Lookup the code in the (possibly custom) cache.
121 bool FindCodeInCache(Code** code_out);
122
123 // Nonvirtual wrapper around the stub-specific Generate function. Call
124 // this function to set up the macro assembler and generate the code.
125 void GenerateCode(MacroAssembler* masm);
126
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 // Generates the assembler code for the stub.
128 virtual void Generate(MacroAssembler* masm) = 0;
129
Leon Clarkee46be812010-01-19 14:06:41 +0000130 // Perform bookkeeping required after code generation when stub code is
131 // initially generated.
132 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 // Returns information for computing the number key.
135 virtual Major MajorKey() = 0;
136 virtual int MinorKey() = 0;
137
138 // The CallFunctionStub needs to override this so it can encode whether a
139 // lazily generated function should be fully optimized or not.
140 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
141
142 // Returns a name for logging/debugging purposes.
Andrei Popescu31002712010-02-23 13:46:05 +0000143 virtual const char* GetName() { return MajorName(MajorKey(), false); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145#ifdef DEBUG
146 virtual void Print() { PrintF("%s\n", GetName()); }
147#endif
148
149 // Computes the key based on major and minor.
150 uint32_t GetKey() {
151 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
152 return MinorKeyBits::encode(MinorKey()) |
153 MajorKeyBits::encode(MajorKey());
154 }
155
156 bool AllowsStubCalls() { return MajorKey() <= RecordWrite; }
157
158 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
159 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
160
161 friend class BreakPointIterator;
162};
163
164} } // namespace v8::internal
165
166#endif // V8_CODE_STUBS_H_