blob: 4a700bd66c0f973274d6f1b3f1c61476531adc92 [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_MIPS64_LITHIUM_CODEGEN_MIPS_H_
6#define V8_CRANKSHAFT_MIPS64_LITHIUM_CODEGEN_MIPS_H_
7
8#include "src/ast/scopes.h"
9#include "src/crankshaft/lithium-codegen.h"
10#include "src/crankshaft/mips64/lithium-gap-resolver-mips64.h"
11#include "src/crankshaft/mips64/lithium-mips64.h"
12#include "src/deoptimizer.h"
13#include "src/safepoint-table.h"
14#include "src/utils.h"
15
16namespace v8 {
17namespace internal {
18
19// Forward declarations.
20class LDeferredCode;
21class SafepointGenerator;
22
23class LCodeGen: public LCodeGenBase {
24 public:
25 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
26 : LCodeGenBase(chunk, assembler, info),
27 jump_table_(4, info->zone()),
28 scope_(info->scope()),
29 deferred_(8, info->zone()),
30 frame_is_built_(false),
31 safepoints_(info->zone()),
32 resolver_(this),
33 expected_safepoint_kind_(Safepoint::kSimple) {
34 PopulateDeoptimizationLiteralsWithInlinedFunctions();
35 }
36
37
38 int LookupDestination(int block_id) const {
39 return chunk()->LookupDestination(block_id);
40 }
41
42 bool IsNextEmittedBlock(int block_id) const {
43 return LookupDestination(block_id) == GetNextEmittedBlock();
44 }
45
46 bool NeedsEagerFrame() const {
Ben Murdoch097c5b22016-05-18 11:27:45 +010047 return HasAllocatedStackSlots() || info()->is_non_deferred_calling() ||
48 !info()->IsStub() || info()->requires_frame();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 }
50 bool NeedsDeferredFrame() const {
51 return !NeedsEagerFrame() && info()->is_deferred_calling();
52 }
53
54 RAStatus GetRAState() const {
55 return frame_is_built_ ? kRAHasBeenSaved : kRAHasNotBeenSaved;
56 }
57
58 // Support for converting LOperands to assembler types.
59 // LOperand must be a register.
60 Register ToRegister(LOperand* op) const;
61
62 // LOperand is loaded into scratch, unless already a register.
63 Register EmitLoadRegister(LOperand* op, Register scratch);
64
65 // LOperand must be a double register.
66 DoubleRegister ToDoubleRegister(LOperand* op) const;
67
68 // LOperand is loaded into dbl_scratch, unless already a double register.
69 DoubleRegister EmitLoadDoubleRegister(LOperand* op,
70 FloatRegister flt_scratch,
71 DoubleRegister dbl_scratch);
72 int64_t ToRepresentation_donotuse(LConstantOperand* op,
73 const Representation& r) const;
74 int32_t ToInteger32(LConstantOperand* op) const;
75 Smi* ToSmi(LConstantOperand* op) const;
76 double ToDouble(LConstantOperand* op) const;
77 Operand ToOperand(LOperand* op);
78 MemOperand ToMemOperand(LOperand* op) const;
79 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
80 MemOperand ToHighMemOperand(LOperand* op) const;
81
82 bool IsInteger32(LConstantOperand* op) const;
83 bool IsSmi(LConstantOperand* op) const;
84 Handle<Object> ToHandle(LConstantOperand* op) const;
85
86 // Try to generate code for the entire chunk, but it may fail if the
87 // chunk contains constructs we cannot handle. Returns true if the
88 // code generation attempt succeeded.
89 bool GenerateCode();
90
91 // Finish the code by setting stack height, safepoint, and bailout
92 // information on it.
93 void FinishCode(Handle<Code> code);
94
95 void DoDeferredNumberTagD(LNumberTagD* instr);
96
97 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
98 void DoDeferredNumberTagIU(LInstruction* instr,
99 LOperand* value,
100 LOperand* temp1,
101 LOperand* temp2,
102 IntegerSignedness signedness);
103
104 void DoDeferredTaggedToI(LTaggedToI* instr);
105 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
106 void DoDeferredStackCheck(LStackCheck* instr);
107 void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr);
108 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
109 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
110 void DoDeferredAllocate(LAllocate* instr);
111
112 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
113 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
114 Register result,
115 Register object,
116 Register index);
117
118 // Parallel move support.
119 void DoParallelMove(LParallelMove* move);
120 void DoGap(LGap* instr);
121
122 MemOperand PrepareKeyedOperand(Register key,
123 Register base,
124 bool key_is_constant,
125 int constant_key,
126 int element_size,
127 int shift_size,
128 int base_offset);
129
130 // Emit frame translation commands for an environment.
131 void WriteTranslation(LEnvironment* environment, Translation* translation);
132
133 // Declare methods that deal with the individual node types.
134#define DECLARE_DO(type) void Do##type(L##type* node);
135 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
136#undef DECLARE_DO
137
138 private:
139 LanguageMode language_mode() const { return info()->language_mode(); }
140
141 Scope* scope() const { return scope_; }
142
143 Register scratch0() { return kLithiumScratchReg; }
144 Register scratch1() { return kLithiumScratchReg2; }
145 DoubleRegister double_scratch0() { return kLithiumScratchDouble; }
146
147 LInstruction* GetNextInstruction();
148
149 void EmitClassOfTest(Label* if_true,
150 Label* if_false,
151 Handle<String> class_name,
152 Register input,
153 Register temporary,
154 Register temporary2);
155
Ben Murdoch097c5b22016-05-18 11:27:45 +0100156 bool HasAllocatedStackSlots() const {
157 return chunk()->HasAllocatedStackSlots();
158 }
159 int GetStackSlotCount() const { return chunk()->GetSpillSlotCount(); }
160 int GetTotalFrameSlotCount() const {
161 return chunk()->GetTotalFrameSlotCount();
162 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163
164 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
165
166 void SaveCallerDoubles();
167 void RestoreCallerDoubles();
168
169 // Code generation passes. Returns true if code generation should
170 // continue.
171 void GenerateBodyInstructionPre(LInstruction* instr) override;
172 bool GeneratePrologue();
173 bool GenerateDeferredCode();
174 bool GenerateJumpTable();
175 bool GenerateSafepointTable();
176
177 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
178 void GenerateOsrPrologue();
179
180 enum SafepointMode {
181 RECORD_SIMPLE_SAFEPOINT,
182 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
183 };
184
185 void CallCode(Handle<Code> code,
186 RelocInfo::Mode mode,
187 LInstruction* instr);
188
189 void CallCodeGeneric(Handle<Code> code,
190 RelocInfo::Mode mode,
191 LInstruction* instr,
192 SafepointMode safepoint_mode);
193
194 void CallRuntime(const Runtime::Function* function,
195 int num_arguments,
196 LInstruction* instr,
197 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
198
199 void CallRuntime(Runtime::FunctionId id,
200 int num_arguments,
201 LInstruction* instr) {
202 const Runtime::Function* function = Runtime::FunctionForId(id);
203 CallRuntime(function, num_arguments, instr);
204 }
205
206 void CallRuntime(Runtime::FunctionId id, LInstruction* instr) {
207 const Runtime::Function* function = Runtime::FunctionForId(id);
208 CallRuntime(function, function->nargs, instr);
209 }
210
211 void LoadContextFromDeferred(LOperand* context);
212 void CallRuntimeFromDeferred(Runtime::FunctionId id,
213 int argc,
214 LInstruction* instr,
215 LOperand* context);
216
Ben Murdochda12d292016-06-02 14:46:10 +0100217 void PrepareForTailCall(const ParameterCount& actual, Register scratch1,
218 Register scratch2, Register scratch3);
219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 // Generate a direct call to a known function. Expects the function
221 // to be in a1.
222 void CallKnownFunction(Handle<JSFunction> function,
223 int formal_parameter_count, int arity,
Ben Murdochda12d292016-06-02 14:46:10 +0100224 bool is_tail_call, LInstruction* instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000225
226 void RecordSafepointWithLazyDeopt(LInstruction* instr,
227 SafepointMode safepoint_mode);
228
229 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
230 Safepoint::DeoptMode mode);
231 void DeoptimizeIf(Condition condition, LInstruction* instr,
232 Deoptimizer::DeoptReason deopt_reason,
233 Deoptimizer::BailoutType bailout_type,
234 Register src1 = zero_reg,
235 const Operand& src2 = Operand(zero_reg));
236 void DeoptimizeIf(
237 Condition condition, LInstruction* instr,
238 Deoptimizer::DeoptReason deopt_reason = Deoptimizer::kNoReason,
239 Register src1 = zero_reg, const Operand& src2 = Operand(zero_reg));
240
241 void AddToTranslation(LEnvironment* environment,
242 Translation* translation,
243 LOperand* op,
244 bool is_tagged,
245 bool is_uint32,
246 int* object_index_pointer,
247 int* dematerialized_index_pointer);
248
249 Register ToRegister(int index) const;
250 DoubleRegister ToDoubleRegister(int index) const;
251
252 MemOperand BuildSeqStringOperand(Register string,
253 LOperand* index,
254 String::Encoding encoding);
255
256 void EmitIntegerMathAbs(LMathAbs* instr);
257 void EmitSmiMathAbs(LMathAbs* instr);
258
259 // Support for recording safepoint and position information.
260 void RecordSafepoint(LPointerMap* pointers,
261 Safepoint::Kind kind,
262 int arguments,
263 Safepoint::DeoptMode mode);
264 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
265 void RecordSafepoint(Safepoint::DeoptMode mode);
266 void RecordSafepointWithRegisters(LPointerMap* pointers,
267 int arguments,
268 Safepoint::DeoptMode mode);
269
270 void RecordAndWritePosition(int position) override;
271
272 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
273 void EmitGoto(int block);
274
275 // EmitBranch expects to be the last instruction of a block.
276 template<class InstrType>
277 void EmitBranch(InstrType instr,
278 Condition condition,
279 Register src1,
280 const Operand& src2);
281 template<class InstrType>
282 void EmitBranchF(InstrType instr,
283 Condition condition,
284 FPURegister src1,
285 FPURegister src2);
286 template <class InstrType>
287 void EmitTrueBranch(InstrType instr, Condition condition, Register src1,
288 const Operand& src2);
289 template <class InstrType>
290 void EmitFalseBranch(InstrType instr, Condition condition, Register src1,
291 const Operand& src2);
292 template<class InstrType>
293 void EmitFalseBranchF(InstrType instr,
294 Condition condition,
295 FPURegister src1,
296 FPURegister src2);
297 void EmitCmpI(LOperand* left, LOperand* right);
298 void EmitNumberUntagD(LNumberUntagD* instr, Register input,
299 DoubleRegister result, NumberUntagDMode mode);
300
301 // Emits optimized code for typeof x == "y". Modifies input register.
302 // Returns the condition on which a final split to
303 // true and false label should be made, to optimize fallthrough.
304 // Returns two registers in cmp1 and cmp2 that can be used in the
305 // Branch instruction after EmitTypeofIs.
306 Condition EmitTypeofIs(Label* true_label,
307 Label* false_label,
308 Register input,
309 Handle<String> type_name,
310 Register* cmp1,
311 Operand* cmp2);
312
313 // Emits optimized code for %_IsString(x). Preserves input register.
314 // Returns the condition on which a final split to
315 // true and false label should be made, to optimize fallthrough.
316 Condition EmitIsString(Register input,
317 Register temp1,
318 Label* is_not_string,
319 SmiCheck check_needed);
320
321 // Emits optimized code to deep-copy the contents of statically known
322 // object graphs (e.g. object literal boilerplate).
323 void EmitDeepCopy(Handle<JSObject> object,
324 Register result,
325 Register source,
326 int* offset,
327 AllocationSiteMode mode);
328 // Emit optimized code for integer division.
329 // Inputs are signed.
330 // All registers are clobbered.
331 // If 'remainder' is no_reg, it is not computed.
332 void EmitSignedIntegerDivisionByConstant(Register result,
333 Register dividend,
334 int32_t divisor,
335 Register remainder,
336 Register scratch,
337 LEnvironment* environment);
338
339
340 void EnsureSpaceForLazyDeopt(int space_needed) override;
341 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
342 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
343 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
344 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
345 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
346 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
347
348 template <class T>
349 void EmitVectorLoadICRegisters(T* instr);
350 template <class T>
351 void EmitVectorStoreICRegisters(T* instr);
352
353 ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
354 Scope* const scope_;
355 ZoneList<LDeferredCode*> deferred_;
356 bool frame_is_built_;
357
358 // Builder that keeps track of safepoints in the code. The table
359 // itself is emitted at the end of the generated code.
360 SafepointTableBuilder safepoints_;
361
362 // Compiler from a set of parallel moves to a sequential list of moves.
363 LGapResolver resolver_;
364
365 Safepoint::Kind expected_safepoint_kind_;
366
367 class PushSafepointRegistersScope final BASE_EMBEDDED {
368 public:
369 explicit PushSafepointRegistersScope(LCodeGen* codegen)
370 : codegen_(codegen) {
371 DCHECK(codegen_->info()->is_calling());
372 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
373 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
374
375 StoreRegistersStateStub stub(codegen_->isolate());
376 codegen_->masm_->push(ra);
377 codegen_->masm_->CallStub(&stub);
378 }
379
380 ~PushSafepointRegistersScope() {
381 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
382 RestoreRegistersStateStub stub(codegen_->isolate());
383 codegen_->masm_->push(ra);
384 codegen_->masm_->CallStub(&stub);
385 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
386 }
387
388 private:
389 LCodeGen* codegen_;
390 };
391
392 friend class LDeferredCode;
393 friend class LEnvironment;
394 friend class SafepointGenerator;
395 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
396};
397
398
399class LDeferredCode : public ZoneObject {
400 public:
401 explicit LDeferredCode(LCodeGen* codegen)
402 : codegen_(codegen),
403 external_exit_(NULL),
404 instruction_index_(codegen->current_instruction_) {
405 codegen->AddDeferredCode(this);
406 }
407
408 virtual ~LDeferredCode() {}
409 virtual void Generate() = 0;
410 virtual LInstruction* instr() = 0;
411
412 void SetExit(Label* exit) { external_exit_ = exit; }
413 Label* entry() { return &entry_; }
414 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
415 int instruction_index() const { return instruction_index_; }
416
417 protected:
418 LCodeGen* codegen() const { return codegen_; }
419 MacroAssembler* masm() const { return codegen_->masm(); }
420
421 private:
422 LCodeGen* codegen_;
423 Label entry_;
424 Label exit_;
425 Label* external_exit_;
426 int instruction_index_;
427};
428
429} // namespace internal
430} // namespace v8
431
432#endif // V8_CRANKSHAFT_MIPS64_LITHIUM_CODEGEN_MIPS_H_