blob: 64c89b93d13d2a4f0621cd0c084d7bea757776a8 [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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032#include "globals.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037// List of code stubs used on all platforms.
Steve Blockd0582a62009-12-15 09:54:21 +000038#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
39 V(CallFunction) \
Ben Murdoch257744e2011-11-30 15:57:28 +000040 V(UnaryOp) \
41 V(BinaryOp) \
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(Compare) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010046 V(CompareIC) \
47 V(MathPow) \
48 V(TranscendentalCache) \
Ben Murdoch086aeea2011-05-13 15:57:08 +010049 V(Instanceof) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000050 /* All stubs above this line only exist in a few versions, which are */ \
51 /* generated ahead of time. Therefore compiling a call to one of */ \
52 /* them can't cause a new stub to be compiled, so compiling a call to */ \
53 /* them is GC safe. The ones below this line exist in many variants */ \
54 /* so code compiling a call to one can cause a GC. This means they */ \
55 /* can't be called from other stubs, since stub generation code is */ \
56 /* not GC safe. */ \
Steve Blockd0582a62009-12-15 09:54:21 +000057 V(ConvertToDouble) \
58 V(WriteInt32ToHeapNumber) \
59 V(StackCheck) \
Leon Clarkee46be812010-01-19 14:06:41 +000060 V(FastNewClosure) \
61 V(FastNewContext) \
62 V(FastCloneShallowArray) \
Steve Blockd0582a62009-12-15 09:54:21 +000063 V(RevertToNumber) \
64 V(ToBoolean) \
Steve Block1e0659c2011-05-24 12:43:12 +010065 V(ToNumber) \
Steve Blockd0582a62009-12-15 09:54:21 +000066 V(CounterOp) \
67 V(ArgumentsAccess) \
Leon Clarkee46be812010-01-19 14:06:41 +000068 V(RegExpExec) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010069 V(RegExpConstructResult) \
Andrei Popescu402d9372010-02-26 13:31:12 +000070 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000071 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000072 V(JSEntry) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000073 V(KeyedLoadElement) \
74 V(KeyedStoreElement) \
Ben Murdoch257744e2011-11-30 15:57:28 +000075 V(DebuggerStatement) \
76 V(StringDictionaryNegativeLookup)
Steve Blockd0582a62009-12-15 09:54:21 +000077
78// List of code stubs only used on ARM platforms.
79#ifdef V8_TARGET_ARCH_ARM
80#define CODE_STUB_LIST_ARM(V) \
81 V(GetProperty) \
82 V(SetProperty) \
83 V(InvokeBuiltin) \
Steve Block1e0659c2011-05-24 12:43:12 +010084 V(RegExpCEntry) \
85 V(DirectCEntry)
Steve Blockd0582a62009-12-15 09:54:21 +000086#else
87#define CODE_STUB_LIST_ARM(V)
88#endif
89
Steve Block44f0eee2011-05-26 01:26:41 +010090// List of code stubs only used on MIPS platforms.
91#ifdef V8_TARGET_ARCH_MIPS
92#define CODE_STUB_LIST_MIPS(V) \
Ben Murdoch257744e2011-11-30 15:57:28 +000093 V(RegExpCEntry) \
94 V(DirectCEntry)
Steve Block44f0eee2011-05-26 01:26:41 +010095#else
96#define CODE_STUB_LIST_MIPS(V)
97#endif
98
Steve Blockd0582a62009-12-15 09:54:21 +000099// Combined list of code stubs.
100#define CODE_STUB_LIST(V) \
101 CODE_STUB_LIST_ALL_PLATFORMS(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100102 CODE_STUB_LIST_ARM(V) \
103 CODE_STUB_LIST_MIPS(V)
Steve Blocka7e24c12009-10-30 11:49:00 +0000104
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100105// Mode to overwrite BinaryExpression values.
106enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
107enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE };
108
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110// Stub is base classes of all stubs.
111class CodeStub BASE_EMBEDDED {
112 public:
113 enum Major {
Steve Blockd0582a62009-12-15 09:54:21 +0000114#define DEF_ENUM(name) name,
115 CODE_STUB_LIST(DEF_ENUM)
116#undef DEF_ENUM
117 NoCache, // marker for stubs that do custom caching
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 NUMBER_OF_IDS
119 };
120
121 // Retrieve the code for the stub. Generate the code if needed.
122 Handle<Code> GetCode();
123
Leon Clarkee46be812010-01-19 14:06:41 +0000124 // Retrieve the code for the stub if already generated. Do not
125 // generate the code if not already generated and instead return a
126 // retry after GC Failure object.
John Reck59135872010-11-02 12:39:01 -0700127 MUST_USE_RESULT MaybeObject* TryGetCode();
Leon Clarkee46be812010-01-19 14:06:41 +0000128
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 static Major MajorKeyFromKey(uint32_t key) {
130 return static_cast<Major>(MajorKeyBits::decode(key));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100131 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 static int MinorKeyFromKey(uint32_t key) {
133 return MinorKeyBits::decode(key);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100134 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100135
136 // Gets the major key from a code object that is a code stub or binary op IC.
137 static Major GetMajorKey(Code* code_stub) {
138 return static_cast<Major>(code_stub->major_key());
139 }
140
Andrei Popescu31002712010-02-23 13:46:05 +0000141 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000142
143 virtual ~CodeStub() {}
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 protected:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100146 static const int kMajorBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
148
149 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000150 // Lookup the code in the (possibly custom) cache.
151 bool FindCodeInCache(Code** code_out);
152
153 // Nonvirtual wrapper around the stub-specific Generate function. Call
154 // this function to set up the macro assembler and generate the code.
155 void GenerateCode(MacroAssembler* masm);
156
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 // Generates the assembler code for the stub.
158 virtual void Generate(MacroAssembler* masm) = 0;
159
Leon Clarkee46be812010-01-19 14:06:41 +0000160 // Perform bookkeeping required after code generation when stub code is
161 // initially generated.
162 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
163
Ben Murdochb0fe1622011-05-05 13:52:32 +0100164 // Finish the code object after it has been generated.
165 virtual void FinishCode(Code* code) { }
166
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 // Returns information for computing the number key.
168 virtual Major MajorKey() = 0;
169 virtual int MinorKey() = 0;
170
Ben Murdoch257744e2011-11-30 15:57:28 +0000171 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100172 virtual int GetCodeKind();
173
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100175 virtual InlineCacheState GetICState() {
176 return UNINITIALIZED;
177 }
178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 // Returns a name for logging/debugging purposes.
Ben Murdoch589d6972011-11-30 16:04:58 +0000180 SmartArrayPointer<const char> GetName();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000181 virtual void PrintName(StringStream* stream) {
182 stream->Add("%s", MajorName(MajorKey(), false));
183 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
Steve Block44f0eee2011-05-26 01:26:41 +0100185 // Returns whether the code generated for this stub needs to be allocated as
186 // a fixed (non-moveable) code object.
187 virtual bool NeedsImmovableCode() { return false; }
188
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 // Computes the key based on major and minor.
190 uint32_t GetKey() {
191 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
192 return MinorKeyBits::encode(MinorKey()) |
193 MajorKeyBits::encode(MajorKey());
194 }
195
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000196 // See comment above, where Instanceof is defined.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100197 bool AllowsStubCalls() { return MajorKey() <= Instanceof; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
200 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
201
202 friend class BreakPointIterator;
203};
204
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100205
206// Helper interface to prepare to/restore after making runtime calls.
207class RuntimeCallHelper {
208 public:
209 virtual ~RuntimeCallHelper() {}
210
211 virtual void BeforeCall(MacroAssembler* masm) const = 0;
212
213 virtual void AfterCall(MacroAssembler* masm) const = 0;
214
215 protected:
216 RuntimeCallHelper() {}
217
218 private:
219 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
220};
221
222} } // namespace v8::internal
223
224#if V8_TARGET_ARCH_IA32
225#include "ia32/code-stubs-ia32.h"
226#elif V8_TARGET_ARCH_X64
227#include "x64/code-stubs-x64.h"
228#elif V8_TARGET_ARCH_ARM
229#include "arm/code-stubs-arm.h"
230#elif V8_TARGET_ARCH_MIPS
231#include "mips/code-stubs-mips.h"
232#else
233#error Unsupported target architecture.
234#endif
235
236namespace v8 {
237namespace internal {
238
239
Ben Murdochb0fe1622011-05-05 13:52:32 +0100240// RuntimeCallHelper implementation used in stubs: enters/leaves a
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100241// newly created internal frame before/after the runtime call.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100242class StubRuntimeCallHelper : public RuntimeCallHelper {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100243 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100244 StubRuntimeCallHelper() {}
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100245
246 virtual void BeforeCall(MacroAssembler* masm) const;
247
248 virtual void AfterCall(MacroAssembler* masm) const;
249};
250
251
252// Trivial RuntimeCallHelper implementation.
253class NopRuntimeCallHelper : public RuntimeCallHelper {
254 public:
255 NopRuntimeCallHelper() {}
256
257 virtual void BeforeCall(MacroAssembler* masm) const {}
258
259 virtual void AfterCall(MacroAssembler* masm) const {}
260};
261
262
263class StackCheckStub : public CodeStub {
264 public:
265 StackCheckStub() { }
266
267 void Generate(MacroAssembler* masm);
268
269 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100270 Major MajorKey() { return StackCheck; }
271 int MinorKey() { return 0; }
272};
273
274
Steve Block1e0659c2011-05-24 12:43:12 +0100275class ToNumberStub: public CodeStub {
276 public:
277 ToNumberStub() { }
278
279 void Generate(MacroAssembler* masm);
280
281 private:
282 Major MajorKey() { return ToNumber; }
283 int MinorKey() { return 0; }
Steve Block1e0659c2011-05-24 12:43:12 +0100284};
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:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100295 Major MajorKey() { return FastNewClosure; }
Steve Block44f0eee2011-05-26 01:26:41 +0100296 int MinorKey() { return strict_mode_; }
297
298 StrictModeFlag strict_mode_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100299};
300
301
302class FastNewContextStub : public CodeStub {
303 public:
304 static const int kMaximumSlots = 64;
305
306 explicit FastNewContextStub(int slots) : slots_(slots) {
307 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
308 }
309
310 void Generate(MacroAssembler* masm);
311
312 private:
313 int slots_;
314
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100315 Major MajorKey() { return FastNewContext; }
316 int MinorKey() { return slots_; }
317};
318
319
320class FastCloneShallowArrayStub : public CodeStub {
321 public:
322 // Maximum length of copied elements array.
323 static const int kMaximumClonedLength = 8;
324
325 enum Mode {
326 CLONE_ELEMENTS,
327 COPY_ON_WRITE_ELEMENTS
328 };
329
330 FastCloneShallowArrayStub(Mode mode, int length)
331 : mode_(mode),
332 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
333 ASSERT(length_ >= 0);
334 ASSERT(length_ <= kMaximumClonedLength);
335 }
336
337 void Generate(MacroAssembler* masm);
338
339 private:
340 Mode mode_;
341 int length_;
342
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100343 Major MajorKey() { return FastCloneShallowArray; }
344 int MinorKey() {
345 ASSERT(mode_ == 0 || mode_ == 1);
346 return (length_ << 1) | mode_;
347 }
348};
349
350
351class InstanceofStub: public CodeStub {
352 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100353 enum Flags {
354 kNoFlags = 0,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100355 kArgsInRegisters = 1 << 0,
356 kCallSiteInlineCheck = 1 << 1,
357 kReturnTrueFalseObject = 1 << 2
Ben Murdochb0fe1622011-05-05 13:52:32 +0100358 };
359
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000360 explicit InstanceofStub(Flags flags) : flags_(flags) { }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100361
362 static Register left();
363 static Register right();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100364
365 void Generate(MacroAssembler* masm);
366
367 private:
368 Major MajorKey() { return Instanceof; }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100369 int MinorKey() { return static_cast<int>(flags_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100370
Ben Murdoch086aeea2011-05-13 15:57:08 +0100371 bool HasArgsInRegisters() const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100372 return (flags_ & kArgsInRegisters) != 0;
373 }
374
Ben Murdoch086aeea2011-05-13 15:57:08 +0100375 bool HasCallSiteInlineCheck() const {
376 return (flags_ & kCallSiteInlineCheck) != 0;
377 }
378
379 bool ReturnTrueFalseObject() const {
380 return (flags_ & kReturnTrueFalseObject) != 0;
381 }
382
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000383 virtual void PrintName(StringStream* stream);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100384
Ben Murdochb0fe1622011-05-05 13:52:32 +0100385 Flags flags_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100386};
387
388
Ben Murdochb0fe1622011-05-05 13:52:32 +0100389class MathPowStub: public CodeStub {
390 public:
391 MathPowStub() {}
392 virtual void Generate(MacroAssembler* masm);
393
394 private:
395 virtual CodeStub::Major MajorKey() { return MathPow; }
396 virtual int MinorKey() { return 0; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100397};
398
399
Ben Murdochb0fe1622011-05-05 13:52:32 +0100400class ICCompareStub: public CodeStub {
401 public:
402 ICCompareStub(Token::Value op, CompareIC::State state)
403 : op_(op), state_(state) {
404 ASSERT(Token::IsCompareOp(op));
405 }
406
407 virtual void Generate(MacroAssembler* masm);
408
409 private:
410 class OpField: public BitField<int, 0, 3> { };
411 class StateField: public BitField<int, 3, 5> { };
412
413 virtual void FinishCode(Code* code) { code->set_compare_state(state_); }
414
415 virtual CodeStub::Major MajorKey() { return CompareIC; }
416 virtual int MinorKey();
417
418 virtual int GetCodeKind() { return Code::COMPARE_IC; }
419
420 void GenerateSmis(MacroAssembler* masm);
421 void GenerateHeapNumbers(MacroAssembler* masm);
Ben Murdoch257744e2011-11-30 15:57:28 +0000422 void GenerateSymbols(MacroAssembler* masm);
423 void GenerateStrings(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100424 void GenerateObjects(MacroAssembler* masm);
425 void GenerateMiss(MacroAssembler* masm);
426
427 bool strict() const { return op_ == Token::EQ_STRICT; }
428 Condition GetCondition() const { return CompareIC::ComputeCondition(op_); }
429
430 Token::Value op_;
431 CompareIC::State state_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100432};
433
434
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100435// Flags that control the compare stub code generation.
436enum CompareFlags {
437 NO_COMPARE_FLAGS = 0,
438 NO_SMI_COMPARE_IN_STUB = 1 << 0,
439 NO_NUMBER_COMPARE_IN_STUB = 1 << 1,
440 CANT_BOTH_BE_NAN = 1 << 2
441};
442
443
Ben Murdochb0fe1622011-05-05 13:52:32 +0100444enum NaNInformation {
445 kBothCouldBeNaN,
446 kCantBothBeNaN
447};
448
449
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100450class CompareStub: public CodeStub {
451 public:
452 CompareStub(Condition cc,
453 bool strict,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100454 CompareFlags flags,
455 Register lhs,
456 Register rhs) :
Ben Murdochb0fe1622011-05-05 13:52:32 +0100457 cc_(cc),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100458 strict_(strict),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100459 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
460 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
461 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100462 lhs_(lhs),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000463 rhs_(rhs) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100464
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100465 CompareStub(Condition cc,
466 bool strict,
467 CompareFlags flags) :
468 cc_(cc),
469 strict_(strict),
470 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
471 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
472 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
473 lhs_(no_reg),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000474 rhs_(no_reg) { }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100475
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100476 void Generate(MacroAssembler* masm);
477
478 private:
479 Condition cc_;
480 bool strict_;
481 // Only used for 'equal' comparisons. Tells the stub that we already know
482 // that at least one side of the comparison is not NaN. This allows the
483 // stub to use object identity in the positive case. We ignore it when
484 // generating the minor key for other comparisons to avoid creating more
485 // stubs.
486 bool never_nan_nan_;
487 // Do generate the number comparison code in the stub. Stubs without number
488 // comparison code is used when the number comparison has been inlined, and
489 // the stub will be called if one of the operands is not a number.
490 bool include_number_compare_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100491
492 // Generate the comparison code for two smi operands in the stub.
493 bool include_smi_compare_;
494
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100495 // Register holding the left hand side of the comparison if the stub gives
496 // a choice, no_reg otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100497
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100498 Register lhs_;
499 // Register holding the right hand side of the comparison if the stub gives
500 // a choice, no_reg otherwise.
501 Register rhs_;
502
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100503 // Encoding of the minor key in 16 bits.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100504 class StrictField: public BitField<bool, 0, 1> {};
505 class NeverNanNanField: public BitField<bool, 1, 1> {};
506 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100507 class IncludeSmiCompareField: public BitField<bool, 3, 1> {};
508 class RegisterField: public BitField<bool, 4, 1> {};
509 class ConditionField: public BitField<int, 5, 11> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100510
511 Major MajorKey() { return Compare; }
512
513 int MinorKey();
514
Ben Murdochb0fe1622011-05-05 13:52:32 +0100515 virtual int GetCodeKind() { return Code::COMPARE_IC; }
516 virtual void FinishCode(Code* code) {
517 code->set_compare_state(CompareIC::GENERIC);
518 }
519
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100520 // Branch to the label if the given object isn't a symbol.
521 void BranchIfNonSymbol(MacroAssembler* masm,
522 Label* label,
523 Register object,
524 Register scratch);
525
526 // Unfortunately you have to run without snapshots to see most of these
527 // names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000528 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100529};
530
531
532class CEntryStub : public CodeStub {
533 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100534 explicit CEntryStub(int result_size)
535 : result_size_(result_size), save_doubles_(false) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100536
537 void Generate(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100538 void SaveDoubles() { save_doubles_ = true; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100539
540 private:
541 void GenerateCore(MacroAssembler* masm,
542 Label* throw_normal_exception,
543 Label* throw_termination_exception,
544 Label* throw_out_of_memory_exception,
545 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +0100546 bool always_allocate_scope);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100547 void GenerateThrowTOS(MacroAssembler* masm);
548 void GenerateThrowUncatchable(MacroAssembler* masm,
549 UncatchableExceptionType type);
550
551 // Number of pointers/values returned.
552 const int result_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100553 bool save_doubles_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100554
555 Major MajorKey() { return CEntry; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100556 int MinorKey();
557
Steve Block44f0eee2011-05-26 01:26:41 +0100558 bool NeedsImmovableCode();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100559};
560
561
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100562class JSEntryStub : public CodeStub {
563 public:
564 JSEntryStub() { }
565
566 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
567
568 protected:
569 void GenerateBody(MacroAssembler* masm, bool is_construct);
570
571 private:
572 Major MajorKey() { return JSEntry; }
573 int MinorKey() { return 0; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100574};
575
576
577class JSConstructEntryStub : public JSEntryStub {
578 public:
579 JSConstructEntryStub() { }
580
581 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
582
583 private:
584 int MinorKey() { return 1; }
585
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000586 virtual void PrintName(StringStream* stream) {
587 stream->Add("JSConstructEntryStub");
588 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100589};
590
591
592class ArgumentsAccessStub: public CodeStub {
593 public:
594 enum Type {
595 READ_ELEMENT,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000596 NEW_NON_STRICT_FAST,
597 NEW_NON_STRICT_SLOW,
Steve Block44f0eee2011-05-26 01:26:41 +0100598 NEW_STRICT
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100599 };
600
601 explicit ArgumentsAccessStub(Type type) : type_(type) { }
602
603 private:
604 Type type_;
605
606 Major MajorKey() { return ArgumentsAccess; }
607 int MinorKey() { return type_; }
608
609 void Generate(MacroAssembler* masm);
610 void GenerateReadElement(MacroAssembler* masm);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000611 void GenerateNewStrict(MacroAssembler* masm);
612 void GenerateNewNonStrictFast(MacroAssembler* masm);
613 void GenerateNewNonStrictSlow(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100614
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000615 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100616};
617
618
619class RegExpExecStub: public CodeStub {
620 public:
621 RegExpExecStub() { }
622
623 private:
624 Major MajorKey() { return RegExpExec; }
625 int MinorKey() { return 0; }
626
627 void Generate(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100628};
629
630
Ben Murdochb0fe1622011-05-05 13:52:32 +0100631class RegExpConstructResultStub: public CodeStub {
632 public:
633 RegExpConstructResultStub() { }
634
635 private:
636 Major MajorKey() { return RegExpConstructResult; }
637 int MinorKey() { return 0; }
638
639 void Generate(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640};
641
642
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100643class CallFunctionStub: public CodeStub {
644 public:
Ben Murdoch589d6972011-11-30 16:04:58 +0000645 CallFunctionStub(int argc, CallFunctionFlags flags)
646 : argc_(argc), flags_(flags) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100647
648 void Generate(MacroAssembler* masm);
649
Ben Murdoch086aeea2011-05-13 15:57:08 +0100650 static int ExtractArgcFromMinorKey(int minor_key) {
651 return ArgcBits::decode(minor_key);
652 }
653
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100654 private:
655 int argc_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100656 CallFunctionFlags flags_;
657
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000658 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100659
660 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Ben Murdoch589d6972011-11-30 16:04:58 +0000661 class FlagBits: public BitField<CallFunctionFlags, 0, 1> {};
662 class ArgcBits: public BitField<unsigned, 1, 32 - 1> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100663
664 Major MajorKey() { return CallFunction; }
665 int MinorKey() {
666 // Encode the parameters in a unique 32 bit value.
Ben Murdoch589d6972011-11-30 16:04:58 +0000667 return FlagBits::encode(flags_) | ArgcBits::encode(argc_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100668 }
669
Ben Murdoch257744e2011-11-30 15:57:28 +0000670 bool ReceiverMightBeImplicit() {
671 return (flags_ & RECEIVER_MIGHT_BE_IMPLICIT) != 0;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100672 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100673};
674
675
676enum StringIndexFlags {
677 // Accepts smis or heap numbers.
678 STRING_INDEX_IS_NUMBER,
679
680 // Accepts smis or heap numbers that are valid array indices
681 // (ECMA-262 15.4). Invalid indices are reported as being out of
682 // range.
683 STRING_INDEX_IS_ARRAY_INDEX
684};
685
686
687// Generates code implementing String.prototype.charCodeAt.
688//
689// Only supports the case when the receiver is a string and the index
690// is a number (smi or heap number) that is a valid index into the
691// string. Additional index constraints are specified by the
692// flags. Otherwise, bails out to the provided labels.
693//
694// Register usage: |object| may be changed to another string in a way
695// that doesn't affect charCodeAt/charAt semantics, |index| is
696// preserved, |scratch| and |result| are clobbered.
697class StringCharCodeAtGenerator {
698 public:
699 StringCharCodeAtGenerator(Register object,
700 Register index,
701 Register scratch,
702 Register result,
703 Label* receiver_not_string,
704 Label* index_not_number,
705 Label* index_out_of_range,
706 StringIndexFlags index_flags)
707 : object_(object),
708 index_(index),
709 scratch_(scratch),
710 result_(result),
711 receiver_not_string_(receiver_not_string),
712 index_not_number_(index_not_number),
713 index_out_of_range_(index_out_of_range),
714 index_flags_(index_flags) {
715 ASSERT(!scratch_.is(object_));
716 ASSERT(!scratch_.is(index_));
717 ASSERT(!scratch_.is(result_));
718 ASSERT(!result_.is(object_));
719 ASSERT(!result_.is(index_));
720 }
721
722 // Generates the fast case code. On the fallthrough path |result|
723 // register contains the result.
724 void GenerateFast(MacroAssembler* masm);
725
726 // Generates the slow case code. Must not be naturally
727 // reachable. Expected to be put after a ret instruction (e.g., in
728 // deferred code). Always jumps back to the fast case.
729 void GenerateSlow(MacroAssembler* masm,
730 const RuntimeCallHelper& call_helper);
731
732 private:
733 Register object_;
734 Register index_;
735 Register scratch_;
736 Register result_;
737
738 Label* receiver_not_string_;
739 Label* index_not_number_;
740 Label* index_out_of_range_;
741
742 StringIndexFlags index_flags_;
743
744 Label call_runtime_;
745 Label index_not_smi_;
746 Label got_smi_index_;
747 Label exit_;
748
749 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
750};
751
752
753// Generates code for creating a one-char string from a char code.
754class StringCharFromCodeGenerator {
755 public:
756 StringCharFromCodeGenerator(Register code,
757 Register result)
758 : code_(code),
759 result_(result) {
760 ASSERT(!code_.is(result_));
761 }
762
763 // Generates the fast case code. On the fallthrough path |result|
764 // register contains the result.
765 void GenerateFast(MacroAssembler* masm);
766
767 // Generates the slow case code. Must not be naturally
768 // reachable. Expected to be put after a ret instruction (e.g., in
769 // deferred code). Always jumps back to the fast case.
770 void GenerateSlow(MacroAssembler* masm,
771 const RuntimeCallHelper& call_helper);
772
773 private:
774 Register code_;
775 Register result_;
776
777 Label slow_case_;
778 Label exit_;
779
780 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
781};
782
783
784// Generates code implementing String.prototype.charAt.
785//
786// Only supports the case when the receiver is a string and the index
787// is a number (smi or heap number) that is a valid index into the
788// string. Additional index constraints are specified by the
789// flags. Otherwise, bails out to the provided labels.
790//
791// Register usage: |object| may be changed to another string in a way
792// that doesn't affect charCodeAt/charAt semantics, |index| is
793// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
794class StringCharAtGenerator {
795 public:
796 StringCharAtGenerator(Register object,
797 Register index,
798 Register scratch1,
799 Register scratch2,
800 Register result,
801 Label* receiver_not_string,
802 Label* index_not_number,
803 Label* index_out_of_range,
804 StringIndexFlags index_flags)
805 : char_code_at_generator_(object,
806 index,
807 scratch1,
808 scratch2,
809 receiver_not_string,
810 index_not_number,
811 index_out_of_range,
812 index_flags),
813 char_from_code_generator_(scratch2, result) {}
814
815 // Generates the fast case code. On the fallthrough path |result|
816 // register contains the result.
817 void GenerateFast(MacroAssembler* masm);
818
819 // Generates the slow case code. Must not be naturally
820 // reachable. Expected to be put after a ret instruction (e.g., in
821 // deferred code). Always jumps back to the fast case.
822 void GenerateSlow(MacroAssembler* masm,
823 const RuntimeCallHelper& call_helper);
824
825 private:
826 StringCharCodeAtGenerator char_code_at_generator_;
827 StringCharFromCodeGenerator char_from_code_generator_;
828
829 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
830};
831
Ben Murdoch086aeea2011-05-13 15:57:08 +0100832
833class AllowStubCallsScope {
834 public:
835 AllowStubCallsScope(MacroAssembler* masm, bool allow)
836 : masm_(masm), previous_allow_(masm->allow_stub_calls()) {
837 masm_->set_allow_stub_calls(allow);
838 }
839 ~AllowStubCallsScope() {
840 masm_->set_allow_stub_calls(previous_allow_);
841 }
842
843 private:
844 MacroAssembler* masm_;
845 bool previous_allow_;
846
847 DISALLOW_COPY_AND_ASSIGN(AllowStubCallsScope);
848};
849
Ben Murdoch257744e2011-11-30 15:57:28 +0000850
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000851class KeyedLoadElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000852 public:
Ben Murdoch589d6972011-11-30 16:04:58 +0000853 explicit KeyedLoadElementStub(ElementsKind elements_kind)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000854 : elements_kind_(elements_kind)
855 { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000856
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000857 Major MajorKey() { return KeyedLoadElement; }
858 int MinorKey() { return elements_kind_; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000859
860 void Generate(MacroAssembler* masm);
861
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000862 private:
Ben Murdoch589d6972011-11-30 16:04:58 +0000863 ElementsKind elements_kind_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000864
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000865 DISALLOW_COPY_AND_ASSIGN(KeyedLoadElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +0000866};
867
868
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000869class KeyedStoreElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000870 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000871 KeyedStoreElementStub(bool is_js_array,
Ben Murdoch589d6972011-11-30 16:04:58 +0000872 ElementsKind elements_kind)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000873 : is_js_array_(is_js_array),
874 elements_kind_(elements_kind) { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000875
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000876 Major MajorKey() { return KeyedStoreElement; }
877 int MinorKey() {
Ben Murdoch589d6972011-11-30 16:04:58 +0000878 return (is_js_array_ ? 0 : kElementsKindCount) + elements_kind_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000879 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000880
881 void Generate(MacroAssembler* masm);
882
Ben Murdoch257744e2011-11-30 15:57:28 +0000883 private:
884 bool is_js_array_;
Ben Murdoch589d6972011-11-30 16:04:58 +0000885 ElementsKind elements_kind_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000886
887 DISALLOW_COPY_AND_ASSIGN(KeyedStoreElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +0000888};
889
890
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000891class ToBooleanStub: public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000892 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000893 enum Type {
894 UNDEFINED,
895 BOOLEAN,
896 NULL_TYPE,
897 SMI,
898 SPEC_OBJECT,
899 STRING,
900 HEAP_NUMBER,
901 NUMBER_OF_TYPES
902 };
903
904 // At most 8 different types can be distinguished, because the Code object
905 // only has room for a single byte to hold a set of these types. :-P
906 STATIC_ASSERT(NUMBER_OF_TYPES <= 8);
907
908 class Types {
909 public:
910 Types() {}
911 explicit Types(byte bits) : set_(bits) {}
912
913 bool IsEmpty() const { return set_.IsEmpty(); }
914 bool Contains(Type type) const { return set_.Contains(type); }
915 void Add(Type type) { set_.Add(type); }
916 byte ToByte() const { return set_.ToIntegral(); }
917 void Print(StringStream* stream) const;
918 void TraceTransition(Types to) const;
919 bool Record(Handle<Object> object);
920 bool NeedsMap() const;
921 bool CanBeUndetectable() const;
922
923 private:
924 EnumSet<Type, byte> set_;
925 };
926
927 static Types no_types() { return Types(); }
928 static Types all_types() { return Types((1 << NUMBER_OF_TYPES) - 1); }
929
930 explicit ToBooleanStub(Register tos, Types types = Types())
931 : tos_(tos), types_(types) { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000932
933 void Generate(MacroAssembler* masm);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000934 virtual int GetCodeKind() { return Code::TO_BOOLEAN_IC; }
935 virtual void PrintName(StringStream* stream);
Ben Murdoch257744e2011-11-30 15:57:28 +0000936
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000937 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000938 Major MajorKey() { return ToBoolean; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000939 int MinorKey() { return (tos_.code() << NUMBER_OF_TYPES) | types_.ToByte(); }
940
941 virtual void FinishCode(Code* code) {
942 code->set_to_boolean_state(types_.ToByte());
943 }
944
945 void CheckOddball(MacroAssembler* masm,
946 Type type,
947 Heap::RootListIndex value,
948 bool result);
949 void GenerateTypeTransition(MacroAssembler* masm);
950
951 Register tos_;
952 Types types_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000953};
954
Steve Blocka7e24c12009-10-30 11:49:00 +0000955} } // namespace v8::internal
956
957#endif // V8_CODE_STUBS_H_