blob: 783bef00d16c084071caf8340422dd347a22b43f [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_CODEGEN_H_
29#define V8_CODEGEN_H_
30
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include "code-stubs.h"
32#include "runtime.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033#include "type-info.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35// Include the declaration of the architecture defined class CodeGenerator.
36// The contract to the shared code is that the the CodeGenerator is a subclass
37// of Visitor and that the following methods are available publicly:
38// MakeCode
Steve Block3ce2e202009-11-05 08:53:23 +000039// MakeCodePrologue
40// MakeCodeEpilogue
Steve Blocka7e24c12009-10-30 11:49:00 +000041// masm
42// frame
Steve Blockd0582a62009-12-15 09:54:21 +000043// script
Steve Blocka7e24c12009-10-30 11:49:00 +000044// has_valid_frame
45// SetFrame
46// DeleteFrame
47// allocator
48// AddDeferred
49// in_spilled_code
50// set_in_spilled_code
Steve Block3ce2e202009-11-05 08:53:23 +000051// RecordPositions
Steve Blocka7e24c12009-10-30 11:49:00 +000052//
53// These methods are either used privately by the shared code or implemented as
54// shared code:
55// CodeGenerator
56// ~CodeGenerator
57// ProcessDeferred
Leon Clarke4515c472010-02-03 11:58:03 +000058// Generate
Steve Block3ce2e202009-11-05 08:53:23 +000059// ComputeLazyCompile
Steve Block6ded16b2010-05-10 14:33:55 +010060// BuildFunctionInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000061// ComputeCallInitialize
62// ComputeCallInitializeInLoop
63// ProcessDeclarations
64// DeclareGlobals
65// FindInlineRuntimeLUT
66// CheckForInlineRuntimeCall
67// PatchInlineRuntimeEntry
Steve Block3ce2e202009-11-05 08:53:23 +000068// AnalyzeCondition
Steve Blocka7e24c12009-10-30 11:49:00 +000069// CodeForFunctionPosition
70// CodeForReturnPosition
71// CodeForStatementPosition
Steve Blockd0582a62009-12-15 09:54:21 +000072// CodeForDoWhileConditionPosition
Steve Blocka7e24c12009-10-30 11:49:00 +000073// CodeForSourcePosition
74
75
76// Mode to overwrite BinaryExpression values.
77enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
Leon Clarkeac952652010-07-15 11:15:24 +010078enum UnaryOverwriteMode { UNARY_OVERWRITE, UNARY_NO_OVERWRITE };
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80// Types of uncatchable exceptions.
81enum UncatchableExceptionType { OUT_OF_MEMORY, TERMINATION };
82
83
84#if V8_TARGET_ARCH_IA32
85#include "ia32/codegen-ia32.h"
86#elif V8_TARGET_ARCH_X64
87#include "x64/codegen-x64.h"
88#elif V8_TARGET_ARCH_ARM
89#include "arm/codegen-arm.h"
Andrei Popescu31002712010-02-23 13:46:05 +000090#elif V8_TARGET_ARCH_MIPS
91#include "mips/codegen-mips.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000092#else
93#error Unsupported target architecture.
94#endif
95
96#include "register-allocator.h"
97
98namespace v8 {
99namespace internal {
100
101
Steve Block6ded16b2010-05-10 14:33:55 +0100102#define INLINE_RUNTIME_FUNCTION_LIST(F) \
103 F(IsSmi, 1, 1) \
104 F(IsNonNegativeSmi, 1, 1) \
105 F(IsArray, 1, 1) \
106 F(IsRegExp, 1, 1) \
107 F(CallFunction, -1 /* receiver + n args + function */, 1) \
108 F(IsConstructCall, 0, 1) \
109 F(ArgumentsLength, 0, 1) \
110 F(Arguments, 1, 1) \
111 F(ClassOf, 1, 1) \
112 F(ValueOf, 1, 1) \
113 F(SetValueOf, 2, 1) \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100114 F(StringCharCodeAt, 2, 1) \
115 F(StringCharFromCode, 1, 1) \
116 F(StringCharAt, 2, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100117 F(ObjectEquals, 2, 1) \
118 F(Log, 3, 1) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100119 F(RandomHeapNumber, 0, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100120 F(IsObject, 1, 1) \
121 F(IsFunction, 1, 1) \
122 F(IsUndetectableObject, 1, 1) \
123 F(StringAdd, 2, 1) \
124 F(SubString, 3, 1) \
125 F(StringCompare, 2, 1) \
126 F(RegExpExec, 4, 1) \
127 F(RegExpConstructResult, 3, 1) \
128 F(GetFromCache, 2, 1) \
129 F(NumberToString, 1, 1) \
130 F(SwapElements, 3, 1) \
131 F(MathPow, 2, 1) \
132 F(MathSin, 1, 1) \
133 F(MathCos, 1, 1) \
134 F(MathSqrt, 1, 1)
135
136
Andrei Popescu31002712010-02-23 13:46:05 +0000137// Support for "structured" code comments.
138#ifdef DEBUG
139
140class Comment BASE_EMBEDDED {
141 public:
142 Comment(MacroAssembler* masm, const char* msg);
143 ~Comment();
144
145 private:
146 MacroAssembler* masm_;
147 const char* msg_;
148};
149
150#else
151
152class Comment BASE_EMBEDDED {
153 public:
154 Comment(MacroAssembler*, const char*) {}
155};
156
157#endif // DEBUG
158
159
Steve Blocka7e24c12009-10-30 11:49:00 +0000160// Code generation can be nested. Code generation scopes form a stack
161// of active code generators.
162class CodeGeneratorScope BASE_EMBEDDED {
163 public:
164 explicit CodeGeneratorScope(CodeGenerator* cgen) {
165 previous_ = top_;
166 top_ = cgen;
167 }
168
169 ~CodeGeneratorScope() {
170 top_ = previous_;
171 }
172
173 static CodeGenerator* Current() {
174 ASSERT(top_ != NULL);
175 return top_;
176 }
177
178 private:
179 static CodeGenerator* top_;
180 CodeGenerator* previous_;
181};
182
183
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100184#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
185
186// State of used registers in a virtual frame.
187class FrameRegisterState {
188 public:
189 // Captures the current state of the given frame.
190 explicit FrameRegisterState(VirtualFrame* frame);
191
192 // Saves the state in the stack.
193 void Save(MacroAssembler* masm) const;
194
195 // Restores the state from the stack.
196 void Restore(MacroAssembler* masm) const;
197
198 private:
199 // Constants indicating special actions. They should not be multiples
200 // of kPointerSize so they will not collide with valid offsets from
201 // the frame pointer.
202 static const int kIgnore = -1;
203 static const int kPush = 1;
204
205 // This flag is ored with a valid offset from the frame pointer, so
206 // it should fit in the low zero bits of a valid offset.
207 static const int kSyncedFlag = 2;
208
209 int registers_[RegisterAllocator::kNumRegisters];
210};
211
212#elif V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS
213
214
215class FrameRegisterState {
216 public:
217 inline FrameRegisterState(VirtualFrame frame) : frame_(frame) { }
218
219 inline const VirtualFrame* frame() const { return &frame_; }
220
221 private:
222 VirtualFrame frame_;
223};
224
225#else
226
227#error Unsupported target architecture.
228
229#endif
230
231
232// Helper interface to prepare to/restore after making runtime calls.
233class RuntimeCallHelper {
234 public:
235 virtual ~RuntimeCallHelper() {}
236
237 virtual void BeforeCall(MacroAssembler* masm) const = 0;
238
239 virtual void AfterCall(MacroAssembler* masm) const = 0;
240
241 protected:
242 RuntimeCallHelper() {}
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
246};
247
248
249// RuntimeCallHelper implementation that saves/restores state of a
250// virtual frame.
251class VirtualFrameRuntimeCallHelper : public RuntimeCallHelper {
252 public:
253 // Does not take ownership of |frame_state|.
254 explicit VirtualFrameRuntimeCallHelper(const FrameRegisterState* frame_state)
255 : frame_state_(frame_state) {}
256
257 virtual void BeforeCall(MacroAssembler* masm) const;
258
259 virtual void AfterCall(MacroAssembler* masm) const;
260
261 private:
262 const FrameRegisterState* frame_state_;
263};
264
265
266// RuntimeCallHelper implementation used in IC stubs: enters/leaves a
267// newly created internal frame before/after the runtime call.
268class ICRuntimeCallHelper : public RuntimeCallHelper {
269 public:
270 ICRuntimeCallHelper() {}
271
272 virtual void BeforeCall(MacroAssembler* masm) const;
273
274 virtual void AfterCall(MacroAssembler* masm) const;
275};
276
277
278// Trivial RuntimeCallHelper implementation.
279class NopRuntimeCallHelper : public RuntimeCallHelper {
280 public:
281 NopRuntimeCallHelper() {}
282
283 virtual void BeforeCall(MacroAssembler* masm) const {}
284
285 virtual void AfterCall(MacroAssembler* masm) const {}
286};
287
288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289// Deferred code objects are small pieces of code that are compiled
290// out of line. They are used to defer the compilation of uncommon
291// paths thereby avoiding expensive jumps around uncommon code parts.
292class DeferredCode: public ZoneObject {
293 public:
294 DeferredCode();
295 virtual ~DeferredCode() { }
296
297 virtual void Generate() = 0;
298
299 MacroAssembler* masm() { return masm_; }
300
301 int statement_position() const { return statement_position_; }
302 int position() const { return position_; }
303
304 Label* entry_label() { return &entry_label_; }
305 Label* exit_label() { return &exit_label_; }
306
307#ifdef DEBUG
308 void set_comment(const char* comment) { comment_ = comment; }
309 const char* comment() const { return comment_; }
310#else
311 void set_comment(const char* comment) { }
312 const char* comment() const { return ""; }
313#endif
314
315 inline void Jump();
316 inline void Branch(Condition cc);
317 void BindExit() { masm_->bind(&exit_label_); }
318
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100319 const FrameRegisterState* frame_state() const { return &frame_state_; }
320
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 void SaveRegisters();
322 void RestoreRegisters();
323
324 protected:
325 MacroAssembler* masm_;
326
327 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 int statement_position_;
329 int position_;
330
331 Label entry_label_;
332 Label exit_label_;
333
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100334 FrameRegisterState frame_state_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
336#ifdef DEBUG
337 const char* comment_;
338#endif
339 DISALLOW_COPY_AND_ASSIGN(DeferredCode);
340};
341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342class StackCheckStub : public CodeStub {
343 public:
344 StackCheckStub() { }
345
346 void Generate(MacroAssembler* masm);
347
348 private:
349
350 const char* GetName() { return "StackCheckStub"; }
351
352 Major MajorKey() { return StackCheck; }
353 int MinorKey() { return 0; }
354};
355
356
Leon Clarkee46be812010-01-19 14:06:41 +0000357class FastNewClosureStub : public CodeStub {
358 public:
359 void Generate(MacroAssembler* masm);
360
361 private:
362 const char* GetName() { return "FastNewClosureStub"; }
363 Major MajorKey() { return FastNewClosure; }
364 int MinorKey() { return 0; }
365};
366
367
368class FastNewContextStub : public CodeStub {
369 public:
370 static const int kMaximumSlots = 64;
371
372 explicit FastNewContextStub(int slots) : slots_(slots) {
373 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
374 }
375
376 void Generate(MacroAssembler* masm);
377
378 private:
379 int slots_;
380
381 const char* GetName() { return "FastNewContextStub"; }
382 Major MajorKey() { return FastNewContext; }
383 int MinorKey() { return slots_; }
384};
385
386
387class FastCloneShallowArrayStub : public CodeStub {
388 public:
389 static const int kMaximumLength = 8;
390
391 explicit FastCloneShallowArrayStub(int length) : length_(length) {
392 ASSERT(length >= 0 && length <= kMaximumLength);
393 }
394
395 void Generate(MacroAssembler* masm);
396
397 private:
398 int length_;
399
400 const char* GetName() { return "FastCloneShallowArrayStub"; }
401 Major MajorKey() { return FastCloneShallowArray; }
402 int MinorKey() { return length_; }
403};
404
405
Steve Blocka7e24c12009-10-30 11:49:00 +0000406class InstanceofStub: public CodeStub {
407 public:
408 InstanceofStub() { }
409
410 void Generate(MacroAssembler* masm);
411
412 private:
413 Major MajorKey() { return Instanceof; }
414 int MinorKey() { return 0; }
415};
416
417
Leon Clarkeac952652010-07-15 11:15:24 +0100418enum NegativeZeroHandling {
419 kStrictNegativeZero,
420 kIgnoreNegativeZero
421};
422
423
Leon Clarkee46be812010-01-19 14:06:41 +0000424class GenericUnaryOpStub : public CodeStub {
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 public:
Leon Clarkeac952652010-07-15 11:15:24 +0100426 GenericUnaryOpStub(Token::Value op,
427 UnaryOverwriteMode overwrite,
428 NegativeZeroHandling negative_zero = kStrictNegativeZero)
429 : op_(op), overwrite_(overwrite), negative_zero_(negative_zero) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
431 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000432 Token::Value op_;
Leon Clarkeac952652010-07-15 11:15:24 +0100433 UnaryOverwriteMode overwrite_;
434 NegativeZeroHandling negative_zero_;
Leon Clarkee46be812010-01-19 14:06:41 +0000435
Leon Clarkeac952652010-07-15 11:15:24 +0100436 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {};
437 class NegativeZeroField: public BitField<NegativeZeroHandling, 1, 1> {};
438 class OpField: public BitField<Token::Value, 2, kMinorBits - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000439
440 Major MajorKey() { return GenericUnaryOp; }
441 int MinorKey() {
Leon Clarkeac952652010-07-15 11:15:24 +0100442 return OpField::encode(op_) |
443 OverwriteField::encode(overwrite_) |
444 NegativeZeroField::encode(negative_zero_);
Leon Clarkee46be812010-01-19 14:06:41 +0000445 }
446
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 void Generate(MacroAssembler* masm);
448
Leon Clarkee46be812010-01-19 14:06:41 +0000449 const char* GetName();
450};
451
452
453enum NaNInformation {
454 kBothCouldBeNaN,
455 kCantBothBeNaN
Steve Blocka7e24c12009-10-30 11:49:00 +0000456};
457
458
459class CompareStub: public CodeStub {
460 public:
Leon Clarkee46be812010-01-19 14:06:41 +0000461 CompareStub(Condition cc,
462 bool strict,
Steve Block6ded16b2010-05-10 14:33:55 +0100463 NaNInformation nan_info = kBothCouldBeNaN,
464 bool include_number_compare = true) :
465 cc_(cc),
466 strict_(strict),
467 never_nan_nan_(nan_info == kCantBothBeNaN),
468 include_number_compare_(include_number_compare),
469 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000470
471 void Generate(MacroAssembler* masm);
472
473 private:
474 Condition cc_;
475 bool strict_;
Leon Clarkee46be812010-01-19 14:06:41 +0000476 // Only used for 'equal' comparisons. Tells the stub that we already know
477 // that at least one side of the comparison is not NaN. This allows the
478 // stub to use object identity in the positive case. We ignore it when
479 // generating the minor key for other comparisons to avoid creating more
480 // stubs.
481 bool never_nan_nan_;
Steve Block6ded16b2010-05-10 14:33:55 +0100482 // Do generate the number comparison code in the stub. Stubs without number
483 // comparison code is used when the number comparison has been inlined, and
484 // the stub will be called if one of the operands is not a number.
485 bool include_number_compare_;
486
487 // Encoding of the minor key CCCCCCCCCCCCCCNS.
488 class StrictField: public BitField<bool, 0, 1> {};
489 class NeverNanNanField: public BitField<bool, 1, 1> {};
490 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
491 class ConditionField: public BitField<int, 3, 13> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000492
493 Major MajorKey() { return Compare; }
494
495 int MinorKey();
496
497 // Branch to the label if the given object isn't a symbol.
498 void BranchIfNonSymbol(MacroAssembler* masm,
499 Label* label,
500 Register object,
501 Register scratch);
502
Leon Clarkee46be812010-01-19 14:06:41 +0000503 // Unfortunately you have to run without snapshots to see most of these
504 // names in the profile since most compare stubs end up in the snapshot.
Steve Block6ded16b2010-05-10 14:33:55 +0100505 char* name_;
Leon Clarkee46be812010-01-19 14:06:41 +0000506 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000507#ifdef DEBUG
508 void Print() {
Steve Block6ded16b2010-05-10 14:33:55 +0100509 PrintF("CompareStub (cc %d), (strict %s), "
510 "(never_nan_nan %s), (number_compare %s)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 static_cast<int>(cc_),
Steve Block6ded16b2010-05-10 14:33:55 +0100512 strict_ ? "true" : "false",
513 never_nan_nan_ ? "true" : "false",
514 include_number_compare_ ? "included" : "not included");
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 }
516#endif
517};
518
519
520class CEntryStub : public CodeStub {
521 public:
Leon Clarke4515c472010-02-03 11:58:03 +0000522 explicit CEntryStub(int result_size,
523 ExitFrame::Mode mode = ExitFrame::MODE_NORMAL)
524 : result_size_(result_size), mode_(mode) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000525
Leon Clarke4515c472010-02-03 11:58:03 +0000526 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
Leon Clarke4515c472010-02-03 11:58:03 +0000528 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 void GenerateCore(MacroAssembler* masm,
530 Label* throw_normal_exception,
531 Label* throw_termination_exception,
532 Label* throw_out_of_memory_exception,
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 bool do_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100534 bool always_allocate_scope,
535 int alignment_skew = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 void GenerateThrowTOS(MacroAssembler* masm);
537 void GenerateThrowUncatchable(MacroAssembler* masm,
538 UncatchableExceptionType type);
Leon Clarke4515c472010-02-03 11:58:03 +0000539
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 // Number of pointers/values returned.
Leon Clarke4515c472010-02-03 11:58:03 +0000541 const int result_size_;
542 const ExitFrame::Mode mode_;
543
544 // Minor key encoding
545 class ExitFrameModeBits: public BitField<ExitFrame::Mode, 0, 1> {};
546 class IndirectResultBits: public BitField<bool, 1, 1> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000547
548 Major MajorKey() { return CEntry; }
549 // Minor key must differ if different result_size_ values means different
550 // code is generated.
551 int MinorKey();
552
553 const char* GetName() { return "CEntryStub"; }
554};
555
556
Steve Blockd0582a62009-12-15 09:54:21 +0000557class ApiGetterEntryStub : public CodeStub {
558 public:
559 ApiGetterEntryStub(Handle<AccessorInfo> info,
560 ApiFunction* fun)
561 : info_(info),
562 fun_(fun) { }
563 void Generate(MacroAssembler* masm);
564 virtual bool has_custom_cache() { return true; }
565 virtual bool GetCustomCache(Code** code_out);
566 virtual void SetCustomCache(Code* value);
567
Steve Block6ded16b2010-05-10 14:33:55 +0100568 static const int kStackSpace = 5;
Steve Blockd0582a62009-12-15 09:54:21 +0000569 static const int kArgc = 4;
570 private:
571 Handle<AccessorInfo> info() { return info_; }
572 ApiFunction* fun() { return fun_; }
573 Major MajorKey() { return NoCache; }
574 int MinorKey() { return 0; }
575 const char* GetName() { return "ApiEntryStub"; }
576 // The accessor info associated with the function.
577 Handle<AccessorInfo> info_;
578 // The function to be called.
579 ApiFunction* fun_;
580};
581
582
Steve Blocka7e24c12009-10-30 11:49:00 +0000583class JSEntryStub : public CodeStub {
584 public:
585 JSEntryStub() { }
586
587 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
588
589 protected:
590 void GenerateBody(MacroAssembler* masm, bool is_construct);
591
592 private:
593 Major MajorKey() { return JSEntry; }
594 int MinorKey() { return 0; }
595
596 const char* GetName() { return "JSEntryStub"; }
597};
598
599
600class JSConstructEntryStub : public JSEntryStub {
601 public:
602 JSConstructEntryStub() { }
603
604 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
605
606 private:
607 int MinorKey() { return 1; }
608
609 const char* GetName() { return "JSConstructEntryStub"; }
610};
611
612
613class ArgumentsAccessStub: public CodeStub {
614 public:
615 enum Type {
Steve Blocka7e24c12009-10-30 11:49:00 +0000616 READ_ELEMENT,
617 NEW_OBJECT
618 };
619
620 explicit ArgumentsAccessStub(Type type) : type_(type) { }
621
622 private:
623 Type type_;
624
625 Major MajorKey() { return ArgumentsAccess; }
626 int MinorKey() { return type_; }
627
628 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000629 void GenerateReadElement(MacroAssembler* masm);
630 void GenerateNewObject(MacroAssembler* masm);
631
632 const char* GetName() { return "ArgumentsAccessStub"; }
633
634#ifdef DEBUG
635 void Print() {
636 PrintF("ArgumentsAccessStub (type %d)\n", type_);
637 }
638#endif
639};
640
641
Leon Clarkee46be812010-01-19 14:06:41 +0000642class RegExpExecStub: public CodeStub {
643 public:
644 RegExpExecStub() { }
645
646 private:
647 Major MajorKey() { return RegExpExec; }
648 int MinorKey() { return 0; }
649
650 void Generate(MacroAssembler* masm);
651
652 const char* GetName() { return "RegExpExecStub"; }
653
654#ifdef DEBUG
655 void Print() {
656 PrintF("RegExpExecStub\n");
657 }
658#endif
659};
660
661
662class CallFunctionStub: public CodeStub {
663 public:
664 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
665 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
666
667 void Generate(MacroAssembler* masm);
668
669 private:
670 int argc_;
671 InLoopFlag in_loop_;
672 CallFunctionFlags flags_;
673
674#ifdef DEBUG
675 void Print() {
676 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
677 argc_,
678 static_cast<int>(in_loop_),
679 static_cast<int>(flags_));
680 }
681#endif
682
Andrei Popescu402d9372010-02-26 13:31:12 +0000683 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Leon Clarkee46be812010-01-19 14:06:41 +0000684 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
685 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000686 class ArgcBits: public BitField<int, 2, 32 - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000687
688 Major MajorKey() { return CallFunction; }
689 int MinorKey() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000690 // Encode the parameters in a unique 32 bit value.
Leon Clarkee46be812010-01-19 14:06:41 +0000691 return InLoopBits::encode(in_loop_)
692 | FlagBits::encode(flags_)
693 | ArgcBits::encode(argc_);
694 }
695
696 InLoopFlag InLoop() { return in_loop_; }
697 bool ReceiverMightBeValue() {
698 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
699 }
700
701 public:
702 static int ExtractArgcFromMinorKey(int minor_key) {
703 return ArgcBits::decode(minor_key);
704 }
705};
706
707
708class ToBooleanStub: public CodeStub {
709 public:
710 ToBooleanStub() { }
711
712 void Generate(MacroAssembler* masm);
713
714 private:
715 Major MajorKey() { return ToBoolean; }
716 int MinorKey() { return 0; }
717};
718
719
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100720enum StringIndexFlags {
721 // Accepts smis or heap numbers.
722 STRING_INDEX_IS_NUMBER,
723
724 // Accepts smis or heap numbers that are valid array indices
725 // (ECMA-262 15.4). Invalid indices are reported as being out of
726 // range.
727 STRING_INDEX_IS_ARRAY_INDEX
728};
729
730
731// Generates code implementing String.prototype.charCodeAt.
732//
733// Only supports the case when the receiver is a string and the index
734// is a number (smi or heap number) that is a valid index into the
735// string. Additional index constraints are specified by the
736// flags. Otherwise, bails out to the provided labels.
737//
738// Register usage: |object| may be changed to another string in a way
739// that doesn't affect charCodeAt/charAt semantics, |index| is
740// preserved, |scratch| and |result| are clobbered.
741class StringCharCodeAtGenerator {
742 public:
743 StringCharCodeAtGenerator(Register object,
744 Register index,
745 Register scratch,
746 Register result,
747 Label* receiver_not_string,
748 Label* index_not_number,
749 Label* index_out_of_range,
750 StringIndexFlags index_flags)
751 : object_(object),
752 index_(index),
753 scratch_(scratch),
754 result_(result),
755 receiver_not_string_(receiver_not_string),
756 index_not_number_(index_not_number),
757 index_out_of_range_(index_out_of_range),
758 index_flags_(index_flags) {
759 ASSERT(!scratch_.is(object_));
760 ASSERT(!scratch_.is(index_));
761 ASSERT(!scratch_.is(result_));
762 ASSERT(!result_.is(object_));
763 ASSERT(!result_.is(index_));
764 }
765
766 // Generates the fast case code. On the fallthrough path |result|
767 // register contains the result.
768 void GenerateFast(MacroAssembler* masm);
769
770 // Generates the slow case code. Must not be naturally
771 // reachable. Expected to be put after a ret instruction (e.g., in
772 // deferred code). Always jumps back to the fast case.
773 void GenerateSlow(MacroAssembler* masm,
774 const RuntimeCallHelper& call_helper);
775
776 private:
777 Register object_;
778 Register index_;
779 Register scratch_;
780 Register result_;
781
782 Label* receiver_not_string_;
783 Label* index_not_number_;
784 Label* index_out_of_range_;
785
786 StringIndexFlags index_flags_;
787
788 Label call_runtime_;
789 Label index_not_smi_;
790 Label got_smi_index_;
791 Label exit_;
792
793 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
794};
795
796
797// Generates code for creating a one-char string from a char code.
798class StringCharFromCodeGenerator {
799 public:
800 StringCharFromCodeGenerator(Register code,
801 Register result)
802 : code_(code),
803 result_(result) {
804 ASSERT(!code_.is(result_));
805 }
806
807 // Generates the fast case code. On the fallthrough path |result|
808 // register contains the result.
809 void GenerateFast(MacroAssembler* masm);
810
811 // Generates the slow case code. Must not be naturally
812 // reachable. Expected to be put after a ret instruction (e.g., in
813 // deferred code). Always jumps back to the fast case.
814 void GenerateSlow(MacroAssembler* masm,
815 const RuntimeCallHelper& call_helper);
816
817 private:
818 Register code_;
819 Register result_;
820
821 Label slow_case_;
822 Label exit_;
823
824 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
825};
826
827
828// Generates code implementing String.prototype.charAt.
829//
830// Only supports the case when the receiver is a string and the index
831// is a number (smi or heap number) that is a valid index into the
832// string. Additional index constraints are specified by the
833// flags. Otherwise, bails out to the provided labels.
834//
835// Register usage: |object| may be changed to another string in a way
836// that doesn't affect charCodeAt/charAt semantics, |index| is
837// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
838class StringCharAtGenerator {
839 public:
840 StringCharAtGenerator(Register object,
841 Register index,
842 Register scratch1,
843 Register scratch2,
844 Register result,
845 Label* receiver_not_string,
846 Label* index_not_number,
847 Label* index_out_of_range,
848 StringIndexFlags index_flags)
849 : char_code_at_generator_(object,
850 index,
851 scratch1,
852 scratch2,
853 receiver_not_string,
854 index_not_number,
855 index_out_of_range,
856 index_flags),
857 char_from_code_generator_(scratch2, result) {}
858
859 // Generates the fast case code. On the fallthrough path |result|
860 // register contains the result.
861 void GenerateFast(MacroAssembler* masm);
862
863 // Generates the slow case code. Must not be naturally
864 // reachable. Expected to be put after a ret instruction (e.g., in
865 // deferred code). Always jumps back to the fast case.
866 void GenerateSlow(MacroAssembler* masm,
867 const RuntimeCallHelper& call_helper);
868
869 private:
870 StringCharCodeAtGenerator char_code_at_generator_;
871 StringCharFromCodeGenerator char_from_code_generator_;
872
873 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
874};
875
876
Steve Blocka7e24c12009-10-30 11:49:00 +0000877} // namespace internal
878} // namespace v8
879
880#endif // V8_CODEGEN_H_