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