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