lithium-codegen-x64.h revision 2b4ba1175df6a5a6b9b5cda034189197bf6565ec
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_X64_LITHIUM_CODEGEN_X64_H_
29#define V8_X64_LITHIUM_CODEGEN_X64_H_
30
31#include "x64/lithium-x64.h"
32
33#include "checks.h"
34#include "deoptimizer.h"
35#include "safepoint-table.h"
36#include "scopes.h"
37#include "x64/lithium-gap-resolver-x64.h"
38
39namespace v8 {
40namespace internal {
41
42// Forward declarations.
43class LDeferredCode;
44class SafepointGenerator;
45
46class LCodeGen BASE_EMBEDDED {
47 public:
48  LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
49      : chunk_(chunk),
50        masm_(assembler),
51        info_(info),
52        current_block_(-1),
53        current_instruction_(-1),
54        instructions_(chunk->instructions()),
55        deoptimizations_(4),
56        jump_table_(4),
57        deoptimization_literals_(8),
58        inlined_function_count_(0),
59        scope_(info->scope()),
60        status_(UNUSED),
61        deferred_(8),
62        osr_pc_offset_(-1),
63        last_lazy_deopt_pc_(0),
64        resolver_(this),
65        expected_safepoint_kind_(Safepoint::kSimple) {
66    PopulateDeoptimizationLiteralsWithInlinedFunctions();
67  }
68
69  // Simple accessors.
70  MacroAssembler* masm() const { return masm_; }
71  CompilationInfo* info() const { return info_; }
72  Isolate* isolate() const { return info_->isolate(); }
73  Factory* factory() const { return isolate()->factory(); }
74  Heap* heap() const { return isolate()->heap(); }
75
76  // Support for converting LOperands to assembler types.
77  Register ToRegister(LOperand* op) const;
78  XMMRegister ToDoubleRegister(LOperand* op) const;
79  bool IsInteger32Constant(LConstantOperand* op) const;
80  int ToInteger32(LConstantOperand* op) const;
81  bool IsTaggedConstant(LConstantOperand* op) const;
82  Handle<Object> ToHandle(LConstantOperand* op) const;
83  Operand ToOperand(LOperand* op) const;
84
85  // Try to generate code for the entire chunk, but it may fail if the
86  // chunk contains constructs we cannot handle. Returns true if the
87  // code generation attempt succeeded.
88  bool GenerateCode();
89
90  // Finish the code by setting stack height, safepoint, and bailout
91  // information on it.
92  void FinishCode(Handle<Code> code);
93
94  // Deferred code support.
95  void DoDeferredNumberTagD(LNumberTagD* instr);
96  void DoDeferredTaggedToI(LTaggedToI* instr);
97  void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
98  void DoDeferredStackCheck(LStackCheck* instr);
99  void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
100  void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
101  void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
102                                       Label* map_check);
103
104  // Parallel move support.
105  void DoParallelMove(LParallelMove* move);
106  void DoGap(LGap* instr);
107
108  // Emit frame translation commands for an environment.
109  void WriteTranslation(LEnvironment* environment, Translation* translation);
110
111  // Declare methods that deal with the individual node types.
112#define DECLARE_DO(type) void Do##type(L##type* node);
113  LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
114#undef DECLARE_DO
115
116 private:
117  enum Status {
118    UNUSED,
119    GENERATING,
120    DONE,
121    ABORTED
122  };
123
124  bool is_unused() const { return status_ == UNUSED; }
125  bool is_generating() const { return status_ == GENERATING; }
126  bool is_done() const { return status_ == DONE; }
127  bool is_aborted() const { return status_ == ABORTED; }
128
129  int strict_mode_flag() const {
130    return info()->is_strict_mode() ? kStrictMode : kNonStrictMode;
131  }
132
133  LChunk* chunk() const { return chunk_; }
134  Scope* scope() const { return scope_; }
135  HGraph* graph() const { return chunk_->graph(); }
136
137  int GetNextEmittedBlock(int block);
138
139  void EmitClassOfTest(Label* if_true,
140                       Label* if_false,
141                       Handle<String> class_name,
142                       Register input,
143                       Register temporary);
144
145  int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
146  int GetParameterCount() const { return scope()->num_parameters(); }
147
148  void Abort(const char* format, ...);
149  void Comment(const char* format, ...);
150
151  void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); }
152
153  // Code generation passes.  Returns true if code generation should
154  // continue.
155  bool GeneratePrologue();
156  bool GenerateBody();
157  bool GenerateDeferredCode();
158  bool GenerateJumpTable();
159  bool GenerateSafepointTable();
160
161  enum SafepointMode {
162    RECORD_SIMPLE_SAFEPOINT,
163    RECORD_SAFEPOINT_WITH_REGISTERS
164  };
165
166  void CallCodeGeneric(Handle<Code> code,
167                       RelocInfo::Mode mode,
168                       LInstruction* instr,
169                       SafepointMode safepoint_mode,
170                       int argc);
171
172
173  void CallCode(Handle<Code> code,
174                RelocInfo::Mode mode,
175                LInstruction* instr);
176
177  void CallRuntime(const Runtime::Function* function,
178                   int num_arguments,
179                   LInstruction* instr);
180
181  void CallRuntime(Runtime::FunctionId id,
182                   int num_arguments,
183                   LInstruction* instr) {
184    const Runtime::Function* function = Runtime::FunctionForId(id);
185    CallRuntime(function, num_arguments, instr);
186  }
187
188  void CallRuntimeFromDeferred(Runtime::FunctionId id,
189                               int argc,
190                               LInstruction* instr);
191
192
193  // Generate a direct call to a known function.  Expects the function
194  // to be in edi.
195  void CallKnownFunction(Handle<JSFunction> function,
196                         int arity,
197                         LInstruction* instr,
198                         CallKind call_kind);
199
200  void LoadHeapObject(Register result, Handle<HeapObject> object);
201
202  void RecordSafepointWithLazyDeopt(LInstruction* instr,
203                                    SafepointMode safepoint_mode,
204                                    int argc);
205  void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
206                                            Safepoint::DeoptMode mode);
207  void DeoptimizeIf(Condition cc, LEnvironment* environment);
208
209  void AddToTranslation(Translation* translation,
210                        LOperand* op,
211                        bool is_tagged);
212  void PopulateDeoptimizationData(Handle<Code> code);
213  int DefineDeoptimizationLiteral(Handle<Object> literal);
214
215  void PopulateDeoptimizationLiteralsWithInlinedFunctions();
216
217  Register ToRegister(int index) const;
218  XMMRegister ToDoubleRegister(int index) const;
219  Operand BuildFastArrayOperand(
220      LOperand* elements_pointer,
221      LOperand* key,
222      ElementsKind elements_kind,
223      uint32_t offset);
224
225  // Specific math operations - used from DoUnaryMathOperation.
226  void EmitIntegerMathAbs(LUnaryMathOperation* instr);
227  void DoMathAbs(LUnaryMathOperation* instr);
228  void DoMathFloor(LUnaryMathOperation* instr);
229  void DoMathRound(LUnaryMathOperation* instr);
230  void DoMathSqrt(LUnaryMathOperation* instr);
231  void DoMathPowHalf(LUnaryMathOperation* instr);
232  void DoMathLog(LUnaryMathOperation* instr);
233  void DoMathCos(LUnaryMathOperation* instr);
234  void DoMathSin(LUnaryMathOperation* instr);
235
236  // Support for recording safepoint and position information.
237  void RecordSafepoint(LPointerMap* pointers,
238                       Safepoint::Kind kind,
239                       int arguments,
240                       Safepoint::DeoptMode mode);
241  void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
242  void RecordSafepoint(Safepoint::DeoptMode mode);
243  void RecordSafepointWithRegisters(LPointerMap* pointers,
244                                    int arguments,
245                                    Safepoint::DeoptMode mode);
246  void RecordPosition(int position);
247
248  static Condition TokenToCondition(Token::Value op, bool is_unsigned);
249  void EmitGoto(int block);
250  void EmitBranch(int left_block, int right_block, Condition cc);
251  void EmitCmpI(LOperand* left, LOperand* right);
252  void EmitNumberUntagD(Register input,
253                        XMMRegister result,
254                        bool deoptimize_on_undefined,
255                        LEnvironment* env);
256
257  // Emits optimized code for typeof x == "y".  Modifies input register.
258  // Returns the condition on which a final split to
259  // true and false label should be made, to optimize fallthrough.
260  Condition EmitTypeofIs(Label* true_label, Label* false_label,
261                         Register input, Handle<String> type_name);
262
263  // Emits optimized code for %_IsObject(x).  Preserves input register.
264  // Returns the condition on which a final split to
265  // true and false label should be made, to optimize fallthrough.
266  Condition EmitIsObject(Register input,
267                         Label* is_not_object,
268                         Label* is_object);
269
270  // Emits optimized code for %_IsConstructCall().
271  // Caller should branch on equal condition.
272  void EmitIsConstructCall(Register temp);
273
274  void EmitLoadFieldOrConstantFunction(Register result,
275                                       Register object,
276                                       Handle<Map> type,
277                                       Handle<String> name);
278
279  // Emits code for pushing either a tagged constant, a (non-double)
280  // register, or a stack slot operand.
281  void EmitPushTaggedOperand(LOperand* operand);
282
283  struct JumpTableEntry {
284    explicit inline JumpTableEntry(Address entry)
285        : label(),
286          address(entry) { }
287    Label label;
288    Address address;
289  };
290
291  void EnsureSpaceForLazyDeopt(int space_needed);
292
293  LChunk* const chunk_;
294  MacroAssembler* const masm_;
295  CompilationInfo* const info_;
296
297  int current_block_;
298  int current_instruction_;
299  const ZoneList<LInstruction*>* instructions_;
300  ZoneList<LEnvironment*> deoptimizations_;
301  ZoneList<JumpTableEntry> jump_table_;
302  ZoneList<Handle<Object> > deoptimization_literals_;
303  int inlined_function_count_;
304  Scope* const scope_;
305  Status status_;
306  TranslationBuffer translations_;
307  ZoneList<LDeferredCode*> deferred_;
308  int osr_pc_offset_;
309  int last_lazy_deopt_pc_;
310
311  // Builder that keeps track of safepoints in the code. The table
312  // itself is emitted at the end of the generated code.
313  SafepointTableBuilder safepoints_;
314
315  // Compiler from a set of parallel moves to a sequential list of moves.
316  LGapResolver resolver_;
317
318  Safepoint::Kind expected_safepoint_kind_;
319
320  class PushSafepointRegistersScope BASE_EMBEDDED {
321   public:
322    explicit PushSafepointRegistersScope(LCodeGen* codegen)
323        : codegen_(codegen) {
324      ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
325      codegen_->masm_->PushSafepointRegisters();
326      codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
327    }
328
329    ~PushSafepointRegistersScope() {
330      ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
331      codegen_->masm_->PopSafepointRegisters();
332      codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
333    }
334
335   private:
336    LCodeGen* codegen_;
337  };
338
339  friend class LDeferredCode;
340  friend class LEnvironment;
341  friend class SafepointGenerator;
342  DISALLOW_COPY_AND_ASSIGN(LCodeGen);
343};
344
345
346class LDeferredCode: public ZoneObject {
347 public:
348  explicit LDeferredCode(LCodeGen* codegen)
349      : codegen_(codegen), external_exit_(NULL) {
350    codegen->AddDeferredCode(this);
351  }
352
353  virtual ~LDeferredCode() { }
354  virtual void Generate() = 0;
355
356  void SetExit(Label *exit) { external_exit_ = exit; }
357  Label* entry() { return &entry_; }
358  Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
359
360 protected:
361  LCodeGen* codegen() const { return codegen_; }
362  MacroAssembler* masm() const { return codegen_->masm(); }
363
364 private:
365  LCodeGen* codegen_;
366  Label entry_;
367  Label exit_;
368  Label* external_exit_;
369};
370
371} }  // namespace v8::internal
372
373#endif  // V8_X64_LITHIUM_CODEGEN_X64_H_
374