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