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_CODEGEN_H_ |
| 29 | #define V8_CODEGEN_H_ |
| 30 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | #include "code-stubs.h" |
| 32 | #include "runtime.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | #include "type-info.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | |
| 35 | // Include the declaration of the architecture defined class CodeGenerator. |
| 36 | // The contract to the shared code is that the the CodeGenerator is a subclass |
| 37 | // of Visitor and that the following methods are available publicly: |
| 38 | // MakeCode |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 39 | // MakeCodePrologue |
| 40 | // MakeCodeEpilogue |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | // masm |
| 42 | // frame |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 43 | // script |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | // has_valid_frame |
| 45 | // SetFrame |
| 46 | // DeleteFrame |
| 47 | // allocator |
| 48 | // AddDeferred |
| 49 | // in_spilled_code |
| 50 | // set_in_spilled_code |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 51 | // RecordPositions |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | // |
| 53 | // These methods are either used privately by the shared code or implemented as |
| 54 | // shared code: |
| 55 | // CodeGenerator |
| 56 | // ~CodeGenerator |
| 57 | // ProcessDeferred |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 58 | // Generate |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 59 | // ComputeLazyCompile |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 60 | // BuildFunctionInfo |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | // ComputeCallInitialize |
| 62 | // ComputeCallInitializeInLoop |
| 63 | // ProcessDeclarations |
| 64 | // DeclareGlobals |
| 65 | // FindInlineRuntimeLUT |
| 66 | // CheckForInlineRuntimeCall |
| 67 | // PatchInlineRuntimeEntry |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 68 | // AnalyzeCondition |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | // CodeForFunctionPosition |
| 70 | // CodeForReturnPosition |
| 71 | // CodeForStatementPosition |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 72 | // CodeForDoWhileConditionPosition |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | // CodeForSourcePosition |
| 74 | |
| 75 | |
| 76 | // Mode to overwrite BinaryExpression values. |
| 77 | enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT }; |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 78 | enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
| 80 | // Types of uncatchable exceptions. |
| 81 | enum UncatchableExceptionType { OUT_OF_MEMORY, TERMINATION }; |
| 82 | |
| 83 | |
| 84 | #if V8_TARGET_ARCH_IA32 |
| 85 | #include "ia32/codegen-ia32.h" |
| 86 | #elif V8_TARGET_ARCH_X64 |
| 87 | #include "x64/codegen-x64.h" |
| 88 | #elif V8_TARGET_ARCH_ARM |
| 89 | #include "arm/codegen-arm.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 90 | #elif V8_TARGET_ARCH_MIPS |
| 91 | #include "mips/codegen-mips.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | #else |
| 93 | #error Unsupported target architecture. |
| 94 | #endif |
| 95 | |
| 96 | #include "register-allocator.h" |
| 97 | |
| 98 | namespace v8 { |
| 99 | namespace internal { |
| 100 | |
| 101 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 102 | #define INLINE_RUNTIME_FUNCTION_LIST(F) \ |
| 103 | F(IsSmi, 1, 1) \ |
| 104 | F(IsNonNegativeSmi, 1, 1) \ |
| 105 | F(IsArray, 1, 1) \ |
| 106 | F(IsRegExp, 1, 1) \ |
| 107 | F(CallFunction, -1 /* receiver + n args + function */, 1) \ |
| 108 | F(IsConstructCall, 0, 1) \ |
| 109 | F(ArgumentsLength, 0, 1) \ |
| 110 | F(Arguments, 1, 1) \ |
| 111 | F(ClassOf, 1, 1) \ |
| 112 | F(ValueOf, 1, 1) \ |
| 113 | F(SetValueOf, 2, 1) \ |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 114 | F(StringCharCodeAt, 2, 1) \ |
| 115 | F(StringCharFromCode, 1, 1) \ |
| 116 | F(StringCharAt, 2, 1) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 117 | F(ObjectEquals, 2, 1) \ |
| 118 | F(Log, 3, 1) \ |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 119 | F(RandomHeapNumber, 0, 1) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 120 | F(IsObject, 1, 1) \ |
| 121 | F(IsFunction, 1, 1) \ |
| 122 | F(IsUndetectableObject, 1, 1) \ |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 123 | F(IsSpecObject, 1, 1) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 124 | F(StringAdd, 2, 1) \ |
| 125 | F(SubString, 3, 1) \ |
| 126 | F(StringCompare, 2, 1) \ |
| 127 | F(RegExpExec, 4, 1) \ |
| 128 | F(RegExpConstructResult, 3, 1) \ |
| 129 | F(GetFromCache, 2, 1) \ |
| 130 | F(NumberToString, 1, 1) \ |
| 131 | F(SwapElements, 3, 1) \ |
| 132 | F(MathPow, 2, 1) \ |
| 133 | F(MathSin, 1, 1) \ |
| 134 | F(MathCos, 1, 1) \ |
| 135 | F(MathSqrt, 1, 1) |
| 136 | |
| 137 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 138 | // Support for "structured" code comments. |
| 139 | #ifdef DEBUG |
| 140 | |
| 141 | class Comment BASE_EMBEDDED { |
| 142 | public: |
| 143 | Comment(MacroAssembler* masm, const char* msg); |
| 144 | ~Comment(); |
| 145 | |
| 146 | private: |
| 147 | MacroAssembler* masm_; |
| 148 | const char* msg_; |
| 149 | }; |
| 150 | |
| 151 | #else |
| 152 | |
| 153 | class Comment BASE_EMBEDDED { |
| 154 | public: |
| 155 | Comment(MacroAssembler*, const char*) {} |
| 156 | }; |
| 157 | |
| 158 | #endif // DEBUG |
| 159 | |
| 160 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | // Code generation can be nested. Code generation scopes form a stack |
| 162 | // of active code generators. |
| 163 | class CodeGeneratorScope BASE_EMBEDDED { |
| 164 | public: |
| 165 | explicit CodeGeneratorScope(CodeGenerator* cgen) { |
| 166 | previous_ = top_; |
| 167 | top_ = cgen; |
| 168 | } |
| 169 | |
| 170 | ~CodeGeneratorScope() { |
| 171 | top_ = previous_; |
| 172 | } |
| 173 | |
| 174 | static CodeGenerator* Current() { |
| 175 | ASSERT(top_ != NULL); |
| 176 | return top_; |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | static CodeGenerator* top_; |
| 181 | CodeGenerator* previous_; |
| 182 | }; |
| 183 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 184 | #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64 |
| 185 | |
| 186 | // State of used registers in a virtual frame. |
| 187 | class FrameRegisterState { |
| 188 | public: |
| 189 | // Captures the current state of the given frame. |
| 190 | explicit FrameRegisterState(VirtualFrame* frame); |
| 191 | |
| 192 | // Saves the state in the stack. |
| 193 | void Save(MacroAssembler* masm) const; |
| 194 | |
| 195 | // Restores the state from the stack. |
| 196 | void Restore(MacroAssembler* masm) const; |
| 197 | |
| 198 | private: |
| 199 | // Constants indicating special actions. They should not be multiples |
| 200 | // of kPointerSize so they will not collide with valid offsets from |
| 201 | // the frame pointer. |
| 202 | static const int kIgnore = -1; |
| 203 | static const int kPush = 1; |
| 204 | |
| 205 | // This flag is ored with a valid offset from the frame pointer, so |
| 206 | // it should fit in the low zero bits of a valid offset. |
| 207 | static const int kSyncedFlag = 2; |
| 208 | |
| 209 | int registers_[RegisterAllocator::kNumRegisters]; |
| 210 | }; |
| 211 | |
| 212 | #elif V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS |
| 213 | |
| 214 | |
| 215 | class FrameRegisterState { |
| 216 | public: |
| 217 | inline FrameRegisterState(VirtualFrame frame) : frame_(frame) { } |
| 218 | |
| 219 | inline const VirtualFrame* frame() const { return &frame_; } |
| 220 | |
| 221 | private: |
| 222 | VirtualFrame frame_; |
| 223 | }; |
| 224 | |
| 225 | #else |
| 226 | |
| 227 | #error Unsupported target architecture. |
| 228 | |
| 229 | #endif |
| 230 | |
| 231 | |
| 232 | // Helper interface to prepare to/restore after making runtime calls. |
| 233 | class RuntimeCallHelper { |
| 234 | public: |
| 235 | virtual ~RuntimeCallHelper() {} |
| 236 | |
| 237 | virtual void BeforeCall(MacroAssembler* masm) const = 0; |
| 238 | |
| 239 | virtual void AfterCall(MacroAssembler* masm) const = 0; |
| 240 | |
| 241 | protected: |
| 242 | RuntimeCallHelper() {} |
| 243 | |
| 244 | private: |
| 245 | DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper); |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | // RuntimeCallHelper implementation that saves/restores state of a |
| 250 | // virtual frame. |
| 251 | class VirtualFrameRuntimeCallHelper : public RuntimeCallHelper { |
| 252 | public: |
| 253 | // Does not take ownership of |frame_state|. |
| 254 | explicit VirtualFrameRuntimeCallHelper(const FrameRegisterState* frame_state) |
| 255 | : frame_state_(frame_state) {} |
| 256 | |
| 257 | virtual void BeforeCall(MacroAssembler* masm) const; |
| 258 | |
| 259 | virtual void AfterCall(MacroAssembler* masm) const; |
| 260 | |
| 261 | private: |
| 262 | const FrameRegisterState* frame_state_; |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | // RuntimeCallHelper implementation used in IC stubs: enters/leaves a |
| 267 | // newly created internal frame before/after the runtime call. |
| 268 | class ICRuntimeCallHelper : public RuntimeCallHelper { |
| 269 | public: |
| 270 | ICRuntimeCallHelper() {} |
| 271 | |
| 272 | virtual void BeforeCall(MacroAssembler* masm) const; |
| 273 | |
| 274 | virtual void AfterCall(MacroAssembler* masm) const; |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | // Trivial RuntimeCallHelper implementation. |
| 279 | class NopRuntimeCallHelper : public RuntimeCallHelper { |
| 280 | public: |
| 281 | NopRuntimeCallHelper() {} |
| 282 | |
| 283 | virtual void BeforeCall(MacroAssembler* masm) const {} |
| 284 | |
| 285 | virtual void AfterCall(MacroAssembler* masm) const {} |
| 286 | }; |
| 287 | |
| 288 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | // Deferred code objects are small pieces of code that are compiled |
| 290 | // out of line. They are used to defer the compilation of uncommon |
| 291 | // paths thereby avoiding expensive jumps around uncommon code parts. |
| 292 | class DeferredCode: public ZoneObject { |
| 293 | public: |
| 294 | DeferredCode(); |
| 295 | virtual ~DeferredCode() { } |
| 296 | |
| 297 | virtual void Generate() = 0; |
| 298 | |
| 299 | MacroAssembler* masm() { return masm_; } |
| 300 | |
| 301 | int statement_position() const { return statement_position_; } |
| 302 | int position() const { return position_; } |
| 303 | |
| 304 | Label* entry_label() { return &entry_label_; } |
| 305 | Label* exit_label() { return &exit_label_; } |
| 306 | |
| 307 | #ifdef DEBUG |
| 308 | void set_comment(const char* comment) { comment_ = comment; } |
| 309 | const char* comment() const { return comment_; } |
| 310 | #else |
| 311 | void set_comment(const char* comment) { } |
| 312 | const char* comment() const { return ""; } |
| 313 | #endif |
| 314 | |
| 315 | inline void Jump(); |
| 316 | inline void Branch(Condition cc); |
| 317 | void BindExit() { masm_->bind(&exit_label_); } |
| 318 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 319 | const FrameRegisterState* frame_state() const { return &frame_state_; } |
| 320 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | void SaveRegisters(); |
| 322 | void RestoreRegisters(); |
| 323 | |
| 324 | protected: |
| 325 | MacroAssembler* masm_; |
| 326 | |
| 327 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | int statement_position_; |
| 329 | int position_; |
| 330 | |
| 331 | Label entry_label_; |
| 332 | Label exit_label_; |
| 333 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 334 | FrameRegisterState frame_state_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef DEBUG |
| 337 | const char* comment_; |
| 338 | #endif |
| 339 | DISALLOW_COPY_AND_ASSIGN(DeferredCode); |
| 340 | }; |
| 341 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | class StackCheckStub : public CodeStub { |
| 343 | public: |
| 344 | StackCheckStub() { } |
| 345 | |
| 346 | void Generate(MacroAssembler* masm); |
| 347 | |
| 348 | private: |
| 349 | |
| 350 | const char* GetName() { return "StackCheckStub"; } |
| 351 | |
| 352 | Major MajorKey() { return StackCheck; } |
| 353 | int MinorKey() { return 0; } |
| 354 | }; |
| 355 | |
| 356 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 357 | class FastNewClosureStub : public CodeStub { |
| 358 | public: |
| 359 | void Generate(MacroAssembler* masm); |
| 360 | |
| 361 | private: |
| 362 | const char* GetName() { return "FastNewClosureStub"; } |
| 363 | Major MajorKey() { return FastNewClosure; } |
| 364 | int MinorKey() { return 0; } |
| 365 | }; |
| 366 | |
| 367 | |
| 368 | class FastNewContextStub : public CodeStub { |
| 369 | public: |
| 370 | static const int kMaximumSlots = 64; |
| 371 | |
| 372 | explicit FastNewContextStub(int slots) : slots_(slots) { |
| 373 | ASSERT(slots_ > 0 && slots <= kMaximumSlots); |
| 374 | } |
| 375 | |
| 376 | void Generate(MacroAssembler* masm); |
| 377 | |
| 378 | private: |
| 379 | int slots_; |
| 380 | |
| 381 | const char* GetName() { return "FastNewContextStub"; } |
| 382 | Major MajorKey() { return FastNewContext; } |
| 383 | int MinorKey() { return slots_; } |
| 384 | }; |
| 385 | |
| 386 | |
| 387 | class FastCloneShallowArrayStub : public CodeStub { |
| 388 | public: |
| 389 | static const int kMaximumLength = 8; |
| 390 | |
| 391 | explicit FastCloneShallowArrayStub(int length) : length_(length) { |
| 392 | ASSERT(length >= 0 && length <= kMaximumLength); |
| 393 | } |
| 394 | |
| 395 | void Generate(MacroAssembler* masm); |
| 396 | |
| 397 | private: |
| 398 | int length_; |
| 399 | |
| 400 | const char* GetName() { return "FastCloneShallowArrayStub"; } |
| 401 | Major MajorKey() { return FastCloneShallowArray; } |
| 402 | int MinorKey() { return length_; } |
| 403 | }; |
| 404 | |
| 405 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | class InstanceofStub: public CodeStub { |
| 407 | public: |
| 408 | InstanceofStub() { } |
| 409 | |
| 410 | void Generate(MacroAssembler* masm); |
| 411 | |
| 412 | private: |
| 413 | Major MajorKey() { return Instanceof; } |
| 414 | int MinorKey() { return 0; } |
| 415 | }; |
| 416 | |
| 417 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 418 | enum NegativeZeroHandling { |
| 419 | kStrictNegativeZero, |
| 420 | kIgnoreNegativeZero |
| 421 | }; |
| 422 | |
| 423 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 424 | class GenericUnaryOpStub : public CodeStub { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 425 | public: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 426 | GenericUnaryOpStub(Token::Value op, |
| 427 | UnaryOverwriteMode overwrite, |
| 428 | NegativeZeroHandling negative_zero = kStrictNegativeZero) |
| 429 | : op_(op), overwrite_(overwrite), negative_zero_(negative_zero) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | |
| 431 | private: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 432 | Token::Value op_; |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 433 | UnaryOverwriteMode overwrite_; |
| 434 | NegativeZeroHandling negative_zero_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 435 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 436 | class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {}; |
| 437 | class NegativeZeroField: public BitField<NegativeZeroHandling, 1, 1> {}; |
| 438 | class OpField: public BitField<Token::Value, 2, kMinorBits - 2> {}; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 439 | |
| 440 | Major MajorKey() { return GenericUnaryOp; } |
| 441 | int MinorKey() { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 442 | return OpField::encode(op_) | |
| 443 | OverwriteField::encode(overwrite_) | |
| 444 | NegativeZeroField::encode(negative_zero_); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | void Generate(MacroAssembler* masm); |
| 448 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 449 | const char* GetName(); |
| 450 | }; |
| 451 | |
| 452 | |
| 453 | enum NaNInformation { |
| 454 | kBothCouldBeNaN, |
| 455 | kCantBothBeNaN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | |
| 459 | class CompareStub: public CodeStub { |
| 460 | public: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 461 | CompareStub(Condition cc, |
| 462 | bool strict, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 463 | NaNInformation nan_info = kBothCouldBeNaN, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 464 | bool include_number_compare = true, |
| 465 | Register lhs = no_reg, |
| 466 | Register rhs = no_reg) : |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 467 | cc_(cc), |
| 468 | strict_(strict), |
| 469 | never_nan_nan_(nan_info == kCantBothBeNaN), |
| 470 | include_number_compare_(include_number_compare), |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 471 | lhs_(lhs), |
| 472 | rhs_(rhs), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 473 | name_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 474 | |
| 475 | void Generate(MacroAssembler* masm); |
| 476 | |
| 477 | private: |
| 478 | Condition cc_; |
| 479 | bool strict_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 480 | // Only used for 'equal' comparisons. Tells the stub that we already know |
| 481 | // that at least one side of the comparison is not NaN. This allows the |
| 482 | // stub to use object identity in the positive case. We ignore it when |
| 483 | // generating the minor key for other comparisons to avoid creating more |
| 484 | // stubs. |
| 485 | bool never_nan_nan_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 486 | // Do generate the number comparison code in the stub. Stubs without number |
| 487 | // comparison code is used when the number comparison has been inlined, and |
| 488 | // the stub will be called if one of the operands is not a number. |
| 489 | bool include_number_compare_; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 490 | // Register holding the left hand side of the comparison if the stub gives |
| 491 | // a choice, no_reg otherwise. |
| 492 | Register lhs_; |
| 493 | // Register holding the right hand side of the comparison if the stub gives |
| 494 | // a choice, no_reg otherwise. |
| 495 | Register rhs_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 496 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 497 | // Encoding of the minor key CCCCCCCCCCCCRCNS. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 498 | class StrictField: public BitField<bool, 0, 1> {}; |
| 499 | class NeverNanNanField: public BitField<bool, 1, 1> {}; |
| 500 | class IncludeNumberCompareField: public BitField<bool, 2, 1> {}; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 501 | class RegisterField: public BitField<bool, 3, 1> {}; |
| 502 | class ConditionField: public BitField<int, 4, 12> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 503 | |
| 504 | Major MajorKey() { return Compare; } |
| 505 | |
| 506 | int MinorKey(); |
| 507 | |
| 508 | // Branch to the label if the given object isn't a symbol. |
| 509 | void BranchIfNonSymbol(MacroAssembler* masm, |
| 510 | Label* label, |
| 511 | Register object, |
| 512 | Register scratch); |
| 513 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 514 | // Unfortunately you have to run without snapshots to see most of these |
| 515 | // names in the profile since most compare stubs end up in the snapshot. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 516 | char* name_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 517 | const char* GetName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 518 | #ifdef DEBUG |
| 519 | void Print() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 520 | PrintF("CompareStub (cc %d), (strict %s), " |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 521 | "(never_nan_nan %s), (number_compare %s) ", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | static_cast<int>(cc_), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 523 | strict_ ? "true" : "false", |
| 524 | never_nan_nan_ ? "true" : "false", |
| 525 | include_number_compare_ ? "included" : "not included"); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 526 | |
| 527 | if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) { |
| 528 | PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code()); |
| 529 | } else { |
| 530 | PrintF("\n"); |
| 531 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
| 534 | }; |
| 535 | |
| 536 | |
| 537 | class CEntryStub : public CodeStub { |
| 538 | public: |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 539 | explicit CEntryStub(int result_size, |
| 540 | ExitFrame::Mode mode = ExitFrame::MODE_NORMAL) |
| 541 | : result_size_(result_size), mode_(mode) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 542 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 543 | void Generate(MacroAssembler* masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 544 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 545 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 546 | void GenerateCore(MacroAssembler* masm, |
| 547 | Label* throw_normal_exception, |
| 548 | Label* throw_termination_exception, |
| 549 | Label* throw_out_of_memory_exception, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 550 | bool do_gc, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 551 | bool always_allocate_scope, |
| 552 | int alignment_skew = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 553 | void GenerateThrowTOS(MacroAssembler* masm); |
| 554 | void GenerateThrowUncatchable(MacroAssembler* masm, |
| 555 | UncatchableExceptionType type); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 556 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | // Number of pointers/values returned. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 558 | const int result_size_; |
| 559 | const ExitFrame::Mode mode_; |
| 560 | |
| 561 | // Minor key encoding |
| 562 | class ExitFrameModeBits: public BitField<ExitFrame::Mode, 0, 1> {}; |
| 563 | class IndirectResultBits: public BitField<bool, 1, 1> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | |
| 565 | Major MajorKey() { return CEntry; } |
| 566 | // Minor key must differ if different result_size_ values means different |
| 567 | // code is generated. |
| 568 | int MinorKey(); |
| 569 | |
| 570 | const char* GetName() { return "CEntryStub"; } |
| 571 | }; |
| 572 | |
| 573 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 574 | class ApiGetterEntryStub : public CodeStub { |
| 575 | public: |
| 576 | ApiGetterEntryStub(Handle<AccessorInfo> info, |
| 577 | ApiFunction* fun) |
| 578 | : info_(info), |
| 579 | fun_(fun) { } |
| 580 | void Generate(MacroAssembler* masm); |
| 581 | virtual bool has_custom_cache() { return true; } |
| 582 | virtual bool GetCustomCache(Code** code_out); |
| 583 | virtual void SetCustomCache(Code* value); |
| 584 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 585 | static const int kStackSpace = 5; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 586 | static const int kArgc = 4; |
| 587 | private: |
| 588 | Handle<AccessorInfo> info() { return info_; } |
| 589 | ApiFunction* fun() { return fun_; } |
| 590 | Major MajorKey() { return NoCache; } |
| 591 | int MinorKey() { return 0; } |
| 592 | const char* GetName() { return "ApiEntryStub"; } |
| 593 | // The accessor info associated with the function. |
| 594 | Handle<AccessorInfo> info_; |
| 595 | // The function to be called. |
| 596 | ApiFunction* fun_; |
| 597 | }; |
| 598 | |
| 599 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 600 | class JSEntryStub : public CodeStub { |
| 601 | public: |
| 602 | JSEntryStub() { } |
| 603 | |
| 604 | void Generate(MacroAssembler* masm) { GenerateBody(masm, false); } |
| 605 | |
| 606 | protected: |
| 607 | void GenerateBody(MacroAssembler* masm, bool is_construct); |
| 608 | |
| 609 | private: |
| 610 | Major MajorKey() { return JSEntry; } |
| 611 | int MinorKey() { return 0; } |
| 612 | |
| 613 | const char* GetName() { return "JSEntryStub"; } |
| 614 | }; |
| 615 | |
| 616 | |
| 617 | class JSConstructEntryStub : public JSEntryStub { |
| 618 | public: |
| 619 | JSConstructEntryStub() { } |
| 620 | |
| 621 | void Generate(MacroAssembler* masm) { GenerateBody(masm, true); } |
| 622 | |
| 623 | private: |
| 624 | int MinorKey() { return 1; } |
| 625 | |
| 626 | const char* GetName() { return "JSConstructEntryStub"; } |
| 627 | }; |
| 628 | |
| 629 | |
| 630 | class ArgumentsAccessStub: public CodeStub { |
| 631 | public: |
| 632 | enum Type { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 633 | READ_ELEMENT, |
| 634 | NEW_OBJECT |
| 635 | }; |
| 636 | |
| 637 | explicit ArgumentsAccessStub(Type type) : type_(type) { } |
| 638 | |
| 639 | private: |
| 640 | Type type_; |
| 641 | |
| 642 | Major MajorKey() { return ArgumentsAccess; } |
| 643 | int MinorKey() { return type_; } |
| 644 | |
| 645 | void Generate(MacroAssembler* masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 646 | void GenerateReadElement(MacroAssembler* masm); |
| 647 | void GenerateNewObject(MacroAssembler* masm); |
| 648 | |
| 649 | const char* GetName() { return "ArgumentsAccessStub"; } |
| 650 | |
| 651 | #ifdef DEBUG |
| 652 | void Print() { |
| 653 | PrintF("ArgumentsAccessStub (type %d)\n", type_); |
| 654 | } |
| 655 | #endif |
| 656 | }; |
| 657 | |
| 658 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 659 | class RegExpExecStub: public CodeStub { |
| 660 | public: |
| 661 | RegExpExecStub() { } |
| 662 | |
| 663 | private: |
| 664 | Major MajorKey() { return RegExpExec; } |
| 665 | int MinorKey() { return 0; } |
| 666 | |
| 667 | void Generate(MacroAssembler* masm); |
| 668 | |
| 669 | const char* GetName() { return "RegExpExecStub"; } |
| 670 | |
| 671 | #ifdef DEBUG |
| 672 | void Print() { |
| 673 | PrintF("RegExpExecStub\n"); |
| 674 | } |
| 675 | #endif |
| 676 | }; |
| 677 | |
| 678 | |
| 679 | class CallFunctionStub: public CodeStub { |
| 680 | public: |
| 681 | CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags) |
| 682 | : argc_(argc), in_loop_(in_loop), flags_(flags) { } |
| 683 | |
| 684 | void Generate(MacroAssembler* masm); |
| 685 | |
| 686 | private: |
| 687 | int argc_; |
| 688 | InLoopFlag in_loop_; |
| 689 | CallFunctionFlags flags_; |
| 690 | |
| 691 | #ifdef DEBUG |
| 692 | void Print() { |
| 693 | PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n", |
| 694 | argc_, |
| 695 | static_cast<int>(in_loop_), |
| 696 | static_cast<int>(flags_)); |
| 697 | } |
| 698 | #endif |
| 699 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 700 | // Minor key encoding in 32 bits with Bitfield <Type, shift, size>. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 701 | class InLoopBits: public BitField<InLoopFlag, 0, 1> {}; |
| 702 | class FlagBits: public BitField<CallFunctionFlags, 1, 1> {}; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 703 | class ArgcBits: public BitField<int, 2, 32 - 2> {}; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 704 | |
| 705 | Major MajorKey() { return CallFunction; } |
| 706 | int MinorKey() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 707 | // Encode the parameters in a unique 32 bit value. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 708 | return InLoopBits::encode(in_loop_) |
| 709 | | FlagBits::encode(flags_) |
| 710 | | ArgcBits::encode(argc_); |
| 711 | } |
| 712 | |
| 713 | InLoopFlag InLoop() { return in_loop_; } |
| 714 | bool ReceiverMightBeValue() { |
| 715 | return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0; |
| 716 | } |
| 717 | |
| 718 | public: |
| 719 | static int ExtractArgcFromMinorKey(int minor_key) { |
| 720 | return ArgcBits::decode(minor_key); |
| 721 | } |
| 722 | }; |
| 723 | |
| 724 | |
| 725 | class ToBooleanStub: public CodeStub { |
| 726 | public: |
| 727 | ToBooleanStub() { } |
| 728 | |
| 729 | void Generate(MacroAssembler* masm); |
| 730 | |
| 731 | private: |
| 732 | Major MajorKey() { return ToBoolean; } |
| 733 | int MinorKey() { return 0; } |
| 734 | }; |
| 735 | |
| 736 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 737 | enum StringIndexFlags { |
| 738 | // Accepts smis or heap numbers. |
| 739 | STRING_INDEX_IS_NUMBER, |
| 740 | |
| 741 | // Accepts smis or heap numbers that are valid array indices |
| 742 | // (ECMA-262 15.4). Invalid indices are reported as being out of |
| 743 | // range. |
| 744 | STRING_INDEX_IS_ARRAY_INDEX |
| 745 | }; |
| 746 | |
| 747 | |
| 748 | // Generates code implementing String.prototype.charCodeAt. |
| 749 | // |
| 750 | // Only supports the case when the receiver is a string and the index |
| 751 | // is a number (smi or heap number) that is a valid index into the |
| 752 | // string. Additional index constraints are specified by the |
| 753 | // flags. Otherwise, bails out to the provided labels. |
| 754 | // |
| 755 | // Register usage: |object| may be changed to another string in a way |
| 756 | // that doesn't affect charCodeAt/charAt semantics, |index| is |
| 757 | // preserved, |scratch| and |result| are clobbered. |
| 758 | class StringCharCodeAtGenerator { |
| 759 | public: |
| 760 | StringCharCodeAtGenerator(Register object, |
| 761 | Register index, |
| 762 | Register scratch, |
| 763 | Register result, |
| 764 | Label* receiver_not_string, |
| 765 | Label* index_not_number, |
| 766 | Label* index_out_of_range, |
| 767 | StringIndexFlags index_flags) |
| 768 | : object_(object), |
| 769 | index_(index), |
| 770 | scratch_(scratch), |
| 771 | result_(result), |
| 772 | receiver_not_string_(receiver_not_string), |
| 773 | index_not_number_(index_not_number), |
| 774 | index_out_of_range_(index_out_of_range), |
| 775 | index_flags_(index_flags) { |
| 776 | ASSERT(!scratch_.is(object_)); |
| 777 | ASSERT(!scratch_.is(index_)); |
| 778 | ASSERT(!scratch_.is(result_)); |
| 779 | ASSERT(!result_.is(object_)); |
| 780 | ASSERT(!result_.is(index_)); |
| 781 | } |
| 782 | |
| 783 | // Generates the fast case code. On the fallthrough path |result| |
| 784 | // register contains the result. |
| 785 | void GenerateFast(MacroAssembler* masm); |
| 786 | |
| 787 | // Generates the slow case code. Must not be naturally |
| 788 | // reachable. Expected to be put after a ret instruction (e.g., in |
| 789 | // deferred code). Always jumps back to the fast case. |
| 790 | void GenerateSlow(MacroAssembler* masm, |
| 791 | const RuntimeCallHelper& call_helper); |
| 792 | |
| 793 | private: |
| 794 | Register object_; |
| 795 | Register index_; |
| 796 | Register scratch_; |
| 797 | Register result_; |
| 798 | |
| 799 | Label* receiver_not_string_; |
| 800 | Label* index_not_number_; |
| 801 | Label* index_out_of_range_; |
| 802 | |
| 803 | StringIndexFlags index_flags_; |
| 804 | |
| 805 | Label call_runtime_; |
| 806 | Label index_not_smi_; |
| 807 | Label got_smi_index_; |
| 808 | Label exit_; |
| 809 | |
| 810 | DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator); |
| 811 | }; |
| 812 | |
| 813 | |
| 814 | // Generates code for creating a one-char string from a char code. |
| 815 | class StringCharFromCodeGenerator { |
| 816 | public: |
| 817 | StringCharFromCodeGenerator(Register code, |
| 818 | Register result) |
| 819 | : code_(code), |
| 820 | result_(result) { |
| 821 | ASSERT(!code_.is(result_)); |
| 822 | } |
| 823 | |
| 824 | // Generates the fast case code. On the fallthrough path |result| |
| 825 | // register contains the result. |
| 826 | void GenerateFast(MacroAssembler* masm); |
| 827 | |
| 828 | // Generates the slow case code. Must not be naturally |
| 829 | // reachable. Expected to be put after a ret instruction (e.g., in |
| 830 | // deferred code). Always jumps back to the fast case. |
| 831 | void GenerateSlow(MacroAssembler* masm, |
| 832 | const RuntimeCallHelper& call_helper); |
| 833 | |
| 834 | private: |
| 835 | Register code_; |
| 836 | Register result_; |
| 837 | |
| 838 | Label slow_case_; |
| 839 | Label exit_; |
| 840 | |
| 841 | DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator); |
| 842 | }; |
| 843 | |
| 844 | |
| 845 | // Generates code implementing String.prototype.charAt. |
| 846 | // |
| 847 | // Only supports the case when the receiver is a string and the index |
| 848 | // is a number (smi or heap number) that is a valid index into the |
| 849 | // string. Additional index constraints are specified by the |
| 850 | // flags. Otherwise, bails out to the provided labels. |
| 851 | // |
| 852 | // Register usage: |object| may be changed to another string in a way |
| 853 | // that doesn't affect charCodeAt/charAt semantics, |index| is |
| 854 | // preserved, |scratch1|, |scratch2|, and |result| are clobbered. |
| 855 | class StringCharAtGenerator { |
| 856 | public: |
| 857 | StringCharAtGenerator(Register object, |
| 858 | Register index, |
| 859 | Register scratch1, |
| 860 | Register scratch2, |
| 861 | Register result, |
| 862 | Label* receiver_not_string, |
| 863 | Label* index_not_number, |
| 864 | Label* index_out_of_range, |
| 865 | StringIndexFlags index_flags) |
| 866 | : char_code_at_generator_(object, |
| 867 | index, |
| 868 | scratch1, |
| 869 | scratch2, |
| 870 | receiver_not_string, |
| 871 | index_not_number, |
| 872 | index_out_of_range, |
| 873 | index_flags), |
| 874 | char_from_code_generator_(scratch2, result) {} |
| 875 | |
| 876 | // Generates the fast case code. On the fallthrough path |result| |
| 877 | // register contains the result. |
| 878 | void GenerateFast(MacroAssembler* masm); |
| 879 | |
| 880 | // Generates the slow case code. Must not be naturally |
| 881 | // reachable. Expected to be put after a ret instruction (e.g., in |
| 882 | // deferred code). Always jumps back to the fast case. |
| 883 | void GenerateSlow(MacroAssembler* masm, |
| 884 | const RuntimeCallHelper& call_helper); |
| 885 | |
| 886 | private: |
| 887 | StringCharCodeAtGenerator char_code_at_generator_; |
| 888 | StringCharFromCodeGenerator char_from_code_generator_; |
| 889 | |
| 890 | DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator); |
| 891 | }; |
| 892 | |
| 893 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | } // namespace internal |
| 895 | } // namespace v8 |
| 896 | |
| 897 | #endif // V8_CODEGEN_H_ |