Upstream version 8.37.186.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);
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   void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
275                                               int arguments,
276                                               Safepoint::DeoptMode mode);
277
278   void RecordAndWritePosition(int position) V8_OVERRIDE;
279
280   static Condition TokenToCondition(Token::Value op, bool is_unsigned);
281   void EmitGoto(int block);
282
283   // EmitBranch expects to be the last instruction of a block.
284   template<class InstrType>
285   void EmitBranch(InstrType instr, Condition condition);
286   template<class InstrType>
287   void EmitFalseBranch(InstrType instr, Condition condition);
288   void EmitNumberUntagD(Register input,
289                         DwVfpRegister result,
290                         bool allow_undefined_as_nan,
291                         bool deoptimize_on_minus_zero,
292                         LEnvironment* env,
293                         NumberUntagDMode mode);
294
295   // Emits optimized code for typeof x == "y".  Modifies input register.
296   // Returns the condition on which a final split to
297   // true and false label should be made, to optimize fallthrough.
298   Condition EmitTypeofIs(Label* true_label,
299                          Label* false_label,
300                          Register input,
301                          Handle<String> type_name);
302
303   // Emits optimized code for %_IsObject(x).  Preserves input register.
304   // Returns the condition on which a final split to
305   // true and false label should be made, to optimize fallthrough.
306   Condition EmitIsObject(Register input,
307                          Register temp1,
308                          Label* is_not_object,
309                          Label* is_object);
310
311   // Emits optimized code for %_IsString(x).  Preserves input register.
312   // Returns the condition on which a final split to
313   // true and false label should be made, to optimize fallthrough.
314   Condition EmitIsString(Register input,
315                          Register temp1,
316                          Label* is_not_string,
317                          SmiCheck check_needed);
318
319   // Emits optimized code for %_IsConstructCall().
320   // Caller should branch on equal condition.
321   void EmitIsConstructCall(Register temp1, Register temp2);
322
323   // Emits optimized code to deep-copy the contents of statically known
324   // object graphs (e.g. object literal boilerplate).
325   void EmitDeepCopy(Handle<JSObject> object,
326                     Register result,
327                     Register source,
328                     int* offset,
329                     AllocationSiteMode mode);
330
331   void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
332   void DoLoadKeyedExternalArray(LLoadKeyed* instr);
333   template<class T>
334   void DoLoadKeyedSIMD128ExternalArray(LLoadKeyed* instr);
335   void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
336   void DoLoadKeyedFixedArray(LLoadKeyed* instr);
337   void DoStoreKeyedExternalArray(LStoreKeyed* instr);
338   template<class T>
339   void DoStoreKeyedSIMD128ExternalArray(LStoreKeyed* instr);
340   void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
341   void DoStoreKeyedFixedArray(LStoreKeyed* instr);
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
354   // itself is 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   class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
363    public:
364     PushSafepointRegistersScope(LCodeGen* codegen,
365                                 Safepoint::Kind kind)
366         : codegen_(codegen) {
367       ASSERT(codegen_->info()->is_calling());
368       ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
369       codegen_->expected_safepoint_kind_ = kind;
370
371       switch (codegen_->expected_safepoint_kind_) {
372         case Safepoint::kWithRegisters:
373           codegen_->masm_->PushSafepointRegisters();
374           break;
375         case Safepoint::kWithRegistersAndDoubles:
376           codegen_->masm_->PushSafepointRegistersAndDoubles();
377           break;
378         default:
379           UNREACHABLE();
380       }
381     }
382
383     ~PushSafepointRegistersScope() {
384       Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
385       ASSERT((kind & Safepoint::kWithRegisters) != 0);
386       switch (kind) {
387         case Safepoint::kWithRegisters:
388           codegen_->masm_->PopSafepointRegisters();
389           break;
390         case Safepoint::kWithRegistersAndDoubles:
391           codegen_->masm_->PopSafepointRegistersAndDoubles();
392           break;
393         default:
394           UNREACHABLE();
395       }
396       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
397     }
398
399    private:
400     LCodeGen* codegen_;
401   };
402
403   friend class LDeferredCode;
404   friend class LEnvironment;
405   friend class SafepointGenerator;
406   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
407 };
408
409
410 class LDeferredCode : public ZoneObject {
411  public:
412   explicit LDeferredCode(LCodeGen* codegen)
413       : codegen_(codegen),
414         external_exit_(NULL),
415         instruction_index_(codegen->current_instruction_) {
416     codegen->AddDeferredCode(this);
417   }
418
419   virtual ~LDeferredCode() {}
420   virtual void Generate() = 0;
421   virtual LInstruction* instr() = 0;
422
423   void SetExit(Label* exit) { external_exit_ = exit; }
424   Label* entry() { return &entry_; }
425   Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
426   int instruction_index() const { return instruction_index_; }
427
428  protected:
429   LCodeGen* codegen() const { return codegen_; }
430   MacroAssembler* masm() const { return codegen_->masm(); }
431
432  private:
433   LCodeGen* codegen_;
434   Label entry_;
435   Label exit_;
436   Label* external_exit_;
437   int instruction_index_;
438 };
439
440 } }  // namespace v8::internal
441
442 #endif  // V8_ARM_LITHIUM_CODEGEN_ARM_H_