OLD | NEW |
| (Empty) |
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 #include "src/code-factory.h" | |
6 #include "src/compiler/pipeline.h" | |
7 #include "src/compiler/raw-machine-assembler.h" | |
8 #include "src/compiler/scheduler.h" | |
9 | |
10 namespace v8 { | |
11 namespace internal { | |
12 namespace compiler { | |
13 | |
14 RawMachineAssembler::RawMachineAssembler(Isolate* isolate, Graph* graph, | |
15 const MachineSignature* machine_sig, | |
16 MachineType word, | |
17 MachineOperatorBuilder::Flags flags) | |
18 : GraphBuilder(isolate, graph), | |
19 schedule_(new (zone()) Schedule(zone())), | |
20 machine_(zone(), word, flags), | |
21 common_(zone()), | |
22 machine_sig_(machine_sig), | |
23 call_descriptor_( | |
24 Linkage::GetSimplifiedCDescriptor(graph->zone(), machine_sig)), | |
25 parameters_(NULL), | |
26 current_block_(schedule()->start()) { | |
27 int param_count = static_cast<int>(parameter_count()); | |
28 Node* s = graph->NewNode(common_.Start(param_count)); | |
29 graph->SetStart(s); | |
30 if (parameter_count() == 0) return; | |
31 parameters_ = zone()->NewArray<Node*>(param_count); | |
32 for (size_t i = 0; i < parameter_count(); ++i) { | |
33 parameters_[i] = | |
34 NewNode(common()->Parameter(static_cast<int>(i)), graph->start()); | |
35 } | |
36 } | |
37 | |
38 | |
39 Schedule* RawMachineAssembler::Export() { | |
40 // Compute the correct codegen order. | |
41 DCHECK(schedule_->rpo_order()->empty()); | |
42 Scheduler::ComputeSpecialRPO(zone(), schedule_); | |
43 // Invalidate MachineAssembler. | |
44 Schedule* schedule = schedule_; | |
45 schedule_ = NULL; | |
46 return schedule; | |
47 } | |
48 | |
49 | |
50 Node* RawMachineAssembler::Parameter(size_t index) { | |
51 DCHECK(index < parameter_count()); | |
52 return parameters_[index]; | |
53 } | |
54 | |
55 | |
56 void RawMachineAssembler::Goto(Label* label) { | |
57 DCHECK(current_block_ != schedule()->end()); | |
58 schedule()->AddGoto(CurrentBlock(), Use(label)); | |
59 current_block_ = NULL; | |
60 } | |
61 | |
62 | |
63 void RawMachineAssembler::Branch(Node* condition, Label* true_val, | |
64 Label* false_val) { | |
65 DCHECK(current_block_ != schedule()->end()); | |
66 Node* branch = NewNode(common()->Branch(), condition); | |
67 schedule()->AddBranch(CurrentBlock(), branch, Use(true_val), Use(false_val)); | |
68 current_block_ = NULL; | |
69 } | |
70 | |
71 | |
72 void RawMachineAssembler::Switch(Node* index, Label* default_label, | |
73 int32_t* case_values, Label** case_labels, | |
74 size_t case_count) { | |
75 DCHECK_NE(schedule()->end(), current_block_); | |
76 size_t succ_count = case_count + 1; | |
77 Node* switch_node = NewNode(common()->Switch(succ_count), index); | |
78 BasicBlock** succ_blocks = zone()->NewArray<BasicBlock*>(succ_count); | |
79 for (size_t index = 0; index < case_count; ++index) { | |
80 int32_t case_value = case_values[index]; | |
81 BasicBlock* case_block = Use(case_labels[index]); | |
82 Node* case_node = | |
83 graph()->NewNode(common()->IfValue(case_value), switch_node); | |
84 schedule()->AddNode(case_block, case_node); | |
85 succ_blocks[index] = case_block; | |
86 } | |
87 BasicBlock* default_block = Use(default_label); | |
88 Node* default_node = graph()->NewNode(common()->IfDefault(), switch_node); | |
89 schedule()->AddNode(default_block, default_node); | |
90 succ_blocks[case_count] = default_block; | |
91 schedule()->AddSwitch(CurrentBlock(), switch_node, succ_blocks, succ_count); | |
92 current_block_ = nullptr; | |
93 } | |
94 | |
95 | |
96 void RawMachineAssembler::Return(Node* value) { | |
97 Node* ret = NewNode(common()->Return(), value); | |
98 schedule()->AddReturn(CurrentBlock(), ret); | |
99 current_block_ = NULL; | |
100 } | |
101 | |
102 | |
103 Node* RawMachineAssembler::CallFunctionStub0(Node* function, Node* receiver, | |
104 Node* context, Node* frame_state, | |
105 CallFunctionFlags flags) { | |
106 Callable callable = CodeFactory::CallFunction(isolate(), 0, flags); | |
107 CallDescriptor* desc = Linkage::GetStubCallDescriptor( | |
108 isolate(), zone(), callable.descriptor(), 1, | |
109 CallDescriptor::kNeedsFrameState, Operator::kNoProperties); | |
110 Node* stub_code = HeapConstant(callable.code()); | |
111 Node* call = graph()->NewNode(common()->Call(desc), stub_code, function, | |
112 receiver, context, frame_state); | |
113 schedule()->AddNode(CurrentBlock(), call); | |
114 return call; | |
115 } | |
116 | |
117 | |
118 Node* RawMachineAssembler::CallJS0(Node* function, Node* receiver, | |
119 Node* context, Node* frame_state) { | |
120 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor( | |
121 zone(), false, 1, CallDescriptor::kNeedsFrameState); | |
122 Node* call = graph()->NewNode(common()->Call(descriptor), function, receiver, | |
123 context, frame_state); | |
124 schedule()->AddNode(CurrentBlock(), call); | |
125 return call; | |
126 } | |
127 | |
128 | |
129 Node* RawMachineAssembler::CallRuntime1(Runtime::FunctionId function, | |
130 Node* arg0, Node* context, | |
131 Node* frame_state) { | |
132 CallDescriptor* descriptor = Linkage::GetRuntimeCallDescriptor( | |
133 zone(), function, 1, Operator::kNoProperties); | |
134 | |
135 Node* centry = HeapConstant(CEntryStub(isolate(), 1).GetCode()); | |
136 Node* ref = NewNode( | |
137 common()->ExternalConstant(ExternalReference(function, isolate()))); | |
138 Node* arity = Int32Constant(1); | |
139 | |
140 Node* call = graph()->NewNode(common()->Call(descriptor), centry, arg0, ref, | |
141 arity, context, frame_state); | |
142 schedule()->AddNode(CurrentBlock(), call); | |
143 return call; | |
144 } | |
145 | |
146 | |
147 void RawMachineAssembler::Bind(Label* label) { | |
148 DCHECK(current_block_ == NULL); | |
149 DCHECK(!label->bound_); | |
150 label->bound_ = true; | |
151 current_block_ = EnsureBlock(label); | |
152 } | |
153 | |
154 | |
155 BasicBlock* RawMachineAssembler::Use(Label* label) { | |
156 label->used_ = true; | |
157 return EnsureBlock(label); | |
158 } | |
159 | |
160 | |
161 BasicBlock* RawMachineAssembler::EnsureBlock(Label* label) { | |
162 if (label->block_ == NULL) label->block_ = schedule()->NewBasicBlock(); | |
163 return label->block_; | |
164 } | |
165 | |
166 | |
167 BasicBlock* RawMachineAssembler::CurrentBlock() { | |
168 DCHECK(current_block_); | |
169 return current_block_; | |
170 } | |
171 | |
172 | |
173 Node* RawMachineAssembler::MakeNode(const Operator* op, int input_count, | |
174 Node** inputs, bool incomplete) { | |
175 DCHECK(ScheduleValid()); | |
176 DCHECK(current_block_ != NULL); | |
177 Node* node = graph()->NewNode(op, input_count, inputs, incomplete); | |
178 BasicBlock* block = op->opcode() == IrOpcode::kParameter ? schedule()->start() | |
179 : CurrentBlock(); | |
180 if (op->opcode() != IrOpcode::kReturn) { | |
181 schedule()->AddNode(block, node); | |
182 } | |
183 return node; | |
184 } | |
185 | |
186 } // namespace compiler | |
187 } // namespace internal | |
188 } // namespace v8 | |
OLD | NEW |