blob: b67e961ac78a48614ffd5ed84c5b5a77f90810a5 [file] [log] [blame]
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001// Copyright 2012 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"
Ben Murdoch592a9fc2012-03-05 11:04:45 +000033#include "codegen.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034
Steve Blocka7e24c12009-10-30 11:49:00 +000035namespace v8 {
36namespace internal {
37
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000038// List of code stubs used on all platforms.
Steve Blockd0582a62009-12-15 09:54:21 +000039#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
40 V(CallFunction) \
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010041 V(CallConstruct) \
Ben Murdoch257744e2011-11-30 15:57:28 +000042 V(UnaryOp) \
43 V(BinaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000044 V(StringAdd) \
Leon Clarkee46be812010-01-19 14:06:41 +000045 V(SubString) \
46 V(StringCompare) \
Steve Blockd0582a62009-12-15 09:54:21 +000047 V(Compare) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010048 V(CompareIC) \
49 V(MathPow) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +000050 V(RecordWrite) \
51 V(StoreBufferOverflow) \
52 V(RegExpExec) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010053 V(TranscendentalCache) \
Ben Murdoch086aeea2011-05-13 15:57:08 +010054 V(Instanceof) \
Steve Blockd0582a62009-12-15 09:54:21 +000055 V(ConvertToDouble) \
56 V(WriteInt32ToHeapNumber) \
57 V(StackCheck) \
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010058 V(Interrupt) \
Leon Clarkee46be812010-01-19 14:06:41 +000059 V(FastNewClosure) \
60 V(FastNewContext) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +000061 V(FastNewBlockContext) \
Leon Clarkee46be812010-01-19 14:06:41 +000062 V(FastCloneShallowArray) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +000063 V(FastCloneShallowObject) \
Steve Blockd0582a62009-12-15 09:54:21 +000064 V(ToBoolean) \
Steve Block1e0659c2011-05-24 12:43:12 +010065 V(ToNumber) \
Steve Blockd0582a62009-12-15 09:54:21 +000066 V(ArgumentsAccess) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010067 V(RegExpConstructResult) \
Andrei Popescu402d9372010-02-26 13:31:12 +000068 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000069 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000070 V(JSEntry) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000071 V(KeyedLoadElement) \
72 V(KeyedStoreElement) \
Ben Murdoch257744e2011-11-30 15:57:28 +000073 V(DebuggerStatement) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +000074 V(StringDictionaryLookup) \
75 V(ElementsTransitionAndStore) \
76 V(StoreArrayLiteralElement)
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
124 static Major MajorKeyFromKey(uint32_t key) {
125 return static_cast<Major>(MajorKeyBits::decode(key));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100126 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 static int MinorKeyFromKey(uint32_t key) {
128 return MinorKeyBits::decode(key);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100129 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100130
131 // Gets the major key from a code object that is a code stub or binary op IC.
132 static Major GetMajorKey(Code* code_stub) {
133 return static_cast<Major>(code_stub->major_key());
134 }
135
Andrei Popescu31002712010-02-23 13:46:05 +0000136 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
138 virtual ~CodeStub() {}
139
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000140 bool CompilingCallsToThisStubIsGCSafe() {
141 bool is_pregenerated = IsPregenerated();
142 Code* code = NULL;
143 CHECK(!is_pregenerated || FindCodeInCache(&code));
144 return is_pregenerated;
145 }
146
147 // See comment above, where Instanceof is defined.
148 virtual bool IsPregenerated() { return false; }
149
150 static void GenerateStubsAheadOfTime();
151 static void GenerateFPStubs();
152
153 // Some stubs put untagged junk on the stack that cannot be scanned by the
154 // GC. This means that we must be statically sure that no GC can occur while
155 // they are running. If that is the case they should override this to return
156 // true, which will cause an assertion if we try to call something that can
157 // GC or if we try to put a stack frame on top of the junk, which would not
158 // result in a traversable stack.
159 virtual bool SometimesSetsUpAFrame() { return true; }
160
161 // Lookup the code in the (possibly custom) cache.
162 bool FindCodeInCache(Code** code_out);
163
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 protected:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 static const int kMajorBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
167
168 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000169 // Nonvirtual wrapper around the stub-specific Generate function. Call
170 // this function to set up the macro assembler and generate the code.
171 void GenerateCode(MacroAssembler* masm);
172
Steve Blocka7e24c12009-10-30 11:49:00 +0000173 // Generates the assembler code for the stub.
174 virtual void Generate(MacroAssembler* masm) = 0;
175
Leon Clarkee46be812010-01-19 14:06:41 +0000176 // Perform bookkeeping required after code generation when stub code is
177 // initially generated.
178 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
179
Ben Murdochb0fe1622011-05-05 13:52:32 +0100180 // Finish the code object after it has been generated.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000181 virtual void FinishCode(Handle<Code> code) { }
182
183 // Activate newly generated stub. Is called after
184 // registering stub in the stub cache.
185 virtual void Activate(Code* code) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 // Returns information for computing the number key.
188 virtual Major MajorKey() = 0;
189 virtual int MinorKey() = 0;
190
Ben Murdoch257744e2011-11-30 15:57:28 +0000191 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100192 virtual int GetCodeKind();
193
Ben Murdoch257744e2011-11-30 15:57:28 +0000194 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100195 virtual InlineCacheState GetICState() {
196 return UNINITIALIZED;
197 }
198
Ben Murdochc7cc0282012-03-05 14:35:55 +0000199 // Add the code to a specialized cache, specific to an individual
200 // stub type. Please note, this method must add the code object to a
201 // roots object, otherwise we will remove the code during GC.
202 virtual void AddToSpecialCache(Handle<Code> new_object) { }
203
204 // Find code in a specialized cache, work is delegated to the specific stub.
205 virtual bool FindCodeInSpecialCache(Code** code_out) { return false; }
206
207 // If a stub uses a special cache override this.
208 virtual bool UseSpecialCache() { return false; }
209
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 // Returns a name for logging/debugging purposes.
Ben Murdoch589d6972011-11-30 16:04:58 +0000211 SmartArrayPointer<const char> GetName();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000212 virtual void PrintName(StringStream* stream);
Steve Blocka7e24c12009-10-30 11:49:00 +0000213
Steve Block44f0eee2011-05-26 01:26:41 +0100214 // Returns whether the code generated for this stub needs to be allocated as
215 // a fixed (non-moveable) code object.
216 virtual bool NeedsImmovableCode() { return false; }
217
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 // Computes the key based on major and minor.
219 uint32_t GetKey() {
220 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
221 return MinorKeyBits::encode(MinorKey()) |
222 MajorKeyBits::encode(MajorKey());
223 }
224
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
226 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
227
228 friend class BreakPointIterator;
229};
230
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100231
232// Helper interface to prepare to/restore after making runtime calls.
233class 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} } // namespace v8::internal
249
250#if V8_TARGET_ARCH_IA32
251#include "ia32/code-stubs-ia32.h"
252#elif V8_TARGET_ARCH_X64
253#include "x64/code-stubs-x64.h"
254#elif V8_TARGET_ARCH_ARM
255#include "arm/code-stubs-arm.h"
256#elif V8_TARGET_ARCH_MIPS
257#include "mips/code-stubs-mips.h"
258#else
259#error Unsupported target architecture.
260#endif
261
262namespace v8 {
263namespace internal {
264
265
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266// RuntimeCallHelper implementation used in stubs: enters/leaves a
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100267// newly created internal frame before/after the runtime call.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100268class StubRuntimeCallHelper : public RuntimeCallHelper {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100269 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100270 StubRuntimeCallHelper() {}
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100271
272 virtual void BeforeCall(MacroAssembler* masm) const;
273
274 virtual void AfterCall(MacroAssembler* masm) const;
275};
276
277
278// Trivial RuntimeCallHelper implementation.
279class 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
289class StackCheckStub : public CodeStub {
290 public:
291 StackCheckStub() { }
292
293 void Generate(MacroAssembler* masm);
294
295 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100296 Major MajorKey() { return StackCheck; }
297 int MinorKey() { return 0; }
298};
299
300
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100301class InterruptStub : public CodeStub {
302 public:
303 InterruptStub() { }
304
305 void Generate(MacroAssembler* masm);
306
307 private:
308 Major MajorKey() { return Interrupt; }
309 int MinorKey() { return 0; }
310};
311
312
Steve Block1e0659c2011-05-24 12:43:12 +0100313class ToNumberStub: public CodeStub {
314 public:
315 ToNumberStub() { }
316
317 void Generate(MacroAssembler* masm);
318
319 private:
320 Major MajorKey() { return ToNumber; }
321 int MinorKey() { return 0; }
Steve Block1e0659c2011-05-24 12:43:12 +0100322};
323
324
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100325class FastNewClosureStub : public CodeStub {
326 public:
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000327 explicit FastNewClosureStub(LanguageMode language_mode)
328 : language_mode_(language_mode) { }
Steve Block44f0eee2011-05-26 01:26:41 +0100329
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100330 void Generate(MacroAssembler* masm);
331
332 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100333 Major MajorKey() { return FastNewClosure; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000334 int MinorKey() { return language_mode_ == CLASSIC_MODE
335 ? kNonStrictMode : kStrictMode; }
Steve Block44f0eee2011-05-26 01:26:41 +0100336
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000337 LanguageMode language_mode_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100338};
339
340
341class FastNewContextStub : public CodeStub {
342 public:
343 static const int kMaximumSlots = 64;
344
345 explicit FastNewContextStub(int slots) : slots_(slots) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000346 ASSERT(slots_ > 0 && slots_ <= kMaximumSlots);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100347 }
348
349 void Generate(MacroAssembler* masm);
350
351 private:
352 int slots_;
353
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100354 Major MajorKey() { return FastNewContext; }
355 int MinorKey() { return slots_; }
356};
357
358
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000359class FastNewBlockContextStub : public CodeStub {
360 public:
361 static const int kMaximumSlots = 64;
362
363 explicit FastNewBlockContextStub(int slots) : slots_(slots) {
364 ASSERT(slots_ > 0 && slots_ <= kMaximumSlots);
365 }
366
367 void Generate(MacroAssembler* masm);
368
369 private:
370 int slots_;
371
372 Major MajorKey() { return FastNewBlockContext; }
373 int MinorKey() { return slots_; }
374};
375
376
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100377class FastCloneShallowArrayStub : public CodeStub {
378 public:
379 // Maximum length of copied elements array.
380 static const int kMaximumClonedLength = 8;
381
382 enum Mode {
383 CLONE_ELEMENTS,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000384 CLONE_DOUBLE_ELEMENTS,
385 COPY_ON_WRITE_ELEMENTS,
386 CLONE_ANY_ELEMENTS
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100387 };
388
389 FastCloneShallowArrayStub(Mode mode, int length)
390 : mode_(mode),
391 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000392 ASSERT_GE(length_, 0);
393 ASSERT_LE(length_, kMaximumClonedLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100394 }
395
396 void Generate(MacroAssembler* masm);
397
398 private:
399 Mode mode_;
400 int length_;
401
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100402 Major MajorKey() { return FastCloneShallowArray; }
403 int MinorKey() {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000404 ASSERT(mode_ == 0 || mode_ == 1 || mode_ == 2 || mode_ == 3);
405 return length_ * 4 + mode_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100406 }
407};
408
409
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000410class FastCloneShallowObjectStub : public CodeStub {
411 public:
412 // Maximum number of properties in copied object.
413 static const int kMaximumClonedProperties = 6;
414
415 explicit FastCloneShallowObjectStub(int length) : length_(length) {
416 ASSERT_GE(length_, 0);
417 ASSERT_LE(length_, kMaximumClonedProperties);
418 }
419
420 void Generate(MacroAssembler* masm);
421
422 private:
423 int length_;
424
425 Major MajorKey() { return FastCloneShallowObject; }
426 int MinorKey() { return length_; }
427};
428
429
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100430class InstanceofStub: public CodeStub {
431 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100432 enum Flags {
433 kNoFlags = 0,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100434 kArgsInRegisters = 1 << 0,
435 kCallSiteInlineCheck = 1 << 1,
436 kReturnTrueFalseObject = 1 << 2
Ben Murdochb0fe1622011-05-05 13:52:32 +0100437 };
438
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000439 explicit InstanceofStub(Flags flags) : flags_(flags) { }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100440
441 static Register left();
442 static Register right();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100443
444 void Generate(MacroAssembler* masm);
445
446 private:
447 Major MajorKey() { return Instanceof; }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100448 int MinorKey() { return static_cast<int>(flags_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100449
Ben Murdoch086aeea2011-05-13 15:57:08 +0100450 bool HasArgsInRegisters() const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100451 return (flags_ & kArgsInRegisters) != 0;
452 }
453
Ben Murdoch086aeea2011-05-13 15:57:08 +0100454 bool HasCallSiteInlineCheck() const {
455 return (flags_ & kCallSiteInlineCheck) != 0;
456 }
457
458 bool ReturnTrueFalseObject() const {
459 return (flags_ & kReturnTrueFalseObject) != 0;
460 }
461
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000462 virtual void PrintName(StringStream* stream);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100463
Ben Murdochb0fe1622011-05-05 13:52:32 +0100464 Flags flags_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100465};
466
467
Ben Murdochb0fe1622011-05-05 13:52:32 +0100468class MathPowStub: public CodeStub {
469 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +0000470 enum ExponentType { INTEGER, DOUBLE, TAGGED, ON_STACK};
471
472 explicit MathPowStub(ExponentType exponent_type)
473 : exponent_type_(exponent_type) { }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100474 virtual void Generate(MacroAssembler* masm);
475
476 private:
477 virtual CodeStub::Major MajorKey() { return MathPow; }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000478 virtual int MinorKey() { return exponent_type_; }
479
480 ExponentType exponent_type_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100481};
482
483
Ben Murdochb0fe1622011-05-05 13:52:32 +0100484class ICCompareStub: public CodeStub {
485 public:
486 ICCompareStub(Token::Value op, CompareIC::State state)
487 : op_(op), state_(state) {
488 ASSERT(Token::IsCompareOp(op));
489 }
490
491 virtual void Generate(MacroAssembler* masm);
492
Ben Murdochc7cc0282012-03-05 14:35:55 +0000493 void set_known_map(Handle<Map> map) { known_map_ = map; }
494
Ben Murdochb0fe1622011-05-05 13:52:32 +0100495 private:
496 class OpField: public BitField<int, 0, 3> { };
497 class StateField: public BitField<int, 3, 5> { };
498
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000499 virtual void FinishCode(Handle<Code> code) {
500 code->set_compare_state(state_);
501 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100502
503 virtual CodeStub::Major MajorKey() { return CompareIC; }
504 virtual int MinorKey();
505
506 virtual int GetCodeKind() { return Code::COMPARE_IC; }
507
508 void GenerateSmis(MacroAssembler* masm);
509 void GenerateHeapNumbers(MacroAssembler* masm);
Ben Murdoch257744e2011-11-30 15:57:28 +0000510 void GenerateSymbols(MacroAssembler* masm);
511 void GenerateStrings(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100512 void GenerateObjects(MacroAssembler* masm);
513 void GenerateMiss(MacroAssembler* masm);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000514 void GenerateKnownObjects(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100515
516 bool strict() const { return op_ == Token::EQ_STRICT; }
517 Condition GetCondition() const { return CompareIC::ComputeCondition(op_); }
518
Ben Murdochc7cc0282012-03-05 14:35:55 +0000519 virtual void AddToSpecialCache(Handle<Code> new_object);
520 virtual bool FindCodeInSpecialCache(Code** code_out);
521 virtual bool UseSpecialCache() { return state_ == CompareIC::KNOWN_OBJECTS; }
522
Ben Murdochb0fe1622011-05-05 13:52:32 +0100523 Token::Value op_;
524 CompareIC::State state_;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000525 Handle<Map> known_map_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100526};
527
528
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100529// Flags that control the compare stub code generation.
530enum CompareFlags {
531 NO_COMPARE_FLAGS = 0,
532 NO_SMI_COMPARE_IN_STUB = 1 << 0,
533 NO_NUMBER_COMPARE_IN_STUB = 1 << 1,
534 CANT_BOTH_BE_NAN = 1 << 2
535};
536
537
Ben Murdochb0fe1622011-05-05 13:52:32 +0100538enum NaNInformation {
539 kBothCouldBeNaN,
540 kCantBothBeNaN
541};
542
543
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100544class CompareStub: public CodeStub {
545 public:
546 CompareStub(Condition cc,
547 bool strict,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100548 CompareFlags flags,
549 Register lhs,
550 Register rhs) :
Ben Murdochb0fe1622011-05-05 13:52:32 +0100551 cc_(cc),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100552 strict_(strict),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100553 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
554 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
555 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100556 lhs_(lhs),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000557 rhs_(rhs) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100558
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100559 CompareStub(Condition cc,
560 bool strict,
561 CompareFlags flags) :
562 cc_(cc),
563 strict_(strict),
564 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
565 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
566 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
567 lhs_(no_reg),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000568 rhs_(no_reg) { }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100569
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100570 void Generate(MacroAssembler* masm);
571
572 private:
573 Condition cc_;
574 bool strict_;
575 // Only used for 'equal' comparisons. Tells the stub that we already know
576 // that at least one side of the comparison is not NaN. This allows the
577 // stub to use object identity in the positive case. We ignore it when
578 // generating the minor key for other comparisons to avoid creating more
579 // stubs.
580 bool never_nan_nan_;
581 // Do generate the number comparison code in the stub. Stubs without number
582 // comparison code is used when the number comparison has been inlined, and
583 // the stub will be called if one of the operands is not a number.
584 bool include_number_compare_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100585
586 // Generate the comparison code for two smi operands in the stub.
587 bool include_smi_compare_;
588
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100589 // Register holding the left hand side of the comparison if the stub gives
590 // a choice, no_reg otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100591
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100592 Register lhs_;
593 // Register holding the right hand side of the comparison if the stub gives
594 // a choice, no_reg otherwise.
595 Register rhs_;
596
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100597 // Encoding of the minor key in 16 bits.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100598 class StrictField: public BitField<bool, 0, 1> {};
599 class NeverNanNanField: public BitField<bool, 1, 1> {};
600 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100601 class IncludeSmiCompareField: public BitField<bool, 3, 1> {};
602 class RegisterField: public BitField<bool, 4, 1> {};
603 class ConditionField: public BitField<int, 5, 11> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100604
605 Major MajorKey() { return Compare; }
606
607 int MinorKey();
608
Ben Murdochb0fe1622011-05-05 13:52:32 +0100609 virtual int GetCodeKind() { return Code::COMPARE_IC; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000610 virtual void FinishCode(Handle<Code> code) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100611 code->set_compare_state(CompareIC::GENERIC);
612 }
613
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100614 // Branch to the label if the given object isn't a symbol.
615 void BranchIfNonSymbol(MacroAssembler* masm,
616 Label* label,
617 Register object,
618 Register scratch);
619
620 // Unfortunately you have to run without snapshots to see most of these
621 // names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000622 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100623};
624
625
626class CEntryStub : public CodeStub {
627 public:
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000628 explicit CEntryStub(int result_size,
629 SaveFPRegsMode save_doubles = kDontSaveFPRegs)
630 : result_size_(result_size), save_doubles_(save_doubles) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100631
632 void Generate(MacroAssembler* masm);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000633
634 // The version of this stub that doesn't save doubles is generated ahead of
635 // time, so it's OK to call it from other stubs that can't cope with GC during
636 // their code generation. On machines that always have gp registers (x64) we
637 // can generate both variants ahead of time.
638 virtual bool IsPregenerated();
639 static void GenerateAheadOfTime();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100640
641 private:
642 void GenerateCore(MacroAssembler* masm,
643 Label* throw_normal_exception,
644 Label* throw_termination_exception,
645 Label* throw_out_of_memory_exception,
646 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +0100647 bool always_allocate_scope);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100648
649 // Number of pointers/values returned.
650 const int result_size_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000651 SaveFPRegsMode save_doubles_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100652
653 Major MajorKey() { return CEntry; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100654 int MinorKey();
655
Steve Block44f0eee2011-05-26 01:26:41 +0100656 bool NeedsImmovableCode();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100657};
658
659
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100660class JSEntryStub : public CodeStub {
661 public:
662 JSEntryStub() { }
663
664 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
665
666 protected:
667 void GenerateBody(MacroAssembler* masm, bool is_construct);
668
669 private:
670 Major MajorKey() { return JSEntry; }
671 int MinorKey() { return 0; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000672
673 virtual void FinishCode(Handle<Code> code);
674
675 int handler_offset_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100676};
677
678
679class JSConstructEntryStub : public JSEntryStub {
680 public:
681 JSConstructEntryStub() { }
682
683 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
684
685 private:
686 int MinorKey() { return 1; }
687
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000688 virtual void PrintName(StringStream* stream) {
689 stream->Add("JSConstructEntryStub");
690 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100691};
692
693
694class ArgumentsAccessStub: public CodeStub {
695 public:
696 enum Type {
697 READ_ELEMENT,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000698 NEW_NON_STRICT_FAST,
699 NEW_NON_STRICT_SLOW,
Steve Block44f0eee2011-05-26 01:26:41 +0100700 NEW_STRICT
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100701 };
702
703 explicit ArgumentsAccessStub(Type type) : type_(type) { }
704
705 private:
706 Type type_;
707
708 Major MajorKey() { return ArgumentsAccess; }
709 int MinorKey() { return type_; }
710
711 void Generate(MacroAssembler* masm);
712 void GenerateReadElement(MacroAssembler* masm);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000713 void GenerateNewStrict(MacroAssembler* masm);
714 void GenerateNewNonStrictFast(MacroAssembler* masm);
715 void GenerateNewNonStrictSlow(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100716
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000717 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100718};
719
720
721class RegExpExecStub: public CodeStub {
722 public:
723 RegExpExecStub() { }
724
725 private:
726 Major MajorKey() { return RegExpExec; }
727 int MinorKey() { return 0; }
728
729 void Generate(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100730};
731
732
Ben Murdochb0fe1622011-05-05 13:52:32 +0100733class RegExpConstructResultStub: public CodeStub {
734 public:
735 RegExpConstructResultStub() { }
736
737 private:
738 Major MajorKey() { return RegExpConstructResult; }
739 int MinorKey() { return 0; }
740
741 void Generate(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100742};
743
744
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100745class CallFunctionStub: public CodeStub {
746 public:
Ben Murdoch589d6972011-11-30 16:04:58 +0000747 CallFunctionStub(int argc, CallFunctionFlags flags)
748 : argc_(argc), flags_(flags) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100749
750 void Generate(MacroAssembler* masm);
751
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100752 virtual void FinishCode(Handle<Code> code) {
753 code->set_has_function_cache(RecordCallTarget());
754 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000755
Ben Murdoch086aeea2011-05-13 15:57:08 +0100756 static int ExtractArgcFromMinorKey(int minor_key) {
757 return ArgcBits::decode(minor_key);
758 }
759
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100760 private:
761 int argc_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100762 CallFunctionFlags flags_;
763
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000764 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100765
766 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000767 class FlagBits: public BitField<CallFunctionFlags, 0, 2> {};
768 class ArgcBits: public BitField<unsigned, 2, 32 - 2> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100769
770 Major MajorKey() { return CallFunction; }
771 int MinorKey() {
772 // Encode the parameters in a unique 32 bit value.
Ben Murdoch589d6972011-11-30 16:04:58 +0000773 return FlagBits::encode(flags_) | ArgcBits::encode(argc_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100774 }
775
Ben Murdoch257744e2011-11-30 15:57:28 +0000776 bool ReceiverMightBeImplicit() {
777 return (flags_ & RECEIVER_MIGHT_BE_IMPLICIT) != 0;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100778 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000779
780 bool RecordCallTarget() {
781 return (flags_ & RECORD_CALL_TARGET) != 0;
782 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100783};
784
785
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100786class CallConstructStub: public CodeStub {
787 public:
788 explicit CallConstructStub(CallFunctionFlags flags) : flags_(flags) {}
789
790 void Generate(MacroAssembler* masm);
791
792 virtual void FinishCode(Handle<Code> code) {
793 code->set_has_function_cache(RecordCallTarget());
794 }
795
796 private:
797 CallFunctionFlags flags_;
798
799 virtual void PrintName(StringStream* stream);
800
801 Major MajorKey() { return CallConstruct; }
802 int MinorKey() { return flags_; }
803
804 bool RecordCallTarget() {
805 return (flags_ & RECORD_CALL_TARGET) != 0;
806 }
807};
808
809
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100810enum StringIndexFlags {
811 // Accepts smis or heap numbers.
812 STRING_INDEX_IS_NUMBER,
813
814 // Accepts smis or heap numbers that are valid array indices
815 // (ECMA-262 15.4). Invalid indices are reported as being out of
816 // range.
817 STRING_INDEX_IS_ARRAY_INDEX
818};
819
820
821// Generates code implementing String.prototype.charCodeAt.
822//
823// Only supports the case when the receiver is a string and the index
824// is a number (smi or heap number) that is a valid index into the
825// string. Additional index constraints are specified by the
826// flags. Otherwise, bails out to the provided labels.
827//
828// Register usage: |object| may be changed to another string in a way
829// that doesn't affect charCodeAt/charAt semantics, |index| is
830// preserved, |scratch| and |result| are clobbered.
831class StringCharCodeAtGenerator {
832 public:
833 StringCharCodeAtGenerator(Register object,
834 Register index,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100835 Register result,
836 Label* receiver_not_string,
837 Label* index_not_number,
838 Label* index_out_of_range,
839 StringIndexFlags index_flags)
840 : object_(object),
841 index_(index),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100842 result_(result),
843 receiver_not_string_(receiver_not_string),
844 index_not_number_(index_not_number),
845 index_out_of_range_(index_out_of_range),
846 index_flags_(index_flags) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100847 ASSERT(!result_.is(object_));
848 ASSERT(!result_.is(index_));
849 }
850
851 // Generates the fast case code. On the fallthrough path |result|
852 // register contains the result.
853 void GenerateFast(MacroAssembler* masm);
854
855 // Generates the slow case code. Must not be naturally
856 // reachable. Expected to be put after a ret instruction (e.g., in
857 // deferred code). Always jumps back to the fast case.
858 void GenerateSlow(MacroAssembler* masm,
859 const RuntimeCallHelper& call_helper);
860
861 private:
862 Register object_;
863 Register index_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100864 Register result_;
865
866 Label* receiver_not_string_;
867 Label* index_not_number_;
868 Label* index_out_of_range_;
869
870 StringIndexFlags index_flags_;
871
872 Label call_runtime_;
873 Label index_not_smi_;
874 Label got_smi_index_;
875 Label exit_;
876
877 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
878};
879
880
881// Generates code for creating a one-char string from a char code.
882class StringCharFromCodeGenerator {
883 public:
884 StringCharFromCodeGenerator(Register code,
885 Register result)
886 : code_(code),
887 result_(result) {
888 ASSERT(!code_.is(result_));
889 }
890
891 // Generates the fast case code. On the fallthrough path |result|
892 // register contains the result.
893 void GenerateFast(MacroAssembler* masm);
894
895 // Generates the slow case code. Must not be naturally
896 // reachable. Expected to be put after a ret instruction (e.g., in
897 // deferred code). Always jumps back to the fast case.
898 void GenerateSlow(MacroAssembler* masm,
899 const RuntimeCallHelper& call_helper);
900
901 private:
902 Register code_;
903 Register result_;
904
905 Label slow_case_;
906 Label exit_;
907
908 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
909};
910
911
912// Generates code implementing String.prototype.charAt.
913//
914// Only supports the case when the receiver is a string and the index
915// is a number (smi or heap number) that is a valid index into the
916// string. Additional index constraints are specified by the
917// flags. Otherwise, bails out to the provided labels.
918//
919// Register usage: |object| may be changed to another string in a way
920// that doesn't affect charCodeAt/charAt semantics, |index| is
921// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
922class StringCharAtGenerator {
923 public:
924 StringCharAtGenerator(Register object,
925 Register index,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000926 Register scratch,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100927 Register result,
928 Label* receiver_not_string,
929 Label* index_not_number,
930 Label* index_out_of_range,
931 StringIndexFlags index_flags)
932 : char_code_at_generator_(object,
933 index,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000934 scratch,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100935 receiver_not_string,
936 index_not_number,
937 index_out_of_range,
938 index_flags),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000939 char_from_code_generator_(scratch, result) {}
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100940
941 // Generates the fast case code. On the fallthrough path |result|
942 // register contains the result.
943 void GenerateFast(MacroAssembler* masm);
944
945 // Generates the slow case code. Must not be naturally
946 // reachable. Expected to be put after a ret instruction (e.g., in
947 // deferred code). Always jumps back to the fast case.
948 void GenerateSlow(MacroAssembler* masm,
949 const RuntimeCallHelper& call_helper);
950
951 private:
952 StringCharCodeAtGenerator char_code_at_generator_;
953 StringCharFromCodeGenerator char_from_code_generator_;
954
955 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
956};
957
Ben Murdoch086aeea2011-05-13 15:57:08 +0100958
959class AllowStubCallsScope {
960 public:
961 AllowStubCallsScope(MacroAssembler* masm, bool allow)
962 : masm_(masm), previous_allow_(masm->allow_stub_calls()) {
963 masm_->set_allow_stub_calls(allow);
964 }
965 ~AllowStubCallsScope() {
966 masm_->set_allow_stub_calls(previous_allow_);
967 }
968
969 private:
970 MacroAssembler* masm_;
971 bool previous_allow_;
972
973 DISALLOW_COPY_AND_ASSIGN(AllowStubCallsScope);
974};
975
Ben Murdoch257744e2011-11-30 15:57:28 +0000976
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000977class KeyedLoadElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000978 public:
Ben Murdoch589d6972011-11-30 16:04:58 +0000979 explicit KeyedLoadElementStub(ElementsKind elements_kind)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000980 : elements_kind_(elements_kind)
981 { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000982
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000983 Major MajorKey() { return KeyedLoadElement; }
984 int MinorKey() { return elements_kind_; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000985
986 void Generate(MacroAssembler* masm);
987
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000988 private:
Ben Murdoch589d6972011-11-30 16:04:58 +0000989 ElementsKind elements_kind_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000990
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000991 DISALLOW_COPY_AND_ASSIGN(KeyedLoadElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +0000992};
993
994
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000995class KeyedStoreElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000996 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000997 KeyedStoreElementStub(bool is_js_array,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100998 ElementsKind elements_kind,
999 KeyedAccessGrowMode grow_mode)
1000 : is_js_array_(is_js_array),
1001 elements_kind_(elements_kind),
1002 grow_mode_(grow_mode) { }
Ben Murdoch257744e2011-11-30 15:57:28 +00001003
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001004 Major MajorKey() { return KeyedStoreElement; }
1005 int MinorKey() {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001006 return ElementsKindBits::encode(elements_kind_) |
1007 IsJSArrayBits::encode(is_js_array_) |
1008 GrowModeBits::encode(grow_mode_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001009 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001010
1011 void Generate(MacroAssembler* masm);
1012
Ben Murdoch257744e2011-11-30 15:57:28 +00001013 private:
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001014 class ElementsKindBits: public BitField<ElementsKind, 0, 8> {};
1015 class GrowModeBits: public BitField<KeyedAccessGrowMode, 8, 1> {};
1016 class IsJSArrayBits: public BitField<bool, 9, 1> {};
1017
Ben Murdoch257744e2011-11-30 15:57:28 +00001018 bool is_js_array_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001019 ElementsKind elements_kind_;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001020 KeyedAccessGrowMode grow_mode_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001021
1022 DISALLOW_COPY_AND_ASSIGN(KeyedStoreElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +00001023};
1024
1025
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001026class ToBooleanStub: public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +00001027 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001028 enum Type {
1029 UNDEFINED,
1030 BOOLEAN,
1031 NULL_TYPE,
1032 SMI,
1033 SPEC_OBJECT,
1034 STRING,
1035 HEAP_NUMBER,
1036 NUMBER_OF_TYPES
1037 };
1038
1039 // At most 8 different types can be distinguished, because the Code object
1040 // only has room for a single byte to hold a set of these types. :-P
1041 STATIC_ASSERT(NUMBER_OF_TYPES <= 8);
1042
1043 class Types {
1044 public:
1045 Types() {}
1046 explicit Types(byte bits) : set_(bits) {}
1047
1048 bool IsEmpty() const { return set_.IsEmpty(); }
1049 bool Contains(Type type) const { return set_.Contains(type); }
1050 void Add(Type type) { set_.Add(type); }
1051 byte ToByte() const { return set_.ToIntegral(); }
1052 void Print(StringStream* stream) const;
1053 void TraceTransition(Types to) const;
1054 bool Record(Handle<Object> object);
1055 bool NeedsMap() const;
1056 bool CanBeUndetectable() const;
1057
1058 private:
1059 EnumSet<Type, byte> set_;
1060 };
1061
1062 static Types no_types() { return Types(); }
1063 static Types all_types() { return Types((1 << NUMBER_OF_TYPES) - 1); }
1064
1065 explicit ToBooleanStub(Register tos, Types types = Types())
1066 : tos_(tos), types_(types) { }
Ben Murdoch257744e2011-11-30 15:57:28 +00001067
1068 void Generate(MacroAssembler* masm);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001069 virtual int GetCodeKind() { return Code::TO_BOOLEAN_IC; }
1070 virtual void PrintName(StringStream* stream);
Ben Murdoch257744e2011-11-30 15:57:28 +00001071
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001072 virtual bool SometimesSetsUpAFrame() { return false; }
1073
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001074 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001075 Major MajorKey() { return ToBoolean; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001076 int MinorKey() { return (tos_.code() << NUMBER_OF_TYPES) | types_.ToByte(); }
1077
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001078 virtual void FinishCode(Handle<Code> code) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001079 code->set_to_boolean_state(types_.ToByte());
1080 }
1081
1082 void CheckOddball(MacroAssembler* masm,
1083 Type type,
1084 Heap::RootListIndex value,
1085 bool result);
1086 void GenerateTypeTransition(MacroAssembler* masm);
1087
1088 Register tos_;
1089 Types types_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001090};
1091
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001092
1093class ElementsTransitionAndStoreStub : public CodeStub {
1094 public:
1095 ElementsTransitionAndStoreStub(ElementsKind from,
1096 ElementsKind to,
1097 bool is_jsarray,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001098 StrictModeFlag strict_mode,
1099 KeyedAccessGrowMode grow_mode)
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001100 : from_(from),
1101 to_(to),
1102 is_jsarray_(is_jsarray),
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001103 strict_mode_(strict_mode),
1104 grow_mode_(grow_mode) {}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001105
1106 private:
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001107 class FromBits: public BitField<ElementsKind, 0, 8> {};
1108 class ToBits: public BitField<ElementsKind, 8, 8> {};
1109 class IsJSArrayBits: public BitField<bool, 16, 1> {};
1110 class StrictModeBits: public BitField<StrictModeFlag, 17, 1> {};
1111 class GrowModeBits: public BitField<KeyedAccessGrowMode, 18, 1> {};
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001112
1113 Major MajorKey() { return ElementsTransitionAndStore; }
1114 int MinorKey() {
1115 return FromBits::encode(from_) |
1116 ToBits::encode(to_) |
1117 IsJSArrayBits::encode(is_jsarray_) |
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001118 StrictModeBits::encode(strict_mode_) |
1119 GrowModeBits::encode(grow_mode_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001120 }
1121
1122 void Generate(MacroAssembler* masm);
1123
1124 ElementsKind from_;
1125 ElementsKind to_;
1126 bool is_jsarray_;
1127 StrictModeFlag strict_mode_;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001128 KeyedAccessGrowMode grow_mode_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001129
1130 DISALLOW_COPY_AND_ASSIGN(ElementsTransitionAndStoreStub);
1131};
1132
1133
1134class StoreArrayLiteralElementStub : public CodeStub {
1135 public:
1136 explicit StoreArrayLiteralElementStub() {}
1137
1138 private:
1139 Major MajorKey() { return StoreArrayLiteralElement; }
1140 int MinorKey() { return 0; }
1141
1142 void Generate(MacroAssembler* masm);
1143
1144 DISALLOW_COPY_AND_ASSIGN(StoreArrayLiteralElementStub);
1145};
1146
Steve Blocka7e24c12009-10-30 11:49:00 +00001147} } // namespace v8::internal
1148
1149#endif // V8_CODE_STUBS_H_