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_X87_LITHIUM_CODEGEN_X87_H_
6 #define V8_X87_LITHIUM_CODEGEN_X87_H_
8 #include "src/x87/lithium-x87.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/x87/lithium-gap-resolver-x87.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 deoptimization_literals_(8, info->zone()),
33 inlined_function_count_(0),
34 scope_(info->scope()),
35 translations_(info->zone()),
36 deferred_(8, info->zone()),
37 dynamic_frame_alignment_(false),
38 support_aligned_spilled_doubles_(false),
40 frame_is_built_(false),
41 x87_stack_(assembler),
42 safepoints_(info->zone()),
44 expected_safepoint_kind_(Safepoint::kSimple) {
45 PopulateDeoptimizationLiteralsWithInlinedFunctions();
48 int LookupDestination(int block_id) const {
49 return chunk()->LookupDestination(block_id);
52 bool IsNextEmittedBlock(int block_id) const {
53 return LookupDestination(block_id) == GetNextEmittedBlock();
56 bool NeedsEagerFrame() const {
57 return GetStackSlotCount() > 0 ||
58 info()->is_non_deferred_calling() ||
60 info()->requires_frame();
62 bool NeedsDeferredFrame() const {
63 return !NeedsEagerFrame() && info()->is_deferred_calling();
66 // Support for converting LOperands to assembler types.
67 Operand ToOperand(LOperand* op) const;
68 Register ToRegister(LOperand* op) const;
69 X87Register ToX87Register(LOperand* op) const;
71 bool IsInteger32(LConstantOperand* op) const;
72 bool IsSmi(LConstantOperand* op) const;
73 Immediate ToImmediate(LOperand* op, const Representation& r) const {
74 return Immediate(ToRepresentation(LConstantOperand::cast(op), r));
76 double ToDouble(LConstantOperand* op) const;
78 // Support for non-sse2 (x87) floating point stack handling.
79 // These functions maintain the mapping of physical stack registers to our
80 // virtual registers between instructions.
81 enum X87OperandType { kX87DoubleOperand, kX87FloatOperand, kX87IntOperand };
83 void X87Mov(X87Register reg, Operand src,
84 X87OperandType operand = kX87DoubleOperand);
85 void X87Mov(Operand src, X87Register reg,
86 X87OperandType operand = kX87DoubleOperand);
88 void X87PrepareBinaryOp(
89 X87Register left, X87Register right, X87Register result);
91 void X87LoadForUsage(X87Register reg);
92 void X87LoadForUsage(X87Register reg1, X87Register reg2);
93 void X87PrepareToWrite(X87Register reg) { x87_stack_.PrepareToWrite(reg); }
94 void X87CommitWrite(X87Register reg) { x87_stack_.CommitWrite(reg); }
96 void X87Fxch(X87Register reg, int other_slot = 0) {
97 x87_stack_.Fxch(reg, other_slot);
99 void X87Free(X87Register reg) {
100 x87_stack_.Free(reg);
104 bool X87StackEmpty() {
105 return x87_stack_.depth() == 0;
108 Handle<Object> ToHandle(LConstantOperand* op) const;
110 // The operand denoting the second word (the one with a higher address) of
111 // a double stack slot.
112 Operand HighOperand(LOperand* op);
114 // Try to generate code for the entire chunk, but it may fail if the
115 // chunk contains constructs we cannot handle. Returns true if the
116 // code generation attempt succeeded.
119 // Finish the code by setting stack height, safepoint, and bailout
120 // information on it.
121 void FinishCode(Handle<Code> code);
123 // Deferred code support.
124 void DoDeferredNumberTagD(LNumberTagD* instr);
126 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
127 void DoDeferredNumberTagIU(LInstruction* instr,
130 IntegerSignedness signedness);
132 void DoDeferredTaggedToI(LTaggedToI* instr, Label* done);
133 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
134 void DoDeferredStackCheck(LStackCheck* instr);
135 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
136 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
137 void DoDeferredAllocate(LAllocate* instr);
138 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
140 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
141 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
145 // Parallel move support.
146 void DoParallelMove(LParallelMove* move);
147 void DoGap(LGap* instr);
149 // Emit frame translation commands for an environment.
150 void WriteTranslation(LEnvironment* environment, Translation* translation);
152 void EnsureRelocSpaceForDeoptimization();
154 // Declare methods that deal with the individual node types.
155 #define DECLARE_DO(type) void Do##type(L##type* node);
156 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
160 StrictMode strict_mode() const { return info()->strict_mode(); }
162 Scope* scope() const { return scope_; }
164 void EmitClassOfTest(Label* if_true,
166 Handle<String> class_name,
169 Register temporary2);
171 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
173 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
175 // Code generation passes. Returns true if code generation should
177 void GenerateBodyInstructionPre(LInstruction* instr) V8_OVERRIDE;
178 void GenerateBodyInstructionPost(LInstruction* instr) V8_OVERRIDE;
179 bool GeneratePrologue();
180 bool GenerateDeferredCode();
181 bool GenerateJumpTable();
182 bool GenerateSafepointTable();
184 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
185 void GenerateOsrPrologue();
188 RECORD_SIMPLE_SAFEPOINT,
189 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
192 void CallCode(Handle<Code> code,
193 RelocInfo::Mode mode,
194 LInstruction* instr);
196 void CallCodeGeneric(Handle<Code> code,
197 RelocInfo::Mode mode,
199 SafepointMode safepoint_mode);
201 void CallRuntime(const Runtime::Function* fun,
203 LInstruction* instr);
205 void CallRuntime(Runtime::FunctionId id,
207 LInstruction* instr) {
208 const Runtime::Function* function = Runtime::FunctionForId(id);
209 CallRuntime(function, argc, instr);
212 void CallRuntimeFromDeferred(Runtime::FunctionId id,
217 void LoadContextFromDeferred(LOperand* context);
224 // Generate a direct call to a known function. Expects the function
226 void CallKnownFunction(Handle<JSFunction> function,
227 int formal_parameter_count,
232 void RecordSafepointWithLazyDeopt(LInstruction* instr,
233 SafepointMode safepoint_mode);
235 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
236 Safepoint::DeoptMode mode);
237 void DeoptimizeIf(Condition cc,
238 LEnvironment* environment,
239 Deoptimizer::BailoutType bailout_type);
240 void DeoptimizeIf(Condition cc, LEnvironment* environment);
242 bool DeoptEveryNTimes() {
243 return FLAG_deopt_every_n_times != 0 && !info()->IsStub();
246 void AddToTranslation(LEnvironment* environment,
247 Translation* translation,
251 int* object_index_pointer,
252 int* dematerialized_index_pointer);
253 void PopulateDeoptimizationData(Handle<Code> code);
254 int DefineDeoptimizationLiteral(Handle<Object> literal);
256 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
258 Register ToRegister(int index) const;
259 X87Register ToX87Register(int index) const;
260 int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
261 int32_t ToInteger32(LConstantOperand* op) const;
262 ExternalReference ToExternalReference(LConstantOperand* op) const;
264 Operand BuildFastArrayOperand(LOperand* elements_pointer,
266 Representation key_representation,
267 ElementsKind elements_kind,
268 uint32_t base_offset);
270 Operand BuildSeqStringOperand(Register string,
272 String::Encoding encoding);
274 void EmitIntegerMathAbs(LMathAbs* instr);
276 // Support for recording safepoint and position information.
277 void RecordSafepoint(LPointerMap* pointers,
278 Safepoint::Kind kind,
280 Safepoint::DeoptMode mode);
281 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
282 void RecordSafepoint(Safepoint::DeoptMode mode);
283 void RecordSafepointWithRegisters(LPointerMap* pointers,
285 Safepoint::DeoptMode mode);
287 void RecordAndWritePosition(int position) V8_OVERRIDE;
289 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
290 void EmitGoto(int block);
292 // EmitBranch expects to be the last instruction of a block.
293 template<class InstrType>
294 void EmitBranch(InstrType instr, Condition cc);
295 template<class InstrType>
296 void EmitFalseBranch(InstrType instr, Condition cc);
297 void EmitNumberUntagDNoSSE2(
301 bool allow_undefined_as_nan,
302 bool deoptimize_on_minus_zero,
304 NumberUntagDMode mode = NUMBER_CANDIDATE_IS_ANY_TAGGED);
306 // Emits optimized code for typeof x == "y". Modifies input register.
307 // Returns the condition on which a final split to
308 // true and false label should be made, to optimize fallthrough.
309 Condition EmitTypeofIs(LTypeofIsAndBranch* instr, Register input);
311 // Emits optimized code for %_IsObject(x). Preserves input register.
312 // Returns the condition on which a final split to
313 // true and false label should be made, to optimize fallthrough.
314 Condition EmitIsObject(Register input,
316 Label* is_not_object,
319 // Emits optimized code for %_IsString(x). Preserves input register.
320 // Returns the condition on which a final split to
321 // true and false label should be made, to optimize fallthrough.
322 Condition EmitIsString(Register input,
324 Label* is_not_string,
325 SmiCheck check_needed);
327 // Emits optimized code for %_IsConstructCall().
328 // Caller should branch on equal condition.
329 void EmitIsConstructCall(Register temp);
331 // Emits optimized code to deep-copy the contents of statically known
332 // object graphs (e.g. object literal boilerplate).
333 void EmitDeepCopy(Handle<JSObject> object,
337 AllocationSiteMode mode);
339 void EnsureSpaceForLazyDeopt(int space_needed) V8_OVERRIDE;
340 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
341 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
342 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
343 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
344 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
345 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
348 void EmitVectorLoadICRegisters(T* instr);
350 void EmitReturn(LReturn* instr, bool dynamic_frame_alignment);
352 // Emits code for pushing either a tagged constant, a (non-double)
353 // register, or a stack slot operand.
354 void EmitPushTaggedOperand(LOperand* operand);
356 void X87Fld(Operand src, X87OperandType opts);
358 void EmitFlushX87ForDeopt();
359 void FlushX87StackIfNecessary(LInstruction* instr) {
360 x87_stack_.FlushIfNecessary(instr, this);
362 friend class LGapResolver;
365 // On windows, you may not access the stack more than one page below
366 // the most recently mapped page. To make the allocated area randomly
367 // accessible, we write an arbitrary value to each page in range
368 // esp + offset - page_size .. esp in turn.
369 void MakeSureStackPagesMapped(int offset);
372 ZoneList<LEnvironment*> deoptimizations_;
373 ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
374 ZoneList<Handle<Object> > deoptimization_literals_;
375 int inlined_function_count_;
377 TranslationBuffer translations_;
378 ZoneList<LDeferredCode*> deferred_;
379 bool dynamic_frame_alignment_;
380 bool support_aligned_spilled_doubles_;
382 bool frame_is_built_;
386 explicit X87Stack(MacroAssembler* masm)
387 : stack_depth_(0), is_mutable_(true), masm_(masm) { }
388 explicit X87Stack(const X87Stack& other)
389 : stack_depth_(other.stack_depth_), is_mutable_(false), masm_(masm()) {
390 for (int i = 0; i < stack_depth_; i++) {
391 stack_[i] = other.stack_[i];
394 bool operator==(const X87Stack& other) const {
395 if (stack_depth_ != other.stack_depth_) return false;
396 for (int i = 0; i < stack_depth_; i++) {
397 if (!stack_[i].is(other.stack_[i])) return false;
401 bool Contains(X87Register reg);
402 void Fxch(X87Register reg, int other_slot = 0);
403 void Free(X87Register reg);
404 void PrepareToWrite(X87Register reg);
405 void CommitWrite(X87Register reg);
406 void FlushIfNecessary(LInstruction* instr, LCodeGen* cgen);
407 void LeavingBlock(int current_block_id, LGoto* goto_instr);
408 int depth() const { return stack_depth_; }
413 void push(X87Register reg) {
415 DCHECK(stack_depth_ < X87Register::kMaxNumAllocatableRegisters);
416 stack_[stack_depth_] = reg;
420 MacroAssembler* masm() const { return masm_; }
421 Isolate* isolate() const { return masm_->isolate(); }
424 int ArrayIndex(X87Register reg);
427 X87Register stack_[X87Register::kMaxNumAllocatableRegisters];
430 MacroAssembler* masm_;
434 // Builder that keeps track of safepoints in the code. The table
435 // itself is emitted at the end of the generated code.
436 SafepointTableBuilder safepoints_;
438 // Compiler from a set of parallel moves to a sequential list of moves.
439 LGapResolver resolver_;
441 Safepoint::Kind expected_safepoint_kind_;
443 class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
445 explicit PushSafepointRegistersScope(LCodeGen* codegen)
446 : codegen_(codegen) {
447 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
448 codegen_->masm_->PushSafepointRegisters();
449 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
450 DCHECK(codegen_->info()->is_calling());
453 ~PushSafepointRegistersScope() {
454 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
455 codegen_->masm_->PopSafepointRegisters();
456 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
463 friend class LDeferredCode;
464 friend class LEnvironment;
465 friend class SafepointGenerator;
466 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
470 class LDeferredCode : public ZoneObject {
472 explicit LDeferredCode(LCodeGen* codegen, const LCodeGen::X87Stack& x87_stack)
474 external_exit_(NULL),
475 instruction_index_(codegen->current_instruction_),
476 x87_stack_(x87_stack) {
477 codegen->AddDeferredCode(this);
480 virtual ~LDeferredCode() {}
481 virtual void Generate() = 0;
482 virtual LInstruction* instr() = 0;
484 void SetExit(Label* exit) { external_exit_ = exit; }
485 Label* entry() { return &entry_; }
486 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
487 Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
488 int instruction_index() const { return instruction_index_; }
489 const LCodeGen::X87Stack& x87_stack() const { return x87_stack_; }
492 LCodeGen* codegen() const { return codegen_; }
493 MacroAssembler* masm() const { return codegen_->masm(); }
499 Label* external_exit_;
501 int instruction_index_;
502 LCodeGen::X87Stack x87_stack_;
505 } } // namespace v8::internal
507 #endif // V8_X87_LITHIUM_CODEGEN_X87_H_