be693898f0eb987a2e59514223725f0ded19fe97
[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 "arm/lithium-arm.h"
9
10 #include "arm/lithium-gap-resolver-arm.h"
11 #include "deoptimizer.h"
12 #include "lithium-codegen.h"
13 #include "safepoint-table.h"
14 #include "scopes.h"
15 #include "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 additional_index,
138                                  int additional_offset);
139
140   // Emit frame translation commands for an environment.
141   void WriteTranslation(LEnvironment* environment, Translation* translation);
142
143   // Declare methods that deal with the individual node types.
144 #define DECLARE_DO(type) void Do##type(L##type* node);
145   LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
146 #undef DECLARE_DO
147
148  private:
149   StrictMode strict_mode() const { return info()->strict_mode(); }
150
151   Scope* scope() const { return scope_; }
152
153   Register scratch0() { return r9; }
154   LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; }
155
156   LInstruction* GetNextInstruction();
157
158   void EmitClassOfTest(Label* if_true,
159                        Label* if_false,
160                        Handle<String> class_name,
161                        Register input,
162                        Register temporary,
163                        Register temporary2);
164
165   int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
166
167   void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
168
169   void SaveCallerDoubles();
170   void RestoreCallerDoubles();
171
172   // Code generation passes.  Returns true if code generation should
173   // continue.
174   void GenerateBodyInstructionPre(LInstruction* instr) V8_OVERRIDE;
175   bool GeneratePrologue();
176   bool GenerateDeferredCode();
177   bool GenerateDeoptJumpTable();
178   bool GenerateSafepointTable();
179
180   // Generates the custom OSR entrypoint and sets the osr_pc_offset.
181   void GenerateOsrPrologue();
182
183   enum SafepointMode {
184     RECORD_SIMPLE_SAFEPOINT,
185     RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
186   };
187
188   int CallCodeSize(Handle<Code> code, RelocInfo::Mode mode);
189
190   void CallCode(
191       Handle<Code> code,
192       RelocInfo::Mode mode,
193       LInstruction* instr,
194       TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
195
196   void CallCodeGeneric(
197       Handle<Code> code,
198       RelocInfo::Mode mode,
199       LInstruction* instr,
200       SafepointMode safepoint_mode,
201       TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
202
203   void CallRuntime(const Runtime::Function* function,
204                    int num_arguments,
205                    LInstruction* instr,
206                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
207
208   void CallRuntime(Runtime::FunctionId id,
209                    int num_arguments,
210                    LInstruction* instr) {
211     const Runtime::Function* function = Runtime::FunctionForId(id);
212     CallRuntime(function, num_arguments, instr);
213   }
214
215   void LoadContextFromDeferred(LOperand* context);
216   void CallRuntimeFromDeferred(Runtime::FunctionId id,
217                                int argc,
218                                LInstruction* instr,
219                                LOperand* context);
220
221   enum R1State {
222     R1_UNINITIALIZED,
223     R1_CONTAINS_TARGET
224   };
225
226   // Generate a direct call to a known function.  Expects the function
227   // to be in r1.
228   void CallKnownFunction(Handle<JSFunction> function,
229                          int formal_parameter_count,
230                          int arity,
231                          LInstruction* instr,
232                          R1State r1_state);
233
234   void RecordSafepointWithLazyDeopt(LInstruction* instr,
235                                     SafepointMode safepoint_mode);
236
237   void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
238                                             Safepoint::DeoptMode mode);
239   void DeoptimizeIf(Condition condition,
240                     LEnvironment* environment,
241                     Deoptimizer::BailoutType bailout_type);
242   void DeoptimizeIf(Condition condition, LEnvironment* environment);
243
244   void AddToTranslation(LEnvironment* environment,
245                         Translation* translation,
246                         LOperand* op,
247                         bool is_tagged,
248                         bool is_uint32,
249                         int* object_index_pointer,
250                         int* dematerialized_index_pointer);
251   void PopulateDeoptimizationData(Handle<Code> code);
252   int DefineDeoptimizationLiteral(Handle<Object> literal);
253
254   void PopulateDeoptimizationLiteralsWithInlinedFunctions();
255
256   Register ToRegister(int index) const;
257   DwVfpRegister ToDoubleRegister(int index) const;
258
259   MemOperand BuildSeqStringOperand(Register string,
260                                    LOperand* index,
261                                    String::Encoding encoding);
262
263   void EmitIntegerMathAbs(LMathAbs* instr);
264
265   // Support for recording safepoint and position information.
266   void RecordSafepoint(LPointerMap* pointers,
267                        Safepoint::Kind kind,
268                        int arguments,
269                        Safepoint::DeoptMode mode);
270   void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
271   void RecordSafepoint(Safepoint::DeoptMode mode);
272   void RecordSafepointWithRegisters(LPointerMap* pointers,
273                                     int arguments,
274                                     Safepoint::DeoptMode mode);
275   void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
276                                               int arguments,
277                                               Safepoint::DeoptMode mode);
278
279   void RecordAndWritePosition(int position) V8_OVERRIDE;
280
281   static Condition TokenToCondition(Token::Value op, bool is_unsigned);
282   void EmitGoto(int block);
283
284   // EmitBranch expects to be the last instruction of a block.
285   template<class InstrType>
286   void EmitBranch(InstrType instr, Condition condition);
287   template<class InstrType>
288   void EmitFalseBranch(InstrType instr, Condition condition);
289   void EmitNumberUntagD(Register input,
290                         DwVfpRegister result,
291                         bool allow_undefined_as_nan,
292                         bool deoptimize_on_minus_zero,
293                         LEnvironment* env,
294                         NumberUntagDMode mode);
295
296   // Emits optimized code for typeof x == "y".  Modifies input register.
297   // Returns the condition on which a final split to
298   // true and false label should be made, to optimize fallthrough.
299   Condition EmitTypeofIs(Label* true_label,
300                          Label* false_label,
301                          Register input,
302                          Handle<String> type_name);
303
304   // Emits optimized code for %_IsObject(x).  Preserves input register.
305   // Returns the condition on which a final split to
306   // true and false label should be made, to optimize fallthrough.
307   Condition EmitIsObject(Register input,
308                          Register temp1,
309                          Label* is_not_object,
310                          Label* is_object);
311
312   // Emits optimized code for %_IsString(x).  Preserves input register.
313   // Returns the condition on which a final split to
314   // true and false label should be made, to optimize fallthrough.
315   Condition EmitIsString(Register input,
316                          Register temp1,
317                          Label* is_not_string,
318                          SmiCheck check_needed);
319
320   // Emits optimized code for %_IsConstructCall().
321   // Caller should branch on equal condition.
322   void EmitIsConstructCall(Register temp1, Register temp2);
323
324   // Emits optimized code to deep-copy the contents of statically known
325   // object graphs (e.g. object literal boilerplate).
326   void EmitDeepCopy(Handle<JSObject> object,
327                     Register result,
328                     Register source,
329                     int* offset,
330                     AllocationSiteMode mode);
331
332   void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
333   void DoLoadKeyedExternalArray(LLoadKeyed* instr);
334   template<class T>
335   void DoLoadKeyedSIMD128ExternalArray(LLoadKeyed* instr);
336   void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
337   void DoLoadKeyedFixedArray(LLoadKeyed* instr);
338   void DoStoreKeyedExternalArray(LStoreKeyed* instr);
339   template<class T>
340   void DoStoreKeyedSIMD128ExternalArray(LStoreKeyed* instr);
341   void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
342   void DoStoreKeyedFixedArray(LStoreKeyed* instr);
343
344   ZoneList<LEnvironment*> deoptimizations_;
345   ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
346   ZoneList<Handle<Object> > deoptimization_literals_;
347   int inlined_function_count_;
348   Scope* const scope_;
349   TranslationBuffer translations_;
350   ZoneList<LDeferredCode*> deferred_;
351   int osr_pc_offset_;
352   bool frame_is_built_;
353
354   // Builder that keeps track of safepoints in the code. The table
355   // itself is emitted at the end of the generated code.
356   SafepointTableBuilder safepoints_;
357
358   // Compiler from a set of parallel moves to a sequential list of moves.
359   LGapResolver resolver_;
360
361   Safepoint::Kind expected_safepoint_kind_;
362
363   class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
364    public:
365     PushSafepointRegistersScope(LCodeGen* codegen,
366                                 Safepoint::Kind kind)
367         : codegen_(codegen) {
368       ASSERT(codegen_->info()->is_calling());
369       ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
370       codegen_->expected_safepoint_kind_ = kind;
371
372       switch (codegen_->expected_safepoint_kind_) {
373         case Safepoint::kWithRegisters:
374           codegen_->masm_->PushSafepointRegisters();
375           break;
376         case Safepoint::kWithRegistersAndDoubles:
377           codegen_->masm_->PushSafepointRegistersAndDoubles();
378           break;
379         default:
380           UNREACHABLE();
381       }
382     }
383
384     ~PushSafepointRegistersScope() {
385       Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
386       ASSERT((kind & Safepoint::kWithRegisters) != 0);
387       switch (kind) {
388         case Safepoint::kWithRegisters:
389           codegen_->masm_->PopSafepointRegisters();
390           break;
391         case Safepoint::kWithRegistersAndDoubles:
392           codegen_->masm_->PopSafepointRegistersAndDoubles();
393           break;
394         default:
395           UNREACHABLE();
396       }
397       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
398     }
399
400    private:
401     LCodeGen* codegen_;
402   };
403
404   friend class LDeferredCode;
405   friend class LEnvironment;
406   friend class SafepointGenerator;
407   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
408 };
409
410
411 class LDeferredCode : public ZoneObject {
412  public:
413   explicit LDeferredCode(LCodeGen* codegen)
414       : codegen_(codegen),
415         external_exit_(NULL),
416         instruction_index_(codegen->current_instruction_) {
417     codegen->AddDeferredCode(this);
418   }
419
420   virtual ~LDeferredCode() {}
421   virtual void Generate() = 0;
422   virtual LInstruction* instr() = 0;
423
424   void SetExit(Label* exit) { external_exit_ = exit; }
425   Label* entry() { return &entry_; }
426   Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
427   int instruction_index() const { return instruction_index_; }
428
429  protected:
430   LCodeGen* codegen() const { return codegen_; }
431   MacroAssembler* masm() const { return codegen_->masm(); }
432
433  private:
434   LCodeGen* codegen_;
435   Label entry_;
436   Label exit_;
437   Label* external_exit_;
438   int instruction_index_;
439 };
440
441 } }  // namespace v8::internal
442
443 #endif  // V8_ARM_LITHIUM_CODEGEN_ARM_H_