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.
5 #ifndef V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_
6 #define V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_
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"
24 class RawMachineAssembler : public GraphBuilder {
28 Label() : block_(NULL), used_(false), bound_(false) {}
29 ~Label() { DCHECK(bound_ || !used_); }
31 BasicBlock* block() { return block_; }
34 // Private constructor for exit label.
35 explicit Label(BasicBlock* block)
36 : block_(block), used_(false), bound_(false) {}
41 friend class RawMachineAssembler;
42 DISALLOW_COPY_AND_ASSIGN(Label);
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 {}
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_; }
59 Node* UndefinedConstant() {
60 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(
61 isolate()->factory()->undefined_value());
62 return NewNode(common()->HeapConstant(unique));
66 Node* PointerConstant(void* value) {
67 return IntPtrConstant(reinterpret_cast<intptr_t>(value));
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));
74 Node* Int32Constant(int32_t value) {
75 return NewNode(common()->Int32Constant(value));
77 Node* Int64Constant(int64_t value) {
78 return NewNode(common()->Int64Constant(value));
80 Node* NumberConstant(double value) {
81 return NewNode(common()->NumberConstant(value));
83 Node* Float32Constant(float value) {
84 return NewNode(common()->Float32Constant(value));
86 Node* Float64Constant(double value) {
87 return NewNode(common()->Float64Constant(value));
89 Node* HeapConstant(Handle<HeapObject> object) {
90 Unique<HeapObject> val = Unique<HeapObject>::CreateUninitialized(object);
91 return NewNode(common()->HeapConstant(val));
93 Node* ExternalConstant(ExternalReference address) {
94 return NewNode(common()->ExternalConstant(address));
97 Node* Projection(int index, Node* a) {
98 return NewNode(common()->Projection(index), a);
101 // Memory Operations.
102 Node* Load(MachineType rep, Node* base) {
103 return Load(rep, base, IntPtrConstant(0));
105 Node* Load(MachineType rep, Node* base, Node* index) {
106 return NewNode(machine()->Load(rep), base, index, graph()->start(),
109 void Store(MachineType rep, Node* base, Node* value) {
110 Store(rep, base, IntPtrConstant(0), value);
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());
116 // Arithmetic Operations.
117 Node* WordAnd(Node* a, Node* b) {
118 return NewNode(machine()->WordAnd(), a, b);
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);
124 Node* WordShl(Node* a, Node* b) {
125 return NewNode(machine()->WordShl(), a, b);
127 Node* WordShr(Node* a, Node* b) {
128 return NewNode(machine()->WordShr(), a, b);
130 Node* WordSar(Node* a, Node* b) {
131 return NewNode(machine()->WordSar(), a, b);
133 Node* WordRor(Node* a, Node* b) {
134 return NewNode(machine()->WordRor(), a, b);
136 Node* WordEqual(Node* a, Node* b) {
137 return NewNode(machine()->WordEqual(), a, b);
139 Node* WordNotEqual(Node* a, Node* b) {
140 return WordBinaryNot(WordEqual(a, b));
142 Node* WordNot(Node* a) {
143 if (machine()->Is32()) {
149 Node* WordBinaryNot(Node* a) {
150 if (machine()->Is32()) {
151 return Word32BinaryNot(a);
153 return Word64BinaryNot(a);
157 Node* Word32And(Node* a, Node* b) {
158 return NewNode(machine()->Word32And(), a, b);
160 Node* Word32Or(Node* a, Node* b) {
161 return NewNode(machine()->Word32Or(), a, b);
163 Node* Word32Xor(Node* a, Node* b) {
164 return NewNode(machine()->Word32Xor(), a, b);
166 Node* Word32Shl(Node* a, Node* b) {
167 return NewNode(machine()->Word32Shl(), a, b);
169 Node* Word32Shr(Node* a, Node* b) {
170 return NewNode(machine()->Word32Shr(), a, b);
172 Node* Word32Sar(Node* a, Node* b) {
173 return NewNode(machine()->Word32Sar(), a, b);
175 Node* Word32Ror(Node* a, Node* b) {
176 return NewNode(machine()->Word32Ror(), a, b);
178 Node* Word32Clz(Node* a) { return NewNode(machine()->Word32Clz(), a); }
179 Node* Word32Equal(Node* a, Node* b) {
180 return NewNode(machine()->Word32Equal(), a, b);
182 Node* Word32NotEqual(Node* a, Node* b) {
183 return Word32BinaryNot(Word32Equal(a, b));
185 Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
186 Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
188 Node* Word64And(Node* a, Node* b) {
189 return NewNode(machine()->Word64And(), a, b);
191 Node* Word64Or(Node* a, Node* b) {
192 return NewNode(machine()->Word64Or(), a, b);
194 Node* Word64Xor(Node* a, Node* b) {
195 return NewNode(machine()->Word64Xor(), a, b);
197 Node* Word64Shl(Node* a, Node* b) {
198 return NewNode(machine()->Word64Shl(), a, b);
200 Node* Word64Shr(Node* a, Node* b) {
201 return NewNode(machine()->Word64Shr(), a, b);
203 Node* Word64Sar(Node* a, Node* b) {
204 return NewNode(machine()->Word64Sar(), a, b);
206 Node* Word64Ror(Node* a, Node* b) {
207 return NewNode(machine()->Word64Ror(), a, b);
209 Node* Word64Equal(Node* a, Node* b) {
210 return NewNode(machine()->Word64Equal(), a, b);
212 Node* Word64NotEqual(Node* a, Node* b) {
213 return Word64BinaryNot(Word64Equal(a, b));
215 Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
216 Node* Word64BinaryNot(Node* a) { return Word64Equal(a, Int64Constant(0)); }
218 Node* Int32Add(Node* a, Node* b) {
219 return NewNode(machine()->Int32Add(), a, b);
221 Node* Int32AddWithOverflow(Node* a, Node* b) {
222 return NewNode(machine()->Int32AddWithOverflow(), a, b);
224 Node* Int32Sub(Node* a, Node* b) {
225 return NewNode(machine()->Int32Sub(), a, b);
227 Node* Int32SubWithOverflow(Node* a, Node* b) {
228 return NewNode(machine()->Int32SubWithOverflow(), a, b);
230 Node* Int32Mul(Node* a, Node* b) {
231 return NewNode(machine()->Int32Mul(), a, b);
233 Node* Int32MulHigh(Node* a, Node* b) {
234 return NewNode(machine()->Int32MulHigh(), a, b);
236 Node* Int32Div(Node* a, Node* b) {
237 return NewNode(machine()->Int32Div(), a, b, graph()->start());
239 Node* Int32Mod(Node* a, Node* b) {
240 return NewNode(machine()->Int32Mod(), a, b, graph()->start());
242 Node* Int32LessThan(Node* a, Node* b) {
243 return NewNode(machine()->Int32LessThan(), a, b);
245 Node* Int32LessThanOrEqual(Node* a, Node* b) {
246 return NewNode(machine()->Int32LessThanOrEqual(), a, b);
248 Node* Uint32Div(Node* a, Node* b) {
249 return NewNode(machine()->Uint32Div(), a, b, graph()->start());
251 Node* Uint32LessThan(Node* a, Node* b) {
252 return NewNode(machine()->Uint32LessThan(), a, b);
254 Node* Uint32LessThanOrEqual(Node* a, Node* b) {
255 return NewNode(machine()->Uint32LessThanOrEqual(), a, b);
257 Node* Uint32Mod(Node* a, Node* b) {
258 return NewNode(machine()->Uint32Mod(), a, b, graph()->start());
260 Node* Uint32MulHigh(Node* a, Node* b) {
261 return NewNode(machine()->Uint32MulHigh(), a, b);
263 Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
264 Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
265 return Int32LessThanOrEqual(b, a);
267 Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
269 Node* Int64Add(Node* a, Node* b) {
270 return NewNode(machine()->Int64Add(), a, b);
272 Node* Int64Sub(Node* a, Node* b) {
273 return NewNode(machine()->Int64Sub(), a, b);
275 Node* Int64Mul(Node* a, Node* b) {
276 return NewNode(machine()->Int64Mul(), a, b);
278 Node* Int64Div(Node* a, Node* b) {
279 return NewNode(machine()->Int64Div(), a, b);
281 Node* Int64Mod(Node* a, Node* b) {
282 return NewNode(machine()->Int64Mod(), a, b);
284 Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
285 Node* Int64LessThan(Node* a, Node* b) {
286 return NewNode(machine()->Int64LessThan(), a, b);
288 Node* Uint64LessThan(Node* a, Node* b) {
289 return NewNode(machine()->Uint64LessThan(), a, b);
291 Node* Int64LessThanOrEqual(Node* a, Node* b) {
292 return NewNode(machine()->Int64LessThanOrEqual(), a, b);
294 Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
295 Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
296 return Int64LessThanOrEqual(b, a);
298 Node* Uint64Div(Node* a, Node* b) {
299 return NewNode(machine()->Uint64Div(), a, b);
301 Node* Uint64Mod(Node* a, Node* b) {
302 return NewNode(machine()->Uint64Mod(), a, b);
305 // TODO(turbofan): What is this used for?
306 Node* ConvertIntPtrToInt32(Node* a) {
307 return kPointerSize == 8 ? NewNode(machine()->TruncateInt64ToInt32(), a)
310 Node* ConvertInt32ToIntPtr(Node* a) {
311 return kPointerSize == 8 ? NewNode(machine()->ChangeInt32ToInt64(), a) : a;
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); \
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);
331 Node* Float64Add(Node* a, Node* b) {
332 return NewNode(machine()->Float64Add(), a, b);
334 Node* Float64Sub(Node* a, Node* b) {
335 return NewNode(machine()->Float64Sub(), a, b);
337 Node* Float64Mul(Node* a, Node* b) {
338 return NewNode(machine()->Float64Mul(), a, b);
340 Node* Float64Div(Node* a, Node* b) {
341 return NewNode(machine()->Float64Div(), a, b);
343 Node* Float64Mod(Node* a, Node* b) {
344 return NewNode(machine()->Float64Mod(), a, b);
346 Node* Float64Equal(Node* a, Node* b) {
347 return NewNode(machine()->Float64Equal(), a, b);
349 Node* Float64NotEqual(Node* a, Node* b) {
350 return WordBinaryNot(Float64Equal(a, b));
352 Node* Float64LessThan(Node* a, Node* b) {
353 return NewNode(machine()->Float64LessThan(), a, b);
355 Node* Float64LessThanOrEqual(Node* a, Node* b) {
356 return NewNode(machine()->Float64LessThanOrEqual(), a, b);
358 Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
359 Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
360 return Float64LessThanOrEqual(b, a);
364 Node* ChangeFloat32ToFloat64(Node* a) {
365 return NewNode(machine()->ChangeFloat32ToFloat64(), a);
367 Node* ChangeInt32ToFloat64(Node* a) {
368 return NewNode(machine()->ChangeInt32ToFloat64(), a);
370 Node* ChangeUint32ToFloat64(Node* a) {
371 return NewNode(machine()->ChangeUint32ToFloat64(), a);
373 Node* ChangeFloat64ToInt32(Node* a) {
374 return NewNode(machine()->ChangeFloat64ToInt32(), a);
376 Node* ChangeFloat64ToUint32(Node* a) {
377 return NewNode(machine()->ChangeFloat64ToUint32(), a);
379 Node* ChangeInt32ToInt64(Node* a) {
380 return NewNode(machine()->ChangeInt32ToInt64(), a);
382 Node* ChangeUint32ToUint64(Node* a) {
383 return NewNode(machine()->ChangeUint32ToUint64(), a);
385 Node* TruncateFloat64ToFloat32(Node* a) {
386 return NewNode(machine()->TruncateFloat64ToFloat32(), a);
388 Node* TruncateFloat64ToInt32(Node* a) {
389 return NewNode(machine()->TruncateFloat64ToInt32(), a);
391 Node* TruncateInt64ToInt32(Node* a) {
392 return NewNode(machine()->TruncateInt64ToInt32(), a);
394 Node* Float64RoundDown(Node* a) {
395 return NewNode(machine()->Float64RoundDown(), a);
397 Node* Float64RoundTruncate(Node* a) {
398 return NewNode(machine()->Float64RoundTruncate(), a);
400 Node* Float64RoundTiesAway(Node* a) {
401 return NewNode(machine()->Float64RoundTiesAway(), a);
404 // Float64 bit operations.
405 Node* Float64ExtractLowWord32(Node* a) {
406 return NewNode(machine()->Float64ExtractLowWord32(), a);
408 Node* Float64ExtractHighWord32(Node* a) {
409 return NewNode(machine()->Float64ExtractHighWord32(), a);
411 Node* Float64InsertLowWord32(Node* a, Node* b) {
412 return NewNode(machine()->Float64InsertLowWord32(), a, b);
414 Node* Float64InsertHighWord32(Node* a, Node* b) {
415 return NewNode(machine()->Float64InsertHighWord32(), a, b);
419 Node* LoadStackPointer() { return NewNode(machine()->LoadStackPointer()); }
422 Node* Parameter(size_t index);
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,
436 // Call to a runtime function with zero parameters.
437 Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context,
439 void Return(Node* value);
440 void Bind(Label* label);
441 void Deoptimize(Node* state);
444 Node* Phi(MachineType type, Node* n1, Node* n2) {
445 return NewNode(common()->Phi(type, 2), n1, n2);
447 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) {
448 return NewNode(common()->Phi(type, 3), n1, n2, n3);
450 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) {
451 return NewNode(common()->Phi(type, 4), n1, n2, n3, n4);
454 // MachineAssembler is invalid after export.
458 Node* MakeNode(const Operator* op, int input_count, Node** inputs,
459 bool incomplete) FINAL;
461 bool ScheduleValid() { return schedule_ != NULL; }
463 Schedule* schedule() {
464 DCHECK(ScheduleValid());
469 BasicBlock* Use(Label* label);
470 BasicBlock* EnsureBlock(Label* label);
471 BasicBlock* CurrentBlock();
474 MachineOperatorBuilder machine_;
475 CommonOperatorBuilder common_;
476 const MachineSignature* machine_sig_;
477 CallDescriptor* call_descriptor_;
480 BasicBlock* current_block_;
482 DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler);
485 } // namespace compiler
486 } // namespace internal
489 #endif // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_