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_ARM_LITHIUM_CODEGEN_ARM_H_
6 #define V8_ARM_LITHIUM_CODEGEN_ARM_H_
8 #include "arm/lithium-arm.h"
10 #include "arm/lithium-gap-resolver-arm.h"
11 #include "deoptimizer.h"
12 #include "lithium-codegen.h"
13 #include "safepoint-table.h"
20 // Forward declarations.
22 class SafepointGenerator;
24 class LCodeGen: public LCodeGenBase {
26 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
27 : LCodeGenBase(chunk, assembler, info),
28 deoptimizations_(4, info->zone()),
29 deopt_jump_table_(4, info->zone()),
30 deoptimization_literals_(8, 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 int LookupDestination(int block_id) const {
45 return chunk()->LookupDestination(block_id);
48 bool IsNextEmittedBlock(int block_id) const {
49 return LookupDestination(block_id) == GetNextEmittedBlock();
52 bool NeedsEagerFrame() const {
53 return GetStackSlotCount() > 0 ||
54 info()->is_non_deferred_calling() ||
56 info()->requires_frame();
58 bool NeedsDeferredFrame() const {
59 return !NeedsEagerFrame() && info()->is_deferred_calling();
62 LinkRegisterStatus GetLinkRegisterState() const {
63 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
66 // Support for converting LOperands to assembler types.
67 // LOperand must be a register.
68 Register ToRegister(LOperand* op) const;
70 // LOperand is loaded into scratch, unless already a register.
71 Register EmitLoadRegister(LOperand* op, Register scratch);
73 // LOperand must be a double register.
74 DwVfpRegister ToDoubleRegister(LOperand* op) const;
76 // LOperand is loaded into dbl_scratch, unless already a double register.
77 DwVfpRegister EmitLoadDoubleRegister(LOperand* op,
78 SwVfpRegister flt_scratch,
79 DwVfpRegister dbl_scratch);
80 int32_t ToRepresentation(LConstantOperand* op, 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 // Deferred code support.
103 void DoDeferredNumberTagD(LNumberTagD* instr);
105 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
106 void DoDeferredNumberTagIU(LInstruction* instr,
110 IntegerSignedness signedness);
112 void DoDeferredTaggedToI(LTaggedToI* instr);
113 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
114 void DoDeferredStackCheck(LStackCheck* instr);
115 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
116 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
117 void DoDeferredAllocate(LAllocate* instr);
118 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
120 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
121 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
125 void DoDeferredSIMD128ToTagged(LInstruction* instr, Runtime::FunctionId id);
127 // Parallel move support.
128 void DoParallelMove(LParallelMove* move);
129 void DoGap(LGap* instr);
131 MemOperand PrepareKeyedOperand(Register key,
133 bool key_is_constant,
137 int additional_index,
138 int additional_offset);
140 // Emit frame translation commands for an environment.
141 void WriteTranslation(LEnvironment* environment, Translation* translation);
143 // Declare methods that deal with the individual node types.
144 #define DECLARE_DO(type) void Do##type(L##type* node);
145 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
149 StrictMode strict_mode() const { return info()->strict_mode(); }
151 Scope* scope() const { return scope_; }
153 Register scratch0() { return r9; }
154 LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; }
156 LInstruction* GetNextInstruction();
158 void EmitClassOfTest(Label* if_true,
160 Handle<String> class_name,
163 Register temporary2);
165 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
167 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
169 void SaveCallerDoubles();
170 void RestoreCallerDoubles();
172 // Code generation passes. Returns true if code generation should
174 void GenerateBodyInstructionPre(LInstruction* instr) V8_OVERRIDE;
175 bool GeneratePrologue();
176 bool GenerateDeferredCode();
177 bool GenerateDeoptJumpTable();
178 bool GenerateSafepointTable();
180 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
181 void GenerateOsrPrologue();
184 RECORD_SIMPLE_SAFEPOINT,
185 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
188 int CallCodeSize(Handle<Code> code, RelocInfo::Mode mode);
192 RelocInfo::Mode mode,
194 TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
196 void CallCodeGeneric(
198 RelocInfo::Mode mode,
200 SafepointMode safepoint_mode,
201 TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
203 void CallRuntime(const Runtime::Function* function,
206 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
208 void CallRuntime(Runtime::FunctionId id,
210 LInstruction* instr) {
211 const Runtime::Function* function = Runtime::FunctionForId(id);
212 CallRuntime(function, num_arguments, instr);
215 void LoadContextFromDeferred(LOperand* context);
216 void CallRuntimeFromDeferred(Runtime::FunctionId id,
226 // Generate a direct call to a known function. Expects the function
228 void CallKnownFunction(Handle<JSFunction> function,
229 int formal_parameter_count,
234 void RecordSafepointWithLazyDeopt(LInstruction* instr,
235 SafepointMode safepoint_mode);
237 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
238 Safepoint::DeoptMode mode);
239 void DeoptimizeIf(Condition condition,
240 LEnvironment* environment,
241 Deoptimizer::BailoutType bailout_type);
242 void DeoptimizeIf(Condition condition, LEnvironment* environment);
244 void AddToTranslation(LEnvironment* environment,
245 Translation* translation,
249 int* object_index_pointer,
250 int* dematerialized_index_pointer);
251 void PopulateDeoptimizationData(Handle<Code> code);
252 int DefineDeoptimizationLiteral(Handle<Object> literal);
254 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
256 Register ToRegister(int index) const;
257 DwVfpRegister ToDoubleRegister(int index) const;
259 MemOperand BuildSeqStringOperand(Register string,
261 String::Encoding encoding);
263 void EmitIntegerMathAbs(LMathAbs* instr);
265 // Support for recording safepoint and position information.
266 void RecordSafepoint(LPointerMap* pointers,
267 Safepoint::Kind kind,
269 Safepoint::DeoptMode mode);
270 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
271 void RecordSafepoint(Safepoint::DeoptMode mode);
272 void RecordSafepointWithRegisters(LPointerMap* pointers,
274 Safepoint::DeoptMode mode);
275 void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
277 Safepoint::DeoptMode mode);
279 void RecordAndWritePosition(int position) V8_OVERRIDE;
281 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
282 void EmitGoto(int block);
284 // EmitBranch expects to be the last instruction of a block.
285 template<class InstrType>
286 void EmitBranch(InstrType instr, Condition condition);
287 template<class InstrType>
288 void EmitFalseBranch(InstrType instr, Condition condition);
289 void EmitNumberUntagD(Register input,
290 DwVfpRegister result,
291 bool allow_undefined_as_nan,
292 bool deoptimize_on_minus_zero,
294 NumberUntagDMode mode);
296 // Emits optimized code for typeof x == "y". Modifies input register.
297 // Returns the condition on which a final split to
298 // true and false label should be made, to optimize fallthrough.
299 Condition EmitTypeofIs(Label* true_label,
302 Handle<String> type_name);
304 // Emits optimized code for %_IsObject(x). Preserves input register.
305 // Returns the condition on which a final split to
306 // true and false label should be made, to optimize fallthrough.
307 Condition EmitIsObject(Register input,
309 Label* is_not_object,
312 // Emits optimized code for %_IsString(x). Preserves input register.
313 // Returns the condition on which a final split to
314 // true and false label should be made, to optimize fallthrough.
315 Condition EmitIsString(Register input,
317 Label* is_not_string,
318 SmiCheck check_needed);
320 // Emits optimized code for %_IsConstructCall().
321 // Caller should branch on equal condition.
322 void EmitIsConstructCall(Register temp1, Register temp2);
324 // Emits optimized code to deep-copy the contents of statically known
325 // object graphs (e.g. object literal boilerplate).
326 void EmitDeepCopy(Handle<JSObject> object,
330 AllocationSiteMode mode);
332 void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
333 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
335 void DoLoadKeyedSIMD128ExternalArray(LLoadKeyed* instr);
336 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
337 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
338 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
340 void DoStoreKeyedSIMD128ExternalArray(LStoreKeyed* instr);
341 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
342 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
344 ZoneList<LEnvironment*> deoptimizations_;
345 ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
346 ZoneList<Handle<Object> > deoptimization_literals_;
347 int inlined_function_count_;
349 TranslationBuffer translations_;
350 ZoneList<LDeferredCode*> deferred_;
352 bool frame_is_built_;
354 // Builder that keeps track of safepoints in the code. The table
355 // itself is emitted at the end of the generated code.
356 SafepointTableBuilder safepoints_;
358 // Compiler from a set of parallel moves to a sequential list of moves.
359 LGapResolver resolver_;
361 Safepoint::Kind expected_safepoint_kind_;
363 class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
365 PushSafepointRegistersScope(LCodeGen* codegen,
366 Safepoint::Kind kind)
367 : codegen_(codegen) {
368 ASSERT(codegen_->info()->is_calling());
369 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
370 codegen_->expected_safepoint_kind_ = kind;
372 switch (codegen_->expected_safepoint_kind_) {
373 case Safepoint::kWithRegisters:
374 codegen_->masm_->PushSafepointRegisters();
376 case Safepoint::kWithRegistersAndDoubles:
377 codegen_->masm_->PushSafepointRegistersAndDoubles();
384 ~PushSafepointRegistersScope() {
385 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
386 ASSERT((kind & Safepoint::kWithRegisters) != 0);
388 case Safepoint::kWithRegisters:
389 codegen_->masm_->PopSafepointRegisters();
391 case Safepoint::kWithRegistersAndDoubles:
392 codegen_->masm_->PopSafepointRegistersAndDoubles();
397 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
404 friend class LDeferredCode;
405 friend class LEnvironment;
406 friend class SafepointGenerator;
407 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
411 class LDeferredCode : public ZoneObject {
413 explicit LDeferredCode(LCodeGen* codegen)
415 external_exit_(NULL),
416 instruction_index_(codegen->current_instruction_) {
417 codegen->AddDeferredCode(this);
420 virtual ~LDeferredCode() {}
421 virtual void Generate() = 0;
422 virtual LInstruction* instr() = 0;
424 void SetExit(Label* exit) { external_exit_ = exit; }
425 Label* entry() { return &entry_; }
426 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
427 int instruction_index() const { return instruction_index_; }
430 LCodeGen* codegen() const { return codegen_; }
431 MacroAssembler* masm() const { return codegen_->masm(); }
437 Label* external_exit_;
438 int instruction_index_;
441 } } // namespace v8::internal
443 #endif // V8_ARM_LITHIUM_CODEGEN_ARM_H_