04b1dc6f57cebff7a001225a92e84c868abf70ec
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / raw-machine-assembler.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_RAW_MACHINE_ASSEMBLER_H_
6 #define V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_
7
8 #include "src/compiler/common-operator.h"
9 #include "src/compiler/graph-builder.h"
10 #include "src/compiler/linkage.h"
11 #include "src/compiler/machine-operator.h"
12 #include "src/compiler/node.h"
13 #include "src/compiler/operator.h"
14
15
16 namespace v8 {
17 namespace internal {
18 namespace compiler {
19
20 class BasicBlock;
21 class Schedule;
22
23
24 class RawMachineAssembler : public GraphBuilder {
25  public:
26   class Label {
27    public:
28     Label() : block_(NULL), used_(false), bound_(false) {}
29     ~Label() { DCHECK(bound_ || !used_); }
30
31     BasicBlock* block() { return block_; }
32
33    private:
34     // Private constructor for exit label.
35     explicit Label(BasicBlock* block)
36         : block_(block), used_(false), bound_(false) {}
37
38     BasicBlock* block_;
39     bool used_;
40     bool bound_;
41     friend class RawMachineAssembler;
42     DISALLOW_COPY_AND_ASSIGN(Label);
43   };
44
45   RawMachineAssembler(Isolate* isolate, Graph* graph,
46                       const MachineSignature* machine_sig,
47                       MachineType word = kMachPtr,
48                       MachineOperatorBuilder::Flags flags =
49                           MachineOperatorBuilder::Flag::kNoFlags);
50   ~RawMachineAssembler() OVERRIDE {}
51
52   Zone* zone() const { return graph()->zone(); }
53   MachineOperatorBuilder* machine() { return &machine_; }
54   CommonOperatorBuilder* common() { return &common_; }
55   CallDescriptor* call_descriptor() const { return call_descriptor_; }
56   size_t parameter_count() const { return machine_sig_->parameter_count(); }
57   const MachineSignature* machine_sig() const { return machine_sig_; }
58
59   Node* UndefinedConstant() {
60     Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(
61         isolate()->factory()->undefined_value());
62     return NewNode(common()->HeapConstant(unique));
63   }
64
65   // Constants.
66   Node* PointerConstant(void* value) {
67     return IntPtrConstant(reinterpret_cast<intptr_t>(value));
68   }
69   Node* IntPtrConstant(intptr_t value) {
70     // TODO(dcarney): mark generated code as unserializable if value != 0.
71     return kPointerSize == 8 ? Int64Constant(value)
72                              : Int32Constant(static_cast<int>(value));
73   }
74   Node* Int32Constant(int32_t value) {
75     return NewNode(common()->Int32Constant(value));
76   }
77   Node* Int64Constant(int64_t value) {
78     return NewNode(common()->Int64Constant(value));
79   }
80   Node* NumberConstant(double value) {
81     return NewNode(common()->NumberConstant(value));
82   }
83   Node* Float32Constant(float value) {
84     return NewNode(common()->Float32Constant(value));
85   }
86   Node* Float64Constant(double value) {
87     return NewNode(common()->Float64Constant(value));
88   }
89   Node* HeapConstant(Handle<HeapObject> object) {
90     Unique<HeapObject> val = Unique<HeapObject>::CreateUninitialized(object);
91     return NewNode(common()->HeapConstant(val));
92   }
93
94   Node* Projection(int index, Node* a) {
95     return NewNode(common()->Projection(index), a);
96   }
97
98   // Memory Operations.
99   Node* Load(MachineType rep, Node* base) {
100     return Load(rep, base, Int32Constant(0));
101   }
102   Node* Load(MachineType rep, Node* base, Node* index) {
103     return NewNode(machine()->Load(rep), base, index, graph()->start(),
104                    graph()->start());
105   }
106   void Store(MachineType rep, Node* base, Node* value) {
107     Store(rep, base, Int32Constant(0), value);
108   }
109   void Store(MachineType rep, Node* base, Node* index, Node* value) {
110     NewNode(machine()->Store(StoreRepresentation(rep, kNoWriteBarrier)), base,
111             index, value, graph()->start(), graph()->start());
112   }
113   // Arithmetic Operations.
114   Node* WordAnd(Node* a, Node* b) {
115     return NewNode(machine()->WordAnd(), a, b);
116   }
117   Node* WordOr(Node* a, Node* b) { return NewNode(machine()->WordOr(), a, b); }
118   Node* WordXor(Node* a, Node* b) {
119     return NewNode(machine()->WordXor(), a, b);
120   }
121   Node* WordShl(Node* a, Node* b) {
122     return NewNode(machine()->WordShl(), a, b);
123   }
124   Node* WordShr(Node* a, Node* b) {
125     return NewNode(machine()->WordShr(), a, b);
126   }
127   Node* WordSar(Node* a, Node* b) {
128     return NewNode(machine()->WordSar(), a, b);
129   }
130   Node* WordRor(Node* a, Node* b) {
131     return NewNode(machine()->WordRor(), a, b);
132   }
133   Node* WordEqual(Node* a, Node* b) {
134     return NewNode(machine()->WordEqual(), a, b);
135   }
136   Node* WordNotEqual(Node* a, Node* b) {
137     return WordBinaryNot(WordEqual(a, b));
138   }
139   Node* WordNot(Node* a) {
140     if (machine()->Is32()) {
141       return Word32Not(a);
142     } else {
143       return Word64Not(a);
144     }
145   }
146   Node* WordBinaryNot(Node* a) {
147     if (machine()->Is32()) {
148       return Word32BinaryNot(a);
149     } else {
150       return Word64BinaryNot(a);
151     }
152   }
153
154   Node* Word32And(Node* a, Node* b) {
155     return NewNode(machine()->Word32And(), a, b);
156   }
157   Node* Word32Or(Node* a, Node* b) {
158     return NewNode(machine()->Word32Or(), a, b);
159   }
160   Node* Word32Xor(Node* a, Node* b) {
161     return NewNode(machine()->Word32Xor(), a, b);
162   }
163   Node* Word32Shl(Node* a, Node* b) {
164     return NewNode(machine()->Word32Shl(), a, b);
165   }
166   Node* Word32Shr(Node* a, Node* b) {
167     return NewNode(machine()->Word32Shr(), a, b);
168   }
169   Node* Word32Sar(Node* a, Node* b) {
170     return NewNode(machine()->Word32Sar(), a, b);
171   }
172   Node* Word32Ror(Node* a, Node* b) {
173     return NewNode(machine()->Word32Ror(), a, b);
174   }
175   Node* Word32Equal(Node* a, Node* b) {
176     return NewNode(machine()->Word32Equal(), a, b);
177   }
178   Node* Word32NotEqual(Node* a, Node* b) {
179     return Word32BinaryNot(Word32Equal(a, b));
180   }
181   Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
182   Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
183
184   Node* Word64And(Node* a, Node* b) {
185     return NewNode(machine()->Word64And(), a, b);
186   }
187   Node* Word64Or(Node* a, Node* b) {
188     return NewNode(machine()->Word64Or(), a, b);
189   }
190   Node* Word64Xor(Node* a, Node* b) {
191     return NewNode(machine()->Word64Xor(), a, b);
192   }
193   Node* Word64Shl(Node* a, Node* b) {
194     return NewNode(machine()->Word64Shl(), a, b);
195   }
196   Node* Word64Shr(Node* a, Node* b) {
197     return NewNode(machine()->Word64Shr(), a, b);
198   }
199   Node* Word64Sar(Node* a, Node* b) {
200     return NewNode(machine()->Word64Sar(), a, b);
201   }
202   Node* Word64Ror(Node* a, Node* b) {
203     return NewNode(machine()->Word64Ror(), a, b);
204   }
205   Node* Word64Equal(Node* a, Node* b) {
206     return NewNode(machine()->Word64Equal(), a, b);
207   }
208   Node* Word64NotEqual(Node* a, Node* b) {
209     return Word64BinaryNot(Word64Equal(a, b));
210   }
211   Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
212   Node* Word64BinaryNot(Node* a) { return Word64Equal(a, Int64Constant(0)); }
213
214   Node* Int32Add(Node* a, Node* b) {
215     return NewNode(machine()->Int32Add(), a, b);
216   }
217   Node* Int32AddWithOverflow(Node* a, Node* b) {
218     return NewNode(machine()->Int32AddWithOverflow(), a, b);
219   }
220   Node* Int32Sub(Node* a, Node* b) {
221     return NewNode(machine()->Int32Sub(), a, b);
222   }
223   Node* Int32SubWithOverflow(Node* a, Node* b) {
224     return NewNode(machine()->Int32SubWithOverflow(), a, b);
225   }
226   Node* Int32Mul(Node* a, Node* b) {
227     return NewNode(machine()->Int32Mul(), a, b);
228   }
229   Node* Int32MulHigh(Node* a, Node* b) {
230     return NewNode(machine()->Int32MulHigh(), a, b);
231   }
232   Node* Int32Div(Node* a, Node* b) {
233     return NewNode(machine()->Int32Div(), a, b, graph()->start());
234   }
235   Node* Int32Mod(Node* a, Node* b) {
236     return NewNode(machine()->Int32Mod(), a, b, graph()->start());
237   }
238   Node* Int32LessThan(Node* a, Node* b) {
239     return NewNode(machine()->Int32LessThan(), a, b);
240   }
241   Node* Int32LessThanOrEqual(Node* a, Node* b) {
242     return NewNode(machine()->Int32LessThanOrEqual(), a, b);
243   }
244   Node* Uint32Div(Node* a, Node* b) {
245     return NewNode(machine()->Uint32Div(), a, b, graph()->start());
246   }
247   Node* Uint32LessThan(Node* a, Node* b) {
248     return NewNode(machine()->Uint32LessThan(), a, b);
249   }
250   Node* Uint32LessThanOrEqual(Node* a, Node* b) {
251     return NewNode(machine()->Uint32LessThanOrEqual(), a, b);
252   }
253   Node* Uint32Mod(Node* a, Node* b) {
254     return NewNode(machine()->Uint32Mod(), a, b, graph()->start());
255   }
256   Node* Uint32MulHigh(Node* a, Node* b) {
257     return NewNode(machine()->Uint32MulHigh(), a, b);
258   }
259   Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
260   Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
261     return Int32LessThanOrEqual(b, a);
262   }
263   Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
264
265   Node* Int64Add(Node* a, Node* b) {
266     return NewNode(machine()->Int64Add(), a, b);
267   }
268   Node* Int64Sub(Node* a, Node* b) {
269     return NewNode(machine()->Int64Sub(), a, b);
270   }
271   Node* Int64Mul(Node* a, Node* b) {
272     return NewNode(machine()->Int64Mul(), a, b);
273   }
274   Node* Int64Div(Node* a, Node* b) {
275     return NewNode(machine()->Int64Div(), a, b);
276   }
277   Node* Int64Mod(Node* a, Node* b) {
278     return NewNode(machine()->Int64Mod(), a, b);
279   }
280   Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
281   Node* Int64LessThan(Node* a, Node* b) {
282     return NewNode(machine()->Int64LessThan(), a, b);
283   }
284   Node* Int64LessThanOrEqual(Node* a, Node* b) {
285     return NewNode(machine()->Int64LessThanOrEqual(), a, b);
286   }
287   Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
288   Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
289     return Int64LessThanOrEqual(b, a);
290   }
291   Node* Uint64Div(Node* a, Node* b) {
292     return NewNode(machine()->Uint64Div(), a, b);
293   }
294   Node* Uint64Mod(Node* a, Node* b) {
295     return NewNode(machine()->Uint64Mod(), a, b);
296   }
297
298   // TODO(turbofan): What is this used for?
299   Node* ConvertIntPtrToInt32(Node* a) {
300     return kPointerSize == 8 ? NewNode(machine()->TruncateInt64ToInt32(), a)
301                              : a;
302   }
303   Node* ConvertInt32ToIntPtr(Node* a) {
304     return kPointerSize == 8 ? NewNode(machine()->ChangeInt32ToInt64(), a) : a;
305   }
306
307 #define INTPTR_BINOP(prefix, name)                     \
308   Node* IntPtr##name(Node* a, Node* b) {               \
309     return kPointerSize == 8 ? prefix##64##name(a, b)  \
310                              : prefix##32##name(a, b); \
311   }
312
313   INTPTR_BINOP(Int, Add);
314   INTPTR_BINOP(Int, Sub);
315   INTPTR_BINOP(Int, LessThan);
316   INTPTR_BINOP(Int, LessThanOrEqual);
317   INTPTR_BINOP(Word, Equal);
318   INTPTR_BINOP(Word, NotEqual);
319   INTPTR_BINOP(Int, GreaterThanOrEqual);
320   INTPTR_BINOP(Int, GreaterThan);
321
322 #undef INTPTR_BINOP
323
324   Node* Float64Add(Node* a, Node* b) {
325     return NewNode(machine()->Float64Add(), a, b);
326   }
327   Node* Float64Sub(Node* a, Node* b) {
328     return NewNode(machine()->Float64Sub(), a, b);
329   }
330   Node* Float64Mul(Node* a, Node* b) {
331     return NewNode(machine()->Float64Mul(), a, b);
332   }
333   Node* Float64Div(Node* a, Node* b) {
334     return NewNode(machine()->Float64Div(), a, b);
335   }
336   Node* Float64Mod(Node* a, Node* b) {
337     return NewNode(machine()->Float64Mod(), a, b);
338   }
339   Node* Float64Equal(Node* a, Node* b) {
340     return NewNode(machine()->Float64Equal(), a, b);
341   }
342   Node* Float64NotEqual(Node* a, Node* b) {
343     return WordBinaryNot(Float64Equal(a, b));
344   }
345   Node* Float64LessThan(Node* a, Node* b) {
346     return NewNode(machine()->Float64LessThan(), a, b);
347   }
348   Node* Float64LessThanOrEqual(Node* a, Node* b) {
349     return NewNode(machine()->Float64LessThanOrEqual(), a, b);
350   }
351   Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
352   Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
353     return Float64LessThanOrEqual(b, a);
354   }
355
356   // Conversions.
357   Node* ChangeFloat32ToFloat64(Node* a) {
358     return NewNode(machine()->ChangeFloat32ToFloat64(), a);
359   }
360   Node* ChangeInt32ToFloat64(Node* a) {
361     return NewNode(machine()->ChangeInt32ToFloat64(), a);
362   }
363   Node* ChangeUint32ToFloat64(Node* a) {
364     return NewNode(machine()->ChangeUint32ToFloat64(), a);
365   }
366   Node* ChangeFloat64ToInt32(Node* a) {
367     return NewNode(machine()->ChangeFloat64ToInt32(), a);
368   }
369   Node* ChangeFloat64ToUint32(Node* a) {
370     return NewNode(machine()->ChangeFloat64ToUint32(), a);
371   }
372   Node* ChangeInt32ToInt64(Node* a) {
373     return NewNode(machine()->ChangeInt32ToInt64(), a);
374   }
375   Node* ChangeUint32ToUint64(Node* a) {
376     return NewNode(machine()->ChangeUint32ToUint64(), a);
377   }
378   Node* TruncateFloat64ToFloat32(Node* a) {
379     return NewNode(machine()->TruncateFloat64ToFloat32(), a);
380   }
381   Node* TruncateFloat64ToInt32(Node* a) {
382     return NewNode(machine()->TruncateFloat64ToInt32(), a);
383   }
384   Node* TruncateInt64ToInt32(Node* a) {
385     return NewNode(machine()->TruncateInt64ToInt32(), a);
386   }
387   Node* Float64Floor(Node* a) { return NewNode(machine()->Float64Floor(), a); }
388   Node* Float64Ceil(Node* a) { return NewNode(machine()->Float64Ceil(), a); }
389   Node* Float64RoundTruncate(Node* a) {
390     return NewNode(machine()->Float64RoundTruncate(), a);
391   }
392   Node* Float64RoundTiesAway(Node* a) {
393     return NewNode(machine()->Float64RoundTiesAway(), a);
394   }
395
396   // Parameters.
397   Node* Parameter(size_t index);
398
399   // Control flow.
400   Label* Exit();
401   void Goto(Label* label);
402   void Branch(Node* condition, Label* true_val, Label* false_val);
403   void Switch(Node* index, Label* default_label, int32_t* case_values,
404               Label** case_labels, size_t case_count);
405   // Call through CallFunctionStub with lazy deopt and frame-state.
406   Node* CallFunctionStub0(Node* function, Node* receiver, Node* context,
407                           Node* frame_state, CallFunctionFlags flags);
408   // Call to a JS function with zero parameters.
409   Node* CallJS0(Node* function, Node* receiver, Node* context,
410                 Node* frame_state);
411   // Call to a runtime function with zero parameters.
412   Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context,
413                      Node* frame_state);
414   void Return(Node* value);
415   void Bind(Label* label);
416   void Deoptimize(Node* state);
417
418   // Variables.
419   Node* Phi(MachineType type, Node* n1, Node* n2) {
420     return NewNode(common()->Phi(type, 2), n1, n2);
421   }
422   Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) {
423     return NewNode(common()->Phi(type, 3), n1, n2, n3);
424   }
425   Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) {
426     return NewNode(common()->Phi(type, 4), n1, n2, n3, n4);
427   }
428
429   // MachineAssembler is invalid after export.
430   Schedule* Export();
431
432  protected:
433   Node* MakeNode(const Operator* op, int input_count, Node** inputs,
434                  bool incomplete) FINAL;
435
436   bool ScheduleValid() { return schedule_ != NULL; }
437
438   Schedule* schedule() {
439     DCHECK(ScheduleValid());
440     return schedule_;
441   }
442
443  private:
444   BasicBlock* Use(Label* label);
445   BasicBlock* EnsureBlock(Label* label);
446   BasicBlock* CurrentBlock();
447
448   Schedule* schedule_;
449   MachineOperatorBuilder machine_;
450   CommonOperatorBuilder common_;
451   const MachineSignature* machine_sig_;
452   CallDescriptor* call_descriptor_;
453   Node** parameters_;
454   Label exit_label_;
455   BasicBlock* current_block_;
456
457   DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler);
458 };
459
460 }  // namespace compiler
461 }  // namespace internal
462 }  // namespace v8
463
464 #endif  // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_