1 // Copyright 2014 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_COMPILER_CODE_GENERATOR_H_
6 #define V8_COMPILER_CODE_GENERATOR_H_
8 #include "src/compiler/gap-resolver.h"
9 #include "src/compiler/instruction.h"
10 #include "src/deoptimizer.h"
11 #include "src/macro-assembler.h"
12 #include "src/safepoint-table.h"
18 // Forward declarations.
23 FlagsCondition condition;
30 // Generates native code for a sequence of instructions.
31 class CodeGenerator FINAL : public GapResolver::Assembler {
33 explicit CodeGenerator(Frame* frame, Linkage* linkage,
34 InstructionSequence* code, CompilationInfo* info);
36 // Generate native code.
37 Handle<Code> GenerateCode();
39 InstructionSequence* code() const { return code_; }
40 Frame* frame() const { return frame_; }
41 Isolate* isolate() const { return info_->isolate(); }
42 Linkage* linkage() const { return linkage_; }
44 Label* GetLabel(BasicBlock::RpoNumber rpo) { return &labels_[rpo.ToSize()]; }
47 MacroAssembler* masm() { return &masm_; }
48 GapResolver* resolver() { return &resolver_; }
49 SafepointTableBuilder* safepoints() { return &safepoints_; }
50 Zone* zone() const { return code()->zone(); }
51 CompilationInfo* info() const { return info_; }
53 // Checks if {block} will appear directly after {current_block_} when
54 // assembling code, in which case, a fall-through can be used.
55 bool IsNextInAssemblyOrder(BasicBlock::RpoNumber block) const;
57 // Record a safepoint with the given pointer map.
58 void RecordSafepoint(PointerMap* pointers, Safepoint::Kind kind,
59 int arguments, Safepoint::DeoptMode deopt_mode);
61 // Assemble code for the specified instruction.
62 void AssembleInstruction(Instruction* instr);
63 void AssembleSourcePosition(SourcePositionInstruction* instr);
64 void AssembleGap(GapInstruction* gap);
66 // ===========================================================================
67 // ============= Architecture-specific code generation methods. ==============
68 // ===========================================================================
70 void AssembleArchInstruction(Instruction* instr);
71 void AssembleArchJump(BasicBlock::RpoNumber target);
72 void AssembleArchBranch(Instruction* instr, BranchInfo* branch);
73 void AssembleArchBoolean(Instruction* instr, FlagsCondition condition);
74 void AssembleArchLookupSwitch(Instruction* instr);
75 void AssembleArchTableSwitch(Instruction* instr);
77 void AssembleDeoptimizerCall(int deoptimization_id);
79 // Generates an architecture-specific, descriptor-specific prologue
80 // to set up a stack frame.
81 void AssemblePrologue();
82 // Generates an architecture-specific, descriptor-specific return sequence
83 // to tear down a stack frame.
84 void AssembleReturn();
86 // ===========================================================================
87 // ============== Architecture-specific gap resolver methods. ================
88 // ===========================================================================
90 // Interface used by the gap resolver to emit moves and swaps.
91 void AssembleMove(InstructionOperand* source,
92 InstructionOperand* destination) FINAL;
93 void AssembleSwap(InstructionOperand* source,
94 InstructionOperand* destination) FINAL;
96 // ===========================================================================
97 // =================== Jump table construction methods. ======================
98 // ===========================================================================
101 // Adds a jump table that is emitted after the actual code. Returns label
102 // pointing to the beginning of the table. {targets} is assumed to be static
103 // or zone allocated.
104 Label* AddJumpTable(Label** targets, size_t target_count);
105 // Emits a jump table.
106 void AssembleJumpTable(Label** targets, size_t target_count);
108 // ===========================================================================
109 // Deoptimization table construction
110 void AddSafepointAndDeopt(Instruction* instr);
111 void PopulateDeoptimizationData(Handle<Code> code);
112 int DefineDeoptimizationLiteral(Handle<Object> literal);
113 FrameStateDescriptor* GetFrameStateDescriptor(Instruction* instr,
114 size_t frame_state_offset);
115 int BuildTranslation(Instruction* instr, int pc_offset,
116 size_t frame_state_offset,
117 OutputFrameStateCombine state_combine);
118 void BuildTranslationForFrameStateDescriptor(
119 FrameStateDescriptor* descriptor, Instruction* instr,
120 Translation* translation, size_t frame_state_offset,
121 OutputFrameStateCombine state_combine);
122 void AddTranslationForOperand(Translation* translation, Instruction* instr,
123 InstructionOperand* op, MachineType type);
124 void AddNopForSmiCodeInlining();
125 void EnsureSpaceForLazyDeopt();
126 void MarkLazyDeoptSite();
128 // ===========================================================================
129 struct DeoptimizationState : ZoneObject {
131 BailoutId bailout_id() const { return bailout_id_; }
132 int translation_id() const { return translation_id_; }
133 int pc_offset() const { return pc_offset_; }
135 DeoptimizationState(BailoutId bailout_id, int translation_id, int pc_offset)
136 : bailout_id_(bailout_id),
137 translation_id_(translation_id),
138 pc_offset_(pc_offset) {}
141 BailoutId bailout_id_;
146 friend class OutOfLineCode;
149 Linkage* const linkage_;
150 InstructionSequence* const code_;
151 CompilationInfo* const info_;
152 Label* const labels_;
153 BasicBlock::RpoNumber current_block_;
154 SourcePosition current_source_position_;
155 MacroAssembler masm_;
156 GapResolver resolver_;
157 SafepointTableBuilder safepoints_;
158 ZoneDeque<DeoptimizationState*> deoptimization_states_;
159 ZoneDeque<Handle<Object> > deoptimization_literals_;
160 TranslationBuffer translations_;
161 int last_lazy_deopt_pc_;
162 JumpTable* jump_tables_;
163 OutOfLineCode* ools_;
167 } // namespace compiler
168 } // namespace internal
171 #endif // V8_COMPILER_CODE_GENERATOR_H