blob: 353e18656a249fb4d6dde4eebbdf4894ecdc5cfa [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
Steve Block6ded16b2010-05-10 14:33:55 +010083#define INLINE_RUNTIME_FUNCTION_LIST(F) \
84 F(IsSmi, 1, 1) \
85 F(IsNonNegativeSmi, 1, 1) \
86 F(IsArray, 1, 1) \
87 F(IsRegExp, 1, 1) \
88 F(CallFunction, -1 /* receiver + n args + function */, 1) \
89 F(IsConstructCall, 0, 1) \
90 F(ArgumentsLength, 0, 1) \
91 F(Arguments, 1, 1) \
92 F(ClassOf, 1, 1) \
93 F(ValueOf, 1, 1) \
94 F(SetValueOf, 2, 1) \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010095 F(StringCharCodeAt, 2, 1) \
96 F(StringCharFromCode, 1, 1) \
97 F(StringCharAt, 2, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +010098 F(ObjectEquals, 2, 1) \
99 F(Log, 3, 1) \
Leon Clarkef7060e22010-06-03 12:02:55 +0100100 F(RandomHeapNumber, 0, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100101 F(IsObject, 1, 1) \
102 F(IsFunction, 1, 1) \
103 F(IsUndetectableObject, 1, 1) \
Iain Merrick75681382010-08-19 15:07:18 +0100104 F(IsSpecObject, 1, 1) \
105 F(IsStringWrapperSafeForDefaultValueOf, 1, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100106 F(StringAdd, 2, 1) \
107 F(SubString, 3, 1) \
108 F(StringCompare, 2, 1) \
109 F(RegExpExec, 4, 1) \
110 F(RegExpConstructResult, 3, 1) \
111 F(GetFromCache, 2, 1) \
112 F(NumberToString, 1, 1) \
113 F(SwapElements, 3, 1) \
114 F(MathPow, 2, 1) \
115 F(MathSin, 1, 1) \
116 F(MathCos, 1, 1) \
Ben Murdochbb769b22010-08-11 14:56:33 +0100117 F(MathSqrt, 1, 1) \
118 F(IsRegExpEquivalent, 2, 1)
Steve Block6ded16b2010-05-10 14:33:55 +0100119
120
Ben Murdochbb769b22010-08-11 14:56:33 +0100121#if V8_TARGET_ARCH_IA32
122#include "ia32/codegen-ia32.h"
123#elif V8_TARGET_ARCH_X64
124#include "x64/codegen-x64.h"
125#elif V8_TARGET_ARCH_ARM
126#include "arm/codegen-arm.h"
127#elif V8_TARGET_ARCH_MIPS
128#include "mips/codegen-mips.h"
129#else
130#error Unsupported target architecture.
131#endif
132
133#include "register-allocator.h"
134
135namespace v8 {
136namespace internal {
137
Andrei Popescu31002712010-02-23 13:46:05 +0000138// Support for "structured" code comments.
139#ifdef DEBUG
140
141class Comment BASE_EMBEDDED {
142 public:
143 Comment(MacroAssembler* masm, const char* msg);
144 ~Comment();
145
146 private:
147 MacroAssembler* masm_;
148 const char* msg_;
149};
150
151#else
152
153class Comment BASE_EMBEDDED {
154 public:
155 Comment(MacroAssembler*, const char*) {}
156};
157
158#endif // DEBUG
159
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161// Code generation can be nested. Code generation scopes form a stack
162// of active code generators.
163class CodeGeneratorScope BASE_EMBEDDED {
164 public:
165 explicit CodeGeneratorScope(CodeGenerator* cgen) {
166 previous_ = top_;
167 top_ = cgen;
168 }
169
170 ~CodeGeneratorScope() {
171 top_ = previous_;
172 }
173
174 static CodeGenerator* Current() {
175 ASSERT(top_ != NULL);
176 return top_;
177 }
178
179 private:
180 static CodeGenerator* top_;
181 CodeGenerator* previous_;
182};
183
Iain Merrick75681382010-08-19 15:07:18 +0100184
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100185#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
186
187// State of used registers in a virtual frame.
188class FrameRegisterState {
189 public:
190 // Captures the current state of the given frame.
191 explicit FrameRegisterState(VirtualFrame* frame);
192
193 // Saves the state in the stack.
194 void Save(MacroAssembler* masm) const;
195
196 // Restores the state from the stack.
197 void Restore(MacroAssembler* masm) const;
198
199 private:
200 // Constants indicating special actions. They should not be multiples
201 // of kPointerSize so they will not collide with valid offsets from
202 // the frame pointer.
203 static const int kIgnore = -1;
204 static const int kPush = 1;
205
206 // This flag is ored with a valid offset from the frame pointer, so
207 // it should fit in the low zero bits of a valid offset.
208 static const int kSyncedFlag = 2;
209
210 int registers_[RegisterAllocator::kNumRegisters];
211};
212
213#elif V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS
214
215
216class FrameRegisterState {
217 public:
218 inline FrameRegisterState(VirtualFrame frame) : frame_(frame) { }
219
220 inline const VirtualFrame* frame() const { return &frame_; }
221
222 private:
223 VirtualFrame frame_;
224};
225
226#else
227
228#error Unsupported target architecture.
229
230#endif
231
232
233// Helper interface to prepare to/restore after making runtime calls.
234class RuntimeCallHelper {
235 public:
236 virtual ~RuntimeCallHelper() {}
237
238 virtual void BeforeCall(MacroAssembler* masm) const = 0;
239
240 virtual void AfterCall(MacroAssembler* masm) const = 0;
241
242 protected:
243 RuntimeCallHelper() {}
244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
247};
248
249
250// RuntimeCallHelper implementation that saves/restores state of a
251// virtual frame.
252class VirtualFrameRuntimeCallHelper : public RuntimeCallHelper {
253 public:
254 // Does not take ownership of |frame_state|.
255 explicit VirtualFrameRuntimeCallHelper(const FrameRegisterState* frame_state)
256 : frame_state_(frame_state) {}
257
258 virtual void BeforeCall(MacroAssembler* masm) const;
259
260 virtual void AfterCall(MacroAssembler* masm) const;
261
262 private:
263 const FrameRegisterState* frame_state_;
264};
265
266
267// RuntimeCallHelper implementation used in IC stubs: enters/leaves a
268// newly created internal frame before/after the runtime call.
269class ICRuntimeCallHelper : public RuntimeCallHelper {
270 public:
271 ICRuntimeCallHelper() {}
272
273 virtual void BeforeCall(MacroAssembler* masm) const;
274
275 virtual void AfterCall(MacroAssembler* masm) const;
276};
277
278
279// Trivial RuntimeCallHelper implementation.
280class NopRuntimeCallHelper : public RuntimeCallHelper {
281 public:
282 NopRuntimeCallHelper() {}
283
284 virtual void BeforeCall(MacroAssembler* masm) const {}
285
286 virtual void AfterCall(MacroAssembler* masm) const {}
287};
288
289
Steve Blocka7e24c12009-10-30 11:49:00 +0000290// Deferred code objects are small pieces of code that are compiled
291// out of line. They are used to defer the compilation of uncommon
292// paths thereby avoiding expensive jumps around uncommon code parts.
293class DeferredCode: public ZoneObject {
294 public:
295 DeferredCode();
296 virtual ~DeferredCode() { }
297
298 virtual void Generate() = 0;
299
300 MacroAssembler* masm() { return masm_; }
301
302 int statement_position() const { return statement_position_; }
303 int position() const { return position_; }
304
305 Label* entry_label() { return &entry_label_; }
306 Label* exit_label() { return &exit_label_; }
307
308#ifdef DEBUG
309 void set_comment(const char* comment) { comment_ = comment; }
310 const char* comment() const { return comment_; }
311#else
312 void set_comment(const char* comment) { }
313 const char* comment() const { return ""; }
314#endif
315
316 inline void Jump();
317 inline void Branch(Condition cc);
318 void BindExit() { masm_->bind(&exit_label_); }
319
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100320 const FrameRegisterState* frame_state() const { return &frame_state_; }
321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 void SaveRegisters();
323 void RestoreRegisters();
Iain Merrick75681382010-08-19 15:07:18 +0100324 void Exit();
325
326 // If this returns true then all registers will be saved for the duration
327 // of the Generate() call. Otherwise the registers are not saved and the
328 // Generate() call must bracket runtime any runtime calls with calls to
329 // SaveRegisters() and RestoreRegisters(). In this case the Generate
330 // method must also call Exit() in order to return to the non-deferred
331 // code.
332 virtual bool AutoSaveAndRestore() { return true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000333
334 protected:
335 MacroAssembler* masm_;
336
337 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 int statement_position_;
339 int position_;
340
341 Label entry_label_;
342 Label exit_label_;
343
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100344 FrameRegisterState frame_state_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
346#ifdef DEBUG
347 const char* comment_;
348#endif
349 DISALLOW_COPY_AND_ASSIGN(DeferredCode);
350};
351
Steve Blocka7e24c12009-10-30 11:49:00 +0000352class StackCheckStub : public CodeStub {
353 public:
354 StackCheckStub() { }
355
356 void Generate(MacroAssembler* masm);
357
358 private:
359
360 const char* GetName() { return "StackCheckStub"; }
361
362 Major MajorKey() { return StackCheck; }
363 int MinorKey() { return 0; }
364};
365
366
Leon Clarkee46be812010-01-19 14:06:41 +0000367class FastNewClosureStub : public CodeStub {
368 public:
369 void Generate(MacroAssembler* masm);
370
371 private:
372 const char* GetName() { return "FastNewClosureStub"; }
373 Major MajorKey() { return FastNewClosure; }
374 int MinorKey() { return 0; }
375};
376
377
378class FastNewContextStub : public CodeStub {
379 public:
380 static const int kMaximumSlots = 64;
381
382 explicit FastNewContextStub(int slots) : slots_(slots) {
383 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
384 }
385
386 void Generate(MacroAssembler* masm);
387
388 private:
389 int slots_;
390
391 const char* GetName() { return "FastNewContextStub"; }
392 Major MajorKey() { return FastNewContext; }
393 int MinorKey() { return slots_; }
394};
395
396
397class FastCloneShallowArrayStub : public CodeStub {
398 public:
Iain Merrick75681382010-08-19 15:07:18 +0100399 // Maximum length of copied elements array.
400 static const int kMaximumClonedLength = 8;
Leon Clarkee46be812010-01-19 14:06:41 +0000401
Iain Merrick75681382010-08-19 15:07:18 +0100402 enum Mode {
403 CLONE_ELEMENTS,
404 COPY_ON_WRITE_ELEMENTS
405 };
406
407 FastCloneShallowArrayStub(Mode mode, int length)
408 : mode_(mode),
409 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
410 ASSERT(length_ >= 0);
411 ASSERT(length_ <= kMaximumClonedLength);
Leon Clarkee46be812010-01-19 14:06:41 +0000412 }
413
414 void Generate(MacroAssembler* masm);
415
416 private:
Iain Merrick75681382010-08-19 15:07:18 +0100417 Mode mode_;
Leon Clarkee46be812010-01-19 14:06:41 +0000418 int length_;
419
420 const char* GetName() { return "FastCloneShallowArrayStub"; }
421 Major MajorKey() { return FastCloneShallowArray; }
Iain Merrick75681382010-08-19 15:07:18 +0100422 int MinorKey() {
423 ASSERT(mode_ == 0 || mode_ == 1);
424 return (length_ << 1) | mode_;
425 }
Leon Clarkee46be812010-01-19 14:06:41 +0000426};
427
428
Steve Blocka7e24c12009-10-30 11:49:00 +0000429class InstanceofStub: public CodeStub {
430 public:
431 InstanceofStub() { }
432
433 void Generate(MacroAssembler* masm);
434
435 private:
436 Major MajorKey() { return Instanceof; }
437 int MinorKey() { return 0; }
438};
439
440
Leon Clarkeac952652010-07-15 11:15:24 +0100441enum NegativeZeroHandling {
442 kStrictNegativeZero,
443 kIgnoreNegativeZero
444};
445
446
Leon Clarkee46be812010-01-19 14:06:41 +0000447class GenericUnaryOpStub : public CodeStub {
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 public:
Leon Clarkeac952652010-07-15 11:15:24 +0100449 GenericUnaryOpStub(Token::Value op,
450 UnaryOverwriteMode overwrite,
451 NegativeZeroHandling negative_zero = kStrictNegativeZero)
452 : op_(op), overwrite_(overwrite), negative_zero_(negative_zero) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000453
454 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000455 Token::Value op_;
Leon Clarkeac952652010-07-15 11:15:24 +0100456 UnaryOverwriteMode overwrite_;
457 NegativeZeroHandling negative_zero_;
Leon Clarkee46be812010-01-19 14:06:41 +0000458
Leon Clarkeac952652010-07-15 11:15:24 +0100459 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {};
460 class NegativeZeroField: public BitField<NegativeZeroHandling, 1, 1> {};
461 class OpField: public BitField<Token::Value, 2, kMinorBits - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000462
463 Major MajorKey() { return GenericUnaryOp; }
464 int MinorKey() {
Leon Clarkeac952652010-07-15 11:15:24 +0100465 return OpField::encode(op_) |
466 OverwriteField::encode(overwrite_) |
467 NegativeZeroField::encode(negative_zero_);
Leon Clarkee46be812010-01-19 14:06:41 +0000468 }
469
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 void Generate(MacroAssembler* masm);
471
Leon Clarkee46be812010-01-19 14:06:41 +0000472 const char* GetName();
473};
474
475
476enum NaNInformation {
477 kBothCouldBeNaN,
478 kCantBothBeNaN
Steve Blocka7e24c12009-10-30 11:49:00 +0000479};
480
481
482class CompareStub: public CodeStub {
483 public:
Leon Clarkee46be812010-01-19 14:06:41 +0000484 CompareStub(Condition cc,
485 bool strict,
Steve Block6ded16b2010-05-10 14:33:55 +0100486 NaNInformation nan_info = kBothCouldBeNaN,
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100487 bool include_number_compare = true,
488 Register lhs = no_reg,
489 Register rhs = no_reg) :
Steve Block6ded16b2010-05-10 14:33:55 +0100490 cc_(cc),
491 strict_(strict),
492 never_nan_nan_(nan_info == kCantBothBeNaN),
493 include_number_compare_(include_number_compare),
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100494 lhs_(lhs),
495 rhs_(rhs),
Steve Block6ded16b2010-05-10 14:33:55 +0100496 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
498 void Generate(MacroAssembler* masm);
499
500 private:
501 Condition cc_;
502 bool strict_;
Leon Clarkee46be812010-01-19 14:06:41 +0000503 // Only used for 'equal' comparisons. Tells the stub that we already know
504 // that at least one side of the comparison is not NaN. This allows the
505 // stub to use object identity in the positive case. We ignore it when
506 // generating the minor key for other comparisons to avoid creating more
507 // stubs.
508 bool never_nan_nan_;
Steve Block6ded16b2010-05-10 14:33:55 +0100509 // Do generate the number comparison code in the stub. Stubs without number
510 // comparison code is used when the number comparison has been inlined, and
511 // the stub will be called if one of the operands is not a number.
512 bool include_number_compare_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100513 // Register holding the left hand side of the comparison if the stub gives
514 // a choice, no_reg otherwise.
515 Register lhs_;
516 // Register holding the right hand side of the comparison if the stub gives
517 // a choice, no_reg otherwise.
518 Register rhs_;
Steve Block6ded16b2010-05-10 14:33:55 +0100519
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100520 // Encoding of the minor key CCCCCCCCCCCCRCNS.
Steve Block6ded16b2010-05-10 14:33:55 +0100521 class StrictField: public BitField<bool, 0, 1> {};
522 class NeverNanNanField: public BitField<bool, 1, 1> {};
523 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100524 class RegisterField: public BitField<bool, 3, 1> {};
525 class ConditionField: public BitField<int, 4, 12> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000526
527 Major MajorKey() { return Compare; }
528
529 int MinorKey();
530
531 // Branch to the label if the given object isn't a symbol.
532 void BranchIfNonSymbol(MacroAssembler* masm,
533 Label* label,
534 Register object,
535 Register scratch);
536
Leon Clarkee46be812010-01-19 14:06:41 +0000537 // Unfortunately you have to run without snapshots to see most of these
538 // names in the profile since most compare stubs end up in the snapshot.
Steve Block6ded16b2010-05-10 14:33:55 +0100539 char* name_;
Leon Clarkee46be812010-01-19 14:06:41 +0000540 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000541#ifdef DEBUG
542 void Print() {
Steve Block6ded16b2010-05-10 14:33:55 +0100543 PrintF("CompareStub (cc %d), (strict %s), "
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100544 "(never_nan_nan %s), (number_compare %s) ",
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 static_cast<int>(cc_),
Steve Block6ded16b2010-05-10 14:33:55 +0100546 strict_ ? "true" : "false",
547 never_nan_nan_ ? "true" : "false",
548 include_number_compare_ ? "included" : "not included");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100549
550 if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) {
551 PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code());
552 } else {
553 PrintF("\n");
554 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 }
556#endif
557};
558
559
560class CEntryStub : public CodeStub {
561 public:
Leon Clarke4515c472010-02-03 11:58:03 +0000562 explicit CEntryStub(int result_size,
563 ExitFrame::Mode mode = ExitFrame::MODE_NORMAL)
564 : result_size_(result_size), mode_(mode) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000565
Leon Clarke4515c472010-02-03 11:58:03 +0000566 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000567
Leon Clarke4515c472010-02-03 11:58:03 +0000568 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000569 void GenerateCore(MacroAssembler* masm,
570 Label* throw_normal_exception,
571 Label* throw_termination_exception,
572 Label* throw_out_of_memory_exception,
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 bool do_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100574 bool always_allocate_scope,
575 int alignment_skew = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 void GenerateThrowTOS(MacroAssembler* masm);
577 void GenerateThrowUncatchable(MacroAssembler* masm,
578 UncatchableExceptionType type);
Leon Clarke4515c472010-02-03 11:58:03 +0000579
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 // Number of pointers/values returned.
Leon Clarke4515c472010-02-03 11:58:03 +0000581 const int result_size_;
582 const ExitFrame::Mode mode_;
583
584 // Minor key encoding
585 class ExitFrameModeBits: public BitField<ExitFrame::Mode, 0, 1> {};
586 class IndirectResultBits: public BitField<bool, 1, 1> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000587
588 Major MajorKey() { return CEntry; }
589 // Minor key must differ if different result_size_ values means different
590 // code is generated.
591 int MinorKey();
592
593 const char* GetName() { return "CEntryStub"; }
594};
595
596
Steve Blockd0582a62009-12-15 09:54:21 +0000597class ApiGetterEntryStub : public CodeStub {
598 public:
599 ApiGetterEntryStub(Handle<AccessorInfo> info,
600 ApiFunction* fun)
601 : info_(info),
602 fun_(fun) { }
603 void Generate(MacroAssembler* masm);
604 virtual bool has_custom_cache() { return true; }
605 virtual bool GetCustomCache(Code** code_out);
606 virtual void SetCustomCache(Code* value);
607
Steve Block6ded16b2010-05-10 14:33:55 +0100608 static const int kStackSpace = 5;
Steve Blockd0582a62009-12-15 09:54:21 +0000609 static const int kArgc = 4;
610 private:
611 Handle<AccessorInfo> info() { return info_; }
612 ApiFunction* fun() { return fun_; }
613 Major MajorKey() { return NoCache; }
614 int MinorKey() { return 0; }
615 const char* GetName() { return "ApiEntryStub"; }
616 // The accessor info associated with the function.
617 Handle<AccessorInfo> info_;
618 // The function to be called.
619 ApiFunction* fun_;
620};
621
622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623class JSEntryStub : public CodeStub {
624 public:
625 JSEntryStub() { }
626
627 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
628
629 protected:
630 void GenerateBody(MacroAssembler* masm, bool is_construct);
631
632 private:
633 Major MajorKey() { return JSEntry; }
634 int MinorKey() { return 0; }
635
636 const char* GetName() { return "JSEntryStub"; }
637};
638
639
640class JSConstructEntryStub : public JSEntryStub {
641 public:
642 JSConstructEntryStub() { }
643
644 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
645
646 private:
647 int MinorKey() { return 1; }
648
649 const char* GetName() { return "JSConstructEntryStub"; }
650};
651
652
653class ArgumentsAccessStub: public CodeStub {
654 public:
655 enum Type {
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 READ_ELEMENT,
657 NEW_OBJECT
658 };
659
660 explicit ArgumentsAccessStub(Type type) : type_(type) { }
661
662 private:
663 Type type_;
664
665 Major MajorKey() { return ArgumentsAccess; }
666 int MinorKey() { return type_; }
667
668 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 void GenerateReadElement(MacroAssembler* masm);
670 void GenerateNewObject(MacroAssembler* masm);
671
672 const char* GetName() { return "ArgumentsAccessStub"; }
673
674#ifdef DEBUG
675 void Print() {
676 PrintF("ArgumentsAccessStub (type %d)\n", type_);
677 }
678#endif
679};
680
681
Leon Clarkee46be812010-01-19 14:06:41 +0000682class RegExpExecStub: public CodeStub {
683 public:
684 RegExpExecStub() { }
685
686 private:
687 Major MajorKey() { return RegExpExec; }
688 int MinorKey() { return 0; }
689
690 void Generate(MacroAssembler* masm);
691
692 const char* GetName() { return "RegExpExecStub"; }
693
694#ifdef DEBUG
695 void Print() {
696 PrintF("RegExpExecStub\n");
697 }
698#endif
699};
700
701
702class CallFunctionStub: public CodeStub {
703 public:
704 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
705 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
706
707 void Generate(MacroAssembler* masm);
708
709 private:
710 int argc_;
711 InLoopFlag in_loop_;
712 CallFunctionFlags flags_;
713
714#ifdef DEBUG
715 void Print() {
716 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
717 argc_,
718 static_cast<int>(in_loop_),
719 static_cast<int>(flags_));
720 }
721#endif
722
Andrei Popescu402d9372010-02-26 13:31:12 +0000723 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Leon Clarkee46be812010-01-19 14:06:41 +0000724 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
725 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000726 class ArgcBits: public BitField<int, 2, 32 - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000727
728 Major MajorKey() { return CallFunction; }
729 int MinorKey() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000730 // Encode the parameters in a unique 32 bit value.
Leon Clarkee46be812010-01-19 14:06:41 +0000731 return InLoopBits::encode(in_loop_)
732 | FlagBits::encode(flags_)
733 | ArgcBits::encode(argc_);
734 }
735
736 InLoopFlag InLoop() { return in_loop_; }
737 bool ReceiverMightBeValue() {
738 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
739 }
740
741 public:
742 static int ExtractArgcFromMinorKey(int minor_key) {
743 return ArgcBits::decode(minor_key);
744 }
745};
746
747
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100748enum StringIndexFlags {
749 // Accepts smis or heap numbers.
750 STRING_INDEX_IS_NUMBER,
751
752 // Accepts smis or heap numbers that are valid array indices
753 // (ECMA-262 15.4). Invalid indices are reported as being out of
754 // range.
755 STRING_INDEX_IS_ARRAY_INDEX
756};
757
758
759// Generates code implementing String.prototype.charCodeAt.
760//
761// Only supports the case when the receiver is a string and the index
762// is a number (smi or heap number) that is a valid index into the
763// string. Additional index constraints are specified by the
764// flags. Otherwise, bails out to the provided labels.
765//
766// Register usage: |object| may be changed to another string in a way
767// that doesn't affect charCodeAt/charAt semantics, |index| is
768// preserved, |scratch| and |result| are clobbered.
769class StringCharCodeAtGenerator {
770 public:
771 StringCharCodeAtGenerator(Register object,
772 Register index,
773 Register scratch,
774 Register result,
775 Label* receiver_not_string,
776 Label* index_not_number,
777 Label* index_out_of_range,
778 StringIndexFlags index_flags)
779 : object_(object),
780 index_(index),
781 scratch_(scratch),
782 result_(result),
783 receiver_not_string_(receiver_not_string),
784 index_not_number_(index_not_number),
785 index_out_of_range_(index_out_of_range),
786 index_flags_(index_flags) {
787 ASSERT(!scratch_.is(object_));
788 ASSERT(!scratch_.is(index_));
789 ASSERT(!scratch_.is(result_));
790 ASSERT(!result_.is(object_));
791 ASSERT(!result_.is(index_));
792 }
793
794 // Generates the fast case code. On the fallthrough path |result|
795 // register contains the result.
796 void GenerateFast(MacroAssembler* masm);
797
798 // Generates the slow case code. Must not be naturally
799 // reachable. Expected to be put after a ret instruction (e.g., in
800 // deferred code). Always jumps back to the fast case.
801 void GenerateSlow(MacroAssembler* masm,
802 const RuntimeCallHelper& call_helper);
803
804 private:
805 Register object_;
806 Register index_;
807 Register scratch_;
808 Register result_;
809
810 Label* receiver_not_string_;
811 Label* index_not_number_;
812 Label* index_out_of_range_;
813
814 StringIndexFlags index_flags_;
815
816 Label call_runtime_;
817 Label index_not_smi_;
818 Label got_smi_index_;
819 Label exit_;
820
821 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
822};
823
824
825// Generates code for creating a one-char string from a char code.
826class StringCharFromCodeGenerator {
827 public:
828 StringCharFromCodeGenerator(Register code,
829 Register result)
830 : code_(code),
831 result_(result) {
832 ASSERT(!code_.is(result_));
833 }
834
835 // Generates the fast case code. On the fallthrough path |result|
836 // register contains the result.
837 void GenerateFast(MacroAssembler* masm);
838
839 // Generates the slow case code. Must not be naturally
840 // reachable. Expected to be put after a ret instruction (e.g., in
841 // deferred code). Always jumps back to the fast case.
842 void GenerateSlow(MacroAssembler* masm,
843 const RuntimeCallHelper& call_helper);
844
845 private:
846 Register code_;
847 Register result_;
848
849 Label slow_case_;
850 Label exit_;
851
852 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
853};
854
855
856// Generates code implementing String.prototype.charAt.
857//
858// Only supports the case when the receiver is a string and the index
859// is a number (smi or heap number) that is a valid index into the
860// string. Additional index constraints are specified by the
861// flags. Otherwise, bails out to the provided labels.
862//
863// Register usage: |object| may be changed to another string in a way
864// that doesn't affect charCodeAt/charAt semantics, |index| is
865// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
866class StringCharAtGenerator {
867 public:
868 StringCharAtGenerator(Register object,
869 Register index,
870 Register scratch1,
871 Register scratch2,
872 Register result,
873 Label* receiver_not_string,
874 Label* index_not_number,
875 Label* index_out_of_range,
876 StringIndexFlags index_flags)
877 : char_code_at_generator_(object,
878 index,
879 scratch1,
880 scratch2,
881 receiver_not_string,
882 index_not_number,
883 index_out_of_range,
884 index_flags),
885 char_from_code_generator_(scratch2, result) {}
886
887 // Generates the fast case code. On the fallthrough path |result|
888 // register contains the result.
889 void GenerateFast(MacroAssembler* masm);
890
891 // Generates the slow case code. Must not be naturally
892 // reachable. Expected to be put after a ret instruction (e.g., in
893 // deferred code). Always jumps back to the fast case.
894 void GenerateSlow(MacroAssembler* masm,
895 const RuntimeCallHelper& call_helper);
896
897 private:
898 StringCharCodeAtGenerator char_code_at_generator_;
899 StringCharFromCodeGenerator char_from_code_generator_;
900
901 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
902};
903
904
Steve Blocka7e24c12009-10-30 11:49:00 +0000905} // namespace internal
906} // namespace v8
907
908#endif // V8_CODEGEN_H_