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_X64_LITHIUM_CODEGEN_X64_H_
6 #define V8_X64_LITHIUM_CODEGEN_X64_H_
8 #include "src/x64/lithium-x64.h"
10 #include "src/base/logging.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"
16 #include "src/x64/lithium-gap-resolver-x64.h"
21 // Forward declarations.
23 class SafepointGenerator;
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 PopulateDeoptimizationLiteralsWithInlinedFunctions();
43 int LookupDestination(int block_id) const {
44 return chunk()->LookupDestination(block_id);
47 bool IsNextEmittedBlock(int block_id) const {
48 return LookupDestination(block_id) == GetNextEmittedBlock();
51 bool NeedsEagerFrame() const {
52 return GetStackSlotCount() > 0 ||
53 info()->is_non_deferred_calling() ||
55 info()->requires_frame();
57 bool NeedsDeferredFrame() const {
58 return !NeedsEagerFrame() && info()->is_deferred_calling();
61 // Support for converting LOperands to assembler types.
62 Register ToRegister(LOperand* op) const;
63 XMMRegister ToDoubleRegister(LOperand* op) const;
64 bool IsInteger32Constant(LConstantOperand* op) const;
65 bool IsExternalConstant(LConstantOperand* op) const;
66 bool IsDehoistedKeyConstant(LConstantOperand* op) const;
67 bool IsSmiConstant(LConstantOperand* op) const;
68 int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
69 int32_t ToInteger32(LConstantOperand* op) const;
70 Smi* ToSmi(LConstantOperand* op) const;
71 double ToDouble(LConstantOperand* op) const;
72 ExternalReference ToExternalReference(LConstantOperand* op) const;
73 Handle<Object> ToHandle(LConstantOperand* op) const;
74 Operand ToOperand(LOperand* op) const;
76 // Try to generate code for the entire chunk, but it may fail if the
77 // chunk contains constructs we cannot handle. Returns true if the
78 // code generation attempt succeeded.
81 // Finish the code by setting stack height, safepoint, and bailout
83 void FinishCode(Handle<Code> code);
85 // Deferred code support.
86 void DoDeferredNumberTagD(LNumberTagD* instr);
88 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
89 void DoDeferredNumberTagIU(LInstruction* instr,
93 IntegerSignedness signedness);
95 void DoDeferredTaggedToI(LTaggedToI* instr, Label* done);
96 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
97 void DoDeferredStackCheck(LStackCheck* instr);
98 void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr);
99 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
100 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
101 void DoDeferredAllocate(LAllocate* instr);
102 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
103 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
107 // Parallel move support.
108 void DoParallelMove(LParallelMove* move);
109 void DoGap(LGap* instr);
111 // Emit frame translation commands for an environment.
112 void WriteTranslation(LEnvironment* environment, Translation* translation);
114 // Declare methods that deal with the individual node types.
115 #define DECLARE_DO(type) void Do##type(L##type* node);
116 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
120 LanguageMode language_mode() const { return info()->language_mode(); }
122 LPlatformChunk* chunk() const { return chunk_; }
123 Scope* scope() const { return scope_; }
124 HGraph* graph() const { return chunk()->graph(); }
126 XMMRegister double_scratch0() const { return xmm0; }
128 void EmitClassOfTest(Label* if_true,
130 Handle<String> class_name,
135 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
137 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
140 void SaveCallerDoubles();
141 void RestoreCallerDoubles();
143 // Code generation passes. Returns true if code generation should
145 void GenerateBodyInstructionPre(LInstruction* instr) override;
146 void GenerateBodyInstructionPost(LInstruction* instr) override;
147 bool GeneratePrologue();
148 bool GenerateDeferredCode();
149 bool GenerateJumpTable();
150 bool GenerateSafepointTable();
152 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
153 void GenerateOsrPrologue();
156 RECORD_SIMPLE_SAFEPOINT,
157 RECORD_SAFEPOINT_WITH_REGISTERS
160 void CallCodeGeneric(Handle<Code> code,
161 RelocInfo::Mode mode,
163 SafepointMode safepoint_mode,
167 void CallCode(Handle<Code> code,
168 RelocInfo::Mode mode,
169 LInstruction* instr);
171 void CallRuntime(const Runtime::Function* function,
174 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
176 void CallRuntime(Runtime::FunctionId id,
178 LInstruction* instr) {
179 const Runtime::Function* function = Runtime::FunctionForId(id);
180 CallRuntime(function, num_arguments, instr);
183 void CallRuntimeFromDeferred(Runtime::FunctionId id,
188 void LoadContextFromDeferred(LOperand* context);
190 // Generate a direct call to a known function. Expects the function
192 void CallKnownFunction(Handle<JSFunction> function,
193 int formal_parameter_count, int arity,
194 LInstruction* instr);
196 void RecordSafepointWithLazyDeopt(LInstruction* instr,
197 SafepointMode safepoint_mode,
199 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
200 Safepoint::DeoptMode mode);
201 void DeoptimizeIf(Condition cc, LInstruction* instr,
202 Deoptimizer::DeoptReason deopt_reason,
203 Deoptimizer::BailoutType bailout_type);
204 void DeoptimizeIf(Condition cc, LInstruction* instr,
205 Deoptimizer::DeoptReason deopt_reason);
207 bool DeoptEveryNTimes() {
208 return FLAG_deopt_every_n_times != 0 && !info()->IsStub();
211 void AddToTranslation(LEnvironment* environment,
212 Translation* translation,
216 int* object_index_pointer,
217 int* dematerialized_index_pointer);
218 void PopulateDeoptimizationData(Handle<Code> code);
220 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
222 Register ToRegister(int index) const;
223 XMMRegister ToDoubleRegister(int index) const;
224 Operand BuildFastArrayOperand(
225 LOperand* elements_pointer,
227 Representation key_representation,
228 ElementsKind elements_kind,
229 uint32_t base_offset);
231 Operand BuildSeqStringOperand(Register string,
233 String::Encoding encoding);
235 void EmitIntegerMathAbs(LMathAbs* instr);
236 void EmitSmiMathAbs(LMathAbs* instr);
238 // Support for recording safepoint and position information.
239 void RecordSafepoint(LPointerMap* pointers,
240 Safepoint::Kind kind,
242 Safepoint::DeoptMode mode);
243 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
244 void RecordSafepoint(Safepoint::DeoptMode mode);
245 void RecordSafepointWithRegisters(LPointerMap* pointers,
247 Safepoint::DeoptMode mode);
248 void RecordAndWritePosition(int position) override;
250 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
251 void EmitGoto(int block);
253 // EmitBranch expects to be the last instruction of a block.
254 template<class InstrType>
255 void EmitBranch(InstrType instr, Condition cc);
256 template <class InstrType>
257 void EmitTrueBranch(InstrType instr, Condition cc);
258 template <class InstrType>
259 void EmitFalseBranch(InstrType instr, Condition cc);
260 void EmitNumberUntagD(LNumberUntagD* instr, Register input,
261 XMMRegister result, NumberUntagDMode mode);
263 // Emits optimized code for typeof x == "y". Modifies input register.
264 // Returns the condition on which a final split to
265 // true and false label should be made, to optimize fallthrough.
266 Condition EmitTypeofIs(LTypeofIsAndBranch* instr, Register input);
268 // Emits optimized code for %_IsString(x). Preserves input register.
269 // Returns the condition on which a final split to
270 // true and false label should be made, to optimize fallthrough.
271 Condition EmitIsString(Register input,
273 Label* is_not_string,
274 SmiCheck check_needed);
276 // Emits optimized code for %_IsConstructCall().
277 // Caller should branch on equal condition.
278 void EmitIsConstructCall(Register temp);
280 // Emits code for pushing either a tagged constant, a (non-double)
281 // register, or a stack slot operand.
282 void EmitPushTaggedOperand(LOperand* operand);
284 // Emits optimized code to deep-copy the contents of statically known
285 // object graphs (e.g. object literal boilerplate).
286 void EmitDeepCopy(Handle<JSObject> object,
290 AllocationSiteMode mode);
292 void EnsureSpaceForLazyDeopt(int space_needed) override;
293 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
294 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
295 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
296 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
297 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
298 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
301 void EmitVectorLoadICRegisters(T* instr);
303 void EmitVectorStoreICRegisters(T* instr);
306 // On windows, you may not access the stack more than one page below
307 // the most recently mapped page. To make the allocated area randomly
308 // accessible, we write an arbitrary value to each page in range
309 // rsp + offset - page_size .. rsp in turn.
310 void MakeSureStackPagesMapped(int offset);
313 ZoneList<LEnvironment*> deoptimizations_;
314 ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
315 int inlined_function_count_;
317 TranslationBuffer translations_;
318 ZoneList<LDeferredCode*> deferred_;
320 bool frame_is_built_;
322 // Builder that keeps track of safepoints in the code. The table
323 // itself is emitted at the end of the generated code.
324 SafepointTableBuilder safepoints_;
326 // Compiler from a set of parallel moves to a sequential list of moves.
327 LGapResolver resolver_;
329 Safepoint::Kind expected_safepoint_kind_;
331 class PushSafepointRegistersScope final BASE_EMBEDDED {
333 explicit PushSafepointRegistersScope(LCodeGen* codegen)
334 : codegen_(codegen) {
335 DCHECK(codegen_->info()->is_calling());
336 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
337 codegen_->masm_->PushSafepointRegisters();
338 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
341 ~PushSafepointRegistersScope() {
342 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
343 codegen_->masm_->PopSafepointRegisters();
344 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
351 friend class LDeferredCode;
352 friend class LEnvironment;
353 friend class SafepointGenerator;
354 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
358 class LDeferredCode: public ZoneObject {
360 explicit LDeferredCode(LCodeGen* codegen)
362 external_exit_(NULL),
363 instruction_index_(codegen->current_instruction_) {
364 codegen->AddDeferredCode(this);
367 virtual ~LDeferredCode() {}
368 virtual void Generate() = 0;
369 virtual LInstruction* instr() = 0;
371 void SetExit(Label* exit) { external_exit_ = exit; }
372 Label* entry() { return &entry_; }
373 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
374 Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
375 int instruction_index() const { return instruction_index_; }
378 LCodeGen* codegen() const { return codegen_; }
379 MacroAssembler* masm() const { return codegen_->masm(); }
386 Label* external_exit_;
387 int instruction_index_;
390 } } // namespace v8::internal
392 #endif // V8_X64_LITHIUM_CODEGEN_X64_H_