]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/misc.c
c4da9226dd8bb7a5739d920800c21b107919c980
[thirdparty/gcc.git] / gcc / ada / misc.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * M I S C *
6 * *
7 * C Implementation File *
8 * *
9 * $Revision: 1.17 $
10 * *
11 * Copyright (C) 1992-2001 Free Software Foundation, Inc. *
12 * *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
23 * *
24 * As a special exception, if you link this file with other files to *
25 * produce an executable, this file does not by itself cause the resulting *
26 * executable to be covered by the GNU General Public License. This except- *
27 * ion does not however invalidate any other reasons why the executable *
28 * file might be covered by the GNU Public License. *
29 * *
30 * GNAT was originally developed by the GNAT team at New York University. *
31 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
32 * *
33 ****************************************************************************/
34
35 /* This file contains parts of the compiler that are required for interfacing
36 with GCC but otherwise do nothing and parts of Gigi that need to know
37 about RTL. */
38
39 #include "config.h"
40 #include "system.h"
41 #include "tree.h"
42 #include "rtl.h"
43 #include "errors.h"
44 #include "diagnostic.h"
45 #include "expr.h"
46 #include "ggc.h"
47 #include "flags.h"
48 #include "insn-codes.h"
49 #include "insn-flags.h"
50 #include "insn-config.h"
51 #include "optabs.h"
52 #include "recog.h"
53 #include "toplev.h"
54 #include "output.h"
55 #include "except.h"
56 #include "tm_p.h"
57 #include "langhooks.h"
58 #include "langhooks-def.h"
59
60 #include "ada.h"
61 #include "types.h"
62 #include "atree.h"
63 #include "elists.h"
64 #include "namet.h"
65 #include "nlists.h"
66 #include "stringt.h"
67 #include "uintp.h"
68 #include "fe.h"
69 #include "sinfo.h"
70 #include "einfo.h"
71 #include "ada-tree.h"
72 #include "gigi.h"
73
74 extern FILE *asm_out_file;
75 extern int save_argc;
76 extern char **save_argv;
77
78 /* Tables describing GCC tree codes used only by GNAT.
79
80 Table indexed by tree code giving a string containing a character
81 classifying the tree code. Possibilities are
82 t, d, s, c, r, <, 1 and 2. See cp-tree.def for details. */
83
84 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
85
86 static char const gnat_tree_code_type[] = {
87 'x',
88 #include "ada-tree.def"
89 };
90 #undef DEFTREECODE
91
92 /* Table indexed by tree code giving number of expression
93 operands beyond the fixed part of the node structure.
94 Not used for types or decls. */
95
96 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
97
98 static int const gnat_tree_code_length[] = {
99 0,
100 #include "ada-tree.def"
101 };
102 #undef DEFTREECODE
103
104 /* Names of tree components.
105 Used for printing out the tree and error messages. */
106 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
107
108 static const char *gnat_tree_code_name[] = {
109 "@@dummy",
110 #include "ada-tree.def"
111 };
112 #undef DEFTREECODE
113
114 static const char *gnat_init PARAMS ((const char *));
115 static void gnat_init_options PARAMS ((void));
116 static int gnat_decode_option PARAMS ((int, char **));
117 static HOST_WIDE_INT gnat_get_alias_set PARAMS ((tree));
118 static void gnat_print_decl PARAMS ((FILE *, tree, int));
119 static void gnat_print_type PARAMS ((FILE *, tree, int));
120 extern void gnat_init_decl_processing PARAMS ((void));
121 static tree gnat_expand_constant PARAMS ((tree));
122
123 /* Structure giving our language-specific hooks. */
124
125 #undef LANG_HOOKS_NAME
126 #define LANG_HOOKS_NAME "GNU Ada"
127 #undef LANG_HOOKS_IDENTIFIER_SIZE
128 #define LANG_HOOKS_IDENTIFIER_SIZE sizeof (struct tree_identifier)
129 #undef LANG_HOOKS_INIT
130 #define LANG_HOOKS_INIT gnat_init
131 #undef LANG_HOOKS_INIT_OPTIONS
132 #define LANG_HOOKS_INIT_OPTIONS gnat_init_options
133 #undef LANG_HOOKS_DECODE_OPTION
134 #define LANG_HOOKS_DECODE_OPTION gnat_decode_option
135 #undef LANG_HOOKS_HONOR_READONLY
136 #define LANG_HOOKS_HONOR_READONLY 1
137 #undef LANG_HOOKS_GET_ALIAS_SET
138 #define LANG_HOOKS_GET_ALIAS_SET gnat_get_alias_set
139 #undef LANG_HOOKS_PRINT_DECL
140 #define LANG_HOOKS_PRINT_DECL gnat_print_decl
141 #undef LANG_HOOKS_PRINT_TYPE
142 #define LANG_HOOKS_PRINT_TYPE gnat_print_type
143 #undef LANG_HOOKS_EXPAND_CONSTANT
144 #define LANG_HOOKS_EXPAND_CONSTANT gnat_expand_constant
145
146 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
147
148 /* gnat standard argc argv */
149
150 extern int gnat_argc;
151 extern char **gnat_argv;
152
153 /* Global Variables Expected by gcc: */
154
155 int ggc_p = 1;
156
157 static void internal_error_function PARAMS ((const char *, va_list *));
158 static rtx gnat_expand_expr PARAMS ((tree, rtx, enum machine_mode,
159 enum expand_modifier));
160 static void gnat_adjust_rli PARAMS ((record_layout_info));
161
162 #if defined(MIPS_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO)
163 static char *convert_ada_name_to_qualified_name PARAMS ((char *));
164 #endif
165 \f
166 /* Routines Expected by gcc: */
167
168 /* For most front-ends, this is the parser for the language. For us, we
169 process the GNAT tree. */
170
171 /* Declare functions we use as part of startup. */
172 extern void __gnat_initialize PARAMS((void));
173 extern void adainit PARAMS((void));
174 extern void _ada_gnat1drv PARAMS((void));
175
176 int
177 yyparse ()
178 {
179 /* call the target specific initializations */
180 __gnat_initialize();
181
182 /* Call the front-end elaboration procedures */
183 adainit ();
184
185 immediate_size_expand = 1;
186
187 /* Call the front end */
188 _ada_gnat1drv ();
189
190 return 0;
191 }
192
193 /* Decode all the language specific options that cannot be decoded by GCC.
194 The option decoding phase of GCC calls this routine on the flags that
195 it cannot decode. This routine returns 1 if it is successful, otherwise
196 it returns 0. */
197
198 static int
199 gnat_decode_option (argc, argv)
200 int argc ATTRIBUTE_UNUSED;
201 char **argv;
202 {
203 char *p = argv[0];
204 int i;
205
206 if (!strncmp (p, "-I", 2))
207 {
208 /* Pass the -I switches as-is. */
209 gnat_argv[gnat_argc] = p;
210 gnat_argc ++;
211 return 1;
212 }
213
214 else if (!strncmp (p, "-gant", 5))
215 {
216 char *q = (char *) xmalloc (strlen (p) + 1);
217
218 warning ("`-gnat' misspelled as `-gant'");
219 strcpy (q, p);
220 q[2] = 'n', q[3] = 'a';
221 p = q;
222 return 1;
223 }
224
225 else if (!strncmp (p, "-gnat", 5))
226 {
227 /* Recopy the switches without the 'gnat' prefix */
228
229 gnat_argv[gnat_argc] = (char *) xmalloc (strlen (p) - 3);
230 gnat_argv[gnat_argc][0] = '-';
231 strcpy (gnat_argv[gnat_argc] + 1, p + 5);
232 gnat_argc ++;
233 if (p[5] == 'O')
234 for (i = 1; i < save_argc - 1; i++)
235 if (!strncmp (save_argv[i], "-gnatO", 6))
236 if (save_argv[++i][0] != '-')
237 {
238 /* Preserve output filename as GCC doesn't save it for GNAT. */
239 gnat_argv[gnat_argc] = save_argv[i];
240 gnat_argc++;
241 break;
242 }
243
244 return 1;
245 }
246
247 /* Ignore -W flags since people may want to use the same flags for all
248 languages. */
249 else if (p[0] == '-' && p[1] == 'W' && p[2] != 0)
250 return 1;
251
252 return 0;
253 }
254
255 /* Initialize for option processing. */
256
257 static void
258 gnat_init_options ()
259 {
260 /* Initialize gnat_argv with save_argv size */
261 gnat_argv = (char **) xmalloc ((save_argc + 1) * sizeof (gnat_argv[0]));
262 gnat_argv [0] = save_argv[0]; /* name of the command */
263 gnat_argc = 1;
264 }
265
266 void
267 lang_mark_tree (t)
268 tree t;
269 {
270 switch (TREE_CODE (t))
271 {
272 case FUNCTION_TYPE:
273 ggc_mark_tree (TYPE_CI_CO_LIST (t));
274 return;
275
276 case INTEGER_TYPE:
277 if (TYPE_MODULAR_P (t))
278 ggc_mark_tree (TYPE_MODULUS (t));
279 else if (TYPE_VAX_FLOATING_POINT_P (t))
280 ;
281 else if (TYPE_HAS_ACTUAL_BOUNDS_P (t))
282 ggc_mark_tree (TYPE_ACTUAL_BOUNDS (t));
283 else
284 ggc_mark_tree (TYPE_INDEX_TYPE (t));
285 return;
286
287 case ENUMERAL_TYPE:
288 ggc_mark_tree (TYPE_RM_SIZE_ENUM (t));
289 return;
290
291 case ARRAY_TYPE:
292 ggc_mark_tree (TYPE_ACTUAL_BOUNDS (t));
293 return;
294
295 case RECORD_TYPE: case UNION_TYPE: case QUAL_UNION_TYPE:
296 /* This is really TYPE_UNCONSTRAINED_ARRAY for fat pointers. */
297 ggc_mark_tree (TYPE_ADA_SIZE (t));
298 return;
299
300 case CONST_DECL:
301 ggc_mark_tree (DECL_CONST_CORRESPONDING_VAR (t));
302 return;
303
304 case FIELD_DECL:
305 ggc_mark_tree (DECL_ORIGINAL_FIELD (t));
306 return;
307
308 default:
309 return;
310 }
311 }
312
313 /* Here we have the function to handle the compiler error processing in GCC. */
314
315 static void
316 internal_error_function (msgid, ap)
317 const char *msgid;
318 va_list *ap;
319 {
320 char buffer[1000]; /* Assume this is big enough. */
321 char *p;
322 String_Template temp;
323 Fat_Pointer fp;
324
325 vsprintf (buffer, msgid, *ap);
326
327 /* Go up to the first newline. */
328 for (p = buffer; *p != 0; p++)
329 if (*p == '\n')
330 {
331 *p = '\0';
332 break;
333 }
334
335 temp.Low_Bound = 1, temp.High_Bound = strlen (buffer);
336 fp.Array = buffer, fp.Bounds = &temp;
337
338 Current_Error_Node = error_gnat_node;
339 Compiler_Abort (fp, -1);
340 }
341
342 /* Perform all the initialization steps that are language-specific. */
343
344 static const char *
345 gnat_init (filename)
346 const char *filename;
347 {
348 /* Performs whatever initialization steps needed by the language-dependent
349 lexical analyzer.
350
351 Define the additional tree codes here. This isn't the best place to put
352 it, but it's where g++ does it. */
353
354 lang_expand_expr = gnat_expand_expr;
355
356 memcpy ((char *) (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE),
357 (char *) gnat_tree_code_type,
358 ((LAST_GNAT_TREE_CODE - (int) LAST_AND_UNUSED_TREE_CODE)
359 * sizeof (char *)));
360
361 memcpy ((char *) (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE),
362 (char *) gnat_tree_code_length,
363 ((LAST_GNAT_TREE_CODE - (int) LAST_AND_UNUSED_TREE_CODE)
364 * sizeof (int)));
365
366 memcpy ((char *) (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE),
367 (char *) gnat_tree_code_name,
368 ((LAST_GNAT_TREE_CODE - (int) LAST_AND_UNUSED_TREE_CODE)
369 * sizeof (char *)));
370
371 gnat_init_decl_processing ();
372
373 /* Add the input filename as the last argument. */
374 gnat_argv [gnat_argc] = (char *) filename;
375 gnat_argc++;
376 gnat_argv [gnat_argc] = 0;
377
378 set_internal_error_function (internal_error_function);
379
380 /* Show that REFERENCE_TYPEs are internal and should be Pmode. */
381 internal_reference_types ();
382
383 /* Show we don't use the common language attributes. */
384 lang_attribute_common = 0;
385
386 set_lang_adjust_rli (gnat_adjust_rli);
387
388 #if defined(MIPS_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO)
389 dwarf2out_set_demangle_name_func (convert_ada_name_to_qualified_name);
390 #endif
391
392 if (filename == 0)
393 filename = "";
394
395 return filename;
396 }
397
398 /* If DECL has a cleanup, build and return that cleanup here.
399 This is a callback called by expand_expr. */
400
401 tree
402 maybe_build_cleanup (decl)
403 tree decl ATTRIBUTE_UNUSED;
404 {
405 /* There are no cleanups in C. */
406 return NULL_TREE;
407 }
408
409 /* integrate_decl_tree calls this function, but since we don't use the
410 DECL_LANG_SPECIFIC field, this is a no-op. */
411
412 void
413 copy_lang_decl (node)
414 tree node ATTRIBUTE_UNUSED;
415 {
416 }
417
418 /* Hooks for print-tree.c: */
419
420 static void
421 gnat_print_decl (file, node, indent)
422 FILE *file;
423 tree node;
424 int indent;
425 {
426 switch (TREE_CODE (node))
427 {
428 case CONST_DECL:
429 print_node (file, "const_corresponding_var",
430 DECL_CONST_CORRESPONDING_VAR (node), indent + 4);
431 break;
432
433 case FIELD_DECL:
434 print_node (file, "original field", DECL_ORIGINAL_FIELD (node),
435 indent + 4);
436 break;
437
438 default:
439 break;
440 }
441 }
442
443 static void
444 gnat_print_type (file, node, indent)
445 FILE *file;
446 tree node;
447 int indent;
448 {
449 switch (TREE_CODE (node))
450 {
451 case FUNCTION_TYPE:
452 print_node (file, "ci_co_list", TYPE_CI_CO_LIST (node), indent + 4);
453 break;
454
455 case ENUMERAL_TYPE:
456 print_node (file, "RM size", TYPE_RM_SIZE_ENUM (node), indent + 4);
457 break;
458
459 case INTEGER_TYPE:
460 if (TYPE_MODULAR_P (node))
461 print_node (file, "modulus", TYPE_MODULUS (node), indent + 4);
462 else if (TYPE_HAS_ACTUAL_BOUNDS_P (node))
463 print_node (file, "actual bounds", TYPE_ACTUAL_BOUNDS (node),
464 indent + 4);
465 else if (TYPE_VAX_FLOATING_POINT_P (node))
466 ;
467 else
468 print_node (file, "index type", TYPE_INDEX_TYPE (node), indent + 4);
469
470 print_node (file, "RM size", TYPE_RM_SIZE_INT (node), indent + 4);
471 break;
472
473 case ARRAY_TYPE:
474 print_node (file,"actual bounds", TYPE_ACTUAL_BOUNDS (node), indent + 4);
475 break;
476
477 case RECORD_TYPE:
478 if (TYPE_IS_FAT_POINTER_P (node) || TYPE_CONTAINS_TEMPLATE_P (node))
479 print_node (file, "unconstrained array",
480 TYPE_UNCONSTRAINED_ARRAY (node), indent + 4);
481 else
482 print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
483 break;
484
485 case UNION_TYPE:
486 case QUAL_UNION_TYPE:
487 print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
488 break;
489
490 default:
491 break;
492 }
493 }
494
495 /* Expands GNAT-specific GCC tree nodes. The only ones we support
496 here are TRANSFORM_EXPR, UNCHECKED_CONVERT_EXPR, ALLOCATE_EXPR,
497 USE_EXPR and NULL_EXPR. */
498
499 static rtx
500 gnat_expand_expr (exp, target, tmode, modifier)
501 tree exp;
502 rtx target;
503 enum machine_mode tmode;
504 enum expand_modifier modifier;
505 {
506 tree type = TREE_TYPE (exp);
507 tree inner_type;
508 tree new;
509 rtx result;
510 int align_ok;
511
512 /* Update EXP to be the new expression to expand. */
513
514 switch (TREE_CODE (exp))
515 {
516 case TRANSFORM_EXPR:
517 gnat_to_code (TREE_COMPLEXITY (exp));
518 return const0_rtx;
519 break;
520
521 case UNCHECKED_CONVERT_EXPR:
522 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
523
524 /* The alignment is OK if the flag saying it is OK is set in either
525 type, if the inner type is already maximally aligned, if the
526 new type is no more strictly aligned than the old type, or
527 if byte accesses are not slow. */
528 align_ok = (! SLOW_BYTE_ACCESS
529 || TYPE_ALIGN_OK_P (type) || TYPE_ALIGN_OK_P (inner_type)
530 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
531 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type));
532
533 /* If we're converting between an aggregate and non-aggregate type
534 and we have a MEM TARGET, we can't use it, since MEM_IN_STRUCT_P
535 would be set incorrectly. */
536 if (target != 0 && GET_CODE (target) == MEM
537 && (MEM_IN_STRUCT_P (target) != AGGREGATE_TYPE_P (inner_type)))
538 target = 0;
539
540 /* If the input and output are both the same mode (usually BLKmode),
541 just return the expanded input since we want just the bits. But
542 we can't do this if the output is more strictly aligned than
543 the input or if the type is BLKmode and the sizes differ. */
544 if (TYPE_MODE (type) == TYPE_MODE (inner_type)
545 && align_ok
546 && ! (TYPE_MODE (type) == BLKmode
547 && ! operand_equal_p (TYPE_SIZE (type),
548 TYPE_SIZE (inner_type), 0)))
549 {
550 new = TREE_OPERAND (exp, 0);
551
552 /* If the new type is less strictly aligned than the inner type,
553 make a new type with the less strict alignment just for
554 code generation purposes of this node. If it is a decl,
555 we can't change the type, so make a NOP_EXPR. */
556 if (TYPE_ALIGN (type) != TYPE_ALIGN (inner_type))
557 {
558 tree copy_type = copy_node (inner_type);
559
560 TYPE_ALIGN (copy_type) = TYPE_ALIGN (type);
561 if (DECL_P (new))
562 new = build1 (NOP_EXPR, copy_type, new);
563 else
564 {
565 /* If NEW is a constant, it might be coming from a CONST_DECL
566 and hence shared. */
567 if (TREE_CONSTANT (new))
568 new = copy_node (new);
569
570 TREE_TYPE (new) = copy_type;
571 }
572 }
573 }
574
575 /* If either mode is BLKmode, memory will be involved, so do this
576 via pointer punning. Likewise, this doesn't work if there
577 is an alignment issue. But we must do it for types that are known
578 to be aligned properly. */
579 else if ((TYPE_MODE (type) == BLKmode
580 || TYPE_MODE (inner_type) == BLKmode)
581 && align_ok)
582 new = build_unary_op (INDIRECT_REF, NULL_TREE,
583 convert
584 (build_pointer_type (type),
585 build_unary_op (ADDR_EXPR, NULL_TREE,
586 TREE_OPERAND (exp, 0))));
587
588 /* Otherwise make a union of the two types, convert to the union, and
589 extract the other value. */
590 else
591 {
592 tree union_type, in_field, out_field;
593
594 /* If this is inside the LHS of an assignment, this would generate
595 bad code, so abort. */
596 if (TREE_ADDRESSABLE (exp))
597 gigi_abort (202);
598
599 union_type = make_node (UNION_TYPE);
600 in_field = create_field_decl (get_identifier ("in"),
601 inner_type, union_type, 0, 0, 0, 0);
602 out_field = create_field_decl (get_identifier ("out"),
603 type, union_type, 0, 0, 0, 0);
604
605 TYPE_FIELDS (union_type) = chainon (in_field, out_field);
606 layout_type (union_type);
607
608 /* Though this is a "union", we can treat its size as that of
609 the output type in case the size of the input type is variable.
610 If the output size is a variable, use the input size. */
611 TYPE_SIZE (union_type) = TYPE_SIZE (type);
612 TYPE_SIZE_UNIT (union_type) = TYPE_SIZE (type);
613 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
614 && TREE_CODE (TYPE_SIZE (inner_type)) == INTEGER_CST)
615 {
616 TYPE_SIZE (union_type) = TYPE_SIZE (inner_type);
617 TYPE_SIZE_UNIT (union_type) = TYPE_SIZE_UNIT (inner_type);
618 }
619
620 new = build (COMPONENT_REF, type,
621 build1 (CONVERT_EXPR, union_type,
622 TREE_OPERAND (exp, 0)),
623 out_field);
624 }
625
626 result = expand_expr (new, target, tmode, modifier);
627
628 if (GET_CODE (result) == MEM)
629 {
630 /* Update so it looks like this is of the proper type. */
631 set_mem_alias_set (result, 0);
632 set_mem_attributes (result, exp, 0);
633 }
634 return result;
635
636 case NULL_EXPR:
637 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 0);
638
639 /* We aren't going to be doing anything with this memory, but allocate
640 it anyway. If it's variable size, make a bogus address. */
641 if (! host_integerp (TYPE_SIZE_UNIT (type), 1))
642 result = gen_rtx_MEM (BLKmode, virtual_stack_vars_rtx);
643 else
644 result = assign_temp (type, 0, TREE_ADDRESSABLE (exp), 1);
645
646 return result;
647
648 case ALLOCATE_EXPR:
649 return
650 allocate_dynamic_stack_space
651 (expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, TYPE_MODE (sizetype),
652 EXPAND_NORMAL),
653 NULL_RTX, tree_low_cst (TREE_OPERAND (exp, 1), 1));
654
655 case USE_EXPR:
656 if (target != const0_rtx)
657 gigi_abort (203);
658
659 /* First write a volatile ASM_INPUT to prevent anything from being
660 moved. */
661 result = gen_rtx_ASM_INPUT (VOIDmode, "");
662 MEM_VOLATILE_P (result) = 1;
663 emit_insn (result);
664
665 result = expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, VOIDmode,
666 modifier);
667 emit_insn (gen_rtx_USE (VOIDmode, result));
668 return target;
669
670 case GNAT_NOP_EXPR:
671 return expand_expr (build1 (NOP_EXPR, type, TREE_OPERAND (exp, 0)),
672 target, tmode, modifier);
673
674 case UNCONSTRAINED_ARRAY_REF:
675 /* If we are evaluating just for side-effects, just evaluate our
676 operand. Otherwise, abort since this code should never appear
677 in a tree to be evaluated (objects aren't unconstrained). */
678 if (target == const0_rtx || TREE_CODE (type) == VOID_TYPE)
679 return expand_expr (TREE_OPERAND (exp, 0), const0_rtx,
680 VOIDmode, modifier);
681
682 /* ... fall through ... */
683
684 default:
685 gigi_abort (201);
686 }
687
688 return expand_expr (new, target, tmode, modifier);
689 }
690
691 /* Transform a constant into a form that the language-independent code
692 can handle. */
693
694 static tree
695 gnat_expand_constant (exp)
696 tree exp;
697 {
698 /* If this is an unchecked conversion that does not change the size of the
699 object and the object is not a CONSTRUCTOR return the operand since the
700 underlying constant is still the same. Otherwise, return our operand. */
701 if (TREE_CODE (exp) == UNCHECKED_CONVERT_EXPR
702 && operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (exp)),
703 TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (exp, 0))),
704 1)
705 && TREE_CODE (TREE_OPERAND (exp, 0)) != CONSTRUCTOR)
706 return TREE_OPERAND (exp, 0);
707
708 return exp;
709 }
710
711 /* Adjusts the RLI used to layout a record after all the fields have been
712 added. We only handle the packed case and cause it to use the alignment
713 that will pad the record at the end. */
714
715 static void
716 gnat_adjust_rli (rli)
717 record_layout_info rli;
718 {
719 if (TYPE_PACKED (rli->t))
720 rli->record_align = rli->unpadded_align;
721 }
722
723 /* Make a TRANSFORM_EXPR to later expand GNAT_NODE into code. */
724
725 tree
726 make_transform_expr (gnat_node)
727 Node_Id gnat_node;
728 {
729 tree gnu_result = build (TRANSFORM_EXPR, void_type_node);
730
731 TREE_SIDE_EFFECTS (gnu_result) = 1;
732 TREE_COMPLEXITY (gnu_result) = gnat_node;
733 return gnu_result;
734 }
735 \f
736 /* Update the setjmp buffer BUF with the current stack pointer. We assume
737 here that a __builtin_setjmp was done to BUF. */
738
739 void
740 update_setjmp_buf (buf)
741 tree buf;
742 {
743 enum machine_mode sa_mode = Pmode;
744 rtx stack_save;
745
746 #ifdef HAVE_save_stack_nonlocal
747 if (HAVE_save_stack_nonlocal)
748 sa_mode = insn_data [(int) CODE_FOR_save_stack_nonlocal].operand[0].mode;
749 #endif
750
751 #ifdef STACK_SAVEAREA_MODE
752 sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
753 #endif
754
755 stack_save
756 = gen_rtx_MEM (sa_mode,
757 memory_address
758 (sa_mode,
759 plus_constant (expand_expr
760 (build_unary_op (ADDR_EXPR, NULL_TREE, buf),
761 NULL_RTX, VOIDmode, 0),
762 2 * GET_MODE_SIZE (Pmode))));
763
764 #ifdef HAVE_setjmp
765 if (HAVE_setjmp)
766 emit_insn (gen_setjmp ());
767 #endif
768
769 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
770 }
771 \f
772 /* See if DECL has an RTL that is indirect via a pseudo-register or a
773 memory location and replace it with an indirect reference if so.
774 This improves the debugger's ability to display the value. */
775
776 void
777 adjust_decl_rtl (decl)
778 tree decl;
779 {
780 tree new_type;
781
782 /* If this decl is already indirect, don't do anything. This should
783 mean that the decl cannot be indirect, but there's no point in
784 adding an abort to check that. */
785 if (TREE_CODE (decl) != CONST_DECL
786 && ! DECL_BY_REF_P (decl)
787 && (GET_CODE (DECL_RTL (decl)) == MEM
788 && (GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
789 || (GET_CODE (XEXP (DECL_RTL (decl), 0)) == REG
790 && (REGNO (XEXP (DECL_RTL (decl), 0))
791 > LAST_VIRTUAL_REGISTER))))
792 /* We can't do this if the reference type's mode is not the same
793 as the current mode, which means this may not work on mixed 32/64
794 bit systems. */
795 && (new_type = build_reference_type (TREE_TYPE (decl))) != 0
796 && TYPE_MODE (new_type) == GET_MODE (XEXP (DECL_RTL (decl), 0))
797 /* If this is a PARM_DECL, we can only do it if DECL_INCOMING_RTL
798 is also an indirect and of the same mode and if the object is
799 readonly, the latter condition because we don't want to upset the
800 handling of CICO_LIST. */
801 && (TREE_CODE (decl) != PARM_DECL
802 || (GET_CODE (DECL_INCOMING_RTL (decl)) == MEM
803 && (TYPE_MODE (new_type)
804 == GET_MODE (XEXP (DECL_INCOMING_RTL (decl), 0)))
805 && TREE_READONLY (decl))))
806 {
807 new_type
808 = build_qualified_type (new_type,
809 (TYPE_QUALS (new_type) | TYPE_QUAL_CONST));
810
811 DECL_POINTS_TO_READONLY_P (decl) = TREE_READONLY (decl);
812 DECL_BY_REF_P (decl) = 1;
813 SET_DECL_RTL (decl, XEXP (DECL_RTL (decl), 0));
814 TREE_TYPE (decl) = new_type;
815 DECL_MODE (decl) = TYPE_MODE (new_type);
816 DECL_ALIGN (decl) = TYPE_ALIGN (new_type);
817 DECL_SIZE (decl) = TYPE_SIZE (new_type);
818
819 if (TREE_CODE (decl) == PARM_DECL)
820 DECL_INCOMING_RTL (decl) = XEXP (DECL_INCOMING_RTL (decl), 0);
821
822 /* If DECL_INITIAL was set, it should be updated to show that
823 the decl is initialized to the address of that thing.
824 Otherwise, just set it to the address of this decl.
825 It needs to be set so that GCC does not think the decl is
826 unused. */
827 DECL_INITIAL (decl)
828 = build1 (ADDR_EXPR, new_type,
829 DECL_INITIAL (decl) != 0 ? DECL_INITIAL (decl) : decl);
830 }
831 }
832 \f
833 /* Record the current code position in GNAT_NODE. */
834
835 void
836 record_code_position (gnat_node)
837 Node_Id gnat_node;
838 {
839 if (global_bindings_p ())
840 {
841 /* Make a dummy entry so multiple things at the same location don't
842 end up in the same place. */
843 add_pending_elaborations (NULL_TREE, NULL_TREE);
844 save_gnu_tree (gnat_node, get_elaboration_location (), 1);
845 }
846 else
847 /* Always emit another insn in case marking the last insn
848 addressable needs some fixups and also for above reason. */
849 save_gnu_tree (gnat_node,
850 build (RTL_EXPR, void_type_node, NULL_TREE,
851 (tree) emit_note (0, NOTE_INSN_DELETED)),
852 1);
853 }
854
855 /* Insert the code for GNAT_NODE at the position saved for that node. */
856
857 void
858 insert_code_for (gnat_node)
859 Node_Id gnat_node;
860 {
861 if (global_bindings_p ())
862 {
863 push_pending_elaborations ();
864 gnat_to_code (gnat_node);
865 Check_Elaboration_Code_Allowed (gnat_node);
866 insert_elaboration_list (get_gnu_tree (gnat_node));
867 pop_pending_elaborations ();
868 }
869 else
870 {
871 rtx insns;
872
873 start_sequence ();
874 mark_all_temps_used ();
875 gnat_to_code (gnat_node);
876 insns = get_insns ();
877 end_sequence ();
878 emit_insns_after (insns, RTL_EXPR_RTL (get_gnu_tree (gnat_node)));
879 }
880 }
881
882 #if 0
883
884 /* Return the alignment for GNAT_TYPE. */
885
886 unsigned int
887 get_type_alignment (gnat_type)
888 Entity_Id gnat_type;
889 {
890 return TYPE_ALIGN (gnat_to_gnu_type (gnat_type)) / BITS_PER_UNIT;
891 }
892 #endif
893
894 /* Get the alias set corresponding to a type or expression. */
895
896 static HOST_WIDE_INT
897 gnat_get_alias_set (type)
898 tree type;
899 {
900 /* If this is a padding type, use the type of the first field. */
901 if (TREE_CODE (type) == RECORD_TYPE
902 && TYPE_IS_PADDING_P (type))
903 return get_alias_set (TREE_TYPE (TYPE_FIELDS (type)));
904
905 return -1;
906 }
907
908 /* Set default attributes for functions. We do nothing. */
909
910 void
911 insert_default_attributes (decl)
912 tree decl ATTRIBUTE_UNUSED;
913 {
914 }
915
916 /* GNU_TYPE is a type. Determine if it should be passed by reference by
917 default. */
918
919 int
920 default_pass_by_ref (gnu_type)
921 tree gnu_type;
922 {
923 CUMULATIVE_ARGS cum;
924
925 INIT_CUMULATIVE_ARGS (cum, NULL_TREE, NULL_RTX, 0);
926
927 /* We pass aggregates by reference if they are sufficiently large. The
928 choice of constant here is somewhat arbitrary. We also pass by
929 reference if the target machine would either pass or return by
930 reference. Strictly speaking, we need only check the return if this
931 is an In Out parameter, but it's probably best to err on the side of
932 passing more things by reference. */
933 return (0
934 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
935 || FUNCTION_ARG_PASS_BY_REFERENCE (cum, TYPE_MODE (gnu_type),
936 gnu_type, 1)
937 #endif
938 || RETURN_IN_MEMORY (gnu_type)
939 || (AGGREGATE_TYPE_P (gnu_type)
940 && (! host_integerp (TYPE_SIZE (gnu_type), 1)
941 || 0 < compare_tree_int (TYPE_SIZE (gnu_type),
942 8 * TYPE_ALIGN (gnu_type)))));
943 }
944
945 /* GNU_TYPE is the type of a subprogram parameter. Determine from the type if
946 it should be passed by reference. */
947
948 int
949 must_pass_by_ref (gnu_type)
950 tree gnu_type;
951 {
952 /* We pass only unconstrained objects, those required by the language
953 to be passed by reference, and objects of variable size. The latter
954 is more efficient, avoids problems with variable size temporaries,
955 and does not produce compatibility problems with C, since C does
956 not have such objects. */
957 return (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
958 || (AGGREGATE_TYPE_P (gnu_type) && TYPE_BY_REFERENCE_P (gnu_type))
959 || (TYPE_SIZE (gnu_type) != 0
960 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST));
961 }
962 \f
963 #if defined(MIPS_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO)
964
965 /* Convert NAME, which is possibly an Ada name, back to standard Ada
966 notation for SGI Workshop. */
967
968 static char *
969 convert_ada_name_to_qualified_name (name)
970 char *name;
971 {
972 int len = strlen (name);
973 char *new_name = xstrdup (name);
974 char *buf;
975 int i, start;
976 char *qual_name_suffix = 0;
977 char *p;
978
979 if (len <= 3 || use_gnu_debug_info_extensions)
980 {
981 free (new_name);
982 return name;
983 }
984
985 /* Find the position of the first "__" after the first character of
986 NAME. This is the same as calling strstr except that we can't assume
987 the host has that function. We start after the first character so
988 we don't eliminate leading "__": these are emitted only by C
989 programs and are not qualified names */
990 for (p = (char *) index (&name[1], '_'); p != 0;
991 p = (char *) index (p+1, '_'))
992 if (p[1] == '_')
993 {
994 qual_name_suffix = p;
995 break;
996 }
997
998 if (qual_name_suffix == 0)
999 {
1000 free (new_name);
1001 return name;
1002 }
1003
1004 start = qual_name_suffix - name;
1005 buf = new_name + start;
1006
1007 for (i = start; i < len; i++)
1008 {
1009 if (name[i] == '_' && name[i + 1] == '_')
1010 {
1011 if (islower (name[i + 2]))
1012 {
1013 *buf++ = '.';
1014 *buf++ = name[i + 2];
1015 i += 2;
1016 }
1017 else if (name[i + 2] == '_' && islower (name[i + 3]))
1018 {
1019 /* convert foo___c___XVN to foo.c___XVN */
1020 *buf++ = '.';
1021 *buf++ = name[i + 3];
1022 i += 3;
1023 }
1024 else if (name[i + 2] == 'T')
1025 {
1026 /* convert foo__TtypeS to foo.__TTypeS */
1027 *buf++ = '.';
1028 *buf++ = '_';
1029 *buf++ = '_';
1030 *buf++ = 'T';
1031 i += 3;
1032 }
1033 else
1034 *buf++ = name[i];
1035 }
1036 else
1037 *buf++ = name[i];
1038 }
1039
1040 *buf = 0;
1041 return new_name;
1042 }
1043 #endif
1044
1045 /* Emit a label UNITNAME_LABEL and specify that it is part of source
1046 file FILENAME. If this is being written for SGI's Workshop
1047 debugger, and we are writing Dwarf2 debugging information, add
1048 additional debug info. */
1049
1050 void
1051 emit_unit_label (unitname_label, filename)
1052 char *unitname_label;
1053 char *filename ATTRIBUTE_UNUSED;
1054 {
1055 ASM_GLOBALIZE_LABEL (asm_out_file, unitname_label);
1056 ASM_OUTPUT_LABEL (asm_out_file, unitname_label);
1057 }