More precise break points and stepping when debugging
[platform/upstream/v8.git] / src / arm / codegen-arm.h
1 // Copyright 2010 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_ARM_CODEGEN_ARM_H_
29 #define V8_ARM_CODEGEN_ARM_H_
30
31 #include "ic-inl.h"
32 #include "ast.h"
33
34 namespace v8 {
35 namespace internal {
36
37 // Forward declarations
38 class CompilationInfo;
39 class DeferredCode;
40 class JumpTarget;
41 class RegisterAllocator;
42 class RegisterFile;
43
44 enum InitState { CONST_INIT, NOT_CONST_INIT };
45 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
46 enum GenerateInlineSmi { DONT_GENERATE_INLINE_SMI, GENERATE_INLINE_SMI };
47
48
49 // -------------------------------------------------------------------------
50 // Reference support
51
52 // A reference is a C++ stack-allocated object that puts a
53 // reference on the virtual frame.  The reference may be consumed
54 // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
55 // When the lifetime (scope) of a valid reference ends, it must have
56 // been consumed, and be in state UNLOADED.
57 class Reference BASE_EMBEDDED {
58  public:
59   // The values of the types is important, see size().
60   enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
61   Reference(CodeGenerator* cgen,
62             Expression* expression,
63             bool persist_after_get = false);
64   ~Reference();
65
66   Expression* expression() const { return expression_; }
67   Type type() const { return type_; }
68   void set_type(Type value) {
69     ASSERT_EQ(ILLEGAL, type_);
70     type_ = value;
71   }
72
73   void set_unloaded() {
74     ASSERT_NE(ILLEGAL, type_);
75     ASSERT_NE(UNLOADED, type_);
76     type_ = UNLOADED;
77   }
78   // The size the reference takes up on the stack.
79   int size() const {
80     return (type_ < SLOT) ? 0 : type_;
81   }
82
83   bool is_illegal() const { return type_ == ILLEGAL; }
84   bool is_slot() const { return type_ == SLOT; }
85   bool is_property() const { return type_ == NAMED || type_ == KEYED; }
86   bool is_unloaded() const { return type_ == UNLOADED; }
87
88   // Return the name.  Only valid for named property references.
89   Handle<String> GetName();
90
91   // Generate code to push the value of the reference on top of the
92   // expression stack.  The reference is expected to be already on top of
93   // the expression stack, and it is consumed by the call unless the
94   // reference is for a compound assignment.
95   // If the reference is not consumed, it is left in place under its value.
96   void GetValue();
97
98   // Generate code to store the value on top of the expression stack in the
99   // reference.  The reference is expected to be immediately below the value
100   // on the expression stack.  The  value is stored in the location specified
101   // by the reference, and is left on top of the stack, after the reference
102   // is popped from beneath it (unloaded).
103   void SetValue(InitState init_state);
104
105   // This is in preparation for something that uses the reference on the stack.
106   // If we need this reference afterwards get then dup it now.  Otherwise mark
107   // it as used.
108   inline void DupIfPersist();
109
110  private:
111   CodeGenerator* cgen_;
112   Expression* expression_;
113   Type type_;
114   // Keep the reference on the stack after get, so it can be used by set later.
115   bool persist_after_get_;
116 };
117
118
119 // -------------------------------------------------------------------------
120 // Code generation state
121
122 // The state is passed down the AST by the code generator (and back up, in
123 // the form of the state of the label pair).  It is threaded through the
124 // call stack.  Constructing a state implicitly pushes it on the owning code
125 // generator's stack of states, and destroying one implicitly pops it.
126
127 class CodeGenState BASE_EMBEDDED {
128  public:
129   // Create an initial code generator state.  Destroying the initial state
130   // leaves the code generator with a NULL state.
131   explicit CodeGenState(CodeGenerator* owner);
132
133   // Destroy a code generator state and restore the owning code generator's
134   // previous state.
135   virtual ~CodeGenState();
136
137   virtual JumpTarget* true_target() const { return NULL; }
138   virtual JumpTarget* false_target() const { return NULL; }
139
140  protected:
141   inline CodeGenerator* owner() { return owner_; }
142   inline CodeGenState* previous() const { return previous_; }
143
144  private:
145   CodeGenerator* owner_;
146   CodeGenState* previous_;
147 };
148
149
150 class ConditionCodeGenState : public CodeGenState {
151  public:
152   // Create a code generator state based on a code generator's current
153   // state.  The new state has its own pair of branch labels.
154   ConditionCodeGenState(CodeGenerator* owner,
155                         JumpTarget* true_target,
156                         JumpTarget* false_target);
157
158   virtual JumpTarget* true_target() const { return true_target_; }
159   virtual JumpTarget* false_target() const { return false_target_; }
160
161  private:
162   JumpTarget* true_target_;
163   JumpTarget* false_target_;
164 };
165
166
167 class TypeInfoCodeGenState : public CodeGenState {
168  public:
169   TypeInfoCodeGenState(CodeGenerator* owner,
170                        Slot* slot_number,
171                        TypeInfo info);
172   ~TypeInfoCodeGenState();
173
174   virtual JumpTarget* true_target() const { return previous()->true_target(); }
175   virtual JumpTarget* false_target() const {
176     return previous()->false_target();
177   }
178
179  private:
180   Slot* slot_;
181   TypeInfo old_type_info_;
182 };
183
184
185 // -------------------------------------------------------------------------
186 // Arguments allocation mode
187
188 enum ArgumentsAllocationMode {
189   NO_ARGUMENTS_ALLOCATION,
190   EAGER_ARGUMENTS_ALLOCATION,
191   LAZY_ARGUMENTS_ALLOCATION
192 };
193
194
195 // Different nop operations are used by the code generator to detect certain
196 // states of the generated code.
197 enum NopMarkerTypes {
198   NON_MARKING_NOP = 0,
199   PROPERTY_ACCESS_INLINED
200 };
201
202
203 // -------------------------------------------------------------------------
204 // CodeGenerator
205
206 class CodeGenerator: public AstVisitor {
207  public:
208   // Takes a function literal, generates code for it. This function should only
209   // be called by compiler.cc.
210   static Handle<Code> MakeCode(CompilationInfo* info);
211
212   // Printing of AST, etc. as requested by flags.
213   static void MakeCodePrologue(CompilationInfo* info);
214
215   // Allocate and install the code.
216   static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
217                                        Code::Flags flags,
218                                        CompilationInfo* info);
219
220 #ifdef ENABLE_LOGGING_AND_PROFILING
221   static bool ShouldGenerateLog(Expression* type);
222 #endif
223
224   static void SetFunctionInfo(Handle<JSFunction> fun,
225                               FunctionLiteral* lit,
226                               bool is_toplevel,
227                               Handle<Script> script);
228
229   static bool RecordPositions(MacroAssembler* masm,
230                               int pos,
231                               bool right_here = false);
232
233   // Accessors
234   MacroAssembler* masm() { return masm_; }
235   VirtualFrame* frame() const { return frame_; }
236   inline Handle<Script> script();
237
238   bool has_valid_frame() const { return frame_ != NULL; }
239
240   // Set the virtual frame to be new_frame, with non-frame register
241   // reference counts given by non_frame_registers.  The non-frame
242   // register reference counts of the old frame are returned in
243   // non_frame_registers.
244   void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
245
246   void DeleteFrame();
247
248   RegisterAllocator* allocator() const { return allocator_; }
249
250   CodeGenState* state() { return state_; }
251   void set_state(CodeGenState* state) { state_ = state; }
252
253   TypeInfo type_info(Slot* slot) {
254     int index = NumberOfSlot(slot);
255     if (index == kInvalidSlotNumber) return TypeInfo::Unknown();
256     return (*type_info_)[index];
257   }
258
259   TypeInfo set_type_info(Slot* slot, TypeInfo info) {
260     int index = NumberOfSlot(slot);
261     ASSERT(index >= kInvalidSlotNumber);
262     if (index != kInvalidSlotNumber) {
263       TypeInfo previous_value = (*type_info_)[index];
264       (*type_info_)[index] = info;
265       return previous_value;
266     }
267     return TypeInfo::Unknown();
268   }
269
270   void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
271
272   static const int kUnknownIntValue = -1;
273
274   // If the name is an inline runtime function call return the number of
275   // expected arguments. Otherwise return -1.
276   static int InlineRuntimeCallArgumentsCount(Handle<String> name);
277
278   // Constants related to patching of inlined load/store.
279   static const int kInlinedKeyedLoadInstructionsAfterPatch = 17;
280   static const int kInlinedKeyedStoreInstructionsAfterPatch = 5;
281
282  private:
283   // Construction/Destruction
284   explicit CodeGenerator(MacroAssembler* masm);
285
286   // Accessors
287   inline bool is_eval();
288   inline Scope* scope();
289
290   // Generating deferred code.
291   void ProcessDeferred();
292
293   static const int kInvalidSlotNumber = -1;
294
295   int NumberOfSlot(Slot* slot);
296
297   // State
298   bool has_cc() const  { return cc_reg_ != al; }
299   JumpTarget* true_target() const  { return state_->true_target(); }
300   JumpTarget* false_target() const  { return state_->false_target(); }
301
302   // Track loop nesting level.
303   int loop_nesting() const { return loop_nesting_; }
304   void IncrementLoopNesting() { loop_nesting_++; }
305   void DecrementLoopNesting() { loop_nesting_--; }
306
307   // Node visitors.
308   void VisitStatements(ZoneList<Statement*>* statements);
309
310 #define DEF_VISIT(type) \
311   void Visit##type(type* node);
312   AST_NODE_LIST(DEF_VISIT)
313 #undef DEF_VISIT
314
315   // Main code generation function
316   void Generate(CompilationInfo* info);
317
318   // Returns the arguments allocation mode.
319   ArgumentsAllocationMode ArgumentsMode();
320
321   // Store the arguments object and allocate it if necessary.
322   void StoreArgumentsObject(bool initial);
323
324   // The following are used by class Reference.
325   void LoadReference(Reference* ref);
326   void UnloadReference(Reference* ref);
327
328   static MemOperand ContextOperand(Register context, int index) {
329     return MemOperand(context, Context::SlotOffset(index));
330   }
331
332   MemOperand SlotOperand(Slot* slot, Register tmp);
333
334   MemOperand ContextSlotOperandCheckExtensions(Slot* slot,
335                                                Register tmp,
336                                                Register tmp2,
337                                                JumpTarget* slow);
338
339   // Expressions
340   static MemOperand GlobalObject()  {
341     return ContextOperand(cp, Context::GLOBAL_INDEX);
342   }
343
344   void LoadCondition(Expression* x,
345                      JumpTarget* true_target,
346                      JumpTarget* false_target,
347                      bool force_cc);
348   void Load(Expression* expr);
349   void LoadGlobal();
350   void LoadGlobalReceiver(Register scratch);
351
352   // Read a value from a slot and leave it on top of the expression stack.
353   void LoadFromSlot(Slot* slot, TypeofState typeof_state);
354   void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
355
356   // Store the value on top of the stack to a slot.
357   void StoreToSlot(Slot* slot, InitState init_state);
358
359   // Support for compiling assignment expressions.
360   void EmitSlotAssignment(Assignment* node);
361   void EmitNamedPropertyAssignment(Assignment* node);
362   void EmitKeyedPropertyAssignment(Assignment* node);
363
364   // Load a named property, returning it in r0. The receiver is passed on the
365   // stack, and remains there.
366   void EmitNamedLoad(Handle<String> name, bool is_contextual);
367
368   // Store to a named property. If the store is contextual, value is passed on
369   // the frame and consumed. Otherwise, receiver and value are passed on the
370   // frame and consumed. The result is returned in r0.
371   void EmitNamedStore(Handle<String> name, bool is_contextual);
372
373   // Load a keyed property, leaving it in r0.  The receiver and key are
374   // passed on the stack, and remain there.
375   void EmitKeyedLoad();
376
377   // Store a keyed property. Key and receiver are on the stack and the value is
378   // in r0. Result is returned in r0.
379   void EmitKeyedStore(StaticType* key_type);
380
381   void LoadFromGlobalSlotCheckExtensions(Slot* slot,
382                                          TypeofState typeof_state,
383                                          JumpTarget* slow);
384
385   // Support for loading from local/global variables and arguments
386   // whose location is known unless they are shadowed by
387   // eval-introduced bindings. Generates no code for unsupported slot
388   // types and therefore expects to fall through to the slow jump target.
389   void EmitDynamicLoadFromSlotFastCase(Slot* slot,
390                                        TypeofState typeof_state,
391                                        JumpTarget* slow,
392                                        JumpTarget* done);
393
394   // Special code for typeof expressions: Unfortunately, we must
395   // be careful when loading the expression in 'typeof'
396   // expressions. We are not allowed to throw reference errors for
397   // non-existing properties of the global object, so we must make it
398   // look like an explicit property access, instead of an access
399   // through the context chain.
400   void LoadTypeofExpression(Expression* x);
401
402   void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
403
404   // Generate code that computes a shortcutting logical operation.
405   void GenerateLogicalBooleanOperation(BinaryOperation* node);
406
407   void GenericBinaryOperation(Token::Value op,
408                               OverwriteMode overwrite_mode,
409                               GenerateInlineSmi inline_smi,
410                               int known_rhs = kUnknownIntValue);
411   void Comparison(Condition cc,
412                   Expression* left,
413                   Expression* right,
414                   bool strict = false);
415
416   void SmiOperation(Token::Value op,
417                     Handle<Object> value,
418                     bool reversed,
419                     OverwriteMode mode);
420
421   void CallWithArguments(ZoneList<Expression*>* arguments,
422                          CallFunctionFlags flags,
423                          int position);
424
425   // An optimized implementation of expressions of the form
426   // x.apply(y, arguments).  We call x the applicand and y the receiver.
427   // The optimization avoids allocating an arguments object if possible.
428   void CallApplyLazy(Expression* applicand,
429                      Expression* receiver,
430                      VariableProxy* arguments,
431                      int position);
432
433   // Control flow
434   void Branch(bool if_true, JumpTarget* target);
435   void CheckStack();
436
437   struct InlineRuntimeLUT {
438     void (CodeGenerator::*method)(ZoneList<Expression*>*);
439     const char* name;
440     int nargs;
441   };
442
443   static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
444   bool CheckForInlineRuntimeCall(CallRuntime* node);
445   static bool PatchInlineRuntimeEntry(Handle<String> name,
446                                       const InlineRuntimeLUT& new_entry,
447                                       InlineRuntimeLUT* old_entry);
448
449   static Handle<Code> ComputeLazyCompile(int argc);
450   void ProcessDeclarations(ZoneList<Declaration*>* declarations);
451
452   static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
453
454   static Handle<Code> ComputeKeyedCallInitialize(int argc, InLoopFlag in_loop);
455
456   // Declare global variables and functions in the given array of
457   // name/value pairs.
458   void DeclareGlobals(Handle<FixedArray> pairs);
459
460   // Instantiate the function based on the shared function info.
461   void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
462
463   // Support for type checks.
464   void GenerateIsSmi(ZoneList<Expression*>* args);
465   void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
466   void GenerateIsArray(ZoneList<Expression*>* args);
467   void GenerateIsRegExp(ZoneList<Expression*>* args);
468   void GenerateIsObject(ZoneList<Expression*>* args);
469   void GenerateIsFunction(ZoneList<Expression*>* args);
470   void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
471
472   // Support for construct call checks.
473   void GenerateIsConstructCall(ZoneList<Expression*>* args);
474
475   // Support for arguments.length and arguments[?].
476   void GenerateArgumentsLength(ZoneList<Expression*>* args);
477   void GenerateArguments(ZoneList<Expression*>* args);
478
479   // Support for accessing the class and value fields of an object.
480   void GenerateClassOf(ZoneList<Expression*>* args);
481   void GenerateValueOf(ZoneList<Expression*>* args);
482   void GenerateSetValueOf(ZoneList<Expression*>* args);
483
484   // Fast support for charCodeAt(n).
485   void GenerateStringCharCodeAt(ZoneList<Expression*>* args);
486
487   // Fast support for string.charAt(n) and string[n].
488   void GenerateStringCharFromCode(ZoneList<Expression*>* args);
489
490   // Fast support for string.charAt(n) and string[n].
491   void GenerateStringCharAt(ZoneList<Expression*>* args);
492
493   // Fast support for object equality testing.
494   void GenerateObjectEquals(ZoneList<Expression*>* args);
495
496   void GenerateLog(ZoneList<Expression*>* args);
497
498   // Fast support for Math.random().
499   void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
500
501   // Fast support for StringAdd.
502   void GenerateStringAdd(ZoneList<Expression*>* args);
503
504   // Fast support for SubString.
505   void GenerateSubString(ZoneList<Expression*>* args);
506
507   // Fast support for StringCompare.
508   void GenerateStringCompare(ZoneList<Expression*>* args);
509
510   // Support for direct calls from JavaScript to native RegExp code.
511   void GenerateRegExpExec(ZoneList<Expression*>* args);
512
513   void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
514
515   // Support for fast native caches.
516   void GenerateGetFromCache(ZoneList<Expression*>* args);
517
518   // Fast support for number to string.
519   void GenerateNumberToString(ZoneList<Expression*>* args);
520
521   // Fast swapping of elements.
522   void GenerateSwapElements(ZoneList<Expression*>* args);
523
524   // Fast call for custom callbacks.
525   void GenerateCallFunction(ZoneList<Expression*>* args);
526
527   // Fast call to math functions.
528   void GenerateMathPow(ZoneList<Expression*>* args);
529   void GenerateMathSin(ZoneList<Expression*>* args);
530   void GenerateMathCos(ZoneList<Expression*>* args);
531   void GenerateMathSqrt(ZoneList<Expression*>* args);
532
533   // Simple condition analysis.
534   enum ConditionAnalysis {
535     ALWAYS_TRUE,
536     ALWAYS_FALSE,
537     DONT_KNOW
538   };
539   ConditionAnalysis AnalyzeCondition(Expression* cond);
540
541   // Methods used to indicate which source code is generated for. Source
542   // positions are collected by the assembler and emitted with the relocation
543   // information.
544   void CodeForFunctionPosition(FunctionLiteral* fun);
545   void CodeForReturnPosition(FunctionLiteral* fun);
546   void CodeForStatementPosition(Statement* node);
547   void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
548   void CodeForSourcePosition(int pos);
549
550 #ifdef DEBUG
551   // True if the registers are valid for entry to a block.
552   bool HasValidEntryRegisters();
553 #endif
554
555   List<DeferredCode*> deferred_;
556
557   // Assembler
558   MacroAssembler* masm_;  // to generate code
559
560   CompilationInfo* info_;
561
562   // Code generation state
563   VirtualFrame* frame_;
564   RegisterAllocator* allocator_;
565   Condition cc_reg_;
566   CodeGenState* state_;
567   int loop_nesting_;
568
569   Vector<TypeInfo>* type_info_;
570
571   // Jump targets
572   BreakTarget function_return_;
573
574   // True if the function return is shadowed (ie, jumping to the target
575   // function_return_ does not jump to the true function return, but rather
576   // to some unlinking code).
577   bool function_return_is_shadowed_;
578
579   static InlineRuntimeLUT kInlineRuntimeLUT[];
580
581   friend class VirtualFrame;
582   friend class JumpTarget;
583   friend class Reference;
584   friend class FastCodeGenerator;
585   friend class FullCodeGenerator;
586   friend class FullCodeGenSyntaxChecker;
587
588   DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
589 };
590
591
592 class GenericBinaryOpStub : public CodeStub {
593  public:
594   GenericBinaryOpStub(Token::Value op,
595                       OverwriteMode mode,
596                       Register lhs,
597                       Register rhs,
598                       int constant_rhs = CodeGenerator::kUnknownIntValue)
599       : op_(op),
600         mode_(mode),
601         lhs_(lhs),
602         rhs_(rhs),
603         constant_rhs_(constant_rhs),
604         specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
605         runtime_operands_type_(BinaryOpIC::DEFAULT),
606         name_(NULL) { }
607
608   GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
609       : op_(OpBits::decode(key)),
610         mode_(ModeBits::decode(key)),
611         lhs_(LhsRegister(RegisterBits::decode(key))),
612         rhs_(RhsRegister(RegisterBits::decode(key))),
613         constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))),
614         specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)),
615         runtime_operands_type_(type_info),
616         name_(NULL) { }
617
618  private:
619   Token::Value op_;
620   OverwriteMode mode_;
621   Register lhs_;
622   Register rhs_;
623   int constant_rhs_;
624   bool specialized_on_rhs_;
625   BinaryOpIC::TypeInfo runtime_operands_type_;
626   char* name_;
627
628   static const int kMaxKnownRhs = 0x40000000;
629   static const int kKnownRhsKeyBits = 6;
630
631   // Minor key encoding in 17 bits.
632   class ModeBits: public BitField<OverwriteMode, 0, 2> {};
633   class OpBits: public BitField<Token::Value, 2, 6> {};
634   class TypeInfoBits: public BitField<int, 8, 2> {};
635   class RegisterBits: public BitField<bool, 10, 1> {};
636   class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
637
638   Major MajorKey() { return GenericBinaryOp; }
639   int MinorKey() {
640     ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
641            (lhs_.is(r1) && rhs_.is(r0)));
642     // Encode the parameters in a unique 18 bit value.
643     return OpBits::encode(op_)
644            | ModeBits::encode(mode_)
645            | KnownIntBits::encode(MinorKeyForKnownInt())
646            | TypeInfoBits::encode(runtime_operands_type_)
647            | RegisterBits::encode(lhs_.is(r0));
648   }
649
650   void Generate(MacroAssembler* masm);
651   void HandleNonSmiBitwiseOp(MacroAssembler* masm, Register lhs, Register rhs);
652   void HandleBinaryOpSlowCases(MacroAssembler* masm,
653                                Label* not_smi,
654                                Register lhs,
655                                Register rhs,
656                                const Builtins::JavaScript& builtin);
657   void GenerateTypeTransition(MacroAssembler* masm);
658
659   static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
660     if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
661     if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
662     if (op == Token::MOD) {
663       if (constant_rhs <= 1) return false;
664       if (constant_rhs <= 10) return true;
665       if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
666       return false;
667     }
668     return false;
669   }
670
671   int MinorKeyForKnownInt() {
672     if (!specialized_on_rhs_) return 0;
673     if (constant_rhs_ <= 10) return constant_rhs_ + 1;
674     ASSERT(IsPowerOf2(constant_rhs_));
675     int key = 12;
676     int d = constant_rhs_;
677     while ((d & 1) == 0) {
678       key++;
679       d >>= 1;
680     }
681     ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
682     return key;
683   }
684
685   int KnownBitsForMinorKey(int key) {
686     if (!key) return 0;
687     if (key <= 11) return key - 1;
688     int d = 1;
689     while (key != 12) {
690       key--;
691       d <<= 1;
692     }
693     return d;
694   }
695
696   Register LhsRegister(bool lhs_is_r0) {
697     return lhs_is_r0 ? r0 : r1;
698   }
699
700   Register RhsRegister(bool lhs_is_r0) {
701     return lhs_is_r0 ? r1 : r0;
702   }
703
704   bool ShouldGenerateSmiCode() {
705     return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) &&
706         runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
707         runtime_operands_type_ != BinaryOpIC::STRINGS;
708   }
709
710   bool ShouldGenerateFPCode() {
711     return runtime_operands_type_ != BinaryOpIC::STRINGS;
712   }
713
714   virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
715
716   virtual InlineCacheState GetICState() {
717     return BinaryOpIC::ToState(runtime_operands_type_);
718   }
719
720   const char* GetName();
721
722 #ifdef DEBUG
723   void Print() {
724     if (!specialized_on_rhs_) {
725       PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
726     } else {
727       PrintF("GenericBinaryOpStub (%s by %d)\n",
728              Token::String(op_),
729              constant_rhs_);
730     }
731   }
732 #endif
733 };
734
735
736 class StringHelper : public AllStatic {
737  public:
738   // Generate code for copying characters using a simple loop. This should only
739   // be used in places where the number of characters is small and the
740   // additional setup and checking in GenerateCopyCharactersLong adds too much
741   // overhead. Copying of overlapping regions is not supported.
742   // Dest register ends at the position after the last character written.
743   static void GenerateCopyCharacters(MacroAssembler* masm,
744                                      Register dest,
745                                      Register src,
746                                      Register count,
747                                      Register scratch,
748                                      bool ascii);
749
750   // Generate code for copying a large number of characters. This function
751   // is allowed to spend extra time setting up conditions to make copying
752   // faster. Copying of overlapping regions is not supported.
753   // Dest register ends at the position after the last character written.
754   static void GenerateCopyCharactersLong(MacroAssembler* masm,
755                                          Register dest,
756                                          Register src,
757                                          Register count,
758                                          Register scratch1,
759                                          Register scratch2,
760                                          Register scratch3,
761                                          Register scratch4,
762                                          Register scratch5,
763                                          int flags);
764
765
766   // Probe the symbol table for a two character string. If the string is
767   // not found by probing a jump to the label not_found is performed. This jump
768   // does not guarantee that the string is not in the symbol table. If the
769   // string is found the code falls through with the string in register r0.
770   // Contents of both c1 and c2 registers are modified. At the exit c1 is
771   // guaranteed to contain halfword with low and high bytes equal to
772   // initial contents of c1 and c2 respectively.
773   static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
774                                                    Register c1,
775                                                    Register c2,
776                                                    Register scratch1,
777                                                    Register scratch2,
778                                                    Register scratch3,
779                                                    Register scratch4,
780                                                    Register scratch5,
781                                                    Label* not_found);
782
783   // Generate string hash.
784   static void GenerateHashInit(MacroAssembler* masm,
785                                Register hash,
786                                Register character);
787
788   static void GenerateHashAddCharacter(MacroAssembler* masm,
789                                        Register hash,
790                                        Register character);
791
792   static void GenerateHashGetHash(MacroAssembler* masm,
793                                   Register hash);
794
795  private:
796   DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
797 };
798
799
800 // Flag that indicates how to generate code for the stub StringAddStub.
801 enum StringAddFlags {
802   NO_STRING_ADD_FLAGS = 0,
803   NO_STRING_CHECK_IN_STUB = 1 << 0  // Omit string check in stub.
804 };
805
806
807 class StringAddStub: public CodeStub {
808  public:
809   explicit StringAddStub(StringAddFlags flags) {
810     string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
811   }
812
813  private:
814   Major MajorKey() { return StringAdd; }
815   int MinorKey() { return string_check_ ? 0 : 1; }
816
817   void Generate(MacroAssembler* masm);
818
819   // Should the stub check whether arguments are strings?
820   bool string_check_;
821 };
822
823
824 class SubStringStub: public CodeStub {
825  public:
826   SubStringStub() {}
827
828  private:
829   Major MajorKey() { return SubString; }
830   int MinorKey() { return 0; }
831
832   void Generate(MacroAssembler* masm);
833 };
834
835
836
837 class StringCompareStub: public CodeStub {
838  public:
839   StringCompareStub() { }
840
841   // Compare two flat ASCII strings and returns result in r0.
842   // Does not use the stack.
843   static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
844                                               Register left,
845                                               Register right,
846                                               Register scratch1,
847                                               Register scratch2,
848                                               Register scratch3,
849                                               Register scratch4);
850
851  private:
852   Major MajorKey() { return StringCompare; }
853   int MinorKey() { return 0; }
854
855   void Generate(MacroAssembler* masm);
856 };
857
858
859 // This stub can convert a signed int32 to a heap number (double).  It does
860 // not work for int32s that are in Smi range!  No GC occurs during this stub
861 // so you don't have to set up the frame.
862 class WriteInt32ToHeapNumberStub : public CodeStub {
863  public:
864   WriteInt32ToHeapNumberStub(Register the_int,
865                              Register the_heap_number,
866                              Register scratch)
867       : the_int_(the_int),
868         the_heap_number_(the_heap_number),
869         scratch_(scratch) { }
870
871  private:
872   Register the_int_;
873   Register the_heap_number_;
874   Register scratch_;
875
876   // Minor key encoding in 16 bits.
877   class IntRegisterBits: public BitField<int, 0, 4> {};
878   class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
879   class ScratchRegisterBits: public BitField<int, 8, 4> {};
880
881   Major MajorKey() { return WriteInt32ToHeapNumber; }
882   int MinorKey() {
883     // Encode the parameters in a unique 16 bit value.
884     return IntRegisterBits::encode(the_int_.code())
885            | HeapNumberRegisterBits::encode(the_heap_number_.code())
886            | ScratchRegisterBits::encode(scratch_.code());
887   }
888
889   void Generate(MacroAssembler* masm);
890
891   const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
892
893 #ifdef DEBUG
894   void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
895 #endif
896 };
897
898
899 class NumberToStringStub: public CodeStub {
900  public:
901   NumberToStringStub() { }
902
903   // Generate code to do a lookup in the number string cache. If the number in
904   // the register object is found in the cache the generated code falls through
905   // with the result in the result register. The object and the result register
906   // can be the same. If the number is not found in the cache the code jumps to
907   // the label not_found with only the content of register object unchanged.
908   static void GenerateLookupNumberStringCache(MacroAssembler* masm,
909                                               Register object,
910                                               Register result,
911                                               Register scratch1,
912                                               Register scratch2,
913                                               Register scratch3,
914                                               bool object_is_smi,
915                                               Label* not_found);
916
917  private:
918   Major MajorKey() { return NumberToString; }
919   int MinorKey() { return 0; }
920
921   void Generate(MacroAssembler* masm);
922
923   const char* GetName() { return "NumberToStringStub"; }
924
925 #ifdef DEBUG
926   void Print() {
927     PrintF("NumberToStringStub\n");
928   }
929 #endif
930 };
931
932
933 class RecordWriteStub : public CodeStub {
934  public:
935   RecordWriteStub(Register object, Register offset, Register scratch)
936       : object_(object), offset_(offset), scratch_(scratch) { }
937
938   void Generate(MacroAssembler* masm);
939
940  private:
941   Register object_;
942   Register offset_;
943   Register scratch_;
944
945 #ifdef DEBUG
946   void Print() {
947     PrintF("RecordWriteStub (object reg %d), (offset reg %d),"
948            " (scratch reg %d)\n",
949            object_.code(), offset_.code(), scratch_.code());
950   }
951 #endif
952
953   // Minor key encoding in 12 bits. 4 bits for each of the three
954   // registers (object, offset and scratch) OOOOAAAASSSS.
955   class ScratchBits: public BitField<uint32_t, 0, 4> {};
956   class OffsetBits: public BitField<uint32_t, 4, 4> {};
957   class ObjectBits: public BitField<uint32_t, 8, 4> {};
958
959   Major MajorKey() { return RecordWrite; }
960
961   int MinorKey() {
962     // Encode the registers.
963     return ObjectBits::encode(object_.code()) |
964            OffsetBits::encode(offset_.code()) |
965            ScratchBits::encode(scratch_.code());
966   }
967 };
968
969
970 } }  // namespace v8::internal
971
972 #endif  // V8_ARM_CODEGEN_ARM_H_