blob: 5455814fa206e7e41b6043cf66a230ada384ccb4 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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/v8.h"
9
10#include "src/compiler/common-operator.h"
11#include "src/compiler/graph-builder.h"
12#include "src/compiler/linkage.h"
13#include "src/compiler/machine-operator.h"
14#include "src/compiler/node.h"
15#include "src/compiler/operator.h"
16
17
18namespace v8 {
19namespace internal {
20namespace compiler {
21
22class BasicBlock;
23class Schedule;
24
25
26class RawMachineAssembler : public GraphBuilder {
27 public:
28 class Label {
29 public:
30 Label() : block_(NULL), used_(false), bound_(false) {}
31 ~Label() { DCHECK(bound_ || !used_); }
32
33 BasicBlock* block() { return block_; }
34
35 private:
36 // Private constructor for exit label.
37 explicit Label(BasicBlock* block)
38 : block_(block), used_(false), bound_(false) {}
39
40 BasicBlock* block_;
41 bool used_;
42 bool bound_;
43 friend class RawMachineAssembler;
44 DISALLOW_COPY_AND_ASSIGN(Label);
45 };
46
47 RawMachineAssembler(Graph* graph, MachineSignature* machine_sig,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048 MachineType word = kMachPtr,
49 MachineOperatorBuilder::Flags flags =
50 MachineOperatorBuilder::Flag::kNoFlags);
51 ~RawMachineAssembler() OVERRIDE {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052
53 Isolate* isolate() const { return zone()->isolate(); }
54 Zone* zone() const { return graph()->zone(); }
55 MachineOperatorBuilder* machine() { return &machine_; }
56 CommonOperatorBuilder* common() { return &common_; }
57 CallDescriptor* call_descriptor() const { return call_descriptor_; }
58 size_t parameter_count() const { return machine_sig_->parameter_count(); }
59 MachineSignature* machine_sig() const { return machine_sig_; }
60
61 Node* UndefinedConstant() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040062 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 isolate()->factory()->undefined_value());
64 return NewNode(common()->HeapConstant(unique));
65 }
66
67 // Constants.
68 Node* PointerConstant(void* value) {
69 return IntPtrConstant(reinterpret_cast<intptr_t>(value));
70 }
71 Node* IntPtrConstant(intptr_t value) {
72 // TODO(dcarney): mark generated code as unserializable if value != 0.
73 return kPointerSize == 8 ? Int64Constant(value)
74 : Int32Constant(static_cast<int>(value));
75 }
76 Node* Int32Constant(int32_t value) {
77 return NewNode(common()->Int32Constant(value));
78 }
79 Node* Int64Constant(int64_t value) {
80 return NewNode(common()->Int64Constant(value));
81 }
82 Node* NumberConstant(double value) {
83 return NewNode(common()->NumberConstant(value));
84 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085 Node* Float32Constant(float value) {
86 return NewNode(common()->Float32Constant(value));
87 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 Node* Float64Constant(double value) {
89 return NewNode(common()->Float64Constant(value));
90 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091 Node* HeapConstant(Handle<HeapObject> object) {
92 Unique<HeapObject> val = Unique<HeapObject>::CreateUninitialized(object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 return NewNode(common()->HeapConstant(val));
94 }
95
96 Node* Projection(int index, Node* a) {
97 return NewNode(common()->Projection(index), a);
98 }
99
100 // Memory Operations.
101 Node* Load(MachineType rep, Node* base) {
102 return Load(rep, base, Int32Constant(0));
103 }
104 Node* Load(MachineType rep, Node* base, Node* index) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400105 return NewNode(machine()->Load(rep), base, index, graph()->start(),
106 graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 }
108 void Store(MachineType rep, Node* base, Node* value) {
109 Store(rep, base, Int32Constant(0), value);
110 }
111 void Store(MachineType rep, Node* base, Node* index, Node* value) {
112 NewNode(machine()->Store(StoreRepresentation(rep, kNoWriteBarrier)), base,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400113 index, value, graph()->start(), graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 }
115 // Arithmetic Operations.
116 Node* WordAnd(Node* a, Node* b) {
117 return NewNode(machine()->WordAnd(), a, b);
118 }
119 Node* WordOr(Node* a, Node* b) { return NewNode(machine()->WordOr(), a, b); }
120 Node* WordXor(Node* a, Node* b) {
121 return NewNode(machine()->WordXor(), a, b);
122 }
123 Node* WordShl(Node* a, Node* b) {
124 return NewNode(machine()->WordShl(), a, b);
125 }
126 Node* WordShr(Node* a, Node* b) {
127 return NewNode(machine()->WordShr(), a, b);
128 }
129 Node* WordSar(Node* a, Node* b) {
130 return NewNode(machine()->WordSar(), a, b);
131 }
132 Node* WordRor(Node* a, Node* b) {
133 return NewNode(machine()->WordRor(), a, b);
134 }
135 Node* WordEqual(Node* a, Node* b) {
136 return NewNode(machine()->WordEqual(), a, b);
137 }
138 Node* WordNotEqual(Node* a, Node* b) {
139 return WordBinaryNot(WordEqual(a, b));
140 }
141 Node* WordNot(Node* a) {
142 if (machine()->Is32()) {
143 return Word32Not(a);
144 } else {
145 return Word64Not(a);
146 }
147 }
148 Node* WordBinaryNot(Node* a) {
149 if (machine()->Is32()) {
150 return Word32BinaryNot(a);
151 } else {
152 return Word64BinaryNot(a);
153 }
154 }
155
156 Node* Word32And(Node* a, Node* b) {
157 return NewNode(machine()->Word32And(), a, b);
158 }
159 Node* Word32Or(Node* a, Node* b) {
160 return NewNode(machine()->Word32Or(), a, b);
161 }
162 Node* Word32Xor(Node* a, Node* b) {
163 return NewNode(machine()->Word32Xor(), a, b);
164 }
165 Node* Word32Shl(Node* a, Node* b) {
166 return NewNode(machine()->Word32Shl(), a, b);
167 }
168 Node* Word32Shr(Node* a, Node* b) {
169 return NewNode(machine()->Word32Shr(), a, b);
170 }
171 Node* Word32Sar(Node* a, Node* b) {
172 return NewNode(machine()->Word32Sar(), a, b);
173 }
174 Node* Word32Ror(Node* a, Node* b) {
175 return NewNode(machine()->Word32Ror(), a, b);
176 }
177 Node* Word32Equal(Node* a, Node* b) {
178 return NewNode(machine()->Word32Equal(), a, b);
179 }
180 Node* Word32NotEqual(Node* a, Node* b) {
181 return Word32BinaryNot(Word32Equal(a, b));
182 }
183 Node* Word32Not(Node* a) { return Word32Xor(a, Int32Constant(-1)); }
184 Node* Word32BinaryNot(Node* a) { return Word32Equal(a, Int32Constant(0)); }
185
186 Node* Word64And(Node* a, Node* b) {
187 return NewNode(machine()->Word64And(), a, b);
188 }
189 Node* Word64Or(Node* a, Node* b) {
190 return NewNode(machine()->Word64Or(), a, b);
191 }
192 Node* Word64Xor(Node* a, Node* b) {
193 return NewNode(machine()->Word64Xor(), a, b);
194 }
195 Node* Word64Shl(Node* a, Node* b) {
196 return NewNode(machine()->Word64Shl(), a, b);
197 }
198 Node* Word64Shr(Node* a, Node* b) {
199 return NewNode(machine()->Word64Shr(), a, b);
200 }
201 Node* Word64Sar(Node* a, Node* b) {
202 return NewNode(machine()->Word64Sar(), a, b);
203 }
204 Node* Word64Ror(Node* a, Node* b) {
205 return NewNode(machine()->Word64Ror(), a, b);
206 }
207 Node* Word64Equal(Node* a, Node* b) {
208 return NewNode(machine()->Word64Equal(), a, b);
209 }
210 Node* Word64NotEqual(Node* a, Node* b) {
211 return Word64BinaryNot(Word64Equal(a, b));
212 }
213 Node* Word64Not(Node* a) { return Word64Xor(a, Int64Constant(-1)); }
214 Node* Word64BinaryNot(Node* a) { return Word64Equal(a, Int64Constant(0)); }
215
216 Node* Int32Add(Node* a, Node* b) {
217 return NewNode(machine()->Int32Add(), a, b);
218 }
219 Node* Int32AddWithOverflow(Node* a, Node* b) {
220 return NewNode(machine()->Int32AddWithOverflow(), a, b);
221 }
222 Node* Int32Sub(Node* a, Node* b) {
223 return NewNode(machine()->Int32Sub(), a, b);
224 }
225 Node* Int32SubWithOverflow(Node* a, Node* b) {
226 return NewNode(machine()->Int32SubWithOverflow(), a, b);
227 }
228 Node* Int32Mul(Node* a, Node* b) {
229 return NewNode(machine()->Int32Mul(), a, b);
230 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400231 Node* Int32MulHigh(Node* a, Node* b) {
232 return NewNode(machine()->Int32MulHigh(), a, b);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234 Node* Int32Div(Node* a, Node* b) {
235 return NewNode(machine()->Int32Div(), a, b, graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 }
237 Node* Int32Mod(Node* a, Node* b) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400238 return NewNode(machine()->Int32Mod(), a, b, graph()->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 }
240 Node* Int32LessThan(Node* a, Node* b) {
241 return NewNode(machine()->Int32LessThan(), a, b);
242 }
243 Node* Int32LessThanOrEqual(Node* a, Node* b) {
244 return NewNode(machine()->Int32LessThanOrEqual(), a, b);
245 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400246 Node* Uint32Div(Node* a, Node* b) {
247 return NewNode(machine()->Uint32Div(), a, b, graph()->start());
248 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 Node* Uint32LessThan(Node* a, Node* b) {
250 return NewNode(machine()->Uint32LessThan(), a, b);
251 }
252 Node* Uint32LessThanOrEqual(Node* a, Node* b) {
253 return NewNode(machine()->Uint32LessThanOrEqual(), a, b);
254 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400255 Node* Uint32Mod(Node* a, Node* b) {
256 return NewNode(machine()->Uint32Mod(), a, b, graph()->start());
257 }
258 Node* Uint32MulHigh(Node* a, Node* b) {
259 return NewNode(machine()->Uint32MulHigh(), a, b);
260 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 Node* Int32GreaterThan(Node* a, Node* b) { return Int32LessThan(b, a); }
262 Node* Int32GreaterThanOrEqual(Node* a, Node* b) {
263 return Int32LessThanOrEqual(b, a);
264 }
265 Node* Int32Neg(Node* a) { return Int32Sub(Int32Constant(0), a); }
266
267 Node* Int64Add(Node* a, Node* b) {
268 return NewNode(machine()->Int64Add(), a, b);
269 }
270 Node* Int64Sub(Node* a, Node* b) {
271 return NewNode(machine()->Int64Sub(), a, b);
272 }
273 Node* Int64Mul(Node* a, Node* b) {
274 return NewNode(machine()->Int64Mul(), a, b);
275 }
276 Node* Int64Div(Node* a, Node* b) {
277 return NewNode(machine()->Int64Div(), a, b);
278 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 Node* Int64Mod(Node* a, Node* b) {
280 return NewNode(machine()->Int64Mod(), a, b);
281 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 Node* Int64Neg(Node* a) { return Int64Sub(Int64Constant(0), a); }
283 Node* Int64LessThan(Node* a, Node* b) {
284 return NewNode(machine()->Int64LessThan(), a, b);
285 }
286 Node* Int64LessThanOrEqual(Node* a, Node* b) {
287 return NewNode(machine()->Int64LessThanOrEqual(), a, b);
288 }
289 Node* Int64GreaterThan(Node* a, Node* b) { return Int64LessThan(b, a); }
290 Node* Int64GreaterThanOrEqual(Node* a, Node* b) {
291 return Int64LessThanOrEqual(b, a);
292 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400293 Node* Uint64Div(Node* a, Node* b) {
294 return NewNode(machine()->Uint64Div(), a, b);
295 }
296 Node* Uint64Mod(Node* a, Node* b) {
297 return NewNode(machine()->Uint64Mod(), a, b);
298 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299
300 // TODO(turbofan): What is this used for?
301 Node* ConvertIntPtrToInt32(Node* a) {
302 return kPointerSize == 8 ? NewNode(machine()->TruncateInt64ToInt32(), a)
303 : a;
304 }
305 Node* ConvertInt32ToIntPtr(Node* a) {
306 return kPointerSize == 8 ? NewNode(machine()->ChangeInt32ToInt64(), a) : a;
307 }
308
309#define INTPTR_BINOP(prefix, name) \
310 Node* IntPtr##name(Node* a, Node* b) { \
311 return kPointerSize == 8 ? prefix##64##name(a, b) \
312 : prefix##32##name(a, b); \
313 }
314
315 INTPTR_BINOP(Int, Add);
316 INTPTR_BINOP(Int, Sub);
317 INTPTR_BINOP(Int, LessThan);
318 INTPTR_BINOP(Int, LessThanOrEqual);
319 INTPTR_BINOP(Word, Equal);
320 INTPTR_BINOP(Word, NotEqual);
321 INTPTR_BINOP(Int, GreaterThanOrEqual);
322 INTPTR_BINOP(Int, GreaterThan);
323
324#undef INTPTR_BINOP
325
326 Node* Float64Add(Node* a, Node* b) {
327 return NewNode(machine()->Float64Add(), a, b);
328 }
329 Node* Float64Sub(Node* a, Node* b) {
330 return NewNode(machine()->Float64Sub(), a, b);
331 }
332 Node* Float64Mul(Node* a, Node* b) {
333 return NewNode(machine()->Float64Mul(), a, b);
334 }
335 Node* Float64Div(Node* a, Node* b) {
336 return NewNode(machine()->Float64Div(), a, b);
337 }
338 Node* Float64Mod(Node* a, Node* b) {
339 return NewNode(machine()->Float64Mod(), a, b);
340 }
341 Node* Float64Equal(Node* a, Node* b) {
342 return NewNode(machine()->Float64Equal(), a, b);
343 }
344 Node* Float64NotEqual(Node* a, Node* b) {
345 return WordBinaryNot(Float64Equal(a, b));
346 }
347 Node* Float64LessThan(Node* a, Node* b) {
348 return NewNode(machine()->Float64LessThan(), a, b);
349 }
350 Node* Float64LessThanOrEqual(Node* a, Node* b) {
351 return NewNode(machine()->Float64LessThanOrEqual(), a, b);
352 }
353 Node* Float64GreaterThan(Node* a, Node* b) { return Float64LessThan(b, a); }
354 Node* Float64GreaterThanOrEqual(Node* a, Node* b) {
355 return Float64LessThanOrEqual(b, a);
356 }
357
358 // Conversions.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400359 Node* ChangeFloat32ToFloat64(Node* a) {
360 return NewNode(machine()->ChangeFloat32ToFloat64(), a);
361 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362 Node* ChangeInt32ToFloat64(Node* a) {
363 return NewNode(machine()->ChangeInt32ToFloat64(), a);
364 }
365 Node* ChangeUint32ToFloat64(Node* a) {
366 return NewNode(machine()->ChangeUint32ToFloat64(), a);
367 }
368 Node* ChangeFloat64ToInt32(Node* a) {
369 return NewNode(machine()->ChangeFloat64ToInt32(), a);
370 }
371 Node* ChangeFloat64ToUint32(Node* a) {
372 return NewNode(machine()->ChangeFloat64ToUint32(), a);
373 }
374 Node* ChangeInt32ToInt64(Node* a) {
375 return NewNode(machine()->ChangeInt32ToInt64(), a);
376 }
377 Node* ChangeUint32ToUint64(Node* a) {
378 return NewNode(machine()->ChangeUint32ToUint64(), a);
379 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400380 Node* TruncateFloat64ToFloat32(Node* a) {
381 return NewNode(machine()->TruncateFloat64ToFloat32(), a);
382 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 Node* TruncateFloat64ToInt32(Node* a) {
384 return NewNode(machine()->TruncateFloat64ToInt32(), a);
385 }
386 Node* TruncateInt64ToInt32(Node* a) {
387 return NewNode(machine()->TruncateInt64ToInt32(), a);
388 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400389 Node* Float64Floor(Node* a) { return NewNode(machine()->Float64Floor(), a); }
390 Node* Float64Ceil(Node* a) { return NewNode(machine()->Float64Ceil(), a); }
391 Node* Float64RoundTruncate(Node* a) {
392 return NewNode(machine()->Float64RoundTruncate(), a);
393 }
394 Node* Float64RoundTiesAway(Node* a) {
395 return NewNode(machine()->Float64RoundTiesAway(), a);
396 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397
398 // Parameters.
399 Node* Parameter(size_t index);
400
401 // Control flow.
402 Label* Exit();
403 void Goto(Label* label);
404 void Branch(Node* condition, Label* true_val, Label* false_val);
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:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400433 Node* MakeNode(const Operator* op, int input_count, Node** inputs,
434 bool incomplete) FINAL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435
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 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_