Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef V8_CRANKSHAFT_ARM_LITHIUM_CODEGEN_ARM_H_ |
| 6 | #define V8_CRANKSHAFT_ARM_LITHIUM_CODEGEN_ARM_H_ |
| 7 | |
| 8 | #include "src/ast/scopes.h" |
| 9 | #include "src/crankshaft/arm/lithium-arm.h" |
| 10 | #include "src/crankshaft/arm/lithium-gap-resolver-arm.h" |
| 11 | #include "src/crankshaft/lithium-codegen.h" |
| 12 | #include "src/deoptimizer.h" |
| 13 | #include "src/safepoint-table.h" |
| 14 | #include "src/utils.h" |
| 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
| 19 | // Forward declarations. |
| 20 | class LDeferredCode; |
| 21 | class SafepointGenerator; |
| 22 | |
| 23 | class LCodeGen: public LCodeGenBase { |
| 24 | public: |
| 25 | LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info) |
| 26 | : LCodeGenBase(chunk, assembler, info), |
| 27 | jump_table_(4, info->zone()), |
| 28 | scope_(info->scope()), |
| 29 | deferred_(8, info->zone()), |
| 30 | frame_is_built_(false), |
| 31 | safepoints_(info->zone()), |
| 32 | resolver_(this), |
| 33 | expected_safepoint_kind_(Safepoint::kSimple) { |
| 34 | PopulateDeoptimizationLiteralsWithInlinedFunctions(); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | int LookupDestination(int block_id) const { |
| 39 | return chunk()->LookupDestination(block_id); |
| 40 | } |
| 41 | |
| 42 | bool IsNextEmittedBlock(int block_id) const { |
| 43 | return LookupDestination(block_id) == GetNextEmittedBlock(); |
| 44 | } |
| 45 | |
| 46 | bool NeedsEagerFrame() const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 47 | return HasAllocatedStackSlots() || info()->is_non_deferred_calling() || |
| 48 | !info()->IsStub() || info()->requires_frame(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | } |
| 50 | bool NeedsDeferredFrame() const { |
| 51 | return !NeedsEagerFrame() && info()->is_deferred_calling(); |
| 52 | } |
| 53 | |
| 54 | LinkRegisterStatus GetLinkRegisterState() const { |
| 55 | return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved; |
| 56 | } |
| 57 | |
| 58 | // Support for converting LOperands to assembler types. |
| 59 | // LOperand must be a register. |
| 60 | Register ToRegister(LOperand* op) const; |
| 61 | |
| 62 | // LOperand is loaded into scratch, unless already a register. |
| 63 | Register EmitLoadRegister(LOperand* op, Register scratch); |
| 64 | |
| 65 | // LOperand must be a double register. |
| 66 | DwVfpRegister ToDoubleRegister(LOperand* op) const; |
| 67 | |
| 68 | // LOperand is loaded into dbl_scratch, unless already a double register. |
| 69 | DwVfpRegister EmitLoadDoubleRegister(LOperand* op, |
| 70 | SwVfpRegister flt_scratch, |
| 71 | DwVfpRegister dbl_scratch); |
| 72 | int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const; |
| 73 | int32_t ToInteger32(LConstantOperand* op) const; |
| 74 | Smi* ToSmi(LConstantOperand* op) const; |
| 75 | double ToDouble(LConstantOperand* op) const; |
| 76 | Operand ToOperand(LOperand* op); |
| 77 | MemOperand ToMemOperand(LOperand* op) const; |
| 78 | // Returns a MemOperand pointing to the high word of a DoubleStackSlot. |
| 79 | MemOperand ToHighMemOperand(LOperand* op) const; |
| 80 | |
| 81 | bool IsInteger32(LConstantOperand* op) const; |
| 82 | bool IsSmi(LConstantOperand* op) const; |
| 83 | Handle<Object> ToHandle(LConstantOperand* op) const; |
| 84 | |
| 85 | // Try to generate code for the entire chunk, but it may fail if the |
| 86 | // chunk contains constructs we cannot handle. Returns true if the |
| 87 | // code generation attempt succeeded. |
| 88 | bool GenerateCode(); |
| 89 | |
| 90 | // Finish the code by setting stack height, safepoint, and bailout |
| 91 | // information on it. |
| 92 | void FinishCode(Handle<Code> code); |
| 93 | |
| 94 | // Deferred code support. |
| 95 | void DoDeferredNumberTagD(LNumberTagD* instr); |
| 96 | |
| 97 | enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 }; |
| 98 | void DoDeferredNumberTagIU(LInstruction* instr, |
| 99 | LOperand* value, |
| 100 | LOperand* temp1, |
| 101 | LOperand* temp2, |
| 102 | IntegerSignedness signedness); |
| 103 | |
| 104 | void DoDeferredTaggedToI(LTaggedToI* instr); |
| 105 | void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr); |
| 106 | void DoDeferredStackCheck(LStackCheck* instr); |
| 107 | void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr); |
| 108 | void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr); |
| 109 | void DoDeferredStringCharFromCode(LStringCharFromCode* instr); |
| 110 | void DoDeferredAllocate(LAllocate* instr); |
| 111 | void DoDeferredInstanceMigration(LCheckMaps* instr, Register object); |
| 112 | void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr, |
| 113 | Register result, |
| 114 | Register object, |
| 115 | Register index); |
| 116 | |
| 117 | // Parallel move support. |
| 118 | void DoParallelMove(LParallelMove* move); |
| 119 | void DoGap(LGap* instr); |
| 120 | |
| 121 | MemOperand PrepareKeyedOperand(Register key, |
| 122 | Register base, |
| 123 | bool key_is_constant, |
| 124 | int constant_key, |
| 125 | int element_size, |
| 126 | int shift_size, |
| 127 | int base_offset); |
| 128 | |
| 129 | // Emit frame translation commands for an environment. |
| 130 | void WriteTranslation(LEnvironment* environment, Translation* translation); |
| 131 | |
| 132 | // Declare methods that deal with the individual node types. |
| 133 | #define DECLARE_DO(type) void Do##type(L##type* node); |
| 134 | LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO) |
| 135 | #undef DECLARE_DO |
| 136 | |
| 137 | private: |
| 138 | LanguageMode language_mode() const { return info()->language_mode(); } |
| 139 | |
| 140 | Scope* scope() const { return scope_; } |
| 141 | |
| 142 | Register scratch0() { return r9; } |
| 143 | LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; } |
| 144 | |
| 145 | LInstruction* GetNextInstruction(); |
| 146 | |
| 147 | void EmitClassOfTest(Label* if_true, |
| 148 | Label* if_false, |
| 149 | Handle<String> class_name, |
| 150 | Register input, |
| 151 | Register temporary, |
| 152 | Register temporary2); |
| 153 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 154 | bool HasAllocatedStackSlots() const { |
| 155 | return chunk()->HasAllocatedStackSlots(); |
| 156 | } |
| 157 | int GetStackSlotCount() const { return chunk()->GetSpillSlotCount(); } |
| 158 | int GetTotalFrameSlotCount() const { |
| 159 | return chunk()->GetTotalFrameSlotCount(); |
| 160 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 161 | |
| 162 | void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); } |
| 163 | |
| 164 | void SaveCallerDoubles(); |
| 165 | void RestoreCallerDoubles(); |
| 166 | |
| 167 | // Code generation passes. Returns true if code generation should |
| 168 | // continue. |
| 169 | void GenerateBodyInstructionPre(LInstruction* instr) override; |
| 170 | bool GeneratePrologue(); |
| 171 | bool GenerateDeferredCode(); |
| 172 | bool GenerateJumpTable(); |
| 173 | bool GenerateSafepointTable(); |
| 174 | |
| 175 | // Generates the custom OSR entrypoint and sets the osr_pc_offset. |
| 176 | void GenerateOsrPrologue(); |
| 177 | |
| 178 | enum SafepointMode { |
| 179 | RECORD_SIMPLE_SAFEPOINT, |
| 180 | RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS |
| 181 | }; |
| 182 | |
| 183 | int CallCodeSize(Handle<Code> code, RelocInfo::Mode mode); |
| 184 | |
| 185 | void CallCode( |
| 186 | Handle<Code> code, |
| 187 | RelocInfo::Mode mode, |
| 188 | LInstruction* instr, |
| 189 | TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS); |
| 190 | |
| 191 | void CallCodeGeneric( |
| 192 | Handle<Code> code, |
| 193 | RelocInfo::Mode mode, |
| 194 | LInstruction* instr, |
| 195 | SafepointMode safepoint_mode, |
| 196 | TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS); |
| 197 | |
| 198 | void CallRuntime(const Runtime::Function* function, |
| 199 | int num_arguments, |
| 200 | LInstruction* instr, |
| 201 | SaveFPRegsMode save_doubles = kDontSaveFPRegs); |
| 202 | |
| 203 | void CallRuntime(Runtime::FunctionId id, |
| 204 | int num_arguments, |
| 205 | LInstruction* instr) { |
| 206 | const Runtime::Function* function = Runtime::FunctionForId(id); |
| 207 | CallRuntime(function, num_arguments, instr); |
| 208 | } |
| 209 | |
| 210 | void CallRuntime(Runtime::FunctionId id, LInstruction* instr) { |
| 211 | const Runtime::Function* function = Runtime::FunctionForId(id); |
| 212 | CallRuntime(function, function->nargs, instr); |
| 213 | } |
| 214 | |
| 215 | void LoadContextFromDeferred(LOperand* context); |
| 216 | void CallRuntimeFromDeferred(Runtime::FunctionId id, |
| 217 | int argc, |
| 218 | LInstruction* instr, |
| 219 | LOperand* context); |
| 220 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 221 | void PrepareForTailCall(const ParameterCount& actual, Register scratch1, |
| 222 | Register scratch2, Register scratch3); |
| 223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 224 | // Generate a direct call to a known function. Expects the function |
| 225 | // to be in r1. |
| 226 | void CallKnownFunction(Handle<JSFunction> function, |
| 227 | int formal_parameter_count, int arity, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 228 | bool is_tail_call, LInstruction* instr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 229 | |
| 230 | void RecordSafepointWithLazyDeopt(LInstruction* instr, |
| 231 | SafepointMode safepoint_mode); |
| 232 | |
| 233 | void RegisterEnvironmentForDeoptimization(LEnvironment* environment, |
| 234 | Safepoint::DeoptMode mode); |
| 235 | void DeoptimizeIf(Condition condition, LInstruction* instr, |
| 236 | Deoptimizer::DeoptReason deopt_reason, |
| 237 | Deoptimizer::BailoutType bailout_type); |
| 238 | void DeoptimizeIf(Condition condition, LInstruction* instr, |
| 239 | Deoptimizer::DeoptReason deopt_reason); |
| 240 | |
| 241 | void AddToTranslation(LEnvironment* environment, |
| 242 | Translation* translation, |
| 243 | LOperand* op, |
| 244 | bool is_tagged, |
| 245 | bool is_uint32, |
| 246 | int* object_index_pointer, |
| 247 | int* dematerialized_index_pointer); |
| 248 | |
| 249 | Register ToRegister(int index) const; |
| 250 | DwVfpRegister ToDoubleRegister(int index) const; |
| 251 | |
| 252 | MemOperand BuildSeqStringOperand(Register string, |
| 253 | LOperand* index, |
| 254 | String::Encoding encoding); |
| 255 | |
| 256 | void EmitIntegerMathAbs(LMathAbs* instr); |
| 257 | |
| 258 | // Support for recording safepoint and position information. |
| 259 | void RecordSafepoint(LPointerMap* pointers, |
| 260 | Safepoint::Kind kind, |
| 261 | int arguments, |
| 262 | Safepoint::DeoptMode mode); |
| 263 | void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode); |
| 264 | void RecordSafepoint(Safepoint::DeoptMode mode); |
| 265 | void RecordSafepointWithRegisters(LPointerMap* pointers, |
| 266 | int arguments, |
| 267 | Safepoint::DeoptMode mode); |
| 268 | |
| 269 | void RecordAndWritePosition(int position) override; |
| 270 | |
| 271 | static Condition TokenToCondition(Token::Value op, bool is_unsigned); |
| 272 | void EmitGoto(int block); |
| 273 | |
| 274 | // EmitBranch expects to be the last instruction of a block. |
| 275 | template<class InstrType> |
| 276 | void EmitBranch(InstrType instr, Condition condition); |
| 277 | template <class InstrType> |
| 278 | void EmitTrueBranch(InstrType instr, Condition condition); |
| 279 | template <class InstrType> |
| 280 | void EmitFalseBranch(InstrType instr, Condition condition); |
| 281 | void EmitNumberUntagD(LNumberUntagD* instr, Register input, |
| 282 | DwVfpRegister result, NumberUntagDMode mode); |
| 283 | |
| 284 | // Emits optimized code for typeof x == "y". Modifies input register. |
| 285 | // Returns the condition on which a final split to |
| 286 | // true and false label should be made, to optimize fallthrough. |
| 287 | Condition EmitTypeofIs(Label* true_label, |
| 288 | Label* false_label, |
| 289 | Register input, |
| 290 | Handle<String> type_name); |
| 291 | |
| 292 | // Emits optimized code for %_IsString(x). Preserves input register. |
| 293 | // Returns the condition on which a final split to |
| 294 | // true and false label should be made, to optimize fallthrough. |
| 295 | Condition EmitIsString(Register input, |
| 296 | Register temp1, |
| 297 | Label* is_not_string, |
| 298 | SmiCheck check_needed); |
| 299 | |
| 300 | // Emits optimized code to deep-copy the contents of statically known |
| 301 | // object graphs (e.g. object literal boilerplate). |
| 302 | void EmitDeepCopy(Handle<JSObject> object, |
| 303 | Register result, |
| 304 | Register source, |
| 305 | int* offset, |
| 306 | AllocationSiteMode mode); |
| 307 | |
| 308 | void EnsureSpaceForLazyDeopt(int space_needed) override; |
| 309 | void DoLoadKeyedExternalArray(LLoadKeyed* instr); |
| 310 | void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr); |
| 311 | void DoLoadKeyedFixedArray(LLoadKeyed* instr); |
| 312 | void DoStoreKeyedExternalArray(LStoreKeyed* instr); |
| 313 | void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr); |
| 314 | void DoStoreKeyedFixedArray(LStoreKeyed* instr); |
| 315 | |
| 316 | template <class T> |
| 317 | void EmitVectorLoadICRegisters(T* instr); |
| 318 | template <class T> |
| 319 | void EmitVectorStoreICRegisters(T* instr); |
| 320 | |
| 321 | ZoneList<Deoptimizer::JumpTableEntry> jump_table_; |
| 322 | Scope* const scope_; |
| 323 | ZoneList<LDeferredCode*> deferred_; |
| 324 | bool frame_is_built_; |
| 325 | |
| 326 | // Builder that keeps track of safepoints in the code. The table |
| 327 | // itself is emitted at the end of the generated code. |
| 328 | SafepointTableBuilder safepoints_; |
| 329 | |
| 330 | // Compiler from a set of parallel moves to a sequential list of moves. |
| 331 | LGapResolver resolver_; |
| 332 | |
| 333 | Safepoint::Kind expected_safepoint_kind_; |
| 334 | |
| 335 | class PushSafepointRegistersScope final BASE_EMBEDDED { |
| 336 | public: |
| 337 | explicit PushSafepointRegistersScope(LCodeGen* codegen) |
| 338 | : codegen_(codegen) { |
| 339 | DCHECK(codegen_->info()->is_calling()); |
| 340 | DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple); |
| 341 | codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters; |
| 342 | codegen_->masm_->PushSafepointRegisters(); |
| 343 | } |
| 344 | |
| 345 | ~PushSafepointRegistersScope() { |
| 346 | DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters); |
| 347 | codegen_->masm_->PopSafepointRegisters(); |
| 348 | codegen_->expected_safepoint_kind_ = Safepoint::kSimple; |
| 349 | } |
| 350 | |
| 351 | private: |
| 352 | LCodeGen* codegen_; |
| 353 | }; |
| 354 | |
| 355 | friend class LDeferredCode; |
| 356 | friend class LEnvironment; |
| 357 | friend class SafepointGenerator; |
| 358 | DISALLOW_COPY_AND_ASSIGN(LCodeGen); |
| 359 | }; |
| 360 | |
| 361 | |
| 362 | class LDeferredCode : public ZoneObject { |
| 363 | public: |
| 364 | explicit LDeferredCode(LCodeGen* codegen) |
| 365 | : codegen_(codegen), |
| 366 | external_exit_(NULL), |
| 367 | instruction_index_(codegen->current_instruction_) { |
| 368 | codegen->AddDeferredCode(this); |
| 369 | } |
| 370 | |
| 371 | virtual ~LDeferredCode() {} |
| 372 | virtual void Generate() = 0; |
| 373 | virtual LInstruction* instr() = 0; |
| 374 | |
| 375 | void SetExit(Label* exit) { external_exit_ = exit; } |
| 376 | Label* entry() { return &entry_; } |
| 377 | Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; } |
| 378 | int instruction_index() const { return instruction_index_; } |
| 379 | |
| 380 | protected: |
| 381 | LCodeGen* codegen() const { return codegen_; } |
| 382 | MacroAssembler* masm() const { return codegen_->masm(); } |
| 383 | |
| 384 | private: |
| 385 | LCodeGen* codegen_; |
| 386 | Label entry_; |
| 387 | Label exit_; |
| 388 | Label* external_exit_; |
| 389 | int instruction_index_; |
| 390 | }; |
| 391 | |
| 392 | } // namespace internal |
| 393 | } // namespace v8 |
| 394 | |
| 395 | #endif // V8_CRANKSHAFT_ARM_LITHIUM_CODEGEN_ARM_H_ |