Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
prologue_builder.cc
Go to the documentation of this file.
1// Copyright (c) 2017, 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
6
11#include "vm/kernel_loader.h"
12#include "vm/longjump.h"
13#include "vm/object_store.h"
14#include "vm/report.h"
15#include "vm/resolver.h"
16#include "vm/stack_frame.h"
17
18namespace dart {
19namespace kernel {
20
21#define Z (zone_)
22
23// Returns static type of the parameter if it can be trusted (was type checked
24// by caller) and dynamic otherwise.
26 Representation representation = kTagged) {
27 return param->was_type_checked_by_caller()
29 representation == kTagged,
31 : ((representation == kTagged)
34}
35
37 const Function& function) {
38 return !function.MakesCopyOfParameters() &&
39 !function.IsNonImplicitClosureFunction() && !function.IsGeneric();
40}
41
43 return !function.MakesCopyOfParameters() && !function.IsGeneric() &&
44 !function.IsClosureFunction();
45}
46
48 PrologueInfo* prologue_info) {
49 // We always have to build the graph, but we only link it sometimes.
50 const bool link = !is_inlining_ && !compiling_for_osr_;
51
52 const intptr_t previous_block_id = last_used_block_id_;
53
54 const bool copy_parameters = function_.MakesCopyOfParameters();
55 const bool expect_type_args = function_.IsGeneric();
56
57 Fragment prologue = Fragment(entry);
58
59 if (copy_parameters) {
61 if (link) prologue += f;
62 }
64 Fragment f = BuildClosureContextHandling();
65 if (!compiling_for_osr_) prologue += f;
66 }
67 if (expect_type_args) {
68 Fragment f = BuildTypeArgumentsHandling();
69 if (link) prologue += f;
70
72 Fragment f = BuildClosureDelayedTypeArgumentsHandling();
73 if (!compiling_for_osr_) prologue += f;
74 }
75 }
76
77 const bool is_empty_prologue = prologue.entry == prologue.current;
78 // Double-check we create empty prologues when HasEmptyPrologue returns true.
79 ASSERT(!HasEmptyPrologue(function_) || is_empty_prologue);
80
81 // Always do this to preserve deoptid numbering.
82 JoinEntryInstr* normal_code = BuildJoinEntry();
83 Fragment jump_to_normal_code = Goto(normal_code);
84
85 if (is_empty_prologue) {
86 *prologue_info = PrologueInfo(-1, -1);
87 return entry;
88 } else {
89 prologue += jump_to_normal_code;
90 *prologue_info =
91 PrologueInfo(previous_block_id, normal_code->block_id() - 1);
92 return normal_code;
93 }
94}
95
97 Fragment copy_args_prologue;
98 const int num_fixed_params = function_.num_fixed_parameters();
99 const int num_opt_pos_params = function_.NumOptionalPositionalParameters();
100 const int num_opt_named_params = function_.NumOptionalNamedParameters();
101 const int num_params =
102 num_fixed_params + num_opt_pos_params + num_opt_named_params;
103 ASSERT(function_.NumParameters() == num_params);
104
105 // This will contain information about registers assigned to fixed
106 // parameters as well as their stack locations relative to callee FP
107 // under the assumption that no other arguments were passed.
108 compiler::ParameterInfoArray fixed_params(num_fixed_params);
111 /*should_assign_stack_locations=*/true, &fixed_params);
112
113 // Check that min_num_pos_args <= num_pos_args <= max_num_pos_args,
114 // where num_pos_args is the number of positional arguments passed in.
115 const int min_num_pos_args = num_fixed_params;
116
117 LocalVariable* count_var = nullptr;
118 LocalVariable* optional_count_var = nullptr;
119 if ((num_opt_pos_params > 0) || (num_opt_named_params > 0)) {
120 copy_args_prologue += LoadArgDescriptor();
121 copy_args_prologue +=
122 LoadNativeField(Slot::ArgumentsDescriptor_positional_count());
123
124 copy_args_prologue += LoadArgDescriptor();
125 copy_args_prologue += LoadNativeField(Slot::ArgumentsDescriptor_count());
126 count_var = MakeTemporary();
127
128 copy_args_prologue += LoadLocal(count_var);
129 copy_args_prologue += IntConstant(min_num_pos_args);
130 copy_args_prologue += SmiBinaryOp(Token::kSUB, /* truncate= */ true);
131 optional_count_var = MakeTemporary();
132 }
133
134 // Copy mandatory parameters down.
135 intptr_t param = 0;
136 for (; param < num_fixed_params; ++param) {
137 const auto [location, representation] = fixed_params[param];
138
139 const auto lo_location =
140 location.IsPairLocation() ? location.AsPairLocation()->At(0) : location;
141
142 if (lo_location.IsMachineRegister()) {
143 continue;
144 }
145
146 if ((num_opt_pos_params > 0) || (num_opt_named_params > 0)) {
147 copy_args_prologue += LoadLocal(optional_count_var);
148 } else {
149 copy_args_prologue += IntConstant(0);
150 }
151 const intptr_t stack_slot_offset = lo_location.ToStackSlotOffset();
152 copy_args_prologue += LoadFpRelativeSlot(
153 stack_slot_offset,
154 ParameterType(ParameterVariable(param), representation),
155 representation);
156 copy_args_prologue +=
157 StoreLocalRaw(TokenPosition::kNoSource, ParameterVariable(param));
158 copy_args_prologue += Drop();
159 }
160
161 // Copy optional parameters down.
162 if (num_opt_pos_params > 0) {
163 JoinEntryInstr* next_missing = nullptr;
164 for (intptr_t opt_param = 0; param < num_params; ++param, ++opt_param) {
165 TargetEntryInstr *supplied, *missing;
166 copy_args_prologue += IntConstant(opt_param + 1);
167 copy_args_prologue += LoadLocal(optional_count_var);
168 copy_args_prologue += SmiRelationalOp(Token::kLTE);
169 copy_args_prologue += BranchIfTrue(&supplied, &missing);
170
171 Fragment good(supplied);
172 good += LoadLocal(optional_count_var);
173 // Note: FP[param_end_from_fp + 1 + (optional_count_var - 1)] points to
174 // the first optional parameter.
175 good += LoadFpRelativeSlot(
176 compiler::target::kWordSize *
177 (compiler::target::frame_layout.param_end_from_fp - opt_param),
178 ParameterType(ParameterVariable(param)));
179 good += StoreLocalRaw(TokenPosition::kNoSource, ParameterVariable(param));
180 good += Drop();
181
182 Fragment not_good(missing);
183 if (next_missing != nullptr) {
184 not_good += Goto(next_missing);
185 not_good.current = next_missing;
186 }
187 next_missing = BuildJoinEntry();
188 not_good += Constant(DefaultParameterValueAt(opt_param));
189 not_good +=
190 StoreLocalRaw(TokenPosition::kNoSource, ParameterVariable(param));
191 not_good += Drop();
192 not_good += Goto(next_missing);
193
194 copy_args_prologue.current = good.current;
195 }
196 copy_args_prologue += Goto(next_missing /* join good/not_good flows */);
197 copy_args_prologue.current = next_missing;
198
199 } else if (num_opt_named_params > 0) {
200 const intptr_t first_name_offset =
201 compiler::target::ArgumentsDescriptor::first_named_entry_offset() -
202 compiler::target::Array::data_offset();
203
204 // Start by alphabetically sorting the names of the optional parameters.
205 int* opt_param_position = Z->Alloc<int>(num_opt_named_params);
206 SortOptionalNamedParametersInto(opt_param_position, num_fixed_params,
207 num_params);
208
209 LocalVariable* optional_count_vars_processed =
211 ASSERT(optional_count_vars_processed != nullptr);
212 copy_args_prologue += IntConstant(0);
213 copy_args_prologue +=
214 StoreLocalRaw(TokenPosition::kNoSource, optional_count_vars_processed);
215 copy_args_prologue += Drop();
216
217 for (intptr_t i = 0; param < num_params; ++param, ++i) {
218 copy_args_prologue += IntConstant(
219 compiler::target::ArgumentsDescriptor::named_entry_size() /
220 compiler::target::kCompressedWordSize);
221 copy_args_prologue += LoadLocal(optional_count_vars_processed);
222 copy_args_prologue += SmiBinaryOp(Token::kMUL, /* truncate= */ true);
223 LocalVariable* tuple_diff = MakeTemporary();
224
225 // Let's load position from arg descriptor (to see which parameter is the
226 // name) and move kEntrySize forward in ArgDescriptor names array.
227 //
228 // Later we'll either add this fragment directly to the copy_args_prologue
229 // if no check is needed or add an appropriate check.
230 Fragment good;
231 {
232 // fp[target::frame_layout.param_end_from_fp + (count_var - pos)]
233 good += LoadLocal(count_var);
234 {
235 // pos = arg_desc[names_offset + arg_desc_name_index + positionOffset]
236 good += LoadArgDescriptor();
237 good += IntConstant(
238 (first_name_offset +
239 compiler::target::ArgumentsDescriptor::position_offset()) /
240 compiler::target::kCompressedWordSize);
241 good += LoadLocal(tuple_diff);
242 good += SmiBinaryOp(Token::kADD, /* truncate= */ true);
243 good += LoadIndexed(
244 kArrayCid, /*index_scale*/ compiler::target::kCompressedWordSize);
245 }
246 good += SmiBinaryOp(Token::kSUB, /* truncate= */ true);
247 good += LoadFpRelativeSlot(
248 compiler::target::kWordSize *
249 compiler::target::frame_layout.param_end_from_fp,
250 ParameterType(ParameterVariable(opt_param_position[i])));
251
252 // Copy down.
253 good += StoreLocalRaw(TokenPosition::kNoSource,
254 ParameterVariable(opt_param_position[i]));
255 good += Drop();
256
257 // Increase processed optional variable count.
258 good += LoadLocal(optional_count_vars_processed);
259 good += IntConstant(1);
260 good += SmiBinaryOp(Token::kADD, /* truncate= */ true);
261 good += StoreLocalRaw(TokenPosition::kNoSource,
262 optional_count_vars_processed);
263 good += Drop();
264 }
265
266 const bool required = function_.IsRequiredAt(opt_param_position[i]);
267
268 if (required) {
269 copy_args_prologue += good;
270 } else {
271 // name = arg_desc[names_offset + arg_desc_name_index + nameOffset]
272 copy_args_prologue += LoadArgDescriptor();
273 copy_args_prologue +=
274 IntConstant((first_name_offset +
275 compiler::target::ArgumentsDescriptor::name_offset()) /
276 compiler::target::kCompressedWordSize);
277 copy_args_prologue += LoadLocal(tuple_diff);
278 copy_args_prologue += SmiBinaryOp(Token::kADD, /* truncate= */ true);
279 copy_args_prologue += LoadIndexed(
280 kArrayCid, /*index_scale*/ compiler::target::kCompressedWordSize);
281
282 // first name in sorted list of all names
283 const String& param_name = String::ZoneHandle(
284 Z, function_.ParameterNameAt(opt_param_position[i]));
285 ASSERT(param_name.IsSymbol());
286 copy_args_prologue += Constant(param_name);
287
288 // Compare the two names: Note that the ArgumentDescriptor array always
289 // terminates with a "null" name (i.e. kNullCid), which will prevent us
290 // from running out-of-bounds.
291 TargetEntryInstr *supplied, *missing;
292 copy_args_prologue += BranchIfStrictEqual(&supplied, &missing);
293
294 // Join good/not_good.
296
297 // Put good in the flowgraph as a separate basic block.
298 good.Prepend(supplied);
299 good += Goto(join);
300
301 // We had no match, so load the default constant.
302 Fragment not_good(missing);
303 not_good += Constant(
304 DefaultParameterValueAt(opt_param_position[i] - num_fixed_params));
305
306 // Copy down with default value.
307 not_good += StoreLocalRaw(TokenPosition::kNoSource,
308 ParameterVariable(opt_param_position[i]));
309 not_good += Drop();
310 not_good += Goto(join);
311
312 copy_args_prologue.current = join;
313 }
314
315 copy_args_prologue += Drop(); // tuple_diff
316 }
317 }
318
319 if ((num_opt_pos_params > 0) || (num_opt_named_params > 0)) {
320 copy_args_prologue += Drop(); // optional_count_var
321 copy_args_prologue += Drop(); // count_var
322 copy_args_prologue += Drop(); // positional_count_var
323 }
324
325 return copy_args_prologue;
326}
327
328Fragment PrologueBuilder::BuildClosureContextHandling() {
329 LocalVariable* closure_parameter = parsed_function_->ParameterVariable(0);
331
332 // Load closure.context & store it into the context variable.
333 // (both load/store happen on the copied-down places).
334 Fragment populate_context;
335 populate_context += LoadLocal(closure_parameter);
336 populate_context += LoadNativeField(Slot::Closure_context());
337 populate_context += StoreLocal(TokenPosition::kNoSource, context);
338 populate_context += Drop();
339 return populate_context;
340}
341
342Fragment PrologueBuilder::BuildTypeArgumentsHandling() {
344 ASSERT(type_args_var != nullptr);
345
346 Fragment handling;
347
348 Fragment store_type_args;
349 store_type_args += LoadArgDescriptor();
350 store_type_args += LoadNativeField(Slot::ArgumentsDescriptor_size());
351 store_type_args += LoadFpRelativeSlot(
352 compiler::target::kWordSize *
353 (1 + compiler::target::frame_layout.param_end_from_fp),
355 kTypeArgumentsCid, nullptr));
356 store_type_args += StoreLocal(TokenPosition::kNoSource, type_args_var);
357 store_type_args += Drop();
358
359 Fragment store_null;
360 store_null += NullConstant();
361 store_null += StoreLocal(TokenPosition::kNoSource, type_args_var);
362 store_null += Drop();
363
364 handling += TestTypeArgsLen(store_null, store_type_args, 0);
365
366 return handling;
367}
368
369Fragment PrologueBuilder::BuildClosureDelayedTypeArgumentsHandling() {
370 const auto& function = parsed_function_->function();
371 ASSERT(function.IsClosureFunction());
372 LocalVariable* const type_args_var =
374 ASSERT(type_args_var != nullptr);
375
376 LocalVariable* const closure = parsed_function_->ParameterVariable(0);
377
378 // Currently, delayed type arguments can only be introduced through type
379 // inference in the FE. So if they are present, we can assume they are
380 // correct in number and bound.
381 Fragment use_delayed_type_args;
382 use_delayed_type_args += LoadLocal(closure);
383 use_delayed_type_args +=
384 LoadNativeField(Slot::Closure_delayed_type_arguments());
385 use_delayed_type_args += StoreLocal(TokenPosition::kNoSource, type_args_var);
386 use_delayed_type_args += Drop();
387
388 return TestDelayedTypeArgs(closure,
389 /*present=*/use_delayed_type_args,
390 /*absent=*/Fragment());
391}
392
393void PrologueBuilder::SortOptionalNamedParametersInto(int* opt_param_position,
394 int num_fixed_params,
395 int num_params) {
396 String& name = String::Handle(Z);
397 String& name_i = String::Handle(Z);
398 for (int pos = num_fixed_params; pos < num_params; pos++) {
400 int i = pos - num_fixed_params;
401 while (--i >= 0) {
402 name_i = function_.ParameterNameAt(opt_param_position[i]);
403 const intptr_t result = name.CompareTo(name_i);
404 ASSERT(result != 0);
405 if (result > 0) break;
406 opt_param_position[i + 1] = opt_param_position[i];
407 }
408 opt_param_position[i + 1] = pos;
409 }
410}
411
412} // namespace kernel
413} // namespace dart
SkPoint pos
#define Z
const T & At(intptr_t index) const
intptr_t block_id() const
Definition il.h:1655
CompileType CopyNonNullable()
static constexpr bool kCannotBeSentinel
static constexpr bool kCanBeNull
static CompileType Dynamic()
static CompileType FromAbstractType(const AbstractType &type, bool can_be_null, bool can_be_sentinel)
static intptr_t ComputeLocationsOfFixedParameters(Zone *zone, const Function &function, bool should_assign_stack_locations=false, compiler::ParameterInfoArray *parameter_info=nullptr)
StringPtr ParameterNameAt(intptr_t index) const
Definition object.cc:8660
bool IsRequiredAt(intptr_t index) const
Definition object.cc:8799
bool MakesCopyOfParameters() const
Definition object.h:3494
bool IsClosureFunction() const
Definition object.h:3871
intptr_t NumOptionalNamedParameters() const
Definition object.cc:8932
intptr_t NumOptionalPositionalParameters() const
Definition object.cc:8929
bool IsGeneric() const
Definition object.cc:8902
intptr_t num_fixed_parameters() const
Definition object.cc:8914
intptr_t NumParameters() const
Definition object.cc:8935
const AbstractType & static_type() const
Definition scopes.h:134
bool was_type_checked_by_caller() const
Definition scopes.h:194
static Object & Handle()
Definition object.h:407
static Object & ZoneHandle()
Definition object.h:419
LocalVariable * expression_temp_var() const
Definition parser.h:151
const Function & function() const
Definition parser.h:73
LocalVariable * RawTypeArgumentsVariable() const
Definition parser.h:223
LocalVariable * ParameterVariable(intptr_t i) const
Definition parser.h:239
LocalVariable * current_context_var() const
Definition parser.h:128
bool IsSymbol() const
Definition object.h:10288
Fragment TestDelayedTypeArgs(LocalVariable *closure, Fragment present, Fragment absent)
Fragment LoadLocal(LocalVariable *variable)
Fragment LoadFpRelativeSlot(intptr_t offset, CompileType result_type, Representation representation=kTagged)
Fragment StoreLocalRaw(TokenPosition position, LocalVariable *variable)
Fragment StoreLocal(LocalVariable *variable)
Fragment LoadNativeField(const Slot &native_field, InnerPointerAccess loads_inner_pointer, bool calls_initializer=false)
Fragment BranchIfTrue(TargetEntryInstr **then_entry, TargetEntryInstr **otherwise_entry, bool negate=false)
Fragment LoadIndexed(classid_t class_id, intptr_t index_scale=compiler::target::kWordSize, bool index_unboxed=false, AlignmentType alignment=kAlignedAccess)
LocalVariable * MakeTemporary(const char *suffix=nullptr)
Fragment TestTypeArgsLen(Fragment eq_branch, Fragment neq_branch, intptr_t num_type_args)
Fragment SmiBinaryOp(Token::Kind op, bool is_truncating=false)
Fragment Goto(JoinEntryInstr *destination)
Fragment BranchIfStrictEqual(TargetEntryInstr **then_entry, TargetEntryInstr **otherwise_entry)
void Prepend(Instruction *start)
BlockEntryInstr * BuildPrologue(BlockEntryInstr *entry, PrologueInfo *prologue_info)
static bool HasEmptyPrologue(const Function &function)
static bool PrologueSkippableOnUncheckedEntry(const Function &function)
#define ASSERT(E)
GAsyncResult * result
Dart_NativeFunction function
Definition fuchsia.cc:51
static CompileType ParameterType(LocalVariable *param, Representation representation=kTagged)
const char *const name
Representation
Definition locations.h:66