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/assembler.h"
9#include "src/compiler/common-operator.h"
10#include "src/compiler/graph.h"
11#include "src/compiler/linkage.h"
12#include "src/compiler/machine-operator.h"
13#include "src/compiler/node.h"
14#include "src/compiler/operator.h"
15#include "src/factory.h"
16#include "src/globals.h"
17
18namespace v8 {
19namespace internal {
20namespace compiler {
21
22class BasicBlock;
23class RawMachineLabel;
24class Schedule;
25
26
27// The RawMachineAssembler produces a low-level IR graph. All nodes are wired
28// into a graph and also placed into a schedule immediately, hence subsequent
29// code generation can happen without the need for scheduling.
30//
31// In order to create a schedule on-the-fly, the assembler keeps track of basic
32// blocks by having one current basic block being populated and by referencing
33// other basic blocks through the use of labels.
34//
35// Also note that the generated graph is only valid together with the generated
36// schedule, using one without the other is invalid as the graph is inherently
37// non-schedulable due to missing control and effect dependencies.
38class V8_EXPORT_PRIVATE RawMachineAssembler {
39 public:
40  RawMachineAssembler(
41      Isolate* isolate, Graph* graph, CallDescriptor* call_descriptor,
42      MachineRepresentation word = MachineType::PointerRepresentation(),
43      MachineOperatorBuilder::Flags flags =
44          MachineOperatorBuilder::Flag::kNoFlags,
45      MachineOperatorBuilder::AlignmentRequirements alignment_requirements =
46          MachineOperatorBuilder::AlignmentRequirements::
47              FullUnalignedAccessSupport());
48  ~RawMachineAssembler() {}
49
50  Isolate* isolate() const { return isolate_; }
51  Graph* graph() const { return graph_; }
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
57  // Finalizes the schedule and exports it to be used for code generation. Note
58  // that this RawMachineAssembler becomes invalid after export.
59  Schedule* Export();
60
61  // ===========================================================================
62  // The following utility methods create new nodes with specific operators and
63  // place them into the current basic block. They don't perform control flow,
64  // hence will not switch the current basic block.
65
66  Node* NullConstant() {
67    return HeapConstant(isolate()->factory()->null_value());
68  }
69
70  Node* UndefinedConstant() {
71    return HeapConstant(isolate()->factory()->undefined_value());
72  }
73
74  // Constants.
75  Node* PointerConstant(void* value) {
76    return IntPtrConstant(reinterpret_cast<intptr_t>(value));
77  }
78  Node* IntPtrConstant(intptr_t value) {
79    // TODO(dcarney): mark generated code as unserializable if value != 0.
80    return kPointerSize == 8 ? Int64Constant(value)
81                             : Int32Constant(static_cast<int>(value));
82  }
83  Node* RelocatableIntPtrConstant(intptr_t value, RelocInfo::Mode rmode);
84  Node* Int32Constant(int32_t value) {
85    return AddNode(common()->Int32Constant(value));
86  }
87  Node* StackSlot(MachineRepresentation rep) {
88    return AddNode(machine()->StackSlot(rep));
89  }
90  Node* Int64Constant(int64_t value) {
91    return AddNode(common()->Int64Constant(value));
92  }
93  Node* NumberConstant(double value) {
94    return AddNode(common()->NumberConstant(value));
95  }
96  Node* Float32Constant(float value) {
97    return AddNode(common()->Float32Constant(value));
98  }
99  Node* Float64Constant(double value) {
100    return AddNode(common()->Float64Constant(value));
101  }
102  Node* HeapConstant(Handle<HeapObject> object) {
103    return AddNode(common()->HeapConstant(object));
104  }
105  Node* BooleanConstant(bool value) {
106    Handle<Object> object = isolate()->factory()->ToBoolean(value);
107    return HeapConstant(Handle<HeapObject>::cast(object));
108  }
109  Node* ExternalConstant(ExternalReference address) {
110    return AddNode(common()->ExternalConstant(address));
111  }
112  Node* RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode) {
113    return AddNode(common()->RelocatableInt32Constant(value, rmode));
114  }
115  Node* RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode) {
116    return AddNode(common()->RelocatableInt64Constant(value, rmode));
117  }
118
119  Node* Projection(int index, Node* a) {
120    return AddNode(common()->Projection(index), a);
121  }
122
123  // Memory Operations.
124  Node* Load(MachineType rep, Node* base) {
125    return Load(rep, base, IntPtrConstant(0));
126  }
127  Node* Load(MachineType rep, Node* base, Node* index) {
128    return AddNode(machine()->Load(rep), base, index);
129  }
130  Node* Store(MachineRepresentation rep, Node* base, Node* value,
131              WriteBarrierKind write_barrier) {
132    return Store(rep, base, IntPtrConstant(0), value, write_barrier);
133  }
134  Node* Store(MachineRepresentation rep, Node* base, Node* index, Node* value,
135              WriteBarrierKind write_barrier) {
136    return AddNode(machine()->Store(StoreRepresentation(rep, write_barrier)),
137                   base, index, value);
138  }
139  Node* Retain(Node* value) { return AddNode(common()->Retain(), value); }
140
141  // Unaligned memory operations
142  Node* UnalignedLoad(MachineType rep, Node* base) {
143    return UnalignedLoad(rep, base, IntPtrConstant(0));
144  }
145  Node* UnalignedLoad(MachineType rep, Node* base, Node* index) {
146    if (machine()->UnalignedLoadSupported(rep, 1)) {
147      return AddNode(machine()->Load(rep), base, index);
148    } else {
149      return AddNode(machine()->UnalignedLoad(rep), base, index);
150    }
151  }
152  Node* UnalignedStore(MachineRepresentation rep, Node* base, Node* value) {
153    return UnalignedStore(rep, base, IntPtrConstant(0), value);
154  }
155  Node* UnalignedStore(MachineRepresentation rep, Node* base, Node* index,
156                       Node* value) {
157    MachineType t = MachineType::TypeForRepresentation(rep);
158    if (machine()->UnalignedStoreSupported(t, 1)) {
159      return AddNode(machine()->Store(StoreRepresentation(
160                         rep, WriteBarrierKind::kNoWriteBarrier)),
161                     base, index, value);
162    } else {
163      return AddNode(
164          machine()->UnalignedStore(UnalignedStoreRepresentation(rep)), base,
165          index, value);
166    }
167  }
168
169  // Atomic memory operations.
170  Node* AtomicLoad(MachineType rep, Node* base, Node* index) {
171    return AddNode(machine()->AtomicLoad(rep), base, index);
172  }
173  Node* AtomicStore(MachineRepresentation rep, Node* base, Node* index,
174                    Node* value) {
175    return AddNode(machine()->AtomicStore(rep), base, index, value);
176  }
177
178  // Arithmetic Operations.
179  Node* WordAnd(Node* a, Node* b) {
180    return AddNode(machine()->WordAnd(), a, b);
181  }
182  Node* WordOr(Node* a, Node* b) { return AddNode(machine()->WordOr(), a, b); }
183  Node* WordXor(Node* a, Node* b) {
184    return AddNode(machine()->WordXor(), a, b);
185  }
186  Node* WordShl(Node* a, Node* b) {
187    return AddNode(machine()->WordShl(), a, b);
188  }
189  Node* WordShr(Node* a, Node* b) {
190    return AddNode(machine()->WordShr(), a, b);
191  }
192  Node* WordSar(Node* a, Node* b) {
193    return AddNode(machine()->WordSar(), a, b);
194  }
195  Node* WordRor(Node* a, Node* b) {
196    return AddNode(machine()->WordRor(), a, b);
197  }
198  Node* WordEqual(Node* a, Node* b) {
199    return AddNode(machine()->WordEqual(), a, b);
200  }
201  Node* WordNotEqual(Node* a, Node* b) {
202    return Word32BinaryNot(WordEqual(a, b));
203  }
204  Node* WordNot(Node* a) {
205    if (machine()->Is32()) {
206      return Word32Not(a);
207    } else {
208      return Word64Not(a);
209    }
210  }
211
212  Node* Word32And(Node* a, Node* b) {
213    return AddNode(machine()->Word32And(), a, b);
214  }
215  Node* Word32Or(Node* a, Node* b) {
216    return AddNode(machine()->Word32Or(), a, b);
217  }
218  Node* Word32Xor(Node* a, Node* b) {
219    return AddNode(machine()->Word32Xor(), a, b);
220  }
221  Node* Word32Shl(Node* a, Node* b) {
222    return AddNode(machine()->Word32Shl(), a, b);
223  }
224  Node* Word32Shr(Node* a, Node* b) {
225    return AddNode(machine()->Word32Shr(), a, b);
226  }
227  Node* Word32Sar(Node* a, Node* b) {
228    return AddNode(machine()->Word32Sar(), a, b);
229  }
230  Node* Word32Ror(Node* a, Node* b) {
231    return AddNode(machine()->Word32Ror(), a, b);
232  }
233  Node* Word32Clz(Node* a) { return AddNode(machine()->Word32Clz(), a); }
234  Node* Word32Equal(Node* a, Node* b) {
235    return AddNode(machine()->Word32Equal(), a, b);
236  }
237  Node* Word32NotEqual(Node* a, Node* b) {
238    return Word32BinaryNot(Word32Equal(a, b));
239  }
240  Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
241  Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
242
243  Node* Word64And(Node* a, Node* b) {
244    return AddNode(machine()->Word64And(), a, b);
245  }
246  Node* Word64Or(Node* a, Node* b) {
247    return AddNode(machine()->Word64Or(), a, b);
248  }
249  Node* Word64Xor(Node* a, Node* b) {
250    return AddNode(machine()->Word64Xor(), a, b);
251  }
252  Node* Word64Shl(Node* a, Node* b) {
253    return AddNode(machine()->Word64Shl(), a, b);
254  }
255  Node* Word64Shr(Node* a, Node* b) {
256    return AddNode(machine()->Word64Shr(), a, b);
257  }
258  Node* Word64Sar(Node* a, Node* b) {
259    return AddNode(machine()->Word64Sar(), a, b);
260  }
261  Node* Word64Ror(Node* a, Node* b) {
262    return AddNode(machine()->Word64Ror(), a, b);
263  }
264  Node* Word64Clz(Node* a) { return AddNode(machine()->Word64Clz(), a); }
265  Node* Word64Equal(Node* a, Node* b) {
266    return AddNode(machine()->Word64Equal(), a, b);
267  }
268  Node* Word64NotEqual(Node* a, Node* b) {
269    return Word32BinaryNot(Word64Equal(a, b));
270  }
271  Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
272
273  Node* Int32Add(Node* a, Node* b) {
274    return AddNode(machine()->Int32Add(), a, b);
275  }
276  Node* Int32AddWithOverflow(Node* a, Node* b) {
277    return AddNode(machine()->Int32AddWithOverflow(), a, b);
278  }
279  Node* Int32Sub(Node* a, Node* b) {
280    return AddNode(machine()->Int32Sub(), a, b);
281  }
282  Node* Int32SubWithOverflow(Node* a, Node* b) {
283    return AddNode(machine()->Int32SubWithOverflow(), a, b);
284  }
285  Node* Int32Mul(Node* a, Node* b) {
286    return AddNode(machine()->Int32Mul(), a, b);
287  }
288  Node* Int32MulHigh(Node* a, Node* b) {
289    return AddNode(machine()->Int32MulHigh(), a, b);
290  }
291  Node* Int32MulWithOverflow(Node* a, Node* b) {
292    return AddNode(machine()->Int32MulWithOverflow(), a, b);
293  }
294  Node* Int32Div(Node* a, Node* b) {
295    return AddNode(machine()->Int32Div(), a, b);
296  }
297  Node* Int32Mod(Node* a, Node* b) {
298    return AddNode(machine()->Int32Mod(), a, b);
299  }
300  Node* Int32LessThan(Node* a, Node* b) {
301    return AddNode(machine()->Int32LessThan(), a, b);
302  }
303  Node* Int32LessThanOrEqual(Node* a, Node* b) {
304    return AddNode(machine()->Int32LessThanOrEqual(), a, b);
305  }
306  Node* Uint32Div(Node* a, Node* b) {
307    return AddNode(machine()->Uint32Div(), a, b);
308  }
309  Node* Uint32LessThan(Node* a, Node* b) {
310    return AddNode(machine()->Uint32LessThan(), a, b);
311  }
312  Node* Uint32LessThanOrEqual(Node* a, Node* b) {
313    return AddNode(machine()->Uint32LessThanOrEqual(), a, b);
314  }
315  Node* Uint32Mod(Node* a, Node* b) {
316    return AddNode(machine()->Uint32Mod(), a, b);
317  }
318  Node* Uint32MulHigh(Node* a, Node* b) {
319    return AddNode(machine()->Uint32MulHigh(), a, b);
320  }
321  Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
322  Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
323    return Int32LessThanOrEqual(b, a);
324  }
325  Node* Uint32GreaterThan(Node* a, Node* b) { return Uint32LessThan(b, a); }
326  Node* Uint32GreaterThanOrEqual(Node* a, Node* b) {
327    return Uint32LessThanOrEqual(b, a);
328  }
329  Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
330
331  Node* Int64Add(Node* a, Node* b) {
332    return AddNode(machine()->Int64Add(), a, b);
333  }
334  Node* Int64AddWithOverflow(Node* a, Node* b) {
335    return AddNode(machine()->Int64AddWithOverflow(), a, b);
336  }
337  Node* Int64Sub(Node* a, Node* b) {
338    return AddNode(machine()->Int64Sub(), a, b);
339  }
340  Node* Int64SubWithOverflow(Node* a, Node* b) {
341    return AddNode(machine()->Int64SubWithOverflow(), a, b);
342  }
343  Node* Int64Mul(Node* a, Node* b) {
344    return AddNode(machine()->Int64Mul(), a, b);
345  }
346  Node* Int64Div(Node* a, Node* b) {
347    return AddNode(machine()->Int64Div(), a, b);
348  }
349  Node* Int64Mod(Node* a, Node* b) {
350    return AddNode(machine()->Int64Mod(), a, b);
351  }
352  Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
353  Node* Int64LessThan(Node* a, Node* b) {
354    return AddNode(machine()->Int64LessThan(), a, b);
355  }
356  Node* Int64LessThanOrEqual(Node* a, Node* b) {
357    return AddNode(machine()->Int64LessThanOrEqual(), a, b);
358  }
359  Node* Uint64LessThan(Node* a, Node* b) {
360    return AddNode(machine()->Uint64LessThan(), a, b);
361  }
362  Node* Uint64LessThanOrEqual(Node* a, Node* b) {
363    return AddNode(machine()->Uint64LessThanOrEqual(), a, b);
364  }
365  Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
366  Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
367    return Int64LessThanOrEqual(b, a);
368  }
369  Node* Uint64GreaterThan(Node* a, Node* b) { return Uint64LessThan(b, a); }
370  Node* Uint64GreaterThanOrEqual(Node* a, Node* b) {
371    return Uint64LessThanOrEqual(b, a);
372  }
373  Node* Uint64Div(Node* a, Node* b) {
374    return AddNode(machine()->Uint64Div(), a, b);
375  }
376  Node* Uint64Mod(Node* a, Node* b) {
377    return AddNode(machine()->Uint64Mod(), a, b);
378  }
379  Node* Int32PairAdd(Node* a_low, Node* a_high, Node* b_low, Node* b_high) {
380    return AddNode(machine()->Int32PairAdd(), a_low, a_high, b_low, b_high);
381  }
382  Node* Int32PairSub(Node* a_low, Node* a_high, Node* b_low, Node* b_high) {
383    return AddNode(machine()->Int32PairSub(), a_low, a_high, b_low, b_high);
384  }
385  Node* Int32PairMul(Node* a_low, Node* a_high, Node* b_low, Node* b_high) {
386    return AddNode(machine()->Int32PairMul(), a_low, a_high, b_low, b_high);
387  }
388  Node* Word32PairShl(Node* low_word, Node* high_word, Node* shift) {
389    return AddNode(machine()->Word32PairShl(), low_word, high_word, shift);
390  }
391  Node* Word32PairShr(Node* low_word, Node* high_word, Node* shift) {
392    return AddNode(machine()->Word32PairShr(), low_word, high_word, shift);
393  }
394  Node* Word32PairSar(Node* low_word, Node* high_word, Node* shift) {
395    return AddNode(machine()->Word32PairSar(), low_word, high_word, shift);
396  }
397
398#define INTPTR_BINOP(prefix, name)                     \
399  Node* IntPtr##name(Node* a, Node* b) {               \
400    return kPointerSize == 8 ? prefix##64##name(a, b)  \
401                             : prefix##32##name(a, b); \
402  }
403
404  INTPTR_BINOP(Int, Add);
405  INTPTR_BINOP(Int, AddWithOverflow);
406  INTPTR_BINOP(Int, Sub);
407  INTPTR_BINOP(Int, SubWithOverflow);
408  INTPTR_BINOP(Int, Mul);
409  INTPTR_BINOP(Int, Div);
410  INTPTR_BINOP(Int, LessThan);
411  INTPTR_BINOP(Int, LessThanOrEqual);
412  INTPTR_BINOP(Word, Equal);
413  INTPTR_BINOP(Word, NotEqual);
414  INTPTR_BINOP(Int, GreaterThanOrEqual);
415  INTPTR_BINOP(Int, GreaterThan);
416
417#undef INTPTR_BINOP
418
419#define UINTPTR_BINOP(prefix, name)                    \
420  Node* UintPtr##name(Node* a, Node* b) {              \
421    return kPointerSize == 8 ? prefix##64##name(a, b)  \
422                             : prefix##32##name(a, b); \
423  }
424
425  UINTPTR_BINOP(Uint, LessThan);
426  UINTPTR_BINOP(Uint, LessThanOrEqual);
427  UINTPTR_BINOP(Uint, GreaterThanOrEqual);
428  UINTPTR_BINOP(Uint, GreaterThan);
429
430#undef UINTPTR_BINOP
431
432  Node* Float32Add(Node* a, Node* b) {
433    return AddNode(machine()->Float32Add(), a, b);
434  }
435  Node* Float32Sub(Node* a, Node* b) {
436    return AddNode(machine()->Float32Sub(), a, b);
437  }
438  Node* Float32Mul(Node* a, Node* b) {
439    return AddNode(machine()->Float32Mul(), a, b);
440  }
441  Node* Float32Div(Node* a, Node* b) {
442    return AddNode(machine()->Float32Div(), a, b);
443  }
444  Node* Float32Abs(Node* a) { return AddNode(machine()->Float32Abs(), a); }
445  Node* Float32Neg(Node* a) { return AddNode(machine()->Float32Neg(), a); }
446  Node* Float32Sqrt(Node* a) { return AddNode(machine()->Float32Sqrt(), a); }
447  Node* Float32Equal(Node* a, Node* b) {
448    return AddNode(machine()->Float32Equal(), a, b);
449  }
450  Node* Float32NotEqual(Node* a, Node* b) {
451    return Word32BinaryNot(Float32Equal(a, b));
452  }
453  Node* Float32LessThan(Node* a, Node* b) {
454    return AddNode(machine()->Float32LessThan(), a, b);
455  }
456  Node* Float32LessThanOrEqual(Node* a, Node* b) {
457    return AddNode(machine()->Float32LessThanOrEqual(), a, b);
458  }
459  Node* Float32GreaterThan(Node* a, Node* b) { return Float32LessThan(b, a); }
460  Node* Float32GreaterThanOrEqual(Node* a, Node* b) {
461    return Float32LessThanOrEqual(b, a);
462  }
463  Node* Float32Max(Node* a, Node* b) {
464    return AddNode(machine()->Float32Max(), a, b);
465  }
466  Node* Float32Min(Node* a, Node* b) {
467    return AddNode(machine()->Float32Min(), a, b);
468  }
469  Node* Float64Add(Node* a, Node* b) {
470    return AddNode(machine()->Float64Add(), a, b);
471  }
472  Node* Float64Sub(Node* a, Node* b) {
473    return AddNode(machine()->Float64Sub(), a, b);
474  }
475  Node* Float64Mul(Node* a, Node* b) {
476    return AddNode(machine()->Float64Mul(), a, b);
477  }
478  Node* Float64Div(Node* a, Node* b) {
479    return AddNode(machine()->Float64Div(), a, b);
480  }
481  Node* Float64Mod(Node* a, Node* b) {
482    return AddNode(machine()->Float64Mod(), a, b);
483  }
484  Node* Float64Max(Node* a, Node* b) {
485    return AddNode(machine()->Float64Max(), a, b);
486  }
487  Node* Float64Min(Node* a, Node* b) {
488    return AddNode(machine()->Float64Min(), a, b);
489  }
490  Node* Float64Abs(Node* a) { return AddNode(machine()->Float64Abs(), a); }
491  Node* Float64Neg(Node* a) { return AddNode(machine()->Float64Neg(), a); }
492  Node* Float64Acos(Node* a) { return AddNode(machine()->Float64Acos(), a); }
493  Node* Float64Acosh(Node* a) { return AddNode(machine()->Float64Acosh(), a); }
494  Node* Float64Asin(Node* a) { return AddNode(machine()->Float64Asin(), a); }
495  Node* Float64Asinh(Node* a) { return AddNode(machine()->Float64Asinh(), a); }
496  Node* Float64Atan(Node* a) { return AddNode(machine()->Float64Atan(), a); }
497  Node* Float64Atanh(Node* a) { return AddNode(machine()->Float64Atanh(), a); }
498  Node* Float64Atan2(Node* a, Node* b) {
499    return AddNode(machine()->Float64Atan2(), a, b);
500  }
501  Node* Float64Cbrt(Node* a) { return AddNode(machine()->Float64Cbrt(), a); }
502  Node* Float64Cos(Node* a) { return AddNode(machine()->Float64Cos(), a); }
503  Node* Float64Cosh(Node* a) { return AddNode(machine()->Float64Cosh(), a); }
504  Node* Float64Exp(Node* a) { return AddNode(machine()->Float64Exp(), a); }
505  Node* Float64Expm1(Node* a) { return AddNode(machine()->Float64Expm1(), a); }
506  Node* Float64Log(Node* a) { return AddNode(machine()->Float64Log(), a); }
507  Node* Float64Log1p(Node* a) { return AddNode(machine()->Float64Log1p(), a); }
508  Node* Float64Log10(Node* a) { return AddNode(machine()->Float64Log10(), a); }
509  Node* Float64Log2(Node* a) { return AddNode(machine()->Float64Log2(), a); }
510  Node* Float64Pow(Node* a, Node* b) {
511    return AddNode(machine()->Float64Pow(), a, b);
512  }
513  Node* Float64Sin(Node* a) { return AddNode(machine()->Float64Sin(), a); }
514  Node* Float64Sinh(Node* a) { return AddNode(machine()->Float64Sinh(), a); }
515  Node* Float64Sqrt(Node* a) { return AddNode(machine()->Float64Sqrt(), a); }
516  Node* Float64Tan(Node* a) { return AddNode(machine()->Float64Tan(), a); }
517  Node* Float64Tanh(Node* a) { return AddNode(machine()->Float64Tanh(), a); }
518  Node* Float64Equal(Node* a, Node* b) {
519    return AddNode(machine()->Float64Equal(), a, b);
520  }
521  Node* Float64NotEqual(Node* a, Node* b) {
522    return Word32BinaryNot(Float64Equal(a, b));
523  }
524  Node* Float64LessThan(Node* a, Node* b) {
525    return AddNode(machine()->Float64LessThan(), a, b);
526  }
527  Node* Float64LessThanOrEqual(Node* a, Node* b) {
528    return AddNode(machine()->Float64LessThanOrEqual(), a, b);
529  }
530  Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
531  Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
532    return Float64LessThanOrEqual(b, a);
533  }
534
535  // Conversions.
536  Node* BitcastTaggedToWord(Node* a) {
537    return AddNode(machine()->BitcastTaggedToWord(), a);
538  }
539  Node* BitcastWordToTagged(Node* a) {
540    return AddNode(machine()->BitcastWordToTagged(), a);
541  }
542  Node* BitcastWordToTaggedSigned(Node* a) {
543    return AddNode(machine()->BitcastWordToTaggedSigned(), a);
544  }
545  Node* TruncateFloat64ToWord32(Node* a) {
546    return AddNode(machine()->TruncateFloat64ToWord32(), a);
547  }
548  Node* ChangeFloat32ToFloat64(Node* a) {
549    return AddNode(machine()->ChangeFloat32ToFloat64(), a);
550  }
551  Node* ChangeInt32ToFloat64(Node* a) {
552    return AddNode(machine()->ChangeInt32ToFloat64(), a);
553  }
554  Node* ChangeUint32ToFloat64(Node* a) {
555    return AddNode(machine()->ChangeUint32ToFloat64(), a);
556  }
557  Node* ChangeFloat64ToInt32(Node* a) {
558    return AddNode(machine()->ChangeFloat64ToInt32(), a);
559  }
560  Node* ChangeFloat64ToUint32(Node* a) {
561    return AddNode(machine()->ChangeFloat64ToUint32(), a);
562  }
563  Node* TruncateFloat64ToUint32(Node* a) {
564    return AddNode(machine()->TruncateFloat64ToUint32(), a);
565  }
566  Node* TruncateFloat32ToInt32(Node* a) {
567    return AddNode(machine()->TruncateFloat32ToInt32(), a);
568  }
569  Node* TruncateFloat32ToUint32(Node* a) {
570    return AddNode(machine()->TruncateFloat32ToUint32(), a);
571  }
572  Node* TryTruncateFloat32ToInt64(Node* a) {
573    return AddNode(machine()->TryTruncateFloat32ToInt64(), a);
574  }
575  Node* TryTruncateFloat64ToInt64(Node* a) {
576    return AddNode(machine()->TryTruncateFloat64ToInt64(), a);
577  }
578  Node* TryTruncateFloat32ToUint64(Node* a) {
579    return AddNode(machine()->TryTruncateFloat32ToUint64(), a);
580  }
581  Node* TryTruncateFloat64ToUint64(Node* a) {
582    return AddNode(machine()->TryTruncateFloat64ToUint64(), a);
583  }
584  Node* ChangeInt32ToInt64(Node* a) {
585    return AddNode(machine()->ChangeInt32ToInt64(), a);
586  }
587  Node* ChangeUint32ToUint64(Node* a) {
588    return AddNode(machine()->ChangeUint32ToUint64(), a);
589  }
590  Node* TruncateFloat64ToFloat32(Node* a) {
591    return AddNode(machine()->TruncateFloat64ToFloat32(), a);
592  }
593  Node* TruncateInt64ToInt32(Node* a) {
594    return AddNode(machine()->TruncateInt64ToInt32(), a);
595  }
596  Node* RoundFloat64ToInt32(Node* a) {
597    return AddNode(machine()->RoundFloat64ToInt32(), a);
598  }
599  Node* RoundInt32ToFloat32(Node* a) {
600    return AddNode(machine()->RoundInt32ToFloat32(), a);
601  }
602  Node* RoundInt64ToFloat32(Node* a) {
603    return AddNode(machine()->RoundInt64ToFloat32(), a);
604  }
605  Node* RoundInt64ToFloat64(Node* a) {
606    return AddNode(machine()->RoundInt64ToFloat64(), a);
607  }
608  Node* RoundUint32ToFloat32(Node* a) {
609    return AddNode(machine()->RoundUint32ToFloat32(), a);
610  }
611  Node* RoundUint64ToFloat32(Node* a) {
612    return AddNode(machine()->RoundUint64ToFloat32(), a);
613  }
614  Node* RoundUint64ToFloat64(Node* a) {
615    return AddNode(machine()->RoundUint64ToFloat64(), a);
616  }
617  Node* BitcastFloat32ToInt32(Node* a) {
618    return AddNode(machine()->BitcastFloat32ToInt32(), a);
619  }
620  Node* BitcastFloat64ToInt64(Node* a) {
621    return AddNode(machine()->BitcastFloat64ToInt64(), a);
622  }
623  Node* BitcastInt32ToFloat32(Node* a) {
624    return AddNode(machine()->BitcastInt32ToFloat32(), a);
625  }
626  Node* BitcastInt64ToFloat64(Node* a) {
627    return AddNode(machine()->BitcastInt64ToFloat64(), a);
628  }
629  Node* Float32RoundDown(Node* a) {
630    return AddNode(machine()->Float32RoundDown().op(), a);
631  }
632  Node* Float64RoundDown(Node* a) {
633    return AddNode(machine()->Float64RoundDown().op(), a);
634  }
635  Node* Float32RoundUp(Node* a) {
636    return AddNode(machine()->Float32RoundUp().op(), a);
637  }
638  Node* Float64RoundUp(Node* a) {
639    return AddNode(machine()->Float64RoundUp().op(), a);
640  }
641  Node* Float32RoundTruncate(Node* a) {
642    return AddNode(machine()->Float32RoundTruncate().op(), a);
643  }
644  Node* Float64RoundTruncate(Node* a) {
645    return AddNode(machine()->Float64RoundTruncate().op(), a);
646  }
647  Node* Float64RoundTiesAway(Node* a) {
648    return AddNode(machine()->Float64RoundTiesAway().op(), a);
649  }
650  Node* Float32RoundTiesEven(Node* a) {
651    return AddNode(machine()->Float32RoundTiesEven().op(), a);
652  }
653  Node* Float64RoundTiesEven(Node* a) {
654    return AddNode(machine()->Float64RoundTiesEven().op(), a);
655  }
656
657  // Float64 bit operations.
658  Node* Float64ExtractLowWord32(Node* a) {
659    return AddNode(machine()->Float64ExtractLowWord32(), a);
660  }
661  Node* Float64ExtractHighWord32(Node* a) {
662    return AddNode(machine()->Float64ExtractHighWord32(), a);
663  }
664  Node* Float64InsertLowWord32(Node* a, Node* b) {
665    return AddNode(machine()->Float64InsertLowWord32(), a, b);
666  }
667  Node* Float64InsertHighWord32(Node* a, Node* b) {
668    return AddNode(machine()->Float64InsertHighWord32(), a, b);
669  }
670  Node* Float64SilenceNaN(Node* a) {
671    return AddNode(machine()->Float64SilenceNaN(), a);
672  }
673
674  // Stack operations.
675  Node* LoadStackPointer() { return AddNode(machine()->LoadStackPointer()); }
676  Node* LoadFramePointer() { return AddNode(machine()->LoadFramePointer()); }
677  Node* LoadParentFramePointer() {
678    return AddNode(machine()->LoadParentFramePointer());
679  }
680
681  // Parameters.
682  Node* Parameter(size_t index);
683
684  // Pointer utilities.
685  Node* LoadFromPointer(void* address, MachineType rep, int32_t offset = 0) {
686    return Load(rep, PointerConstant(address), Int32Constant(offset));
687  }
688  Node* StoreToPointer(void* address, MachineRepresentation rep, Node* node) {
689    return Store(rep, PointerConstant(address), node, kNoWriteBarrier);
690  }
691  Node* UnalignedLoadFromPointer(void* address, MachineType rep,
692                                 int32_t offset = 0) {
693    return UnalignedLoad(rep, PointerConstant(address), Int32Constant(offset));
694  }
695  Node* UnalignedStoreToPointer(void* address, MachineRepresentation rep,
696                                Node* node) {
697    return UnalignedStore(rep, PointerConstant(address), node);
698  }
699  Node* StringConstant(const char* string) {
700    return HeapConstant(isolate()->factory()->InternalizeUtf8String(string));
701  }
702
703  // Call a given call descriptor and the given arguments.
704  Node* CallN(CallDescriptor* desc, Node* function, Node** args);
705  // Call a given call descriptor and the given arguments and frame-state.
706  Node* CallNWithFrameState(CallDescriptor* desc, Node* function, Node** args,
707                            Node* frame_state);
708  // Call to a runtime function with zero arguments.
709  Node* CallRuntime0(Runtime::FunctionId function, Node* context);
710  // Call to a runtime function with one arguments.
711  Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context);
712  // Call to a runtime function with two arguments.
713  Node* CallRuntime2(Runtime::FunctionId function, Node* arg1, Node* arg2,
714                     Node* context);
715  // Call to a runtime function with three arguments.
716  Node* CallRuntime3(Runtime::FunctionId function, Node* arg1, Node* arg2,
717                     Node* arg3, Node* context);
718  // Call to a runtime function with four arguments.
719  Node* CallRuntime4(Runtime::FunctionId function, Node* arg1, Node* arg2,
720                     Node* arg3, Node* arg4, Node* context);
721  // Call to a runtime function with five arguments.
722  Node* CallRuntime5(Runtime::FunctionId function, Node* arg1, Node* arg2,
723                     Node* arg3, Node* arg4, Node* arg5, Node* context);
724  // Call to a C function with zero arguments.
725  Node* CallCFunction0(MachineType return_type, Node* function);
726  // Call to a C function with one parameter.
727  Node* CallCFunction1(MachineType return_type, MachineType arg0_type,
728                       Node* function, Node* arg0);
729  // Call to a C function with two arguments.
730  Node* CallCFunction2(MachineType return_type, MachineType arg0_type,
731                       MachineType arg1_type, Node* function, Node* arg0,
732                       Node* arg1);
733  // Call to a C function with eight arguments.
734  Node* CallCFunction8(MachineType return_type, MachineType arg0_type,
735                       MachineType arg1_type, MachineType arg2_type,
736                       MachineType arg3_type, MachineType arg4_type,
737                       MachineType arg5_type, MachineType arg6_type,
738                       MachineType arg7_type, Node* function, Node* arg0,
739                       Node* arg1, Node* arg2, Node* arg3, Node* arg4,
740                       Node* arg5, Node* arg6, Node* arg7);
741
742  // Tail call the given call descriptor and the given arguments.
743  Node* TailCallN(CallDescriptor* call_descriptor, Node* function, Node** args);
744  // Tail call to a runtime function with zero arguments.
745  Node* TailCallRuntime0(Runtime::FunctionId function, Node* context);
746  // Tail call to a runtime function with one argument.
747  Node* TailCallRuntime1(Runtime::FunctionId function, Node* arg0,
748                         Node* context);
749  // Tail call to a runtime function with two arguments.
750  Node* TailCallRuntime2(Runtime::FunctionId function, Node* arg1, Node* arg2,
751                         Node* context);
752  // Tail call to a runtime function with three arguments.
753  Node* TailCallRuntime3(Runtime::FunctionId function, Node* arg1, Node* arg2,
754                         Node* arg3, Node* context);
755  // Tail call to a runtime function with four arguments.
756  Node* TailCallRuntime4(Runtime::FunctionId function, Node* arg1, Node* arg2,
757                         Node* arg3, Node* arg4, Node* context);
758  // Tail call to a runtime function with five arguments.
759  Node* TailCallRuntime5(Runtime::FunctionId function, Node* arg1, Node* arg2,
760                         Node* arg3, Node* arg4, Node* arg5, Node* context);
761  // Tail call to a runtime function with six arguments.
762  Node* TailCallRuntime6(Runtime::FunctionId function, Node* arg1, Node* arg2,
763                         Node* arg3, Node* arg4, Node* arg5, Node* arg6,
764                         Node* context);
765
766  // ===========================================================================
767  // The following utility methods deal with control flow, hence might switch
768  // the current basic block or create new basic blocks for labels.
769
770  // Control flow.
771  void Goto(RawMachineLabel* label);
772  void Branch(Node* condition, RawMachineLabel* true_val,
773              RawMachineLabel* false_val);
774  void Switch(Node* index, RawMachineLabel* default_label,
775              const int32_t* case_values, RawMachineLabel** case_labels,
776              size_t case_count);
777  void Return(Node* value);
778  void Return(Node* v1, Node* v2);
779  void Return(Node* v1, Node* v2, Node* v3);
780  void PopAndReturn(Node* pop, Node* value);
781  void PopAndReturn(Node* pop, Node* v1, Node* v2);
782  void PopAndReturn(Node* pop, Node* v1, Node* v2, Node* v3);
783  void Bind(RawMachineLabel* label);
784  void Deoptimize(Node* state);
785  void DebugBreak();
786  void Comment(const char* msg);
787
788  // Add success / exception successor blocks and ends the current block ending
789  // in a potentially throwing call node.
790  void Continuations(Node* call, RawMachineLabel* if_success,
791                     RawMachineLabel* if_exception);
792
793  // Variables.
794  Node* Phi(MachineRepresentation rep, Node* n1, Node* n2) {
795    return AddNode(common()->Phi(rep, 2), n1, n2, graph()->start());
796  }
797  Node* Phi(MachineRepresentation rep, Node* n1, Node* n2, Node* n3) {
798    return AddNode(common()->Phi(rep, 3), n1, n2, n3, graph()->start());
799  }
800  Node* Phi(MachineRepresentation rep, Node* n1, Node* n2, Node* n3, Node* n4) {
801    return AddNode(common()->Phi(rep, 4), n1, n2, n3, n4, graph()->start());
802  }
803  Node* Phi(MachineRepresentation rep, int input_count, Node* const* inputs);
804  void AppendPhiInput(Node* phi, Node* new_input);
805
806  // ===========================================================================
807  // The following generic node creation methods can be used for operators that
808  // are not covered by the above utility methods. There should rarely be a need
809  // to do that outside of testing though.
810
811  Node* AddNode(const Operator* op, int input_count, Node* const* inputs);
812
813  Node* AddNode(const Operator* op) {
814    return AddNode(op, 0, static_cast<Node* const*>(nullptr));
815  }
816
817  template <class... TArgs>
818  Node* AddNode(const Operator* op, Node* n1, TArgs... args) {
819    Node* buffer[] = {n1, args...};
820    return AddNode(op, sizeof...(args) + 1, buffer);
821  }
822
823 private:
824  Node* MakeNode(const Operator* op, int input_count, Node* const* inputs);
825  BasicBlock* Use(RawMachineLabel* label);
826  BasicBlock* EnsureBlock(RawMachineLabel* label);
827  BasicBlock* CurrentBlock();
828
829  Schedule* schedule() { return schedule_; }
830  size_t parameter_count() const { return call_descriptor_->ParameterCount(); }
831
832  Isolate* isolate_;
833  Graph* graph_;
834  Schedule* schedule_;
835  MachineOperatorBuilder machine_;
836  CommonOperatorBuilder common_;
837  CallDescriptor* call_descriptor_;
838  NodeVector parameters_;
839  BasicBlock* current_block_;
840
841  DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler);
842};
843
844class V8_EXPORT_PRIVATE RawMachineLabel final {
845 public:
846  enum Type { kDeferred, kNonDeferred };
847
848  explicit RawMachineLabel(Type type = kNonDeferred)
849      : deferred_(type == kDeferred) {}
850  ~RawMachineLabel();
851
852 private:
853  BasicBlock* block_ = nullptr;
854  bool used_ = false;
855  bool bound_ = false;
856  bool deferred_;
857  friend class RawMachineAssembler;
858  DISALLOW_COPY_AND_ASSIGN(RawMachineLabel);
859};
860
861}  // namespace compiler
862}  // namespace internal
863}  // namespace v8
864
865#endif  // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_
866