658394b3217ad79b00bfe632425f495dfd3f13e9
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / code-generator.h
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.
4
5 #ifndef V8_COMPILER_CODE_GENERATOR_H_
6 #define V8_COMPILER_CODE_GENERATOR_H_
7
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"
13
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17
18 // Forward declarations.
19 class Linkage;
20 class OutOfLineCode;
21
22 struct BranchInfo {
23   FlagsCondition condition;
24   Label* true_label;
25   Label* false_label;
26   bool fallthru;
27 };
28
29
30 // Generates native code for a sequence of instructions.
31 class CodeGenerator FINAL : public GapResolver::Assembler {
32  public:
33   explicit CodeGenerator(Frame* frame, Linkage* linkage,
34                          InstructionSequence* code, CompilationInfo* info);
35
36   // Generate native code.
37   Handle<Code> GenerateCode();
38
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_; }
43
44   Label* GetLabel(BasicBlock::RpoNumber rpo) { return &labels_[rpo.ToSize()]; }
45
46  private:
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_; }
52
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;
56
57   // Record a safepoint with the given pointer map.
58   void RecordSafepoint(PointerMap* pointers, Safepoint::Kind kind,
59                        int arguments, Safepoint::DeoptMode deopt_mode);
60
61   // Assemble code for the specified instruction.
62   void AssembleInstruction(Instruction* instr);
63   void AssembleSourcePosition(SourcePositionInstruction* instr);
64   void AssembleGap(GapInstruction* gap);
65
66   // ===========================================================================
67   // ============= Architecture-specific code generation methods. ==============
68   // ===========================================================================
69
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);
76
77   void AssembleDeoptimizerCall(int deoptimization_id);
78
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();
85
86   // ===========================================================================
87   // ============== Architecture-specific gap resolver methods. ================
88   // ===========================================================================
89
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;
95
96   // ===========================================================================
97   // =================== Jump table construction methods. ======================
98   // ===========================================================================
99
100   class JumpTable;
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);
107
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();
127
128   // ===========================================================================
129   struct DeoptimizationState : ZoneObject {
130    public:
131     BailoutId bailout_id() const { return bailout_id_; }
132     int translation_id() const { return translation_id_; }
133     int pc_offset() const { return pc_offset_; }
134
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) {}
139
140    private:
141     BailoutId bailout_id_;
142     int translation_id_;
143     int pc_offset_;
144   };
145
146   friend class OutOfLineCode;
147
148   Frame* const frame_;
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_;
164   int osr_pc_offset_;
165 };
166
167 }  // namespace compiler
168 }  // namespace internal
169 }  // namespace v8
170
171 #endif  // V8_COMPILER_CODE_GENERATOR_H