1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "vm/compiler/frontend/bytecode_flow_graph_builder.h" |
6 | |
7 | #include "vm/compiler/backend/il_printer.h" |
8 | #include "vm/compiler/ffi/callback.h" |
9 | #include "vm/compiler/frontend/bytecode_reader.h" |
10 | #include "vm/compiler/frontend/prologue_builder.h" |
11 | #include "vm/compiler/jit/compiler.h" |
12 | #include "vm/object_store.h" |
13 | #include "vm/stack_frame.h" |
14 | #include "vm/stack_frame_kbc.h" |
15 | |
16 | #define B (flow_graph_builder_) |
17 | #define Z (zone_) |
18 | |
19 | namespace dart { |
20 | |
21 | DEFINE_FLAG(bool, |
22 | print_flow_graph_from_bytecode, |
23 | false, |
24 | "Print flow graph constructed from bytecode" ); |
25 | |
26 | namespace kernel { |
27 | |
28 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandA() { |
29 | if (is_generating_interpreter()) { |
30 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
31 | } else { |
32 | intptr_t value = KernelBytecode::DecodeA(bytecode_instr_); |
33 | return Operand(value); |
34 | } |
35 | } |
36 | |
37 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandB() { |
38 | if (is_generating_interpreter()) { |
39 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
40 | } else { |
41 | intptr_t value = KernelBytecode::DecodeB(bytecode_instr_); |
42 | return Operand(value); |
43 | } |
44 | } |
45 | |
46 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandC() { |
47 | if (is_generating_interpreter()) { |
48 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
49 | } else { |
50 | intptr_t value = KernelBytecode::DecodeC(bytecode_instr_); |
51 | return Operand(value); |
52 | } |
53 | } |
54 | |
55 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandD() { |
56 | if (is_generating_interpreter()) { |
57 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
58 | } else { |
59 | intptr_t value = KernelBytecode::DecodeD(bytecode_instr_); |
60 | return Operand(value); |
61 | } |
62 | } |
63 | |
64 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandE() { |
65 | if (is_generating_interpreter()) { |
66 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
67 | } else { |
68 | intptr_t value = KernelBytecode::DecodeE(bytecode_instr_); |
69 | return Operand(value); |
70 | } |
71 | } |
72 | |
73 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandF() { |
74 | if (is_generating_interpreter()) { |
75 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
76 | } else { |
77 | intptr_t value = KernelBytecode::DecodeF(bytecode_instr_); |
78 | return Operand(value); |
79 | } |
80 | } |
81 | |
82 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandX() { |
83 | if (is_generating_interpreter()) { |
84 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
85 | } else { |
86 | intptr_t value = KernelBytecode::DecodeX(bytecode_instr_); |
87 | return Operand(value); |
88 | } |
89 | } |
90 | |
91 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandY() { |
92 | if (is_generating_interpreter()) { |
93 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
94 | } else { |
95 | intptr_t value = KernelBytecode::DecodeY(bytecode_instr_); |
96 | return Operand(value); |
97 | } |
98 | } |
99 | |
100 | BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandT() { |
101 | if (is_generating_interpreter()) { |
102 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
103 | } else { |
104 | intptr_t value = KernelBytecode::DecodeT(bytecode_instr_); |
105 | return Operand(value); |
106 | } |
107 | } |
108 | |
109 | BytecodeFlowGraphBuilder::Constant BytecodeFlowGraphBuilder::ConstantAt( |
110 | Operand entry_index, |
111 | intptr_t add_index) { |
112 | if (is_generating_interpreter()) { |
113 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
114 | } else { |
115 | const Object& value = Object::ZoneHandle( |
116 | Z, object_pool_.ObjectAt(entry_index.value() + add_index)); |
117 | return Constant(Z, value); |
118 | } |
119 | } |
120 | |
121 | void BytecodeFlowGraphBuilder::PushConstant(Constant constant) { |
122 | if (is_generating_interpreter()) { |
123 | B->Push(constant.definition()); |
124 | } else { |
125 | code_ += B->Constant(constant.value()); |
126 | } |
127 | } |
128 | |
129 | BytecodeFlowGraphBuilder::Constant BytecodeFlowGraphBuilder::PopConstant() { |
130 | if (is_generating_interpreter()) { |
131 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
132 | } else { |
133 | ASSERT(!IsStackEmpty()); |
134 | const Object& value = B->stack_->definition()->AsConstant()->value(); |
135 | code_ += B->Drop(); |
136 | return Constant(Z, value); |
137 | } |
138 | } |
139 | |
140 | void BytecodeFlowGraphBuilder::LoadStackSlots(intptr_t num_slots) { |
141 | if (is_generating_interpreter()) { |
142 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
143 | } |
144 | |
145 | ASSERT(GetStackDepth() >= num_slots); |
146 | } |
147 | |
148 | void BytecodeFlowGraphBuilder::AllocateLocalVariables( |
149 | Operand frame_size, |
150 | intptr_t num_param_locals) { |
151 | if (is_generating_interpreter()) { |
152 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
153 | } else { |
154 | ASSERT(local_vars_.is_empty()); |
155 | |
156 | const intptr_t num_bytecode_locals = frame_size.value(); |
157 | ASSERT(num_bytecode_locals >= 0); |
158 | |
159 | intptr_t num_locals = num_bytecode_locals; |
160 | if (exception_var_ != nullptr) { |
161 | ++num_locals; |
162 | } |
163 | if (stacktrace_var_ != nullptr) { |
164 | ++num_locals; |
165 | } |
166 | if (scratch_var_ != nullptr) { |
167 | ++num_locals; |
168 | } |
169 | if (parsed_function()->has_arg_desc_var()) { |
170 | ++num_locals; |
171 | } |
172 | if (parsed_function()->has_entry_points_temp_var()) { |
173 | ++num_locals; |
174 | } |
175 | |
176 | if (num_locals == 0) { |
177 | return; |
178 | } |
179 | |
180 | local_vars_.EnsureLength(num_bytecode_locals, nullptr); |
181 | intptr_t idx = num_param_locals; |
182 | for (; idx < num_bytecode_locals; ++idx) { |
183 | String& name = String::ZoneHandle( |
184 | Z, Symbols::NewFormatted(thread(), "var%" Pd, idx)); |
185 | LocalVariable* local = new (Z) |
186 | LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
187 | name, Object::dynamic_type()); |
188 | local->set_index(VariableIndex(-idx)); |
189 | local_vars_[idx] = local; |
190 | } |
191 | |
192 | if (exception_var_ != nullptr) { |
193 | exception_var_->set_index(VariableIndex(-idx)); |
194 | ++idx; |
195 | } |
196 | if (stacktrace_var_ != nullptr) { |
197 | stacktrace_var_->set_index(VariableIndex(-idx)); |
198 | ++idx; |
199 | } |
200 | if (scratch_var_ != nullptr) { |
201 | scratch_var_->set_index(VariableIndex(-idx)); |
202 | ++idx; |
203 | } |
204 | if (parsed_function()->has_arg_desc_var()) { |
205 | parsed_function()->arg_desc_var()->set_index(VariableIndex(-idx)); |
206 | ++idx; |
207 | } |
208 | if (parsed_function()->has_entry_points_temp_var()) { |
209 | parsed_function()->entry_points_temp_var()->set_index( |
210 | VariableIndex(-idx)); |
211 | ++idx; |
212 | } |
213 | ASSERT(idx == num_locals); |
214 | |
215 | ASSERT(parsed_function()->scope() == nullptr); |
216 | parsed_function()->AllocateBytecodeVariables(num_locals); |
217 | } |
218 | } |
219 | |
220 | LocalVariable* BytecodeFlowGraphBuilder::AllocateParameter( |
221 | intptr_t param_index, |
222 | VariableIndex var_index) { |
223 | const String& name = |
224 | String::ZoneHandle(Z, function().ParameterNameAt(param_index)); |
225 | const AbstractType& type = |
226 | AbstractType::ZoneHandle(Z, function().ParameterTypeAt(param_index)); |
227 | |
228 | CompileType* param_type = nullptr; |
229 | if (!inferred_types_attribute_.IsNull()) { |
230 | // Parameter types are assigned to synthetic PCs = -N,..,-1 |
231 | // where N is number of parameters. |
232 | const intptr_t pc = -function().NumParameters() + param_index; |
233 | // Search from the beginning as parameters may be declared in arbitrary |
234 | // order. |
235 | inferred_types_index_ = 0; |
236 | const InferredTypeMetadata inferred_type = GetInferredType(pc); |
237 | if (!inferred_type.IsTrivial()) { |
238 | param_type = new (Z) CompileType(inferred_type.ToCompileType(Z)); |
239 | } |
240 | } |
241 | |
242 | LocalVariable* param_var = |
243 | new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
244 | name, type, param_type); |
245 | param_var->set_index(var_index); |
246 | |
247 | if (!function().IsNonImplicitClosureFunction() && |
248 | (function().is_static() || |
249 | ((function().name() != Symbols::Call().raw()) && |
250 | !parsed_function()->IsCovariantParameter(param_index) && |
251 | !parsed_function()->IsGenericCovariantImplParameter(param_index)))) { |
252 | param_var->set_type_check_mode(LocalVariable::kTypeCheckedByCaller); |
253 | } |
254 | |
255 | if (var_index.value() <= 0) { |
256 | local_vars_[-var_index.value()] = param_var; |
257 | } |
258 | |
259 | return param_var; |
260 | } |
261 | |
262 | void BytecodeFlowGraphBuilder::AllocateFixedParameters() { |
263 | if (is_generating_interpreter()) { |
264 | return; |
265 | } |
266 | |
267 | ASSERT(!function().HasOptionalParameters()); |
268 | |
269 | const intptr_t num_fixed_params = function().num_fixed_parameters(); |
270 | auto parameters = |
271 | new (Z) ZoneGrowableArray<LocalVariable*>(Z, num_fixed_params); |
272 | |
273 | for (intptr_t i = 0; i < num_fixed_params; ++i) { |
274 | LocalVariable* param_var = |
275 | AllocateParameter(i, VariableIndex(num_fixed_params - i)); |
276 | parameters->Add(param_var); |
277 | } |
278 | |
279 | parsed_function()->SetRawParameters(parameters); |
280 | } |
281 | |
282 | const KBCInstr* |
283 | BytecodeFlowGraphBuilder::AllocateParametersAndLocalsForEntryOptional() { |
284 | ASSERT(KernelBytecode::IsEntryOptionalOpcode(bytecode_instr_)); |
285 | |
286 | const intptr_t num_fixed_params = DecodeOperandA().value(); |
287 | const intptr_t num_opt_pos_params = DecodeOperandB().value(); |
288 | const intptr_t num_opt_named_params = DecodeOperandC().value(); |
289 | |
290 | ASSERT(num_fixed_params == function().num_fixed_parameters()); |
291 | ASSERT(num_opt_pos_params == function().NumOptionalPositionalParameters()); |
292 | ASSERT(num_opt_named_params == function().NumOptionalNamedParameters()); |
293 | |
294 | ASSERT((num_opt_pos_params == 0) || (num_opt_named_params == 0)); |
295 | const intptr_t num_load_const = num_opt_pos_params + 2 * num_opt_named_params; |
296 | |
297 | const KBCInstr* instr = KernelBytecode::Next(bytecode_instr_); |
298 | const KBCInstr* frame_instr = instr; |
299 | for (intptr_t i = 0; i < num_load_const; ++i) { |
300 | frame_instr = KernelBytecode::Next(frame_instr); |
301 | } |
302 | ASSERT(KernelBytecode::IsFrameOpcode(frame_instr)); |
303 | const intptr_t = KernelBytecode::DecodeD(frame_instr); |
304 | const intptr_t num_params = |
305 | num_fixed_params + num_opt_pos_params + num_opt_named_params; |
306 | const intptr_t total_locals = num_params + num_extra_locals; |
307 | |
308 | AllocateLocalVariables(Operand(total_locals), num_params); |
309 | |
310 | ZoneGrowableArray<const Instance*>* default_values = |
311 | new (Z) ZoneGrowableArray<const Instance*>( |
312 | Z, num_opt_pos_params + num_opt_named_params); |
313 | ZoneGrowableArray<LocalVariable*>* raw_parameters = |
314 | new (Z) ZoneGrowableArray<LocalVariable*>(Z, num_params); |
315 | |
316 | intptr_t param = 0; |
317 | for (; param < num_fixed_params; ++param) { |
318 | LocalVariable* param_var = AllocateParameter(param, VariableIndex(-param)); |
319 | raw_parameters->Add(param_var); |
320 | } |
321 | |
322 | for (intptr_t i = 0; i < num_opt_pos_params; ++i, ++param) { |
323 | const KBCInstr* load_value_instr = instr; |
324 | instr = KernelBytecode::Next(instr); |
325 | ASSERT(KernelBytecode::IsLoadConstantOpcode(load_value_instr)); |
326 | ASSERT(KernelBytecode::DecodeA(load_value_instr) == param); |
327 | const Object& default_value = |
328 | ConstantAt(Operand(KernelBytecode::DecodeE(load_value_instr))).value(); |
329 | |
330 | LocalVariable* param_var = AllocateParameter(param, VariableIndex(-param)); |
331 | raw_parameters->Add(param_var); |
332 | default_values->Add( |
333 | &Instance::ZoneHandle(Z, Instance::RawCast(default_value.raw()))); |
334 | } |
335 | |
336 | if (num_opt_named_params > 0) { |
337 | default_values->EnsureLength(num_opt_named_params, nullptr); |
338 | raw_parameters->EnsureLength(num_params, nullptr); |
339 | |
340 | ASSERT(scratch_var_ != nullptr); |
341 | |
342 | for (intptr_t i = 0; i < num_opt_named_params; ++i, ++param) { |
343 | const KBCInstr* load_name_instr = instr; |
344 | const KBCInstr* load_value_instr = KernelBytecode::Next(load_name_instr); |
345 | instr = KernelBytecode::Next(load_value_instr); |
346 | ASSERT(KernelBytecode::IsLoadConstantOpcode(load_name_instr)); |
347 | ASSERT(KernelBytecode::IsLoadConstantOpcode(load_value_instr)); |
348 | const String& param_name = String::Cast( |
349 | ConstantAt(Operand(KernelBytecode::DecodeE(load_name_instr))) |
350 | .value()); |
351 | ASSERT(param_name.IsSymbol()); |
352 | const Object& default_value = |
353 | ConstantAt(Operand(KernelBytecode::DecodeE(load_value_instr))) |
354 | .value(); |
355 | |
356 | intptr_t param_index = num_fixed_params; |
357 | for (; param_index < num_params; ++param_index) { |
358 | if (function().ParameterNameAt(param_index) == param_name.raw()) { |
359 | break; |
360 | } |
361 | } |
362 | ASSERT(param_index < num_params); |
363 | |
364 | ASSERT(default_values->At(param_index - num_fixed_params) == nullptr); |
365 | (*default_values)[param_index - num_fixed_params] = |
366 | &Instance::ZoneHandle(Z, Instance::RawCast(default_value.raw())); |
367 | |
368 | const intptr_t local_index = KernelBytecode::DecodeA(load_name_instr); |
369 | ASSERT(local_index == KernelBytecode::DecodeA(load_value_instr)); |
370 | |
371 | LocalVariable* param_var = |
372 | AllocateParameter(param_index, VariableIndex(-param)); |
373 | ASSERT(raw_parameters->At(param_index) == nullptr); |
374 | (*raw_parameters)[param_index] = param_var; |
375 | } |
376 | } |
377 | |
378 | ASSERT(instr == frame_instr); |
379 | |
380 | parsed_function()->set_default_parameter_values(default_values); |
381 | parsed_function()->SetRawParameters(raw_parameters); |
382 | |
383 | return KernelBytecode::Next(frame_instr); |
384 | } |
385 | |
386 | LocalVariable* BytecodeFlowGraphBuilder::LocalVariableAt(intptr_t local_index) { |
387 | ASSERT(!is_generating_interpreter()); |
388 | if (local_index < 0) { |
389 | // Parameter |
390 | ASSERT(!function().HasOptionalParameters()); |
391 | const intptr_t param_index = local_index + |
392 | function().num_fixed_parameters() + |
393 | kKBCParamEndSlotFromFp; |
394 | ASSERT((0 <= param_index) && |
395 | (param_index < function().num_fixed_parameters())); |
396 | return parsed_function()->RawParameterVariable(param_index); |
397 | } else { |
398 | return local_vars_.At(local_index); |
399 | } |
400 | } |
401 | |
402 | void BytecodeFlowGraphBuilder::StoreLocal(Operand local_index) { |
403 | if (is_generating_interpreter()) { |
404 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
405 | } else { |
406 | LocalVariable* local_var = LocalVariableAt(local_index.value()); |
407 | code_ += B->StoreLocalRaw(position_, local_var); |
408 | } |
409 | } |
410 | |
411 | void BytecodeFlowGraphBuilder::LoadLocal(Operand local_index) { |
412 | if (is_generating_interpreter()) { |
413 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
414 | } else { |
415 | LocalVariable* local_var = LocalVariableAt(local_index.value()); |
416 | code_ += B->LoadLocal(local_var); |
417 | } |
418 | } |
419 | |
420 | Value* BytecodeFlowGraphBuilder::Pop() { |
421 | LoadStackSlots(1); |
422 | return B->Pop(); |
423 | } |
424 | |
425 | intptr_t BytecodeFlowGraphBuilder::GetStackDepth() const { |
426 | ASSERT(!is_generating_interpreter()); |
427 | return B->GetStackDepth(); |
428 | } |
429 | |
430 | bool BytecodeFlowGraphBuilder::IsStackEmpty() const { |
431 | ASSERT(!is_generating_interpreter()); |
432 | return B->GetStackDepth() == 0; |
433 | } |
434 | |
435 | InferredTypeMetadata BytecodeFlowGraphBuilder::GetInferredType(intptr_t pc) { |
436 | ASSERT(!inferred_types_attribute_.IsNull()); |
437 | intptr_t i = inferred_types_index_; |
438 | const intptr_t len = inferred_types_attribute_.Length(); |
439 | for (; i < len; i += InferredTypeBytecodeAttribute::kNumElements) { |
440 | ASSERT(i + InferredTypeBytecodeAttribute::kNumElements <= len); |
441 | const intptr_t attr_pc = |
442 | InferredTypeBytecodeAttribute::GetPCAt(inferred_types_attribute_, i); |
443 | if (attr_pc == pc) { |
444 | const InferredTypeMetadata result = |
445 | InferredTypeBytecodeAttribute::GetInferredTypeAt( |
446 | Z, inferred_types_attribute_, i); |
447 | // Found. Next time, continue search at the next entry. |
448 | inferred_types_index_ = i + InferredTypeBytecodeAttribute::kNumElements; |
449 | return result; |
450 | } |
451 | if (attr_pc > pc) { |
452 | break; |
453 | } |
454 | } |
455 | // Not found. Next time, continue search at the last inspected entry. |
456 | inferred_types_index_ = i; |
457 | return InferredTypeMetadata(kDynamicCid, InferredTypeMetadata::kFlagNullable); |
458 | } |
459 | |
460 | void BytecodeFlowGraphBuilder::PropagateStackState(intptr_t target_pc) { |
461 | if (is_generating_interpreter() || IsStackEmpty()) { |
462 | return; |
463 | } |
464 | |
465 | Value* current_stack = B->stack_; |
466 | Value* target_stack = stack_states_.Lookup(target_pc); |
467 | |
468 | if (target_stack != nullptr) { |
469 | // Control flow join should observe the same stack state from |
470 | // all incoming branches. |
471 | RELEASE_ASSERT(target_stack == current_stack); |
472 | } else { |
473 | // Stack state propagation is supported for forward branches only. |
474 | RELEASE_ASSERT(target_pc > pc_); |
475 | stack_states_.Insert(target_pc, current_stack); |
476 | } |
477 | } |
478 | |
479 | // Drop values from the stack unless they are used in control flow joins |
480 | // which are not generated yet (dartbug.com/36374). |
481 | void BytecodeFlowGraphBuilder::DropUnusedValuesFromStack() { |
482 | intptr_t drop_depth = GetStackDepth(); |
483 | auto it = stack_states_.GetIterator(); |
484 | for (const auto* current = it.Next(); current != nullptr; |
485 | current = it.Next()) { |
486 | if (current->key > pc_) { |
487 | Value* used_value = current->value; |
488 | Value* value = B->stack_; |
489 | // Find if a value on the expression stack is used in a propagated |
490 | // stack state, and adjust [drop_depth] to preserve it. |
491 | for (intptr_t i = 0; i < drop_depth; ++i) { |
492 | if (value == used_value) { |
493 | drop_depth = i; |
494 | break; |
495 | } |
496 | value = value->next_use(); |
497 | } |
498 | } |
499 | } |
500 | for (intptr_t i = 0; i < drop_depth; ++i) { |
501 | B->Pop(); |
502 | } |
503 | } |
504 | |
505 | void BytecodeFlowGraphBuilder::BuildInstruction(KernelBytecode::Opcode opcode) { |
506 | switch (opcode) { |
507 | #define WIDE_CASE(name) case KernelBytecode::k##name##_Wide: |
508 | #define WIDE_CASE_0(name) |
509 | #define WIDE_CASE_A(name) |
510 | #define WIDE_CASE_D(name) WIDE_CASE(name) |
511 | #define WIDE_CASE_X(name) WIDE_CASE(name) |
512 | #define WIDE_CASE_T(name) WIDE_CASE(name) |
513 | #define WIDE_CASE_A_E(name) WIDE_CASE(name) |
514 | #define WIDE_CASE_A_Y(name) WIDE_CASE(name) |
515 | #define WIDE_CASE_D_F(name) WIDE_CASE(name) |
516 | #define WIDE_CASE_A_B_C(name) |
517 | |
518 | #define BUILD_BYTECODE_CASE(name, encoding, kind, op1, op2, op3) \ |
519 | BUILD_BYTECODE_CASE_##kind(name, encoding) |
520 | |
521 | #define BUILD_BYTECODE_CASE_WIDE(name, encoding) |
522 | #define BUILD_BYTECODE_CASE_RESV(name, encoding) |
523 | #define BUILD_BYTECODE_CASE_ORDN(name, encoding) \ |
524 | case KernelBytecode::k##name: \ |
525 | WIDE_CASE_##encoding(name) Build##name(); \ |
526 | break; |
527 | |
528 | PUBLIC_KERNEL_BYTECODES_LIST(BUILD_BYTECODE_CASE) |
529 | |
530 | #undef WIDE_CASE |
531 | #undef WIDE_CASE_0 |
532 | #undef WIDE_CASE_A |
533 | #undef WIDE_CASE_D |
534 | #undef WIDE_CASE_X |
535 | #undef WIDE_CASE_T |
536 | #undef WIDE_CASE_A_E |
537 | #undef WIDE_CASE_A_Y |
538 | #undef WIDE_CASE_D_F |
539 | #undef WIDE_CASE_A_B_C |
540 | #undef BUILD_BYTECODE_CASE |
541 | #undef BUILD_BYTECODE_CASE_WIDE |
542 | #undef BUILD_BYTECODE_CASE_RESV |
543 | #undef BUILD_BYTECODE_CASE_ORDN |
544 | |
545 | default: |
546 | FATAL1("Unsupported bytecode instruction %s\n" , |
547 | KernelBytecode::NameOf(opcode)); |
548 | } |
549 | } |
550 | |
551 | void BytecodeFlowGraphBuilder::BuildEntry() { |
552 | AllocateLocalVariables(DecodeOperandD()); |
553 | AllocateFixedParameters(); |
554 | } |
555 | |
556 | void BytecodeFlowGraphBuilder::BuildEntryFixed() { |
557 | if (is_generating_interpreter()) { |
558 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
559 | } |
560 | |
561 | const intptr_t num_fixed_params = DecodeOperandA().value(); |
562 | ASSERT(num_fixed_params == function().num_fixed_parameters()); |
563 | |
564 | AllocateLocalVariables(DecodeOperandE()); |
565 | AllocateFixedParameters(); |
566 | |
567 | Fragment check_args; |
568 | |
569 | ASSERT(throw_no_such_method_ == nullptr); |
570 | throw_no_such_method_ = B->BuildThrowNoSuchMethod(); |
571 | |
572 | check_args += B->LoadArgDescriptor(); |
573 | check_args += |
574 | B->LoadNativeField(Slot::ArgumentsDescriptor_positional_count()); |
575 | check_args += B->IntConstant(num_fixed_params); |
576 | TargetEntryInstr *success1, *fail1; |
577 | check_args += B->BranchIfEqual(&success1, &fail1); |
578 | check_args = Fragment(check_args.entry, success1); |
579 | |
580 | check_args += B->LoadArgDescriptor(); |
581 | check_args += B->LoadNativeField(Slot::ArgumentsDescriptor_count()); |
582 | check_args += B->IntConstant(num_fixed_params); |
583 | TargetEntryInstr *success2, *fail2; |
584 | check_args += B->BranchIfEqual(&success2, &fail2); |
585 | check_args = Fragment(check_args.entry, success2); |
586 | |
587 | Fragment(fail1) + B->Goto(throw_no_such_method_); |
588 | Fragment(fail2) + B->Goto(throw_no_such_method_); |
589 | |
590 | ASSERT(IsStackEmpty()); |
591 | |
592 | if (!B->IsInlining() && !B->IsCompiledForOsr()) { |
593 | code_ += check_args; |
594 | } |
595 | } |
596 | |
597 | void BytecodeFlowGraphBuilder::BuildEntryOptional() { |
598 | if (is_generating_interpreter()) { |
599 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
600 | } |
601 | |
602 | const KBCInstr* next_instr = AllocateParametersAndLocalsForEntryOptional(); |
603 | |
604 | LocalVariable* temp_var = nullptr; |
605 | if (function().HasOptionalNamedParameters()) { |
606 | ASSERT(scratch_var_ != nullptr); |
607 | temp_var = scratch_var_; |
608 | } |
609 | |
610 | Fragment copy_args_prologue; |
611 | |
612 | // Code generated for EntryOptional is considered a prologue code. |
613 | // Prologue should span a range of block ids, so start a new block at the |
614 | // beginning and end a block at the end. |
615 | JoinEntryInstr* prologue_entry = B->BuildJoinEntry(); |
616 | copy_args_prologue += B->Goto(prologue_entry); |
617 | copy_args_prologue = Fragment(copy_args_prologue.entry, prologue_entry); |
618 | |
619 | ASSERT(throw_no_such_method_ == nullptr); |
620 | throw_no_such_method_ = B->BuildThrowNoSuchMethod(); |
621 | |
622 | PrologueBuilder prologue_builder(parsed_function(), B->last_used_block_id_, |
623 | B->IsCompiledForOsr(), B->IsInlining()); |
624 | |
625 | copy_args_prologue += prologue_builder.BuildOptionalParameterHandling( |
626 | throw_no_such_method_, temp_var); |
627 | |
628 | B->last_used_block_id_ = prologue_builder.last_used_block_id(); |
629 | |
630 | JoinEntryInstr* prologue_exit = B->BuildJoinEntry(); |
631 | copy_args_prologue += B->Goto(prologue_exit); |
632 | copy_args_prologue.current = prologue_exit; |
633 | |
634 | if (!B->IsInlining() && !B->IsCompiledForOsr()) { |
635 | code_ += copy_args_prologue; |
636 | } |
637 | |
638 | prologue_info_ = |
639 | PrologueInfo(prologue_entry->block_id(), prologue_exit->block_id() - 1); |
640 | |
641 | // Skip LoadConstant and Frame instructions. |
642 | next_pc_ = pc_ + (next_instr - bytecode_instr_); |
643 | |
644 | ASSERT(IsStackEmpty()); |
645 | } |
646 | |
647 | void BytecodeFlowGraphBuilder::BuildLoadConstant() { |
648 | if (is_generating_interpreter()) { |
649 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
650 | } |
651 | |
652 | // Handled in EntryOptional instruction. |
653 | UNREACHABLE(); |
654 | } |
655 | |
656 | void BytecodeFlowGraphBuilder::BuildFrame() { |
657 | if (is_generating_interpreter()) { |
658 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
659 | } |
660 | |
661 | // Handled in EntryOptional instruction. |
662 | UNREACHABLE(); |
663 | } |
664 | |
665 | void BytecodeFlowGraphBuilder::BuildCheckFunctionTypeArgs() { |
666 | if (is_generating_interpreter()) { |
667 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
668 | } |
669 | |
670 | const intptr_t expected_num_type_args = DecodeOperandA().value(); |
671 | LocalVariable* type_args_var = LocalVariableAt(DecodeOperandE().value()); |
672 | |
673 | if (throw_no_such_method_ == nullptr) { |
674 | throw_no_such_method_ = B->BuildThrowNoSuchMethod(); |
675 | } |
676 | |
677 | Fragment setup_type_args; |
678 | JoinEntryInstr* done = B->BuildJoinEntry(); |
679 | |
680 | // Type args are always optional, so length can always be zero. |
681 | // If expect_type_args, a non-zero length must match the declaration length. |
682 | TargetEntryInstr *then, *fail; |
683 | setup_type_args += B->LoadArgDescriptor(); |
684 | setup_type_args += |
685 | B->LoadNativeField(Slot::ArgumentsDescriptor_type_args_len()); |
686 | |
687 | if (expected_num_type_args != 0) { |
688 | JoinEntryInstr* join2 = B->BuildJoinEntry(); |
689 | |
690 | LocalVariable* len = B->MakeTemporary(); |
691 | |
692 | TargetEntryInstr* otherwise; |
693 | setup_type_args += B->LoadLocal(len); |
694 | setup_type_args += B->IntConstant(0); |
695 | setup_type_args += B->BranchIfEqual(&then, &otherwise); |
696 | |
697 | TargetEntryInstr* then2; |
698 | Fragment check_len(otherwise); |
699 | check_len += B->LoadLocal(len); |
700 | check_len += B->IntConstant(expected_num_type_args); |
701 | check_len += B->BranchIfEqual(&then2, &fail); |
702 | |
703 | Fragment null_type_args(then); |
704 | null_type_args += B->NullConstant(); |
705 | null_type_args += B->StoreLocalRaw(TokenPosition::kNoSource, type_args_var); |
706 | null_type_args += B->Drop(); |
707 | null_type_args += B->Goto(join2); |
708 | |
709 | Fragment store_type_args(then2); |
710 | store_type_args += B->LoadArgDescriptor(); |
711 | store_type_args += B->LoadNativeField(Slot::ArgumentsDescriptor_count()); |
712 | store_type_args += B->LoadFpRelativeSlot( |
713 | compiler::target::kWordSize * |
714 | (1 + compiler::target::frame_layout.param_end_from_fp), |
715 | CompileType::CreateNullable(/*is_nullable=*/true, kTypeArgumentsCid)); |
716 | store_type_args += |
717 | B->StoreLocalRaw(TokenPosition::kNoSource, type_args_var); |
718 | store_type_args += B->Drop(); |
719 | store_type_args += B->Goto(join2); |
720 | |
721 | Fragment(join2) + B->Drop() + B->Goto(done); |
722 | Fragment(fail) + B->Goto(throw_no_such_method_); |
723 | } else { |
724 | setup_type_args += B->IntConstant(0); |
725 | setup_type_args += B->BranchIfEqual(&then, &fail); |
726 | Fragment(then) + B->Goto(done); |
727 | Fragment(fail) + B->Goto(throw_no_such_method_); |
728 | } |
729 | |
730 | setup_type_args = Fragment(setup_type_args.entry, done); |
731 | ASSERT(IsStackEmpty()); |
732 | |
733 | if (expected_num_type_args != 0) { |
734 | parsed_function()->set_function_type_arguments(type_args_var); |
735 | parsed_function()->SetRawTypeArgumentsVariable(type_args_var); |
736 | } |
737 | |
738 | if (!B->IsInlining() && !B->IsCompiledForOsr()) { |
739 | code_ += setup_type_args; |
740 | } |
741 | } |
742 | |
743 | void BytecodeFlowGraphBuilder::BuildCheckStack() { |
744 | if (is_generating_interpreter()) { |
745 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
746 | } |
747 | const intptr_t loop_depth = DecodeOperandA().value(); |
748 | if (loop_depth == 0) { |
749 | ASSERT(IsStackEmpty()); |
750 | code_ += B->CheckStackOverflowInPrologue(position_); |
751 | } else { |
752 | const intptr_t stack_depth = B->GetStackDepth(); |
753 | code_ += B->CheckStackOverflow(position_, stack_depth, loop_depth); |
754 | } |
755 | } |
756 | |
757 | void BytecodeFlowGraphBuilder::BuildDebugCheck() { |
758 | if (is_generating_interpreter()) { |
759 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
760 | } |
761 | // DebugStepCheck instructions are emitted for all explicit DebugCheck |
762 | // opcodes as well as for implicit DEBUG_CHECK executed by the interpreter |
763 | // for some opcodes, but not before the first explicit DebugCheck opcode is |
764 | // encountered. |
765 | build_debug_step_checks_ = true; |
766 | BuildDebugStepCheck(); |
767 | } |
768 | |
769 | void BytecodeFlowGraphBuilder::BuildPushConstant() { |
770 | PushConstant(ConstantAt(DecodeOperandD())); |
771 | } |
772 | |
773 | void BytecodeFlowGraphBuilder::BuildPushNull() { |
774 | code_ += B->NullConstant(); |
775 | } |
776 | |
777 | void BytecodeFlowGraphBuilder::BuildPushTrue() { |
778 | code_ += B->Constant(Bool::True()); |
779 | } |
780 | |
781 | void BytecodeFlowGraphBuilder::BuildPushFalse() { |
782 | code_ += B->Constant(Bool::False()); |
783 | } |
784 | |
785 | void BytecodeFlowGraphBuilder::BuildPushInt() { |
786 | if (is_generating_interpreter()) { |
787 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
788 | } |
789 | code_ += B->IntConstant(DecodeOperandX().value()); |
790 | } |
791 | |
792 | void BytecodeFlowGraphBuilder::BuildStoreLocal() { |
793 | LoadStackSlots(1); |
794 | const Operand local_index = DecodeOperandX(); |
795 | StoreLocal(local_index); |
796 | } |
797 | |
798 | void BytecodeFlowGraphBuilder::BuildPopLocal() { |
799 | BuildStoreLocal(); |
800 | code_ += B->Drop(); |
801 | } |
802 | |
803 | void BytecodeFlowGraphBuilder::BuildPush() { |
804 | const Operand local_index = DecodeOperandX(); |
805 | LoadLocal(local_index); |
806 | } |
807 | |
808 | void BytecodeFlowGraphBuilder::BuildDirectCallCommon(bool is_unchecked_call) { |
809 | if (is_generating_interpreter()) { |
810 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
811 | } |
812 | |
813 | // A DebugStepCheck is performed as part of the calling stub. |
814 | |
815 | const Function& target = Function::Cast(ConstantAt(DecodeOperandD()).value()); |
816 | const intptr_t argc = DecodeOperandF().value(); |
817 | |
818 | switch (target.recognized_kind()) { |
819 | case MethodRecognizer::kFfiAsFunctionInternal: |
820 | BuildFfiAsFunction(); |
821 | return; |
822 | case MethodRecognizer::kFfiNativeCallbackFunction: |
823 | if (CompilerState::Current().is_aot()) { |
824 | BuildFfiNativeCallbackFunction(); |
825 | return; |
826 | } |
827 | break; |
828 | case MethodRecognizer::kObjectIdentical: |
829 | // Note: similar optimization is performed in AST flow graph builder - |
830 | // see StreamingFlowGraphBuilder::BuildStaticInvocation, |
831 | // special_case_identical. |
832 | // TODO(alexmarkov): find a better place for this optimization. |
833 | ASSERT(argc == 2); |
834 | code_ += B->StrictCompare(Token::kEQ_STRICT, /*number_check=*/true); |
835 | return; |
836 | case MethodRecognizer::kAsyncStackTraceHelper: |
837 | case MethodRecognizer::kSetAsyncThreadStackTrace: |
838 | if (!FLAG_causal_async_stacks) { |
839 | ASSERT(argc == 1); |
840 | // Drop the ignored parameter to _asyncStackTraceHelper(:async_op) or |
841 | // _setAsyncThreadStackTrace(stackTrace). |
842 | code_ += B->Drop(); |
843 | code_ += B->NullConstant(); |
844 | return; |
845 | } |
846 | break; |
847 | case MethodRecognizer::kClearAsyncThreadStackTrace: |
848 | if (!FLAG_causal_async_stacks) { |
849 | ASSERT(argc == 0); |
850 | code_ += B->NullConstant(); |
851 | return; |
852 | } |
853 | break; |
854 | case MethodRecognizer::kStringBaseInterpolate: |
855 | ASSERT(argc == 1); |
856 | code_ += B->StringInterpolate(position_); |
857 | return; |
858 | default: |
859 | break; |
860 | } |
861 | |
862 | const Array& arg_desc_array = |
863 | Array::Cast(ConstantAt(DecodeOperandD(), 1).value()); |
864 | const ArgumentsDescriptor arg_desc(arg_desc_array); |
865 | |
866 | InputsArray* arguments = B->GetArguments(argc); |
867 | |
868 | StaticCallInstr* call = new (Z) StaticCallInstr( |
869 | position_, target, arg_desc.TypeArgsLen(), |
870 | Array::ZoneHandle(Z, arg_desc.GetArgumentNames()), arguments, |
871 | *ic_data_array_, B->GetNextDeoptId(), |
872 | target.IsDynamicFunction() ? ICData::kSuper : ICData::kStatic); |
873 | |
874 | if (is_unchecked_call) { |
875 | call->set_entry_kind(Code::EntryKind::kUnchecked); |
876 | } |
877 | |
878 | if (!call->InitResultType(Z)) { |
879 | if (!inferred_types_attribute_.IsNull()) { |
880 | const InferredTypeMetadata result_type = GetInferredType(pc_); |
881 | if (!result_type.IsTrivial()) { |
882 | call->SetResultType(Z, result_type.ToCompileType(Z)); |
883 | } |
884 | } |
885 | } |
886 | |
887 | code_ <<= call; |
888 | B->Push(call); |
889 | } |
890 | |
891 | void BytecodeFlowGraphBuilder::BuildDirectCall() { |
892 | BuildDirectCallCommon(/* is_unchecked_call = */ false); |
893 | } |
894 | |
895 | void BytecodeFlowGraphBuilder::BuildUncheckedDirectCall() { |
896 | BuildDirectCallCommon(/* is_unchecked_call = */ true); |
897 | } |
898 | |
899 | static void ComputeTokenKindAndCheckedArguments( |
900 | const String& name, |
901 | const ArgumentsDescriptor& arg_desc, |
902 | Token::Kind* token_kind, |
903 | intptr_t* checked_argument_count) { |
904 | *token_kind = MethodTokenRecognizer::RecognizeTokenKind(name); |
905 | |
906 | *checked_argument_count = 1; |
907 | if (*token_kind != Token::kILLEGAL) { |
908 | intptr_t argument_count = arg_desc.Count(); |
909 | ASSERT(argument_count <= 2); |
910 | *checked_argument_count = (*token_kind == Token::kSET) ? 1 : argument_count; |
911 | } else if (Library::IsPrivateCoreLibName(name, |
912 | Symbols::_simpleInstanceOf())) { |
913 | ASSERT(arg_desc.Count() == 2); |
914 | *checked_argument_count = 2; |
915 | *token_kind = Token::kIS; |
916 | } else if (Library::IsPrivateCoreLibName(name, Symbols::_instanceOf())) { |
917 | ASSERT(arg_desc.Count() == 4); |
918 | *token_kind = Token::kIS; |
919 | } |
920 | } |
921 | |
922 | void BytecodeFlowGraphBuilder::BuildInterfaceCallCommon( |
923 | bool is_unchecked_call, |
924 | bool is_instantiated_call) { |
925 | if (is_generating_interpreter()) { |
926 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
927 | } |
928 | |
929 | // A DebugStepCheck is performed as part of the calling stub. |
930 | |
931 | const Function& interface_target = |
932 | Function::Cast(ConstantAt(DecodeOperandD()).value()); |
933 | const String& name = String::ZoneHandle(Z, interface_target.name()); |
934 | ASSERT(name.IsSymbol()); |
935 | |
936 | const Array& arg_desc_array = |
937 | Array::Cast(ConstantAt(DecodeOperandD(), 1).value()); |
938 | const ArgumentsDescriptor arg_desc(arg_desc_array); |
939 | |
940 | Token::Kind token_kind; |
941 | intptr_t checked_argument_count; |
942 | ComputeTokenKindAndCheckedArguments(name, arg_desc, &token_kind, |
943 | &checked_argument_count); |
944 | |
945 | const intptr_t argc = DecodeOperandF().value(); |
946 | InputsArray* arguments = B->GetArguments(argc); |
947 | |
948 | InstanceCallInstr* call = new (Z) InstanceCallInstr( |
949 | position_, name, token_kind, arguments, arg_desc.TypeArgsLen(), |
950 | Array::ZoneHandle(Z, arg_desc.GetArgumentNames()), checked_argument_count, |
951 | *ic_data_array_, B->GetNextDeoptId(), interface_target); |
952 | |
953 | if (!inferred_types_attribute_.IsNull()) { |
954 | const InferredTypeMetadata result_type = GetInferredType(pc_); |
955 | if (!result_type.IsTrivial()) { |
956 | call->SetResultType(Z, result_type.ToCompileType(Z)); |
957 | } |
958 | } |
959 | |
960 | if (is_unchecked_call) { |
961 | call->set_entry_kind(Code::EntryKind::kUnchecked); |
962 | } |
963 | |
964 | if (is_instantiated_call) { |
965 | const AbstractType& static_receiver_type = |
966 | AbstractType::Cast(ConstantAt(DecodeOperandD(), 2).value()); |
967 | call->set_receivers_static_type(&static_receiver_type); |
968 | } else { |
969 | const Class& owner = Class::Handle(Z, interface_target.Owner()); |
970 | const AbstractType& type = |
971 | AbstractType::ZoneHandle(Z, owner.DeclarationType()); |
972 | call->set_receivers_static_type(&type); |
973 | } |
974 | |
975 | code_ <<= call; |
976 | B->Push(call); |
977 | } |
978 | |
979 | void BytecodeFlowGraphBuilder::BuildInterfaceCall() { |
980 | BuildInterfaceCallCommon(/*is_unchecked_call=*/false, |
981 | /*is_instantiated_call=*/false); |
982 | } |
983 | |
984 | void BytecodeFlowGraphBuilder::BuildInstantiatedInterfaceCall() { |
985 | BuildInterfaceCallCommon(/*is_unchecked_call=*/false, |
986 | /*is_instantiated_call=*/true); |
987 | } |
988 | |
989 | void BytecodeFlowGraphBuilder::BuildUncheckedInterfaceCall() { |
990 | BuildInterfaceCallCommon(/*is_unchecked_call=*/true, |
991 | /*is_instantiated_call=*/false); |
992 | } |
993 | |
994 | void BytecodeFlowGraphBuilder::BuildUncheckedClosureCall() { |
995 | if (is_generating_interpreter()) { |
996 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
997 | } |
998 | |
999 | BuildDebugStepCheck(); |
1000 | |
1001 | const Array& arg_desc_array = |
1002 | Array::Cast(ConstantAt(DecodeOperandD()).value()); |
1003 | const ArgumentsDescriptor arg_desc(arg_desc_array); |
1004 | |
1005 | const intptr_t argc = DecodeOperandF().value(); |
1006 | |
1007 | LocalVariable* receiver_temp = B->MakeTemporary(); |
1008 | code_ += B->CheckNull(position_, receiver_temp, Symbols::Call(), |
1009 | /*clear_temp=*/false); |
1010 | |
1011 | code_ += B->LoadNativeField(Slot::Closure_function()); |
1012 | |
1013 | InputsArray* arguments = B->GetArguments(argc + 1); |
1014 | |
1015 | ClosureCallInstr* call = new (Z) ClosureCallInstr( |
1016 | arguments, arg_desc.TypeArgsLen(), |
1017 | Array::ZoneHandle(Z, arg_desc.GetArgumentNames()), position_, |
1018 | B->GetNextDeoptId(), Code::EntryKind::kUnchecked); |
1019 | |
1020 | // TODO(alexmarkov): use inferred result type for ClosureCallInstr |
1021 | // if (!inferred_types_attribute_.IsNull()) { |
1022 | // const InferredTypeMetadata result_type = GetInferredType(pc_); |
1023 | // if (!result_type.IsTrivial()) { |
1024 | // call->SetResultType(Z, result_type.ToCompileType(Z)); |
1025 | // } |
1026 | // } |
1027 | |
1028 | code_ <<= call; |
1029 | B->Push(call); |
1030 | } |
1031 | |
1032 | void BytecodeFlowGraphBuilder::BuildDynamicCall() { |
1033 | if (is_generating_interpreter()) { |
1034 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1035 | } |
1036 | |
1037 | // A DebugStepCheck is performed as part of the calling stub. |
1038 | |
1039 | const String& name = String::Cast(ConstantAt(DecodeOperandD()).value()); |
1040 | const ArgumentsDescriptor arg_desc( |
1041 | Array::Cast(ConstantAt(DecodeOperandD(), 1).value())); |
1042 | |
1043 | Token::Kind token_kind; |
1044 | intptr_t checked_argument_count; |
1045 | ComputeTokenKindAndCheckedArguments(name, arg_desc, &token_kind, |
1046 | &checked_argument_count); |
1047 | |
1048 | const intptr_t argc = DecodeOperandF().value(); |
1049 | InputsArray* arguments = B->GetArguments(argc); |
1050 | |
1051 | const Function& interface_target = Function::null_function(); |
1052 | |
1053 | InstanceCallInstr* call = new (Z) InstanceCallInstr( |
1054 | position_, name, token_kind, arguments, arg_desc.TypeArgsLen(), |
1055 | Array::ZoneHandle(Z, arg_desc.GetArgumentNames()), checked_argument_count, |
1056 | *ic_data_array_, B->GetNextDeoptId(), interface_target); |
1057 | |
1058 | if (!inferred_types_attribute_.IsNull()) { |
1059 | const InferredTypeMetadata result_type = GetInferredType(pc_); |
1060 | if (!result_type.IsTrivial()) { |
1061 | call->SetResultType(Z, result_type.ToCompileType(Z)); |
1062 | } |
1063 | } |
1064 | |
1065 | code_ <<= call; |
1066 | B->Push(call); |
1067 | } |
1068 | |
1069 | void BytecodeFlowGraphBuilder::BuildNativeCall() { |
1070 | if (is_generating_interpreter()) { |
1071 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1072 | } |
1073 | |
1074 | ASSERT(function().is_native()); |
1075 | B->InlineBailout("BytecodeFlowGraphBuilder::BuildNativeCall" ); |
1076 | |
1077 | const auto& name = String::ZoneHandle(Z, function().native_name()); |
1078 | const intptr_t num_args = |
1079 | function().NumParameters() + (function().IsGeneric() ? 1 : 0); |
1080 | InputsArray* arguments = B->GetArguments(num_args); |
1081 | auto* call = |
1082 | new (Z) NativeCallInstr(&name, &function(), FLAG_link_natives_lazily, |
1083 | function().end_token_pos(), arguments); |
1084 | code_ <<= call; |
1085 | B->Push(call); |
1086 | } |
1087 | |
1088 | void BytecodeFlowGraphBuilder::BuildAllocate() { |
1089 | if (is_generating_interpreter()) { |
1090 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1091 | } |
1092 | |
1093 | const Class& klass = Class::Cast(ConstantAt(DecodeOperandD()).value()); |
1094 | |
1095 | AllocateObjectInstr* allocate = new (Z) AllocateObjectInstr(position_, klass); |
1096 | |
1097 | code_ <<= allocate; |
1098 | B->Push(allocate); |
1099 | } |
1100 | |
1101 | void BytecodeFlowGraphBuilder::BuildAllocateT() { |
1102 | if (is_generating_interpreter()) { |
1103 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1104 | } |
1105 | |
1106 | const Class& klass = Class::Cast(PopConstant().value()); |
1107 | Value* type_arguments = Pop(); |
1108 | |
1109 | AllocateObjectInstr* allocate = |
1110 | new (Z) AllocateObjectInstr(position_, klass, type_arguments); |
1111 | |
1112 | code_ <<= allocate; |
1113 | B->Push(allocate); |
1114 | } |
1115 | |
1116 | void BytecodeFlowGraphBuilder::BuildAllocateContext() { |
1117 | if (is_generating_interpreter()) { |
1118 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1119 | } |
1120 | |
1121 | const intptr_t context_id = DecodeOperandA().value(); |
1122 | const intptr_t num_context_vars = DecodeOperandE().value(); |
1123 | |
1124 | auto& context_slots = CompilerState::Current().GetDummyContextSlots( |
1125 | context_id, num_context_vars); |
1126 | code_ += B->AllocateContext(context_slots); |
1127 | } |
1128 | |
1129 | void BytecodeFlowGraphBuilder::BuildCloneContext() { |
1130 | if (is_generating_interpreter()) { |
1131 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1132 | } |
1133 | |
1134 | LoadStackSlots(1); |
1135 | const intptr_t context_id = DecodeOperandA().value(); |
1136 | const intptr_t num_context_vars = DecodeOperandE().value(); |
1137 | |
1138 | auto& context_slots = CompilerState::Current().GetDummyContextSlots( |
1139 | context_id, num_context_vars); |
1140 | CloneContextInstr* clone_instruction = new (Z) CloneContextInstr( |
1141 | TokenPosition::kNoSource, Pop(), context_slots, B->GetNextDeoptId()); |
1142 | code_ <<= clone_instruction; |
1143 | B->Push(clone_instruction); |
1144 | } |
1145 | |
1146 | void BytecodeFlowGraphBuilder::BuildCreateArrayTOS() { |
1147 | LoadStackSlots(2); |
1148 | code_ += B->CreateArray(); |
1149 | } |
1150 | |
1151 | const Slot& ClosureSlotByField(const Field& field) { |
1152 | const intptr_t offset = field.HostOffset(); |
1153 | if (offset == Closure::instantiator_type_arguments_offset()) { |
1154 | return Slot::Closure_instantiator_type_arguments(); |
1155 | } else if (offset == Closure::function_type_arguments_offset()) { |
1156 | return Slot::Closure_function_type_arguments(); |
1157 | } else if (offset == Closure::delayed_type_arguments_offset()) { |
1158 | return Slot::Closure_delayed_type_arguments(); |
1159 | } else if (offset == Closure::function_offset()) { |
1160 | return Slot::Closure_function(); |
1161 | } else if (offset == Closure::context_offset()) { |
1162 | return Slot::Closure_context(); |
1163 | } else { |
1164 | RELEASE_ASSERT(offset == Closure::hash_offset()); |
1165 | return Slot::Closure_hash(); |
1166 | } |
1167 | } |
1168 | |
1169 | void BytecodeFlowGraphBuilder::BuildStoreFieldTOS() { |
1170 | if (is_generating_interpreter()) { |
1171 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1172 | } |
1173 | |
1174 | LoadStackSlots(2); |
1175 | Operand cp_index = DecodeOperandD(); |
1176 | |
1177 | const Field& field = Field::Cast(ConstantAt(cp_index, 1).value()); |
1178 | ASSERT(Smi::Cast(ConstantAt(cp_index).value()).Value() * kWordSize == |
1179 | field.HostOffset()); |
1180 | |
1181 | if (field.Owner() == isolate()->object_store()->closure_class()) { |
1182 | // Stores to _Closure fields are lower-level. |
1183 | code_ += |
1184 | B->StoreInstanceField(position_, ClosureSlotByField(field), |
1185 | StoreInstanceFieldInstr::Kind::kInitializing); |
1186 | } else { |
1187 | // The rest of the StoreFieldTOS are for field initializers. |
1188 | // TODO(alexmarkov): Consider adding a flag to StoreFieldTOS or even |
1189 | // adding a separate bytecode instruction. |
1190 | code_ += B->StoreInstanceFieldGuarded( |
1191 | field, StoreInstanceFieldInstr::Kind::kInitializing); |
1192 | } |
1193 | } |
1194 | |
1195 | void BytecodeFlowGraphBuilder::BuildLoadFieldTOS() { |
1196 | if (is_generating_interpreter()) { |
1197 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1198 | } |
1199 | |
1200 | LoadStackSlots(1); |
1201 | Operand cp_index = DecodeOperandD(); |
1202 | |
1203 | const Field& field = Field::Cast(ConstantAt(cp_index, 1).value()); |
1204 | ASSERT(Smi::Cast(ConstantAt(cp_index).value()).Value() * kWordSize == |
1205 | field.HostOffset()); |
1206 | |
1207 | if (field.Owner() == isolate()->object_store()->closure_class()) { |
1208 | // Loads from _Closure fields are lower-level. |
1209 | code_ += B->LoadNativeField(ClosureSlotByField(field)); |
1210 | } else { |
1211 | code_ += B->LoadField(field, /*calls_initializer=*/false); |
1212 | } |
1213 | } |
1214 | |
1215 | void BytecodeFlowGraphBuilder::BuildStoreContextParent() { |
1216 | LoadStackSlots(2); |
1217 | |
1218 | code_ += B->StoreInstanceField(position_, Slot::Context_parent(), |
1219 | StoreInstanceFieldInstr::Kind::kInitializing); |
1220 | } |
1221 | |
1222 | void BytecodeFlowGraphBuilder::BuildLoadContextParent() { |
1223 | LoadStackSlots(1); |
1224 | |
1225 | code_ += B->LoadNativeField(Slot::Context_parent()); |
1226 | } |
1227 | |
1228 | void BytecodeFlowGraphBuilder::BuildStoreContextVar() { |
1229 | if (is_generating_interpreter()) { |
1230 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1231 | } |
1232 | |
1233 | LoadStackSlots(2); |
1234 | const intptr_t context_id = DecodeOperandA().value(); |
1235 | const intptr_t var_index = DecodeOperandE().value(); |
1236 | |
1237 | auto var = |
1238 | CompilerState::Current().GetDummyCapturedVariable(context_id, var_index); |
1239 | code_ += B->StoreInstanceField( |
1240 | position_, Slot::GetContextVariableSlotFor(thread(), *var)); |
1241 | } |
1242 | |
1243 | void BytecodeFlowGraphBuilder::BuildLoadContextVar() { |
1244 | if (is_generating_interpreter()) { |
1245 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1246 | } |
1247 | |
1248 | LoadStackSlots(1); |
1249 | const intptr_t context_id = DecodeOperandA().value(); |
1250 | const intptr_t var_index = DecodeOperandE().value(); |
1251 | |
1252 | auto var = |
1253 | CompilerState::Current().GetDummyCapturedVariable(context_id, var_index); |
1254 | code_ += B->LoadNativeField(Slot::GetContextVariableSlotFor(thread(), *var)); |
1255 | } |
1256 | |
1257 | void BytecodeFlowGraphBuilder::BuildLoadTypeArgumentsField() { |
1258 | if (is_generating_interpreter()) { |
1259 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1260 | } |
1261 | |
1262 | LoadStackSlots(1); |
1263 | const intptr_t offset = |
1264 | Smi::Cast(ConstantAt(DecodeOperandD()).value()).Value() * |
1265 | compiler::target::kWordSize; |
1266 | |
1267 | code_ += B->LoadNativeField(Slot::GetTypeArgumentsSlotAt(thread(), offset)); |
1268 | } |
1269 | |
1270 | void BytecodeFlowGraphBuilder::BuildStoreStaticTOS() { |
1271 | if (is_generating_interpreter()) { |
1272 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1273 | } |
1274 | |
1275 | LoadStackSlots(1); |
1276 | Operand cp_index = DecodeOperandD(); |
1277 | |
1278 | const Field& field = Field::Cast(ConstantAt(cp_index).value()); |
1279 | |
1280 | code_ += B->StoreStaticField(position_, field); |
1281 | } |
1282 | |
1283 | void BytecodeFlowGraphBuilder::BuildInitLateField() { |
1284 | if (is_generating_interpreter()) { |
1285 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1286 | } |
1287 | |
1288 | LoadStackSlots(1); |
1289 | Operand cp_index = DecodeOperandD(); |
1290 | |
1291 | const Field& field = Field::Cast(ConstantAt(cp_index, 1).value()); |
1292 | ASSERT(Smi::Cast(ConstantAt(cp_index).value()).Value() * kWordSize == |
1293 | field.HostOffset()); |
1294 | |
1295 | code_ += B->Constant(Object::sentinel()); |
1296 | code_ += B->StoreInstanceField( |
1297 | field, StoreInstanceFieldInstr::Kind::kInitializing, kNoStoreBarrier); |
1298 | } |
1299 | |
1300 | void BytecodeFlowGraphBuilder::BuildPushUninitializedSentinel() { |
1301 | code_ += B->Constant(Object::sentinel()); |
1302 | } |
1303 | |
1304 | void BytecodeFlowGraphBuilder::BuildJumpIfInitialized() { |
1305 | code_ += B->Constant(Object::sentinel()); |
1306 | BuildJumpIfStrictCompare(Token::kNE); |
1307 | } |
1308 | |
1309 | void BytecodeFlowGraphBuilder::BuildLoadStatic() { |
1310 | const Constant operand = ConstantAt(DecodeOperandD()); |
1311 | const auto& field = Field::Cast(operand.value()); |
1312 | // All constant expressions (including access to const fields) are evaluated |
1313 | // in bytecode. However, values of injected cid fields are only available in |
1314 | // the VM. In such case, evaluate const fields with known value here. |
1315 | if (field.is_const() && !field.has_nontrivial_initializer()) { |
1316 | const auto& value = Object::ZoneHandle(Z, field.StaticValue()); |
1317 | ASSERT((value.raw() != Object::sentinel().raw()) && |
1318 | (value.raw() != Object::transition_sentinel().raw())); |
1319 | code_ += B->Constant(value); |
1320 | return; |
1321 | } |
1322 | code_ += B->LoadStaticField(field, /*calls_initializer=*/false); |
1323 | } |
1324 | |
1325 | void BytecodeFlowGraphBuilder::BuildStoreIndexedTOS() { |
1326 | LoadStackSlots(3); |
1327 | code_ += B->StoreIndexed(kArrayCid); |
1328 | } |
1329 | |
1330 | void BytecodeFlowGraphBuilder::BuildBooleanNegateTOS() { |
1331 | LoadStackSlots(1); |
1332 | code_ += B->BooleanNegate(); |
1333 | } |
1334 | |
1335 | void BytecodeFlowGraphBuilder::BuildInstantiateType() { |
1336 | if (is_generating_interpreter()) { |
1337 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1338 | } |
1339 | |
1340 | const AbstractType& type = |
1341 | AbstractType::Cast(ConstantAt(DecodeOperandD()).value()); |
1342 | |
1343 | LoadStackSlots(2); |
1344 | code_ += B->InstantiateType(type); |
1345 | } |
1346 | |
1347 | void BytecodeFlowGraphBuilder::BuildInstantiateTypeArgumentsTOS() { |
1348 | if (is_generating_interpreter()) { |
1349 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1350 | } |
1351 | |
1352 | const TypeArguments& type_args = |
1353 | TypeArguments::Cast(ConstantAt(DecodeOperandE()).value()); |
1354 | |
1355 | LoadStackSlots(2); |
1356 | code_ += B->InstantiateTypeArguments(type_args); |
1357 | } |
1358 | |
1359 | void BytecodeFlowGraphBuilder::BuildAssertBoolean() { |
1360 | LoadStackSlots(1); |
1361 | code_ += B->AssertBool(position_); |
1362 | } |
1363 | |
1364 | void BytecodeFlowGraphBuilder::BuildAssertAssignable() { |
1365 | if (is_generating_interpreter()) { |
1366 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1367 | } |
1368 | |
1369 | LoadStackSlots(5); |
1370 | |
1371 | const AbstractType& dst_type = |
1372 | AbstractType::Cast(B->Peek(/*depth=*/3)->AsConstant()->value()); |
1373 | if (dst_type.IsTopTypeForSubtyping()) { |
1374 | code_ += B->Drop(); // dst_name |
1375 | code_ += B->Drop(); // function_type_args |
1376 | code_ += B->Drop(); // instantiator_type_args |
1377 | code_ += B->Drop(); // dst_type |
1378 | // Leave value on top. |
1379 | return; |
1380 | } |
1381 | |
1382 | const String& dst_name = String::Cast(PopConstant().value()); |
1383 | Value* function_type_args = Pop(); |
1384 | Value* instantiator_type_args = Pop(); |
1385 | Value* dst_type_value = Pop(); |
1386 | Value* value = Pop(); |
1387 | |
1388 | AssertAssignableInstr* instr = new (Z) AssertAssignableInstr( |
1389 | position_, value, dst_type_value, instantiator_type_args, |
1390 | function_type_args, dst_name, B->GetNextDeoptId()); |
1391 | |
1392 | code_ <<= instr; |
1393 | |
1394 | B->Push(instr); |
1395 | } |
1396 | |
1397 | void BytecodeFlowGraphBuilder::BuildAssertSubtype() { |
1398 | if (is_generating_interpreter()) { |
1399 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1400 | } |
1401 | |
1402 | LoadStackSlots(5); |
1403 | |
1404 | const String& dst_name = String::Cast(PopConstant().value()); |
1405 | Value* super_type = Pop(); |
1406 | Value* sub_type = Pop(); |
1407 | Value* function_type_args = Pop(); |
1408 | Value* instantiator_type_args = Pop(); |
1409 | |
1410 | AssertSubtypeInstr* instr = new (Z) |
1411 | AssertSubtypeInstr(position_, instantiator_type_args, function_type_args, |
1412 | sub_type, super_type, dst_name, B->GetNextDeoptId()); |
1413 | code_ <<= instr; |
1414 | } |
1415 | |
1416 | void BytecodeFlowGraphBuilder::BuildNullCheck() { |
1417 | if (is_generating_interpreter()) { |
1418 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1419 | } |
1420 | |
1421 | const String& selector = |
1422 | String::CheckedZoneHandle(Z, ConstantAt(DecodeOperandD()).value().raw()); |
1423 | |
1424 | LocalVariable* receiver_temp = B->MakeTemporary(); |
1425 | code_ += |
1426 | B->CheckNull(position_, receiver_temp, selector, /*clear_temp=*/false); |
1427 | code_ += B->Drop(); |
1428 | } |
1429 | |
1430 | void BytecodeFlowGraphBuilder::BuildJump() { |
1431 | if (is_generating_interpreter()) { |
1432 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1433 | } |
1434 | |
1435 | const intptr_t target_pc = pc_ + DecodeOperandT().value(); |
1436 | JoinEntryInstr* join = jump_targets_.Lookup(target_pc); |
1437 | ASSERT(join != nullptr); |
1438 | code_ += B->Goto(join); |
1439 | PropagateStackState(target_pc); |
1440 | B->stack_ = nullptr; |
1441 | } |
1442 | |
1443 | void BytecodeFlowGraphBuilder::BuildJumpIfNoAsserts() { |
1444 | ASSERT(IsStackEmpty()); |
1445 | if (!isolate()->asserts()) { |
1446 | BuildJump(); |
1447 | // Skip all instructions up to the target PC, as they are all unreachable. |
1448 | // If not skipped, some of the assert code may be considered reachable |
1449 | // (if it contains jumps) and generated. The problem is that generated |
1450 | // code may expect values left on the stack from unreachable |
1451 | // (and not generated) code which immediately follows this Jump. |
1452 | next_pc_ = pc_ + DecodeOperandT().value(); |
1453 | ASSERT(next_pc_ > pc_); |
1454 | } |
1455 | } |
1456 | |
1457 | void BytecodeFlowGraphBuilder::BuildJumpIfNotZeroTypeArgs() { |
1458 | if (is_generating_interpreter()) { |
1459 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1460 | } |
1461 | |
1462 | TargetEntryInstr *is_zero, *is_not_zero; |
1463 | code_ += B->LoadArgDescriptor(); |
1464 | code_ += B->LoadNativeField(Slot::ArgumentsDescriptor_type_args_len()); |
1465 | code_ += B->IntConstant(0); |
1466 | code_ += B->BranchIfEqual(&is_zero, &is_not_zero); |
1467 | |
1468 | const intptr_t target_pc = pc_ + DecodeOperandT().value(); |
1469 | JoinEntryInstr* join = jump_targets_.Lookup(target_pc); |
1470 | ASSERT(join != nullptr); |
1471 | Fragment(is_not_zero) += B->Goto(join); |
1472 | PropagateStackState(target_pc); |
1473 | |
1474 | code_ = Fragment(code_.entry, is_zero); |
1475 | } |
1476 | |
1477 | void BytecodeFlowGraphBuilder::BuildJumpIfStrictCompare(Token::Kind cmp_kind) { |
1478 | ASSERT((cmp_kind == Token::kEQ) || (cmp_kind == Token::kNE)); |
1479 | |
1480 | if (is_generating_interpreter()) { |
1481 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1482 | } |
1483 | |
1484 | LoadStackSlots(2); |
1485 | |
1486 | // Fallthrough should correspond to 'then' branch target. |
1487 | // This results in a slightly better regalloc. |
1488 | TargetEntryInstr* then_entry = nullptr; |
1489 | TargetEntryInstr* else_entry = nullptr; |
1490 | code_ += B->BranchIfEqual(&then_entry, &else_entry, |
1491 | /* negate = */ (cmp_kind == Token::kEQ)); |
1492 | |
1493 | const intptr_t target_pc = pc_ + DecodeOperandT().value(); |
1494 | JoinEntryInstr* join = jump_targets_.Lookup(target_pc); |
1495 | ASSERT(join != nullptr); |
1496 | |
1497 | code_ = Fragment(else_entry); |
1498 | code_ += B->Goto(join); |
1499 | PropagateStackState(target_pc); |
1500 | |
1501 | code_ = Fragment(then_entry); |
1502 | } |
1503 | |
1504 | void BytecodeFlowGraphBuilder::BuildJumpIfEqStrict() { |
1505 | BuildJumpIfStrictCompare(Token::kEQ); |
1506 | } |
1507 | |
1508 | void BytecodeFlowGraphBuilder::BuildJumpIfNeStrict() { |
1509 | BuildJumpIfStrictCompare(Token::kNE); |
1510 | } |
1511 | |
1512 | void BytecodeFlowGraphBuilder::BuildJumpIfTrue() { |
1513 | code_ += B->Constant(Bool::True()); |
1514 | BuildJumpIfStrictCompare(Token::kEQ); |
1515 | } |
1516 | |
1517 | void BytecodeFlowGraphBuilder::BuildJumpIfFalse() { |
1518 | code_ += B->Constant(Bool::False()); |
1519 | BuildJumpIfStrictCompare(Token::kEQ); |
1520 | } |
1521 | |
1522 | void BytecodeFlowGraphBuilder::BuildJumpIfNull() { |
1523 | code_ += B->NullConstant(); |
1524 | BuildJumpIfStrictCompare(Token::kEQ); |
1525 | } |
1526 | |
1527 | void BytecodeFlowGraphBuilder::BuildJumpIfNotNull() { |
1528 | code_ += B->NullConstant(); |
1529 | BuildJumpIfStrictCompare(Token::kNE); |
1530 | } |
1531 | |
1532 | void BytecodeFlowGraphBuilder::BuildJumpIfUnchecked() { |
1533 | if (is_generating_interpreter()) { |
1534 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1535 | } |
1536 | |
1537 | ASSERT(IsStackEmpty()); |
1538 | |
1539 | const intptr_t target_pc = pc_ + DecodeOperandT().value(); |
1540 | JoinEntryInstr* target = jump_targets_.Lookup(target_pc); |
1541 | ASSERT(target != nullptr); |
1542 | FunctionEntryInstr* unchecked_entry = nullptr; |
1543 | const intptr_t kCheckedEntry = |
1544 | static_cast<intptr_t>(UncheckedEntryPointStyle::kNone); |
1545 | const intptr_t kUncheckedEntry = |
1546 | static_cast<intptr_t>(UncheckedEntryPointStyle::kSharedWithVariable); |
1547 | |
1548 | switch (entry_point_style_) { |
1549 | case UncheckedEntryPointStyle::kNone: { |
1550 | JoinEntryInstr* do_checks = B->BuildJoinEntry(); |
1551 | code_ += B->Goto(B->InliningUncheckedEntry() ? target : do_checks); |
1552 | code_ = Fragment(do_checks); |
1553 | } break; |
1554 | |
1555 | case UncheckedEntryPointStyle::kSeparate: { |
1556 | // Route normal entry to checks. |
1557 | if (FLAG_enable_testing_pragmas) { |
1558 | code_ += B->IntConstant(kCheckedEntry); |
1559 | code_ += B->BuildEntryPointsIntrospection(); |
1560 | } |
1561 | Fragment do_checks = code_; |
1562 | |
1563 | // Create a separate unchecked entry point. |
1564 | unchecked_entry = B->BuildFunctionEntry(graph_entry_); |
1565 | code_ = Fragment(unchecked_entry); |
1566 | |
1567 | // Re-build prologue for unchecked entry point. It can only contain |
1568 | // Entry, CheckStack and DebugCheck instructions. |
1569 | bytecode_instr_ = raw_bytecode_; |
1570 | ASSERT(KernelBytecode::IsEntryOpcode(bytecode_instr_)); |
1571 | bytecode_instr_ = KernelBytecode::Next(bytecode_instr_); |
1572 | while (!KernelBytecode::IsJumpIfUncheckedOpcode(bytecode_instr_)) { |
1573 | ASSERT(KernelBytecode::IsCheckStackOpcode(bytecode_instr_) || |
1574 | KernelBytecode::IsDebugCheckOpcode(bytecode_instr_)); |
1575 | ASSERT(jump_targets_.Lookup(bytecode_instr_ - raw_bytecode_) == |
1576 | nullptr); |
1577 | BuildInstruction(KernelBytecode::DecodeOpcode(bytecode_instr_)); |
1578 | bytecode_instr_ = KernelBytecode::Next(bytecode_instr_); |
1579 | } |
1580 | ASSERT((bytecode_instr_ - raw_bytecode_) == pc_); |
1581 | |
1582 | if (FLAG_enable_testing_pragmas) { |
1583 | code_ += B->IntConstant( |
1584 | static_cast<intptr_t>(UncheckedEntryPointStyle::kSeparate)); |
1585 | code_ += B->BuildEntryPointsIntrospection(); |
1586 | } |
1587 | code_ += B->Goto(target); |
1588 | |
1589 | code_ = do_checks; |
1590 | } break; |
1591 | |
1592 | case UncheckedEntryPointStyle::kSharedWithVariable: { |
1593 | LocalVariable* ep_var = parsed_function()->entry_points_temp_var(); |
1594 | |
1595 | // Dispatch based on the value of entry_points_temp_var. |
1596 | TargetEntryInstr *do_checks, *skip_checks; |
1597 | if (FLAG_enable_testing_pragmas) { |
1598 | code_ += B->LoadLocal(ep_var); |
1599 | code_ += B->BuildEntryPointsIntrospection(); |
1600 | } |
1601 | code_ += B->LoadLocal(ep_var); |
1602 | code_ += B->IntConstant(kUncheckedEntry); |
1603 | code_ += B->BranchIfEqual(&skip_checks, &do_checks, /*negate=*/false); |
1604 | |
1605 | code_ = Fragment(skip_checks); |
1606 | code_ += B->Goto(target); |
1607 | |
1608 | // Relink the body of the function from normal entry to 'prologue_join'. |
1609 | JoinEntryInstr* prologue_join = B->BuildJoinEntry(); |
1610 | FunctionEntryInstr* normal_entry = graph_entry_->normal_entry(); |
1611 | if (normal_entry->next() != nullptr) { |
1612 | prologue_join->LinkTo(normal_entry->next()); |
1613 | normal_entry->set_next(nullptr); |
1614 | } |
1615 | |
1616 | unchecked_entry = B->BuildFunctionEntry(graph_entry_); |
1617 | code_ = Fragment(unchecked_entry); |
1618 | code_ += B->IntConstant(kUncheckedEntry); |
1619 | code_ += B->StoreLocal(TokenPosition::kNoSource, ep_var); |
1620 | code_ += B->Drop(); |
1621 | code_ += B->Goto(prologue_join); |
1622 | |
1623 | code_ = Fragment(normal_entry); |
1624 | code_ += B->IntConstant(kCheckedEntry); |
1625 | code_ += B->StoreLocal(TokenPosition::kNoSource, ep_var); |
1626 | code_ += B->Drop(); |
1627 | code_ += B->Goto(prologue_join); |
1628 | |
1629 | code_ = Fragment(do_checks); |
1630 | } break; |
1631 | } |
1632 | |
1633 | if (unchecked_entry != nullptr) { |
1634 | B->RecordUncheckedEntryPoint(graph_entry_, unchecked_entry); |
1635 | } |
1636 | } |
1637 | |
1638 | void BytecodeFlowGraphBuilder::BuildDrop1() { |
1639 | if (is_generating_interpreter()) { |
1640 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1641 | // AdjustSP(-1); |
1642 | } else { |
1643 | code_ += B->Drop(); |
1644 | } |
1645 | } |
1646 | |
1647 | void BytecodeFlowGraphBuilder::BuildReturnTOS() { |
1648 | BuildDebugStepCheck(); |
1649 | LoadStackSlots(1); |
1650 | ASSERT(code_.is_open()); |
1651 | intptr_t yield_index = PcDescriptorsLayout::kInvalidYieldIndex; |
1652 | if (function().IsAsyncClosure() || function().IsAsyncGenClosure()) { |
1653 | if (pc_ == last_yield_point_pc_) { |
1654 | // The return might actually be a yield point, if so we need to attach the |
1655 | // yield index to the return instruction. |
1656 | yield_index = last_yield_point_index_; |
1657 | } |
1658 | } |
1659 | code_ += B->Return(position_, yield_index); |
1660 | ASSERT(IsStackEmpty()); |
1661 | } |
1662 | |
1663 | void BytecodeFlowGraphBuilder::BuildTrap() { |
1664 | code_ += Fragment(new (Z) StopInstr("Bytecode Trap instruction" )).closed(); |
1665 | } |
1666 | |
1667 | void BytecodeFlowGraphBuilder::BuildThrow() { |
1668 | if (is_generating_interpreter()) { |
1669 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1670 | } |
1671 | |
1672 | if (DecodeOperandA().value() == 0) { |
1673 | // throw |
1674 | LoadStackSlots(1); |
1675 | Value* exception = Pop(); |
1676 | code_ += |
1677 | Fragment(new (Z) ThrowInstr(position_, B->GetNextDeoptId(), exception)) |
1678 | .closed(); |
1679 | } else { |
1680 | // rethrow |
1681 | LoadStackSlots(2); |
1682 | Value* stacktrace = Pop(); |
1683 | Value* exception = Pop(); |
1684 | code_ += Fragment(new (Z) ReThrowInstr(position_, kInvalidTryIndex, |
1685 | B->GetNextDeoptId(), exception, |
1686 | stacktrace)) |
1687 | .closed(); |
1688 | } |
1689 | |
1690 | ASSERT(code_.is_closed()); |
1691 | |
1692 | if (!IsStackEmpty()) { |
1693 | DropUnusedValuesFromStack(); |
1694 | B->stack_ = nullptr; |
1695 | } |
1696 | } |
1697 | |
1698 | void BytecodeFlowGraphBuilder::BuildMoveSpecial() { |
1699 | if (is_generating_interpreter()) { |
1700 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1701 | } |
1702 | |
1703 | LocalVariable* special_var = nullptr; |
1704 | switch (DecodeOperandA().value()) { |
1705 | case KernelBytecode::kExceptionSpecialIndex: |
1706 | ASSERT(exception_var_ != nullptr); |
1707 | special_var = exception_var_; |
1708 | break; |
1709 | case KernelBytecode::kStackTraceSpecialIndex: |
1710 | ASSERT(stacktrace_var_ != nullptr); |
1711 | special_var = stacktrace_var_; |
1712 | break; |
1713 | default: |
1714 | UNREACHABLE(); |
1715 | } |
1716 | |
1717 | code_ += B->LoadLocal(special_var); |
1718 | StoreLocal(DecodeOperandY()); |
1719 | code_ += B->Drop(); |
1720 | } |
1721 | |
1722 | void BytecodeFlowGraphBuilder::BuildSetFrame() { |
1723 | if (is_generating_interpreter()) { |
1724 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1725 | } |
1726 | |
1727 | // No-op in compiled code. |
1728 | ASSERT(IsStackEmpty()); |
1729 | } |
1730 | |
1731 | void BytecodeFlowGraphBuilder::BuildEqualsNull() { |
1732 | BuildDebugStepCheck(); |
1733 | |
1734 | ASSERT(scratch_var_ != nullptr); |
1735 | LoadStackSlots(1); |
1736 | |
1737 | TargetEntryInstr* true_branch = nullptr; |
1738 | TargetEntryInstr* false_branch = nullptr; |
1739 | code_ += B->BranchIfNull(&true_branch, &false_branch); |
1740 | |
1741 | JoinEntryInstr* join = B->BuildJoinEntry(); |
1742 | |
1743 | code_ = Fragment(true_branch); |
1744 | code_ += B->Constant(Bool::True()); |
1745 | code_ += B->StoreLocalRaw(position_, scratch_var_); |
1746 | code_ += B->Drop(); |
1747 | code_ += B->Goto(join); |
1748 | |
1749 | code_ = Fragment(false_branch); |
1750 | code_ += B->Constant(Bool::False()); |
1751 | code_ += B->StoreLocalRaw(position_, scratch_var_); |
1752 | code_ += B->Drop(); |
1753 | code_ += B->Goto(join); |
1754 | |
1755 | code_ = Fragment(join); |
1756 | code_ += B->LoadLocal(scratch_var_); |
1757 | } |
1758 | |
1759 | void BytecodeFlowGraphBuilder::BuildPrimitiveOp( |
1760 | const String& name, |
1761 | Token::Kind token_kind, |
1762 | const AbstractType& static_receiver_type, |
1763 | int num_args) { |
1764 | ASSERT((num_args == 1) || (num_args == 2)); |
1765 | ASSERT(MethodTokenRecognizer::RecognizeTokenKind(name) == token_kind); |
1766 | |
1767 | // A DebugStepCheck is performed as part of the calling stub. |
1768 | |
1769 | LoadStackSlots(num_args); |
1770 | InputsArray* arguments = B->GetArguments(num_args); |
1771 | |
1772 | InstanceCallInstr* call = new (Z) InstanceCallInstr( |
1773 | position_, name, token_kind, arguments, 0, Array::null_array(), num_args, |
1774 | *ic_data_array_, B->GetNextDeoptId()); |
1775 | |
1776 | call->set_receivers_static_type(&static_receiver_type); |
1777 | |
1778 | code_ <<= call; |
1779 | B->Push(call); |
1780 | } |
1781 | |
1782 | void BytecodeFlowGraphBuilder::BuildIntOp(const String& name, |
1783 | Token::Kind token_kind, |
1784 | int num_args) { |
1785 | BuildPrimitiveOp(name, token_kind, |
1786 | AbstractType::ZoneHandle(Z, Type::IntType()), num_args); |
1787 | } |
1788 | |
1789 | void BytecodeFlowGraphBuilder::BuildDoubleOp(const String& name, |
1790 | Token::Kind token_kind, |
1791 | int num_args) { |
1792 | BuildPrimitiveOp(name, token_kind, |
1793 | AbstractType::ZoneHandle(Z, Type::Double()), num_args); |
1794 | } |
1795 | |
1796 | void BytecodeFlowGraphBuilder::BuildNegateInt() { |
1797 | BuildIntOp(Symbols::UnaryMinus(), Token::kNEGATE, 1); |
1798 | } |
1799 | |
1800 | void BytecodeFlowGraphBuilder::BuildAddInt() { |
1801 | BuildIntOp(Symbols::Plus(), Token::kADD, 2); |
1802 | } |
1803 | |
1804 | void BytecodeFlowGraphBuilder::BuildSubInt() { |
1805 | BuildIntOp(Symbols::Minus(), Token::kSUB, 2); |
1806 | } |
1807 | |
1808 | void BytecodeFlowGraphBuilder::BuildMulInt() { |
1809 | BuildIntOp(Symbols::Star(), Token::kMUL, 2); |
1810 | } |
1811 | |
1812 | void BytecodeFlowGraphBuilder::BuildTruncDivInt() { |
1813 | BuildIntOp(Symbols::TruncDivOperator(), Token::kTRUNCDIV, 2); |
1814 | } |
1815 | |
1816 | void BytecodeFlowGraphBuilder::BuildModInt() { |
1817 | BuildIntOp(Symbols::Percent(), Token::kMOD, 2); |
1818 | } |
1819 | |
1820 | void BytecodeFlowGraphBuilder::BuildBitAndInt() { |
1821 | BuildIntOp(Symbols::Ampersand(), Token::kBIT_AND, 2); |
1822 | } |
1823 | |
1824 | void BytecodeFlowGraphBuilder::BuildBitOrInt() { |
1825 | BuildIntOp(Symbols::BitOr(), Token::kBIT_OR, 2); |
1826 | } |
1827 | |
1828 | void BytecodeFlowGraphBuilder::BuildBitXorInt() { |
1829 | BuildIntOp(Symbols::Caret(), Token::kBIT_XOR, 2); |
1830 | } |
1831 | |
1832 | void BytecodeFlowGraphBuilder::BuildShlInt() { |
1833 | BuildIntOp(Symbols::LeftShiftOperator(), Token::kSHL, 2); |
1834 | } |
1835 | |
1836 | void BytecodeFlowGraphBuilder::BuildShrInt() { |
1837 | BuildIntOp(Symbols::RightShiftOperator(), Token::kSHR, 2); |
1838 | } |
1839 | |
1840 | void BytecodeFlowGraphBuilder::BuildCompareIntEq() { |
1841 | BuildIntOp(Symbols::EqualOperator(), Token::kEQ, 2); |
1842 | } |
1843 | |
1844 | void BytecodeFlowGraphBuilder::BuildCompareIntGt() { |
1845 | BuildIntOp(Symbols::RAngleBracket(), Token::kGT, 2); |
1846 | } |
1847 | |
1848 | void BytecodeFlowGraphBuilder::BuildCompareIntLt() { |
1849 | BuildIntOp(Symbols::LAngleBracket(), Token::kLT, 2); |
1850 | } |
1851 | |
1852 | void BytecodeFlowGraphBuilder::BuildCompareIntGe() { |
1853 | BuildIntOp(Symbols::GreaterEqualOperator(), Token::kGTE, 2); |
1854 | } |
1855 | |
1856 | void BytecodeFlowGraphBuilder::BuildCompareIntLe() { |
1857 | BuildIntOp(Symbols::LessEqualOperator(), Token::kLTE, 2); |
1858 | } |
1859 | |
1860 | void BytecodeFlowGraphBuilder::BuildNegateDouble() { |
1861 | BuildDoubleOp(Symbols::UnaryMinus(), Token::kNEGATE, 1); |
1862 | } |
1863 | |
1864 | void BytecodeFlowGraphBuilder::BuildAddDouble() { |
1865 | BuildDoubleOp(Symbols::Plus(), Token::kADD, 2); |
1866 | } |
1867 | |
1868 | void BytecodeFlowGraphBuilder::BuildSubDouble() { |
1869 | BuildDoubleOp(Symbols::Minus(), Token::kSUB, 2); |
1870 | } |
1871 | |
1872 | void BytecodeFlowGraphBuilder::BuildMulDouble() { |
1873 | BuildDoubleOp(Symbols::Star(), Token::kMUL, 2); |
1874 | } |
1875 | |
1876 | void BytecodeFlowGraphBuilder::BuildDivDouble() { |
1877 | BuildDoubleOp(Symbols::Slash(), Token::kDIV, 2); |
1878 | } |
1879 | |
1880 | void BytecodeFlowGraphBuilder::BuildCompareDoubleEq() { |
1881 | BuildDoubleOp(Symbols::EqualOperator(), Token::kEQ, 2); |
1882 | } |
1883 | |
1884 | void BytecodeFlowGraphBuilder::BuildCompareDoubleGt() { |
1885 | BuildDoubleOp(Symbols::RAngleBracket(), Token::kGT, 2); |
1886 | } |
1887 | |
1888 | void BytecodeFlowGraphBuilder::BuildCompareDoubleLt() { |
1889 | BuildDoubleOp(Symbols::LAngleBracket(), Token::kLT, 2); |
1890 | } |
1891 | |
1892 | void BytecodeFlowGraphBuilder::BuildCompareDoubleGe() { |
1893 | BuildDoubleOp(Symbols::GreaterEqualOperator(), Token::kGTE, 2); |
1894 | } |
1895 | |
1896 | void BytecodeFlowGraphBuilder::BuildCompareDoubleLe() { |
1897 | BuildDoubleOp(Symbols::LessEqualOperator(), Token::kLTE, 2); |
1898 | } |
1899 | |
1900 | void BytecodeFlowGraphBuilder::BuildAllocateClosure() { |
1901 | if (is_generating_interpreter()) { |
1902 | UNIMPLEMENTED(); // TODO(alexmarkov): interpreter |
1903 | } |
1904 | |
1905 | const Function& target = Function::Cast(ConstantAt(DecodeOperandD()).value()); |
1906 | code_ += B->AllocateClosure(position_, target); |
1907 | } |
1908 | |
1909 | // Builds graph for a call to 'dart:ffi::_asFunctionInternal'. The stack must |
1910 | // look like: |
1911 | // |
1912 | // <receiver> => pointer argument |
1913 | // <type arguments vector> => signatures |
1914 | // ... |
1915 | void BytecodeFlowGraphBuilder::BuildFfiAsFunction() { |
1916 | // The bytecode FGB doesn't eagerly insert PushArguments, so the type |
1917 | // arguments won't be wrapped in a PushArgumentsInstr. |
1918 | const TypeArguments& type_args = |
1919 | TypeArguments::Cast(B->Peek(/*depth=*/1)->AsConstant()->value()); |
1920 | // Drop type arguments, preserving pointer. |
1921 | code_ += B->DropTempsPreserveTop(1); |
1922 | code_ += B->BuildFfiAsFunctionInternalCall(type_args); |
1923 | } |
1924 | |
1925 | // Builds graph for a call to 'dart:ffi::_nativeCallbackFunction'. |
1926 | // The call-site must look like this (guaranteed by the FE which inserts it): |
1927 | // |
1928 | // _nativeCallbackFunction<NativeSignatureType>(target, exceptionalReturn) |
1929 | // |
1930 | // Therefore the stack shall look like: |
1931 | // |
1932 | // <exceptional return value> => ensured (by FE) to be a constant |
1933 | // <target> => closure, ensured (by FE) to be a (non-partially-instantiated) |
1934 | // static tearoff |
1935 | // <type args> => [NativeSignatureType] |
1936 | void BytecodeFlowGraphBuilder::BuildFfiNativeCallbackFunction() { |
1937 | const TypeArguments& type_args = |
1938 | TypeArguments::Cast(B->Peek(/*depth=*/2)->AsConstant()->value()); |
1939 | ASSERT(type_args.IsInstantiated() && type_args.Length() == 1); |
1940 | const Function& native_sig = Function::Handle( |
1941 | Z, Type::CheckedHandle(Z, type_args.TypeAt(0)).signature()); |
1942 | |
1943 | const Closure& target_closure = |
1944 | Closure::Cast(B->Peek(/*depth=*/1)->AsConstant()->value()); |
1945 | ASSERT(!target_closure.IsNull()); |
1946 | Function& target = Function::Handle(Z, target_closure.function()); |
1947 | ASSERT(!target.IsNull() && target.IsImplicitClosureFunction()); |
1948 | target = target.parent_function(); |
1949 | |
1950 | const Instance& exceptional_return = |
1951 | Instance::Cast(B->Peek(/*depth=*/0)->AsConstant()->value()); |
1952 | |
1953 | const Function& result = |
1954 | Function::ZoneHandle(Z, compiler::ffi::NativeCallbackFunction( |
1955 | native_sig, target, exceptional_return)); |
1956 | code_ += B->Constant(result); |
1957 | code_ += B->DropTempsPreserveTop(3); |
1958 | } |
1959 | |
1960 | void BytecodeFlowGraphBuilder::BuildDebugStepCheck() { |
1961 | #if !defined(PRODUCT) |
1962 | if (build_debug_step_checks_) { |
1963 | code_ += B->DebugStepCheck(position_); |
1964 | } |
1965 | #endif // !defined(PRODUCT) |
1966 | } |
1967 | |
1968 | intptr_t BytecodeFlowGraphBuilder::GetTryIndex(const PcDescriptors& descriptors, |
1969 | intptr_t pc) { |
1970 | const uword pc_offset = |
1971 | KernelBytecode::BytecodePcToOffset(pc, /* is_return_address = */ true); |
1972 | PcDescriptors::Iterator iter(descriptors, PcDescriptorsLayout::kAnyKind); |
1973 | intptr_t try_index = kInvalidTryIndex; |
1974 | while (iter.MoveNext()) { |
1975 | const intptr_t current_try_index = iter.TryIndex(); |
1976 | const uword start_pc = iter.PcOffset(); |
1977 | if (pc_offset < start_pc) { |
1978 | break; |
1979 | } |
1980 | const bool has_next = iter.MoveNext(); |
1981 | ASSERT(has_next); |
1982 | const uword end_pc = iter.PcOffset(); |
1983 | if (start_pc <= pc_offset && pc_offset < end_pc) { |
1984 | ASSERT(try_index < current_try_index); |
1985 | try_index = current_try_index; |
1986 | } |
1987 | } |
1988 | return try_index; |
1989 | } |
1990 | |
1991 | JoinEntryInstr* BytecodeFlowGraphBuilder::EnsureControlFlowJoin( |
1992 | const PcDescriptors& descriptors, |
1993 | intptr_t pc) { |
1994 | ASSERT((0 <= pc) && (pc < bytecode_length_)); |
1995 | JoinEntryInstr* join = jump_targets_.Lookup(pc); |
1996 | if (join == nullptr) { |
1997 | join = B->BuildJoinEntry(GetTryIndex(descriptors, pc)); |
1998 | jump_targets_.Insert(pc, join); |
1999 | } |
2000 | return join; |
2001 | } |
2002 | |
2003 | bool BytecodeFlowGraphBuilder::RequiresScratchVar(const KBCInstr* instr) { |
2004 | switch (KernelBytecode::DecodeOpcode(instr)) { |
2005 | case KernelBytecode::kEntryOptional: |
2006 | return KernelBytecode::DecodeC(instr) > 0; |
2007 | |
2008 | case KernelBytecode::kEqualsNull: |
2009 | return true; |
2010 | |
2011 | case KernelBytecode::kNativeCall: |
2012 | case KernelBytecode::kNativeCall_Wide: |
2013 | return function().recognized_kind() == MethodRecognizer::kListFactory; |
2014 | |
2015 | default: |
2016 | return false; |
2017 | } |
2018 | } |
2019 | |
2020 | void BytecodeFlowGraphBuilder::CollectControlFlow( |
2021 | const PcDescriptors& descriptors, |
2022 | const ExceptionHandlers& handlers, |
2023 | GraphEntryInstr* graph_entry) { |
2024 | bool seen_jump_if_unchecked = false; |
2025 | for (intptr_t pc = 0; pc < bytecode_length_;) { |
2026 | const KBCInstr* instr = &(raw_bytecode_[pc]); |
2027 | |
2028 | if (KernelBytecode::IsJumpOpcode(instr)) { |
2029 | const intptr_t target = pc + KernelBytecode::DecodeT(instr); |
2030 | EnsureControlFlowJoin(descriptors, target); |
2031 | |
2032 | if (KernelBytecode::IsJumpIfUncheckedOpcode(instr)) { |
2033 | if (seen_jump_if_unchecked) { |
2034 | FATAL1( |
2035 | "Multiple JumpIfUnchecked bytecode instructions are not allowed: " |
2036 | "%s." , |
2037 | function().ToFullyQualifiedCString()); |
2038 | } |
2039 | seen_jump_if_unchecked = true; |
2040 | ASSERT(entry_point_style_ == UncheckedEntryPointStyle::kNone); |
2041 | entry_point_style_ = ChooseEntryPointStyle(instr); |
2042 | if (entry_point_style_ == |
2043 | UncheckedEntryPointStyle::kSharedWithVariable) { |
2044 | parsed_function_->EnsureEntryPointsTemp(); |
2045 | } |
2046 | } |
2047 | } else if (KernelBytecode::IsCheckStackOpcode(instr) && |
2048 | (KernelBytecode::DecodeA(instr) != 0)) { |
2049 | // (dartbug.com/36590) BlockEntryInstr::FindOsrEntryAndRelink assumes |
2050 | // that CheckStackOverflow instruction is at the beginning of a join |
2051 | // block. |
2052 | EnsureControlFlowJoin(descriptors, pc); |
2053 | } |
2054 | |
2055 | if ((scratch_var_ == nullptr) && RequiresScratchVar(instr)) { |
2056 | scratch_var_ = new (Z) |
2057 | LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
2058 | Symbols::ExprTemp(), Object::dynamic_type()); |
2059 | } |
2060 | |
2061 | pc += (KernelBytecode::Next(instr) - instr); |
2062 | } |
2063 | |
2064 | PcDescriptors::Iterator iter(descriptors, PcDescriptorsLayout::kAnyKind); |
2065 | while (iter.MoveNext()) { |
2066 | const intptr_t start_pc = KernelBytecode::OffsetToBytecodePc( |
2067 | iter.PcOffset(), /* is_return_address = */ true); |
2068 | EnsureControlFlowJoin(descriptors, start_pc); |
2069 | |
2070 | const bool has_next = iter.MoveNext(); |
2071 | ASSERT(has_next); |
2072 | const intptr_t end_pc = KernelBytecode::OffsetToBytecodePc( |
2073 | iter.PcOffset(), /* is_return_address = */ true); |
2074 | EnsureControlFlowJoin(descriptors, end_pc); |
2075 | } |
2076 | |
2077 | if (handlers.num_entries() > 0) { |
2078 | B->InlineBailout("kernel::BytecodeFlowGraphBuilder::CollectControlFlow" ); |
2079 | |
2080 | exception_var_ = new (Z) |
2081 | LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
2082 | Symbols::ExceptionVar(), Object::dynamic_type()); |
2083 | stacktrace_var_ = new (Z) |
2084 | LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
2085 | Symbols::StackTraceVar(), Object::dynamic_type()); |
2086 | } |
2087 | |
2088 | for (intptr_t try_index = 0; try_index < handlers.num_entries(); |
2089 | ++try_index) { |
2090 | ExceptionHandlerInfo handler_info; |
2091 | handlers.GetHandlerInfo(try_index, &handler_info); |
2092 | |
2093 | const intptr_t handler_pc = KernelBytecode::OffsetToBytecodePc( |
2094 | handler_info.handler_pc_offset, /* is_return_address = */ false); |
2095 | JoinEntryInstr* join = EnsureControlFlowJoin(descriptors, handler_pc); |
2096 | |
2097 | // Make sure exception handler starts with SetFrame bytecode instruction. |
2098 | ASSERT(KernelBytecode::IsSetFrameOpcode(&(raw_bytecode_[handler_pc]))); |
2099 | |
2100 | const Array& handler_types = |
2101 | Array::ZoneHandle(Z, handlers.GetHandledTypes(try_index)); |
2102 | |
2103 | CatchBlockEntryInstr* entry = new (Z) CatchBlockEntryInstr( |
2104 | handler_info.is_generated != 0, B->AllocateBlockId(), |
2105 | handler_info.outer_try_index, graph_entry, handler_types, try_index, |
2106 | handler_info.needs_stacktrace != 0, B->GetNextDeoptId(), nullptr, |
2107 | nullptr, exception_var_, stacktrace_var_); |
2108 | graph_entry->AddCatchEntry(entry); |
2109 | |
2110 | code_ = Fragment(entry); |
2111 | code_ += B->Goto(join); |
2112 | } |
2113 | } |
2114 | |
2115 | UncheckedEntryPointStyle BytecodeFlowGraphBuilder::ChooseEntryPointStyle( |
2116 | const KBCInstr* jump_if_unchecked) { |
2117 | ASSERT(KernelBytecode::IsJumpIfUncheckedOpcode(jump_if_unchecked)); |
2118 | |
2119 | if (!function().MayHaveUncheckedEntryPoint()) { |
2120 | return UncheckedEntryPointStyle::kNone; |
2121 | } |
2122 | |
2123 | // Separate entry points are used if bytecode has the following pattern: |
2124 | // Entry |
2125 | // CheckStack (optional) |
2126 | // DebugCheck (optional) |
2127 | // JumpIfUnchecked |
2128 | // |
2129 | const KBCInstr* instr = raw_bytecode_; |
2130 | if (!KernelBytecode::IsEntryOpcode(instr)) { |
2131 | return UncheckedEntryPointStyle::kSharedWithVariable; |
2132 | } |
2133 | instr = KernelBytecode::Next(instr); |
2134 | if (KernelBytecode::IsCheckStackOpcode(instr)) { |
2135 | instr = KernelBytecode::Next(instr); |
2136 | } |
2137 | if (KernelBytecode::IsDebugCheckOpcode(instr)) { |
2138 | instr = KernelBytecode::Next(instr); |
2139 | } |
2140 | if (instr != jump_if_unchecked) { |
2141 | return UncheckedEntryPointStyle::kSharedWithVariable; |
2142 | } |
2143 | return UncheckedEntryPointStyle::kSeparate; |
2144 | } |
2145 | |
2146 | void BytecodeFlowGraphBuilder::CreateParameterVariables() { |
2147 | const Bytecode& bytecode = Bytecode::Handle(Z, function().bytecode()); |
2148 | object_pool_ = bytecode.object_pool(); |
2149 | bytecode_instr_ = reinterpret_cast<const KBCInstr*>(bytecode.PayloadStart()); |
2150 | |
2151 | scratch_var_ = parsed_function_->EnsureExpressionTemp(); |
2152 | |
2153 | if (KernelBytecode::IsEntryOptionalOpcode(bytecode_instr_)) { |
2154 | AllocateParametersAndLocalsForEntryOptional(); |
2155 | } else if (KernelBytecode::IsEntryOpcode(bytecode_instr_)) { |
2156 | AllocateLocalVariables(DecodeOperandD()); |
2157 | AllocateFixedParameters(); |
2158 | } else if (KernelBytecode::IsEntryFixedOpcode(bytecode_instr_)) { |
2159 | AllocateLocalVariables(DecodeOperandE()); |
2160 | AllocateFixedParameters(); |
2161 | } else { |
2162 | UNREACHABLE(); |
2163 | } |
2164 | |
2165 | if (function().IsGeneric()) { |
2166 | // For recognized methods we generate the IL by hand. Yet we need to find |
2167 | // out which [LocalVariable] is holding the function type arguments. We |
2168 | // scan the bytecode for the CheckFunctionTypeArgs bytecode. |
2169 | // |
2170 | // Note that we cannot add an extra local variable for the type argument |
2171 | // in [AllocateLocalVariables]. We sometimes reuse the same ParsedFunction |
2172 | // multiple times. For non-recognized generic bytecode functions |
2173 | // ParsedFunction::RawTypeArgumentsVariable() is set during flow graph |
2174 | // construction (after local variables are allocated). So the next time, |
2175 | // if ParsedFunction is reused, we would allocate an extra local variable. |
2176 | // TODO(alexmarkov): revise how function type args variable is allocated |
2177 | // and avoid looking at CheckFunctionTypeArgs bytecode. |
2178 | const KBCInstr* instr = |
2179 | reinterpret_cast<const KBCInstr*>(bytecode.PayloadStart()); |
2180 | const KBCInstr* end = reinterpret_cast<const KBCInstr*>( |
2181 | bytecode.PayloadStart() + bytecode.Size()); |
2182 | |
2183 | LocalVariable* type_args_var = nullptr; |
2184 | while (instr < end) { |
2185 | if (KernelBytecode::IsCheckFunctionTypeArgs(instr)) { |
2186 | const intptr_t expected_num_type_args = KernelBytecode::DecodeA(instr); |
2187 | if (expected_num_type_args > 0) { // Exclude weird closure case. |
2188 | type_args_var = LocalVariableAt(KernelBytecode::DecodeE(instr)); |
2189 | break; |
2190 | } |
2191 | } |
2192 | instr = KernelBytecode::Next(instr); |
2193 | } |
2194 | |
2195 | // Every generic function *must* have a kCheckFunctionTypeArgs bytecode. |
2196 | ASSERT(type_args_var != nullptr); |
2197 | |
2198 | // Normally the flow graph building code of bytecode will, as a side-effect |
2199 | // of building the flow graph, register the function type arguments variable |
2200 | // in the [ParsedFunction] (see [BuildCheckFunctionTypeArgs]). |
2201 | parsed_function_->set_function_type_arguments(type_args_var); |
2202 | parsed_function_->SetRawTypeArgumentsVariable(type_args_var); |
2203 | } |
2204 | } |
2205 | |
2206 | intptr_t BytecodeFlowGraphBuilder::UpdateScope( |
2207 | BytecodeLocalVariablesIterator* iter, |
2208 | intptr_t pc) { |
2209 | // Leave scopes that have ended. |
2210 | while ((current_scope_ != nullptr) && (current_scope_->end_pc_ <= pc)) { |
2211 | for (LocalVariable* local : current_scope_->hidden_vars_) { |
2212 | local_vars_[-local->index().value()] = local; |
2213 | } |
2214 | current_scope_ = current_scope_->parent_; |
2215 | } |
2216 | |
2217 | // Enter scopes that have started. |
2218 | intptr_t next_pc = bytecode_length_; |
2219 | while (!iter->IsDone()) { |
2220 | if (iter->IsScope()) { |
2221 | if (iter->StartPC() > pc) { |
2222 | next_pc = iter->StartPC(); |
2223 | break; |
2224 | } |
2225 | if (iter->EndPC() > pc) { |
2226 | // Push new scope and declare its variables. |
2227 | current_scope_ = new (Z) BytecodeScope( |
2228 | Z, iter->EndPC(), iter->ContextLevel(), current_scope_); |
2229 | if (!seen_parameters_scope_) { |
2230 | // Skip variables from the first scope as it may contain variables |
2231 | // which were used in prologue (parameters, function type arguments). |
2232 | // The already used variables should not be replaced with new ones. |
2233 | seen_parameters_scope_ = true; |
2234 | iter->MoveNext(); |
2235 | continue; |
2236 | } |
2237 | while (iter->MoveNext() && iter->IsVariableDeclaration()) { |
2238 | const intptr_t index = iter->Index(); |
2239 | if (!iter->IsCaptured() && (index >= 0)) { |
2240 | LocalVariable* local = new (Z) LocalVariable( |
2241 | TokenPosition::kNoSource, TokenPosition::kNoSource, |
2242 | String::ZoneHandle(Z, iter->Name()), |
2243 | AbstractType::ZoneHandle(Z, iter->Type())); |
2244 | local->set_index(VariableIndex(-index)); |
2245 | ASSERT(local_vars_[index]->index().value() == -index); |
2246 | current_scope_->hidden_vars_.Add(local_vars_[index]); |
2247 | local_vars_[index] = local; |
2248 | } |
2249 | } |
2250 | continue; |
2251 | } |
2252 | } |
2253 | iter->MoveNext(); |
2254 | } |
2255 | if (current_scope_ != nullptr && next_pc > current_scope_->end_pc_) { |
2256 | next_pc = current_scope_->end_pc_; |
2257 | } |
2258 | B->set_context_depth( |
2259 | current_scope_ != nullptr ? current_scope_->context_level_ : 0); |
2260 | return next_pc; |
2261 | } |
2262 | |
2263 | FlowGraph* BytecodeFlowGraphBuilder::BuildGraph() { |
2264 | const Bytecode& bytecode = Bytecode::Handle(Z, function().bytecode()); |
2265 | |
2266 | object_pool_ = bytecode.object_pool(); |
2267 | raw_bytecode_ = reinterpret_cast<const KBCInstr*>(bytecode.PayloadStart()); |
2268 | bytecode_length_ = bytecode.Size() / sizeof(KBCInstr); |
2269 | |
2270 | graph_entry_ = new (Z) GraphEntryInstr(*parsed_function_, B->osr_id_); |
2271 | |
2272 | auto normal_entry = B->BuildFunctionEntry(graph_entry_); |
2273 | graph_entry_->set_normal_entry(normal_entry); |
2274 | |
2275 | const PcDescriptors& descriptors = |
2276 | PcDescriptors::Handle(Z, bytecode.pc_descriptors()); |
2277 | const ExceptionHandlers& handlers = |
2278 | ExceptionHandlers::Handle(Z, bytecode.exception_handlers()); |
2279 | |
2280 | CollectControlFlow(descriptors, handlers, graph_entry_); |
2281 | |
2282 | inferred_types_attribute_ ^= BytecodeReader::GetBytecodeAttribute( |
2283 | function(), Symbols::vm_inferred_type_metadata()); |
2284 | |
2285 | kernel::BytecodeSourcePositionsIterator source_pos_iter(Z, bytecode); |
2286 | bool update_position = source_pos_iter.MoveNext(); |
2287 | |
2288 | kernel::BytecodeLocalVariablesIterator local_vars_iter(Z, bytecode); |
2289 | intptr_t next_pc_to_update_scope = |
2290 | local_vars_iter.MoveNext() ? 0 : bytecode_length_; |
2291 | |
2292 | code_ = Fragment(normal_entry); |
2293 | |
2294 | for (pc_ = 0; pc_ < bytecode_length_; pc_ = next_pc_) { |
2295 | bytecode_instr_ = &(raw_bytecode_[pc_]); |
2296 | next_pc_ = pc_ + (KernelBytecode::Next(bytecode_instr_) - bytecode_instr_); |
2297 | |
2298 | JoinEntryInstr* join = jump_targets_.Lookup(pc_); |
2299 | if (join != nullptr) { |
2300 | Value* stack_state = stack_states_.Lookup(pc_); |
2301 | if (code_.is_open()) { |
2302 | if (stack_state != B->stack_) { |
2303 | ASSERT(stack_state == nullptr); |
2304 | stack_states_.Insert(pc_, B->stack_); |
2305 | } |
2306 | code_ += B->Goto(join); |
2307 | } else { |
2308 | ASSERT(IsStackEmpty()); |
2309 | B->stack_ = stack_state; |
2310 | } |
2311 | code_ = Fragment(join); |
2312 | join->set_stack_depth(B->GetStackDepth()); |
2313 | B->SetCurrentTryIndex(join->try_index()); |
2314 | } else { |
2315 | // Unreachable bytecode is not allowed. |
2316 | ASSERT(!code_.is_closed()); |
2317 | } |
2318 | |
2319 | while (update_position && |
2320 | static_cast<uword>(pc_) >= source_pos_iter.PcOffset()) { |
2321 | position_ = source_pos_iter.TokenPos(); |
2322 | if (source_pos_iter.IsYieldPoint()) { |
2323 | last_yield_point_pc_ = source_pos_iter.PcOffset(); |
2324 | ++last_yield_point_index_; |
2325 | } |
2326 | update_position = source_pos_iter.MoveNext(); |
2327 | } |
2328 | |
2329 | if (pc_ >= next_pc_to_update_scope) { |
2330 | next_pc_to_update_scope = UpdateScope(&local_vars_iter, pc_); |
2331 | } |
2332 | |
2333 | BuildInstruction(KernelBytecode::DecodeOpcode(bytecode_instr_)); |
2334 | |
2335 | if (code_.is_closed()) { |
2336 | ASSERT(IsStackEmpty()); |
2337 | } |
2338 | } |
2339 | |
2340 | // When compiling for OSR, use a depth first search to find the OSR |
2341 | // entry and make graph entry jump to it instead of normal entry. |
2342 | // Catch entries are always considered reachable, even if they |
2343 | // become unreachable after OSR. |
2344 | if (B->IsCompiledForOsr()) { |
2345 | graph_entry_->RelinkToOsrEntry(Z, B->last_used_block_id_ + 1); |
2346 | } |
2347 | |
2348 | FlowGraph* flow_graph = new (Z) FlowGraph( |
2349 | *parsed_function_, graph_entry_, B->last_used_block_id_, prologue_info_); |
2350 | |
2351 | if (FLAG_print_flow_graph_from_bytecode) { |
2352 | FlowGraphPrinter::PrintGraph("Constructed from bytecode" , flow_graph); |
2353 | } |
2354 | |
2355 | return flow_graph; |
2356 | } |
2357 | |
2358 | } // namespace kernel |
2359 | } // namespace dart |
2360 | |