blob: 6f8f06e345826a20508ecc29adf2375622b81326 [file] [log] [blame]
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// 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
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000033#include "checks.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "deoptimizer.h"
35#include "safepoint-table.h"
36#include "scopes.h"
ager@chromium.org0ee099b2011-01-25 14:06:47 +000037#include "x64/lithium-gap-resolver-x64.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038
39namespace v8 {
40namespace internal {
41
42// Forward declarations.
43class LDeferredCode;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000044class SafepointGenerator;
45
kasperl@chromium.orga5551262010-12-07 12:49:48 +000046class LCodeGen BASE_EMBEDDED {
47 public:
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000048 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 deoptimization_literals_(8),
57 inlined_function_count_(0),
58 scope_(chunk->graph()->info()->scope()),
59 status_(UNUSED),
60 deferred_(8),
ager@chromium.org0ee099b2011-01-25 14:06:47 +000061 osr_pc_offset_(-1),
62 resolver_(this) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000063 PopulateDeoptimizationLiteralsWithInlinedFunctions();
64 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +000065
ager@chromium.org0ee099b2011-01-25 14:06:47 +000066 // Simple accessors.
67 MacroAssembler* masm() const { return masm_; }
68
69 // Support for converting LOperands to assembler types.
70 Register ToRegister(LOperand* op) const;
71 XMMRegister ToDoubleRegister(LOperand* op) const;
72 bool IsInteger32Constant(LConstantOperand* op) const;
73 int ToInteger32(LConstantOperand* op) const;
74 bool IsTaggedConstant(LConstantOperand* op) const;
75 Handle<Object> ToHandle(LConstantOperand* op) const;
76 Operand ToOperand(LOperand* op) const;
77
78
kasperl@chromium.orga5551262010-12-07 12:49:48 +000079 // Try to generate code for the entire chunk, but it may fail if the
80 // chunk contains constructs we cannot handle. Returns true if the
81 // code generation attempt succeeded.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000082 bool GenerateCode();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000083
84 // Finish the code by setting stack height, safepoint, and bailout
85 // information on it.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000086 void FinishCode(Handle<Code> code);
87
88 // Deferred code support.
89 void DoDeferredNumberTagD(LNumberTagD* instr);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000090 void DoDeferredTaggedToI(LTaggedToI* instr);
91 void DoDeferredMathAbsTaggedHeapNumber(LUnaryMathOperation* instr);
92 void DoDeferredStackCheck(LGoto* instr);
93 void DoDeferredLInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
94 Label* map_check);
95
96 // Parallel move support.
97 void DoParallelMove(LParallelMove* move);
98
99 // Emit frame translation commands for an environment.
100 void WriteTranslation(LEnvironment* environment, Translation* translation);
101
102 // Declare methods that deal with the individual node types.
103#define DECLARE_DO(type) void Do##type(L##type* node);
104 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
105#undef DECLARE_DO
106
107 private:
108 enum Status {
109 UNUSED,
110 GENERATING,
111 DONE,
112 ABORTED
113 };
114
115 bool is_unused() const { return status_ == UNUSED; }
116 bool is_generating() const { return status_ == GENERATING; }
117 bool is_done() const { return status_ == DONE; }
118 bool is_aborted() const { return status_ == ABORTED; }
119
120 LChunk* chunk() const { return chunk_; }
121 Scope* scope() const { return scope_; }
122 HGraph* graph() const { return chunk_->graph(); }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000123
124 int GetNextEmittedBlock(int block);
125 LInstruction* GetNextInstruction();
126
127 void EmitClassOfTest(Label* if_true,
128 Label* if_false,
129 Handle<String> class_name,
130 Register input,
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000131 Register temporary);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000132
133 int StackSlotCount() const { return chunk()->spill_slot_count(); }
134 int ParameterCount() const { return scope()->num_parameters(); }
135
136 void Abort(const char* format, ...);
137 void Comment(const char* format, ...);
138
139 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code); }
140
141 // Code generation passes. Returns true if code generation should
142 // continue.
143 bool GeneratePrologue();
144 bool GenerateBody();
145 bool GenerateDeferredCode();
146 bool GenerateSafepointTable();
147
148 void CallCode(Handle<Code> code,
149 RelocInfo::Mode mode,
150 LInstruction* instr);
151 void CallRuntime(Runtime::Function* function,
152 int num_arguments,
153 LInstruction* instr);
154 void CallRuntime(Runtime::FunctionId id,
155 int num_arguments,
156 LInstruction* instr) {
157 Runtime::Function* function = Runtime::FunctionForId(id);
158 CallRuntime(function, num_arguments, instr);
159 }
160
161 // Generate a direct call to a known function. Expects the function
162 // to be in edi.
163 void CallKnownFunction(Handle<JSFunction> function,
164 int arity,
165 LInstruction* instr);
166
167 void LoadHeapObject(Register result, Handle<HeapObject> object);
168
169 void RegisterLazyDeoptimization(LInstruction* instr);
170 void RegisterEnvironmentForDeoptimization(LEnvironment* environment);
171 void DeoptimizeIf(Condition cc, LEnvironment* environment);
172
173 void AddToTranslation(Translation* translation,
174 LOperand* op,
175 bool is_tagged);
176 void PopulateDeoptimizationData(Handle<Code> code);
177 int DefineDeoptimizationLiteral(Handle<Object> literal);
178
179 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
180
181 Register ToRegister(int index) const;
182 XMMRegister ToDoubleRegister(int index) const;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000183
184 // Specific math operations - used from DoUnaryMathOperation.
185 void DoMathAbs(LUnaryMathOperation* instr);
186 void DoMathFloor(LUnaryMathOperation* instr);
187 void DoMathRound(LUnaryMathOperation* instr);
188 void DoMathSqrt(LUnaryMathOperation* instr);
189 void DoMathPowHalf(LUnaryMathOperation* instr);
190 void DoMathLog(LUnaryMathOperation* instr);
191 void DoMathCos(LUnaryMathOperation* instr);
192 void DoMathSin(LUnaryMathOperation* instr);
193
194 // Support for recording safepoint and position information.
ager@chromium.org378b34e2011-01-28 08:04:38 +0000195 void RecordSafepoint(LPointerMap* pointers,
196 Safepoint::Kind kind,
197 int arguments,
198 int deoptimization_index);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000199 void RecordSafepoint(LPointerMap* pointers, int deoptimization_index);
200 void RecordSafepointWithRegisters(LPointerMap* pointers,
201 int arguments,
202 int deoptimization_index);
203 void RecordPosition(int position);
204
205 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
206 void EmitGoto(int block, LDeferredCode* deferred_stack_check = NULL);
207 void EmitBranch(int left_block, int right_block, Condition cc);
208 void EmitCmpI(LOperand* left, LOperand* right);
209 void EmitNumberUntagD(Register input, XMMRegister result, LEnvironment* env);
210
211 // Emits optimized code for typeof x == "y". Modifies input register.
212 // Returns the condition on which a final split to
213 // true and false label should be made, to optimize fallthrough.
214 Condition EmitTypeofIs(Label* true_label, Label* false_label,
215 Register input, Handle<String> type_name);
216
217 // Emits optimized code for %_IsObject(x). Preserves input register.
218 // Returns the condition on which a final split to
219 // true and false label should be made, to optimize fallthrough.
220 Condition EmitIsObject(Register input,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000221 Label* is_not_object,
222 Label* is_object);
223
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000224 // Emits optimized code for %_IsConstructCall().
225 // Caller should branch on equal condition.
226 void EmitIsConstructCall(Register temp);
227
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000228 LChunk* const chunk_;
229 MacroAssembler* const masm_;
230 CompilationInfo* const info_;
231
232 int current_block_;
233 int current_instruction_;
234 const ZoneList<LInstruction*>* instructions_;
235 ZoneList<LEnvironment*> deoptimizations_;
236 ZoneList<Handle<Object> > deoptimization_literals_;
237 int inlined_function_count_;
238 Scope* const scope_;
239 Status status_;
240 TranslationBuffer translations_;
241 ZoneList<LDeferredCode*> deferred_;
242 int osr_pc_offset_;
243
244 // Builder that keeps track of safepoints in the code. The table
245 // itself is emitted at the end of the generated code.
246 SafepointTableBuilder safepoints_;
247
248 // Compiler from a set of parallel moves to a sequential list of moves.
249 LGapResolver resolver_;
250
251 friend class LDeferredCode;
252 friend class LEnvironment;
253 friend class SafepointGenerator;
254 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
255};
256
257
258class LDeferredCode: public ZoneObject {
259 public:
260 explicit LDeferredCode(LCodeGen* codegen)
261 : codegen_(codegen), external_exit_(NULL) {
262 codegen->AddDeferredCode(this);
263 }
264
265 virtual ~LDeferredCode() { }
266 virtual void Generate() = 0;
267
268 void SetExit(Label *exit) { external_exit_ = exit; }
269 Label* entry() { return &entry_; }
270 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
271
272 protected:
273 LCodeGen* codegen() const { return codegen_; }
274 MacroAssembler* masm() const { return codegen_->masm(); }
275
276 private:
277 LCodeGen* codegen_;
278 Label entry_;
279 Label exit_;
280 Label* external_exit_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000281};
282
283} } // namespace v8::internal
284
285#endif // V8_X64_LITHIUM_CODEGEN_X64_H_