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_IA32_LITHIUM_CODEGEN_IA32_H_
6 #define V8_IA32_LITHIUM_CODEGEN_IA32_H_
8 #include "src/ia32/lithium-ia32.h"
10 #include "src/base/logging.h"
11 #include "src/deoptimizer.h"
12 #include "src/ia32/lithium-gap-resolver-ia32.h"
13 #include "src/lithium-codegen.h"
14 #include "src/safepoint-table.h"
15 #include "src/scopes.h"
16 #include "src/utils.h"
21 // Forward declarations.
24 class SafepointGenerator;
26 class LCodeGen: public LCodeGenBase {
28 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
29 : LCodeGenBase(chunk, assembler, info),
30 deoptimizations_(4, info->zone()),
31 jump_table_(4, info->zone()),
32 inlined_function_count_(0),
33 scope_(info->scope()),
34 translations_(info->zone()),
35 deferred_(8, info->zone()),
36 dynamic_frame_alignment_(false),
37 support_aligned_spilled_doubles_(false),
39 frame_is_built_(false),
40 safepoints_(info->zone()),
42 expected_safepoint_kind_(Safepoint::kSimple) {
43 PopulateDeoptimizationLiteralsWithInlinedFunctions();
46 int LookupDestination(int block_id) const {
47 return chunk()->LookupDestination(block_id);
50 bool IsNextEmittedBlock(int block_id) const {
51 return LookupDestination(block_id) == GetNextEmittedBlock();
54 bool NeedsEagerFrame() const {
55 return GetStackSlotCount() > 0 ||
56 info()->is_non_deferred_calling() ||
58 info()->requires_frame();
60 bool NeedsDeferredFrame() const {
61 return !NeedsEagerFrame() && info()->is_deferred_calling();
64 // Support for converting LOperands to assembler types.
65 Operand ToOperand(LOperand* op) const;
66 Register ToRegister(LOperand* op) const;
67 XMMRegister ToDoubleRegister(LOperand* op) const;
69 bool IsInteger32(LConstantOperand* op) const;
70 bool IsSmi(LConstantOperand* op) const;
71 Immediate ToImmediate(LOperand* op, const Representation& r) const {
72 return Immediate(ToRepresentation(LConstantOperand::cast(op), r));
74 double ToDouble(LConstantOperand* op) const;
76 Handle<Object> ToHandle(LConstantOperand* op) const;
78 // The operand denoting the second word (the one with a higher address) of
79 // a double stack slot.
80 Operand HighOperand(LOperand* op);
82 // Try to generate code for the entire chunk, but it may fail if the
83 // chunk contains constructs we cannot handle. Returns true if the
84 // code generation attempt succeeded.
87 // Finish the code by setting stack height, safepoint, and bailout
89 void FinishCode(Handle<Code> code);
91 // Deferred code support.
92 void DoDeferredNumberTagD(LNumberTagD* instr);
94 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
95 void DoDeferredNumberTagIU(LInstruction* instr,
98 IntegerSignedness signedness);
100 void DoDeferredTaggedToI(LTaggedToI* instr, Label* done);
101 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
102 void DoDeferredStackCheck(LStackCheck* instr);
103 void DoDeferredMaybeGrowElements(LMaybeGrowElements* instr);
104 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
105 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
106 void DoDeferredAllocate(LAllocate* instr);
107 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
108 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
112 // Parallel move support.
113 void DoParallelMove(LParallelMove* move);
114 void DoGap(LGap* instr);
116 // Emit frame translation commands for an environment.
117 void WriteTranslation(LEnvironment* environment, Translation* translation);
119 void EnsureRelocSpaceForDeoptimization();
121 // Declare methods that deal with the individual node types.
122 #define DECLARE_DO(type) void Do##type(L##type* node);
123 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
127 LanguageMode language_mode() const { return info()->language_mode(); }
129 Scope* scope() const { return scope_; }
131 XMMRegister double_scratch0() const { return xmm0; }
133 void EmitClassOfTest(Label* if_true,
135 Handle<String> class_name,
138 Register temporary2);
140 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
142 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
144 void SaveCallerDoubles();
145 void RestoreCallerDoubles();
147 // Code generation passes. Returns true if code generation should
149 void GenerateBodyInstructionPre(LInstruction* instr) override;
150 void GenerateBodyInstructionPost(LInstruction* instr) override;
151 bool GeneratePrologue();
152 bool GenerateDeferredCode();
153 bool GenerateJumpTable();
154 bool GenerateSafepointTable();
156 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
157 void GenerateOsrPrologue();
160 RECORD_SIMPLE_SAFEPOINT,
161 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
164 void CallCode(Handle<Code> code,
165 RelocInfo::Mode mode,
166 LInstruction* instr);
168 void CallCodeGeneric(Handle<Code> code,
169 RelocInfo::Mode mode,
171 SafepointMode safepoint_mode);
173 void CallRuntime(const Runtime::Function* fun,
176 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
178 void CallRuntime(Runtime::FunctionId id,
180 LInstruction* instr) {
181 const Runtime::Function* function = Runtime::FunctionForId(id);
182 CallRuntime(function, argc, instr);
185 void CallRuntimeFromDeferred(Runtime::FunctionId id,
190 void LoadContextFromDeferred(LOperand* context);
192 // Generate a direct call to a known function. Expects the function
194 void CallKnownFunction(Handle<JSFunction> function,
195 int formal_parameter_count, int arity,
196 LInstruction* instr);
198 void RecordSafepointWithLazyDeopt(LInstruction* instr,
199 SafepointMode safepoint_mode);
201 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
202 Safepoint::DeoptMode mode);
203 void DeoptimizeIf(Condition cc, LInstruction* instr,
204 Deoptimizer::DeoptReason deopt_reason,
205 Deoptimizer::BailoutType bailout_type);
206 void DeoptimizeIf(Condition cc, LInstruction* instr,
207 Deoptimizer::DeoptReason deopt_reason);
209 bool DeoptEveryNTimes() {
210 return FLAG_deopt_every_n_times != 0 && !info()->IsStub();
213 void AddToTranslation(LEnvironment* environment,
214 Translation* translation,
218 int* object_index_pointer,
219 int* dematerialized_index_pointer);
220 void PopulateDeoptimizationData(Handle<Code> code);
222 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
224 Register ToRegister(int index) const;
225 XMMRegister ToDoubleRegister(int index) const;
226 int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
227 int32_t ToInteger32(LConstantOperand* op) const;
228 ExternalReference ToExternalReference(LConstantOperand* op) const;
230 Operand BuildFastArrayOperand(LOperand* elements_pointer,
232 Representation key_representation,
233 ElementsKind elements_kind,
234 uint32_t base_offset);
236 Operand BuildSeqStringOperand(Register string,
238 String::Encoding encoding);
240 void EmitIntegerMathAbs(LMathAbs* instr);
242 // Support for recording safepoint and position information.
243 void RecordSafepoint(LPointerMap* pointers,
244 Safepoint::Kind kind,
246 Safepoint::DeoptMode mode);
247 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
248 void RecordSafepoint(Safepoint::DeoptMode mode);
249 void RecordSafepointWithRegisters(LPointerMap* pointers,
251 Safepoint::DeoptMode mode);
253 void RecordAndWritePosition(int position) override;
255 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
256 void EmitGoto(int block);
258 // EmitBranch expects to be the last instruction of a block.
259 template<class InstrType>
260 void EmitBranch(InstrType instr, Condition cc);
261 template <class InstrType>
262 void EmitTrueBranch(InstrType instr, Condition cc);
263 template <class InstrType>
264 void EmitFalseBranch(InstrType instr, Condition cc);
265 void EmitNumberUntagD(LNumberUntagD* instr, Register input, Register temp,
266 XMMRegister result, NumberUntagDMode mode);
268 // Emits optimized code for typeof x == "y". Modifies 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 EmitTypeofIs(LTypeofIsAndBranch* instr, Register input);
273 // Emits optimized code for %_IsString(x). Preserves input register.
274 // Returns the condition on which a final split to
275 // true and false label should be made, to optimize fallthrough.
276 Condition EmitIsString(Register input,
278 Label* is_not_string,
279 SmiCheck check_needed);
281 // Emits optimized code for %_IsConstructCall().
282 // Caller should branch on equal condition.
283 void EmitIsConstructCall(Register temp);
285 // Emits optimized code to deep-copy the contents of statically known
286 // object graphs (e.g. object literal boilerplate).
287 void EmitDeepCopy(Handle<JSObject> object,
291 AllocationSiteMode mode);
293 void EnsureSpaceForLazyDeopt(int space_needed) override;
294 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
295 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
296 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
297 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
298 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
299 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
302 void EmitVectorLoadICRegisters(T* instr);
304 void EmitVectorStoreICRegisters(T* instr);
306 void EmitReturn(LReturn* instr, bool dynamic_frame_alignment);
308 // Emits code for pushing either a tagged constant, a (non-double)
309 // register, or a stack slot operand.
310 void EmitPushTaggedOperand(LOperand* operand);
312 friend class LGapResolver;
315 // On windows, you may not access the stack more than one page below
316 // the most recently mapped page. To make the allocated area randomly
317 // accessible, we write an arbitrary value to each page in range
318 // esp + offset - page_size .. esp in turn.
319 void MakeSureStackPagesMapped(int offset);
322 ZoneList<LEnvironment*> deoptimizations_;
323 ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
324 int inlined_function_count_;
326 TranslationBuffer translations_;
327 ZoneList<LDeferredCode*> deferred_;
328 bool dynamic_frame_alignment_;
329 bool support_aligned_spilled_doubles_;
331 bool frame_is_built_;
333 // Builder that keeps track of safepoints in the code. The table
334 // itself is emitted at the end of the generated code.
335 SafepointTableBuilder safepoints_;
337 // Compiler from a set of parallel moves to a sequential list of moves.
338 LGapResolver resolver_;
340 Safepoint::Kind expected_safepoint_kind_;
342 class PushSafepointRegistersScope final BASE_EMBEDDED {
344 explicit PushSafepointRegistersScope(LCodeGen* codegen)
345 : codegen_(codegen) {
346 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
347 codegen_->masm_->PushSafepointRegisters();
348 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
349 DCHECK(codegen_->info()->is_calling());
352 ~PushSafepointRegistersScope() {
353 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
354 codegen_->masm_->PopSafepointRegisters();
355 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
362 friend class LDeferredCode;
363 friend class LEnvironment;
364 friend class SafepointGenerator;
365 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
369 class LDeferredCode : public ZoneObject {
371 explicit LDeferredCode(LCodeGen* codegen)
373 external_exit_(NULL),
374 instruction_index_(codegen->current_instruction_) {
375 codegen->AddDeferredCode(this);
378 virtual ~LDeferredCode() {}
379 virtual void Generate() = 0;
380 virtual LInstruction* instr() = 0;
382 void SetExit(Label* exit) { external_exit_ = exit; }
383 Label* entry() { return &entry_; }
384 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
385 Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
386 int instruction_index() const { return instruction_index_; }
389 LCodeGen* codegen() const { return codegen_; }
390 MacroAssembler* masm() const { return codegen_->masm(); }
396 Label* external_exit_;
398 int instruction_index_;
401 } // namespace internal
404 #endif // V8_IA32_LITHIUM_CODEGEN_IA32_H_