Use Chrome compatible naming for compiler specifics.
[platform/upstream/v8.git] / src / arm64 / lithium-codegen-arm64.h
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_ARM64_LITHIUM_CODEGEN_ARM64_H_
6 #define V8_ARM64_LITHIUM_CODEGEN_ARM64_H_
7
8 #include "src/arm64/lithium-arm64.h"
9
10 #include "src/arm64/lithium-gap-resolver-arm64.h"
11 #include "src/deoptimizer.h"
12 #include "src/lithium-codegen.h"
13 #include "src/safepoint-table.h"
14 #include "src/scopes.h"
15 #include "src/utils.h"
16
17 namespace v8 {
18 namespace internal {
19
20 // Forward declarations.
21 class LDeferredCode;
22 class SafepointGenerator;
23 class BranchGenerator;
24
25 class LCodeGen: public LCodeGenBase {
26  public:
27   LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
28       : LCodeGenBase(chunk, assembler, info),
29         deoptimizations_(4, info->zone()),
30         deopt_jump_table_(4, info->zone()),
31         deoptimization_literals_(8, info->zone()),
32         inlined_function_count_(0),
33         scope_(info->scope()),
34         translations_(info->zone()),
35         deferred_(8, info->zone()),
36         osr_pc_offset_(-1),
37         frame_is_built_(false),
38         safepoints_(info->zone()),
39         resolver_(this),
40         expected_safepoint_kind_(Safepoint::kSimple),
41         after_push_argument_(false),
42         inlined_arguments_(false) {
43     PopulateDeoptimizationLiteralsWithInlinedFunctions();
44   }
45
46   ~LCodeGen() {
47     DCHECK(!after_push_argument_ || inlined_arguments_);
48   }
49
50   // Simple accessors.
51   Scope* scope() const { return scope_; }
52
53   int LookupDestination(int block_id) const {
54     return chunk()->LookupDestination(block_id);
55   }
56
57   bool IsNextEmittedBlock(int block_id) const {
58     return LookupDestination(block_id) == GetNextEmittedBlock();
59   }
60
61   bool NeedsEagerFrame() const {
62     return GetStackSlotCount() > 0 ||
63         info()->is_non_deferred_calling() ||
64         !info()->IsStub() ||
65         info()->requires_frame();
66   }
67   bool NeedsDeferredFrame() const {
68     return !NeedsEagerFrame() && info()->is_deferred_calling();
69   }
70
71   LinkRegisterStatus GetLinkRegisterState() const {
72     return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
73   }
74
75   // Try to generate code for the entire chunk, but it may fail if the
76   // chunk contains constructs we cannot handle. Returns true if the
77   // code generation attempt succeeded.
78   bool GenerateCode();
79
80   // Finish the code by setting stack height, safepoint, and bailout
81   // information on it.
82   void FinishCode(Handle<Code> code);
83
84   enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
85   // Support for converting LOperands to assembler types.
86   Register ToRegister(LOperand* op) const;
87   Register ToRegister32(LOperand* op) const;
88   Operand ToOperand(LOperand* op);
89   Operand ToOperand32(LOperand* op);
90   enum StackMode { kMustUseFramePointer, kCanUseStackPointer };
91   MemOperand ToMemOperand(LOperand* op,
92                           StackMode stack_mode = kCanUseStackPointer) const;
93   Handle<Object> ToHandle(LConstantOperand* op) const;
94
95   template <class LI>
96   Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
97
98   int JSShiftAmountFromLConstant(LOperand* constant) {
99     return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
100   }
101
102   // TODO(jbramley): Examine these helpers and check that they make sense.
103   // IsInteger32Constant returns true for smi constants, for example.
104   bool IsInteger32Constant(LConstantOperand* op) const;
105   bool IsSmi(LConstantOperand* op) const;
106
107   int32_t ToInteger32(LConstantOperand* op) const;
108   Smi* ToSmi(LConstantOperand* op) const;
109   double ToDouble(LConstantOperand* op) const;
110   DoubleRegister ToDoubleRegister(LOperand* op) const;
111
112   // Declare methods that deal with the individual node types.
113 #define DECLARE_DO(type) void Do##type(L##type* node);
114   LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
115 #undef DECLARE_DO
116
117  private:
118   // Return a double scratch register which can be used locally
119   // when generating code for a lithium instruction.
120   DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
121
122   // Deferred code support.
123   void DoDeferredNumberTagD(LNumberTagD* instr);
124   void DoDeferredStackCheck(LStackCheck* instr);
125   void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
126   void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
127   void DoDeferredMathAbsTagged(LMathAbsTagged* instr,
128                                Label* exit,
129                                Label* allocation_entry);
130
131   void DoDeferredNumberTagU(LInstruction* instr,
132                             LOperand* value,
133                             LOperand* temp1,
134                             LOperand* temp2);
135   void DoDeferredTaggedToI(LTaggedToI* instr,
136                            LOperand* value,
137                            LOperand* temp1,
138                            LOperand* temp2);
139   void DoDeferredAllocate(LAllocate* instr);
140   void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr);
141   void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
142   void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
143                                    Register result,
144                                    Register object,
145                                    Register index);
146
147   static Condition TokenToCondition(Token::Value op, bool is_unsigned);
148   void EmitGoto(int block);
149   void DoGap(LGap* instr);
150
151   // Generic version of EmitBranch. It contains some code to avoid emitting a
152   // branch on the next emitted basic block where we could just fall-through.
153   // You shouldn't use that directly but rather consider one of the helper like
154   // LCodeGen::EmitBranch, LCodeGen::EmitCompareAndBranch...
155   template<class InstrType>
156   void EmitBranchGeneric(InstrType instr,
157                          const BranchGenerator& branch);
158
159   template<class InstrType>
160   void EmitBranch(InstrType instr, Condition condition);
161
162   template<class InstrType>
163   void EmitCompareAndBranch(InstrType instr,
164                             Condition condition,
165                             const Register& lhs,
166                             const Operand& rhs);
167
168   template<class InstrType>
169   void EmitTestAndBranch(InstrType instr,
170                          Condition condition,
171                          const Register& value,
172                          uint64_t mask);
173
174   template<class InstrType>
175   void EmitBranchIfNonZeroNumber(InstrType instr,
176                                  const FPRegister& value,
177                                  const FPRegister& scratch);
178
179   template<class InstrType>
180   void EmitBranchIfHeapNumber(InstrType instr,
181                               const Register& value);
182
183   template<class InstrType>
184   void EmitBranchIfRoot(InstrType instr,
185                         const Register& value,
186                         Heap::RootListIndex index);
187
188   // Emits optimized code to deep-copy the contents of statically known object
189   // graphs (e.g. object literal boilerplate). Expects a pointer to the
190   // allocated destination object in the result register, and a pointer to the
191   // source object in the source register.
192   void EmitDeepCopy(Handle<JSObject> object,
193                     Register result,
194                     Register source,
195                     Register scratch,
196                     int* offset,
197                     AllocationSiteMode mode);
198
199   template <class T>
200   void EmitVectorLoadICRegisters(T* instr);
201
202   // Emits optimized code for %_IsString(x).  Preserves input register.
203   // Returns the condition on which a final split to
204   // true and false label should be made, to optimize fallthrough.
205   Condition EmitIsString(Register input, Register temp1, Label* is_not_string,
206                          SmiCheck check_needed);
207
208   int DefineDeoptimizationLiteral(Handle<Object> literal);
209   void PopulateDeoptimizationData(Handle<Code> code);
210   void PopulateDeoptimizationLiteralsWithInlinedFunctions();
211
212   MemOperand BuildSeqStringOperand(Register string,
213                                    Register temp,
214                                    LOperand* index,
215                                    String::Encoding encoding);
216   void DeoptimizeBranch(
217       LEnvironment* environment,
218       BranchType branch_type, Register reg = NoReg, int bit = -1,
219       Deoptimizer::BailoutType* override_bailout_type = NULL);
220   void Deoptimize(LEnvironment* environment,
221                   Deoptimizer::BailoutType* override_bailout_type = NULL);
222   void DeoptimizeIf(Condition cond, LEnvironment* environment);
223   void DeoptimizeIfZero(Register rt, LEnvironment* environment);
224   void DeoptimizeIfNotZero(Register rt, LEnvironment* environment);
225   void DeoptimizeIfNegative(Register rt, LEnvironment* environment);
226   void DeoptimizeIfSmi(Register rt, LEnvironment* environment);
227   void DeoptimizeIfNotSmi(Register rt, LEnvironment* environment);
228   void DeoptimizeIfRoot(Register rt,
229                         Heap::RootListIndex index,
230                         LEnvironment* environment);
231   void DeoptimizeIfNotRoot(Register rt,
232                            Heap::RootListIndex index,
233                            LEnvironment* environment);
234   void DeoptimizeIfMinusZero(DoubleRegister input, LEnvironment* environment);
235   void DeoptimizeIfBitSet(Register rt, int bit, LEnvironment* environment);
236   void DeoptimizeIfBitClear(Register rt, int bit, LEnvironment* environment);
237
238   MemOperand PrepareKeyedExternalArrayOperand(Register key,
239                                               Register base,
240                                               Register scratch,
241                                               bool key_is_smi,
242                                               bool key_is_constant,
243                                               int constant_key,
244                                               ElementsKind elements_kind,
245                                               int base_offset);
246   MemOperand PrepareKeyedArrayOperand(Register base,
247                                       Register elements,
248                                       Register key,
249                                       bool key_is_tagged,
250                                       ElementsKind elements_kind,
251                                       Representation representation,
252                                       int base_offset);
253
254   void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
255                                             Safepoint::DeoptMode mode);
256
257   int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
258
259   void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
260
261   // Emit frame translation commands for an environment.
262   void WriteTranslation(LEnvironment* environment, Translation* translation);
263
264   void AddToTranslation(LEnvironment* environment,
265                         Translation* translation,
266                         LOperand* op,
267                         bool is_tagged,
268                         bool is_uint32,
269                         int* object_index_pointer,
270                         int* dematerialized_index_pointer);
271
272   void SaveCallerDoubles();
273   void RestoreCallerDoubles();
274
275   // Code generation steps.  Returns true if code generation should continue.
276   void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
277   bool GeneratePrologue();
278   bool GenerateDeferredCode();
279   bool GenerateDeoptJumpTable();
280   bool GenerateSafepointTable();
281
282   // Generates the custom OSR entrypoint and sets the osr_pc_offset.
283   void GenerateOsrPrologue();
284
285   enum SafepointMode {
286     RECORD_SIMPLE_SAFEPOINT,
287     RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
288   };
289
290   void CallCode(Handle<Code> code,
291                 RelocInfo::Mode mode,
292                 LInstruction* instr);
293
294   void CallCodeGeneric(Handle<Code> code,
295                        RelocInfo::Mode mode,
296                        LInstruction* instr,
297                        SafepointMode safepoint_mode);
298
299   void CallRuntime(const Runtime::Function* function,
300                    int num_arguments,
301                    LInstruction* instr,
302                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
303
304   void CallRuntime(Runtime::FunctionId id,
305                    int num_arguments,
306                    LInstruction* instr) {
307     const Runtime::Function* function = Runtime::FunctionForId(id);
308     CallRuntime(function, num_arguments, instr);
309   }
310
311   void LoadContextFromDeferred(LOperand* context);
312   void CallRuntimeFromDeferred(Runtime::FunctionId id,
313                                int argc,
314                                LInstruction* instr,
315                                LOperand* context);
316
317   // Generate a direct call to a known function.
318   // If the function is already loaded into x1 by the caller, function_reg may
319   // be set to x1. Otherwise, it must be NoReg, and CallKnownFunction will
320   // automatically load it.
321   void CallKnownFunction(Handle<JSFunction> function,
322                          int formal_parameter_count,
323                          int arity,
324                          LInstruction* instr,
325                          Register function_reg = NoReg);
326
327   // Support for recording safepoint and position information.
328   void RecordAndWritePosition(int position) OVERRIDE;
329   void RecordSafepoint(LPointerMap* pointers,
330                        Safepoint::Kind kind,
331                        int arguments,
332                        Safepoint::DeoptMode mode);
333   void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
334   void RecordSafepoint(Safepoint::DeoptMode mode);
335   void RecordSafepointWithRegisters(LPointerMap* pointers,
336                                     int arguments,
337                                     Safepoint::DeoptMode mode);
338   void RecordSafepointWithLazyDeopt(LInstruction* instr,
339                                     SafepointMode safepoint_mode);
340
341   void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
342
343   ZoneList<LEnvironment*> deoptimizations_;
344   ZoneList<Deoptimizer::JumpTableEntry*> deopt_jump_table_;
345   ZoneList<Handle<Object> > deoptimization_literals_;
346   int inlined_function_count_;
347   Scope* const scope_;
348   TranslationBuffer translations_;
349   ZoneList<LDeferredCode*> deferred_;
350   int osr_pc_offset_;
351   bool frame_is_built_;
352
353   // Builder that keeps track of safepoints in the code. The table itself is
354   // emitted at the end of the generated code.
355   SafepointTableBuilder safepoints_;
356
357   // Compiler from a set of parallel moves to a sequential list of moves.
358   LGapResolver resolver_;
359
360   Safepoint::Kind expected_safepoint_kind_;
361
362   // This flag is true when we are after a push (but before a call).
363   // In this situation, jssp no longer references the end of the stack slots so,
364   // we can only reference a stack slot via fp.
365   bool after_push_argument_;
366   // If we have inlined arguments, we are no longer able to use jssp because
367   // jssp is modified and we never know if we are in a block after or before
368   // the pop of the arguments (which restores jssp).
369   bool inlined_arguments_;
370
371   int old_position_;
372
373   class PushSafepointRegistersScope BASE_EMBEDDED {
374    public:
375     explicit PushSafepointRegistersScope(LCodeGen* codegen)
376         : codegen_(codegen) {
377       DCHECK(codegen_->info()->is_calling());
378       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
379       codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
380
381       UseScratchRegisterScope temps(codegen_->masm_);
382       // Preserve the value of lr which must be saved on the stack (the call to
383       // the stub will clobber it).
384       Register to_be_pushed_lr =
385           temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
386       codegen_->masm_->Mov(to_be_pushed_lr, lr);
387       StoreRegistersStateStub stub(codegen_->isolate());
388       codegen_->masm_->CallStub(&stub);
389     }
390
391     ~PushSafepointRegistersScope() {
392       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
393       RestoreRegistersStateStub stub(codegen_->isolate());
394       codegen_->masm_->CallStub(&stub);
395       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
396     }
397
398    private:
399     LCodeGen* codegen_;
400   };
401
402   friend class LDeferredCode;
403   friend class SafepointGenerator;
404   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
405 };
406
407
408 class LDeferredCode: public ZoneObject {
409  public:
410   explicit LDeferredCode(LCodeGen* codegen)
411       : codegen_(codegen),
412         external_exit_(NULL),
413         instruction_index_(codegen->current_instruction_) {
414     codegen->AddDeferredCode(this);
415   }
416
417   virtual ~LDeferredCode() { }
418   virtual void Generate() = 0;
419   virtual LInstruction* instr() = 0;
420
421   void SetExit(Label* exit) { external_exit_ = exit; }
422   Label* entry() { return &entry_; }
423   Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
424   int instruction_index() const { return instruction_index_; }
425
426  protected:
427   LCodeGen* codegen() const { return codegen_; }
428   MacroAssembler* masm() const { return codegen_->masm(); }
429
430  private:
431   LCodeGen* codegen_;
432   Label entry_;
433   Label exit_;
434   Label* external_exit_;
435   int instruction_index_;
436 };
437
438
439 // This is the abstract class used by EmitBranchGeneric.
440 // It is used to emit code for conditional branching. The Emit() function
441 // emits code to branch when the condition holds and EmitInverted() emits
442 // the branch when the inverted condition is verified.
443 //
444 // For actual examples of condition see the concrete implementation in
445 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
446 class BranchGenerator BASE_EMBEDDED {
447  public:
448   explicit BranchGenerator(LCodeGen* codegen)
449     : codegen_(codegen) { }
450
451   virtual ~BranchGenerator() { }
452
453   virtual void Emit(Label* label) const = 0;
454   virtual void EmitInverted(Label* label) const = 0;
455
456  protected:
457   MacroAssembler* masm() const { return codegen_->masm(); }
458
459   LCodeGen* codegen_;
460 };
461
462 } }  // namespace v8::internal
463
464 #endif  // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_