OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/compiler/bytecode-graph-builder.h" | |
6 | |
7 #include "src/compiler/operator-properties.h" | |
8 | |
9 namespace v8 { | |
10 namespace internal { | |
11 namespace compiler { | |
12 | |
13 // Issues: | |
14 // - Need to deal with FrameState / FrameStateBeforeAndAfter / StateValue. | |
15 // - Scopes - intimately tied to AST. Need to eval what is needed. | |
16 // - Need story for context parameter, closure parameter, this. | |
17 // * GetFunctionClosureForContext() | |
18 // * GetFunctionClosure() | |
19 // * GetFunctionContext() | |
20 BytecodeGraphBuilder::Environment::Environment(BytecodeGraphBuilder* builder, | |
21 int locals_count, | |
22 int parameters_count, | |
23 Node* control_dependency) | |
24 : builder_(builder), | |
25 locals_count_(locals_count), | |
26 parameters_count_(parameters_count), | |
27 control_dependency_(control_dependency), | |
28 effect_dependency_(control_dependency), | |
29 values_(builder->local_zone()) { | |
30 // | |
31 // values_ layout | |
32 // | |
33 // [parameters] [receiver] [registers] | |
34 // | |
35 // The accumulator is treated as a bytecode register. As values are | |
36 // overwritten, the old values are pushed to the back of the values | |
37 // array. | |
38 // | |
39 | |
40 // parameters | |
41 for (int i = 0; i < parameters_count; i++) { | |
42 const Operator* op = common()->Parameter(i, nullptr); | |
43 Node* parameter = builder->graph()->NewNode(op, builder->graph()->start()); | |
44 values()->push_back(parameter); | |
45 } | |
46 | |
47 // TODO(oth): receiver | |
48 | |
49 // registers | |
50 register_base_ = static_cast<int>(values()->size()); | |
51 Node* undefined_constant = builder->jsgraph()->UndefinedConstant(); | |
52 values()->insert(values()->end(), locals_count, undefined_constant); | |
53 | |
54 // accumulator | |
55 accumulator_ = undefined_constant; | |
56 } | |
57 | |
58 | |
59 void BytecodeGraphBuilder::Environment::BindRegister(int register_index, | |
60 Node* node) { | |
61 DCHECK(register_index >= 0 || -register_index < parameters_count()); | |
62 int values_index = register_index + register_base(); | |
63 values()->at(values_index) = node; | |
64 } | |
65 | |
66 | |
67 Node* BytecodeGraphBuilder::Environment::LookupRegister( | |
68 int register_index) const { | |
69 DCHECK(register_index >= 0 || -register_index < parameters_count()); | |
70 int values_index = register_index + register_base(); | |
71 return values()->at(values_index); | |
72 } | |
73 | |
74 | |
75 void BytecodeGraphBuilder::Environment::BindAccumulator(Node* node) { | |
76 accumulator_ = node; | |
77 } | |
78 | |
79 | |
80 Node* BytecodeGraphBuilder::Environment::LookupAccumulator() const { | |
81 return accumulator_; | |
82 } | |
83 | |
84 | |
85 bool BytecodeGraphBuilder::Environment::IsMarkedAsUnreachable() const { | |
86 return GetControlDependency()->opcode() == IrOpcode::kDead; | |
87 } | |
88 | |
89 | |
90 void BytecodeGraphBuilder::Environment::MarkAsUnreachable() { | |
91 UpdateControlDependency(builder()->jsgraph()->Dead()); | |
92 } | |
93 | |
94 | |
95 BytecodeGraphBuilder::BytecodeGraphBuilder(Zone* local_zone, | |
96 CompilationInfo* compilation_info, | |
97 JSGraph* jsgraph) | |
98 : local_zone_(local_zone), | |
99 info_(compilation_info), | |
100 jsgraph_(jsgraph), | |
101 exit_controls_(local_zone) { | |
102 bytecode_array_ = handle(info()->shared_info()->bytecode_array()); | |
103 } | |
104 | |
105 | |
106 BytecodeGraphBuilder::BytecodeGraphBuilder(Zone* local_zone, | |
107 Handle<BytecodeArray> bytecode_array, | |
108 JSGraph* jsgraph) | |
109 : local_zone_(local_zone), | |
110 info_(nullptr), | |
111 jsgraph_(jsgraph), | |
112 bytecode_array_(bytecode_array), | |
113 input_buffer_size_(0), | |
114 input_buffer_(nullptr), | |
115 exit_controls_(local_zone) {} | |
116 | |
117 | |
118 bool BytecodeGraphBuilder::CreateGraph(bool stack_check) { | |
119 // Set up the basic structure of the graph. Outputs for {Start} are | |
120 // the formal parameters (including the receiver) plus context and | |
121 // closure. | |
122 | |
123 // TODO(oth): add context and closure as additional parameters | |
124 int actual_parameter_count = info()->num_parameters_including_this(); | |
125 graph()->SetStart(graph()->NewNode(common()->Start(actual_parameter_count))); | |
126 | |
127 // Locals count are the explicitly named interpreter registers. | |
128 int locals_count = bytecode_array()->local_count(); | |
129 Environment env(this, locals_count, actual_parameter_count, graph()->start()); | |
130 set_environment(&env); | |
131 | |
132 // Build function context only if there are context allocated variables. | |
133 if (info()->num_heap_slots() > 0) { | |
134 UNIMPLEMENTED(); // write ast-graph-builder equivalent. | |
135 } else { | |
136 // Simply use the outer function context in building the graph. | |
137 CreateGraphBody(stack_check); | |
138 } | |
139 | |
140 // Finish the basic structure of the graph. | |
141 DCHECK_NE(0u, exit_controls_.size()); | |
142 int const input_count = static_cast<int>(exit_controls_.size()); | |
143 Node** const inputs = &exit_controls_.front(); | |
144 Node* end = graph()->NewNode(common()->End(input_count), input_count, inputs); | |
145 graph()->SetEnd(end); | |
146 | |
147 return true; | |
148 } | |
149 | |
150 | |
151 bool BytecodeGraphBuilder::CreateGraphForTest(bool stack_check) { | |
Michael Starzinger
2015/09/02 12:19:22
Would be nice to only have one entry point. Especi
| |
152 // Set up the basic structure of the graph. Outputs for {Start} are | |
153 // the formal parameters (including the receiver) plus context and | |
154 // closure. | |
155 | |
156 // TODO(oth): add context and closure as additional parameters | |
157 int actual_parameter_count = bytecode_array()->parameter_count(); | |
158 graph()->SetStart(graph()->NewNode(common()->Start(actual_parameter_count))); | |
159 | |
160 // Locals count are the explicitly named interpreter registers. | |
161 int locals_count = bytecode_array()->frame_size() / kPointerSize; | |
162 Environment env(this, locals_count, actual_parameter_count, graph()->start()); | |
163 set_environment(&env); | |
164 | |
165 // Build function context only if there are context allocated variables. | |
166 // - Assume no context allocated variables. | |
167 // Simply use the outer function context in building the graph. | |
168 CreateGraphBody(stack_check); | |
169 | |
170 // Finish the basic structure of the graph. | |
171 DCHECK_NE(0u, exit_controls_.size()); | |
172 int const input_count = static_cast<int>(exit_controls_.size()); | |
173 Node** const inputs = &exit_controls_.front(); | |
174 Node* end = graph()->NewNode(common()->End(input_count), input_count, inputs); | |
175 graph()->SetEnd(end); | |
176 | |
177 return true; | |
178 } | |
179 | |
180 | |
181 void BytecodeGraphBuilder::CreateGraphBody(bool stack_check) { | |
182 // TODO(oth): review ast-graph-builder equivalent, ie arguments | |
183 // object setup, this function variable if used, tracing hooks. | |
184 VisitBytecodes(); | |
185 } | |
186 | |
187 | |
188 void BytecodeGraphBuilder::VisitBytecodes() { | |
189 int last_bytecode_length = 0; | |
190 for (int i = 0; i < bytecode_array()->length(); i += last_bytecode_length) { | |
191 interpreter::Bytecode bytecode = bytecode_at(i); | |
192 int operand_offset = i + 1; | |
193 switch (bytecode) { | |
194 #define BYTECODE_CASE(name, ...) \ | |
195 case interpreter::Bytecode::k##name: \ | |
196 Visit##name(operand_offset); \ | |
197 break; | |
198 BYTECODE_LIST(BYTECODE_CASE) | |
199 #undef BYTECODE_CODE | |
200 } | |
201 last_bytecode_length = interpreter::Bytecodes::Size(bytecode); | |
202 } | |
203 } | |
204 | |
205 | |
206 void BytecodeGraphBuilder::VisitLdaZero(int operand_offset) { | |
207 Node* node = jsgraph()->ZeroConstant(); | |
208 environment()->BindAccumulator(node); | |
209 } | |
210 | |
211 | |
212 void BytecodeGraphBuilder::VisitLdaSmi8(int operand_offset) { | |
213 Node* node = jsgraph()->Int32Constant(smi8_at(operand_offset)); | |
214 environment()->BindAccumulator(node); | |
215 } | |
216 | |
217 | |
218 void BytecodeGraphBuilder::VisitLdaConstant(int operand_offset) { | |
219 Node* node = jsgraph()->Constant(constant_at(operand_offset)); | |
220 environment()->BindAccumulator(node); | |
221 } | |
222 | |
223 | |
224 void BytecodeGraphBuilder::VisitLdaUndefined(int operand_offset) { | |
225 Node* node = jsgraph()->UndefinedConstant(); | |
226 environment()->BindAccumulator(node); | |
227 } | |
228 | |
229 | |
230 void BytecodeGraphBuilder::VisitLdaNull(int operand_offset) { | |
231 Node* node = jsgraph()->NullConstant(); | |
232 environment()->BindAccumulator(node); | |
233 } | |
234 | |
235 | |
236 void BytecodeGraphBuilder::VisitLdaTheHole(int operand_offset) { | |
237 Node* node = jsgraph()->TheHoleConstant(); | |
238 environment()->BindAccumulator(node); | |
239 } | |
240 | |
241 | |
242 void BytecodeGraphBuilder::VisitLdaTrue(int operand_offset) { | |
243 Node* node = jsgraph()->TrueConstant(); | |
244 environment()->BindAccumulator(node); | |
245 } | |
246 | |
247 | |
248 void BytecodeGraphBuilder::VisitLdaFalse(int operand_offset) { | |
249 Node* node = jsgraph()->FalseConstant(); | |
250 environment()->BindAccumulator(node); | |
251 } | |
252 | |
253 | |
254 void BytecodeGraphBuilder::VisitLdar(int operand_offset) { | |
255 Node* value = environment()->LookupRegister(register_at(operand_offset)); | |
256 environment()->BindAccumulator(value); | |
257 } | |
258 | |
259 | |
260 void BytecodeGraphBuilder::VisitStar(int operand_offset) { | |
261 Node* value = environment()->LookupAccumulator(); | |
262 environment()->BindRegister(register_at(operand_offset), value); | |
263 } | |
264 | |
265 | |
266 void BytecodeGraphBuilder::FinishBinaryOperation(Node* node) { | |
267 // What is the minimal work here? | |
268 // In the initial absence of: | |
269 // - FrameStateBeforeAndAfter / BailOutId | |
270 // - Environment check pointing | |
271 environment()->BindAccumulator(node); | |
272 } | |
273 | |
274 | |
275 void BytecodeGraphBuilder::VisitAdd(int operand_offset) { | |
276 const Operator* js_op = javascript()->Add(language_mode()); | |
277 Node* left = environment()->LookupRegister(register_at(operand_offset)); | |
278 Node* right = environment()->LookupAccumulator(); | |
279 Node* node = NewNode(js_op, left, right); | |
280 FinishBinaryOperation(node); | |
281 } | |
282 | |
283 | |
284 void BytecodeGraphBuilder::VisitSub(int operand_offset) { | |
285 const Operator* js_op = javascript()->Subtract(language_mode()); | |
286 Node* left = environment()->LookupRegister(register_at(operand_offset)); | |
287 Node* right = environment()->LookupAccumulator(); | |
288 Node* node = NewNode(js_op, left, right); | |
289 FinishBinaryOperation(node); | |
290 } | |
291 | |
292 | |
293 void BytecodeGraphBuilder::VisitMul(int operand_offset) { | |
294 const Operator* js_op = javascript()->Multiply(language_mode()); | |
295 Node* left = environment()->LookupRegister(register_at(operand_offset)); | |
296 Node* right = environment()->LookupAccumulator(); | |
297 Node* node = NewNode(js_op, left, right); | |
298 FinishBinaryOperation(node); | |
299 } | |
300 | |
301 | |
302 void BytecodeGraphBuilder::VisitDiv(int operand_offset) { | |
303 const Operator* js_op = javascript()->Divide(language_mode()); | |
304 Node* left = environment()->LookupRegister(register_at(operand_offset)); | |
305 Node* right = environment()->LookupAccumulator(); | |
306 Node* node = NewNode(js_op, left, right); | |
307 FinishBinaryOperation(node); | |
308 } | |
309 | |
310 | |
311 void BytecodeGraphBuilder::VisitMod(int operand_offset) { | |
312 const Operator* js_op = javascript()->Modulus(language_mode()); | |
313 Node* left = environment()->LookupRegister(register_at(operand_offset)); | |
314 Node* right = environment()->LookupAccumulator(); | |
315 Node* node = NewNode(js_op, left, right); | |
316 FinishBinaryOperation(node); | |
317 } | |
318 | |
319 | |
320 void BytecodeGraphBuilder::VisitReturn(int operand_offset) { | |
321 Node* control = | |
322 NewNode(common()->Return(), environment()->LookupAccumulator()); | |
323 UpdateControlDependencyToLeaveFunction(control); | |
324 } | |
325 | |
326 | |
327 Node** BytecodeGraphBuilder::EnsureInputBufferSize(int size) { | |
328 if (size > input_buffer_size_) { | |
329 size = size + kInputBufferSizeIncrement + input_buffer_size_; | |
330 input_buffer_ = local_zone()->NewArray<Node*>(size); | |
331 input_buffer_size_ = size; | |
332 } | |
333 return input_buffer_; | |
334 } | |
335 | |
336 | |
337 Node* BytecodeGraphBuilder::MakeNode(const Operator* op, int value_input_count, | |
338 Node** value_inputs, bool incomplete) { | |
339 DCHECK_EQ(op->ValueInputCount(), value_input_count); | |
340 | |
341 bool has_context = OperatorProperties::HasContextInput(op); | |
342 int frame_state_count = OperatorProperties::GetFrameStateInputCount(op); | |
343 bool has_control = op->ControlInputCount() == 1; | |
344 bool has_effect = op->EffectInputCount() == 1; | |
345 | |
346 DCHECK_LT(op->ControlInputCount(), 2); | |
347 DCHECK_LT(op->EffectInputCount(), 2); | |
348 | |
349 Node* result = NULL; | |
350 if (!has_context && frame_state_count == 0 && !has_control && !has_effect) { | |
351 result = graph()->NewNode(op, value_input_count, value_inputs, incomplete); | |
352 } else { | |
353 int input_count_with_deps = value_input_count; | |
354 if (has_context) ++input_count_with_deps; | |
355 input_count_with_deps += frame_state_count; | |
356 if (has_control) ++input_count_with_deps; | |
357 if (has_effect) ++input_count_with_deps; | |
358 Node** buffer = EnsureInputBufferSize(input_count_with_deps); | |
359 memcpy(buffer, value_inputs, kPointerSize * value_input_count); | |
360 Node** current_input = buffer + value_input_count; | |
361 if (has_context) { | |
362 // TODO(oth): need context. | |
363 *current_input++ = jsgraph()->Dead(); | |
364 } | |
365 for (int i = 0; i < frame_state_count; i++) { | |
366 // The frame state will be inserted later. Here we misuse | |
367 // the {Dead} node as a sentinel to be later overwritten | |
368 // with the real frame state. | |
369 *current_input++ = jsgraph()->Dead(); | |
370 } | |
371 if (has_effect) { | |
372 *current_input++ = environment()->GetEffectDependency(); | |
373 } | |
374 if (has_control) { | |
375 *current_input++ = environment()->GetControlDependency(); | |
376 } | |
377 result = graph()->NewNode(op, input_count_with_deps, buffer, incomplete); | |
378 if (!environment()->IsMarkedAsUnreachable()) { | |
379 // Update the current control dependency for control-producing nodes. | |
380 if (NodeProperties::IsControl(result)) { | |
381 environment()->UpdateControlDependency(result); | |
382 } | |
383 // Update the current effect dependency for effect-producing nodes. | |
384 if (result->op()->EffectOutputCount() > 0) { | |
385 environment()->UpdateEffectDependency(result); | |
386 } | |
387 // Add implicit success continuation for throwing nodes. | |
388 if (!result->op()->HasProperty(Operator::kNoThrow)) { | |
389 const Operator* op = common()->IfSuccess(); | |
390 Node* on_success = graph()->NewNode(op, result); | |
391 environment_->UpdateControlDependency(on_success); | |
392 } | |
393 } | |
394 } | |
395 | |
396 return result; | |
397 } | |
398 | |
399 | |
400 Node* BytecodeGraphBuilder::MergeControl(Node* control, Node* other) { | |
401 int inputs = control->op()->ControlInputCount() + 1; | |
402 if (control->opcode() == IrOpcode::kLoop) { | |
403 // Control node for loop exists, add input. | |
404 const Operator* op = common()->Loop(inputs); | |
405 control->AppendInput(graph_zone(), other); | |
406 control->set_op(op); | |
407 } else if (control->opcode() == IrOpcode::kMerge) { | |
408 // Control node for merge exists, add input. | |
409 const Operator* op = common()->Merge(inputs); | |
410 control->AppendInput(graph_zone(), other); | |
411 control->set_op(op); | |
412 } else { | |
413 // Control node is a singleton, introduce a merge. | |
414 const Operator* op = common()->Merge(inputs); | |
415 Node* inputs[] = {control, other}; | |
416 control = graph()->NewNode(op, arraysize(inputs), inputs, true); | |
417 } | |
418 return control; | |
419 } | |
420 | |
421 | |
422 void BytecodeGraphBuilder::UpdateControlDependencyToLeaveFunction(Node* exit) { | |
423 if (environment()->IsMarkedAsUnreachable()) return; | |
424 environment()->MarkAsUnreachable(); | |
425 exit_controls_.push_back(exit); | |
426 } | |
427 | |
428 | |
429 interpreter::Bytecode BytecodeGraphBuilder::bytecode_at(int offset) const { | |
430 return interpreter::Bytecodes::FromByte(bytecode_array()->get(offset)); | |
431 } | |
432 | |
433 | |
434 Handle<Object> BytecodeGraphBuilder::constant_at(int offset) const { | |
435 Handle<FixedArray> constants = handle(bytecode_array()->constant_pool()); | |
436 int constant_index = static_cast<int8_t>(bytecode_array()->get(offset)); | |
437 return FixedArray::get(constants, constant_index); | |
438 } | |
439 | |
440 | |
441 int8_t BytecodeGraphBuilder::smi8_at(int offset) const { | |
442 return static_cast<int8_t>(bytecode_array()->get(offset)); | |
443 } | |
444 | |
445 | |
446 int8_t BytecodeGraphBuilder::register_at(int offset) const { | |
447 return static_cast<int8_t>(-bytecode_array()->get(offset)); | |
448 } | |
449 | |
450 } // namespace compiler | |
451 } // namespace internal | |
452 } // namespace v8 | |
OLD | NEW |