blob: 89e99a8d085709489004ec8d56fa211614babd69 [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
171 // 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
Ben Murdoch257744e2011-11-30 15:57:28 +0000175 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100176 virtual int GetCodeKind();
177
Ben Murdoch257744e2011-11-30 15:57:28 +0000178 // BinaryOpStub needs to override this.
Steve Block6ded16b2010-05-10 14:33:55 +0100179 virtual InlineCacheState GetICState() {
180 return UNINITIALIZED;
181 }
182
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 // Returns a name for logging/debugging purposes.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000184 SmartPointer<const char> GetName();
185 virtual void PrintName(StringStream* stream) {
186 stream->Add("%s", MajorName(MajorKey(), false));
187 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000188
Steve Block44f0eee2011-05-26 01:26:41 +0100189 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000200 // See comment above, where Instanceof is defined.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100201 bool AllowsStubCalls() { return MajorKey() <= Instanceof; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
204 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
205
206 friend class BreakPointIterator;
207};
208
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100209
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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100244// RuntimeCallHelper implementation used in stubs: enters/leaves a
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100245// newly created internal frame before/after the runtime call.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100246class StubRuntimeCallHelper : public RuntimeCallHelper {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100247 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100248 StubRuntimeCallHelper() {}
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100249
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:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100274 Major MajorKey() { return StackCheck; }
275 int MinorKey() { return 0; }
276};
277
278
Steve Block1e0659c2011-05-24 12:43:12 +0100279class 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; }
Steve Block1e0659c2011-05-24 12:43:12 +0100288};
289
290
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100291class FastNewClosureStub : public CodeStub {
292 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100293 explicit FastNewClosureStub(StrictModeFlag strict_mode)
294 : strict_mode_(strict_mode) { }
295
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100296 void Generate(MacroAssembler* masm);
297
298 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100299 Major MajorKey() { return FastNewClosure; }
Steve Block44f0eee2011-05-26 01:26:41 +0100300 int MinorKey() { return strict_mode_; }
301
302 StrictModeFlag strict_mode_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100303};
304
305
306class FastNewContextStub : public CodeStub {
307 public:
308 static const int kMaximumSlots = 64;
309
310 explicit FastNewContextStub(int slots) : slots_(slots) {
311 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
312 }
313
314 void Generate(MacroAssembler* masm);
315
316 private:
317 int slots_;
318
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100319 Major MajorKey() { return FastNewContext; }
320 int MinorKey() { return slots_; }
321};
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
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100347 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:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100357 enum Flags {
358 kNoFlags = 0,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100359 kArgsInRegisters = 1 << 0,
360 kCallSiteInlineCheck = 1 << 1,
361 kReturnTrueFalseObject = 1 << 2
Ben Murdochb0fe1622011-05-05 13:52:32 +0100362 };
363
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000364 explicit InstanceofStub(Flags flags) : flags_(flags) { }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100365
366 static Register left();
367 static Register right();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100368
369 void Generate(MacroAssembler* masm);
370
371 private:
372 Major MajorKey() { return Instanceof; }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100373 int MinorKey() { return static_cast<int>(flags_); }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100374
Ben Murdoch086aeea2011-05-13 15:57:08 +0100375 bool HasArgsInRegisters() const {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100376 return (flags_ & kArgsInRegisters) != 0;
377 }
378
Ben Murdoch086aeea2011-05-13 15:57:08 +0100379 bool HasCallSiteInlineCheck() const {
380 return (flags_ & kCallSiteInlineCheck) != 0;
381 }
382
383 bool ReturnTrueFalseObject() const {
384 return (flags_ & kReturnTrueFalseObject) != 0;
385 }
386
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387 virtual void PrintName(StringStream* stream);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100388
Ben Murdochb0fe1622011-05-05 13:52:32 +0100389 Flags flags_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100390};
391
392
Ben Murdochb0fe1622011-05-05 13:52:32 +0100393class 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; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100401};
402
403
Ben Murdochb0fe1622011-05-05 13:52:32 +0100404class 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);
Ben Murdoch257744e2011-11-30 15:57:28 +0000426 void GenerateSymbols(MacroAssembler* masm);
427 void GenerateStrings(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100428 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_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100436};
437
438
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100439// 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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100448enum NaNInformation {
449 kBothCouldBeNaN,
450 kCantBothBeNaN
451};
452
453
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100454class CompareStub: public CodeStub {
455 public:
456 CompareStub(Condition cc,
457 bool strict,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100458 CompareFlags flags,
459 Register lhs,
460 Register rhs) :
Ben Murdochb0fe1622011-05-05 13:52:32 +0100461 cc_(cc),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100462 strict_(strict),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100463 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),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100466 lhs_(lhs),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000467 rhs_(rhs) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100468
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100469 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),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000478 rhs_(no_reg) { }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100479
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100480 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_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100495
496 // Generate the comparison code for two smi operands in the stub.
497 bool include_smi_compare_;
498
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100499 // Register holding the left hand side of the comparison if the stub gives
500 // a choice, no_reg otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100501
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100502 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
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100507 // Encoding of the minor key in 16 bits.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100508 class StrictField: public BitField<bool, 0, 1> {};
509 class NeverNanNanField: public BitField<bool, 1, 1> {};
510 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100511 class IncludeSmiCompareField: public BitField<bool, 3, 1> {};
512 class RegisterField: public BitField<bool, 4, 1> {};
513 class ConditionField: public BitField<int, 5, 11> {};
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100514
515 Major MajorKey() { return Compare; }
516
517 int MinorKey();
518
Ben Murdochb0fe1622011-05-05 13:52:32 +0100519 virtual int GetCodeKind() { return Code::COMPARE_IC; }
520 virtual void FinishCode(Code* code) {
521 code->set_compare_state(CompareIC::GENERIC);
522 }
523
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100524 // 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.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000532 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100533};
534
535
536class CEntryStub : public CodeStub {
537 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100538 explicit CEntryStub(int result_size)
539 : result_size_(result_size), save_doubles_(false) { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100540
541 void Generate(MacroAssembler* masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100542 void SaveDoubles() { save_doubles_ = true; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100543
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,
Steve Block1e0659c2011-05-24 12:43:12 +0100550 bool always_allocate_scope);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100551 void GenerateThrowTOS(MacroAssembler* masm);
552 void GenerateThrowUncatchable(MacroAssembler* masm,
553 UncatchableExceptionType type);
554
555 // Number of pointers/values returned.
556 const int result_size_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100557 bool save_doubles_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100558
559 Major MajorKey() { return CEntry; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100560 int MinorKey();
561
Steve Block44f0eee2011-05-26 01:26:41 +0100562 bool NeedsImmovableCode();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100563};
564
565
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100566class 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; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100578};
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
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000590 virtual void PrintName(StringStream* stream) {
591 stream->Add("JSConstructEntryStub");
592 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100593};
594
595
596class ArgumentsAccessStub: public CodeStub {
597 public:
598 enum Type {
599 READ_ELEMENT,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000600 NEW_NON_STRICT_FAST,
601 NEW_NON_STRICT_SLOW,
Steve Block44f0eee2011-05-26 01:26:41 +0100602 NEW_STRICT
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100603 };
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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000615 void GenerateNewStrict(MacroAssembler* masm);
616 void GenerateNewNonStrictFast(MacroAssembler* masm);
617 void GenerateNewNonStrictSlow(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100618
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000619 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100620};
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);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100632};
633
634
Ben Murdochb0fe1622011-05-05 13:52:32 +0100635class 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);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100644};
645
646
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100647class 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
Ben Murdoch086aeea2011-05-13 15:57:08 +0100654 static int ExtractArgcFromMinorKey(int minor_key) {
655 return ArgcBits::decode(minor_key);
656 }
657
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100658 private:
659 int argc_;
660 InLoopFlag in_loop_;
661 CallFunctionFlags flags_;
662
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000663 virtual void PrintName(StringStream* stream);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100664
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_; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000679
680 bool ReceiverMightBeImplicit() {
681 return (flags_ & RECEIVER_MIGHT_BE_IMPLICIT) != 0;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100682 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100683};
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
Ben Murdoch086aeea2011-05-13 15:57:08 +0100842
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
Ben Murdoch257744e2011-11-30 15:57:28 +0000860
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000861class KeyedLoadElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000862 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000863 explicit KeyedLoadElementStub(JSObject::ElementsKind elements_kind)
864 : elements_kind_(elements_kind)
865 { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000866
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000867 Major MajorKey() { return KeyedLoadElement; }
868 int MinorKey() { return elements_kind_; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000869
870 void Generate(MacroAssembler* masm);
871
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000872 private:
873 JSObject::ElementsKind elements_kind_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000874
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000875 DISALLOW_COPY_AND_ASSIGN(KeyedLoadElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +0000876};
877
878
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000879class KeyedStoreElementStub : public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000880 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000881 KeyedStoreElementStub(bool is_js_array,
882 JSObject::ElementsKind elements_kind)
883 : is_js_array_(is_js_array),
884 elements_kind_(elements_kind) { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000885
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000886 Major MajorKey() { return KeyedStoreElement; }
887 int MinorKey() {
888 return (is_js_array_ ? 0 : JSObject::kElementsKindCount) + elements_kind_;
889 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000890
891 void Generate(MacroAssembler* masm);
892
Ben Murdoch257744e2011-11-30 15:57:28 +0000893 private:
894 bool is_js_array_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000895 JSObject::ElementsKind elements_kind_;
896
897 DISALLOW_COPY_AND_ASSIGN(KeyedStoreElementStub);
Ben Murdoch257744e2011-11-30 15:57:28 +0000898};
899
900
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000901class ToBooleanStub: public CodeStub {
Ben Murdoch257744e2011-11-30 15:57:28 +0000902 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000903 enum Type {
904 UNDEFINED,
905 BOOLEAN,
906 NULL_TYPE,
907 SMI,
908 SPEC_OBJECT,
909 STRING,
910 HEAP_NUMBER,
911 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(); }
927 void Print(StringStream* stream) const;
928 void TraceTransition(Types to) const;
929 bool Record(Handle<Object> object);
930 bool NeedsMap() const;
931 bool CanBeUndetectable() const;
932
933 private:
934 EnumSet<Type, byte> set_;
935 };
936
937 static Types no_types() { return Types(); }
938 static Types all_types() { return Types((1 << NUMBER_OF_TYPES) - 1); }
939
940 explicit ToBooleanStub(Register tos, Types types = Types())
941 : tos_(tos), types_(types) { }
Ben Murdoch257744e2011-11-30 15:57:28 +0000942
943 void Generate(MacroAssembler* masm);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000944 virtual int GetCodeKind() { return Code::TO_BOOLEAN_IC; }
945 virtual void PrintName(StringStream* stream);
Ben Murdoch257744e2011-11-30 15:57:28 +0000946
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000947 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000948 Major MajorKey() { return ToBoolean; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +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,
957 Heap::RootListIndex value,
958 bool result);
959 void GenerateTypeTransition(MacroAssembler* masm);
960
961 Register tos_;
962 Types types_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000963};
964
Steve Blocka7e24c12009-10-30 11:49:00 +0000965} } // namespace v8::internal
966
967#endif // V8_CODE_STUBS_H_