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 inlined_function_count_(0),
32 scope_(info->scope()),
33 translations_(info->zone()),
34 deferred_(8, info->zone()),
36 frame_is_built_(false),
37 safepoints_(info->zone()),
39 expected_safepoint_kind_(Safepoint::kSimple) {
40 PopulateDeoptimizationLiteralsWithInlinedFunctions();
44 Scope* scope() const { return scope_; }
46 int LookupDestination(int block_id) const {
47 return chunk()->LookupDestination(block_id);
50 bool IsNextEmittedBlock(int block_id) const {
51 return LookupDestination(block_id) == GetNextEmittedBlock();
54 bool NeedsEagerFrame() const {
55 return GetStackSlotCount() > 0 ||
56 info()->is_non_deferred_calling() ||
58 info()->requires_frame();
60 bool NeedsDeferredFrame() const {
61 return !NeedsEagerFrame() && info()->is_deferred_calling();
64 LinkRegisterStatus GetLinkRegisterState() const {
65 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
68 // Try to generate code for the entire chunk, but it may fail if the
69 // chunk contains constructs we cannot handle. Returns true if the
70 // code generation attempt succeeded.
73 // Finish the code by setting stack height, safepoint, and bailout
75 void FinishCode(Handle<Code> code);
77 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
78 // Support for converting LOperands to assembler types.
79 Register ToRegister(LOperand* op) const;
80 Register ToRegister32(LOperand* op) const;
81 Operand ToOperand(LOperand* op);
82 Operand ToOperand32(LOperand* op);
83 MemOperand ToMemOperand(LOperand* op) const;
84 Handle<Object> ToHandle(LConstantOperand* op) const;
87 Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
89 int JSShiftAmountFromLConstant(LOperand* constant) {
90 return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
93 // TODO(jbramley): Examine these helpers and check that they make sense.
94 // IsInteger32Constant returns true for smi constants, for example.
95 bool IsInteger32Constant(LConstantOperand* op) const;
96 bool IsSmi(LConstantOperand* op) const;
98 int32_t ToInteger32(LConstantOperand* op) const;
99 Smi* ToSmi(LConstantOperand* op) const;
100 double ToDouble(LConstantOperand* op) const;
101 DoubleRegister ToDoubleRegister(LOperand* op) const;
103 // Declare methods that deal with the individual node types.
104 #define DECLARE_DO(type) void Do##type(L##type* node);
105 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
109 // Return a double scratch register which can be used locally
110 // when generating code for a lithium instruction.
111 DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
113 // Deferred code support.
114 void DoDeferredNumberTagD(LNumberTagD* instr);
115 void DoDeferredStackCheck(LStackCheck* instr);
116 void DoDeferredMaybeGrowElements(LMaybeGrowElements* 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 void PopulateDeoptimizationData(Handle<Code> code);
201 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
203 MemOperand BuildSeqStringOperand(Register string,
206 String::Encoding encoding);
207 void DeoptimizeBranch(LInstruction* instr,
208 Deoptimizer::DeoptReason deopt_reason,
209 BranchType branch_type, Register reg = NoReg,
211 Deoptimizer::BailoutType* override_bailout_type = NULL);
212 void Deoptimize(LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
213 Deoptimizer::BailoutType* override_bailout_type = NULL);
214 void DeoptimizeIf(Condition cond, LInstruction* instr,
215 Deoptimizer::DeoptReason deopt_reason);
216 void DeoptimizeIfZero(Register rt, LInstruction* instr,
217 Deoptimizer::DeoptReason deopt_reason);
218 void DeoptimizeIfNotZero(Register rt, LInstruction* instr,
219 Deoptimizer::DeoptReason deopt_reason);
220 void DeoptimizeIfNegative(Register rt, LInstruction* instr,
221 Deoptimizer::DeoptReason deopt_reason);
222 void DeoptimizeIfSmi(Register rt, LInstruction* instr,
223 Deoptimizer::DeoptReason deopt_reason);
224 void DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
225 Deoptimizer::DeoptReason deopt_reason);
226 void DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
228 Deoptimizer::DeoptReason deopt_reason);
229 void DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
231 Deoptimizer::DeoptReason deopt_reason);
232 void DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr);
233 void DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
234 Deoptimizer::DeoptReason deopt_reason);
235 void DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
236 Deoptimizer::DeoptReason deopt_reason);
237 void DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
238 Deoptimizer::DeoptReason deopt_reason);
240 MemOperand PrepareKeyedExternalArrayOperand(Register key,
244 bool key_is_constant,
246 ElementsKind elements_kind,
248 MemOperand PrepareKeyedArrayOperand(Register base,
252 ElementsKind elements_kind,
253 Representation representation,
256 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
257 Safepoint::DeoptMode mode);
259 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
261 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
263 // Emit frame translation commands for an environment.
264 void WriteTranslation(LEnvironment* environment, Translation* translation);
266 void AddToTranslation(LEnvironment* environment,
267 Translation* translation,
271 int* object_index_pointer,
272 int* dematerialized_index_pointer);
274 void SaveCallerDoubles();
275 void RestoreCallerDoubles();
277 // Code generation steps. Returns true if code generation should continue.
278 void GenerateBodyInstructionPre(LInstruction* instr) override;
279 bool GeneratePrologue();
280 bool GenerateDeferredCode();
281 bool GenerateJumpTable();
282 bool GenerateSafepointTable();
284 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
285 void GenerateOsrPrologue();
288 RECORD_SIMPLE_SAFEPOINT,
289 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
292 void CallCode(Handle<Code> code,
293 RelocInfo::Mode mode,
294 LInstruction* instr);
296 void CallCodeGeneric(Handle<Code> code,
297 RelocInfo::Mode mode,
299 SafepointMode safepoint_mode);
301 void CallRuntime(const Runtime::Function* function,
304 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
306 void CallRuntime(Runtime::FunctionId id,
308 LInstruction* instr) {
309 const Runtime::Function* function = Runtime::FunctionForId(id);
310 CallRuntime(function, num_arguments, instr);
313 void LoadContextFromDeferred(LOperand* context);
314 void CallRuntimeFromDeferred(Runtime::FunctionId id,
319 // Generate a direct call to a known function. Expects the function
321 void CallKnownFunction(Handle<JSFunction> function,
322 int formal_parameter_count, int arity,
323 LInstruction* instr);
325 // Support for recording safepoint and position information.
326 void RecordAndWritePosition(int position) override;
327 void RecordSafepoint(LPointerMap* pointers,
328 Safepoint::Kind kind,
330 Safepoint::DeoptMode mode);
331 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
332 void RecordSafepoint(Safepoint::DeoptMode mode);
333 void RecordSafepointWithRegisters(LPointerMap* pointers,
335 Safepoint::DeoptMode mode);
336 void RecordSafepointWithLazyDeopt(LInstruction* instr,
337 SafepointMode safepoint_mode);
339 void EnsureSpaceForLazyDeopt(int space_needed) override;
341 ZoneList<LEnvironment*> deoptimizations_;
342 ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
343 int inlined_function_count_;
345 TranslationBuffer translations_;
346 ZoneList<LDeferredCode*> deferred_;
348 bool frame_is_built_;
350 // Builder that keeps track of safepoints in the code. The table itself is
351 // emitted at the end of the generated code.
352 SafepointTableBuilder safepoints_;
354 // Compiler from a set of parallel moves to a sequential list of moves.
355 LGapResolver resolver_;
357 Safepoint::Kind expected_safepoint_kind_;
361 class PushSafepointRegistersScope BASE_EMBEDDED {
363 explicit PushSafepointRegistersScope(LCodeGen* codegen)
364 : codegen_(codegen) {
365 DCHECK(codegen_->info()->is_calling());
366 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
367 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
369 UseScratchRegisterScope temps(codegen_->masm_);
370 // Preserve the value of lr which must be saved on the stack (the call to
371 // the stub will clobber it).
372 Register to_be_pushed_lr =
373 temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
374 codegen_->masm_->Mov(to_be_pushed_lr, lr);
375 StoreRegistersStateStub stub(codegen_->isolate());
376 codegen_->masm_->CallStub(&stub);
379 ~PushSafepointRegistersScope() {
380 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
381 RestoreRegistersStateStub stub(codegen_->isolate());
382 codegen_->masm_->CallStub(&stub);
383 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
390 friend class LDeferredCode;
391 friend class SafepointGenerator;
392 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
396 class LDeferredCode: public ZoneObject {
398 explicit LDeferredCode(LCodeGen* codegen)
400 external_exit_(NULL),
401 instruction_index_(codegen->current_instruction_) {
402 codegen->AddDeferredCode(this);
405 virtual ~LDeferredCode() { }
406 virtual void Generate() = 0;
407 virtual LInstruction* instr() = 0;
409 void SetExit(Label* exit) { external_exit_ = exit; }
410 Label* entry() { return &entry_; }
411 Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
412 int instruction_index() const { return instruction_index_; }
415 LCodeGen* codegen() const { return codegen_; }
416 MacroAssembler* masm() const { return codegen_->masm(); }
422 Label* external_exit_;
423 int instruction_index_;
427 // This is the abstract class used by EmitBranchGeneric.
428 // It is used to emit code for conditional branching. The Emit() function
429 // emits code to branch when the condition holds and EmitInverted() emits
430 // the branch when the inverted condition is verified.
432 // For actual examples of condition see the concrete implementation in
433 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
434 class BranchGenerator BASE_EMBEDDED {
436 explicit BranchGenerator(LCodeGen* codegen)
437 : codegen_(codegen) { }
439 virtual ~BranchGenerator() { }
441 virtual void Emit(Label* label) const = 0;
442 virtual void EmitInverted(Label* label) const = 0;
445 MacroAssembler* masm() const { return codegen_->masm(); }
450 } } // namespace v8::internal
452 #endif // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_