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_
9 #include "src/x87/lithium-x87.h"
11 #include "src/base/logging.h"
12 #include "src/deoptimizer.h"
13 #include "src/lithium-codegen.h"
14 #include "src/safepoint-table.h"
15 #include "src/scopes.h"
16 #include "src/utils.h"
17 #include "src/x87/lithium-gap-resolver-x87.h"
22 // Forward declarations.
25 class SafepointGenerator;
27 class LCodeGen: public LCodeGenBase {
29 LCodeGen(LChunk* chunk, MacroAssembler* assembler, CompilationInfo* info)
30 : LCodeGenBase(chunk, assembler, info),
31 deoptimizations_(4, info->zone()),
32 jump_table_(4, info->zone()),
33 deoptimization_literals_(8, info->zone()),
34 inlined_function_count_(0),
35 scope_(info->scope()),
36 translations_(info->zone()),
37 deferred_(8, info->zone()),
38 dynamic_frame_alignment_(false),
39 support_aligned_spilled_doubles_(false),
41 frame_is_built_(false),
42 x87_stack_(assembler),
43 safepoints_(info->zone()),
45 expected_safepoint_kind_(Safepoint::kSimple) {
46 PopulateDeoptimizationLiteralsWithInlinedFunctions();
49 int LookupDestination(int block_id) const {
50 return chunk()->LookupDestination(block_id);
53 bool IsNextEmittedBlock(int block_id) const {
54 return LookupDestination(block_id) == GetNextEmittedBlock();
57 bool NeedsEagerFrame() const {
58 return GetStackSlotCount() > 0 ||
59 info()->is_non_deferred_calling() ||
61 info()->requires_frame();
63 bool NeedsDeferredFrame() const {
64 return !NeedsEagerFrame() && info()->is_deferred_calling();
67 // Support for converting LOperands to assembler types.
68 Operand ToOperand(LOperand* op) const;
69 Register ToRegister(LOperand* op) const;
70 X87Register ToX87Register(LOperand* op) const;
72 bool IsInteger32(LConstantOperand* op) const;
73 bool IsSmi(LConstantOperand* op) const;
74 Immediate ToImmediate(LOperand* op, const Representation& r) const {
75 return Immediate(ToRepresentation(LConstantOperand::cast(op), r));
77 double ToDouble(LConstantOperand* op) const;
79 // Support for non-sse2 (x87) floating point stack handling.
80 // These functions maintain the mapping of physical stack registers to our
81 // virtual registers between instructions.
82 enum X87OperandType { kX87DoubleOperand, kX87FloatOperand, kX87IntOperand };
84 void X87Mov(X87Register reg, Operand src,
85 X87OperandType operand = kX87DoubleOperand);
86 void X87Mov(Operand src, X87Register reg,
87 X87OperandType operand = kX87DoubleOperand);
88 void X87Mov(X87Register reg, X87Register src,
89 X87OperandType operand = kX87DoubleOperand);
91 void X87PrepareBinaryOp(
92 X87Register left, X87Register right, X87Register result);
94 void X87LoadForUsage(X87Register reg);
95 void X87LoadForUsage(X87Register reg1, X87Register reg2);
96 void X87PrepareToWrite(X87Register reg) { x87_stack_.PrepareToWrite(reg); }
97 void X87CommitWrite(X87Register reg) { x87_stack_.CommitWrite(reg); }
99 void X87Fxch(X87Register reg, int other_slot = 0) {
100 x87_stack_.Fxch(reg, other_slot);
102 void X87Free(X87Register reg) {
103 x87_stack_.Free(reg);
107 bool X87StackEmpty() {
108 return x87_stack_.depth() == 0;
111 Handle<Object> ToHandle(LConstantOperand* op) const;
113 // The operand denoting the second word (the one with a higher address) of
114 // a double stack slot.
115 Operand HighOperand(LOperand* op);
117 // Try to generate code for the entire chunk, but it may fail if the
118 // chunk contains constructs we cannot handle. Returns true if the
119 // code generation attempt succeeded.
122 // Finish the code by setting stack height, safepoint, and bailout
123 // information on it.
124 void FinishCode(Handle<Code> code);
126 // Deferred code support.
127 void DoDeferredNumberTagD(LNumberTagD* instr);
129 enum IntegerSignedness { SIGNED_INT32, UNSIGNED_INT32 };
130 void DoDeferredNumberTagIU(LInstruction* instr,
133 IntegerSignedness signedness);
135 void DoDeferredTaggedToI(LTaggedToI* instr, Label* done);
136 void DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr);
137 void DoDeferredStackCheck(LStackCheck* instr);
138 void DoDeferredStringCharCodeAt(LStringCharCodeAt* instr);
139 void DoDeferredStringCharFromCode(LStringCharFromCode* instr);
140 void DoDeferredAllocate(LAllocate* instr);
141 void DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
143 void DoDeferredInstanceMigration(LCheckMaps* instr, Register object);
144 void DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
148 // Parallel move support.
149 void DoParallelMove(LParallelMove* move);
150 void DoGap(LGap* instr);
152 // Emit frame translation commands for an environment.
153 void WriteTranslation(LEnvironment* environment, Translation* translation);
155 void EnsureRelocSpaceForDeoptimization();
157 // Declare methods that deal with the individual node types.
158 #define DECLARE_DO(type) void Do##type(L##type* node);
159 LITHIUM_CONCRETE_INSTRUCTION_LIST(DECLARE_DO)
163 StrictMode strict_mode() const { return info()->strict_mode(); }
165 Scope* scope() const { return scope_; }
167 void EmitClassOfTest(Label* if_true,
169 Handle<String> class_name,
172 Register temporary2);
174 int GetStackSlotCount() const { return chunk()->spill_slot_count(); }
176 void AddDeferredCode(LDeferredCode* code) { deferred_.Add(code, zone()); }
178 // Code generation passes. Returns true if code generation should
180 void GenerateBodyInstructionPre(LInstruction* instr) OVERRIDE;
181 void GenerateBodyInstructionPost(LInstruction* instr) OVERRIDE;
182 bool GeneratePrologue();
183 bool GenerateDeferredCode();
184 bool GenerateJumpTable();
185 bool GenerateSafepointTable();
187 // Generates the custom OSR entrypoint and sets the osr_pc_offset.
188 void GenerateOsrPrologue();
191 RECORD_SIMPLE_SAFEPOINT,
192 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS
195 void CallCode(Handle<Code> code,
196 RelocInfo::Mode mode,
197 LInstruction* instr);
199 void CallCodeGeneric(Handle<Code> code,
200 RelocInfo::Mode mode,
202 SafepointMode safepoint_mode);
204 void CallRuntime(const Runtime::Function* fun, int argc, LInstruction* instr,
205 SaveFPRegsMode save_doubles = kDontSaveFPRegs);
207 void CallRuntime(Runtime::FunctionId id,
209 LInstruction* instr) {
210 const Runtime::Function* function = Runtime::FunctionForId(id);
211 CallRuntime(function, argc, instr);
214 void CallRuntimeFromDeferred(Runtime::FunctionId id,
219 void LoadContextFromDeferred(LOperand* context);
226 // Generate a direct call to a known function. Expects the function
228 void CallKnownFunction(Handle<JSFunction> function,
229 int formal_parameter_count,
234 void RecordSafepointWithLazyDeopt(LInstruction* instr,
235 SafepointMode safepoint_mode);
237 void RegisterEnvironmentForDeoptimization(LEnvironment* environment,
238 Safepoint::DeoptMode mode);
239 void DeoptimizeIf(Condition cc, LInstruction* instr, const char* detail,
240 Deoptimizer::BailoutType bailout_type);
241 void DeoptimizeIf(Condition cc, LInstruction* instr,
242 const char* detail = NULL);
244 bool DeoptEveryNTimes() {
245 return FLAG_deopt_every_n_times != 0 && !info()->IsStub();
248 void AddToTranslation(LEnvironment* environment,
249 Translation* translation,
253 int* object_index_pointer,
254 int* dematerialized_index_pointer);
255 void PopulateDeoptimizationData(Handle<Code> code);
256 int DefineDeoptimizationLiteral(Handle<Object> literal);
258 void PopulateDeoptimizationLiteralsWithInlinedFunctions();
260 Register ToRegister(int index) const;
261 X87Register ToX87Register(int index) const;
262 int32_t ToRepresentation(LConstantOperand* op, const Representation& r) const;
263 int32_t ToInteger32(LConstantOperand* op) const;
264 ExternalReference ToExternalReference(LConstantOperand* op) const;
266 Operand BuildFastArrayOperand(LOperand* elements_pointer,
268 Representation key_representation,
269 ElementsKind elements_kind,
270 uint32_t base_offset);
272 Operand BuildSeqStringOperand(Register string,
274 String::Encoding encoding);
276 void EmitIntegerMathAbs(LMathAbs* instr);
278 // Support for recording safepoint and position information.
279 void RecordSafepoint(LPointerMap* pointers,
280 Safepoint::Kind kind,
282 Safepoint::DeoptMode mode);
283 void RecordSafepoint(LPointerMap* pointers, Safepoint::DeoptMode mode);
284 void RecordSafepoint(Safepoint::DeoptMode mode);
285 void RecordSafepointWithRegisters(LPointerMap* pointers,
287 Safepoint::DeoptMode mode);
289 void RecordAndWritePosition(int position) OVERRIDE;
291 static Condition TokenToCondition(Token::Value op, bool is_unsigned);
292 void EmitGoto(int block);
294 // EmitBranch expects to be the last instruction of a block.
295 template<class InstrType>
296 void EmitBranch(InstrType instr, Condition cc);
297 template<class InstrType>
298 void EmitFalseBranch(InstrType instr, Condition cc);
299 void EmitNumberUntagDNoSSE2(LNumberUntagD* instr, Register input,
300 Register temp, X87Register res_reg,
301 NumberUntagDMode mode);
303 // Emits optimized code for typeof x == "y". Modifies input register.
304 // Returns the condition on which a final split to
305 // true and false label should be made, to optimize fallthrough.
306 Condition EmitTypeofIs(LTypeofIsAndBranch* instr, Register input);
308 // Emits optimized code for %_IsObject(x). Preserves input register.
309 // Returns the condition on which a final split to
310 // true and false label should be made, to optimize fallthrough.
311 Condition EmitIsObject(Register input,
313 Label* is_not_object,
316 // Emits optimized code for %_IsString(x). Preserves input register.
317 // Returns the condition on which a final split to
318 // true and false label should be made, to optimize fallthrough.
319 Condition EmitIsString(Register input,
321 Label* is_not_string,
322 SmiCheck check_needed);
324 // Emits optimized code for %_IsConstructCall().
325 // Caller should branch on equal condition.
326 void EmitIsConstructCall(Register temp);
328 // Emits optimized code to deep-copy the contents of statically known
329 // object graphs (e.g. object literal boilerplate).
330 void EmitDeepCopy(Handle<JSObject> object,
334 AllocationSiteMode mode);
336 void EnsureSpaceForLazyDeopt(int space_needed) OVERRIDE;
337 void DoLoadKeyedExternalArray(LLoadKeyed* instr);
338 void DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr);
339 void DoLoadKeyedFixedArray(LLoadKeyed* instr);
340 void DoStoreKeyedExternalArray(LStoreKeyed* instr);
341 void DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr);
342 void DoStoreKeyedFixedArray(LStoreKeyed* instr);
345 void EmitVectorLoadICRegisters(T* instr);
347 void EmitReturn(LReturn* instr, bool dynamic_frame_alignment);
349 // Emits code for pushing either a tagged constant, a (non-double)
350 // register, or a stack slot operand.
351 void EmitPushTaggedOperand(LOperand* operand);
353 void X87Fld(Operand src, X87OperandType opts);
355 void EmitFlushX87ForDeopt();
356 void FlushX87StackIfNecessary(LInstruction* instr) {
357 x87_stack_.FlushIfNecessary(instr, this);
359 friend class LGapResolver;
362 // On windows, you may not access the stack more than one page below
363 // the most recently mapped page. To make the allocated area randomly
364 // accessible, we write an arbitrary value to each page in range
365 // esp + offset - page_size .. esp in turn.
366 void MakeSureStackPagesMapped(int offset);
369 ZoneList<LEnvironment*> deoptimizations_;
370 ZoneList<Deoptimizer::JumpTableEntry> jump_table_;
371 ZoneList<Handle<Object> > deoptimization_literals_;
372 int inlined_function_count_;
374 TranslationBuffer translations_;
375 ZoneList<LDeferredCode*> deferred_;
376 bool dynamic_frame_alignment_;
377 bool support_aligned_spilled_doubles_;
379 bool frame_is_built_;
381 class X87Stack : public ZoneObject {
383 explicit X87Stack(MacroAssembler* masm)
384 : stack_depth_(0), is_mutable_(true), masm_(masm) { }
385 explicit X87Stack(const X87Stack& other)
386 : stack_depth_(other.stack_depth_), is_mutable_(false), masm_(masm()) {
387 for (int i = 0; i < stack_depth_; i++) {
388 stack_[i] = other.stack_[i];
391 bool operator==(const X87Stack& other) const {
392 if (stack_depth_ != other.stack_depth_) return false;
393 for (int i = 0; i < stack_depth_; i++) {
394 if (!stack_[i].is(other.stack_[i])) return false;
398 X87Stack& operator=(const X87Stack& other) {
399 stack_depth_ = other.stack_depth_;
400 for (int i = 0; i < stack_depth_; i++) {
401 stack_[i] = other.stack_[i];
405 bool Contains(X87Register reg);
406 void Fxch(X87Register reg, int other_slot = 0);
407 void Free(X87Register reg);
408 void PrepareToWrite(X87Register reg);
409 void CommitWrite(X87Register reg);
410 void FlushIfNecessary(LInstruction* instr, LCodeGen* cgen);
411 void LeavingBlock(int current_block_id, LGoto* goto_instr, LCodeGen* cgen);
412 int depth() const { return stack_depth_; }
414 int st(X87Register reg) { return st2idx(ArrayIndex(reg)); }
419 void push(X87Register reg) {
421 DCHECK(stack_depth_ < X87Register::kMaxNumAllocatableRegisters);
422 stack_[stack_depth_] = reg;
426 MacroAssembler* masm() const { return masm_; }
427 Isolate* isolate() const { return masm_->isolate(); }
430 int ArrayIndex(X87Register reg);
433 X87Register stack_[X87Register::kMaxNumAllocatableRegisters];
436 MacroAssembler* masm_;
439 // block_id -> X87Stack*;
440 typedef std::map<int, X87Stack*> X87StackMap;
441 X87StackMap x87_stack_map_;
443 // Builder that keeps track of safepoints in the code. The table
444 // itself is emitted at the end of the generated code.
445 SafepointTableBuilder safepoints_;
447 // Compiler from a set of parallel moves to a sequential list of moves.
448 LGapResolver resolver_;
450 Safepoint::Kind expected_safepoint_kind_;
452 class PushSafepointRegistersScope FINAL BASE_EMBEDDED {
454 explicit PushSafepointRegistersScope(LCodeGen* codegen)
455 : codegen_(codegen) {
456 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
457 codegen_->masm_->PushSafepointRegisters();
458 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
459 DCHECK(codegen_->info()->is_calling());
462 ~PushSafepointRegistersScope() {
463 DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
464 codegen_->masm_->PopSafepointRegisters();
465 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
472 friend class LDeferredCode;
473 friend class LEnvironment;
474 friend class SafepointGenerator;
475 friend class X87Stack;
476 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
480 class LDeferredCode : public ZoneObject {
482 explicit LDeferredCode(LCodeGen* codegen, const LCodeGen::X87Stack& x87_stack)
484 external_exit_(NULL),
485 instruction_index_(codegen->current_instruction_),
486 x87_stack_(x87_stack) {
487 codegen->AddDeferredCode(this);
490 virtual ~LDeferredCode() {}
491 virtual void Generate() = 0;
492 virtual LInstruction* instr() = 0;
494 void SetExit(Label* exit) { external_exit_ = exit; }
495 Label* entry() { return &entry_; }
496 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
497 Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
498 int instruction_index() const { return instruction_index_; }
499 const LCodeGen::X87Stack& x87_stack() const { return x87_stack_; }
502 LCodeGen* codegen() const { return codegen_; }
503 MacroAssembler* masm() const { return codegen_->masm(); }
509 Label* external_exit_;
511 int instruction_index_;
512 LCodeGen::X87Stack x87_stack_;
515 } } // namespace v8::internal
517 #endif // V8_X87_LITHIUM_CODEGEN_X87_H_