blob: 56b88b74d1379a248a25b3d17f2af15140e1abc2 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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_X64_CODEGEN_X64_H_
29#define V8_X64_CODEGEN_X64_H_
30
31namespace v8 {
32namespace internal {
33
34// Forward declarations
35class DeferredCode;
36class RegisterAllocator;
37class RegisterFile;
38
39enum InitState { CONST_INIT, NOT_CONST_INIT };
40enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
41
42
43// -------------------------------------------------------------------------
44// Reference support
45
46// A reference is a C++ stack-allocated object that keeps an ECMA
47// reference on the execution stack while in scope. For variables
48// the reference is empty, indicating that it isn't necessary to
49// store state on the stack for keeping track of references to those.
50// For properties, we keep either one (named) or two (indexed) values
51// on the execution stack to represent the reference.
52
53class Reference BASE_EMBEDDED {
54 public:
55 // The values of the types is important, see size().
56 enum Type { ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
57 Reference(CodeGenerator* cgen, Expression* expression);
58 ~Reference();
59
60 Expression* expression() const { return expression_; }
61 Type type() const { return type_; }
62 void set_type(Type value) {
63 ASSERT(type_ == ILLEGAL);
64 type_ = value;
65 }
66
67 // The size the reference takes up on the stack.
68 int size() const { return (type_ == ILLEGAL) ? 0 : type_; }
69
70 bool is_illegal() const { return type_ == ILLEGAL; }
71 bool is_slot() const { return type_ == SLOT; }
72 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
73
74 // Return the name. Only valid for named property references.
75 Handle<String> GetName();
76
77 // Generate code to push the value of the reference on top of the
78 // expression stack. The reference is expected to be already on top of
79 // the expression stack, and it is left in place with its value above it.
80 void GetValue(TypeofState typeof_state);
81
82 // Like GetValue except that the slot is expected to be written to before
83 // being read from again. Thae value of the reference may be invalidated,
84 // causing subsequent attempts to read it to fail.
85 void TakeValue(TypeofState typeof_state);
86
87 // Generate code to store the value on top of the expression stack in the
88 // reference. The reference is expected to be immediately below the value
89 // on the expression stack. The stored value is left in place (with the
90 // reference intact below it) to support chained assignments.
91 void SetValue(InitState init_state);
92
93 private:
94 CodeGenerator* cgen_;
95 Expression* expression_;
96 Type type_;
97};
98
99
100// -------------------------------------------------------------------------
101// Control destinations.
102
103// A control destination encapsulates a pair of jump targets and a
104// flag indicating which one is the preferred fall-through. The
105// preferred fall-through must be unbound, the other may be already
106// bound (ie, a backward target).
107//
108// The true and false targets may be jumped to unconditionally or
109// control may split conditionally. Unconditional jumping and
110// splitting should be emitted in tail position (as the last thing
111// when compiling an expression) because they can cause either label
112// to be bound or the non-fall through to be jumped to leaving an
113// invalid virtual frame.
114//
115// The labels in the control destination can be extracted and
116// manipulated normally without affecting the state of the
117// destination.
118
119class ControlDestination BASE_EMBEDDED {
120 public:
121 ControlDestination(JumpTarget* true_target,
122 JumpTarget* false_target,
123 bool true_is_fall_through)
124 : true_target_(true_target),
125 false_target_(false_target),
126 true_is_fall_through_(true_is_fall_through),
127 is_used_(false) {
128 ASSERT(true_is_fall_through ? !true_target->is_bound()
129 : !false_target->is_bound());
130 }
131
132 // Accessors for the jump targets. Directly jumping or branching to
133 // or binding the targets will not update the destination's state.
134 JumpTarget* true_target() const { return true_target_; }
135 JumpTarget* false_target() const { return false_target_; }
136
137 // True if the the destination has been jumped to unconditionally or
138 // control has been split to both targets. This predicate does not
139 // test whether the targets have been extracted and manipulated as
140 // raw jump targets.
141 bool is_used() const { return is_used_; }
142
143 // True if the destination is used and the true target (respectively
144 // false target) was the fall through. If the target is backward,
145 // "fall through" included jumping unconditionally to it.
146 bool true_was_fall_through() const {
147 return is_used_ && true_is_fall_through_;
148 }
149
150 bool false_was_fall_through() const {
151 return is_used_ && !true_is_fall_through_;
152 }
153
154 // Emit a branch to one of the true or false targets, and bind the
155 // other target. Because this binds the fall-through target, it
156 // should be emitted in tail position (as the last thing when
157 // compiling an expression).
158 void Split(Condition cc) {
159 ASSERT(!is_used_);
160 if (true_is_fall_through_) {
161 false_target_->Branch(NegateCondition(cc));
162 true_target_->Bind();
163 } else {
164 true_target_->Branch(cc);
165 false_target_->Bind();
166 }
167 is_used_ = true;
168 }
169
170 // Emit an unconditional jump in tail position, to the true target
171 // (if the argument is true) or the false target. The "jump" will
172 // actually bind the jump target if it is forward, jump to it if it
173 // is backward.
174 void Goto(bool where) {
175 ASSERT(!is_used_);
176 JumpTarget* target = where ? true_target_ : false_target_;
177 if (target->is_bound()) {
178 target->Jump();
179 } else {
180 target->Bind();
181 }
182 is_used_ = true;
183 true_is_fall_through_ = where;
184 }
185
186 // Mark this jump target as used as if Goto had been called, but
187 // without generating a jump or binding a label (the control effect
188 // should have already happened). This is used when the left
189 // subexpression of the short-circuit boolean operators are
190 // compiled.
191 void Use(bool where) {
192 ASSERT(!is_used_);
193 ASSERT((where ? true_target_ : false_target_)->is_bound());
194 is_used_ = true;
195 true_is_fall_through_ = where;
196 }
197
198 // Swap the true and false targets but keep the same actual label as
199 // the fall through. This is used when compiling negated
200 // expressions, where we want to swap the targets but preserve the
201 // state.
202 void Invert() {
203 JumpTarget* temp_target = true_target_;
204 true_target_ = false_target_;
205 false_target_ = temp_target;
206
207 true_is_fall_through_ = !true_is_fall_through_;
208 }
209
210 private:
211 // True and false jump targets.
212 JumpTarget* true_target_;
213 JumpTarget* false_target_;
214
215 // Before using the destination: true if the true target is the
216 // preferred fall through, false if the false target is. After
217 // using the destination: true if the true target was actually used
218 // as the fall through, false if the false target was.
219 bool true_is_fall_through_;
220
221 // True if the Split or Goto functions have been called.
222 bool is_used_;
223};
224
225
226// -------------------------------------------------------------------------
227// Code generation state
228
229// The state is passed down the AST by the code generator (and back up, in
230// the form of the state of the jump target pair). It is threaded through
231// the call stack. Constructing a state implicitly pushes it on the owning
232// code generator's stack of states, and destroying one implicitly pops it.
233//
234// The code generator state is only used for expressions, so statements have
235// the initial state.
236
237class CodeGenState BASE_EMBEDDED {
238 public:
239 // Create an initial code generator state. Destroying the initial state
240 // leaves the code generator with a NULL state.
241 explicit CodeGenState(CodeGenerator* owner);
242
243 // Create a code generator state based on a code generator's current
244 // state. The new state may or may not be inside a typeof, and has its
245 // own control destination.
246 CodeGenState(CodeGenerator* owner,
247 TypeofState typeof_state,
248 ControlDestination* destination);
249
250 // Destroy a code generator state and restore the owning code generator's
251 // previous state.
252 ~CodeGenState();
253
254 // Accessors for the state.
255 TypeofState typeof_state() const { return typeof_state_; }
256 ControlDestination* destination() const { return destination_; }
257
258 private:
259 // The owning code generator.
260 CodeGenerator* owner_;
261
262 // A flag indicating whether we are compiling the immediate subexpression
263 // of a typeof expression.
264 TypeofState typeof_state_;
265
266 // A control destination in case the expression has a control-flow
267 // effect.
268 ControlDestination* destination_;
269
270 // The previous state of the owning code generator, restored when
271 // this state is destroyed.
272 CodeGenState* previous_;
273};
274
275
276// -------------------------------------------------------------------------
277// Arguments allocation mode
278
279enum ArgumentsAllocationMode {
280 NO_ARGUMENTS_ALLOCATION,
281 EAGER_ARGUMENTS_ALLOCATION,
282 LAZY_ARGUMENTS_ALLOCATION
283};
284
285
286// -------------------------------------------------------------------------
287// CodeGenerator
288
289class CodeGenerator: public AstVisitor {
290 public:
291 // Takes a function literal, generates code for it. This function should only
292 // be called by compiler.cc.
293 static Handle<Code> MakeCode(FunctionLiteral* fun,
294 Handle<Script> script,
295 bool is_eval);
296
Steve Block3ce2e202009-11-05 08:53:23 +0000297 // Printing of AST, etc. as requested by flags.
298 static void MakeCodePrologue(FunctionLiteral* fun);
299
300 // Allocate and install the code.
301 static Handle<Code> MakeCodeEpilogue(FunctionLiteral* fun,
302 MacroAssembler* masm,
303 Code::Flags flags,
304 Handle<Script> script);
305
Steve Blocka7e24c12009-10-30 11:49:00 +0000306#ifdef ENABLE_LOGGING_AND_PROFILING
307 static bool ShouldGenerateLog(Expression* type);
308#endif
309
310 static void SetFunctionInfo(Handle<JSFunction> fun,
311 FunctionLiteral* lit,
312 bool is_toplevel,
313 Handle<Script> script);
314
Steve Block3ce2e202009-11-05 08:53:23 +0000315 static void RecordPositions(MacroAssembler* masm, int pos);
316
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 // Accessors
318 MacroAssembler* masm() { return masm_; }
319
320 VirtualFrame* frame() const { return frame_; }
321
322 bool has_valid_frame() const { return frame_ != NULL; }
323
324 // Set the virtual frame to be new_frame, with non-frame register
325 // reference counts given by non_frame_registers. The non-frame
326 // register reference counts of the old frame are returned in
327 // non_frame_registers.
328 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
329
330 void DeleteFrame();
331
332 RegisterAllocator* allocator() const { return allocator_; }
333
334 CodeGenState* state() { return state_; }
335 void set_state(CodeGenState* state) { state_ = state; }
336
337 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
338
339 bool in_spilled_code() const { return in_spilled_code_; }
340 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
341
342 private:
343 // Construction/Destruction
344 CodeGenerator(int buffer_size, Handle<Script> script, bool is_eval);
345 virtual ~CodeGenerator() { delete masm_; }
346
347 // Accessors
348 Scope* scope() const { return scope_; }
349
350 // Generating deferred code.
351 void ProcessDeferred();
352
353 bool is_eval() { return is_eval_; }
354
355 // State
356 TypeofState typeof_state() const { return state_->typeof_state(); }
357 ControlDestination* destination() const { return state_->destination(); }
358
359 // Track loop nesting level.
360 int loop_nesting() const { return loop_nesting_; }
361 void IncrementLoopNesting() { loop_nesting_++; }
362 void DecrementLoopNesting() { loop_nesting_--; }
363
364
365 // Node visitors.
366 void VisitStatements(ZoneList<Statement*>* statements);
367
368#define DEF_VISIT(type) \
369 void Visit##type(type* node);
370 AST_NODE_LIST(DEF_VISIT)
371#undef DEF_VISIT
372
373 // Visit a statement and then spill the virtual frame if control flow can
374 // reach the end of the statement (ie, it does not exit via break,
375 // continue, return, or throw). This function is used temporarily while
376 // the code generator is being transformed.
377 void VisitAndSpill(Statement* statement);
378
379 // Visit a list of statements and then spill the virtual frame if control
380 // flow can reach the end of the list.
381 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
382
383 // Main code generation function
384 void GenCode(FunctionLiteral* fun);
385
386 // Generate the return sequence code. Should be called no more than
387 // once per compiled function, immediately after binding the return
388 // target (which can not be done more than once).
389 void GenerateReturnSequence(Result* return_value);
390
391 // Returns the arguments allocation mode.
392 ArgumentsAllocationMode ArgumentsMode() const;
393
394 // Store the arguments object and allocate it if necessary.
395 Result StoreArgumentsObject(bool initial);
396
397 // The following are used by class Reference.
398 void LoadReference(Reference* ref);
399 void UnloadReference(Reference* ref);
400
Steve Block3ce2e202009-11-05 08:53:23 +0000401 static Operand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 return Operand(context, Context::SlotOffset(index));
403 }
404
405 Operand SlotOperand(Slot* slot, Register tmp);
406
407 Operand ContextSlotOperandCheckExtensions(Slot* slot,
408 Result tmp,
409 JumpTarget* slow);
410
411 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000412 static Operand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 return ContextOperand(rsi, Context::GLOBAL_INDEX);
414 }
415
416 void LoadCondition(Expression* x,
417 TypeofState typeof_state,
418 ControlDestination* destination,
419 bool force_control);
420 void Load(Expression* x, TypeofState typeof_state = NOT_INSIDE_TYPEOF);
421 void LoadGlobal();
422 void LoadGlobalReceiver();
423
424 // Generate code to push the value of an expression on top of the frame
425 // and then spill the frame fully to memory. This function is used
426 // temporarily while the code generator is being transformed.
427 void LoadAndSpill(Expression* expression,
428 TypeofState typeof_state = NOT_INSIDE_TYPEOF);
429
430 // Read a value from a slot and leave it on top of the expression stack.
431 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
432 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
433 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
434 TypeofState typeof_state,
435 JumpTarget* slow);
436
437 // Store the value on top of the expression stack into a slot, leaving the
438 // value in place.
439 void StoreToSlot(Slot* slot, InitState init_state);
440
441 // Special code for typeof expressions: Unfortunately, we must
442 // be careful when loading the expression in 'typeof'
443 // expressions. We are not allowed to throw reference errors for
444 // non-existing properties of the global object, so we must make it
445 // look like an explicit property access, instead of an access
446 // through the context chain.
447 void LoadTypeofExpression(Expression* x);
448
449 // Translate the value on top of the frame into control flow to the
450 // control destination.
451 void ToBoolean(ControlDestination* destination);
452
453 void GenericBinaryOperation(
454 Token::Value op,
455 SmiAnalysis* type,
456 OverwriteMode overwrite_mode);
457
458 // If possible, combine two constant smi values using op to produce
459 // a smi result, and push it on the virtual frame, all at compile time.
460 // Returns true if it succeeds. Otherwise it has no effect.
461 bool FoldConstantSmis(Token::Value op, int left, int right);
462
463 // Emit code to perform a binary operation on a constant
464 // smi and a likely smi. Consumes the Result *operand.
465 void ConstantSmiBinaryOperation(Token::Value op,
466 Result* operand,
467 Handle<Object> constant_operand,
468 SmiAnalysis* type,
469 bool reversed,
470 OverwriteMode overwrite_mode);
471
472 // Emit code to perform a binary operation on two likely smis.
473 // The code to handle smi arguments is produced inline.
474 // Consumes the Results *left and *right.
475 void LikelySmiBinaryOperation(Token::Value op,
476 Result* left,
477 Result* right,
478 OverwriteMode overwrite_mode);
479
480 void Comparison(Condition cc,
481 bool strict,
482 ControlDestination* destination);
483
484 // To prevent long attacker-controlled byte sequences, integer constants
485 // from the JavaScript source are loaded in two parts if they are larger
486 // than 16 bits.
487 static const int kMaxSmiInlinedBits = 16;
488 bool IsUnsafeSmi(Handle<Object> value);
489 // Load an integer constant x into a register target using
490 // at most 16 bits of user-controlled data per assembly operation.
491 void LoadUnsafeSmi(Register target, Handle<Object> value);
492
493 void CallWithArguments(ZoneList<Expression*>* arguments, int position);
494
495 // Use an optimized version of Function.prototype.apply that avoid
496 // allocating the arguments object and just copies the arguments
497 // from the stack.
498 void CallApplyLazy(Property* apply,
499 Expression* receiver,
500 VariableProxy* arguments,
501 int position);
502
503 void CheckStack();
504
505 struct InlineRuntimeLUT {
506 void (CodeGenerator::*method)(ZoneList<Expression*>*);
507 const char* name;
508 };
509 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
510 bool CheckForInlineRuntimeCall(CallRuntime* node);
511 static bool PatchInlineRuntimeEntry(Handle<String> name,
512 const InlineRuntimeLUT& new_entry,
513 InlineRuntimeLUT* old_entry);
Steve Block3ce2e202009-11-05 08:53:23 +0000514 static Handle<Code> ComputeLazyCompile(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 Handle<JSFunction> BuildBoilerplate(FunctionLiteral* node);
516 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
517
Steve Block3ce2e202009-11-05 08:53:23 +0000518 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000519
520 // Declare global variables and functions in the given array of
521 // name/value pairs.
522 void DeclareGlobals(Handle<FixedArray> pairs);
523
524 // Instantiate the function boilerplate.
525 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
526
527 // Support for type checks.
528 void GenerateIsSmi(ZoneList<Expression*>* args);
529 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
530 void GenerateIsArray(ZoneList<Expression*>* args);
531
532 // Support for construct call checks.
533 void GenerateIsConstructCall(ZoneList<Expression*>* args);
534
535 // Support for arguments.length and arguments[?].
536 void GenerateArgumentsLength(ZoneList<Expression*>* args);
537 void GenerateArgumentsAccess(ZoneList<Expression*>* args);
538
539 // Support for accessing the class and value fields of an object.
540 void GenerateClassOf(ZoneList<Expression*>* args);
541 void GenerateValueOf(ZoneList<Expression*>* args);
542 void GenerateSetValueOf(ZoneList<Expression*>* args);
543
544 // Fast support for charCodeAt(n).
545 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
546
547 // Fast support for object equality testing.
548 void GenerateObjectEquals(ZoneList<Expression*>* args);
549
550 void GenerateLog(ZoneList<Expression*>* args);
551
552 void GenerateGetFramePointer(ZoneList<Expression*>* args);
553
554 // Fast support for Math.random().
555 void GenerateRandomPositiveSmi(ZoneList<Expression*>* args);
556
557 // Fast support for Math.sin and Math.cos.
558 enum MathOp { SIN, COS };
559 void GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args);
560 inline void GenerateMathSin(ZoneList<Expression*>* args);
561 inline void GenerateMathCos(ZoneList<Expression*>* args);
562
Steve Block3ce2e202009-11-05 08:53:23 +0000563 // Simple condition analysis.
564 enum ConditionAnalysis {
565 ALWAYS_TRUE,
566 ALWAYS_FALSE,
567 DONT_KNOW
568 };
569 ConditionAnalysis AnalyzeCondition(Expression* cond);
570
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 // Methods used to indicate which source code is generated for. Source
572 // positions are collected by the assembler and emitted with the relocation
573 // information.
574 void CodeForFunctionPosition(FunctionLiteral* fun);
575 void CodeForReturnPosition(FunctionLiteral* fun);
576 void CodeForStatementPosition(Statement* node);
577 void CodeForSourcePosition(int pos);
578
579#ifdef DEBUG
580 // True if the registers are valid for entry to a block. There should
581 // be no frame-external references to (non-reserved) registers.
582 bool HasValidEntryRegisters();
583#endif
584
585 bool is_eval_; // Tells whether code is generated for eval.
586 Handle<Script> script_;
587 ZoneList<DeferredCode*> deferred_;
588
589 // Assembler
590 MacroAssembler* masm_; // to generate code
591
592 // Code generation state
593 Scope* scope_;
594 VirtualFrame* frame_;
595 RegisterAllocator* allocator_;
596 CodeGenState* state_;
597 int loop_nesting_;
598
599 // Jump targets.
600 // The target of the return from the function.
601 BreakTarget function_return_;
602
603 // True if the function return is shadowed (ie, jumping to the target
604 // function_return_ does not jump to the true function return, but rather
605 // to some unlinking code).
606 bool function_return_is_shadowed_;
607
608 // True when we are in code that expects the virtual frame to be fully
609 // spilled. Some virtual frame function are disabled in DEBUG builds when
610 // called from spilled code, because they do not leave the virtual frame
611 // in a spilled state.
612 bool in_spilled_code_;
613
614 static InlineRuntimeLUT kInlineRuntimeLUT[];
615
616 friend class VirtualFrame;
617 friend class JumpTarget;
618 friend class Reference;
619 friend class Result;
Steve Block3ce2e202009-11-05 08:53:23 +0000620 friend class FastCodeGenerator;
621 friend class CodeGenSelector;
Steve Blocka7e24c12009-10-30 11:49:00 +0000622
623 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
624
625 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
626};
627
628
629// -------------------------------------------------------------------------
630// Code stubs
631//
632// These independent code objects are created once, and used multiple
633// times by generated code to perform common tasks, often the slow
634// case of a JavaScript operation. They are all subclasses of CodeStub,
635// which is declared in code-stubs.h.
636
637
Steve Block3ce2e202009-11-05 08:53:23 +0000638class ToBooleanStub: public CodeStub {
639 public:
640 ToBooleanStub() { }
641
642 void Generate(MacroAssembler* masm);
643
644 private:
645 Major MajorKey() { return ToBoolean; }
646 int MinorKey() { return 0; }
647};
648
649
Steve Blocka7e24c12009-10-30 11:49:00 +0000650// Flag that indicates whether or not the code that handles smi arguments
651// should be placed in the stub, inlined, or omitted entirely.
652enum GenericBinaryFlags {
653 SMI_CODE_IN_STUB,
654 SMI_CODE_INLINED
655};
656
657
658class GenericBinaryOpStub: public CodeStub {
659 public:
660 GenericBinaryOpStub(Token::Value op,
661 OverwriteMode mode,
662 GenericBinaryFlags flags)
663 : op_(op), mode_(mode), flags_(flags) {
664 use_sse3_ = CpuFeatures::IsSupported(CpuFeatures::SSE3);
665 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
666 }
667
668 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
669
670 private:
671 Token::Value op_;
672 OverwriteMode mode_;
673 GenericBinaryFlags flags_;
674 bool use_sse3_;
675
676 const char* GetName();
677
678#ifdef DEBUG
679 void Print() {
680 PrintF("GenericBinaryOpStub (op %s), (mode %d, flags %d)\n",
681 Token::String(op_),
682 static_cast<int>(mode_),
683 static_cast<int>(flags_));
684 }
685#endif
686
687 // Minor key encoding in 16 bits FSOOOOOOOOOOOOMM.
688 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
689 class OpBits: public BitField<Token::Value, 2, 12> {};
690 class SSE3Bits: public BitField<bool, 14, 1> {};
691 class FlagBits: public BitField<GenericBinaryFlags, 15, 1> {};
692
693 Major MajorKey() { return GenericBinaryOp; }
694 int MinorKey() {
695 // Encode the parameters in a unique 16 bit value.
696 return OpBits::encode(op_)
697 | ModeBits::encode(mode_)
698 | FlagBits::encode(flags_)
699 | SSE3Bits::encode(use_sse3_);
700 }
701 void Generate(MacroAssembler* masm);
702};
703
704
705} } // namespace v8::internal
706
707#endif // V8_X64_CODEGEN_X64_H_