1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_ARM_LITHIUM_CODEGEN_ARM_H_
29 #define V8_ARM_LITHIUM_CODEGEN_ARM_H_
31 #include "arm/lithium-arm.h"
33 #include "arm/lithium-gap-resolver-arm.h"
34 #include "deoptimizer.h"
35 #include "lithium-codegen.h"
36 #include "safepoint-table.h"
43 // Forward declarations.
45 class SafepointGenerator;
47 class LCodeGen: public LCodeGenBase {
49 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
50 : LCodeGenBase(chunk, assembler, info),
51 deoptimizations_(4, info->zone()),
52 deopt_jump_table_(4, info->zone()),
53 deoptimization_literals_(8, info->zone()),
54 inlined_function_count_(0),
55 scope_(info->scope()),
56 translations_(info->zone()),
57 deferred_(8, info->zone()),
59 frame_is_built_(false),
60 safepoints_(info->zone()),
62 expected_safepoint_kind_(Safepoint::kSimple) {
63 PopulateDeoptimizationLiteralsWithInlinedFunctions();
67 int LookupDestination(int block_id) const {
68 return chunk()->LookupDestination(block_id);
71 bool IsNextEmittedBlock(int block_id) const {
72 return LookupDestination(block_id) == GetNextEmittedBlock();
75 bool NeedsEagerFrame() const {
76 return GetStackSlotCount() > 0 ||
77 info()->is_non_deferred_calling() ||
79 info()->requires_frame();
81 bool NeedsDeferredFrame() const {
82 return !NeedsEagerFrame() && info()->is_deferred_calling();
85 LinkRegisterStatus GetLinkRegisterState() const {
86 return frame_is_built_ ? kLRHasBeenSaved : kLRHasNotBeenSaved;
89 // Support for converting LOperands to assembler types.
90 // LOperand must be a register.
91 Register ToRegister(LOperand* op) const;
93 // LOperand is loaded into scratch, unless already a register.
94 Register EmitLoadRegister(LOperand* op, Register scratch);
96 // LOperand must be a double register.
97 DwVfpRegister ToDoubleRegister(LOperand* op) const;
99 // LOperand is loaded into dbl_scratch, unless already a double register.
100 DwVfpRegister EmitLoadDoubleRegister(LOperand* op,
101 SwVfpRegister flt_scratch,
102 DwVfpRegister dbl_scratch);
103 int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
104 int32_t ToInteger32(LConstantOperand* op) const;
105 Smi* ToSmi(LConstantOperand* op) const;
106 double ToDouble(LConstantOperand* op) const;
107 Operand ToOperand(LOperand* op);
108 MemOperand ToMemOperand(LOperand* op) const;
109 // Returns a MemOperand pointing to the high word of a DoubleStackSlot.
110 MemOperand ToHighMemOperand(LOperand* op) const;
112 bool IsInteger32(LConstantOperand* op) const;
113 bool IsSmi(LConstantOperand* op) const;
114 Handle<Object> ToHandle(LConstantOperand* op) const;
116 // Try to generate code for the entire chunk, but it may fail if the
117 // chunk contains constructs we cannot handle. Returns true if the
118 // code generation attempt succeeded.
121 // Finish the code by setting stack height, safepoint, and bailout
122 // information on it.
123 void FinishCode(Handle<Code> code);
125 // Deferred code support.
126 void DoDeferredNumberTagD(LNumberTagD* instr);
128 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
129 void DoDeferredNumberTagI(LInstruction* instr,
131 IntegerSignedness signedness);
133 void DoDeferredTaggedToI(LTaggedToI* instr);
134 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
135 void DoDeferredStackCheck(LStackCheck* instr);
136 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
137 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
138 void DoDeferredAllocate(LAllocate* instr);
139 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
141 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
143 // Parallel move support.
144 void DoParallelMove(LParallelMove* move);
145 void DoGap(LGap* instr);
147 MemOperand PrepareKeyedOperand(Register key,
149 bool key_is_constant,
153 int additional_index,
154 int additional_offset);
156 // Emit frame translation commands for an environment.
157 void WriteTranslation(LEnvironment* environment, Translation* translation);
159 // Declare methods that deal with the individual node types.
160 #define DECLARE_DO(type) void Do##type(L##type* node);
161 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
165 StrictModeFlag strict_mode_flag() const {
166 return info()->is_classic_mode() ? kNonStrictMode : kStrictMode;
169 Scope* scope() const { return scope_; }
171 Register scratch0() { return r9; }
172 LowDwVfpRegister double_scratch0() { return kScratchDoubleReg; }
174 LInstruction* GetNextInstruction();
176 void EmitClassOfTest(Label* if_true,
178 Handle<String> class_name,
181 Register temporary2);
183 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
185 void Abort(BailoutReason reason);
187 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
189 void SaveCallerDoubles();
190 void RestoreCallerDoubles();
192 // Code generation passes. Returns true if code generation should
194 bool GeneratePrologue();
195 bool GenerateDeferredCode();
196 bool GenerateDeoptJumpTable();
197 bool GenerateSafepointTable();
199 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
200 void GenerateOsrPrologue();
203 RECORD_SIMPLE_SAFEPOINT,
204 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
209 RelocInfo::Mode mode,
211 TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
213 void CallCodeGeneric(
215 RelocInfo::Mode mode,
217 SafepointMode safepoint_mode,
218 TargetAddressStorageMode storage_mode = CAN_INLINE_TARGET_ADDRESS);
220 void CallRuntime(const Runtime::Function* function,
223 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
225 void CallRuntime(Runtime::FunctionId id,
227 LInstruction* instr) {
228 const Runtime::Function* function = Runtime::FunctionForId(id);
229 CallRuntime(function, num_arguments, instr);
232 void LoadContextFromDeferred(LOperand* context);
233 void CallRuntimeFromDeferred(Runtime::FunctionId id,
243 // Generate a direct call to a known function. Expects the function
245 void CallKnownFunction(Handle<JSFunction> function,
246 int formal_parameter_count,
251 void RecordSafepointWithLazyDeopt(LInstruction* instr,
252 SafepointMode safepoint_mode);
254 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
255 Safepoint::DeoptMode mode);
256 void DeoptimizeIf(Condition condition,
257 LEnvironment* environment,
258 Deoptimizer::BailoutType bailout_type);
259 void DeoptimizeIf(Condition condition, LEnvironment* environment);
260 void ApplyCheckIf(Condition condition, LBoundsCheck* check);
262 void AddToTranslation(LEnvironment* environment,
263 Translation* translation,
267 int* object_index_pointer,
268 int* dematerialized_index_pointer);
269 void PopulateDeoptimizationData(Handle<Code> code);
270 int DefineDeoptimizationLiteral(Handle<Object> literal);
272 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
274 Register ToRegister(int index) const;
275 DwVfpRegister ToDoubleRegister(int index) const;
277 MemOperand BuildSeqStringOperand(Register string,
279 String::Encoding encoding);
281 void EmitIntegerMathAbs(LMathAbs* instr);
283 // Support for recording safepoint and position information.
284 void RecordSafepoint(LPointerMap* pointers,
285 Safepoint::Kind kind,
287 Safepoint::DeoptMode mode);
288 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
289 void RecordSafepoint(Safepoint::DeoptMode mode);
290 void RecordSafepointWithRegisters(LPointerMap* pointers,
292 Safepoint::DeoptMode mode);
293 void RecordSafepointWithRegistersAndDoubles(LPointerMap* pointers,
295 Safepoint::DeoptMode mode);
297 void RecordAndWritePosition(int position) V8_OVERRIDE;
299 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
300 void EmitGoto(int block);
302 // EmitBranch expects to be the last instruction of a block.
303 template<class InstrType>
304 void EmitBranch(InstrType instr, Condition condition);
305 template<class InstrType>
306 void EmitFalseBranch(InstrType instr, Condition condition);
307 void EmitNumberUntagD(Register input,
308 DwVfpRegister result,
309 bool allow_undefined_as_nan,
310 bool deoptimize_on_minus_zero,
312 NumberUntagDMode mode);
314 // Emits optimized code for typeof x == "y". Modifies input register.
315 // Returns the condition on which a final split to
316 // true and false label should be made, to optimize fallthrough.
317 Condition EmitTypeofIs(Label* true_label,
320 Handle<String> type_name);
322 // Emits optimized code for %_IsObject(x). Preserves input register.
323 // Returns the condition on which a final split to
324 // true and false label should be made, to optimize fallthrough.
325 Condition EmitIsObject(Register input,
327 Label* is_not_object,
330 // Emits optimized code for %_IsString(x). Preserves input register.
331 // Returns the condition on which a final split to
332 // true and false label should be made, to optimize fallthrough.
333 Condition EmitIsString(Register input,
335 Label* is_not_string,
336 SmiCheck check_needed);
338 // Emits optimized code for %_IsConstructCall().
339 // Caller should branch on equal condition.
340 void EmitIsConstructCall(Register temp1, Register temp2);
342 // Emits optimized code to deep-copy the contents of statically known
343 // object graphs (e.g. object literal boilerplate).
344 void EmitDeepCopy(Handle<JSObject> object,
348 AllocationSiteMode mode);
350 // Emit optimized code for integer division.
351 // Inputs are signed.
352 // All registers are clobbered.
353 // If 'remainder' is no_reg, it is not computed.
354 void EmitSignedIntegerDivisionByConstant(Register result,
359 LEnvironment* environment);
361 void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
362 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
363 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
364 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
365 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
366 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
367 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
369 ZoneList<LEnvironment*> deoptimizations_;
370 ZoneList<Deoptimizer::JumpTableEntry> deopt_jump_table_;
371 ZoneList<Handle<Object> > deoptimization_literals_;
372 int inlined_function_count_;
374 TranslationBuffer translations_;
375 ZoneList<LDeferredCode*> deferred_;
377 bool frame_is_built_;
379 // Builder that keeps track of safepoints in the code. The table
380 // itself is emitted at the end of the generated code.
381 SafepointTableBuilder safepoints_;
383 // Compiler from a set of parallel moves to a sequential list of moves.
384 LGapResolver resolver_;
386 Safepoint::Kind expected_safepoint_kind_;
388 class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
390 PushSafepointRegistersScope(LCodeGen* codegen,
391 Safepoint::Kind kind)
392 : codegen_(codegen) {
393 ASSERT(codegen_->info()->is_calling());
394 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
395 codegen_->expected_safepoint_kind_ = kind;
397 switch (codegen_->expected_safepoint_kind_) {
398 case Safepoint::kWithRegisters:
399 codegen_->masm_->PushSafepointRegisters();
401 case Safepoint::kWithRegistersAndDoubles:
402 codegen_->masm_->PushSafepointRegistersAndDoubles();
409 ~PushSafepointRegistersScope() {
410 Safepoint::Kind kind = codegen_->expected_safepoint_kind_;
411 ASSERT((kind & Safepoint::kWithRegisters) != 0);
413 case Safepoint::kWithRegisters:
414 codegen_->masm_->PopSafepointRegisters();
416 case Safepoint::kWithRegistersAndDoubles:
417 codegen_->masm_->PopSafepointRegistersAndDoubles();
422 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
429 friend class LDeferredCode;
430 friend class LEnvironment;
431 friend class SafepointGenerator;
432 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
436 class LDeferredCode : public ZoneObject {
438 explicit LDeferredCode(LCodeGen* codegen)
440 external_exit_(NULL),
441 instruction_index_(codegen->current_instruction_) {
442 codegen->AddDeferredCode(this);
445 virtual ~LDeferredCode() {}
446 virtual void Generate() = 0;
447 virtual LInstruction* instr() = 0;
449 void SetExit(Label* exit) { external_exit_ = exit; }
450 Label* entry() { return &entry_; }
451 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
452 int instruction_index() const { return instruction_index_; }
455 LCodeGen* codegen() const { return codegen_; }
456 MacroAssembler* masm() const { return codegen_->masm(); }
462 Label* external_exit_;
463 int instruction_index_;
466 } } // namespace v8::internal
468 #endif // V8_ARM_LITHIUM_CODEGEN_ARM_H_