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 pushed_arguments_(0) {
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 enum StackMode { kMustUseFramePointer, kCanUseStackPointer };
85 MemOperand ToMemOperand(LOperand* op,
86 StackMode stack_mode = kCanUseStackPointer) const;
87 Handle<Object> ToHandle(LConstantOperand* op) const;
90 Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
92 int JSShiftAmountFromLConstant(LOperand* constant) {
93 return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
96 // TODO(jbramley): Examine these helpers and check that they make sense.
97 // IsInteger32Constant returns true for smi constants, for example.
98 bool IsInteger32Constant(LConstantOperand* op) const;
99 bool IsSmi(LConstantOperand* op) const;
101 int32_t ToInteger32(LConstantOperand* op) const;
102 Smi* ToSmi(LConstantOperand* op) const;
103 double ToDouble(LConstantOperand* op) const;
104 DoubleRegister ToDoubleRegister(LOperand* op) const;
106 // Declare methods that deal with the individual node types.
107 #define DECLARE_DO(type) void Do##type(L##type* node);
108 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
112 // Return a double scratch register which can be used locally
113 // when generating code for a lithium instruction.
114 DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
116 // Deferred code support.
117 void DoDeferredNumberTagD(LNumberTagD* instr);
118 void DoDeferredStackCheck(LStackCheck* instr);
119 void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr);
120 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
121 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
122 void DoDeferredMathAbsTagged(LMathAbsTagged* instr,
124 Label* allocation_entry);
126 void DoDeferredNumberTagU(LInstruction* instr,
130 void DoDeferredTaggedToI(LTaggedToI* instr,
134 void DoDeferredAllocate(LAllocate* instr);
135 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr);
136 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
137 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
142 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
143 void EmitGoto(int block);
144 void DoGap(LGap* instr);
146 // Generic version of EmitBranch. It contains some code to avoid emitting a
147 // branch on the next emitted basic block where we could just fall-through.
148 // You shouldn't use that directly but rather consider one of the helper like
149 // LCodeGen::EmitBranch, LCodeGen::EmitCompareAndBranch...
150 template<class InstrType>
151 void EmitBranchGeneric(InstrType instr,
152 const BranchGenerator& branch);
154 template<class InstrType>
155 void EmitBranch(InstrType instr, Condition condition);
157 template<class InstrType>
158 void EmitCompareAndBranch(InstrType instr,
163 template<class InstrType>
164 void EmitTestAndBranch(InstrType instr,
166 const Register& value,
169 template<class InstrType>
170 void EmitBranchIfNonZeroNumber(InstrType instr,
171 const FPRegister& value,
172 const FPRegister& scratch);
174 template<class InstrType>
175 void EmitBranchIfHeapNumber(InstrType instr,
176 const Register& value);
178 template<class InstrType>
179 void EmitBranchIfRoot(InstrType instr,
180 const Register& value,
181 Heap::RootListIndex index);
183 // Emits optimized code to deep-copy the contents of statically known object
184 // graphs (e.g. object literal boilerplate). Expects a pointer to the
185 // allocated destination object in the result register, and a pointer to the
186 // source object in the source register.
187 void EmitDeepCopy(Handle<JSObject> object,
192 AllocationSiteMode mode);
195 void EmitVectorLoadICRegisters(T* instr);
197 // Emits optimized code for %_IsString(x). Preserves input register.
198 // Returns the condition on which a final split to
199 // true and false label should be made, to optimize fallthrough.
200 Condition EmitIsString(Register input, Register temp1, Label* is_not_string,
201 SmiCheck check_needed);
203 void PopulateDeoptimizationData(Handle<Code> code);
204 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
206 MemOperand BuildSeqStringOperand(Register string,
209 String::Encoding encoding);
210 void DeoptimizeBranch(LInstruction* instr,
211 Deoptimizer::DeoptReason deopt_reason,
212 BranchType branch_type, Register reg = NoReg,
214 Deoptimizer::BailoutType* override_bailout_type = NULL);
215 void Deoptimize(LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
216 Deoptimizer::BailoutType* override_bailout_type = NULL);
217 void DeoptimizeIf(Condition cond, LInstruction* instr,
218 Deoptimizer::DeoptReason deopt_reason);
219 void DeoptimizeIfZero(Register rt, LInstruction* instr,
220 Deoptimizer::DeoptReason deopt_reason);
221 void DeoptimizeIfNotZero(Register rt, LInstruction* instr,
222 Deoptimizer::DeoptReason deopt_reason);
223 void DeoptimizeIfNegative(Register rt, LInstruction* instr,
224 Deoptimizer::DeoptReason deopt_reason);
225 void DeoptimizeIfSmi(Register rt, LInstruction* instr,
226 Deoptimizer::DeoptReason deopt_reason);
227 void DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
228 Deoptimizer::DeoptReason deopt_reason);
229 void DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
231 Deoptimizer::DeoptReason deopt_reason);
232 void DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
234 Deoptimizer::DeoptReason deopt_reason);
235 void DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr);
236 void DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
237 Deoptimizer::DeoptReason deopt_reason);
238 void DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
239 Deoptimizer::DeoptReason deopt_reason);
240 void DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
241 Deoptimizer::DeoptReason deopt_reason);
243 MemOperand PrepareKeyedExternalArrayOperand(Register key,
247 bool key_is_constant,
249 ElementsKind elements_kind,
251 MemOperand PrepareKeyedArrayOperand(Register base,
255 ElementsKind elements_kind,
256 Representation representation,
259 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
260 Safepoint::DeoptMode mode);
262 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
264 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
266 // Emit frame translation commands for an environment.
267 void WriteTranslation(LEnvironment* environment, Translation* translation);
269 void AddToTranslation(LEnvironment* environment,
270 Translation* translation,
274 int* object_index_pointer,
275 int* dematerialized_index_pointer);
277 void SaveCallerDoubles();
278 void RestoreCallerDoubles();
280 // Code generation steps. Returns true if code generation should continue.
281 void GenerateBodyInstructionPre(LInstruction* instr) override;
282 bool GeneratePrologue();
283 bool GenerateDeferredCode();
284 bool GenerateJumpTable();
285 bool GenerateSafepointTable();
287 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
288 void GenerateOsrPrologue();
291 RECORD_SIMPLE_SAFEPOINT,
292 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
295 void CallCode(Handle<Code> code,
296 RelocInfo::Mode mode,
297 LInstruction* instr);
299 void CallCodeGeneric(Handle<Code> code,
300 RelocInfo::Mode mode,
302 SafepointMode safepoint_mode);
304 void CallRuntime(const Runtime::Function* function,
307 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
309 void CallRuntime(Runtime::FunctionId id,
311 LInstruction* instr) {
312 const Runtime::Function* function = Runtime::FunctionForId(id);
313 CallRuntime(function, num_arguments, instr);
316 void LoadContextFromDeferred(LOperand* context);
317 void CallRuntimeFromDeferred(Runtime::FunctionId id,
322 // Generate a direct call to a known function. Expects the function
324 void CallKnownFunction(Handle<JSFunction> function,
325 int formal_parameter_count, int arity,
326 LInstruction* instr);
328 // Support for recording safepoint and position information.
329 void RecordAndWritePosition(int position) override;
330 void RecordSafepoint(LPointerMap* pointers,
331 Safepoint::Kind kind,
333 Safepoint::DeoptMode mode);
334 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
335 void RecordSafepoint(Safepoint::DeoptMode mode);
336 void RecordSafepointWithRegisters(LPointerMap* pointers,
338 Safepoint::DeoptMode mode);
339 void RecordSafepointWithLazyDeopt(LInstruction* instr,
340 SafepointMode safepoint_mode);
342 void EnsureSpaceForLazyDeopt(int space_needed) override;
344 ZoneList<LEnvironment*> deoptimizations_;
345 ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
346 int inlined_function_count_;
348 TranslationBuffer translations_;
349 ZoneList<LDeferredCode*> deferred_;
351 bool frame_is_built_;
353 // Builder that keeps track of safepoints in the code. The table itself is
354 // emitted at the end of the generated code.
355 SafepointTableBuilder safepoints_;
357 // Compiler from a set of parallel moves to a sequential list of moves.
358 LGapResolver resolver_;
360 Safepoint::Kind expected_safepoint_kind_;
362 // The number of arguments pushed onto the stack, either by this block or by a
364 int pushed_arguments_;
366 void RecordPushedArgumentsDelta(int delta) {
367 pushed_arguments_ += delta;
368 DCHECK(pushed_arguments_ >= 0);
373 class PushSafepointRegistersScope BASE_EMBEDDED {
375 explicit PushSafepointRegistersScope(LCodeGen* codegen)
376 : codegen_(codegen) {
377 DCHECK(codegen_->info()->is_calling());
378 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
379 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
381 UseScratchRegisterScope temps(codegen_->masm_);
382 // Preserve the value of lr which must be saved on the stack (the call to
383 // the stub will clobber it).
384 Register to_be_pushed_lr =
385 temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
386 codegen_->masm_->Mov(to_be_pushed_lr, lr);
387 StoreRegistersStateStub stub(codegen_->isolate());
388 codegen_->masm_->CallStub(&stub);
391 ~PushSafepointRegistersScope() {
392 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
393 RestoreRegistersStateStub stub(codegen_->isolate());
394 codegen_->masm_->CallStub(&stub);
395 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
402 friend class LDeferredCode;
403 friend class SafepointGenerator;
404 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
408 class LDeferredCode: public ZoneObject {
410 explicit LDeferredCode(LCodeGen* codegen)
412 external_exit_(NULL),
413 instruction_index_(codegen->current_instruction_) {
414 codegen->AddDeferredCode(this);
417 virtual ~LDeferredCode() { }
418 virtual void Generate() = 0;
419 virtual LInstruction* instr() = 0;
421 void SetExit(Label* exit) { external_exit_ = exit; }
422 Label* entry() { return &entry_; }
423 Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
424 int instruction_index() const { return instruction_index_; }
427 LCodeGen* codegen() const { return codegen_; }
428 MacroAssembler* masm() const { return codegen_->masm(); }
434 Label* external_exit_;
435 int instruction_index_;
439 // This is the abstract class used by EmitBranchGeneric.
440 // It is used to emit code for conditional branching. The Emit() function
441 // emits code to branch when the condition holds and EmitInverted() emits
442 // the branch when the inverted condition is verified.
444 // For actual examples of condition see the concrete implementation in
445 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
446 class BranchGenerator BASE_EMBEDDED {
448 explicit BranchGenerator(LCodeGen* codegen)
449 : codegen_(codegen) { }
451 virtual ~BranchGenerator() { }
453 virtual void Emit(Label* label) const = 0;
454 virtual void EmitInverted(Label* label) const = 0;
457 MacroAssembler* masm() const { return codegen_->masm(); }
462 } } // namespace v8::internal
464 #endif // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_