blob: 5f35e8a36ec5da8f9e5a81b0c3e49ed30461630a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
4
5#ifndef V8_COMPILER_CODE_GENERATOR_H_
6#define V8_COMPILER_CODE_GENERATOR_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#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"
13
14namespace v8 {
15namespace internal {
16namespace compiler {
17
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018// Forward declarations.
Ben Murdochda12d292016-06-02 14:46:10 +010019class DeoptimizationExit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020class FrameAccessState;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040021class Linkage;
22class OutOfLineCode;
23
24struct BranchInfo {
25 FlagsCondition condition;
26 Label* true_label;
27 Label* false_label;
28 bool fallthru;
29};
30
31
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032class InstructionOperandIterator {
33 public:
34 InstructionOperandIterator(Instruction* instr, size_t pos)
35 : instr_(instr), pos_(pos) {}
36
37 Instruction* instruction() const { return instr_; }
38 InstructionOperand* Advance() { return instr_->InputAt(pos_++); }
39
40 private:
41 Instruction* instr_;
42 size_t pos_;
43};
44
45
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046// Generates native code for a sequence of instructions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047class CodeGenerator final : public GapResolver::Assembler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 explicit CodeGenerator(Frame* frame, Linkage* linkage,
50 InstructionSequence* code, CompilationInfo* info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051
52 // Generate native code.
53 Handle<Code> GenerateCode();
54
55 InstructionSequence* code() const { return code_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 FrameAccessState* frame_access_state() const { return frame_access_state_; }
Ben Murdochc5610432016-08-08 18:44:38 +010057 const Frame* frame() const { return frame_access_state_->frame(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 Isolate* isolate() const { return info_->isolate(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 Linkage* linkage() const { return linkage_; }
60
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 Label* GetLabel(RpoNumber rpo) { return &labels_[rpo.ToSize()]; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062
63 private:
64 MacroAssembler* masm() { return &masm_; }
65 GapResolver* resolver() { return &resolver_; }
66 SafepointTableBuilder* safepoints() { return &safepoints_; }
67 Zone* zone() const { return code()->zone(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068 CompilationInfo* info() const { return info_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069
Ben Murdochc5610432016-08-08 18:44:38 +010070 // Create the FrameAccessState object. The Frame is immutable from here on.
71 void CreateFrameAccessState(Frame* frame);
72
73 // Architecture - specific frame finalization.
74 void FinishFrame(Frame* frame);
75
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 // Checks if {block} will appear directly after {current_block_} when
77 // assembling code, in which case, a fall-through can be used.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 bool IsNextInAssemblyOrder(RpoNumber block) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079
80 // Record a safepoint with the given pointer map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 void RecordSafepoint(ReferenceMap* references, Safepoint::Kind kind,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 int arguments, Safepoint::DeoptMode deopt_mode);
83
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 // Check if a heap object can be materialized by loading from the frame, which
85 // is usually way cheaper than materializing the actual heap object constant.
Ben Murdochda12d292016-06-02 14:46:10 +010086 bool IsMaterializableFromFrame(Handle<HeapObject> object, int* slot_return);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 // Check if a heap object can be materialized by loading from a heap root,
88 // which is cheaper on some platforms than materializing the actual heap
89 // object constant.
90 bool IsMaterializableFromRoot(Handle<HeapObject> object,
91 Heap::RootListIndex* index_return);
92
Ben Murdochc5610432016-08-08 18:44:38 +010093 enum CodeGenResult { kSuccess, kTooManyDeoptimizationBailouts };
94
Ben Murdochda12d292016-06-02 14:46:10 +010095 // Assemble instructions for the specified block.
Ben Murdochc5610432016-08-08 18:44:38 +010096 CodeGenResult AssembleBlock(const InstructionBlock* block);
Ben Murdochda12d292016-06-02 14:46:10 +010097
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 // Assemble code for the specified instruction.
Ben Murdochc5610432016-08-08 18:44:38 +010099 CodeGenResult AssembleInstruction(Instruction* instr,
100 const InstructionBlock* block);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 void AssembleSourcePosition(Instruction* instr);
102 void AssembleGaps(Instruction* instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103
104 // ===========================================================================
105 // ============= Architecture-specific code generation methods. ==============
106 // ===========================================================================
107
Ben Murdochc5610432016-08-08 18:44:38 +0100108 CodeGenResult AssembleArchInstruction(Instruction* instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 void AssembleArchJump(RpoNumber target);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400110 void AssembleArchBranch(Instruction* instr, BranchInfo* branch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 void AssembleArchBoolean(Instruction* instr, FlagsCondition condition);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 void AssembleArchLookupSwitch(Instruction* instr);
113 void AssembleArchTableSwitch(Instruction* instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114
Ben Murdochc5610432016-08-08 18:44:38 +0100115 CodeGenResult AssembleDeoptimizerCall(int deoptimization_id,
116 Deoptimizer::BailoutType bailout_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117
118 // Generates an architecture-specific, descriptor-specific prologue
119 // to set up a stack frame.
Ben Murdochc5610432016-08-08 18:44:38 +0100120 void AssembleConstructFrame();
Ben Murdochda12d292016-06-02 14:46:10 +0100121
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 // Generates an architecture-specific, descriptor-specific return sequence
123 // to tear down a stack frame.
124 void AssembleReturn();
125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 // Generates code to deconstruct a the caller's frame, including arguments.
127 void AssembleDeconstructActivationRecord(int stack_param_delta);
128
Ben Murdochda12d292016-06-02 14:46:10 +0100129 void AssembleDeconstructFrame();
130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 // Generates code to manipulate the stack in preparation for a tail call.
132 void AssemblePrepareTailCall(int stack_param_delta);
133
Ben Murdochda12d292016-06-02 14:46:10 +0100134 // Generates code to pop current frame if it is an arguments adaptor frame.
135 void AssemblePopArgumentsAdaptorFrame(Register args_reg, Register scratch1,
136 Register scratch2, Register scratch3);
137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 // ===========================================================================
139 // ============== Architecture-specific gap resolver methods. ================
140 // ===========================================================================
141
142 // Interface used by the gap resolver to emit moves and swaps.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400143 void AssembleMove(InstructionOperand* source,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 InstructionOperand* destination) final;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400145 void AssembleSwap(InstructionOperand* source,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146 InstructionOperand* destination) final;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147
148 // ===========================================================================
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 // =================== Jump table construction methods. ======================
150 // ===========================================================================
151
152 class JumpTable;
153 // Adds a jump table that is emitted after the actual code. Returns label
154 // pointing to the beginning of the table. {targets} is assumed to be static
155 // or zone allocated.
156 Label* AddJumpTable(Label** targets, size_t target_count);
157 // Emits a jump table.
158 void AssembleJumpTable(Label** targets, size_t target_count);
159
160 // ===========================================================================
161 // ================== Deoptimization table construction. =====================
162 // ===========================================================================
163
164 void RecordCallPosition(Instruction* instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 void PopulateDeoptimizationData(Handle<Code> code);
166 int DefineDeoptimizationLiteral(Handle<Object> literal);
Ben Murdochda12d292016-06-02 14:46:10 +0100167 FrameStateDescriptor* GetFrameStateDescriptor(Instruction* instr,
168 size_t frame_state_offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 int BuildTranslation(Instruction* instr, int pc_offset,
Ben Murdochda12d292016-06-02 14:46:10 +0100170 size_t frame_state_offset,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 OutputFrameStateCombine state_combine);
172 void BuildTranslationForFrameStateDescriptor(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 FrameStateDescriptor* descriptor, InstructionOperandIterator* iter,
174 Translation* translation, OutputFrameStateCombine state_combine);
175 void TranslateStateValueDescriptor(StateValueDescriptor* desc,
176 Translation* translation,
177 InstructionOperandIterator* iter);
178 void TranslateFrameStateDescriptorOperands(FrameStateDescriptor* desc,
179 InstructionOperandIterator* iter,
180 OutputFrameStateCombine combine,
181 Translation* translation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 void AddTranslationForOperand(Translation* translation, Instruction* instr,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400183 InstructionOperand* op, MachineType type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 void EnsureSpaceForLazyDeopt();
185 void MarkLazyDeoptSite();
186
Ben Murdochda12d292016-06-02 14:46:10 +0100187 DeoptimizationExit* AddDeoptimizationExit(Instruction* instr,
188 size_t frame_state_offset);
189
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000190 // Converts the delta in the number of stack parameter passed from a tail
191 // caller to the callee into the distance (in pointers) the SP must be
192 // adjusted, taking frame elision and other relevant factors into
193 // consideration.
194 int TailCallFrameStackSlotDelta(int stack_param_delta);
195
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 // ===========================================================================
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 struct DeoptimizationState : ZoneObject {
199 public:
200 BailoutId bailout_id() const { return bailout_id_; }
201 int translation_id() const { return translation_id_; }
202 int pc_offset() const { return pc_offset_; }
203
204 DeoptimizationState(BailoutId bailout_id, int translation_id, int pc_offset)
205 : bailout_id_(bailout_id),
206 translation_id_(translation_id),
207 pc_offset_(pc_offset) {}
208
209 private:
210 BailoutId bailout_id_;
211 int translation_id_;
212 int pc_offset_;
213 };
214
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 struct HandlerInfo {
216 bool caught_locally;
217 Label* handler;
218 int pc_offset;
219 };
220
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400221 friend class OutOfLineCode;
222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 FrameAccessState* frame_access_state_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400224 Linkage* const linkage_;
225 InstructionSequence* const code_;
226 CompilationInfo* const info_;
227 Label* const labels_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228 Label return_label_;
229 RpoNumber current_block_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 SourcePosition current_source_position_;
231 MacroAssembler masm_;
232 GapResolver resolver_;
233 SafepointTableBuilder safepoints_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 ZoneVector<HandlerInfo> handlers_;
Ben Murdochda12d292016-06-02 14:46:10 +0100235 ZoneDeque<DeoptimizationExit*> deoptimization_exits_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 ZoneDeque<DeoptimizationState*> deoptimization_states_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237 ZoneDeque<Handle<Object>> deoptimization_literals_;
238 size_t inlined_function_count_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 TranslationBuffer translations_;
240 int last_lazy_deopt_pc_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241 JumpTable* jump_tables_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400242 OutOfLineCode* ools_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243 int osr_pc_offset_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244};
245
246} // namespace compiler
247} // namespace internal
248} // namespace v8
249
250#endif // V8_COMPILER_CODE_GENERATOR_H