]>
Commit | Line | Data |
---|---|---|
a1ab4c31 AC |
1 | /**************************************************************************** |
2 | * * | |
3 | * GNAT COMPILER COMPONENTS * | |
4 | * * | |
5 | * T R A N S * | |
6 | * * | |
7 | * C Implementation File * | |
8 | * * | |
3b0cd3f0 | 9 | * Copyright (C) 1992-2022, Free Software Foundation, Inc. * |
a1ab4c31 AC |
10 | * * |
11 | * GNAT is free software; you can redistribute it and/or modify it under * | |
12 | * terms of the GNU General Public License as published by the Free Soft- * | |
748086b7 | 13 | * ware Foundation; either version 3, or (at your option) any later ver- * |
a1ab4c31 AC |
14 | * sion. GNAT is distributed in the hope that it will be useful, but WITH- * |
15 | * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * | |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * | |
17 | * for more details. You should have received a copy of the GNU General * | |
748086b7 JJ |
18 | * Public License distributed with GNAT; see file COPYING3. If not see * |
19 | * <http://www.gnu.org/licenses/>. * | |
a1ab4c31 AC |
20 | * * |
21 | * GNAT was originally developed by the GNAT team at New York University. * | |
22 | * Extensive contributions were provided by Ada Core Technologies Inc. * | |
23 | * * | |
24 | ****************************************************************************/ | |
25 | ||
26 | #include "config.h" | |
27 | #include "system.h" | |
28 | #include "coretypes.h" | |
2adfab87 AM |
29 | #include "target.h" |
30 | #include "function.h" | |
31 | #include "bitmap.h" | |
a1ab4c31 | 32 | #include "tree.h" |
2adfab87 | 33 | #include "gimple-expr.h" |
d8a2d370 | 34 | #include "stringpool.h" |
2adfab87 | 35 | #include "cgraph.h" |
64235766 | 36 | #include "predict.h" |
2adfab87 AM |
37 | #include "diagnostic.h" |
38 | #include "alias.h" | |
39 | #include "fold-const.h" | |
d8a2d370 DN |
40 | #include "stor-layout.h" |
41 | #include "stmt.h" | |
42 | #include "varasm.h" | |
a1ab4c31 | 43 | #include "output.h" |
fa6fd7b7 | 44 | #include "debug.h" |
d477d1fe | 45 | #include "libfuncs.h" /* For set_stack_check_libfunc. */ |
a1ab4c31 | 46 | #include "tree-iterator.h" |
2fb9a547 | 47 | #include "gimplify.h" |
f2423384 | 48 | #include "opts.h" |
48a24fcf | 49 | #include "common/common-target.h" |
7beb2b9d | 50 | #include "gomp-constants.h" |
314e6352 ML |
51 | #include "stringpool.h" |
52 | #include "attribs.h" | |
89576d86 | 53 | #include "tree-nested.h" |
8713b7e4 | 54 | |
a1ab4c31 | 55 | #include "ada.h" |
8713b7e4 | 56 | #include "adadecode.h" |
a1ab4c31 AC |
57 | #include "types.h" |
58 | #include "atree.h" | |
a1ab4c31 AC |
59 | #include "namet.h" |
60 | #include "nlists.h" | |
61 | #include "snames.h" | |
62 | #include "stringt.h" | |
63 | #include "uintp.h" | |
64 | #include "urealp.h" | |
65 | #include "fe.h" | |
66 | #include "sinfo.h" | |
67 | #include "einfo.h" | |
831f44c6 | 68 | #include "gadaint.h" |
a1ab4c31 AC |
69 | #include "ada-tree.h" |
70 | #include "gigi.h" | |
a1ab4c31 AC |
71 | |
72 | /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca, | |
73 | for fear of running out of stack space. If we need more, we use xmalloc | |
74 | instead. */ | |
75 | #define ALLOCA_THRESHOLD 1000 | |
76 | ||
831f44c6 | 77 | /* Pointers to front-end tables accessed through macros. */ |
99e30ba8 | 78 | Node_Header *Node_Offsets_Ptr; |
a6d3b499 | 79 | any_slot *Slots_Ptr; |
a1ab4c31 AC |
80 | Node_Id *Next_Node_Ptr; |
81 | Node_Id *Prev_Node_Ptr; | |
82 | struct Elist_Header *Elists_Ptr; | |
83 | struct Elmt_Item *Elmts_Ptr; | |
84 | struct String_Entry *Strings_Ptr; | |
85 | Char_Code *String_Chars_Ptr; | |
86 | struct List_Header *List_Headers_Ptr; | |
87 | ||
831f44c6 EB |
88 | /* Highest number in the front-end node table. */ |
89 | int max_gnat_nodes; | |
90 | ||
1e17ef87 | 91 | /* True when gigi is being called on an analyzed but unexpanded |
a1ab4c31 | 92 | tree, and the only purpose of the call is to properly annotate |
1e17ef87 | 93 | types with representation information. */ |
a1ab4c31 AC |
94 | bool type_annotate_only; |
95 | ||
f04b8d69 | 96 | /* List of N_Validate_Unchecked_Conversion nodes in the unit. */ |
9771b263 | 97 | static vec<Node_Id> gnat_validate_uc_list; |
f04b8d69 | 98 | |
ec4a0d83 EB |
99 | /* List of expressions of pragma Compile_Time_{Error|Warning} in the unit. */ |
100 | static vec<Node_Id> gnat_compile_time_expr_list; | |
101 | ||
a1ab4c31 AC |
102 | /* When not optimizing, we cache the 'First, 'Last and 'Length attributes |
103 | of unconstrained array IN parameters to avoid emitting a great deal of | |
104 | redundant instructions to recompute them each time. */ | |
6bf68a93 | 105 | struct GTY (()) parm_attr_d { |
a1ab4c31 AC |
106 | int id; /* GTY doesn't like Entity_Id. */ |
107 | int dim; | |
108 | tree first; | |
109 | tree last; | |
110 | tree length; | |
111 | }; | |
112 | ||
6bf68a93 | 113 | typedef struct parm_attr_d *parm_attr; |
a1ab4c31 | 114 | |
852dd866 | 115 | /* Structure used to record information for a function. */ |
d1b38208 | 116 | struct GTY(()) language_function { |
9771b263 | 117 | vec<parm_attr, va_gc> *parm_attr_cache; |
71196d4e | 118 | bitmap named_ret_val; |
9771b263 | 119 | vec<tree, va_gc> *other_ret_val; |
088b91c7 | 120 | int gnat_ret; |
a1ab4c31 AC |
121 | }; |
122 | ||
123 | #define f_parm_attr_cache \ | |
124 | DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache | |
125 | ||
71196d4e EB |
126 | #define f_named_ret_val \ |
127 | DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val | |
128 | ||
129 | #define f_other_ret_val \ | |
130 | DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val | |
131 | ||
088b91c7 EB |
132 | #define f_gnat_ret \ |
133 | DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret | |
134 | ||
a1ab4c31 AC |
135 | /* A structure used to gather together information about a statement group. |
136 | We use this to gather related statements, for example the "then" part | |
137 | of a IF. In the case where it represents a lexical scope, we may also | |
138 | have a BLOCK node corresponding to it and/or cleanups. */ | |
139 | ||
d1b38208 | 140 | struct GTY((chain_next ("%h.previous"))) stmt_group { |
a1ab4c31 | 141 | struct stmt_group *previous; /* Previous code group. */ |
1e17ef87 EB |
142 | tree stmt_list; /* List of statements for this code group. */ |
143 | tree block; /* BLOCK for this code group, if any. */ | |
a1ab4c31 AC |
144 | tree cleanups; /* Cleanups for this code group, if any. */ |
145 | }; | |
146 | ||
147 | static GTY(()) struct stmt_group *current_stmt_group; | |
148 | ||
149 | /* List of unused struct stmt_group nodes. */ | |
150 | static GTY((deletable)) struct stmt_group *stmt_group_free_list; | |
151 | ||
152 | /* A structure used to record information on elaboration procedures | |
153 | we've made and need to process. | |
154 | ||
155 | ??? gnat_node should be Node_Id, but gengtype gets confused. */ | |
156 | ||
d1b38208 | 157 | struct GTY((chain_next ("%h.next"))) elab_info { |
1e17ef87 | 158 | struct elab_info *next; /* Pointer to next in chain. */ |
a1ab4c31 AC |
159 | tree elab_proc; /* Elaboration procedure. */ |
160 | int gnat_node; /* The N_Compilation_Unit. */ | |
161 | }; | |
162 | ||
163 | static GTY(()) struct elab_info *elab_info_list; | |
164 | ||
39f579c7 NF |
165 | /* Stack of exception pointer variables. Each entry is the VAR_DECL |
166 | that stores the address of the raised exception. Nonzero means we | |
167 | are in an exception handler. Not used in the zero-cost case. */ | |
9771b263 | 168 | static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack; |
a1ab4c31 | 169 | |
624e1688 AC |
170 | /* In ZCX case, current exception pointer. Used to re-raise it. */ |
171 | static GTY(()) tree gnu_incoming_exc_ptr; | |
172 | ||
39f579c7 | 173 | /* Stack for storing the current elaboration procedure decl. */ |
9771b263 | 174 | static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack; |
a1ab4c31 | 175 | |
39f579c7 NF |
176 | /* Stack of labels to be used as a goto target instead of a return in |
177 | some functions. See processing for N_Subprogram_Body. */ | |
9771b263 | 178 | static GTY(()) vec<tree, va_gc> *gnu_return_label_stack; |
a1ab4c31 | 179 | |
35a382b8 EB |
180 | /* Stack of variable for the return value of a function with copy-in/copy-out |
181 | parameters. See processing for N_Subprogram_Body. */ | |
9771b263 | 182 | static GTY(()) vec<tree, va_gc> *gnu_return_var_stack; |
35a382b8 | 183 | |
15bf7d19 EB |
184 | /* Structure used to record information for a range check. */ |
185 | struct GTY(()) range_check_info_d { | |
186 | tree low_bound; | |
187 | tree high_bound; | |
933a7325 EB |
188 | tree disp; |
189 | bool neg_p; | |
15bf7d19 EB |
190 | tree type; |
191 | tree invariant_cond; | |
64235766 | 192 | tree inserted_cond; |
15bf7d19 EB |
193 | }; |
194 | ||
195 | typedef struct range_check_info_d *range_check_info; | |
196 | ||
15bf7d19 EB |
197 | /* Structure used to record information for a loop. */ |
198 | struct GTY(()) loop_info_d { | |
852dd866 | 199 | tree fndecl; |
633a3f2a | 200 | tree stmt; |
15bf7d19 | 201 | tree loop_var; |
87ab2b04 EB |
202 | tree low_bound; |
203 | tree high_bound; | |
7beb2b9d OH |
204 | tree omp_loop_clauses; |
205 | tree omp_construct_clauses; | |
206 | enum tree_code omp_code; | |
9771b263 | 207 | vec<range_check_info, va_gc> *checks; |
852dd866 | 208 | vec<tree, va_gc> *invariants; |
15bf7d19 EB |
209 | }; |
210 | ||
211 | typedef struct loop_info_d *loop_info; | |
212 | ||
15bf7d19 | 213 | /* Stack of loop_info structures associated with LOOP_STMT nodes. */ |
9771b263 | 214 | static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack; |
a1ab4c31 | 215 | |
39f579c7 | 216 | /* The stacks for N_{Push,Pop}_*_Label. */ |
8f8f531f PMR |
217 | static vec<Entity_Id> gnu_constraint_error_label_stack; |
218 | static vec<Entity_Id> gnu_storage_error_label_stack; | |
219 | static vec<Entity_Id> gnu_program_error_label_stack; | |
a1ab4c31 AC |
220 | |
221 | /* Map GNAT tree codes to GCC tree codes for simple expressions. */ | |
222 | static enum tree_code gnu_codes[Number_Node_Kinds]; | |
223 | ||
a1ab4c31 | 224 | static void init_code_table (void); |
7543c8b6 | 225 | static tree get_elaboration_procedure (void); |
a1ab4c31 | 226 | static void Compilation_Unit_to_gnu (Node_Id); |
102a1631 | 227 | static bool empty_stmt_list_p (tree); |
a1ab4c31 AC |
228 | static void record_code_position (Node_Id); |
229 | static void insert_code_for (Node_Id); | |
230 | static void add_cleanup (tree, Node_Id); | |
a1ab4c31 | 231 | static void add_stmt_list (List_Id); |
a1ab4c31 | 232 | static tree build_stmt_group (List_Id, bool); |
a712b009 | 233 | static inline bool stmt_group_may_fallthru (void); |
a1ab4c31 AC |
234 | static enum gimplify_status gnat_gimplify_stmt (tree *); |
235 | static void elaborate_all_entities (Node_Id); | |
236 | static void process_freeze_entity (Node_Id); | |
6d16658d | 237 | static void process_decls (List_Id, List_Id, bool, bool); |
10069d53 EB |
238 | static tree emit_check (tree, tree, int, Node_Id); |
239 | static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id); | |
240 | static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id); | |
815b5368 | 241 | static tree convert_with_check (Entity_Id, tree, bool, bool, Node_Id); |
a1ab4c31 AC |
242 | static bool addressable_p (tree, tree); |
243 | static tree assoc_to_constructor (Entity_Id, Node_Id, tree); | |
815b5368 | 244 | static tree pos_to_constructor (Node_Id, tree); |
f04b8d69 | 245 | static void validate_unchecked_conversion (Node_Id); |
ba464315 | 246 | static void set_expr_location_from_node (tree, Node_Id, bool = false); |
17c168fe | 247 | static void set_gnu_expr_location_from_node (tree, Node_Id); |
ba464315 | 248 | static bool set_end_locus_from_node (tree, Node_Id); |
86da71db | 249 | static int lvalue_required_p (Node_Id, tree, bool, bool); |
c1fd8753 | 250 | static tree build_raise_check (int, enum exception_info_kind); |
6162cec0 | 251 | static tree create_init_temporary (const char *, tree, tree *, Node_Id); |
44662f68 | 252 | static bool maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk); |
a1ab4c31 | 253 | |
fa6fd7b7 AO |
254 | /* This makes gigi's file_info_ptr visible in this translation unit, |
255 | so that Sloc_to_locus can look it up when deciding whether to map | |
256 | decls to instances. */ | |
257 | ||
258 | static struct File_Info_Type *file_map; | |
259 | ||
39fa0de5 EB |
260 | /* Return the string of the identifier allocated for the file name Id. */ |
261 | ||
262 | static const char* | |
263 | File_Name_to_gnu (Name_Id Id) | |
264 | { | |
265 | /* __gnat_to_canonical_file_spec translates file names from pragmas | |
266 | Source_Reference that contain host style syntax not understood by GDB. */ | |
267 | const char *name = __gnat_to_canonical_file_spec (Get_Name_String (Id)); | |
268 | ||
269 | /* Use the identifier table to make a permanent copy of the file name as | |
270 | the name table gets reallocated after Gigi returns but before all the | |
271 | debugging information is output. */ | |
272 | return IDENTIFIER_POINTER (get_identifier (name)); | |
273 | } | |
274 | ||
a1ab4c31 AC |
275 | /* This is the main program of the back-end. It sets up all the table |
276 | structures and then generates code. */ | |
277 | ||
278 | void | |
4bcf6815 AC |
279 | gigi (Node_Id gnat_root, |
280 | int max_gnat_node, | |
281 | int number_name ATTRIBUTE_UNUSED, | |
99e30ba8 | 282 | Node_Header *node_offsets_ptr, |
a6d3b499 | 283 | any_slot *slots_ptr, |
4bcf6815 AC |
284 | Node_Id *next_node_ptr, |
285 | Node_Id *prev_node_ptr, | |
286 | struct Elist_Header *elists_ptr, | |
287 | struct Elmt_Item *elmts_ptr, | |
288 | struct String_Entry *strings_ptr, | |
289 | Char_Code *string_chars_ptr, | |
290 | struct List_Header *list_headers_ptr, | |
291 | Nat number_file, | |
6936c61a | 292 | struct File_Info_Type *file_info_ptr, |
4bcf6815 AC |
293 | Entity_Id standard_boolean, |
294 | Entity_Id standard_integer, | |
295 | Entity_Id standard_character, | |
296 | Entity_Id standard_long_long_float, | |
297 | Entity_Id standard_exception_type, | |
298 | Int gigi_operating_mode) | |
a1ab4c31 | 299 | { |
f04b8d69 | 300 | Node_Id gnat_iter; |
01ddebf2 | 301 | Entity_Id gnat_literal; |
1eb58520 | 302 | tree t, ftype, int64_type; |
a1ab4c31 AC |
303 | struct elab_info *info; |
304 | int i; | |
305 | ||
306 | max_gnat_nodes = max_gnat_node; | |
831f44c6 | 307 | |
76f9c7f4 BD |
308 | Node_Offsets_Ptr = node_offsets_ptr; |
309 | Slots_Ptr = slots_ptr; | |
a1ab4c31 AC |
310 | Next_Node_Ptr = next_node_ptr; |
311 | Prev_Node_Ptr = prev_node_ptr; | |
312 | Elists_Ptr = elists_ptr; | |
313 | Elmts_Ptr = elmts_ptr; | |
314 | Strings_Ptr = strings_ptr; | |
315 | String_Chars_Ptr = string_chars_ptr; | |
316 | List_Headers_Ptr = list_headers_ptr; | |
317 | ||
318 | type_annotate_only = (gigi_operating_mode == 1); | |
319 | ||
fa6fd7b7 AO |
320 | if (Generate_SCO_Instance_Table != 0) |
321 | { | |
322 | file_map = file_info_ptr; | |
323 | maybe_create_decl_to_instance_map (number_file); | |
324 | } | |
325 | ||
831f44c6 | 326 | for (i = 0; i < number_file; i++) |
a1ab4c31 | 327 | { |
a1ab4c31 | 328 | /* We rely on the order isomorphism between files and line maps. */ |
39fa0de5 EB |
329 | if ((int) LINEMAPS_ORDINARY_USED (line_table) != i) |
330 | { | |
331 | gcc_assert (i > 0); | |
332 | error ("%s contains too many lines", | |
333 | File_Name_to_gnu (file_info_ptr[i - 1].File_Name)); | |
334 | } | |
a1ab4c31 AC |
335 | |
336 | /* We create the line map for a source file at once, with a fixed number | |
337 | of columns chosen to avoid jumping over the next power of 2. */ | |
39fa0de5 EB |
338 | linemap_add (line_table, LC_ENTER, 0, |
339 | File_Name_to_gnu (file_info_ptr[i].File_Name), 1); | |
a1ab4c31 AC |
340 | linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252); |
341 | linemap_position_for_column (line_table, 252 - 1); | |
342 | linemap_add (line_table, LC_LEAVE, 0, NULL, 0); | |
343 | } | |
344 | ||
52e04e56 EB |
345 | gcc_assert (Nkind (gnat_root) == N_Compilation_Unit); |
346 | ||
347 | /* Declare the name of the compilation unit as the first global | |
348 | name in order to make the middle-end fully deterministic. */ | |
349 | t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL); | |
350 | first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t)); | |
351 | ||
a1ab4c31 AC |
352 | /* Initialize ourselves. */ |
353 | init_code_table (); | |
4116e7d0 | 354 | init_gnat_decl (); |
842d4ee2 | 355 | init_gnat_utils (); |
a1ab4c31 AC |
356 | |
357 | /* If we are just annotating types, give VOID_TYPE zero sizes to avoid | |
358 | errors. */ | |
359 | if (type_annotate_only) | |
360 | { | |
361 | TYPE_SIZE (void_type_node) = bitsize_zero_node; | |
362 | TYPE_SIZE_UNIT (void_type_node) = size_zero_node; | |
363 | } | |
364 | ||
a1ab4c31 AC |
365 | /* Enable GNAT stack checking method if needed */ |
366 | if (!Stack_Check_Probes_On_Target) | |
1f3f64b9 EB |
367 | { |
368 | set_stack_check_libfunc ("__gnat_stack_check"); | |
369 | if (flag_stack_check != NO_STACK_CHECK) | |
370 | Check_Restriction_No_Dependence_On_System (Name_Stack_Checking, | |
371 | gnat_root); | |
372 | } | |
a1ab4c31 | 373 | |
caa9d12a EB |
374 | /* Retrieve alignment settings. */ |
375 | double_float_alignment = get_target_double_float_alignment (); | |
376 | double_scalar_alignment = get_target_double_scalar_alignment (); | |
377 | ||
6936c61a EB |
378 | /* Record the builtin types. Define `integer' and `character' first so that |
379 | dbx will output them first. */ | |
1aeb40dd | 380 | record_builtin_type ("integer", integer_type_node, false); |
825da0d2 | 381 | record_builtin_type ("character", char_type_node, false); |
1aeb40dd EB |
382 | record_builtin_type ("boolean", boolean_type_node, false); |
383 | record_builtin_type ("void", void_type_node, false); | |
10069d53 EB |
384 | |
385 | /* Save the type we made for integer as the type for Standard.Integer. */ | |
6936c61a EB |
386 | save_gnu_tree (Base_Type (standard_integer), |
387 | TYPE_NAME (integer_type_node), | |
10069d53 | 388 | false); |
a1ab4c31 | 389 | |
6936c61a | 390 | /* Likewise for character as the type for Standard.Character. */ |
825da0d2 | 391 | finish_character_type (char_type_node); |
6936c61a | 392 | save_gnu_tree (Base_Type (standard_character), |
825da0d2 | 393 | TYPE_NAME (char_type_node), |
6936c61a EB |
394 | false); |
395 | ||
396 | /* Likewise for boolean as the type for Standard.Boolean. */ | |
397 | save_gnu_tree (Base_Type (standard_boolean), | |
398 | TYPE_NAME (boolean_type_node), | |
01ddebf2 EB |
399 | false); |
400 | gnat_literal = First_Literal (Base_Type (standard_boolean)); | |
401 | t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node); | |
402 | gcc_assert (t == boolean_false_node); | |
403 | t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE, | |
2056c5ed | 404 | boolean_type_node, t, true, false, false, false, false, |
c1a569ef | 405 | true, false, NULL, gnat_literal); |
01ddebf2 EB |
406 | save_gnu_tree (gnat_literal, t, false); |
407 | gnat_literal = Next_Literal (gnat_literal); | |
408 | t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node); | |
409 | gcc_assert (t == boolean_true_node); | |
410 | t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE, | |
2056c5ed | 411 | boolean_type_node, t, true, false, false, false, false, |
c1a569ef | 412 | true, false, NULL, gnat_literal); |
01ddebf2 EB |
413 | save_gnu_tree (gnat_literal, t, false); |
414 | ||
9182f718 | 415 | /* Declare the building blocks of function nodes. */ |
c1fd8753 | 416 | void_ftype = build_function_type_list (void_type_node, NULL_TREE); |
10069d53 EB |
417 | ptr_void_ftype = build_pointer_type (void_ftype); |
418 | ||
c01fe451 | 419 | /* Now declare run-time functions. */ |
10069d53 EB |
420 | malloc_decl |
421 | = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE, | |
ab3dd4ab EB |
422 | build_function_type_list (ptr_type_node, sizetype, |
423 | NULL_TREE), | |
13a6dfe3 | 424 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 425 | false, NULL, Empty); |
10069d53 EB |
426 | DECL_IS_MALLOC (malloc_decl) = 1; |
427 | ||
10069d53 EB |
428 | free_decl |
429 | = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE, | |
ab3dd4ab EB |
430 | build_function_type_list (void_type_node, |
431 | ptr_type_node, NULL_TREE), | |
13a6dfe3 | 432 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 433 | false, NULL, Empty); |
9182f718 | 434 | |
9182f718 EB |
435 | realloc_decl |
436 | = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE, | |
ab3dd4ab EB |
437 | build_function_type_list (ptr_type_node, |
438 | ptr_type_node, sizetype, | |
439 | NULL_TREE), | |
13a6dfe3 | 440 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 441 | false, NULL, Empty); |
10069d53 EB |
442 | |
443 | /* This is used for 64-bit multiplication with overflow checking. */ | |
1eb58520 | 444 | int64_type = gnat_type_for_size (64, 0); |
10069d53 EB |
445 | mulv64_decl |
446 | = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE, | |
447 | build_function_type_list (int64_type, int64_type, | |
448 | int64_type, NULL_TREE), | |
13a6dfe3 | 449 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 450 | false, NULL, Empty); |
10069d53 | 451 | |
f2d9f95e EB |
452 | if (Enable_128bit_Types) |
453 | { | |
454 | tree int128_type = gnat_type_for_size (128, 0); | |
455 | mulv128_decl | |
456 | = create_subprog_decl (get_identifier ("__gnat_mulv128"), NULL_TREE, | |
457 | build_function_type_list (int128_type, | |
458 | int128_type, | |
459 | int128_type, | |
460 | NULL_TREE), | |
461 | NULL_TREE, is_default, true, true, true, false, | |
462 | false, NULL, Empty); | |
463 | } | |
464 | ||
76af763d EB |
465 | /* Name of the _Parent field in tagged record types. */ |
466 | parent_name_id = get_identifier (Get_Name_String (Name_uParent)); | |
467 | ||
17ba0ad5 EB |
468 | /* Name of the Not_Handled_By_Others field in exception record types. */ |
469 | not_handled_by_others_name_id = get_identifier ("not_handled_by_others"); | |
871fda0a | 470 | |
10069d53 | 471 | /* Make the types and functions used for exception processing. */ |
64235766 EB |
472 | except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type)); |
473 | ||
64235766 EB |
474 | set_exception_parameter_decl |
475 | = create_subprog_decl | |
476 | (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE, | |
477 | build_function_type_list (void_type_node, ptr_type_node, ptr_type_node, | |
478 | NULL_TREE), | |
13a6dfe3 | 479 | NULL_TREE, is_default, true, true, true, false, false, NULL, Empty); |
64235766 | 480 | |
10069d53 | 481 | /* Hooks to call when entering/leaving an exception handler. */ |
5d733372 AO |
482 | ftype = build_function_type_list (ptr_type_node, |
483 | ptr_type_node, NULL_TREE); | |
10069d53 | 484 | begin_handler_decl |
5d733372 AO |
485 | = create_subprog_decl (get_identifier ("__gnat_begin_handler_v1"), |
486 | NULL_TREE, ftype, NULL_TREE, | |
13a6dfe3 | 487 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 488 | Empty); |
5d733372 AO |
489 | /* __gnat_begin_handler_v1 is not a dummy procedure, but we arrange |
490 | for it not to throw. */ | |
4ec7c4ec | 491 | TREE_NOTHROW (begin_handler_decl) = 1; |
10069d53 | 492 | |
5d733372 AO |
493 | ftype = build_function_type_list (ptr_type_node, |
494 | ptr_type_node, ptr_type_node, | |
495 | ptr_type_node, NULL_TREE); | |
10069d53 | 496 | end_handler_decl |
5d733372 | 497 | = create_subprog_decl (get_identifier ("__gnat_end_handler_v1"), NULL_TREE, |
c1a569ef | 498 | ftype, NULL_TREE, |
13a6dfe3 | 499 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 500 | Empty); |
10069d53 | 501 | |
5d733372 | 502 | ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); |
48a24fcf TG |
503 | unhandled_except_decl |
504 | = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"), | |
c1a569ef | 505 | NULL_TREE, ftype, NULL_TREE, |
13a6dfe3 | 506 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 507 | Empty); |
1e55d29a EB |
508 | |
509 | /* Indicate that it never returns. */ | |
510 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); | |
511 | reraise_zcx_decl | |
512 | = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE, | |
513 | ftype, NULL_TREE, | |
13a6dfe3 | 514 | is_default, true, true, true, false, false, NULL, |
ff9baa5f | 515 | Empty); |
48a24fcf | 516 | |
64235766 EB |
517 | /* Dummy objects to materialize "others" and "all others" in the exception |
518 | tables. These are exported by a-exexpr-gcc.adb, so see this unit for | |
519 | the types to use. */ | |
520 | others_decl | |
521 | = create_var_decl (get_identifier ("OTHERS"), | |
522 | get_identifier ("__gnat_others_value"), | |
825da0d2 | 523 | char_type_node, NULL_TREE, |
2056c5ed | 524 | true, false, true, false, false, true, false, |
64235766 EB |
525 | NULL, Empty); |
526 | ||
527 | all_others_decl | |
528 | = create_var_decl (get_identifier ("ALL_OTHERS"), | |
529 | get_identifier ("__gnat_all_others_value"), | |
825da0d2 | 530 | char_type_node, NULL_TREE, |
2056c5ed | 531 | true, false, true, false, false, true, false, |
64235766 EB |
532 | NULL, Empty); |
533 | ||
534 | unhandled_others_decl | |
535 | = create_var_decl (get_identifier ("UNHANDLED_OTHERS"), | |
536 | get_identifier ("__gnat_unhandled_others_value"), | |
825da0d2 | 537 | char_type_node, NULL_TREE, |
2056c5ed | 538 | true, false, true, false, false, true, false, |
64235766 | 539 | NULL, Empty); |
624e1688 | 540 | |
10069d53 | 541 | /* If in no exception handlers mode, all raise statements are redirected to |
d6d9f534 | 542 | __gnat_last_chance_handler. */ |
10069d53 EB |
543 | if (No_Exception_Handlers_Set ()) |
544 | { | |
1e55d29a EB |
545 | /* Indicate that it never returns. */ |
546 | ftype = build_function_type_list (void_type_node, | |
547 | build_pointer_type (char_type_node), | |
548 | integer_type_node, NULL_TREE); | |
549 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); | |
10069d53 EB |
550 | tree decl |
551 | = create_subprog_decl | |
1e55d29a | 552 | (get_identifier ("__gnat_last_chance_handler"), NULL_TREE, ftype, |
13a6dfe3 | 553 | NULL_TREE, is_default, true, true, true, false, false, NULL, |
ff9baa5f | 554 | Empty); |
cfc839a4 EB |
555 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++) |
556 | gnat_raise_decls[i] = decl; | |
10069d53 EB |
557 | } |
558 | else | |
10069d53 | 559 | { |
437f8c1e AC |
560 | /* Otherwise, make one decl for each exception reason. */ |
561 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++) | |
c1fd8753 | 562 | gnat_raise_decls[i] = build_raise_check (i, exception_simple); |
437f8c1e AC |
563 | for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++) |
564 | gnat_raise_decls_ext[i] | |
c1fd8753 | 565 | = build_raise_check (i, |
437f8c1e | 566 | i == CE_Index_Check_Failed |
ea034236 AC |
567 | || i == CE_Range_Check_Failed |
568 | || i == CE_Invalid_Data | |
569 | ? exception_range : exception_column); | |
10069d53 EB |
570 | } |
571 | ||
10069d53 EB |
572 | /* Build the special descriptor type and its null node if needed. */ |
573 | if (TARGET_VTABLE_USES_DESCRIPTORS) | |
574 | { | |
575 | tree null_node = fold_convert (ptr_void_ftype, null_pointer_node); | |
0e228dd9 | 576 | tree field_list = NULL_TREE; |
10069d53 | 577 | int j; |
9771b263 | 578 | vec<constructor_elt, va_gc> *null_vec = NULL; |
0e228dd9 | 579 | constructor_elt *elt; |
10069d53 EB |
580 | |
581 | fdesc_type_node = make_node (RECORD_TYPE); | |
cb3874dc | 582 | vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS, true); |
9771b263 | 583 | elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1); |
10069d53 EB |
584 | |
585 | for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++) | |
586 | { | |
da01bfee EB |
587 | tree field |
588 | = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node, | |
589 | NULL_TREE, NULL_TREE, 0, 1); | |
7d76717d | 590 | DECL_CHAIN (field) = field_list; |
10069d53 | 591 | field_list = field; |
0e228dd9 NF |
592 | elt->index = field; |
593 | elt->value = null_node; | |
594 | elt--; | |
10069d53 EB |
595 | } |
596 | ||
032d1b71 | 597 | finish_record_type (fdesc_type_node, nreverse (field_list), 0, false); |
1aeb40dd | 598 | record_builtin_type ("descriptor", fdesc_type_node, true); |
0e228dd9 | 599 | null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec); |
10069d53 EB |
600 | } |
601 | ||
1eb58520 AC |
602 | longest_float_type_node |
603 | = get_unpadded_type (Base_Type (standard_long_long_float)); | |
f7ebc6a8 | 604 | |
10069d53 EB |
605 | main_identifier_node = get_identifier ("main"); |
606 | ||
ce19ac12 | 607 | gnat_init_gcc_eh (); |
c8a23c29 EB |
608 | |
609 | /* Initialize the GCC support for FP operations. */ | |
610 | gnat_init_gcc_fp (); | |
611 | ||
612 | /* Install the builtins we might need, either internally or as user-available | |
613 | facilities for Intrinsic imports. Note that this must be done after the | |
614 | GCC exception mechanism is initialized. */ | |
10069d53 | 615 | gnat_install_builtins (); |
a1ab4c31 | 616 | |
9771b263 | 617 | vec_safe_push (gnu_except_ptr_stack, NULL_TREE); |
8f8f531f PMR |
618 | |
619 | gnu_constraint_error_label_stack.safe_push (Empty); | |
620 | gnu_storage_error_label_stack.safe_push (Empty); | |
621 | gnu_program_error_label_stack.safe_push (Empty); | |
a1ab4c31 | 622 | |
a1ab4c31 | 623 | /* Process any Pragma Ident for the main unit. */ |
a1ab4c31 | 624 | if (Present (Ident_String (Main_Unit))) |
a8781821 SB |
625 | targetm.asm_out.output_ident |
626 | (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit)))); | |
a1ab4c31 | 627 | |
c3831524 AC |
628 | /* Force -fno-strict-aliasing if the configuration pragma was seen. */ |
629 | if (No_Strict_Aliasing_CP) | |
630 | flag_strict_aliasing = 0; | |
631 | ||
c0c91386 JJ |
632 | /* Save the current optimization options again after the above possible |
633 | global_options changes. */ | |
ba948b37 JJ |
634 | optimization_default_node |
635 | = build_optimization_node (&global_options, &global_options_set); | |
c0c91386 JJ |
636 | optimization_current_node = optimization_default_node; |
637 | ||
6a7a3f31 | 638 | /* Now translate the compilation unit proper. */ |
a1ab4c31 AC |
639 | Compilation_Unit_to_gnu (gnat_root); |
640 | ||
f04b8d69 EB |
641 | /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at |
642 | the very end to avoid having to second-guess the front-end when we run | |
643 | into dummy nodes during the regular processing. */ | |
9771b263 | 644 | for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++) |
f04b8d69 | 645 | validate_unchecked_conversion (gnat_iter); |
9771b263 | 646 | gnat_validate_uc_list.release (); |
f04b8d69 | 647 | |
6a7a3f31 | 648 | /* Finally see if we have any elaboration procedures to deal with. */ |
a1ab4c31 AC |
649 | for (info = elab_info_list; info; info = info->next) |
650 | { | |
102a1631 | 651 | tree gnu_body = DECL_SAVED_TREE (info->elab_proc); |
a1ab4c31 | 652 | |
2fa03086 EB |
653 | /* We should have a BIND_EXPR but it may not have any statements in it. |
654 | If it doesn't have any, we have nothing to do except for setting the | |
655 | flag on the GNAT node. Otherwise, process the function as others. */ | |
102a1631 | 656 | tree gnu_stmts = gnu_body; |
a406865a RG |
657 | if (TREE_CODE (gnu_stmts) == BIND_EXPR) |
658 | gnu_stmts = BIND_EXPR_BODY (gnu_stmts); | |
102a1631 | 659 | if (!gnu_stmts || empty_stmt_list_p (gnu_stmts)) |
2fa03086 | 660 | Set_Has_No_Elaboration_Code (info->gnat_node, 1); |
a406865a RG |
661 | else |
662 | { | |
a406865a RG |
663 | begin_subprog_body (info->elab_proc); |
664 | end_subprog_body (gnu_body); | |
71196d4e | 665 | rest_of_subprog_body_compilation (info->elab_proc); |
a406865a | 666 | } |
a1ab4c31 AC |
667 | } |
668 | ||
f04b8d69 | 669 | /* Destroy ourselves. */ |
fa6fd7b7 | 670 | file_map = NULL; |
4116e7d0 | 671 | destroy_gnat_decl (); |
842d4ee2 | 672 | destroy_gnat_utils (); |
f04b8d69 | 673 | |
a1ab4c31 | 674 | /* We cannot track the location of errors past this point. */ |
738b83cd | 675 | Current_Error_Node = Empty; |
a1ab4c31 | 676 | } |
ce2d0ce2 | 677 | |
437f8c1e | 678 | /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given |
c1fd8753 | 679 | CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */ |
437f8c1e AC |
680 | |
681 | static tree | |
c1fd8753 | 682 | build_raise_check (int check, enum exception_info_kind kind) |
437f8c1e | 683 | { |
c1fd8753 | 684 | tree result, ftype; |
0c644c99 TG |
685 | const char pfx[] = "__gnat_rcheck_"; |
686 | ||
687 | strcpy (Name_Buffer, pfx); | |
688 | Name_Len = sizeof (pfx) - 1; | |
a6d3b499 | 689 | Get_RT_Exception_Name ((enum RT_Exception_Code) check); |
437f8c1e | 690 | |
c1fd8753 | 691 | if (kind == exception_simple) |
437f8c1e | 692 | { |
0c644c99 | 693 | Name_Buffer[Name_Len] = 0; |
c1fd8753 NF |
694 | ftype |
695 | = build_function_type_list (void_type_node, | |
825da0d2 | 696 | build_pointer_type (char_type_node), |
c1fd8753 | 697 | integer_type_node, NULL_TREE); |
437f8c1e AC |
698 | } |
699 | else | |
700 | { | |
c1fd8753 | 701 | tree t = (kind == exception_column ? NULL_TREE : integer_type_node); |
0c644c99 TG |
702 | |
703 | strcpy (Name_Buffer + Name_Len, "_ext"); | |
704 | Name_Buffer[Name_Len + 4] = 0; | |
c1fd8753 NF |
705 | ftype |
706 | = build_function_type_list (void_type_node, | |
825da0d2 | 707 | build_pointer_type (char_type_node), |
c1fd8753 NF |
708 | integer_type_node, integer_type_node, |
709 | t, t, NULL_TREE); | |
437f8c1e | 710 | } |
cfc839a4 | 711 | |
2056c5ed | 712 | /* Indicate that it never returns. */ |
1e55d29a | 713 | ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE); |
c1fd8753 | 714 | result |
1e55d29a | 715 | = create_subprog_decl (get_identifier (Name_Buffer), NULL_TREE, ftype, |
13a6dfe3 | 716 | NULL_TREE, is_default, true, true, true, false, |
ff9baa5f | 717 | false, NULL, Empty); |
c1fd8753 | 718 | |
437f8c1e AC |
719 | return result; |
720 | } | |
ce2d0ce2 | 721 | |
3cd64bab EB |
722 | /* Return a positive value if an lvalue is required for GNAT_NODE, which is |
723 | an N_Attribute_Reference. */ | |
724 | ||
725 | static int | |
726 | lvalue_required_for_attribute_p (Node_Id gnat_node) | |
727 | { | |
728 | switch (Get_Attribute_Id (Attribute_Name (gnat_node))) | |
729 | { | |
3cd64bab EB |
730 | case Attr_Pred: |
731 | case Attr_Succ: | |
732 | case Attr_First: | |
733 | case Attr_Last: | |
734 | case Attr_Range_Length: | |
735 | case Attr_Length: | |
736 | case Attr_Object_Size: | |
79069232 | 737 | case Attr_Size: |
3cd64bab EB |
738 | case Attr_Value_Size: |
739 | case Attr_Component_Size: | |
24228312 | 740 | case Attr_Descriptor_Size: |
3cd64bab EB |
741 | case Attr_Max_Size_In_Storage_Elements: |
742 | case Attr_Min: | |
743 | case Attr_Max: | |
744 | case Attr_Null_Parameter: | |
745 | case Attr_Passed_By_Reference: | |
746 | case Attr_Mechanism_Code: | |
24228312 AC |
747 | case Attr_Machine: |
748 | case Attr_Model: | |
3cd64bab EB |
749 | return 0; |
750 | ||
751 | case Attr_Address: | |
752 | case Attr_Access: | |
753 | case Attr_Unchecked_Access: | |
754 | case Attr_Unrestricted_Access: | |
755 | case Attr_Code_Address: | |
756 | case Attr_Pool_Address: | |
3cd64bab EB |
757 | case Attr_Alignment: |
758 | case Attr_Bit_Position: | |
759 | case Attr_Position: | |
760 | case Attr_First_Bit: | |
761 | case Attr_Last_Bit: | |
762 | case Attr_Bit: | |
7e4680c1 EB |
763 | case Attr_Asm_Input: |
764 | case Attr_Asm_Output: | |
3cd64bab EB |
765 | default: |
766 | return 1; | |
767 | } | |
768 | } | |
769 | ||
22d12fc2 EB |
770 | /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE |
771 | is the type that will be used for GNAT_NODE in the translated GNU tree. | |
772 | CONSTANT indicates whether the underlying object represented by GNAT_NODE | |
cb3d597d | 773 | is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates |
86da71db EB |
774 | whether its value is the address of another constant. If it isn't, then |
775 | ADDRESS_OF_CONSTANT is ignored. | |
22d12fc2 EB |
776 | |
777 | The function climbs up the GNAT tree starting from the node and returns 1 | |
778 | upon encountering a node that effectively requires an lvalue downstream. | |
779 | It returns int instead of bool to facilitate usage in non-purely binary | |
780 | logic contexts. */ | |
a1ab4c31 AC |
781 | |
782 | static int | |
03b6f8a2 | 783 | lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant, |
86da71db | 784 | bool address_of_constant) |
a1ab4c31 AC |
785 | { |
786 | Node_Id gnat_parent = Parent (gnat_node), gnat_temp; | |
787 | ||
788 | switch (Nkind (gnat_parent)) | |
789 | { | |
790 | case N_Reference: | |
791 | return 1; | |
792 | ||
793 | case N_Attribute_Reference: | |
3cd64bab | 794 | return lvalue_required_for_attribute_p (gnat_parent); |
a1ab4c31 AC |
795 | |
796 | case N_Parameter_Association: | |
797 | case N_Function_Call: | |
798 | case N_Procedure_Call_Statement: | |
1fc24649 EB |
799 | /* If the parameter is by reference, an lvalue is required. */ |
800 | return (!constant | |
801 | || must_pass_by_ref (gnu_type) | |
802 | || default_pass_by_ref (gnu_type)); | |
a1ab4c31 | 803 | |
0c6fbbfc EB |
804 | case N_Pragma_Argument_Association: |
805 | return lvalue_required_p (gnat_parent, gnu_type, constant, | |
806 | address_of_constant); | |
807 | ||
808 | case N_Pragma: | |
809 | if (Is_Pragma_Name (Chars (Pragma_Identifier (gnat_parent)))) | |
810 | { | |
811 | const unsigned char id | |
812 | = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_parent))); | |
813 | return id == Pragma_Inspection_Point; | |
814 | } | |
815 | else | |
816 | return 0; | |
817 | ||
a1ab4c31 AC |
818 | case N_Indexed_Component: |
819 | /* Only the array expression can require an lvalue. */ | |
820 | if (Prefix (gnat_parent) != gnat_node) | |
821 | return 0; | |
822 | ||
ed98eed8 EB |
823 | /* ??? Consider that referencing an indexed component with a variable |
824 | index forces the whole aggregate to memory. Note that testing only | |
825 | for literals is conservative, any static expression in the RM sense | |
826 | could probably be accepted with some additional work. */ | |
a1ab4c31 AC |
827 | for (gnat_temp = First (Expressions (gnat_parent)); |
828 | Present (gnat_temp); | |
829 | gnat_temp = Next (gnat_temp)) | |
ed98eed8 EB |
830 | if (Nkind (gnat_temp) != N_Character_Literal |
831 | && Nkind (gnat_temp) != N_Integer_Literal | |
832 | && !(Is_Entity_Name (gnat_temp) | |
833 | && Ekind (Entity (gnat_temp)) == E_Enumeration_Literal)) | |
a1ab4c31 AC |
834 | return 1; |
835 | ||
9c453de7 | 836 | /* ... fall through ... */ |
a1ab4c31 | 837 | |
527ed00b | 838 | case N_Selected_Component: |
a1ab4c31 | 839 | case N_Slice: |
527ed00b | 840 | /* Only the prefix expression can require an lvalue. */ |
a1ab4c31 AC |
841 | if (Prefix (gnat_parent) != gnat_node) |
842 | return 0; | |
843 | ||
7f6dd102 EB |
844 | return lvalue_required_p (gnat_parent, |
845 | get_unpadded_type (Etype (gnat_parent)), | |
846 | constant, address_of_constant); | |
a1ab4c31 | 847 | |
a1ab4c31 | 848 | case N_Object_Renaming_Declaration: |
e297e2ea EB |
849 | /* We need to preserve addresses through a renaming. */ |
850 | return 1; | |
a1ab4c31 | 851 | |
bbaba73f EB |
852 | case N_Object_Declaration: |
853 | /* We cannot use a constructor if this is an atomic object because | |
854 | the actual assignment might end up being done component-wise. */ | |
1fc24649 EB |
855 | return (!constant |
856 | ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node))) | |
b120ca61 | 857 | && Is_Full_Access (Defining_Entity (gnat_parent))) |
cb3d597d EB |
858 | /* We don't use a constructor if this is a class-wide object |
859 | because the effective type of the object is the equivalent | |
860 | type of the class-wide subtype and it smashes most of the | |
861 | data into an array of bytes to which we cannot convert. */ | |
862 | || Ekind ((Etype (Defining_Entity (gnat_parent)))) | |
863 | == E_Class_Wide_Subtype); | |
bbaba73f EB |
864 | |
865 | case N_Assignment_Statement: | |
866 | /* We cannot use a constructor if the LHS is an atomic object because | |
867 | the actual assignment might end up being done component-wise. */ | |
1fc24649 EB |
868 | return (!constant |
869 | || Name (gnat_parent) == gnat_node | |
03b6f8a2 | 870 | || (Is_Composite_Type (Underlying_Type (Etype (gnat_node))) |
f797c2b7 | 871 | && Is_Entity_Name (Name (gnat_parent)) |
b120ca61 | 872 | && Is_Full_Access (Entity (Name (gnat_parent))))); |
bbaba73f | 873 | |
054d6b83 EB |
874 | case N_Unchecked_Type_Conversion: |
875 | if (!constant) | |
876 | return 1; | |
76af763d | 877 | |
9c453de7 | 878 | /* ... fall through ... */ |
76af763d | 879 | |
054d6b83 EB |
880 | case N_Type_Conversion: |
881 | case N_Qualified_Expression: | |
882 | /* We must look through all conversions because we may need to bypass | |
883 | an intermediate conversion that is meant to be purely formal. */ | |
884 | return lvalue_required_p (gnat_parent, | |
885 | get_unpadded_type (Etype (gnat_parent)), | |
86da71db | 886 | constant, address_of_constant); |
cb3d597d EB |
887 | |
888 | case N_Explicit_Dereference: | |
889 | /* We look through dereferences for address of constant because we need | |
890 | to handle the special cases listed above. */ | |
891 | if (constant && address_of_constant) | |
892 | return lvalue_required_p (gnat_parent, | |
893 | get_unpadded_type (Etype (gnat_parent)), | |
86da71db | 894 | true, false); |
cb3d597d | 895 | |
9c453de7 | 896 | /* ... fall through ... */ |
22d12fc2 | 897 | |
a1ab4c31 AC |
898 | default: |
899 | return 0; | |
900 | } | |
901 | ||
902 | gcc_unreachable (); | |
903 | } | |
904 | ||
527ed00b EB |
905 | /* Return true if an lvalue should be used for GNAT_NODE. GNU_TYPE is the type |
906 | that will be used for GNAT_NODE in the translated GNU tree and is assumed to | |
907 | be an aggregate type. | |
908 | ||
909 | The function climbs up the GNAT tree starting from the node and returns true | |
910 | upon encountering a node that makes it doable to decide. lvalue_required_p | |
911 | should have been previously invoked on the arguments and returned false. */ | |
912 | ||
913 | static bool | |
914 | lvalue_for_aggregate_p (Node_Id gnat_node, tree gnu_type) | |
915 | { | |
916 | Node_Id gnat_parent = Parent (gnat_node); | |
917 | ||
918 | switch (Nkind (gnat_parent)) | |
919 | { | |
920 | case N_Parameter_Association: | |
921 | case N_Function_Call: | |
922 | case N_Procedure_Call_Statement: | |
923 | /* Even if the parameter is by copy, prefer an lvalue. */ | |
924 | return true; | |
925 | ||
6a1e04b2 EB |
926 | case N_Simple_Return_Statement: |
927 | /* Likewise for a return value. */ | |
928 | return true; | |
929 | ||
527ed00b EB |
930 | case N_Indexed_Component: |
931 | case N_Selected_Component: | |
932 | /* If an elementary component is used, take it from the constant. */ | |
933 | if (!Is_Composite_Type (Underlying_Type (Etype (gnat_parent)))) | |
934 | return false; | |
935 | ||
936 | /* ... fall through ... */ | |
937 | ||
938 | case N_Slice: | |
939 | return lvalue_for_aggregate_p (gnat_parent, | |
940 | get_unpadded_type (Etype (gnat_parent))); | |
941 | ||
942 | case N_Object_Declaration: | |
fe47e816 EB |
943 | /* For an aggregate object declaration, return false consistently. */ |
944 | return false; | |
527ed00b EB |
945 | |
946 | case N_Assignment_Statement: | |
947 | /* For an aggregate assignment, decide based on the size. */ | |
948 | { | |
949 | const HOST_WIDE_INT size = int_size_in_bytes (gnu_type); | |
950 | return size < 0 || size >= param_large_stack_frame / 4; | |
951 | } | |
952 | ||
953 | case N_Unchecked_Type_Conversion: | |
954 | case N_Type_Conversion: | |
955 | case N_Qualified_Expression: | |
956 | return lvalue_for_aggregate_p (gnat_parent, | |
957 | get_unpadded_type (Etype (gnat_parent))); | |
958 | ||
959 | case N_Allocator: | |
960 | /* We should only reach here through the N_Qualified_Expression case. | |
961 | Force an lvalue for aggregate types since a block-copy to the newly | |
962 | allocated area of memory is made. */ | |
963 | return true; | |
964 | ||
965 | default: | |
966 | return false; | |
967 | } | |
968 | ||
969 | gcc_unreachable (); | |
970 | } | |
971 | ||
972 | ||
e297e2ea EB |
973 | /* Return true if T is a constant DECL node that can be safely replaced |
974 | by its initializer. */ | |
975 | ||
976 | static bool | |
977 | constant_decl_with_initializer_p (tree t) | |
978 | { | |
979 | if (!TREE_CONSTANT (t) || !DECL_P (t) || !DECL_INITIAL (t)) | |
980 | return false; | |
981 | ||
982 | /* Return false for aggregate types that contain a placeholder since | |
983 | their initializers cannot be manipulated easily. */ | |
984 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
985 | && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t)) | |
986 | && type_contains_placeholder_p (TREE_TYPE (t))) | |
987 | return false; | |
988 | ||
989 | return true; | |
990 | } | |
991 | ||
992 | /* Return an expression equivalent to EXP but where constant DECL nodes | |
993 | have been replaced by their initializer. */ | |
994 | ||
995 | static tree | |
996 | fold_constant_decl_in_expr (tree exp) | |
997 | { | |
998 | enum tree_code code = TREE_CODE (exp); | |
999 | tree op0; | |
1000 | ||
1001 | switch (code) | |
1002 | { | |
1003 | case CONST_DECL: | |
1004 | case VAR_DECL: | |
1005 | if (!constant_decl_with_initializer_p (exp)) | |
1006 | return exp; | |
1007 | ||
1008 | return DECL_INITIAL (exp); | |
1009 | ||
e297e2ea EB |
1010 | case COMPONENT_REF: |
1011 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); | |
1012 | if (op0 == TREE_OPERAND (exp, 0)) | |
1013 | return exp; | |
1014 | ||
552cc590 EB |
1015 | return fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, |
1016 | TREE_OPERAND (exp, 1), NULL_TREE); | |
1017 | ||
1018 | case BIT_FIELD_REF: | |
1019 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); | |
1020 | if (op0 == TREE_OPERAND (exp, 0)) | |
1021 | return exp; | |
1022 | ||
1023 | return fold_build3 (BIT_FIELD_REF, TREE_TYPE (exp), op0, | |
1024 | TREE_OPERAND (exp, 1), TREE_OPERAND (exp, 2)); | |
e297e2ea EB |
1025 | |
1026 | case ARRAY_REF: | |
1027 | case ARRAY_RANGE_REF: | |
a239279e EB |
1028 | /* If the index is not itself constant, then nothing can be folded. */ |
1029 | if (!TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1030 | return exp; | |
e297e2ea EB |
1031 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); |
1032 | if (op0 == TREE_OPERAND (exp, 0)) | |
1033 | return exp; | |
1034 | ||
1035 | return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1), | |
552cc590 | 1036 | TREE_OPERAND (exp, 2), NULL_TREE)); |
e297e2ea | 1037 | |
e297e2ea EB |
1038 | case REALPART_EXPR: |
1039 | case IMAGPART_EXPR: | |
ea292448 | 1040 | case VIEW_CONVERT_EXPR: |
e297e2ea EB |
1041 | op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0)); |
1042 | if (op0 == TREE_OPERAND (exp, 0)) | |
1043 | return exp; | |
1044 | ||
1045 | return fold_build1 (code, TREE_TYPE (exp), op0); | |
1046 | ||
1047 | default: | |
1048 | return exp; | |
1049 | } | |
1050 | ||
1051 | gcc_unreachable (); | |
1052 | } | |
1053 | ||
3016ec8a EB |
1054 | /* Return true if TYPE and DEF_TYPE are compatible GNAT types for Gigi. */ |
1055 | ||
1056 | static bool | |
1057 | Gigi_Types_Compatible (Entity_Id type, Entity_Id def_type) | |
1058 | { | |
1059 | /* The trivial case. */ | |
1060 | if (type == def_type) | |
1061 | return true; | |
1062 | ||
1063 | /* A class-wide type is equivalent to a subtype of itself. */ | |
1064 | if (Is_Class_Wide_Type (type)) | |
1065 | return true; | |
1066 | ||
1067 | /* A packed array type is compatible with its implementation type. */ | |
1068 | if (Is_Packed (def_type) && type == Packed_Array_Impl_Type (def_type)) | |
1069 | return true; | |
1070 | ||
1071 | /* If both types are Itypes, one may be a copy of the other. */ | |
1072 | if (Is_Itype (def_type) && Is_Itype (type)) | |
1073 | return true; | |
1074 | ||
1075 | /* If the type is incomplete and comes from a limited context, then also | |
1076 | consider its non-limited view. */ | |
1077 | if (Is_Incomplete_Type (def_type) | |
1078 | && From_Limited_With (def_type) | |
1079 | && Present (Non_Limited_View (def_type))) | |
1080 | return Gigi_Types_Compatible (type, Non_Limited_View (def_type)); | |
1081 | ||
1082 | /* If the type is incomplete/private, then also consider its full view. */ | |
1083 | if (Is_Incomplete_Or_Private_Type (def_type) | |
1084 | && Present (Full_View (def_type))) | |
1085 | return Gigi_Types_Compatible (type, Full_View (def_type)); | |
1086 | ||
1087 | return false; | |
1088 | } | |
1089 | ||
ef12e74c EB |
1090 | /* Return the full view of a private constant E, or of a renaming thereof, if |
1091 | its type has discriminants, and Empty otherwise. */ | |
1092 | ||
1093 | static Entity_Id | |
1094 | Full_View_Of_Private_Constant (Entity_Id E) | |
1095 | { | |
1096 | while (Present (Renamed_Object (E)) && Is_Entity_Name (Renamed_Object (E))) | |
1097 | E = Entity (Renamed_Object (E)); | |
1098 | ||
1099 | if (Ekind (E) != E_Constant || No (Full_View (E))) | |
1100 | return Empty; | |
1101 | ||
1102 | const Entity_Id T = Etype (E); | |
1103 | ||
1104 | if (Is_Private_Type (T) | |
1105 | && (Has_Unknown_Discriminants (T) | |
1106 | || (Present (Full_View (T)) && Has_Discriminants (Full_View (T))))) | |
1107 | return Full_View (E); | |
1108 | ||
1109 | return Empty; | |
1110 | } | |
1111 | ||
e08f1aad EB |
1112 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Identifier, to a GCC |
1113 | tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we should | |
1114 | place the result type. */ | |
a1ab4c31 AC |
1115 | |
1116 | static tree | |
1117 | Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p) | |
1118 | { | |
ef12e74c EB |
1119 | Entity_Id gnat_entity = (Nkind (gnat_node) == N_Defining_Identifier |
1120 | || Nkind (gnat_node) == N_Defining_Operator_Symbol) | |
1121 | ? gnat_node : Entity (gnat_node); | |
1122 | Entity_Id gnat_result_type; | |
1123 | tree gnu_result, gnu_result_type; | |
a1ab4c31 AC |
1124 | /* If GNAT_NODE is a constant, whether we should use the initialization |
1125 | value instead of the constant entity, typically for scalars with an | |
1126 | address clause when the parent doesn't require an lvalue. */ | |
ef12e74c | 1127 | bool use_constant_initializer; |
3016ec8a EB |
1128 | /* Whether we should require an lvalue for GNAT_NODE. Needed in |
1129 | specific circumstances only, so evaluated lazily. < 0 means | |
1130 | unknown, > 0 means known true, 0 means known false. */ | |
ef12e74c | 1131 | int require_lvalue; |
a1ab4c31 | 1132 | |
1e55d29a EB |
1133 | /* If the Etype of this node is not the same as that of the Entity, then |
1134 | something went wrong, probably in generic instantiation. However, this | |
1135 | does not apply to types. Since we sometime have strange Ekind's, just | |
3016ec8a EB |
1136 | do this test for objects, except for discriminants because their type |
1137 | may have been changed to a subtype by Exp_Ch3.Adjust_Discriminants. */ | |
1138 | gcc_assert (!Is_Object (gnat_entity) | |
1139 | || Ekind (gnat_entity) == E_Discriminant | |
ef12e74c EB |
1140 | || Etype (gnat_node) == Etype (gnat_entity) |
1141 | || Gigi_Types_Compatible (Etype (gnat_node), | |
1142 | Etype (gnat_entity))); | |
a1ab4c31 | 1143 | |
ef12e74c | 1144 | /* If this is a reference to a deferred constant whose partial view is of |
a1ab4c31 | 1145 | unconstrained private type, the proper type is on the full view of the |
ef12e74c EB |
1146 | constant, not on the full view of the type which may be unconstrained. */ |
1147 | const Entity_Id gnat_full_view = Full_View_Of_Private_Constant (gnat_entity); | |
1148 | if (Present (gnat_full_view)) | |
a1ab4c31 | 1149 | { |
ef12e74c | 1150 | gnat_entity = gnat_full_view; |
3016ec8a | 1151 | gnat_result_type = Etype (gnat_entity); |
a1ab4c31 AC |
1152 | } |
1153 | else | |
1154 | { | |
eb0f4e48 EB |
1155 | /* We use the Actual_Subtype only if it has already been elaborated, |
1156 | as we may be invoked precisely during its elaboration, otherwise | |
79069232 EB |
1157 | the Etype. Avoid using it for packed arrays to simplify things, |
1158 | except in a return statement because we need the actual size and | |
1159 | the front-end does not make it explicit in this case. */ | |
3016ec8a | 1160 | if ((Ekind (gnat_entity) == E_Constant |
eb0f4e48 EB |
1161 | || Ekind (gnat_entity) == E_Variable |
1162 | || Is_Formal (gnat_entity)) | |
3016ec8a | 1163 | && !(Is_Array_Type (Etype (gnat_entity)) |
79069232 EB |
1164 | && Present (Packed_Array_Impl_Type (Etype (gnat_entity))) |
1165 | && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement) | |
3016ec8a EB |
1166 | && Present (Actual_Subtype (gnat_entity)) |
1167 | && present_gnu_tree (Actual_Subtype (gnat_entity))) | |
1168 | gnat_result_type = Actual_Subtype (gnat_entity); | |
a1ab4c31 | 1169 | else |
3016ec8a | 1170 | gnat_result_type = Etype (gnat_node); |
a1ab4c31 AC |
1171 | } |
1172 | ||
59a6274d EB |
1173 | /* Expand the type of this identifier first if it is needed, in case it is an |
1174 | enumeral literal, which only get made when the type is expanded. There is | |
1175 | no order-of-elaboration issue here. */ | |
1176 | if (Is_Subprogram (gnat_entity)) | |
1177 | gnu_result_type = NULL_TREE; | |
1178 | else | |
1179 | gnu_result_type = get_unpadded_type (gnat_result_type); | |
a1ab4c31 | 1180 | |
e4270465 | 1181 | /* If this is a non-imported elementary constant with an address clause, |
a1ab4c31 AC |
1182 | retrieve the value instead of a pointer to be dereferenced unless |
1183 | an lvalue is required. This is generally more efficient and actually | |
1184 | required if this is a static expression because it might be used | |
1185 | in a context where a dereference is inappropriate, such as a case | |
1186 | statement alternative or a record discriminant. There is no possible | |
308e6f3a | 1187 | volatile-ness short-circuit here since Volatile constants must be |
1e17ef87 | 1188 | imported per C.6. */ |
3016ec8a EB |
1189 | if (Ekind (gnat_entity) == E_Constant |
1190 | && Is_Elementary_Type (gnat_result_type) | |
1191 | && !Is_Imported (gnat_entity) | |
1192 | && Present (Address_Clause (gnat_entity))) | |
a1ab4c31 | 1193 | { |
86da71db EB |
1194 | require_lvalue |
1195 | = lvalue_required_p (gnat_node, gnu_result_type, true, false); | |
a1ab4c31 AC |
1196 | use_constant_initializer = !require_lvalue; |
1197 | } | |
ef12e74c EB |
1198 | else |
1199 | { | |
1200 | require_lvalue = -1; | |
1201 | use_constant_initializer = false; | |
1202 | } | |
a1ab4c31 | 1203 | |
ef12e74c | 1204 | /* Fetch the initialization value of a constant if requested. */ |
a1ab4c31 AC |
1205 | if (use_constant_initializer) |
1206 | { | |
1207 | /* If this is a deferred constant, the initializer is attached to | |
1208 | the full view. */ | |
3016ec8a EB |
1209 | if (Present (Full_View (gnat_entity))) |
1210 | gnat_entity = Full_View (gnat_entity); | |
a1ab4c31 | 1211 | |
3016ec8a | 1212 | gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_entity))); |
a1ab4c31 AC |
1213 | } |
1214 | else | |
3016ec8a | 1215 | gnu_result = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
a1ab4c31 | 1216 | |
a1ab4c31 AC |
1217 | /* Some objects (such as parameters passed by reference, globals of |
1218 | variable size, and renamed objects) actually represent the address | |
1219 | of the object. In that case, we must do the dereference. Likewise, | |
1220 | deal with parameters to foreign convention subprograms. */ | |
1221 | if (DECL_P (gnu_result) | |
1222 | && (DECL_BY_REF_P (gnu_result) | |
1223 | || (TREE_CODE (gnu_result) == PARM_DECL | |
1224 | && DECL_BY_COMPONENT_PTR_P (gnu_result)))) | |
1225 | { | |
ced57283 | 1226 | const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result); |
a1ab4c31 | 1227 | |
ad1d36ba | 1228 | /* If it's a PARM_DECL to foreign convention subprogram, convert it. */ |
a1ab4c31 AC |
1229 | if (TREE_CODE (gnu_result) == PARM_DECL |
1230 | && DECL_BY_COMPONENT_PTR_P (gnu_result)) | |
ad1d36ba EB |
1231 | gnu_result |
1232 | = convert (build_pointer_type (gnu_result_type), gnu_result); | |
1233 | ||
1234 | /* If it's a CONST_DECL, return the underlying constant like below. */ | |
e4270465 EB |
1235 | else if (TREE_CODE (gnu_result) == CONST_DECL |
1236 | && !(DECL_CONST_ADDRESS_P (gnu_result) | |
1237 | && lvalue_required_p (gnat_node, gnu_result_type, true, | |
86da71db | 1238 | true))) |
ad1d36ba | 1239 | gnu_result = DECL_INITIAL (gnu_result); |
a1ab4c31 | 1240 | |
5bdd063b EB |
1241 | /* Do the final dereference. */ |
1242 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result); | |
a1ab4c31 | 1243 | |
5bdd063b EB |
1244 | if ((TREE_CODE (gnu_result) == INDIRECT_REF |
1245 | || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF) | |
1246 | && No (Address_Clause (gnat_entity))) | |
1247 | TREE_THIS_NOTRAP (gnu_result) = 1; | |
ad1d36ba | 1248 | |
5bdd063b EB |
1249 | if (read_only) |
1250 | TREE_READONLY (gnu_result) = 1; | |
a1ab4c31 AC |
1251 | } |
1252 | ||
58c8f770 EB |
1253 | /* If we have a constant declaration and its initializer, try to return the |
1254 | latter to avoid the need to call fold in lots of places and the need for | |
e297e2ea EB |
1255 | elaboration code if this identifier is used as an initializer itself. */ |
1256 | if (constant_decl_with_initializer_p (gnu_result)) | |
a1ab4c31 | 1257 | { |
c34f3839 EB |
1258 | bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL |
1259 | && !DECL_CONST_CORRESPONDING_VAR (gnu_result)); | |
cb3d597d EB |
1260 | bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL |
1261 | && DECL_CONST_ADDRESS_P (gnu_result)); | |
1262 | ||
1263 | /* If there is a (corresponding) variable or this is the address of a | |
1264 | constant, we only want to return the initializer if an lvalue isn't | |
1265 | required. Evaluate this now if we have not already done so. */ | |
1266 | if ((!constant_only || address_of_constant) && require_lvalue < 0) | |
1267 | require_lvalue | |
1268 | = lvalue_required_p (gnat_node, gnu_result_type, true, | |
527ed00b EB |
1269 | address_of_constant) |
1270 | || (AGGREGATE_TYPE_P (gnu_result_type) | |
1271 | && lvalue_for_aggregate_p (gnat_node, gnu_result_type)); | |
cb3d597d | 1272 | |
6ba4f08f | 1273 | /* Finally retrieve the initializer if this is deemed valid. */ |
cb3d597d | 1274 | if ((constant_only && !address_of_constant) || !require_lvalue) |
6ba4f08f | 1275 | gnu_result = DECL_INITIAL (gnu_result); |
a1ab4c31 AC |
1276 | } |
1277 | ||
e297e2ea EB |
1278 | /* But for a constant renaming we couldn't do that incrementally for its |
1279 | definition because of the need to return an lvalue so, if the present | |
1280 | context doesn't itself require an lvalue, we try again here. */ | |
3016ec8a EB |
1281 | else if (Ekind (gnat_entity) == E_Constant |
1282 | && Is_Elementary_Type (gnat_result_type) | |
1283 | && Present (Renamed_Object (gnat_entity))) | |
e297e2ea EB |
1284 | { |
1285 | if (require_lvalue < 0) | |
1286 | require_lvalue | |
86da71db | 1287 | = lvalue_required_p (gnat_node, gnu_result_type, true, false); |
e297e2ea EB |
1288 | if (!require_lvalue) |
1289 | gnu_result = fold_constant_decl_in_expr (gnu_result); | |
1290 | } | |
1291 | ||
1c4ae4e5 EB |
1292 | /* The GNAT tree has the type of a function set to its result type, so we |
1293 | adjust here. Also use the type of the result if the Etype is a subtype | |
1294 | that is nominally unconstrained. Likewise if this is a deferred constant | |
1295 | of a discriminated type whose full view can be elaborated statically, to | |
1296 | avoid problematic conversions to the nominal subtype. But remove any | |
1297 | padding from the resulting type. */ | |
69720717 | 1298 | if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_result)) |
3016ec8a EB |
1299 | || Is_Constr_Subt_For_UN_Aliased (gnat_result_type) |
1300 | || (Ekind (gnat_entity) == E_Constant | |
1301 | && Present (Full_View (gnat_entity)) | |
1302 | && Has_Discriminants (gnat_result_type) | |
1c4ae4e5 EB |
1303 | && TREE_CODE (gnu_result) == CONSTRUCTOR)) |
1304 | { | |
1305 | gnu_result_type = TREE_TYPE (gnu_result); | |
1306 | if (TYPE_IS_PADDING_P (gnu_result_type)) | |
1307 | gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type)); | |
1308 | } | |
1309 | ||
a1ab4c31 | 1310 | *gnu_result_type_p = gnu_result_type; |
58c8f770 | 1311 | |
a1ab4c31 AC |
1312 | return gnu_result; |
1313 | } | |
e76a8cac | 1314 | |
e08f1aad EB |
1315 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Pragma, to a GCC |
1316 | tree, which is returned. */ | |
a1ab4c31 AC |
1317 | |
1318 | static tree | |
1319 | Pragma_to_gnu (Node_Id gnat_node) | |
1320 | { | |
47625858 | 1321 | tree gnu_result = alloc_stmt_list (); |
f2423384 | 1322 | Node_Id gnat_temp; |
a1ab4c31 | 1323 | |
ec4a0d83 EB |
1324 | /* Check for (and ignore) unrecognized pragmas. */ |
1325 | if (!Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node)))) | |
a1ab4c31 AC |
1326 | return gnu_result; |
1327 | ||
ec4a0d83 EB |
1328 | const unsigned char id |
1329 | = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node))); | |
1330 | ||
1331 | /* Save the expression of pragma Compile_Time_{Error|Warning} for later. */ | |
1332 | if (id == Pragma_Compile_Time_Error || id == Pragma_Compile_Time_Warning) | |
1333 | { | |
1334 | gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1335 | gnat_compile_time_expr_list.safe_push (Expression (gnat_temp)); | |
1336 | return gnu_result; | |
1337 | } | |
1338 | ||
1339 | /* Stop there if we are just annotating types. */ | |
1340 | if (type_annotate_only) | |
1341 | return gnu_result; | |
1342 | ||
1343 | switch (id) | |
a1ab4c31 AC |
1344 | { |
1345 | case Pragma_Inspection_Point: | |
1346 | /* Do nothing at top level: all such variables are already viewable. */ | |
1347 | if (global_bindings_p ()) | |
1348 | break; | |
1349 | ||
1350 | for (gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1351 | Present (gnat_temp); | |
1352 | gnat_temp = Next (gnat_temp)) | |
1353 | { | |
1354 | Node_Id gnat_expr = Expression (gnat_temp); | |
1355 | tree gnu_expr = gnat_to_gnu (gnat_expr); | |
a1ab4c31 AC |
1356 | tree asm_constraint = NULL_TREE; |
1357 | #ifdef ASM_COMMENT_START | |
1358 | char *comment; | |
1359 | #endif | |
9e4cacfa | 1360 | gnu_expr = maybe_unconstrained_array (gnu_expr); |
0c6fbbfc EB |
1361 | if (TREE_CODE (gnu_expr) == CONST_DECL |
1362 | && DECL_CONST_CORRESPONDING_VAR (gnu_expr)) | |
1363 | gnu_expr = DECL_CONST_CORRESPONDING_VAR (gnu_expr); | |
9e4cacfa | 1364 | gnat_mark_addressable (gnu_expr); |
a1ab4c31 AC |
1365 | |
1366 | #ifdef ASM_COMMENT_START | |
1367 | comment = concat (ASM_COMMENT_START, | |
1368 | " inspection point: ", | |
1369 | Get_Name_String (Chars (gnat_expr)), | |
9e4cacfa | 1370 | " is at %0", |
a1ab4c31 AC |
1371 | NULL); |
1372 | asm_constraint = build_string (strlen (comment), comment); | |
1373 | free (comment); | |
1374 | #endif | |
1c384bf1 | 1375 | gnu_expr = build5 (ASM_EXPR, void_type_node, |
a1ab4c31 AC |
1376 | asm_constraint, |
1377 | NULL_TREE, | |
1378 | tree_cons | |
1379 | (build_tree_list (NULL_TREE, | |
9e4cacfa EB |
1380 | build_string (1, "m")), |
1381 | gnu_expr, NULL_TREE), | |
1c384bf1 | 1382 | NULL_TREE, NULL_TREE); |
a1ab4c31 AC |
1383 | ASM_VOLATILE_P (gnu_expr) = 1; |
1384 | set_expr_location_from_node (gnu_expr, gnat_node); | |
1385 | append_to_statement_list (gnu_expr, &gnu_result); | |
1386 | } | |
1387 | break; | |
1388 | ||
633a3f2a EB |
1389 | case Pragma_Loop_Optimize: |
1390 | for (gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1391 | Present (gnat_temp); | |
1392 | gnat_temp = Next (gnat_temp)) | |
1393 | { | |
3418f5e9 | 1394 | tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt; |
633a3f2a EB |
1395 | |
1396 | switch (Chars (Expression (gnat_temp))) | |
1397 | { | |
3418f5e9 EB |
1398 | case Name_Ivdep: |
1399 | LOOP_STMT_IVDEP (gnu_loop_stmt) = 1; | |
1400 | break; | |
1401 | ||
633a3f2a EB |
1402 | case Name_No_Unroll: |
1403 | LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1; | |
1404 | break; | |
1405 | ||
1406 | case Name_Unroll: | |
1407 | LOOP_STMT_UNROLL (gnu_loop_stmt) = 1; | |
1408 | break; | |
1409 | ||
1410 | case Name_No_Vector: | |
1411 | LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1; | |
1412 | break; | |
1413 | ||
1414 | case Name_Vector: | |
1415 | LOOP_STMT_VECTOR (gnu_loop_stmt) = 1; | |
1416 | break; | |
1417 | ||
1418 | default: | |
1419 | gcc_unreachable (); | |
1420 | } | |
1421 | } | |
1422 | break; | |
1423 | ||
a1ab4c31 AC |
1424 | case Pragma_Optimize: |
1425 | switch (Chars (Expression | |
1426 | (First (Pragma_Argument_Associations (gnat_node))))) | |
1427 | { | |
a1ab4c31 | 1428 | case Name_Off: |
e84319a3 | 1429 | if (optimize) |
4a29b8d6 | 1430 | post_error ("must specify -O0??", gnat_node); |
a1ab4c31 AC |
1431 | break; |
1432 | ||
257e81a6 EB |
1433 | case Name_Space: |
1434 | if (!optimize_size) | |
4a29b8d6 | 1435 | post_error ("must specify -Os??", gnat_node); |
257e81a6 EB |
1436 | break; |
1437 | ||
1438 | case Name_Time: | |
1439 | if (!optimize) | |
4a29b8d6 | 1440 | post_error ("insufficient -O value??", gnat_node); |
257e81a6 EB |
1441 | break; |
1442 | ||
a1ab4c31 AC |
1443 | default: |
1444 | gcc_unreachable (); | |
1445 | } | |
1446 | break; | |
1447 | ||
1448 | case Pragma_Reviewable: | |
1449 | if (write_symbols == NO_DEBUG) | |
4a29b8d6 | 1450 | post_error ("must specify -g??", gnat_node); |
a1ab4c31 | 1451 | break; |
f2423384 | 1452 | |
2749b01b | 1453 | case Pragma_Warning_As_Error: |
f2423384 EB |
1454 | case Pragma_Warnings: |
1455 | { | |
1456 | Node_Id gnat_expr; | |
1457 | /* Preserve the location of the pragma. */ | |
1458 | const location_t location = input_location; | |
1459 | struct cl_option_handlers handlers; | |
1460 | unsigned int option_index; | |
1461 | diagnostic_t kind; | |
1462 | bool imply; | |
1463 | ||
1464 | gnat_temp = First (Pragma_Argument_Associations (gnat_node)); | |
1465 | ||
2749b01b | 1466 | /* This is the String form: pragma Warning{s|_As_Error}(String). */ |
f2423384 EB |
1467 | if (Nkind (Expression (gnat_temp)) == N_String_Literal) |
1468 | { | |
ec4a0d83 | 1469 | switch (id) |
2749b01b EB |
1470 | { |
1471 | case Pragma_Warning_As_Error: | |
1472 | kind = DK_ERROR; | |
1473 | imply = false; | |
1474 | break; | |
1475 | ||
1476 | case Pragma_Warnings: | |
1477 | kind = DK_WARNING; | |
1478 | imply = true; | |
1479 | break; | |
1480 | ||
1481 | default: | |
1482 | gcc_unreachable (); | |
1483 | } | |
1484 | ||
f2423384 | 1485 | gnat_expr = Expression (gnat_temp); |
f2423384 EB |
1486 | } |
1487 | ||
1488 | /* This is the On/Off form: pragma Warnings (On | Off [,String]). */ | |
1489 | else if (Nkind (Expression (gnat_temp)) == N_Identifier) | |
1490 | { | |
1491 | switch (Chars (Expression (gnat_temp))) | |
1492 | { | |
1493 | case Name_Off: | |
1494 | kind = DK_IGNORED; | |
1495 | break; | |
1496 | ||
1497 | case Name_On: | |
1498 | kind = DK_WARNING; | |
1499 | break; | |
1500 | ||
1501 | default: | |
1502 | gcc_unreachable (); | |
1503 | } | |
1504 | ||
f10ff6cc | 1505 | /* Deal with optional pattern (but ignore Reason => "..."). */ |
113c69ff EB |
1506 | if (Present (Next (gnat_temp)) |
1507 | && Chars (Next (gnat_temp)) != Name_Reason) | |
f2423384 EB |
1508 | { |
1509 | /* pragma Warnings (On | Off, Name) is handled differently. */ | |
1510 | if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal) | |
1511 | break; | |
1512 | ||
1513 | gnat_expr = Expression (Next (gnat_temp)); | |
1514 | } | |
1515 | else | |
e3371908 EB |
1516 | { |
1517 | gnat_expr = Empty; | |
1518 | ||
1519 | /* For pragma Warnings (Off), we save the current state... */ | |
1520 | if (kind == DK_IGNORED) | |
1521 | diagnostic_push_diagnostics (global_dc, location); | |
1522 | ||
1523 | /* ...so that, for pragma Warnings (On), we do not enable all | |
1524 | the warnings but just restore the previous state. */ | |
1525 | else | |
1526 | { | |
1527 | diagnostic_pop_diagnostics (global_dc, location); | |
1528 | break; | |
1529 | } | |
1530 | } | |
f2423384 EB |
1531 | |
1532 | imply = false; | |
1533 | } | |
1534 | ||
1535 | else | |
1536 | gcc_unreachable (); | |
1537 | ||
1538 | /* This is the same implementation as in the C family of compilers. */ | |
0cec1c2d | 1539 | const unsigned int lang_mask = CL_Ada | CL_COMMON; |
63bbf46d | 1540 | const char *arg = NULL; |
f2423384 EB |
1541 | if (Present (gnat_expr)) |
1542 | { | |
1543 | tree gnu_expr = gnat_to_gnu (gnat_expr); | |
0cec1c2d | 1544 | const char *option_string = TREE_STRING_POINTER (gnu_expr); |
f2423384 | 1545 | const int len = TREE_STRING_LENGTH (gnu_expr); |
0cec1c2d | 1546 | if (len < 3 || option_string[0] != '-' || option_string[1] != 'W') |
f2423384 | 1547 | break; |
0cec1c2d EB |
1548 | option_index = find_opt (option_string + 1, lang_mask); |
1549 | if (option_index == OPT_SPECIAL_unknown) | |
1550 | { | |
4a29b8d6 | 1551 | post_error ("unknown -W switch??", gnat_node); |
f2423384 | 1552 | break; |
0cec1c2d EB |
1553 | } |
1554 | else if (!(cl_options[option_index].flags & CL_WARNING)) | |
1555 | { | |
4a29b8d6 | 1556 | post_error ("-W switch does not control warning??", gnat_node); |
0cec1c2d EB |
1557 | break; |
1558 | } | |
1559 | else if (!(cl_options[option_index].flags & lang_mask)) | |
f2423384 | 1560 | { |
4a29b8d6 | 1561 | post_error ("-W switch not valid for Ada??", gnat_node); |
f2423384 EB |
1562 | break; |
1563 | } | |
63bbf46d JJ |
1564 | if (cl_options[option_index].flags & CL_JOINED) |
1565 | arg = option_string + 1 + cl_options[option_index].opt_len; | |
f2423384 EB |
1566 | } |
1567 | else | |
1568 | option_index = 0; | |
1569 | ||
0694da45 | 1570 | set_default_handlers (&handlers, NULL); |
63bbf46d | 1571 | control_warning_option (option_index, (int) kind, arg, imply, location, |
0cec1c2d | 1572 | lang_mask, &handlers, &global_options, |
f2423384 EB |
1573 | &global_options_set, global_dc); |
1574 | } | |
1575 | break; | |
633a3f2a EB |
1576 | |
1577 | default: | |
1578 | break; | |
a1ab4c31 AC |
1579 | } |
1580 | ||
1581 | return gnu_result; | |
1582 | } | |
ce2d0ce2 | 1583 | |
ae5a77fa | 1584 | /* Check the inline status of nested function FNDECL wrt its parent function. |
87411e95 EB |
1585 | |
1586 | If a non-inline nested function is referenced from an inline external | |
1587 | function, we cannot honor both requests at the same time without cloning | |
1588 | the nested function in the current unit since it is private to its unit. | |
1589 | We could inline it as well but it's probably better to err on the side | |
1590 | of too little inlining. | |
1591 | ||
ae5a77fa | 1592 | This must be done only on nested functions present in the source code |
87411e95 | 1593 | and not on nested functions generated by the compiler, e.g. finalizers, |
ae5a77fa EB |
1594 | because they may be not marked inline and we don't want them to block |
1595 | the inlining of the parent function. */ | |
87411e95 EB |
1596 | |
1597 | static void | |
1598 | check_inlining_for_nested_subprog (tree fndecl) | |
1599 | { | |
ae5a77fa EB |
1600 | if (DECL_IGNORED_P (current_function_decl) || DECL_IGNORED_P (fndecl)) |
1601 | return; | |
1602 | ||
1603 | if (DECL_DECLARED_INLINE_P (fndecl)) | |
1604 | return; | |
1605 | ||
1606 | tree parent_decl = decl_function_context (fndecl); | |
1607 | if (DECL_EXTERNAL (parent_decl) && DECL_DECLARED_INLINE_P (parent_decl)) | |
87411e95 EB |
1608 | { |
1609 | const location_t loc1 = DECL_SOURCE_LOCATION (fndecl); | |
ae5a77fa | 1610 | const location_t loc2 = DECL_SOURCE_LOCATION (parent_decl); |
87411e95 | 1611 | |
ae5a77fa | 1612 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (parent_decl))) |
87411e95 | 1613 | { |
a9c697b8 MS |
1614 | error_at (loc1, "subprogram %q+F not marked %<Inline_Always%>", |
1615 | fndecl); | |
87411e95 EB |
1616 | error_at (loc2, "parent subprogram cannot be inlined"); |
1617 | } | |
1618 | else | |
1619 | { | |
a9c697b8 | 1620 | warning_at (loc1, OPT_Winline, "subprogram %q+F not marked %<Inline%>", |
87411e95 EB |
1621 | fndecl); |
1622 | warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined"); | |
1623 | } | |
1624 | ||
ae5a77fa EB |
1625 | DECL_DECLARED_INLINE_P (parent_decl) = 0; |
1626 | DECL_UNINLINABLE (parent_decl) = 1; | |
87411e95 EB |
1627 | } |
1628 | } | |
ce2d0ce2 | 1629 | |
1eb58520 AC |
1630 | /* Return an expression for the length of TYPE, an integral type, computed in |
1631 | RESULT_TYPE, another integral type. | |
1632 | ||
1633 | We used to compute the length as MAX (hb - lb + 1, 0) which could overflow | |
1634 | when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0 | |
1635 | which would only overflow in much rarer cases, for extremely large arrays | |
1636 | we expect never to encounter in practice. Besides, the former computation | |
1637 | required the use of potentially constraining signed arithmetics while the | |
1638 | latter does not. Note that the comparison must be done in the original | |
1639 | base index type in order to avoid any overflow during the conversion. */ | |
1640 | ||
1641 | static tree | |
1642 | get_type_length (tree type, tree result_type) | |
1643 | { | |
1644 | tree comp_type = get_base_type (result_type); | |
825da0d2 | 1645 | tree base_type = maybe_character_type (get_base_type (type)); |
1eb58520 AC |
1646 | tree lb = convert (base_type, TYPE_MIN_VALUE (type)); |
1647 | tree hb = convert (base_type, TYPE_MAX_VALUE (type)); | |
1648 | tree length | |
1649 | = build_binary_op (PLUS_EXPR, comp_type, | |
1650 | build_binary_op (MINUS_EXPR, comp_type, | |
1651 | convert (comp_type, hb), | |
1652 | convert (comp_type, lb)), | |
9a1bdc31 | 1653 | build_int_cst (comp_type, 1)); |
1eb58520 AC |
1654 | length |
1655 | = build_cond_expr (result_type, | |
1656 | build_binary_op (GE_EXPR, boolean_type_node, hb, lb), | |
1657 | convert (result_type, length), | |
9a1bdc31 | 1658 | build_int_cst (result_type, 0)); |
1eb58520 AC |
1659 | return length; |
1660 | } | |
1661 | ||
e08f1aad EB |
1662 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node, to a |
1663 | GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we | |
1664 | should place the result type. ATTRIBUTE is the attribute ID. */ | |
a1ab4c31 AC |
1665 | |
1666 | static tree | |
1667 | Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute) | |
1668 | { | |
088d3b0f | 1669 | const Node_Id gnat_prefix = Prefix (gnat_node); |
1e55d29a EB |
1670 | tree gnu_prefix = gnat_to_gnu (gnat_prefix); |
1671 | tree gnu_type = TREE_TYPE (gnu_prefix); | |
1672 | tree gnu_expr, gnu_result_type, gnu_result = error_mark_node; | |
caa9d12a | 1673 | bool prefix_unused = false; |
a1ab4c31 AC |
1674 | |
1675 | /* If the input is a NULL_EXPR, make a new one. */ | |
1676 | if (TREE_CODE (gnu_prefix) == NULL_EXPR) | |
1677 | { | |
feec4372 EB |
1678 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
1679 | *gnu_result_type_p = gnu_result_type; | |
1680 | return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0)); | |
a1ab4c31 AC |
1681 | } |
1682 | ||
1683 | switch (attribute) | |
1684 | { | |
a1ab4c31 AC |
1685 | case Attr_Pred: |
1686 | case Attr_Succ: | |
feec4372 EB |
1687 | /* These just add or subtract the constant 1 since representation |
1688 | clauses for enumeration types are handled in the front-end. */ | |
a1ab4c31 AC |
1689 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); |
1690 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
825da0d2 EB |
1691 | gnu_type = maybe_character_type (gnu_result_type); |
1692 | if (TREE_TYPE (gnu_expr) != gnu_type) | |
1693 | gnu_expr = convert (gnu_type, gnu_expr); | |
a1ab4c31 | 1694 | gnu_result |
feec4372 | 1695 | = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR, |
825da0d2 | 1696 | gnu_type, gnu_expr, build_int_cst (gnu_type, 1)); |
a1ab4c31 AC |
1697 | break; |
1698 | ||
1699 | case Attr_Address: | |
1700 | case Attr_Unrestricted_Access: | |
3fd9ae96 EB |
1701 | /* Conversions don't change the address of references but can cause |
1702 | build_unary_op to miss the references below, so strip them off. | |
1703 | On the contrary, if the address-of operation causes a temporary | |
1704 | to be created, then it must be created with the proper type. */ | |
1705 | gnu_expr = remove_conversions (gnu_prefix, | |
1706 | !Must_Be_Byte_Aligned (gnat_node)); | |
1707 | if (REFERENCE_CLASS_P (gnu_expr)) | |
1708 | gnu_prefix = gnu_expr; | |
a1ab4c31 AC |
1709 | |
1710 | /* If we are taking 'Address of an unconstrained object, this is the | |
1711 | pointer to the underlying array. */ | |
1712 | if (attribute == Attr_Address) | |
1713 | gnu_prefix = maybe_unconstrained_array (gnu_prefix); | |
1714 | ||
1715 | /* If we are building a static dispatch table, we have to honor | |
1716 | TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible | |
1717 | with the C++ ABI. We do it in the non-static case as well, | |
1718 | see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */ | |
1719 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
1720 | && Is_Dispatch_Table_Entity (Etype (gnat_node))) | |
1721 | { | |
0e228dd9 | 1722 | tree gnu_field, t; |
a1ab4c31 AC |
1723 | /* Descriptors can only be built here for top-level functions. */ |
1724 | bool build_descriptor = (global_bindings_p () != 0); | |
1725 | int i; | |
9771b263 | 1726 | vec<constructor_elt, va_gc> *gnu_vec = NULL; |
0e228dd9 | 1727 | constructor_elt *elt; |
a1ab4c31 AC |
1728 | |
1729 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
1730 | ||
1731 | /* If we're not going to build the descriptor, we have to retrieve | |
1732 | the one which will be built by the linker (or by the compiler | |
1733 | later if a static chain is requested). */ | |
1734 | if (!build_descriptor) | |
1735 | { | |
1736 | gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix); | |
1737 | gnu_result = fold_convert (build_pointer_type (gnu_result_type), | |
1738 | gnu_result); | |
1739 | gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result); | |
1740 | } | |
1741 | ||
cb3874dc | 1742 | vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS, true); |
9771b263 | 1743 | elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1); |
a1ab4c31 AC |
1744 | for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0; |
1745 | i < TARGET_VTABLE_USES_DESCRIPTORS; | |
7d76717d | 1746 | gnu_field = DECL_CHAIN (gnu_field), i++) |
a1ab4c31 AC |
1747 | { |
1748 | if (build_descriptor) | |
1749 | { | |
1750 | t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix, | |
1751 | build_int_cst (NULL_TREE, i)); | |
1752 | TREE_CONSTANT (t) = 1; | |
1753 | } | |
1754 | else | |
1755 | t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result, | |
1756 | gnu_field, NULL_TREE); | |
1757 | ||
0e228dd9 NF |
1758 | elt->index = gnu_field; |
1759 | elt->value = t; | |
1760 | elt--; | |
a1ab4c31 AC |
1761 | } |
1762 | ||
0e228dd9 | 1763 | gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec); |
a1ab4c31 AC |
1764 | break; |
1765 | } | |
1766 | ||
9c453de7 | 1767 | /* ... fall through ... */ |
a1ab4c31 AC |
1768 | |
1769 | case Attr_Access: | |
1770 | case Attr_Unchecked_Access: | |
1771 | case Attr_Code_Address: | |
a5720c08 EB |
1772 | /* Taking the address of a type does not make sense. */ |
1773 | gcc_assert (TREE_CODE (gnu_prefix) != TYPE_DECL); | |
1774 | ||
a1ab4c31 AC |
1775 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
1776 | gnu_result | |
1777 | = build_unary_op (((attribute == Attr_Address | |
1778 | || attribute == Attr_Unrestricted_Access) | |
1779 | && !Must_Be_Byte_Aligned (gnat_node)) | |
1780 | ? ATTR_ADDR_EXPR : ADDR_EXPR, | |
1781 | gnu_result_type, gnu_prefix); | |
1782 | ||
1783 | /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we | |
1e039275 | 1784 | don't try to build a trampoline. */ |
a1ab4c31 AC |
1785 | if (attribute == Attr_Code_Address) |
1786 | { | |
722356ce | 1787 | gnu_expr = remove_conversions (gnu_result, false); |
a1ab4c31 AC |
1788 | |
1789 | if (TREE_CODE (gnu_expr) == ADDR_EXPR) | |
1790 | TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1; | |
4c640e26 EB |
1791 | |
1792 | /* On targets for which function symbols denote a descriptor, the | |
1793 | code address is stored within the first slot of the descriptor | |
1794 | so we do an additional dereference: | |
1795 | result = *((result_type *) result) | |
1796 | where we expect result to be of some pointer type already. */ | |
1797 | if (targetm.calls.custom_function_descriptors == 0) | |
1798 | gnu_result | |
1799 | = build_unary_op (INDIRECT_REF, NULL_TREE, | |
1800 | convert (build_pointer_type (gnu_result_type), | |
1801 | gnu_result)); | |
a1ab4c31 AC |
1802 | } |
1803 | ||
28dd0055 EB |
1804 | /* For 'Access, issue an error message if the prefix is a C++ method |
1805 | since it can use a special calling convention on some platforms, | |
1806 | which cannot be propagated to the access type. */ | |
1807 | else if (attribute == Attr_Access | |
69720717 | 1808 | && TREE_CODE (TREE_TYPE (gnu_prefix)) == METHOD_TYPE) |
28dd0055 EB |
1809 | post_error ("access to C++ constructor or member function not allowed", |
1810 | gnat_node); | |
1811 | ||
a1ab4c31 AC |
1812 | /* For other address attributes applied to a nested function, |
1813 | find an inner ADDR_EXPR and annotate it so that we can issue | |
1814 | a useful warning with -Wtrampolines. */ | |
ef5a9557 EB |
1815 | else if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_prefix)) |
1816 | && (gnu_expr = remove_conversions (gnu_result, false)) | |
1817 | && TREE_CODE (gnu_expr) == ADDR_EXPR | |
1818 | && decl_function_context (TREE_OPERAND (gnu_expr, 0))) | |
a1ab4c31 | 1819 | { |
ef5a9557 | 1820 | set_expr_location_from_node (gnu_expr, gnat_node); |
a1ab4c31 | 1821 | |
ef5a9557 EB |
1822 | /* Also check the inlining status. */ |
1823 | check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0)); | |
1824 | ||
1825 | /* Moreover, for 'Access or 'Unrestricted_Access with non- | |
1826 | foreign-compatible representation, mark the ADDR_EXPR so | |
1827 | that we can build a descriptor instead of a trampoline. */ | |
1828 | if ((attribute == Attr_Access | |
1829 | || attribute == Attr_Unrestricted_Access) | |
1830 | && targetm.calls.custom_function_descriptors > 0 | |
1831 | && Can_Use_Internal_Rep (Underlying_Type (Etype (gnat_node)))) | |
1832 | FUNC_ADDR_BY_DESCRIPTOR (gnu_expr) = 1; | |
1833 | ||
1834 | /* Otherwise, we need to check that we are not violating the | |
1835 | No_Implicit_Dynamic_Code restriction. */ | |
1836 | else if (targetm.calls.custom_function_descriptors != 0) | |
1837 | Check_Implicit_Dynamic_Code_Allowed (gnat_node); | |
a1ab4c31 AC |
1838 | } |
1839 | break; | |
1840 | ||
1841 | case Attr_Pool_Address: | |
1842 | { | |
a1ab4c31 | 1843 | tree gnu_ptr = gnu_prefix; |
0d7de0e1 | 1844 | tree gnu_obj_type; |
a1ab4c31 AC |
1845 | |
1846 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
1847 | ||
0d7de0e1 EB |
1848 | /* If this is fat pointer, the object must have been allocated with the |
1849 | template in front of the array. So compute the template address; do | |
1850 | it by converting to a thin pointer. */ | |
315cff15 | 1851 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
1852 | gnu_ptr |
1853 | = convert (build_pointer_type | |
1854 | (TYPE_OBJECT_RECORD_TYPE | |
1855 | (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))), | |
1856 | gnu_ptr); | |
1857 | ||
1858 | gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr)); | |
0d7de0e1 EB |
1859 | |
1860 | /* If this is a thin pointer, the object must have been allocated with | |
1861 | the template in front of the array. So compute the template address | |
1862 | and return it. */ | |
1863 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) | |
1864 | gnu_ptr | |
1865 | = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr), | |
1866 | gnu_ptr, | |
2b45154d EB |
1867 | fold_build1 (NEGATE_EXPR, sizetype, |
1868 | byte_position | |
1869 | (DECL_CHAIN | |
1870 | TYPE_FIELDS ((gnu_obj_type))))); | |
a1ab4c31 AC |
1871 | |
1872 | gnu_result = convert (gnu_result_type, gnu_ptr); | |
1873 | } | |
1874 | break; | |
1875 | ||
1876 | case Attr_Size: | |
1877 | case Attr_Object_Size: | |
1878 | case Attr_Value_Size: | |
1879 | case Attr_Max_Size_In_Storage_Elements: | |
79069232 EB |
1880 | /* Strip NOPs, conversions between original and packable versions, and |
1881 | unpadding from GNU_PREFIX. Note that we cannot simply strip every | |
1882 | VIEW_CONVERT_EXPR because some of them may give the actual size, e.g. | |
1883 | for nominally unconstrained packed array. We use GNU_EXPR to see | |
1884 | if a COMPONENT_REF was involved. */ | |
1885 | while (CONVERT_EXPR_P (gnu_prefix) | |
1886 | || TREE_CODE (gnu_prefix) == NON_LVALUE_EXPR | |
1887 | || (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR | |
1888 | && TREE_CODE (TREE_TYPE (gnu_prefix)) == RECORD_TYPE | |
1889 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))) | |
20faffe7 | 1890 | == RECORD_TYPE |
79069232 EB |
1891 | && TYPE_NAME (TREE_TYPE (gnu_prefix)) |
1892 | == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))) | |
1893 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); | |
1894 | gnu_expr = gnu_prefix; | |
1895 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF | |
1896 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))) | |
1897 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); | |
a1ab4c31 AC |
1898 | prefix_unused = true; |
1899 | gnu_type = TREE_TYPE (gnu_prefix); | |
1900 | ||
1901 | /* Replace an unconstrained array type with the type of the underlying | |
65e0a92b EB |
1902 | array, except for 'Max_Size_In_Storage_Elements because we need to |
1903 | return the (maximum) size requested for an allocator. */ | |
a1ab4c31 AC |
1904 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) |
1905 | { | |
1906 | gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type); | |
1907 | if (attribute != Attr_Max_Size_In_Storage_Elements) | |
7d76717d | 1908 | gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))); |
a1ab4c31 AC |
1909 | } |
1910 | ||
da81cc57 EB |
1911 | /* The type must be frozen at this point. */ |
1912 | gcc_assert (COMPLETE_TYPE_P (gnu_type)); | |
1913 | ||
842d4ee2 | 1914 | /* If we're looking for the size of a field, return the field size. */ |
a1ab4c31 AC |
1915 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) |
1916 | gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1)); | |
842d4ee2 EB |
1917 | |
1918 | /* Otherwise, if the prefix is an object, or if we are looking for | |
1919 | 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the | |
1920 | GCC size of the type. We make an exception for padded objects, | |
1921 | as we do not take into account alignment promotions for the size. | |
1922 | This is in keeping with the object case of gnat_to_gnu_entity. */ | |
1923 | else if ((TREE_CODE (gnu_prefix) != TYPE_DECL | |
1924 | && !(TYPE_IS_PADDING_P (gnu_type) | |
4d39941e EB |
1925 | && TREE_CODE (gnu_expr) == COMPONENT_REF |
1926 | && pad_type_has_rm_size (gnu_type))) | |
a1ab4c31 AC |
1927 | || attribute == Attr_Object_Size |
1928 | || attribute == Attr_Max_Size_In_Storage_Elements) | |
1929 | { | |
842d4ee2 EB |
1930 | /* If this is a dereference and we have a special dynamic constrained |
1931 | subtype on the prefix, use it to compute the size; otherwise, use | |
1932 | the designated subtype. */ | |
088d3b0f | 1933 | if (Nkind (gnat_prefix) == N_Explicit_Dereference) |
a1ab4c31 | 1934 | { |
1e17ef87 | 1935 | Node_Id gnat_actual_subtype |
088d3b0f | 1936 | = Actual_Designated_Subtype (gnat_prefix); |
1e17ef87 | 1937 | tree gnu_ptr_type |
088d3b0f | 1938 | = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix))); |
1e17ef87 | 1939 | |
315cff15 | 1940 | if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type) |
1e17ef87 EB |
1941 | && Present (gnat_actual_subtype)) |
1942 | { | |
1943 | tree gnu_actual_obj_type | |
1944 | = gnat_to_gnu_type (gnat_actual_subtype); | |
1945 | gnu_type | |
1946 | = build_unc_object_type_from_ptr (gnu_ptr_type, | |
1947 | gnu_actual_obj_type, | |
928dfa4b EB |
1948 | get_identifier ("SIZE"), |
1949 | false); | |
1e17ef87 | 1950 | } |
a1ab4c31 | 1951 | } |
842d4ee2 EB |
1952 | |
1953 | gnu_result = TYPE_SIZE (gnu_type); | |
a1ab4c31 | 1954 | } |
842d4ee2 EB |
1955 | |
1956 | /* Otherwise, the result is the RM size of the type. */ | |
a1ab4c31 AC |
1957 | else |
1958 | gnu_result = rm_size (gnu_type); | |
1959 | ||
683ccd05 EB |
1960 | /* Deal with a self-referential size by qualifying the size with the |
1961 | object or returning the maximum size for a type. */ | |
1962 | if (TREE_CODE (gnu_prefix) != TYPE_DECL) | |
79069232 | 1963 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); |
683ccd05 EB |
1964 | else if (CONTAINS_PLACEHOLDER_P (gnu_result)) |
1965 | gnu_result = max_size (gnu_result, true); | |
a1ab4c31 | 1966 | |
65e0a92b EB |
1967 | /* If the type contains a template, subtract the padded size of the |
1968 | template, except for 'Max_Size_In_Storage_Elements because we need | |
1969 | to return the (maximum) size requested for an allocator. */ | |
a1ab4c31 | 1970 | if (TREE_CODE (gnu_type) == RECORD_TYPE |
65e0a92b EB |
1971 | && TYPE_CONTAINS_TEMPLATE_P (gnu_type) |
1972 | && attribute != Attr_Max_Size_In_Storage_Elements) | |
1973 | gnu_result | |
1974 | = size_binop (MINUS_EXPR, gnu_result, | |
1975 | bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)))); | |
a1ab4c31 | 1976 | |
58c8f770 | 1977 | /* For 'Max_Size_In_Storage_Elements, adjust the unit. */ |
a1ab4c31 | 1978 | if (attribute == Attr_Max_Size_In_Storage_Elements) |
58c8f770 EB |
1979 | gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node); |
1980 | ||
1981 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
a1ab4c31 AC |
1982 | break; |
1983 | ||
1984 | case Attr_Alignment: | |
caa9d12a EB |
1985 | { |
1986 | unsigned int align; | |
a1ab4c31 | 1987 | |
caa9d12a | 1988 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF |
315cff15 | 1989 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))) |
caa9d12a | 1990 | gnu_prefix = TREE_OPERAND (gnu_prefix, 0); |
a1ab4c31 | 1991 | |
caa9d12a EB |
1992 | gnu_type = TREE_TYPE (gnu_prefix); |
1993 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
1994 | prefix_unused = true; | |
1995 | ||
1996 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) | |
1997 | align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT; | |
1998 | else | |
1999 | { | |
caa9d12a EB |
2000 | Entity_Id gnat_type = Etype (gnat_prefix); |
2001 | unsigned int double_align; | |
2002 | bool is_capped_double, align_clause; | |
2003 | ||
2004 | /* If the default alignment of "double" or larger scalar types is | |
2005 | specifically capped and there is an alignment clause neither | |
2006 | on the type nor on the prefix itself, return the cap. */ | |
2007 | if ((double_align = double_float_alignment) > 0) | |
2008 | is_capped_double | |
2009 | = is_double_float_or_array (gnat_type, &align_clause); | |
2010 | else if ((double_align = double_scalar_alignment) > 0) | |
2011 | is_capped_double | |
2012 | = is_double_scalar_or_array (gnat_type, &align_clause); | |
2013 | else | |
2014 | is_capped_double = align_clause = false; | |
2015 | ||
2016 | if (is_capped_double | |
2017 | && Nkind (gnat_prefix) == N_Identifier | |
2018 | && Present (Alignment_Clause (Entity (gnat_prefix)))) | |
2019 | align_clause = true; | |
2020 | ||
2021 | if (is_capped_double && !align_clause) | |
2022 | align = double_align; | |
2023 | else | |
2024 | align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT; | |
2025 | } | |
2026 | ||
2027 | gnu_result = size_int (align); | |
2028 | } | |
a1ab4c31 AC |
2029 | break; |
2030 | ||
2031 | case Attr_First: | |
2032 | case Attr_Last: | |
2033 | case Attr_Range_Length: | |
2034 | prefix_unused = true; | |
2035 | ||
c95f808d | 2036 | if (INTEGRAL_TYPE_P (gnu_type) || SCALAR_FLOAT_TYPE_P (gnu_type)) |
a1ab4c31 AC |
2037 | { |
2038 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2039 | ||
2040 | if (attribute == Attr_First) | |
2041 | gnu_result = TYPE_MIN_VALUE (gnu_type); | |
2042 | else if (attribute == Attr_Last) | |
2043 | gnu_result = TYPE_MAX_VALUE (gnu_type); | |
2044 | else | |
1eb58520 | 2045 | gnu_result = get_type_length (gnu_type, gnu_result_type); |
a1ab4c31 AC |
2046 | break; |
2047 | } | |
2048 | ||
9c453de7 | 2049 | /* ... fall through ... */ |
a1ab4c31 AC |
2050 | |
2051 | case Attr_Length: | |
2052 | { | |
2053 | int Dimension = (Present (Expressions (gnat_node)) | |
2054 | ? UI_To_Int (Intval (First (Expressions (gnat_node)))) | |
2055 | : 1), i; | |
6bf68a93 | 2056 | struct parm_attr_d *pa = NULL; |
a1ab4c31 | 2057 | Entity_Id gnat_param = Empty; |
088d3b0f | 2058 | bool unconstrained_ptr_deref = false; |
a1ab4c31 | 2059 | |
bb24f343 | 2060 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 | 2061 | gnu_prefix = maybe_unconstrained_array (gnu_prefix); |
ad1d36ba | 2062 | |
088d3b0f EB |
2063 | /* We treat unconstrained array In parameters specially. We also note |
2064 | whether we are dereferencing a pointer to unconstrained array. */ | |
2065 | if (!Is_Constrained (Etype (gnat_prefix))) | |
2066 | switch (Nkind (gnat_prefix)) | |
2067 | { | |
2068 | case N_Identifier: | |
2069 | /* This is the direct case. */ | |
2070 | if (Ekind (Entity (gnat_prefix)) == E_In_Parameter) | |
2071 | gnat_param = Entity (gnat_prefix); | |
2072 | break; | |
2073 | ||
2074 | case N_Explicit_Dereference: | |
2075 | /* This is the indirect case. Note that we need to be sure that | |
2076 | the access value cannot be null as we'll hoist the load. */ | |
2077 | if (Nkind (Prefix (gnat_prefix)) == N_Identifier | |
2078 | && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter) | |
2079 | { | |
2080 | if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix)))) | |
2081 | gnat_param = Entity (Prefix (gnat_prefix)); | |
2082 | } | |
2083 | else | |
2084 | unconstrained_ptr_deref = true; | |
2085 | break; | |
2086 | ||
2087 | default: | |
2088 | break; | |
ad1d36ba EB |
2089 | } |
2090 | ||
9b17f12b EB |
2091 | /* If the prefix is the view conversion of a constrained array to an |
2092 | unconstrained form, we retrieve the constrained array because we | |
2093 | might not be able to substitute the PLACEHOLDER_EXPR coming from | |
2094 | the conversion. This can occur with the 'Old attribute applied | |
2095 | to a parameter with an unconstrained type, which gets rewritten | |
2096 | into a constrained local variable very late in the game. */ | |
2097 | if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR | |
2098 | && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix))) | |
2099 | && !CONTAINS_PLACEHOLDER_P | |
2100 | (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))) | |
2101 | gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)); | |
2102 | else | |
2103 | gnu_type = TREE_TYPE (gnu_prefix); | |
2104 | ||
a1ab4c31 AC |
2105 | prefix_unused = true; |
2106 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2107 | ||
2108 | if (TYPE_CONVENTION_FORTRAN_P (gnu_type)) | |
2109 | { | |
2110 | int ndim; | |
2111 | tree gnu_type_temp; | |
2112 | ||
2113 | for (ndim = 1, gnu_type_temp = gnu_type; | |
2114 | TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE | |
2115 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp)); | |
2116 | ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp)) | |
2117 | ; | |
2118 | ||
2119 | Dimension = ndim + 1 - Dimension; | |
2120 | } | |
2121 | ||
2122 | for (i = 1; i < Dimension; i++) | |
2123 | gnu_type = TREE_TYPE (gnu_type); | |
2124 | ||
2125 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
2126 | ||
2127 | /* When not optimizing, look up the slot associated with the parameter | |
9a30c7c4 AC |
2128 | and the dimension in the cache and create a new one on failure. |
2129 | Don't do this when the actual subtype needs debug info (this happens | |
2130 | with -gnatD): in elaborate_expression_1, we create variables that | |
2131 | hold the bounds, so caching attributes isn't very interesting and | |
2132 | causes dependency issues between these variables and cached | |
2133 | expressions. */ | |
2134 | if (!optimize | |
2135 | && Present (gnat_param) | |
2136 | && !(Present (Actual_Subtype (gnat_param)) | |
2137 | && Needs_Debug_Info (Actual_Subtype (gnat_param)))) | |
a1ab4c31 | 2138 | { |
9771b263 | 2139 | FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa) |
a1ab4c31 AC |
2140 | if (pa->id == gnat_param && pa->dim == Dimension) |
2141 | break; | |
2142 | ||
2143 | if (!pa) | |
2144 | { | |
766090c2 | 2145 | pa = ggc_cleared_alloc<parm_attr_d> (); |
a1ab4c31 AC |
2146 | pa->id = gnat_param; |
2147 | pa->dim = Dimension; | |
9771b263 | 2148 | vec_safe_push (f_parm_attr_cache, pa); |
a1ab4c31 AC |
2149 | } |
2150 | } | |
2151 | ||
2152 | /* Return the cached expression or build a new one. */ | |
2153 | if (attribute == Attr_First) | |
2154 | { | |
2155 | if (pa && pa->first) | |
2156 | { | |
2157 | gnu_result = pa->first; | |
2158 | break; | |
2159 | } | |
2160 | ||
2161 | gnu_result | |
2162 | = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))); | |
2163 | } | |
2164 | ||
2165 | else if (attribute == Attr_Last) | |
2166 | { | |
2167 | if (pa && pa->last) | |
2168 | { | |
2169 | gnu_result = pa->last; | |
2170 | break; | |
2171 | } | |
2172 | ||
2173 | gnu_result | |
2174 | = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))); | |
2175 | } | |
2176 | ||
2177 | else /* attribute == Attr_Range_Length || attribute == Attr_Length */ | |
2178 | { | |
2179 | if (pa && pa->length) | |
2180 | { | |
2181 | gnu_result = pa->length; | |
2182 | break; | |
2183 | } | |
1eb58520 AC |
2184 | |
2185 | gnu_result | |
2186 | = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)), | |
2187 | gnu_result_type); | |
a1ab4c31 AC |
2188 | } |
2189 | ||
2190 | /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are | |
2191 | handling. Note that these attributes could not have been used on | |
2192 | an unconstrained array type. */ | |
4e6602a8 | 2193 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); |
a1ab4c31 AC |
2194 | |
2195 | /* Cache the expression we have just computed. Since we want to do it | |
c01fe451 | 2196 | at run time, we force the use of a SAVE_EXPR and let the gimplifier |
586fea26 EB |
2197 | create the temporary in the outermost binding level. We will make |
2198 | sure in Subprogram_Body_to_gnu that it is evaluated on all possible | |
2199 | paths by forcing its evaluation on entry of the function. */ | |
a1ab4c31 AC |
2200 | if (pa) |
2201 | { | |
2202 | gnu_result | |
2203 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result); | |
088d3b0f EB |
2204 | switch (attribute) |
2205 | { | |
2206 | case Attr_First: | |
2207 | pa->first = gnu_result; | |
2208 | break; | |
2209 | ||
2210 | case Attr_Last: | |
2211 | pa->last = gnu_result; | |
2212 | break; | |
2213 | ||
2214 | case Attr_Length: | |
2215 | case Attr_Range_Length: | |
2216 | pa->length = gnu_result; | |
2217 | break; | |
2218 | ||
2219 | default: | |
2220 | gcc_unreachable (); | |
2221 | } | |
a1ab4c31 | 2222 | } |
321e10dd | 2223 | |
088d3b0f EB |
2224 | /* Otherwise, evaluate it each time it is referenced. */ |
2225 | else | |
2226 | switch (attribute) | |
2227 | { | |
2228 | case Attr_First: | |
2229 | case Attr_Last: | |
2230 | /* If we are dereferencing a pointer to unconstrained array, we | |
2231 | need to capture the value because the pointed-to bounds may | |
2232 | subsequently be released. */ | |
2233 | if (unconstrained_ptr_deref) | |
2234 | gnu_result | |
2235 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result); | |
2236 | break; | |
2237 | ||
2238 | case Attr_Length: | |
2239 | case Attr_Range_Length: | |
2240 | /* Set the source location onto the predicate of the condition | |
2241 | but not if the expression is cached to avoid messing up the | |
2242 | debug info. */ | |
2243 | if (TREE_CODE (gnu_result) == COND_EXPR | |
2244 | && EXPR_P (TREE_OPERAND (gnu_result, 0))) | |
2245 | set_expr_location_from_node (TREE_OPERAND (gnu_result, 0), | |
2246 | gnat_node); | |
2247 | break; | |
2248 | ||
2249 | default: | |
2250 | gcc_unreachable (); | |
2251 | } | |
321e10dd | 2252 | |
a1ab4c31 AC |
2253 | break; |
2254 | } | |
2255 | ||
2256 | case Attr_Bit_Position: | |
2257 | case Attr_Position: | |
2258 | case Attr_First_Bit: | |
2259 | case Attr_Last_Bit: | |
2260 | case Attr_Bit: | |
2261 | { | |
f37fac2b RS |
2262 | poly_int64 bitsize; |
2263 | poly_int64 bitpos; | |
a1ab4c31 AC |
2264 | tree gnu_offset; |
2265 | tree gnu_field_bitpos; | |
2266 | tree gnu_field_offset; | |
2267 | tree gnu_inner; | |
ef4bddc2 | 2268 | machine_mode mode; |
ee45a32d | 2269 | int unsignedp, reversep, volatilep; |
a1ab4c31 AC |
2270 | |
2271 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2272 | gnu_prefix = remove_conversions (gnu_prefix, true); | |
2273 | prefix_unused = true; | |
2274 | ||
2275 | /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF, | |
1e17ef87 | 2276 | the result is 0. Don't allow 'Bit on a bare component, though. */ |
a1ab4c31 AC |
2277 | if (attribute == Attr_Bit |
2278 | && TREE_CODE (gnu_prefix) != COMPONENT_REF | |
2279 | && TREE_CODE (gnu_prefix) != FIELD_DECL) | |
2280 | { | |
2281 | gnu_result = integer_zero_node; | |
2282 | break; | |
2283 | } | |
2284 | ||
2285 | else | |
2286 | gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF | |
2287 | || (attribute == Attr_Bit_Position | |
2288 | && TREE_CODE (gnu_prefix) == FIELD_DECL)); | |
2289 | ||
2290 | get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset, | |
25b75a48 | 2291 | &mode, &unsignedp, &reversep, &volatilep); |
a1ab4c31 AC |
2292 | |
2293 | if (TREE_CODE (gnu_prefix) == COMPONENT_REF) | |
2294 | { | |
2295 | gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1)); | |
2296 | gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1)); | |
2297 | ||
2298 | for (gnu_inner = TREE_OPERAND (gnu_prefix, 0); | |
2299 | TREE_CODE (gnu_inner) == COMPONENT_REF | |
2300 | && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1)); | |
2301 | gnu_inner = TREE_OPERAND (gnu_inner, 0)) | |
2302 | { | |
2303 | gnu_field_bitpos | |
2304 | = size_binop (PLUS_EXPR, gnu_field_bitpos, | |
2305 | bit_position (TREE_OPERAND (gnu_inner, 1))); | |
2306 | gnu_field_offset | |
2307 | = size_binop (PLUS_EXPR, gnu_field_offset, | |
2308 | byte_position (TREE_OPERAND (gnu_inner, 1))); | |
2309 | } | |
2310 | } | |
2311 | else if (TREE_CODE (gnu_prefix) == FIELD_DECL) | |
2312 | { | |
2313 | gnu_field_bitpos = bit_position (gnu_prefix); | |
2314 | gnu_field_offset = byte_position (gnu_prefix); | |
2315 | } | |
2316 | else | |
2317 | { | |
2318 | gnu_field_bitpos = bitsize_zero_node; | |
2319 | gnu_field_offset = size_zero_node; | |
2320 | } | |
2321 | ||
2322 | switch (attribute) | |
2323 | { | |
2324 | case Attr_Position: | |
2325 | gnu_result = gnu_field_offset; | |
2326 | break; | |
2327 | ||
2328 | case Attr_First_Bit: | |
2329 | case Attr_Bit: | |
f37fac2b | 2330 | gnu_result = size_int (num_trailing_bits (bitpos)); |
a1ab4c31 AC |
2331 | break; |
2332 | ||
2333 | case Attr_Last_Bit: | |
f37fac2b | 2334 | gnu_result = bitsize_int (num_trailing_bits (bitpos)); |
a1ab4c31 AC |
2335 | gnu_result = size_binop (PLUS_EXPR, gnu_result, |
2336 | TYPE_SIZE (TREE_TYPE (gnu_prefix))); | |
7cc15171 EB |
2337 | /* ??? Avoid a large unsigned result that will overflow when |
2338 | converted to the signed universal_integer. */ | |
2339 | if (integer_zerop (gnu_result)) | |
2340 | gnu_result = integer_minus_one_node; | |
2341 | else | |
2342 | gnu_result | |
2343 | = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node); | |
a1ab4c31 AC |
2344 | break; |
2345 | ||
2346 | case Attr_Bit_Position: | |
2347 | gnu_result = gnu_field_bitpos; | |
2348 | break; | |
7cc15171 | 2349 | } |
a1ab4c31 | 2350 | |
feec4372 EB |
2351 | /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are |
2352 | handling. */ | |
a1ab4c31 AC |
2353 | gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix); |
2354 | break; | |
2355 | } | |
2356 | ||
2357 | case Attr_Min: | |
2358 | case Attr_Max: | |
2359 | { | |
2360 | tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node))); | |
2361 | tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node)))); | |
2362 | ||
2363 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
881cdd61 EB |
2364 | |
2365 | /* The result of {MIN,MAX}_EXPR is unspecified if either operand is | |
2366 | a NaN so we implement the semantics of C99 f{min,max} to make it | |
2367 | predictable in this case: if either operand is a NaN, the other | |
2368 | is returned; if both operands are NaN's, a NaN is returned. */ | |
89d5c50b EB |
2369 | if (SCALAR_FLOAT_TYPE_P (gnu_result_type) |
2370 | && !Machine_Overflows_On_Target) | |
881cdd61 EB |
2371 | { |
2372 | const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs); | |
2373 | const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs); | |
2374 | tree t = builtin_decl_explicit (BUILT_IN_ISNAN); | |
2375 | tree lhs_is_nan, rhs_is_nan; | |
2376 | ||
2377 | /* If the operands have side-effects, they need to be evaluated | |
2378 | only once in spite of the multiple references in the result. */ | |
2379 | if (lhs_side_effects_p) | |
2380 | gnu_lhs = gnat_protect_expr (gnu_lhs); | |
2381 | if (rhs_side_effects_p) | |
2382 | gnu_rhs = gnat_protect_expr (gnu_rhs); | |
2383 | ||
2384 | lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node, | |
2385 | build_call_expr (t, 1, gnu_lhs), | |
2386 | integer_zero_node); | |
2387 | ||
2388 | rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node, | |
2389 | build_call_expr (t, 1, gnu_rhs), | |
2390 | integer_zero_node); | |
2391 | ||
2392 | gnu_result = build_binary_op (attribute == Attr_Min | |
2393 | ? MIN_EXPR : MAX_EXPR, | |
2394 | gnu_result_type, gnu_lhs, gnu_rhs); | |
2395 | gnu_result = fold_build3 (COND_EXPR, gnu_result_type, | |
2396 | rhs_is_nan, gnu_lhs, gnu_result); | |
2397 | gnu_result = fold_build3 (COND_EXPR, gnu_result_type, | |
2398 | lhs_is_nan, gnu_rhs, gnu_result); | |
2399 | ||
2400 | /* If the operands have side-effects, they need to be evaluated | |
2401 | before doing the tests above since the place they otherwise | |
2402 | would end up being evaluated at run time could be wrong. */ | |
2403 | if (lhs_side_effects_p) | |
2404 | gnu_result | |
2405 | = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result); | |
2406 | ||
2407 | if (rhs_side_effects_p) | |
2408 | gnu_result | |
2409 | = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result); | |
2410 | } | |
2411 | else | |
2412 | gnu_result = build_binary_op (attribute == Attr_Min | |
2413 | ? MIN_EXPR : MAX_EXPR, | |
2414 | gnu_result_type, gnu_lhs, gnu_rhs); | |
a1ab4c31 AC |
2415 | } |
2416 | break; | |
2417 | ||
2418 | case Attr_Passed_By_Reference: | |
2419 | gnu_result = size_int (default_pass_by_ref (gnu_type) | |
2420 | || must_pass_by_ref (gnu_type)); | |
2421 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2422 | break; | |
2423 | ||
2424 | case Attr_Component_Size: | |
bb24f343 | 2425 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 AC |
2426 | gnu_type = TREE_TYPE (gnu_prefix); |
2427 | ||
2428 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
2429 | gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type)))); | |
2430 | ||
2431 | while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE | |
2432 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) | |
2433 | gnu_type = TREE_TYPE (gnu_type); | |
2434 | ||
2435 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
2436 | ||
2437 | /* Note this size cannot be self-referential. */ | |
2438 | gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type)); | |
2439 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2440 | prefix_unused = true; | |
2441 | break; | |
2442 | ||
203ddcea AC |
2443 | case Attr_Descriptor_Size: |
2444 | gnu_type = TREE_TYPE (gnu_prefix); | |
2445 | gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE); | |
2446 | ||
65e0a92b | 2447 | /* Return the padded size of the template in the object record type. */ |
203ddcea | 2448 | gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type); |
2b45154d | 2449 | gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type))); |
203ddcea AC |
2450 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
2451 | prefix_unused = true; | |
2452 | break; | |
2453 | ||
a1ab4c31 | 2454 | case Attr_Null_Parameter: |
feec4372 EB |
2455 | /* This is just a zero cast to the pointer type for our prefix and |
2456 | dereferenced. */ | |
a1ab4c31 AC |
2457 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
2458 | gnu_result | |
2459 | = build_unary_op (INDIRECT_REF, NULL_TREE, | |
2460 | convert (build_pointer_type (gnu_result_type), | |
2461 | integer_zero_node)); | |
a1ab4c31 AC |
2462 | break; |
2463 | ||
2464 | case Attr_Mechanism_Code: | |
2465 | { | |
088d3b0f | 2466 | Entity_Id gnat_obj = Entity (gnat_prefix); |
a1ab4c31 | 2467 | int code; |
a1ab4c31 AC |
2468 | |
2469 | prefix_unused = true; | |
2470 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2471 | if (Present (Expressions (gnat_node))) | |
2472 | { | |
2473 | int i = UI_To_Int (Intval (First (Expressions (gnat_node)))); | |
2474 | ||
2475 | for (gnat_obj = First_Formal (gnat_obj); i > 1; | |
2476 | i--, gnat_obj = Next_Formal (gnat_obj)) | |
2477 | ; | |
2478 | } | |
2479 | ||
2480 | code = Mechanism (gnat_obj); | |
2481 | if (code == Default) | |
2482 | code = ((present_gnu_tree (gnat_obj) | |
2483 | && (DECL_BY_REF_P (get_gnu_tree (gnat_obj)) | |
2484 | || ((TREE_CODE (get_gnu_tree (gnat_obj)) | |
2485 | == PARM_DECL) | |
2486 | && (DECL_BY_COMPONENT_PTR_P | |
2487 | (get_gnu_tree (gnat_obj)))))) | |
2488 | ? By_Reference : By_Copy); | |
2489 | gnu_result = convert (gnu_result_type, size_int (- code)); | |
2490 | } | |
2491 | break; | |
2492 | ||
24228312 AC |
2493 | case Attr_Model: |
2494 | /* We treat Model as identical to Machine. This is true for at least | |
2495 | IEEE and some other nice floating-point systems. */ | |
2496 | ||
9c453de7 | 2497 | /* ... fall through ... */ |
24228312 AC |
2498 | |
2499 | case Attr_Machine: | |
2500 | /* The trick is to force the compiler to store the result in memory so | |
2501 | that we do not have extra precision used. But do this only when this | |
1eb58520 AC |
2502 | is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of |
2503 | the type is lower than that of the longest floating-point type. */ | |
24228312 AC |
2504 | prefix_unused = true; |
2505 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); | |
2506 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2507 | gnu_result = convert (gnu_result_type, gnu_expr); | |
2508 | ||
c68cdfac EB |
2509 | if (TREE_CODE (gnu_result) != REAL_CST |
2510 | && fp_arith_may_widen | |
1eb58520 AC |
2511 | && TYPE_PRECISION (gnu_result_type) |
2512 | < TYPE_PRECISION (longest_float_type_node)) | |
24228312 AC |
2513 | { |
2514 | tree rec_type = make_node (RECORD_TYPE); | |
2515 | tree field | |
2516 | = create_field_decl (get_identifier ("OBJ"), gnu_result_type, | |
2517 | rec_type, NULL_TREE, NULL_TREE, 0, 0); | |
2518 | tree rec_val, asm_expr; | |
2519 | ||
2520 | finish_record_type (rec_type, field, 0, false); | |
2521 | ||
2522 | rec_val = build_constructor_single (rec_type, field, gnu_result); | |
c68cdfac | 2523 | rec_val = build1 (SAVE_EXPR, rec_type, rec_val); |
24228312 AC |
2524 | |
2525 | asm_expr | |
2526 | = build5 (ASM_EXPR, void_type_node, | |
2527 | build_string (0, ""), | |
2528 | tree_cons (build_tree_list (NULL_TREE, | |
2529 | build_string (2, "=m")), | |
2530 | rec_val, NULL_TREE), | |
2531 | tree_cons (build_tree_list (NULL_TREE, | |
2532 | build_string (1, "m")), | |
2533 | rec_val, NULL_TREE), | |
2534 | NULL_TREE, NULL_TREE); | |
2535 | ASM_VOLATILE_P (asm_expr) = 1; | |
2536 | ||
2537 | gnu_result | |
2538 | = build_compound_expr (gnu_result_type, asm_expr, | |
64235766 EB |
2539 | build_component_ref (rec_val, field, |
2540 | false)); | |
24228312 AC |
2541 | } |
2542 | break; | |
2543 | ||
06d75031 EB |
2544 | case Attr_Deref: |
2545 | prefix_unused = true; | |
2546 | gnu_expr = gnat_to_gnu (First (Expressions (gnat_node))); | |
2547 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
2548 | /* This can be a random address so build an alias-all pointer type. */ | |
2549 | gnu_expr | |
2550 | = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode, | |
2551 | true), | |
2552 | gnu_expr); | |
2553 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr); | |
2554 | break; | |
2555 | ||
a1ab4c31 | 2556 | default: |
010dffa9 EB |
2557 | /* This abort means that we have an unimplemented attribute. */ |
2558 | gcc_unreachable (); | |
a1ab4c31 AC |
2559 | } |
2560 | ||
2561 | /* If this is an attribute where the prefix was unused, force a use of it if | |
2562 | it has a side-effect. But don't do it if the prefix is just an entity | |
2563 | name. However, if an access check is needed, we must do it. See second | |
1e17ef87 | 2564 | example in AARM 11.6(5.e). */ |
088d3b0f EB |
2565 | if (prefix_unused |
2566 | && TREE_SIDE_EFFECTS (gnu_prefix) | |
2567 | && !Is_Entity_Name (gnat_prefix)) | |
2568 | gnu_result | |
24228312 | 2569 | = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result); |
a1ab4c31 AC |
2570 | |
2571 | *gnu_result_type_p = gnu_result_type; | |
2572 | return gnu_result; | |
2573 | } | |
ce2d0ce2 | 2574 | |
e08f1aad EB |
2575 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Case_Statement, to a |
2576 | GCC tree, which is returned. */ | |
a1ab4c31 AC |
2577 | |
2578 | static tree | |
2579 | Case_Statement_to_gnu (Node_Id gnat_node) | |
2580 | { | |
a31d78c6 | 2581 | tree gnu_result, gnu_expr, gnu_type, gnu_label; |
a1ab4c31 | 2582 | Node_Id gnat_when; |
2d3c7e4f | 2583 | location_t end_locus; |
83e279c4 | 2584 | bool may_fallthru = false; |
a1ab4c31 AC |
2585 | |
2586 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); | |
2587 | gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr); | |
a31d78c6 EB |
2588 | gnu_expr = maybe_character_value (gnu_expr); |
2589 | gnu_type = TREE_TYPE (gnu_expr); | |
a1ab4c31 | 2590 | |
a1ab4c31 AC |
2591 | /* We build a SWITCH_EXPR that contains the code with interspersed |
2592 | CASE_LABEL_EXPRs for each label. */ | |
08b8b90c | 2593 | if (!Sloc_to_locus (End_Location (gnat_node), &end_locus)) |
2d3c7e4f EB |
2594 | end_locus = input_location; |
2595 | gnu_label = create_artificial_label (end_locus); | |
a1ab4c31 | 2596 | start_stmt_group (); |
b4f73deb | 2597 | |
a1ab4c31 AC |
2598 | for (gnat_when = First_Non_Pragma (Alternatives (gnat_node)); |
2599 | Present (gnat_when); | |
2600 | gnat_when = Next_Non_Pragma (gnat_when)) | |
2601 | { | |
9c69c3af | 2602 | bool choices_added_p = false; |
a1ab4c31 | 2603 | Node_Id gnat_choice; |
a1ab4c31 AC |
2604 | |
2605 | /* First compile all the different case choices for the current WHEN | |
2606 | alternative. */ | |
2607 | for (gnat_choice = First (Discrete_Choices (gnat_when)); | |
ac43e11e AC |
2608 | Present (gnat_choice); |
2609 | gnat_choice = Next (gnat_choice)) | |
a1ab4c31 AC |
2610 | { |
2611 | tree gnu_low = NULL_TREE, gnu_high = NULL_TREE; | |
ac43e11e | 2612 | tree label = create_artificial_label (input_location); |
a1ab4c31 AC |
2613 | |
2614 | switch (Nkind (gnat_choice)) | |
2615 | { | |
2616 | case N_Range: | |
2617 | gnu_low = gnat_to_gnu (Low_Bound (gnat_choice)); | |
2618 | gnu_high = gnat_to_gnu (High_Bound (gnat_choice)); | |
2619 | break; | |
2620 | ||
2621 | case N_Subtype_Indication: | |
2622 | gnu_low = gnat_to_gnu (Low_Bound (Range_Expression | |
2623 | (Constraint (gnat_choice)))); | |
2624 | gnu_high = gnat_to_gnu (High_Bound (Range_Expression | |
2625 | (Constraint (gnat_choice)))); | |
2626 | break; | |
2627 | ||
2628 | case N_Identifier: | |
2629 | case N_Expanded_Name: | |
2630 | /* This represents either a subtype range or a static value of | |
2631 | some kind; Ekind says which. */ | |
7ed9919d | 2632 | if (Is_Type (Entity (gnat_choice))) |
a1ab4c31 AC |
2633 | { |
2634 | tree gnu_type = get_unpadded_type (Entity (gnat_choice)); | |
2635 | ||
ac43e11e AC |
2636 | gnu_low = TYPE_MIN_VALUE (gnu_type); |
2637 | gnu_high = TYPE_MAX_VALUE (gnu_type); | |
a1ab4c31 AC |
2638 | break; |
2639 | } | |
2640 | ||
9c453de7 | 2641 | /* ... fall through ... */ |
a1ab4c31 AC |
2642 | |
2643 | case N_Character_Literal: | |
2644 | case N_Integer_Literal: | |
2645 | gnu_low = gnat_to_gnu (gnat_choice); | |
2646 | break; | |
2647 | ||
2648 | case N_Others_Choice: | |
2649 | break; | |
2650 | ||
2651 | default: | |
2652 | gcc_unreachable (); | |
2653 | } | |
2654 | ||
ac43e11e AC |
2655 | /* Everything should be folded into constants at this point. */ |
2656 | gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST); | |
2657 | gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST); | |
2658 | ||
a31d78c6 EB |
2659 | if (gnu_low && TREE_TYPE (gnu_low) != gnu_type) |
2660 | gnu_low = convert (gnu_type, gnu_low); | |
2661 | if (gnu_high && TREE_TYPE (gnu_high) != gnu_type) | |
2662 | gnu_high = convert (gnu_type, gnu_high); | |
2663 | ||
ac43e11e AC |
2664 | add_stmt_with_node (build_case_label (gnu_low, gnu_high, label), |
2665 | gnat_choice); | |
2666 | choices_added_p = true; | |
a1ab4c31 AC |
2667 | } |
2668 | ||
a1d7a124 EB |
2669 | /* This construct doesn't define a scope so we shouldn't push a binding |
2670 | level around the statement list. Except that we have always done so | |
2671 | historically and this makes it possible to reduce stack usage. As a | |
2672 | compromise, we keep doing it for case statements, for which this has | |
2673 | never been problematic, but not for case expressions in Ada 2012. */ | |
9c69c3af | 2674 | if (choices_added_p) |
a1ab4c31 | 2675 | { |
a1d7a124 EB |
2676 | const bool is_case_expression |
2677 | = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions); | |
2678 | tree group | |
2679 | = build_stmt_group (Statements (gnat_when), !is_case_expression); | |
83e279c4 EB |
2680 | bool group_may_fallthru = block_may_fallthru (group); |
2681 | add_stmt (group); | |
2682 | if (group_may_fallthru) | |
2683 | { | |
2d3c7e4f EB |
2684 | tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label); |
2685 | SET_EXPR_LOCATION (stmt, end_locus); | |
2686 | add_stmt (stmt); | |
83e279c4 EB |
2687 | may_fallthru = true; |
2688 | } | |
a1ab4c31 AC |
2689 | } |
2690 | } | |
2691 | ||
41a961e9 | 2692 | /* Now emit a definition of the label the cases branch to, if any. */ |
83e279c4 EB |
2693 | if (may_fallthru) |
2694 | add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label)); | |
9e851845 | 2695 | gnu_result = build2 (SWITCH_EXPR, gnu_type, gnu_expr, end_stmt_group ()); |
a1ab4c31 AC |
2696 | |
2697 | return gnu_result; | |
2698 | } | |
ce2d0ce2 | 2699 | |
87ab2b04 EB |
2700 | /* Return true if we are in the body of a loop. */ |
2701 | ||
2702 | static inline bool | |
2703 | inside_loop_p (void) | |
2704 | { | |
2705 | return !vec_safe_is_empty (gnu_loop_stack); | |
2706 | } | |
2707 | ||
933a7325 EB |
2708 | /* Find out whether EXPR is a simple additive expression based on the iteration |
2709 | variable of some enclosing loop in the current function. If so, return the | |
2710 | loop and set *DISP to the displacement and *NEG_P to true if this is for a | |
2711 | subtraction; otherwise, return NULL. */ | |
15bf7d19 | 2712 | |
87ab2b04 | 2713 | static struct loop_info_d * |
22903f22 | 2714 | find_loop_for (tree expr, tree *disp, bool *neg_p) |
15bf7d19 | 2715 | { |
933a7325 EB |
2716 | tree var, add, cst; |
2717 | bool minus_p; | |
15bf7d19 EB |
2718 | struct loop_info_d *iter = NULL; |
2719 | unsigned int i; | |
2720 | ||
933a7325 EB |
2721 | if (is_simple_additive_expression (expr, &add, &cst, &minus_p)) |
2722 | { | |
2723 | var = add; | |
2724 | if (disp) | |
2725 | *disp = cst; | |
2726 | if (neg_p) | |
2727 | *neg_p = minus_p; | |
2728 | } | |
2729 | else | |
2730 | { | |
2731 | var = expr; | |
2732 | if (disp) | |
2733 | *disp = NULL_TREE; | |
2734 | if (neg_p) | |
2735 | *neg_p = false; | |
2736 | } | |
2737 | ||
722356ce | 2738 | var = remove_conversions (var, false); |
15bf7d19 EB |
2739 | |
2740 | if (TREE_CODE (var) != VAR_DECL) | |
2741 | return NULL; | |
2742 | ||
852dd866 EB |
2743 | gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0); |
2744 | ||
2745 | FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter) | |
2746 | if (iter->loop_var == var && iter->fndecl == current_function_decl) | |
2747 | break; | |
2748 | ||
2749 | return iter; | |
2750 | } | |
15bf7d19 | 2751 | |
852dd866 EB |
2752 | /* Return the innermost enclosing loop in the current function. */ |
2753 | ||
2754 | static struct loop_info_d * | |
2755 | find_loop (void) | |
2756 | { | |
2757 | struct loop_info_d *iter = NULL; | |
2758 | unsigned int i; | |
2759 | ||
2760 | gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0); | |
90b4c164 | 2761 | |
547bbe49 | 2762 | FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter) |
852dd866 | 2763 | if (iter->fndecl == current_function_decl) |
15bf7d19 EB |
2764 | break; |
2765 | ||
87ab2b04 | 2766 | return iter; |
15bf7d19 EB |
2767 | } |
2768 | ||
d88bbbb9 EB |
2769 | /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is |
2770 | false, or the maximum value if MAX is true, of TYPE. */ | |
2771 | ||
2772 | static bool | |
2773 | can_equal_min_or_max_val_p (tree val, tree type, bool max) | |
2774 | { | |
2775 | tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type)); | |
2776 | ||
2777 | if (TREE_CODE (min_or_max_val) != INTEGER_CST) | |
2778 | return true; | |
2779 | ||
2780 | if (TREE_CODE (val) == NOP_EXPR) | |
2781 | val = (max | |
2782 | ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))) | |
2783 | : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))); | |
2784 | ||
2785 | if (TREE_CODE (val) != INTEGER_CST) | |
2786 | return true; | |
2787 | ||
2efdbf0f EB |
2788 | if (max) |
2789 | return tree_int_cst_lt (val, min_or_max_val) == 0; | |
2790 | else | |
2791 | return tree_int_cst_lt (min_or_max_val, val) == 0; | |
d88bbbb9 EB |
2792 | } |
2793 | ||
2794 | /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE. | |
2795 | If REVERSE is true, minimum value is taken as maximum value. */ | |
2796 | ||
2797 | static inline bool | |
2798 | can_equal_min_val_p (tree val, tree type, bool reverse) | |
2799 | { | |
2800 | return can_equal_min_or_max_val_p (val, type, reverse); | |
2801 | } | |
2802 | ||
2803 | /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE. | |
2804 | If REVERSE is true, maximum value is taken as minimum value. */ | |
2805 | ||
2806 | static inline bool | |
2807 | can_equal_max_val_p (tree val, tree type, bool reverse) | |
2808 | { | |
2809 | return can_equal_min_or_max_val_p (val, type, !reverse); | |
2810 | } | |
2811 | ||
64235766 EB |
2812 | /* Replace EXPR1 and EXPR2 by invariant expressions if possible. Return |
2813 | true if both expressions have been replaced and false otherwise. */ | |
2814 | ||
2815 | static bool | |
2816 | make_invariant (tree *expr1, tree *expr2) | |
2817 | { | |
2818 | tree inv_expr1 = gnat_invariant_expr (*expr1); | |
2819 | tree inv_expr2 = gnat_invariant_expr (*expr2); | |
2820 | ||
2821 | if (inv_expr1) | |
2822 | *expr1 = inv_expr1; | |
2823 | ||
2824 | if (inv_expr2) | |
2825 | *expr2 = inv_expr2; | |
2826 | ||
2827 | return inv_expr1 && inv_expr2; | |
2828 | } | |
2829 | ||
1ddde8dc EB |
2830 | /* Helper function for walk_tree, used by independent_iterations_p below. */ |
2831 | ||
2832 | static tree | |
2833 | scan_rhs_r (tree *tp, int *walk_subtrees, void *data) | |
2834 | { | |
2835 | bitmap *params = (bitmap *)data; | |
2836 | tree t = *tp; | |
2837 | ||
2838 | /* No need to walk into types or decls. */ | |
2839 | if (IS_TYPE_OR_DECL_P (t)) | |
2840 | *walk_subtrees = 0; | |
2841 | ||
2842 | if (TREE_CODE (t) == PARM_DECL && bitmap_bit_p (*params, DECL_UID (t))) | |
2843 | return t; | |
2844 | ||
2845 | return NULL_TREE; | |
2846 | } | |
2847 | ||
2848 | /* Return true if STMT_LIST generates independent iterations in a loop. */ | |
2849 | ||
2850 | static bool | |
2851 | independent_iterations_p (tree stmt_list) | |
2852 | { | |
2853 | tree_stmt_iterator tsi; | |
2854 | bitmap params = BITMAP_GGC_ALLOC(); | |
b9778c45 | 2855 | auto_vec<tree, 16> rhs; |
1ddde8dc EB |
2856 | tree iter; |
2857 | int i; | |
2858 | ||
2859 | if (TREE_CODE (stmt_list) == BIND_EXPR) | |
2860 | stmt_list = BIND_EXPR_BODY (stmt_list); | |
2861 | ||
2862 | /* Scan the list and return false on anything that is not either a check | |
2863 | or an assignment to a parameter with restricted aliasing. */ | |
2864 | for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi)) | |
2865 | { | |
2866 | tree stmt = tsi_stmt (tsi); | |
2867 | ||
2868 | switch (TREE_CODE (stmt)) | |
2869 | { | |
2870 | case COND_EXPR: | |
2871 | { | |
2872 | if (COND_EXPR_ELSE (stmt)) | |
2873 | return false; | |
2874 | if (TREE_CODE (COND_EXPR_THEN (stmt)) != CALL_EXPR) | |
2875 | return false; | |
2876 | tree func = get_callee_fndecl (COND_EXPR_THEN (stmt)); | |
2877 | if (!(func && TREE_THIS_VOLATILE (func))) | |
2878 | return false; | |
2879 | break; | |
2880 | } | |
2881 | ||
2882 | case MODIFY_EXPR: | |
2883 | { | |
2884 | tree lhs = TREE_OPERAND (stmt, 0); | |
2885 | while (handled_component_p (lhs)) | |
2886 | lhs = TREE_OPERAND (lhs, 0); | |
2887 | if (TREE_CODE (lhs) != INDIRECT_REF) | |
2888 | return false; | |
2889 | lhs = TREE_OPERAND (lhs, 0); | |
2890 | if (!(TREE_CODE (lhs) == PARM_DECL | |
2891 | && DECL_RESTRICTED_ALIASING_P (lhs))) | |
2892 | return false; | |
2893 | bitmap_set_bit (params, DECL_UID (lhs)); | |
2894 | rhs.safe_push (TREE_OPERAND (stmt, 1)); | |
2895 | break; | |
2896 | } | |
2897 | ||
2898 | default: | |
2899 | return false; | |
2900 | } | |
2901 | } | |
2902 | ||
2903 | /* At this point we know that the list contains only statements that will | |
2904 | modify parameters with restricted aliasing. Check that the statements | |
2905 | don't at the time read from these parameters. */ | |
2906 | FOR_EACH_VEC_ELT (rhs, i, iter) | |
2907 | if (walk_tree_without_duplicates (&iter, scan_rhs_r, ¶ms)) | |
2908 | return false; | |
2909 | ||
2910 | return true; | |
2911 | } | |
2912 | ||
e08f1aad EB |
2913 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Loop_Statement, to a |
2914 | GCC tree, which is returned. */ | |
7beb2b9d OH |
2915 | |
2916 | static tree | |
852dd866 | 2917 | Loop_Statement_to_gnu (Node_Id gnat_node) |
7beb2b9d | 2918 | { |
7beb2b9d | 2919 | const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node); |
852dd866 EB |
2920 | struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> (); |
2921 | tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE, | |
2922 | NULL_TREE, NULL_TREE, NULL_TREE); | |
2923 | tree gnu_loop_label = create_artificial_label (input_location); | |
2924 | tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE; | |
2925 | tree gnu_result; | |
2926 | ||
2927 | /* Push the loop_info structure associated with the LOOP_STMT. */ | |
2928 | gnu_loop_info->fndecl = current_function_decl; | |
2929 | gnu_loop_info->stmt = gnu_loop_stmt; | |
2930 | vec_safe_push (gnu_loop_stack, gnu_loop_info); | |
2931 | ||
2932 | /* Set location information for statement and end label. */ | |
2933 | set_expr_location_from_node (gnu_loop_stmt, gnat_node); | |
2934 | Sloc_to_locus (Sloc (End_Label (gnat_node)), | |
2935 | &DECL_SOURCE_LOCATION (gnu_loop_label)); | |
2936 | LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label; | |
7beb2b9d | 2937 | |
93101d23 EB |
2938 | /* Set the condition under which the loop must keep going. If we have an |
2939 | explicit condition, use it to set the location information throughout | |
2940 | the translation of the loop statement to avoid having multiple SLOCs. | |
a1ab4c31 | 2941 | |
7fda1596 | 2942 | For the case "LOOP .... END LOOP;" the condition is always true. */ |
a1ab4c31 AC |
2943 | if (No (gnat_iter_scheme)) |
2944 | ; | |
7fda1596 EB |
2945 | |
2946 | /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */ | |
a1ab4c31 | 2947 | else if (Present (Condition (gnat_iter_scheme))) |
93101d23 EB |
2948 | { |
2949 | LOOP_STMT_COND (gnu_loop_stmt) | |
2950 | = gnat_to_gnu (Condition (gnat_iter_scheme)); | |
2951 | ||
2952 | set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme); | |
2953 | } | |
7fda1596 | 2954 | |
58c8f770 EB |
2955 | /* Otherwise we have an iteration scheme and the condition is given by the |
2956 | bounds of the subtype of the iteration variable. */ | |
a1ab4c31 AC |
2957 | else |
2958 | { | |
a1ab4c31 AC |
2959 | Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme); |
2960 | Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec); | |
2961 | Entity_Id gnat_type = Etype (gnat_loop_var); | |
2962 | tree gnu_type = get_unpadded_type (gnat_type); | |
825da0d2 | 2963 | tree gnu_base_type = maybe_character_type (get_base_type (gnu_type)); |
9a1bdc31 | 2964 | tree gnu_one_node = build_int_cst (gnu_base_type, 1); |
6162cec0 | 2965 | tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt; |
d88bbbb9 | 2966 | enum tree_code update_code, test_code, shift_code; |
6162cec0 | 2967 | bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false; |
82d3b03a | 2968 | |
1eb58520 AC |
2969 | gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type)); |
2970 | gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type)); | |
15bf7d19 | 2971 | |
58c8f770 | 2972 | /* We must disable modulo reduction for the iteration variable, if any, |
82d3b03a | 2973 | in order for the loop comparison to be effective. */ |
d88bbbb9 | 2974 | if (reverse) |
82d3b03a EB |
2975 | { |
2976 | gnu_first = gnu_high; | |
2977 | gnu_last = gnu_low; | |
2978 | update_code = MINUS_NOMOD_EXPR; | |
58c8f770 | 2979 | test_code = GE_EXPR; |
d88bbbb9 | 2980 | shift_code = PLUS_NOMOD_EXPR; |
82d3b03a EB |
2981 | } |
2982 | else | |
2983 | { | |
2984 | gnu_first = gnu_low; | |
2985 | gnu_last = gnu_high; | |
2986 | update_code = PLUS_NOMOD_EXPR; | |
58c8f770 | 2987 | test_code = LE_EXPR; |
d88bbbb9 EB |
2988 | shift_code = MINUS_NOMOD_EXPR; |
2989 | } | |
2990 | ||
2991 | /* We use two different strategies to translate the loop, depending on | |
2992 | whether optimization is enabled. | |
2993 | ||
6162cec0 EB |
2994 | If it is, we generate the canonical loop form expected by the loop |
2995 | optimizer and the loop vectorizer, which is the do-while form: | |
d88bbbb9 EB |
2996 | |
2997 | ENTRY_COND | |
2998 | loop: | |
2999 | TOP_UPDATE | |
3000 | BODY | |
3001 | BOTTOM_COND | |
3002 | GOTO loop | |
3003 | ||
6162cec0 EB |
3004 | This avoids an implicit dependency on loop header copying and makes |
3005 | it possible to turn BOTTOM_COND into an inequality test. | |
3006 | ||
3007 | If optimization is disabled, loop header copying doesn't come into | |
3008 | play and we try to generate the loop form with the fewer conditional | |
3009 | branches. First, the default form, which is: | |
d88bbbb9 EB |
3010 | |
3011 | loop: | |
3012 | TOP_COND | |
3013 | BODY | |
3014 | BOTTOM_UPDATE | |
3015 | GOTO loop | |
3016 | ||
6162cec0 EB |
3017 | It should catch most loops with constant ending point. Then, if we |
3018 | cannot, we try to generate the shifted form: | |
d88bbbb9 | 3019 | |
d88bbbb9 | 3020 | loop: |
6162cec0 EB |
3021 | TOP_COND |
3022 | TOP_UPDATE | |
d88bbbb9 | 3023 | BODY |
d88bbbb9 EB |
3024 | GOTO loop |
3025 | ||
6162cec0 EB |
3026 | which should catch loops with constant starting point. Otherwise, if |
3027 | we cannot, we generate the fallback form: | |
d88bbbb9 | 3028 | |
6162cec0 | 3029 | ENTRY_COND |
d88bbbb9 | 3030 | loop: |
d88bbbb9 | 3031 | BODY |
6162cec0 EB |
3032 | BOTTOM_COND |
3033 | BOTTOM_UPDATE | |
d88bbbb9 EB |
3034 | GOTO loop |
3035 | ||
6162cec0 | 3036 | which works in all cases. */ |
d88bbbb9 | 3037 | |
09248547 | 3038 | if (optimize && !optimize_debug) |
d88bbbb9 | 3039 | { |
6162cec0 EB |
3040 | /* We can use the do-while form directly if GNU_FIRST-1 doesn't |
3041 | overflow. */ | |
d88bbbb9 | 3042 | if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)) |
d88bbbb9 EB |
3043 | ; |
3044 | ||
6162cec0 | 3045 | /* Otherwise, use the do-while form with the help of a special |
15bf7d19 | 3046 | induction variable in the unsigned version of the base type |
48d3d75d | 3047 | or the unsigned version of the size type, whichever is the |
15bf7d19 | 3048 | largest, in order to have wrap-around arithmetics for it. */ |
d88bbbb9 | 3049 | else |
6162cec0 | 3050 | { |
48d3d75d EB |
3051 | if (TYPE_PRECISION (gnu_base_type) |
3052 | > TYPE_PRECISION (size_type_node)) | |
00a22e5e EB |
3053 | gnu_base_type |
3054 | = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1); | |
15bf7d19 | 3055 | else |
48d3d75d | 3056 | gnu_base_type = size_type_node; |
15bf7d19 EB |
3057 | |
3058 | gnu_first = convert (gnu_base_type, gnu_first); | |
3059 | gnu_last = convert (gnu_base_type, gnu_last); | |
9a1bdc31 | 3060 | gnu_one_node = build_int_cst (gnu_base_type, 1); |
6162cec0 EB |
3061 | use_iv = true; |
3062 | } | |
3063 | ||
3064 | gnu_first | |
3065 | = build_binary_op (shift_code, gnu_base_type, gnu_first, | |
3066 | gnu_one_node); | |
3067 | LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1; | |
3068 | LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1; | |
d88bbbb9 EB |
3069 | } |
3070 | else | |
3071 | { | |
3072 | /* We can use the default form if GNU_LAST+1 doesn't overflow. */ | |
3073 | if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse)) | |
3074 | ; | |
3075 | ||
3076 | /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor | |
3077 | GNU_LAST-1 does. */ | |
3078 | else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse) | |
3079 | && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse)) | |
3080 | { | |
6162cec0 EB |
3081 | gnu_first |
3082 | = build_binary_op (shift_code, gnu_base_type, gnu_first, | |
3083 | gnu_one_node); | |
3084 | gnu_last | |
3085 | = build_binary_op (shift_code, gnu_base_type, gnu_last, | |
3086 | gnu_one_node); | |
d88bbbb9 EB |
3087 | LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1; |
3088 | } | |
3089 | ||
3090 | /* Otherwise, use the fallback form. */ | |
3091 | else | |
6162cec0 | 3092 | LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1; |
82d3b03a | 3093 | } |
a1ab4c31 | 3094 | |
d88bbbb9 | 3095 | /* If we use the BOTTOM_COND, we can turn the test into an inequality |
ec1b8711 | 3096 | test but we have to add ENTRY_COND to protect the empty loop. */ |
d88bbbb9 | 3097 | if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt)) |
a1ab4c31 | 3098 | { |
d88bbbb9 | 3099 | test_code = NE_EXPR; |
ec1b8711 EB |
3100 | gnu_cond_expr |
3101 | = build3 (COND_EXPR, void_type_node, | |
3102 | build_binary_op (LE_EXPR, boolean_type_node, | |
3103 | gnu_low, gnu_high), | |
3104 | NULL_TREE, alloc_stmt_list ()); | |
3105 | set_expr_location_from_node (gnu_cond_expr, gnat_iter_scheme); | |
a1ab4c31 AC |
3106 | } |
3107 | ||
3108 | /* Open a new nesting level that will surround the loop to declare the | |
58c8f770 | 3109 | iteration variable. */ |
a1ab4c31 AC |
3110 | start_stmt_group (); |
3111 | gnat_pushlevel (); | |
3112 | ||
6162cec0 EB |
3113 | /* If we use the special induction variable, create it and set it to |
3114 | its initial value. Morever, the regular iteration variable cannot | |
3115 | itself be initialized, lest the initial value wrapped around. */ | |
3116 | if (use_iv) | |
3117 | { | |
3118 | gnu_loop_iv | |
3119 | = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var); | |
3120 | add_stmt (gnu_stmt); | |
3121 | gnu_first = NULL_TREE; | |
3122 | } | |
3123 | else | |
3124 | gnu_loop_iv = NULL_TREE; | |
3125 | ||
58c8f770 | 3126 | /* Declare the iteration variable and set it to its initial value. */ |
afc737f0 | 3127 | gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, true); |
a1ab4c31 AC |
3128 | if (DECL_BY_REF_P (gnu_loop_var)) |
3129 | gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var); | |
15bf7d19 EB |
3130 | else if (use_iv) |
3131 | { | |
3132 | gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var)); | |
3133 | SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv); | |
3134 | } | |
3135 | gnu_loop_info->loop_var = gnu_loop_var; | |
87ab2b04 EB |
3136 | gnu_loop_info->low_bound = gnu_low; |
3137 | gnu_loop_info->high_bound = gnu_high; | |
a1ab4c31 | 3138 | |
58c8f770 EB |
3139 | /* Do all the arithmetics in the base type. */ |
3140 | gnu_loop_var = convert (gnu_base_type, gnu_loop_var); | |
a1ab4c31 | 3141 | |
d88bbbb9 | 3142 | /* Set either the top or bottom exit condition. */ |
6162cec0 EB |
3143 | if (use_iv) |
3144 | LOOP_STMT_COND (gnu_loop_stmt) | |
3145 | = build_binary_op (test_code, boolean_type_node, gnu_loop_iv, | |
3146 | gnu_last); | |
3147 | else | |
3148 | LOOP_STMT_COND (gnu_loop_stmt) | |
3149 | = build_binary_op (test_code, boolean_type_node, gnu_loop_var, | |
3150 | gnu_last); | |
a1ab4c31 | 3151 | |
d88bbbb9 EB |
3152 | /* Set either the top or bottom update statement and give it the source |
3153 | location of the iteration for better coverage info. */ | |
6162cec0 EB |
3154 | if (use_iv) |
3155 | { | |
3156 | gnu_stmt | |
3157 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv, | |
3158 | build_binary_op (update_code, gnu_base_type, | |
3159 | gnu_loop_iv, gnu_one_node)); | |
3160 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3161 | append_to_statement_list (gnu_stmt, | |
3162 | &LOOP_STMT_UPDATE (gnu_loop_stmt)); | |
3163 | gnu_stmt | |
3164 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var, | |
3165 | gnu_loop_iv); | |
3166 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3167 | append_to_statement_list (gnu_stmt, | |
3168 | &LOOP_STMT_UPDATE (gnu_loop_stmt)); | |
3169 | } | |
3170 | else | |
3171 | { | |
3172 | gnu_stmt | |
3173 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var, | |
3174 | build_binary_op (update_code, gnu_base_type, | |
3175 | gnu_loop_var, gnu_one_node)); | |
3176 | set_expr_location_from_node (gnu_stmt, gnat_iter_scheme); | |
3177 | LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt; | |
3178 | } | |
93101d23 EB |
3179 | |
3180 | set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme); | |
a1ab4c31 AC |
3181 | } |
3182 | ||
3183 | /* If the loop was named, have the name point to this loop. In this case, | |
58c8f770 | 3184 | the association is not a DECL node, but the end label of the loop. */ |
a1ab4c31 | 3185 | if (Present (Identifier (gnat_node))) |
58c8f770 | 3186 | save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true); |
a1ab4c31 AC |
3187 | |
3188 | /* Make the loop body into its own block, so any allocated storage will be | |
3189 | released every iteration. This is needed for stack allocation. */ | |
3190 | LOOP_STMT_BODY (gnu_loop_stmt) | |
3191 | = build_stmt_group (Statements (gnat_node), true); | |
58c8f770 | 3192 | TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1; |
a1ab4c31 | 3193 | |
6162cec0 EB |
3194 | /* If we have an iteration scheme, then we are in a statement group. Add |
3195 | the LOOP_STMT to it, finish it and make it the "loop". */ | |
3196 | if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme))) | |
a1ab4c31 | 3197 | { |
64235766 EB |
3198 | /* First, if we have computed invariant conditions for range (or index) |
3199 | checks applied to the iteration variable, find out whether they can | |
3200 | be evaluated to false at compile time; otherwise, if there are not | |
3201 | too many of them, combine them with the original checks. If loop | |
3202 | unswitching is enabled, do not require the loop bounds to be also | |
3203 | invariant, as their evaluation will still be ahead of the loop. */ | |
3204 | if (vec_safe_length (gnu_loop_info->checks) > 0 | |
3bdf0b64 | 3205 | && (make_invariant (&gnu_low, &gnu_high) || optimize >= 3)) |
64235766 EB |
3206 | { |
3207 | struct range_check_info_d *rci; | |
3208 | unsigned int i, n_remaining_checks = 0; | |
3209 | ||
3210 | FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci) | |
3211 | { | |
933a7325 EB |
3212 | tree low_ok, high_ok; |
3213 | ||
3214 | if (rci->low_bound) | |
3215 | { | |
3216 | tree gnu_adjusted_low = convert (rci->type, gnu_low); | |
3217 | if (rci->disp) | |
3218 | gnu_adjusted_low | |
3219 | = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR, | |
3220 | rci->type, gnu_adjusted_low, rci->disp); | |
3221 | low_ok | |
3222 | = build_binary_op (GE_EXPR, boolean_type_node, | |
3223 | gnu_adjusted_low, rci->low_bound); | |
3224 | } | |
3225 | else | |
3226 | low_ok = boolean_true_node; | |
3227 | ||
3228 | if (rci->high_bound) | |
3229 | { | |
3230 | tree gnu_adjusted_high = convert (rci->type, gnu_high); | |
3231 | if (rci->disp) | |
3232 | gnu_adjusted_high | |
3233 | = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR, | |
3234 | rci->type, gnu_adjusted_high, rci->disp); | |
3235 | high_ok | |
3236 | = build_binary_op (LE_EXPR, boolean_type_node, | |
3237 | gnu_adjusted_high, rci->high_bound); | |
3238 | } | |
3239 | else | |
3240 | high_ok = boolean_true_node; | |
64235766 EB |
3241 | |
3242 | tree range_ok | |
3243 | = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node, | |
3244 | low_ok, high_ok); | |
3245 | ||
3246 | rci->invariant_cond | |
3247 | = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok); | |
3248 | ||
3249 | if (rci->invariant_cond == boolean_false_node) | |
3250 | TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond; | |
3251 | else | |
3252 | n_remaining_checks++; | |
3253 | } | |
3254 | ||
3255 | /* Note that loop unswitching can only be applied a small number of | |
3256 | times to a given loop (PARAM_MAX_UNSWITCH_LEVEL default to 3). */ | |
278f422c | 3257 | if (IN_RANGE (n_remaining_checks, 1, 3) |
3bdf0b64 | 3258 | && optimize >= 2 |
278f422c | 3259 | && !optimize_size) |
64235766 EB |
3260 | FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci) |
3261 | if (rci->invariant_cond != boolean_false_node) | |
3262 | { | |
3263 | TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond; | |
3264 | ||
3bdf0b64 | 3265 | if (optimize >= 3) |
64235766 EB |
3266 | add_stmt_with_node_force (rci->inserted_cond, gnat_node); |
3267 | } | |
3268 | } | |
15bf7d19 | 3269 | |
852dd866 EB |
3270 | /* Second, if we have recorded invariants to be hoisted, emit them. */ |
3271 | if (vec_safe_length (gnu_loop_info->invariants) > 0) | |
3272 | { | |
3273 | tree *iter; | |
3274 | unsigned int i; | |
3275 | FOR_EACH_VEC_ELT (*gnu_loop_info->invariants, i, iter) | |
3276 | add_stmt_with_node_force (*iter, gnat_node); | |
3277 | } | |
3278 | ||
3279 | /* Third, if loop vectorization is enabled and the iterations of the | |
1ddde8dc | 3280 | loop can easily be proved as independent, mark the loop. */ |
3bdf0b64 | 3281 | if (optimize >= 3 |
1ddde8dc EB |
3282 | && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt))) |
3283 | LOOP_STMT_IVDEP (gnu_loop_stmt) = 1; | |
3284 | ||
a1ab4c31 AC |
3285 | add_stmt (gnu_loop_stmt); |
3286 | gnat_poplevel (); | |
3287 | gnu_loop_stmt = end_stmt_group (); | |
3288 | } | |
3289 | ||
3290 | /* If we have an outer COND_EXPR, that's our result and this loop is its | |
7fda1596 | 3291 | "true" statement. Otherwise, the result is the LOOP_STMT. */ |
a1ab4c31 AC |
3292 | if (gnu_cond_expr) |
3293 | { | |
3294 | COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt; | |
66c14933 | 3295 | TREE_SIDE_EFFECTS (gnu_cond_expr) = 1; |
a1ab4c31 | 3296 | gnu_result = gnu_cond_expr; |
a1ab4c31 AC |
3297 | } |
3298 | else | |
3299 | gnu_result = gnu_loop_stmt; | |
3300 | ||
9771b263 | 3301 | gnu_loop_stack->pop (); |
a1ab4c31 AC |
3302 | |
3303 | return gnu_result; | |
3304 | } | |
ce2d0ce2 | 3305 | |
17a98a3d | 3306 | /* This page implements a form of Named Return Value optimization modeled |
71196d4e EB |
3307 | on the C++ optimization of the same name. The main difference is that |
3308 | we disregard any semantical considerations when applying it here, the | |
3309 | counterpart being that we don't try to apply it to semantically loaded | |
a0b8b1b7 | 3310 | return types, i.e. types with the TYPE_BY_REFERENCE_P flag set. |
71196d4e EB |
3311 | |
3312 | We consider a function body of the following GENERIC form: | |
3313 | ||
3314 | return_type R1; | |
3315 | [...] | |
3316 | RETURN_EXPR [<retval> = ...] | |
3317 | [...] | |
3318 | RETURN_EXPR [<retval> = R1] | |
3319 | [...] | |
3320 | return_type Ri; | |
3321 | [...] | |
3322 | RETURN_EXPR [<retval> = ...] | |
3323 | [...] | |
3324 | RETURN_EXPR [<retval> = Ri] | |
3325 | [...] | |
3326 | ||
13b6b773 EB |
3327 | where the Ri are not addressable and we try to fulfill a simple criterion |
3328 | that would make it possible to replace one or several Ri variables by the | |
3329 | single RESULT_DECL of the function. | |
71196d4e EB |
3330 | |
3331 | The first observation is that RETURN_EXPRs that don't directly reference | |
3332 | any of the Ri variables on the RHS of their assignment are transparent wrt | |
3333 | the optimization. This is because the Ri variables aren't addressable so | |
3334 | any transformation applied to them doesn't affect the RHS; moreover, the | |
3335 | assignment writes the full <retval> object so existing values are entirely | |
3336 | discarded. | |
3337 | ||
3338 | This property can be extended to some forms of RETURN_EXPRs that reference | |
3339 | the Ri variables, for example CONSTRUCTORs, but isn't true in the general | |
3340 | case, in particular when function calls are involved. | |
3341 | ||
3342 | Therefore the algorithm is as follows: | |
3343 | ||
3344 | 1. Collect the list of candidates for a Named Return Value (Ri variables | |
3345 | on the RHS of assignments of RETURN_EXPRs) as well as the list of the | |
3346 | other expressions on the RHS of such assignments. | |
3347 | ||
3348 | 2. Prune the members of the first list (candidates) that are referenced | |
3349 | by a member of the second list (expressions). | |
3350 | ||
3351 | 3. Extract a set of candidates with non-overlapping live ranges from the | |
3352 | first list. These are the Named Return Values. | |
3353 | ||
3354 | 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the | |
088b91c7 EB |
3355 | Named Return Values in the function with the RESULT_DECL. |
3356 | ||
3357 | If the function returns an unconstrained type, things are a bit different | |
3358 | because the anonymous return object is allocated on the secondary stack | |
3359 | and RESULT_DECL is only a pointer to it. Each return object can be of a | |
3360 | different size and is allocated separately so we need not care about the | |
13b6b773 EB |
3361 | addressability and the aforementioned overlapping issues. Therefore, we |
3362 | don't collect the other expressions and skip step #2 in the algorithm. */ | |
71196d4e EB |
3363 | |
3364 | struct nrv_data | |
3365 | { | |
3366 | bitmap nrv; | |
3367 | tree result; | |
088b91c7 | 3368 | Node_Id gnat_ret; |
6e2830c3 | 3369 | hash_set<tree> *visited; |
71196d4e EB |
3370 | }; |
3371 | ||
3372 | /* Return true if T is a Named Return Value. */ | |
3373 | ||
3374 | static inline bool | |
3375 | is_nrv_p (bitmap nrv, tree t) | |
3376 | { | |
3377 | return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t)); | |
3378 | } | |
3379 | ||
3380 | /* Helper function for walk_tree, used by finalize_nrv below. */ | |
3381 | ||
3382 | static tree | |
3383 | prune_nrv_r (tree *tp, int *walk_subtrees, void *data) | |
3384 | { | |
3385 | struct nrv_data *dp = (struct nrv_data *)data; | |
3386 | tree t = *tp; | |
3387 | ||
3388 | /* No need to walk into types or decls. */ | |
3389 | if (IS_TYPE_OR_DECL_P (t)) | |
3390 | *walk_subtrees = 0; | |
3391 | ||
3392 | if (is_nrv_p (dp->nrv, t)) | |
3393 | bitmap_clear_bit (dp->nrv, DECL_UID (t)); | |
3394 | ||
3395 | return NULL_TREE; | |
3396 | } | |
3397 | ||
3398 | /* Prune Named Return Values in BLOCK and return true if there is still a | |
3399 | Named Return Value in BLOCK or one of its sub-blocks. */ | |
3400 | ||
3401 | static bool | |
3402 | prune_nrv_in_block (bitmap nrv, tree block) | |
3403 | { | |
3404 | bool has_nrv = false; | |
3405 | tree t; | |
3406 | ||
3407 | /* First recurse on the sub-blocks. */ | |
3408 | for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t)) | |
3409 | has_nrv |= prune_nrv_in_block (nrv, t); | |
3410 | ||
3411 | /* Then make sure to keep at most one NRV per block. */ | |
3412 | for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t)) | |
3413 | if (is_nrv_p (nrv, t)) | |
3414 | { | |
3415 | if (has_nrv) | |
3416 | bitmap_clear_bit (nrv, DECL_UID (t)); | |
3417 | else | |
3418 | has_nrv = true; | |
3419 | } | |
3420 | ||
3421 | return has_nrv; | |
3422 | } | |
3423 | ||
3424 | /* Helper function for walk_tree, used by finalize_nrv below. */ | |
3425 | ||
3426 | static tree | |
3427 | finalize_nrv_r (tree *tp, int *walk_subtrees, void *data) | |
3428 | { | |
3429 | struct nrv_data *dp = (struct nrv_data *)data; | |
3430 | tree t = *tp; | |
3431 | ||
3432 | /* No need to walk into types. */ | |
3433 | if (TYPE_P (t)) | |
3434 | *walk_subtrees = 0; | |
3435 | ||
3436 | /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a | |
3437 | nop, but differs from using NULL_TREE in that it indicates that we care | |
3438 | about the value of the RESULT_DECL. */ | |
3439 | else if (TREE_CODE (t) == RETURN_EXPR | |
d864aeeb | 3440 | && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR) |
71196d4e | 3441 | { |
5d2a63dc | 3442 | tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1); |
71196d4e EB |
3443 | |
3444 | /* Strip useless conversions around the return value. */ | |
3445 | if (gnat_useless_type_conversion (ret_val)) | |
3446 | ret_val = TREE_OPERAND (ret_val, 0); | |
3447 | ||
3448 | if (is_nrv_p (dp->nrv, ret_val)) | |
5d2a63dc | 3449 | TREE_OPERAND (t, 0) = dp->result; |
71196d4e EB |
3450 | } |
3451 | ||
3452 | /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL, | |
3453 | if needed. */ | |
3454 | else if (TREE_CODE (t) == DECL_EXPR | |
3455 | && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t))) | |
3456 | { | |
3457 | tree var = DECL_EXPR_DECL (t), init; | |
3458 | ||
3459 | if (DECL_INITIAL (var)) | |
3460 | { | |
3461 | init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result, | |
3462 | DECL_INITIAL (var)); | |
3463 | SET_EXPR_LOCATION (init, EXPR_LOCATION (t)); | |
3464 | DECL_INITIAL (var) = NULL_TREE; | |
3465 | } | |
3466 | else | |
3467 | init = build_empty_stmt (EXPR_LOCATION (t)); | |
3468 | *tp = init; | |
3469 | ||
3470 | /* Identify the NRV to the RESULT_DECL for debugging purposes. */ | |
3471 | SET_DECL_VALUE_EXPR (var, dp->result); | |
3472 | DECL_HAS_VALUE_EXPR_P (var) = 1; | |
3473 | /* ??? Kludge to avoid an assertion failure during inlining. */ | |
3474 | DECL_SIZE (var) = bitsize_unit_node; | |
3475 | DECL_SIZE_UNIT (var) = size_one_node; | |
3476 | } | |
3477 | ||
3478 | /* And replace all uses of NRVs with the RESULT_DECL. */ | |
3479 | else if (is_nrv_p (dp->nrv, t)) | |
3480 | *tp = convert (TREE_TYPE (t), dp->result); | |
3481 | ||
3482 | /* Avoid walking into the same tree more than once. Unfortunately, we | |
088b91c7 EB |
3483 | can't just use walk_tree_without_duplicates because it would only |
3484 | call us for the first occurrence of NRVs in the function body. */ | |
6e2830c3 | 3485 | if (dp->visited->add (*tp)) |
088b91c7 EB |
3486 | *walk_subtrees = 0; |
3487 | ||
3488 | return NULL_TREE; | |
3489 | } | |
3490 | ||
3491 | /* Likewise, but used when the function returns an unconstrained type. */ | |
3492 | ||
3493 | static tree | |
3494 | finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data) | |
3495 | { | |
3496 | struct nrv_data *dp = (struct nrv_data *)data; | |
3497 | tree t = *tp; | |
3498 | ||
3499 | /* No need to walk into types. */ | |
3500 | if (TYPE_P (t)) | |
3501 | *walk_subtrees = 0; | |
3502 | ||
3503 | /* We need to see the DECL_EXPR of NRVs before any other references so we | |
3504 | walk the body of BIND_EXPR before walking its variables. */ | |
3505 | else if (TREE_CODE (t) == BIND_EXPR) | |
3506 | walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL); | |
3507 | ||
3508 | /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final | |
3509 | return value built by the allocator instead of the whole construct. */ | |
3510 | else if (TREE_CODE (t) == RETURN_EXPR | |
d864aeeb | 3511 | && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR) |
088b91c7 EB |
3512 | { |
3513 | tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1); | |
3514 | ||
3515 | /* This is the construct returned by the allocator. */ | |
3516 | if (TREE_CODE (ret_val) == COMPOUND_EXPR | |
3517 | && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR) | |
3518 | { | |
2117b9bb EB |
3519 | tree rhs = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1); |
3520 | ||
088b91c7 | 3521 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val))) |
2117b9bb | 3522 | ret_val = CONSTRUCTOR_ELT (rhs, 1)->value; |
088b91c7 | 3523 | else |
2117b9bb | 3524 | ret_val = rhs; |
088b91c7 EB |
3525 | } |
3526 | ||
3527 | /* Strip useless conversions around the return value. */ | |
3528 | if (gnat_useless_type_conversion (ret_val) | |
3529 | || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR) | |
3530 | ret_val = TREE_OPERAND (ret_val, 0); | |
3531 | ||
3532 | /* Strip unpadding around the return value. */ | |
3533 | if (TREE_CODE (ret_val) == COMPONENT_REF | |
3534 | && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0)))) | |
3535 | ret_val = TREE_OPERAND (ret_val, 0); | |
3536 | ||
3537 | /* Assign the new return value to the RESULT_DECL. */ | |
3538 | if (is_nrv_p (dp->nrv, ret_val)) | |
3539 | TREE_OPERAND (TREE_OPERAND (t, 0), 1) | |
3540 | = TREE_OPERAND (DECL_INITIAL (ret_val), 0); | |
3541 | } | |
3542 | ||
3543 | /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result | |
3544 | into a new variable. */ | |
3545 | else if (TREE_CODE (t) == DECL_EXPR | |
3546 | && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t))) | |
3547 | { | |
3548 | tree saved_current_function_decl = current_function_decl; | |
3549 | tree var = DECL_EXPR_DECL (t); | |
3550 | tree alloc, p_array, new_var, new_ret; | |
9771b263 DN |
3551 | vec<constructor_elt, va_gc> *v; |
3552 | vec_alloc (v, 2); | |
088b91c7 EB |
3553 | |
3554 | /* Create an artificial context to build the allocation. */ | |
3555 | current_function_decl = decl_function_context (var); | |
3556 | start_stmt_group (); | |
3557 | gnat_pushlevel (); | |
3558 | ||
3559 | /* This will return a COMPOUND_EXPR with the allocation in the first | |
3560 | arm and the final return value in the second arm. */ | |
3561 | alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var), | |
3562 | TREE_TYPE (dp->result), | |
3563 | Procedure_To_Call (dp->gnat_ret), | |
3564 | Storage_Pool (dp->gnat_ret), | |
3565 | Empty, false); | |
3566 | ||
3567 | /* The new variable is built as a reference to the allocated space. */ | |
3568 | new_var | |
3569 | = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var), | |
3570 | build_reference_type (TREE_TYPE (var))); | |
3571 | DECL_BY_REFERENCE (new_var) = 1; | |
3572 | ||
3573 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc))) | |
3574 | { | |
2117b9bb EB |
3575 | tree cst = TREE_OPERAND (alloc, 1); |
3576 | ||
088b91c7 EB |
3577 | /* The new initial value is a COMPOUND_EXPR with the allocation in |
3578 | the first arm and the value of P_ARRAY in the second arm. */ | |
3579 | DECL_INITIAL (new_var) | |
3580 | = build2 (COMPOUND_EXPR, TREE_TYPE (new_var), | |
3581 | TREE_OPERAND (alloc, 0), | |
2117b9bb | 3582 | CONSTRUCTOR_ELT (cst, 0)->value); |
088b91c7 EB |
3583 | |
3584 | /* Build a modified CONSTRUCTOR that references NEW_VAR. */ | |
3585 | p_array = TYPE_FIELDS (TREE_TYPE (alloc)); | |
3586 | CONSTRUCTOR_APPEND_ELT (v, p_array, | |
3587 | fold_convert (TREE_TYPE (p_array), new_var)); | |
3588 | CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array), | |
2117b9bb | 3589 | CONSTRUCTOR_ELT (cst, 1)->value); |
088b91c7 EB |
3590 | new_ret = build_constructor (TREE_TYPE (alloc), v); |
3591 | } | |
3592 | else | |
3593 | { | |
3594 | /* The new initial value is just the allocation. */ | |
3595 | DECL_INITIAL (new_var) = alloc; | |
3596 | new_ret = fold_convert (TREE_TYPE (alloc), new_var); | |
3597 | } | |
3598 | ||
3599 | gnat_pushdecl (new_var, Empty); | |
3600 | ||
3601 | /* Destroy the artificial context and insert the new statements. */ | |
3602 | gnat_zaplevel (); | |
3603 | *tp = end_stmt_group (); | |
3604 | current_function_decl = saved_current_function_decl; | |
3605 | ||
3606 | /* Chain NEW_VAR immediately after VAR and ignore the latter. */ | |
3607 | DECL_CHAIN (new_var) = DECL_CHAIN (var); | |
3608 | DECL_CHAIN (var) = new_var; | |
3609 | DECL_IGNORED_P (var) = 1; | |
3610 | ||
3611 | /* Save the new return value and the dereference of NEW_VAR. */ | |
3612 | DECL_INITIAL (var) | |
3613 | = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret, | |
3614 | build1 (INDIRECT_REF, TREE_TYPE (var), new_var)); | |
3615 | /* ??? Kludge to avoid messing up during inlining. */ | |
3616 | DECL_CONTEXT (var) = NULL_TREE; | |
3617 | } | |
3618 | ||
3619 | /* And replace all uses of NRVs with the dereference of NEW_VAR. */ | |
3620 | else if (is_nrv_p (dp->nrv, t)) | |
3621 | *tp = TREE_OPERAND (DECL_INITIAL (t), 1); | |
3622 | ||
3623 | /* Avoid walking into the same tree more than once. Unfortunately, we | |
3624 | can't just use walk_tree_without_duplicates because it would only | |
3625 | call us for the first occurrence of NRVs in the function body. */ | |
6e2830c3 | 3626 | if (dp->visited->add (*tp)) |
71196d4e EB |
3627 | *walk_subtrees = 0; |
3628 | ||
3629 | return NULL_TREE; | |
3630 | } | |
3631 | ||
81edaf2f EB |
3632 | /* Apply FUNC to all the sub-trees of nested functions in NODE. FUNC is called |
3633 | with the DATA and the address of each sub-tree. If FUNC returns a non-NULL | |
3634 | value, the traversal is stopped. */ | |
3635 | ||
3636 | static void | |
3637 | walk_nesting_tree (struct cgraph_node *node, walk_tree_fn func, void *data) | |
3638 | { | |
89576d86 JH |
3639 | for (node = first_nested_function (node); |
3640 | node; node = next_nested_function (node)) | |
81edaf2f EB |
3641 | { |
3642 | walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), func, data); | |
3643 | walk_nesting_tree (node, func, data); | |
3644 | } | |
3645 | } | |
3646 | ||
71196d4e EB |
3647 | /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap |
3648 | contains the candidates for Named Return Value and OTHER is a list of | |
088b91c7 | 3649 | the other return values. GNAT_RET is a representative return node. */ |
71196d4e EB |
3650 | |
3651 | static void | |
9771b263 | 3652 | finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret) |
71196d4e | 3653 | { |
71196d4e | 3654 | struct nrv_data data; |
088b91c7 | 3655 | walk_tree_fn func; |
71196d4e EB |
3656 | unsigned int i; |
3657 | tree iter; | |
3658 | ||
3659 | /* We shouldn't be applying the optimization to return types that we aren't | |
3660 | allowed to manipulate freely. */ | |
a0b8b1b7 | 3661 | gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl)))); |
71196d4e EB |
3662 | |
3663 | /* Prune the candidates that are referenced by other return values. */ | |
3664 | data.nrv = nrv; | |
3665 | data.result = NULL_TREE; | |
547bbe49 | 3666 | data.gnat_ret = Empty; |
71196d4e | 3667 | data.visited = NULL; |
547bbe49 | 3668 | FOR_EACH_VEC_SAFE_ELT (other, i, iter) |
71196d4e EB |
3669 | walk_tree_without_duplicates (&iter, prune_nrv_r, &data); |
3670 | if (bitmap_empty_p (nrv)) | |
3671 | return; | |
3672 | ||
3673 | /* Prune also the candidates that are referenced by nested functions. */ | |
81edaf2f | 3674 | walk_nesting_tree (cgraph_node::get_create (fndecl), prune_nrv_r, &data); |
71196d4e EB |
3675 | if (bitmap_empty_p (nrv)) |
3676 | return; | |
3677 | ||
3678 | /* Extract a set of NRVs with non-overlapping live ranges. */ | |
3679 | if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl))) | |
3680 | return; | |
3681 | ||
3682 | /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */ | |
3683 | data.nrv = nrv; | |
3684 | data.result = DECL_RESULT (fndecl); | |
088b91c7 | 3685 | data.gnat_ret = gnat_ret; |
6e2830c3 | 3686 | data.visited = new hash_set<tree>; |
89e037d0 | 3687 | if (TYPE_RETURN_BY_DIRECT_REF_P (TREE_TYPE (fndecl))) |
088b91c7 EB |
3688 | func = finalize_nrv_unc_r; |
3689 | else | |
3690 | func = finalize_nrv_r; | |
3691 | walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL); | |
6e2830c3 | 3692 | delete data.visited; |
71196d4e EB |
3693 | } |
3694 | ||
3695 | /* Return true if RET_VAL can be used as a Named Return Value for the | |
3696 | anonymous return object RET_OBJ. */ | |
3697 | ||
3698 | static bool | |
3699 | return_value_ok_for_nrv_p (tree ret_obj, tree ret_val) | |
3700 | { | |
3701 | if (TREE_CODE (ret_val) != VAR_DECL) | |
3702 | return false; | |
3703 | ||
3704 | if (TREE_THIS_VOLATILE (ret_val)) | |
3705 | return false; | |
3706 | ||
3707 | if (DECL_CONTEXT (ret_val) != current_function_decl) | |
3708 | return false; | |
3709 | ||
3710 | if (TREE_STATIC (ret_val)) | |
3711 | return false; | |
3712 | ||
13b6b773 EB |
3713 | /* For the constrained case, test for addressability. */ |
3714 | if (ret_obj && TREE_ADDRESSABLE (ret_val)) | |
71196d4e EB |
3715 | return false; |
3716 | ||
0b9cdb9a | 3717 | /* For the constrained case, test for overalignment. */ |
088b91c7 | 3718 | if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj)) |
71196d4e EB |
3719 | return false; |
3720 | ||
0b9cdb9a EB |
3721 | /* For the unconstrained case, test for bogus initialization. */ |
3722 | if (!ret_obj | |
3723 | && DECL_INITIAL (ret_val) | |
3724 | && TREE_CODE (DECL_INITIAL (ret_val)) == NULL_EXPR) | |
3725 | return false; | |
3726 | ||
71196d4e EB |
3727 | return true; |
3728 | } | |
3729 | ||
3730 | /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around | |
3731 | the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR | |
3732 | around RESULT_OBJ, which may be null in this case. */ | |
f3d34576 EB |
3733 | |
3734 | static tree | |
3735 | build_return_expr (tree ret_obj, tree ret_val) | |
3736 | { | |
3737 | tree result_expr; | |
3738 | ||
3739 | if (ret_val) | |
3740 | { | |
3741 | /* The gimplifier explicitly enforces the following invariant: | |
3742 | ||
3743 | RETURN_EXPR | |
3744 | | | |
d864aeeb | 3745 | INIT_EXPR |
f3d34576 EB |
3746 | / \ |
3747 | / \ | |
3748 | RET_OBJ ... | |
3749 | ||
3750 | As a consequence, type consistency dictates that we use the type | |
3751 | of the RET_OBJ as the operation type. */ | |
3752 | tree operation_type = TREE_TYPE (ret_obj); | |
3753 | ||
d864aeeb EB |
3754 | /* Convert the right operand to the operation type. Note that this is |
3755 | the transformation applied in the INIT_EXPR case of build_binary_op, | |
f3d34576 EB |
3756 | with the assumption that the type cannot involve a placeholder. */ |
3757 | if (operation_type != TREE_TYPE (ret_val)) | |
3758 | ret_val = convert (operation_type, ret_val); | |
3759 | ||
d864aeeb EB |
3760 | /* We always can use an INIT_EXPR for the return object. */ |
3761 | result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val); | |
71196d4e EB |
3762 | |
3763 | /* If the function returns an aggregate type, find out whether this is | |
3764 | a candidate for Named Return Value. If so, record it. Otherwise, | |
3765 | if this is an expression of some kind, record it elsewhere. */ | |
3766 | if (optimize | |
09248547 | 3767 | && !optimize_debug |
71196d4e EB |
3768 | && AGGREGATE_TYPE_P (operation_type) |
3769 | && !TYPE_IS_FAT_POINTER_P (operation_type) | |
f563ce55 | 3770 | && TYPE_MODE (operation_type) == BLKmode |
71196d4e EB |
3771 | && aggregate_value_p (operation_type, current_function_decl)) |
3772 | { | |
71196d4e EB |
3773 | /* Strip useless conversions around the return value. */ |
3774 | if (gnat_useless_type_conversion (ret_val)) | |
3775 | ret_val = TREE_OPERAND (ret_val, 0); | |
3776 | ||
3777 | /* Now apply the test to the return value. */ | |
3778 | if (return_value_ok_for_nrv_p (ret_obj, ret_val)) | |
3779 | { | |
3780 | if (!f_named_ret_val) | |
3781 | f_named_ret_val = BITMAP_GGC_ALLOC (); | |
3782 | bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val)); | |
3783 | } | |
3784 | ||
3785 | /* Note that we need not care about CONSTRUCTORs here, as they are | |
3786 | totally transparent given the read-compose-write semantics of | |
3787 | assignments from CONSTRUCTORs. */ | |
3788 | else if (EXPR_P (ret_val)) | |
9771b263 | 3789 | vec_safe_push (f_other_ret_val, ret_val); |
71196d4e | 3790 | } |
f3d34576 EB |
3791 | } |
3792 | else | |
3793 | result_expr = ret_obj; | |
3794 | ||
3795 | return build1 (RETURN_EXPR, void_type_node, result_expr); | |
3796 | } | |
ce2d0ce2 | 3797 | |
6d16658d BD |
3798 | /* Subroutine of gnat_to_gnu to translate the At_End_Proc of GNAT_NODE, an |
3799 | N_Block_Statement or N_Handled_Sequence_Of_Statements or N_*_Body node. | |
3800 | ||
3801 | To invoked the GCC mechanism, we call add_cleanup and when we leave the | |
3802 | group, end_stmt_group will create the TRY_FINALLY_EXPR construct. */ | |
3803 | ||
3804 | static void | |
3805 | At_End_Proc_to_gnu (Node_Id gnat_node) | |
3806 | { | |
3807 | tree proc_decl = gnat_to_gnu (At_End_Proc (gnat_node)); | |
0b66f882 | 3808 | Node_Id gnat_end_label; |
6d16658d BD |
3809 | |
3810 | /* When not optimizing, disable inlining of finalizers as this can | |
3811 | create a more complex CFG in the parent function. */ | |
3812 | if (!optimize || optimize_debug) | |
3813 | DECL_DECLARED_INLINE_P (proc_decl) = 0; | |
3814 | ||
0b66f882 EB |
3815 | /* Retrieve the end label attached to the node, if any. */ |
3816 | if (Nkind (gnat_node) == N_Handled_Sequence_Of_Statements) | |
3817 | gnat_end_label = End_Label (gnat_node); | |
3818 | else if (Present (Handled_Statement_Sequence (gnat_node))) | |
3819 | gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node)); | |
3820 | else | |
3821 | gnat_end_label = Empty; | |
3822 | ||
6d16658d BD |
3823 | /* If there is no end label attached, we use the location of the At_End |
3824 | procedure because Expand_Cleanup_Actions might reset the location of | |
0b66f882 | 3825 | the enclosing construct to that of an inner statement. */ |
6d16658d | 3826 | add_cleanup (build_call_n_expr (proc_decl, 0), |
0b66f882 EB |
3827 | Present (gnat_end_label) |
3828 | ? gnat_end_label : At_End_Proc (gnat_node)); | |
6d16658d BD |
3829 | } |
3830 | ||
e08f1aad | 3831 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Subprogram_Body. */ |
a1ab4c31 AC |
3832 | |
3833 | static void | |
3834 | Subprogram_Body_to_gnu (Node_Id gnat_node) | |
3835 | { | |
a1ab4c31 AC |
3836 | /* The defining identifier for the subprogram body. Note that if a |
3837 | specification has appeared before for this body, then the identifier | |
e08f1aad EB |
3838 | occurring in that specification will also be a defining identifier |
3839 | and calls to this subprogram will point to that specification. */ | |
4e279bd9 | 3840 | Entity_Id gnat_subprog |
a1ab4c31 AC |
3841 | = (Present (Corresponding_Spec (gnat_node)) |
3842 | ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node)); | |
e08f1aad | 3843 | /* The FUNCTION_DECL node corresponding to the defining identifier. */ |
4e279bd9 | 3844 | tree gnu_subprog; |
d47d0a8d EB |
3845 | /* Its RESULT_DECL node. */ |
3846 | tree gnu_result_decl; | |
35a382b8 | 3847 | /* Its FUNCTION_TYPE node. */ |
a1ab4c31 | 3848 | tree gnu_subprog_type; |
35a382b8 | 3849 | /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */ |
a1ab4c31 | 3850 | tree gnu_cico_list; |
35a382b8 | 3851 | /* The entry in the CI_CO_LIST that represents a function return, if any. */ |
e08f1aad EB |
3852 | tree gnu_return_var_elmt; |
3853 | /* Its source location. */ | |
40e63ada | 3854 | location_t locus; |
a1ab4c31 | 3855 | |
4e279bd9 EB |
3856 | /* If this is a generic subprogram or it has been eliminated, ignore it. */ |
3857 | if (Is_Generic_Subprogram (gnat_subprog) || Is_Eliminated (gnat_subprog)) | |
a1ab4c31 AC |
3858 | return; |
3859 | ||
3860 | /* If this subprogram acts as its own spec, define it. Otherwise, just get | |
3861 | the already-elaborated tree node. However, if this subprogram had its | |
3862 | elaboration deferred, we will already have made a tree node for it. So | |
3863 | treat it as not being defined in that case. Such a subprogram cannot | |
3864 | have an address clause or a freeze node, so this test is safe, though it | |
3865 | does disable some otherwise-useful error checking. */ | |
4e279bd9 EB |
3866 | gnu_subprog |
3867 | = gnat_to_gnu_entity (gnat_subprog, NULL_TREE, | |
a1ab4c31 | 3868 | Acts_As_Spec (gnat_node) |
4e279bd9 EB |
3869 | && !present_gnu_tree (gnat_subprog)); |
3870 | DECL_FUNCTION_IS_DEF (gnu_subprog) = true; | |
3871 | gnu_result_decl = DECL_RESULT (gnu_subprog); | |
3872 | gnu_subprog_type = TREE_TYPE (gnu_subprog); | |
35a382b8 | 3873 | gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type); |
2374257a EB |
3874 | if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node) |
3875 | gnu_return_var_elmt = gnu_cico_list; | |
e08f1aad EB |
3876 | else |
3877 | gnu_return_var_elmt = NULL_TREE; | |
a1ab4c31 | 3878 | |
d47d0a8d | 3879 | /* If the function returns by invisible reference, make it explicit in the |
e08f1aad | 3880 | function body. See gnat_to_gnu_subprog_type for more details. */ |
2374257a | 3881 | if (TREE_ADDRESSABLE (gnu_subprog_type)) |
d47d0a8d EB |
3882 | { |
3883 | TREE_TYPE (gnu_result_decl) | |
3884 | = build_reference_type (TREE_TYPE (gnu_result_decl)); | |
3885 | relayout_decl (gnu_result_decl); | |
3886 | } | |
3887 | ||
40e63ada | 3888 | /* Set the line number in the decl to correspond to that of the body. */ |
4e279bd9 | 3889 | if (DECL_IGNORED_P (gnu_subprog)) |
a45918f8 | 3890 | locus = UNKNOWN_LOCATION; |
4e279bd9 | 3891 | else if (!Sloc_to_locus (Sloc (gnat_node), &locus, false, gnu_subprog)) |
0c8af82a | 3892 | locus = input_location; |
4e279bd9 | 3893 | DECL_SOURCE_LOCATION (gnu_subprog) = locus; |
a1ab4c31 | 3894 | |
44662f68 | 3895 | /* Try to create a bona-fide thunk and hand it over to the middle-end. */ |
4e279bd9 | 3896 | if (Is_Thunk (gnat_subprog) |
89e037d0 | 3897 | && !Is_Secondary_Stack_Thunk (gnat_subprog) |
4e279bd9 | 3898 | && maybe_make_gnu_thunk (gnat_subprog, gnu_subprog)) |
44662f68 EB |
3899 | return; |
3900 | ||
a1ab4c31 | 3901 | /* Initialize the information structure for the function. */ |
4e279bd9 | 3902 | allocate_struct_function (gnu_subprog, false); |
e08f1aad EB |
3903 | language_function *gnu_subprog_lang = ggc_cleared_alloc<language_function> (); |
3904 | DECL_STRUCT_FUNCTION (gnu_subprog)->language = gnu_subprog_lang; | |
4e279bd9 | 3905 | DECL_STRUCT_FUNCTION (gnu_subprog)->function_start_locus = locus; |
58c8f770 | 3906 | set_cfun (NULL); |
a1ab4c31 | 3907 | |
4e279bd9 | 3908 | begin_subprog_body (gnu_subprog); |
a1ab4c31 | 3909 | |
2374257a EB |
3910 | /* If there are copy-in/copy-out parameters, we need to ensure that they are |
3911 | properly copied out by the return statement. We do this by making a new | |
3912 | block and converting any return into a goto to a label at the end of the | |
3913 | block. */ | |
a963da4d EB |
3914 | if (gnu_cico_list) |
3915 | { | |
e08f1aad | 3916 | tree gnu_return_var; |
35a382b8 | 3917 | |
b16b6cc9 | 3918 | vec_safe_push (gnu_return_label_stack, |
a963da4d EB |
3919 | create_artificial_label (input_location)); |
3920 | ||
3921 | start_stmt_group (); | |
3922 | gnat_pushlevel (); | |
3923 | ||
2374257a EB |
3924 | /* If this is a function with copy-in/copy-out parameters and which does |
3925 | not return by invisible reference, we also need a variable for the | |
3926 | return value to be placed. */ | |
3927 | if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type)) | |
35a382b8 EB |
3928 | { |
3929 | tree gnu_return_type | |
3930 | = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt)); | |
3931 | ||
35a382b8 EB |
3932 | gnu_return_var |
3933 | = create_var_decl (get_identifier ("RETVAL"), NULL_TREE, | |
2056c5ed EB |
3934 | gnu_return_type, NULL_TREE, |
3935 | false, false, false, false, false, | |
4e279bd9 | 3936 | true, false, NULL, gnat_subprog); |
35a382b8 EB |
3937 | TREE_VALUE (gnu_return_var_elmt) = gnu_return_var; |
3938 | } | |
e08f1aad EB |
3939 | else |
3940 | gnu_return_var = NULL_TREE; | |
35a382b8 | 3941 | |
9771b263 | 3942 | vec_safe_push (gnu_return_var_stack, gnu_return_var); |
35a382b8 | 3943 | |
a963da4d EB |
3944 | /* See whether there are parameters for which we don't have a GCC tree |
3945 | yet. These must be Out parameters. Make a VAR_DECL for them and | |
3946 | put it into TYPE_CI_CO_LIST, which must contain an empty entry too. | |
3947 | We can match up the entries because TYPE_CI_CO_LIST is in the order | |
3948 | of the parameters. */ | |
e08f1aad | 3949 | for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog); |
a963da4d EB |
3950 | Present (gnat_param); |
3951 | gnat_param = Next_Formal_With_Extras (gnat_param)) | |
3952 | if (!present_gnu_tree (gnat_param)) | |
3953 | { | |
3954 | tree gnu_cico_entry = gnu_cico_list; | |
54625ca1 | 3955 | tree gnu_decl; |
a963da4d EB |
3956 | |
3957 | /* Skip any entries that have been already filled in; they must | |
3958 | correspond to In Out parameters. */ | |
3959 | while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry)) | |
3960 | gnu_cico_entry = TREE_CHAIN (gnu_cico_entry); | |
3961 | ||
54625ca1 | 3962 | /* Do any needed dereferences for by-ref objects. */ |
afc737f0 | 3963 | gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, true); |
54625ca1 EB |
3964 | gcc_assert (DECL_P (gnu_decl)); |
3965 | if (DECL_BY_REF_P (gnu_decl)) | |
3966 | gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl); | |
3967 | ||
a963da4d EB |
3968 | /* Do any needed references for padded types. */ |
3969 | TREE_VALUE (gnu_cico_entry) | |
54625ca1 | 3970 | = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl); |
a963da4d EB |
3971 | } |
3972 | } | |
3973 | else | |
9771b263 | 3974 | vec_safe_push (gnu_return_label_stack, NULL_TREE); |
a1ab4c31 AC |
3975 | |
3976 | /* Get a tree corresponding to the code for the subprogram. */ | |
3977 | start_stmt_group (); | |
3978 | gnat_pushlevel (); | |
3979 | ||
e08f1aad | 3980 | /* First translate the declarations of the subprogram. */ |
6d16658d | 3981 | process_decls (Declarations (gnat_node), Empty, true, true); |
a1ab4c31 | 3982 | |
e08f1aad EB |
3983 | /* Then generate the code of the subprogram itself. A return statement will |
3984 | be present and any Out parameters will be handled there. */ | |
a1ab4c31 | 3985 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); |
e08f1aad | 3986 | |
6d16658d BD |
3987 | /* Process the At_End_Proc, if any. */ |
3988 | if (Present (At_End_Proc (gnat_node))) | |
3989 | At_End_Proc_to_gnu (gnat_node); | |
3990 | ||
a1ab4c31 | 3991 | gnat_poplevel (); |
e08f1aad | 3992 | tree gnu_result = end_stmt_group (); |
a1ab4c31 | 3993 | |
cc997e0c EB |
3994 | /* Attempt setting the end_locus of our GCC body tree, typically a BIND_EXPR, |
3995 | then the end_locus of our GCC subprogram declaration tree. */ | |
3996 | set_end_locus_from_node (gnu_result, gnat_node); | |
4e279bd9 | 3997 | set_end_locus_from_node (gnu_subprog, gnat_node); |
cc997e0c | 3998 | |
0394741f EB |
3999 | /* If we populated the parameter attributes cache, we need to make sure that |
4000 | the cached expressions are evaluated on all the possible paths leading to | |
4001 | their uses. So we force their evaluation on entry of the function. */ | |
e08f1aad | 4002 | vec<parm_attr, va_gc> *cache = gnu_subprog_lang->parm_attr_cache; |
0394741f EB |
4003 | if (cache) |
4004 | { | |
4005 | struct parm_attr_d *pa; | |
4006 | int i; | |
4007 | ||
4008 | start_stmt_group (); | |
4009 | ||
9771b263 | 4010 | FOR_EACH_VEC_ELT (*cache, i, pa) |
0394741f EB |
4011 | { |
4012 | if (pa->first) | |
4013 | add_stmt_with_node_force (pa->first, gnat_node); | |
4014 | if (pa->last) | |
4015 | add_stmt_with_node_force (pa->last, gnat_node); | |
4016 | if (pa->length) | |
4017 | add_stmt_with_node_force (pa->length, gnat_node); | |
4018 | } | |
4019 | ||
4020 | add_stmt (gnu_result); | |
4021 | gnu_result = end_stmt_group (); | |
f3d34576 | 4022 | |
e08f1aad | 4023 | gnu_subprog_lang->parm_attr_cache = NULL; |
0394741f EB |
4024 | } |
4025 | ||
a963da4d EB |
4026 | /* If we are dealing with a return from an Ada procedure with parameters |
4027 | passed by copy-in/copy-out, we need to return a record containing the | |
4028 | final values of these parameters. If the list contains only one entry, | |
4029 | return just that entry though. | |
4030 | ||
4031 | For a full description of the copy-in/copy-out parameter mechanism, see | |
4032 | the part of the gnat_to_gnu_entity routine dealing with the translation | |
4033 | of subprograms. | |
4034 | ||
4035 | We need to make a block that contains the definition of that label and | |
4036 | the copying of the return value. It first contains the function, then | |
4037 | the label and copy statement. */ | |
4038 | if (gnu_cico_list) | |
4039 | { | |
2374257a EB |
4040 | const Node_Id gnat_end_label |
4041 | = End_Label (Handled_Statement_Sequence (gnat_node)); | |
a963da4d | 4042 | |
df2abf54 EB |
4043 | gnu_return_var_stack->pop (); |
4044 | ||
a963da4d EB |
4045 | add_stmt (gnu_result); |
4046 | add_stmt (build1 (LABEL_EXPR, void_type_node, | |
9771b263 | 4047 | gnu_return_label_stack->last ())); |
a963da4d | 4048 | |
2374257a EB |
4049 | /* If this is a function which returns by invisible reference, the |
4050 | return value has already been dealt with at the return statements, | |
4051 | so we only need to indirectly copy out the parameters. */ | |
4052 | if (TREE_ADDRESSABLE (gnu_subprog_type)) | |
4053 | { | |
4054 | tree gnu_ret_deref | |
4055 | = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl); | |
4056 | tree t; | |
4057 | ||
4058 | gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node); | |
4059 | ||
4060 | for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t)) | |
4061 | { | |
4062 | tree gnu_field_deref | |
64235766 | 4063 | = build_component_ref (gnu_ret_deref, TREE_PURPOSE (t), true); |
2374257a EB |
4064 | gnu_result = build2 (MODIFY_EXPR, void_type_node, |
4065 | gnu_field_deref, TREE_VALUE (t)); | |
4066 | add_stmt_with_node (gnu_result, gnat_end_label); | |
4067 | } | |
4068 | } | |
4069 | ||
4070 | /* Otherwise, if this is a procedure or a function which does not return | |
4071 | by invisible reference, we can do a direct block-copy out. */ | |
a963da4d | 4072 | else |
2374257a EB |
4073 | { |
4074 | tree gnu_retval; | |
4075 | ||
4076 | if (list_length (gnu_cico_list) == 1) | |
4077 | gnu_retval = TREE_VALUE (gnu_cico_list); | |
4078 | else | |
4079 | gnu_retval | |
4080 | = build_constructor_from_list (TREE_TYPE (gnu_subprog_type), | |
4081 | gnu_cico_list); | |
4082 | ||
4083 | gnu_result = build_return_expr (gnu_result_decl, gnu_retval); | |
4084 | add_stmt_with_node (gnu_result, gnat_end_label); | |
4085 | } | |
a963da4d | 4086 | |
a963da4d EB |
4087 | gnat_poplevel (); |
4088 | gnu_result = end_stmt_group (); | |
4089 | } | |
4090 | ||
9771b263 | 4091 | gnu_return_label_stack->pop (); |
a963da4d | 4092 | |
48a24fcf TG |
4093 | /* On SEH targets, install an exception handler around the main entry |
4094 | point to catch unhandled exceptions. */ | |
4e279bd9 | 4095 | if (DECL_NAME (gnu_subprog) == main_identifier_node |
48a24fcf TG |
4096 | && targetm_common.except_unwind_info (&global_options) == UI_SEH) |
4097 | { | |
4098 | tree t; | |
4099 | tree etype; | |
4100 | ||
4101 | t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), | |
4102 | 1, integer_zero_node); | |
4103 | t = build_call_n_expr (unhandled_except_decl, 1, t); | |
4104 | ||
4105 | etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl); | |
4106 | etype = tree_cons (NULL_TREE, etype, NULL_TREE); | |
4107 | ||
4108 | t = build2 (CATCH_EXPR, void_type_node, etype, t); | |
4109 | gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result), | |
4110 | gnu_result, t); | |
4111 | } | |
4112 | ||
f3d34576 EB |
4113 | end_subprog_body (gnu_result); |
4114 | ||
f4cd2542 EB |
4115 | /* Finally annotate the parameters and disconnect the trees for parameters |
4116 | that we have turned into variables since they are now unusable. */ | |
e08f1aad | 4117 | for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog); |
a1ab4c31 AC |
4118 | Present (gnat_param); |
4119 | gnat_param = Next_Formal_With_Extras (gnat_param)) | |
f4cd2542 EB |
4120 | { |
4121 | tree gnu_param = get_gnu_tree (gnat_param); | |
0c700259 EB |
4122 | bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL); |
4123 | ||
f4cd2542 | 4124 | annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE, |
491f54a7 | 4125 | DECL_BY_REF_P (gnu_param)); |
0c700259 EB |
4126 | |
4127 | if (is_var_decl) | |
f4cd2542 EB |
4128 | save_gnu_tree (gnat_param, NULL_TREE, false); |
4129 | } | |
a1ab4c31 | 4130 | |
0d24bf76 | 4131 | /* Disconnect the variable created for the return value. */ |
35a382b8 EB |
4132 | if (gnu_return_var_elmt) |
4133 | TREE_VALUE (gnu_return_var_elmt) = void_type_node; | |
4134 | ||
71196d4e EB |
4135 | /* If the function returns an aggregate type and we have candidates for |
4136 | a Named Return Value, finalize the optimization. */ | |
e08f1aad | 4137 | if (optimize && !optimize_debug && gnu_subprog_lang->named_ret_val) |
71196d4e | 4138 | { |
4e279bd9 | 4139 | finalize_nrv (gnu_subprog, |
e08f1aad EB |
4140 | gnu_subprog_lang->named_ret_val, |
4141 | gnu_subprog_lang->other_ret_val, | |
4142 | gnu_subprog_lang->gnat_ret); | |
4143 | gnu_subprog_lang->named_ret_val = NULL; | |
4144 | gnu_subprog_lang->other_ret_val = NULL; | |
71196d4e EB |
4145 | } |
4146 | ||
87411e95 EB |
4147 | /* If this is an inlined external function that has been marked uninlinable, |
4148 | drop the body and stop there. Otherwise compile the body. */ | |
4e279bd9 EB |
4149 | if (DECL_EXTERNAL (gnu_subprog) && DECL_UNINLINABLE (gnu_subprog)) |
4150 | DECL_SAVED_TREE (gnu_subprog) = NULL_TREE; | |
87411e95 | 4151 | else |
4e279bd9 | 4152 | rest_of_subprog_body_compilation (gnu_subprog); |
a1ab4c31 | 4153 | } |
ce2d0ce2 | 4154 | |
17a98a3d EB |
4155 | /* The type of an atomic access. */ |
4156 | ||
4157 | typedef enum { NOT_ATOMIC, SIMPLE_ATOMIC, OUTER_ATOMIC } atomic_acces_t; | |
4158 | ||
4159 | /* Return true if GNAT_NODE references an Atomic entity. This is modeled on | |
4160 | the Is_Atomic_Object predicate of the front-end, but additionally handles | |
4161 | explicit dereferences. */ | |
033ba5bf EB |
4162 | |
4163 | static bool | |
f797c2b7 | 4164 | node_is_atomic (Node_Id gnat_node) |
033ba5bf | 4165 | { |
f797c2b7 | 4166 | Entity_Id gnat_entity; |
033ba5bf | 4167 | |
f797c2b7 | 4168 | switch (Nkind (gnat_node)) |
033ba5bf | 4169 | { |
f797c2b7 EB |
4170 | case N_Identifier: |
4171 | case N_Expanded_Name: | |
4172 | gnat_entity = Entity (gnat_node); | |
1141fbd7 | 4173 | if (!Is_Object (gnat_entity)) |
f797c2b7 | 4174 | break; |
1141fbd7 EB |
4175 | return Is_Atomic (gnat_entity) |
4176 | || (Is_Atomic (Etype (gnat_entity)) | |
4177 | && !simple_constant_p (gnat_entity)); | |
f797c2b7 EB |
4178 | |
4179 | case N_Selected_Component: | |
17a98a3d EB |
4180 | return Is_Atomic (Etype (gnat_node)) |
4181 | || Is_Atomic (Entity (Selector_Name (gnat_node))); | |
f797c2b7 EB |
4182 | |
4183 | case N_Indexed_Component: | |
17a98a3d EB |
4184 | return Is_Atomic (Etype (gnat_node)) |
4185 | || Has_Atomic_Components (Etype (Prefix (gnat_node))) | |
4186 | || (Is_Entity_Name (Prefix (gnat_node)) | |
4187 | && Has_Atomic_Components (Entity (Prefix (gnat_node)))); | |
f797c2b7 EB |
4188 | |
4189 | case N_Explicit_Dereference: | |
4190 | return Is_Atomic (Etype (gnat_node)); | |
4191 | ||
4192 | default: | |
4193 | break; | |
033ba5bf EB |
4194 | } |
4195 | ||
f797c2b7 EB |
4196 | return false; |
4197 | } | |
4198 | ||
17a98a3d | 4199 | /* Return true if GNAT_NODE references a Volatile_Full_Access entity. This is |
2cee58d8 EB |
4200 | modeled on the Is_Volatile_Full_Access_Object predicate of the front-end, |
4201 | but additionally handles explicit dereferences. */ | |
f797c2b7 EB |
4202 | |
4203 | static bool | |
17a98a3d | 4204 | node_is_volatile_full_access (Node_Id gnat_node) |
f797c2b7 EB |
4205 | { |
4206 | Entity_Id gnat_entity; | |
4207 | ||
4208 | switch (Nkind (gnat_node)) | |
033ba5bf | 4209 | { |
033ba5bf | 4210 | case N_Identifier: |
f797c2b7 EB |
4211 | case N_Expanded_Name: |
4212 | gnat_entity = Entity (gnat_node); | |
f1bc6414 EB |
4213 | if (!Is_Object (gnat_entity)) |
4214 | break; | |
f797c2b7 | 4215 | return Is_Volatile_Full_Access (gnat_entity) |
1141fbd7 EB |
4216 | || (Is_Volatile_Full_Access (Etype (gnat_entity)) |
4217 | && !simple_constant_p (gnat_entity)); | |
f797c2b7 | 4218 | |
033ba5bf | 4219 | case N_Selected_Component: |
17a98a3d EB |
4220 | return Is_Volatile_Full_Access (Etype (gnat_node)) |
4221 | || Is_Volatile_Full_Access (Entity (Selector_Name (gnat_node))); | |
f797c2b7 EB |
4222 | |
4223 | case N_Indexed_Component: | |
4224 | case N_Explicit_Dereference: | |
4225 | return Is_Volatile_Full_Access (Etype (gnat_node)); | |
033ba5bf EB |
4226 | |
4227 | default: | |
f797c2b7 | 4228 | break; |
033ba5bf EB |
4229 | } |
4230 | ||
f797c2b7 EB |
4231 | return false; |
4232 | } | |
4233 | ||
17a98a3d | 4234 | /* Return true if GNAT_NODE references a component of a larger object. */ |
f797c2b7 | 4235 | |
17a98a3d EB |
4236 | static inline bool |
4237 | node_is_component (Node_Id gnat_node) | |
f797c2b7 | 4238 | { |
17a98a3d EB |
4239 | const Node_Kind k = Nkind (gnat_node); |
4240 | return | |
4241 | (k == N_Indexed_Component || k == N_Selected_Component || k == N_Slice); | |
f797c2b7 EB |
4242 | } |
4243 | ||
17a98a3d EB |
4244 | /* Compute whether GNAT_NODE requires atomic access and set TYPE to the type |
4245 | of access and SYNC according to the associated synchronization setting. | |
241125b2 | 4246 | |
17a98a3d | 4247 | We implement 3 different semantics of atomicity in this function: |
241125b2 | 4248 | |
17a98a3d | 4249 | 1. the Ada 95/2005/2012 semantics of the Atomic aspect/pragma, |
81e68a19 | 4250 | 2. the Ada 2022 semantics of the Atomic aspect/pragma, |
17a98a3d | 4251 | 3. the semantics of the Volatile_Full_Access GNAT aspect/pragma. |
f797c2b7 | 4252 | |
17a98a3d | 4253 | They are mutually exclusive and the FE should have rejected conflicts. */ |
f797c2b7 | 4254 | |
17a98a3d EB |
4255 | static void |
4256 | get_atomic_access (Node_Id gnat_node, atomic_acces_t *type, bool *sync) | |
f797c2b7 | 4257 | { |
17a98a3d | 4258 | Node_Id gnat_parent, gnat_temp; |
f797c2b7 | 4259 | unsigned char attr_id; |
f797c2b7 | 4260 | |
17a98a3d EB |
4261 | /* First, scan the parent to filter out irrelevant cases. */ |
4262 | gnat_parent = Parent (gnat_node); | |
f797c2b7 | 4263 | switch (Nkind (gnat_parent)) |
033ba5bf EB |
4264 | { |
4265 | case N_Attribute_Reference: | |
4266 | attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent)); | |
4267 | /* Do not mess up machine code insertions. */ | |
4268 | if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output) | |
17a98a3d | 4269 | goto not_atomic; |
f797c2b7 EB |
4270 | |
4271 | /* Nothing to do if we are the prefix of an attribute, since we do not | |
4272 | want an atomic access for things like 'Size. */ | |
4273 | ||
9c453de7 | 4274 | /* ... fall through ... */ |
f797c2b7 EB |
4275 | |
4276 | case N_Reference: | |
4277 | /* The N_Reference node is like an attribute. */ | |
4278 | if (Prefix (gnat_parent) == gnat_node) | |
17a98a3d | 4279 | goto not_atomic; |
033ba5bf EB |
4280 | break; |
4281 | ||
4282 | case N_Object_Renaming_Declaration: | |
f797c2b7 EB |
4283 | /* Nothing to do for the identifier in an object renaming declaration, |
4284 | the renaming itself does not need atomic access. */ | |
17a98a3d | 4285 | goto not_atomic; |
033ba5bf EB |
4286 | |
4287 | default: | |
4288 | break; | |
4289 | } | |
4290 | ||
17a98a3d EB |
4291 | /* Now strip any type conversion from GNAT_NODE. */ |
4292 | if (Nkind (gnat_node) == N_Type_Conversion | |
4293 | || Nkind (gnat_node) == N_Unchecked_Type_Conversion) | |
4294 | gnat_node = Expression (gnat_node); | |
f797c2b7 | 4295 | |
17a98a3d | 4296 | /* Up to Ada 2012, for Atomic itself, only reads and updates of the object as |
81e68a19 | 4297 | a whole require atomic access (RM C.6(15)). But, starting with Ada 2022, |
17a98a3d EB |
4298 | reads of or writes to a nonatomic subcomponent of the object also require |
4299 | atomic access (RM C.6(19)). */ | |
4300 | if (node_is_atomic (gnat_node)) | |
4301 | { | |
4302 | bool as_a_whole = true; | |
f797c2b7 | 4303 | |
17a98a3d EB |
4304 | /* If we are the prefix of the parent, then the access is partial. */ |
4305 | for (gnat_temp = gnat_node, gnat_parent = Parent (gnat_temp); | |
4306 | node_is_component (gnat_parent) && Prefix (gnat_parent) == gnat_temp; | |
4307 | gnat_temp = gnat_parent, gnat_parent = Parent (gnat_temp)) | |
81e68a19 | 4308 | if (Ada_Version < Ada_2022 || node_is_atomic (gnat_parent)) |
17a98a3d EB |
4309 | goto not_atomic; |
4310 | else | |
4311 | as_a_whole = false; | |
f797c2b7 | 4312 | |
17a98a3d EB |
4313 | /* We consider that partial accesses are not sequential actions and, |
4314 | therefore, do not require synchronization. */ | |
4315 | *type = SIMPLE_ATOMIC; | |
4316 | *sync = as_a_whole ? Atomic_Sync_Required (gnat_node) : false; | |
4317 | return; | |
4318 | } | |
f797c2b7 | 4319 | |
17a98a3d EB |
4320 | /* Look for an outer atomic access of a nonatomic subcomponent. Note that, |
4321 | for VFA, we do this before looking at the node itself because we need to | |
4322 | access the outermost VFA object atomically, unlike for Atomic where it is | |
4323 | the innermost atomic object (RM C.6(19)). */ | |
4324 | for (gnat_temp = gnat_node; | |
4325 | node_is_component (gnat_temp); | |
4326 | gnat_temp = Prefix (gnat_temp)) | |
81e68a19 | 4327 | if ((Ada_Version >= Ada_2022 && node_is_atomic (Prefix (gnat_temp))) |
17a98a3d EB |
4328 | || node_is_volatile_full_access (Prefix (gnat_temp))) |
4329 | { | |
4330 | *type = OUTER_ATOMIC; | |
4331 | *sync = false; | |
4332 | return; | |
4333 | } | |
4334 | ||
4335 | /* Unlike Atomic, accessing a VFA object always requires atomic access. */ | |
4336 | if (node_is_volatile_full_access (gnat_node)) | |
4337 | { | |
4338 | *type = SIMPLE_ATOMIC; | |
4339 | *sync = false; | |
4340 | return; | |
4341 | } | |
4342 | ||
4343 | not_atomic: | |
4344 | *type = NOT_ATOMIC; | |
4345 | *sync = false; | |
033ba5bf | 4346 | } |
ce2d0ce2 EB |
4347 | |
4348 | /* Return true if GNAT_NODE requires simple atomic access and, if so, set SYNC | |
17a98a3d EB |
4349 | according to the associated synchronization setting. */ |
4350 | ||
4351 | static inline bool | |
4352 | simple_atomic_access_required_p (Node_Id gnat_node, bool *sync) | |
4353 | { | |
4354 | atomic_acces_t type; | |
4355 | get_atomic_access (gnat_node, &type, sync); | |
4356 | return type == SIMPLE_ATOMIC; | |
4357 | } | |
4358 | ||
ddb5a105 EB |
4359 | /* Create a temporary variable with PREFIX and TYPE, and return it. */ |
4360 | ||
4361 | static tree | |
4362 | create_temporary (const char *prefix, tree type) | |
4363 | { | |
2056c5ed EB |
4364 | tree gnu_temp |
4365 | = create_var_decl (create_tmp_var_name (prefix), NULL_TREE, | |
4366 | type, NULL_TREE, | |
4367 | false, false, false, false, false, | |
4368 | true, false, NULL, Empty); | |
ddb5a105 EB |
4369 | return gnu_temp; |
4370 | } | |
35a382b8 EB |
4371 | |
4372 | /* Create a temporary variable with PREFIX and initialize it with GNU_INIT. | |
4373 | Put the initialization statement into GNU_INIT_STMT and annotate it with | |
4374 | the SLOC of GNAT_NODE. Return the temporary variable. */ | |
4375 | ||
4376 | static tree | |
4377 | create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt, | |
4378 | Node_Id gnat_node) | |
4379 | { | |
ddb5a105 | 4380 | tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init)); |
35a382b8 EB |
4381 | |
4382 | *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init); | |
4383 | set_expr_location_from_node (*gnu_init_stmt, gnat_node); | |
4384 | ||
4385 | return gnu_temp; | |
4386 | } | |
4387 | ||
b81e2d5e EB |
4388 | /* Return true if TYPE is an array of scalar type. */ |
4389 | ||
4390 | static bool | |
4391 | is_array_of_scalar_type (tree type) | |
4392 | { | |
4393 | if (TREE_CODE (type) != ARRAY_TYPE) | |
4394 | return false; | |
4395 | ||
4396 | type = TREE_TYPE (type); | |
4397 | ||
4398 | return !AGGREGATE_TYPE_P (type) && !POINTER_TYPE_P (type); | |
4399 | } | |
4400 | ||
4401 | /* Helper function for walk_tree, used by return_slot_opt_for_pure_call_p. */ | |
4402 | ||
4403 | static tree | |
4404 | find_decls_r (tree *tp, int *walk_subtrees, void *data) | |
4405 | { | |
4406 | bitmap decls = (bitmap) data; | |
4407 | ||
4408 | if (TYPE_P (*tp)) | |
4409 | *walk_subtrees = 0; | |
4410 | ||
4411 | else if (DECL_P (*tp)) | |
4412 | bitmap_set_bit (decls, DECL_UID (*tp)); | |
4413 | ||
4414 | return NULL_TREE; | |
4415 | } | |
4416 | ||
4417 | /* Return whether the assignment TARGET = CALL can be subject to the return | |
4418 | slot optimization, under the assumption that the called function be pure | |
4419 | in the Ada sense and return an array of scalar type. */ | |
4420 | ||
4421 | static bool | |
4422 | return_slot_opt_for_pure_call_p (tree target, tree call) | |
4423 | { | |
4424 | /* Check that the target is a DECL. */ | |
4425 | if (!DECL_P (target)) | |
4426 | return false; | |
4427 | ||
4428 | const bitmap decls = BITMAP_GGC_ALLOC (); | |
4429 | call_expr_arg_iterator iter; | |
4430 | tree arg; | |
4431 | ||
4432 | /* Check that all the arguments have either a scalar type (we assume that | |
4433 | this means by-copy passing mechanism) or array of scalar type. */ | |
4434 | FOR_EACH_CALL_EXPR_ARG (arg, iter, call) | |
4435 | { | |
4436 | tree arg_type = TREE_TYPE (arg); | |
4437 | if (TREE_CODE (arg_type) == REFERENCE_TYPE) | |
4438 | arg_type = TREE_TYPE (arg_type); | |
4439 | ||
4440 | if (is_array_of_scalar_type (arg_type)) | |
4441 | walk_tree_without_duplicates (&arg, find_decls_r, decls); | |
4442 | ||
4443 | else if (AGGREGATE_TYPE_P (arg_type) || POINTER_TYPE_P (arg_type)) | |
4444 | return false; | |
4445 | } | |
4446 | ||
4447 | /* Check that the target is not referenced by the non-scalar arguments. */ | |
4448 | return !bitmap_bit_p (decls, DECL_UID (target)); | |
4449 | } | |
4450 | ||
a42dd9fe EB |
4451 | /* Elaborate types referenced in the profile (FIRST_FORMAL, RESULT_TYPE). */ |
4452 | ||
4453 | static void | |
4454 | elaborate_profile (Entity_Id first_formal, Entity_Id result_type) | |
4455 | { | |
4456 | Entity_Id formal; | |
4457 | ||
4458 | for (formal = first_formal; | |
4459 | Present (formal); | |
4460 | formal = Next_Formal_With_Extras (formal)) | |
4461 | (void) gnat_to_gnu_type (Etype (formal)); | |
4462 | ||
4463 | if (Present (result_type) && Ekind (result_type) != E_Void) | |
4464 | (void) gnat_to_gnu_type (result_type); | |
4465 | } | |
4466 | ||
e08f1aad | 4467 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Function_Call |
a1ab4c31 AC |
4468 | or an N_Procedure_Call_Statement, to a GCC tree, which is returned. |
4469 | GNU_RESULT_TYPE_P is a pointer to where we should place the result type. | |
0b3467c4 | 4470 | If GNU_TARGET is non-null, this must be a function call on the RHS of a |
033ba5bf | 4471 | N_Assignment_Statement and the result is to be placed into that object. |
17a98a3d EB |
4472 | ATOMIC_ACCESS is the type of atomic access to be used for the assignment |
4473 | to GNU_TARGET. If, in addition, ATOMIC_SYNC is true, then the assignment | |
4474 | to GNU_TARGET requires atomic synchronization. */ | |
a1ab4c31 AC |
4475 | |
4476 | static tree | |
80096613 | 4477 | Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target, |
17a98a3d | 4478 | atomic_acces_t atomic_access, bool atomic_sync) |
a1ab4c31 | 4479 | { |
ddb5a105 EB |
4480 | const bool function_call = (Nkind (gnat_node) == N_Function_Call); |
4481 | const bool returning_value = (function_call && !gnu_target); | |
a1ab4c31 AC |
4482 | /* The GCC node corresponding to the GNAT subprogram name. This can either |
4483 | be a FUNCTION_DECL node if we are dealing with a standard subprogram call, | |
4484 | or an indirect reference expression (an INDIRECT_REF node) pointing to a | |
4485 | subprogram. */ | |
298339ef EB |
4486 | const Node_Id gnat_subprog = Name (gnat_node); |
4487 | tree gnu_subprog = gnat_to_gnu (gnat_subprog); | |
a1ab4c31 | 4488 | /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */ |
ced57283 | 4489 | tree gnu_subprog_type = TREE_TYPE (gnu_subprog); |
ddb5a105 | 4490 | /* The return type of the FUNCTION_TYPE. */ |
a42dd9fe | 4491 | tree gnu_result_type;; |
b9778c45 EB |
4492 | const bool frontend_builtin |
4493 | = (TREE_CODE (gnu_subprog) == FUNCTION_DECL | |
4494 | && DECL_BUILT_IN_CLASS (gnu_subprog) == BUILT_IN_FRONTEND); | |
4495 | auto_vec<tree, 16> gnu_actual_vec; | |
a1ab4c31 | 4496 | tree gnu_name_list = NULL_TREE; |
ddb5a105 | 4497 | tree gnu_stmt_list = NULL_TREE; |
a1ab4c31 | 4498 | tree gnu_after_list = NULL_TREE; |
ddb5a105 | 4499 | tree gnu_retval = NULL_TREE; |
35a382b8 | 4500 | tree gnu_call, gnu_result; |
f53aff92 EB |
4501 | bool went_into_elab_proc; |
4502 | bool pushed_binding_level; | |
c95f808d EB |
4503 | bool variadic; |
4504 | bool by_descriptor; | |
ddb5a105 | 4505 | Entity_Id gnat_formal; |
a42dd9fe | 4506 | Entity_Id gnat_result_type; |
ddb5a105 | 4507 | Node_Id gnat_actual; |
17a98a3d EB |
4508 | atomic_acces_t aa_type; |
4509 | bool aa_sync; | |
a1ab4c31 | 4510 | |
f15ad1e3 | 4511 | /* The only way we can make a call via an access type is if GNAT_NAME is an |
a1ab4c31 | 4512 | explicit dereference. In that case, get the list of formal args from the |
ced57283 | 4513 | type the access type is pointing to. Otherwise, get the formals from the |
a1ab4c31 | 4514 | entity being called. */ |
298339ef | 4515 | if (Nkind (gnat_subprog) == N_Explicit_Dereference) |
4c640e26 | 4516 | { |
c95f808d | 4517 | const Entity_Id gnat_prefix_type |
298339ef | 4518 | = Underlying_Type (Etype (Prefix (gnat_subprog))); |
c95f808d | 4519 | |
298339ef | 4520 | gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog)); |
a42dd9fe | 4521 | gnat_result_type = Etype (Etype (gnat_subprog)); |
c95f808d | 4522 | variadic = IN (Convention (gnat_prefix_type), Convention_C_Variadic); |
4c640e26 EB |
4523 | |
4524 | /* If the access type doesn't require foreign-compatible representation, | |
4525 | be prepared for descriptors. */ | |
c95f808d EB |
4526 | by_descriptor |
4527 | = targetm.calls.custom_function_descriptors > 0 | |
4528 | && Can_Use_Internal_Rep (gnat_prefix_type); | |
4c640e26 | 4529 | } |
f15ad1e3 | 4530 | |
298339ef | 4531 | else if (Nkind (gnat_subprog) == N_Attribute_Reference) |
c95f808d EB |
4532 | { |
4533 | /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */ | |
4534 | gnat_formal = Empty; | |
a42dd9fe | 4535 | gnat_result_type = Empty; |
c95f808d EB |
4536 | variadic = false; |
4537 | by_descriptor = false; | |
4538 | } | |
f15ad1e3 | 4539 | |
a1ab4c31 | 4540 | else |
c95f808d | 4541 | { |
298339ef | 4542 | gcc_checking_assert (Is_Entity_Name (gnat_subprog)); |
f15ad1e3 | 4543 | |
298339ef | 4544 | gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog)); |
a42dd9fe | 4545 | gnat_result_type = Etype (Entity_Id (gnat_subprog)); |
298339ef | 4546 | variadic = IN (Convention (Entity (gnat_subprog)), Convention_C_Variadic); |
c95f808d | 4547 | by_descriptor = false; |
f15ad1e3 EB |
4548 | |
4549 | /* If we are calling a stubbed function, then raise Program_Error, but | |
4550 | elaborate all our args first. */ | |
298339ef | 4551 | if (Convention (Entity (gnat_subprog)) == Convention_Stubbed) |
f15ad1e3 EB |
4552 | { |
4553 | tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called, | |
4554 | gnat_node, N_Raise_Program_Error); | |
4555 | ||
4556 | for (gnat_actual = First_Actual (gnat_node); | |
4557 | Present (gnat_actual); | |
4558 | gnat_actual = Next_Actual (gnat_actual)) | |
4559 | add_stmt (gnat_to_gnu (gnat_actual)); | |
4560 | ||
4561 | if (returning_value) | |
4562 | { | |
a42dd9fe | 4563 | gnu_result_type = TREE_TYPE (gnu_subprog_type); |
f15ad1e3 EB |
4564 | *gnu_result_type_p = gnu_result_type; |
4565 | return build1 (NULL_EXPR, gnu_result_type, call_expr); | |
4566 | } | |
4567 | ||
4568 | return call_expr; | |
4569 | } | |
4570 | } | |
4571 | ||
a42dd9fe EB |
4572 | /* We must elaborate the entire profile now because, if it references types |
4573 | that were initially incomplete,, their elaboration changes the contents | |
4574 | of GNU_SUBPROG_TYPE and, in particular, may change the result type. */ | |
4575 | elaborate_profile (gnat_formal, gnat_result_type); | |
4576 | ||
f15ad1e3 | 4577 | gcc_assert (FUNC_OR_METHOD_TYPE_P (gnu_subprog_type)); |
a42dd9fe | 4578 | gnu_result_type = TREE_TYPE (gnu_subprog_type); |
f15ad1e3 EB |
4579 | |
4580 | if (TREE_CODE (gnu_subprog) == FUNCTION_DECL) | |
4581 | { | |
4582 | /* For a call to a nested function, check the inlining status. */ | |
4583 | if (decl_function_context (gnu_subprog)) | |
4584 | check_inlining_for_nested_subprog (gnu_subprog); | |
4585 | ||
4586 | /* For a recursive call, avoid explosion due to recursive inlining. */ | |
4587 | if (gnu_subprog == current_function_decl) | |
4588 | DECL_DISREGARD_INLINE_LIMITS (gnu_subprog) = 0; | |
c95f808d | 4589 | } |
a1ab4c31 | 4590 | |
ddb5a105 EB |
4591 | /* The lifetime of the temporaries created for the call ends right after the |
4592 | return value is copied, so we can give them the scope of the elaboration | |
4593 | routine at top level. */ | |
35a382b8 | 4594 | if (!current_function_decl) |
0b3467c4 | 4595 | { |
2231f17f | 4596 | current_function_decl = get_elaboration_procedure (); |
0b3467c4 EB |
4597 | went_into_elab_proc = true; |
4598 | } | |
f53aff92 EB |
4599 | else |
4600 | went_into_elab_proc = false; | |
0b3467c4 | 4601 | |
4a582c9f EB |
4602 | /* First, create the temporary for the return value when: |
4603 | ||
4604 | 1. There is no target and the function has copy-in/copy-out parameters, | |
4605 | because we need to preserve the return value before copying back the | |
4606 | parameters. | |
4607 | ||
b81e2d5e EB |
4608 | 2. There is no target and the call is made for neither the declaration |
4609 | of an object (regular or renaming), nor a return statement, nor an | |
4610 | allocator, nor an aggregate, and the return type has variable size | |
4611 | because in this case the gimplifier cannot create the temporary, or | |
4612 | more generally is an aggregate type, because the gimplifier would | |
4613 | create the temporary in the outermost scope instead of locally here. | |
4614 | But there is an exception for an allocator of unconstrained record | |
4615 | type with default discriminant because we allocate the actual size | |
4616 | in this case, unlike in the other cases, so we need a temporary to | |
4617 | fetch the discriminant and we create it here. | |
4a582c9f EB |
4618 | |
4619 | 3. There is a target and it is a slice or an array with fixed size, | |
4620 | and the return type has variable size, because the gimplifier | |
4621 | doesn't handle these cases. | |
4622 | ||
02221bed EB |
4623 | 4. There is a target which is a bit-field and the function returns an |
4624 | unconstrained record type with default discriminant, because the | |
4625 | return may copy more data than the bit-field can contain. | |
4626 | ||
4627 | 5. There is no target and we have misaligned In Out or Out parameters | |
5d2a63dc EB |
4628 | passed by reference, because we need to preserve the return value |
4629 | before copying back the parameters. However, in this case, we'll | |
4630 | defer creating the temporary, see below. | |
4631 | ||
4a582c9f EB |
4632 | This must be done before we push a binding level around the call, since |
4633 | we will pop it before copying the return value. */ | |
ddb5a105 | 4634 | if (function_call |
4a582c9f EB |
4635 | && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type)) |
4636 | || (!gnu_target | |
4637 | && Nkind (Parent (gnat_node)) != N_Object_Declaration | |
fc7a823e | 4638 | && Nkind (Parent (gnat_node)) != N_Object_Renaming_Declaration |
5d2a63dc | 4639 | && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement |
1b6f8e97 EB |
4640 | && (!(Nkind (Parent (gnat_node)) == N_Qualified_Expression |
4641 | && Nkind (Parent (Parent (gnat_node))) == N_Allocator) | |
4642 | || type_is_padding_self_referential (gnu_result_type)) | |
b81e2d5e | 4643 | && Nkind (Parent (gnat_node)) != N_Aggregate |
5d2a63dc EB |
4644 | && AGGREGATE_TYPE_P (gnu_result_type) |
4645 | && !TYPE_IS_FAT_POINTER_P (gnu_result_type)) | |
4a582c9f EB |
4646 | || (gnu_target |
4647 | && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF | |
4648 | || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE | |
4649 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target))) | |
4650 | == INTEGER_CST)) | |
02221bed EB |
4651 | && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST) |
4652 | || (gnu_target | |
4653 | && TREE_CODE (gnu_target) == COMPONENT_REF | |
4654 | && DECL_BIT_FIELD (TREE_OPERAND (gnu_target, 1)) | |
1e66a917 EB |
4655 | && DECL_SIZE (TREE_OPERAND (gnu_target, 1)) |
4656 | != TYPE_SIZE (TREE_TYPE (gnu_target)) | |
02221bed | 4657 | && type_is_padding_self_referential (gnu_result_type)))) |
93e708f9 EB |
4658 | { |
4659 | gnu_retval = create_temporary ("R", gnu_result_type); | |
4660 | DECL_RETURN_VALUE_P (gnu_retval) = 1; | |
4661 | } | |
ddb5a105 | 4662 | |
5d2a63dc EB |
4663 | /* If we don't need a value or have already created it, push a binding level |
4664 | around the call. This will narrow the lifetime of the temporaries we may | |
4665 | need to make when translating the parameters as much as possible. */ | |
4666 | if (!returning_value || gnu_retval) | |
4667 | { | |
4668 | start_stmt_group (); | |
4669 | gnat_pushlevel (); | |
4670 | pushed_binding_level = true; | |
4671 | } | |
f53aff92 EB |
4672 | else |
4673 | pushed_binding_level = false; | |
5d2a63dc | 4674 | |
ced57283 EB |
4675 | /* Create the list of the actual parameters as GCC expects it, namely a |
4676 | chain of TREE_LIST nodes in which the TREE_VALUE field of each node | |
4677 | is an expression and the TREE_PURPOSE field is null. But skip Out | |
4678 | parameters not passed by reference and that need not be copied in. */ | |
a1ab4c31 AC |
4679 | for (gnat_actual = First_Actual (gnat_node); |
4680 | Present (gnat_actual); | |
4681 | gnat_formal = Next_Formal_With_Extras (gnat_formal), | |
4682 | gnat_actual = Next_Actual (gnat_actual)) | |
4683 | { | |
19f51f28 | 4684 | Entity_Id gnat_formal_type = Etype (gnat_formal); |
7cdb6871 | 4685 | tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type); |
ced57283 EB |
4686 | tree gnu_formal = present_gnu_tree (gnat_formal) |
4687 | ? get_gnu_tree (gnat_formal) : NULL_TREE; | |
6e03839f | 4688 | const bool in_param = (Ekind (gnat_formal) == E_In_Parameter); |
c946adde EB |
4689 | const bool is_true_formal_parm |
4690 | = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL; | |
033ba5bf | 4691 | const bool is_by_ref_formal_parm |
1eb58520 AC |
4692 | = is_true_formal_parm |
4693 | && (DECL_BY_REF_P (gnu_formal) | |
4694 | || DECL_BY_COMPONENT_PTR_P (gnu_formal)); | |
6e03839f | 4695 | /* In the In Out or Out case, we must suppress conversions that yield |
c34f3839 EB |
4696 | an lvalue but can nevertheless cause the creation of a temporary, |
4697 | because we need the real object in this case, either to pass its | |
4698 | address if it's passed by reference or as target of the back copy | |
ddb5a105 | 4699 | done after the call if it uses the copy-in/copy-out mechanism. |
c34f3839 | 4700 | We do it in the In case too, except for an unchecked conversion |
19f51f28 EB |
4701 | to an elementary type or a constrained composite type because it |
4702 | alone can cause the actual to be misaligned and the addressability | |
4703 | test is applied to the real object. */ | |
c946adde | 4704 | const bool suppress_type_conversion |
a1ab4c31 | 4705 | = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion |
6e03839f | 4706 | && (!in_param |
1ab1c4ee | 4707 | || !is_by_ref_formal_parm |
19f51f28 EB |
4708 | || (Is_Composite_Type (Underlying_Type (gnat_formal_type)) |
4709 | && !Is_Constrained (Underlying_Type (gnat_formal_type))))) | |
a1ab4c31 | 4710 | || (Nkind (gnat_actual) == N_Type_Conversion |
19f51f28 | 4711 | && Is_Composite_Type (Underlying_Type (gnat_formal_type)))); |
ced57283 EB |
4712 | Node_Id gnat_name = suppress_type_conversion |
4713 | ? Expression (gnat_actual) : gnat_actual; | |
a1ab4c31 | 4714 | tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type; |
a1ab4c31 AC |
4715 | |
4716 | /* If it's possible we may need to use this expression twice, make sure | |
ced57283 | 4717 | that any side-effects are handled via SAVE_EXPRs; likewise if we need |
7194767c | 4718 | to force side-effects before the call. */ |
6e03839f | 4719 | if (!in_param && !is_by_ref_formal_parm) |
fc7a823e EB |
4720 | { |
4721 | tree init = NULL_TREE; | |
4722 | gnu_name = gnat_stabilize_reference (gnu_name, true, &init); | |
4723 | if (init) | |
4724 | gnu_name | |
4725 | = build_compound_expr (TREE_TYPE (gnu_name), init, gnu_name); | |
4726 | } | |
a1ab4c31 | 4727 | |
6a6ac079 EB |
4728 | /* If we are passing a non-addressable parameter by reference, pass the |
4729 | address of a copy. In the In Out or Out case, set up to copy back | |
4730 | out after the call. */ | |
033ba5bf | 4731 | if (is_by_ref_formal_parm |
a1ab4c31 | 4732 | && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name))) |
6a6ac079 | 4733 | && !addressable_p (gnu_name, gnu_name_type)) |
a1ab4c31 | 4734 | { |
0b3467c4 EB |
4735 | tree gnu_orig = gnu_name, gnu_temp, gnu_stmt; |
4736 | ||
4737 | /* Do not issue warnings for CONSTRUCTORs since this is not a copy | |
4738 | but sort of an instantiation for them. */ | |
1edbeb15 | 4739 | if (TREE_CODE (remove_conversions (gnu_name, true)) == CONSTRUCTOR) |
0b3467c4 EB |
4740 | ; |
4741 | ||
1edbeb15 EB |
4742 | /* If the formal is passed by reference, a copy is not allowed. */ |
4743 | else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type) | |
4744 | || Is_Aliased (gnat_formal)) | |
0b3467c4 EB |
4745 | post_error ("misaligned actual cannot be passed by reference", |
4746 | gnat_actual); | |
4747 | ||
1edbeb15 EB |
4748 | /* If the mechanism was forced to by-ref, a copy is not allowed but |
4749 | we issue only a warning because this case is not strict Ada. */ | |
4750 | else if (DECL_FORCED_BY_REF_P (gnu_formal)) | |
4751 | post_error ("misaligned actual cannot be passed by reference??", | |
4752 | gnat_actual); | |
a1ab4c31 | 4753 | |
56fe7b05 EB |
4754 | /* If the actual type of the object is already the nominal type, |
4755 | we have nothing to do, except if the size is self-referential | |
4756 | in which case we'll remove the unpadding below. */ | |
4757 | if (TREE_TYPE (gnu_name) == gnu_name_type | |
4758 | && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type))) | |
4759 | ; | |
4760 | ||
0b3467c4 | 4761 | /* Otherwise remove the unpadding from all the objects. */ |
56fe7b05 | 4762 | else if (TREE_CODE (gnu_name) == COMPONENT_REF |
315cff15 EB |
4763 | && TYPE_IS_PADDING_P |
4764 | (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))) | |
0b3467c4 | 4765 | gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0); |
a1ab4c31 | 4766 | |
169afcb9 EB |
4767 | /* Otherwise convert to the nominal type of the object if needed. |
4768 | There are several cases in which we need to make the temporary | |
4769 | using this type instead of the actual type of the object when | |
4770 | they are distinct, because the expectations of the callee would | |
4771 | otherwise not be met: | |
a1ab4c31 | 4772 | - if it's a justified modular type, |
169afcb9 EB |
4773 | - if the actual type is a smaller form of it, |
4774 | - if it's a smaller form of the actual type. */ | |
4775 | else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE | |
4776 | && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type) | |
4777 | || smaller_form_type_p (TREE_TYPE (gnu_name), | |
4778 | gnu_name_type))) | |
4779 | || (INTEGRAL_TYPE_P (gnu_name_type) | |
4780 | && smaller_form_type_p (gnu_name_type, | |
4781 | TREE_TYPE (gnu_name)))) | |
a1ab4c31 AC |
4782 | gnu_name = convert (gnu_name_type, gnu_name); |
4783 | ||
ddb5a105 EB |
4784 | /* If this is an In Out or Out parameter and we're returning a value, |
4785 | we need to create a temporary for the return value because we must | |
4786 | preserve it before copying back at the very end. */ | |
4787 | if (!in_param && returning_value && !gnu_retval) | |
93e708f9 EB |
4788 | { |
4789 | gnu_retval = create_temporary ("R", gnu_result_type); | |
4790 | DECL_RETURN_VALUE_P (gnu_retval) = 1; | |
4791 | } | |
ddb5a105 | 4792 | |
5d2a63dc EB |
4793 | /* If we haven't pushed a binding level, push it now. This will |
4794 | narrow the lifetime of the temporary we are about to make as | |
4795 | much as possible. */ | |
4796 | if (!pushed_binding_level && (!returning_value || gnu_retval)) | |
35a382b8 EB |
4797 | { |
4798 | start_stmt_group (); | |
4799 | gnat_pushlevel (); | |
4800 | pushed_binding_level = true; | |
4801 | } | |
4802 | ||
ddb5a105 | 4803 | /* Create an explicit temporary holding the copy. */ |
8da3776f EB |
4804 | /* Do not initialize it for the _Init parameter of an initialization |
4805 | procedure since no data is meant to be passed in. */ | |
4806 | if (Ekind (gnat_formal) == E_Out_Parameter | |
298339ef EB |
4807 | && Is_Entity_Name (gnat_subprog) |
4808 | && Is_Init_Proc (Entity (gnat_subprog))) | |
8da3776f EB |
4809 | gnu_name = gnu_temp = create_temporary ("A", TREE_TYPE (gnu_name)); |
4810 | ||
4811 | /* Initialize it on the fly like for an implicit temporary in the | |
4812 | other cases, as we don't necessarily have a statement list. */ | |
4813 | else | |
4814 | { | |
4815 | gnu_temp = create_init_temporary ("A", gnu_name, &gnu_stmt, | |
4816 | gnat_actual); | |
4817 | gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt, | |
4818 | gnu_temp); | |
4819 | } | |
cb3d597d | 4820 | |
ced57283 | 4821 | /* Set up to move the copy back to the original if needed. */ |
35a382b8 | 4822 | if (!in_param) |
a1ab4c31 | 4823 | { |
bb29e9ff EB |
4824 | /* If the original is a COND_EXPR whose first arm isn't meant to |
4825 | be further used, just deal with the second arm. This is very | |
4826 | likely the conditional expression built for a check. */ | |
4827 | if (TREE_CODE (gnu_orig) == COND_EXPR | |
4828 | && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR | |
4829 | && integer_zerop | |
4830 | (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1))) | |
4831 | gnu_orig = TREE_OPERAND (gnu_orig, 2); | |
4832 | ||
6a6ac079 EB |
4833 | gnu_stmt |
4834 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp); | |
0b3467c4 | 4835 | set_expr_location_from_node (gnu_stmt, gnat_node); |
bb29e9ff | 4836 | |
0b3467c4 | 4837 | append_to_statement_list (gnu_stmt, &gnu_after_list); |
a1ab4c31 AC |
4838 | } |
4839 | } | |
4840 | ||
4841 | /* Start from the real object and build the actual. */ | |
8234d02a | 4842 | tree gnu_actual = gnu_name; |
a1ab4c31 | 4843 | |
f797c2b7 EB |
4844 | /* If atomic access is required for an In or In Out actual parameter, |
4845 | build the atomic load. */ | |
033ba5bf EB |
4846 | if (is_true_formal_parm |
4847 | && !is_by_ref_formal_parm | |
4848 | && Ekind (gnat_formal) != E_Out_Parameter | |
17a98a3d EB |
4849 | && simple_atomic_access_required_p (gnat_actual, &aa_sync)) |
4850 | gnu_actual = build_atomic_load (gnu_actual, aa_sync); | |
033ba5bf | 4851 | |
a1ab4c31 AC |
4852 | /* If this was a procedure call, we may not have removed any padding. |
4853 | So do it here for the part we will use as an input, if any. */ | |
4854 | if (Ekind (gnat_formal) != E_Out_Parameter | |
a1ab4c31 | 4855 | && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))) |
c34f3839 EB |
4856 | gnu_actual |
4857 | = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual); | |
4858 | ||
4859 | /* Put back the conversion we suppressed above in the computation of the | |
4860 | real object. And even if we didn't suppress any conversion there, we | |
4861 | may have suppressed a conversion to the Etype of the actual earlier, | |
8234d02a EB |
4862 | since the parent is a procedure call, so put it back here. Note that |
4863 | we might have a dummy type here if the actual is the dereference of a | |
fccc47dd EB |
4864 | pointer to it, but that's OK when the formal is passed by reference. |
4865 | We also do not put back a conversion between an actual and a formal | |
4866 | that are unconstrained array types to avoid creating local bounds. */ | |
7a0877c0 | 4867 | tree gnu_actual_type = get_unpadded_type (Etype (gnat_actual)); |
8234d02a EB |
4868 | if (TYPE_IS_DUMMY_P (gnu_actual_type)) |
4869 | gcc_assert (is_true_formal_parm && DECL_BY_REF_P (gnu_formal)); | |
4870 | else if (suppress_type_conversion | |
4871 | && Nkind (gnat_actual) == N_Unchecked_Type_Conversion) | |
4872 | gnu_actual = unchecked_convert (gnu_actual_type, gnu_actual, | |
4873 | No_Truncation (gnat_actual)); | |
fccc47dd EB |
4874 | else if ((TREE_CODE (TREE_TYPE (gnu_actual)) == UNCONSTRAINED_ARRAY_TYPE |
4875 | || (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE | |
4876 | && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual)))) | |
4877 | && TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) | |
4878 | ; | |
a1ab4c31 | 4879 | else |
8234d02a | 4880 | gnu_actual = convert (gnu_actual_type, gnu_actual); |
c34f3839 | 4881 | |
815b5368 | 4882 | gigi_checking_assert (!Do_Range_Check (gnat_actual)); |
a1ab4c31 | 4883 | |
96540b24 | 4884 | /* First see if the parameter is passed by reference. */ |
c946adde | 4885 | if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal)) |
a1ab4c31 | 4886 | { |
6e03839f | 4887 | if (!in_param) |
a1ab4c31 AC |
4888 | { |
4889 | /* In Out or Out parameters passed by reference don't use the | |
ddb5a105 | 4890 | copy-in/copy-out mechanism so the address of the real object |
a1ab4c31 AC |
4891 | must be passed to the function. */ |
4892 | gnu_actual = gnu_name; | |
4893 | ||
4894 | /* If we have a padded type, be sure we've removed padding. */ | |
0b3467c4 | 4895 | if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))) |
a1ab4c31 AC |
4896 | gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)), |
4897 | gnu_actual); | |
4898 | ||
4899 | /* If we have the constructed subtype of an aliased object | |
4900 | with an unconstrained nominal subtype, the type of the | |
4901 | actual includes the template, although it is formally | |
4902 | constrained. So we need to convert it back to the real | |
4903 | constructed subtype to retrieve the constrained part | |
4904 | and takes its address. */ | |
4905 | if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE | |
4906 | && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual)) | |
a1ab4c31 | 4907 | && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual)) |
43a4dd82 | 4908 | && Is_Array_Type (Underlying_Type (Etype (gnat_actual)))) |
6e03839f | 4909 | gnu_actual = convert (gnu_actual_type, gnu_actual); |
a1ab4c31 AC |
4910 | } |
4911 | ||
0b3467c4 EB |
4912 | /* There is no need to convert the actual to the formal's type before |
4913 | taking its address. The only exception is for unconstrained array | |
4914 | types because of the way we build fat pointers. */ | |
7bf9a5ac EB |
4915 | if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) |
4916 | { | |
6e03839f EB |
4917 | /* Put back the conversion we suppressed above for In Out or Out |
4918 | parameters, since it may set the bounds of the actual. */ | |
4919 | if (!in_param && suppress_type_conversion) | |
4920 | gnu_actual = convert (gnu_actual_type, gnu_actual); | |
7bf9a5ac EB |
4921 | gnu_actual = convert (gnu_formal_type, gnu_actual); |
4922 | } | |
0b3467c4 | 4923 | |
6e03839f EB |
4924 | /* Take the address of the object and convert to the proper pointer |
4925 | type. */ | |
7bf9a5ac | 4926 | gnu_formal_type = TREE_TYPE (gnu_formal); |
a1ab4c31 AC |
4927 | gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); |
4928 | } | |
96540b24 EB |
4929 | |
4930 | /* Then see if the parameter is an array passed to a foreign convention | |
4931 | subprogram. */ | |
c946adde | 4932 | else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal)) |
a1ab4c31 | 4933 | { |
bb24f343 | 4934 | gnu_actual = maybe_padded_object (gnu_actual); |
a1ab4c31 AC |
4935 | gnu_actual = maybe_unconstrained_array (gnu_actual); |
4936 | ||
a1ab4c31 AC |
4937 | /* Take the address of the object and convert to the proper pointer |
4938 | type. We'd like to actually compute the address of the beginning | |
4939 | of the array using an ADDR_EXPR of an ARRAY_REF, but there's a | |
4940 | possibility that the ARRAY_REF might return a constant and we'd be | |
4941 | getting the wrong address. Neither approach is exactly correct, | |
4942 | but this is the most likely to work in all cases. */ | |
6e03839f | 4943 | gnu_formal_type = TREE_TYPE (gnu_formal); |
0b3467c4 | 4944 | gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); |
a1ab4c31 | 4945 | } |
96540b24 | 4946 | |
c95f808d EB |
4947 | /* Then see if the parameter is passed by copy. */ |
4948 | else if (is_true_formal_parm) | |
a1ab4c31 | 4949 | { |
6e03839f | 4950 | if (!in_param) |
a1ab4c31 AC |
4951 | gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list); |
4952 | ||
0b3467c4 EB |
4953 | gnu_actual = convert (gnu_formal_type, gnu_actual); |
4954 | ||
b9778c45 EB |
4955 | /* If this is a front-end built-in function, there is no need to |
4956 | convert to the type used to pass the argument. */ | |
ad00a297 | 4957 | if (!frontend_builtin) |
a1ab4c31 AC |
4958 | gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual); |
4959 | } | |
4960 | ||
c95f808d EB |
4961 | /* Then see if this is an unnamed parameter in a variadic C function. */ |
4962 | else if (variadic) | |
4963 | { | |
4964 | /* This is based on the processing done in gnat_to_gnu_param, but | |
4965 | we expect the mechanism to be set in (almost) all cases. */ | |
4966 | const Mechanism_Type mech = Mechanism (gnat_formal); | |
4967 | ||
4968 | /* Strip off possible padding type. */ | |
4969 | if (TYPE_IS_PADDING_P (gnu_formal_type)) | |
4970 | gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type)); | |
4971 | ||
4972 | /* Arrays are passed as pointers to element type. First check for | |
4973 | unconstrained array and get the underlying array. */ | |
4974 | if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE) | |
4975 | gnu_formal_type | |
4976 | = TREE_TYPE | |
4977 | (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_formal_type)))); | |
4978 | ||
4979 | /* Arrays are passed as pointers to element type. */ | |
4980 | if (mech != By_Copy && TREE_CODE (gnu_formal_type) == ARRAY_TYPE) | |
4981 | { | |
bb24f343 | 4982 | gnu_actual = maybe_padded_object (gnu_actual); |
c95f808d EB |
4983 | gnu_actual = maybe_unconstrained_array (gnu_actual); |
4984 | ||
4985 | /* Strip off any multi-dimensional entries, then strip | |
4986 | off the last array to get the component type. */ | |
4987 | while (TREE_CODE (TREE_TYPE (gnu_formal_type)) == ARRAY_TYPE | |
4988 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_formal_type))) | |
4989 | gnu_formal_type = TREE_TYPE (gnu_formal_type); | |
4990 | ||
4991 | gnu_formal_type = TREE_TYPE (gnu_formal_type); | |
4992 | gnu_formal_type = build_pointer_type (gnu_formal_type); | |
4993 | gnu_actual | |
4994 | = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); | |
4995 | } | |
4996 | ||
4997 | /* Fat pointers are passed as thin pointers. */ | |
4998 | else if (TYPE_IS_FAT_POINTER_P (gnu_formal_type)) | |
4999 | gnu_formal_type | |
5000 | = make_type_from_size (gnu_formal_type, | |
5001 | size_int (POINTER_SIZE), 0); | |
5002 | ||
5003 | /* If we were requested or muss pass by reference, do so. | |
5004 | If we were requested to pass by copy, do so. | |
5005 | Otherwise, pass In Out or Out parameters or aggregates by | |
5006 | reference. */ | |
5007 | else if (mech == By_Reference | |
5008 | || must_pass_by_ref (gnu_formal_type) | |
5009 | || (mech != By_Copy | |
5010 | && (!in_param || AGGREGATE_TYPE_P (gnu_formal_type)))) | |
5011 | { | |
5012 | gnu_formal_type = build_reference_type (gnu_formal_type); | |
5013 | gnu_actual | |
5014 | = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual); | |
5015 | } | |
5016 | ||
5017 | /* Otherwise pass by copy after applying default C promotions. */ | |
5018 | else | |
5019 | { | |
5020 | if (INTEGRAL_TYPE_P (gnu_formal_type) | |
5021 | && TYPE_PRECISION (gnu_formal_type) | |
5022 | < TYPE_PRECISION (integer_type_node)) | |
5023 | gnu_formal_type = integer_type_node; | |
5024 | ||
5025 | else if (SCALAR_FLOAT_TYPE_P (gnu_formal_type) | |
5026 | && TYPE_PRECISION (gnu_formal_type) | |
5027 | < TYPE_PRECISION (double_type_node)) | |
5028 | gnu_formal_type = double_type_node; | |
5029 | } | |
5030 | ||
5031 | gnu_actual = convert (gnu_formal_type, gnu_actual); | |
5032 | } | |
5033 | ||
5034 | /* If we didn't create a PARM_DECL for the formal, this means that | |
5035 | it is an Out parameter not passed by reference and that need not | |
5036 | be copied in. In this case, the value of the actual need not be | |
5037 | read. However, we still need to make sure that its side-effects | |
5038 | are evaluated before the call, so we evaluate its address. */ | |
5039 | else | |
5040 | { | |
5041 | if (!in_param) | |
5042 | gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list); | |
5043 | ||
5044 | if (TREE_SIDE_EFFECTS (gnu_name)) | |
5045 | { | |
5046 | tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name); | |
5047 | append_to_statement_list (addr, &gnu_stmt_list); | |
5048 | } | |
5049 | ||
5050 | continue; | |
5051 | } | |
5052 | ||
b9778c45 EB |
5053 | gnu_actual_vec.safe_push (gnu_actual); |
5054 | } | |
5055 | ||
5056 | if (frontend_builtin) | |
5057 | { | |
5058 | tree pred_cst = build_int_cst (integer_type_node, PRED_BUILTIN_EXPECT); | |
5059 | enum internal_fn icode = IFN_BUILTIN_EXPECT; | |
5060 | ||
4d732405 | 5061 | switch (DECL_FE_FUNCTION_CODE (gnu_subprog)) |
b9778c45 EB |
5062 | { |
5063 | case BUILT_IN_EXPECT: | |
5064 | break; | |
5065 | case BUILT_IN_LIKELY: | |
5066 | gnu_actual_vec.safe_push (boolean_true_node); | |
5067 | break; | |
5068 | case BUILT_IN_UNLIKELY: | |
5069 | gnu_actual_vec.safe_push (boolean_false_node); | |
5070 | break; | |
5071 | default: | |
5072 | gcc_unreachable (); | |
5073 | } | |
5074 | ||
5075 | gnu_actual_vec.safe_push (pred_cst); | |
5076 | ||
5077 | gnu_call | |
5078 | = build_call_expr_internal_loc_array (UNKNOWN_LOCATION, | |
5079 | icode, | |
5080 | gnu_result_type, | |
5081 | gnu_actual_vec.length (), | |
5082 | gnu_actual_vec.begin ()); | |
5083 | } | |
5084 | else | |
5085 | { | |
5086 | gnu_call | |
5087 | = build_call_array_loc (UNKNOWN_LOCATION, | |
5088 | gnu_result_type, | |
5089 | build_unary_op (ADDR_EXPR, NULL_TREE, | |
5090 | gnu_subprog), | |
5091 | gnu_actual_vec.length (), | |
5092 | gnu_actual_vec.begin ()); | |
5093 | CALL_EXPR_BY_DESCRIPTOR (gnu_call) = by_descriptor; | |
a1ab4c31 AC |
5094 | } |
5095 | ||
ced57283 | 5096 | set_expr_location_from_node (gnu_call, gnat_node); |
a1ab4c31 | 5097 | |
ddb5a105 EB |
5098 | /* If we have created a temporary for the return value, initialize it. */ |
5099 | if (gnu_retval) | |
5100 | { | |
5101 | tree gnu_stmt | |
5102 | = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call); | |
5103 | set_expr_location_from_node (gnu_stmt, gnat_node); | |
5104 | append_to_statement_list (gnu_stmt, &gnu_stmt_list); | |
5105 | gnu_call = gnu_retval; | |
5106 | } | |
5107 | ||
35a382b8 EB |
5108 | /* If this is a subprogram with copy-in/copy-out parameters, we need to |
5109 | unpack the valued returned from the function into the In Out or Out | |
5110 | parameters. We deal with the function return (if this is an Ada | |
5111 | function) below. */ | |
d47d0a8d | 5112 | if (TYPE_CI_CO_LIST (gnu_subprog_type)) |
a1ab4c31 | 5113 | { |
0b3467c4 EB |
5114 | /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/ |
5115 | copy-out parameters. */ | |
a09d56d8 EB |
5116 | tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type); |
5117 | const int length = list_length (gnu_cico_list); | |
a1ab4c31 | 5118 | |
35a382b8 EB |
5119 | /* The call sequence must contain one and only one call, even though the |
5120 | function is pure. Save the result into a temporary if needed. */ | |
a1ab4c31 AC |
5121 | if (length > 1) |
5122 | { | |
ddb5a105 EB |
5123 | if (!gnu_retval) |
5124 | { | |
5125 | tree gnu_stmt; | |
ddb5a105 EB |
5126 | gnu_call |
5127 | = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node); | |
5128 | append_to_statement_list (gnu_stmt, &gnu_stmt_list); | |
5129 | } | |
0b3467c4 | 5130 | |
a1ab4c31 | 5131 | gnu_name_list = nreverse (gnu_name_list); |
a1ab4c31 AC |
5132 | } |
5133 | ||
35a382b8 EB |
5134 | /* The first entry is for the actual return value if this is a |
5135 | function, so skip it. */ | |
b83053bf | 5136 | if (function_call) |
35a382b8 EB |
5137 | gnu_cico_list = TREE_CHAIN (gnu_cico_list); |
5138 | ||
298339ef EB |
5139 | if (Nkind (gnat_subprog) == N_Explicit_Dereference) |
5140 | gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog)); | |
a1ab4c31 | 5141 | else |
298339ef | 5142 | gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog)); |
a1ab4c31 AC |
5143 | |
5144 | for (gnat_actual = First_Actual (gnat_node); | |
5145 | Present (gnat_actual); | |
5146 | gnat_formal = Next_Formal_With_Extras (gnat_formal), | |
5147 | gnat_actual = Next_Actual (gnat_actual)) | |
35a382b8 | 5148 | /* If we are dealing with a copy-in/copy-out parameter, we must |
a1ab4c31 AC |
5149 | retrieve its value from the record returned in the call. */ |
5150 | if (!(present_gnu_tree (gnat_formal) | |
5151 | && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL | |
5152 | && (DECL_BY_REF_P (get_gnu_tree (gnat_formal)) | |
1eb58520 | 5153 | || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal)))) |
a1ab4c31 AC |
5154 | && Ekind (gnat_formal) != E_In_Parameter) |
5155 | { | |
6e03839f | 5156 | /* Get the value to assign to this In Out or Out parameter. It is |
a1ab4c31 AC |
5157 | either the result of the function if there is only a single such |
5158 | parameter or the appropriate field from the record returned. */ | |
5159 | tree gnu_result | |
ced57283 EB |
5160 | = length == 1 |
5161 | ? gnu_call | |
64235766 EB |
5162 | : build_component_ref (gnu_call, TREE_PURPOSE (gnu_cico_list), |
5163 | false); | |
a1ab4c31 AC |
5164 | |
5165 | /* If the actual is a conversion, get the inner expression, which | |
5166 | will be the real destination, and convert the result to the | |
5167 | type of the actual parameter. */ | |
5168 | tree gnu_actual | |
5169 | = maybe_unconstrained_array (TREE_VALUE (gnu_name_list)); | |
5170 | ||
ad00a297 EB |
5171 | /* If the result is padded, remove the padding. */ |
5172 | gnu_result = maybe_padded_object (gnu_result); | |
a1ab4c31 AC |
5173 | |
5174 | /* If the actual is a type conversion, the real target object is | |
5175 | denoted by the inner Expression and we need to convert the | |
5176 | result to the associated type. | |
5177 | We also need to convert our gnu assignment target to this type | |
5178 | if the corresponding GNU_NAME was constructed from the GNAT | |
5179 | conversion node and not from the inner Expression. */ | |
5180 | if (Nkind (gnat_actual) == N_Type_Conversion) | |
5181 | { | |
815b5368 EB |
5182 | const Node_Id gnat_expr = Expression (gnat_actual); |
5183 | ||
5184 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); | |
5185 | ||
a1ab4c31 | 5186 | gnu_result |
815b5368 EB |
5187 | = convert_with_check (Etype (gnat_expr), gnu_result, |
5188 | Do_Overflow_Check (gnat_actual), | |
5189 | Float_Truncate (gnat_actual), | |
5190 | gnat_actual); | |
a1ab4c31 AC |
5191 | |
5192 | if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))) | |
5193 | gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual); | |
5194 | } | |
5195 | ||
5196 | /* Unchecked conversions as actuals for Out parameters are not | |
5197 | allowed in user code because they are not variables, but do | |
5198 | occur in front-end expansions. The associated GNU_NAME is | |
5199 | always obtained from the inner expression in such cases. */ | |
5200 | else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion) | |
5201 | gnu_result = unchecked_convert (TREE_TYPE (gnu_actual), | |
5202 | gnu_result, | |
5203 | No_Truncation (gnat_actual)); | |
5204 | else | |
5205 | { | |
815b5368 | 5206 | gigi_checking_assert (!Do_Range_Check (gnat_actual)); |
a1ab4c31 AC |
5207 | |
5208 | if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual))) | |
5209 | && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result))))) | |
5210 | gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result); | |
5211 | } | |
5212 | ||
17a98a3d EB |
5213 | get_atomic_access (gnat_actual, &aa_type, &aa_sync); |
5214 | ||
f797c2b7 EB |
5215 | /* If an outer atomic access is required for an actual parameter, |
5216 | build the load-modify-store sequence. */ | |
17a98a3d | 5217 | if (aa_type == OUTER_ATOMIC) |
f797c2b7 EB |
5218 | gnu_result |
5219 | = build_load_modify_store (gnu_actual, gnu_result, gnat_node); | |
5220 | ||
17a98a3d | 5221 | /* Or else, if a simple atomic access is required, build the atomic |
f797c2b7 | 5222 | store. */ |
17a98a3d EB |
5223 | else if (aa_type == SIMPLE_ATOMIC) |
5224 | gnu_result | |
5225 | = build_atomic_store (gnu_actual, gnu_result, aa_sync); | |
f797c2b7 EB |
5226 | |
5227 | /* Otherwise build a regular assignment. */ | |
033ba5bf EB |
5228 | else |
5229 | gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE, | |
5230 | gnu_actual, gnu_result); | |
f797c2b7 EB |
5231 | |
5232 | if (EXPR_P (gnu_result)) | |
5233 | set_expr_location_from_node (gnu_result, gnat_node); | |
ddb5a105 | 5234 | append_to_statement_list (gnu_result, &gnu_stmt_list); |
a09d56d8 | 5235 | gnu_cico_list = TREE_CHAIN (gnu_cico_list); |
a1ab4c31 AC |
5236 | gnu_name_list = TREE_CHAIN (gnu_name_list); |
5237 | } | |
ced57283 | 5238 | } |
35a382b8 EB |
5239 | |
5240 | /* If this is a function call, the result is the call expression unless a | |
5241 | target is specified, in which case we copy the result into the target | |
5242 | and return the assignment statement. */ | |
ddb5a105 | 5243 | if (function_call) |
35a382b8 | 5244 | { |
35a382b8 EB |
5245 | /* If this is a function with copy-in/copy-out parameters, extract the |
5246 | return value from it and update the return type. */ | |
5247 | if (TYPE_CI_CO_LIST (gnu_subprog_type)) | |
5248 | { | |
b83053bf | 5249 | tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type); |
64235766 EB |
5250 | gnu_call |
5251 | = build_component_ref (gnu_call, TREE_PURPOSE (gnu_elmt), false); | |
35a382b8 EB |
5252 | gnu_result_type = TREE_TYPE (gnu_call); |
5253 | } | |
5254 | ||
89e037d0 EB |
5255 | /* If the function returns by direct reference, we have to dereference |
5256 | the pointer. */ | |
5257 | if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)) | |
35a382b8 EB |
5258 | gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call); |
5259 | ||
5260 | if (gnu_target) | |
5261 | { | |
5262 | Node_Id gnat_parent = Parent (gnat_node); | |
5263 | enum tree_code op_code; | |
5264 | ||
815b5368 | 5265 | gigi_checking_assert (!Do_Range_Check (gnat_node)); |
35a382b8 | 5266 | |
ddb5a105 EB |
5267 | /* ??? If the return type has variable size, then force the return |
5268 | slot optimization as we would not be able to create a temporary. | |
ddb5a105 | 5269 | That's what has been done historically. */ |
fc7a823e | 5270 | if (return_type_with_variable_size_p (gnu_result_type)) |
35a382b8 | 5271 | op_code = INIT_EXPR; |
b81e2d5e EB |
5272 | |
5273 | /* If this is a call to a pure function returning an array of scalar | |
5274 | type, try to apply the return slot optimization. */ | |
5275 | else if ((TYPE_READONLY (gnu_subprog_type) | |
5276 | || TYPE_RESTRICT (gnu_subprog_type)) | |
5277 | && is_array_of_scalar_type (gnu_result_type) | |
5278 | && TYPE_MODE (gnu_result_type) == BLKmode | |
5279 | && aggregate_value_p (gnu_result_type, gnu_subprog_type) | |
5280 | && return_slot_opt_for_pure_call_p (gnu_target, gnu_call)) | |
5281 | op_code = INIT_EXPR; | |
5282 | ||
9aa9ba95 EB |
5283 | /* If this is the initialization of a return object in a function |
5284 | returning by invisible reference, we can always use the return | |
5285 | slot optimization. */ | |
5286 | else if (TREE_CODE (gnu_target) == INDIRECT_REF | |
5287 | && TREE_CODE (TREE_OPERAND (gnu_target, 0)) == RESULT_DECL | |
5288 | && current_function_decl | |
5289 | && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl))) | |
5290 | op_code = INIT_EXPR; | |
5291 | ||
35a382b8 EB |
5292 | else |
5293 | op_code = MODIFY_EXPR; | |
5294 | ||
f797c2b7 | 5295 | /* Use the required method to move the result to the target. */ |
17a98a3d | 5296 | if (atomic_access == OUTER_ATOMIC) |
f797c2b7 EB |
5297 | gnu_call |
5298 | = build_load_modify_store (gnu_target, gnu_call, gnat_node); | |
17a98a3d | 5299 | else if (atomic_access == SIMPLE_ATOMIC) |
f797c2b7 | 5300 | gnu_call = build_atomic_store (gnu_target, gnu_call, atomic_sync); |
033ba5bf EB |
5301 | else |
5302 | gnu_call | |
5303 | = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call); | |
f797c2b7 EB |
5304 | |
5305 | if (EXPR_P (gnu_call)) | |
5306 | set_expr_location_from_node (gnu_call, gnat_parent); | |
ddb5a105 | 5307 | append_to_statement_list (gnu_call, &gnu_stmt_list); |
35a382b8 EB |
5308 | } |
5309 | else | |
5310 | *gnu_result_type_p = get_unpadded_type (Etype (gnat_node)); | |
5311 | } | |
5312 | ||
5313 | /* Otherwise, if this is a procedure call statement without copy-in/copy-out | |
5314 | parameters, the result is just the call statement. */ | |
5315 | else if (!TYPE_CI_CO_LIST (gnu_subprog_type)) | |
ddb5a105 EB |
5316 | append_to_statement_list (gnu_call, &gnu_stmt_list); |
5317 | ||
5318 | /* Finally, add the copy back statements, if any. */ | |
5319 | append_to_statement_list (gnu_after_list, &gnu_stmt_list); | |
a1ab4c31 | 5320 | |
35a382b8 EB |
5321 | if (went_into_elab_proc) |
5322 | current_function_decl = NULL_TREE; | |
ced57283 | 5323 | |
ddb5a105 EB |
5324 | /* If we have pushed a binding level, pop it and finish up the enclosing |
5325 | statement group. */ | |
35a382b8 EB |
5326 | if (pushed_binding_level) |
5327 | { | |
ddb5a105 | 5328 | add_stmt (gnu_stmt_list); |
35a382b8 EB |
5329 | gnat_poplevel (); |
5330 | gnu_result = end_stmt_group (); | |
5331 | } | |
ddb5a105 EB |
5332 | |
5333 | /* Otherwise, retrieve the statement list, if any. */ | |
5334 | else if (gnu_stmt_list) | |
5335 | gnu_result = gnu_stmt_list; | |
5336 | ||
5337 | /* Otherwise, just return the call expression. */ | |
35a382b8 EB |
5338 | else |
5339 | return gnu_call; | |
5340 | ||
71196d4e EB |
5341 | /* If we nevertheless need a value, make a COMPOUND_EXPR to return it. |
5342 | But first simplify if we have only one statement in the list. */ | |
35a382b8 | 5343 | if (returning_value) |
71196d4e EB |
5344 | { |
5345 | tree first = expr_first (gnu_result), last = expr_last (gnu_result); | |
5346 | if (first == last) | |
5347 | gnu_result = first; | |
5348 | gnu_result | |
5349 | = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call); | |
5350 | } | |
35a382b8 EB |
5351 | |
5352 | return gnu_result; | |
a1ab4c31 | 5353 | } |
ce2d0ce2 | 5354 | |
e08f1aad | 5355 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an |
a1ab4c31 AC |
5356 | N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */ |
5357 | ||
5358 | static tree | |
5359 | Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node) | |
5360 | { | |
a1ab4c31 | 5361 | /* If just annotating, ignore all EH and cleanups. */ |
6d16658d | 5362 | const bool eh |
ce19ac12 | 5363 | = !type_annotate_only && Present (Exception_Handlers (gnat_node)); |
736e16ef | 5364 | const bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node)); |
a1ab4c31 | 5365 | tree gnu_result; |
a1ab4c31 AC |
5366 | Node_Id gnat_temp; |
5367 | ||
6d16658d BD |
5368 | /* The exception handling mechanism can handle both ZCX and SJLJ schemes, and |
5369 | is exposed through the TRY_CATCH_EXPR construct that we build manually. | |
a1ab4c31 AC |
5370 | |
5371 | ??? The region level calls down there have been specifically put in place | |
5372 | for a ZCX context and currently the order in which things are emitted | |
736e16ef | 5373 | (region/handlers) is different from the SJLJ case. Instead of putting |
a1ab4c31 AC |
5374 | other calls with different conditions at other places for the SJLJ case, |
5375 | it seems cleaner to reorder things for the SJLJ case and generalize the | |
6d16658d | 5376 | condition to make it not ZCX specific. */ |
a1ab4c31 | 5377 | |
6d16658d | 5378 | /* First build the tree for the statements inside the sequence. */ |
a1ab4c31 AC |
5379 | start_stmt_group (); |
5380 | ||
6d16658d BD |
5381 | for (gnat_temp = First (Statements (gnat_node)); |
5382 | Present (gnat_temp); | |
5383 | gnat_temp = Next (gnat_temp)) | |
a1ab4c31 | 5384 | add_stmt (gnat_to_gnu (gnat_temp)); |
736e16ef | 5385 | |
6d16658d | 5386 | gnu_result = end_stmt_group (); |
a1ab4c31 | 5387 | |
6d16658d BD |
5388 | /* Then process the exception handlers, if any. */ |
5389 | if (eh) | |
a1ab4c31 AC |
5390 | { |
5391 | tree gnu_handlers; | |
678400b3 | 5392 | location_t locus; |
a1ab4c31 | 5393 | |
6d16658d | 5394 | /* First make a group containing the handlers. */ |
a1ab4c31 AC |
5395 | start_stmt_group (); |
5396 | for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node)); | |
5397 | Present (gnat_temp); | |
5398 | gnat_temp = Next_Non_Pragma (gnat_temp)) | |
5399 | add_stmt (gnat_to_gnu (gnat_temp)); | |
5400 | gnu_handlers = end_stmt_group (); | |
5401 | ||
6d16658d BD |
5402 | /* Now make the TRY_CATCH_EXPR for the group. */ |
5403 | gnu_result | |
5404 | = build2 (TRY_CATCH_EXPR, void_type_node, gnu_result, gnu_handlers); | |
5405 | ||
362db0b2 | 5406 | /* Set a location. We need to find a unique location for the dispatching |
678400b3 AC |
5407 | code, otherwise we can get coverage or debugging issues. Try with |
5408 | the location of the end label. */ | |
5409 | if (Present (End_Label (gnat_node)) | |
5410 | && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus)) | |
5411 | SET_EXPR_LOCATION (gnu_result, locus); | |
5412 | else | |
362db0b2 TQ |
5413 | /* Clear column information so that the exception handler of an |
5414 | implicit transient block does not incorrectly inherit the slocs | |
5415 | of a decision, which would otherwise confuse control flow based | |
5416 | coverage analysis tools. */ | |
ba464315 | 5417 | set_expr_location_from_node (gnu_result, gnat_node, true); |
a1ab4c31 | 5418 | } |
a1ab4c31 | 5419 | |
6d16658d BD |
5420 | /* Process the At_End_Proc, if any. */ |
5421 | if (at_end) | |
a1ab4c31 | 5422 | { |
6d16658d | 5423 | start_stmt_group (); |
a1ab4c31 | 5424 | add_stmt (gnu_result); |
6d16658d | 5425 | At_End_Proc_to_gnu (gnat_node); |
a1ab4c31 AC |
5426 | gnu_result = end_stmt_group (); |
5427 | } | |
5428 | ||
5429 | return gnu_result; | |
5430 | } | |
ce2d0ce2 | 5431 | |
4ec7c4ec EB |
5432 | /* Return true if no statement in GNAT_LIST can alter the control flow. */ |
5433 | ||
5434 | static bool | |
5435 | stmt_list_cannot_alter_control_flow_p (List_Id gnat_list) | |
5436 | { | |
5437 | if (No (gnat_list)) | |
5438 | return true; | |
5439 | ||
5440 | /* This is very conservative, we reject everything except for simple | |
5441 | assignments between identifiers or literals. */ | |
5442 | for (Node_Id gnat_node = First (gnat_list); | |
5443 | Present (gnat_node); | |
5444 | gnat_node = Next (gnat_node)) | |
5445 | { | |
5446 | if (Nkind (gnat_node) != N_Assignment_Statement) | |
5447 | return false; | |
5448 | ||
5449 | if (Nkind (Name (gnat_node)) != N_Identifier) | |
5450 | return false; | |
5451 | ||
5452 | Node_Kind nkind = Nkind (Expression (gnat_node)); | |
5453 | if (nkind != N_Identifier | |
5454 | && nkind != N_Integer_Literal | |
5455 | && nkind != N_Real_Literal) | |
5456 | return false; | |
5457 | } | |
5458 | ||
5459 | return true; | |
5460 | } | |
5461 | ||
e08f1aad EB |
5462 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Exception_Handler, |
5463 | to a GCC tree, which is returned. */ | |
a1ab4c31 AC |
5464 | |
5465 | static tree | |
e08f1aad | 5466 | Exception_Handler_to_gnu (Node_Id gnat_node) |
a1ab4c31 AC |
5467 | { |
5468 | tree gnu_etypes_list = NULL_TREE; | |
a1ab4c31 AC |
5469 | |
5470 | /* We build a TREE_LIST of nodes representing what exception types this | |
5471 | handler can catch, with special cases for others and all others cases. | |
5472 | ||
5473 | Each exception type is actually identified by a pointer to the exception | |
1a710808 | 5474 | id, or to a dummy object for "others" and "all others". */ |
4ec7c4ec EB |
5475 | for (Node_Id gnat_temp = First (Exception_Choices (gnat_node)); |
5476 | gnat_temp; | |
5477 | gnat_temp = Next (gnat_temp)) | |
a1ab4c31 | 5478 | { |
fd6e497e EB |
5479 | tree gnu_expr, gnu_etype; |
5480 | ||
a1ab4c31 AC |
5481 | if (Nkind (gnat_temp) == N_Others_Choice) |
5482 | { | |
fd6e497e EB |
5483 | gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl; |
5484 | gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); | |
a1ab4c31 AC |
5485 | } |
5486 | else if (Nkind (gnat_temp) == N_Identifier | |
5487 | || Nkind (gnat_temp) == N_Expanded_Name) | |
5488 | { | |
5489 | Entity_Id gnat_ex_id = Entity (gnat_temp); | |
5490 | ||
fd6e497e | 5491 | /* Exception may be a renaming. Recover original exception which is |
a1ab4c31 AC |
5492 | the one elaborated and registered. */ |
5493 | if (Present (Renamed_Object (gnat_ex_id))) | |
5494 | gnat_ex_id = Renamed_Object (gnat_ex_id); | |
5495 | ||
afc737f0 | 5496 | gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false); |
a1ab4c31 | 5497 | gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); |
a1ab4c31 AC |
5498 | } |
5499 | else | |
5500 | gcc_unreachable (); | |
5501 | ||
5502 | /* The GCC interface expects NULL to be passed for catch all handlers, so | |
5503 | it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype | |
5504 | is integer_zero_node. It would not work, however, because GCC's | |
5505 | notion of "catch all" is stronger than our notion of "others". Until | |
5506 | we correctly use the cleanup interface as well, doing that would | |
5507 | prevent the "all others" handlers from being seen, because nothing | |
5508 | can be caught beyond a catch all from GCC's point of view. */ | |
5509 | gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list); | |
5510 | } | |
5511 | ||
5512 | start_stmt_group (); | |
a1ab4c31 | 5513 | |
5d733372 AO |
5514 | /* Expand a call to the begin_handler hook at the beginning of the |
5515 | handler, and arrange for a call to the end_handler hook to occur | |
5516 | on every possible exit path. GDB sets a breakpoint in the | |
5517 | begin_handler for catchpoints. | |
a1ab4c31 | 5518 | |
5d733372 AO |
5519 | A v1 begin handler saves the cleanup from the exception object, |
5520 | and marks the exception as in use, so that it will not be | |
5521 | released by other handlers. A v1 end handler restores the | |
5522 | cleanup and releases the exception object, unless it is still | |
5523 | claimed, or the exception is being propagated (reraised). | |
a1ab4c31 | 5524 | |
1d65f45c | 5525 | __builtin_eh_pointer references the exception occurrence being |
5d733372 AO |
5526 | handled or propagated. Within the handler region, it is the |
5527 | former, but within the else branch of the EH_ELSE_EXPR, i.e. the | |
5528 | exceptional cleanup path, it is the latter, so we must save the | |
5529 | occurrence being handled early on, so that, should an exception | |
5530 | be (re)raised, we can release the current exception, or figure | |
5531 | out we're not to release it because we're propagating a reraise | |
5532 | thereof. | |
5533 | ||
5534 | We use local variables to retrieve the incoming value at handler | |
5535 | entry time (EXPTR), the saved cleanup (EXCLN) and the token | |
5536 | (EXVTK), and reuse them to feed the end_handler hook's argument | |
5537 | at exit. */ | |
5538 | ||
5539 | /* CODE: void *EXPTR = __builtin_eh_pointer (0); */ | |
4ec7c4ec | 5540 | tree gnu_current_exc_ptr |
e79983f4 | 5541 | = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER), |
1d65f45c | 5542 | 1, integer_zero_node); |
5d733372 | 5543 | tree exc_ptr |
c1a569ef EB |
5544 | = create_var_decl (get_identifier ("EXPTR"), NULL_TREE, |
5545 | ptr_type_node, gnu_current_exc_ptr, | |
5d733372 | 5546 | true, false, false, false, false, true, true, |
c1a569ef | 5547 | NULL, gnat_node); |
a1ab4c31 | 5548 | |
5d733372 AO |
5549 | tree prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr; |
5550 | gnu_incoming_exc_ptr = exc_ptr; | |
5551 | ||
5552 | /* begin_handler_decl must not throw, so we can use it as an | |
5553 | initializer for a variable used in cleanups. | |
5554 | ||
5555 | CODE: void *EXCLN = __gnat_begin_handler_v1 (EXPTR); */ | |
5556 | tree exc_cleanup | |
5557 | = create_var_decl (get_identifier ("EXCLN"), NULL_TREE, | |
5558 | ptr_type_node, | |
5559 | build_call_n_expr (begin_handler_decl, 1, | |
5560 | exc_ptr), | |
5561 | true, false, false, false, false, | |
5562 | true, true, NULL, gnat_node); | |
811189d9 AC |
5563 | |
5564 | /* Declare and initialize the choice parameter, if present. */ | |
5565 | if (Present (Choice_Parameter (gnat_node))) | |
5566 | { | |
fd6e497e | 5567 | tree gnu_param |
afc737f0 | 5568 | = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, true); |
811189d9 | 5569 | |
5d733372 | 5570 | /* CODE: __gnat_set_exception_parameter (&choice_param, EXPTR); */ |
811189d9 AC |
5571 | add_stmt (build_call_n_expr |
5572 | (set_exception_parameter_decl, 2, | |
5573 | build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param), | |
5574 | gnu_incoming_exc_ptr)); | |
5575 | } | |
5576 | ||
5d733372 | 5577 | /* CODE: <handler proper> */ |
4ec7c4ec EB |
5578 | add_stmt_list (Statements (gnat_node)); |
5579 | ||
5d733372 AO |
5580 | tree call = build_call_n_expr (end_handler_decl, 3, |
5581 | exc_ptr, | |
5582 | exc_cleanup, | |
5583 | null_pointer_node); | |
5584 | /* If the handler can only end by falling off the end, don't bother | |
5585 | with cleanups. */ | |
4ec7c4ec | 5586 | if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node))) |
5d733372 AO |
5587 | /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, NULL); */ |
5588 | add_stmt_with_node (call, gnat_node); | |
5589 | /* Otherwise, all of the above is after | |
5590 | CODE: try { | |
5591 | ||
5592 | The call above will appear after | |
5593 | CODE: } finally { | |
5594 | ||
5595 | And the code below will appear after | |
5596 | CODE: } else { | |
5597 | ||
5598 | The else block to a finally block is taken instead of the finally | |
5599 | block when an exception propagates out of the try block. */ | |
4ec7c4ec | 5600 | else |
5d733372 AO |
5601 | { |
5602 | start_stmt_group (); | |
6d16658d | 5603 | |
5d733372 AO |
5604 | /* CODE: void *EXPRP = __builtin_eh_handler (0); */ |
5605 | tree prop_ptr | |
5606 | = create_var_decl (get_identifier ("EXPRP"), NULL_TREE, | |
5607 | ptr_type_node, | |
5608 | build_call_expr (builtin_decl_explicit | |
5609 | (BUILT_IN_EH_POINTER), | |
5610 | 1, integer_zero_node), | |
5611 | true, false, false, false, false, | |
5612 | true, true, NULL, gnat_node); | |
5613 | ||
5614 | /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, EXPRP); */ | |
5615 | tree ecall = build_call_n_expr (end_handler_decl, 3, | |
5616 | exc_ptr, | |
5617 | exc_cleanup, | |
5618 | prop_ptr); | |
5619 | ||
5620 | add_stmt_with_node (ecall, gnat_node); | |
5621 | ||
5622 | /* CODE: } */ | |
5d733372 AO |
5623 | tree eblk = end_stmt_group (); |
5624 | tree ehls = build2 (EH_ELSE_EXPR, void_type_node, call, eblk); | |
5625 | add_cleanup (ehls, gnat_node); | |
5626 | } | |
4ec7c4ec | 5627 | |
624e1688 AC |
5628 | gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr; |
5629 | ||
fd6e497e EB |
5630 | return |
5631 | build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ()); | |
a1ab4c31 | 5632 | } |
ce2d0ce2 | 5633 | |
e08f1aad | 5634 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Compilation_Unit. */ |
a1ab4c31 AC |
5635 | |
5636 | static void | |
5637 | Compilation_Unit_to_gnu (Node_Id gnat_node) | |
5638 | { | |
58c8f770 EB |
5639 | const Node_Id gnat_unit = Unit (gnat_node); |
5640 | const bool body_p = (Nkind (gnat_unit) == N_Package_Body | |
5641 | || Nkind (gnat_unit) == N_Subprogram_Body); | |
5642 | const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit); | |
384e3fb1 | 5643 | Entity_Id gnat_entity; |
ec4a0d83 | 5644 | Node_Id gnat_pragma, gnat_iter; |
ff9baa5f PMR |
5645 | /* Make the decl for the elaboration procedure. Emit debug info for it, so |
5646 | that users can break into their elaboration code in debuggers. Kludge: | |
19d9ce46 OH |
5647 | don't consider it as a definition so that we have a line map for its |
5648 | body, but no subprogram description in debug info. In addition, don't | |
5649 | qualify it as artificial, even though it is not a user subprogram per se, | |
5650 | in particular for specs. Unlike, say, clones created internally by the | |
5651 | compiler, this subprogram materializes specific user code and flagging it | |
5652 | artificial would take elab code away from gcov's analysis. */ | |
a1ab4c31 AC |
5653 | tree gnu_elab_proc_decl |
5654 | = create_subprog_decl | |
58c8f770 | 5655 | (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"), |
2056c5ed | 5656 | NULL_TREE, void_ftype, NULL_TREE, |
19d9ce46 | 5657 | is_default, true, false, false, true, false, NULL, gnat_unit); |
a1ab4c31 AC |
5658 | struct elab_info *info; |
5659 | ||
9771b263 | 5660 | vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl); |
a1ab4c31 | 5661 | DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1; |
58c8f770 EB |
5662 | |
5663 | /* Initialize the information structure for the function. */ | |
a1ab4c31 | 5664 | allocate_struct_function (gnu_elab_proc_decl, false); |
a1ab4c31 | 5665 | set_cfun (NULL); |
58c8f770 EB |
5666 | |
5667 | current_function_decl = NULL_TREE; | |
5668 | ||
a09d56d8 EB |
5669 | start_stmt_group (); |
5670 | gnat_pushlevel (); | |
a1ab4c31 | 5671 | |
1e17ef87 | 5672 | /* For a body, first process the spec if there is one. */ |
6ddf9843 EB |
5673 | if (Nkind (gnat_unit) == N_Package_Body |
5674 | || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node))) | |
5675 | add_stmt (gnat_to_gnu (Library_Unit (gnat_node))); | |
a1ab4c31 | 5676 | |
5daed84a EB |
5677 | if (type_annotate_only && gnat_node == Cunit (Main_Unit)) |
5678 | { | |
5679 | elaborate_all_entities (gnat_node); | |
5680 | ||
6ddf9843 EB |
5681 | if (Nkind (gnat_unit) == N_Subprogram_Declaration |
5682 | || Nkind (gnat_unit) == N_Generic_Package_Declaration | |
5683 | || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration) | |
5daed84a EB |
5684 | return; |
5685 | } | |
5686 | ||
257e81a6 EB |
5687 | /* Then process any pragmas and declarations preceding the unit. */ |
5688 | for (gnat_pragma = First (Context_Items (gnat_node)); | |
5689 | Present (gnat_pragma); | |
5690 | gnat_pragma = Next (gnat_pragma)) | |
5691 | if (Nkind (gnat_pragma) == N_Pragma) | |
5692 | add_stmt (gnat_to_gnu (gnat_pragma)); | |
6d16658d | 5693 | process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, |
5daed84a | 5694 | true, true); |
257e81a6 EB |
5695 | |
5696 | /* Process the unit itself. */ | |
6ddf9843 | 5697 | add_stmt (gnat_to_gnu (gnat_unit)); |
5daed84a | 5698 | |
384e3fb1 JM |
5699 | /* Generate code for all the inlined subprograms. */ |
5700 | for (gnat_entity = First_Inlined_Subprogram (gnat_node); | |
5701 | Present (gnat_entity); | |
5702 | gnat_entity = Next_Inlined_Subprogram (gnat_entity)) | |
58c8f770 | 5703 | { |
384e3fb1 | 5704 | Node_Id gnat_body; |
58c8f770 | 5705 | |
384e3fb1 JM |
5706 | /* Without optimization, process only the required subprograms. */ |
5707 | if (!optimize && !Has_Pragma_Inline_Always (gnat_entity)) | |
5708 | continue; | |
58c8f770 | 5709 | |
0c09a01e EB |
5710 | /* The set of inlined subprograms is computed from data recorded early |
5711 | during expansion and it can be a strict superset of the final set | |
5712 | computed after semantic analysis, for example if a call to such a | |
5713 | subprogram occurs in a pragma Assert and assertions are disabled. | |
5714 | In that case, semantic analysis resets Is_Public to false but the | |
5715 | entry for the subprogram in the inlining tables is stalled. */ | |
5716 | if (!Is_Public (gnat_entity)) | |
5717 | continue; | |
5718 | ||
384e3fb1 JM |
5719 | gnat_body = Parent (Declaration_Node (gnat_entity)); |
5720 | if (Nkind (gnat_body) != N_Subprogram_Body) | |
5721 | { | |
5722 | /* ??? This happens when only the spec of a package is provided. */ | |
5723 | if (No (Corresponding_Body (gnat_body))) | |
5724 | continue; | |
58c8f770 | 5725 | |
384e3fb1 JM |
5726 | gnat_body |
5727 | = Parent (Declaration_Node (Corresponding_Body (gnat_body))); | |
58c8f770 | 5728 | } |
384e3fb1 JM |
5729 | |
5730 | /* Define the entity first so we set DECL_EXTERNAL. */ | |
afc737f0 | 5731 | gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
384e3fb1 | 5732 | add_stmt (gnat_to_gnu (gnat_body)); |
58c8f770 | 5733 | } |
a1ab4c31 | 5734 | |
a1ab4c31 AC |
5735 | /* Process any pragmas and actions following the unit. */ |
5736 | add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node))); | |
5737 | add_stmt_list (Actions (Aux_Decls_Node (gnat_node))); | |
7b56a91b | 5738 | finalize_from_limited_with (); |
a1ab4c31 | 5739 | |
ec4a0d83 EB |
5740 | /* Then process the expressions of pragma Compile_Time_{Error|Warning} to |
5741 | annotate types referenced therein if they have not been annotated. */ | |
5742 | for (int i = 0; gnat_compile_time_expr_list.iterate (i, &gnat_iter); i++) | |
5743 | (void) gnat_to_gnu_external (gnat_iter); | |
5744 | gnat_compile_time_expr_list.release (); | |
5745 | ||
ea292448 | 5746 | /* Save away what we've made so far and finish it up. */ |
a1ab4c31 AC |
5747 | set_current_block_context (gnu_elab_proc_decl); |
5748 | gnat_poplevel (); | |
5749 | DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group (); | |
2a02d090 | 5750 | set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit); |
ea292448 | 5751 | gnu_elab_proc_stack->pop (); |
58c8f770 | 5752 | |
ea292448 EB |
5753 | /* Record this potential elaboration procedure for later processing. */ |
5754 | info = ggc_alloc<elab_info> (); | |
a1ab4c31 AC |
5755 | info->next = elab_info_list; |
5756 | info->elab_proc = gnu_elab_proc_decl; | |
5757 | info->gnat_node = gnat_node; | |
5758 | elab_info_list = info; | |
5759 | ||
9a30c7c4 AC |
5760 | /* Force the processing for all nodes that remain in the queue. */ |
5761 | process_deferred_decl_context (true); | |
a1ab4c31 | 5762 | } |
ce2d0ce2 | 5763 | |
64235766 EB |
5764 | /* Mark COND, a boolean expression, as predicating a call to a noreturn |
5765 | function, i.e. predict that it is very likely false, and return it. | |
5766 | ||
5767 | The compiler will automatically predict the last edge leading to a call | |
5768 | to a noreturn function as very unlikely taken. This function makes it | |
b9778c45 | 5769 | possible to extend the prediction to predecessors in case the condition |
64235766 EB |
5770 | is made up of several short-circuit operators. */ |
5771 | ||
5772 | static tree | |
5773 | build_noreturn_cond (tree cond) | |
5774 | { | |
b9778c45 EB |
5775 | tree pred_cst = build_int_cst (integer_type_node, PRED_NORETURN); |
5776 | return | |
5777 | build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_BUILTIN_EXPECT, | |
5778 | boolean_type_node, 3, cond, | |
5779 | boolean_false_node, pred_cst); | |
64235766 EB |
5780 | } |
5781 | ||
3eaec285 EB |
5782 | /* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a |
5783 | range of values, into GNU_LOW and GNU_HIGH bounds. */ | |
5784 | ||
5785 | static void | |
5786 | Range_to_gnu (Node_Id gnat_range, tree *gnu_low, tree *gnu_high) | |
5787 | { | |
5788 | /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */ | |
5789 | switch (Nkind (gnat_range)) | |
5790 | { | |
5791 | case N_Range: | |
5792 | *gnu_low = gnat_to_gnu (Low_Bound (gnat_range)); | |
5793 | *gnu_high = gnat_to_gnu (High_Bound (gnat_range)); | |
5794 | break; | |
5795 | ||
5796 | case N_Expanded_Name: | |
5797 | case N_Identifier: | |
5798 | { | |
5799 | tree gnu_range_type = get_unpadded_type (Entity (gnat_range)); | |
5800 | tree gnu_range_base_type = get_base_type (gnu_range_type); | |
5801 | ||
5802 | *gnu_low | |
5803 | = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type)); | |
5804 | *gnu_high | |
5805 | = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type)); | |
5806 | } | |
5807 | break; | |
5808 | ||
5809 | default: | |
5810 | gcc_unreachable (); | |
5811 | } | |
5812 | } | |
5813 | ||
9a1bdc31 | 5814 | /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error, |
e08f1aad EB |
5815 | to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to |
5816 | where we should place the result type. */ | |
80096613 EB |
5817 | |
5818 | static tree | |
5819 | Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p) | |
5820 | { | |
5821 | const Node_Kind kind = Nkind (gnat_node); | |
80096613 | 5822 | const Node_Id gnat_cond = Condition (gnat_node); |
c900c700 | 5823 | const int reason = UI_To_Int (Reason (gnat_node)); |
80096613 EB |
5824 | const bool with_extra_info |
5825 | = Exception_Extra_Info | |
5826 | && !No_Exception_Handlers_Set () | |
8f8f531f | 5827 | && No (get_exception_label (kind)); |
80096613 | 5828 | tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE; |
c900c700 | 5829 | Node_Id gnat_rcond; |
80096613 | 5830 | |
9a1bdc31 EB |
5831 | /* The following processing is not required for correctness. Its purpose is |
5832 | to give more precise error messages and to record some information. */ | |
80096613 EB |
5833 | switch (reason) |
5834 | { | |
5835 | case CE_Access_Check_Failed: | |
5836 | if (with_extra_info) | |
9a1bdc31 | 5837 | gnu_result = build_call_raise_column (reason, gnat_node, kind); |
80096613 EB |
5838 | break; |
5839 | ||
5840 | case CE_Index_Check_Failed: | |
5841 | case CE_Range_Check_Failed: | |
5842 | case CE_Invalid_Data: | |
c900c700 EB |
5843 | if (No (gnat_cond) || Nkind (gnat_cond) != N_Op_Not) |
5844 | break; | |
5845 | gnat_rcond = Right_Opnd (gnat_cond); | |
5846 | if (Nkind (gnat_rcond) == N_In | |
5847 | || Nkind (gnat_rcond) == N_Op_Ge | |
5848 | || Nkind (gnat_rcond) == N_Op_Le) | |
80096613 | 5849 | { |
c900c700 EB |
5850 | const Node_Id gnat_index = Left_Opnd (gnat_rcond); |
5851 | const Node_Id gnat_type = Etype (gnat_index); | |
5852 | tree gnu_index = gnat_to_gnu (gnat_index); | |
5853 | tree gnu_type = get_unpadded_type (gnat_type); | |
5854 | tree gnu_low_bound, gnu_high_bound, disp; | |
87ab2b04 | 5855 | struct loop_info_d *loop; |
c900c700 | 5856 | bool neg_p; |
80096613 | 5857 | |
c900c700 | 5858 | switch (Nkind (gnat_rcond)) |
80096613 EB |
5859 | { |
5860 | case N_In: | |
c900c700 | 5861 | Range_to_gnu (Right_Opnd (gnat_rcond), |
3eaec285 | 5862 | &gnu_low_bound, &gnu_high_bound); |
80096613 EB |
5863 | break; |
5864 | ||
5865 | case N_Op_Ge: | |
c900c700 EB |
5866 | gnu_low_bound = gnat_to_gnu (Right_Opnd (gnat_rcond)); |
5867 | gnu_high_bound = TYPE_MAX_VALUE (gnu_type); | |
80096613 EB |
5868 | break; |
5869 | ||
5870 | case N_Op_Le: | |
c900c700 EB |
5871 | gnu_low_bound = TYPE_MIN_VALUE (gnu_type); |
5872 | gnu_high_bound = gnat_to_gnu (Right_Opnd (gnat_rcond)); | |
80096613 EB |
5873 | break; |
5874 | ||
5875 | default: | |
c900c700 | 5876 | gcc_unreachable (); |
80096613 EB |
5877 | } |
5878 | ||
c900c700 | 5879 | gnu_type = maybe_character_type (gnu_type); |
825da0d2 EB |
5880 | if (TREE_TYPE (gnu_index) != gnu_type) |
5881 | { | |
c900c700 EB |
5882 | gnu_low_bound = convert (gnu_type, gnu_low_bound); |
5883 | gnu_high_bound = convert (gnu_type, gnu_high_bound); | |
825da0d2 EB |
5884 | gnu_index = convert (gnu_type, gnu_index); |
5885 | } | |
5886 | ||
80096613 | 5887 | if (with_extra_info |
80096613 EB |
5888 | && Known_Esize (gnat_type) |
5889 | && UI_To_Int (Esize (gnat_type)) <= 32) | |
5890 | gnu_result | |
9a1bdc31 | 5891 | = build_call_raise_range (reason, gnat_node, kind, gnu_index, |
80096613 EB |
5892 | gnu_low_bound, gnu_high_bound); |
5893 | ||
64235766 EB |
5894 | /* If optimization is enabled and we are inside a loop, we try to |
5895 | compute invariant conditions for checks applied to the iteration | |
5896 | variable, i.e. conditions that are independent of the variable | |
5897 | and necessary in order for the checks to fail in the course of | |
5898 | some iteration. If we succeed, we consider an alternative: | |
5899 | ||
5900 | 1. If loop unswitching is enabled, we prepend these conditions | |
5901 | to the original conditions of the checks. This will make it | |
5902 | possible for the loop unswitching pass to replace the loop | |
5903 | with two loops, one of which has the checks eliminated and | |
5904 | the other has the original checks reinstated, and a prologue | |
5905 | implementing a run-time selection. The former loop will be | |
5906 | for example suitable for vectorization. | |
5907 | ||
5908 | 2. Otherwise, we instead append the conditions to the original | |
5909 | conditions of the checks. At worse, if the conditions cannot | |
5910 | be evaluated at compile time, they will be evaluated as true | |
5911 | at run time only when the checks have already failed, thus | |
5912 | contributing negatively only to the size of the executable. | |
5913 | But the hope is that these invariant conditions be evaluated | |
5914 | at compile time to false, thus taking away the entire checks | |
5915 | with them. */ | |
87ab2b04 | 5916 | if (optimize |
87ab2b04 | 5917 | && inside_loop_p () |
80096613 EB |
5918 | && (!gnu_low_bound |
5919 | || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound))) | |
5920 | && (!gnu_high_bound | |
5921 | || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound))) | |
933a7325 | 5922 | && (loop = find_loop_for (gnu_index, &disp, &neg_p))) |
80096613 | 5923 | { |
87ab2b04 | 5924 | struct range_check_info_d *rci = ggc_alloc<range_check_info_d> (); |
80096613 EB |
5925 | rci->low_bound = gnu_low_bound; |
5926 | rci->high_bound = gnu_high_bound; | |
933a7325 EB |
5927 | rci->disp = disp; |
5928 | rci->neg_p = neg_p; | |
825da0d2 | 5929 | rci->type = gnu_type; |
64235766 EB |
5930 | rci->inserted_cond |
5931 | = build1 (SAVE_EXPR, boolean_type_node, boolean_true_node); | |
87ab2b04 | 5932 | vec_safe_push (loop->checks, rci); |
64235766 | 5933 | gnu_cond = build_noreturn_cond (gnat_to_gnu (gnat_cond)); |
3bdf0b64 | 5934 | if (optimize >= 3) |
64235766 EB |
5935 | gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR, |
5936 | boolean_type_node, | |
5937 | rci->inserted_cond, | |
5938 | gnu_cond); | |
5939 | else | |
5940 | gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR, | |
5941 | boolean_type_node, | |
5942 | gnu_cond, | |
5943 | rci->inserted_cond); | |
80096613 EB |
5944 | } |
5945 | } | |
5946 | break; | |
5947 | ||
5948 | default: | |
5949 | break; | |
5950 | } | |
5951 | ||
c900c700 EB |
5952 | /* The following processing does the real work, but we must nevertheless make |
5953 | sure not to override the result of the previous processing. */ | |
80096613 EB |
5954 | if (!gnu_result) |
5955 | gnu_result = build_call_raise (reason, gnat_node, kind); | |
5956 | set_expr_location_from_node (gnu_result, gnat_node); | |
5957 | ||
9a1bdc31 EB |
5958 | *gnu_result_type_p = get_unpadded_type (Etype (gnat_node)); |
5959 | ||
80096613 EB |
5960 | /* If the type is VOID, this is a statement, so we need to generate the code |
5961 | for the call. Handle a condition, if there is one. */ | |
5962 | if (VOID_TYPE_P (*gnu_result_type_p)) | |
5963 | { | |
5964 | if (Present (gnat_cond)) | |
5965 | { | |
5966 | if (!gnu_cond) | |
5967 | gnu_cond = gnat_to_gnu (gnat_cond); | |
f53aff92 EB |
5968 | if (integer_zerop (gnu_cond)) |
5969 | return alloc_stmt_list (); | |
80096613 EB |
5970 | gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result, |
5971 | alloc_stmt_list ()); | |
5972 | } | |
5973 | } | |
5974 | else | |
6fdacaf4 PT |
5975 | { |
5976 | /* The condition field must not be present when the node is used as an | |
5977 | expression form. */ | |
5978 | gigi_checking_assert (No (gnat_cond)); | |
5979 | gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result); | |
5980 | } | |
80096613 EB |
5981 | |
5982 | return gnu_result; | |
5983 | } | |
ce2d0ce2 | 5984 | |
27ab5bd8 EB |
5985 | /* Return true if GNAT_NODE is on the LHS of an assignment or an actual |
5986 | parameter of a call. */ | |
5987 | ||
5988 | static bool | |
5989 | lhs_or_actual_p (Node_Id gnat_node) | |
5990 | { | |
17a98a3d EB |
5991 | const Node_Id gnat_parent = Parent (gnat_node); |
5992 | const Node_Kind kind = Nkind (gnat_parent); | |
27ab5bd8 EB |
5993 | |
5994 | if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node) | |
5995 | return true; | |
5996 | ||
5997 | if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call) | |
5998 | && Name (gnat_parent) != gnat_node) | |
5999 | return true; | |
6000 | ||
6001 | if (kind == N_Parameter_Association) | |
6002 | return true; | |
6003 | ||
6004 | return false; | |
6005 | } | |
6006 | ||
033ba5bf EB |
6007 | /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS |
6008 | of an assignment or an actual parameter of a call. */ | |
6009 | ||
6010 | static bool | |
6011 | present_in_lhs_or_actual_p (Node_Id gnat_node) | |
6012 | { | |
033ba5bf EB |
6013 | if (lhs_or_actual_p (gnat_node)) |
6014 | return true; | |
6015 | ||
17a98a3d | 6016 | const Node_Kind kind = Nkind (Parent (gnat_node)); |
033ba5bf EB |
6017 | |
6018 | if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion) | |
6019 | && lhs_or_actual_p (Parent (gnat_node))) | |
6020 | return true; | |
6021 | ||
6022 | return false; | |
6023 | } | |
6024 | ||
4f8a6678 EB |
6025 | /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far |
6026 | as gigi is concerned. This is used to avoid conversions on the LHS. */ | |
c2efda0d EB |
6027 | |
6028 | static bool | |
4f8a6678 | 6029 | unchecked_conversion_nop (Node_Id gnat_node) |
c2efda0d EB |
6030 | { |
6031 | Entity_Id from_type, to_type; | |
6032 | ||
4f8a6678 EB |
6033 | /* The conversion must be on the LHS of an assignment or an actual parameter |
6034 | of a call. Otherwise, even if the conversion was essentially a no-op, it | |
6035 | could de facto ensure type consistency and this should be preserved. */ | |
27ab5bd8 | 6036 | if (!lhs_or_actual_p (gnat_node)) |
c2efda0d EB |
6037 | return false; |
6038 | ||
6039 | from_type = Etype (Expression (gnat_node)); | |
6040 | ||
6041 | /* We're interested in artificial conversions generated by the front-end | |
6042 | to make private types explicit, e.g. in Expand_Assign_Array. */ | |
6043 | if (!Is_Private_Type (from_type)) | |
6044 | return false; | |
6045 | ||
6046 | from_type = Underlying_Type (from_type); | |
6047 | to_type = Etype (gnat_node); | |
6048 | ||
6049 | /* The direct conversion to the underlying type is a no-op. */ | |
6050 | if (to_type == from_type) | |
6051 | return true; | |
6052 | ||
1a4cb227 | 6053 | /* For an array subtype, the conversion to the PAIT is a no-op. */ |
c2efda0d | 6054 | if (Ekind (from_type) == E_Array_Subtype |
1a4cb227 | 6055 | && to_type == Packed_Array_Impl_Type (from_type)) |
c2efda0d EB |
6056 | return true; |
6057 | ||
68a3eb69 EB |
6058 | /* For a record subtype, the conversion to the type is a no-op. */ |
6059 | if (Ekind (from_type) == E_Record_Subtype | |
6060 | && to_type == Etype (from_type)) | |
6061 | return true; | |
6062 | ||
c2efda0d EB |
6063 | return false; |
6064 | } | |
6065 | ||
4c335163 EB |
6066 | /* Return true if GNAT_NODE represents a statement. */ |
6067 | ||
6068 | static bool | |
6069 | statement_node_p (Node_Id gnat_node) | |
6070 | { | |
6071 | const Node_Kind kind = Nkind (gnat_node); | |
6072 | ||
6073 | if (kind == N_Label) | |
6074 | return true; | |
6075 | ||
6076 | if (IN (kind, N_Statement_Other_Than_Procedure_Call)) | |
6077 | return true; | |
6078 | ||
6079 | if (kind == N_Procedure_Call_Statement) | |
6080 | return true; | |
6081 | ||
6082 | if (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void) | |
6083 | return true; | |
6084 | ||
6085 | return false; | |
6086 | } | |
6087 | ||
3f13dd77 EB |
6088 | /* This function is the driver of the GNAT to GCC tree transformation process. |
6089 | It is the entry point of the tree transformer. GNAT_NODE is the root of | |
6090 | some GNAT tree. Return the root of the corresponding GCC tree. If this | |
6091 | is an expression, return the GCC equivalent of the expression. If this | |
6092 | is a statement, return the statement or add it to the current statement | |
6093 | group, in which case anything returned is to be interpreted as occurring | |
6094 | after anything added. */ | |
a1ab4c31 AC |
6095 | |
6096 | tree | |
6097 | gnat_to_gnu (Node_Id gnat_node) | |
6098 | { | |
3f13dd77 | 6099 | const Node_Kind kind = Nkind (gnat_node); |
1e17ef87 | 6100 | tree gnu_result = error_mark_node; /* Default to no value. */ |
a1ab4c31 | 6101 | tree gnu_result_type = void_type_node; |
3f13dd77 | 6102 | tree gnu_expr, gnu_lhs, gnu_rhs; |
a1ab4c31 | 6103 | Node_Id gnat_temp; |
17a98a3d | 6104 | atomic_acces_t aa_type; |
f53aff92 | 6105 | bool went_into_elab_proc; |
17a98a3d | 6106 | bool aa_sync; |
a1ab4c31 AC |
6107 | |
6108 | /* Save node number for error message and set location information. */ | |
b64c4968 EB |
6109 | if (Sloc (gnat_node) > No_Location) |
6110 | Current_Error_Node = gnat_node; | |
a1ab4c31 AC |
6111 | Sloc_to_locus (Sloc (gnat_node), &input_location); |
6112 | ||
4c335163 EB |
6113 | /* If we are only annotating types and this node is a statement, return |
6114 | an empty statement list. */ | |
6115 | if (type_annotate_only && statement_node_p (gnat_node)) | |
a1ab4c31 AC |
6116 | return alloc_stmt_list (); |
6117 | ||
4c335163 EB |
6118 | /* If we are only annotating types and this node is a subexpression, return |
6119 | a NULL_EXPR, but filter out nodes appearing in the expressions attached | |
6120 | to packed array implementation types. */ | |
a1ab4c31 | 6121 | if (type_annotate_only |
3f13dd77 | 6122 | && IN (kind, N_Subexpr) |
4c335163 EB |
6123 | && !(((IN (kind, N_Op) && kind != N_Op_Expon) |
6124 | || kind == N_Type_Conversion) | |
6125 | && Is_Integer_Type (Etype (gnat_node))) | |
6126 | && !(kind == N_Attribute_Reference | |
4b9e1bc7 EB |
6127 | && (Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Length |
6128 | || Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Size) | |
6129 | && Is_Constrained (Etype (Prefix (gnat_node))) | |
4c335163 | 6130 | && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node)))) |
93200f92 | 6131 | && kind != N_Expanded_Name |
3f13dd77 | 6132 | && kind != N_Identifier |
a1ab4c31 AC |
6133 | && !Compile_Time_Known_Value (gnat_node)) |
6134 | return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)), | |
6135 | build_call_raise (CE_Range_Check_Failed, gnat_node, | |
6136 | N_Raise_Constraint_Error)); | |
6137 | ||
f53aff92 EB |
6138 | /* If this is a statement and we are at top level, it must be part of the |
6139 | elaboration procedure, so mark us as being in that procedure. */ | |
6140 | if ((statement_node_p (gnat_node) | |
6141 | || kind == N_Handled_Sequence_Of_Statements | |
6142 | || kind == N_Implicit_Label_Declaration) | |
6143 | && !current_function_decl) | |
a1ab4c31 | 6144 | { |
f53aff92 EB |
6145 | current_function_decl = get_elaboration_procedure (); |
6146 | went_into_elab_proc = true; | |
a1ab4c31 | 6147 | } |
f53aff92 EB |
6148 | else |
6149 | went_into_elab_proc = false; | |
a1ab4c31 | 6150 | |
3f13dd77 | 6151 | switch (kind) |
a1ab4c31 AC |
6152 | { |
6153 | /********************************/ | |
1e17ef87 | 6154 | /* Chapter 2: Lexical Elements */ |
a1ab4c31 AC |
6155 | /********************************/ |
6156 | ||
6157 | case N_Identifier: | |
6158 | case N_Expanded_Name: | |
6159 | case N_Operator_Symbol: | |
6160 | case N_Defining_Identifier: | |
1d4b96e0 | 6161 | case N_Defining_Operator_Symbol: |
a1ab4c31 | 6162 | gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type); |
033ba5bf | 6163 | |
f797c2b7 | 6164 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6165 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6166 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6167 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
a1ab4c31 AC |
6168 | break; |
6169 | ||
6170 | case N_Integer_Literal: | |
6171 | { | |
6172 | tree gnu_type; | |
6173 | ||
6174 | /* Get the type of the result, looking inside any padding and | |
6175 | justified modular types. Then get the value in that type. */ | |
6176 | gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6177 | ||
6178 | if (TREE_CODE (gnu_type) == RECORD_TYPE | |
6179 | && TYPE_JUSTIFIED_MODULAR_P (gnu_type)) | |
6180 | gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type)); | |
6181 | ||
6182 | gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type); | |
6183 | ||
6184 | /* If the result overflows (meaning it doesn't fit in its base type), | |
104099b8 EB |
6185 | abort, unless this is for a named number because that's not fatal. |
6186 | We would like to check that the value is within the range of the | |
6187 | subtype, but that causes problems with subtypes whose usage will | |
6188 | raise Constraint_Error and also with biased representation. */ | |
6189 | if (TREE_OVERFLOW (gnu_result)) | |
6190 | { | |
6191 | if (Nkind (Parent (gnat_node)) == N_Number_Declaration) | |
6192 | gnu_result = error_mark_node; | |
6193 | else | |
6194 | gcc_unreachable (); | |
6195 | } | |
a1ab4c31 AC |
6196 | } |
6197 | break; | |
6198 | ||
6199 | case N_Character_Literal: | |
6200 | /* If a Entity is present, it means that this was one of the | |
6201 | literals in a user-defined character type. In that case, | |
6202 | just return the value in the CONST_DECL. Otherwise, use the | |
6203 | character code. In that case, the base type should be an | |
6204 | INTEGER_TYPE, but we won't bother checking for that. */ | |
6205 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6206 | if (Present (Entity (gnat_node))) | |
6207 | gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node))); | |
6208 | else | |
6209 | gnu_result | |
9a1bdc31 EB |
6210 | = build_int_cst (gnu_result_type, |
6211 | UI_To_CC (Char_Literal_Value (gnat_node))); | |
a1ab4c31 AC |
6212 | break; |
6213 | ||
6214 | case N_Real_Literal: | |
015bee83 AC |
6215 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6216 | ||
08477031 EB |
6217 | /* If this is of a fixed-point type, the value we want is the value of |
6218 | the corresponding integer. */ | |
7ed9919d | 6219 | if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node)))) |
a1ab4c31 | 6220 | { |
a1ab4c31 AC |
6221 | gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node), |
6222 | gnu_result_type); | |
6223 | gcc_assert (!TREE_OVERFLOW (gnu_result)); | |
6224 | } | |
6225 | ||
a1ab4c31 | 6226 | else |
1e17ef87 | 6227 | { |
a1ab4c31 AC |
6228 | Ureal ur_realval = Realval (gnat_node); |
6229 | ||
08477031 EB |
6230 | /* First convert the value to a machine number if it isn't already. |
6231 | That will force the base to 2 for non-zero values and simplify | |
6232 | the rest of the logic. */ | |
015bee83 AC |
6233 | if (!Is_Machine_Number (gnat_node)) |
6234 | ur_realval | |
6235 | = Machine (Base_Type (Underlying_Type (Etype (gnat_node))), | |
6236 | ur_realval, Round_Even, gnat_node); | |
a1ab4c31 | 6237 | |
a1ab4c31 | 6238 | if (UR_Is_Zero (ur_realval)) |
9a1bdc31 | 6239 | gnu_result = build_real (gnu_result_type, dconst0); |
a1ab4c31 AC |
6240 | else |
6241 | { | |
015bee83 | 6242 | REAL_VALUE_TYPE tmp; |
a1ab4c31 | 6243 | |
08477031 | 6244 | gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type); |
a1ab4c31 | 6245 | |
015bee83 | 6246 | /* The base must be 2 as Machine guarantees this, so we scale |
08477031 EB |
6247 | the value, which we know can fit in the mantissa of the type |
6248 | (hence the use of that type above). */ | |
015bee83 AC |
6249 | gcc_assert (Rbase (ur_realval) == 2); |
6250 | real_ldexp (&tmp, &TREE_REAL_CST (gnu_result), | |
6251 | - UI_To_Int (Denominator (ur_realval))); | |
6252 | gnu_result = build_real (gnu_result_type, tmp); | |
a1ab4c31 AC |
6253 | } |
6254 | ||
6255 | /* Now see if we need to negate the result. Do it this way to | |
6256 | properly handle -0. */ | |
6257 | if (UR_Is_Negative (Realval (gnat_node))) | |
6258 | gnu_result | |
6259 | = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type), | |
6260 | gnu_result); | |
6261 | } | |
6262 | ||
6263 | break; | |
6264 | ||
6265 | case N_String_Literal: | |
6266 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6267 | if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR) | |
6268 | { | |
6269 | String_Id gnat_string = Strval (gnat_node); | |
6270 | int length = String_Length (gnat_string); | |
6271 | int i; | |
6272 | char *string; | |
6273 | if (length >= ALLOCA_THRESHOLD) | |
ad00a297 | 6274 | string = XNEWVEC (char, length); |
1e17ef87 | 6275 | else |
ad00a297 | 6276 | string = (char *) alloca (length); |
a1ab4c31 AC |
6277 | |
6278 | /* Build the string with the characters in the literal. Note | |
6279 | that Ada strings are 1-origin. */ | |
6280 | for (i = 0; i < length; i++) | |
6281 | string[i] = Get_String_Char (gnat_string, i + 1); | |
6282 | ||
a1ab4c31 AC |
6283 | gnu_result = build_string (length, string); |
6284 | ||
6285 | /* Strings in GCC don't normally have types, but we want | |
6286 | this to not be converted to the array type. */ | |
6287 | TREE_TYPE (gnu_result) = gnu_result_type; | |
6288 | ||
1e17ef87 EB |
6289 | if (length >= ALLOCA_THRESHOLD) |
6290 | free (string); | |
a1ab4c31 AC |
6291 | } |
6292 | else | |
6293 | { | |
6294 | /* Build a list consisting of each character, then make | |
6295 | the aggregate. */ | |
6296 | String_Id gnat_string = Strval (gnat_node); | |
6297 | int length = String_Length (gnat_string); | |
6298 | int i; | |
a1ab4c31 | 6299 | tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type)); |
8b9aec86 | 6300 | tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node); |
9771b263 DN |
6301 | vec<constructor_elt, va_gc> *gnu_vec; |
6302 | vec_alloc (gnu_vec, length); | |
a1ab4c31 AC |
6303 | |
6304 | for (i = 0; i < length; i++) | |
6305 | { | |
0e228dd9 NF |
6306 | tree t = build_int_cst (TREE_TYPE (gnu_result_type), |
6307 | Get_String_Char (gnat_string, i + 1)); | |
a1ab4c31 | 6308 | |
0e228dd9 | 6309 | CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t); |
8b9aec86 | 6310 | gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node); |
a1ab4c31 AC |
6311 | } |
6312 | ||
0e228dd9 | 6313 | gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec); |
a1ab4c31 AC |
6314 | } |
6315 | break; | |
6316 | ||
6317 | case N_Pragma: | |
6318 | gnu_result = Pragma_to_gnu (gnat_node); | |
6319 | break; | |
6320 | ||
6321 | /**************************************/ | |
1e17ef87 | 6322 | /* Chapter 3: Declarations and Types */ |
a1ab4c31 AC |
6323 | /**************************************/ |
6324 | ||
6325 | case N_Subtype_Declaration: | |
6326 | case N_Full_Type_Declaration: | |
6327 | case N_Incomplete_Type_Declaration: | |
6328 | case N_Private_Type_Declaration: | |
6329 | case N_Private_Extension_Declaration: | |
6330 | case N_Task_Type_Declaration: | |
6331 | process_type (Defining_Entity (gnat_node)); | |
6332 | gnu_result = alloc_stmt_list (); | |
6333 | break; | |
6334 | ||
6335 | case N_Object_Declaration: | |
104099b8 | 6336 | case N_Number_Declaration: |
a1ab4c31 AC |
6337 | case N_Exception_Declaration: |
6338 | gnat_temp = Defining_Entity (gnat_node); | |
6339 | gnu_result = alloc_stmt_list (); | |
6340 | ||
6341 | /* If we are just annotating types and this object has an unconstrained | |
6342 | or task type, don't elaborate it. */ | |
6343 | if (type_annotate_only | |
6344 | && (((Is_Array_Type (Etype (gnat_temp)) | |
6345 | || Is_Record_Type (Etype (gnat_temp))) | |
6346 | && !Is_Constrained (Etype (gnat_temp))) | |
3d25b3ba | 6347 | || Is_Concurrent_Type (Etype (gnat_temp)))) |
a1ab4c31 AC |
6348 | break; |
6349 | ||
9aa9ba95 EB |
6350 | /* If this is a constant related to a return initialized by a reference |
6351 | to a function call in a function returning by invisible reference: | |
6352 | ||
6353 | type Ann is access all Result_Type; | |
6354 | Rnn : constant Ann := Func'reference; | |
6355 | [...] | |
6356 | return Rnn.all; | |
6357 | ||
6358 | then elide the temporary by forwarding the return object to Func: | |
6359 | ||
6360 | *<retval> = Func (); [return slot optimization] | |
6361 | [...] | |
6362 | return <retval>; | |
6363 | ||
6364 | That's necessary if the result type needs finalization because the | |
6365 | temporary would never be adjusted as Expand_Simple_Function_Return | |
6366 | also elides the temporary in this case. */ | |
6367 | if (Ekind (gnat_temp) == E_Constant | |
6368 | && Is_Related_To_Func_Return (gnat_temp) | |
6369 | && Nkind (Expression (gnat_node)) == N_Reference | |
6370 | && Nkind (Prefix (Expression (gnat_node))) == N_Function_Call | |
6371 | && current_function_decl | |
6372 | && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl))) | |
6373 | { | |
6374 | gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, true); | |
6375 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result); | |
6376 | gnu_result | |
6377 | = Call_to_gnu (Prefix (Expression (gnat_node)), | |
6378 | &gnu_result_type, gnu_result, | |
6379 | NOT_ATOMIC, false); | |
6380 | break; | |
6381 | } | |
6382 | ||
a1ab4c31 | 6383 | if (Present (Expression (gnat_node)) |
3f13dd77 | 6384 | && !(kind == N_Object_Declaration && No_Initialization (gnat_node)) |
a1ab4c31 AC |
6385 | && (!type_annotate_only |
6386 | || Compile_Time_Known_Value (Expression (gnat_node)))) | |
6387 | { | |
815b5368 EB |
6388 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_node))); |
6389 | ||
a1ab4c31 | 6390 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); |
a1ab4c31 | 6391 | |
b4407150 | 6392 | /* First deal with erroneous expressions. */ |
104099b8 EB |
6393 | if (TREE_CODE (gnu_expr) == ERROR_MARK) |
6394 | { | |
6395 | /* If this is a named number for which we cannot manipulate | |
6396 | the value, just skip the declaration altogether. */ | |
6397 | if (kind == N_Number_Declaration) | |
6398 | break; | |
6399 | else if (type_annotate_only) | |
6400 | gnu_expr = NULL_TREE; | |
6401 | } | |
b4407150 EB |
6402 | |
6403 | /* Then a special case: we do not want the SLOC of the expression | |
6404 | of the tag to pop up every time it is referenced somewhere. */ | |
6405 | else if (EXPR_P (gnu_expr) && Is_Tag (gnat_temp)) | |
6406 | SET_EXPR_LOCATION (gnu_expr, UNKNOWN_LOCATION); | |
a1ab4c31 AC |
6407 | } |
6408 | else | |
6409 | gnu_expr = NULL_TREE; | |
6410 | ||
8df2e902 EB |
6411 | /* If this is a deferred constant with an address clause, we ignore the |
6412 | full view since the clause is on the partial view and we cannot have | |
6413 | 2 different GCC trees for the object. The only bits of the full view | |
6414 | we will use is the initializer, but it will be directly fetched. */ | |
7c775aca | 6415 | if (Ekind (gnat_temp) == E_Constant |
8df2e902 EB |
6416 | && Present (Address_Clause (gnat_temp)) |
6417 | && Present (Full_View (gnat_temp))) | |
6418 | save_gnu_tree (Full_View (gnat_temp), error_mark_node, true); | |
6419 | ||
545b4923 EB |
6420 | /* If this object has its elaboration delayed, we must force evaluation |
6421 | of GNU_EXPR now and save it for the freeze point. Note that we need | |
6422 | not do anything special at the global level since the lifetime of the | |
6423 | temporary is fully contained within the elaboration routine. */ | |
6424 | if (Present (Freeze_Node (gnat_temp))) | |
6425 | { | |
6426 | if (gnu_expr) | |
6427 | { | |
6428 | gnu_result = gnat_save_expr (gnu_expr); | |
6429 | save_gnu_tree (gnat_node, gnu_result, true); | |
6430 | } | |
6431 | } | |
6432 | else | |
afc737f0 | 6433 | gnat_to_gnu_entity (gnat_temp, gnu_expr, true); |
a1ab4c31 AC |
6434 | break; |
6435 | ||
6436 | case N_Object_Renaming_Declaration: | |
6437 | gnat_temp = Defining_Entity (gnat_node); | |
ea292448 | 6438 | gnu_result = alloc_stmt_list (); |
a1ab4c31 | 6439 | |
656b0311 PMR |
6440 | /* Don't do anything if this renaming is handled by the front end and it |
6441 | does not need debug info. Note that we consider renamings don't need | |
6442 | debug info when optimizing: our way to describe them has a | |
6443 | memory/elaboration footprint. | |
6444 | ||
6445 | Don't do anything neither if we are just annotating types and this | |
6446 | object has a composite or task type, don't elaborate it. */ | |
6447 | if ((!Is_Renaming_Of_Object (gnat_temp) | |
6448 | || (Needs_Debug_Info (gnat_temp) | |
6449 | && !optimize | |
6450 | && can_materialize_object_renaming_p | |
6451 | (Renamed_Object (gnat_temp)))) | |
a1ab4c31 AC |
6452 | && ! (type_annotate_only |
6453 | && (Is_Array_Type (Etype (gnat_temp)) | |
6454 | || Is_Record_Type (Etype (gnat_temp)) | |
6455 | || Is_Concurrent_Type (Etype (gnat_temp))))) | |
5bdd063b EB |
6456 | gnat_to_gnu_entity (gnat_temp, |
6457 | gnat_to_gnu (Renamed_Object (gnat_temp)), | |
6458 | true); | |
a1ab4c31 AC |
6459 | break; |
6460 | ||
ac43e11e AC |
6461 | case N_Exception_Renaming_Declaration: |
6462 | gnat_temp = Defining_Entity (gnat_node); | |
ea292448 EB |
6463 | gnu_result = alloc_stmt_list (); |
6464 | ||
ea292448 | 6465 | if (Present (Renamed_Entity (gnat_temp))) |
5bdd063b EB |
6466 | gnat_to_gnu_entity (gnat_temp, |
6467 | gnat_to_gnu (Renamed_Entity (gnat_temp)), | |
6468 | true); | |
ac43e11e AC |
6469 | break; |
6470 | ||
1d4b96e0 AC |
6471 | case N_Subprogram_Renaming_Declaration: |
6472 | { | |
6473 | const Node_Id gnat_renaming = Defining_Entity (gnat_node); | |
6474 | const Node_Id gnat_renamed = Renamed_Entity (gnat_renaming); | |
6475 | ||
6476 | gnu_result = alloc_stmt_list (); | |
6477 | ||
6478 | /* Materializing renamed subprograms will only benefit the debugging | |
6479 | information as they aren't referenced in the generated code. So | |
6480 | skip them when they aren't needed. Avoid doing this if: | |
6481 | ||
6482 | - there is a freeze node: in this case the renamed entity is not | |
16f19962 | 6483 | elaborated yet, |
1d4b96e0 AC |
6484 | - the renamed subprogram is intrinsic: it will not be available in |
6485 | the debugging information (note that both or only one of the | |
6486 | renaming and the renamed subprograms can be intrinsic). */ | |
16f19962 | 6487 | if (!type_annotate_only |
1d4b96e0 | 6488 | && Needs_Debug_Info (gnat_renaming) |
16f19962 | 6489 | && No (Freeze_Node (gnat_renaming)) |
1d4b96e0 AC |
6490 | && Present (gnat_renamed) |
6491 | && (Ekind (gnat_renamed) == E_Function | |
6492 | || Ekind (gnat_renamed) == E_Procedure) | |
6493 | && !Is_Intrinsic_Subprogram (gnat_renaming) | |
6494 | && !Is_Intrinsic_Subprogram (gnat_renamed)) | |
afc737f0 | 6495 | gnat_to_gnu_entity (gnat_renaming, gnat_to_gnu (gnat_renamed), true); |
1d4b96e0 AC |
6496 | break; |
6497 | } | |
6498 | ||
a1ab4c31 | 6499 | case N_Implicit_Label_Declaration: |
afc737f0 | 6500 | gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true); |
a1ab4c31 AC |
6501 | gnu_result = alloc_stmt_list (); |
6502 | break; | |
6503 | ||
ac43e11e | 6504 | case N_Package_Renaming_Declaration: |
a1ab4c31 | 6505 | /* These are fully handled in the front end. */ |
ac43e11e AC |
6506 | /* ??? For package renamings, find a way to use GENERIC namespaces so |
6507 | that we get proper debug information for them. */ | |
a1ab4c31 AC |
6508 | gnu_result = alloc_stmt_list (); |
6509 | break; | |
6510 | ||
6511 | /*************************************/ | |
1e17ef87 | 6512 | /* Chapter 4: Names and Expressions */ |
a1ab4c31 AC |
6513 | /*************************************/ |
6514 | ||
6515 | case N_Explicit_Dereference: | |
806fcf71 | 6516 | /* Make sure the designated type is complete before dereferencing. */ |
a1ab4c31 | 6517 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
806fcf71 | 6518 | gnu_result = gnat_to_gnu (Prefix (gnat_node)); |
a1ab4c31 | 6519 | gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result); |
033ba5bf | 6520 | |
f797c2b7 | 6521 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6522 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6523 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6524 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
a1ab4c31 AC |
6525 | break; |
6526 | ||
6527 | case N_Indexed_Component: | |
6528 | { | |
bb24f343 | 6529 | tree gnu_array_object = gnat_to_gnu ((Prefix (gnat_node))); |
a1ab4c31 | 6530 | tree gnu_type; |
bb24f343 | 6531 | int ndim, i; |
a1ab4c31 AC |
6532 | Node_Id *gnat_expr_array; |
6533 | ||
bb24f343 | 6534 | gnu_array_object = maybe_padded_object (gnu_array_object); |
ad00a297 | 6535 | gnu_array_object = maybe_unconstrained_array (gnu_array_object); |
7948ae37 OH |
6536 | |
6537 | /* Convert vector inputs to their representative array type, to fit | |
6538 | what the code below expects. */ | |
f71d5704 EB |
6539 | if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object))) |
6540 | { | |
6541 | if (present_in_lhs_or_actual_p (gnat_node)) | |
6542 | gnat_mark_addressable (gnu_array_object); | |
6543 | gnu_array_object = maybe_vector_array (gnu_array_object); | |
6544 | } | |
7948ae37 | 6545 | |
54441a34 EB |
6546 | /* The failure of this assertion will very likely come from a missing |
6547 | expansion for a packed array access. */ | |
6548 | gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE); | |
6549 | ||
a1ab4c31 AC |
6550 | /* First compute the number of dimensions of the array, then |
6551 | fill the expression array, the order depending on whether | |
6552 | this is a Convention_Fortran array or not. */ | |
6553 | for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object); | |
6554 | TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE | |
6555 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)); | |
6556 | ndim++, gnu_type = TREE_TYPE (gnu_type)) | |
6557 | ; | |
6558 | ||
2bb1fc26 | 6559 | gnat_expr_array = XALLOCAVEC (Node_Id, ndim); |
a1ab4c31 AC |
6560 | |
6561 | if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object))) | |
6562 | for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node)); | |
6563 | i >= 0; | |
6564 | i--, gnat_temp = Next (gnat_temp)) | |
6565 | gnat_expr_array[i] = gnat_temp; | |
6566 | else | |
6567 | for (i = 0, gnat_temp = First (Expressions (gnat_node)); | |
6568 | i < ndim; | |
6569 | i++, gnat_temp = Next (gnat_temp)) | |
6570 | gnat_expr_array[i] = gnat_temp; | |
6571 | ||
825da0d2 EB |
6572 | /* Start with the prefix and build the successive references. */ |
6573 | gnu_result = gnu_array_object; | |
6574 | ||
a1ab4c31 | 6575 | for (i = 0, gnu_type = TREE_TYPE (gnu_array_object); |
87ab2b04 EB |
6576 | i < ndim; |
6577 | i++, gnu_type = TREE_TYPE (gnu_type)) | |
a1ab4c31 AC |
6578 | { |
6579 | gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE); | |
6580 | gnat_temp = gnat_expr_array[i]; | |
825da0d2 | 6581 | gnu_expr = maybe_character_value (gnat_to_gnu (gnat_temp)); |
a1ab4c31 | 6582 | |
384e3fb1 JM |
6583 | gnu_result |
6584 | = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr); | |
a1ab4c31 | 6585 | } |
a1ab4c31 | 6586 | |
033ba5bf EB |
6587 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6588 | ||
f797c2b7 | 6589 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6590 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6591 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6592 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
033ba5bf | 6593 | } |
a1ab4c31 AC |
6594 | break; |
6595 | ||
6596 | case N_Slice: | |
6597 | { | |
bb24f343 | 6598 | tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node)); |
a1ab4c31 | 6599 | |
a1ab4c31 AC |
6600 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6601 | ||
bb24f343 | 6602 | gnu_array_object = maybe_padded_object (gnu_array_object); |
825da0d2 EB |
6603 | gnu_array_object = maybe_unconstrained_array (gnu_array_object); |
6604 | ||
6605 | gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type)); | |
6606 | gnu_expr = maybe_character_value (gnu_expr); | |
a1ab4c31 | 6607 | |
f76d6e6f EB |
6608 | /* If this is a slice with non-constant size of an array with constant |
6609 | size, set the maximum size for the allocation of temporaries. */ | |
6610 | if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type)) | |
825da0d2 EB |
6611 | && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object)))) |
6612 | TYPE_ARRAY_MAX_SIZE (gnu_result_type) | |
6613 | = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object)); | |
f76d6e6f | 6614 | |
a1ab4c31 | 6615 | gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type, |
825da0d2 | 6616 | gnu_array_object, gnu_expr); |
a1ab4c31 AC |
6617 | } |
6618 | break; | |
6619 | ||
6620 | case N_Selected_Component: | |
6621 | { | |
bb24f343 | 6622 | const Entity_Id gnat_prefix = Prefix (gnat_node); |
a1ab4c31 | 6623 | Entity_Id gnat_field = Entity (Selector_Name (gnat_node)); |
1eb58520 | 6624 | tree gnu_prefix = gnat_to_gnu (gnat_prefix); |
a1ab4c31 | 6625 | |
bb24f343 | 6626 | gnu_prefix = maybe_padded_object (gnu_prefix); |
a1ab4c31 | 6627 | |
4ec7c4ec EB |
6628 | /* gnat_to_gnu_entity does not save the GNU tree made for renamed |
6629 | discriminants so avoid making recursive calls on each reference | |
6630 | to them by following the appropriate link directly here. */ | |
6631 | if (Ekind (gnat_field) == E_Discriminant) | |
6632 | { | |
6633 | /* For discriminant references in tagged types always substitute | |
6634 | the corresponding discriminant as the actual component. */ | |
6635 | if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix)))) | |
6636 | while (Present (Corresponding_Discriminant (gnat_field))) | |
6637 | gnat_field = Corresponding_Discriminant (gnat_field); | |
6638 | ||
6639 | /* For discriminant references in untagged types always substitute | |
6640 | the corresponding stored discriminant. */ | |
6641 | else if (Present (Corresponding_Discriminant (gnat_field))) | |
6642 | gnat_field = Original_Record_Component (gnat_field); | |
6643 | } | |
a1ab4c31 AC |
6644 | |
6645 | /* Handle extracting the real or imaginary part of a complex. | |
6646 | The real part is the first field and the imaginary the last. */ | |
a1ab4c31 AC |
6647 | if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE) |
6648 | gnu_result = build_unary_op (Present (Next_Entity (gnat_field)) | |
6649 | ? REALPART_EXPR : IMAGPART_EXPR, | |
6650 | NULL_TREE, gnu_prefix); | |
6651 | else | |
6652 | { | |
d3271136 | 6653 | tree gnu_field = gnat_to_gnu_field_decl (gnat_field); |
852dd866 EB |
6654 | tree gnu_offset; |
6655 | struct loop_info_d *loop; | |
d3271136 | 6656 | |
a1ab4c31 | 6657 | gnu_result |
64235766 | 6658 | = build_component_ref (gnu_prefix, gnu_field, |
a1ab4c31 | 6659 | (Nkind (Parent (gnat_node)) |
3cd64bab EB |
6660 | == N_Attribute_Reference) |
6661 | && lvalue_required_for_attribute_p | |
6662 | (Parent (gnat_node))); | |
852dd866 EB |
6663 | |
6664 | /* If optimization is enabled and we are inside a loop, we try to | |
6665 | hoist nonconstant but invariant offset computations outside of | |
6666 | the loop, since they very likely contain loads that could turn | |
6667 | out to be hard to move if they end up in active EH regions. */ | |
6668 | if (optimize | |
6669 | && inside_loop_p () | |
6670 | && TREE_CODE (gnu_result) == COMPONENT_REF | |
6671 | && (gnu_offset = component_ref_field_offset (gnu_result)) | |
6672 | && !TREE_CONSTANT (gnu_offset) | |
6673 | && (gnu_offset = gnat_invariant_expr (gnu_offset)) | |
6674 | && (loop = find_loop ())) | |
6675 | { | |
6676 | tree invariant | |
6677 | = build1 (SAVE_EXPR, TREE_TYPE (gnu_offset), gnu_offset); | |
6678 | vec_safe_push (loop->invariants, invariant); | |
6679 | tree field = TREE_OPERAND (gnu_result, 1); | |
6680 | tree factor | |
6681 | = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT); | |
6682 | /* Divide the offset by its alignment. */ | |
6683 | TREE_OPERAND (gnu_result, 2) | |
6684 | = size_binop (EXACT_DIV_EXPR, invariant, factor); | |
6685 | } | |
a1ab4c31 AC |
6686 | } |
6687 | ||
a1ab4c31 | 6688 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
033ba5bf | 6689 | |
f797c2b7 | 6690 | /* If atomic access is required on the RHS, build the atomic load. */ |
17a98a3d | 6691 | if (simple_atomic_access_required_p (gnat_node, &aa_sync) |
033ba5bf | 6692 | && !present_in_lhs_or_actual_p (gnat_node)) |
17a98a3d | 6693 | gnu_result = build_atomic_load (gnu_result, aa_sync); |
a1ab4c31 AC |
6694 | } |
6695 | break; | |
6696 | ||
6697 | case N_Attribute_Reference: | |
6698 | { | |
86060344 EB |
6699 | /* The attribute designator. */ |
6700 | const int attr = Get_Attribute_Id (Attribute_Name (gnat_node)); | |
6701 | ||
6702 | /* The Elab_Spec and Elab_Body attributes are special in that Prefix | |
6703 | is a unit, not an object with a GCC equivalent. */ | |
6704 | if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body) | |
6705 | return | |
6706 | create_subprog_decl (create_concat_name | |
6707 | (Entity (Prefix (gnat_node)), | |
6708 | attr == Attr_Elab_Body ? "elabb" : "elabs"), | |
13a6dfe3 | 6709 | NULL_TREE, void_ftype, NULL_TREE, is_default, |
ff9baa5f PMR |
6710 | true, true, true, true, false, NULL, |
6711 | gnat_node); | |
86060344 EB |
6712 | |
6713 | gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr); | |
a1ab4c31 AC |
6714 | } |
6715 | break; | |
6716 | ||
6717 | case N_Reference: | |
6718 | /* Like 'Access as far as we are concerned. */ | |
6719 | gnu_result = gnat_to_gnu (Prefix (gnat_node)); | |
6720 | gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result); | |
6721 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6722 | break; | |
6723 | ||
6724 | case N_Aggregate: | |
6725 | case N_Extension_Aggregate: | |
6726 | { | |
6727 | tree gnu_aggr_type; | |
6728 | ||
14cc7b26 | 6729 | /* Check that this aggregate has not slipped through the cracks. */ |
a1ab4c31 AC |
6730 | gcc_assert (!Expansion_Delayed (gnat_node)); |
6731 | ||
14cc7b26 | 6732 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
a1ab4c31 AC |
6733 | |
6734 | if (TREE_CODE (gnu_result_type) == RECORD_TYPE | |
6735 | && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type)) | |
6736 | gnu_aggr_type | |
7d76717d | 6737 | = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type))); |
7948ae37 OH |
6738 | else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE) |
6739 | gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type); | |
14cc7b26 EB |
6740 | else |
6741 | gnu_aggr_type = gnu_result_type; | |
a1ab4c31 AC |
6742 | |
6743 | if (Null_Record_Present (gnat_node)) | |
b1b2b511 | 6744 | gnu_result = gnat_build_constructor (gnu_aggr_type, NULL); |
a1ab4c31 AC |
6745 | |
6746 | else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE | |
6747 | || TREE_CODE (gnu_aggr_type) == UNION_TYPE) | |
6748 | gnu_result | |
6749 | = assoc_to_constructor (Etype (gnat_node), | |
6750 | First (Component_Associations (gnat_node)), | |
6751 | gnu_aggr_type); | |
6752 | else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE) | |
6753 | gnu_result = pos_to_constructor (First (Expressions (gnat_node)), | |
815b5368 | 6754 | gnu_aggr_type); |
a1ab4c31 AC |
6755 | else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE) |
6756 | gnu_result | |
6757 | = build_binary_op | |
6758 | (COMPLEX_EXPR, gnu_aggr_type, | |
6759 | gnat_to_gnu (Expression (First | |
6760 | (Component_Associations (gnat_node)))), | |
6761 | gnat_to_gnu (Expression | |
6762 | (Next | |
6763 | (First (Component_Associations (gnat_node)))))); | |
6764 | else | |
6765 | gcc_unreachable (); | |
6766 | ||
6767 | gnu_result = convert (gnu_result_type, gnu_result); | |
6768 | } | |
6769 | break; | |
6770 | ||
6771 | case N_Null: | |
6772 | if (TARGET_VTABLE_USES_DESCRIPTORS | |
6773 | && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type | |
6774 | && Is_Dispatch_Table_Entity (Etype (gnat_node))) | |
6775 | gnu_result = null_fdesc_node; | |
6776 | else | |
6777 | gnu_result = null_pointer_node; | |
6778 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6779 | break; | |
6780 | ||
6781 | case N_Type_Conversion: | |
6782 | case N_Qualified_Expression: | |
825da0d2 | 6783 | gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node))); |
a1ab4c31 AC |
6784 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6785 | ||
0029bafd EB |
6786 | /* If this is a qualified expression for a tagged type, we mark the type |
6787 | as used. Because of polymorphism, this might be the only reference to | |
6788 | the tagged type in the program while objects have it as dynamic type. | |
6789 | The debugger needs to see it to display these objects properly. */ | |
6790 | if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node))) | |
6791 | used_types_insert (gnu_result_type); | |
6792 | ||
815b5368 EB |
6793 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_node))); |
6794 | ||
a1ab4c31 | 6795 | gnu_result |
825da0d2 | 6796 | = convert_with_check (Etype (gnat_node), gnu_expr, |
a1ab4c31 | 6797 | Do_Overflow_Check (gnat_node), |
3f13dd77 | 6798 | kind == N_Type_Conversion |
10069d53 | 6799 | && Float_Truncate (gnat_node), gnat_node); |
a1ab4c31 AC |
6800 | break; |
6801 | ||
6802 | case N_Unchecked_Type_Conversion: | |
4ec7c4ec | 6803 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
825da0d2 | 6804 | gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node))); |
c2efda0d EB |
6805 | |
6806 | /* Skip further processing if the conversion is deemed a no-op. */ | |
4f8a6678 | 6807 | if (unchecked_conversion_nop (gnat_node)) |
c2efda0d | 6808 | { |
825da0d2 | 6809 | gnu_result = gnu_expr; |
c2efda0d EB |
6810 | gnu_result_type = TREE_TYPE (gnu_result); |
6811 | break; | |
6812 | } | |
6813 | ||
a1ab4c31 AC |
6814 | /* If the result is a pointer type, see if we are improperly |
6815 | converting to a stricter alignment. */ | |
6816 | if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type) | |
7ed9919d | 6817 | && Is_Access_Type (Etype (gnat_node))) |
a1ab4c31 | 6818 | { |
825da0d2 | 6819 | unsigned int align = known_alignment (gnu_expr); |
a1ab4c31 AC |
6820 | tree gnu_obj_type = TREE_TYPE (gnu_result_type); |
6821 | unsigned int oalign = TYPE_ALIGN (gnu_obj_type); | |
6822 | ||
6823 | if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type)) | |
6824 | post_error_ne_tree_2 | |
4a29b8d6 | 6825 | ("??source alignment (^) '< alignment of & (^)", |
a1ab4c31 AC |
6826 | gnat_node, Designated_Type (Etype (gnat_node)), |
6827 | size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT); | |
6828 | } | |
6829 | ||
6830 | /* If we are converting a descriptor to a function pointer, first | |
6831 | build the pointer. */ | |
6832 | if (TARGET_VTABLE_USES_DESCRIPTORS | |
825da0d2 | 6833 | && TREE_TYPE (gnu_expr) == fdesc_type_node |
a1ab4c31 | 6834 | && POINTER_TYPE_P (gnu_result_type)) |
825da0d2 | 6835 | gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr); |
a1ab4c31 | 6836 | |
825da0d2 | 6837 | gnu_result = unchecked_convert (gnu_result_type, gnu_expr, |
a1ab4c31 AC |
6838 | No_Truncation (gnat_node)); |
6839 | break; | |
6840 | ||
6841 | case N_In: | |
6842 | case N_Not_In: | |
6843 | { | |
da49a783 | 6844 | tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node)); |
da49a783 | 6845 | tree gnu_low, gnu_high; |
a1ab4c31 | 6846 | |
3eaec285 | 6847 | Range_to_gnu (Right_Opnd (gnat_node), &gnu_low, &gnu_high); |
a1ab4c31 AC |
6848 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
6849 | ||
825da0d2 EB |
6850 | tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_obj)); |
6851 | if (TREE_TYPE (gnu_obj) != gnu_op_type) | |
6852 | { | |
6853 | gnu_obj = convert (gnu_op_type, gnu_obj); | |
6854 | gnu_low = convert (gnu_op_type, gnu_low); | |
6855 | gnu_high = convert (gnu_op_type, gnu_high); | |
6856 | } | |
6857 | ||
da49a783 EB |
6858 | /* If LOW and HIGH are identical, perform an equality test. Otherwise, |
6859 | ensure that GNU_OBJ is evaluated only once and perform a full range | |
6860 | test. */ | |
a1ab4c31 | 6861 | if (operand_equal_p (gnu_low, gnu_high, 0)) |
da49a783 EB |
6862 | gnu_result |
6863 | = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low); | |
a1ab4c31 AC |
6864 | else |
6865 | { | |
da49a783 | 6866 | tree t1, t2; |
7d7a1fe8 | 6867 | gnu_obj = gnat_protect_expr (gnu_obj); |
da49a783 EB |
6868 | t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low); |
6869 | if (EXPR_P (t1)) | |
6870 | set_expr_location_from_node (t1, gnat_node); | |
6871 | t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high); | |
6872 | if (EXPR_P (t2)) | |
6873 | set_expr_location_from_node (t2, gnat_node); | |
a1ab4c31 | 6874 | gnu_result |
da49a783 | 6875 | = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2); |
a1ab4c31 AC |
6876 | } |
6877 | ||
3f13dd77 | 6878 | if (kind == N_Not_In) |
658a41ac EB |
6879 | gnu_result |
6880 | = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result); | |
a1ab4c31 AC |
6881 | } |
6882 | break; | |
6883 | ||
6884 | case N_Op_Divide: | |
6885 | gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node)); | |
6886 | gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node)); | |
6887 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6888 | gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type) | |
6889 | ? RDIV_EXPR | |
6890 | : (Rounded_Result (gnat_node) | |
6891 | ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR), | |
6892 | gnu_result_type, gnu_lhs, gnu_rhs); | |
a80e0583 EB |
6893 | /* If the result type is larger than a word, then declare the dependence |
6894 | on the libgcc routine. */ | |
6895 | if (INTEGRAL_TYPE_P (gnu_result_type) | |
6896 | && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
6897 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
a1ab4c31 AC |
6898 | break; |
6899 | ||
9a1bdc31 EB |
6900 | case N_Op_Eq: |
6901 | case N_Op_Ne: | |
6902 | case N_Op_Lt: | |
6903 | case N_Op_Le: | |
6904 | case N_Op_Gt: | |
6905 | case N_Op_Ge: | |
6906 | case N_Op_Add: | |
6907 | case N_Op_Subtract: | |
6908 | case N_Op_Multiply: | |
6909 | case N_Op_Mod: | |
6910 | case N_Op_Rem: | |
a1ab4c31 AC |
6911 | case N_Op_Rotate_Left: |
6912 | case N_Op_Rotate_Right: | |
6913 | case N_Op_Shift_Left: | |
6914 | case N_Op_Shift_Right: | |
6915 | case N_Op_Shift_Right_Arithmetic: | |
22903f22 EB |
6916 | case N_Op_And: |
6917 | case N_Op_Or: | |
6918 | case N_Op_Xor: | |
9a1bdc31 EB |
6919 | case N_And_Then: |
6920 | case N_Or_Else: | |
a1ab4c31 | 6921 | { |
3f13dd77 | 6922 | enum tree_code code = gnu_codes[kind]; |
a1ab4c31 | 6923 | bool ignore_lhs_overflow = false; |
1fc24649 | 6924 | location_t saved_location = input_location; |
73cd514a | 6925 | tree gnu_type, gnu_max_shift = NULL_TREE; |
a1ab4c31 | 6926 | |
22903f22 EB |
6927 | /* Fix operations set up for boolean types in GNU_CODES above. */ |
6928 | if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node)))) | |
6929 | switch (kind) | |
6930 | { | |
6931 | case N_Op_And: | |
6932 | code = BIT_AND_EXPR; | |
6933 | break; | |
6934 | case N_Op_Or: | |
6935 | code = BIT_IOR_EXPR; | |
6936 | break; | |
6937 | case N_Op_Xor: | |
6938 | code = BIT_XOR_EXPR; | |
6939 | break; | |
6940 | default: | |
6941 | break; | |
6942 | } | |
6943 | ||
a1ab4c31 AC |
6944 | gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node)); |
6945 | gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node)); | |
6946 | gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
6947 | ||
73cd514a EB |
6948 | /* If this is a shift, take the count as unsigned since that is what |
6949 | most machines do and will generate simpler adjustments below. */ | |
6950 | if (IN (kind, N_Op_Shift)) | |
6951 | { | |
6952 | tree gnu_count_type | |
6953 | = gnat_unsigned_type_for (get_base_type (TREE_TYPE (gnu_rhs))); | |
6954 | gnu_rhs = convert (gnu_count_type, gnu_rhs); | |
6955 | gnu_max_shift | |
6956 | = convert (TREE_TYPE (gnu_rhs), TYPE_SIZE (gnu_type)); | |
a80e0583 EB |
6957 | /* If the result type is larger than a word, then declare the dependence |
6958 | on the libgcc routine. */ | |
6959 | if (TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
6960 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
73cd514a EB |
6961 | } |
6962 | ||
1f3f64b9 EB |
6963 | /* If this is a comparison between (potentially) large aggregates, then |
6964 | declare the dependence on the memcmp routine. */ | |
6965 | else if ((kind == N_Op_Eq || kind == N_Op_Ne) | |
6966 | && AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs)) | |
6967 | && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs))) | |
6968 | || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)), | |
6969 | 2 * BITS_PER_WORD) > 0)) | |
6970 | Check_Restriction_No_Dependence_On_System (Name_Memory_Compare, | |
6971 | gnat_node); | |
6972 | ||
a80e0583 EB |
6973 | /* If this is a modulo/remainder and the result type is larger than a |
6974 | word, then declare the dependence on the libgcc routine. */ | |
6975 | else if ((kind == N_Op_Mod ||kind == N_Op_Rem) | |
6976 | && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD) | |
6977 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
6978 | ||
7948ae37 | 6979 | /* Pending generic support for efficient vector logical operations in |
1f3f64b9 | 6980 | GCC, convert vectors to their representative array type view. */ |
7948ae37 OH |
6981 | gnu_lhs = maybe_vector_array (gnu_lhs); |
6982 | gnu_rhs = maybe_vector_array (gnu_rhs); | |
6983 | ||
43a4dd82 EB |
6984 | /* If this is a comparison operator, convert any references to an |
6985 | unconstrained array value into a reference to the actual array. */ | |
a1ab4c31 AC |
6986 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
6987 | { | |
6988 | gnu_lhs = maybe_unconstrained_array (gnu_lhs); | |
6989 | gnu_rhs = maybe_unconstrained_array (gnu_rhs); | |
825da0d2 EB |
6990 | |
6991 | tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_lhs)); | |
6992 | if (TREE_TYPE (gnu_lhs) != gnu_op_type) | |
6993 | { | |
6994 | gnu_lhs = convert (gnu_op_type, gnu_lhs); | |
6995 | gnu_rhs = convert (gnu_op_type, gnu_rhs); | |
6996 | } | |
a1ab4c31 AC |
6997 | } |
6998 | ||
a1ab4c31 AC |
6999 | /* If this is a shift whose count is not guaranteed to be correct, |
7000 | we need to adjust the shift count. */ | |
73cd514a EB |
7001 | if ((kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right) |
7002 | && !Shift_Count_OK (gnat_node)) | |
7003 | gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, TREE_TYPE (gnu_rhs), | |
7004 | gnu_rhs, gnu_max_shift); | |
7005 | else if (kind == N_Op_Shift_Right_Arithmetic | |
7006 | && !Shift_Count_OK (gnat_node)) | |
7007 | gnu_rhs | |
7008 | = build_binary_op (MIN_EXPR, TREE_TYPE (gnu_rhs), | |
7009 | build_binary_op (MINUS_EXPR, | |
7010 | TREE_TYPE (gnu_rhs), | |
7011 | gnu_max_shift, | |
7012 | build_int_cst | |
7013 | (TREE_TYPE (gnu_rhs), 1)), | |
7014 | gnu_rhs); | |
a1ab4c31 AC |
7015 | |
7016 | /* For right shifts, the type says what kind of shift to do, | |
7017 | so we may need to choose a different type. In this case, | |
7018 | we have to ignore integer overflow lest it propagates all | |
7019 | the way down and causes a CE to be explicitly raised. */ | |
3f13dd77 | 7020 | if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type)) |
a1ab4c31 | 7021 | { |
9a1bdc31 | 7022 | gnu_type = gnat_unsigned_type_for (gnu_type); |
a1ab4c31 AC |
7023 | ignore_lhs_overflow = true; |
7024 | } | |
3f13dd77 | 7025 | else if (kind == N_Op_Shift_Right_Arithmetic |
a1ab4c31 AC |
7026 | && TYPE_UNSIGNED (gnu_type)) |
7027 | { | |
9a1bdc31 | 7028 | gnu_type = gnat_signed_type_for (gnu_type); |
a1ab4c31 AC |
7029 | ignore_lhs_overflow = true; |
7030 | } | |
7031 | ||
7032 | if (gnu_type != gnu_result_type) | |
7033 | { | |
7034 | tree gnu_old_lhs = gnu_lhs; | |
7035 | gnu_lhs = convert (gnu_type, gnu_lhs); | |
7036 | if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow) | |
7037 | TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs); | |
7038 | gnu_rhs = convert (gnu_type, gnu_rhs); | |
1f8fc1f4 EB |
7039 | if (gnu_max_shift) |
7040 | gnu_max_shift = convert (gnu_type, gnu_max_shift); | |
a1ab4c31 AC |
7041 | } |
7042 | ||
f552aa62 EB |
7043 | /* For signed integer addition, subtraction and multiplication, do an |
7044 | overflow check if required. */ | |
9a1bdc31 | 7045 | if (Do_Overflow_Check (gnat_node) |
9a1bdc31 | 7046 | && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR) |
b666e568 GB |
7047 | && !TYPE_UNSIGNED (gnu_type) |
7048 | && !FLOAT_TYPE_P (gnu_type)) | |
f552aa62 EB |
7049 | gnu_result |
7050 | = build_binary_op_trapv (code, gnu_type, gnu_lhs, gnu_rhs, | |
7051 | gnat_node); | |
b666e568 | 7052 | else |
1fc24649 EB |
7053 | { |
7054 | /* Some operations, e.g. comparisons of arrays, generate complex | |
7055 | trees that need to be annotated while they are being built. */ | |
7056 | input_location = saved_location; | |
7057 | gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs); | |
7058 | } | |
a1ab4c31 AC |
7059 | |
7060 | /* If this is a logical shift with the shift count not verified, | |
7061 | we must return zero if it is too large. We cannot compensate | |
73cd514a | 7062 | beforehand in this case. */ |
3f13dd77 | 7063 | if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right) |
a1ab4c31 AC |
7064 | && !Shift_Count_OK (gnat_node)) |
7065 | gnu_result | |
73cd514a EB |
7066 | = build_cond_expr (gnu_type, |
7067 | build_binary_op (GE_EXPR, boolean_type_node, | |
7068 | gnu_rhs, gnu_max_shift), | |
7069 | build_int_cst (gnu_type, 0), | |
7070 | gnu_result); | |
a1ab4c31 AC |
7071 | } |
7072 | break; | |
7073 | ||
9b16cb57 | 7074 | case N_If_Expression: |
a1ab4c31 | 7075 | { |
1e17ef87 EB |
7076 | tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node))); |
7077 | tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node)))); | |
7078 | tree gnu_false | |
7079 | = gnat_to_gnu (Next (Next (First (Expressions (gnat_node))))); | |
a1ab4c31 AC |
7080 | |
7081 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
3f13dd77 EB |
7082 | gnu_result |
7083 | = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false); | |
a1ab4c31 AC |
7084 | } |
7085 | break; | |
7086 | ||
7087 | case N_Op_Plus: | |
7088 | gnu_result = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7089 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7090 | break; | |
7091 | ||
7092 | case N_Op_Not: | |
7093 | /* This case can apply to a boolean or a modular type. | |
7094 | Fall through for a boolean operand since GNU_CODES is set | |
7095 | up to handle this. */ | |
43a4dd82 | 7096 | if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node)))) |
a1ab4c31 AC |
7097 | { |
7098 | gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node)); | |
7099 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7100 | gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type, | |
7101 | gnu_expr); | |
7102 | break; | |
7103 | } | |
7104 | ||
9c453de7 | 7105 | /* ... fall through ... */ |
a1ab4c31 | 7106 | |
9a1bdc31 EB |
7107 | case N_Op_Minus: |
7108 | case N_Op_Abs: | |
a1ab4c31 | 7109 | gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node)); |
fd6e497e | 7110 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
a1ab4c31 | 7111 | |
f552aa62 EB |
7112 | /* For signed integer negation and absolute value, do an overflow check |
7113 | if required. */ | |
b666e568 GB |
7114 | if (Do_Overflow_Check (gnat_node) |
7115 | && !TYPE_UNSIGNED (gnu_result_type) | |
7116 | && !FLOAT_TYPE_P (gnu_result_type)) | |
10069d53 | 7117 | gnu_result |
f552aa62 EB |
7118 | = build_unary_op_trapv (gnu_codes[kind], gnu_result_type, gnu_expr, |
7119 | gnat_node); | |
b666e568 | 7120 | else |
f552aa62 EB |
7121 | gnu_result |
7122 | = build_unary_op (gnu_codes[kind], gnu_result_type, gnu_expr); | |
a1ab4c31 AC |
7123 | break; |
7124 | ||
7125 | case N_Allocator: | |
7126 | { | |
d63fbcf8 EB |
7127 | tree gnu_type, gnu_init; |
7128 | bool ignore_init_type; | |
a1ab4c31 AC |
7129 | |
7130 | gnat_temp = Expression (gnat_node); | |
7131 | ||
815b5368 EB |
7132 | /* The expression can be either an N_Identifier or an Expanded_Name, |
7133 | which must represent a type, or a N_Qualified_Expression, which | |
7134 | contains both the type and an initial value for the object. */ | |
a1ab4c31 AC |
7135 | if (Nkind (gnat_temp) == N_Identifier |
7136 | || Nkind (gnat_temp) == N_Expanded_Name) | |
d63fbcf8 EB |
7137 | { |
7138 | ignore_init_type = false; | |
7139 | gnu_init = NULL_TREE; | |
7140 | gnu_type = gnat_to_gnu_type (Entity (gnat_temp)); | |
7141 | } | |
7142 | ||
a1ab4c31 AC |
7143 | else if (Nkind (gnat_temp) == N_Qualified_Expression) |
7144 | { | |
bb24f343 | 7145 | const Entity_Id gnat_desig_type |
a1ab4c31 AC |
7146 | = Designated_Type (Underlying_Type (Etype (gnat_node))); |
7147 | ||
ce2d0ce2 | 7148 | ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type); |
a1ab4c31 | 7149 | |
d63fbcf8 | 7150 | gnu_init = gnat_to_gnu (Expression (gnat_temp)); |
a1ab4c31 | 7151 | gnu_init = maybe_unconstrained_array (gnu_init); |
815b5368 EB |
7152 | |
7153 | gigi_checking_assert (!Do_Range_Check (Expression (gnat_temp))); | |
a1ab4c31 AC |
7154 | |
7155 | if (Is_Elementary_Type (gnat_desig_type) | |
7156 | || Is_Constrained (gnat_desig_type)) | |
0029bafd | 7157 | gnu_type = gnat_to_gnu_type (gnat_desig_type); |
a1ab4c31 AC |
7158 | else |
7159 | { | |
7160 | gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp))); | |
7161 | if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE) | |
7162 | gnu_type = TREE_TYPE (gnu_init); | |
a1ab4c31 | 7163 | } |
0029bafd EB |
7164 | |
7165 | /* See the N_Qualified_Expression case for the rationale. */ | |
7166 | if (Is_Tagged_Type (gnat_desig_type)) | |
7167 | used_types_insert (gnu_type); | |
7168 | ||
7169 | gnu_init = convert (gnu_type, gnu_init); | |
a1ab4c31 AC |
7170 | } |
7171 | else | |
7172 | gcc_unreachable (); | |
7173 | ||
7174 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); | |
7175 | return build_allocator (gnu_type, gnu_init, gnu_result_type, | |
7176 | Procedure_To_Call (gnat_node), | |
7177 | Storage_Pool (gnat_node), gnat_node, | |
7178 | ignore_init_type); | |
7179 | } | |
7180 | break; | |
7181 | ||
1e17ef87 EB |
7182 | /**************************/ |
7183 | /* Chapter 5: Statements */ | |
7184 | /**************************/ | |
a1ab4c31 AC |
7185 | |
7186 | case N_Label: | |
7187 | gnu_result = build1 (LABEL_EXPR, void_type_node, | |
7188 | gnat_to_gnu (Identifier (gnat_node))); | |
7189 | break; | |
7190 | ||
7191 | case N_Null_Statement: | |
9c69c3af EB |
7192 | /* When not optimizing, turn null statements from source into gotos to |
7193 | the next statement that the middle-end knows how to preserve. */ | |
7194 | if (!optimize && Comes_From_Source (gnat_node)) | |
7195 | { | |
88a94e2b EB |
7196 | tree stmt, label = create_label_decl (NULL_TREE, gnat_node); |
7197 | DECL_IGNORED_P (label) = 1; | |
9c69c3af EB |
7198 | start_stmt_group (); |
7199 | stmt = build1 (GOTO_EXPR, void_type_node, label); | |
7200 | set_expr_location_from_node (stmt, gnat_node); | |
7201 | add_stmt (stmt); | |
7202 | stmt = build1 (LABEL_EXPR, void_type_node, label); | |
7203 | set_expr_location_from_node (stmt, gnat_node); | |
7204 | add_stmt (stmt); | |
7205 | gnu_result = end_stmt_group (); | |
7206 | } | |
7207 | else | |
7208 | gnu_result = alloc_stmt_list (); | |
a1ab4c31 AC |
7209 | break; |
7210 | ||
7211 | case N_Assignment_Statement: | |
0c5730a6 | 7212 | /* First get the LHS of the statement and convert any reference to an |
0b3467c4 | 7213 | unconstrained array into a reference to the underlying array. */ |
a1ab4c31 AC |
7214 | gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node))); |
7215 | ||
7216 | /* If the type has a size that overflows, convert this into raise of | |
7217 | Storage_Error: execution shouldn't have gotten here anyway. */ | |
7218 | if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST | |
ce3da0d0 | 7219 | && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs)))) |
a1ab4c31 AC |
7220 | gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node, |
7221 | N_Raise_Storage_Error); | |
0c5730a6 EB |
7222 | |
7223 | /* If the RHS is a function call, let Call_to_gnu do the entire work. */ | |
0b3467c4 | 7224 | else if (Nkind (Expression (gnat_node)) == N_Function_Call) |
f797c2b7 | 7225 | { |
17a98a3d | 7226 | get_atomic_access (Name (gnat_node), &aa_type, &aa_sync); |
f797c2b7 EB |
7227 | gnu_result |
7228 | = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs, | |
17a98a3d | 7229 | aa_type, aa_sync); |
f797c2b7 | 7230 | } |
0c5730a6 EB |
7231 | |
7232 | /* Otherwise we need to build the assignment statement manually. */ | |
a1ab4c31 AC |
7233 | else |
7234 | { | |
ac43e11e | 7235 | const Node_Id gnat_expr = Expression (gnat_node); |
b9364a56 EB |
7236 | const Node_Id gnat_inner |
7237 | = Nkind (gnat_expr) == N_Qualified_Expression | |
7238 | ? Expression (gnat_expr) | |
7239 | : gnat_expr; | |
ac43e11e AC |
7240 | const Entity_Id gnat_type |
7241 | = Underlying_Type (Etype (Name (gnat_node))); | |
ac43e11e | 7242 | const bool use_memset_p |
95c9c1c0 | 7243 | = Is_Array_Type (gnat_type) |
b9364a56 | 7244 | && Nkind (gnat_inner) == N_Aggregate |
27c3d986 | 7245 | && Is_Single_Aggregate (gnat_inner); |
ac43e11e | 7246 | |
b9364a56 | 7247 | /* If we use memset, we need to find the innermost expression. */ |
ac43e11e AC |
7248 | if (use_memset_p) |
7249 | { | |
b9364a56 EB |
7250 | gnat_temp = gnat_inner; |
7251 | do { | |
7252 | gnat_temp | |
7253 | = Expression (First (Component_Associations (gnat_temp))); | |
7254 | } while (Nkind (gnat_temp) == N_Aggregate | |
27c3d986 | 7255 | && Is_Single_Aggregate (gnat_temp)); |
b9364a56 | 7256 | gnu_rhs = gnat_to_gnu (gnat_temp); |
ac43e11e | 7257 | } |
0c5730a6 EB |
7258 | |
7259 | /* Otherwise get the RHS of the statement and do the same processing | |
7260 | as for the LHS above. */ | |
ac43e11e AC |
7261 | else |
7262 | gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr)); | |
a1ab4c31 | 7263 | |
815b5368 | 7264 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 | 7265 | |
17a98a3d EB |
7266 | get_atomic_access (Name (gnat_node), &aa_type, &aa_sync); |
7267 | ||
f797c2b7 EB |
7268 | /* If an outer atomic access is required on the LHS, build the load- |
7269 | modify-store sequence. */ | |
17a98a3d | 7270 | if (aa_type == OUTER_ATOMIC) |
f797c2b7 EB |
7271 | gnu_result = build_load_modify_store (gnu_lhs, gnu_rhs, gnat_node); |
7272 | ||
17a98a3d EB |
7273 | /* Or else, if a simple atomic access is required, build the atomic |
7274 | store. */ | |
7275 | else if (aa_type == SIMPLE_ATOMIC) | |
7276 | gnu_result = build_atomic_store (gnu_lhs, gnu_rhs, aa_sync); | |
ac43e11e | 7277 | |
df8aa2b7 EB |
7278 | /* Or else, use memset when the conditions are met. This has already |
7279 | been validated by Aggr_Assignment_OK_For_Backend in the front-end | |
7280 | and the RHS is thus guaranteed to be of the appropriate form. */ | |
ac43e11e AC |
7281 | else if (use_memset_p) |
7282 | { | |
333e4f86 AC |
7283 | tree value |
7284 | = real_zerop (gnu_rhs) | |
7285 | ? integer_zero_node | |
7286 | : fold_convert (integer_type_node, gnu_rhs); | |
e54b6268 | 7287 | tree dest = build_fold_addr_expr (gnu_lhs); |
881cdd61 | 7288 | tree t = builtin_decl_explicit (BUILT_IN_MEMSET); |
e54b6268 EB |
7289 | /* Be extra careful not to write too much data. */ |
7290 | tree size; | |
7291 | if (TREE_CODE (gnu_lhs) == COMPONENT_REF) | |
7292 | size = DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs, 1)); | |
7293 | else if (DECL_P (gnu_lhs)) | |
7294 | size = DECL_SIZE_UNIT (gnu_lhs); | |
7295 | else | |
7296 | size = TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs)); | |
7297 | size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_lhs); | |
333e4f86 | 7298 | if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value)) |
ac43e11e AC |
7299 | { |
7300 | tree mask | |
7301 | = build_int_cst (integer_type_node, | |
7302 | ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1); | |
7303 | value = int_const_binop (BIT_AND_EXPR, value, mask); | |
7304 | } | |
e54b6268 | 7305 | gnu_result = build_call_expr (t, 3, dest, value, size); |
1f3f64b9 EB |
7306 | Check_Restriction_No_Dependence_On_System (Name_Memory_Set, |
7307 | gnat_node); | |
ac43e11e AC |
7308 | } |
7309 | ||
7310 | /* Otherwise build a regular assignment. */ | |
033ba5bf EB |
7311 | else |
7312 | gnu_result | |
7313 | = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs); | |
8b659f79 | 7314 | |
ac43e11e | 7315 | /* If the assignment type is a regular array and the two sides are |
82d6f532 EB |
7316 | not completely disjoint, play safe and use memmove. But don't do |
7317 | it for a bit-packed array as it might not be byte-aligned. */ | |
8b659f79 | 7318 | if (TREE_CODE (gnu_result) == MODIFY_EXPR |
95c9c1c0 EB |
7319 | && Is_Array_Type (gnat_type) |
7320 | && !Is_Bit_Packed_Array (gnat_type) | |
8b659f79 EB |
7321 | && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node))) |
7322 | { | |
ac43e11e AC |
7323 | tree to = TREE_OPERAND (gnu_result, 0); |
7324 | tree from = TREE_OPERAND (gnu_result, 1); | |
7325 | tree type = TREE_TYPE (from); | |
7326 | tree size | |
7327 | = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from); | |
7328 | tree to_ptr = build_fold_addr_expr (to); | |
7329 | tree from_ptr = build_fold_addr_expr (from); | |
881cdd61 | 7330 | tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE); |
8b659f79 | 7331 | gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size); |
1f3f64b9 EB |
7332 | Check_Restriction_No_Dependence_On_System (Name_Memory_Move, |
7333 | gnat_node); | |
8b659f79 | 7334 | } |
1f3f64b9 EB |
7335 | |
7336 | /* If this is an assignment between (potentially) large aggregates, | |
7337 | then declare the dependence on the memcpy routine. */ | |
7338 | else if (AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs)) | |
7339 | && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs))) | |
7340 | || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)), | |
7341 | 2 * BITS_PER_WORD) > 0)) | |
7342 | Check_Restriction_No_Dependence_On_System (Name_Memory_Copy, | |
7343 | gnat_node); | |
a1ab4c31 AC |
7344 | } |
7345 | break; | |
7346 | ||
7347 | case N_If_Statement: | |
7348 | { | |
1e17ef87 | 7349 | tree *gnu_else_ptr; /* Point to put next "else if" or "else". */ |
a1ab4c31 AC |
7350 | |
7351 | /* Make the outer COND_EXPR. Avoid non-determinism. */ | |
7352 | gnu_result = build3 (COND_EXPR, void_type_node, | |
7353 | gnat_to_gnu (Condition (gnat_node)), | |
7354 | NULL_TREE, NULL_TREE); | |
7355 | COND_EXPR_THEN (gnu_result) | |
7356 | = build_stmt_group (Then_Statements (gnat_node), false); | |
7357 | TREE_SIDE_EFFECTS (gnu_result) = 1; | |
7358 | gnu_else_ptr = &COND_EXPR_ELSE (gnu_result); | |
7359 | ||
7360 | /* Now make a COND_EXPR for each of the "else if" parts. Put each | |
7361 | into the previous "else" part and point to where to put any | |
7362 | outer "else". Also avoid non-determinism. */ | |
7363 | if (Present (Elsif_Parts (gnat_node))) | |
7364 | for (gnat_temp = First (Elsif_Parts (gnat_node)); | |
7365 | Present (gnat_temp); gnat_temp = Next (gnat_temp)) | |
7366 | { | |
7367 | gnu_expr = build3 (COND_EXPR, void_type_node, | |
7368 | gnat_to_gnu (Condition (gnat_temp)), | |
7369 | NULL_TREE, NULL_TREE); | |
7370 | COND_EXPR_THEN (gnu_expr) | |
7371 | = build_stmt_group (Then_Statements (gnat_temp), false); | |
7372 | TREE_SIDE_EFFECTS (gnu_expr) = 1; | |
7373 | set_expr_location_from_node (gnu_expr, gnat_temp); | |
7374 | *gnu_else_ptr = gnu_expr; | |
7375 | gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr); | |
7376 | } | |
7377 | ||
7378 | *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false); | |
7379 | } | |
7380 | break; | |
7381 | ||
7382 | case N_Case_Statement: | |
7383 | gnu_result = Case_Statement_to_gnu (gnat_node); | |
7384 | break; | |
7385 | ||
7386 | case N_Loop_Statement: | |
7387 | gnu_result = Loop_Statement_to_gnu (gnat_node); | |
7388 | break; | |
7389 | ||
7390 | case N_Block_Statement: | |
a712b009 EB |
7391 | /* The only way to enter the block is to fall through to it. */ |
7392 | if (stmt_group_may_fallthru ()) | |
7393 | { | |
7394 | start_stmt_group (); | |
7395 | gnat_pushlevel (); | |
6d16658d | 7396 | process_decls (Declarations (gnat_node), Empty, true, true); |
a712b009 | 7397 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); |
6d16658d BD |
7398 | if (Present (At_End_Proc (gnat_node))) |
7399 | At_End_Proc_to_gnu (gnat_node); | |
a712b009 EB |
7400 | gnat_poplevel (); |
7401 | gnu_result = end_stmt_group (); | |
7402 | } | |
7403 | else | |
7404 | gnu_result = alloc_stmt_list (); | |
a1ab4c31 AC |
7405 | break; |
7406 | ||
7407 | case N_Exit_Statement: | |
7408 | gnu_result | |
7409 | = build2 (EXIT_STMT, void_type_node, | |
7410 | (Present (Condition (gnat_node)) | |
7411 | ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE), | |
7412 | (Present (Name (gnat_node)) | |
7413 | ? get_gnu_tree (Entity (Name (gnat_node))) | |
633a3f2a | 7414 | : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt))); |
a1ab4c31 AC |
7415 | break; |
7416 | ||
7640ef8a | 7417 | case N_Simple_Return_Statement: |
a1ab4c31 | 7418 | { |
f3d34576 | 7419 | tree gnu_ret_obj, gnu_ret_val; |
a1ab4c31 | 7420 | |
d47d0a8d EB |
7421 | /* If the subprogram is a function, we must return the expression. */ |
7422 | if (Present (Expression (gnat_node))) | |
a1ab4c31 | 7423 | { |
d47d0a8d | 7424 | tree gnu_subprog_type = TREE_TYPE (current_function_decl); |
d47d0a8d | 7425 | |
2374257a EB |
7426 | /* If this function has copy-in/copy-out parameters parameters and |
7427 | doesn't return by invisible reference, get the real object for | |
7428 | the return. See Subprogram_Body_to_gnu. */ | |
7429 | if (TYPE_CI_CO_LIST (gnu_subprog_type) | |
7430 | && !TREE_ADDRESSABLE (gnu_subprog_type)) | |
9771b263 | 7431 | gnu_ret_obj = gnu_return_var_stack->last (); |
f3d34576 EB |
7432 | else |
7433 | gnu_ret_obj = DECL_RESULT (current_function_decl); | |
7434 | ||
7435 | /* Get the GCC tree for the expression to be returned. */ | |
7436 | gnu_ret_val = gnat_to_gnu (Expression (gnat_node)); | |
35a382b8 | 7437 | |
d47d0a8d EB |
7438 | /* Do not remove the padding from GNU_RET_VAL if the inner type is |
7439 | self-referential since we want to allocate the fixed size. */ | |
7440 | if (TREE_CODE (gnu_ret_val) == COMPONENT_REF | |
fc7a823e | 7441 | && type_is_padding_self_referential |
d020df06 | 7442 | (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))) |
d47d0a8d EB |
7443 | gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0); |
7444 | ||
f3d34576 | 7445 | /* If the function returns by direct reference, return a pointer |
89e037d0 EB |
7446 | to the return value, possibly after allocating it. */ |
7447 | if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)) | |
a1ab4c31 | 7448 | { |
89e037d0 | 7449 | if (Present (Storage_Pool (gnat_node))) |
088b91c7 | 7450 | { |
89e037d0 EB |
7451 | gnu_ret_val = maybe_unconstrained_array (gnu_ret_val); |
7452 | ||
7453 | /* And find out whether it is a candidate for Named Return | |
fc84bb8c | 7454 | Value. If so, record it. */ |
89e037d0 EB |
7455 | if (optimize |
7456 | && !optimize_debug | |
7457 | && !TYPE_CI_CO_LIST (gnu_subprog_type)) | |
088b91c7 | 7458 | { |
89e037d0 EB |
7459 | tree ret_val = gnu_ret_val; |
7460 | ||
7461 | /* Strip conversions around the return value. */ | |
7462 | if (gnat_useless_type_conversion (ret_val)) | |
7463 | ret_val = TREE_OPERAND (ret_val, 0); | |
7464 | ||
7465 | /* Strip unpadding around the return value. */ | |
7466 | if (TREE_CODE (ret_val) == COMPONENT_REF | |
7467 | && TYPE_IS_PADDING_P | |
7468 | (TREE_TYPE (TREE_OPERAND (ret_val, 0)))) | |
7469 | ret_val = TREE_OPERAND (ret_val, 0); | |
7470 | ||
7471 | /* Now apply the test to the return value. */ | |
7472 | if (return_value_ok_for_nrv_p (NULL_TREE, ret_val)) | |
7473 | { | |
7474 | if (!f_named_ret_val) | |
7475 | f_named_ret_val = BITMAP_GGC_ALLOC (); | |
7476 | bitmap_set_bit (f_named_ret_val, | |
7477 | DECL_UID (ret_val)); | |
7478 | if (!f_gnat_ret) | |
7479 | f_gnat_ret = gnat_node; | |
7480 | } | |
088b91c7 | 7481 | } |
89e037d0 EB |
7482 | |
7483 | gnu_ret_val | |
7484 | = build_allocator (TREE_TYPE (gnu_ret_val), | |
7485 | gnu_ret_val, | |
7486 | TREE_TYPE (gnu_ret_obj), | |
7487 | Procedure_To_Call (gnat_node), | |
7488 | Storage_Pool (gnat_node), | |
7489 | gnat_node, | |
7490 | false); | |
088b91c7 EB |
7491 | } |
7492 | ||
89e037d0 EB |
7493 | else |
7494 | gnu_ret_val | |
7495 | = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val); | |
a1ab4c31 | 7496 | } |
d47d0a8d | 7497 | |
0d24bf76 | 7498 | /* Otherwise, if it returns by invisible reference, dereference |
d47d0a8d EB |
7499 | the pointer it is passed using the type of the return value |
7500 | and build the copy operation manually. This ensures that we | |
7501 | don't copy too much data, for example if the return type is | |
7502 | unconstrained with a maximum size. */ | |
0d24bf76 | 7503 | else if (TREE_ADDRESSABLE (gnu_subprog_type)) |
a1ab4c31 | 7504 | { |
f3d34576 | 7505 | tree gnu_ret_deref |
d47d0a8d | 7506 | = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val), |
f3d34576 | 7507 | gnu_ret_obj); |
b24f7345 | 7508 | gnu_result = build2 (INIT_EXPR, void_type_node, |
2374257a | 7509 | gnu_ret_deref, gnu_ret_val); |
c697f593 EB |
7510 | /* Avoid a useless copy with __builtin_return_slot. */ |
7511 | if (TREE_CODE (gnu_ret_val) == INDIRECT_REF) | |
7512 | gnu_result | |
7513 | = build3 (COND_EXPR, void_type_node, | |
7514 | fold_build2 (NE_EXPR, boolean_type_node, | |
7515 | TREE_OPERAND (gnu_ret_val, 0), | |
7516 | gnu_ret_obj), | |
7517 | gnu_result, NULL_TREE); | |
d47d0a8d EB |
7518 | add_stmt_with_node (gnu_result, gnat_node); |
7519 | gnu_ret_val = NULL_TREE; | |
a1ab4c31 AC |
7520 | } |
7521 | } | |
0d24bf76 | 7522 | |
a1ab4c31 | 7523 | else |
0d24bf76 | 7524 | gnu_ret_obj = gnu_ret_val = NULL_TREE; |
a1ab4c31 | 7525 | |
35a382b8 EB |
7526 | /* If we have a return label defined, convert this into a branch to |
7527 | that label. The return proper will be handled elsewhere. */ | |
9771b263 | 7528 | if (gnu_return_label_stack->last ()) |
35a382b8 | 7529 | { |
2374257a | 7530 | if (gnu_ret_val) |
b1cd7461 EB |
7531 | add_stmt_with_node (build_binary_op (MODIFY_EXPR, |
7532 | NULL_TREE, gnu_ret_obj, | |
7533 | gnu_ret_val), | |
7534 | gnat_node); | |
35a382b8 EB |
7535 | |
7536 | gnu_result = build1 (GOTO_EXPR, void_type_node, | |
9771b263 | 7537 | gnu_return_label_stack->last ()); |
f3d34576 | 7538 | |
35a382b8 EB |
7539 | /* When not optimizing, make sure the return is preserved. */ |
7540 | if (!optimize && Comes_From_Source (gnat_node)) | |
9771b263 | 7541 | DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0; |
35a382b8 EB |
7542 | } |
7543 | ||
f3d34576 EB |
7544 | /* Otherwise, build a regular return. */ |
7545 | else | |
7546 | gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val); | |
a1ab4c31 AC |
7547 | } |
7548 | break; | |
7549 | ||
7550 | case N_Goto_Statement: | |
8f8f531f PMR |
7551 | gnu_expr = gnat_to_gnu (Name (gnat_node)); |
7552 | gnu_result = build1 (GOTO_EXPR, void_type_node, gnu_expr); | |
7553 | TREE_USED (gnu_expr) = 1; | |
a1ab4c31 AC |
7554 | break; |
7555 | ||
1e17ef87 EB |
7556 | /***************************/ |
7557 | /* Chapter 6: Subprograms */ | |
7558 | /***************************/ | |
a1ab4c31 AC |
7559 | |
7560 | case N_Subprogram_Declaration: | |
d5ebeb8c EB |
7561 | /* Unless there is a freeze node, declare the entity. We consider |
7562 | this a definition even though we're not generating code for the | |
7563 | subprogram because we will be making the corresponding GCC node. | |
7564 | When there is a freeze node, it is considered the definition of | |
7565 | the subprogram and we do nothing until after it is encountered. | |
7566 | That's an efficiency issue: the types involved in the profile | |
7567 | are far more likely to be frozen between the declaration and | |
7568 | the freeze node than before the declaration, so we save some | |
7569 | updates of the GCC node by waiting until the freeze node. | |
7570 | The counterpart is that we assume that there is no reference | |
7571 | to the subprogram between the declaration and the freeze node | |
7572 | in the expanded code; otherwise, it will be interpreted as an | |
7573 | external reference and very likely give rise to a link failure. */ | |
a1ab4c31 AC |
7574 | if (No (Freeze_Node (Defining_Entity (Specification (gnat_node))))) |
7575 | gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)), | |
afc737f0 | 7576 | NULL_TREE, true); |
a1ab4c31 AC |
7577 | gnu_result = alloc_stmt_list (); |
7578 | break; | |
7579 | ||
7580 | case N_Abstract_Subprogram_Declaration: | |
7581 | /* This subprogram doesn't exist for code generation purposes, but we | |
7582 | have to elaborate the types of any parameters and result, unless | |
76e3504f | 7583 | they are imported types (nothing to generate in this case). |
a1ab4c31 | 7584 | |
76e3504f AC |
7585 | The parameter list may contain types with freeze nodes, e.g. not null |
7586 | subtypes, so the subprogram itself may carry a freeze node, in which | |
7587 | case its elaboration must be deferred. */ | |
a1ab4c31 | 7588 | |
76e3504f AC |
7589 | /* Process the parameter types first. */ |
7590 | if (No (Freeze_Node (Defining_Entity (Specification (gnat_node))))) | |
a1ab4c31 AC |
7591 | for (gnat_temp |
7592 | = First_Formal_With_Extras | |
7593 | (Defining_Entity (Specification (gnat_node))); | |
7594 | Present (gnat_temp); | |
7595 | gnat_temp = Next_Formal_With_Extras (gnat_temp)) | |
7596 | if (Is_Itype (Etype (gnat_temp)) | |
7b56a91b | 7597 | && !From_Limited_With (Etype (gnat_temp))) |
afc737f0 | 7598 | gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false); |
a1ab4c31 | 7599 | |
a1ab4c31 | 7600 | /* Then the result type, set to Standard_Void_Type for procedures. */ |
a1ab4c31 AC |
7601 | { |
7602 | Entity_Id gnat_temp_type | |
7603 | = Etype (Defining_Entity (Specification (gnat_node))); | |
7604 | ||
7b56a91b | 7605 | if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type)) |
afc737f0 | 7606 | gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, false); |
a1ab4c31 AC |
7607 | } |
7608 | ||
7609 | gnu_result = alloc_stmt_list (); | |
7610 | break; | |
7611 | ||
7612 | case N_Defining_Program_Unit_Name: | |
1e17ef87 EB |
7613 | /* For a child unit identifier go up a level to get the specification. |
7614 | We get this when we try to find the spec of a child unit package | |
7615 | that is the compilation unit being compiled. */ | |
a1ab4c31 AC |
7616 | gnu_result = gnat_to_gnu (Parent (gnat_node)); |
7617 | break; | |
7618 | ||
7619 | case N_Subprogram_Body: | |
7620 | Subprogram_Body_to_gnu (gnat_node); | |
7621 | gnu_result = alloc_stmt_list (); | |
7622 | break; | |
7623 | ||
7624 | case N_Function_Call: | |
7625 | case N_Procedure_Call_Statement: | |
f797c2b7 | 7626 | gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, |
17a98a3d | 7627 | NOT_ATOMIC, false); |
a1ab4c31 AC |
7628 | break; |
7629 | ||
1e17ef87 EB |
7630 | /************************/ |
7631 | /* Chapter 7: Packages */ | |
7632 | /************************/ | |
a1ab4c31 AC |
7633 | |
7634 | case N_Package_Declaration: | |
7635 | gnu_result = gnat_to_gnu (Specification (gnat_node)); | |
7636 | break; | |
7637 | ||
7638 | case N_Package_Specification: | |
a1ab4c31 AC |
7639 | start_stmt_group (); |
7640 | process_decls (Visible_Declarations (gnat_node), | |
6d16658d BD |
7641 | Private_Declarations (gnat_node), |
7642 | true, true); | |
a1ab4c31 AC |
7643 | gnu_result = end_stmt_group (); |
7644 | break; | |
7645 | ||
7646 | case N_Package_Body: | |
1e17ef87 | 7647 | /* If this is the body of a generic package - do nothing. */ |
a1ab4c31 AC |
7648 | if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package) |
7649 | { | |
7650 | gnu_result = alloc_stmt_list (); | |
7651 | break; | |
7652 | } | |
7653 | ||
7654 | start_stmt_group (); | |
6d16658d | 7655 | process_decls (Declarations (gnat_node), Empty, true, true); |
a1ab4c31 AC |
7656 | if (Present (Handled_Statement_Sequence (gnat_node))) |
7657 | add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node))); | |
6d16658d BD |
7658 | if (Present (At_End_Proc (gnat_node))) |
7659 | At_End_Proc_to_gnu (gnat_node); | |
a1ab4c31 AC |
7660 | gnu_result = end_stmt_group (); |
7661 | break; | |
7662 | ||
1e17ef87 EB |
7663 | /********************************/ |
7664 | /* Chapter 8: Visibility Rules */ | |
7665 | /********************************/ | |
a1ab4c31 AC |
7666 | |
7667 | case N_Use_Package_Clause: | |
7668 | case N_Use_Type_Clause: | |
1e17ef87 | 7669 | /* Nothing to do here - but these may appear in list of declarations. */ |
a1ab4c31 AC |
7670 | gnu_result = alloc_stmt_list (); |
7671 | break; | |
7672 | ||
1e17ef87 EB |
7673 | /*********************/ |
7674 | /* Chapter 9: Tasks */ | |
7675 | /*********************/ | |
a1ab4c31 AC |
7676 | |
7677 | case N_Protected_Type_Declaration: | |
7678 | gnu_result = alloc_stmt_list (); | |
7679 | break; | |
7680 | ||
7681 | case N_Single_Task_Declaration: | |
afc737f0 | 7682 | gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true); |
a1ab4c31 AC |
7683 | gnu_result = alloc_stmt_list (); |
7684 | break; | |
7685 | ||
1e17ef87 EB |
7686 | /*********************************************************/ |
7687 | /* Chapter 10: Program Structure and Compilation Issues */ | |
7688 | /*********************************************************/ | |
a1ab4c31 AC |
7689 | |
7690 | case N_Compilation_Unit: | |
a09d56d8 | 7691 | /* This is not called for the main unit on which gigi is invoked. */ |
a1ab4c31 AC |
7692 | Compilation_Unit_to_gnu (gnat_node); |
7693 | gnu_result = alloc_stmt_list (); | |
7694 | break; | |
7695 | ||
c4833de1 EB |
7696 | case N_Subunit: |
7697 | gnu_result = gnat_to_gnu (Proper_Body (gnat_node)); | |
7698 | break; | |
7699 | ||
7700 | case N_Entry_Body: | |
7701 | case N_Protected_Body: | |
7702 | case N_Task_Body: | |
7703 | /* These nodes should only be present when annotating types. */ | |
7704 | gcc_assert (type_annotate_only); | |
6d16658d | 7705 | process_decls (Declarations (gnat_node), Empty, true, true); |
c4833de1 EB |
7706 | gnu_result = alloc_stmt_list (); |
7707 | break; | |
7708 | ||
a1ab4c31 AC |
7709 | case N_Subprogram_Body_Stub: |
7710 | case N_Package_Body_Stub: | |
7711 | case N_Protected_Body_Stub: | |
7712 | case N_Task_Body_Stub: | |
7713 | /* Simply process whatever unit is being inserted. */ | |
4e568a15 EB |
7714 | if (Present (Library_Unit (gnat_node))) |
7715 | gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node))); | |
7716 | else | |
7717 | { | |
7718 | gcc_assert (type_annotate_only); | |
7719 | gnu_result = alloc_stmt_list (); | |
7720 | } | |
a1ab4c31 AC |
7721 | break; |
7722 | ||
a1ab4c31 | 7723 | /***************************/ |
1e17ef87 | 7724 | /* Chapter 11: Exceptions */ |
a1ab4c31 AC |
7725 | /***************************/ |
7726 | ||
7727 | case N_Handled_Sequence_Of_Statements: | |
a1ab4c31 AC |
7728 | gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node); |
7729 | break; | |
7730 | ||
7731 | case N_Exception_Handler: | |
e08f1aad | 7732 | gnu_result = Exception_Handler_to_gnu (gnat_node); |
624e1688 AC |
7733 | break; |
7734 | ||
7735 | case N_Raise_Statement: | |
7736 | /* Only for reraise in back-end exceptions mode. */ | |
ce19ac12 | 7737 | gcc_assert (No (Name (gnat_node))); |
624e1688 AC |
7738 | |
7739 | start_stmt_group (); | |
a1ab4c31 | 7740 | |
5d733372 AO |
7741 | add_stmt_with_node (build_call_n_expr (reraise_zcx_decl, 1, |
7742 | gnu_incoming_exc_ptr), | |
7743 | gnat_node); | |
624e1688 | 7744 | |
624e1688 | 7745 | gnu_result = end_stmt_group (); |
a1ab4c31 AC |
7746 | break; |
7747 | ||
7748 | case N_Push_Constraint_Error_Label: | |
8f8f531f | 7749 | gnu_constraint_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7750 | break; |
7751 | ||
7752 | case N_Push_Storage_Error_Label: | |
8f8f531f | 7753 | gnu_storage_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7754 | break; |
7755 | ||
7756 | case N_Push_Program_Error_Label: | |
8f8f531f | 7757 | gnu_program_error_label_stack.safe_push (Exception_Label (gnat_node)); |
a1ab4c31 AC |
7758 | break; |
7759 | ||
7760 | case N_Pop_Constraint_Error_Label: | |
8f8f531f PMR |
7761 | gnat_temp = gnu_constraint_error_label_stack.pop (); |
7762 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7763 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7764 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7765 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7766 | break; |
7767 | ||
7768 | case N_Pop_Storage_Error_Label: | |
8f8f531f PMR |
7769 | gnat_temp = gnu_storage_error_label_stack.pop (); |
7770 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7771 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7772 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7773 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7774 | break; |
7775 | ||
7776 | case N_Pop_Program_Error_Label: | |
8f8f531f PMR |
7777 | gnat_temp = gnu_program_error_label_stack.pop (); |
7778 | if (Present (gnat_temp) | |
4a0d6b70 EB |
7779 | && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)) |
7780 | && No_Exception_Propagation_Active ()) | |
8f8f531f | 7781 | Warn_If_No_Local_Raise (gnat_temp); |
a1ab4c31 AC |
7782 | break; |
7783 | ||
1e17ef87 EB |
7784 | /******************************/ |
7785 | /* Chapter 12: Generic Units */ | |
7786 | /******************************/ | |
a1ab4c31 AC |
7787 | |
7788 | case N_Generic_Function_Renaming_Declaration: | |
7789 | case N_Generic_Package_Renaming_Declaration: | |
7790 | case N_Generic_Procedure_Renaming_Declaration: | |
7791 | case N_Generic_Package_Declaration: | |
7792 | case N_Generic_Subprogram_Declaration: | |
7793 | case N_Package_Instantiation: | |
7794 | case N_Procedure_Instantiation: | |
7795 | case N_Function_Instantiation: | |
7796 | /* These nodes can appear on a declaration list but there is nothing to | |
7797 | to be done with them. */ | |
7798 | gnu_result = alloc_stmt_list (); | |
7799 | break; | |
7800 | ||
1e17ef87 EB |
7801 | /**************************************************/ |
7802 | /* Chapter 13: Representation Clauses and */ | |
7803 | /* Implementation-Dependent Features */ | |
7804 | /**************************************************/ | |
a1ab4c31 AC |
7805 | |
7806 | case N_Attribute_Definition_Clause: | |
a1ab4c31 AC |
7807 | gnu_result = alloc_stmt_list (); |
7808 | ||
8df2e902 EB |
7809 | /* The only one we need to deal with is 'Address since, for the others, |
7810 | the front-end puts the information elsewhere. */ | |
7811 | if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address) | |
7812 | break; | |
7813 | ||
7814 | /* And we only deal with 'Address if the object has a Freeze node. */ | |
7815 | gnat_temp = Entity (Name (gnat_node)); | |
3b9d1594 EB |
7816 | if (Freeze_Node (gnat_temp)) |
7817 | { | |
d587d1e4 | 7818 | tree gnu_address = gnat_to_gnu (Expression (gnat_node)), gnu_temp; |
3b9d1594 EB |
7819 | |
7820 | /* Get the value to use as the address and save it as the equivalent | |
7821 | for the object; when it is frozen, gnat_to_gnu_entity will do the | |
7822 | right thing. For a subprogram, put the naked address but build a | |
7823 | meaningfull expression for an object in case its address is taken | |
7824 | before the Freeze node is encountered; this can happen if the type | |
7825 | of the object is limited and it is initialized with the result of | |
7826 | a function call. */ | |
7827 | if (Is_Subprogram (gnat_temp)) | |
d587d1e4 | 7828 | gnu_temp = gnu_address; |
3b9d1594 EB |
7829 | else |
7830 | { | |
7831 | tree gnu_type = gnat_to_gnu_type (Etype (gnat_temp)); | |
7832 | /* Drop atomic and volatile qualifiers for the expression. */ | |
7833 | gnu_type = TYPE_MAIN_VARIANT (gnu_type); | |
7834 | gnu_type | |
7835 | = build_reference_type_for_mode (gnu_type, ptr_mode, true); | |
7836 | gnu_address = convert (gnu_type, gnu_address); | |
d587d1e4 | 7837 | gnu_temp |
3b9d1594 EB |
7838 | = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_address); |
7839 | } | |
a1ab4c31 | 7840 | |
d587d1e4 | 7841 | save_gnu_tree (gnat_temp, gnu_temp, true); |
3b9d1594 | 7842 | } |
a1ab4c31 AC |
7843 | break; |
7844 | ||
7845 | case N_Enumeration_Representation_Clause: | |
7846 | case N_Record_Representation_Clause: | |
7847 | case N_At_Clause: | |
7848 | /* We do nothing with these. SEM puts the information elsewhere. */ | |
7849 | gnu_result = alloc_stmt_list (); | |
7850 | break; | |
7851 | ||
7852 | case N_Code_Statement: | |
7853 | if (!type_annotate_only) | |
7854 | { | |
7855 | tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node)); | |
7856 | tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE; | |
7857 | tree gnu_clobbers = NULL_TREE, tail; | |
7858 | bool allows_mem, allows_reg, fake; | |
7859 | int ninputs, noutputs, i; | |
7860 | const char **oconstraints; | |
7861 | const char *constraint; | |
7862 | char *clobber; | |
7863 | ||
7864 | /* First retrieve the 3 operand lists built by the front-end. */ | |
7865 | Setup_Asm_Outputs (gnat_node); | |
7866 | while (Present (gnat_temp = Asm_Output_Variable ())) | |
7867 | { | |
7868 | tree gnu_value = gnat_to_gnu (gnat_temp); | |
7869 | tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu | |
7870 | (Asm_Output_Constraint ())); | |
7871 | ||
7872 | gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs); | |
7873 | Next_Asm_Output (); | |
7874 | } | |
7875 | ||
7876 | Setup_Asm_Inputs (gnat_node); | |
7877 | while (Present (gnat_temp = Asm_Input_Value ())) | |
7878 | { | |
7879 | tree gnu_value = gnat_to_gnu (gnat_temp); | |
7880 | tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu | |
7881 | (Asm_Input_Constraint ())); | |
7882 | ||
7883 | gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs); | |
7884 | Next_Asm_Input (); | |
7885 | } | |
7886 | ||
7887 | Clobber_Setup (gnat_node); | |
b0ba442b | 7888 | while ((clobber = (char *) Clobber_Get_Next ())) |
a1ab4c31 AC |
7889 | gnu_clobbers |
7890 | = tree_cons (NULL_TREE, | |
7891 | build_string (strlen (clobber) + 1, clobber), | |
7892 | gnu_clobbers); | |
7893 | ||
1e17ef87 | 7894 | /* Then perform some standard checking and processing on the |
a1ab4c31 AC |
7895 | operands. In particular, mark them addressable if needed. */ |
7896 | gnu_outputs = nreverse (gnu_outputs); | |
7897 | noutputs = list_length (gnu_outputs); | |
7898 | gnu_inputs = nreverse (gnu_inputs); | |
7899 | ninputs = list_length (gnu_inputs); | |
2bb1fc26 | 7900 | oconstraints = XALLOCAVEC (const char *, noutputs); |
a1ab4c31 AC |
7901 | |
7902 | for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
7903 | { | |
7904 | tree output = TREE_VALUE (tail); | |
7905 | constraint | |
7906 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
7907 | oconstraints[i] = constraint; | |
7908 | ||
7909 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
7910 | &allows_mem, &allows_reg, &fake)) | |
7911 | { | |
7912 | /* If the operand is going to end up in memory, | |
7913 | mark it addressable. Note that we don't test | |
7914 | allows_mem like in the input case below; this | |
17a98a3d | 7915 | is modeled on the C front-end. */ |
7e4680c1 EB |
7916 | if (!allows_reg) |
7917 | { | |
722356ce | 7918 | output = remove_conversions (output, false); |
7e4680c1 EB |
7919 | if (TREE_CODE (output) == CONST_DECL |
7920 | && DECL_CONST_CORRESPONDING_VAR (output)) | |
7921 | output = DECL_CONST_CORRESPONDING_VAR (output); | |
7922 | if (!gnat_mark_addressable (output)) | |
7923 | output = error_mark_node; | |
7924 | } | |
a1ab4c31 AC |
7925 | } |
7926 | else | |
7927 | output = error_mark_node; | |
7928 | ||
7929 | TREE_VALUE (tail) = output; | |
7930 | } | |
7931 | ||
7932 | for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail)) | |
7933 | { | |
7934 | tree input = TREE_VALUE (tail); | |
7935 | constraint | |
7936 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail))); | |
7937 | ||
7938 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, | |
7939 | 0, oconstraints, | |
7940 | &allows_mem, &allows_reg)) | |
7941 | { | |
7942 | /* If the operand is going to end up in memory, | |
7943 | mark it addressable. */ | |
7e4680c1 EB |
7944 | if (!allows_reg && allows_mem) |
7945 | { | |
722356ce | 7946 | input = remove_conversions (input, false); |
7e4680c1 EB |
7947 | if (TREE_CODE (input) == CONST_DECL |
7948 | && DECL_CONST_CORRESPONDING_VAR (input)) | |
7949 | input = DECL_CONST_CORRESPONDING_VAR (input); | |
7950 | if (!gnat_mark_addressable (input)) | |
7951 | input = error_mark_node; | |
7952 | } | |
a1ab4c31 AC |
7953 | } |
7954 | else | |
7955 | input = error_mark_node; | |
7956 | ||
7957 | TREE_VALUE (tail) = input; | |
7958 | } | |
7959 | ||
1c384bf1 | 7960 | gnu_result = build5 (ASM_EXPR, void_type_node, |
a1ab4c31 | 7961 | gnu_template, gnu_outputs, |
1c384bf1 | 7962 | gnu_inputs, gnu_clobbers, NULL_TREE); |
a1ab4c31 AC |
7963 | ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node); |
7964 | } | |
7965 | else | |
7966 | gnu_result = alloc_stmt_list (); | |
7967 | ||
7968 | break; | |
7969 | ||
1e17ef87 EB |
7970 | /****************/ |
7971 | /* Added Nodes */ | |
7972 | /****************/ | |
a1ab4c31 | 7973 | |
a3a6d41f PMR |
7974 | /* Markers are created by the ABE mechanism to capture information which |
7975 | is either unavailable of expensive to recompute. Markers do not have | |
7976 | and runtime semantics, and should be ignored. */ | |
90e491a7 PMR |
7977 | |
7978 | case N_Call_Marker: | |
a3a6d41f | 7979 | case N_Variable_Reference_Marker: |
90e491a7 PMR |
7980 | gnu_result = alloc_stmt_list (); |
7981 | break; | |
7982 | ||
17c168fe | 7983 | case N_Expression_With_Actions: |
1a9ee222 OH |
7984 | /* This construct doesn't define a scope so we don't push a binding |
7985 | level around the statement list, but we wrap it in a SAVE_EXPR to | |
7986 | protect it from unsharing. Elaborate the expression as part of the | |
7987 | same statement group as the actions so that the type declaration | |
7988 | gets inserted there as well. This ensures that the type elaboration | |
7989 | code is issued past the actions computing values on which it might | |
7990 | depend. */ | |
1a9ee222 OH |
7991 | start_stmt_group (); |
7992 | add_stmt_list (Actions (gnat_node)); | |
7993 | gnu_expr = gnat_to_gnu (Expression (gnat_node)); | |
7994 | gnu_result = end_stmt_group (); | |
7995 | ||
17c168fe EB |
7996 | gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result); |
7997 | TREE_SIDE_EFFECTS (gnu_result) = 1; | |
1a9ee222 | 7998 | |
17c168fe | 7999 | gnu_result |
39ab2e8f | 8000 | = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr); |
e1860041 | 8001 | gnu_result_type = get_unpadded_type (Etype (gnat_node)); |
17c168fe EB |
8002 | break; |
8003 | ||
a1ab4c31 AC |
8004 | case N_Freeze_Entity: |
8005 | start_stmt_group (); | |
8006 | process_freeze_entity (gnat_node); | |
6d16658d | 8007 | process_decls (Actions (gnat_node), Empty, true, true); |
a1ab4c31 AC |
8008 | gnu_result = end_stmt_group (); |
8009 | break; | |
8010 | ||
3cd4a210 AC |
8011 | case N_Freeze_Generic_Entity: |
8012 | gnu_result = alloc_stmt_list (); | |
8013 | break; | |
8014 | ||
a1ab4c31 AC |
8015 | case N_Itype_Reference: |
8016 | if (!present_gnu_tree (Itype (gnat_node))) | |
8017 | process_type (Itype (gnat_node)); | |
a1ab4c31 AC |
8018 | gnu_result = alloc_stmt_list (); |
8019 | break; | |
8020 | ||
8021 | case N_Free_Statement: | |
bb24f343 EB |
8022 | gnat_temp = Expression (gnat_node); |
8023 | ||
a1ab4c31 AC |
8024 | if (!type_annotate_only) |
8025 | { | |
bb24f343 EB |
8026 | tree gnu_ptr, gnu_ptr_type, gnu_obj_type, gnu_actual_obj_type; |
8027 | ||
8028 | const Entity_Id gnat_desig_type | |
8029 | = Designated_Type (Underlying_Type (Etype (gnat_temp))); | |
8030 | ||
8031 | /* Make sure the designated type is complete before dereferencing, | |
8032 | in case it is a Taft Amendment type. */ | |
8033 | (void) gnat_to_gnu_entity (gnat_desig_type, NULL_TREE, false); | |
8034 | ||
8035 | gnu_ptr = gnat_to_gnu (gnat_temp); | |
8036 | gnu_ptr_type = TREE_TYPE (gnu_ptr); | |
0d7de0e1 EB |
8037 | |
8038 | /* If this is a thin pointer, we must first dereference it to create | |
8039 | a fat pointer, then go back below to a thin pointer. The reason | |
8040 | for this is that we need to have a fat pointer someplace in order | |
8041 | to properly compute the size. */ | |
315cff15 | 8042 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
8043 | gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE, |
8044 | build_unary_op (INDIRECT_REF, NULL_TREE, | |
8045 | gnu_ptr)); | |
8046 | ||
0d7de0e1 EB |
8047 | /* If this is a fat pointer, the object must have been allocated with |
8048 | the template in front of the array. So pass the template address, | |
8049 | and get the total size; do it by converting to a thin pointer. */ | |
315cff15 | 8050 | if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr))) |
a1ab4c31 AC |
8051 | gnu_ptr |
8052 | = convert (build_pointer_type | |
8053 | (TYPE_OBJECT_RECORD_TYPE | |
8054 | (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))), | |
8055 | gnu_ptr); | |
8056 | ||
8057 | gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr)); | |
8058 | ||
0d7de0e1 EB |
8059 | /* If this is a thin pointer, the object must have been allocated with |
8060 | the template in front of the array. So pass the template address, | |
8061 | and get the total size. */ | |
8062 | if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr))) | |
8063 | gnu_ptr | |
8064 | = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr), | |
8065 | gnu_ptr, | |
2b45154d EB |
8066 | fold_build1 (NEGATE_EXPR, sizetype, |
8067 | byte_position | |
8068 | (DECL_CHAIN | |
8069 | TYPE_FIELDS ((gnu_obj_type))))); | |
0d7de0e1 EB |
8070 | |
8071 | /* If we have a special dynamic constrained subtype on the node, use | |
8072 | it to compute the size; otherwise, use the designated subtype. */ | |
a1ab4c31 AC |
8073 | if (Present (Actual_Designated_Subtype (gnat_node))) |
8074 | { | |
8075 | gnu_actual_obj_type | |
1e17ef87 | 8076 | = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node)); |
a1ab4c31 | 8077 | |
315cff15 | 8078 | if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)) |
1e17ef87 EB |
8079 | gnu_actual_obj_type |
8080 | = build_unc_object_type_from_ptr (gnu_ptr_type, | |
8081 | gnu_actual_obj_type, | |
928dfa4b EB |
8082 | get_identifier ("DEALLOC"), |
8083 | false); | |
a1ab4c31 AC |
8084 | } |
8085 | else | |
8086 | gnu_actual_obj_type = gnu_obj_type; | |
8087 | ||
62801a77 EB |
8088 | tree gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type); |
8089 | gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_ptr); | |
8090 | ||
ff346f70 | 8091 | gnu_result |
62801a77 | 8092 | = build_call_alloc_dealloc (gnu_ptr, gnu_size, gnu_obj_type, |
ff346f70 OH |
8093 | Procedure_To_Call (gnat_node), |
8094 | Storage_Pool (gnat_node), | |
8095 | gnat_node); | |
a1ab4c31 AC |
8096 | } |
8097 | break; | |
8098 | ||
8099 | case N_Raise_Constraint_Error: | |
8100 | case N_Raise_Program_Error: | |
8101 | case N_Raise_Storage_Error: | |
80096613 EB |
8102 | if (type_annotate_only) |
8103 | gnu_result = alloc_stmt_list (); | |
8104 | else | |
8105 | gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type); | |
a1ab4c31 AC |
8106 | break; |
8107 | ||
8108 | case N_Validate_Unchecked_Conversion: | |
f04b8d69 EB |
8109 | /* The only validation we currently do on an unchecked conversion is |
8110 | that of aliasing assumptions. */ | |
8111 | if (flag_strict_aliasing) | |
9771b263 | 8112 | gnat_validate_uc_list.safe_push (gnat_node); |
a1ab4c31 AC |
8113 | gnu_result = alloc_stmt_list (); |
8114 | break; | |
8115 | ||
4e568a15 EB |
8116 | case N_Function_Specification: |
8117 | case N_Procedure_Specification: | |
8118 | case N_Op_Concat: | |
8119 | case N_Component_Association: | |
4e568a15 EB |
8120 | /* These nodes should only be present when annotating types. */ |
8121 | gcc_assert (type_annotate_only); | |
a1ab4c31 | 8122 | gnu_result = alloc_stmt_list (); |
4e568a15 EB |
8123 | break; |
8124 | ||
8125 | default: | |
8126 | /* Other nodes are not supposed to reach here. */ | |
8127 | gcc_unreachable (); | |
a1ab4c31 AC |
8128 | } |
8129 | ||
f53aff92 EB |
8130 | /* If we are in the elaboration procedure, check if we are violating the |
8131 | No_Elaboration_Code restriction by having a non-empty statement. */ | |
8132 | if (statement_node_p (gnat_node) | |
8133 | && !(TREE_CODE (gnu_result) == STATEMENT_LIST | |
8134 | && empty_stmt_list_p (gnu_result)) | |
8135 | && current_function_decl == get_elaboration_procedure ()) | |
8136 | Check_Elaboration_Code_Allowed (gnat_node); | |
8137 | ||
a09d56d8 | 8138 | /* If we pushed the processing of the elaboration routine, pop it back. */ |
a1ab4c31 | 8139 | if (went_into_elab_proc) |
a09d56d8 | 8140 | current_function_decl = NULL_TREE; |
a1ab4c31 | 8141 | |
1fc24649 | 8142 | /* When not optimizing, turn boolean rvalues B into B != false tests |
f1ff07ec EB |
8143 | so that we can put the location information of the reference to B on |
8144 | the inequality operator for better debug info. */ | |
1fc24649 | 8145 | if (!optimize |
054d6b83 | 8146 | && TREE_CODE (gnu_result) != INTEGER_CST |
f1ff07ec | 8147 | && TREE_CODE (gnu_result) != TYPE_DECL |
1fc24649 EB |
8148 | && (kind == N_Identifier |
8149 | || kind == N_Expanded_Name | |
8150 | || kind == N_Explicit_Dereference | |
1fc24649 EB |
8151 | || kind == N_Indexed_Component |
8152 | || kind == N_Selected_Component) | |
8153 | && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE | |
aff22074 | 8154 | && Nkind (Parent (gnat_node)) != N_Attribute_Reference |
bbdf2d39 | 8155 | && Nkind (Parent (gnat_node)) != N_Pragma_Argument_Association |
aff22074 EB |
8156 | && Nkind (Parent (gnat_node)) != N_Variant_Part |
8157 | && !lvalue_required_p (gnat_node, gnu_result_type, false, false)) | |
f1ff07ec EB |
8158 | { |
8159 | gnu_result | |
8160 | = build_binary_op (NE_EXPR, gnu_result_type, | |
8161 | convert (gnu_result_type, gnu_result), | |
8162 | convert (gnu_result_type, boolean_false_node)); | |
8163 | if (TREE_CODE (gnu_result) != INTEGER_CST) | |
8164 | set_gnu_expr_location_from_node (gnu_result, gnat_node); | |
8165 | } | |
8166 | ||
5e017b1e EB |
8167 | /* Set the location information on the result if it's not a simple name |
8168 | or something that contains a simple name, for example a tag, because | |
8169 | we don"t want all the references to get the location of the first use. | |
f1ff07ec EB |
8170 | Note that we may have no result if we tried to build a CALL_EXPR node |
8171 | to a procedure with no side-effects and optimization is enabled. */ | |
5e017b1e EB |
8172 | else if (kind != N_Identifier |
8173 | && !(kind == N_Selected_Component | |
8174 | && Chars (Selector_Name (gnat_node)) == Name_uTag) | |
8175 | && gnu_result | |
8176 | && EXPR_P (gnu_result)) | |
17c168fe | 8177 | set_gnu_expr_location_from_node (gnu_result, gnat_node); |
a1ab4c31 AC |
8178 | |
8179 | /* If we're supposed to return something of void_type, it means we have | |
8180 | something we're elaborating for effect, so just return. */ | |
8181 | if (TREE_CODE (gnu_result_type) == VOID_TYPE) | |
8182 | return gnu_result; | |
8183 | ||
c1abd261 EB |
8184 | /* If the result is a constant that overflowed, raise Constraint_Error. */ |
8185 | if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result)) | |
a1ab4c31 | 8186 | { |
57136d60 | 8187 | post_error ("??Constraint_Error will be raised at run time", gnat_node); |
a1ab4c31 AC |
8188 | gnu_result |
8189 | = build1 (NULL_EXPR, gnu_result_type, | |
8190 | build_call_raise (CE_Overflow_Check_Failed, gnat_node, | |
8191 | N_Raise_Constraint_Error)); | |
8192 | } | |
8193 | ||
62801a77 EB |
8194 | /* If the result has side-effects and is of an unconstrained type, protect |
8195 | the expression in case it will be referenced multiple times, i.e. for | |
8196 | its value and to compute the size of an object. But do it neither for | |
8197 | an object nor a renaming declaration, nor a return statement of a call | |
8198 | to a function that returns an unconstrained record type with default | |
8199 | discriminant, because there is no size to be computed in these cases | |
8200 | and this will create a useless temporary. We must do this before any | |
8201 | conversions. */ | |
a1ab4c31 AC |
8202 | if (TREE_SIDE_EFFECTS (gnu_result) |
8203 | && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE | |
62801a77 EB |
8204 | || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))) |
8205 | && !(TREE_CODE (gnu_result) == CALL_EXPR | |
8206 | && type_is_padding_self_referential (TREE_TYPE (gnu_result)) | |
8207 | && (Nkind (Parent (gnat_node)) == N_Object_Declaration | |
8208 | || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration | |
8209 | || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement))) | |
7194767c | 8210 | gnu_result = gnat_protect_expr (gnu_result); |
a1ab4c31 AC |
8211 | |
8212 | /* Now convert the result to the result type, unless we are in one of the | |
8213 | following cases: | |
8214 | ||
27ab5bd8 EB |
8215 | 1. If this is the LHS of an assignment or an actual parameter of a |
8216 | call, return the result almost unmodified since the RHS will have | |
8217 | to be converted to our type in that case, unless the result type | |
5dce843f EB |
8218 | has a simpler size or for array types because this size might be |
8219 | changed in-between. Likewise if there is just a no-op unchecked | |
27ab5bd8 EB |
8220 | conversion in-between. Similarly, don't convert integral types |
8221 | that are the operands of an unchecked conversion since we need | |
8222 | to ignore those conversions (for 'Valid). | |
a1ab4c31 AC |
8223 | |
8224 | 2. If we have a label (which doesn't have any well-defined type), a | |
abbc8c7b EB |
8225 | field or an error, return the result almost unmodified. Similarly, |
8226 | if the two types are record types with the same name, don't convert. | |
8227 | This will be the case when we are converting from a packable version | |
8228 | of a type to its original type and we need those conversions to be | |
8229 | NOPs in order for assignments into these types to work properly. | |
a1ab4c31 AC |
8230 | |
8231 | 3. If the type is void or if we have no result, return error_mark_node | |
8232 | to show we have no result. | |
8233 | ||
fc7a823e EB |
8234 | 4. If this is a call to a function that returns with variable size and |
8235 | the call is used as the expression in either an object or a renaming | |
8236 | declaration, return the result unmodified because we want to use the | |
8237 | return slot optimization in this case. | |
16934bbf | 8238 | |
eb0f4e48 EB |
8239 | 5. If this is a reference to an unconstrained array which is used as the |
8240 | prefix of an attribute reference that requires an lvalue, return the | |
875bdbe2 | 8241 | result unmodified because we want to return the original bounds. |
eb0f4e48 EB |
8242 | |
8243 | 6. Finally, if the type of the result is already correct. */ | |
a1ab4c31 AC |
8244 | |
8245 | if (Present (Parent (gnat_node)) | |
27ab5bd8 | 8246 | && (lhs_or_actual_p (gnat_node) |
c2efda0d | 8247 | || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion |
4f8a6678 | 8248 | && unchecked_conversion_nop (Parent (gnat_node))) |
a1ab4c31 AC |
8249 | || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion |
8250 | && !AGGREGATE_TYPE_P (gnu_result_type) | |
8251 | && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))) | |
8252 | && !(TYPE_SIZE (gnu_result_type) | |
8253 | && TYPE_SIZE (TREE_TYPE (gnu_result)) | |
5dce843f EB |
8254 | && AGGREGATE_TYPE_P (gnu_result_type) |
8255 | == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)) | |
a1ab4c31 AC |
8256 | && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST |
8257 | && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result))) | |
8258 | != INTEGER_CST)) | |
8259 | || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST | |
8260 | && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)) | |
8261 | && (CONTAINS_PLACEHOLDER_P | |
5dce843f EB |
8262 | (TYPE_SIZE (TREE_TYPE (gnu_result))))) |
8263 | || (TREE_CODE (gnu_result_type) == ARRAY_TYPE | |
8264 | && TREE_CODE (TREE_TYPE (gnu_result)) == ARRAY_TYPE)) | |
a1ab4c31 AC |
8265 | && !(TREE_CODE (gnu_result_type) == RECORD_TYPE |
8266 | && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type)))) | |
8267 | { | |
8268 | /* Remove padding only if the inner object is of self-referential | |
8269 | size: in that case it must be an object of unconstrained type | |
8270 | with a default discriminant and we want to avoid copying too | |
02221bed EB |
8271 | much data. But do not remove it if it is already too small. */ |
8272 | if (type_is_padding_self_referential (TREE_TYPE (gnu_result)) | |
8273 | && !(TREE_CODE (gnu_result) == COMPONENT_REF | |
1e66a917 EB |
8274 | && DECL_BIT_FIELD (TREE_OPERAND (gnu_result, 1)) |
8275 | && DECL_SIZE (TREE_OPERAND (gnu_result, 1)) | |
8276 | != TYPE_SIZE (TREE_TYPE (gnu_result)))) | |
a1ab4c31 AC |
8277 | gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))), |
8278 | gnu_result); | |
8279 | } | |
8280 | ||
8281 | else if (TREE_CODE (gnu_result) == LABEL_DECL | |
8282 | || TREE_CODE (gnu_result) == FIELD_DECL | |
8283 | || TREE_CODE (gnu_result) == ERROR_MARK | |
abbc8c7b EB |
8284 | || (TYPE_NAME (gnu_result_type) |
8285 | == TYPE_NAME (TREE_TYPE (gnu_result)) | |
a1ab4c31 AC |
8286 | && TREE_CODE (gnu_result_type) == RECORD_TYPE |
8287 | && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE)) | |
8288 | { | |
8289 | /* Remove any padding. */ | |
ad00a297 | 8290 | gnu_result = maybe_padded_object (gnu_result); |
a1ab4c31 AC |
8291 | } |
8292 | ||
8293 | else if (gnu_result == error_mark_node || gnu_result_type == void_type_node) | |
8294 | gnu_result = error_mark_node; | |
8295 | ||
eb0f4e48 EB |
8296 | else if (TREE_CODE (gnu_result) == CALL_EXPR |
8297 | && Present (Parent (gnat_node)) | |
fc7a823e EB |
8298 | && (Nkind (Parent (gnat_node)) == N_Object_Declaration |
8299 | || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration) | |
fc7a823e | 8300 | && return_type_with_variable_size_p (TREE_TYPE (gnu_result))) |
842d4ee2 | 8301 | ; |
16934bbf | 8302 | |
eb0f4e48 EB |
8303 | else if (TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF |
8304 | && Present (Parent (gnat_node)) | |
8305 | && Nkind (Parent (gnat_node)) == N_Attribute_Reference | |
8306 | && lvalue_required_for_attribute_p (Parent (gnat_node))) | |
8307 | ; | |
8308 | ||
16934bbf | 8309 | else if (TREE_TYPE (gnu_result) != gnu_result_type) |
a1ab4c31 AC |
8310 | gnu_result = convert (gnu_result_type, gnu_result); |
8311 | ||
8312 | /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */ | |
8313 | while ((TREE_CODE (gnu_result) == NOP_EXPR | |
8314 | || TREE_CODE (gnu_result) == NON_LVALUE_EXPR) | |
8315 | && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result)) | |
8316 | gnu_result = TREE_OPERAND (gnu_result, 0); | |
8317 | ||
8318 | return gnu_result; | |
8319 | } | |
93e708f9 EB |
8320 | |
8321 | /* Similar to gnat_to_gnu, but discard any object that might be created in | |
8322 | the course of the translation of GNAT_NODE, which must be an "external" | |
8323 | expression in the sense that it will be elaborated elsewhere. */ | |
8324 | ||
8325 | tree | |
8326 | gnat_to_gnu_external (Node_Id gnat_node) | |
8327 | { | |
8328 | const int save_force_global = force_global; | |
f53aff92 | 8329 | bool went_into_elab_proc; |
93e708f9 EB |
8330 | |
8331 | /* Force the local context and create a fake scope that we zap | |
8332 | at the end so declarations will not be stuck either in the | |
8333 | global varpool or in the current scope. */ | |
8334 | if (!current_function_decl) | |
8335 | { | |
8336 | current_function_decl = get_elaboration_procedure (); | |
8337 | went_into_elab_proc = true; | |
8338 | } | |
f53aff92 EB |
8339 | else |
8340 | went_into_elab_proc = false; | |
93e708f9 EB |
8341 | force_global = 0; |
8342 | gnat_pushlevel (); | |
8343 | ||
8344 | tree gnu_result = gnat_to_gnu (gnat_node); | |
8345 | ||
8346 | gnat_zaplevel (); | |
8347 | force_global = save_force_global; | |
8348 | if (went_into_elab_proc) | |
8349 | current_function_decl = NULL_TREE; | |
8350 | ||
f1ff07ec EB |
8351 | /* Do not import locations from external units. */ |
8352 | if (gnu_result && EXPR_P (gnu_result)) | |
8353 | SET_EXPR_LOCATION (gnu_result, UNKNOWN_LOCATION); | |
8354 | ||
93e708f9 EB |
8355 | return gnu_result; |
8356 | } | |
ce2d0ce2 | 8357 | |
102a1631 EB |
8358 | /* Return true if the statement list STMT_LIST is empty. */ |
8359 | ||
8360 | static bool | |
8361 | empty_stmt_list_p (tree stmt_list) | |
8362 | { | |
8363 | tree_stmt_iterator tsi; | |
8364 | ||
8365 | for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi)) | |
8366 | { | |
8367 | tree stmt = tsi_stmt (tsi); | |
8368 | ||
8369 | /* Anything else than an empty STMT_STMT counts as something. */ | |
8370 | if (TREE_CODE (stmt) != STMT_STMT || STMT_STMT_STMT (stmt)) | |
8371 | return false; | |
8372 | } | |
8373 | ||
8374 | return true; | |
8375 | } | |
8376 | ||
a1ab4c31 AC |
8377 | /* Record the current code position in GNAT_NODE. */ |
8378 | ||
8379 | static void | |
8380 | record_code_position (Node_Id gnat_node) | |
8381 | { | |
8382 | tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE); | |
8383 | ||
8384 | add_stmt_with_node (stmt_stmt, gnat_node); | |
8385 | save_gnu_tree (gnat_node, stmt_stmt, true); | |
8386 | } | |
8387 | ||
8388 | /* Insert the code for GNAT_NODE at the position saved for that node. */ | |
8389 | ||
8390 | static void | |
8391 | insert_code_for (Node_Id gnat_node) | |
8392 | { | |
102a1631 EB |
8393 | tree code = gnat_to_gnu (gnat_node); |
8394 | ||
8395 | /* It's too late to remove the STMT_STMT itself at this point. */ | |
8396 | if (!empty_stmt_list_p (code)) | |
8397 | STMT_STMT_STMT (get_gnu_tree (gnat_node)) = code; | |
8398 | ||
a1ab4c31 AC |
8399 | save_gnu_tree (gnat_node, NULL_TREE, true); |
8400 | } | |
ce2d0ce2 | 8401 | |
a1ab4c31 AC |
8402 | /* Start a new statement group chained to the previous group. */ |
8403 | ||
8404 | void | |
8405 | start_stmt_group (void) | |
8406 | { | |
8407 | struct stmt_group *group = stmt_group_free_list; | |
8408 | ||
8409 | /* First see if we can get one from the free list. */ | |
8410 | if (group) | |
8411 | stmt_group_free_list = group->previous; | |
8412 | else | |
766090c2 | 8413 | group = ggc_alloc<stmt_group> (); |
a1ab4c31 AC |
8414 | |
8415 | group->previous = current_stmt_group; | |
8416 | group->stmt_list = group->block = group->cleanups = NULL_TREE; | |
8417 | current_stmt_group = group; | |
8418 | } | |
8419 | ||
586fea26 EB |
8420 | /* Add GNU_STMT to the current statement group. If it is an expression with |
8421 | no effects, it is ignored. */ | |
a1ab4c31 AC |
8422 | |
8423 | void | |
8424 | add_stmt (tree gnu_stmt) | |
8425 | { | |
8426 | append_to_statement_list (gnu_stmt, ¤t_stmt_group->stmt_list); | |
8427 | } | |
8428 | ||
586fea26 EB |
8429 | /* Similar, but the statement is always added, regardless of side-effects. */ |
8430 | ||
8431 | void | |
8432 | add_stmt_force (tree gnu_stmt) | |
8433 | { | |
8434 | append_to_statement_list_force (gnu_stmt, ¤t_stmt_group->stmt_list); | |
8435 | } | |
8436 | ||
8437 | /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */ | |
a1ab4c31 AC |
8438 | |
8439 | void | |
8440 | add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node) | |
8441 | { | |
56b8aa0c | 8442 | if (Present (gnat_node)) |
a1ab4c31 AC |
8443 | set_expr_location_from_node (gnu_stmt, gnat_node); |
8444 | add_stmt (gnu_stmt); | |
8445 | } | |
8446 | ||
586fea26 EB |
8447 | /* Similar, but the statement is always added, regardless of side-effects. */ |
8448 | ||
8449 | void | |
8450 | add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node) | |
8451 | { | |
8452 | if (Present (gnat_node)) | |
8453 | set_expr_location_from_node (gnu_stmt, gnat_node); | |
8454 | add_stmt_force (gnu_stmt); | |
8455 | } | |
8456 | ||
a1ab4c31 | 8457 | /* Add a declaration statement for GNU_DECL to the current statement group. |
56b8aa0c | 8458 | Get the SLOC to be put onto the statement from GNAT_NODE. */ |
a1ab4c31 AC |
8459 | |
8460 | void | |
56b8aa0c | 8461 | add_decl_expr (tree gnu_decl, Node_Id gnat_node) |
a1ab4c31 AC |
8462 | { |
8463 | tree type = TREE_TYPE (gnu_decl); | |
7352723b | 8464 | tree gnu_stmt, gnu_init; |
a1ab4c31 AC |
8465 | |
8466 | /* If this is a variable that Gigi is to ignore, we may have been given | |
8467 | an ERROR_MARK. So test for it. We also might have been given a | |
8468 | reference for a renaming. So only do something for a decl. Also | |
8469 | ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */ | |
8470 | if (!DECL_P (gnu_decl) | |
8471 | || (TREE_CODE (gnu_decl) == TYPE_DECL | |
8472 | && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)) | |
8473 | return; | |
8474 | ||
8475 | gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl); | |
8476 | ||
6ba4f08f EB |
8477 | /* If we are external or global, we don't want to output the DECL_EXPR for |
8478 | this DECL node since we already have evaluated the expressions in the | |
a1ab4c31 | 8479 | sizes and positions as globals and doing it again would be wrong. */ |
6ba4f08f | 8480 | if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ()) |
a1ab4c31 AC |
8481 | { |
8482 | /* Mark everything as used to prevent node sharing with subprograms. | |
8483 | Note that walk_tree knows how to deal with TYPE_DECL, but neither | |
8484 | VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */ | |
3f13dd77 | 8485 | MARK_VISITED (gnu_stmt); |
a1ab4c31 AC |
8486 | if (TREE_CODE (gnu_decl) == VAR_DECL |
8487 | || TREE_CODE (gnu_decl) == CONST_DECL) | |
8488 | { | |
3f13dd77 EB |
8489 | MARK_VISITED (DECL_SIZE (gnu_decl)); |
8490 | MARK_VISITED (DECL_SIZE_UNIT (gnu_decl)); | |
8491 | MARK_VISITED (DECL_INITIAL (gnu_decl)); | |
a1ab4c31 | 8492 | } |
a1ab4c31 | 8493 | } |
6ba4f08f | 8494 | else |
56b8aa0c | 8495 | add_stmt_with_node (gnu_stmt, gnat_node); |
a1ab4c31 | 8496 | |
665e80ca EB |
8497 | /* Mark our TYPE_ADA_SIZE field now since it will not be gimplified. */ |
8498 | if (TREE_CODE (gnu_decl) == TYPE_DECL | |
8499 | && RECORD_OR_UNION_TYPE_P (type) | |
8500 | && !TYPE_FAT_POINTER_P (type)) | |
8501 | MARK_VISITED (TYPE_ADA_SIZE (type)); | |
8502 | ||
1f3f64b9 EB |
8503 | if (TREE_CODE (gnu_decl) == VAR_DECL && (gnu_init = DECL_INITIAL (gnu_decl))) |
8504 | { | |
8505 | /* If this is a variable and an initializer is attached to it, it must be | |
8506 | valid for the context. Similar to init_const in create_var_decl. */ | |
8507 | if (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init)) | |
a1ab4c31 AC |
8508 | || (TREE_STATIC (gnu_decl) |
8509 | && !initializer_constant_valid_p (gnu_init, | |
1f3f64b9 | 8510 | TREE_TYPE (gnu_init)))) |
a1ab4c31 | 8511 | { |
1f3f64b9 EB |
8512 | DECL_INITIAL (gnu_decl) = NULL_TREE; |
8513 | if (TREE_READONLY (gnu_decl)) | |
8514 | { | |
8515 | TREE_READONLY (gnu_decl) = 0; | |
8516 | DECL_READONLY_ONCE_ELAB (gnu_decl) = 1; | |
8517 | } | |
8518 | ||
8519 | /* Remove any padding so the assignment is done properly. */ | |
8520 | gnu_decl = maybe_padded_object (gnu_decl); | |
a1ab4c31 | 8521 | |
1f3f64b9 EB |
8522 | gnu_stmt |
8523 | = build_binary_op (INIT_EXPR, NULL_TREE, gnu_decl, gnu_init); | |
8524 | add_stmt_with_node (gnu_stmt, gnat_node); | |
8525 | } | |
7352723b | 8526 | |
1f3f64b9 EB |
8527 | /* If this is the initialization of a (potentially) large aggregate, then |
8528 | declare the dependence on the memcpy routine. */ | |
8529 | if (AGGREGATE_TYPE_P (type) | |
8530 | && (!TREE_CONSTANT (TYPE_SIZE (type)) | |
8531 | || compare_tree_int (TYPE_SIZE (type), 2 * BITS_PER_WORD) > 0)) | |
8532 | Check_Restriction_No_Dependence_On_System (Name_Memory_Copy, | |
8533 | gnat_node); | |
a1ab4c31 AC |
8534 | } |
8535 | } | |
8536 | ||
8537 | /* Callback for walk_tree to mark the visited trees rooted at *TP. */ | |
8538 | ||
8539 | static tree | |
8540 | mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) | |
8541 | { | |
3f13dd77 EB |
8542 | tree t = *tp; |
8543 | ||
8544 | if (TREE_VISITED (t)) | |
a1ab4c31 AC |
8545 | *walk_subtrees = 0; |
8546 | ||
8547 | /* Don't mark a dummy type as visited because we want to mark its sizes | |
8548 | and fields once it's filled in. */ | |
3f13dd77 EB |
8549 | else if (!TYPE_IS_DUMMY_P (t)) |
8550 | TREE_VISITED (t) = 1; | |
a1ab4c31 | 8551 | |
4ed9ab2d | 8552 | /* The test in gimplify_type_sizes is on the main variant. */ |
3f13dd77 | 8553 | if (TYPE_P (t)) |
4ed9ab2d | 8554 | TYPE_SIZES_GIMPLIFIED (TYPE_MAIN_VARIANT (t)) = 1; |
a1ab4c31 AC |
8555 | |
8556 | return NULL_TREE; | |
8557 | } | |
8558 | ||
3f13dd77 EB |
8559 | /* Mark nodes rooted at T with TREE_VISITED and types as having their |
8560 | sized gimplified. We use this to indicate all variable sizes and | |
8561 | positions in global types may not be shared by any subprogram. */ | |
8562 | ||
8563 | void | |
8564 | mark_visited (tree t) | |
8565 | { | |
8566 | walk_tree (&t, mark_visited_r, NULL, NULL); | |
8567 | } | |
8568 | ||
a1ab4c31 | 8569 | /* Add GNU_CLEANUP, a cleanup action, to the current code group and |
362db0b2 TQ |
8570 | set its location to that of GNAT_NODE if present, but with column info |
8571 | cleared so that conditional branches generated as part of the cleanup | |
8572 | code do not interfere with coverage analysis tools. */ | |
a1ab4c31 AC |
8573 | |
8574 | static void | |
8575 | add_cleanup (tree gnu_cleanup, Node_Id gnat_node) | |
8576 | { | |
8577 | if (Present (gnat_node)) | |
ba464315 | 8578 | set_expr_location_from_node (gnu_cleanup, gnat_node, true); |
c900c700 | 8579 | |
5d733372 AO |
8580 | /* An EH_ELSE_EXPR must be by itself, and that's all we need when we |
8581 | use it. The assert below makes sure that is so. Should we ever | |
8582 | need more than that, we could combine EH_ELSE_EXPRs, and copy | |
8583 | non-EH_ELSE_EXPR stmts into both cleanup paths of an | |
8584 | EH_ELSE_EXPR. */ | |
8585 | if (TREE_CODE (gnu_cleanup) == EH_ELSE_EXPR) | |
8586 | { | |
8587 | gcc_assert (!current_stmt_group->cleanups); | |
8588 | current_stmt_group->cleanups = gnu_cleanup; | |
8589 | } | |
8590 | else | |
8591 | { | |
8592 | gcc_assert (!current_stmt_group->cleanups | |
8593 | || (TREE_CODE (current_stmt_group->cleanups) | |
8594 | != EH_ELSE_EXPR)); | |
8595 | append_to_statement_list (gnu_cleanup, ¤t_stmt_group->cleanups); | |
8596 | } | |
a1ab4c31 AC |
8597 | } |
8598 | ||
8599 | /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */ | |
8600 | ||
8601 | void | |
8602 | set_block_for_group (tree gnu_block) | |
8603 | { | |
8604 | gcc_assert (!current_stmt_group->block); | |
8605 | current_stmt_group->block = gnu_block; | |
8606 | } | |
8607 | ||
8608 | /* Return code corresponding to the current code group. It is normally | |
8609 | a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if | |
8610 | BLOCK or cleanups were set. */ | |
8611 | ||
8612 | tree | |
8613 | end_stmt_group (void) | |
8614 | { | |
8615 | struct stmt_group *group = current_stmt_group; | |
8616 | tree gnu_retval = group->stmt_list; | |
8617 | ||
8618 | /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there | |
8619 | are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK, | |
8620 | make a BIND_EXPR. Note that we nest in that because the cleanup may | |
8621 | reference variables in the block. */ | |
7c775aca | 8622 | if (!gnu_retval) |
a1ab4c31 AC |
8623 | gnu_retval = alloc_stmt_list (); |
8624 | ||
8625 | if (group->cleanups) | |
8626 | gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval, | |
8627 | group->cleanups); | |
8628 | ||
8629 | if (current_stmt_group->block) | |
8630 | gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block), | |
8631 | gnu_retval, group->block); | |
8632 | ||
8633 | /* Remove this group from the stack and add it to the free list. */ | |
8634 | current_stmt_group = group->previous; | |
8635 | group->previous = stmt_group_free_list; | |
8636 | stmt_group_free_list = group; | |
8637 | ||
8638 | return gnu_retval; | |
8639 | } | |
8640 | ||
a712b009 EB |
8641 | /* Return whether the current statement group may fall through. */ |
8642 | ||
8643 | static inline bool | |
8644 | stmt_group_may_fallthru (void) | |
8645 | { | |
8646 | if (current_stmt_group->stmt_list) | |
8647 | return block_may_fallthru (current_stmt_group->stmt_list); | |
8648 | else | |
8649 | return true; | |
8650 | } | |
8651 | ||
a1ab4c31 AC |
8652 | /* Add a list of statements from GNAT_LIST, a possibly-empty list of |
8653 | statements.*/ | |
8654 | ||
8655 | static void | |
8656 | add_stmt_list (List_Id gnat_list) | |
8657 | { | |
8658 | Node_Id gnat_node; | |
8659 | ||
8660 | if (Present (gnat_list)) | |
8661 | for (gnat_node = First (gnat_list); Present (gnat_node); | |
8662 | gnat_node = Next (gnat_node)) | |
8663 | add_stmt (gnat_to_gnu (gnat_node)); | |
8664 | } | |
8665 | ||
8666 | /* Build a tree from GNAT_LIST, a possibly-empty list of statements. | |
8667 | If BINDING_P is true, push and pop a binding level around the list. */ | |
8668 | ||
8669 | static tree | |
8670 | build_stmt_group (List_Id gnat_list, bool binding_p) | |
8671 | { | |
8672 | start_stmt_group (); | |
547bbe49 | 8673 | |
a1ab4c31 AC |
8674 | if (binding_p) |
8675 | gnat_pushlevel (); | |
8676 | ||
8677 | add_stmt_list (gnat_list); | |
547bbe49 | 8678 | |
a1ab4c31 AC |
8679 | if (binding_p) |
8680 | gnat_poplevel (); | |
8681 | ||
8682 | return end_stmt_group (); | |
8683 | } | |
ce2d0ce2 | 8684 | |
a1ab4c31 AC |
8685 | /* Generate GIMPLE in place for the expression at *EXPR_P. */ |
8686 | ||
8687 | int | |
8688 | gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p, | |
8689 | gimple_seq *post_p ATTRIBUTE_UNUSED) | |
8690 | { | |
8691 | tree expr = *expr_p; | |
5e0f1fca | 8692 | tree type = TREE_TYPE (expr); |
a1ab4c31 AC |
8693 | tree op; |
8694 | ||
8695 | if (IS_ADA_STMT (expr)) | |
8696 | return gnat_gimplify_stmt (expr_p); | |
8697 | ||
8698 | switch (TREE_CODE (expr)) | |
8699 | { | |
a1ab4c31 AC |
8700 | case ADDR_EXPR: |
8701 | op = TREE_OPERAND (expr, 0); | |
8702 | ||
bb021771 EB |
8703 | /* If we are taking the address of a constant CONSTRUCTOR, make sure it |
8704 | is put into static memory. We know that it's going to be read-only | |
8705 | given the semantics we have and it must be in static memory when the | |
8706 | reference is in an elaboration procedure. */ | |
8707 | if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op)) | |
a1ab4c31 | 8708 | { |
bb021771 | 8709 | tree addr = build_fold_addr_expr (tree_output_constant_def (op)); |
5e0f1fca | 8710 | *expr_p = fold_convert (type, addr); |
bb021771 EB |
8711 | return GS_ALL_DONE; |
8712 | } | |
cb3d597d | 8713 | |
241125b2 EB |
8714 | /* Replace atomic loads with their first argument. That's necessary |
8715 | because the gimplifier would create a temporary otherwise. */ | |
8716 | if (TREE_SIDE_EFFECTS (op)) | |
8717 | while (handled_component_p (op) || CONVERT_EXPR_P (op)) | |
8718 | { | |
8719 | tree inner = TREE_OPERAND (op, 0); | |
8720 | if (TREE_CODE (inner) == CALL_EXPR && call_is_atomic_load (inner)) | |
8721 | { | |
8722 | tree t = CALL_EXPR_ARG (inner, 0); | |
8723 | if (TREE_CODE (t) == NOP_EXPR) | |
8724 | t = TREE_OPERAND (t, 0); | |
8725 | if (TREE_CODE (t) == ADDR_EXPR) | |
8726 | TREE_OPERAND (op, 0) = TREE_OPERAND (t, 0); | |
8727 | else | |
8728 | TREE_OPERAND (op, 0) = build_fold_indirect_ref (t); | |
8729 | } | |
8730 | else | |
8731 | op = inner; | |
8732 | } | |
9ae38416 | 8733 | break; |
456976d8 | 8734 | |
fccc47dd EB |
8735 | case CALL_EXPR: |
8736 | /* If we are passing a constant fat pointer CONSTRUCTOR, make sure it is | |
8737 | put into static memory; this performs a restricted version of constant | |
8738 | propagation on fat pointers in calls. But do not do it for strings to | |
8739 | avoid blocking concatenation in the caller when it is inlined. */ | |
8740 | for (int i = 0; i < call_expr_nargs (expr); i++) | |
8741 | { | |
8742 | tree arg = *(CALL_EXPR_ARGP (expr) + i); | |
8743 | ||
8744 | if (TREE_CODE (arg) == CONSTRUCTOR | |
8745 | && TREE_CONSTANT (arg) | |
8746 | && TYPE_IS_FAT_POINTER_P (TREE_TYPE (arg))) | |
8747 | { | |
8748 | tree t = CONSTRUCTOR_ELT (arg, 0)->value; | |
8749 | if (TREE_CODE (t) == NOP_EXPR) | |
8750 | t = TREE_OPERAND (t, 0); | |
8751 | if (TREE_CODE (t) == ADDR_EXPR) | |
8752 | t = TREE_OPERAND (t, 0); | |
8753 | if (TREE_CODE (t) != STRING_CST) | |
8754 | *(CALL_EXPR_ARGP (expr) + i) = tree_output_constant_def (arg); | |
8755 | } | |
8756 | } | |
9ae38416 EB |
8757 | break; |
8758 | ||
8759 | case DECL_EXPR: | |
8760 | op = DECL_EXPR_DECL (expr); | |
8761 | ||
8762 | /* The expressions for the RM bounds must be gimplified to ensure that | |
8763 | they are properly elaborated. See gimplify_decl_expr. */ | |
8764 | if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL) | |
8765 | && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)) | |
8766 | && (INTEGRAL_TYPE_P (TREE_TYPE (op)) | |
8767 | || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))) | |
8768 | { | |
8769 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val; | |
8770 | ||
8771 | val = TYPE_RM_MIN_VALUE (type); | |
8772 | if (val) | |
8773 | { | |
8774 | gimplify_one_sizepos (&val, pre_p); | |
8775 | for (t = type; t; t = TYPE_NEXT_VARIANT (t)) | |
8776 | SET_TYPE_RM_MIN_VALUE (t, val); | |
8777 | } | |
8778 | ||
8779 | val = TYPE_RM_MAX_VALUE (type); | |
8780 | if (val) | |
8781 | { | |
8782 | gimplify_one_sizepos (&val, pre_p); | |
8783 | for (t = type; t; t = TYPE_NEXT_VARIANT (t)) | |
8784 | SET_TYPE_RM_MAX_VALUE (t, val); | |
8785 | } | |
8786 | } | |
8787 | break; | |
8788 | ||
8789 | case NULL_EXPR: | |
8790 | /* If this is an aggregate type, build a null pointer of the appropriate | |
8791 | type and dereference it. */ | |
8792 | if (AGGREGATE_TYPE_P (type) | |
8793 | || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE) | |
8794 | *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE, | |
8795 | convert (build_pointer_type (type), | |
8796 | integer_zero_node)); | |
8797 | ||
8798 | /* Otherwise, just make a VAR_DECL. */ | |
8799 | else | |
8800 | { | |
8801 | *expr_p = create_tmp_var (type, NULL); | |
8802 | suppress_warning (*expr_p); | |
8803 | } | |
fccc47dd | 8804 | |
9ae38416 EB |
8805 | gimplify_and_add (TREE_OPERAND (expr, 0), pre_p); |
8806 | return GS_OK; | |
8807 | ||
889db59e EB |
8808 | case SAVE_EXPR: |
8809 | op = TREE_OPERAND (expr, 0); | |
8810 | ||
8811 | /* Propagate TREE_NO_WARNING from expression to temporary by using the | |
8812 | SAVE_EXPR itself as an intermediate step. See gimplify_save_expr. */ | |
8813 | if (SAVE_EXPR_RESOLVED_P (expr)) | |
8814 | TREE_NO_WARNING (op) = TREE_NO_WARNING (expr); | |
8815 | else | |
8816 | TREE_NO_WARNING (expr) = TREE_NO_WARNING (op); | |
8817 | break; | |
8818 | ||
9ae38416 EB |
8819 | case UNCONSTRAINED_ARRAY_REF: |
8820 | /* We should only do this if we are just elaborating for side effects, | |
8821 | but we can't know that yet. */ | |
8822 | *expr_p = TREE_OPERAND (*expr_p, 0); | |
8823 | return GS_OK; | |
fccc47dd | 8824 | |
819a653e EB |
8825 | case VIEW_CONVERT_EXPR: |
8826 | op = TREE_OPERAND (expr, 0); | |
8827 | ||
8828 | /* If we are view-converting a CONSTRUCTOR or a call from an aggregate | |
8829 | type to a scalar one, explicitly create the local temporary. That's | |
8830 | required if the type is passed by reference. */ | |
8831 | if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR) | |
8832 | && AGGREGATE_TYPE_P (TREE_TYPE (op)) | |
5e0f1fca | 8833 | && !AGGREGATE_TYPE_P (type)) |
819a653e EB |
8834 | { |
8835 | tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C"); | |
8836 | gimple_add_tmp_var (new_var); | |
8837 | ||
8838 | mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op); | |
8839 | gimplify_and_add (mod, pre_p); | |
8840 | ||
8841 | TREE_OPERAND (expr, 0) = new_var; | |
8842 | return GS_OK; | |
8843 | } | |
9ae38416 | 8844 | break; |
a1ab4c31 AC |
8845 | |
8846 | default: | |
9ae38416 | 8847 | break; |
a1ab4c31 | 8848 | } |
9ae38416 EB |
8849 | |
8850 | return GS_UNHANDLED; | |
a1ab4c31 AC |
8851 | } |
8852 | ||
8853 | /* Generate GIMPLE in place for the statement at *STMT_P. */ | |
8854 | ||
8855 | static enum gimplify_status | |
8856 | gnat_gimplify_stmt (tree *stmt_p) | |
8857 | { | |
8858 | tree stmt = *stmt_p; | |
8859 | ||
8860 | switch (TREE_CODE (stmt)) | |
8861 | { | |
8862 | case STMT_STMT: | |
8863 | *stmt_p = STMT_STMT_STMT (stmt); | |
8864 | return GS_OK; | |
8865 | ||
8866 | case LOOP_STMT: | |
8867 | { | |
c172df28 | 8868 | tree gnu_start_label = create_artificial_label (input_location); |
d88bbbb9 EB |
8869 | tree gnu_cond = LOOP_STMT_COND (stmt); |
8870 | tree gnu_update = LOOP_STMT_UPDATE (stmt); | |
a1ab4c31 | 8871 | tree gnu_end_label = LOOP_STMT_LABEL (stmt); |
a1ab4c31 | 8872 | |
d88bbbb9 EB |
8873 | /* Build the condition expression from the test, if any. */ |
8874 | if (gnu_cond) | |
3418f5e9 EB |
8875 | { |
8876 | /* Deal with the optimization hints. */ | |
8877 | if (LOOP_STMT_IVDEP (stmt)) | |
ac9effed | 8878 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
3418f5e9 | 8879 | build_int_cst (integer_type_node, |
ac9effed EB |
8880 | annot_expr_ivdep_kind), |
8881 | integer_zero_node); | |
8882 | if (LOOP_STMT_NO_UNROLL (stmt)) | |
8883 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, | |
8884 | build_int_cst (integer_type_node, | |
8885 | annot_expr_unroll_kind), | |
8886 | integer_one_node); | |
8887 | if (LOOP_STMT_UNROLL (stmt)) | |
8888 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, | |
8889 | build_int_cst (integer_type_node, | |
8890 | annot_expr_unroll_kind), | |
8891 | build_int_cst (NULL_TREE, USHRT_MAX)); | |
718c4601 | 8892 | if (LOOP_STMT_NO_VECTOR (stmt)) |
ac9effed | 8893 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
718c4601 | 8894 | build_int_cst (integer_type_node, |
ac9effed EB |
8895 | annot_expr_no_vector_kind), |
8896 | integer_zero_node); | |
718c4601 | 8897 | if (LOOP_STMT_VECTOR (stmt)) |
ac9effed | 8898 | gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond, |
718c4601 | 8899 | build_int_cst (integer_type_node, |
ac9effed EB |
8900 | annot_expr_vector_kind), |
8901 | integer_zero_node); | |
718c4601 | 8902 | |
3418f5e9 EB |
8903 | gnu_cond |
8904 | = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE, | |
8905 | build1 (GOTO_EXPR, void_type_node, gnu_end_label)); | |
8906 | } | |
d88bbbb9 | 8907 | |
a1ab4c31 AC |
8908 | /* Set to emit the statements of the loop. */ |
8909 | *stmt_p = NULL_TREE; | |
8910 | ||
d88bbbb9 EB |
8911 | /* We first emit the start label and then a conditional jump to the |
8912 | end label if there's a top condition, then the update if it's at | |
8913 | the top, then the body of the loop, then a conditional jump to | |
8914 | the end label if there's a bottom condition, then the update if | |
8915 | it's at the bottom, and finally a jump to the start label and the | |
8916 | definition of the end label. */ | |
a1ab4c31 AC |
8917 | append_to_statement_list (build1 (LABEL_EXPR, void_type_node, |
8918 | gnu_start_label), | |
8919 | stmt_p); | |
8920 | ||
d88bbbb9 EB |
8921 | if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt)) |
8922 | append_to_statement_list (gnu_cond, stmt_p); | |
8923 | ||
8924 | if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt)) | |
8925 | append_to_statement_list (gnu_update, stmt_p); | |
a1ab4c31 AC |
8926 | |
8927 | append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p); | |
8928 | ||
d88bbbb9 EB |
8929 | if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt)) |
8930 | append_to_statement_list (gnu_cond, stmt_p); | |
8931 | ||
8932 | if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt)) | |
8933 | append_to_statement_list (gnu_update, stmt_p); | |
a1ab4c31 | 8934 | |
3418f5e9 | 8935 | tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label); |
a1ab4c31 AC |
8936 | SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label)); |
8937 | append_to_statement_list (t, stmt_p); | |
8938 | ||
8939 | append_to_statement_list (build1 (LABEL_EXPR, void_type_node, | |
8940 | gnu_end_label), | |
8941 | stmt_p); | |
8942 | return GS_OK; | |
8943 | } | |
8944 | ||
8945 | case EXIT_STMT: | |
8946 | /* Build a statement to jump to the corresponding end label, then | |
8947 | see if it needs to be conditional. */ | |
8948 | *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt)); | |
8949 | if (EXIT_STMT_COND (stmt)) | |
8950 | *stmt_p = build3 (COND_EXPR, void_type_node, | |
8951 | EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ()); | |
8952 | return GS_OK; | |
8953 | ||
8954 | default: | |
8955 | gcc_unreachable (); | |
8956 | } | |
8957 | } | |
ce2d0ce2 | 8958 | |
2b2a2e9e EB |
8959 | /* Force a reference to each of the entities in GNAT_PACKAGE recursively. |
8960 | ||
8961 | This routine is exclusively called in type_annotate mode, to compute DDA | |
8962 | information for types in withed units, for ASIS use. */ | |
8963 | ||
8964 | static void | |
8965 | elaborate_all_entities_for_package (Entity_Id gnat_package) | |
8966 | { | |
8967 | Entity_Id gnat_entity; | |
8968 | ||
8969 | for (gnat_entity = First_Entity (gnat_package); | |
8970 | Present (gnat_entity); | |
8971 | gnat_entity = Next_Entity (gnat_entity)) | |
8972 | { | |
8973 | const Entity_Kind kind = Ekind (gnat_entity); | |
8974 | ||
8975 | /* We are interested only in entities visible from the main unit. */ | |
8976 | if (!Is_Public (gnat_entity)) | |
8977 | continue; | |
8978 | ||
8979 | /* Skip stuff internal to the compiler. */ | |
abb540a7 | 8980 | if (Is_Intrinsic_Subprogram (gnat_entity)) |
2b2a2e9e EB |
8981 | continue; |
8982 | if (kind == E_Operator) | |
8983 | continue; | |
56bad7db EB |
8984 | if (IN (kind, Subprogram_Kind) |
8985 | && (Present (Alias (gnat_entity)) | |
8986 | || Is_Intrinsic_Subprogram (gnat_entity))) | |
2b2a2e9e | 8987 | continue; |
f8a9b81f EB |
8988 | if (Is_Itype (gnat_entity)) |
8989 | continue; | |
2b2a2e9e EB |
8990 | |
8991 | /* Skip named numbers. */ | |
8992 | if (IN (kind, Named_Kind)) | |
8993 | continue; | |
8994 | ||
8995 | /* Skip generic declarations. */ | |
8996 | if (IN (kind, Generic_Unit_Kind)) | |
8997 | continue; | |
8998 | ||
cce309d7 EB |
8999 | /* Skip formal objects. */ |
9000 | if (IN (kind, Formal_Object_Kind)) | |
9001 | continue; | |
9002 | ||
2b2a2e9e EB |
9003 | /* Skip package bodies. */ |
9004 | if (kind == E_Package_Body) | |
9005 | continue; | |
9006 | ||
9007 | /* Skip limited views that point back to the main unit. */ | |
9008 | if (IN (kind, Incomplete_Kind) | |
9009 | && From_Limited_With (gnat_entity) | |
9010 | && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity))) | |
9011 | continue; | |
9012 | ||
9013 | /* Skip types that aren't frozen. */ | |
9014 | if (IN (kind, Type_Kind) && !Is_Frozen (gnat_entity)) | |
9015 | continue; | |
9016 | ||
9017 | /* Recurse on real packages that aren't in the main unit. */ | |
9018 | if (kind == E_Package) | |
9019 | { | |
9020 | if (No (Renamed_Entity (gnat_entity)) | |
9021 | && !In_Extended_Main_Code_Unit (gnat_entity)) | |
9022 | elaborate_all_entities_for_package (gnat_entity); | |
9023 | } | |
9024 | else | |
afc737f0 | 9025 | gnat_to_gnu_entity (gnat_entity, NULL_TREE, false); |
2b2a2e9e EB |
9026 | } |
9027 | } | |
9028 | ||
9029 | /* Force a reference to each of the entities in packages withed by GNAT_NODE. | |
a1ab4c31 AC |
9030 | Operate recursively but check that we aren't elaborating something more |
9031 | than once. | |
9032 | ||
9033 | This routine is exclusively called in type_annotate mode, to compute DDA | |
9034 | information for types in withed units, for ASIS use. */ | |
9035 | ||
9036 | static void | |
9037 | elaborate_all_entities (Node_Id gnat_node) | |
9038 | { | |
2b2a2e9e | 9039 | Entity_Id gnat_with_clause; |
a1ab4c31 AC |
9040 | |
9041 | /* Process each unit only once. As we trace the context of all relevant | |
9042 | units transitively, including generic bodies, we may encounter the | |
9043 | same generic unit repeatedly. */ | |
9044 | if (!present_gnu_tree (gnat_node)) | |
9045 | save_gnu_tree (gnat_node, integer_zero_node, true); | |
9046 | ||
9047 | /* Save entities in all context units. A body may have an implicit_with | |
9048 | on its own spec, if the context includes a child unit, so don't save | |
9049 | the spec twice. */ | |
9050 | for (gnat_with_clause = First (Context_Items (gnat_node)); | |
9051 | Present (gnat_with_clause); | |
9052 | gnat_with_clause = Next (gnat_with_clause)) | |
9053 | if (Nkind (gnat_with_clause) == N_With_Clause | |
9054 | && !present_gnu_tree (Library_Unit (gnat_with_clause)) | |
9055 | && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit))) | |
9056 | { | |
2b2a2e9e EB |
9057 | Node_Id gnat_unit = Library_Unit (gnat_with_clause); |
9058 | Entity_Id gnat_entity = Entity (Name (gnat_with_clause)); | |
a1ab4c31 | 9059 | |
2b2a2e9e EB |
9060 | elaborate_all_entities (gnat_unit); |
9061 | ||
0850f23b EB |
9062 | if (Ekind (gnat_entity) == E_Package |
9063 | && No (Renamed_Entity (gnat_entity))) | |
2b2a2e9e EB |
9064 | elaborate_all_entities_for_package (gnat_entity); |
9065 | ||
9066 | else if (Ekind (gnat_entity) == E_Generic_Package) | |
a1ab4c31 | 9067 | { |
2b2a2e9e | 9068 | Node_Id gnat_body = Corresponding_Body (Unit (gnat_unit)); |
a1ab4c31 AC |
9069 | |
9070 | /* Retrieve compilation unit node of generic body. */ | |
9071 | while (Present (gnat_body) | |
9072 | && Nkind (gnat_body) != N_Compilation_Unit) | |
9073 | gnat_body = Parent (gnat_body); | |
9074 | ||
9075 | /* If body is available, elaborate its context. */ | |
9076 | if (Present (gnat_body)) | |
9077 | elaborate_all_entities (gnat_body); | |
9078 | } | |
9079 | } | |
9080 | ||
9081 | if (Nkind (Unit (gnat_node)) == N_Package_Body) | |
9082 | elaborate_all_entities (Library_Unit (gnat_node)); | |
9083 | } | |
ce2d0ce2 | 9084 | |
f08863f9 | 9085 | /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */ |
a1ab4c31 AC |
9086 | |
9087 | static void | |
9088 | process_freeze_entity (Node_Id gnat_node) | |
9089 | { | |
f08863f9 EB |
9090 | const Entity_Id gnat_entity = Entity (gnat_node); |
9091 | const Entity_Kind kind = Ekind (gnat_entity); | |
9092 | tree gnu_old, gnu_new; | |
9093 | ||
692317f4 | 9094 | /* If this is a package, generate code for the package body, if any. */ |
f08863f9 | 9095 | if (kind == E_Package) |
a1ab4c31 | 9096 | { |
692317f4 EB |
9097 | const Node_Id gnat_decl = Parent (Declaration_Node (gnat_entity)); |
9098 | if (Present (Corresponding_Body (gnat_decl))) | |
9099 | insert_code_for (Parent (Corresponding_Body (gnat_decl))); | |
a1ab4c31 AC |
9100 | return; |
9101 | } | |
9102 | ||
f08863f9 EB |
9103 | /* Don't do anything for class-wide types as they are always transformed |
9104 | into their root type. */ | |
9105 | if (kind == E_Class_Wide_Type) | |
9106 | return; | |
9107 | ||
d4090614 EB |
9108 | /* Likewise for the entities internally used by the front-end to register |
9109 | primitives covering abstract interfaces, see Expand_N_Freeze_Entity. */ | |
9110 | if (Is_Subprogram (gnat_entity) && Present (Interface_Alias (gnat_entity))) | |
9111 | return; | |
9112 | ||
1e55d29a EB |
9113 | /* Check for an old definition if this isn't an object with address clause, |
9114 | since the saved GCC tree is the address expression in that case. */ | |
a1ab4c31 | 9115 | gnu_old |
1e55d29a EB |
9116 | = present_gnu_tree (gnat_entity) && No (Address_Clause (gnat_entity)) |
9117 | ? get_gnu_tree (gnat_entity) : NULL_TREE; | |
a1ab4c31 AC |
9118 | |
9119 | /* Don't do anything for subprograms that may have been elaborated before | |
f08863f9 EB |
9120 | their freeze nodes. This can happen, for example, because of an inner |
9121 | call in an instance body or because of previous compilation of a spec | |
9122 | for inlining purposes. */ | |
a1ab4c31 AC |
9123 | if (gnu_old |
9124 | && ((TREE_CODE (gnu_old) == FUNCTION_DECL | |
f08863f9 | 9125 | && (kind == E_Function || kind == E_Procedure)) |
69720717 | 9126 | || (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_old)) |
f08863f9 | 9127 | && kind == E_Subprogram_Type))) |
a1ab4c31 AC |
9128 | return; |
9129 | ||
d5ebeb8c EB |
9130 | /* If we have a non-dummy type old tree, we have nothing to do, except for |
9131 | aborting, since this node was never delayed as it should have been. We | |
a1ab4c31 | 9132 | let this happen for concurrent types and their Corresponding_Record_Type, |
f08863f9 | 9133 | however, because each might legitimately be elaborated before its own |
a1ab4c31 AC |
9134 | freeze node, e.g. while processing the other. */ |
9135 | if (gnu_old | |
9136 | && !(TREE_CODE (gnu_old) == TYPE_DECL | |
9137 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)))) | |
9138 | { | |
d5ebeb8c | 9139 | gcc_assert (Is_Concurrent_Type (gnat_entity) |
4ec7c4ec | 9140 | || (Is_Record_Type (gnat_entity) |
a1ab4c31 AC |
9141 | && Is_Concurrent_Record_Type (gnat_entity))); |
9142 | return; | |
9143 | } | |
9144 | ||
9145 | /* Reset the saved tree, if any, and elaborate the object or type for real. | |
f08863f9 EB |
9146 | If there is a full view, elaborate it and use the result. And, if this |
9147 | is the root type of a class-wide type, reuse it for the latter. */ | |
a1ab4c31 AC |
9148 | if (gnu_old) |
9149 | { | |
9150 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
bf0b0e5e | 9151 | |
7ed9919d | 9152 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
bf0b0e5e AC |
9153 | && Present (Full_View (gnat_entity))) |
9154 | { | |
9155 | Entity_Id full_view = Full_View (gnat_entity); | |
9156 | ||
f10ff6cc AC |
9157 | save_gnu_tree (full_view, NULL_TREE, false); |
9158 | ||
7ed9919d | 9159 | if (Is_Private_Type (full_view) |
bf0b0e5e | 9160 | && Present (Underlying_Full_View (full_view))) |
f10ff6cc AC |
9161 | { |
9162 | full_view = Underlying_Full_View (full_view); | |
9163 | save_gnu_tree (full_view, NULL_TREE, false); | |
9164 | } | |
bf0b0e5e AC |
9165 | } |
9166 | ||
7ed9919d | 9167 | if (Is_Type (gnat_entity) |
f08863f9 EB |
9168 | && Present (Class_Wide_Type (gnat_entity)) |
9169 | && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity) | |
a1ab4c31 AC |
9170 | save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false); |
9171 | } | |
9172 | ||
7ed9919d | 9173 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
a1ab4c31 AC |
9174 | && Present (Full_View (gnat_entity))) |
9175 | { | |
bf0b0e5e AC |
9176 | Entity_Id full_view = Full_View (gnat_entity); |
9177 | ||
7ed9919d | 9178 | if (Is_Private_Type (full_view) |
bf0b0e5e AC |
9179 | && Present (Underlying_Full_View (full_view))) |
9180 | full_view = Underlying_Full_View (full_view); | |
9181 | ||
afc737f0 | 9182 | gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, true); |
a1ab4c31 AC |
9183 | |
9184 | /* Propagate back-annotations from full view to partial view. */ | |
8de68eb3 | 9185 | if (!Known_Alignment (gnat_entity)) |
0c8ff35e | 9186 | Copy_Alignment (gnat_entity, full_view); |
a1ab4c31 | 9187 | |
8de68eb3 | 9188 | if (!Known_Esize (gnat_entity)) |
b23cdc01 | 9189 | Copy_Esize (gnat_entity, full_view); |
a1ab4c31 | 9190 | |
8de68eb3 | 9191 | if (!Known_RM_Size (gnat_entity)) |
b23cdc01 | 9192 | Copy_RM_Size (gnat_entity, full_view); |
a1ab4c31 AC |
9193 | |
9194 | /* The above call may have defined this entity (the simplest example | |
f08863f9 EB |
9195 | of this is when we have a private enumeral type since the bounds |
9196 | will have the public view). */ | |
a1ab4c31 | 9197 | if (!present_gnu_tree (gnat_entity)) |
f08863f9 | 9198 | save_gnu_tree (gnat_entity, gnu_new, false); |
a1ab4c31 AC |
9199 | } |
9200 | else | |
f08863f9 EB |
9201 | { |
9202 | tree gnu_init | |
9203 | = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration | |
9204 | && present_gnu_tree (Declaration_Node (gnat_entity))) | |
9205 | ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE; | |
9206 | ||
afc737f0 | 9207 | gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, true); |
f08863f9 EB |
9208 | } |
9209 | ||
7ed9919d | 9210 | if (Is_Type (gnat_entity) |
f08863f9 EB |
9211 | && Present (Class_Wide_Type (gnat_entity)) |
9212 | && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity) | |
9213 | save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false); | |
a1ab4c31 | 9214 | |
65444786 EB |
9215 | /* If we have an old type and we've made pointers to this type, update those |
9216 | pointers. If this is a Taft amendment type in the main unit, we need to | |
9217 | mark the type as used since other units referencing it don't see the full | |
9218 | declaration and, therefore, cannot mark it as used themselves. */ | |
a1ab4c31 | 9219 | if (gnu_old) |
65444786 EB |
9220 | { |
9221 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)), | |
9222 | TREE_TYPE (gnu_new)); | |
1e55d29a EB |
9223 | if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old))) |
9224 | update_profiles_with (TREE_TYPE (gnu_old)); | |
65444786 EB |
9225 | if (DECL_TAFT_TYPE_P (gnu_old)) |
9226 | used_types_insert (TREE_TYPE (gnu_new)); | |
9227 | } | |
a1ab4c31 | 9228 | } |
ce2d0ce2 | 9229 | |
a1ab4c31 AC |
9230 | /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present. |
9231 | We make two passes, one to elaborate anything other than bodies (but | |
9232 | we declare a function if there was no spec). The second pass | |
9233 | elaborates the bodies. | |
9234 | ||
a1ab4c31 AC |
9235 | We make a complete pass through both lists if PASS1P is true, then make |
9236 | the second pass over both lists if PASS2P is true. The lists usually | |
9237 | correspond to the public and private parts of a package. */ | |
9238 | ||
9239 | static void | |
9240 | process_decls (List_Id gnat_decls, List_Id gnat_decls2, | |
6d16658d | 9241 | bool pass1p, bool pass2p) |
a1ab4c31 AC |
9242 | { |
9243 | List_Id gnat_decl_array[2]; | |
9244 | Node_Id gnat_decl; | |
9245 | int i; | |
9246 | ||
9247 | gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2; | |
9248 | ||
9249 | if (pass1p) | |
9250 | for (i = 0; i <= 1; i++) | |
9251 | if (Present (gnat_decl_array[i])) | |
9252 | for (gnat_decl = First (gnat_decl_array[i]); | |
6d16658d BD |
9253 | Present (gnat_decl); |
9254 | gnat_decl = Next (gnat_decl)) | |
a1ab4c31 AC |
9255 | { |
9256 | /* For package specs, we recurse inside the declarations, | |
9257 | thus taking the two pass approach inside the boundary. */ | |
9258 | if (Nkind (gnat_decl) == N_Package_Declaration | |
9259 | && (Nkind (Specification (gnat_decl) | |
9260 | == N_Package_Specification))) | |
9261 | process_decls (Visible_Declarations (Specification (gnat_decl)), | |
9262 | Private_Declarations (Specification (gnat_decl)), | |
6d16658d | 9263 | true, false); |
a1ab4c31 AC |
9264 | |
9265 | /* Similarly for any declarations in the actions of a | |
9266 | freeze node. */ | |
9267 | else if (Nkind (gnat_decl) == N_Freeze_Entity) | |
9268 | { | |
9269 | process_freeze_entity (gnat_decl); | |
6d16658d | 9270 | process_decls (Actions (gnat_decl), Empty, true, false); |
a1ab4c31 AC |
9271 | } |
9272 | ||
9273 | /* Package bodies with freeze nodes get their elaboration deferred | |
9274 | until the freeze node, but the code must be placed in the right | |
9275 | place, so record the code position now. */ | |
9276 | else if (Nkind (gnat_decl) == N_Package_Body | |
9277 | && Present (Freeze_Node (Corresponding_Spec (gnat_decl)))) | |
9278 | record_code_position (gnat_decl); | |
9279 | ||
1e17ef87 | 9280 | else if (Nkind (gnat_decl) == N_Package_Body_Stub |
a1ab4c31 AC |
9281 | && Present (Library_Unit (gnat_decl)) |
9282 | && Present (Freeze_Node | |
9283 | (Corresponding_Spec | |
9284 | (Proper_Body (Unit | |
9285 | (Library_Unit (gnat_decl))))))) | |
9286 | record_code_position | |
9287 | (Proper_Body (Unit (Library_Unit (gnat_decl)))); | |
9288 | ||
4e279bd9 EB |
9289 | /* We defer most subprogram bodies to the second pass. For bodies |
9290 | that act as their own specs and stubs, the entity itself must be | |
9291 | elaborated in the first pass, because it may be used in other | |
9292 | declarations. */ | |
a1ab4c31 AC |
9293 | else if (Nkind (gnat_decl) == N_Subprogram_Body) |
9294 | { | |
9295 | if (Acts_As_Spec (gnat_decl)) | |
9296 | { | |
4e279bd9 | 9297 | Entity_Id gnat_subprog = Defining_Entity (gnat_decl); |
a1ab4c31 | 9298 | |
4e279bd9 EB |
9299 | if (!Is_Generic_Subprogram (gnat_subprog)) |
9300 | gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true); | |
a1ab4c31 AC |
9301 | } |
9302 | } | |
1e17ef87 | 9303 | |
a1ab4c31 AC |
9304 | else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub) |
9305 | { | |
4e279bd9 | 9306 | Entity_Id gnat_subprog |
1e17ef87 | 9307 | = Defining_Entity (Specification (gnat_decl)); |
a1ab4c31 | 9308 | |
4e279bd9 EB |
9309 | if (!Is_Generic_Subprogram (gnat_subprog) |
9310 | && Ekind (gnat_subprog) != E_Subprogram_Body) | |
9311 | gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true); | |
1e17ef87 | 9312 | } |
a1ab4c31 AC |
9313 | |
9314 | /* Concurrent stubs stand for the corresponding subprogram bodies, | |
9315 | which are deferred like other bodies. */ | |
9316 | else if (Nkind (gnat_decl) == N_Task_Body_Stub | |
9317 | || Nkind (gnat_decl) == N_Protected_Body_Stub) | |
9318 | ; | |
1e17ef87 | 9319 | |
1d4b96e0 AC |
9320 | /* Renamed subprograms may not be elaborated yet at this point |
9321 | since renamings do not trigger freezing. Wait for the second | |
9322 | pass to take care of them. */ | |
9323 | else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration) | |
9324 | ; | |
9325 | ||
a1ab4c31 AC |
9326 | else |
9327 | add_stmt (gnat_to_gnu (gnat_decl)); | |
9328 | } | |
9329 | ||
9330 | /* Here we elaborate everything we deferred above except for package bodies, | |
9331 | which are elaborated at their freeze nodes. Note that we must also | |
9332 | go inside things (package specs and freeze nodes) the first pass did. */ | |
9333 | if (pass2p) | |
9334 | for (i = 0; i <= 1; i++) | |
9335 | if (Present (gnat_decl_array[i])) | |
9336 | for (gnat_decl = First (gnat_decl_array[i]); | |
6d16658d BD |
9337 | Present (gnat_decl); |
9338 | gnat_decl = Next (gnat_decl)) | |
a1ab4c31 AC |
9339 | { |
9340 | if (Nkind (gnat_decl) == N_Subprogram_Body | |
9341 | || Nkind (gnat_decl) == N_Subprogram_Body_Stub | |
9342 | || Nkind (gnat_decl) == N_Task_Body_Stub | |
9343 | || Nkind (gnat_decl) == N_Protected_Body_Stub) | |
9344 | add_stmt (gnat_to_gnu (gnat_decl)); | |
9345 | ||
9346 | else if (Nkind (gnat_decl) == N_Package_Declaration | |
9347 | && (Nkind (Specification (gnat_decl) | |
9348 | == N_Package_Specification))) | |
9349 | process_decls (Visible_Declarations (Specification (gnat_decl)), | |
9350 | Private_Declarations (Specification (gnat_decl)), | |
6d16658d | 9351 | false, true); |
a1ab4c31 AC |
9352 | |
9353 | else if (Nkind (gnat_decl) == N_Freeze_Entity) | |
6d16658d | 9354 | process_decls (Actions (gnat_decl), Empty, false, true); |
1d4b96e0 AC |
9355 | |
9356 | else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration) | |
9357 | add_stmt (gnat_to_gnu (gnat_decl)); | |
a1ab4c31 AC |
9358 | } |
9359 | } | |
ce2d0ce2 | 9360 | |
b666e568 | 9361 | /* Make a unary operation of kind CODE using build_unary_op, but guard |
a7c43bbc EB |
9362 | the operation by an overflow check. CODE can be one of NEGATE_EXPR |
9363 | or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually | |
10069d53 EB |
9364 | the operation is to be performed in that type. GNAT_NODE is the gnat |
9365 | node conveying the source location for which the error should be | |
9366 | signaled. */ | |
b666e568 GB |
9367 | |
9368 | static tree | |
10069d53 EB |
9369 | build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand, |
9370 | Node_Id gnat_node) | |
b666e568 | 9371 | { |
a7c43bbc | 9372 | gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR); |
b666e568 | 9373 | |
7d7a1fe8 | 9374 | operand = gnat_protect_expr (operand); |
b666e568 | 9375 | |
1139f2e8 | 9376 | return emit_check (build_binary_op (EQ_EXPR, boolean_type_node, |
b666e568 GB |
9377 | operand, TYPE_MIN_VALUE (gnu_type)), |
9378 | build_unary_op (code, gnu_type, operand), | |
10069d53 | 9379 | CE_Overflow_Check_Failed, gnat_node); |
b666e568 GB |
9380 | } |
9381 | ||
a7c43bbc EB |
9382 | /* Make a binary operation of kind CODE using build_binary_op, but guard |
9383 | the operation by an overflow check. CODE can be one of PLUS_EXPR, | |
9384 | MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result. | |
10069d53 EB |
9385 | Usually the operation is to be performed in that type. GNAT_NODE is |
9386 | the GNAT node conveying the source location for which the error should | |
9387 | be signaled. */ | |
b666e568 GB |
9388 | |
9389 | static tree | |
a7c43bbc | 9390 | build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left, |
10069d53 | 9391 | tree right, Node_Id gnat_node) |
b666e568 | 9392 | { |
3f1be5be | 9393 | const unsigned int precision = TYPE_PRECISION (gnu_type); |
7d7a1fe8 EB |
9394 | tree lhs = gnat_protect_expr (left); |
9395 | tree rhs = gnat_protect_expr (right); | |
b666e568 GB |
9396 | tree type_max = TYPE_MAX_VALUE (gnu_type); |
9397 | tree type_min = TYPE_MIN_VALUE (gnu_type); | |
aa18f65a EB |
9398 | tree gnu_expr, check; |
9399 | int sgn; | |
b666e568 | 9400 | |
3f1be5be AC |
9401 | /* Assert that the precision is a power of 2. */ |
9402 | gcc_assert ((precision & (precision - 1)) == 0); | |
b666e568 | 9403 | |
aa18f65a EB |
9404 | /* Prefer a constant on the RHS to simplify checks. */ |
9405 | if (TREE_CODE (rhs) != INTEGER_CST | |
9406 | && TREE_CODE (lhs) == INTEGER_CST | |
9407 | && (code == PLUS_EXPR || code == MULT_EXPR)) | |
b666e568 | 9408 | { |
a7c43bbc EB |
9409 | tree tmp = lhs; |
9410 | lhs = rhs; | |
9411 | rhs = tmp; | |
4ae39383 GB |
9412 | } |
9413 | ||
3f1be5be AC |
9414 | gnu_expr = build_binary_op (code, gnu_type, lhs, rhs); |
9415 | ||
9416 | /* If we can fold the expression to a constant, just return it. | |
9417 | The caller will deal with overflow, no need to generate a check. */ | |
aa18f65a | 9418 | if (TREE_CODE (gnu_expr) == INTEGER_CST) |
3f1be5be AC |
9419 | return gnu_expr; |
9420 | ||
aa18f65a EB |
9421 | /* If no operand is a constant, we use the generic implementation. */ |
9422 | if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST) | |
b666e568 | 9423 | { |
4e1a2158 EB |
9424 | /* First convert the operands to the result type like build_binary_op. |
9425 | This is where the bias is made explicit for biased types. */ | |
9426 | lhs = convert (gnu_type, lhs); | |
9427 | rhs = convert (gnu_type, rhs); | |
9428 | ||
aa18f65a EB |
9429 | /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */ |
9430 | if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64) | |
f7ebc6a8 | 9431 | { |
aa18f65a | 9432 | tree int64 = gnat_type_for_size (64, 0); |
1f3f64b9 | 9433 | Check_Restriction_No_Dependence_On_System (Name_Arith_64, gnat_node); |
dddf8120 | 9434 | return convert (gnu_type, build_call_n_expr (mulv64_decl, 2, |
aa18f65a EB |
9435 | convert (int64, lhs), |
9436 | convert (int64, rhs))); | |
58e94443 | 9437 | } |
a7c43bbc | 9438 | |
f2d9f95e EB |
9439 | /* Likewise for a 128-bit mult and a 64-bit target. */ |
9440 | else if (code == MULT_EXPR && precision == 128 && BITS_PER_WORD < 128) | |
9441 | { | |
9442 | tree int128 = gnat_type_for_size (128, 0); | |
1f3f64b9 | 9443 | Check_Restriction_No_Dependence_On_System (Name_Arith_128, gnat_node); |
f2d9f95e EB |
9444 | return convert (gnu_type, build_call_n_expr (mulv128_decl, 2, |
9445 | convert (int128, lhs), | |
9446 | convert (int128, rhs))); | |
9447 | } | |
9448 | ||
aa18f65a | 9449 | enum internal_fn icode; |
4ae39383 | 9450 | |
aa18f65a | 9451 | switch (code) |
4ae39383 | 9452 | { |
aa18f65a EB |
9453 | case PLUS_EXPR: |
9454 | icode = IFN_ADD_OVERFLOW; | |
9455 | break; | |
9456 | case MINUS_EXPR: | |
9457 | icode = IFN_SUB_OVERFLOW; | |
9458 | break; | |
9459 | case MULT_EXPR: | |
9460 | icode = IFN_MUL_OVERFLOW; | |
9461 | break; | |
9462 | default: | |
9463 | gcc_unreachable (); | |
4ae39383 | 9464 | } |
aa18f65a EB |
9465 | |
9466 | tree gnu_ctype = build_complex_type (gnu_type); | |
9467 | tree call | |
9468 | = build_call_expr_internal_loc (UNKNOWN_LOCATION, icode, gnu_ctype, 2, | |
9469 | lhs, rhs); | |
9470 | tree tgt = save_expr (call); | |
9471 | gnu_expr = build1 (REALPART_EXPR, gnu_type, tgt); | |
87e25aac EB |
9472 | check = fold_build2 (NE_EXPR, boolean_type_node, |
9473 | build1 (IMAGPART_EXPR, gnu_type, tgt), | |
9474 | build_int_cst (gnu_type, 0)); | |
aa18f65a EB |
9475 | return |
9476 | emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node); | |
4ae39383 | 9477 | } |
b666e568 | 9478 | |
aa18f65a EB |
9479 | /* If one operand is a constant, we expose the overflow condition to enable |
9480 | a subsequent simplication or even elimination. */ | |
b666e568 GB |
9481 | switch (code) |
9482 | { | |
9483 | case PLUS_EXPR: | |
aa18f65a EB |
9484 | sgn = tree_int_cst_sgn (rhs); |
9485 | if (sgn > 0) | |
9486 | /* When rhs > 0, overflow when lhs > type_max - rhs. */ | |
9487 | check = build_binary_op (GT_EXPR, boolean_type_node, lhs, | |
9488 | build_binary_op (MINUS_EXPR, gnu_type, | |
9489 | type_max, rhs)); | |
9490 | else if (sgn < 0) | |
9491 | /* When rhs < 0, overflow when lhs < type_min - rhs. */ | |
9492 | check = build_binary_op (LT_EXPR, boolean_type_node, lhs, | |
9493 | build_binary_op (MINUS_EXPR, gnu_type, | |
9494 | type_min, rhs)); | |
9495 | else | |
9496 | return gnu_expr; | |
b666e568 GB |
9497 | break; |
9498 | ||
9499 | case MINUS_EXPR: | |
aa18f65a EB |
9500 | if (TREE_CODE (lhs) == INTEGER_CST) |
9501 | { | |
9502 | sgn = tree_int_cst_sgn (lhs); | |
9503 | if (sgn > 0) | |
9504 | /* When lhs > 0, overflow when rhs < lhs - type_max. */ | |
9505 | check = build_binary_op (LT_EXPR, boolean_type_node, rhs, | |
9506 | build_binary_op (MINUS_EXPR, gnu_type, | |
9507 | lhs, type_max)); | |
9508 | else if (sgn < 0) | |
9509 | /* When lhs < 0, overflow when rhs > lhs - type_min. */ | |
9510 | check = build_binary_op (GT_EXPR, boolean_type_node, rhs, | |
9511 | build_binary_op (MINUS_EXPR, gnu_type, | |
9512 | lhs, type_min)); | |
9513 | else | |
9514 | return gnu_expr; | |
9515 | } | |
9516 | else | |
9517 | { | |
9518 | sgn = tree_int_cst_sgn (rhs); | |
9519 | if (sgn > 0) | |
9520 | /* When rhs > 0, overflow when lhs < type_min + rhs. */ | |
9521 | check = build_binary_op (LT_EXPR, boolean_type_node, lhs, | |
9522 | build_binary_op (PLUS_EXPR, gnu_type, | |
9523 | type_min, rhs)); | |
9524 | else if (sgn < 0) | |
9525 | /* When rhs < 0, overflow when lhs > type_max + rhs. */ | |
9526 | check = build_binary_op (GT_EXPR, boolean_type_node, lhs, | |
9527 | build_binary_op (PLUS_EXPR, gnu_type, | |
9528 | type_max, rhs)); | |
9529 | else | |
9530 | return gnu_expr; | |
9531 | } | |
b666e568 GB |
9532 | break; |
9533 | ||
9534 | case MULT_EXPR: | |
aa18f65a EB |
9535 | sgn = tree_int_cst_sgn (rhs); |
9536 | if (sgn > 0) | |
9537 | { | |
9538 | if (integer_onep (rhs)) | |
9539 | return gnu_expr; | |
9540 | ||
9541 | tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs); | |
9542 | tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs); | |
9543 | ||
9544 | /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */ | |
9545 | check | |
9546 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, | |
9547 | build_binary_op (LT_EXPR, boolean_type_node, | |
9548 | lhs, lb), | |
9549 | build_binary_op (GT_EXPR, boolean_type_node, | |
9550 | lhs, ub)); | |
9551 | } | |
9552 | else if (sgn < 0) | |
9553 | { | |
9554 | tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs); | |
9555 | tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs); | |
9556 | ||
9557 | if (integer_minus_onep (rhs)) | |
9558 | /* When rhs == -1, overflow if lhs == type_min. */ | |
9559 | check | |
9560 | = build_binary_op (EQ_EXPR, boolean_type_node, lhs, type_min); | |
9561 | else | |
9562 | /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */ | |
9563 | check | |
9564 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, | |
9565 | build_binary_op (LT_EXPR, boolean_type_node, | |
9566 | lhs, lb), | |
9567 | build_binary_op (GT_EXPR, boolean_type_node, | |
9568 | lhs, ub)); | |
9569 | } | |
9570 | else | |
9571 | return gnu_expr; | |
b666e568 GB |
9572 | break; |
9573 | ||
9574 | default: | |
7c775aca | 9575 | gcc_unreachable (); |
b666e568 GB |
9576 | } |
9577 | ||
10069d53 | 9578 | return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node); |
b666e568 GB |
9579 | } |
9580 | ||
4e9a8214 EB |
9581 | /* GNU_COND contains the condition corresponding to an index, overflow or |
9582 | range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR | |
9583 | if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true. | |
9584 | REASON is the code that says why the exception is raised. GNAT_NODE is | |
9585 | the node conveying the source location for which the error should be | |
9586 | signaled. | |
9587 | ||
9588 | We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR, | |
9589 | overwriting the setting inherited from the call statement, on the ground | |
9590 | that the expression need not be evaluated just for the check. However | |
9591 | that's incorrect because, in the GCC type system, its value is presumed | |
9592 | to be valid so its comparison against the type bounds always yields true | |
9593 | and, therefore, could be done without evaluating it; given that it can | |
9594 | be a computation that overflows the bounds, the language may require the | |
9595 | check to fail and thus the expression to be evaluated in this case. */ | |
a1ab4c31 AC |
9596 | |
9597 | static tree | |
10069d53 | 9598 | emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node) |
a1ab4c31 | 9599 | { |
10069d53 EB |
9600 | tree gnu_call |
9601 | = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error); | |
4e9a8214 EB |
9602 | return |
9603 | fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond, | |
9604 | build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call, | |
9a1bdc31 EB |
9605 | SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr)) |
9606 | ? build_real (TREE_TYPE (gnu_expr), dconst0) | |
9607 | : build_int_cst (TREE_TYPE (gnu_expr), 0)), | |
4e9a8214 | 9608 | gnu_expr); |
a1ab4c31 | 9609 | } |
ce2d0ce2 | 9610 | |
1e17ef87 | 9611 | /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow |
815b5368 EB |
9612 | checks if OVERFLOW_P is true. If TRUNCATE_P is true, do a fp-to-integer |
9613 | conversion with truncation, otherwise round. GNAT_NODE is the GNAT node | |
9614 | conveying the source location for which the error should be signaled. */ | |
a1ab4c31 AC |
9615 | |
9616 | static tree | |
a4f7374e | 9617 | convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflow_p, |
815b5368 | 9618 | bool truncate_p, Node_Id gnat_node) |
a1ab4c31 AC |
9619 | { |
9620 | tree gnu_type = get_unpadded_type (gnat_type); | |
a1ab4c31 | 9621 | tree gnu_base_type = get_base_type (gnu_type); |
a4f7374e EB |
9622 | tree gnu_in_type = TREE_TYPE (gnu_expr); |
9623 | tree gnu_in_base_type = get_base_type (gnu_in_type); | |
a1ab4c31 AC |
9624 | tree gnu_result = gnu_expr; |
9625 | ||
1eb58520 AC |
9626 | /* If we are not doing any checks, the output is an integral type and the |
9627 | input is not a floating-point type, just do the conversion. This is | |
9628 | required for packed array types and is simpler in all cases anyway. */ | |
815b5368 | 9629 | if (!overflow_p |
1eb58520 | 9630 | && INTEGRAL_TYPE_P (gnu_base_type) |
a4f7374e | 9631 | && !FLOAT_TYPE_P (gnu_in_base_type)) |
a1ab4c31 AC |
9632 | return convert (gnu_type, gnu_expr); |
9633 | ||
a4f7374e EB |
9634 | /* If the mode of the input base type is larger, then converting to it below |
9635 | may pessimize the final conversion step, for example generate a libcall | |
9636 | instead of a simple instruction, so use a narrower type in this case. */ | |
9637 | if (TYPE_MODE (gnu_in_base_type) != TYPE_MODE (gnu_in_type) | |
9638 | && !(TREE_CODE (gnu_in_type) == INTEGER_TYPE | |
9639 | && TYPE_BIASED_REPRESENTATION_P (gnu_in_type))) | |
9640 | gnu_in_base_type = gnat_type_for_mode (TYPE_MODE (gnu_in_type), | |
9641 | TYPE_UNSIGNED (gnu_in_type)); | |
9642 | ||
9643 | /* First convert the expression to the base type. This will never generate | |
9644 | code, but makes the tests below simpler. But don't do this if converting | |
9645 | from an integer type to an unconstrained array type since then we need to | |
9646 | get the bounds from the original (unpacked) type. */ | |
a1ab4c31 | 9647 | if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE) |
a4f7374e | 9648 | gnu_result = convert (gnu_in_base_type, gnu_result); |
a1ab4c31 | 9649 | |
a4f7374e EB |
9650 | /* If overflow checks are requested, we need to be sure the result will fit |
9651 | in the output base type. But don't do this if the input is integer and | |
9652 | the output floating-point. */ | |
9653 | if (overflow_p | |
9654 | && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_base_type))) | |
a1ab4c31 AC |
9655 | { |
9656 | /* Ensure GNU_EXPR only gets evaluated once. */ | |
7d7a1fe8 | 9657 | tree gnu_input = gnat_protect_expr (gnu_result); |
bf6490b5 | 9658 | tree gnu_cond = boolean_false_node; |
a4f7374e EB |
9659 | tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_base_type); |
9660 | tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_base_type); | |
a1ab4c31 | 9661 | tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type); |
815b5368 EB |
9662 | tree gnu_out_ub |
9663 | = (TREE_CODE (gnu_base_type) == INTEGER_TYPE | |
9664 | && TYPE_MODULAR_P (gnu_base_type)) | |
9665 | ? fold_build2 (MINUS_EXPR, gnu_base_type, | |
9666 | TYPE_MODULUS (gnu_base_type), | |
9667 | build_int_cst (gnu_base_type, 1)) | |
9668 | : TYPE_MAX_VALUE (gnu_base_type); | |
a1ab4c31 AC |
9669 | |
9670 | /* Convert the lower bounds to signed types, so we're sure we're | |
9671 | comparing them properly. Likewise, convert the upper bounds | |
9672 | to unsigned types. */ | |
a4f7374e EB |
9673 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
9674 | && TYPE_UNSIGNED (gnu_in_base_type)) | |
9a1bdc31 | 9675 | gnu_in_lb |
a4f7374e | 9676 | = convert (gnat_signed_type_for (gnu_in_base_type), gnu_in_lb); |
a1ab4c31 | 9677 | |
a4f7374e EB |
9678 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
9679 | && !TYPE_UNSIGNED (gnu_in_base_type)) | |
9a1bdc31 | 9680 | gnu_in_ub |
a4f7374e | 9681 | = convert (gnat_unsigned_type_for (gnu_in_base_type), gnu_in_ub); |
a1ab4c31 AC |
9682 | |
9683 | if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type)) | |
9a1bdc31 EB |
9684 | gnu_out_lb |
9685 | = convert (gnat_signed_type_for (gnu_base_type), gnu_out_lb); | |
a1ab4c31 AC |
9686 | |
9687 | if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type)) | |
9a1bdc31 EB |
9688 | gnu_out_ub |
9689 | = convert (gnat_unsigned_type_for (gnu_base_type), gnu_out_ub); | |
a1ab4c31 AC |
9690 | |
9691 | /* Check each bound separately and only if the result bound | |
9692 | is tighter than the bound on the input type. Note that all the | |
9693 | types are base types, so the bounds must be constant. Also, | |
9694 | the comparison is done in the base type of the input, which | |
9695 | always has the proper signedness. First check for input | |
9696 | integer (which means output integer), output float (which means | |
9697 | both float), or mixed, in which case we always compare. | |
9698 | Note that we have to do the comparison which would *fail* in the | |
9699 | case of an error since if it's an FP comparison and one of the | |
9700 | values is a NaN or Inf, the comparison will fail. */ | |
a4f7374e | 9701 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
a1ab4c31 AC |
9702 | ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb) |
9703 | : (FLOAT_TYPE_P (gnu_base_type) | |
8cb41028 RS |
9704 | ? real_less (&TREE_REAL_CST (gnu_in_lb), |
9705 | &TREE_REAL_CST (gnu_out_lb)) | |
a1ab4c31 AC |
9706 | : 1)) |
9707 | gnu_cond | |
9708 | = invert_truthvalue | |
1139f2e8 | 9709 | (build_binary_op (GE_EXPR, boolean_type_node, |
a4f7374e | 9710 | gnu_input, convert (gnu_in_base_type, |
a1ab4c31 AC |
9711 | gnu_out_lb))); |
9712 | ||
a4f7374e | 9713 | if (INTEGRAL_TYPE_P (gnu_in_base_type) |
a1ab4c31 AC |
9714 | ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub) |
9715 | : (FLOAT_TYPE_P (gnu_base_type) | |
8cb41028 | 9716 | ? real_less (&TREE_REAL_CST (gnu_out_ub), |
21288963 | 9717 | &TREE_REAL_CST (gnu_in_ub)) |
a1ab4c31 AC |
9718 | : 1)) |
9719 | gnu_cond | |
1139f2e8 | 9720 | = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond, |
a1ab4c31 | 9721 | invert_truthvalue |
1139f2e8 | 9722 | (build_binary_op (LE_EXPR, boolean_type_node, |
a1ab4c31 | 9723 | gnu_input, |
a4f7374e | 9724 | convert (gnu_in_base_type, |
a1ab4c31 AC |
9725 | gnu_out_ub)))); |
9726 | ||
9727 | if (!integer_zerop (gnu_cond)) | |
10069d53 EB |
9728 | gnu_result = emit_check (gnu_cond, gnu_input, |
9729 | CE_Overflow_Check_Failed, gnat_node); | |
a1ab4c31 AC |
9730 | } |
9731 | ||
9732 | /* Now convert to the result base type. If this is a non-truncating | |
9733 | float-to-integer conversion, round. */ | |
24228312 | 9734 | if (INTEGRAL_TYPE_P (gnu_base_type) |
a4f7374e EB |
9735 | && FLOAT_TYPE_P (gnu_in_base_type) |
9736 | && !truncate_p) | |
a1ab4c31 AC |
9737 | { |
9738 | REAL_VALUE_TYPE half_minus_pred_half, pred_half; | |
ced57283 | 9739 | tree gnu_conv, gnu_zero, gnu_comp, calc_type; |
a1ab4c31 AC |
9740 | tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half; |
9741 | const struct real_format *fmt; | |
9742 | ||
9743 | /* The following calculations depend on proper rounding to even | |
24228312 | 9744 | of each arithmetic operation. In order to prevent excess |
1e17ef87 EB |
9745 | precision from spoiling this property, use the widest hardware |
9746 | floating-point type if FP_ARITH_MAY_WIDEN is true. */ | |
9747 | calc_type | |
a4f7374e | 9748 | = fp_arith_may_widen ? longest_float_type_node : gnu_in_base_type; |
a1ab4c31 | 9749 | |
1e17ef87 | 9750 | /* Compute the exact value calc_type'Pred (0.5) at compile time. */ |
a1ab4c31 AC |
9751 | fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type)); |
9752 | real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type)); | |
5c88ea94 RS |
9753 | real_arithmetic (&pred_half, MINUS_EXPR, &dconsthalf, |
9754 | &half_minus_pred_half); | |
a1ab4c31 AC |
9755 | gnu_pred_half = build_real (calc_type, pred_half); |
9756 | ||
9757 | /* If the input is strictly negative, subtract this value | |
ced57283 | 9758 | and otherwise add it from the input. For 0.5, the result |
1e17ef87 | 9759 | is exactly between 1.0 and the machine number preceding 1.0 |
ced57283 | 9760 | (for calc_type). Since the last bit of 1.0 is even, this 0.5 |
1e17ef87 | 9761 | will round to 1.0, while all other number with an absolute |
ced57283 | 9762 | value less than 0.5 round to 0.0. For larger numbers exactly |
1e17ef87 EB |
9763 | halfway between integers, rounding will always be correct as |
9764 | the true mathematical result will be closer to the higher | |
ced57283 | 9765 | integer compared to the lower one. So, this constant works |
1e17ef87 EB |
9766 | for all floating-point numbers. |
9767 | ||
9768 | The reason to use the same constant with subtract/add instead | |
9769 | of a positive and negative constant is to allow the comparison | |
9770 | to be scheduled in parallel with retrieval of the constant and | |
9771 | conversion of the input to the calc_type (if necessary). */ | |
a1ab4c31 | 9772 | |
a4f7374e | 9773 | gnu_zero = build_real (gnu_in_base_type, dconst0); |
7d7a1fe8 | 9774 | gnu_result = gnat_protect_expr (gnu_result); |
ced57283 EB |
9775 | gnu_conv = convert (calc_type, gnu_result); |
9776 | gnu_comp | |
1139f2e8 | 9777 | = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero); |
a1ab4c31 | 9778 | gnu_add_pred_half |
ced57283 | 9779 | = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half); |
a1ab4c31 | 9780 | gnu_subtract_pred_half |
ced57283 EB |
9781 | = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half); |
9782 | gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp, | |
9783 | gnu_add_pred_half, gnu_subtract_pred_half); | |
a1ab4c31 AC |
9784 | } |
9785 | ||
9786 | if (TREE_CODE (gnu_base_type) == INTEGER_TYPE | |
9787 | && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type) | |
9788 | && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF) | |
9789 | gnu_result = unchecked_convert (gnu_base_type, gnu_result, false); | |
9790 | else | |
9791 | gnu_result = convert (gnu_base_type, gnu_result); | |
9792 | ||
a80e0583 EB |
9793 | /* If this is a conversion between an integer type larger than a word and a |
9794 | floating-point type, then declare the dependence on the libgcc routine. */ | |
9795 | if ((INTEGRAL_TYPE_P (gnu_in_base_type) | |
9796 | && TYPE_PRECISION (gnu_in_base_type) > BITS_PER_WORD | |
9797 | && FLOAT_TYPE_P (gnu_base_type)) | |
9798 | || (FLOAT_TYPE_P (gnu_in_base_type) | |
9799 | && INTEGRAL_TYPE_P (gnu_base_type) | |
9800 | && TYPE_PRECISION (gnu_base_type) > BITS_PER_WORD)) | |
9801 | Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node); | |
9802 | ||
a1ab4c31 AC |
9803 | return convert (gnu_type, gnu_result); |
9804 | } | |
ce2d0ce2 | 9805 | |
a1ab4c31 AC |
9806 | /* Return true if GNU_EXPR can be directly addressed. This is the case |
9807 | unless it is an expression involving computation or if it involves a | |
9808 | reference to a bitfield or to an object not sufficiently aligned for | |
9809 | its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can | |
9810 | be directly addressed as an object of this type. | |
9811 | ||
9812 | *** Notes on addressability issues in the Ada compiler *** | |
9813 | ||
9814 | This predicate is necessary in order to bridge the gap between Gigi | |
9815 | and the middle-end about addressability of GENERIC trees. A tree | |
9816 | is said to be addressable if it can be directly addressed, i.e. if | |
9817 | its address can be taken, is a multiple of the type's alignment on | |
9818 | strict-alignment architectures and returns the first storage unit | |
9819 | assigned to the object represented by the tree. | |
9820 | ||
9821 | In the C family of languages, everything is in practice addressable | |
9822 | at the language level, except for bit-fields. This means that these | |
9823 | compilers will take the address of any tree that doesn't represent | |
9824 | a bit-field reference and expect the result to be the first storage | |
9825 | unit assigned to the object. Even in cases where this will result | |
9826 | in unaligned accesses at run time, nothing is supposed to be done | |
9827 | and the program is considered as erroneous instead (see PR c/18287). | |
9828 | ||
9829 | The implicit assumptions made in the middle-end are in keeping with | |
9830 | the C viewpoint described above: | |
9831 | - the address of a bit-field reference is supposed to be never | |
9832 | taken; the compiler (generally) will stop on such a construct, | |
9833 | - any other tree is addressable if it is formally addressable, | |
9834 | i.e. if it is formally allowed to be the operand of ADDR_EXPR. | |
9835 | ||
9836 | In Ada, the viewpoint is the opposite one: nothing is addressable | |
9837 | at the language level unless explicitly declared so. This means | |
9838 | that the compiler will both make sure that the trees representing | |
9839 | references to addressable ("aliased" in Ada parlance) objects are | |
9840 | addressable and make no real attempts at ensuring that the trees | |
9841 | representing references to non-addressable objects are addressable. | |
9842 | ||
9843 | In the first case, Ada is effectively equivalent to C and handing | |
9844 | down the direct result of applying ADDR_EXPR to these trees to the | |
9845 | middle-end works flawlessly. In the second case, Ada cannot afford | |
9846 | to consider the program as erroneous if the address of trees that | |
9847 | are not addressable is requested for technical reasons, unlike C; | |
9848 | as a consequence, the Ada compiler must arrange for either making | |
9849 | sure that this address is not requested in the middle-end or for | |
9850 | compensating by inserting temporaries if it is requested in Gigi. | |
9851 | ||
9852 | The first goal can be achieved because the middle-end should not | |
9853 | request the address of non-addressable trees on its own; the only | |
9854 | exception is for the invocation of low-level block operations like | |
9855 | memcpy, for which the addressability requirements are lower since | |
9856 | the type's alignment can be disregarded. In practice, this means | |
9857 | that Gigi must make sure that such operations cannot be applied to | |
9858 | non-BLKmode bit-fields. | |
9859 | ||
5a19bc0a EB |
9860 | The second goal is achieved by means of the addressable_p predicate, |
9861 | which computes whether a temporary must be inserted by Gigi when the | |
9862 | address of a tree is requested; if so, the address of the temporary | |
9863 | will be used in lieu of that of the original tree and some glue code | |
9864 | generated to connect everything together. */ | |
a1ab4c31 AC |
9865 | |
9866 | static bool | |
9867 | addressable_p (tree gnu_expr, tree gnu_type) | |
9868 | { | |
169afcb9 EB |
9869 | /* For an integral type, the size of the actual type of the object may not |
9870 | be greater than that of the expected type, otherwise an indirect access | |
9871 | in the latter type wouldn't correctly set all the bits of the object. */ | |
9872 | if (gnu_type | |
9873 | && INTEGRAL_TYPE_P (gnu_type) | |
9874 | && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr))) | |
9875 | return false; | |
9876 | ||
9877 | /* The size of the actual type of the object may not be smaller than that | |
9878 | of the expected type, otherwise an indirect access in the latter type | |
9879 | would be larger than the object. But only record types need to be | |
9880 | considered in practice for this case. */ | |
a1ab4c31 AC |
9881 | if (gnu_type |
9882 | && TREE_CODE (gnu_type) == RECORD_TYPE | |
169afcb9 | 9883 | && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type)) |
a1ab4c31 AC |
9884 | return false; |
9885 | ||
9886 | switch (TREE_CODE (gnu_expr)) | |
9887 | { | |
9888 | case VAR_DECL: | |
9889 | case PARM_DECL: | |
9890 | case FUNCTION_DECL: | |
9891 | case RESULT_DECL: | |
9892 | /* All DECLs are addressable: if they are in a register, we can force | |
9893 | them to memory. */ | |
9894 | return true; | |
9895 | ||
9896 | case UNCONSTRAINED_ARRAY_REF: | |
9897 | case INDIRECT_REF: | |
0b3467c4 | 9898 | /* Taking the address of a dereference yields the original pointer. */ |
42c08997 EB |
9899 | return true; |
9900 | ||
a1ab4c31 AC |
9901 | case STRING_CST: |
9902 | case INTEGER_CST: | |
1edbeb15 | 9903 | case REAL_CST: |
0b3467c4 EB |
9904 | /* Taking the address yields a pointer to the constant pool. */ |
9905 | return true; | |
9906 | ||
9907 | case CONSTRUCTOR: | |
9908 | /* Taking the address of a static constructor yields a pointer to the | |
9909 | tree constant pool. */ | |
9910 | return TREE_STATIC (gnu_expr) ? true : false; | |
9911 | ||
a1ab4c31 | 9912 | case NULL_EXPR: |
1edbeb15 | 9913 | case ADDR_EXPR: |
a1ab4c31 AC |
9914 | case SAVE_EXPR: |
9915 | case CALL_EXPR: | |
42c08997 EB |
9916 | case PLUS_EXPR: |
9917 | case MINUS_EXPR: | |
9f4afcd4 EB |
9918 | case BIT_IOR_EXPR: |
9919 | case BIT_XOR_EXPR: | |
9920 | case BIT_AND_EXPR: | |
9921 | case BIT_NOT_EXPR: | |
42c08997 EB |
9922 | /* All rvalues are deemed addressable since taking their address will |
9923 | force a temporary to be created by the middle-end. */ | |
a1ab4c31 AC |
9924 | return true; |
9925 | ||
0b3467c4 EB |
9926 | case COMPOUND_EXPR: |
9927 | /* The address of a compound expression is that of its 2nd operand. */ | |
9928 | return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type); | |
9929 | ||
a1ab4c31 AC |
9930 | case COND_EXPR: |
9931 | /* We accept &COND_EXPR as soon as both operands are addressable and | |
9932 | expect the outcome to be the address of the selected operand. */ | |
9933 | return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE) | |
9934 | && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE)); | |
9935 | ||
9936 | case COMPONENT_REF: | |
9937 | return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1)) | |
9938 | /* Even with DECL_BIT_FIELD cleared, we have to ensure that | |
9939 | the field is sufficiently aligned, in case it is subject | |
9940 | to a pragma Component_Alignment. But we don't need to | |
9941 | check the alignment of the containing record, as it is | |
9942 | guaranteed to be not smaller than that of its most | |
9943 | aligned field that is not a bit-field. */ | |
1e17ef87 | 9944 | && (!STRICT_ALIGNMENT |
a1ab4c31 AC |
9945 | || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1)) |
9946 | >= TYPE_ALIGN (TREE_TYPE (gnu_expr)))) | |
9947 | /* The field of a padding record is always addressable. */ | |
3c157c27 | 9948 | || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))) |
a1ab4c31 AC |
9949 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); |
9950 | ||
9951 | case ARRAY_REF: case ARRAY_RANGE_REF: | |
9952 | case REALPART_EXPR: case IMAGPART_EXPR: | |
9953 | case NOP_EXPR: | |
9954 | return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE); | |
9955 | ||
9956 | case CONVERT_EXPR: | |
9957 | return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr)) | |
9958 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); | |
9959 | ||
9960 | case VIEW_CONVERT_EXPR: | |
9961 | { | |
9962 | /* This is addressable if we can avoid a copy. */ | |
9963 | tree type = TREE_TYPE (gnu_expr); | |
9964 | tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0)); | |
9965 | return (((TYPE_MODE (type) == TYPE_MODE (inner_type) | |
9966 | && (!STRICT_ALIGNMENT | |
9967 | || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type) | |
9968 | || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT)) | |
9969 | || ((TYPE_MODE (type) == BLKmode | |
9970 | || TYPE_MODE (inner_type) == BLKmode) | |
9971 | && (!STRICT_ALIGNMENT | |
9972 | || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type) | |
9973 | || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT | |
9974 | || TYPE_ALIGN_OK (type) | |
9975 | || TYPE_ALIGN_OK (inner_type)))) | |
9976 | && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE)); | |
9977 | } | |
9978 | ||
9979 | default: | |
9980 | return false; | |
9981 | } | |
9982 | } | |
ce2d0ce2 | 9983 | |
d5ebeb8c EB |
9984 | /* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype. |
9985 | If a Freeze node exists for the entity, delay the bulk of the processing. | |
9986 | Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */ | |
a1ab4c31 AC |
9987 | |
9988 | void | |
9989 | process_type (Entity_Id gnat_entity) | |
9990 | { | |
9991 | tree gnu_old | |
d5ebeb8c EB |
9992 | = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE; |
9993 | ||
9994 | /* If we are to delay elaboration of this type, just do any elaboration | |
9995 | needed for expressions within the declaration and make a dummy node | |
9996 | for it and its Full_View (if any), in case something points to it. | |
9997 | Do not do this if it has already been done (the only way that can | |
9998 | happen is if the private completion is also delayed). */ | |
9999 | if (Present (Freeze_Node (gnat_entity))) | |
a1ab4c31 AC |
10000 | { |
10001 | elaborate_entity (gnat_entity); | |
10002 | ||
10003 | if (!gnu_old) | |
1e17ef87 | 10004 | { |
10069d53 | 10005 | tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity)); |
a1ab4c31 | 10006 | save_gnu_tree (gnat_entity, gnu_decl, false); |
7ed9919d | 10007 | if (Is_Incomplete_Or_Private_Type (gnat_entity) |
a1ab4c31 | 10008 | && Present (Full_View (gnat_entity))) |
65444786 EB |
10009 | { |
10010 | if (Has_Completion_In_Body (gnat_entity)) | |
10011 | DECL_TAFT_TYPE_P (gnu_decl) = 1; | |
10012 | save_gnu_tree (Full_View (gnat_entity), gnu_decl, false); | |
10013 | } | |
a1ab4c31 AC |
10014 | } |
10015 | ||
10016 | return; | |
10017 | } | |
10018 | ||
d5ebeb8c EB |
10019 | /* If we saved away a dummy type for this node, it means that this made the |
10020 | type that corresponds to the full type of an incomplete type. Clear that | |
5a4916be EB |
10021 | type for now and then update the type in the pointers below. But, if the |
10022 | saved type is not dummy, it very likely means that we have a use before | |
10023 | declaration for the type in the tree, what we really cannot handle. */ | |
a1ab4c31 AC |
10024 | if (gnu_old) |
10025 | { | |
10026 | gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL | |
10027 | && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))); | |
10028 | ||
10029 | save_gnu_tree (gnat_entity, NULL_TREE, false); | |
10030 | } | |
10031 | ||
10032 | /* Now fully elaborate the type. */ | |
d5ebeb8c | 10033 | tree gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, true); |
a1ab4c31 AC |
10034 | gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL); |
10035 | ||
65444786 EB |
10036 | /* If we have an old type and we've made pointers to this type, update those |
10037 | pointers. If this is a Taft amendment type in the main unit, we need to | |
10038 | mark the type as used since other units referencing it don't see the full | |
10039 | declaration and, therefore, cannot mark it as used themselves. */ | |
a1ab4c31 | 10040 | if (gnu_old) |
65444786 EB |
10041 | { |
10042 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)), | |
10043 | TREE_TYPE (gnu_new)); | |
10044 | if (DECL_TAFT_TYPE_P (gnu_old)) | |
10045 | used_types_insert (TREE_TYPE (gnu_new)); | |
10046 | } | |
a1ab4c31 AC |
10047 | |
10048 | /* If this is a record type corresponding to a task or protected type | |
10049 | that is a completion of an incomplete type, perform a similar update | |
1e17ef87 | 10050 | on the type. ??? Including protected types here is a guess. */ |
4ec7c4ec | 10051 | if (Is_Record_Type (gnat_entity) |
a1ab4c31 AC |
10052 | && Is_Concurrent_Record_Type (gnat_entity) |
10053 | && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity))) | |
10054 | { | |
10055 | tree gnu_task_old | |
10056 | = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)); | |
10057 | ||
10058 | save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity), | |
10059 | NULL_TREE, false); | |
10060 | save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity), | |
10061 | gnu_new, false); | |
10062 | ||
10063 | update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)), | |
10064 | TREE_TYPE (gnu_new)); | |
10065 | } | |
10066 | } | |
ce2d0ce2 | 10067 | |
b1b2b511 EB |
10068 | /* Subroutine of assoc_to_constructor: VALUES is a list of field associations, |
10069 | some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the | |
10070 | associations that are from RECORD_TYPE. If we see an internal record, make | |
10071 | a recursive call to fill it in as well. */ | |
10072 | ||
10073 | static tree | |
10074 | extract_values (tree values, tree record_type) | |
10075 | { | |
10076 | vec<constructor_elt, va_gc> *v = NULL; | |
10077 | tree field; | |
10078 | ||
10079 | for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field)) | |
10080 | { | |
10081 | tree tem, value = NULL_TREE; | |
10082 | ||
10083 | /* _Parent is an internal field, but may have values in the aggregate, | |
10084 | so check for values first. */ | |
10085 | if ((tem = purpose_member (field, values))) | |
10086 | { | |
10087 | value = TREE_VALUE (tem); | |
10088 | TREE_ADDRESSABLE (tem) = 1; | |
10089 | } | |
10090 | ||
10091 | else if (DECL_INTERNAL_P (field)) | |
10092 | { | |
10093 | value = extract_values (values, TREE_TYPE (field)); | |
10094 | if (TREE_CODE (value) == CONSTRUCTOR | |
10095 | && vec_safe_is_empty (CONSTRUCTOR_ELTS (value))) | |
10096 | value = NULL_TREE; | |
10097 | } | |
10098 | else | |
10099 | /* If we have a record subtype, the names will match, but not the | |
10100 | actual FIELD_DECLs. */ | |
10101 | for (tem = values; tem; tem = TREE_CHAIN (tem)) | |
10102 | if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field)) | |
10103 | { | |
10104 | value = convert (TREE_TYPE (field), TREE_VALUE (tem)); | |
10105 | TREE_ADDRESSABLE (tem) = 1; | |
10106 | } | |
10107 | ||
10108 | if (!value) | |
10109 | continue; | |
10110 | ||
10111 | CONSTRUCTOR_APPEND_ELT (v, field, value); | |
10112 | } | |
10113 | ||
10114 | return gnat_build_constructor (record_type, v); | |
10115 | } | |
10116 | ||
42acad07 EB |
10117 | /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the |
10118 | front of the Component_Associations of an N_Aggregate and GNU_TYPE is the | |
10119 | GCC type of the corresponding record type. Return the CONSTRUCTOR. */ | |
a1ab4c31 AC |
10120 | |
10121 | static tree | |
10122 | assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type) | |
10123 | { | |
42acad07 | 10124 | tree gnu_list = NULL_TREE, gnu_result; |
a1ab4c31 AC |
10125 | |
10126 | /* We test for GNU_FIELD being empty in the case where a variant | |
10127 | was the last thing since we don't take things off GNAT_ASSOC in | |
10128 | that case. We check GNAT_ASSOC in case we have a variant, but it | |
10129 | has no fields. */ | |
10130 | ||
42acad07 | 10131 | for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc)) |
a1ab4c31 | 10132 | { |
815b5368 EB |
10133 | const Node_Id gnat_field = First (Choices (gnat_assoc)); |
10134 | const Node_Id gnat_expr = Expression (gnat_assoc); | |
a1ab4c31 AC |
10135 | tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field)); |
10136 | tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc)); | |
10137 | ||
10138 | /* The expander is supposed to put a single component selector name | |
1e17ef87 | 10139 | in every record component association. */ |
a1ab4c31 AC |
10140 | gcc_assert (No (Next (gnat_field))); |
10141 | ||
4ec7c4ec EB |
10142 | /* Ignore discriminants that have Corresponding_Discriminants in tagged |
10143 | types since we'll be setting those fields in the parent subtype. */ | |
10144 | if (Ekind (Entity (gnat_field)) == E_Discriminant | |
10145 | && Present (Corresponding_Discriminant (Entity (gnat_field))) | |
a1ab4c31 AC |
10146 | && Is_Tagged_Type (Scope (Entity (gnat_field)))) |
10147 | continue; | |
10148 | ||
10149 | /* Also ignore discriminants of Unchecked_Unions. */ | |
4ec7c4ec EB |
10150 | if (Ekind (Entity (gnat_field)) == E_Discriminant |
10151 | && Is_Unchecked_Union (gnat_entity)) | |
a1ab4c31 AC |
10152 | continue; |
10153 | ||
815b5368 | 10154 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 | 10155 | |
815b5368 | 10156 | /* Convert to the type of the field. */ |
a1ab4c31 AC |
10157 | gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr); |
10158 | ||
10159 | /* Add the field and expression to the list. */ | |
10160 | gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list); | |
10161 | } | |
10162 | ||
10163 | gnu_result = extract_values (gnu_list, gnu_type); | |
10164 | ||
9abe8b74 MM |
10165 | if (flag_checking) |
10166 | { | |
10167 | /* Verify that every entry in GNU_LIST was used. */ | |
10168 | for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list)) | |
10169 | gcc_assert (TREE_ADDRESSABLE (gnu_list)); | |
10170 | } | |
a1ab4c31 AC |
10171 | |
10172 | return gnu_result; | |
10173 | } | |
10174 | ||
1e17ef87 EB |
10175 | /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is |
10176 | the first element of an array aggregate. It may itself be an aggregate. | |
815b5368 | 10177 | GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate. */ |
a1ab4c31 AC |
10178 | |
10179 | static tree | |
815b5368 | 10180 | pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type) |
a1ab4c31 | 10181 | { |
a1ab4c31 | 10182 | tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type)); |
9771b263 | 10183 | vec<constructor_elt, va_gc> *gnu_expr_vec = NULL; |
a1ab4c31 | 10184 | |
b1b2b511 | 10185 | for (; Present (gnat_expr); gnat_expr = Next (gnat_expr)) |
a1ab4c31 | 10186 | { |
b1b2b511 EB |
10187 | tree gnu_expr; |
10188 | ||
a1ab4c31 AC |
10189 | /* If the expression is itself an array aggregate then first build the |
10190 | innermost constructor if it is part of our array (multi-dimensional | |
10191 | case). */ | |
a1ab4c31 AC |
10192 | if (Nkind (gnat_expr) == N_Aggregate |
10193 | && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE | |
10194 | && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type))) | |
10195 | gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)), | |
815b5368 | 10196 | TREE_TYPE (gnu_array_type)); |
a1ab4c31 AC |
10197 | else |
10198 | { | |
babf697a EB |
10199 | /* If the expression is a conversion to an unconstrained array type, |
10200 | skip it to avoid spilling to memory. */ | |
10201 | if (Nkind (gnat_expr) == N_Type_Conversion | |
10202 | && Is_Array_Type (Etype (gnat_expr)) | |
10203 | && !Is_Constrained (Etype (gnat_expr))) | |
10204 | gnu_expr = gnat_to_gnu (Expression (gnat_expr)); | |
10205 | else | |
10206 | gnu_expr = gnat_to_gnu (gnat_expr); | |
a1ab4c31 | 10207 | |
815b5368 | 10208 | gigi_checking_assert (!Do_Range_Check (gnat_expr)); |
a1ab4c31 AC |
10209 | } |
10210 | ||
0e228dd9 NF |
10211 | CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index, |
10212 | convert (TREE_TYPE (gnu_array_type), gnu_expr)); | |
a1ab4c31 | 10213 | |
8b9aec86 RS |
10214 | gnu_index = int_const_binop (PLUS_EXPR, gnu_index, |
10215 | convert (TREE_TYPE (gnu_index), | |
10216 | integer_one_node)); | |
a1ab4c31 AC |
10217 | } |
10218 | ||
0e228dd9 | 10219 | return gnat_build_constructor (gnu_array_type, gnu_expr_vec); |
a1ab4c31 | 10220 | } |
ce2d0ce2 | 10221 | |
f04b8d69 EB |
10222 | /* Process a N_Validate_Unchecked_Conversion node. */ |
10223 | ||
10224 | static void | |
10225 | validate_unchecked_conversion (Node_Id gnat_node) | |
10226 | { | |
10227 | tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node)); | |
10228 | tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node)); | |
10229 | ||
10230 | /* If the target is a pointer type, see if we are either converting from a | |
10231 | non-pointer or from a pointer to a type with a different alias set and | |
10232 | warn if so, unless the pointer has been marked to alias everything. */ | |
10233 | if (POINTER_TYPE_P (gnu_target_type) | |
10234 | && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type)) | |
10235 | { | |
10236 | tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type) | |
10237 | ? TREE_TYPE (gnu_source_type) | |
10238 | : NULL_TREE; | |
10239 | tree gnu_target_desig_type = TREE_TYPE (gnu_target_type); | |
10240 | alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type); | |
10241 | ||
10242 | if (target_alias_set != 0 | |
10243 | && (!POINTER_TYPE_P (gnu_source_type) | |
10244 | || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type), | |
10245 | target_alias_set))) | |
10246 | { | |
4a29b8d6 | 10247 | post_error_ne ("??possible aliasing problem for type&", |
f04b8d69 EB |
10248 | gnat_node, Target_Type (gnat_node)); |
10249 | post_error ("\\?use -fno-strict-aliasing switch for references", | |
10250 | gnat_node); | |
10251 | post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`", | |
10252 | gnat_node, Target_Type (gnat_node)); | |
10253 | } | |
10254 | } | |
10255 | ||
10256 | /* Likewise if the target is a fat pointer type, but we have no mechanism to | |
10257 | mitigate the problem in this case, so we unconditionally warn. */ | |
10258 | else if (TYPE_IS_FAT_POINTER_P (gnu_target_type)) | |
10259 | { | |
10260 | tree gnu_source_desig_type | |
10261 | = TYPE_IS_FAT_POINTER_P (gnu_source_type) | |
10262 | ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type))) | |
10263 | : NULL_TREE; | |
10264 | tree gnu_target_desig_type | |
10265 | = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type))); | |
10266 | alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type); | |
10267 | ||
10268 | if (target_alias_set != 0 | |
10269 | && (!TYPE_IS_FAT_POINTER_P (gnu_source_type) | |
10270 | || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type), | |
10271 | target_alias_set))) | |
10272 | { | |
4a29b8d6 | 10273 | post_error_ne ("??possible aliasing problem for type&", |
f04b8d69 EB |
10274 | gnat_node, Target_Type (gnat_node)); |
10275 | post_error ("\\?use -fno-strict-aliasing switch for references", | |
10276 | gnat_node); | |
10277 | } | |
10278 | } | |
10279 | } | |
ce2d0ce2 | 10280 | |
fa6fd7b7 AO |
10281 | /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a |
10282 | source code location and false if it doesn't. If CLEAR_COLUMN is | |
10283 | true, set the column information to 0. If DECL is given and SLOC | |
10284 | refers to a File with an instance, map DECL to that instance. */ | |
a1ab4c31 | 10285 | |
ba464315 | 10286 | bool |
fa6fd7b7 AO |
10287 | Sloc_to_locus (Source_Ptr Sloc, location_t *locus, bool clear_column, |
10288 | const_tree decl) | |
a1ab4c31 AC |
10289 | { |
10290 | if (Sloc == No_Location) | |
10291 | return false; | |
10292 | ||
10293 | if (Sloc <= Standard_Location) | |
10294 | { | |
10069d53 | 10295 | *locus = BUILTINS_LOCATION; |
a1ab4c31 AC |
10296 | return false; |
10297 | } | |
a1ab4c31 | 10298 | |
ba464315 | 10299 | Source_File_Index file = Get_Source_File_Index (Sloc); |
b67e2ad8 EB |
10300 | Line_Number_Type line = Get_Logical_Line_Number (Sloc); |
10301 | Column_Number_Type column = (clear_column ? 0 : Get_Column_Number (Sloc)); | |
ba464315 | 10302 | line_map_ordinary *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1); |
b7562769 | 10303 | |
ba464315 EB |
10304 | /* We can have zero if pragma Source_Reference is in effect. */ |
10305 | if (line < 1) | |
10306 | line = 1; | |
a1ab4c31 | 10307 | |
ba464315 | 10308 | /* Translate the location. */ |
b1b2b511 EB |
10309 | *locus |
10310 | = linemap_position_for_line_and_column (line_table, map, line, column); | |
a1ab4c31 | 10311 | |
fa6fd7b7 AO |
10312 | if (file_map && file_map[file - 1].Instance) |
10313 | decl_to_instance_map->put (decl, file_map[file - 1].Instance); | |
10314 | ||
a1ab4c31 AC |
10315 | return true; |
10316 | } | |
10317 | ||
56b8aa0c EB |
10318 | /* Return whether GNAT_NODE is a defining identifier for a renaming that comes |
10319 | from the parameter association for the instantiation of a generic. We do | |
10320 | not want to emit source location for them: the code generated for their | |
10321 | initialization is likely to disturb debugging. */ | |
10322 | ||
10323 | bool | |
10324 | renaming_from_instantiation_p (Node_Id gnat_node) | |
10325 | { | |
10326 | if (Nkind (gnat_node) != N_Defining_Identifier | |
10327 | || !Is_Object (gnat_node) | |
10328 | || Comes_From_Source (gnat_node) | |
10329 | || !Present (Renamed_Object (gnat_node))) | |
10330 | return false; | |
10331 | ||
10332 | /* Get the object declaration of the renamed object, if any and if the | |
10333 | renamed object is a mere identifier. */ | |
10334 | gnat_node = Renamed_Object (gnat_node); | |
10335 | if (Nkind (gnat_node) != N_Identifier) | |
10336 | return false; | |
10337 | ||
10338 | gnat_node = Parent (Entity (gnat_node)); | |
10339 | return (Present (gnat_node) | |
10340 | && Nkind (gnat_node) == N_Object_Declaration | |
10341 | && Present (Corresponding_Generic_Association (gnat_node))); | |
10342 | } | |
10343 | ||
a1ab4c31 | 10344 | /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and |
ba464315 EB |
10345 | don't do anything if it doesn't correspond to a source location. And, |
10346 | if CLEAR_COLUMN is true, set the column information to 0. */ | |
a1ab4c31 AC |
10347 | |
10348 | static void | |
ba464315 | 10349 | set_expr_location_from_node (tree node, Node_Id gnat_node, bool clear_column) |
a1ab4c31 AC |
10350 | { |
10351 | location_t locus; | |
10352 | ||
56b8aa0c EB |
10353 | /* Do not set a location for constructs likely to disturb debugging. */ |
10354 | if (Nkind (gnat_node) == N_Defining_Identifier) | |
10355 | { | |
10356 | if (Is_Type (gnat_node) && Is_Actual_Subtype (gnat_node)) | |
10357 | return; | |
10358 | ||
10359 | if (renaming_from_instantiation_p (gnat_node)) | |
10360 | return; | |
10361 | } | |
10362 | ||
ba464315 | 10363 | if (!Sloc_to_locus (Sloc (gnat_node), &locus, clear_column)) |
a1ab4c31 AC |
10364 | return; |
10365 | ||
10366 | SET_EXPR_LOCATION (node, locus); | |
10367 | } | |
17c168fe EB |
10368 | |
10369 | /* More elaborate version of set_expr_location_from_node to be used in more | |
10370 | general contexts, for example the result of the translation of a generic | |
10371 | GNAT node. */ | |
10372 | ||
10373 | static void | |
10374 | set_gnu_expr_location_from_node (tree node, Node_Id gnat_node) | |
10375 | { | |
10376 | /* Set the location information on the node if it is a real expression. | |
10377 | References can be reused for multiple GNAT nodes and they would get | |
10378 | the location information of their last use. Also make sure not to | |
10379 | overwrite an existing location as it is probably more precise. */ | |
10380 | ||
10381 | switch (TREE_CODE (node)) | |
10382 | { | |
10383 | CASE_CONVERT: | |
10384 | case NON_LVALUE_EXPR: | |
384e3fb1 | 10385 | case SAVE_EXPR: |
17c168fe EB |
10386 | break; |
10387 | ||
10388 | case COMPOUND_EXPR: | |
10389 | if (EXPR_P (TREE_OPERAND (node, 1))) | |
10390 | set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node); | |
10391 | ||
9c453de7 | 10392 | /* ... fall through ... */ |
17c168fe EB |
10393 | |
10394 | default: | |
10395 | if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node)) | |
2a02d090 OH |
10396 | { |
10397 | set_expr_location_from_node (node, gnat_node); | |
10398 | set_end_locus_from_node (node, gnat_node); | |
10399 | } | |
17c168fe EB |
10400 | break; |
10401 | } | |
10402 | } | |
ba464315 EB |
10403 | |
10404 | /* Set the end_locus information for GNU_NODE, if any, from an explicit end | |
10405 | location associated with GNAT_NODE or GNAT_NODE itself, whichever makes | |
10406 | most sense. Return true if a sensible assignment was performed. */ | |
10407 | ||
10408 | static bool | |
10409 | set_end_locus_from_node (tree gnu_node, Node_Id gnat_node) | |
10410 | { | |
10411 | Node_Id gnat_end_label; | |
10412 | location_t end_locus; | |
10413 | ||
10414 | /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node | |
10415 | end_locus when there is one. We consider only GNAT nodes with a possible | |
10416 | End_Label attached. If the End_Label actually was unassigned, fallback | |
10417 | on the original node. We'd better assign an explicit sloc associated with | |
10418 | the outer construct in any case. */ | |
10419 | ||
10420 | switch (Nkind (gnat_node)) | |
10421 | { | |
10422 | case N_Package_Body: | |
10423 | case N_Subprogram_Body: | |
10424 | case N_Block_Statement: | |
650ad524 BD |
10425 | if (Present (Handled_Statement_Sequence (gnat_node))) |
10426 | gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node)); | |
10427 | else | |
10428 | gnat_end_label = Empty; | |
ba464315 EB |
10429 | break; |
10430 | ||
10431 | case N_Package_Declaration: | |
650ad524 | 10432 | gcc_checking_assert (Present (Specification (gnat_node))); |
ba464315 EB |
10433 | gnat_end_label = End_Label (Specification (gnat_node)); |
10434 | break; | |
10435 | ||
10436 | default: | |
10437 | return false; | |
10438 | } | |
10439 | ||
10440 | if (Present (gnat_end_label)) | |
10441 | gnat_node = gnat_end_label; | |
10442 | ||
10443 | /* Some expanded subprograms have neither an End_Label nor a Sloc | |
10444 | attached. Notify that to callers. For a block statement with no | |
10445 | End_Label, clear column information, so that the tree for a | |
10446 | transient block does not receive the sloc of a source condition. */ | |
10447 | if (!Sloc_to_locus (Sloc (gnat_node), &end_locus, | |
10448 | No (gnat_end_label) | |
0b66f882 | 10449 | && Nkind (gnat_node) == N_Block_Statement)) |
ba464315 EB |
10450 | return false; |
10451 | ||
10452 | switch (TREE_CODE (gnu_node)) | |
10453 | { | |
10454 | case BIND_EXPR: | |
10455 | BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus; | |
10456 | return true; | |
10457 | ||
10458 | case FUNCTION_DECL: | |
10459 | DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus; | |
10460 | return true; | |
10461 | ||
10462 | default: | |
10463 | return false; | |
10464 | } | |
10465 | } | |
ce2d0ce2 | 10466 | |
a1ab4c31 AC |
10467 | /* Post an error message. MSG is the error message, properly annotated. |
10468 | NODE is the node at which to post the error and the node to use for the | |
586388fd | 10469 | '&' substitution. */ |
a1ab4c31 AC |
10470 | |
10471 | void | |
10472 | post_error (const char *msg, Node_Id node) | |
10473 | { | |
10474 | String_Template temp; | |
93582885 | 10475 | String_Pointer sp; |
a1ab4c31 | 10476 | |
457f72ac EB |
10477 | if (No (node)) |
10478 | return; | |
10479 | ||
10480 | temp.Low_Bound = 1; | |
10481 | temp.High_Bound = strlen (msg); | |
93582885 EB |
10482 | sp.Bounds = &temp; |
10483 | sp.Array = msg; | |
10484 | Error_Msg_N (sp, node); | |
a1ab4c31 AC |
10485 | } |
10486 | ||
586388fd EB |
10487 | /* Similar to post_error, but NODE is the node at which to post the error and |
10488 | ENT is the node to use for the '&' substitution. */ | |
a1ab4c31 AC |
10489 | |
10490 | void | |
10491 | post_error_ne (const char *msg, Node_Id node, Entity_Id ent) | |
10492 | { | |
10493 | String_Template temp; | |
93582885 | 10494 | String_Pointer sp; |
a1ab4c31 | 10495 | |
457f72ac EB |
10496 | if (No (node)) |
10497 | return; | |
10498 | ||
10499 | temp.Low_Bound = 1; | |
10500 | temp.High_Bound = strlen (msg); | |
93582885 EB |
10501 | sp.Bounds = &temp; |
10502 | sp.Array = msg; | |
10503 | Error_Msg_NE (sp, node, ent); | |
a1ab4c31 AC |
10504 | } |
10505 | ||
586388fd | 10506 | /* Similar to post_error_ne, but NUM is the number to use for the '^'. */ |
a1ab4c31 AC |
10507 | |
10508 | void | |
58c8f770 | 10509 | post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num) |
a1ab4c31 | 10510 | { |
58c8f770 | 10511 | Error_Msg_Uint_1 = UI_From_Int (num); |
586388fd | 10512 | post_error_ne (msg, node, ent); |
a1ab4c31 | 10513 | } |
2a02d090 | 10514 | |
586388fd EB |
10515 | /* Similar to post_error_ne, but T is a GCC tree representing the number to |
10516 | write. If T represents a constant, the text inside curly brackets in | |
10517 | MSG will be output (presumably including a '^'). Otherwise it will not | |
10518 | be output and the text inside square brackets will be output instead. */ | |
a1ab4c31 AC |
10519 | |
10520 | void | |
10521 | post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t) | |
10522 | { | |
586388fd | 10523 | char *new_msg = XALLOCAVEC (char, strlen (msg) + 1); |
a1ab4c31 AC |
10524 | char start_yes, end_yes, start_no, end_no; |
10525 | const char *p; | |
10526 | char *q; | |
10527 | ||
586388fd | 10528 | if (TREE_CODE (t) == INTEGER_CST) |
a1ab4c31 | 10529 | { |
586388fd | 10530 | Error_Msg_Uint_1 = UI_From_gnu (t); |
a1ab4c31 AC |
10531 | start_yes = '{', end_yes = '}', start_no = '[', end_no = ']'; |
10532 | } | |
10533 | else | |
10534 | start_yes = '[', end_yes = ']', start_no = '{', end_no = '}'; | |
10535 | ||
586388fd | 10536 | for (p = msg, q = new_msg; *p; p++) |
a1ab4c31 AC |
10537 | { |
10538 | if (*p == start_yes) | |
10539 | for (p++; *p != end_yes; p++) | |
10540 | *q++ = *p; | |
10541 | else if (*p == start_no) | |
10542 | for (p++; *p != end_no; p++) | |
10543 | ; | |
10544 | else | |
10545 | *q++ = *p; | |
10546 | } | |
10547 | ||
10548 | *q = 0; | |
10549 | ||
586388fd | 10550 | post_error_ne (new_msg, node, ent); |
a1ab4c31 AC |
10551 | } |
10552 | ||
586388fd | 10553 | /* Similar to post_error_ne_tree, but NUM is a second integer to write. */ |
a1ab4c31 AC |
10554 | |
10555 | void | |
1e17ef87 EB |
10556 | post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t, |
10557 | int num) | |
a1ab4c31 AC |
10558 | { |
10559 | Error_Msg_Uint_2 = UI_From_Int (num); | |
10560 | post_error_ne_tree (msg, node, ent, t); | |
10561 | } | |
7c775aca | 10562 | |
8f8f531f | 10563 | /* Return a label to branch to for the exception type in KIND or Empty |
7c775aca EB |
10564 | if none. */ |
10565 | ||
8f8f531f | 10566 | Entity_Id |
7c775aca EB |
10567 | get_exception_label (char kind) |
10568 | { | |
10569 | switch (kind) | |
10570 | { | |
10571 | case N_Raise_Constraint_Error: | |
8f8f531f | 10572 | return gnu_constraint_error_label_stack.last (); |
7c775aca EB |
10573 | |
10574 | case N_Raise_Storage_Error: | |
8f8f531f | 10575 | return gnu_storage_error_label_stack.last (); |
7c775aca EB |
10576 | |
10577 | case N_Raise_Program_Error: | |
8f8f531f | 10578 | return gnu_program_error_label_stack.last (); |
7c775aca EB |
10579 | |
10580 | default: | |
8f8f531f | 10581 | return Empty; |
7c775aca EB |
10582 | } |
10583 | ||
8f8f531f | 10584 | gcc_unreachable (); |
7c775aca EB |
10585 | } |
10586 | ||
10587 | /* Return the decl for the current elaboration procedure. */ | |
10588 | ||
7543c8b6 | 10589 | static tree |
7c775aca EB |
10590 | get_elaboration_procedure (void) |
10591 | { | |
10592 | return gnu_elab_proc_stack->last (); | |
10593 | } | |
10594 | ||
44662f68 EB |
10595 | /* Return the controlling type of a dispatching subprogram. */ |
10596 | ||
10597 | static Entity_Id | |
10598 | get_controlling_type (Entity_Id subprog) | |
10599 | { | |
17a98a3d | 10600 | /* This is modeled on Expand_Interface_Thunk. */ |
44662f68 EB |
10601 | Entity_Id controlling_type = Etype (First_Formal (subprog)); |
10602 | if (Is_Access_Type (controlling_type)) | |
10603 | controlling_type = Directly_Designated_Type (controlling_type); | |
10604 | controlling_type = Underlying_Type (controlling_type); | |
10605 | if (Is_Concurrent_Type (controlling_type)) | |
10606 | controlling_type = Corresponding_Record_Type (controlling_type); | |
10607 | controlling_type = Base_Type (controlling_type); | |
10608 | return controlling_type; | |
10609 | } | |
10610 | ||
10611 | /* Return whether we should use an alias for the TARGET of a thunk | |
10612 | in order to make the call generated in the thunk local. */ | |
10613 | ||
10614 | static bool | |
10615 | use_alias_for_thunk_p (tree target) | |
10616 | { | |
10617 | /* We cannot generate a local call in this case. */ | |
10618 | if (DECL_EXTERNAL (target)) | |
10619 | return false; | |
10620 | ||
10621 | /* The call is already local in this case. */ | |
10622 | if (TREE_CODE (DECL_CONTEXT (target)) == FUNCTION_DECL) | |
10623 | return false; | |
10624 | ||
10625 | return TARGET_USE_LOCAL_THUNK_ALIAS_P (target); | |
10626 | } | |
10627 | ||
10628 | static GTY(()) unsigned long thunk_labelno = 0; | |
10629 | ||
10630 | /* Create an alias for TARGET to be used as the target of a thunk. */ | |
10631 | ||
10632 | static tree | |
10633 | make_alias_for_thunk (tree target) | |
10634 | { | |
10635 | char buf[64]; | |
10636 | targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno++); | |
10637 | ||
10638 | tree alias = build_decl (DECL_SOURCE_LOCATION (target), TREE_CODE (target), | |
10639 | get_identifier (buf), TREE_TYPE (target)); | |
10640 | ||
10641 | DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target); | |
10642 | DECL_CONTEXT (alias) = DECL_CONTEXT (target); | |
10643 | TREE_READONLY (alias) = TREE_READONLY (target); | |
10644 | TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target); | |
10645 | DECL_ARTIFICIAL (alias) = 1; | |
10646 | DECL_INITIAL (alias) = error_mark_node; | |
10647 | DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target)); | |
10648 | TREE_ADDRESSABLE (alias) = 1; | |
10649 | SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias)); | |
10650 | ||
10651 | cgraph_node *n = cgraph_node::create_same_body_alias (alias, target); | |
10652 | gcc_assert (n); | |
10653 | ||
10654 | return alias; | |
10655 | } | |
10656 | ||
5d01fc7c | 10657 | /* Create the local covariant part of {GNAT,GNU}_THUNK. */ |
44662f68 EB |
10658 | |
10659 | static tree | |
10660 | make_covariant_thunk (Entity_Id gnat_thunk, tree gnu_thunk) | |
10661 | { | |
10662 | tree gnu_name = create_concat_name (gnat_thunk, "CV"); | |
10663 | tree gnu_cv_thunk | |
10664 | = build_decl (DECL_SOURCE_LOCATION (gnu_thunk), TREE_CODE (gnu_thunk), | |
10665 | gnu_name, TREE_TYPE (gnu_thunk)); | |
10666 | ||
10667 | DECL_ARGUMENTS (gnu_cv_thunk) = copy_list (DECL_ARGUMENTS (gnu_thunk)); | |
5d01fc7c EB |
10668 | for (tree param_decl = DECL_ARGUMENTS (gnu_cv_thunk); |
10669 | param_decl; | |
10670 | param_decl = DECL_CHAIN (param_decl)) | |
10671 | DECL_CONTEXT (param_decl) = gnu_cv_thunk; | |
10672 | ||
44662f68 EB |
10673 | DECL_RESULT (gnu_cv_thunk) = copy_node (DECL_RESULT (gnu_thunk)); |
10674 | DECL_CONTEXT (DECL_RESULT (gnu_cv_thunk)) = gnu_cv_thunk; | |
10675 | ||
10676 | DECL_LANG_SPECIFIC (gnu_cv_thunk) = DECL_LANG_SPECIFIC (gnu_thunk); | |
10677 | DECL_CONTEXT (gnu_cv_thunk) = DECL_CONTEXT (gnu_thunk); | |
10678 | TREE_READONLY (gnu_cv_thunk) = TREE_READONLY (gnu_thunk); | |
10679 | TREE_THIS_VOLATILE (gnu_cv_thunk) = TREE_THIS_VOLATILE (gnu_thunk); | |
44662f68 EB |
10680 | DECL_ARTIFICIAL (gnu_cv_thunk) = 1; |
10681 | ||
10682 | return gnu_cv_thunk; | |
10683 | } | |
10684 | ||
10685 | /* Try to create a GNU thunk for {GNAT,GNU}_THUNK and return true on success. | |
10686 | ||
10687 | GNU thunks are more efficient than GNAT thunks because they don't call into | |
10688 | the runtime to retrieve the offset used in the displacement operation, but | |
10689 | they are tailored to C++ and thus too limited to support the full range of | |
10690 | thunks generated in Ada. Here's the complete list of limitations: | |
10691 | ||
10692 | 1. Multi-controlling thunks, i.e thunks with more than one controlling | |
10693 | parameter, are simply not supported. | |
10694 | ||
10695 | 2. Covariant thunks, i.e. thunks for which the result is also controlling, | |
10696 | are split into a pair of (this, covariant-only) thunks. | |
10697 | ||
10698 | 3. Variable-offset thunks, i.e. thunks for which the offset depends on the | |
10699 | object and not only on its type, are supported as 2nd class citizens. | |
10700 | ||
10701 | 4. External thunks, i.e. thunks for which the target is not declared in | |
10702 | the same unit as the thunk, are supported as 2nd class citizens. | |
10703 | ||
10704 | 5. Local thunks, i.e. thunks generated for a local type, are supported as | |
10705 | 2nd class citizens. */ | |
10706 | ||
10707 | static bool | |
10708 | maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk) | |
10709 | { | |
89e037d0 EB |
10710 | /* We use the Thunk_Target to compute the properties of the thunk. */ |
10711 | const Entity_Id gnat_target = Thunk_Target (gnat_thunk); | |
44662f68 EB |
10712 | |
10713 | /* Check that the first formal of the target is the only controlling one. */ | |
10714 | Entity_Id gnat_formal = First_Formal (gnat_target); | |
10715 | if (!Is_Controlling_Formal (gnat_formal)) | |
10716 | return false; | |
10717 | for (gnat_formal = Next_Formal (gnat_formal); | |
10718 | Present (gnat_formal); | |
10719 | gnat_formal = Next_Formal (gnat_formal)) | |
10720 | if (Is_Controlling_Formal (gnat_formal)) | |
10721 | return false; | |
10722 | ||
10723 | /* Look for the types that control the target and the thunk. */ | |
10724 | const Entity_Id gnat_controlling_type = get_controlling_type (gnat_target); | |
10725 | const Entity_Id gnat_interface_type = get_controlling_type (gnat_thunk); | |
10726 | ||
20dc266e JM |
10727 | /* We must have an interface type at this point. */ |
10728 | gcc_assert (Is_Interface (gnat_interface_type)); | |
10729 | ||
44662f68 EB |
10730 | /* Now compute whether the former covers the latter. */ |
10731 | const Entity_Id gnat_interface_tag | |
20dc266e | 10732 | = Find_Interface_Tag (gnat_controlling_type, gnat_interface_type); |
44662f68 EB |
10733 | tree gnu_interface_tag |
10734 | = Present (gnat_interface_tag) | |
10735 | ? gnat_to_gnu_field_decl (gnat_interface_tag) | |
10736 | : NULL_TREE; | |
10737 | tree gnu_interface_offset | |
10738 | = gnu_interface_tag ? byte_position (gnu_interface_tag) : NULL_TREE; | |
10739 | ||
10740 | /* There are three ways to retrieve the offset between the interface view | |
10741 | and the base object. Either the controlling type covers the interface | |
10742 | type and the offset of the corresponding tag is fixed, in which case it | |
10743 | can be statically encoded in the thunk (see FIXED_OFFSET below). Or the | |
10744 | controlling type doesn't cover the interface type but is of fixed size, | |
10745 | in which case the offset is stored in the dispatch table, two pointers | |
10746 | above the dispatch table address (see VIRTUAL_VALUE below). Otherwise, | |
10747 | the offset is variable and is stored right after the tag in every object | |
10748 | (see INDIRECT_OFFSET below). See also a-tags.ads for more details. */ | |
10749 | HOST_WIDE_INT fixed_offset, virtual_value, indirect_offset; | |
10750 | tree virtual_offset; | |
10751 | ||
10752 | if (gnu_interface_offset && TREE_CODE (gnu_interface_offset) == INTEGER_CST) | |
10753 | { | |
10754 | fixed_offset = - tree_to_shwi (gnu_interface_offset); | |
10755 | virtual_value = 0; | |
10756 | virtual_offset = NULL_TREE; | |
10757 | indirect_offset = 0; | |
10758 | } | |
10759 | else if (!gnu_interface_offset | |
10760 | && !Is_Variable_Size_Record (gnat_controlling_type)) | |
10761 | { | |
10762 | fixed_offset = 0; | |
10763 | virtual_value = - 2 * (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT); | |
10764 | virtual_offset = build_int_cst (integer_type_node, virtual_value); | |
10765 | indirect_offset = 0; | |
10766 | } | |
10767 | else | |
10768 | { | |
10769 | /* Covariant thunks with variable offset are not supported. */ | |
10770 | if (Has_Controlling_Result (gnat_target)) | |
10771 | return false; | |
10772 | ||
10773 | fixed_offset = 0; | |
10774 | virtual_value = 0; | |
10775 | virtual_offset = NULL_TREE; | |
10776 | indirect_offset = (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT); | |
10777 | } | |
10778 | ||
89e037d0 EB |
10779 | /* But we generate a call to the Thunk_Entity in the thunk. */ |
10780 | tree gnu_target | |
10781 | = gnat_to_gnu_entity (Thunk_Entity (gnat_thunk), NULL_TREE, false); | |
44662f68 | 10782 | |
67c4d1c7 EB |
10783 | /* If the target is local, then thunk and target must have the same context |
10784 | because cgraph_node::expand_thunk can only forward the static chain. */ | |
10785 | if (DECL_STATIC_CHAIN (gnu_target) | |
10786 | && DECL_CONTEXT (gnu_thunk) != DECL_CONTEXT (gnu_target)) | |
10787 | return false; | |
44662f68 EB |
10788 | |
10789 | /* If the target returns by invisible reference and is external, apply the | |
10790 | same transformation as Subprogram_Body_to_gnu here. */ | |
10791 | if (TREE_ADDRESSABLE (TREE_TYPE (gnu_target)) | |
10792 | && DECL_EXTERNAL (gnu_target) | |
10793 | && !POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (gnu_target)))) | |
10794 | { | |
10795 | TREE_TYPE (DECL_RESULT (gnu_target)) | |
10796 | = build_reference_type (TREE_TYPE (DECL_RESULT (gnu_target))); | |
10797 | relayout_decl (DECL_RESULT (gnu_target)); | |
10798 | } | |
10799 | ||
10800 | /* The thunk expander requires the return types of thunk and target to be | |
10801 | compatible, which is not fully the case with the CICO mechanism. */ | |
10802 | if (TYPE_CI_CO_LIST (TREE_TYPE (gnu_thunk))) | |
10803 | { | |
10804 | tree gnu_target_type = TREE_TYPE (gnu_target); | |
10805 | gcc_assert (TYPE_CI_CO_LIST (gnu_target_type)); | |
10806 | TYPE_CANONICAL (TREE_TYPE (TREE_TYPE (gnu_thunk))) | |
10807 | = TYPE_CANONICAL (TREE_TYPE (gnu_target_type)); | |
10808 | } | |
10809 | ||
10810 | cgraph_node *target_node = cgraph_node::get_create (gnu_target); | |
10811 | ||
5d01fc7c EB |
10812 | /* We may also need to create an alias for the target in order to make |
10813 | the call local, depending on the linkage of the target. */ | |
10814 | tree gnu_alias = use_alias_for_thunk_p (gnu_target) | |
10815 | ? make_alias_for_thunk (gnu_target) | |
10816 | : gnu_target; | |
10817 | ||
44662f68 EB |
10818 | /* If the return type of the target is a controlling type, then we need |
10819 | both an usual this thunk and a covariant thunk in this order: | |
10820 | ||
10821 | this thunk --> covariant thunk --> target | |
10822 | ||
10823 | For covariant thunks, we can only handle a fixed offset. */ | |
10824 | if (Has_Controlling_Result (gnat_target)) | |
10825 | { | |
10826 | gcc_assert (fixed_offset < 0); | |
10827 | tree gnu_cv_thunk = make_covariant_thunk (gnat_thunk, gnu_thunk); | |
10828 | target_node->create_thunk (gnu_cv_thunk, gnu_target, false, | |
10829 | - fixed_offset, 0, 0, | |
5d01fc7c | 10830 | NULL_TREE, gnu_alias); |
44662f68 | 10831 | |
5d01fc7c | 10832 | gnu_alias = gnu_target = gnu_cv_thunk; |
44662f68 EB |
10833 | } |
10834 | ||
44662f68 EB |
10835 | target_node->create_thunk (gnu_thunk, gnu_target, true, |
10836 | fixed_offset, virtual_value, indirect_offset, | |
10837 | virtual_offset, gnu_alias); | |
10838 | ||
10839 | return true; | |
10840 | } | |
10841 | ||
a1ab4c31 AC |
10842 | /* Initialize the table that maps GNAT codes to GCC codes for simple |
10843 | binary and unary operations. */ | |
10844 | ||
10845 | static void | |
10846 | init_code_table (void) | |
10847 | { | |
a1ab4c31 AC |
10848 | gnu_codes[N_Op_And] = TRUTH_AND_EXPR; |
10849 | gnu_codes[N_Op_Or] = TRUTH_OR_EXPR; | |
10850 | gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR; | |
10851 | gnu_codes[N_Op_Eq] = EQ_EXPR; | |
10852 | gnu_codes[N_Op_Ne] = NE_EXPR; | |
10853 | gnu_codes[N_Op_Lt] = LT_EXPR; | |
10854 | gnu_codes[N_Op_Le] = LE_EXPR; | |
10855 | gnu_codes[N_Op_Gt] = GT_EXPR; | |
10856 | gnu_codes[N_Op_Ge] = GE_EXPR; | |
10857 | gnu_codes[N_Op_Add] = PLUS_EXPR; | |
10858 | gnu_codes[N_Op_Subtract] = MINUS_EXPR; | |
10859 | gnu_codes[N_Op_Multiply] = MULT_EXPR; | |
10860 | gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR; | |
10861 | gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR; | |
10862 | gnu_codes[N_Op_Minus] = NEGATE_EXPR; | |
10863 | gnu_codes[N_Op_Abs] = ABS_EXPR; | |
10864 | gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR; | |
10865 | gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR; | |
10866 | gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR; | |
10867 | gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR; | |
10868 | gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR; | |
10869 | gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR; | |
9a1bdc31 EB |
10870 | gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR; |
10871 | gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR; | |
a1ab4c31 AC |
10872 | } |
10873 | ||
a1ab4c31 | 10874 | #include "gt-ada-trans.h" |