Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / arm / lithium-codegen-arm.h
1 // Copyright 2012 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_ARM_LITHIUM_CODEGEN_ARM_H_
6 #define V8_ARM_LITHIUM_CODEGEN_ARM_H_
7
8 #include "src/arm/lithium-arm.h"
9
10 #include "src/arm/lithium-gap-resolver-arm.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
24 class LCodeGen: public LCodeGenBase {
25  public:
26   LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
27       : LCodeGenBase(chunk, assembler, info),
28         deoptimizations_(4, info->zone()),
29         deopt_jump_table_(4, info->zone()),
30         deoptimization_literals_(8, 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
44   int LookupDestination(int block_id) const {
45     return chunk()->LookupDestination(block_id);
46   }
47
48   bool IsNextEmittedBlock(int block_id) const {
49     return LookupDestination(block_id) == GetNextEmittedBlock();
50   }
51
52   bool NeedsEagerFrame() const {
53     return GetStackSlotCount() > 0 ||
54         info()->is_non_deferred_calling() ||
55         !info()->IsStub() ||
56         info()->requires_frame();
57   }
58   bool NeedsDeferredFrame() const {
59     return !NeedsEagerFrame() && info()->is_deferred_calling();
60   }
61
62   LinkRegisterStatus GetLinkRegisterState() const {
63     return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
64   }
65
66   // Support for converting LOperands to assembler types.
67   // LOperand must be a register.
68   Register ToRegister(LOperand* op) const;
69
70   // LOperand is loaded into scratch, unless already a register.
71   Register EmitLoadRegister(LOperand* op, Register scratch);
72
73   // LOperand must be a double register.
74   DwVfpRegister ToDoubleRegister(LOperand* op) const;
75
76   // LOperand is loaded into dbl_scratch, unless already a double register.
77   DwVfpRegister EmitLoadDoubleRegister(LOperand* op,
78                                        SwVfpRegister flt_scratch,
79                                        DwVfpRegister dbl_scratch);
80   int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
81   int32_t ToInteger32(LConstantOperand* op) const;
82   Smi* ToSmi(LConstantOperand* op) const;
83   double ToDouble(LConstantOperand* op) const;
84   Operand ToOperand(LOperand* op);
85   MemOperand ToMemOperand(LOperand* op) const;
86   // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
87   MemOperand ToHighMemOperand(LOperand* op) const;
88
89   bool IsInteger32(LConstantOperand* op) const;
90   bool IsSmi(LConstantOperand* op) const;
91   Handle<Object> ToHandle(LConstantOperand* op) const;
92
93   // Try to generate code for the entire chunk, but it may fail if the
94   // chunk contains constructs we cannot handle. Returns true if the
95   // code generation attempt succeeded.
96   bool GenerateCode();
97
98   // Finish the code by setting stack height, safepoint, and bailout
99   // information on it.
100   void FinishCode(Handle<Code> code);
101
102   // Deferred code support.
103   void DoDeferredNumberTagD(LNumberTagD* instr);
104
105   enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
106   void DoDeferredNumberTagIU(LInstruction* instr,
107                              LOperand* value,
108                              LOperand* temp1,
109                              LOperand* temp2,
110                              IntegerSignedness signedness);
111
112   void DoDeferredTaggedToI(LTaggedToI* instr);
113   void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
114   void DoDeferredStackCheck(LStackCheck* instr);
115   void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
116   void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
117   void DoDeferredAllocate(LAllocate* instr);
118   void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
119                                        Label* map_check, Label* bool_load);
120   void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
121   void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
122                                    Register result,
123                                    Register object,
124                                    Register index);
125   void DoDeferredSIMD128ToTagged(LInstruction* instr, Runtime::FunctionId id);
126
127   // Parallel move support.
128   void DoParallelMove(LParallelMove* move);
129   void DoGap(LGap* instr);
130
131   MemOperand PrepareKeyedOperand(Register key,
132                                  Register base,
133                                  bool key_is_constant,
134                                  int constant_key,
135                                  int element_size,
136                                  int shift_size,
137                                  int base_offset);
138
139   // Emit frame translation commands for an environment.
140   void WriteTranslation(LEnvironment* environment, Translation* translation);
141
142   // Declare methods that deal with the individual node types.
143 #define DECLARE_DO(type) void Do##type(L##type* node);
144   LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
145 #undef DECLARE_DO
146
147  private:
148   StrictMode strict_mode() const { return info()->strict_mode(); }
149
150   Scope* scope() const { return scope_; }
151
152   Register scratch0() { return r9; }
153   LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; }
154
155   LInstruction* GetNextInstruction();
156
157   void EmitClassOfTest(Label* if_true,
158                        Label* if_false,
159                        Handle<String> class_name,
160                        Register input,
161                        Register temporary,
162                        Register temporary2);
163
164   int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
165
166   void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
167
168   void SaveCallerDoubles();
169   void RestoreCallerDoubles();
170
171   // Code generation passes.  Returns true if code generation should
172   // continue.
173   void GenerateBodyInstructionPre(LInstruction* instr) V8_OVERRIDE;
174   bool GeneratePrologue();
175   bool GenerateDeferredCode();
176   bool GenerateDeoptJumpTable();
177   bool GenerateSafepointTable();
178
179   // Generates the custom OSR entrypoint and sets the osr_pc_offset.
180   void GenerateOsrPrologue();
181
182   enum SafepointMode {
183     RECORD_SIMPLE_SAFEPOINT,
184     RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
185   };
186
187   int CallCodeSize(Handle<Code> code, RelocInfo::Mode mode);
188
189   void CallCode(
190       Handle<Code> code,
191       RelocInfo::Mode mode,
192       LInstruction* instr,
193       TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
194
195   void CallCodeGeneric(
196       Handle<Code> code,
197       RelocInfo::Mode mode,
198       LInstruction* instr,
199       SafepointMode safepoint_mode,
200       TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
201
202   void CallRuntime(const Runtime::Function* function,
203                    int num_arguments,
204                    LInstruction* instr,
205                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
206
207   void CallRuntime(Runtime::FunctionId id,
208                    int num_arguments,
209                    LInstruction* instr) {
210     const Runtime::Function* function = Runtime::FunctionForId(id);
211     CallRuntime(function, num_arguments, instr);
212   }
213
214   void LoadContextFromDeferred(LOperand* context);
215   void CallRuntimeFromDeferred(Runtime::FunctionId id,
216                                int argc,
217                                LInstruction* instr,
218                                LOperand* context);
219
220   enum R1State {
221     R1_UNINITIALIZED,
222     R1_CONTAINS_TARGET
223   };
224
225   // Generate a direct call to a known function.  Expects the function
226   // to be in r1.
227   void CallKnownFunction(Handle<JSFunction> function,
228                          int formal_parameter_count,
229                          int arity,
230                          LInstruction* instr,
231                          R1State r1_state);
232
233   void RecordSafepointWithLazyDeopt(LInstruction* instr,
234                                     SafepointMode safepoint_mode);
235
236   void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
237                                             Safepoint::DeoptMode mode);
238   void DeoptimizeIf(Condition condition,
239                     LEnvironment* environment,
240                     Deoptimizer::BailoutType bailout_type);
241   void DeoptimizeIf(Condition condition, LEnvironment* environment);
242
243   void AddToTranslation(LEnvironment* environment,
244                         Translation* translation,
245                         LOperand* op,
246                         bool is_tagged,
247                         bool is_uint32,
248                         int* object_index_pointer,
249                         int* dematerialized_index_pointer);
250   void PopulateDeoptimizationData(Handle<Code> code);
251   int DefineDeoptimizationLiteral(Handle<Object> literal);
252
253   void PopulateDeoptimizationLiteralsWithInlinedFunctions();
254
255   Register ToRegister(int index) const;
256   DwVfpRegister ToDoubleRegister(int index) const;
257
258   MemOperand BuildSeqStringOperand(Register string,
259                                    LOperand* index,
260                                    String::Encoding encoding);
261
262   void EmitIntegerMathAbs(LMathAbs* instr);
263
264   // Support for recording safepoint and position information.
265   void RecordSafepoint(LPointerMap* pointers,
266                        Safepoint::Kind kind,
267                        int arguments,
268                        Safepoint::DeoptMode mode);
269   void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
270   void RecordSafepoint(Safepoint::DeoptMode mode);
271   void RecordSafepointWithRegisters(LPointerMap* pointers,
272                                     int arguments,
273                                     Safepoint::DeoptMode mode);
274
275   void RecordAndWritePosition(int position) V8_OVERRIDE;
276
277   static Condition TokenToCondition(Token::Value op, bool is_unsigned);
278   void EmitGoto(int block);
279
280   // EmitBranch expects to be the last instruction of a block.
281   template<class InstrType>
282   void EmitBranch(InstrType instr, Condition condition);
283   template<class InstrType>
284   void EmitFalseBranch(InstrType instr, Condition condition);
285   void EmitNumberUntagD(Register input,
286                         DwVfpRegister result,
287                         bool allow_undefined_as_nan,
288                         bool deoptimize_on_minus_zero,
289                         LEnvironment* env,
290                         NumberUntagDMode mode);
291
292   // Emits optimized code for typeof x == "y".  Modifies input register.
293   // Returns the condition on which a final split to
294   // true and false label should be made, to optimize fallthrough.
295   Condition EmitTypeofIs(Label* true_label,
296                          Label* false_label,
297                          Register input,
298                          Handle<String> type_name);
299
300   // Emits optimized code for %_IsObject(x).  Preserves input register.
301   // Returns the condition on which a final split to
302   // true and false label should be made, to optimize fallthrough.
303   Condition EmitIsObject(Register input,
304                          Register temp1,
305                          Label* is_not_object,
306                          Label* is_object);
307
308   // Emits optimized code for %_IsString(x).  Preserves input register.
309   // Returns the condition on which a final split to
310   // true and false label should be made, to optimize fallthrough.
311   Condition EmitIsString(Register input,
312                          Register temp1,
313                          Label* is_not_string,
314                          SmiCheck check_needed);
315
316   // Emits optimized code for %_IsConstructCall().
317   // Caller should branch on equal condition.
318   void EmitIsConstructCall(Register temp1, Register temp2);
319
320   // Emits optimized code to deep-copy the contents of statically known
321   // object graphs (e.g. object literal boilerplate).
322   void EmitDeepCopy(Handle<JSObject> object,
323                     Register result,
324                     Register source,
325                     int* offset,
326                     AllocationSiteMode mode);
327
328   void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
329   void DoLoadKeyedExternalArray(LLoadKeyed* instr);
330   template<class T>
331   void DoLoadKeyedSIMD128ExternalArray(LLoadKeyed* instr);
332   void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
333   void DoLoadKeyedFixedArray(LLoadKeyed* instr);
334   void DoStoreKeyedExternalArray(LStoreKeyed* instr);
335   template<class T>
336   void DoStoreKeyedSIMD128ExternalArray(LStoreKeyed* instr);
337   void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
338   void DoStoreKeyedFixedArray(LStoreKeyed* instr);
339
340   ZoneList<LEnvironment*> deoptimizations_;
341   ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
342   ZoneList<Handle<Object> > deoptimization_literals_;
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
351   // itself is 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   class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
360    public:
361     explicit PushSafepointRegistersScope(LCodeGen* codegen)
362         : codegen_(codegen) {
363       DCHECK(codegen_->info()->is_calling());
364       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
365       codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
366       codegen_->masm_->PushSafepointRegisters();
367     }
368
369     ~PushSafepointRegistersScope() {
370       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
371       codegen_->masm_->PopSafepointRegisters();
372       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
373     }
374
375    private:
376     LCodeGen* codegen_;
377   };
378
379   friend class LDeferredCode;
380   friend class LEnvironment;
381   friend class SafepointGenerator;
382   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
383 };
384
385
386 class LDeferredCode : public ZoneObject {
387  public:
388   explicit LDeferredCode(LCodeGen* codegen)
389       : codegen_(codegen),
390         external_exit_(NULL),
391         instruction_index_(codegen->current_instruction_) {
392     codegen->AddDeferredCode(this);
393   }
394
395   virtual ~LDeferredCode() {}
396   virtual void Generate() = 0;
397   virtual LInstruction* instr() = 0;
398
399   void SetExit(Label* exit) { external_exit_ = exit; }
400   Label* entry() { return &entry_; }
401   Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
402   int instruction_index() const { return instruction_index_; }
403
404  protected:
405   LCodeGen* codegen() const { return codegen_; }
406   MacroAssembler* masm() const { return codegen_->masm(); }
407
408  private:
409   LCodeGen* codegen_;
410   Label entry_;
411   Label exit_;
412   Label* external_exit_;
413   int instruction_index_;
414 };
415
416 } }  // namespace v8::internal
417
418 #endif  // V8_ARM_LITHIUM_CODEGEN_ARM_H_