code-stubs.h revision 402d937239b0e2fd11bf2f4fe972ad78aa9fd481
1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2006-2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifndef V8_CODE_STUBS_H_
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define V8_CODE_STUBS_H_
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 {
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal {
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
34d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block// List of code stubs used on all platforms. The order in this list is important
35d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block// as only the stubs up to and including RecordWrite allows nested stub calls.
36d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#define CODE_STUB_LIST_ALL_PLATFORMS(V)  \
37d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(CallFunction)                        \
38d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(GenericBinaryOp)                     \
39d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(StringAdd)                           \
40e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(SubString)                           \
41e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(StringCompare)                       \
42d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(SmiOp)                               \
43d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(Compare)                             \
44d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(RecordWrite)                         \
45d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(ConvertToDouble)                     \
46d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(WriteInt32ToHeapNumber)              \
47d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(StackCheck)                          \
48e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(FastNewClosure)                      \
49e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(FastNewContext)                      \
50e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(FastCloneShallowArray)               \
51402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  V(TranscendentalCache)                 \
52e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(GenericUnaryOp)                      \
53d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(RevertToNumber)                      \
54d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(ToBoolean)                           \
55d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(Instanceof)                          \
56d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(CounterOp)                           \
57d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(ArgumentsAccess)                     \
58e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(RegExpExec)                          \
59402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  V(NumberToString)                      \
60d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(CEntry)                              \
614515c472dc3e5ed2448a564600976759e569a0a8Leon Clarke  V(JSEntry)                             \
624515c472dc3e5ed2448a564600976759e569a0a8Leon Clarke  V(DebuggerStatement)
63d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block
64d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block// List of code stubs only used on ARM platforms.
65d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#ifdef V8_TARGET_ARCH_ARM
66d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#define CODE_STUB_LIST_ARM(V)  \
67d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(GetProperty)               \
68d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(SetProperty)               \
69d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(InvokeBuiltin)             \
70d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  V(RegExpCEntry)
71d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#else
72d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#define CODE_STUB_LIST_ARM(V)
73d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#endif
74d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block
75d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block// Combined list of code stubs.
76d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#define CODE_STUB_LIST(V)            \
77d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  CODE_STUB_LIST_ALL_PLATFORMS(V)    \
78d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  CODE_STUB_LIST_ARM(V)
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Stub is base classes of all stubs.
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockclass CodeStub BASE_EMBEDDED {
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block public:
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  enum Major {
84d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#define DEF_ENUM(name) name,
85d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block    CODE_STUB_LIST(DEF_ENUM)
86d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block#undef DEF_ENUM
87d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block    NoCache,  // marker for stubs that do custom caching
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    NUMBER_OF_IDS
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Retrieve the code for the stub. Generate the code if needed.
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Code> GetCode();
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
94e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // Retrieve the code for the stub if already generated.  Do not
95e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // generate the code if not already generated and instead return a
96e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // retry after GC Failure object.
97e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  Object* TryGetCode();
98e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static Major MajorKeyFromKey(uint32_t key) {
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return static_cast<Major>(MajorKeyBits::decode(key));
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static int MinorKeyFromKey(uint32_t key) {
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return MinorKeyBits::decode(key);
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
1053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  static const char* MajorName(Major major_key, bool allow_unknown_keys);
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual ~CodeStub() {}
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
109d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  // Override these methods to provide a custom caching mechanism for
110d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  // an individual type of code stub.
111d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  virtual bool GetCustomCache(Code** code_out) { return false; }
112d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  virtual void SetCustomCache(Code* value) { }
113d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block  virtual bool has_custom_cache() { return false; }
114d0582a6c46733687d045e4188a1bcd0123c758a1Steve Block
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block protected:
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static const int kMajorBits = 5;
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block private:
120e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // Lookup the code in the (possibly custom) cache.
121e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  bool FindCodeInCache(Code** code_out);
122e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke
123e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // Nonvirtual wrapper around the stub-specific Generate function.  Call
124e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // this function to set up the macro assembler and generate the code.
125e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  void GenerateCode(MacroAssembler* masm);
126e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Generates the assembler code for the stub.
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual void Generate(MacroAssembler* masm) = 0;
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
130e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // Perform bookkeeping required after code generation when stub code is
131e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  // initially generated.
132e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  void RecordCodeGeneration(Code* code, MacroAssembler* masm);
133e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Returns information for computing the number key.
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual Major MajorKey() = 0;
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual int MinorKey() = 0;
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The CallFunctionStub needs to override this so it can encode whether a
139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // lazily generated function should be fully optimized or not.
140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Returns a name for logging/debugging purposes.
1433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  virtual const char* GetName() { return MajorName(MajorKey(), false); }
144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG
146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  virtual void Print() { PrintF("%s\n", GetName()); }
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif
148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Computes the key based on major and minor.
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  uint32_t GetKey() {
151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return MinorKeyBits::encode(MinorKey()) |
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block           MajorKeyBits::encode(MajorKey());
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool AllowsStubCalls() { return MajorKey() <= RecordWrite; }
157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  friend class BreakPointIterator;
162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} }  // namespace v8::internal
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif  // V8_CODE_STUBS_H_
167