blob: 56c175e43a55d03bfc6caae0e5a455b7a0435618 [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) \
Steve Block791712a2010-08-27 10:21:07 +0100111 F(RegExpCloneResult, 1, 1) \
Steve Block6ded16b2010-05-10 14:33:55 +0100112 F(GetFromCache, 2, 1) \
113 F(NumberToString, 1, 1) \
114 F(SwapElements, 3, 1) \
115 F(MathPow, 2, 1) \
116 F(MathSin, 1, 1) \
117 F(MathCos, 1, 1) \
Ben Murdochbb769b22010-08-11 14:56:33 +0100118 F(MathSqrt, 1, 1) \
119 F(IsRegExpEquivalent, 2, 1)
Steve Block6ded16b2010-05-10 14:33:55 +0100120
121
Ben Murdochbb769b22010-08-11 14:56:33 +0100122#if V8_TARGET_ARCH_IA32
123#include "ia32/codegen-ia32.h"
124#elif V8_TARGET_ARCH_X64
125#include "x64/codegen-x64.h"
126#elif V8_TARGET_ARCH_ARM
127#include "arm/codegen-arm.h"
128#elif V8_TARGET_ARCH_MIPS
129#include "mips/codegen-mips.h"
130#else
131#error Unsupported target architecture.
132#endif
133
134#include "register-allocator.h"
135
136namespace v8 {
137namespace internal {
138
Andrei Popescu31002712010-02-23 13:46:05 +0000139// Support for "structured" code comments.
140#ifdef DEBUG
141
142class Comment BASE_EMBEDDED {
143 public:
144 Comment(MacroAssembler* masm, const char* msg);
145 ~Comment();
146
147 private:
148 MacroAssembler* masm_;
149 const char* msg_;
150};
151
152#else
153
154class Comment BASE_EMBEDDED {
155 public:
156 Comment(MacroAssembler*, const char*) {}
157};
158
159#endif // DEBUG
160
161
Steve Blocka7e24c12009-10-30 11:49:00 +0000162// Code generation can be nested. Code generation scopes form a stack
163// of active code generators.
164class CodeGeneratorScope BASE_EMBEDDED {
165 public:
166 explicit CodeGeneratorScope(CodeGenerator* cgen) {
167 previous_ = top_;
168 top_ = cgen;
169 }
170
171 ~CodeGeneratorScope() {
172 top_ = previous_;
173 }
174
175 static CodeGenerator* Current() {
176 ASSERT(top_ != NULL);
177 return top_;
178 }
179
180 private:
181 static CodeGenerator* top_;
182 CodeGenerator* previous_;
183};
184
Iain Merrick75681382010-08-19 15:07:18 +0100185
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100186#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64
187
188// State of used registers in a virtual frame.
189class FrameRegisterState {
190 public:
191 // Captures the current state of the given frame.
192 explicit FrameRegisterState(VirtualFrame* frame);
193
194 // Saves the state in the stack.
195 void Save(MacroAssembler* masm) const;
196
197 // Restores the state from the stack.
198 void Restore(MacroAssembler* masm) const;
199
200 private:
201 // Constants indicating special actions. They should not be multiples
202 // of kPointerSize so they will not collide with valid offsets from
203 // the frame pointer.
204 static const int kIgnore = -1;
205 static const int kPush = 1;
206
207 // This flag is ored with a valid offset from the frame pointer, so
208 // it should fit in the low zero bits of a valid offset.
209 static const int kSyncedFlag = 2;
210
211 int registers_[RegisterAllocator::kNumRegisters];
212};
213
214#elif V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_MIPS
215
216
217class FrameRegisterState {
218 public:
219 inline FrameRegisterState(VirtualFrame frame) : frame_(frame) { }
220
221 inline const VirtualFrame* frame() const { return &frame_; }
222
223 private:
224 VirtualFrame frame_;
225};
226
227#else
228
229#error Unsupported target architecture.
230
231#endif
232
233
234// Helper interface to prepare to/restore after making runtime calls.
235class RuntimeCallHelper {
236 public:
237 virtual ~RuntimeCallHelper() {}
238
239 virtual void BeforeCall(MacroAssembler* masm) const = 0;
240
241 virtual void AfterCall(MacroAssembler* masm) const = 0;
242
243 protected:
244 RuntimeCallHelper() {}
245
246 private:
247 DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
248};
249
250
251// RuntimeCallHelper implementation that saves/restores state of a
252// virtual frame.
253class VirtualFrameRuntimeCallHelper : public RuntimeCallHelper {
254 public:
255 // Does not take ownership of |frame_state|.
256 explicit VirtualFrameRuntimeCallHelper(const FrameRegisterState* frame_state)
257 : frame_state_(frame_state) {}
258
259 virtual void BeforeCall(MacroAssembler* masm) const;
260
261 virtual void AfterCall(MacroAssembler* masm) const;
262
263 private:
264 const FrameRegisterState* frame_state_;
265};
266
267
268// RuntimeCallHelper implementation used in IC stubs: enters/leaves a
269// newly created internal frame before/after the runtime call.
270class ICRuntimeCallHelper : public RuntimeCallHelper {
271 public:
272 ICRuntimeCallHelper() {}
273
274 virtual void BeforeCall(MacroAssembler* masm) const;
275
276 virtual void AfterCall(MacroAssembler* masm) const;
277};
278
279
280// Trivial RuntimeCallHelper implementation.
281class NopRuntimeCallHelper : public RuntimeCallHelper {
282 public:
283 NopRuntimeCallHelper() {}
284
285 virtual void BeforeCall(MacroAssembler* masm) const {}
286
287 virtual void AfterCall(MacroAssembler* masm) const {}
288};
289
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291// Deferred code objects are small pieces of code that are compiled
292// out of line. They are used to defer the compilation of uncommon
293// paths thereby avoiding expensive jumps around uncommon code parts.
294class DeferredCode: public ZoneObject {
295 public:
296 DeferredCode();
297 virtual ~DeferredCode() { }
298
299 virtual void Generate() = 0;
300
301 MacroAssembler* masm() { return masm_; }
302
303 int statement_position() const { return statement_position_; }
304 int position() const { return position_; }
305
306 Label* entry_label() { return &entry_label_; }
307 Label* exit_label() { return &exit_label_; }
308
309#ifdef DEBUG
310 void set_comment(const char* comment) { comment_ = comment; }
311 const char* comment() const { return comment_; }
312#else
313 void set_comment(const char* comment) { }
314 const char* comment() const { return ""; }
315#endif
316
317 inline void Jump();
318 inline void Branch(Condition cc);
319 void BindExit() { masm_->bind(&exit_label_); }
320
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100321 const FrameRegisterState* frame_state() const { return &frame_state_; }
322
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 void SaveRegisters();
324 void RestoreRegisters();
Iain Merrick75681382010-08-19 15:07:18 +0100325 void Exit();
326
327 // If this returns true then all registers will be saved for the duration
328 // of the Generate() call. Otherwise the registers are not saved and the
329 // Generate() call must bracket runtime any runtime calls with calls to
330 // SaveRegisters() and RestoreRegisters(). In this case the Generate
331 // method must also call Exit() in order to return to the non-deferred
332 // code.
333 virtual bool AutoSaveAndRestore() { return true; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000334
335 protected:
336 MacroAssembler* masm_;
337
338 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 int statement_position_;
340 int position_;
341
342 Label entry_label_;
343 Label exit_label_;
344
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100345 FrameRegisterState frame_state_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000346
347#ifdef DEBUG
348 const char* comment_;
349#endif
350 DISALLOW_COPY_AND_ASSIGN(DeferredCode);
351};
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353class StackCheckStub : public CodeStub {
354 public:
355 StackCheckStub() { }
356
357 void Generate(MacroAssembler* masm);
358
359 private:
360
361 const char* GetName() { return "StackCheckStub"; }
362
363 Major MajorKey() { return StackCheck; }
364 int MinorKey() { return 0; }
365};
366
367
Leon Clarkee46be812010-01-19 14:06:41 +0000368class FastNewClosureStub : public CodeStub {
369 public:
370 void Generate(MacroAssembler* masm);
371
372 private:
373 const char* GetName() { return "FastNewClosureStub"; }
374 Major MajorKey() { return FastNewClosure; }
375 int MinorKey() { return 0; }
376};
377
378
379class FastNewContextStub : public CodeStub {
380 public:
381 static const int kMaximumSlots = 64;
382
383 explicit FastNewContextStub(int slots) : slots_(slots) {
384 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
385 }
386
387 void Generate(MacroAssembler* masm);
388
389 private:
390 int slots_;
391
392 const char* GetName() { return "FastNewContextStub"; }
393 Major MajorKey() { return FastNewContext; }
394 int MinorKey() { return slots_; }
395};
396
397
398class FastCloneShallowArrayStub : public CodeStub {
399 public:
Iain Merrick75681382010-08-19 15:07:18 +0100400 // Maximum length of copied elements array.
401 static const int kMaximumClonedLength = 8;
Leon Clarkee46be812010-01-19 14:06:41 +0000402
Iain Merrick75681382010-08-19 15:07:18 +0100403 enum Mode {
404 CLONE_ELEMENTS,
405 COPY_ON_WRITE_ELEMENTS
406 };
407
408 FastCloneShallowArrayStub(Mode mode, int length)
409 : mode_(mode),
410 length_((mode == COPY_ON_WRITE_ELEMENTS) ? 0 : length) {
411 ASSERT(length_ >= 0);
412 ASSERT(length_ <= kMaximumClonedLength);
Leon Clarkee46be812010-01-19 14:06:41 +0000413 }
414
415 void Generate(MacroAssembler* masm);
416
417 private:
Iain Merrick75681382010-08-19 15:07:18 +0100418 Mode mode_;
Leon Clarkee46be812010-01-19 14:06:41 +0000419 int length_;
420
421 const char* GetName() { return "FastCloneShallowArrayStub"; }
422 Major MajorKey() { return FastCloneShallowArray; }
Iain Merrick75681382010-08-19 15:07:18 +0100423 int MinorKey() {
424 ASSERT(mode_ == 0 || mode_ == 1);
425 return (length_ << 1) | mode_;
426 }
Leon Clarkee46be812010-01-19 14:06:41 +0000427};
428
429
Steve Blocka7e24c12009-10-30 11:49:00 +0000430class InstanceofStub: public CodeStub {
431 public:
432 InstanceofStub() { }
433
434 void Generate(MacroAssembler* masm);
435
436 private:
437 Major MajorKey() { return Instanceof; }
438 int MinorKey() { return 0; }
439};
440
441
Leon Clarkeac952652010-07-15 11:15:24 +0100442enum NegativeZeroHandling {
443 kStrictNegativeZero,
444 kIgnoreNegativeZero
445};
446
447
Leon Clarkee46be812010-01-19 14:06:41 +0000448class GenericUnaryOpStub : public CodeStub {
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 public:
Leon Clarkeac952652010-07-15 11:15:24 +0100450 GenericUnaryOpStub(Token::Value op,
451 UnaryOverwriteMode overwrite,
452 NegativeZeroHandling negative_zero = kStrictNegativeZero)
453 : op_(op), overwrite_(overwrite), negative_zero_(negative_zero) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000454
455 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000456 Token::Value op_;
Leon Clarkeac952652010-07-15 11:15:24 +0100457 UnaryOverwriteMode overwrite_;
458 NegativeZeroHandling negative_zero_;
Leon Clarkee46be812010-01-19 14:06:41 +0000459
Leon Clarkeac952652010-07-15 11:15:24 +0100460 class OverwriteField: public BitField<UnaryOverwriteMode, 0, 1> {};
461 class NegativeZeroField: public BitField<NegativeZeroHandling, 1, 1> {};
462 class OpField: public BitField<Token::Value, 2, kMinorBits - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000463
464 Major MajorKey() { return GenericUnaryOp; }
465 int MinorKey() {
Leon Clarkeac952652010-07-15 11:15:24 +0100466 return OpField::encode(op_) |
467 OverwriteField::encode(overwrite_) |
468 NegativeZeroField::encode(negative_zero_);
Leon Clarkee46be812010-01-19 14:06:41 +0000469 }
470
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 void Generate(MacroAssembler* masm);
472
Leon Clarkee46be812010-01-19 14:06:41 +0000473 const char* GetName();
474};
475
476
477enum NaNInformation {
478 kBothCouldBeNaN,
479 kCantBothBeNaN
Steve Blocka7e24c12009-10-30 11:49:00 +0000480};
481
482
483class CompareStub: public CodeStub {
484 public:
Leon Clarkee46be812010-01-19 14:06:41 +0000485 CompareStub(Condition cc,
486 bool strict,
Steve Block6ded16b2010-05-10 14:33:55 +0100487 NaNInformation nan_info = kBothCouldBeNaN,
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100488 bool include_number_compare = true,
489 Register lhs = no_reg,
490 Register rhs = no_reg) :
Steve Block6ded16b2010-05-10 14:33:55 +0100491 cc_(cc),
492 strict_(strict),
493 never_nan_nan_(nan_info == kCantBothBeNaN),
494 include_number_compare_(include_number_compare),
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100495 lhs_(lhs),
496 rhs_(rhs),
Steve Block6ded16b2010-05-10 14:33:55 +0100497 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000498
499 void Generate(MacroAssembler* masm);
500
501 private:
502 Condition cc_;
503 bool strict_;
Leon Clarkee46be812010-01-19 14:06:41 +0000504 // Only used for 'equal' comparisons. Tells the stub that we already know
505 // that at least one side of the comparison is not NaN. This allows the
506 // stub to use object identity in the positive case. We ignore it when
507 // generating the minor key for other comparisons to avoid creating more
508 // stubs.
509 bool never_nan_nan_;
Steve Block6ded16b2010-05-10 14:33:55 +0100510 // Do generate the number comparison code in the stub. Stubs without number
511 // comparison code is used when the number comparison has been inlined, and
512 // the stub will be called if one of the operands is not a number.
513 bool include_number_compare_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100514 // Register holding the left hand side of the comparison if the stub gives
515 // a choice, no_reg otherwise.
516 Register lhs_;
517 // Register holding the right hand side of the comparison if the stub gives
518 // a choice, no_reg otherwise.
519 Register rhs_;
Steve Block6ded16b2010-05-10 14:33:55 +0100520
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100521 // Encoding of the minor key CCCCCCCCCCCCRCNS.
Steve Block6ded16b2010-05-10 14:33:55 +0100522 class StrictField: public BitField<bool, 0, 1> {};
523 class NeverNanNanField: public BitField<bool, 1, 1> {};
524 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100525 class RegisterField: public BitField<bool, 3, 1> {};
526 class ConditionField: public BitField<int, 4, 12> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
528 Major MajorKey() { return Compare; }
529
530 int MinorKey();
531
532 // Branch to the label if the given object isn't a symbol.
533 void BranchIfNonSymbol(MacroAssembler* masm,
534 Label* label,
535 Register object,
536 Register scratch);
537
Leon Clarkee46be812010-01-19 14:06:41 +0000538 // Unfortunately you have to run without snapshots to see most of these
539 // names in the profile since most compare stubs end up in the snapshot.
Steve Block6ded16b2010-05-10 14:33:55 +0100540 char* name_;
Leon Clarkee46be812010-01-19 14:06:41 +0000541 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000542#ifdef DEBUG
543 void Print() {
Steve Block6ded16b2010-05-10 14:33:55 +0100544 PrintF("CompareStub (cc %d), (strict %s), "
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100545 "(never_nan_nan %s), (number_compare %s) ",
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 static_cast<int>(cc_),
Steve Block6ded16b2010-05-10 14:33:55 +0100547 strict_ ? "true" : "false",
548 never_nan_nan_ ? "true" : "false",
549 include_number_compare_ ? "included" : "not included");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100550
551 if (!lhs_.is(no_reg) && !rhs_.is(no_reg)) {
552 PrintF("(lhs r%d), (rhs r%d)\n", lhs_.code(), rhs_.code());
553 } else {
554 PrintF("\n");
555 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 }
557#endif
558};
559
560
561class CEntryStub : public CodeStub {
562 public:
Leon Clarke4515c472010-02-03 11:58:03 +0000563 explicit CEntryStub(int result_size,
564 ExitFrame::Mode mode = ExitFrame::MODE_NORMAL)
565 : result_size_(result_size), mode_(mode) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000566
Leon Clarke4515c472010-02-03 11:58:03 +0000567 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
Leon Clarke4515c472010-02-03 11:58:03 +0000569 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 void GenerateCore(MacroAssembler* masm,
571 Label* throw_normal_exception,
572 Label* throw_termination_exception,
573 Label* throw_out_of_memory_exception,
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 bool do_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100575 bool always_allocate_scope,
576 int alignment_skew = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000577 void GenerateThrowTOS(MacroAssembler* masm);
578 void GenerateThrowUncatchable(MacroAssembler* masm,
579 UncatchableExceptionType type);
Leon Clarke4515c472010-02-03 11:58:03 +0000580
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 // Number of pointers/values returned.
Leon Clarke4515c472010-02-03 11:58:03 +0000582 const int result_size_;
583 const ExitFrame::Mode mode_;
584
585 // Minor key encoding
586 class ExitFrameModeBits: public BitField<ExitFrame::Mode, 0, 1> {};
587 class IndirectResultBits: public BitField<bool, 1, 1> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000588
589 Major MajorKey() { return CEntry; }
590 // Minor key must differ if different result_size_ values means different
591 // code is generated.
592 int MinorKey();
593
594 const char* GetName() { return "CEntryStub"; }
595};
596
597
Steve Blockd0582a62009-12-15 09:54:21 +0000598class ApiGetterEntryStub : public CodeStub {
599 public:
600 ApiGetterEntryStub(Handle<AccessorInfo> info,
601 ApiFunction* fun)
602 : info_(info),
603 fun_(fun) { }
604 void Generate(MacroAssembler* masm);
605 virtual bool has_custom_cache() { return true; }
606 virtual bool GetCustomCache(Code** code_out);
607 virtual void SetCustomCache(Code* value);
608
Steve Block6ded16b2010-05-10 14:33:55 +0100609 static const int kStackSpace = 5;
Steve Blockd0582a62009-12-15 09:54:21 +0000610 static const int kArgc = 4;
611 private:
612 Handle<AccessorInfo> info() { return info_; }
613 ApiFunction* fun() { return fun_; }
614 Major MajorKey() { return NoCache; }
615 int MinorKey() { return 0; }
616 const char* GetName() { return "ApiEntryStub"; }
617 // The accessor info associated with the function.
618 Handle<AccessorInfo> info_;
619 // The function to be called.
620 ApiFunction* fun_;
621};
622
623
Steve Blocka7e24c12009-10-30 11:49:00 +0000624class JSEntryStub : public CodeStub {
625 public:
626 JSEntryStub() { }
627
628 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
629
630 protected:
631 void GenerateBody(MacroAssembler* masm, bool is_construct);
632
633 private:
634 Major MajorKey() { return JSEntry; }
635 int MinorKey() { return 0; }
636
637 const char* GetName() { return "JSEntryStub"; }
638};
639
640
641class JSConstructEntryStub : public JSEntryStub {
642 public:
643 JSConstructEntryStub() { }
644
645 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
646
647 private:
648 int MinorKey() { return 1; }
649
650 const char* GetName() { return "JSConstructEntryStub"; }
651};
652
653
654class ArgumentsAccessStub: public CodeStub {
655 public:
656 enum Type {
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 READ_ELEMENT,
658 NEW_OBJECT
659 };
660
661 explicit ArgumentsAccessStub(Type type) : type_(type) { }
662
663 private:
664 Type type_;
665
666 Major MajorKey() { return ArgumentsAccess; }
667 int MinorKey() { return type_; }
668
669 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000670 void GenerateReadElement(MacroAssembler* masm);
671 void GenerateNewObject(MacroAssembler* masm);
672
673 const char* GetName() { return "ArgumentsAccessStub"; }
674
675#ifdef DEBUG
676 void Print() {
677 PrintF("ArgumentsAccessStub (type %d)\n", type_);
678 }
679#endif
680};
681
682
Leon Clarkee46be812010-01-19 14:06:41 +0000683class RegExpExecStub: public CodeStub {
684 public:
685 RegExpExecStub() { }
686
687 private:
688 Major MajorKey() { return RegExpExec; }
689 int MinorKey() { return 0; }
690
691 void Generate(MacroAssembler* masm);
692
693 const char* GetName() { return "RegExpExecStub"; }
694
695#ifdef DEBUG
696 void Print() {
697 PrintF("RegExpExecStub\n");
698 }
699#endif
700};
701
702
703class CallFunctionStub: public CodeStub {
704 public:
705 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
706 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
707
708 void Generate(MacroAssembler* masm);
709
710 private:
711 int argc_;
712 InLoopFlag in_loop_;
713 CallFunctionFlags flags_;
714
715#ifdef DEBUG
716 void Print() {
717 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
718 argc_,
719 static_cast<int>(in_loop_),
720 static_cast<int>(flags_));
721 }
722#endif
723
Andrei Popescu402d9372010-02-26 13:31:12 +0000724 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Leon Clarkee46be812010-01-19 14:06:41 +0000725 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
726 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000727 class ArgcBits: public BitField<int, 2, 32 - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000728
729 Major MajorKey() { return CallFunction; }
730 int MinorKey() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000731 // Encode the parameters in a unique 32 bit value.
Leon Clarkee46be812010-01-19 14:06:41 +0000732 return InLoopBits::encode(in_loop_)
733 | FlagBits::encode(flags_)
734 | ArgcBits::encode(argc_);
735 }
736
737 InLoopFlag InLoop() { return in_loop_; }
738 bool ReceiverMightBeValue() {
739 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
740 }
741
742 public:
743 static int ExtractArgcFromMinorKey(int minor_key) {
744 return ArgcBits::decode(minor_key);
745 }
746};
747
748
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100749enum StringIndexFlags {
750 // Accepts smis or heap numbers.
751 STRING_INDEX_IS_NUMBER,
752
753 // Accepts smis or heap numbers that are valid array indices
754 // (ECMA-262 15.4). Invalid indices are reported as being out of
755 // range.
756 STRING_INDEX_IS_ARRAY_INDEX
757};
758
759
760// Generates code implementing String.prototype.charCodeAt.
761//
762// Only supports the case when the receiver is a string and the index
763// is a number (smi or heap number) that is a valid index into the
764// string. Additional index constraints are specified by the
765// flags. Otherwise, bails out to the provided labels.
766//
767// Register usage: |object| may be changed to another string in a way
768// that doesn't affect charCodeAt/charAt semantics, |index| is
769// preserved, |scratch| and |result| are clobbered.
770class StringCharCodeAtGenerator {
771 public:
772 StringCharCodeAtGenerator(Register object,
773 Register index,
774 Register scratch,
775 Register result,
776 Label* receiver_not_string,
777 Label* index_not_number,
778 Label* index_out_of_range,
779 StringIndexFlags index_flags)
780 : object_(object),
781 index_(index),
782 scratch_(scratch),
783 result_(result),
784 receiver_not_string_(receiver_not_string),
785 index_not_number_(index_not_number),
786 index_out_of_range_(index_out_of_range),
787 index_flags_(index_flags) {
788 ASSERT(!scratch_.is(object_));
789 ASSERT(!scratch_.is(index_));
790 ASSERT(!scratch_.is(result_));
791 ASSERT(!result_.is(object_));
792 ASSERT(!result_.is(index_));
793 }
794
795 // Generates the fast case code. On the fallthrough path |result|
796 // register contains the result.
797 void GenerateFast(MacroAssembler* masm);
798
799 // Generates the slow case code. Must not be naturally
800 // reachable. Expected to be put after a ret instruction (e.g., in
801 // deferred code). Always jumps back to the fast case.
802 void GenerateSlow(MacroAssembler* masm,
803 const RuntimeCallHelper& call_helper);
804
805 private:
806 Register object_;
807 Register index_;
808 Register scratch_;
809 Register result_;
810
811 Label* receiver_not_string_;
812 Label* index_not_number_;
813 Label* index_out_of_range_;
814
815 StringIndexFlags index_flags_;
816
817 Label call_runtime_;
818 Label index_not_smi_;
819 Label got_smi_index_;
820 Label exit_;
821
822 DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
823};
824
825
826// Generates code for creating a one-char string from a char code.
827class StringCharFromCodeGenerator {
828 public:
829 StringCharFromCodeGenerator(Register code,
830 Register result)
831 : code_(code),
832 result_(result) {
833 ASSERT(!code_.is(result_));
834 }
835
836 // Generates the fast case code. On the fallthrough path |result|
837 // register contains the result.
838 void GenerateFast(MacroAssembler* masm);
839
840 // Generates the slow case code. Must not be naturally
841 // reachable. Expected to be put after a ret instruction (e.g., in
842 // deferred code). Always jumps back to the fast case.
843 void GenerateSlow(MacroAssembler* masm,
844 const RuntimeCallHelper& call_helper);
845
846 private:
847 Register code_;
848 Register result_;
849
850 Label slow_case_;
851 Label exit_;
852
853 DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
854};
855
856
857// Generates code implementing String.prototype.charAt.
858//
859// Only supports the case when the receiver is a string and the index
860// is a number (smi or heap number) that is a valid index into the
861// string. Additional index constraints are specified by the
862// flags. Otherwise, bails out to the provided labels.
863//
864// Register usage: |object| may be changed to another string in a way
865// that doesn't affect charCodeAt/charAt semantics, |index| is
866// preserved, |scratch1|, |scratch2|, and |result| are clobbered.
867class StringCharAtGenerator {
868 public:
869 StringCharAtGenerator(Register object,
870 Register index,
871 Register scratch1,
872 Register scratch2,
873 Register result,
874 Label* receiver_not_string,
875 Label* index_not_number,
876 Label* index_out_of_range,
877 StringIndexFlags index_flags)
878 : char_code_at_generator_(object,
879 index,
880 scratch1,
881 scratch2,
882 receiver_not_string,
883 index_not_number,
884 index_out_of_range,
885 index_flags),
886 char_from_code_generator_(scratch2, result) {}
887
888 // Generates the fast case code. On the fallthrough path |result|
889 // register contains the result.
890 void GenerateFast(MacroAssembler* masm);
891
892 // Generates the slow case code. Must not be naturally
893 // reachable. Expected to be put after a ret instruction (e.g., in
894 // deferred code). Always jumps back to the fast case.
895 void GenerateSlow(MacroAssembler* masm,
896 const RuntimeCallHelper& call_helper);
897
898 private:
899 StringCharCodeAtGenerator char_code_at_generator_;
900 StringCharFromCodeGenerator char_from_code_generator_;
901
902 DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
903};
904
905
Steve Blocka7e24c12009-10-30 11:49:00 +0000906} // namespace internal
907} // namespace v8
908
909#endif // V8_CODEGEN_H_