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/checks.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);
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_;
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 bool Contains(X87Register reg);
399 void Fxch(X87Register reg, int other_slot = 0);
400 void Free(X87Register reg);
401 void PrepareToWrite(X87Register reg);
402 void CommitWrite(X87Register reg);
403 void FlushIfNecessary(LInstruction* instr, LCodeGen* cgen);
404 void LeavingBlock(int current_block_id, LGoto* goto_instr);
405 int depth() const { return stack_depth_; }
410 void push(X87Register reg) {
412 ASSERT(stack_depth_ < X87Register::kMaxNumAllocatableRegisters);
413 stack_[stack_depth_] = reg;
417 MacroAssembler* masm() const { return masm_; }
418 Isolate* isolate() const { return masm_->isolate(); }
421 int ArrayIndex(X87Register reg);
424 X87Register stack_[X87Register::kMaxNumAllocatableRegisters];
427 MacroAssembler* masm_;
431 // Builder that keeps track of safepoints in the code. The table
432 // itself is emitted at the end of the generated code.
433 SafepointTableBuilder safepoints_;
435 // Compiler from a set of parallel moves to a sequential list of moves.
436 LGapResolver resolver_;
438 Safepoint::Kind expected_safepoint_kind_;
440 class PushSafepointRegistersScope V8_FINAL BASE_EMBEDDED {
442 explicit PushSafepointRegistersScope(LCodeGen* codegen)
443 : codegen_(codegen) {
444 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
445 codegen_->masm_->PushSafepointRegisters();
446 codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
447 ASSERT(codegen_->info()->is_calling());
450 ~PushSafepointRegistersScope() {
451 ASSERT(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
452 codegen_->masm_->PopSafepointRegisters();
453 codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
460 friend class LDeferredCode;
461 friend class LEnvironment;
462 friend class SafepointGenerator;
463 DISALLOW_COPY_AND_ASSIGN(LCodeGen);
467 class LDeferredCode : public ZoneObject {
469 explicit LDeferredCode(LCodeGen* codegen, const LCodeGen::X87Stack& x87_stack)
471 external_exit_(NULL),
472 instruction_index_(codegen->current_instruction_),
473 x87_stack_(x87_stack) {
474 codegen->AddDeferredCode(this);
477 virtual ~LDeferredCode() {}
478 virtual void Generate() = 0;
479 virtual LInstruction* instr() = 0;
481 void SetExit(Label* exit) { external_exit_ = exit; }
482 Label* entry() { return &entry_; }
483 Label* exit() { return external_exit_ != NULL ? external_exit_ : &exit_; }
484 Label* done() { return codegen_->NeedsDeferredFrame() ? &done_ : exit(); }
485 int instruction_index() const { return instruction_index_; }
486 const LCodeGen::X87Stack& x87_stack() const { return x87_stack_; }
489 LCodeGen* codegen() const { return codegen_; }
490 MacroAssembler* masm() const { return codegen_->masm(); }
496 Label* external_exit_;
498 int instruction_index_;
499 LCodeGen::X87Stack x87_stack_;
502 } } // namespace v8::internal
504 #endif // V8_X87_LITHIUM_CODEGEN_X87_H_