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