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