blob: d408034f29ab31cb878239ce42a59d998a227de6 [file] [log] [blame]
Ben Murdoch086aeea2011-05-13 15:57:08 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Block6ded16b2010-05-10 14:33:55 +010031#include "globals.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
Steve Blockd0582a62009-12-15 09:54:21 +000036// List of code stubs used on all platforms. The order in this list is important
Ben Murdoch086aeea2011-05-13 15:57:08 +010037// as only the stubs up to and including Instanceof allows nested stub calls.
Steve Blockd0582a62009-12-15 09:54:21 +000038#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
39 V(CallFunction) \
40 V(GenericBinaryOp) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010041 V(TypeRecordingBinaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000042 V(StringAdd) \
Leon Clarkee46be812010-01-19 14:06:41 +000043 V(SubString) \
44 V(StringCompare) \
Steve Blockd0582a62009-12-15 09:54:21 +000045 V(SmiOp) \
46 V(Compare) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010047 V(CompareIC) \
48 V(MathPow) \
49 V(TranscendentalCache) \
Ben Murdoch086aeea2011-05-13 15:57:08 +010050 V(Instanceof) \
Steve Blockd0582a62009-12-15 09:54:21 +000051 V(ConvertToDouble) \
52 V(WriteInt32ToHeapNumber) \
Steve Block8defd9f2010-07-08 12:39:36 +010053 V(IntegerMod) \
Steve Blockd0582a62009-12-15 09:54:21 +000054 V(StackCheck) \
Leon Clarkee46be812010-01-19 14:06:41 +000055 V(FastNewClosure) \
56 V(FastNewContext) \
57 V(FastCloneShallowArray) \
58 V(GenericUnaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000059 V(RevertToNumber) \
60 V(ToBoolean) \
Steve Block1e0659c2011-05-24 12:43:12 +010061 V(ToNumber) \
Steve Blockd0582a62009-12-15 09:54:21 +000062 V(CounterOp) \
63 V(ArgumentsAccess) \
Leon Clarkee46be812010-01-19 14:06:41 +000064 V(RegExpExec) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010065 V(RegExpConstructResult) \
Andrei Popescu402d9372010-02-26 13:31:12 +000066 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000067 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000068 V(JSEntry) \
69 V(DebuggerStatement)
Steve Blockd0582a62009-12-15 09:54:21 +000070
71// List of code stubs only used on ARM platforms.
72#ifdef V8_TARGET_ARCH_ARM
73#define CODE_STUB_LIST_ARM(V) \
74 V(GetProperty) \
75 V(SetProperty) \
76 V(InvokeBuiltin) \
Steve Block1e0659c2011-05-24 12:43:12 +010077 V(RegExpCEntry) \
78 V(DirectCEntry)
Steve Blockd0582a62009-12-15 09:54:21 +000079#else
80#define CODE_STUB_LIST_ARM(V)
81#endif
82
Steve Block44f0eee2011-05-26 01:26:41 +010083// List of code stubs only used on MIPS platforms.
84#ifdef V8_TARGET_ARCH_MIPS
85#define CODE_STUB_LIST_MIPS(V) \
86 V(RegExpCEntry)
87#else
88#define CODE_STUB_LIST_MIPS(V)
89#endif
90
Steve Blockd0582a62009-12-15 09:54:21 +000091// Combined list of code stubs.
92#define CODE_STUB_LIST(V) \
93 CODE_STUB_LIST_ALL_PLATFORMS(V) \
Steve Block44f0eee2011-05-26 01:26:41 +010094 CODE_STUB_LIST_ARM(V) \
95 CODE_STUB_LIST_MIPS(V)
Steve Blocka7e24c12009-10-30 11:49:00 +000096
Kristian Monsen80d68ea2010-09-08 11:05:35 +010097// Mode to overwrite BinaryExpression values.
98enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
99enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE };
100
101
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// Stub is base classes of all stubs.
103class CodeStub BASE_EMBEDDED {
104 public:
105 enum Major {
Steve Blockd0582a62009-12-15 09:54:21 +0000106#define DEF_ENUM(name) name,
107 CODE_STUB_LIST(DEF_ENUM)
108#undef DEF_ENUM
109 NoCache, // marker for stubs that do custom caching
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 NUMBER_OF_IDS
111 };
112
113 // Retrieve the code for the stub. Generate the code if needed.
114 Handle<Code> GetCode();
115
Leon Clarkee46be812010-01-19 14:06:41 +0000116 // Retrieve the code for the stub if already generated. Do not
117 // generate the code if not already generated and instead return a
118 // retry after GC Failure object.
John Reck59135872010-11-02 12:39:01 -0700119 MUST_USE_RESULT MaybeObject* TryGetCode();
Leon Clarkee46be812010-01-19 14:06:41 +0000120
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 static Major MajorKeyFromKey(uint32_t key) {
122 return static_cast<Major>(MajorKeyBits::decode(key));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100123 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 static int MinorKeyFromKey(uint32_t key) {
125 return MinorKeyBits::decode(key);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100126 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100127
128 // Gets the major key from a code object that is a code stub or binary op IC.
129 static Major GetMajorKey(Code* code_stub) {
130 return static_cast<Major>(code_stub->major_key());
131 }
132
Andrei Popescu31002712010-02-23 13:46:05 +0000133 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000134
135 virtual ~CodeStub() {}
136
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 protected:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100138 static const int kMajorBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
140
141 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000142 // Lookup the code in the (possibly custom) cache.
143 bool FindCodeInCache(Code** code_out);
144
145 // Nonvirtual wrapper around the stub-specific Generate function. Call
146 // this function to set up the macro assembler and generate the code.
147 void GenerateCode(MacroAssembler* masm);
148
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 // Generates the assembler code for the stub.
150 virtual void Generate(MacroAssembler* masm) = 0;
151
Leon Clarkee46be812010-01-19 14:06:41 +0000152 // Perform bookkeeping required after code generation when stub code is
153 // initially generated.
154 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
155
Ben Murdochb0fe1622011-05-05 13:52:32 +0100156 // Finish the code object after it has been generated.
157 virtual void FinishCode(Code* code) { }
158
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 // Returns information for computing the number key.
160 virtual Major MajorKey() = 0;
161 virtual int MinorKey() = 0;
162
163 // The CallFunctionStub needs to override this so it can encode whether a
164 // lazily generated function should be fully optimized or not.
165 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
166
Steve Block6ded16b2010-05-10 14:33:55 +0100167 // GenericBinaryOpStub needs to override this.
168 virtual int GetCodeKind();
169
170 // GenericBinaryOpStub needs to override this.
171 virtual InlineCacheState GetICState() {
172 return UNINITIALIZED;
173 }
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 // Returns a name for logging/debugging purposes.
Andrei Popescu31002712010-02-23 13:46:05 +0000176 virtual const char* GetName() { return MajorName(MajorKey(), false); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000177
Steve Block44f0eee2011-05-26 01:26:41 +0100178 // Returns whether the code generated for this stub needs to be allocated as
179 // a fixed (non-moveable) code object.
180 virtual bool NeedsImmovableCode() { return false; }
181
182 #ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 virtual void Print() { PrintF("%s\n", GetName()); }
184#endif
185
186 // Computes the key based on major and minor.
187 uint32_t GetKey() {
188 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
189 return MinorKeyBits::encode(MinorKey()) |
190 MajorKeyBits::encode(MajorKey());
191 }
192
Ben Murdoch086aeea2011-05-13 15:57:08 +0100193 bool AllowsStubCalls() { return MajorKey() <= Instanceof; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
195 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
196 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
197
198 friend class BreakPointIterator;
199};
200
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100201
202// Helper interface to prepare to/restore after making runtime calls.
203class RuntimeCallHelper {
204 public:
205 virtual ~RuntimeCallHelper() {}
206
207 virtual void BeforeCall(MacroAssembler* masm) const = 0;
208
209 virtual void AfterCall(MacroAssembler* masm) const = 0;
210
211 protected:
212 RuntimeCallHelper() {}
213
214 private:
215 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
216};
217
218} } // namespace v8::internal
219
220#if V8_TARGET_ARCH_IA32
221#include "ia32/code-stubs-ia32.h"
222#elif V8_TARGET_ARCH_X64
223#include "x64/code-stubs-x64.h"
224#elif V8_TARGET_ARCH_ARM
225#include "arm/code-stubs-arm.h"
226#elif V8_TARGET_ARCH_MIPS
227#include "mips/code-stubs-mips.h"
228#else
229#error Unsupported target architecture.
230#endif
231
232namespace v8 {
233namespace internal {
234
235
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236// RuntimeCallHelper implementation used in stubs: enters/leaves a
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100237// newly created internal frame before/after the runtime call.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100238class StubRuntimeCallHelper : public RuntimeCallHelper {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100239 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100240 StubRuntimeCallHelper() {}
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100241
242 virtual void BeforeCall(MacroAssembler* masm) const;
243
244 virtual void AfterCall(MacroAssembler* masm) const;
245};
246
247
248// Trivial RuntimeCallHelper implementation.
249class NopRuntimeCallHelper : public RuntimeCallHelper {
250 public:
251 NopRuntimeCallHelper() {}
252
253 virtual void BeforeCall(MacroAssembler* masm) const {}
254
255 virtual void AfterCall(MacroAssembler* masm) const {}
256};
257
258
259class StackCheckStub : public CodeStub {
260 public:
261 StackCheckStub() { }
262
263 void Generate(MacroAssembler* masm);
264
265 private:
266
267 const char* GetName() { return "StackCheckStub"; }
268
269 Major MajorKey() { return StackCheck; }
270 int MinorKey() { return 0; }
271};
272
273
Steve Block1e0659c2011-05-24 12:43:12 +0100274class ToNumberStub: public CodeStub {
275 public:
276 ToNumberStub() { }
277
278 void Generate(MacroAssembler* masm);
279
280 private:
281 Major MajorKey() { return ToNumber; }
282 int MinorKey() { return 0; }
283 const char* GetName() { return "ToNumberStub"; }
284};
285
286
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100287class FastNewClosureStub : public CodeStub {
288 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100289 explicit FastNewClosureStub(StrictModeFlag strict_mode)
290 : strict_mode_(strict_mode) { }
291
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100292 void Generate(MacroAssembler* masm);
293
294 private:
295 const char* GetName() { return "FastNewClosureStub"; }
296 Major MajorKey() { return FastNewClosure; }
Steve Block44f0eee2011-05-26 01:26:41 +0100297 int MinorKey() { return strict_mode_; }
298
299 StrictModeFlag strict_mode_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100300};
301
302
303class FastNewContextStub : public CodeStub {
304 public:
305 static const int kMaximumSlots = 64;
306
307 explicit FastNewContextStub(int slots) : slots_(slots) {
308 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
309 }
310
311 void Generate(MacroAssembler* masm);
312
313 private:
314 int slots_;
315
316 const char* GetName() { return "FastNewContextStub"; }
317 Major MajorKey() { return FastNewContext; }
318 int MinorKey() { return slots_; }
319};
320
321
322class FastCloneShallowArrayStub : public CodeStub {
323 public:
324 // Maximum length of copied elements array.
325 static const int kMaximumClonedLength = 8;
326
327 enum Mode {
328 CLONE_ELEMENTS,
329 COPY_ON_WRITE_ELEMENTS
330 };
331
332 FastCloneShallowArrayStub(Mode mode, int length)
333 : mode_(mode),
334 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
335 ASSERT(length_ >= 0);
336 ASSERT(length_ <= kMaximumClonedLength);
337 }
338
339 void Generate(MacroAssembler* masm);
340
341 private:
342 Mode mode_;
343 int length_;
344
345 const char* GetName() { return "FastCloneShallowArrayStub"; }
346 Major MajorKey() { return FastCloneShallowArray; }
347 int MinorKey() {
348 ASSERT(mode_ == 0 || mode_ == 1);
349 return (length_ << 1) | mode_;
350 }
351};
352
353
354class InstanceofStub: public CodeStub {
355 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100356 enum Flags {
357 kNoFlags = 0,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100358 kArgsInRegisters = 1 << 0,
359 kCallSiteInlineCheck = 1 << 1,
360 kReturnTrueFalseObject = 1 << 2
Ben Murdochb0fe1622011-05-05 13:52:32 +0100361 };
362
Ben Murdoch086aeea2011-05-13 15:57:08 +0100363 explicit InstanceofStub(Flags flags) : flags_(flags), name_(NULL) { }
364
365 static Register left();
366 static Register right();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100367
368 void Generate(MacroAssembler* masm);
369
370 private:
371 Major MajorKey() { return Instanceof; }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100372 int MinorKey() { return static_cast<int>(flags_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100373
Ben Murdoch086aeea2011-05-13 15:57:08 +0100374 bool HasArgsInRegisters() const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100375 return (flags_ & kArgsInRegisters) != 0;
376 }
377
Ben Murdoch086aeea2011-05-13 15:57:08 +0100378 bool HasCallSiteInlineCheck() const {
379 return (flags_ & kCallSiteInlineCheck) != 0;
380 }
381
382 bool ReturnTrueFalseObject() const {
383 return (flags_ & kReturnTrueFalseObject) != 0;
384 }
385
386 const char* GetName();
387
Ben Murdochb0fe1622011-05-05 13:52:32 +0100388 Flags flags_;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100389 char* name_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100390};
391
392
393enum NegativeZeroHandling {
394 kStrictNegativeZero,
395 kIgnoreNegativeZero
396};
397
398
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100399enum UnaryOpFlags {
400 NO_UNARY_FLAGS = 0,
401 NO_UNARY_SMI_CODE_IN_STUB = 1 << 0
402};
403
404
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100405class GenericUnaryOpStub : public CodeStub {
406 public:
407 GenericUnaryOpStub(Token::Value op,
408 UnaryOverwriteMode overwrite,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100409 UnaryOpFlags flags,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100410 NegativeZeroHandling negative_zero = kStrictNegativeZero)
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100411 : op_(op),
412 overwrite_(overwrite),
413 include_smi_code_((flags & NO_UNARY_SMI_CODE_IN_STUB) == 0),
414 negative_zero_(negative_zero) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100415
416 private:
417 Token::Value op_;
418 UnaryOverwriteMode overwrite_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100419 bool include_smi_code_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100420 NegativeZeroHandling negative_zero_;
421
422 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {};
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100423 class IncludeSmiCodeField: public BitField<bool, 1, 1> {};
424 class NegativeZeroField: public BitField<NegativeZeroHandling, 2, 1> {};
425 class OpField: public BitField<Token::Value, 3, kMinorBits - 3> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100426
427 Major MajorKey() { return GenericUnaryOp; }
428 int MinorKey() {
429 return OpField::encode(op_) |
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100430 OverwriteField::encode(overwrite_) |
431 IncludeSmiCodeField::encode(include_smi_code_) |
432 NegativeZeroField::encode(negative_zero_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100433 }
434
435 void Generate(MacroAssembler* masm);
436
437 const char* GetName();
438};
439
440
Ben Murdochb0fe1622011-05-05 13:52:32 +0100441class MathPowStub: public CodeStub {
442 public:
443 MathPowStub() {}
444 virtual void Generate(MacroAssembler* masm);
445
446 private:
447 virtual CodeStub::Major MajorKey() { return MathPow; }
448 virtual int MinorKey() { return 0; }
449
450 const char* GetName() { return "MathPowStub"; }
451};
452
453
Ben Murdochb0fe1622011-05-05 13:52:32 +0100454class ICCompareStub: public CodeStub {
455 public:
456 ICCompareStub(Token::Value op, CompareIC::State state)
457 : op_(op), state_(state) {
458 ASSERT(Token::IsCompareOp(op));
459 }
460
461 virtual void Generate(MacroAssembler* masm);
462
463 private:
464 class OpField: public BitField<int, 0, 3> { };
465 class StateField: public BitField<int, 3, 5> { };
466
467 virtual void FinishCode(Code* code) { code->set_compare_state(state_); }
468
469 virtual CodeStub::Major MajorKey() { return CompareIC; }
470 virtual int MinorKey();
471
472 virtual int GetCodeKind() { return Code::COMPARE_IC; }
473
474 void GenerateSmis(MacroAssembler* masm);
475 void GenerateHeapNumbers(MacroAssembler* masm);
476 void GenerateObjects(MacroAssembler* masm);
477 void GenerateMiss(MacroAssembler* masm);
478
479 bool strict() const { return op_ == Token::EQ_STRICT; }
480 Condition GetCondition() const { return CompareIC::ComputeCondition(op_); }
481
482 Token::Value op_;
483 CompareIC::State state_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100484};
485
486
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100487// Flags that control the compare stub code generation.
488enum CompareFlags {
489 NO_COMPARE_FLAGS = 0,
490 NO_SMI_COMPARE_IN_STUB = 1 << 0,
491 NO_NUMBER_COMPARE_IN_STUB = 1 << 1,
492 CANT_BOTH_BE_NAN = 1 << 2
493};
494
495
Ben Murdochb0fe1622011-05-05 13:52:32 +0100496enum NaNInformation {
497 kBothCouldBeNaN,
498 kCantBothBeNaN
499};
500
501
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100502class CompareStub: public CodeStub {
503 public:
504 CompareStub(Condition cc,
505 bool strict,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100506 CompareFlags flags,
507 Register lhs,
508 Register rhs) :
Ben Murdochb0fe1622011-05-05 13:52:32 +0100509 cc_(cc),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100510 strict_(strict),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100511 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
512 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
513 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100514 lhs_(lhs),
515 rhs_(rhs),
516 name_(NULL) { }
517
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100518 CompareStub(Condition cc,
519 bool strict,
520 CompareFlags flags) :
521 cc_(cc),
522 strict_(strict),
523 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
524 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
525 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
526 lhs_(no_reg),
527 rhs_(no_reg),
528 name_(NULL) { }
529
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100530 void Generate(MacroAssembler* masm);
531
532 private:
533 Condition cc_;
534 bool strict_;
535 // Only used for 'equal' comparisons. Tells the stub that we already know
536 // that at least one side of the comparison is not NaN. This allows the
537 // stub to use object identity in the positive case. We ignore it when
538 // generating the minor key for other comparisons to avoid creating more
539 // stubs.
540 bool never_nan_nan_;
541 // Do generate the number comparison code in the stub. Stubs without number
542 // comparison code is used when the number comparison has been inlined, and
543 // the stub will be called if one of the operands is not a number.
544 bool include_number_compare_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100545
546 // Generate the comparison code for two smi operands in the stub.
547 bool include_smi_compare_;
548
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100549 // Register holding the left hand side of the comparison if the stub gives
550 // a choice, no_reg otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100551
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100552 Register lhs_;
553 // Register holding the right hand side of the comparison if the stub gives
554 // a choice, no_reg otherwise.
555 Register rhs_;
556
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100557 // Encoding of the minor key in 16 bits.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100558 class StrictField: public BitField<bool, 0, 1> {};
559 class NeverNanNanField: public BitField<bool, 1, 1> {};
560 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100561 class IncludeSmiCompareField: public BitField<bool, 3, 1> {};
562 class RegisterField: public BitField<bool, 4, 1> {};
563 class ConditionField: public BitField<int, 5, 11> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100564
565 Major MajorKey() { return Compare; }
566
567 int MinorKey();
568
Ben Murdochb0fe1622011-05-05 13:52:32 +0100569 virtual int GetCodeKind() { return Code::COMPARE_IC; }
570 virtual void FinishCode(Code* code) {
571 code->set_compare_state(CompareIC::GENERIC);
572 }
573
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100574 // Branch to the label if the given object isn't a symbol.
575 void BranchIfNonSymbol(MacroAssembler* masm,
576 Label* label,
577 Register object,
578 Register scratch);
579
580 // Unfortunately you have to run without snapshots to see most of these
581 // names in the profile since most compare stubs end up in the snapshot.
582 char* name_;
583 const char* GetName();
584#ifdef DEBUG
585 void Print() {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100586 PrintF("CompareStub (minor %d) (cc %d), (strict %s), "
587 "(never_nan_nan %s), (smi_compare %s) (number_compare %s) ",
588 MinorKey(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100589 static_cast<int>(cc_),
590 strict_ ? "true" : "false",
591 never_nan_nan_ ? "true" : "false",
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100592 include_smi_compare_ ? "inluded" : "not included",
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100593 include_number_compare_ ? "included" : "not included");
594
595 if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) {
596 PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code());
597 } else {
598 PrintF("\n");
599 }
600 }
601#endif
602};
603
604
605class CEntryStub : public CodeStub {
606 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100607 explicit CEntryStub(int result_size)
608 : result_size_(result_size), save_doubles_(false) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100609
610 void Generate(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100611 void SaveDoubles() { save_doubles_ = true; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100612
613 private:
614 void GenerateCore(MacroAssembler* masm,
615 Label* throw_normal_exception,
616 Label* throw_termination_exception,
617 Label* throw_out_of_memory_exception,
618 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +0100619 bool always_allocate_scope);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100620 void GenerateThrowTOS(MacroAssembler* masm);
621 void GenerateThrowUncatchable(MacroAssembler* masm,
622 UncatchableExceptionType type);
623
624 // Number of pointers/values returned.
625 const int result_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100626 bool save_doubles_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100627
628 Major MajorKey() { return CEntry; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100629 int MinorKey();
630
Steve Block44f0eee2011-05-26 01:26:41 +0100631 bool NeedsImmovableCode();
632
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100633 const char* GetName() { return "CEntryStub"; }
634};
635
636
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100637class JSEntryStub : public CodeStub {
638 public:
639 JSEntryStub() { }
640
641 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
642
643 protected:
644 void GenerateBody(MacroAssembler* masm, bool is_construct);
645
646 private:
647 Major MajorKey() { return JSEntry; }
648 int MinorKey() { return 0; }
649
650 const char* GetName() { return "JSEntryStub"; }
651};
652
653
654class JSConstructEntryStub : public JSEntryStub {
655 public:
656 JSConstructEntryStub() { }
657
658 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
659
660 private:
661 int MinorKey() { return 1; }
662
663 const char* GetName() { return "JSConstructEntryStub"; }
664};
665
666
667class ArgumentsAccessStub: public CodeStub {
668 public:
669 enum Type {
670 READ_ELEMENT,
Steve Block44f0eee2011-05-26 01:26:41 +0100671 NEW_NON_STRICT,
672 NEW_STRICT
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100673 };
674
675 explicit ArgumentsAccessStub(Type type) : type_(type) { }
676
677 private:
678 Type type_;
679
680 Major MajorKey() { return ArgumentsAccess; }
681 int MinorKey() { return type_; }
682
683 void Generate(MacroAssembler* masm);
684 void GenerateReadElement(MacroAssembler* masm);
685 void GenerateNewObject(MacroAssembler* masm);
686
Steve Block44f0eee2011-05-26 01:26:41 +0100687 int GetArgumentsBoilerplateIndex() const {
688 return (type_ == NEW_STRICT)
689 ? Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX
690 : Context::ARGUMENTS_BOILERPLATE_INDEX;
691 }
692
693 int GetArgumentsObjectSize() const {
694 if (type_ == NEW_STRICT)
695 return Heap::kArgumentsObjectSizeStrict;
696 else
697 return Heap::kArgumentsObjectSize;
698 }
699
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100700 const char* GetName() { return "ArgumentsAccessStub"; }
701
702#ifdef DEBUG
703 void Print() {
704 PrintF("ArgumentsAccessStub (type %d)\n", type_);
705 }
706#endif
707};
708
709
710class RegExpExecStub: public CodeStub {
711 public:
712 RegExpExecStub() { }
713
714 private:
715 Major MajorKey() { return RegExpExec; }
716 int MinorKey() { return 0; }
717
718 void Generate(MacroAssembler* masm);
719
720 const char* GetName() { return "RegExpExecStub"; }
721
722#ifdef DEBUG
723 void Print() {
724 PrintF("RegExpExecStub\n");
725 }
726#endif
727};
728
729
Ben Murdochb0fe1622011-05-05 13:52:32 +0100730class RegExpConstructResultStub: public CodeStub {
731 public:
732 RegExpConstructResultStub() { }
733
734 private:
735 Major MajorKey() { return RegExpConstructResult; }
736 int MinorKey() { return 0; }
737
738 void Generate(MacroAssembler* masm);
739
740 const char* GetName() { return "RegExpConstructResultStub"; }
741
742#ifdef DEBUG
743 void Print() {
744 PrintF("RegExpConstructResultStub\n");
745 }
746#endif
747};
748
749
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100750class CallFunctionStub: public CodeStub {
751 public:
752 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
753 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
754
755 void Generate(MacroAssembler* masm);
756
Ben Murdoch086aeea2011-05-13 15:57:08 +0100757 static int ExtractArgcFromMinorKey(int minor_key) {
758 return ArgcBits::decode(minor_key);
759 }
760
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100761 private:
762 int argc_;
763 InLoopFlag in_loop_;
764 CallFunctionFlags flags_;
765
766#ifdef DEBUG
767 void Print() {
768 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
769 argc_,
770 static_cast<int>(in_loop_),
771 static_cast<int>(flags_));
772 }
773#endif
774
775 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
776 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
777 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
778 class ArgcBits: public BitField<int, 2, 32 - 2> {};
779
780 Major MajorKey() { return CallFunction; }
781 int MinorKey() {
782 // Encode the parameters in a unique 32 bit value.
783 return InLoopBits::encode(in_loop_)
784 | FlagBits::encode(flags_)
785 | ArgcBits::encode(argc_);
786 }
787
788 InLoopFlag InLoop() { return in_loop_; }
789 bool ReceiverMightBeValue() {
790 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
791 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100792};
793
794
795enum StringIndexFlags {
796 // Accepts smis or heap numbers.
797 STRING_INDEX_IS_NUMBER,
798
799 // Accepts smis or heap numbers that are valid array indices
800 // (ECMA-262 15.4). Invalid indices are reported as being out of
801 // range.
802 STRING_INDEX_IS_ARRAY_INDEX
803};
804
805
806// Generates code implementing String.prototype.charCodeAt.
807//
808// Only supports the case when the receiver is a string and the index
809// is a number (smi or heap number) that is a valid index into the
810// string. Additional index constraints are specified by the
811// flags. Otherwise, bails out to the provided labels.
812//
813// Register usage: |object| may be changed to another string in a way
814// that doesn't affect charCodeAt/charAt semantics, |index| is
815// preserved, |scratch| and |result| are clobbered.
816class StringCharCodeAtGenerator {
817 public:
818 StringCharCodeAtGenerator(Register object,
819 Register index,
820 Register scratch,
821 Register result,
822 Label* receiver_not_string,
823 Label* index_not_number,
824 Label* index_out_of_range,
825 StringIndexFlags index_flags)
826 : object_(object),
827 index_(index),
828 scratch_(scratch),
829 result_(result),
830 receiver_not_string_(receiver_not_string),
831 index_not_number_(index_not_number),
832 index_out_of_range_(index_out_of_range),
833 index_flags_(index_flags) {
834 ASSERT(!scratch_.is(object_));
835 ASSERT(!scratch_.is(index_));
836 ASSERT(!scratch_.is(result_));
837 ASSERT(!result_.is(object_));
838 ASSERT(!result_.is(index_));
839 }
840
841 // Generates the fast case code. On the fallthrough path |result|
842 // register contains the result.
843 void GenerateFast(MacroAssembler* masm);
844
845 // Generates the slow case code. Must not be naturally
846 // reachable. Expected to be put after a ret instruction (e.g., in
847 // deferred code). Always jumps back to the fast case.
848 void GenerateSlow(MacroAssembler* masm,
849 const RuntimeCallHelper& call_helper);
850
851 private:
852 Register object_;
853 Register index_;
854 Register scratch_;
855 Register result_;
856
857 Label* receiver_not_string_;
858 Label* index_not_number_;
859 Label* index_out_of_range_;
860
861 StringIndexFlags index_flags_;
862
863 Label call_runtime_;
864 Label index_not_smi_;
865 Label got_smi_index_;
866 Label exit_;
867
868 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
869};
870
871
872// Generates code for creating a one-char string from a char code.
873class StringCharFromCodeGenerator {
874 public:
875 StringCharFromCodeGenerator(Register code,
876 Register result)
877 : code_(code),
878 result_(result) {
879 ASSERT(!code_.is(result_));
880 }
881
882 // Generates the fast case code. On the fallthrough path |result|
883 // register contains the result.
884 void GenerateFast(MacroAssembler* masm);
885
886 // Generates the slow case code. Must not be naturally
887 // reachable. Expected to be put after a ret instruction (e.g., in
888 // deferred code). Always jumps back to the fast case.
889 void GenerateSlow(MacroAssembler* masm,
890 const RuntimeCallHelper& call_helper);
891
892 private:
893 Register code_;
894 Register result_;
895
896 Label slow_case_;
897 Label exit_;
898
899 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
900};
901
902
903// Generates code implementing String.prototype.charAt.
904//
905// Only supports the case when the receiver is a string and the index
906// is a number (smi or heap number) that is a valid index into the
907// string. Additional index constraints are specified by the
908// flags. Otherwise, bails out to the provided labels.
909//
910// Register usage: |object| may be changed to another string in a way
911// that doesn't affect charCodeAt/charAt semantics, |index| is
912// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
913class StringCharAtGenerator {
914 public:
915 StringCharAtGenerator(Register object,
916 Register index,
917 Register scratch1,
918 Register scratch2,
919 Register result,
920 Label* receiver_not_string,
921 Label* index_not_number,
922 Label* index_out_of_range,
923 StringIndexFlags index_flags)
924 : char_code_at_generator_(object,
925 index,
926 scratch1,
927 scratch2,
928 receiver_not_string,
929 index_not_number,
930 index_out_of_range,
931 index_flags),
932 char_from_code_generator_(scratch2, result) {}
933
934 // Generates the fast case code. On the fallthrough path |result|
935 // register contains the result.
936 void GenerateFast(MacroAssembler* masm);
937
938 // Generates the slow case code. Must not be naturally
939 // reachable. Expected to be put after a ret instruction (e.g., in
940 // deferred code). Always jumps back to the fast case.
941 void GenerateSlow(MacroAssembler* masm,
942 const RuntimeCallHelper& call_helper);
943
944 private:
945 StringCharCodeAtGenerator char_code_at_generator_;
946 StringCharFromCodeGenerator char_from_code_generator_;
947
948 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
949};
950
Ben Murdoch086aeea2011-05-13 15:57:08 +0100951
952class AllowStubCallsScope {
953 public:
954 AllowStubCallsScope(MacroAssembler* masm, bool allow)
955 : masm_(masm), previous_allow_(masm->allow_stub_calls()) {
956 masm_->set_allow_stub_calls(allow);
957 }
958 ~AllowStubCallsScope() {
959 masm_->set_allow_stub_calls(previous_allow_);
960 }
961
962 private:
963 MacroAssembler* masm_;
964 bool previous_allow_;
965
966 DISALLOW_COPY_AND_ASSIGN(AllowStubCallsScope);
967};
968
Steve Blocka7e24c12009-10-30 11:49:00 +0000969} } // namespace v8::internal
970
971#endif // V8_CODE_STUBS_H_