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));
94 Node* Projection(int index, Node* a) {
95 return NewNode(common()->Projection(index), a);
99 Node* Load(MachineType rep, Node* base) {
100 return Load(rep, base, Int32Constant(0));
102 Node* Load(MachineType rep, Node* base, Node* index) {
103 return NewNode(machine()->Load(rep), base, index, graph()->start(),
106 void Store(MachineType rep, Node* base, Node* value) {
107 Store(rep, base, Int32Constant(0), value);
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());
113 // Arithmetic Operations.
114 Node* WordAnd(Node* a, Node* b) {
115 return NewNode(machine()->WordAnd(), a, b);
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);
121 Node* WordShl(Node* a, Node* b) {
122 return NewNode(machine()->WordShl(), a, b);
124 Node* WordShr(Node* a, Node* b) {
125 return NewNode(machine()->WordShr(), a, b);
127 Node* WordSar(Node* a, Node* b) {
128 return NewNode(machine()->WordSar(), a, b);
130 Node* WordRor(Node* a, Node* b) {
131 return NewNode(machine()->WordRor(), a, b);
133 Node* WordEqual(Node* a, Node* b) {
134 return NewNode(machine()->WordEqual(), a, b);
136 Node* WordNotEqual(Node* a, Node* b) {
137 return WordBinaryNot(WordEqual(a, b));
139 Node* WordNot(Node* a) {
140 if (machine()->Is32()) {
146 Node* WordBinaryNot(Node* a) {
147 if (machine()->Is32()) {
148 return Word32BinaryNot(a);
150 return Word64BinaryNot(a);
154 Node* Word32And(Node* a, Node* b) {
155 return NewNode(machine()->Word32And(), a, b);
157 Node* Word32Or(Node* a, Node* b) {
158 return NewNode(machine()->Word32Or(), a, b);
160 Node* Word32Xor(Node* a, Node* b) {
161 return NewNode(machine()->Word32Xor(), a, b);
163 Node* Word32Shl(Node* a, Node* b) {
164 return NewNode(machine()->Word32Shl(), a, b);
166 Node* Word32Shr(Node* a, Node* b) {
167 return NewNode(machine()->Word32Shr(), a, b);
169 Node* Word32Sar(Node* a, Node* b) {
170 return NewNode(machine()->Word32Sar(), a, b);
172 Node* Word32Ror(Node* a, Node* b) {
173 return NewNode(machine()->Word32Ror(), a, b);
175 Node* Word32Equal(Node* a, Node* b) {
176 return NewNode(machine()->Word32Equal(), a, b);
178 Node* Word32NotEqual(Node* a, Node* b) {
179 return Word32BinaryNot(Word32Equal(a, b));
181 Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
182 Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
184 Node* Word64And(Node* a, Node* b) {
185 return NewNode(machine()->Word64And(), a, b);
187 Node* Word64Or(Node* a, Node* b) {
188 return NewNode(machine()->Word64Or(), a, b);
190 Node* Word64Xor(Node* a, Node* b) {
191 return NewNode(machine()->Word64Xor(), a, b);
193 Node* Word64Shl(Node* a, Node* b) {
194 return NewNode(machine()->Word64Shl(), a, b);
196 Node* Word64Shr(Node* a, Node* b) {
197 return NewNode(machine()->Word64Shr(), a, b);
199 Node* Word64Sar(Node* a, Node* b) {
200 return NewNode(machine()->Word64Sar(), a, b);
202 Node* Word64Ror(Node* a, Node* b) {
203 return NewNode(machine()->Word64Ror(), a, b);
205 Node* Word64Equal(Node* a, Node* b) {
206 return NewNode(machine()->Word64Equal(), a, b);
208 Node* Word64NotEqual(Node* a, Node* b) {
209 return Word64BinaryNot(Word64Equal(a, b));
211 Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
212 Node* Word64BinaryNot(Node* a) { return Word64Equal(a, Int64Constant(0)); }
214 Node* Int32Add(Node* a, Node* b) {
215 return NewNode(machine()->Int32Add(), a, b);
217 Node* Int32AddWithOverflow(Node* a, Node* b) {
218 return NewNode(machine()->Int32AddWithOverflow(), a, b);
220 Node* Int32Sub(Node* a, Node* b) {
221 return NewNode(machine()->Int32Sub(), a, b);
223 Node* Int32SubWithOverflow(Node* a, Node* b) {
224 return NewNode(machine()->Int32SubWithOverflow(), a, b);
226 Node* Int32Mul(Node* a, Node* b) {
227 return NewNode(machine()->Int32Mul(), a, b);
229 Node* Int32MulHigh(Node* a, Node* b) {
230 return NewNode(machine()->Int32MulHigh(), a, b);
232 Node* Int32Div(Node* a, Node* b) {
233 return NewNode(machine()->Int32Div(), a, b, graph()->start());
235 Node* Int32Mod(Node* a, Node* b) {
236 return NewNode(machine()->Int32Mod(), a, b, graph()->start());
238 Node* Int32LessThan(Node* a, Node* b) {
239 return NewNode(machine()->Int32LessThan(), a, b);
241 Node* Int32LessThanOrEqual(Node* a, Node* b) {
242 return NewNode(machine()->Int32LessThanOrEqual(), a, b);
244 Node* Uint32Div(Node* a, Node* b) {
245 return NewNode(machine()->Uint32Div(), a, b, graph()->start());
247 Node* Uint32LessThan(Node* a, Node* b) {
248 return NewNode(machine()->Uint32LessThan(), a, b);
250 Node* Uint32LessThanOrEqual(Node* a, Node* b) {
251 return NewNode(machine()->Uint32LessThanOrEqual(), a, b);
253 Node* Uint32Mod(Node* a, Node* b) {
254 return NewNode(machine()->Uint32Mod(), a, b, graph()->start());
256 Node* Uint32MulHigh(Node* a, Node* b) {
257 return NewNode(machine()->Uint32MulHigh(), a, b);
259 Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
260 Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
261 return Int32LessThanOrEqual(b, a);
263 Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
265 Node* Int64Add(Node* a, Node* b) {
266 return NewNode(machine()->Int64Add(), a, b);
268 Node* Int64Sub(Node* a, Node* b) {
269 return NewNode(machine()->Int64Sub(), a, b);
271 Node* Int64Mul(Node* a, Node* b) {
272 return NewNode(machine()->Int64Mul(), a, b);
274 Node* Int64Div(Node* a, Node* b) {
275 return NewNode(machine()->Int64Div(), a, b);
277 Node* Int64Mod(Node* a, Node* b) {
278 return NewNode(machine()->Int64Mod(), a, b);
280 Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
281 Node* Int64LessThan(Node* a, Node* b) {
282 return NewNode(machine()->Int64LessThan(), a, b);
284 Node* Int64LessThanOrEqual(Node* a, Node* b) {
285 return NewNode(machine()->Int64LessThanOrEqual(), a, b);
287 Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
288 Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
289 return Int64LessThanOrEqual(b, a);
291 Node* Uint64Div(Node* a, Node* b) {
292 return NewNode(machine()->Uint64Div(), a, b);
294 Node* Uint64Mod(Node* a, Node* b) {
295 return NewNode(machine()->Uint64Mod(), a, b);
298 // TODO(turbofan): What is this used for?
299 Node* ConvertIntPtrToInt32(Node* a) {
300 return kPointerSize == 8 ? NewNode(machine()->TruncateInt64ToInt32(), a)
303 Node* ConvertInt32ToIntPtr(Node* a) {
304 return kPointerSize == 8 ? NewNode(machine()->ChangeInt32ToInt64(), a) : a;
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); \
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);
324 Node* Float64Add(Node* a, Node* b) {
325 return NewNode(machine()->Float64Add(), a, b);
327 Node* Float64Sub(Node* a, Node* b) {
328 return NewNode(machine()->Float64Sub(), a, b);
330 Node* Float64Mul(Node* a, Node* b) {
331 return NewNode(machine()->Float64Mul(), a, b);
333 Node* Float64Div(Node* a, Node* b) {
334 return NewNode(machine()->Float64Div(), a, b);
336 Node* Float64Mod(Node* a, Node* b) {
337 return NewNode(machine()->Float64Mod(), a, b);
339 Node* Float64Equal(Node* a, Node* b) {
340 return NewNode(machine()->Float64Equal(), a, b);
342 Node* Float64NotEqual(Node* a, Node* b) {
343 return WordBinaryNot(Float64Equal(a, b));
345 Node* Float64LessThan(Node* a, Node* b) {
346 return NewNode(machine()->Float64LessThan(), a, b);
348 Node* Float64LessThanOrEqual(Node* a, Node* b) {
349 return NewNode(machine()->Float64LessThanOrEqual(), a, b);
351 Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
352 Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
353 return Float64LessThanOrEqual(b, a);
357 Node* ChangeFloat32ToFloat64(Node* a) {
358 return NewNode(machine()->ChangeFloat32ToFloat64(), a);
360 Node* ChangeInt32ToFloat64(Node* a) {
361 return NewNode(machine()->ChangeInt32ToFloat64(), a);
363 Node* ChangeUint32ToFloat64(Node* a) {
364 return NewNode(machine()->ChangeUint32ToFloat64(), a);
366 Node* ChangeFloat64ToInt32(Node* a) {
367 return NewNode(machine()->ChangeFloat64ToInt32(), a);
369 Node* ChangeFloat64ToUint32(Node* a) {
370 return NewNode(machine()->ChangeFloat64ToUint32(), a);
372 Node* ChangeInt32ToInt64(Node* a) {
373 return NewNode(machine()->ChangeInt32ToInt64(), a);
375 Node* ChangeUint32ToUint64(Node* a) {
376 return NewNode(machine()->ChangeUint32ToUint64(), a);
378 Node* TruncateFloat64ToFloat32(Node* a) {
379 return NewNode(machine()->TruncateFloat64ToFloat32(), a);
381 Node* TruncateFloat64ToInt32(Node* a) {
382 return NewNode(machine()->TruncateFloat64ToInt32(), a);
384 Node* TruncateInt64ToInt32(Node* a) {
385 return NewNode(machine()->TruncateInt64ToInt32(), a);
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);
392 Node* Float64RoundTiesAway(Node* a) {
393 return NewNode(machine()->Float64RoundTiesAway(), a);
397 Node* Parameter(size_t index);
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,
411 // Call to a runtime function with zero parameters.
412 Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context,
414 void Return(Node* value);
415 void Bind(Label* label);
416 void Deoptimize(Node* state);
419 Node* Phi(MachineType type, Node* n1, Node* n2) {
420 return NewNode(common()->Phi(type, 2), n1, n2);
422 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) {
423 return NewNode(common()->Phi(type, 3), n1, n2, n3);
425 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) {
426 return NewNode(common()->Phi(type, 4), n1, n2, n3, n4);
429 // MachineAssembler is invalid after export.
433 Node* MakeNode(const Operator* op, int input_count, Node** inputs,
434 bool incomplete) FINAL;
436 bool ScheduleValid() { return schedule_ != NULL; }
438 Schedule* schedule() {
439 DCHECK(ScheduleValid());
444 BasicBlock* Use(Label* label);
445 BasicBlock* EnsureBlock(Label* label);
446 BasicBlock* CurrentBlock();
449 MachineOperatorBuilder machine_;
450 CommonOperatorBuilder common_;
451 const MachineSignature* machine_sig_;
452 CallDescriptor* call_descriptor_;
455 BasicBlock* current_block_;
457 DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler);
460 } // namespace compiler
461 } // namespace internal
464 #endif // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_