1 // 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.
5 #ifndef V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
6 #define V8_MIPS_LITHIUM_CODEGEN_MIPS_H_
8 #include "src/deoptimizer.h"
9 #include "src/lithium-codegen.h"
10 #include "src/mips64/lithium-gap-resolver-mips64.h"
11 #include "src/mips64/lithium-mips64.h"
12 #include "src/safepoint-table.h"
13 #include "src/scopes.h"
14 #include "src/utils.h"
19 // Forward declarations.
21 class SafepointGenerator;
23 class LCodeGen: public LCodeGenBase {
25 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
26 : LCodeGenBase(chunk, assembler, info),
27 deoptimizations_(4, info->zone()),
28 deopt_jump_table_(4, info->zone()),
29 deoptimization_literals_(8, info->zone()),
30 inlined_function_count_(0),
31 scope_(info->scope()),
32 translations_(info->zone()),
33 deferred_(8, info->zone()),
35 frame_is_built_(false),
36 safepoints_(info->zone()),
38 expected_safepoint_kind_(Safepoint::kSimple) {
39 PopulateDeoptimizationLiteralsWithInlinedFunctions();
43 int LookupDestination(int block_id) const {
44 return chunk()->LookupDestination(block_id);
47 bool IsNextEmittedBlock(int block_id) const {
48 return LookupDestination(block_id) == GetNextEmittedBlock();
51 bool NeedsEagerFrame() const {
52 return GetStackSlotCount() > 0 ||
53 info()->is_non_deferred_calling() ||
55 info()->requires_frame();
57 bool NeedsDeferredFrame() const {
58 return !NeedsEagerFrame() && info()->is_deferred_calling();
61 RAStatus GetRAState() const {
62 return frame_is_built_ ? kRAHasBeenSaved : kRAHasNotBeenSaved;
65 // Support for converting LOperands to assembler types.
66 // LOperand must be a register.
67 Register ToRegister(LOperand* op) const;
69 // LOperand is loaded into scratch, unless already a register.
70 Register EmitLoadRegister(LOperand* op, Register scratch);
72 // LOperand must be a double register.
73 DoubleRegister ToDoubleRegister(LOperand* op) const;
75 // LOperand is loaded into dbl_scratch, unless already a double register.
76 DoubleRegister EmitLoadDoubleRegister(LOperand* op,
77 FloatRegister flt_scratch,
78 DoubleRegister dbl_scratch);
79 int32_t ToRepresentation_donotuse(LConstantOperand* op,
80 const Representation& r) const;
81 int32_t ToInteger32(LConstantOperand* op) const;
82 Smi* ToSmi(LConstantOperand* op) const;
83 double ToDouble(LConstantOperand* op) const;
84 Operand ToOperand(LOperand* op);
85 MemOperand ToMemOperand(LOperand* op) const;
86 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
87 MemOperand ToHighMemOperand(LOperand* op) const;
89 bool IsInteger32(LConstantOperand* op) const;
90 bool IsSmi(LConstantOperand* op) const;
91 Handle<Object> ToHandle(LConstantOperand* op) const;
93 // Try to generate code for the entire chunk, but it may fail if the
94 // chunk contains constructs we cannot handle. Returns true if the
95 // code generation attempt succeeded.
98 // Finish the code by setting stack height, safepoint, and bailout
100 void FinishCode(Handle<Code> code);
102 void DoDeferredNumberTagD(LNumberTagD* instr);
104 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
105 void DoDeferredNumberTagIU(LInstruction* instr,
109 IntegerSignedness signedness);
111 void DoDeferredTaggedToI(LTaggedToI* instr);
112 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
113 void DoDeferredStackCheck(LStackCheck* instr);
114 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
115 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
116 void DoDeferredAllocate(LAllocate* instr);
117 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
120 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
121 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
126 // Parallel move support.
127 void DoParallelMove(LParallelMove* move);
128 void DoGap(LGap* instr);
130 MemOperand PrepareKeyedOperand(Register key,
132 bool key_is_constant,
138 // Emit frame translation commands for an environment.
139 void WriteTranslation(LEnvironment* environment, Translation* translation);
141 // Declare methods that deal with the individual node types.
142 #define DECLARE_DO(type) void Do##type(L##type* node);
143 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
147 StrictMode strict_mode() const { return info()->strict_mode(); }
149 Scope* scope() const { return scope_; }
151 Register scratch0() { return kLithiumScratchReg; }
152 Register scratch1() { return kLithiumScratchReg2; }
153 DoubleRegister double_scratch0() { return kLithiumScratchDouble; }
155 LInstruction* GetNextInstruction();
157 void EmitClassOfTest(Label* if_true,
159 Handle<String> class_name,
162 Register temporary2);
164 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
166 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
168 void SaveCallerDoubles();
169 void RestoreCallerDoubles();
171 // Code generation passes. Returns true if code generation should
173 void GenerateBodyInstructionPre(LInstruction* instr) V8_OVERRIDE;
174 bool GeneratePrologue();
175 bool GenerateDeferredCode();
176 bool GenerateDeoptJumpTable();
177 bool GenerateSafepointTable();
179 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
180 void GenerateOsrPrologue();
183 RECORD_SIMPLE_SAFEPOINT,
184 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
187 void CallCode(Handle<Code> code,
188 RelocInfo::Mode mode,
189 LInstruction* instr);
191 void CallCodeGeneric(Handle<Code> code,
192 RelocInfo::Mode mode,
194 SafepointMode safepoint_mode);
196 void CallRuntime(const Runtime::Function* function,
199 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
201 void CallRuntime(Runtime::FunctionId id,
203 LInstruction* instr) {
204 const Runtime::Function* function = Runtime::FunctionForId(id);
205 CallRuntime(function, num_arguments, instr);
208 void LoadContextFromDeferred(LOperand* context);
209 void CallRuntimeFromDeferred(Runtime::FunctionId id,
219 // Generate a direct call to a known function. Expects the function
221 void CallKnownFunction(Handle<JSFunction> function,
222 int formal_parameter_count,
227 void RecordSafepointWithLazyDeopt(LInstruction* instr,
228 SafepointMode safepoint_mode);
230 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
231 Safepoint::DeoptMode mode);
232 void DeoptimizeIf(Condition condition,
233 LEnvironment* environment,
234 Deoptimizer::BailoutType bailout_type,
235 Register src1 = zero_reg,
236 const Operand& src2 = Operand(zero_reg));
237 void DeoptimizeIf(Condition condition,
238 LEnvironment* environment,
239 Register src1 = zero_reg,
240 const Operand& src2 = Operand(zero_reg));
242 void AddToTranslation(LEnvironment* environment,
243 Translation* translation,
247 int* object_index_pointer,
248 int* dematerialized_index_pointer);
249 void PopulateDeoptimizationData(Handle<Code> code);
250 int DefineDeoptimizationLiteral(Handle<Object> literal);
252 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
254 Register ToRegister(int index) const;
255 DoubleRegister ToDoubleRegister(int index) const;
257 MemOperand BuildSeqStringOperand(Register string,
259 String::Encoding encoding);
261 void EmitIntegerMathAbs(LMathAbs* instr);
263 // Support for recording safepoint and position information.
264 void RecordSafepoint(LPointerMap* pointers,
265 Safepoint::Kind kind,
267 Safepoint::DeoptMode mode);
268 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
269 void RecordSafepoint(Safepoint::DeoptMode mode);
270 void RecordSafepointWithRegisters(LPointerMap* pointers,
272 Safepoint::DeoptMode mode);
274 void RecordAndWritePosition(int position) V8_OVERRIDE;
276 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
277 void EmitGoto(int block);
279 // EmitBranch expects to be the last instruction of a block.
280 template<class InstrType>
281 void EmitBranch(InstrType instr,
284 const Operand& src2);
285 template<class InstrType>
286 void EmitBranchF(InstrType instr,
290 template<class InstrType>
291 void EmitFalseBranch(InstrType instr,
294 const Operand& src2);
295 template<class InstrType>
296 void EmitFalseBranchF(InstrType instr,
300 void EmitCmpI(LOperand* left, LOperand* right);
301 void EmitNumberUntagD(Register input,
302 DoubleRegister result,
303 bool allow_undefined_as_nan,
304 bool deoptimize_on_minus_zero,
306 NumberUntagDMode mode);
308 // Emits optimized code for typeof x == "y". Modifies input register.
309 // Returns the condition on which a final split to
310 // true and false label should be made, to optimize fallthrough.
311 // Returns two registers in cmp1 and cmp2 that can be used in the
312 // Branch instruction after EmitTypeofIs.
313 Condition EmitTypeofIs(Label* true_label,
316 Handle<String> type_name,
320 // Emits optimized code for %_IsObject(x). Preserves input register.
321 // Returns the condition on which a final split to
322 // true and false label should be made, to optimize fallthrough.
323 Condition EmitIsObject(Register input,
326 Label* is_not_object,
329 // Emits optimized code for %_IsString(x). Preserves input register.
330 // Returns the condition on which a final split to
331 // true and false label should be made, to optimize fallthrough.
332 Condition EmitIsString(Register input,
334 Label* is_not_string,
335 SmiCheck check_needed);
337 // Emits optimized code for %_IsConstructCall().
338 // Caller should branch on equal condition.
339 void EmitIsConstructCall(Register temp1, Register temp2);
341 // Emits optimized code to deep-copy the contents of statically known
342 // object graphs (e.g. object literal boilerplate).
343 void EmitDeepCopy(Handle<JSObject> object,
347 AllocationSiteMode mode);
348 // Emit optimized code for integer division.
349 // Inputs are signed.
350 // All registers are clobbered.
351 // If 'remainder' is no_reg, it is not computed.
352 void EmitSignedIntegerDivisionByConstant(Register result,
357 LEnvironment* environment);
360 void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
361 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
362 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
363 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
364 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
365 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
366 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
368 ZoneList<LEnvironment*> deoptimizations_;
369 ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
370 ZoneList<Handle<Object> > deoptimization_literals_;
371 int inlined_function_count_;
373 TranslationBuffer translations_;
374 ZoneList<LDeferredCode*> deferred_;
376 bool frame_is_built_;
378 // Builder that keeps track of safepoints in the code. The table
379 // itself is emitted at the end of the generated code.
380 SafepointTableBuilder safepoints_;
382 // Compiler from a set of parallel moves to a sequential list of moves.
383 LGapResolver resolver_;
385 Safepoint::Kind expected_safepoint_kind_;
387 class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
389 explicit PushSafepointRegistersScope(LCodeGen* codegen)
390 : codegen_(codegen) {
391 DCHECK(codegen_->info()->is_calling());
392 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
393 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
395 StoreRegistersStateStub stub(codegen_->isolate());
396 codegen_->masm_->push(ra);
397 codegen_->masm_->CallStub(&stub);
400 ~PushSafepointRegistersScope() {
401 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
402 RestoreRegistersStateStub stub(codegen_->isolate());
403 codegen_->masm_->push(ra);
404 codegen_->masm_->CallStub(&stub);
405 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
412 friend class LDeferredCode;
413 friend class LEnvironment;
414 friend class SafepointGenerator;
415 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
419 class LDeferredCode : public ZoneObject {
421 explicit LDeferredCode(LCodeGen* codegen)
423 external_exit_(NULL),
424 instruction_index_(codegen->current_instruction_) {
425 codegen->AddDeferredCode(this);
428 virtual ~LDeferredCode() {}
429 virtual void Generate() = 0;
430 virtual LInstruction* instr() = 0;
432 void SetExit(Label* exit) { external_exit_ = exit; }
433 Label* entry() { return &entry_; }
434 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
435 int instruction_index() const { return instruction_index_; }
438 LCodeGen* codegen() const { return codegen_; }
439 MacroAssembler* masm() const { return codegen_->masm(); }
445 Label* external_exit_;
446 int instruction_index_;
449 } } // namespace v8::internal
451 #endif // V8_MIPS_LITHIUM_CODEGEN_MIPS_H_