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