deps: update v8 to 4.3.61.21
[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   Node* ExternalConstant(ExternalReference address) {
94     return NewNode(common()->ExternalConstant(address));
95   }
96
97   Node* Projection(int index, Node* a) {
98     return NewNode(common()->Projection(index), a);
99   }
100
101   // Memory Operations.
102   Node* Load(MachineType rep, Node* base) {
103     return Load(rep, base, IntPtrConstant(0));
104   }
105   Node* Load(MachineType rep, Node* base, Node* index) {
106     return NewNode(machine()->Load(rep), base, index, graph()->start(),
107                    graph()->start());
108   }
109   void Store(MachineType rep, Node* base, Node* value) {
110     Store(rep, base, IntPtrConstant(0), value);
111   }
112   void Store(MachineType rep, Node* base, Node* index, Node* value) {
113     NewNode(machine()->Store(StoreRepresentation(rep, kNoWriteBarrier)), base,
114             index, value, graph()->start(), graph()->start());
115   }
116   // Arithmetic Operations.
117   Node* WordAnd(Node* a, Node* b) {
118     return NewNode(machine()->WordAnd(), a, b);
119   }
120   Node* WordOr(Node* a, Node* b) { return NewNode(machine()->WordOr(), a, b); }
121   Node* WordXor(Node* a, Node* b) {
122     return NewNode(machine()->WordXor(), a, b);
123   }
124   Node* WordShl(Node* a, Node* b) {
125     return NewNode(machine()->WordShl(), a, b);
126   }
127   Node* WordShr(Node* a, Node* b) {
128     return NewNode(machine()->WordShr(), a, b);
129   }
130   Node* WordSar(Node* a, Node* b) {
131     return NewNode(machine()->WordSar(), a, b);
132   }
133   Node* WordRor(Node* a, Node* b) {
134     return NewNode(machine()->WordRor(), a, b);
135   }
136   Node* WordEqual(Node* a, Node* b) {
137     return NewNode(machine()->WordEqual(), a, b);
138   }
139   Node* WordNotEqual(Node* a, Node* b) {
140     return WordBinaryNot(WordEqual(a, b));
141   }
142   Node* WordNot(Node* a) {
143     if (machine()->Is32()) {
144       return Word32Not(a);
145     } else {
146       return Word64Not(a);
147     }
148   }
149   Node* WordBinaryNot(Node* a) {
150     if (machine()->Is32()) {
151       return Word32BinaryNot(a);
152     } else {
153       return Word64BinaryNot(a);
154     }
155   }
156
157   Node* Word32And(Node* a, Node* b) {
158     return NewNode(machine()->Word32And(), a, b);
159   }
160   Node* Word32Or(Node* a, Node* b) {
161     return NewNode(machine()->Word32Or(), a, b);
162   }
163   Node* Word32Xor(Node* a, Node* b) {
164     return NewNode(machine()->Word32Xor(), a, b);
165   }
166   Node* Word32Shl(Node* a, Node* b) {
167     return NewNode(machine()->Word32Shl(), a, b);
168   }
169   Node* Word32Shr(Node* a, Node* b) {
170     return NewNode(machine()->Word32Shr(), a, b);
171   }
172   Node* Word32Sar(Node* a, Node* b) {
173     return NewNode(machine()->Word32Sar(), a, b);
174   }
175   Node* Word32Ror(Node* a, Node* b) {
176     return NewNode(machine()->Word32Ror(), a, b);
177   }
178   Node* Word32Clz(Node* a) { return NewNode(machine()->Word32Clz(), a); }
179   Node* Word32Equal(Node* a, Node* b) {
180     return NewNode(machine()->Word32Equal(), a, b);
181   }
182   Node* Word32NotEqual(Node* a, Node* b) {
183     return Word32BinaryNot(Word32Equal(a, b));
184   }
185   Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
186   Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
187
188   Node* Word64And(Node* a, Node* b) {
189     return NewNode(machine()->Word64And(), a, b);
190   }
191   Node* Word64Or(Node* a, Node* b) {
192     return NewNode(machine()->Word64Or(), a, b);
193   }
194   Node* Word64Xor(Node* a, Node* b) {
195     return NewNode(machine()->Word64Xor(), a, b);
196   }
197   Node* Word64Shl(Node* a, Node* b) {
198     return NewNode(machine()->Word64Shl(), a, b);
199   }
200   Node* Word64Shr(Node* a, Node* b) {
201     return NewNode(machine()->Word64Shr(), a, b);
202   }
203   Node* Word64Sar(Node* a, Node* b) {
204     return NewNode(machine()->Word64Sar(), a, b);
205   }
206   Node* Word64Ror(Node* a, Node* b) {
207     return NewNode(machine()->Word64Ror(), a, b);
208   }
209   Node* Word64Equal(Node* a, Node* b) {
210     return NewNode(machine()->Word64Equal(), a, b);
211   }
212   Node* Word64NotEqual(Node* a, Node* b) {
213     return Word64BinaryNot(Word64Equal(a, b));
214   }
215   Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
216   Node* Word64BinaryNot(Node* a) { return Word64Equal(a, Int64Constant(0)); }
217
218   Node* Int32Add(Node* a, Node* b) {
219     return NewNode(machine()->Int32Add(), a, b);
220   }
221   Node* Int32AddWithOverflow(Node* a, Node* b) {
222     return NewNode(machine()->Int32AddWithOverflow(), a, b);
223   }
224   Node* Int32Sub(Node* a, Node* b) {
225     return NewNode(machine()->Int32Sub(), a, b);
226   }
227   Node* Int32SubWithOverflow(Node* a, Node* b) {
228     return NewNode(machine()->Int32SubWithOverflow(), a, b);
229   }
230   Node* Int32Mul(Node* a, Node* b) {
231     return NewNode(machine()->Int32Mul(), a, b);
232   }
233   Node* Int32MulHigh(Node* a, Node* b) {
234     return NewNode(machine()->Int32MulHigh(), a, b);
235   }
236   Node* Int32Div(Node* a, Node* b) {
237     return NewNode(machine()->Int32Div(), a, b, graph()->start());
238   }
239   Node* Int32Mod(Node* a, Node* b) {
240     return NewNode(machine()->Int32Mod(), a, b, graph()->start());
241   }
242   Node* Int32LessThan(Node* a, Node* b) {
243     return NewNode(machine()->Int32LessThan(), a, b);
244   }
245   Node* Int32LessThanOrEqual(Node* a, Node* b) {
246     return NewNode(machine()->Int32LessThanOrEqual(), a, b);
247   }
248   Node* Uint32Div(Node* a, Node* b) {
249     return NewNode(machine()->Uint32Div(), a, b, graph()->start());
250   }
251   Node* Uint32LessThan(Node* a, Node* b) {
252     return NewNode(machine()->Uint32LessThan(), a, b);
253   }
254   Node* Uint32LessThanOrEqual(Node* a, Node* b) {
255     return NewNode(machine()->Uint32LessThanOrEqual(), a, b);
256   }
257   Node* Uint32Mod(Node* a, Node* b) {
258     return NewNode(machine()->Uint32Mod(), a, b, graph()->start());
259   }
260   Node* Uint32MulHigh(Node* a, Node* b) {
261     return NewNode(machine()->Uint32MulHigh(), a, b);
262   }
263   Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
264   Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
265     return Int32LessThanOrEqual(b, a);
266   }
267   Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
268
269   Node* Int64Add(Node* a, Node* b) {
270     return NewNode(machine()->Int64Add(), a, b);
271   }
272   Node* Int64Sub(Node* a, Node* b) {
273     return NewNode(machine()->Int64Sub(), a, b);
274   }
275   Node* Int64Mul(Node* a, Node* b) {
276     return NewNode(machine()->Int64Mul(), a, b);
277   }
278   Node* Int64Div(Node* a, Node* b) {
279     return NewNode(machine()->Int64Div(), a, b);
280   }
281   Node* Int64Mod(Node* a, Node* b) {
282     return NewNode(machine()->Int64Mod(), a, b);
283   }
284   Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
285   Node* Int64LessThan(Node* a, Node* b) {
286     return NewNode(machine()->Int64LessThan(), a, b);
287   }
288   Node* Uint64LessThan(Node* a, Node* b) {
289     return NewNode(machine()->Uint64LessThan(), a, b);
290   }
291   Node* Int64LessThanOrEqual(Node* a, Node* b) {
292     return NewNode(machine()->Int64LessThanOrEqual(), a, b);
293   }
294   Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
295   Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
296     return Int64LessThanOrEqual(b, a);
297   }
298   Node* Uint64Div(Node* a, Node* b) {
299     return NewNode(machine()->Uint64Div(), a, b);
300   }
301   Node* Uint64Mod(Node* a, Node* b) {
302     return NewNode(machine()->Uint64Mod(), a, b);
303   }
304
305   // TODO(turbofan): What is this used for?
306   Node* ConvertIntPtrToInt32(Node* a) {
307     return kPointerSize == 8 ? NewNode(machine()->TruncateInt64ToInt32(), a)
308                              : a;
309   }
310   Node* ConvertInt32ToIntPtr(Node* a) {
311     return kPointerSize == 8 ? NewNode(machine()->ChangeInt32ToInt64(), a) : a;
312   }
313
314 #define INTPTR_BINOP(prefix, name)                     \
315   Node* IntPtr##name(Node* a, Node* b) {               \
316     return kPointerSize == 8 ? prefix##64##name(a, b)  \
317                              : prefix##32##name(a, b); \
318   }
319
320   INTPTR_BINOP(Int, Add);
321   INTPTR_BINOP(Int, Sub);
322   INTPTR_BINOP(Int, LessThan);
323   INTPTR_BINOP(Int, LessThanOrEqual);
324   INTPTR_BINOP(Word, Equal);
325   INTPTR_BINOP(Word, NotEqual);
326   INTPTR_BINOP(Int, GreaterThanOrEqual);
327   INTPTR_BINOP(Int, GreaterThan);
328
329 #undef INTPTR_BINOP
330
331   Node* Float64Add(Node* a, Node* b) {
332     return NewNode(machine()->Float64Add(), a, b);
333   }
334   Node* Float64Sub(Node* a, Node* b) {
335     return NewNode(machine()->Float64Sub(), a, b);
336   }
337   Node* Float64Mul(Node* a, Node* b) {
338     return NewNode(machine()->Float64Mul(), a, b);
339   }
340   Node* Float64Div(Node* a, Node* b) {
341     return NewNode(machine()->Float64Div(), a, b);
342   }
343   Node* Float64Mod(Node* a, Node* b) {
344     return NewNode(machine()->Float64Mod(), a, b);
345   }
346   Node* Float64Equal(Node* a, Node* b) {
347     return NewNode(machine()->Float64Equal(), a, b);
348   }
349   Node* Float64NotEqual(Node* a, Node* b) {
350     return WordBinaryNot(Float64Equal(a, b));
351   }
352   Node* Float64LessThan(Node* a, Node* b) {
353     return NewNode(machine()->Float64LessThan(), a, b);
354   }
355   Node* Float64LessThanOrEqual(Node* a, Node* b) {
356     return NewNode(machine()->Float64LessThanOrEqual(), a, b);
357   }
358   Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
359   Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
360     return Float64LessThanOrEqual(b, a);
361   }
362
363   // Conversions.
364   Node* ChangeFloat32ToFloat64(Node* a) {
365     return NewNode(machine()->ChangeFloat32ToFloat64(), a);
366   }
367   Node* ChangeInt32ToFloat64(Node* a) {
368     return NewNode(machine()->ChangeInt32ToFloat64(), a);
369   }
370   Node* ChangeUint32ToFloat64(Node* a) {
371     return NewNode(machine()->ChangeUint32ToFloat64(), a);
372   }
373   Node* ChangeFloat64ToInt32(Node* a) {
374     return NewNode(machine()->ChangeFloat64ToInt32(), a);
375   }
376   Node* ChangeFloat64ToUint32(Node* a) {
377     return NewNode(machine()->ChangeFloat64ToUint32(), a);
378   }
379   Node* ChangeInt32ToInt64(Node* a) {
380     return NewNode(machine()->ChangeInt32ToInt64(), a);
381   }
382   Node* ChangeUint32ToUint64(Node* a) {
383     return NewNode(machine()->ChangeUint32ToUint64(), a);
384   }
385   Node* TruncateFloat64ToFloat32(Node* a) {
386     return NewNode(machine()->TruncateFloat64ToFloat32(), a);
387   }
388   Node* TruncateFloat64ToInt32(Node* a) {
389     return NewNode(machine()->TruncateFloat64ToInt32(), a);
390   }
391   Node* TruncateInt64ToInt32(Node* a) {
392     return NewNode(machine()->TruncateInt64ToInt32(), a);
393   }
394   Node* Float64RoundDown(Node* a) {
395     return NewNode(machine()->Float64RoundDown(), a);
396   }
397   Node* Float64RoundTruncate(Node* a) {
398     return NewNode(machine()->Float64RoundTruncate(), a);
399   }
400   Node* Float64RoundTiesAway(Node* a) {
401     return NewNode(machine()->Float64RoundTiesAway(), a);
402   }
403
404   // Float64 bit operations.
405   Node* Float64ExtractLowWord32(Node* a) {
406     return NewNode(machine()->Float64ExtractLowWord32(), a);
407   }
408   Node* Float64ExtractHighWord32(Node* a) {
409     return NewNode(machine()->Float64ExtractHighWord32(), a);
410   }
411   Node* Float64InsertLowWord32(Node* a, Node* b) {
412     return NewNode(machine()->Float64InsertLowWord32(), a, b);
413   }
414   Node* Float64InsertHighWord32(Node* a, Node* b) {
415     return NewNode(machine()->Float64InsertHighWord32(), a, b);
416   }
417
418   // Stack operations.
419   Node* LoadStackPointer() { return NewNode(machine()->LoadStackPointer()); }
420
421   // Parameters.
422   Node* Parameter(size_t index);
423
424   // Control flow.
425   Label* Exit();
426   void Goto(Label* label);
427   void Branch(Node* condition, Label* true_val, Label* false_val);
428   void Switch(Node* index, Label* default_label, int32_t* case_values,
429               Label** case_labels, size_t case_count);
430   // Call through CallFunctionStub with lazy deopt and frame-state.
431   Node* CallFunctionStub0(Node* function, Node* receiver, Node* context,
432                           Node* frame_state, CallFunctionFlags flags);
433   // Call to a JS function with zero parameters.
434   Node* CallJS0(Node* function, Node* receiver, Node* context,
435                 Node* frame_state);
436   // Call to a runtime function with zero parameters.
437   Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context,
438                      Node* frame_state);
439   void Return(Node* value);
440   void Bind(Label* label);
441   void Deoptimize(Node* state);
442
443   // Variables.
444   Node* Phi(MachineType type, Node* n1, Node* n2) {
445     return NewNode(common()->Phi(type, 2), n1, n2);
446   }
447   Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) {
448     return NewNode(common()->Phi(type, 3), n1, n2, n3);
449   }
450   Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) {
451     return NewNode(common()->Phi(type, 4), n1, n2, n3, n4);
452   }
453
454   // MachineAssembler is invalid after export.
455   Schedule* Export();
456
457  protected:
458   Node* MakeNode(const Operator* op, int input_count, Node** inputs,
459                  bool incomplete) FINAL;
460
461   bool ScheduleValid() { return schedule_ != NULL; }
462
463   Schedule* schedule() {
464     DCHECK(ScheduleValid());
465     return schedule_;
466   }
467
468  private:
469   BasicBlock* Use(Label* label);
470   BasicBlock* EnsureBlock(Label* label);
471   BasicBlock* CurrentBlock();
472
473   Schedule* schedule_;
474   MachineOperatorBuilder machine_;
475   CommonOperatorBuilder common_;
476   const MachineSignature* machine_sig_;
477   CallDescriptor* call_descriptor_;
478   Node** parameters_;
479   Label exit_label_;
480   BasicBlock* current_block_;
481
482   DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler);
483 };
484
485 }  // namespace compiler
486 }  // namespace internal
487 }  // namespace v8
488
489 #endif  // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_