1 // Copyright 2013 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_ARM64_LITHIUM_CODEGEN_ARM64_H_
6 #define V8_ARM64_LITHIUM_CODEGEN_ARM64_H_
8 #include "src/arm64/lithium-arm64.h"
10 #include "src/arm64/lithium-gap-resolver-arm64.h"
11 #include "src/deoptimizer.h"
12 #include "src/lithium-codegen.h"
13 #include "src/safepoint-table.h"
14 #include "src/scopes.h"
15 #include "src/utils.h"
20 // Forward declarations.
22 class SafepointGenerator;
23 class BranchGenerator;
25 class LCodeGen: public LCodeGenBase {
27 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
28 : LCodeGenBase(chunk, assembler, info),
29 deoptimizations_(4, info->zone()),
30 jump_table_(4, info->zone()),
31 deoptimization_literals_(8, info->zone()),
32 inlined_function_count_(0),
33 scope_(info->scope()),
34 translations_(info->zone()),
35 deferred_(8, info->zone()),
37 frame_is_built_(false),
38 safepoints_(info->zone()),
40 expected_safepoint_kind_(Safepoint::kSimple) {
41 PopulateDeoptimizationLiteralsWithInlinedFunctions();
45 Scope* scope() const { return scope_; }
47 int LookupDestination(int block_id) const {
48 return chunk()->LookupDestination(block_id);
51 bool IsNextEmittedBlock(int block_id) const {
52 return LookupDestination(block_id) == GetNextEmittedBlock();
55 bool NeedsEagerFrame() const {
56 return GetStackSlotCount() > 0 ||
57 info()->is_non_deferred_calling() ||
59 info()->requires_frame();
61 bool NeedsDeferredFrame() const {
62 return !NeedsEagerFrame() && info()->is_deferred_calling();
65 LinkRegisterStatus GetLinkRegisterState() const {
66 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
69 // Try to generate code for the entire chunk, but it may fail if the
70 // chunk contains constructs we cannot handle. Returns true if the
71 // code generation attempt succeeded.
74 // Finish the code by setting stack height, safepoint, and bailout
76 void FinishCode(Handle<Code> code);
78 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
79 // Support for converting LOperands to assembler types.
80 Register ToRegister(LOperand* op) const;
81 Register ToRegister32(LOperand* op) const;
82 Operand ToOperand(LOperand* op);
83 Operand ToOperand32(LOperand* op);
84 MemOperand ToMemOperand(LOperand* op) const;
85 Handle<Object> ToHandle(LConstantOperand* op) const;
88 Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
90 int JSShiftAmountFromLConstant(LOperand* constant) {
91 return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
94 // TODO(jbramley): Examine these helpers and check that they make sense.
95 // IsInteger32Constant returns true for smi constants, for example.
96 bool IsInteger32Constant(LConstantOperand* op) const;
97 bool IsSmi(LConstantOperand* op) const;
99 int32_t ToInteger32(LConstantOperand* op) const;
100 Smi* ToSmi(LConstantOperand* op) const;
101 double ToDouble(LConstantOperand* op) const;
102 DoubleRegister ToDoubleRegister(LOperand* op) const;
104 // Declare methods that deal with the individual node types.
105 #define DECLARE_DO(type) void Do##type(L##type* node);
106 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
110 // Return a double scratch register which can be used locally
111 // when generating code for a lithium instruction.
112 DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
114 // Deferred code support.
115 void DoDeferredNumberTagD(LNumberTagD* instr);
116 void DoDeferredStackCheck(LStackCheck* instr);
117 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
118 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
119 void DoDeferredMathAbsTagged(LMathAbsTagged* instr,
121 Label* allocation_entry);
123 void DoDeferredNumberTagU(LInstruction* instr,
127 void DoDeferredTaggedToI(LTaggedToI* instr,
131 void DoDeferredAllocate(LAllocate* instr);
132 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr);
133 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
134 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
139 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
140 void EmitGoto(int block);
141 void DoGap(LGap* instr);
143 // Generic version of EmitBranch. It contains some code to avoid emitting a
144 // branch on the next emitted basic block where we could just fall-through.
145 // You shouldn't use that directly but rather consider one of the helper like
146 // LCodeGen::EmitBranch, LCodeGen::EmitCompareAndBranch...
147 template<class InstrType>
148 void EmitBranchGeneric(InstrType instr,
149 const BranchGenerator& branch);
151 template<class InstrType>
152 void EmitBranch(InstrType instr, Condition condition);
154 template<class InstrType>
155 void EmitCompareAndBranch(InstrType instr,
160 template<class InstrType>
161 void EmitTestAndBranch(InstrType instr,
163 const Register& value,
166 template<class InstrType>
167 void EmitBranchIfNonZeroNumber(InstrType instr,
168 const FPRegister& value,
169 const FPRegister& scratch);
171 template<class InstrType>
172 void EmitBranchIfHeapNumber(InstrType instr,
173 const Register& value);
175 template<class InstrType>
176 void EmitBranchIfRoot(InstrType instr,
177 const Register& value,
178 Heap::RootListIndex index);
180 // Emits optimized code to deep-copy the contents of statically known object
181 // graphs (e.g. object literal boilerplate). Expects a pointer to the
182 // allocated destination object in the result register, and a pointer to the
183 // source object in the source register.
184 void EmitDeepCopy(Handle<JSObject> object,
189 AllocationSiteMode mode);
192 void EmitVectorLoadICRegisters(T* instr);
194 // Emits optimized code for %_IsString(x). Preserves input register.
195 // Returns the condition on which a final split to
196 // true and false label should be made, to optimize fallthrough.
197 Condition EmitIsString(Register input, Register temp1, Label* is_not_string,
198 SmiCheck check_needed);
200 int DefineDeoptimizationLiteral(Handle<Object> literal);
201 void PopulateDeoptimizationData(Handle<Code> code);
202 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
204 MemOperand BuildSeqStringOperand(Register string,
207 String::Encoding encoding);
208 void DeoptimizeBranch(LInstruction* instr,
209 Deoptimizer::DeoptReason deopt_reason,
210 BranchType branch_type, Register reg = NoReg,
212 Deoptimizer::BailoutType* override_bailout_type = NULL);
213 void Deoptimize(LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
214 Deoptimizer::BailoutType* override_bailout_type = NULL);
215 void DeoptimizeIf(Condition cond, LInstruction* instr,
216 Deoptimizer::DeoptReason deopt_reason);
217 void DeoptimizeIfZero(Register rt, LInstruction* instr,
218 Deoptimizer::DeoptReason deopt_reason);
219 void DeoptimizeIfNotZero(Register rt, LInstruction* instr,
220 Deoptimizer::DeoptReason deopt_reason);
221 void DeoptimizeIfNegative(Register rt, LInstruction* instr,
222 Deoptimizer::DeoptReason deopt_reason);
223 void DeoptimizeIfSmi(Register rt, LInstruction* instr,
224 Deoptimizer::DeoptReason deopt_reason);
225 void DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
226 Deoptimizer::DeoptReason deopt_reason);
227 void DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
229 Deoptimizer::DeoptReason deopt_reason);
230 void DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
232 Deoptimizer::DeoptReason deopt_reason);
233 void DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr);
234 void DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
235 Deoptimizer::DeoptReason deopt_reason);
236 void DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
237 Deoptimizer::DeoptReason deopt_reason);
238 void DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
239 Deoptimizer::DeoptReason deopt_reason);
241 MemOperand PrepareKeyedExternalArrayOperand(Register key,
245 bool key_is_constant,
247 ElementsKind elements_kind,
249 MemOperand PrepareKeyedArrayOperand(Register base,
253 ElementsKind elements_kind,
254 Representation representation,
257 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
258 Safepoint::DeoptMode mode);
260 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
262 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
264 // Emit frame translation commands for an environment.
265 void WriteTranslation(LEnvironment* environment, Translation* translation);
267 void AddToTranslation(LEnvironment* environment,
268 Translation* translation,
272 int* object_index_pointer,
273 int* dematerialized_index_pointer);
275 void SaveCallerDoubles();
276 void RestoreCallerDoubles();
278 // Code generation steps. Returns true if code generation should continue.
279 void GenerateBodyInstructionPre(LInstruction* instr) override;
280 bool GeneratePrologue();
281 bool GenerateDeferredCode();
282 bool GenerateJumpTable();
283 bool GenerateSafepointTable();
285 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
286 void GenerateOsrPrologue();
289 RECORD_SIMPLE_SAFEPOINT,
290 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
293 void CallCode(Handle<Code> code,
294 RelocInfo::Mode mode,
295 LInstruction* instr);
297 void CallCodeGeneric(Handle<Code> code,
298 RelocInfo::Mode mode,
300 SafepointMode safepoint_mode);
302 void CallRuntime(const Runtime::Function* function,
305 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
307 void CallRuntime(Runtime::FunctionId id,
309 LInstruction* instr) {
310 const Runtime::Function* function = Runtime::FunctionForId(id);
311 CallRuntime(function, num_arguments, instr);
314 void LoadContextFromDeferred(LOperand* context);
315 void CallRuntimeFromDeferred(Runtime::FunctionId id,
320 // Generate a direct call to a known function. Expects the function
322 void CallKnownFunction(Handle<JSFunction> function,
323 int formal_parameter_count, int arity,
324 LInstruction* instr);
326 // Support for recording safepoint and position information.
327 void RecordAndWritePosition(int position) override;
328 void RecordSafepoint(LPointerMap* pointers,
329 Safepoint::Kind kind,
331 Safepoint::DeoptMode mode);
332 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
333 void RecordSafepoint(Safepoint::DeoptMode mode);
334 void RecordSafepointWithRegisters(LPointerMap* pointers,
336 Safepoint::DeoptMode mode);
337 void RecordSafepointWithLazyDeopt(LInstruction* instr,
338 SafepointMode safepoint_mode);
340 void EnsureSpaceForLazyDeopt(int space_needed) override;
342 ZoneList<LEnvironment*> deoptimizations_;
343 ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
344 ZoneList<Handle<Object> > deoptimization_literals_;
345 int inlined_function_count_;
347 TranslationBuffer translations_;
348 ZoneList<LDeferredCode*> deferred_;
350 bool frame_is_built_;
352 // Builder that keeps track of safepoints in the code. The table itself is
353 // emitted at the end of the generated code.
354 SafepointTableBuilder safepoints_;
356 // Compiler from a set of parallel moves to a sequential list of moves.
357 LGapResolver resolver_;
359 Safepoint::Kind expected_safepoint_kind_;
363 class PushSafepointRegistersScope BASE_EMBEDDED {
365 explicit PushSafepointRegistersScope(LCodeGen* codegen)
366 : codegen_(codegen) {
367 DCHECK(codegen_->info()->is_calling());
368 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
369 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
371 UseScratchRegisterScope temps(codegen_->masm_);
372 // Preserve the value of lr which must be saved on the stack (the call to
373 // the stub will clobber it).
374 Register to_be_pushed_lr =
375 temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
376 codegen_->masm_->Mov(to_be_pushed_lr, lr);
377 StoreRegistersStateStub stub(codegen_->isolate());
378 codegen_->masm_->CallStub(&stub);
381 ~PushSafepointRegistersScope() {
382 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
383 RestoreRegistersStateStub stub(codegen_->isolate());
384 codegen_->masm_->CallStub(&stub);
385 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
392 friend class LDeferredCode;
393 friend class SafepointGenerator;
394 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
398 class LDeferredCode: public ZoneObject {
400 explicit LDeferredCode(LCodeGen* codegen)
402 external_exit_(NULL),
403 instruction_index_(codegen->current_instruction_) {
404 codegen->AddDeferredCode(this);
407 virtual ~LDeferredCode() { }
408 virtual void Generate() = 0;
409 virtual LInstruction* instr() = 0;
411 void SetExit(Label* exit) { external_exit_ = exit; }
412 Label* entry() { return &entry_; }
413 Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
414 int instruction_index() const { return instruction_index_; }
417 LCodeGen* codegen() const { return codegen_; }
418 MacroAssembler* masm() const { return codegen_->masm(); }
424 Label* external_exit_;
425 int instruction_index_;
429 // This is the abstract class used by EmitBranchGeneric.
430 // It is used to emit code for conditional branching. The Emit() function
431 // emits code to branch when the condition holds and EmitInverted() emits
432 // the branch when the inverted condition is verified.
434 // For actual examples of condition see the concrete implementation in
435 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
436 class BranchGenerator BASE_EMBEDDED {
438 explicit BranchGenerator(LCodeGen* codegen)
439 : codegen_(codegen) { }
441 virtual ~BranchGenerator() { }
443 virtual void Emit(Label* label) const = 0;
444 virtual void EmitInverted(Label* label) const = 0;
447 MacroAssembler* masm() const { return codegen_->masm(); }
452 } } // namespace v8::internal
454 #endif // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_