OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ | 5 #ifndef V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ |
6 #define V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ | 6 #define V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ |
7 | 7 |
8 #include "src/compiler/common-operator.h" | 8 #include "src/compiler/common-operator.h" |
9 #include "src/compiler/graph.h" | 9 #include "src/compiler/graph.h" |
10 #include "src/compiler/linkage.h" | 10 #include "src/compiler/linkage.h" |
11 #include "src/compiler/machine-operator.h" | 11 #include "src/compiler/machine-operator.h" |
12 #include "src/compiler/node.h" | 12 #include "src/compiler/node.h" |
13 #include "src/compiler/operator.h" | 13 #include "src/compiler/operator.h" |
14 | 14 |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 namespace compiler { | 18 namespace compiler { |
19 | 19 |
20 class BasicBlock; | 20 class BasicBlock; |
21 class Schedule; | 21 class Schedule; |
22 | 22 |
23 | 23 // The RawMachineAssembler produces a low-level IR graph. All nodes are wired |
| 24 // into a graph and also placed into a schedule immediately, hence subsequent |
| 25 // code generation can happen without the need for scheduling. |
| 26 // |
| 27 // In order to create a schedule on-the-fly, the assembler keeps track of basic |
| 28 // blocks by having one current basic block being populated and by referencing |
| 29 // other basic blocks through the use of labels. |
24 class RawMachineAssembler { | 30 class RawMachineAssembler { |
25 public: | 31 public: |
26 class Label { | 32 class Label { |
27 public: | 33 public: |
28 Label() : block_(NULL), used_(false), bound_(false) {} | 34 Label() : block_(NULL), used_(false), bound_(false) {} |
29 ~Label() { DCHECK(bound_ || !used_); } | 35 ~Label() { DCHECK(bound_ || !used_); } |
30 | 36 |
31 BasicBlock* block() { return block_; } | 37 BasicBlock* block() { return block_; } |
32 | 38 |
33 private: | 39 private: |
(...skipping 10 matching lines...) Expand all Loading... |
44 | 50 |
45 RawMachineAssembler(Isolate* isolate, Graph* graph, | 51 RawMachineAssembler(Isolate* isolate, Graph* graph, |
46 CallDescriptor* call_descriptor, | 52 CallDescriptor* call_descriptor, |
47 MachineType word = kMachPtr, | 53 MachineType word = kMachPtr, |
48 MachineOperatorBuilder::Flags flags = | 54 MachineOperatorBuilder::Flags flags = |
49 MachineOperatorBuilder::Flag::kNoFlags); | 55 MachineOperatorBuilder::Flag::kNoFlags); |
50 ~RawMachineAssembler() {} | 56 ~RawMachineAssembler() {} |
51 | 57 |
52 Isolate* isolate() const { return isolate_; } | 58 Isolate* isolate() const { return isolate_; } |
53 Graph* graph() const { return graph_; } | 59 Graph* graph() const { return graph_; } |
| 60 Schedule* schedule() { return schedule_; } |
54 Zone* zone() const { return graph()->zone(); } | 61 Zone* zone() const { return graph()->zone(); } |
55 MachineOperatorBuilder* machine() { return &machine_; } | 62 MachineOperatorBuilder* machine() { return &machine_; } |
56 CommonOperatorBuilder* common() { return &common_; } | 63 CommonOperatorBuilder* common() { return &common_; } |
57 CallDescriptor* call_descriptor() const { return call_descriptor_; } | 64 CallDescriptor* call_descriptor() const { return call_descriptor_; } |
58 size_t parameter_count() const { return machine_sig()->parameter_count(); } | 65 size_t parameter_count() const { return machine_sig()->parameter_count(); } |
59 const MachineSignature* machine_sig() const { | 66 const MachineSignature* machine_sig() const { |
60 return call_descriptor_->GetMachineSignature(); | 67 return call_descriptor_->GetMachineSignature(); |
61 } | 68 } |
62 | 69 |
| 70 // Finalizes the schedule and exports it to be used for code generation. Note |
| 71 // that this RawMachineAssembler becomes invalid after export. |
| 72 Schedule* Export(); |
| 73 |
| 74 // =========================================================================== |
| 75 // The following utility methods create new nodes with specific operators and |
| 76 // place them into the current basic block. They don't perform control flow, |
| 77 // hence will not switch the current basic block. |
| 78 |
63 Node* UndefinedConstant() { | 79 Node* UndefinedConstant() { |
64 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable( | 80 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable( |
65 isolate()->factory()->undefined_value()); | 81 isolate()->factory()->undefined_value()); |
66 return NewNode(common()->HeapConstant(unique)); | 82 return NewNode(common()->HeapConstant(unique)); |
67 } | 83 } |
68 | 84 |
69 // Constants. | 85 // Constants. |
70 Node* PointerConstant(void* value) { | 86 Node* PointerConstant(void* value) { |
71 return IntPtrConstant(reinterpret_cast<intptr_t>(value)); | 87 return IntPtrConstant(reinterpret_cast<intptr_t>(value)); |
72 } | 88 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 return NewNode(machine()->Load(rep), base, index, graph()->start(), | 126 return NewNode(machine()->Load(rep), base, index, graph()->start(), |
111 graph()->start()); | 127 graph()->start()); |
112 } | 128 } |
113 void Store(MachineType rep, Node* base, Node* value) { | 129 void Store(MachineType rep, Node* base, Node* value) { |
114 Store(rep, base, IntPtrConstant(0), value); | 130 Store(rep, base, IntPtrConstant(0), value); |
115 } | 131 } |
116 void Store(MachineType rep, Node* base, Node* index, Node* value) { | 132 void Store(MachineType rep, Node* base, Node* index, Node* value) { |
117 NewNode(machine()->Store(StoreRepresentation(rep, kNoWriteBarrier)), base, | 133 NewNode(machine()->Store(StoreRepresentation(rep, kNoWriteBarrier)), base, |
118 index, value, graph()->start(), graph()->start()); | 134 index, value, graph()->start(), graph()->start()); |
119 } | 135 } |
| 136 |
120 // Arithmetic Operations. | 137 // Arithmetic Operations. |
121 Node* WordAnd(Node* a, Node* b) { | 138 Node* WordAnd(Node* a, Node* b) { |
122 return NewNode(machine()->WordAnd(), a, b); | 139 return NewNode(machine()->WordAnd(), a, b); |
123 } | 140 } |
124 Node* WordOr(Node* a, Node* b) { return NewNode(machine()->WordOr(), a, b); } | 141 Node* WordOr(Node* a, Node* b) { return NewNode(machine()->WordOr(), a, b); } |
125 Node* WordXor(Node* a, Node* b) { | 142 Node* WordXor(Node* a, Node* b) { |
126 return NewNode(machine()->WordXor(), a, b); | 143 return NewNode(machine()->WordXor(), a, b); |
127 } | 144 } |
128 Node* WordShl(Node* a, Node* b) { | 145 Node* WordShl(Node* a, Node* b) { |
129 return NewNode(machine()->WordShl(), a, b); | 146 return NewNode(machine()->WordShl(), a, b); |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 Node* LoadFromPointer(void* address, MachineType rep, int32_t offset = 0) { | 474 Node* LoadFromPointer(void* address, MachineType rep, int32_t offset = 0) { |
458 return Load(rep, PointerConstant(address), Int32Constant(offset)); | 475 return Load(rep, PointerConstant(address), Int32Constant(offset)); |
459 } | 476 } |
460 void StoreToPointer(void* address, MachineType rep, Node* node) { | 477 void StoreToPointer(void* address, MachineType rep, Node* node) { |
461 Store(rep, PointerConstant(address), node); | 478 Store(rep, PointerConstant(address), node); |
462 } | 479 } |
463 Node* StringConstant(const char* string) { | 480 Node* StringConstant(const char* string) { |
464 return HeapConstant(isolate()->factory()->InternalizeUtf8String(string)); | 481 return HeapConstant(isolate()->factory()->InternalizeUtf8String(string)); |
465 } | 482 } |
466 | 483 |
467 // Control flow. | |
468 void Goto(Label* label); | |
469 void Branch(Node* condition, Label* true_val, Label* false_val); | |
470 void Switch(Node* index, Label* default_label, int32_t* case_values, | |
471 Label** case_labels, size_t case_count); | |
472 // Call through CallFunctionStub with lazy deopt and frame-state. | 484 // Call through CallFunctionStub with lazy deopt and frame-state. |
473 Node* CallFunctionStub0(Node* function, Node* receiver, Node* context, | 485 Node* CallFunctionStub0(Node* function, Node* receiver, Node* context, |
474 Node* frame_state, CallFunctionFlags flags); | 486 Node* frame_state, CallFunctionFlags flags); |
475 // Call to a JS function with zero parameters. | 487 // Call to a JS function with zero parameters. |
476 Node* CallJS0(Node* function, Node* receiver, Node* context, | 488 Node* CallJS0(Node* function, Node* receiver, Node* context, |
477 Node* frame_state); | 489 Node* frame_state); |
478 // Call to a runtime function with zero parameters. | 490 // Call to a runtime function with zero parameters. |
479 Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context, | 491 Node* CallRuntime1(Runtime::FunctionId function, Node* arg0, Node* context, |
480 Node* frame_state); | 492 Node* frame_state); |
481 // Call to a C function with zero parameters. | 493 // Call to a C function with zero parameters. |
482 Node* CallCFunction0(MachineType return_type, Node* function); | 494 Node* CallCFunction0(MachineType return_type, Node* function); |
483 // Call to a C function with one parameter. | 495 // Call to a C function with one parameter. |
484 Node* CallCFunction1(MachineType return_type, MachineType arg0_type, | 496 Node* CallCFunction1(MachineType return_type, MachineType arg0_type, |
485 Node* function, Node* arg0); | 497 Node* function, Node* arg0); |
486 // Call to a C function with two parameters. | 498 // Call to a C function with two parameters. |
487 Node* CallCFunction2(MachineType return_type, MachineType arg0_type, | 499 Node* CallCFunction2(MachineType return_type, MachineType arg0_type, |
488 MachineType arg1_type, Node* function, Node* arg0, | 500 MachineType arg1_type, Node* function, Node* arg0, |
489 Node* arg1); | 501 Node* arg1); |
490 // Call to a C function with eight parameters. | 502 // Call to a C function with eight parameters. |
491 Node* CallCFunction8(MachineType return_type, MachineType arg0_type, | 503 Node* CallCFunction8(MachineType return_type, MachineType arg0_type, |
492 MachineType arg1_type, MachineType arg2_type, | 504 MachineType arg1_type, MachineType arg2_type, |
493 MachineType arg3_type, MachineType arg4_type, | 505 MachineType arg3_type, MachineType arg4_type, |
494 MachineType arg5_type, MachineType arg6_type, | 506 MachineType arg5_type, MachineType arg6_type, |
495 MachineType arg7_type, Node* function, Node* arg0, | 507 MachineType arg7_type, Node* function, Node* arg0, |
496 Node* arg1, Node* arg2, Node* arg3, Node* arg4, | 508 Node* arg1, Node* arg2, Node* arg3, Node* arg4, |
497 Node* arg5, Node* arg6, Node* arg7); | 509 Node* arg5, Node* arg6, Node* arg7); |
| 510 |
| 511 // =========================================================================== |
| 512 // The following utility methods deal with control flow, hence might switch |
| 513 // the current basic block or create new basic blocks for labels. |
| 514 |
| 515 // Control flow. |
| 516 void Goto(Label* label); |
| 517 void Branch(Node* condition, Label* true_val, Label* false_val); |
| 518 void Switch(Node* index, Label* default_label, int32_t* case_values, |
| 519 Label** case_labels, size_t case_count); |
498 void Return(Node* value); | 520 void Return(Node* value); |
499 void Bind(Label* label); | 521 void Bind(Label* label); |
500 void Deoptimize(Node* state); | 522 void Deoptimize(Node* state); |
501 | 523 |
502 // Variables. | 524 // Variables. |
503 Node* Phi(MachineType type, Node* n1, Node* n2) { | 525 Node* Phi(MachineType type, Node* n1, Node* n2) { |
504 return NewNode(common()->Phi(type, 2), n1, n2); | 526 return NewNode(common()->Phi(type, 2), n1, n2); |
505 } | 527 } |
506 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) { | 528 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3) { |
507 return NewNode(common()->Phi(type, 3), n1, n2, n3); | 529 return NewNode(common()->Phi(type, 3), n1, n2, n3); |
508 } | 530 } |
509 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) { | 531 Node* Phi(MachineType type, Node* n1, Node* n2, Node* n3, Node* n4) { |
510 return NewNode(common()->Phi(type, 4), n1, n2, n3, n4); | 532 return NewNode(common()->Phi(type, 4), n1, n2, n3, n4); |
511 } | 533 } |
512 | 534 |
513 // MachineAssembler is invalid after export. | 535 // =========================================================================== |
514 Schedule* Export(); | 536 // The following generic node creation methods can be used for operators that |
| 537 // are not covered by the above utility methods. There should rarely be a need |
| 538 // to do that outside of testing though. |
515 | 539 |
516 Node* NewNode(const Operator* op) { | 540 Node* NewNode(const Operator* op) { |
517 return MakeNode(op, 0, static_cast<Node**>(NULL)); | 541 return MakeNode(op, 0, static_cast<Node**>(NULL)); |
518 } | 542 } |
519 | 543 |
520 Node* NewNode(const Operator* op, Node* n1) { return MakeNode(op, 1, &n1); } | 544 Node* NewNode(const Operator* op, Node* n1) { return MakeNode(op, 1, &n1); } |
521 | 545 |
522 Node* NewNode(const Operator* op, Node* n1, Node* n2) { | 546 Node* NewNode(const Operator* op, Node* n1, Node* n2) { |
523 Node* buffer[] = {n1, n2}; | 547 Node* buffer[] = {n1, n2}; |
524 return MakeNode(op, arraysize(buffer), buffer); | 548 return MakeNode(op, arraysize(buffer), buffer); |
(...skipping 19 matching lines...) Expand all Loading... |
544 Node* n5, Node* n6) { | 568 Node* n5, Node* n6) { |
545 Node* nodes[] = {n1, n2, n3, n4, n5, n6}; | 569 Node* nodes[] = {n1, n2, n3, n4, n5, n6}; |
546 return MakeNode(op, arraysize(nodes), nodes); | 570 return MakeNode(op, arraysize(nodes), nodes); |
547 } | 571 } |
548 | 572 |
549 Node* NewNode(const Operator* op, int value_input_count, | 573 Node* NewNode(const Operator* op, int value_input_count, |
550 Node** value_inputs) { | 574 Node** value_inputs) { |
551 return MakeNode(op, value_input_count, value_inputs); | 575 return MakeNode(op, value_input_count, value_inputs); |
552 } | 576 } |
553 | 577 |
554 protected: | 578 private: |
555 Node* MakeNode(const Operator* op, int input_count, Node** inputs); | 579 Node* MakeNode(const Operator* op, int input_count, Node** inputs); |
556 | |
557 bool ScheduleValid() { return schedule_ != NULL; } | |
558 | |
559 Schedule* schedule() { | |
560 DCHECK(ScheduleValid()); | |
561 return schedule_; | |
562 } | |
563 | |
564 private: | |
565 BasicBlock* Use(Label* label); | 580 BasicBlock* Use(Label* label); |
566 BasicBlock* EnsureBlock(Label* label); | 581 BasicBlock* EnsureBlock(Label* label); |
567 BasicBlock* CurrentBlock(); | 582 BasicBlock* CurrentBlock(); |
568 | 583 |
569 Isolate* isolate_; | 584 Isolate* isolate_; |
570 Graph* graph_; | 585 Graph* graph_; |
571 Schedule* schedule_; | 586 Schedule* schedule_; |
572 MachineOperatorBuilder machine_; | 587 MachineOperatorBuilder machine_; |
573 CommonOperatorBuilder common_; | 588 CommonOperatorBuilder common_; |
574 CallDescriptor* call_descriptor_; | 589 CallDescriptor* call_descriptor_; |
575 Node** parameters_; | 590 Node** parameters_; |
576 BasicBlock* current_block_; | 591 BasicBlock* current_block_; |
577 | 592 |
578 DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler); | 593 DISALLOW_COPY_AND_ASSIGN(RawMachineAssembler); |
579 }; | 594 }; |
580 | 595 |
581 } // namespace compiler | 596 } // namespace compiler |
582 } // namespace internal | 597 } // namespace internal |
583 } // namespace v8 | 598 } // namespace v8 |
584 | 599 |
585 #endif // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ | 600 #endif // V8_COMPILER_RAW_MACHINE_ASSEMBLER_H_ |
OLD | NEW |