blob: a5bb31f1415cfb77cf178185d4b2269fbb4213f1 [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
31#include "ast.h"
32#include "code-stubs.h"
33#include "runtime.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034#include "type-info.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36// Include the declaration of the architecture defined class CodeGenerator.
37// The contract to the shared code is that the the CodeGenerator is a subclass
38// of Visitor and that the following methods are available publicly:
39// MakeCode
Steve Block3ce2e202009-11-05 08:53:23 +000040// MakeCodePrologue
41// MakeCodeEpilogue
Steve Blocka7e24c12009-10-30 11:49:00 +000042// masm
43// frame
Steve Blockd0582a62009-12-15 09:54:21 +000044// script
Steve Blocka7e24c12009-10-30 11:49:00 +000045// has_valid_frame
46// SetFrame
47// DeleteFrame
48// allocator
49// AddDeferred
50// in_spilled_code
51// set_in_spilled_code
Steve Block3ce2e202009-11-05 08:53:23 +000052// RecordPositions
Steve Blocka7e24c12009-10-30 11:49:00 +000053//
54// These methods are either used privately by the shared code or implemented as
55// shared code:
56// CodeGenerator
57// ~CodeGenerator
58// ProcessDeferred
Leon Clarke4515c472010-02-03 11:58:03 +000059// Generate
Steve Block3ce2e202009-11-05 08:53:23 +000060// ComputeLazyCompile
Steve Block6ded16b2010-05-10 14:33:55 +010061// BuildFunctionInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000062// ComputeCallInitialize
63// ComputeCallInitializeInLoop
64// ProcessDeclarations
65// DeclareGlobals
66// FindInlineRuntimeLUT
67// CheckForInlineRuntimeCall
68// PatchInlineRuntimeEntry
Steve Block3ce2e202009-11-05 08:53:23 +000069// AnalyzeCondition
Steve Blocka7e24c12009-10-30 11:49:00 +000070// CodeForFunctionPosition
71// CodeForReturnPosition
72// CodeForStatementPosition
Steve Blockd0582a62009-12-15 09:54:21 +000073// CodeForDoWhileConditionPosition
Steve Blocka7e24c12009-10-30 11:49:00 +000074// CodeForSourcePosition
75
76
77// Mode to overwrite BinaryExpression values.
78enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
79
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) \
114 F(FastCharCodeAt, 2, 1) \
115 F(CharFromCode, 1, 1) \
116 F(ObjectEquals, 2, 1) \
117 F(Log, 3, 1) \
118 F(RandomHeapNumber, 0, 1) \
119 F(IsObject, 1, 1) \
120 F(IsFunction, 1, 1) \
121 F(IsUndetectableObject, 1, 1) \
122 F(StringAdd, 2, 1) \
123 F(SubString, 3, 1) \
124 F(StringCompare, 2, 1) \
125 F(RegExpExec, 4, 1) \
126 F(RegExpConstructResult, 3, 1) \
127 F(GetFromCache, 2, 1) \
128 F(NumberToString, 1, 1) \
129 F(SwapElements, 3, 1) \
130 F(MathPow, 2, 1) \
131 F(MathSin, 1, 1) \
132 F(MathCos, 1, 1) \
133 F(MathSqrt, 1, 1)
134
135
Andrei Popescu31002712010-02-23 13:46:05 +0000136// Support for "structured" code comments.
137#ifdef DEBUG
138
139class Comment BASE_EMBEDDED {
140 public:
141 Comment(MacroAssembler* masm, const char* msg);
142 ~Comment();
143
144 private:
145 MacroAssembler* masm_;
146 const char* msg_;
147};
148
149#else
150
151class Comment BASE_EMBEDDED {
152 public:
153 Comment(MacroAssembler*, const char*) {}
154};
155
156#endif // DEBUG
157
158
Steve Blocka7e24c12009-10-30 11:49:00 +0000159// Code generation can be nested. Code generation scopes form a stack
160// of active code generators.
161class CodeGeneratorScope BASE_EMBEDDED {
162 public:
163 explicit CodeGeneratorScope(CodeGenerator* cgen) {
164 previous_ = top_;
165 top_ = cgen;
166 }
167
168 ~CodeGeneratorScope() {
169 top_ = previous_;
170 }
171
172 static CodeGenerator* Current() {
173 ASSERT(top_ != NULL);
174 return top_;
175 }
176
177 private:
178 static CodeGenerator* top_;
179 CodeGenerator* previous_;
180};
181
182
183// Deferred code objects are small pieces of code that are compiled
184// out of line. They are used to defer the compilation of uncommon
185// paths thereby avoiding expensive jumps around uncommon code parts.
186class DeferredCode: public ZoneObject {
187 public:
188 DeferredCode();
189 virtual ~DeferredCode() { }
190
191 virtual void Generate() = 0;
192
193 MacroAssembler* masm() { return masm_; }
194
195 int statement_position() const { return statement_position_; }
196 int position() const { return position_; }
197
198 Label* entry_label() { return &entry_label_; }
199 Label* exit_label() { return &exit_label_; }
200
201#ifdef DEBUG
202 void set_comment(const char* comment) { comment_ = comment; }
203 const char* comment() const { return comment_; }
204#else
205 void set_comment(const char* comment) { }
206 const char* comment() const { return ""; }
207#endif
208
209 inline void Jump();
210 inline void Branch(Condition cc);
211 void BindExit() { masm_->bind(&exit_label_); }
212
213 void SaveRegisters();
214 void RestoreRegisters();
215
216 protected:
217 MacroAssembler* masm_;
218
219 private:
220 // Constants indicating special actions. They should not be multiples
221 // of kPointerSize so they will not collide with valid offsets from
222 // the frame pointer.
223 static const int kIgnore = -1;
224 static const int kPush = 1;
225
226 // This flag is ored with a valid offset from the frame pointer, so
227 // it should fit in the low zero bits of a valid offset.
228 static const int kSyncedFlag = 2;
229
230 int statement_position_;
231 int position_;
232
233 Label entry_label_;
234 Label exit_label_;
235
Steve Block6ded16b2010-05-10 14:33:55 +0100236 // C++ doesn't allow zero length arrays, so we make the array length 1 even
237 // if we don't need it.
238 static const int kRegistersArrayLength =
239 (RegisterAllocator::kNumRegisters == 0) ?
240 1 : RegisterAllocator::kNumRegisters;
241 int registers_[kRegistersArrayLength];
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243#ifdef DEBUG
244 const char* comment_;
245#endif
246 DISALLOW_COPY_AND_ASSIGN(DeferredCode);
247};
248
Steve Blocka7e24c12009-10-30 11:49:00 +0000249class StackCheckStub : public CodeStub {
250 public:
251 StackCheckStub() { }
252
253 void Generate(MacroAssembler* masm);
254
255 private:
256
257 const char* GetName() { return "StackCheckStub"; }
258
259 Major MajorKey() { return StackCheck; }
260 int MinorKey() { return 0; }
261};
262
263
Leon Clarkee46be812010-01-19 14:06:41 +0000264class FastNewClosureStub : public CodeStub {
265 public:
266 void Generate(MacroAssembler* masm);
267
268 private:
269 const char* GetName() { return "FastNewClosureStub"; }
270 Major MajorKey() { return FastNewClosure; }
271 int MinorKey() { return 0; }
272};
273
274
275class FastNewContextStub : public CodeStub {
276 public:
277 static const int kMaximumSlots = 64;
278
279 explicit FastNewContextStub(int slots) : slots_(slots) {
280 ASSERT(slots_ > 0 && slots <= kMaximumSlots);
281 }
282
283 void Generate(MacroAssembler* masm);
284
285 private:
286 int slots_;
287
288 const char* GetName() { return "FastNewContextStub"; }
289 Major MajorKey() { return FastNewContext; }
290 int MinorKey() { return slots_; }
291};
292
293
294class FastCloneShallowArrayStub : public CodeStub {
295 public:
296 static const int kMaximumLength = 8;
297
298 explicit FastCloneShallowArrayStub(int length) : length_(length) {
299 ASSERT(length >= 0 && length <= kMaximumLength);
300 }
301
302 void Generate(MacroAssembler* masm);
303
304 private:
305 int length_;
306
307 const char* GetName() { return "FastCloneShallowArrayStub"; }
308 Major MajorKey() { return FastCloneShallowArray; }
309 int MinorKey() { return length_; }
310};
311
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313class InstanceofStub: public CodeStub {
314 public:
315 InstanceofStub() { }
316
317 void Generate(MacroAssembler* masm);
318
319 private:
320 Major MajorKey() { return Instanceof; }
321 int MinorKey() { return 0; }
322};
323
324
Leon Clarkee46be812010-01-19 14:06:41 +0000325class GenericUnaryOpStub : public CodeStub {
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 public:
Leon Clarkee46be812010-01-19 14:06:41 +0000327 GenericUnaryOpStub(Token::Value op, bool overwrite)
328 : op_(op), overwrite_(overwrite) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000329
330 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000331 Token::Value op_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 bool overwrite_;
Leon Clarkee46be812010-01-19 14:06:41 +0000333
334 class OverwriteField: public BitField<int, 0, 1> {};
335 class OpField: public BitField<Token::Value, 1, kMinorBits - 1> {};
336
337 Major MajorKey() { return GenericUnaryOp; }
338 int MinorKey() {
339 return OpField::encode(op_) | OverwriteField::encode(overwrite_);
340 }
341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 void Generate(MacroAssembler* masm);
343
Leon Clarkee46be812010-01-19 14:06:41 +0000344 const char* GetName();
345};
346
347
348enum NaNInformation {
349 kBothCouldBeNaN,
350 kCantBothBeNaN
Steve Blocka7e24c12009-10-30 11:49:00 +0000351};
352
353
354class CompareStub: public CodeStub {
355 public:
Leon Clarkee46be812010-01-19 14:06:41 +0000356 CompareStub(Condition cc,
357 bool strict,
Steve Block6ded16b2010-05-10 14:33:55 +0100358 NaNInformation nan_info = kBothCouldBeNaN,
359 bool include_number_compare = true) :
360 cc_(cc),
361 strict_(strict),
362 never_nan_nan_(nan_info == kCantBothBeNaN),
363 include_number_compare_(include_number_compare),
364 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
366 void Generate(MacroAssembler* masm);
367
368 private:
369 Condition cc_;
370 bool strict_;
Leon Clarkee46be812010-01-19 14:06:41 +0000371 // Only used for 'equal' comparisons. Tells the stub that we already know
372 // that at least one side of the comparison is not NaN. This allows the
373 // stub to use object identity in the positive case. We ignore it when
374 // generating the minor key for other comparisons to avoid creating more
375 // stubs.
376 bool never_nan_nan_;
Steve Block6ded16b2010-05-10 14:33:55 +0100377 // Do generate the number comparison code in the stub. Stubs without number
378 // comparison code is used when the number comparison has been inlined, and
379 // the stub will be called if one of the operands is not a number.
380 bool include_number_compare_;
381
382 // Encoding of the minor key CCCCCCCCCCCCCCNS.
383 class StrictField: public BitField<bool, 0, 1> {};
384 class NeverNanNanField: public BitField<bool, 1, 1> {};
385 class IncludeNumberCompareField: public BitField<bool, 2, 1> {};
386 class ConditionField: public BitField<int, 3, 13> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000387
388 Major MajorKey() { return Compare; }
389
390 int MinorKey();
391
392 // Branch to the label if the given object isn't a symbol.
393 void BranchIfNonSymbol(MacroAssembler* masm,
394 Label* label,
395 Register object,
396 Register scratch);
397
Leon Clarkee46be812010-01-19 14:06:41 +0000398 // Unfortunately you have to run without snapshots to see most of these
399 // names in the profile since most compare stubs end up in the snapshot.
Steve Block6ded16b2010-05-10 14:33:55 +0100400 char* name_;
Leon Clarkee46be812010-01-19 14:06:41 +0000401 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000402#ifdef DEBUG
403 void Print() {
Steve Block6ded16b2010-05-10 14:33:55 +0100404 PrintF("CompareStub (cc %d), (strict %s), "
405 "(never_nan_nan %s), (number_compare %s)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 static_cast<int>(cc_),
Steve Block6ded16b2010-05-10 14:33:55 +0100407 strict_ ? "true" : "false",
408 never_nan_nan_ ? "true" : "false",
409 include_number_compare_ ? "included" : "not included");
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 }
411#endif
412};
413
414
415class CEntryStub : public CodeStub {
416 public:
Leon Clarke4515c472010-02-03 11:58:03 +0000417 explicit CEntryStub(int result_size,
418 ExitFrame::Mode mode = ExitFrame::MODE_NORMAL)
419 : result_size_(result_size), mode_(mode) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000420
Leon Clarke4515c472010-02-03 11:58:03 +0000421 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422
Leon Clarke4515c472010-02-03 11:58:03 +0000423 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 void GenerateCore(MacroAssembler* masm,
425 Label* throw_normal_exception,
426 Label* throw_termination_exception,
427 Label* throw_out_of_memory_exception,
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 bool do_gc,
Steve Block6ded16b2010-05-10 14:33:55 +0100429 bool always_allocate_scope,
430 int alignment_skew = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 void GenerateThrowTOS(MacroAssembler* masm);
432 void GenerateThrowUncatchable(MacroAssembler* masm,
433 UncatchableExceptionType type);
Leon Clarke4515c472010-02-03 11:58:03 +0000434
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 // Number of pointers/values returned.
Leon Clarke4515c472010-02-03 11:58:03 +0000436 const int result_size_;
437 const ExitFrame::Mode mode_;
438
439 // Minor key encoding
440 class ExitFrameModeBits: public BitField<ExitFrame::Mode, 0, 1> {};
441 class IndirectResultBits: public BitField<bool, 1, 1> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000442
443 Major MajorKey() { return CEntry; }
444 // Minor key must differ if different result_size_ values means different
445 // code is generated.
446 int MinorKey();
447
448 const char* GetName() { return "CEntryStub"; }
449};
450
451
Steve Blockd0582a62009-12-15 09:54:21 +0000452class ApiGetterEntryStub : public CodeStub {
453 public:
454 ApiGetterEntryStub(Handle<AccessorInfo> info,
455 ApiFunction* fun)
456 : info_(info),
457 fun_(fun) { }
458 void Generate(MacroAssembler* masm);
459 virtual bool has_custom_cache() { return true; }
460 virtual bool GetCustomCache(Code** code_out);
461 virtual void SetCustomCache(Code* value);
462
Steve Block6ded16b2010-05-10 14:33:55 +0100463 static const int kStackSpace = 5;
Steve Blockd0582a62009-12-15 09:54:21 +0000464 static const int kArgc = 4;
465 private:
466 Handle<AccessorInfo> info() { return info_; }
467 ApiFunction* fun() { return fun_; }
468 Major MajorKey() { return NoCache; }
469 int MinorKey() { return 0; }
470 const char* GetName() { return "ApiEntryStub"; }
471 // The accessor info associated with the function.
472 Handle<AccessorInfo> info_;
473 // The function to be called.
474 ApiFunction* fun_;
475};
476
477
Steve Blocka7e24c12009-10-30 11:49:00 +0000478class JSEntryStub : public CodeStub {
479 public:
480 JSEntryStub() { }
481
482 void Generate(MacroAssembler* masm) { GenerateBody(masm, false); }
483
484 protected:
485 void GenerateBody(MacroAssembler* masm, bool is_construct);
486
487 private:
488 Major MajorKey() { return JSEntry; }
489 int MinorKey() { return 0; }
490
491 const char* GetName() { return "JSEntryStub"; }
492};
493
494
495class JSConstructEntryStub : public JSEntryStub {
496 public:
497 JSConstructEntryStub() { }
498
499 void Generate(MacroAssembler* masm) { GenerateBody(masm, true); }
500
501 private:
502 int MinorKey() { return 1; }
503
504 const char* GetName() { return "JSConstructEntryStub"; }
505};
506
507
508class ArgumentsAccessStub: public CodeStub {
509 public:
510 enum Type {
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 READ_ELEMENT,
512 NEW_OBJECT
513 };
514
515 explicit ArgumentsAccessStub(Type type) : type_(type) { }
516
517 private:
518 Type type_;
519
520 Major MajorKey() { return ArgumentsAccess; }
521 int MinorKey() { return type_; }
522
523 void Generate(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 void GenerateReadElement(MacroAssembler* masm);
525 void GenerateNewObject(MacroAssembler* masm);
526
527 const char* GetName() { return "ArgumentsAccessStub"; }
528
529#ifdef DEBUG
530 void Print() {
531 PrintF("ArgumentsAccessStub (type %d)\n", type_);
532 }
533#endif
534};
535
536
Leon Clarkee46be812010-01-19 14:06:41 +0000537class RegExpExecStub: public CodeStub {
538 public:
539 RegExpExecStub() { }
540
541 private:
542 Major MajorKey() { return RegExpExec; }
543 int MinorKey() { return 0; }
544
545 void Generate(MacroAssembler* masm);
546
547 const char* GetName() { return "RegExpExecStub"; }
548
549#ifdef DEBUG
550 void Print() {
551 PrintF("RegExpExecStub\n");
552 }
553#endif
554};
555
556
557class CallFunctionStub: public CodeStub {
558 public:
559 CallFunctionStub(int argc, InLoopFlag in_loop, CallFunctionFlags flags)
560 : argc_(argc), in_loop_(in_loop), flags_(flags) { }
561
562 void Generate(MacroAssembler* masm);
563
564 private:
565 int argc_;
566 InLoopFlag in_loop_;
567 CallFunctionFlags flags_;
568
569#ifdef DEBUG
570 void Print() {
571 PrintF("CallFunctionStub (args %d, in_loop %d, flags %d)\n",
572 argc_,
573 static_cast<int>(in_loop_),
574 static_cast<int>(flags_));
575 }
576#endif
577
Andrei Popescu402d9372010-02-26 13:31:12 +0000578 // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
Leon Clarkee46be812010-01-19 14:06:41 +0000579 class InLoopBits: public BitField<InLoopFlag, 0, 1> {};
580 class FlagBits: public BitField<CallFunctionFlags, 1, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000581 class ArgcBits: public BitField<int, 2, 32 - 2> {};
Leon Clarkee46be812010-01-19 14:06:41 +0000582
583 Major MajorKey() { return CallFunction; }
584 int MinorKey() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000585 // Encode the parameters in a unique 32 bit value.
Leon Clarkee46be812010-01-19 14:06:41 +0000586 return InLoopBits::encode(in_loop_)
587 | FlagBits::encode(flags_)
588 | ArgcBits::encode(argc_);
589 }
590
591 InLoopFlag InLoop() { return in_loop_; }
592 bool ReceiverMightBeValue() {
593 return (flags_ & RECEIVER_MIGHT_BE_VALUE) != 0;
594 }
595
596 public:
597 static int ExtractArgcFromMinorKey(int minor_key) {
598 return ArgcBits::decode(minor_key);
599 }
600};
601
602
603class ToBooleanStub: public CodeStub {
604 public:
605 ToBooleanStub() { }
606
607 void Generate(MacroAssembler* masm);
608
609 private:
610 Major MajorKey() { return ToBoolean; }
611 int MinorKey() { return 0; }
612};
613
614
Steve Blocka7e24c12009-10-30 11:49:00 +0000615} // namespace internal
616} // namespace v8
617
618#endif // V8_CODEGEN_H_