• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CRANKSHAFT_IA32_LITHIUM_CODEGEN_IA32_H_
6 #define V8_CRANKSHAFT_IA32_LITHIUM_CODEGEN_IA32_H_
7 
8 #include "src/ast/scopes.h"
9 #include "src/base/logging.h"
10 #include "src/crankshaft/ia32/lithium-gap-resolver-ia32.h"
11 #include "src/crankshaft/ia32/lithium-ia32.h"
12 #include "src/crankshaft/lithium-codegen.h"
13 #include "src/deoptimizer.h"
14 #include "src/safepoint-table.h"
15 #include "src/utils.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 // Forward declarations.
21 class LDeferredCode;
22 class LGapNode;
23 class SafepointGenerator;
24 
25 class LCodeGen: public LCodeGenBase {
26  public:
LCodeGen(LChunk * chunk,MacroAssembler * assembler,CompilationInfo * info)27   LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
28       : LCodeGenBase(chunk, assembler, info),
29         jump_table_(4, info->zone()),
30         scope_(info->scope()),
31         deferred_(8, info->zone()),
32         dynamic_frame_alignment_(false),
33         support_aligned_spilled_doubles_(false),
34         frame_is_built_(false),
35         safepoints_(info->zone()),
36         resolver_(this),
37         expected_safepoint_kind_(Safepoint::kSimple) {
38     PopulateDeoptimizationLiteralsWithInlinedFunctions();
39   }
40 
LookupDestination(int block_id)41   int LookupDestination(int block_id) const {
42     return chunk()->LookupDestination(block_id);
43   }
44 
IsNextEmittedBlock(int block_id)45   bool IsNextEmittedBlock(int block_id) const {
46     return LookupDestination(block_id) == GetNextEmittedBlock();
47   }
48 
NeedsEagerFrame()49   bool NeedsEagerFrame() const {
50     return GetStackSlotCount() > 0 ||
51         info()->is_non_deferred_calling() ||
52         !info()->IsStub() ||
53         info()->requires_frame();
54   }
NeedsDeferredFrame()55   bool NeedsDeferredFrame() const {
56     return !NeedsEagerFrame() && info()->is_deferred_calling();
57   }
58 
59   // Support for converting LOperands to assembler types.
60   Operand ToOperand(LOperand* op) const;
61   Register ToRegister(LOperand* op) const;
62   XMMRegister ToDoubleRegister(LOperand* op) const;
63 
64   bool IsInteger32(LConstantOperand* op) const;
65   bool IsSmi(LConstantOperand* op) const;
ToImmediate(LOperand * op,const Representation & r)66   Immediate ToImmediate(LOperand* op, const Representation& r) const {
67     return Immediate(ToRepresentation(LConstantOperand::cast(op), r));
68   }
69   double ToDouble(LConstantOperand* op) const;
70 
71   Handle<Object> ToHandle(LConstantOperand* op) const;
72 
73   // The operand denoting the second word (the one with a higher address) of
74   // a double stack slot.
75   Operand HighOperand(LOperand* op);
76 
77   // Try to generate code for the entire chunk, but it may fail if the
78   // chunk contains constructs we cannot handle. Returns true if the
79   // code generation attempt succeeded.
80   bool GenerateCode();
81 
82   // Finish the code by setting stack height, safepoint, and bailout
83   // information on it.
84   void FinishCode(Handle<Code> code);
85 
86   // Deferred code support.
87   void DoDeferredNumberTagD(LNumberTagD* instr);
88 
89   enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
90   void DoDeferredNumberTagIU(LInstruction* instr,
91                              LOperand* value,
92                              LOperand* temp,
93                              IntegerSignedness signedness);
94 
95   void DoDeferredTaggedToI(LTaggedToI* instr, Label* done);
96   void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
97   void DoDeferredStackCheck(LStackCheck* instr);
98   void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr);
99   void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
100   void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
101   void DoDeferredAllocate(LAllocate* instr);
102   void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
103   void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
104                                    Register object,
105                                    Register index);
106 
107   // Parallel move support.
108   void DoParallelMove(LParallelMove* move);
109   void DoGap(LGap* instr);
110 
111   // Emit frame translation commands for an environment.
112   void WriteTranslation(LEnvironment* environment, Translation* translation);
113 
114   void EnsureRelocSpaceForDeoptimization();
115 
116   // Declare methods that deal with the individual node types.
117 #define DECLARE_DO(type) void Do##type(L##type* node);
LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)118   LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
119 #undef DECLARE_DO
120 
121  private:
122   LanguageMode language_mode() const { return info()->language_mode(); }
123 
scope()124   Scope* scope() const { return scope_; }
125 
double_scratch0()126   XMMRegister double_scratch0() const { return xmm0; }
127 
128   void EmitClassOfTest(Label* if_true,
129                        Label* if_false,
130                        Handle<String> class_name,
131                        Register input,
132                        Register temporary,
133                        Register temporary2);
134 
GetStackSlotCount()135   int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
136 
AddDeferredCode(LDeferredCode * code)137   void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
138 
139   void SaveCallerDoubles();
140   void RestoreCallerDoubles();
141 
142   // Code generation passes.  Returns true if code generation should
143   // continue.
144   void GenerateBodyInstructionPre(LInstruction* instr) override;
145   void GenerateBodyInstructionPost(LInstruction* instr) override;
146   bool GeneratePrologue();
147   bool GenerateDeferredCode();
148   bool GenerateJumpTable();
149   bool GenerateSafepointTable();
150 
151   // Generates the custom OSR entrypoint and sets the osr_pc_offset.
152   void GenerateOsrPrologue();
153 
154   enum SafepointMode {
155     RECORD_SIMPLE_SAFEPOINT,
156     RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
157   };
158 
159   void CallCode(Handle<Code> code,
160                 RelocInfo::Mode mode,
161                 LInstruction* instr);
162 
163   void CallCodeGeneric(Handle<Code> code,
164                        RelocInfo::Mode mode,
165                        LInstruction* instr,
166                        SafepointMode safepoint_mode);
167 
168   void CallRuntime(const Runtime::Function* fun,
169                    int argc,
170                    LInstruction* instr,
171                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
172 
CallRuntime(Runtime::FunctionId id,int argc,LInstruction * instr)173   void CallRuntime(Runtime::FunctionId id,
174                    int argc,
175                    LInstruction* instr) {
176     const Runtime::Function* function = Runtime::FunctionForId(id);
177     CallRuntime(function, argc, instr);
178   }
179 
CallRuntime(Runtime::FunctionId id,LInstruction * instr)180   void CallRuntime(Runtime::FunctionId id, LInstruction* instr) {
181     const Runtime::Function* function = Runtime::FunctionForId(id);
182     CallRuntime(function, function->nargs, instr);
183   }
184 
185   void CallRuntimeFromDeferred(Runtime::FunctionId id,
186                                int argc,
187                                LInstruction* instr,
188                                LOperand* context);
189 
190   void LoadContextFromDeferred(LOperand* context);
191 
192   // Generate a direct call to a known function. Expects the function
193   // to be in edi.
194   void CallKnownFunction(Handle<JSFunction> function,
195                          int formal_parameter_count, int arity,
196                          LInstruction* instr);
197 
198   void RecordSafepointWithLazyDeopt(LInstruction* instr,
199                                     SafepointMode safepoint_mode);
200 
201   void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
202                                             Safepoint::DeoptMode mode);
203   void DeoptimizeIf(Condition cc, LInstruction* instr,
204                     Deoptimizer::DeoptReason deopt_reason,
205                     Deoptimizer::BailoutType bailout_type);
206   void DeoptimizeIf(Condition cc, LInstruction* instr,
207                     Deoptimizer::DeoptReason deopt_reason);
208 
DeoptEveryNTimes()209   bool DeoptEveryNTimes() {
210     return FLAG_deopt_every_n_times != 0 && !info()->IsStub();
211   }
212 
213   void AddToTranslation(LEnvironment* environment,
214                         Translation* translation,
215                         LOperand* op,
216                         bool is_tagged,
217                         bool is_uint32,
218                         int* object_index_pointer,
219                         int* dematerialized_index_pointer);
220 
221   Register ToRegister(int index) const;
222   XMMRegister ToDoubleRegister(int index) const;
223   int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
224   int32_t ToInteger32(LConstantOperand* op) const;
225   ExternalReference ToExternalReference(LConstantOperand* op) const;
226 
227   Operand BuildFastArrayOperand(LOperand* elements_pointer,
228                                 LOperand* key,
229                                 Representation key_representation,
230                                 ElementsKind elements_kind,
231                                 uint32_t base_offset);
232 
233   Operand BuildSeqStringOperand(Register string,
234                                 LOperand* index,
235                                 String::Encoding encoding);
236 
237   void EmitIntegerMathAbs(LMathAbs* instr);
238 
239   // Support for recording safepoint and position information.
240   void RecordSafepoint(LPointerMap* pointers,
241                        Safepoint::Kind kind,
242                        int arguments,
243                        Safepoint::DeoptMode mode);
244   void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
245   void RecordSafepoint(Safepoint::DeoptMode mode);
246   void RecordSafepointWithRegisters(LPointerMap* pointers,
247                                     int arguments,
248                                     Safepoint::DeoptMode mode);
249 
250   void RecordAndWritePosition(int position) override;
251 
252   static Condition TokenToCondition(Token::Value op, bool is_unsigned);
253   void EmitGoto(int block);
254 
255   // EmitBranch expects to be the last instruction of a block.
256   template<class InstrType>
257   void EmitBranch(InstrType instr, Condition cc);
258   template <class InstrType>
259   void EmitTrueBranch(InstrType instr, Condition cc);
260   template <class InstrType>
261   void EmitFalseBranch(InstrType instr, Condition cc);
262   void EmitNumberUntagD(LNumberUntagD* instr, Register input, Register temp,
263                         XMMRegister result, NumberUntagDMode mode);
264 
265   // Emits optimized code for typeof x == "y".  Modifies input register.
266   // Returns the condition on which a final split to
267   // true and false label should be made, to optimize fallthrough.
268   Condition EmitTypeofIs(LTypeofIsAndBranch* instr, Register input);
269 
270   // Emits optimized code for %_IsString(x).  Preserves input register.
271   // Returns the condition on which a final split to
272   // true and false label should be made, to optimize fallthrough.
273   Condition EmitIsString(Register input,
274                          Register temp1,
275                          Label* is_not_string,
276                          SmiCheck check_needed);
277 
278   // Emits optimized code to deep-copy the contents of statically known
279   // object graphs (e.g. object literal boilerplate).
280   void EmitDeepCopy(Handle<JSObject> object,
281                     Register result,
282                     Register source,
283                     int* offset,
284                     AllocationSiteMode mode);
285 
286   void EnsureSpaceForLazyDeopt(int space_needed) override;
287   void DoLoadKeyedExternalArray(LLoadKeyed* instr);
288   void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
289   void DoLoadKeyedFixedArray(LLoadKeyed* instr);
290   void DoStoreKeyedExternalArray(LStoreKeyed* instr);
291   void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
292   void DoStoreKeyedFixedArray(LStoreKeyed* instr);
293 
294   template <class T>
295   void EmitVectorLoadICRegisters(T* instr);
296   template <class T>
297   void EmitVectorStoreICRegisters(T* instr);
298 
299   void EmitReturn(LReturn* instr, bool dynamic_frame_alignment);
300 
301   // Emits code for pushing either a tagged constant, a (non-double)
302   // register, or a stack slot operand.
303   void EmitPushTaggedOperand(LOperand* operand);
304 
305   friend class LGapResolver;
306 
307 #ifdef _MSC_VER
308   // On windows, you may not access the stack more than one page below
309   // the most recently mapped page. To make the allocated area randomly
310   // accessible, we write an arbitrary value to each page in range
311   // esp + offset - page_size .. esp in turn.
312   void MakeSureStackPagesMapped(int offset);
313 #endif
314 
315   ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
316   Scope* const scope_;
317   ZoneList<LDeferredCode*> deferred_;
318   bool dynamic_frame_alignment_;
319   bool support_aligned_spilled_doubles_;
320   bool frame_is_built_;
321 
322   // Builder that keeps track of safepoints in the code. The table
323   // itself is emitted at the end of the generated code.
324   SafepointTableBuilder safepoints_;
325 
326   // Compiler from a set of parallel moves to a sequential list of moves.
327   LGapResolver resolver_;
328 
329   Safepoint::Kind expected_safepoint_kind_;
330 
331   class PushSafepointRegistersScope final BASE_EMBEDDED {
332    public:
PushSafepointRegistersScope(LCodeGen * codegen)333     explicit PushSafepointRegistersScope(LCodeGen* codegen)
334         : codegen_(codegen) {
335       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
336       codegen_->masm_->PushSafepointRegisters();
337       codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
338       DCHECK(codegen_->info()->is_calling());
339     }
340 
~PushSafepointRegistersScope()341     ~PushSafepointRegistersScope() {
342       DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
343       codegen_->masm_->PopSafepointRegisters();
344       codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
345     }
346 
347    private:
348     LCodeGen* codegen_;
349   };
350 
351   friend class LDeferredCode;
352   friend class LEnvironment;
353   friend class SafepointGenerator;
354   DISALLOW_COPY_AND_ASSIGN(LCodeGen);
355 };
356 
357 
358 class LDeferredCode : public ZoneObject {
359  public:
LDeferredCode(LCodeGen * codegen)360   explicit LDeferredCode(LCodeGen* codegen)
361       : codegen_(codegen),
362         external_exit_(NULL),
363         instruction_index_(codegen->current_instruction_) {
364     codegen->AddDeferredCode(this);
365   }
366 
~LDeferredCode()367   virtual ~LDeferredCode() {}
368   virtual void Generate() = 0;
369   virtual LInstruction* instr() = 0;
370 
SetExit(Label * exit)371   void SetExit(Label* exit) { external_exit_ = exit; }
entry()372   Label* entry() { return &entry_; }
exit()373   Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
done()374   Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
instruction_index()375   int instruction_index() const { return instruction_index_; }
376 
377  protected:
codegen()378   LCodeGen* codegen() const { return codegen_; }
masm()379   MacroAssembler* masm() const { return codegen_->masm(); }
380 
381  private:
382   LCodeGen* codegen_;
383   Label entry_;
384   Label exit_;
385   Label* external_exit_;
386   Label done_;
387   int instruction_index_;
388 };
389 
390 }  // namespace internal
391 }  // namespace v8
392 
393 #endif  // V8_CRANKSHAFT_IA32_LITHIUM_CODEGEN_IA32_H_
394