deps: update v8 to 4.3.61.21
[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(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(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(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                                Deoptimizer::BailoutType bailout_type);
79
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();
86
87   // ===========================================================================
88   // ============== Architecture-specific gap resolver methods. ================
89   // ===========================================================================
90
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;
96
97   // ===========================================================================
98   // =================== Jump table construction methods. ======================
99   // ===========================================================================
100
101   class JumpTable;
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);
108
109   // ===========================================================================
110   // ================== Deoptimization table construction. =====================
111   // ===========================================================================
112
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();
130
131   // ===========================================================================
132
133   struct DeoptimizationState : ZoneObject {
134    public:
135     BailoutId bailout_id() const { return bailout_id_; }
136     int translation_id() const { return translation_id_; }
137     int pc_offset() const { return pc_offset_; }
138
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) {}
143
144    private:
145     BailoutId bailout_id_;
146     int translation_id_;
147     int pc_offset_;
148   };
149
150   struct HandlerInfo {
151     Label* handler;
152     int pc_offset;
153   };
154
155   friend class OutOfLineCode;
156
157   Frame* const frame_;
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_;
174   int osr_pc_offset_;
175 };
176
177 }  // namespace compiler
178 }  // namespace internal
179 }  // namespace v8
180
181 #endif  // V8_COMPILER_CODE_GENERATOR_H