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 after_push_argument_(false),
42 inlined_arguments_(false) {
43 PopulateDeoptimizationLiteralsWithInlinedFunctions();
47 DCHECK(!after_push_argument_ || inlined_arguments_);
51 Scope* scope() const { return scope_; }
53 int LookupDestination(int block_id) const {
54 return chunk()->LookupDestination(block_id);
57 bool IsNextEmittedBlock(int block_id) const {
58 return LookupDestination(block_id) == GetNextEmittedBlock();
61 bool NeedsEagerFrame() const {
62 return GetStackSlotCount() > 0 ||
63 info()->is_non_deferred_calling() ||
65 info()->requires_frame();
67 bool NeedsDeferredFrame() const {
68 return !NeedsEagerFrame() && info()->is_deferred_calling();
71 LinkRegisterStatus GetLinkRegisterState() const {
72 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
75 // Try to generate code for the entire chunk, but it may fail if the
76 // chunk contains constructs we cannot handle. Returns true if the
77 // code generation attempt succeeded.
80 // Finish the code by setting stack height, safepoint, and bailout
82 void FinishCode(Handle<Code> code);
84 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
85 // Support for converting LOperands to assembler types.
86 Register ToRegister(LOperand* op) const;
87 Register ToRegister32(LOperand* op) const;
88 Operand ToOperand(LOperand* op);
89 Operand ToOperand32(LOperand* op);
90 enum StackMode { kMustUseFramePointer, kCanUseStackPointer };
91 MemOperand ToMemOperand(LOperand* op,
92 StackMode stack_mode = kCanUseStackPointer) const;
93 Handle<Object> ToHandle(LConstantOperand* op) const;
96 Operand ToShiftedRightOperand32(LOperand* right, LI* shift_info);
98 int JSShiftAmountFromLConstant(LOperand* constant) {
99 return ToInteger32(LConstantOperand::cast(constant)) & 0x1f;
102 // TODO(jbramley): Examine these helpers and check that they make sense.
103 // IsInteger32Constant returns true for smi constants, for example.
104 bool IsInteger32Constant(LConstantOperand* op) const;
105 bool IsSmi(LConstantOperand* op) const;
107 int32_t ToInteger32(LConstantOperand* op) const;
108 Smi* ToSmi(LConstantOperand* op) const;
109 double ToDouble(LConstantOperand* op) const;
110 DoubleRegister ToDoubleRegister(LOperand* op) const;
112 // Declare methods that deal with the individual node types.
113 #define DECLARE_DO(type) void Do##type(L##type* node);
114 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
118 // Return a double scratch register which can be used locally
119 // when generating code for a lithium instruction.
120 DoubleRegister double_scratch() { return crankshaft_fp_scratch; }
122 // Deferred code support.
123 void DoDeferredNumberTagD(LNumberTagD* instr);
124 void DoDeferredStackCheck(LStackCheck* instr);
125 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
126 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
127 void DoDeferredMathAbsTagged(LMathAbsTagged* instr,
129 Label* allocation_entry);
131 void DoDeferredNumberTagU(LInstruction* instr,
135 void DoDeferredTaggedToI(LTaggedToI* instr,
139 void DoDeferredAllocate(LAllocate* instr);
140 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr);
141 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
142 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
147 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
148 void EmitGoto(int block);
149 void DoGap(LGap* instr);
151 // Generic version of EmitBranch. It contains some code to avoid emitting a
152 // branch on the next emitted basic block where we could just fall-through.
153 // You shouldn't use that directly but rather consider one of the helper like
154 // LCodeGen::EmitBranch, LCodeGen::EmitCompareAndBranch...
155 template<class InstrType>
156 void EmitBranchGeneric(InstrType instr,
157 const BranchGenerator& branch);
159 template<class InstrType>
160 void EmitBranch(InstrType instr, Condition condition);
162 template<class InstrType>
163 void EmitCompareAndBranch(InstrType instr,
168 template<class InstrType>
169 void EmitTestAndBranch(InstrType instr,
171 const Register& value,
174 template<class InstrType>
175 void EmitBranchIfNonZeroNumber(InstrType instr,
176 const FPRegister& value,
177 const FPRegister& scratch);
179 template<class InstrType>
180 void EmitBranchIfHeapNumber(InstrType instr,
181 const Register& value);
183 template<class InstrType>
184 void EmitBranchIfRoot(InstrType instr,
185 const Register& value,
186 Heap::RootListIndex index);
188 // Emits optimized code to deep-copy the contents of statically known object
189 // graphs (e.g. object literal boilerplate). Expects a pointer to the
190 // allocated destination object in the result register, and a pointer to the
191 // source object in the source register.
192 void EmitDeepCopy(Handle<JSObject> object,
197 AllocationSiteMode mode);
200 void EmitVectorLoadICRegisters(T* instr);
202 // Emits optimized code for %_IsString(x). Preserves input register.
203 // Returns the condition on which a final split to
204 // true and false label should be made, to optimize fallthrough.
205 Condition EmitIsString(Register input, Register temp1, Label* is_not_string,
206 SmiCheck check_needed);
208 int DefineDeoptimizationLiteral(Handle<Object> literal);
209 void PopulateDeoptimizationData(Handle<Code> code);
210 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
212 MemOperand BuildSeqStringOperand(Register string,
215 String::Encoding encoding);
216 void DeoptimizeBranch(LInstruction* instr,
217 Deoptimizer::DeoptReason deopt_reason,
218 BranchType branch_type, Register reg = NoReg,
220 Deoptimizer::BailoutType* override_bailout_type = NULL);
221 void Deoptimize(LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
222 Deoptimizer::BailoutType* override_bailout_type = NULL);
223 void DeoptimizeIf(Condition cond, LInstruction* instr,
224 Deoptimizer::DeoptReason deopt_reason);
225 void DeoptimizeIfZero(Register rt, LInstruction* instr,
226 Deoptimizer::DeoptReason deopt_reason);
227 void DeoptimizeIfNotZero(Register rt, LInstruction* instr,
228 Deoptimizer::DeoptReason deopt_reason);
229 void DeoptimizeIfNegative(Register rt, LInstruction* instr,
230 Deoptimizer::DeoptReason deopt_reason);
231 void DeoptimizeIfSmi(Register rt, LInstruction* instr,
232 Deoptimizer::DeoptReason deopt_reason);
233 void DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
234 Deoptimizer::DeoptReason deopt_reason);
235 void DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
237 Deoptimizer::DeoptReason deopt_reason);
238 void DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
240 Deoptimizer::DeoptReason deopt_reason);
241 void DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr);
242 void DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
243 Deoptimizer::DeoptReason deopt_reason);
244 void DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
245 Deoptimizer::DeoptReason deopt_reason);
246 void DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
247 Deoptimizer::DeoptReason deopt_reason);
249 MemOperand PrepareKeyedExternalArrayOperand(Register key,
253 bool key_is_constant,
255 ElementsKind elements_kind,
257 MemOperand PrepareKeyedArrayOperand(Register base,
261 ElementsKind elements_kind,
262 Representation representation,
265 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
266 Safepoint::DeoptMode mode);
268 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
270 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
272 // Emit frame translation commands for an environment.
273 void WriteTranslation(LEnvironment* environment, Translation* translation);
275 void AddToTranslation(LEnvironment* environment,
276 Translation* translation,
280 int* object_index_pointer,
281 int* dematerialized_index_pointer);
283 void SaveCallerDoubles();
284 void RestoreCallerDoubles();
286 // Code generation steps. Returns true if code generation should continue.
287 void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
288 bool GeneratePrologue();
289 bool GenerateDeferredCode();
290 bool GenerateJumpTable();
291 bool GenerateSafepointTable();
293 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
294 void GenerateOsrPrologue();
297 RECORD_SIMPLE_SAFEPOINT,
298 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
301 void CallCode(Handle<Code> code,
302 RelocInfo::Mode mode,
303 LInstruction* instr);
305 void CallCodeGeneric(Handle<Code> code,
306 RelocInfo::Mode mode,
308 SafepointMode safepoint_mode);
310 void CallRuntime(const Runtime::Function* function,
313 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
315 void CallRuntime(Runtime::FunctionId id,
317 LInstruction* instr) {
318 const Runtime::Function* function = Runtime::FunctionForId(id);
319 CallRuntime(function, num_arguments, instr);
322 void LoadContextFromDeferred(LOperand* context);
323 void CallRuntimeFromDeferred(Runtime::FunctionId id,
328 // Generate a direct call to a known function. Expects the function
330 void CallKnownFunction(Handle<JSFunction> function,
331 int formal_parameter_count, int arity,
332 LInstruction* instr);
334 // Support for recording safepoint and position information.
335 void RecordAndWritePosition(int position) OVERRIDE;
336 void RecordSafepoint(LPointerMap* pointers,
337 Safepoint::Kind kind,
339 Safepoint::DeoptMode mode);
340 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
341 void RecordSafepoint(Safepoint::DeoptMode mode);
342 void RecordSafepointWithRegisters(LPointerMap* pointers,
344 Safepoint::DeoptMode mode);
345 void RecordSafepointWithLazyDeopt(LInstruction* instr,
346 SafepointMode safepoint_mode);
348 void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
350 ZoneList<LEnvironment*> deoptimizations_;
351 ZoneList<Deoptimizer::JumpTableEntry*> jump_table_;
352 ZoneList<Handle<Object> > deoptimization_literals_;
353 int inlined_function_count_;
355 TranslationBuffer translations_;
356 ZoneList<LDeferredCode*> deferred_;
358 bool frame_is_built_;
360 // Builder that keeps track of safepoints in the code. The table itself is
361 // emitted at the end of the generated code.
362 SafepointTableBuilder safepoints_;
364 // Compiler from a set of parallel moves to a sequential list of moves.
365 LGapResolver resolver_;
367 Safepoint::Kind expected_safepoint_kind_;
369 // This flag is true when we are after a push (but before a call).
370 // In this situation, jssp no longer references the end of the stack slots so,
371 // we can only reference a stack slot via fp.
372 bool after_push_argument_;
373 // If we have inlined arguments, we are no longer able to use jssp because
374 // jssp is modified and we never know if we are in a block after or before
375 // the pop of the arguments (which restores jssp).
376 bool inlined_arguments_;
380 class PushSafepointRegistersScope BASE_EMBEDDED {
382 explicit PushSafepointRegistersScope(LCodeGen* codegen)
383 : codegen_(codegen) {
384 DCHECK(codegen_->info()->is_calling());
385 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
386 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
388 UseScratchRegisterScope temps(codegen_->masm_);
389 // Preserve the value of lr which must be saved on the stack (the call to
390 // the stub will clobber it).
391 Register to_be_pushed_lr =
392 temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
393 codegen_->masm_->Mov(to_be_pushed_lr, lr);
394 StoreRegistersStateStub stub(codegen_->isolate());
395 codegen_->masm_->CallStub(&stub);
398 ~PushSafepointRegistersScope() {
399 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
400 RestoreRegistersStateStub stub(codegen_->isolate());
401 codegen_->masm_->CallStub(&stub);
402 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
409 friend class LDeferredCode;
410 friend class SafepointGenerator;
411 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
415 class LDeferredCode: public ZoneObject {
417 explicit LDeferredCode(LCodeGen* codegen)
419 external_exit_(NULL),
420 instruction_index_(codegen->current_instruction_) {
421 codegen->AddDeferredCode(this);
424 virtual ~LDeferredCode() { }
425 virtual void Generate() = 0;
426 virtual LInstruction* instr() = 0;
428 void SetExit(Label* exit) { external_exit_ = exit; }
429 Label* entry() { return &entry_; }
430 Label* exit() { return (external_exit_ != NULL) ? external_exit_ : &exit_; }
431 int instruction_index() const { return instruction_index_; }
434 LCodeGen* codegen() const { return codegen_; }
435 MacroAssembler* masm() const { return codegen_->masm(); }
441 Label* external_exit_;
442 int instruction_index_;
446 // This is the abstract class used by EmitBranchGeneric.
447 // It is used to emit code for conditional branching. The Emit() function
448 // emits code to branch when the condition holds and EmitInverted() emits
449 // the branch when the inverted condition is verified.
451 // For actual examples of condition see the concrete implementation in
452 // lithium-codegen-arm64.cc (e.g. BranchOnCondition, CompareAndBranch).
453 class BranchGenerator BASE_EMBEDDED {
455 explicit BranchGenerator(LCodeGen* codegen)
456 : codegen_(codegen) { }
458 virtual ~BranchGenerator() { }
460 virtual void Emit(Label* label) const = 0;
461 virtual void EmitInverted(Label* label) const = 0;
464 MacroAssembler* masm() const { return codegen_->masm(); }
469 } } // namespace v8::internal
471 #endif // V8_ARM64_LITHIUM_CODEGEN_ARM64_H_