blob: e5a222fcda065b780344894e2ec7c8af6d871422 [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
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "globals.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
Steve Blockd0582a62009-12-15 09:54:21 +000036// List of code stubs used on all platforms. The order in this list is important
37// as only the stubs up to and including RecordWrite allows nested stub calls.
38#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
39 V(CallFunction) \
40 V(GenericBinaryOp) \
41 V(StringAdd) \
Leon Clarkee46be812010-01-19 14:06:41 +000042 V(SubString) \
43 V(StringCompare) \
Steve Blockd0582a62009-12-15 09:54:21 +000044 V(SmiOp) \
45 V(Compare) \
46 V(RecordWrite) \
47 V(ConvertToDouble) \
48 V(WriteInt32ToHeapNumber) \
Steve Block8defd9f2010-07-08 12:39:36 +010049 V(IntegerMod) \
Steve Blockd0582a62009-12-15 09:54:21 +000050 V(StackCheck) \
Leon Clarkee46be812010-01-19 14:06:41 +000051 V(FastNewClosure) \
52 V(FastNewContext) \
53 V(FastCloneShallowArray) \
Andrei Popescu402d9372010-02-26 13:31:12 +000054 V(TranscendentalCache) \
Leon Clarkee46be812010-01-19 14:06:41 +000055 V(GenericUnaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000056 V(RevertToNumber) \
57 V(ToBoolean) \
58 V(Instanceof) \
59 V(CounterOp) \
60 V(ArgumentsAccess) \
Leon Clarkee46be812010-01-19 14:06:41 +000061 V(RegExpExec) \
Andrei Popescu402d9372010-02-26 13:31:12 +000062 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000063 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000064 V(JSEntry) \
65 V(DebuggerStatement)
Steve Blockd0582a62009-12-15 09:54:21 +000066
67// List of code stubs only used on ARM platforms.
68#ifdef V8_TARGET_ARCH_ARM
69#define CODE_STUB_LIST_ARM(V) \
70 V(GetProperty) \
71 V(SetProperty) \
72 V(InvokeBuiltin) \
73 V(RegExpCEntry)
74#else
75#define CODE_STUB_LIST_ARM(V)
76#endif
77
78// Combined list of code stubs.
79#define CODE_STUB_LIST(V) \
80 CODE_STUB_LIST_ALL_PLATFORMS(V) \
81 CODE_STUB_LIST_ARM(V)
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83// Stub is base classes of all stubs.
84class CodeStub BASE_EMBEDDED {
85 public:
86 enum Major {
Steve Blockd0582a62009-12-15 09:54:21 +000087#define DEF_ENUM(name) name,
88 CODE_STUB_LIST(DEF_ENUM)
89#undef DEF_ENUM
90 NoCache, // marker for stubs that do custom caching
Steve Blocka7e24c12009-10-30 11:49:00 +000091 NUMBER_OF_IDS
92 };
93
94 // Retrieve the code for the stub. Generate the code if needed.
95 Handle<Code> GetCode();
96
Leon Clarkee46be812010-01-19 14:06:41 +000097 // Retrieve the code for the stub if already generated. Do not
98 // generate the code if not already generated and instead return a
99 // retry after GC Failure object.
100 Object* TryGetCode();
101
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 static Major MajorKeyFromKey(uint32_t key) {
103 return static_cast<Major>(MajorKeyBits::decode(key));
104 };
105 static int MinorKeyFromKey(uint32_t key) {
106 return MinorKeyBits::decode(key);
107 };
Andrei Popescu31002712010-02-23 13:46:05 +0000108 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110 virtual ~CodeStub() {}
111
Steve Blockd0582a62009-12-15 09:54:21 +0000112 // Override these methods to provide a custom caching mechanism for
113 // an individual type of code stub.
114 virtual bool GetCustomCache(Code** code_out) { return false; }
115 virtual void SetCustomCache(Code* value) { }
116 virtual bool has_custom_cache() { return false; }
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 protected:
119 static const int kMajorBits = 5;
120 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
121
122 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000123 // Lookup the code in the (possibly custom) cache.
124 bool FindCodeInCache(Code** code_out);
125
126 // Nonvirtual wrapper around the stub-specific Generate function. Call
127 // this function to set up the macro assembler and generate the code.
128 void GenerateCode(MacroAssembler* masm);
129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 // Generates the assembler code for the stub.
131 virtual void Generate(MacroAssembler* masm) = 0;
132
Leon Clarkee46be812010-01-19 14:06:41 +0000133 // Perform bookkeeping required after code generation when stub code is
134 // initially generated.
135 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
136
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 // Returns information for computing the number key.
138 virtual Major MajorKey() = 0;
139 virtual int MinorKey() = 0;
140
141 // The CallFunctionStub needs to override this so it can encode whether a
142 // lazily generated function should be fully optimized or not.
143 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
144
Steve Block6ded16b2010-05-10 14:33:55 +0100145 // GenericBinaryOpStub needs to override this.
146 virtual int GetCodeKind();
147
148 // GenericBinaryOpStub needs to override this.
149 virtual InlineCacheState GetICState() {
150 return UNINITIALIZED;
151 }
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 // Returns a name for logging/debugging purposes.
Andrei Popescu31002712010-02-23 13:46:05 +0000154 virtual const char* GetName() { return MajorName(MajorKey(), false); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
156#ifdef DEBUG
157 virtual void Print() { PrintF("%s\n", GetName()); }
158#endif
159
160 // Computes the key based on major and minor.
161 uint32_t GetKey() {
162 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
163 return MinorKeyBits::encode(MinorKey()) |
164 MajorKeyBits::encode(MajorKey());
165 }
166
167 bool AllowsStubCalls() { return MajorKey() <= RecordWrite; }
168
169 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
170 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
171
172 friend class BreakPointIterator;
173};
174
175} } // namespace v8::internal
176
177#endif // V8_CODE_STUBS_H_