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(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(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(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,
78 Deoptimizer::BailoutType bailout_type);
80 // Generates an architecture-specific, descriptor-specific prologue
81 // to set up a stack frame.
82 void AssemblePrologue();
83 // Generates an architecture-specific, descriptor-specific return sequence
84 // to tear down a stack frame.
85 void AssembleReturn();
87 // ===========================================================================
88 // ============== Architecture-specific gap resolver methods. ================
89 // ===========================================================================
91 // Interface used by the gap resolver to emit moves and swaps.
92 void AssembleMove(InstructionOperand* source,
93 InstructionOperand* destination) FINAL;
94 void AssembleSwap(InstructionOperand* source,
95 InstructionOperand* destination) FINAL;
97 // ===========================================================================
98 // =================== Jump table construction methods. ======================
99 // ===========================================================================
102 // Adds a jump table that is emitted after the actual code. Returns label
103 // pointing to the beginning of the table. {targets} is assumed to be static
104 // or zone allocated.
105 Label* AddJumpTable(Label** targets, size_t target_count);
106 // Emits a jump table.
107 void AssembleJumpTable(Label** targets, size_t target_count);
109 // ===========================================================================
110 // ================== Deoptimization table construction. =====================
111 // ===========================================================================
113 void RecordCallPosition(Instruction* instr);
114 void PopulateDeoptimizationData(Handle<Code> code);
115 int DefineDeoptimizationLiteral(Handle<Object> literal);
116 FrameStateDescriptor* GetFrameStateDescriptor(Instruction* instr,
117 size_t frame_state_offset);
118 int BuildTranslation(Instruction* instr, int pc_offset,
119 size_t frame_state_offset,
120 OutputFrameStateCombine state_combine);
121 void BuildTranslationForFrameStateDescriptor(
122 FrameStateDescriptor* descriptor, Instruction* instr,
123 Translation* translation, size_t frame_state_offset,
124 OutputFrameStateCombine state_combine);
125 void AddTranslationForOperand(Translation* translation, Instruction* instr,
126 InstructionOperand* op, MachineType type);
127 void AddNopForSmiCodeInlining();
128 void EnsureSpaceForLazyDeopt();
129 void MarkLazyDeoptSite();
131 // ===========================================================================
133 struct DeoptimizationState : ZoneObject {
135 BailoutId bailout_id() const { return bailout_id_; }
136 int translation_id() const { return translation_id_; }
137 int pc_offset() const { return pc_offset_; }
139 DeoptimizationState(BailoutId bailout_id, int translation_id, int pc_offset)
140 : bailout_id_(bailout_id),
141 translation_id_(translation_id),
142 pc_offset_(pc_offset) {}
145 BailoutId bailout_id_;
155 friend class OutOfLineCode;
158 Linkage* const linkage_;
159 InstructionSequence* const code_;
160 CompilationInfo* const info_;
161 Label* const labels_;
162 RpoNumber current_block_;
163 SourcePosition current_source_position_;
164 MacroAssembler masm_;
165 GapResolver resolver_;
166 SafepointTableBuilder safepoints_;
167 ZoneVector<HandlerInfo> handlers_;
168 ZoneDeque<DeoptimizationState*> deoptimization_states_;
169 ZoneDeque<Handle<Object>> deoptimization_literals_;
170 TranslationBuffer translations_;
171 int last_lazy_deopt_pc_;
172 JumpTable* jump_tables_;
173 OutOfLineCode* ools_;
177 } // namespace compiler
178 } // namespace internal
181 #endif // V8_COMPILER_CODE_GENERATOR_H