blob: c2dd0a7aacef7d3416e9c570b0c4f7df074d170b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_CODE_STUBS_H_
29#define V8_CODE_STUBS_H_
30
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "globals.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010032#include "macro-assembler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
Steve Blockd0582a62009-12-15 09:54:21 +000037// List of code stubs used on all platforms. The order in this list is important
38// as only the stubs up to and including RecordWrite allows nested stub calls.
39#define CODE_STUB_LIST_ALL_PLATFORMS(V) \
40 V(CallFunction) \
41 V(GenericBinaryOp) \
42 V(StringAdd) \
Leon Clarkee46be812010-01-19 14:06:41 +000043 V(SubString) \
44 V(StringCompare) \
Steve Blockd0582a62009-12-15 09:54:21 +000045 V(SmiOp) \
46 V(Compare) \
47 V(RecordWrite) \
48 V(ConvertToDouble) \
49 V(WriteInt32ToHeapNumber) \
Steve Block8defd9f2010-07-08 12:39:36 +010050 V(IntegerMod) \
Steve Blockd0582a62009-12-15 09:54:21 +000051 V(StackCheck) \
Leon Clarkee46be812010-01-19 14:06:41 +000052 V(FastNewClosure) \
53 V(FastNewContext) \
54 V(FastCloneShallowArray) \
Andrei Popescu402d9372010-02-26 13:31:12 +000055 V(TranscendentalCache) \
Leon Clarkee46be812010-01-19 14:06:41 +000056 V(GenericUnaryOp) \
Steve Blockd0582a62009-12-15 09:54:21 +000057 V(RevertToNumber) \
58 V(ToBoolean) \
59 V(Instanceof) \
60 V(CounterOp) \
61 V(ArgumentsAccess) \
Leon Clarkee46be812010-01-19 14:06:41 +000062 V(RegExpExec) \
Andrei Popescu402d9372010-02-26 13:31:12 +000063 V(NumberToString) \
Steve Blockd0582a62009-12-15 09:54:21 +000064 V(CEntry) \
Leon Clarke4515c472010-02-03 11:58:03 +000065 V(JSEntry) \
66 V(DebuggerStatement)
Steve Blockd0582a62009-12-15 09:54:21 +000067
68// List of code stubs only used on ARM platforms.
69#ifdef V8_TARGET_ARCH_ARM
70#define CODE_STUB_LIST_ARM(V) \
71 V(GetProperty) \
72 V(SetProperty) \
73 V(InvokeBuiltin) \
74 V(RegExpCEntry)
75#else
76#define CODE_STUB_LIST_ARM(V)
77#endif
78
79// Combined list of code stubs.
80#define CODE_STUB_LIST(V) \
81 CODE_STUB_LIST_ALL_PLATFORMS(V) \
82 CODE_STUB_LIST_ARM(V)
Steve Blocka7e24c12009-10-30 11:49:00 +000083
Kristian Monsen80d68ea2010-09-08 11:05:35 +010084// Types of uncatchable exceptions.
85enum UncatchableExceptionType { OUT_OF_MEMORY, TERMINATION };
86
87// Mode to overwrite BinaryExpression values.
88enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
89enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE };
90
91
Steve Blocka7e24c12009-10-30 11:49:00 +000092// Stub is base classes of all stubs.
93class CodeStub BASE_EMBEDDED {
94 public:
95 enum Major {
Steve Blockd0582a62009-12-15 09:54:21 +000096#define DEF_ENUM(name) name,
97 CODE_STUB_LIST(DEF_ENUM)
98#undef DEF_ENUM
99 NoCache, // marker for stubs that do custom caching
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 NUMBER_OF_IDS
101 };
102
103 // Retrieve the code for the stub. Generate the code if needed.
104 Handle<Code> GetCode();
105
Leon Clarkee46be812010-01-19 14:06:41 +0000106 // Retrieve the code for the stub if already generated. Do not
107 // generate the code if not already generated and instead return a
108 // retry after GC Failure object.
109 Object* TryGetCode();
110
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 static Major MajorKeyFromKey(uint32_t key) {
112 return static_cast<Major>(MajorKeyBits::decode(key));
113 };
114 static int MinorKeyFromKey(uint32_t key) {
115 return MinorKeyBits::decode(key);
116 };
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100117
118 // Gets the major key from a code object that is a code stub or binary op IC.
119 static Major GetMajorKey(Code* code_stub) {
120 return static_cast<Major>(code_stub->major_key());
121 }
122
Andrei Popescu31002712010-02-23 13:46:05 +0000123 static const char* MajorName(Major major_key, bool allow_unknown_keys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
125 virtual ~CodeStub() {}
126
Steve Blockd0582a62009-12-15 09:54:21 +0000127 // Override these methods to provide a custom caching mechanism for
128 // an individual type of code stub.
129 virtual bool GetCustomCache(Code** code_out) { return false; }
130 virtual void SetCustomCache(Code* value) { }
131 virtual bool has_custom_cache() { return false; }
132
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 protected:
134 static const int kMajorBits = 5;
135 static const int kMinorBits = kBitsPerInt - kSmiTagSize - kMajorBits;
136
137 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000138 // Lookup the code in the (possibly custom) cache.
139 bool FindCodeInCache(Code** code_out);
140
141 // Nonvirtual wrapper around the stub-specific Generate function. Call
142 // this function to set up the macro assembler and generate the code.
143 void GenerateCode(MacroAssembler* masm);
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 // Generates the assembler code for the stub.
146 virtual void Generate(MacroAssembler* masm) = 0;
147
Leon Clarkee46be812010-01-19 14:06:41 +0000148 // Perform bookkeeping required after code generation when stub code is
149 // initially generated.
150 void RecordCodeGeneration(Code* code, MacroAssembler* masm);
151
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 // Returns information for computing the number key.
153 virtual Major MajorKey() = 0;
154 virtual int MinorKey() = 0;
155
156 // The CallFunctionStub needs to override this so it can encode whether a
157 // lazily generated function should be fully optimized or not.
158 virtual InLoopFlag InLoop() { return NOT_IN_LOOP; }
159
Steve Block6ded16b2010-05-10 14:33:55 +0100160 // GenericBinaryOpStub needs to override this.
161 virtual int GetCodeKind();
162
163 // GenericBinaryOpStub needs to override this.
164 virtual InlineCacheState GetICState() {
165 return UNINITIALIZED;
166 }
167
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 // Returns a name for logging/debugging purposes.
Andrei Popescu31002712010-02-23 13:46:05 +0000169 virtual const char* GetName() { return MajorName(MajorKey(), false); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000170
171#ifdef DEBUG
172 virtual void Print() { PrintF("%s\n", GetName()); }
173#endif
174
175 // Computes the key based on major and minor.
176 uint32_t GetKey() {
177 ASSERT(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
178 return MinorKeyBits::encode(MinorKey()) |
179 MajorKeyBits::encode(MajorKey());
180 }
181
182 bool AllowsStubCalls() { return MajorKey() <= RecordWrite; }
183
184 class MajorKeyBits: public BitField<uint32_t, 0, kMajorBits> {};
185 class MinorKeyBits: public BitField<uint32_t, kMajorBits, kMinorBits> {};
186
187 friend class BreakPointIterator;
188};
189
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100190
191// Helper interface to prepare to/restore after making runtime calls.
192class RuntimeCallHelper {
193 public:
194 virtual ~RuntimeCallHelper() {}
195
196 virtual void BeforeCall(MacroAssembler* masm) const = 0;
197
198 virtual void AfterCall(MacroAssembler* masm) const = 0;
199
200 protected:
201 RuntimeCallHelper() {}
202
203 private:
204 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
205};
206
207} } // namespace v8::internal
208
209#if V8_TARGET_ARCH_IA32
210#include "ia32/code-stubs-ia32.h"
211#elif V8_TARGET_ARCH_X64
212#include "x64/code-stubs-x64.h"
213#elif V8_TARGET_ARCH_ARM
214#include "arm/code-stubs-arm.h"
215#elif V8_TARGET_ARCH_MIPS
216#include "mips/code-stubs-mips.h"
217#else
218#error Unsupported target architecture.
219#endif
220
221namespace v8 {
222namespace internal {
223
224
225// RuntimeCallHelper implementation used in IC stubs: enters/leaves a
226// newly created internal frame before/after the runtime call.
227class ICRuntimeCallHelper : public RuntimeCallHelper {
228 public:
229 ICRuntimeCallHelper() {}
230
231 virtual void BeforeCall(MacroAssembler* masm) const;
232
233 virtual void AfterCall(MacroAssembler* masm) const;
234};
235
236
237// Trivial RuntimeCallHelper implementation.
238class NopRuntimeCallHelper : public RuntimeCallHelper {
239 public:
240 NopRuntimeCallHelper() {}
241
242 virtual void BeforeCall(MacroAssembler* masm) const {}
243
244 virtual void AfterCall(MacroAssembler* masm) const {}
245};
246
247
248class StackCheckStub : public CodeStub {
249 public:
250 StackCheckStub() { }
251
252 void Generate(MacroAssembler* masm);
253
254 private:
255
256 const char* GetName() { return "StackCheckStub"; }
257
258 Major MajorKey() { return StackCheck; }
259 int MinorKey() { return 0; }
260};
261
262
263class FastNewClosureStub : public CodeStub {
264 public:
265 void Generate(MacroAssembler* masm);
266
267 private:
268 const char* GetName() { return "FastNewClosureStub"; }
269 Major MajorKey() { return FastNewClosure; }
270 int MinorKey() { return 0; }
271};
272
273
274class FastNewContextStub : public CodeStub {
275 public:
276 static const int kMaximumSlots = 64;
277
278 explicit FastNewContextStub(int slots) : slots_(slots) {
279 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
280 }
281
282 void Generate(MacroAssembler* masm);
283
284 private:
285 int slots_;
286
287 const char* GetName() { return "FastNewContextStub"; }
288 Major MajorKey() { return FastNewContext; }
289 int MinorKey() { return slots_; }
290};
291
292
293class FastCloneShallowArrayStub : public CodeStub {
294 public:
295 // Maximum length of copied elements array.
296 static const int kMaximumClonedLength = 8;
297
298 enum Mode {
299 CLONE_ELEMENTS,
300 COPY_ON_WRITE_ELEMENTS
301 };
302
303 FastCloneShallowArrayStub(Mode mode, int length)
304 : mode_(mode),
305 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
306 ASSERT(length_ >= 0);
307 ASSERT(length_ <= kMaximumClonedLength);
308 }
309
310 void Generate(MacroAssembler* masm);
311
312 private:
313 Mode mode_;
314 int length_;
315
316 const char* GetName() { return "FastCloneShallowArrayStub"; }
317 Major MajorKey() { return FastCloneShallowArray; }
318 int MinorKey() {
319 ASSERT(mode_ == 0 || mode_ == 1);
320 return (length_ << 1) | mode_;
321 }
322};
323
324
325class InstanceofStub: public CodeStub {
326 public:
327 InstanceofStub() { }
328
329 void Generate(MacroAssembler* masm);
330
331 private:
332 Major MajorKey() { return Instanceof; }
333 int MinorKey() { return 0; }
334};
335
336
337enum NegativeZeroHandling {
338 kStrictNegativeZero,
339 kIgnoreNegativeZero
340};
341
342
343class GenericUnaryOpStub : public CodeStub {
344 public:
345 GenericUnaryOpStub(Token::Value op,
346 UnaryOverwriteMode overwrite,
347 NegativeZeroHandling negative_zero = kStrictNegativeZero)
348 : op_(op), overwrite_(overwrite), negative_zero_(negative_zero) { }
349
350 private:
351 Token::Value op_;
352 UnaryOverwriteMode overwrite_;
353 NegativeZeroHandling negative_zero_;
354
355 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {};
356 class NegativeZeroField: public BitField<NegativeZeroHandling, 1, 1> {};
357 class OpField: public BitField<Token::Value, 2, kMinorBits - 2> {};
358
359 Major MajorKey() { return GenericUnaryOp; }
360 int MinorKey() {
361 return OpField::encode(op_) |
362 OverwriteField::encode(overwrite_) |
363 NegativeZeroField::encode(negative_zero_);
364 }
365
366 void Generate(MacroAssembler* masm);
367
368 const char* GetName();
369};
370
371
372enum NaNInformation {
373 kBothCouldBeNaN,
374 kCantBothBeNaN
375};
376
377
378class CompareStub: public CodeStub {
379 public:
380 CompareStub(Condition cc,
381 bool strict,
382 NaNInformation nan_info = kBothCouldBeNaN,
383 bool include_number_compare = true,
384 Register lhs = no_reg,
385 Register rhs = no_reg) :
386 cc_(cc),
387 strict_(strict),
388 never_nan_nan_(nan_info == kCantBothBeNaN),
389 include_number_compare_(include_number_compare),
390 lhs_(lhs),
391 rhs_(rhs),
392 name_(NULL) { }
393
394 void Generate(MacroAssembler* masm);
395
396 private:
397 Condition cc_;
398 bool strict_;
399 // Only used for 'equal' comparisons. Tells the stub that we already know
400 // that at least one side of the comparison is not NaN. This allows the
401 // stub to use object identity in the positive case. We ignore it when
402 // generating the minor key for other comparisons to avoid creating more
403 // stubs.
404 bool never_nan_nan_;
405 // Do generate the number comparison code in the stub. Stubs without number
406 // comparison code is used when the number comparison has been inlined, and
407 // the stub will be called if one of the operands is not a number.
408 bool include_number_compare_;
409 // Register holding the left hand side of the comparison if the stub gives
410 // a choice, no_reg otherwise.
411 Register lhs_;
412 // Register holding the right hand side of the comparison if the stub gives
413 // a choice, no_reg otherwise.
414 Register rhs_;
415
416 // Encoding of the minor key CCCCCCCCCCCCRCNS.
417 class StrictField: public BitField<bool, 0, 1> {};
418 class NeverNanNanField: public BitField<bool, 1, 1> {};
419 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
420 class RegisterField: public BitField<bool, 3, 1> {};
421 class ConditionField: public BitField<int, 4, 12> {};
422
423 Major MajorKey() { return Compare; }
424
425 int MinorKey();
426
427 // Branch to the label if the given object isn't a symbol.
428 void BranchIfNonSymbol(MacroAssembler* masm,
429 Label* label,
430 Register object,
431 Register scratch);
432
433 // Unfortunately you have to run without snapshots to see most of these
434 // names in the profile since most compare stubs end up in the snapshot.
435 char* name_;
436 const char* GetName();
437#ifdef DEBUG
438 void Print() {
439 PrintF("CompareStub (cc %d), (strict %s), "
440 "(never_nan_nan %s), (number_compare %s) ",
441 static_cast<int>(cc_),
442 strict_ ? "true" : "false",
443 never_nan_nan_ ? "true" : "false",
444 include_number_compare_ ? "included" : "not included");
445
446 if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) {
447 PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code());
448 } else {
449 PrintF("\n");
450 }
451 }
452#endif
453};
454
455
456class CEntryStub : public CodeStub {
457 public:
458 explicit CEntryStub(int result_size) : result_size_(result_size) { }
459
460 void Generate(MacroAssembler* masm);
461
462 private:
463 void GenerateCore(MacroAssembler* masm,
464 Label* throw_normal_exception,
465 Label* throw_termination_exception,
466 Label* throw_out_of_memory_exception,
467 bool do_gc,
468 bool always_allocate_scope,
469 int alignment_skew = 0);
470 void GenerateThrowTOS(MacroAssembler* masm);
471 void GenerateThrowUncatchable(MacroAssembler* masm,
472 UncatchableExceptionType type);
473
474 // Number of pointers/values returned.
475 const int result_size_;
476
477 Major MajorKey() { return CEntry; }
478 // Minor key must differ if different result_size_ values means different
479 // code is generated.
480 int MinorKey();
481
482 const char* GetName() { return "CEntryStub"; }
483};
484
485
486class ApiGetterEntryStub : public CodeStub {
487 public:
488 ApiGetterEntryStub(Handle<AccessorInfo> info,
489 ApiFunction* fun)
490 : info_(info),
491 fun_(fun) { }
492 void Generate(MacroAssembler* masm);
493 virtual bool has_custom_cache() { return true; }
494 virtual bool GetCustomCache(Code** code_out);
495 virtual void SetCustomCache(Code* value);
496
497 static const int kStackSpace = 5;
498 static const int kArgc = 4;
499 private:
500 Handle<AccessorInfo> info() { return info_; }
501 ApiFunction* fun() { return fun_; }
502 Major MajorKey() { return NoCache; }
503 int MinorKey() { return 0; }
504 const char* GetName() { return "ApiEntryStub"; }
505 // The accessor info associated with the function.
506 Handle<AccessorInfo> info_;
507 // The function to be called.
508 ApiFunction* fun_;
509};
510
511
512class JSEntryStub : public CodeStub {
513 public:
514 JSEntryStub() { }
515
516 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
517
518 protected:
519 void GenerateBody(MacroAssembler* masm, bool is_construct);
520
521 private:
522 Major MajorKey() { return JSEntry; }
523 int MinorKey() { return 0; }
524
525 const char* GetName() { return "JSEntryStub"; }
526};
527
528
529class JSConstructEntryStub : public JSEntryStub {
530 public:
531 JSConstructEntryStub() { }
532
533 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
534
535 private:
536 int MinorKey() { return 1; }
537
538 const char* GetName() { return "JSConstructEntryStub"; }
539};
540
541
542class ArgumentsAccessStub: public CodeStub {
543 public:
544 enum Type {
545 READ_ELEMENT,
546 NEW_OBJECT
547 };
548
549 explicit ArgumentsAccessStub(Type type) : type_(type) { }
550
551 private:
552 Type type_;
553
554 Major MajorKey() { return ArgumentsAccess; }
555 int MinorKey() { return type_; }
556
557 void Generate(MacroAssembler* masm);
558 void GenerateReadElement(MacroAssembler* masm);
559 void GenerateNewObject(MacroAssembler* masm);
560
561 const char* GetName() { return "ArgumentsAccessStub"; }
562
563#ifdef DEBUG
564 void Print() {
565 PrintF("ArgumentsAccessStub (type %d)\n", type_);
566 }
567#endif
568};
569
570
571class RegExpExecStub: public CodeStub {
572 public:
573 RegExpExecStub() { }
574
575 private:
576 Major MajorKey() { return RegExpExec; }
577 int MinorKey() { return 0; }
578
579 void Generate(MacroAssembler* masm);
580
581 const char* GetName() { return "RegExpExecStub"; }
582
583#ifdef DEBUG
584 void Print() {
585 PrintF("RegExpExecStub\n");
586 }
587#endif
588};
589
590
591class CallFunctionStub: public CodeStub {
592 public:
593 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
594 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
595
596 void Generate(MacroAssembler* masm);
597
598 private:
599 int argc_;
600 InLoopFlag in_loop_;
601 CallFunctionFlags flags_;
602
603#ifdef DEBUG
604 void Print() {
605 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
606 argc_,
607 static_cast<int>(in_loop_),
608 static_cast<int>(flags_));
609 }
610#endif
611
612 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
613 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
614 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
615 class ArgcBits: public BitField<int, 2, 32 - 2> {};
616
617 Major MajorKey() { return CallFunction; }
618 int MinorKey() {
619 // Encode the parameters in a unique 32 bit value.
620 return InLoopBits::encode(in_loop_)
621 | FlagBits::encode(flags_)
622 | ArgcBits::encode(argc_);
623 }
624
625 InLoopFlag InLoop() { return in_loop_; }
626 bool ReceiverMightBeValue() {
627 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
628 }
629
630 public:
631 static int ExtractArgcFromMinorKey(int minor_key) {
632 return ArgcBits::decode(minor_key);
633 }
634};
635
636
637enum StringIndexFlags {
638 // Accepts smis or heap numbers.
639 STRING_INDEX_IS_NUMBER,
640
641 // Accepts smis or heap numbers that are valid array indices
642 // (ECMA-262 15.4). Invalid indices are reported as being out of
643 // range.
644 STRING_INDEX_IS_ARRAY_INDEX
645};
646
647
648// Generates code implementing String.prototype.charCodeAt.
649//
650// Only supports the case when the receiver is a string and the index
651// is a number (smi or heap number) that is a valid index into the
652// string. Additional index constraints are specified by the
653// flags. Otherwise, bails out to the provided labels.
654//
655// Register usage: |object| may be changed to another string in a way
656// that doesn't affect charCodeAt/charAt semantics, |index| is
657// preserved, |scratch| and |result| are clobbered.
658class StringCharCodeAtGenerator {
659 public:
660 StringCharCodeAtGenerator(Register object,
661 Register index,
662 Register scratch,
663 Register result,
664 Label* receiver_not_string,
665 Label* index_not_number,
666 Label* index_out_of_range,
667 StringIndexFlags index_flags)
668 : object_(object),
669 index_(index),
670 scratch_(scratch),
671 result_(result),
672 receiver_not_string_(receiver_not_string),
673 index_not_number_(index_not_number),
674 index_out_of_range_(index_out_of_range),
675 index_flags_(index_flags) {
676 ASSERT(!scratch_.is(object_));
677 ASSERT(!scratch_.is(index_));
678 ASSERT(!scratch_.is(result_));
679 ASSERT(!result_.is(object_));
680 ASSERT(!result_.is(index_));
681 }
682
683 // Generates the fast case code. On the fallthrough path |result|
684 // register contains the result.
685 void GenerateFast(MacroAssembler* masm);
686
687 // Generates the slow case code. Must not be naturally
688 // reachable. Expected to be put after a ret instruction (e.g., in
689 // deferred code). Always jumps back to the fast case.
690 void GenerateSlow(MacroAssembler* masm,
691 const RuntimeCallHelper& call_helper);
692
693 private:
694 Register object_;
695 Register index_;
696 Register scratch_;
697 Register result_;
698
699 Label* receiver_not_string_;
700 Label* index_not_number_;
701 Label* index_out_of_range_;
702
703 StringIndexFlags index_flags_;
704
705 Label call_runtime_;
706 Label index_not_smi_;
707 Label got_smi_index_;
708 Label exit_;
709
710 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
711};
712
713
714// Generates code for creating a one-char string from a char code.
715class StringCharFromCodeGenerator {
716 public:
717 StringCharFromCodeGenerator(Register code,
718 Register result)
719 : code_(code),
720 result_(result) {
721 ASSERT(!code_.is(result_));
722 }
723
724 // Generates the fast case code. On the fallthrough path |result|
725 // register contains the result.
726 void GenerateFast(MacroAssembler* masm);
727
728 // Generates the slow case code. Must not be naturally
729 // reachable. Expected to be put after a ret instruction (e.g., in
730 // deferred code). Always jumps back to the fast case.
731 void GenerateSlow(MacroAssembler* masm,
732 const RuntimeCallHelper& call_helper);
733
734 private:
735 Register code_;
736 Register result_;
737
738 Label slow_case_;
739 Label exit_;
740
741 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
742};
743
744
745// Generates code implementing String.prototype.charAt.
746//
747// Only supports the case when the receiver is a string and the index
748// is a number (smi or heap number) that is a valid index into the
749// string. Additional index constraints are specified by the
750// flags. Otherwise, bails out to the provided labels.
751//
752// Register usage: |object| may be changed to another string in a way
753// that doesn't affect charCodeAt/charAt semantics, |index| is
754// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
755class StringCharAtGenerator {
756 public:
757 StringCharAtGenerator(Register object,
758 Register index,
759 Register scratch1,
760 Register scratch2,
761 Register result,
762 Label* receiver_not_string,
763 Label* index_not_number,
764 Label* index_out_of_range,
765 StringIndexFlags index_flags)
766 : char_code_at_generator_(object,
767 index,
768 scratch1,
769 scratch2,
770 receiver_not_string,
771 index_not_number,
772 index_out_of_range,
773 index_flags),
774 char_from_code_generator_(scratch2, result) {}
775
776 // Generates the fast case code. On the fallthrough path |result|
777 // register contains the result.
778 void GenerateFast(MacroAssembler* masm);
779
780 // Generates the slow case code. Must not be naturally
781 // reachable. Expected to be put after a ret instruction (e.g., in
782 // deferred code). Always jumps back to the fast case.
783 void GenerateSlow(MacroAssembler* masm,
784 const RuntimeCallHelper& call_helper);
785
786 private:
787 StringCharCodeAtGenerator char_code_at_generator_;
788 StringCharFromCodeGenerator char_from_code_generator_;
789
790 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
791};
792
Steve Blocka7e24c12009-10-30 11:49:00 +0000793} } // namespace v8::internal
794
795#endif // V8_CODE_STUBS_H_