blob: 89e99a8d085709489004ec8d56fa211614babd69 [file] [log] [blame]
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000032#include "globals.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ricow@chromium.orgc54d3652011-05-30 09:20:16 +000037// List of code stubs used on all platforms.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000038#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
39 V(CallFunction) \
danno@chromium.org40cb8782011-05-25 07:58:50 +000040 V(UnaryOp) \
41 V(BinaryOp) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000042 V(StringAdd) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000043 V(SubString) \
44 V(StringCompare) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +000045 V(Compare) \
kasperl@chromium.orga5551262010-12-07 12:49:48 +000046 V(CompareIC) \
47 V(MathPow) \
whesse@chromium.org871db8c2010-12-21 15:17:57 +000048 V(TranscendentalCache) \
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +000049 V(Instanceof) \
ricow@chromium.orgc54d3652011-05-30 09:20:16 +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. */ \
ager@chromium.orgc4c92722009-11-18 14:12:51 +000057 V(ConvertToDouble) \
58 V(WriteInt32ToHeapNumber) \
59 V(StackCheck) \
sgjesse@chromium.org846fb742009-12-18 08:56:33 +000060 V(FastNewClosure) \
61 V(FastNewContext) \
62 V(FastCloneShallowArray) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +000063 V(RevertToNumber) \
64 V(ToBoolean) \
whesse@chromium.org7a392b32011-01-31 11:30:36 +000065 V(ToNumber) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +000066 V(CounterOp) \
67 V(ArgumentsAccess) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000068 V(RegExpExec) \
kasperl@chromium.orga5551262010-12-07 12:49:48 +000069 V(RegExpConstructResult) \
ager@chromium.org5c838252010-02-19 08:53:10 +000070 V(NumberToString) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +000071 V(CEntry) \
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000072 V(JSEntry) \
sgjesse@chromium.org6db88712011-07-11 11:41:22 +000073 V(KeyedLoadElement) \
74 V(KeyedStoreElement) \
lrn@chromium.org1c092762011-05-09 09:42:16 +000075 V(DebuggerStatement) \
76 V(StringDictionaryNegativeLookup)
ager@chromium.org3811b432009-10-28 14:53:37 +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) \
ricow@chromium.org83aa5492011-02-07 12:42:56 +000084 V(RegExpCEntry) \
85 V(DirectCEntry)
ager@chromium.org3811b432009-10-28 14:53:37 +000086#else
87#define CODE_STUB_LIST_ARM(V)
88#endif
89
lrn@chromium.org7516f052011-03-30 08:52:27 +000090// List of code stubs only used on MIPS platforms.
91#ifdef V8_TARGET_ARCH_MIPS
92#define CODE_STUB_LIST_MIPS(V) \
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000093 V(RegExpCEntry) \
94 V(DirectCEntry)
lrn@chromium.org7516f052011-03-30 08:52:27 +000095#else
96#define CODE_STUB_LIST_MIPS(V)
97#endif
98
ager@chromium.org3811b432009-10-28 14:53:37 +000099// Combined list of code stubs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000100#define CODE_STUB_LIST(V) \
101 CODE_STUB_LIST_ALL_PLATFORMS(V) \
lrn@chromium.org7516f052011-03-30 08:52:27 +0000102 CODE_STUB_LIST_ARM(V) \
103 CODE_STUB_LIST_MIPS(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000105// Mode to overwrite BinaryExpression values.
106enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
107enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE };
108
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110// Stub is base classes of all stubs.
111class CodeStub BASE_EMBEDDED {
112 public:
113 enum Major {
ager@chromium.org3811b432009-10-28 14:53:37 +0000114#define DEF_ENUM(name) name,
115 CODE_STUB_LIST(DEF_ENUM)
116#undef DEF_ENUM
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000117 NoCache, // marker for stubs that do custom caching
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 NUMBER_OF_IDS
119 };
120
121 // Retrieve the code for the stub. Generate the code if needed.
122 Handle<Code> GetCode();
123
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +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.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000127 MUST_USE_RESULT MaybeObject* TryGetCode();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 static Major MajorKeyFromKey(uint32_t key) {
130 return static_cast<Major>(MajorKeyBits::decode(key));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000131 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 static int MinorKeyFromKey(uint32_t key) {
133 return MinorKeyBits::decode(key);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000134 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000135
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
ager@chromium.org5c838252010-02-19 08:53:10 +0000141 static const char* MajorName(Major major_key, bool allow_unknown_keys);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
143 virtual ~CodeStub() {}
144
mads.s.ager31e71382008-08-13 09:32:07 +0000145 protected:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000146 static const int kMajorBits = 6;
ager@chromium.org9085a012009-05-11 19:22:57 +0000147 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
mads.s.ager31e71382008-08-13 09:32:07 +0000148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 private:
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 // Generates the assembler code for the stub.
158 virtual void Generate(MacroAssembler* masm) = 0;
159
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000160 // Perform bookkeeping required after code generation when stub code is
161 // initially generated.
162 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
163
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000164 // Finish the code object after it has been generated.
165 virtual void FinishCode(Code* code) { }
166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // Returns information for computing the number key.
168 virtual Major MajorKey() = 0;
169 virtual int MinorKey() = 0;
170
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000171 // The CallFunctionStub needs to override this so it can encode whether a
172 // lazily generated function should be fully optimized or not.
173 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
174
danno@chromium.org40cb8782011-05-25 07:58:50 +0000175 // BinaryOpStub needs to override this.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000176 virtual int GetCodeKind();
177
danno@chromium.org40cb8782011-05-25 07:58:50 +0000178 // BinaryOpStub needs to override this.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000179 virtual InlineCacheState GetICState() {
180 return UNINITIALIZED;
181 }
182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 // Returns a name for logging/debugging purposes.
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000184 SmartPointer<const char> GetName();
185 virtual void PrintName(StringStream* stream) {
186 stream->Add("%s", MajorName(MajorKey(), false));
187 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000189 // Returns whether the code generated for this stub needs to be allocated as
190 // a fixed (non-moveable) code object.
191 virtual bool NeedsImmovableCode() { return false; }
192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 // Computes the key based on major and minor.
194 uint32_t GetKey() {
195 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
196 return MinorKeyBits::encode(MinorKey()) |
197 MajorKeyBits::encode(MajorKey());
198 }
199
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000200 // See comment above, where Instanceof is defined.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000201 bool AllowsStubCalls() { return MajorKey() <= Instanceof; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
204 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
205
206 friend class BreakPointIterator;
207};
208
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000209
210// Helper interface to prepare to/restore after making runtime calls.
211class RuntimeCallHelper {
212 public:
213 virtual ~RuntimeCallHelper() {}
214
215 virtual void BeforeCall(MacroAssembler* masm) const = 0;
216
217 virtual void AfterCall(MacroAssembler* masm) const = 0;
218
219 protected:
220 RuntimeCallHelper() {}
221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
224};
225
226} } // namespace v8::internal
227
228#if V8_TARGET_ARCH_IA32
229#include "ia32/code-stubs-ia32.h"
230#elif V8_TARGET_ARCH_X64
231#include "x64/code-stubs-x64.h"
232#elif V8_TARGET_ARCH_ARM
233#include "arm/code-stubs-arm.h"
234#elif V8_TARGET_ARCH_MIPS
235#include "mips/code-stubs-mips.h"
236#else
237#error Unsupported target architecture.
238#endif
239
240namespace v8 {
241namespace internal {
242
243
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244// RuntimeCallHelper implementation used in stubs: enters/leaves a
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000245// newly created internal frame before/after the runtime call.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000246class StubRuntimeCallHelper : public RuntimeCallHelper {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000247 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000248 StubRuntimeCallHelper() {}
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000249
250 virtual void BeforeCall(MacroAssembler* masm) const;
251
252 virtual void AfterCall(MacroAssembler* masm) const;
253};
254
255
256// Trivial RuntimeCallHelper implementation.
257class NopRuntimeCallHelper : public RuntimeCallHelper {
258 public:
259 NopRuntimeCallHelper() {}
260
261 virtual void BeforeCall(MacroAssembler* masm) const {}
262
263 virtual void AfterCall(MacroAssembler* masm) const {}
264};
265
266
267class StackCheckStub : public CodeStub {
268 public:
269 StackCheckStub() { }
270
271 void Generate(MacroAssembler* masm);
272
273 private:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000274 Major MajorKey() { return StackCheck; }
275 int MinorKey() { return 0; }
276};
277
278
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000279class ToNumberStub: public CodeStub {
280 public:
281 ToNumberStub() { }
282
283 void Generate(MacroAssembler* masm);
284
285 private:
286 Major MajorKey() { return ToNumber; }
287 int MinorKey() { return 0; }
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000288};
289
290
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000291class FastNewClosureStub : public CodeStub {
292 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 explicit FastNewClosureStub(StrictModeFlag strict_mode)
294 : strict_mode_(strict_mode) { }
295
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000296 void Generate(MacroAssembler* masm);
297
298 private:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000299 Major MajorKey() { return FastNewClosure; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 int MinorKey() { return strict_mode_; }
301
302 StrictModeFlag strict_mode_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000303};
304
305
306class FastNewContextStub : public CodeStub {
307 public:
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000308 static const int kMaximumSlots = 64;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000309
310 explicit FastNewContextStub(int slots) : slots_(slots) {
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000311 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000312 }
313
314 void Generate(MacroAssembler* masm);
315
316 private:
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000317 int slots_;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000318
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000319 Major MajorKey() { return FastNewContext; }
320 int MinorKey() { return slots_; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000321};
322
323
324class FastCloneShallowArrayStub : public CodeStub {
325 public:
326 // Maximum length of copied elements array.
327 static const int kMaximumClonedLength = 8;
328
329 enum Mode {
330 CLONE_ELEMENTS,
331 COPY_ON_WRITE_ELEMENTS
332 };
333
334 FastCloneShallowArrayStub(Mode mode, int length)
335 : mode_(mode),
336 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
337 ASSERT(length_ >= 0);
338 ASSERT(length_ <= kMaximumClonedLength);
339 }
340
341 void Generate(MacroAssembler* masm);
342
343 private:
344 Mode mode_;
345 int length_;
346
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000347 Major MajorKey() { return FastCloneShallowArray; }
348 int MinorKey() {
349 ASSERT(mode_ == 0 || mode_ == 1);
350 return (length_ << 1) | mode_;
351 }
352};
353
354
355class InstanceofStub: public CodeStub {
356 public:
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000357 enum Flags {
358 kNoFlags = 0,
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000359 kArgsInRegisters = 1 << 0,
360 kCallSiteInlineCheck = 1 << 1,
361 kReturnTrueFalseObject = 1 << 2
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000362 };
363
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000364 explicit InstanceofStub(Flags flags) : flags_(flags) { }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000365
366 static Register left();
367 static Register right();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000368
369 void Generate(MacroAssembler* masm);
370
371 private:
372 Major MajorKey() { return Instanceof; }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000373 int MinorKey() { return static_cast<int>(flags_); }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000374
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000375 bool HasArgsInRegisters() const {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000376 return (flags_ & kArgsInRegisters) != 0;
377 }
378
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000379 bool HasCallSiteInlineCheck() const {
380 return (flags_ & kCallSiteInlineCheck) != 0;
381 }
382
383 bool ReturnTrueFalseObject() const {
384 return (flags_ & kReturnTrueFalseObject) != 0;
385 }
386
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000387 virtual void PrintName(StringStream* stream);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000388
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000389 Flags flags_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000390};
391
392
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000393class MathPowStub: public CodeStub {
394 public:
395 MathPowStub() {}
396 virtual void Generate(MacroAssembler* masm);
397
398 private:
399 virtual CodeStub::Major MajorKey() { return MathPow; }
400 virtual int MinorKey() { return 0; }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000401};
402
403
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000404class ICCompareStub: public CodeStub {
405 public:
406 ICCompareStub(Token::Value op, CompareIC::State state)
407 : op_(op), state_(state) {
408 ASSERT(Token::IsCompareOp(op));
409 }
410
411 virtual void Generate(MacroAssembler* masm);
412
413 private:
414 class OpField: public BitField<int, 0, 3> { };
415 class StateField: public BitField<int, 3, 5> { };
416
417 virtual void FinishCode(Code* code) { code->set_compare_state(state_); }
418
419 virtual CodeStub::Major MajorKey() { return CompareIC; }
420 virtual int MinorKey();
421
422 virtual int GetCodeKind() { return Code::COMPARE_IC; }
423
424 void GenerateSmis(MacroAssembler* masm);
425 void GenerateHeapNumbers(MacroAssembler* masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000426 void GenerateSymbols(MacroAssembler* masm);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000427 void GenerateStrings(MacroAssembler* masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000428 void GenerateObjects(MacroAssembler* masm);
429 void GenerateMiss(MacroAssembler* masm);
430
431 bool strict() const { return op_ == Token::EQ_STRICT; }
432 Condition GetCondition() const { return CompareIC::ComputeCondition(op_); }
433
434 Token::Value op_;
435 CompareIC::State state_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000436};
437
438
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000439// Flags that control the compare stub code generation.
440enum CompareFlags {
441 NO_COMPARE_FLAGS = 0,
442 NO_SMI_COMPARE_IN_STUB = 1 << 0,
443 NO_NUMBER_COMPARE_IN_STUB = 1 << 1,
444 CANT_BOTH_BE_NAN = 1 << 2
445};
446
447
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000448enum NaNInformation {
449 kBothCouldBeNaN,
450 kCantBothBeNaN
451};
452
453
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000454class CompareStub: public CodeStub {
455 public:
456 CompareStub(Condition cc,
457 bool strict,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000458 CompareFlags flags,
459 Register lhs,
460 Register rhs) :
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461 cc_(cc),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000462 strict_(strict),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000463 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
464 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
465 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000466 lhs_(lhs),
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000467 rhs_(rhs) { }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000468
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000469 CompareStub(Condition cc,
470 bool strict,
471 CompareFlags flags) :
472 cc_(cc),
473 strict_(strict),
474 never_nan_nan_((flags & CANT_BOTH_BE_NAN) != 0),
475 include_number_compare_((flags & NO_NUMBER_COMPARE_IN_STUB) == 0),
476 include_smi_compare_((flags & NO_SMI_COMPARE_IN_STUB) == 0),
477 lhs_(no_reg),
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000478 rhs_(no_reg) { }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000479
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000480 void Generate(MacroAssembler* masm);
481
482 private:
483 Condition cc_;
484 bool strict_;
485 // Only used for 'equal' comparisons. Tells the stub that we already know
486 // that at least one side of the comparison is not NaN. This allows the
487 // stub to use object identity in the positive case. We ignore it when
488 // generating the minor key for other comparisons to avoid creating more
489 // stubs.
490 bool never_nan_nan_;
491 // Do generate the number comparison code in the stub. Stubs without number
492 // comparison code is used when the number comparison has been inlined, and
493 // the stub will be called if one of the operands is not a number.
494 bool include_number_compare_;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000495
496 // Generate the comparison code for two smi operands in the stub.
497 bool include_smi_compare_;
498
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000499 // Register holding the left hand side of the comparison if the stub gives
500 // a choice, no_reg otherwise.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000501
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000502 Register lhs_;
503 // Register holding the right hand side of the comparison if the stub gives
504 // a choice, no_reg otherwise.
505 Register rhs_;
506
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000507 // Encoding of the minor key in 16 bits.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000508 class StrictField: public BitField<bool, 0, 1> {};
509 class NeverNanNanField: public BitField<bool, 1, 1> {};
510 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000511 class IncludeSmiCompareField: public BitField<bool, 3, 1> {};
512 class RegisterField: public BitField<bool, 4, 1> {};
513 class ConditionField: public BitField<int, 5, 11> {};
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000514
515 Major MajorKey() { return Compare; }
516
517 int MinorKey();
518
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000519 virtual int GetCodeKind() { return Code::COMPARE_IC; }
520 virtual void FinishCode(Code* code) {
521 code->set_compare_state(CompareIC::GENERIC);
522 }
523
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000524 // Branch to the label if the given object isn't a symbol.
525 void BranchIfNonSymbol(MacroAssembler* masm,
526 Label* label,
527 Register object,
528 Register scratch);
529
530 // Unfortunately you have to run without snapshots to see most of these
531 // names in the profile since most compare stubs end up in the snapshot.
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000532 virtual void PrintName(StringStream* stream);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000533};
534
535
536class CEntryStub : public CodeStub {
537 public:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000538 explicit CEntryStub(int result_size)
539 : result_size_(result_size), save_doubles_(false) { }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000540
541 void Generate(MacroAssembler* masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000542 void SaveDoubles() { save_doubles_ = true; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000543
544 private:
545 void GenerateCore(MacroAssembler* masm,
546 Label* throw_normal_exception,
547 Label* throw_termination_exception,
548 Label* throw_out_of_memory_exception,
549 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000550 bool always_allocate_scope);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000551 void GenerateThrowTOS(MacroAssembler* masm);
552 void GenerateThrowUncatchable(MacroAssembler* masm,
553 UncatchableExceptionType type);
554
555 // Number of pointers/values returned.
556 const int result_size_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000557 bool save_doubles_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000558
559 Major MajorKey() { return CEntry; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000560 int MinorKey();
561
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000562 bool NeedsImmovableCode();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000563};
564
565
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000566class JSEntryStub : public CodeStub {
567 public:
568 JSEntryStub() { }
569
570 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
571
572 protected:
573 void GenerateBody(MacroAssembler* masm, bool is_construct);
574
575 private:
576 Major MajorKey() { return JSEntry; }
577 int MinorKey() { return 0; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000578};
579
580
581class JSConstructEntryStub : public JSEntryStub {
582 public:
583 JSConstructEntryStub() { }
584
585 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
586
587 private:
588 int MinorKey() { return 1; }
589
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000590 virtual void PrintName(StringStream* stream) {
591 stream->Add("JSConstructEntryStub");
592 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000593};
594
595
596class ArgumentsAccessStub: public CodeStub {
597 public:
598 enum Type {
599 READ_ELEMENT,
whesse@chromium.org7b260152011-06-20 15:33:18 +0000600 NEW_NON_STRICT_FAST,
601 NEW_NON_STRICT_SLOW,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000602 NEW_STRICT
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000603 };
604
605 explicit ArgumentsAccessStub(Type type) : type_(type) { }
606
607 private:
608 Type type_;
609
610 Major MajorKey() { return ArgumentsAccess; }
611 int MinorKey() { return type_; }
612
613 void Generate(MacroAssembler* masm);
614 void GenerateReadElement(MacroAssembler* masm);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000615 void GenerateNewStrict(MacroAssembler* masm);
616 void GenerateNewNonStrictFast(MacroAssembler* masm);
617 void GenerateNewNonStrictSlow(MacroAssembler* masm);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000619 virtual void PrintName(StringStream* stream);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000620};
621
622
623class RegExpExecStub: public CodeStub {
624 public:
625 RegExpExecStub() { }
626
627 private:
628 Major MajorKey() { return RegExpExec; }
629 int MinorKey() { return 0; }
630
631 void Generate(MacroAssembler* masm);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000632};
633
634
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000635class RegExpConstructResultStub: public CodeStub {
636 public:
637 RegExpConstructResultStub() { }
638
639 private:
640 Major MajorKey() { return RegExpConstructResult; }
641 int MinorKey() { return 0; }
642
643 void Generate(MacroAssembler* masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644};
645
646
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000647class CallFunctionStub: public CodeStub {
648 public:
649 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
650 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
651
652 void Generate(MacroAssembler* masm);
653
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000654 static int ExtractArgcFromMinorKey(int minor_key) {
655 return ArgcBits::decode(minor_key);
656 }
657
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000658 private:
659 int argc_;
660 InLoopFlag in_loop_;
661 CallFunctionFlags flags_;
662
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000663 virtual void PrintName(StringStream* stream);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000664
665 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
666 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
667 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
668 class ArgcBits: public BitField<int, 2, 32 - 2> {};
669
670 Major MajorKey() { return CallFunction; }
671 int MinorKey() {
672 // Encode the parameters in a unique 32 bit value.
673 return InLoopBits::encode(in_loop_)
674 | FlagBits::encode(flags_)
675 | ArgcBits::encode(argc_);
676 }
677
678 InLoopFlag InLoop() { return in_loop_; }
danno@chromium.org40cb8782011-05-25 07:58:50 +0000679
680 bool ReceiverMightBeImplicit() {
681 return (flags_ & RECEIVER_MIGHT_BE_IMPLICIT) != 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000682 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000683};
684
685
686enum StringIndexFlags {
687 // Accepts smis or heap numbers.
688 STRING_INDEX_IS_NUMBER,
689
690 // Accepts smis or heap numbers that are valid array indices
691 // (ECMA-262 15.4). Invalid indices are reported as being out of
692 // range.
693 STRING_INDEX_IS_ARRAY_INDEX
694};
695
696
697// Generates code implementing String.prototype.charCodeAt.
698//
699// Only supports the case when the receiver is a string and the index
700// is a number (smi or heap number) that is a valid index into the
701// string. Additional index constraints are specified by the
702// flags. Otherwise, bails out to the provided labels.
703//
704// Register usage: |object| may be changed to another string in a way
705// that doesn't affect charCodeAt/charAt semantics, |index| is
706// preserved, |scratch| and |result| are clobbered.
707class StringCharCodeAtGenerator {
708 public:
709 StringCharCodeAtGenerator(Register object,
710 Register index,
711 Register scratch,
712 Register result,
713 Label* receiver_not_string,
714 Label* index_not_number,
715 Label* index_out_of_range,
716 StringIndexFlags index_flags)
717 : object_(object),
718 index_(index),
719 scratch_(scratch),
720 result_(result),
721 receiver_not_string_(receiver_not_string),
722 index_not_number_(index_not_number),
723 index_out_of_range_(index_out_of_range),
724 index_flags_(index_flags) {
725 ASSERT(!scratch_.is(object_));
726 ASSERT(!scratch_.is(index_));
727 ASSERT(!scratch_.is(result_));
728 ASSERT(!result_.is(object_));
729 ASSERT(!result_.is(index_));
730 }
731
732 // Generates the fast case code. On the fallthrough path |result|
733 // register contains the result.
734 void GenerateFast(MacroAssembler* masm);
735
736 // Generates the slow case code. Must not be naturally
737 // reachable. Expected to be put after a ret instruction (e.g., in
738 // deferred code). Always jumps back to the fast case.
739 void GenerateSlow(MacroAssembler* masm,
740 const RuntimeCallHelper& call_helper);
741
742 private:
743 Register object_;
744 Register index_;
745 Register scratch_;
746 Register result_;
747
748 Label* receiver_not_string_;
749 Label* index_not_number_;
750 Label* index_out_of_range_;
751
752 StringIndexFlags index_flags_;
753
754 Label call_runtime_;
755 Label index_not_smi_;
756 Label got_smi_index_;
757 Label exit_;
758
759 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
760};
761
762
763// Generates code for creating a one-char string from a char code.
764class StringCharFromCodeGenerator {
765 public:
766 StringCharFromCodeGenerator(Register code,
767 Register result)
768 : code_(code),
769 result_(result) {
770 ASSERT(!code_.is(result_));
771 }
772
773 // Generates the fast case code. On the fallthrough path |result|
774 // register contains the result.
775 void GenerateFast(MacroAssembler* masm);
776
777 // Generates the slow case code. Must not be naturally
778 // reachable. Expected to be put after a ret instruction (e.g., in
779 // deferred code). Always jumps back to the fast case.
780 void GenerateSlow(MacroAssembler* masm,
781 const RuntimeCallHelper& call_helper);
782
783 private:
784 Register code_;
785 Register result_;
786
787 Label slow_case_;
788 Label exit_;
789
790 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
791};
792
793
794// Generates code implementing String.prototype.charAt.
795//
796// Only supports the case when the receiver is a string and the index
797// is a number (smi or heap number) that is a valid index into the
798// string. Additional index constraints are specified by the
799// flags. Otherwise, bails out to the provided labels.
800//
801// Register usage: |object| may be changed to another string in a way
802// that doesn't affect charCodeAt/charAt semantics, |index| is
803// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
804class StringCharAtGenerator {
805 public:
806 StringCharAtGenerator(Register object,
807 Register index,
808 Register scratch1,
809 Register scratch2,
810 Register result,
811 Label* receiver_not_string,
812 Label* index_not_number,
813 Label* index_out_of_range,
814 StringIndexFlags index_flags)
815 : char_code_at_generator_(object,
816 index,
817 scratch1,
818 scratch2,
819 receiver_not_string,
820 index_not_number,
821 index_out_of_range,
822 index_flags),
823 char_from_code_generator_(scratch2, result) {}
824
825 // Generates the fast case code. On the fallthrough path |result|
826 // register contains the result.
827 void GenerateFast(MacroAssembler* masm);
828
829 // Generates the slow case code. Must not be naturally
830 // reachable. Expected to be put after a ret instruction (e.g., in
831 // deferred code). Always jumps back to the fast case.
832 void GenerateSlow(MacroAssembler* masm,
833 const RuntimeCallHelper& call_helper);
834
835 private:
836 StringCharCodeAtGenerator char_code_at_generator_;
837 StringCharFromCodeGenerator char_from_code_generator_;
838
839 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
840};
841
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000842
843class AllowStubCallsScope {
844 public:
845 AllowStubCallsScope(MacroAssembler* masm, bool allow)
846 : masm_(masm), previous_allow_(masm->allow_stub_calls()) {
847 masm_->set_allow_stub_calls(allow);
848 }
849 ~AllowStubCallsScope() {
850 masm_->set_allow_stub_calls(previous_allow_);
851 }
852
853 private:
854 MacroAssembler* masm_;
855 bool previous_allow_;
856
857 DISALLOW_COPY_AND_ASSIGN(AllowStubCallsScope);
858};
859
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000860
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000861class KeyedLoadElementStub : public CodeStub {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000862 public:
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000863 explicit KeyedLoadElementStub(JSObject::ElementsKind elements_kind)
864 : elements_kind_(elements_kind)
865 { }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000866
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000867 Major MajorKey() { return KeyedLoadElement; }
868 int MinorKey() { return elements_kind_; }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000869
870 void Generate(MacroAssembler* masm);
871
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000872 private:
873 JSObject::ElementsKind elements_kind_;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000874
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000875 DISALLOW_COPY_AND_ASSIGN(KeyedLoadElementStub);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000876};
877
878
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000879class KeyedStoreElementStub : public CodeStub {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000880 public:
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000881 KeyedStoreElementStub(bool is_js_array,
882 JSObject::ElementsKind elements_kind)
883 : is_js_array_(is_js_array),
884 elements_kind_(elements_kind) { }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000885
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000886 Major MajorKey() { return KeyedStoreElement; }
887 int MinorKey() {
888 return (is_js_array_ ? 0 : JSObject::kElementsKindCount) + elements_kind_;
889 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000890
891 void Generate(MacroAssembler* masm);
892
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000893 private:
894 bool is_js_array_;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000895 JSObject::ElementsKind elements_kind_;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000896
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000897 DISALLOW_COPY_AND_ASSIGN(KeyedStoreElementStub);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000898};
899
900
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000901class ToBooleanStub: public CodeStub {
902 public:
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000903 enum Type {
904 UNDEFINED,
905 BOOLEAN,
906 NULL_TYPE,
907 SMI,
908 SPEC_OBJECT,
909 STRING,
910 HEAP_NUMBER,
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000911 NUMBER_OF_TYPES
912 };
913
914 // At most 8 different types can be distinguished, because the Code object
915 // only has room for a single byte to hold a set of these types. :-P
916 STATIC_ASSERT(NUMBER_OF_TYPES <= 8);
917
918 class Types {
919 public:
920 Types() {}
921 explicit Types(byte bits) : set_(bits) {}
922
923 bool IsEmpty() const { return set_.IsEmpty(); }
924 bool Contains(Type type) const { return set_.Contains(type); }
925 void Add(Type type) { set_.Add(type); }
926 byte ToByte() const { return set_.ToIntegral(); }
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000927 void Print(StringStream* stream) const;
928 void TraceTransition(Types to) const;
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000929 bool Record(Handle<Object> object);
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000930 bool NeedsMap() const;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000931 bool CanBeUndetectable() const;
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000932
933 private:
934 EnumSet<Type, byte> set_;
935 };
936
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000937 static Types no_types() { return Types(); }
938 static Types all_types() { return Types((1 << NUMBER_OF_TYPES) - 1); }
939
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000940 explicit ToBooleanStub(Register tos, Types types = Types())
941 : tos_(tos), types_(types) { }
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000942
943 void Generate(MacroAssembler* masm);
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000944 virtual int GetCodeKind() { return Code::TO_BOOLEAN_IC; }
945 virtual void PrintName(StringStream* stream);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000946
947 private:
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000948 Major MajorKey() { return ToBoolean; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000949 int MinorKey() { return (tos_.code() << NUMBER_OF_TYPES) | types_.ToByte(); }
950
951 virtual void FinishCode(Code* code) {
952 code->set_to_boolean_state(types_.ToByte());
953 }
954
955 void CheckOddball(MacroAssembler* masm,
956 Type type,
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000957 Heap::RootListIndex value,
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000958 bool result);
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000959 void GenerateTypeTransition(MacroAssembler* masm);
960
961 Register tos_;
962 Types types_;
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000963};
964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965} } // namespace v8::internal
966
967#endif // V8_CODE_STUBS_H_