deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / 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         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     PopulateDeoptimizationLiteralsWithInlinedFunctions();
42   }
43
44   // Simple accessors.
45   Scope* scope() const { return scope_; }
46
47   int LookupDestination(int block_id) const {
48     return chunk()->LookupDestination(block_id);
49   }
50
51   bool IsNextEmittedBlock(int block_id) const {
52     return LookupDestination(block_id) == GetNextEmittedBlock();
53   }
54
55   bool NeedsEagerFrame() const {
56     return GetStackSlotCount() > 0 ||
57         info()->is_non_deferred_calling() ||
58         !info()->IsStub() ||
59         info()->requires_frame();
60   }
61   bool NeedsDeferredFrame() const {
62     return !NeedsEagerFrame() && info()->is_deferred_calling();
63   }
64
65   LinkRegisterStatus GetLinkRegisterState() const {
66     return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
67   }
68
69   // Try to generate code for the entire chunk, but it may fail if the
70   // chunk contains constructs we cannot handle. Returns true if the
71   // code generation attempt succeeded.
72   bool GenerateCode();
73
74   // Finish the code by setting stack height, safepoint, and bailout
75   // information on it.
76   void FinishCode(Handle<Code> code);
77
78   enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
79   // Support for converting LOperands to assembler types.
80   Register ToRegister(LOperand* op) const;
81   Register ToRegister32(LOperand* op) const;
82   Operand ToOperand(LOperand* op);
83   Operand ToOperand32(LOperand* op);
84   MemOperand ToMemOperand(LOperand* op) const;
85   Handle<Object> ToHandle(LConstantOperand* op) const;
86
87   template <class LI>
88   Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
89
90   int JSShiftAmountFromLConstant(LOperand* constant) {
91     return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
92   }
93
94   // TODO(jbramley): Examine these helpers and check that they make sense.
95   // IsInteger32Constant returns true for smi constants, for example.
96   bool IsInteger32Constant(LConstantOperand* op) const;
97   bool IsSmi(LConstantOperand* op) const;
98
99   int32_t ToInteger32(LConstantOperand* op) const;
100   Smi* ToSmi(LConstantOperand* op) const;
101   double ToDouble(LConstantOperand* op) const;
102   DoubleRegister ToDoubleRegister(LOperand* op) const;
103
104   // Declare methods that deal with the individual node types.
105 #define DECLARE_DO(type) void Do##type(L##type* node);
106   LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
107 #undef DECLARE_DO
108
109  private:
110   // Return a double scratch register which can be used locally
111   // when generating code for a lithium instruction.
112   DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
113
114   // Deferred code support.
115   void DoDeferredNumberTagD(LNumberTagD* instr);
116   void DoDeferredStackCheck(LStackCheck* 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   int DefineDeoptimizationLiteral(Handle<Object> literal);
201   void PopulateDeoptimizationData(Handle<Code> code);
202   void PopulateDeoptimizationLiteralsWithInlinedFunctions();
203
204   MemOperand BuildSeqStringOperand(Register string,
205                                    Register temp,
206                                    LOperand* index,
207                                    String::Encoding encoding);
208   void DeoptimizeBranch(LInstruction* instr,
209                         Deoptimizer::DeoptReason deopt_reason,
210                         BranchType branch_type, Register reg = NoReg,
211                         int bit = -1,
212                         Deoptimizer::BailoutType* override_bailout_type = NULL);
213   void Deoptimize(LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
214                   Deoptimizer::BailoutType* override_bailout_type = NULL);
215   void DeoptimizeIf(Condition cond, LInstruction* instr,
216                     Deoptimizer::DeoptReason deopt_reason);
217   void DeoptimizeIfZero(Register rt, LInstruction* instr,
218                         Deoptimizer::DeoptReason deopt_reason);
219   void DeoptimizeIfNotZero(Register rt, LInstruction* instr,
220                            Deoptimizer::DeoptReason deopt_reason);
221   void DeoptimizeIfNegative(Register rt, LInstruction* instr,
222                             Deoptimizer::DeoptReason deopt_reason);
223   void DeoptimizeIfSmi(Register rt, LInstruction* instr,
224                        Deoptimizer::DeoptReason deopt_reason);
225   void DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
226                           Deoptimizer::DeoptReason deopt_reason);
227   void DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
228                         LInstruction* instr,
229                         Deoptimizer::DeoptReason deopt_reason);
230   void DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
231                            LInstruction* instr,
232                            Deoptimizer::DeoptReason deopt_reason);
233   void DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr);
234   void DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
235                              Deoptimizer::DeoptReason deopt_reason);
236   void DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
237                           Deoptimizer::DeoptReason deopt_reason);
238   void DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
239                             Deoptimizer::DeoptReason deopt_reason);
240
241   MemOperand PrepareKeyedExternalArrayOperand(Register key,
242                                               Register base,
243                                               Register scratch,
244                                               bool key_is_smi,
245                                               bool key_is_constant,
246                                               int constant_key,
247                                               ElementsKind elements_kind,
248                                               int base_offset);
249   MemOperand PrepareKeyedArrayOperand(Register base,
250                                       Register elements,
251                                       Register key,
252                                       bool key_is_tagged,
253                                       ElementsKind elements_kind,
254                                       Representation representation,
255                                       int base_offset);
256
257   void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
258                                             Safepoint::DeoptMode mode);
259
260   int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
261
262   void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
263
264   // Emit frame translation commands for an environment.
265   void WriteTranslation(LEnvironment* environment, Translation* translation);
266
267   void AddToTranslation(LEnvironment* environment,
268                         Translation* translation,
269                         LOperand* op,
270                         bool is_tagged,
271                         bool is_uint32,
272                         int* object_index_pointer,
273                         int* dematerialized_index_pointer);
274
275   void SaveCallerDoubles();
276   void RestoreCallerDoubles();
277
278   // Code generation steps.  Returns true if code generation should continue.
279   void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
280   bool GeneratePrologue();
281   bool GenerateDeferredCode();
282   bool GenerateJumpTable();
283   bool GenerateSafepointTable();
284
285   // Generates the custom OSR entrypoint and sets the osr_pc_offset.
286   void GenerateOsrPrologue();
287
288   enum SafepointMode {
289     RECORD_SIMPLE_SAFEPOINT,
290     RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
291   };
292
293   void CallCode(Handle<Code> code,
294                 RelocInfo::Mode mode,
295                 LInstruction* instr);
296
297   void CallCodeGeneric(Handle<Code> code,
298                        RelocInfo::Mode mode,
299                        LInstruction* instr,
300                        SafepointMode safepoint_mode);
301
302   void CallRuntime(const Runtime::Function* function,
303                    int num_arguments,
304                    LInstruction* instr,
305                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
306
307   void CallRuntime(Runtime::FunctionId id,
308                    int num_arguments,
309                    LInstruction* instr) {
310     const Runtime::Function* function = Runtime::FunctionForId(id);
311     CallRuntime(function, num_arguments, instr);
312   }
313
314   void LoadContextFromDeferred(LOperand* context);
315   void CallRuntimeFromDeferred(Runtime::FunctionId id,
316                                int argc,
317                                LInstruction* instr,
318                                LOperand* context);
319
320   // Generate a direct call to a known function.  Expects the function
321   // to be in x1.
322   void CallKnownFunction(Handle<JSFunction> function,
323                          int formal_parameter_count, int arity,
324                          LInstruction* instr);
325
326   // Support for recording safepoint and position information.
327   void RecordAndWritePosition(int position) OVERRIDE;
328   void RecordSafepoint(LPointerMap* pointers,
329                        Safepoint::Kind kind,
330                        int arguments,
331                        Safepoint::DeoptMode mode);
332   void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
333   void RecordSafepoint(Safepoint::DeoptMode mode);
334   void RecordSafepointWithRegisters(LPointerMap* pointers,
335                                     int arguments,
336                                     Safepoint::DeoptMode mode);
337   void RecordSafepointWithLazyDeopt(LInstruction* instr,
338                                     SafepointMode safepoint_mode);
339
340   void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
341
342   ZoneList<LEnvironment*> deoptimizations_;
343   ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
344   ZoneList<Handle<Object> > deoptimization_literals_;
345   int inlined_function_count_;
346   Scope* const scope_;
347   TranslationBuffer translations_;
348   ZoneList<LDeferredCode*> deferred_;
349   int osr_pc_offset_;
350   bool frame_is_built_;
351
352   // Builder that keeps track of safepoints in the code. The table itself is
353   // emitted at the end of the generated code.
354   SafepointTableBuilder safepoints_;
355
356   // Compiler from a set of parallel moves to a sequential list of moves.
357   LGapResolver resolver_;
358
359   Safepoint::Kind expected_safepoint_kind_;
360
361   int old_position_;
362
363   class PushSafepointRegistersScope BASE_EMBEDDED {
364    public:
365     explicit PushSafepointRegistersScope(LCodeGen* codegen)
366         : codegen_(codegen) {
367       DCHECK(codegen_->info()->is_calling());
368       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
369       codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
370
371       UseScratchRegisterScope temps(codegen_->masm_);
372       // Preserve the value of lr which must be saved on the stack (the call to
373       // the stub will clobber it).
374       Register to_be_pushed_lr =
375           temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
376       codegen_->masm_->Mov(to_be_pushed_lr, lr);
377       StoreRegistersStateStub stub(codegen_->isolate());
378       codegen_->masm_->CallStub(&stub);
379     }
380
381     ~PushSafepointRegistersScope() {
382       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
383       RestoreRegistersStateStub stub(codegen_->isolate());
384       codegen_->masm_->CallStub(&stub);
385       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
386     }
387
388    private:
389     LCodeGen* codegen_;
390   };
391
392   friend class LDeferredCode;
393   friend class SafepointGenerator;
394   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
395 };
396
397
398 class LDeferredCode: public ZoneObject {
399  public:
400   explicit LDeferredCode(LCodeGen* codegen)
401       : codegen_(codegen),
402         external_exit_(NULL),
403         instruction_index_(codegen->current_instruction_) {
404     codegen->AddDeferredCode(this);
405   }
406
407   virtual ~LDeferredCode() { }
408   virtual void Generate() = 0;
409   virtual LInstruction* instr() = 0;
410
411   void SetExit(Label* exit) { external_exit_ = exit; }
412   Label* entry() { return &entry_; }
413   Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
414   int instruction_index() const { return instruction_index_; }
415
416  protected:
417   LCodeGen* codegen() const { return codegen_; }
418   MacroAssembler* masm() const { return codegen_->masm(); }
419
420  private:
421   LCodeGen* codegen_;
422   Label entry_;
423   Label exit_;
424   Label* external_exit_;
425   int instruction_index_;
426 };
427
428
429 // This is the abstract class used by EmitBranchGeneric.
430 // It is used to emit code for conditional branching. The Emit() function
431 // emits code to branch when the condition holds and EmitInverted() emits
432 // the branch when the inverted condition is verified.
433 //
434 // For actual examples of condition see the concrete implementation in
435 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
436 class BranchGenerator BASE_EMBEDDED {
437  public:
438   explicit BranchGenerator(LCodeGen* codegen)
439     : codegen_(codegen) { }
440
441   virtual ~BranchGenerator() { }
442
443   virtual void Emit(Label* label) const = 0;
444   virtual void EmitInverted(Label* label) const = 0;
445
446  protected:
447   MacroAssembler* masm() const { return codegen_->masm(); }
448
449   LCodeGen* codegen_;
450 };
451
452 } }  // namespace v8::internal
453
454 #endif  // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_