1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2007, Free Software Foundation, Inc. *
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- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
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 *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
25 ****************************************************************************/
29 #include "coretypes.h"
50 static tree find_common_type (tree, tree);
51 static bool contains_save_expr_p (tree);
52 static tree contains_null_expr (tree);
53 static tree compare_arrays (tree, tree, tree);
54 static tree nonbinary_modular_operation (enum tree_code, tree, tree, tree);
55 static tree build_simple_component_ref (tree, tree, tree, bool);
57 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR or other logical
60 This preparation consists of taking the ordinary representation of
61 an expression expr and producing a valid tree boolean expression
62 describing whether expr is nonzero. We could simply always do
64 build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
66 but we optimize comparisons, &&, ||, and !.
68 The resulting type should always be the same as the input type.
69 This function is simpler than the corresponding C version since
70 the only possible operands will be things of Boolean type. */
73 gnat_truthvalue_conversion (tree expr)
75 tree type = TREE_TYPE (expr);
77 switch (TREE_CODE (expr))
79 case EQ_EXPR: case NE_EXPR: case LE_EXPR: case GE_EXPR:
80 case LT_EXPR: case GT_EXPR:
81 case TRUTH_ANDIF_EXPR:
90 return (integer_zerop (expr)
91 ? build_int_cst (type, 0)
92 : build_int_cst (type, 1));
95 return (real_zerop (expr)
96 ? fold_convert (type, integer_zero_node)
97 : fold_convert (type, integer_one_node));
100 /* Distribute the conversion into the arms of a COND_EXPR. */
102 tree arg1 = gnat_truthvalue_conversion (TREE_OPERAND (expr, 1));
103 tree arg2 = gnat_truthvalue_conversion (TREE_OPERAND (expr, 2));
104 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
109 return build_binary_op (NE_EXPR, type, expr,
110 fold_convert (type, integer_zero_node));
114 /* Return the base type of TYPE. */
117 get_base_type (tree type)
119 if (TREE_CODE (type) == RECORD_TYPE
120 && TYPE_JUSTIFIED_MODULAR_P (type))
121 type = TREE_TYPE (TYPE_FIELDS (type));
123 while (TREE_TYPE (type)
124 && (TREE_CODE (type) == INTEGER_TYPE
125 || TREE_CODE (type) == REAL_TYPE))
126 type = TREE_TYPE (type);
131 /* EXP is a GCC tree representing an address. See if we can find how
132 strictly the object at that address is aligned. Return that alignment
133 in bits. If we don't know anything about the alignment, return 0. */
136 known_alignment (tree exp)
138 unsigned int this_alignment;
139 unsigned int lhs, rhs;
140 unsigned int type_alignment;
142 /* For pointer expressions, we know that the designated object is always at
143 least as strictly aligned as the designated subtype, so we account for
144 both type and expression information in this case.
146 Beware that we can still get a dummy designated subtype here (e.g. Taft
147 Amendement types), in which the alignment information is meaningless and
150 We always compute a type_alignment value and return the MAX of it
151 compared with what we get from the expression tree. Just set the
152 type_alignment value to 0 when the type information is to be ignored. */
154 = ((POINTER_TYPE_P (TREE_TYPE (exp))
155 && !TYPE_IS_DUMMY_P (TREE_TYPE (TREE_TYPE (exp))))
156 ? TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))) : 0);
158 switch (TREE_CODE (exp))
161 case VIEW_CONVERT_EXPR:
163 case NON_LVALUE_EXPR:
164 /* Conversions between pointers and integers don't change the alignment
165 of the underlying object. */
166 this_alignment = known_alignment (TREE_OPERAND (exp, 0));
170 /* The value of a COMPOUND_EXPR is that of it's second operand. */
171 this_alignment = known_alignment (TREE_OPERAND (exp, 1));
176 /* If two address are added, the alignment of the result is the
177 minimum of the two alignments. */
178 lhs = known_alignment (TREE_OPERAND (exp, 0));
179 rhs = known_alignment (TREE_OPERAND (exp, 1));
180 this_alignment = MIN (lhs, rhs);
184 /* If there is a choice between two values, use the smallest one. */
185 lhs = known_alignment (TREE_OPERAND (exp, 1));
186 rhs = known_alignment (TREE_OPERAND (exp, 2));
187 this_alignment = MIN (lhs, rhs);
191 /* The first part of this represents the lowest bit in the constant,
192 but is it in bytes, not bits. */
195 * (TREE_INT_CST_LOW (exp) & - TREE_INT_CST_LOW (exp)),
200 /* If we know the alignment of just one side, use it. Otherwise,
201 use the product of the alignments. */
202 lhs = known_alignment (TREE_OPERAND (exp, 0));
203 rhs = known_alignment (TREE_OPERAND (exp, 1));
205 if (lhs == 0 || rhs == 0)
206 this_alignment = MIN (BIGGEST_ALIGNMENT, MAX (lhs, rhs));
208 this_alignment = MIN (BIGGEST_ALIGNMENT, lhs * rhs);
212 /* A bit-and expression is as aligned as the maximum alignment of the
213 operands. We typically get here for a complex lhs and a constant
214 negative power of two on the rhs to force an explicit alignment, so
215 don't bother looking at the lhs. */
216 this_alignment = known_alignment (TREE_OPERAND (exp, 1));
220 this_alignment = expr_align (TREE_OPERAND (exp, 0));
228 return MAX (type_alignment, this_alignment);
231 /* We have a comparison or assignment operation on two types, T1 and T2,
232 which are both either array types or both record types.
233 Return the type that both operands should be converted to, if any.
234 Otherwise return zero. */
237 find_common_type (tree t1, tree t2)
239 /* If either type is non-BLKmode, use it. Note that we know that we will
240 not have any alignment problems since if we did the non-BLKmode
241 type could not have been used. */
242 if (TYPE_MODE (t1) != BLKmode)
244 else if (TYPE_MODE (t2) != BLKmode)
247 /* If both types have constant size, use the smaller one. Keep returning
248 T1 if we have a tie, to be consistent with the other cases. */
249 if (TREE_CONSTANT (TYPE_SIZE (t1)) && TREE_CONSTANT (TYPE_SIZE (t2)))
250 return tree_int_cst_lt (TYPE_SIZE (t2), TYPE_SIZE (t1)) ? t2 : t1;
252 /* Otherwise, if either type has a constant size, use it. */
253 else if (TREE_CONSTANT (TYPE_SIZE (t1)))
255 else if (TREE_CONSTANT (TYPE_SIZE (t2)))
258 /* In this case, both types have variable size. It's probably
259 best to leave the "type mismatch" because changing it could
260 case a bad self-referential reference. */
264 /* See if EXP contains a SAVE_EXPR in a position where we would
267 ??? This is a real kludge, but is probably the best approach short
268 of some very general solution. */
271 contains_save_expr_p (tree exp)
273 switch (TREE_CODE (exp))
278 case ADDR_EXPR: case INDIRECT_REF:
280 case NOP_EXPR: case CONVERT_EXPR: case VIEW_CONVERT_EXPR:
281 return contains_save_expr_p (TREE_OPERAND (exp, 0));
286 unsigned HOST_WIDE_INT ix;
288 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), ix, value)
289 if (contains_save_expr_p (value))
299 /* See if EXP contains a NULL_EXPR in an expression we use for sizes. Return
300 it if so. This is used to detect types whose sizes involve computations
301 that are known to raise Constraint_Error. */
304 contains_null_expr (tree exp)
308 if (TREE_CODE (exp) == NULL_EXPR)
311 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
314 return contains_null_expr (TREE_OPERAND (exp, 0));
318 tem = contains_null_expr (TREE_OPERAND (exp, 0));
322 return contains_null_expr (TREE_OPERAND (exp, 1));
325 switch (TREE_CODE (exp))
328 return contains_null_expr (TREE_OPERAND (exp, 0));
331 tem = contains_null_expr (TREE_OPERAND (exp, 0));
335 tem = contains_null_expr (TREE_OPERAND (exp, 1));
339 return contains_null_expr (TREE_OPERAND (exp, 2));
350 /* Return an expression tree representing an equality comparison of
351 A1 and A2, two objects of ARRAY_TYPE. The returned expression should
352 be of type RESULT_TYPE
354 Two arrays are equal in one of two ways: (1) if both have zero length
355 in some dimension (not necessarily the same dimension) or (2) if the
356 lengths in each dimension are equal and the data is equal. We perform the
357 length tests in as efficient a manner as possible. */
360 compare_arrays (tree result_type, tree a1, tree a2)
362 tree t1 = TREE_TYPE (a1);
363 tree t2 = TREE_TYPE (a2);
364 tree result = convert (result_type, integer_one_node);
365 tree a1_is_null = convert (result_type, integer_zero_node);
366 tree a2_is_null = convert (result_type, integer_zero_node);
367 bool length_zero_p = false;
369 /* Process each dimension separately and compare the lengths. If any
370 dimension has a size known to be zero, set SIZE_ZERO_P to 1 to
371 suppress the comparison of the data. */
372 while (TREE_CODE (t1) == ARRAY_TYPE && TREE_CODE (t2) == ARRAY_TYPE)
374 tree lb1 = TYPE_MIN_VALUE (TYPE_DOMAIN (t1));
375 tree ub1 = TYPE_MAX_VALUE (TYPE_DOMAIN (t1));
376 tree lb2 = TYPE_MIN_VALUE (TYPE_DOMAIN (t2));
377 tree ub2 = TYPE_MAX_VALUE (TYPE_DOMAIN (t2));
378 tree bt = get_base_type (TREE_TYPE (lb1));
379 tree length1 = fold_build2 (MINUS_EXPR, bt, ub1, lb1);
380 tree length2 = fold_build2 (MINUS_EXPR, bt, ub2, lb2);
383 tree comparison, this_a1_is_null, this_a2_is_null;
385 /* If the length of the first array is a constant, swap our operands
386 unless the length of the second array is the constant zero.
387 Note that we have set the `length' values to the length - 1. */
388 if (TREE_CODE (length1) == INTEGER_CST
389 && !integer_zerop (fold_build2 (PLUS_EXPR, bt, length2,
390 convert (bt, integer_one_node))))
392 tem = a1, a1 = a2, a2 = tem;
393 tem = t1, t1 = t2, t2 = tem;
394 tem = lb1, lb1 = lb2, lb2 = tem;
395 tem = ub1, ub1 = ub2, ub2 = tem;
396 tem = length1, length1 = length2, length2 = tem;
397 tem = a1_is_null, a1_is_null = a2_is_null, a2_is_null = tem;
400 /* If the length of this dimension in the second array is the constant
401 zero, we can just go inside the original bounds for the first
402 array and see if last < first. */
403 if (integer_zerop (fold_build2 (PLUS_EXPR, bt, length2,
404 convert (bt, integer_one_node))))
406 tree ub = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
407 tree lb = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
409 comparison = build_binary_op (LT_EXPR, result_type, ub, lb);
410 comparison = SUBSTITUTE_PLACEHOLDER_IN_EXPR (comparison, a1);
411 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
413 length_zero_p = true;
414 this_a1_is_null = comparison;
415 this_a2_is_null = convert (result_type, integer_one_node);
418 /* If the length is some other constant value, we know that the
419 this dimension in the first array cannot be superflat, so we
420 can just use its length from the actual stored bounds. */
421 else if (TREE_CODE (length2) == INTEGER_CST)
423 ub1 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
424 lb1 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t1)));
425 ub2 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t2)));
426 lb2 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (t2)));
427 nbt = get_base_type (TREE_TYPE (ub1));
430 = build_binary_op (EQ_EXPR, result_type,
431 build_binary_op (MINUS_EXPR, nbt, ub1, lb1),
432 build_binary_op (MINUS_EXPR, nbt, ub2, lb2));
434 /* Note that we know that UB2 and LB2 are constant and hence
435 cannot contain a PLACEHOLDER_EXPR. */
437 comparison = SUBSTITUTE_PLACEHOLDER_IN_EXPR (comparison, a1);
438 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
440 this_a1_is_null = build_binary_op (LT_EXPR, result_type, ub1, lb1);
441 this_a2_is_null = convert (result_type, integer_zero_node);
444 /* Otherwise compare the computed lengths. */
447 length1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length1, a1);
448 length2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (length2, a2);
451 = build_binary_op (EQ_EXPR, result_type, length1, length2);
454 = build_binary_op (LT_EXPR, result_type, length1,
455 convert (bt, integer_zero_node));
457 = build_binary_op (LT_EXPR, result_type, length2,
458 convert (bt, integer_zero_node));
461 result = build_binary_op (TRUTH_ANDIF_EXPR, result_type,
464 a1_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
465 this_a1_is_null, a1_is_null);
466 a2_is_null = build_binary_op (TRUTH_ORIF_EXPR, result_type,
467 this_a2_is_null, a2_is_null);
473 /* Unless the size of some bound is known to be zero, compare the
474 data in the array. */
477 tree type = find_common_type (TREE_TYPE (a1), TREE_TYPE (a2));
480 a1 = convert (type, a1), a2 = convert (type, a2);
482 result = build_binary_op (TRUTH_ANDIF_EXPR, result_type, result,
483 fold_build2 (EQ_EXPR, result_type, a1, a2));
487 /* The result is also true if both sizes are zero. */
488 result = build_binary_op (TRUTH_ORIF_EXPR, result_type,
489 build_binary_op (TRUTH_ANDIF_EXPR, result_type,
490 a1_is_null, a2_is_null),
493 /* If either operand contains SAVE_EXPRs, they have to be evaluated before
494 starting the comparison above since the place it would be otherwise
495 evaluated would be wrong. */
497 if (contains_save_expr_p (a1))
498 result = build2 (COMPOUND_EXPR, result_type, a1, result);
500 if (contains_save_expr_p (a2))
501 result = build2 (COMPOUND_EXPR, result_type, a2, result);
506 /* Compute the result of applying OP_CODE to LHS and RHS, where both are of
507 type TYPE. We know that TYPE is a modular type with a nonbinary
511 nonbinary_modular_operation (enum tree_code op_code, tree type, tree lhs,
514 tree modulus = TYPE_MODULUS (type);
515 unsigned int needed_precision = tree_floor_log2 (modulus) + 1;
516 unsigned int precision;
517 bool unsignedp = true;
521 /* If this is an addition of a constant, convert it to a subtraction
522 of a constant since we can do that faster. */
523 if (op_code == PLUS_EXPR && TREE_CODE (rhs) == INTEGER_CST)
525 rhs = fold_build2 (MINUS_EXPR, type, modulus, rhs);
526 op_code = MINUS_EXPR;
529 /* For the logical operations, we only need PRECISION bits. For
530 addition and subtraction, we need one more and for multiplication we
531 need twice as many. But we never want to make a size smaller than
533 if (op_code == PLUS_EXPR || op_code == MINUS_EXPR)
534 needed_precision += 1;
535 else if (op_code == MULT_EXPR)
536 needed_precision *= 2;
538 precision = MAX (needed_precision, TYPE_PRECISION (op_type));
540 /* Unsigned will do for everything but subtraction. */
541 if (op_code == MINUS_EXPR)
544 /* If our type is the wrong signedness or isn't wide enough, make a new
545 type and convert both our operands to it. */
546 if (TYPE_PRECISION (op_type) < precision
547 || TYPE_UNSIGNED (op_type) != unsignedp)
549 /* Copy the node so we ensure it can be modified to make it modular. */
550 op_type = copy_node (gnat_type_for_size (precision, unsignedp));
551 modulus = convert (op_type, modulus);
552 SET_TYPE_MODULUS (op_type, modulus);
553 TYPE_MODULAR_P (op_type) = 1;
554 lhs = convert (op_type, lhs);
555 rhs = convert (op_type, rhs);
558 /* Do the operation, then we'll fix it up. */
559 result = fold_build2 (op_code, op_type, lhs, rhs);
561 /* For multiplication, we have no choice but to do a full modulus
562 operation. However, we want to do this in the narrowest
564 if (op_code == MULT_EXPR)
566 tree div_type = copy_node (gnat_type_for_size (needed_precision, 1));
567 modulus = convert (div_type, modulus);
568 SET_TYPE_MODULUS (div_type, modulus);
569 TYPE_MODULAR_P (div_type) = 1;
570 result = convert (op_type,
571 fold_build2 (TRUNC_MOD_EXPR, div_type,
572 convert (div_type, result), modulus));
575 /* For subtraction, add the modulus back if we are negative. */
576 else if (op_code == MINUS_EXPR)
578 result = save_expr (result);
579 result = fold_build3 (COND_EXPR, op_type,
580 fold_build2 (LT_EXPR, integer_type_node, result,
581 convert (op_type, integer_zero_node)),
582 fold_build2 (PLUS_EXPR, op_type, result, modulus),
586 /* For the other operations, subtract the modulus if we are >= it. */
589 result = save_expr (result);
590 result = fold_build3 (COND_EXPR, op_type,
591 fold_build2 (GE_EXPR, integer_type_node,
593 fold_build2 (MINUS_EXPR, op_type,
598 return convert (type, result);
601 /* Make a binary operation of kind OP_CODE. RESULT_TYPE is the type
602 desired for the result. Usually the operation is to be performed
603 in that type. For MODIFY_EXPR and ARRAY_REF, RESULT_TYPE may be 0
604 in which case the type to be used will be derived from the operands.
606 This function is very much unlike the ones for C and C++ since we
607 have already done any type conversion and matching required. All we
608 have to do here is validate the work done by SEM and handle subtypes. */
611 build_binary_op (enum tree_code op_code, tree result_type,
612 tree left_operand, tree right_operand)
614 tree left_type = TREE_TYPE (left_operand);
615 tree right_type = TREE_TYPE (right_operand);
616 tree left_base_type = get_base_type (left_type);
617 tree right_base_type = get_base_type (right_type);
618 tree operation_type = result_type;
619 tree best_type = NULL_TREE;
622 bool has_side_effects = false;
625 && TREE_CODE (operation_type) == RECORD_TYPE
626 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
627 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
630 && !AGGREGATE_TYPE_P (operation_type)
631 && TYPE_EXTRA_SUBTYPE_P (operation_type))
632 operation_type = get_base_type (operation_type);
634 modulus = (operation_type && TREE_CODE (operation_type) == INTEGER_TYPE
635 && TYPE_MODULAR_P (operation_type)
636 ? TYPE_MODULUS (operation_type) : 0);
641 /* If there were any integral or pointer conversions on LHS, remove
642 them; we'll be putting them back below if needed. Likewise for
643 conversions between array and record types. But don't do this if
644 the right operand is not BLKmode (for packed arrays)
645 unless we are not changing the mode. */
646 while ((TREE_CODE (left_operand) == CONVERT_EXPR
647 || TREE_CODE (left_operand) == NOP_EXPR
648 || TREE_CODE (left_operand) == VIEW_CONVERT_EXPR)
649 && (((INTEGRAL_TYPE_P (left_type)
650 || POINTER_TYPE_P (left_type))
651 && (INTEGRAL_TYPE_P (TREE_TYPE
652 (TREE_OPERAND (left_operand, 0)))
653 || POINTER_TYPE_P (TREE_TYPE
654 (TREE_OPERAND (left_operand, 0)))))
655 || (((TREE_CODE (left_type) == RECORD_TYPE
656 /* Don't remove conversions to justified modular
658 && !TYPE_JUSTIFIED_MODULAR_P (left_type))
659 || TREE_CODE (left_type) == ARRAY_TYPE)
660 && ((TREE_CODE (TREE_TYPE
661 (TREE_OPERAND (left_operand, 0)))
663 || (TREE_CODE (TREE_TYPE
664 (TREE_OPERAND (left_operand, 0)))
666 && (TYPE_MODE (right_type) == BLKmode
667 || (TYPE_MODE (left_type)
668 == TYPE_MODE (TREE_TYPE
670 (left_operand, 0))))))))
672 left_operand = TREE_OPERAND (left_operand, 0);
673 left_type = TREE_TYPE (left_operand);
677 operation_type = left_type;
679 /* If we are copying one array or record to another, find the best type
681 if (((TREE_CODE (left_type) == ARRAY_TYPE
682 && TREE_CODE (right_type) == ARRAY_TYPE)
683 || (TREE_CODE (left_type) == RECORD_TYPE
684 && TREE_CODE (right_type) == RECORD_TYPE))
685 && (best_type = find_common_type (left_type, right_type)))
686 operation_type = best_type;
688 /* If a class-wide type may be involved, force use of the RHS type. */
689 if ((TREE_CODE (right_type) == RECORD_TYPE
690 || TREE_CODE (right_type) == UNION_TYPE)
691 && TYPE_ALIGN_OK (right_type))
692 operation_type = right_type;
694 /* Ensure everything on the LHS is valid. If we have a field reference,
695 strip anything that get_inner_reference can handle. Then remove any
696 conversions with type types having the same code and mode. Mark
697 VIEW_CONVERT_EXPRs with TREE_ADDRESSABLE. When done, we must have
698 either an INDIRECT_REF or a decl. */
699 result = left_operand;
702 tree restype = TREE_TYPE (result);
704 if (TREE_CODE (result) == COMPONENT_REF
705 || TREE_CODE (result) == ARRAY_REF
706 || TREE_CODE (result) == ARRAY_RANGE_REF)
707 while (handled_component_p (result))
708 result = TREE_OPERAND (result, 0);
709 else if (TREE_CODE (result) == REALPART_EXPR
710 || TREE_CODE (result) == IMAGPART_EXPR
711 || ((TREE_CODE (result) == NOP_EXPR
712 || TREE_CODE (result) == CONVERT_EXPR)
713 && (((TREE_CODE (restype)
714 == TREE_CODE (TREE_TYPE
715 (TREE_OPERAND (result, 0))))
716 && (TYPE_MODE (TREE_TYPE
717 (TREE_OPERAND (result, 0)))
718 == TYPE_MODE (restype)))
719 || TYPE_ALIGN_OK (restype))))
720 result = TREE_OPERAND (result, 0);
721 else if (TREE_CODE (result) == VIEW_CONVERT_EXPR)
723 TREE_ADDRESSABLE (result) = 1;
724 result = TREE_OPERAND (result, 0);
730 gcc_assert (TREE_CODE (result) == INDIRECT_REF
731 || TREE_CODE (result) == NULL_EXPR || DECL_P (result));
733 /* Convert the right operand to the operation type unless
734 it is either already of the correct type or if the type
735 involves a placeholder, since the RHS may not have the same
737 if (operation_type != right_type
738 && (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (operation_type))))
740 right_operand = convert (operation_type, right_operand);
741 right_type = operation_type;
744 /* If the left operand is not the same type as the operation type,
745 surround it in a VIEW_CONVERT_EXPR. */
746 if (left_type != operation_type)
747 left_operand = unchecked_convert (operation_type, left_operand, false);
749 has_side_effects = true;
755 operation_type = TREE_TYPE (left_type);
757 /* ... fall through ... */
759 case ARRAY_RANGE_REF:
761 /* First convert the right operand to its base type. This will
762 prevent unneeded signedness conversions when sizetype is wider than
764 right_operand = convert (right_base_type, right_operand);
765 right_operand = convert (TYPE_DOMAIN (left_type), right_operand);
767 if (!TREE_CONSTANT (right_operand)
768 || !TREE_CONSTANT (TYPE_MIN_VALUE (right_type)))
769 gnat_mark_addressable (left_operand);
778 gcc_assert (!POINTER_TYPE_P (left_type));
780 /* ... fall through ... */
784 /* If either operand is a NULL_EXPR, just return a new one. */
785 if (TREE_CODE (left_operand) == NULL_EXPR)
786 return build2 (op_code, result_type,
787 build1 (NULL_EXPR, integer_type_node,
788 TREE_OPERAND (left_operand, 0)),
791 else if (TREE_CODE (right_operand) == NULL_EXPR)
792 return build2 (op_code, result_type,
793 build1 (NULL_EXPR, integer_type_node,
794 TREE_OPERAND (right_operand, 0)),
797 /* If either object is a justified modular types, get the
798 fields from within. */
799 if (TREE_CODE (left_type) == RECORD_TYPE
800 && TYPE_JUSTIFIED_MODULAR_P (left_type))
802 left_operand = convert (TREE_TYPE (TYPE_FIELDS (left_type)),
804 left_type = TREE_TYPE (left_operand);
805 left_base_type = get_base_type (left_type);
808 if (TREE_CODE (right_type) == RECORD_TYPE
809 && TYPE_JUSTIFIED_MODULAR_P (right_type))
811 right_operand = convert (TREE_TYPE (TYPE_FIELDS (right_type)),
813 right_type = TREE_TYPE (right_operand);
814 right_base_type = get_base_type (right_type);
817 /* If both objects are arrays, compare them specially. */
818 if ((TREE_CODE (left_type) == ARRAY_TYPE
819 || (TREE_CODE (left_type) == INTEGER_TYPE
820 && TYPE_HAS_ACTUAL_BOUNDS_P (left_type)))
821 && (TREE_CODE (right_type) == ARRAY_TYPE
822 || (TREE_CODE (right_type) == INTEGER_TYPE
823 && TYPE_HAS_ACTUAL_BOUNDS_P (right_type))))
825 result = compare_arrays (result_type, left_operand, right_operand);
827 if (op_code == NE_EXPR)
828 result = invert_truthvalue (result);
830 gcc_assert (op_code == EQ_EXPR);
835 /* Otherwise, the base types must be the same unless the objects are
836 records. If we have records, use the best type and convert both
837 operands to that type. */
838 if (left_base_type != right_base_type)
840 if (TREE_CODE (left_base_type) == RECORD_TYPE
841 && TREE_CODE (right_base_type) == RECORD_TYPE)
843 /* The only way these are permitted to be the same is if both
844 types have the same name. In that case, one of them must
845 not be self-referential. Use that one as the best type.
846 Even better is if one is of fixed size. */
847 best_type = NULL_TREE;
849 gcc_assert (TYPE_NAME (left_base_type)
850 && (TYPE_NAME (left_base_type)
851 == TYPE_NAME (right_base_type)));
853 if (TREE_CONSTANT (TYPE_SIZE (left_base_type)))
854 best_type = left_base_type;
855 else if (TREE_CONSTANT (TYPE_SIZE (right_base_type)))
856 best_type = right_base_type;
857 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (left_base_type)))
858 best_type = left_base_type;
859 else if (!CONTAINS_PLACEHOLDER_P (TYPE_SIZE (right_base_type)))
860 best_type = right_base_type;
864 left_operand = convert (best_type, left_operand);
865 right_operand = convert (best_type, right_operand);
871 /* If we are comparing a fat pointer against zero, we need to
872 just compare the data pointer. */
873 else if (TYPE_FAT_POINTER_P (left_base_type)
874 && TREE_CODE (right_operand) == CONSTRUCTOR
875 && integer_zerop (VEC_index (constructor_elt,
876 CONSTRUCTOR_ELTS (right_operand),
880 right_operand = build_component_ref (left_operand, NULL_TREE,
881 TYPE_FIELDS (left_base_type),
883 left_operand = convert (TREE_TYPE (right_operand),
888 left_operand = convert (left_base_type, left_operand);
889 right_operand = convert (right_base_type, right_operand);
895 case PREINCREMENT_EXPR:
896 case PREDECREMENT_EXPR:
897 case POSTINCREMENT_EXPR:
898 case POSTDECREMENT_EXPR:
899 /* In these, the result type and the left operand type should be the
900 same. Do the operation in the base type of those and convert the
901 right operand (which is an integer) to that type.
903 Note that these operations are only used in loop control where
904 we guarantee that no overflow can occur. So nothing special need
905 be done for modular types. */
907 gcc_assert (left_type == result_type);
908 operation_type = get_base_type (result_type);
909 left_operand = convert (operation_type, left_operand);
910 right_operand = convert (operation_type, right_operand);
911 has_side_effects = true;
919 /* The RHS of a shift can be any type. Also, ignore any modulus
920 (we used to abort, but this is needed for unchecked conversion
921 to modular types). Otherwise, processing is the same as normal. */
922 gcc_assert (operation_type == left_base_type);
924 left_operand = convert (operation_type, left_operand);
927 case TRUTH_ANDIF_EXPR:
928 case TRUTH_ORIF_EXPR:
932 left_operand = gnat_truthvalue_conversion (left_operand);
933 right_operand = gnat_truthvalue_conversion (right_operand);
939 /* For binary modulus, if the inputs are in range, so are the
941 if (modulus && integer_pow2p (modulus))
947 gcc_assert (TREE_TYPE (result_type) == left_base_type
948 && TREE_TYPE (result_type) == right_base_type);
949 left_operand = convert (left_base_type, left_operand);
950 right_operand = convert (right_base_type, right_operand);
953 case TRUNC_DIV_EXPR: case TRUNC_MOD_EXPR:
954 case CEIL_DIV_EXPR: case CEIL_MOD_EXPR:
955 case FLOOR_DIV_EXPR: case FLOOR_MOD_EXPR:
956 case ROUND_DIV_EXPR: case ROUND_MOD_EXPR:
957 /* These always produce results lower than either operand. */
963 /* The result type should be the same as the base types of the
964 both operands (and they should be the same). Convert
965 everything to the result type. */
967 gcc_assert (operation_type == left_base_type
968 && left_base_type == right_base_type);
969 left_operand = convert (operation_type, left_operand);
970 right_operand = convert (operation_type, right_operand);
973 if (modulus && !integer_pow2p (modulus))
975 result = nonbinary_modular_operation (op_code, operation_type,
976 left_operand, right_operand);
979 /* If either operand is a NULL_EXPR, just return a new one. */
980 else if (TREE_CODE (left_operand) == NULL_EXPR)
981 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (left_operand, 0));
982 else if (TREE_CODE (right_operand) == NULL_EXPR)
983 return build1 (NULL_EXPR, operation_type, TREE_OPERAND (right_operand, 0));
984 else if (op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
985 result = build4 (op_code, operation_type, left_operand,
986 right_operand, NULL_TREE, NULL_TREE);
989 = fold_build2 (op_code, operation_type, left_operand, right_operand);
991 TREE_SIDE_EFFECTS (result) |= has_side_effects;
992 TREE_CONSTANT (result)
993 |= (TREE_CONSTANT (left_operand) & TREE_CONSTANT (right_operand)
994 && op_code != ARRAY_REF && op_code != ARRAY_RANGE_REF);
996 if ((op_code == ARRAY_REF || op_code == ARRAY_RANGE_REF)
997 && TYPE_VOLATILE (operation_type))
998 TREE_THIS_VOLATILE (result) = 1;
1000 /* If we are working with modular types, perform the MOD operation
1001 if something above hasn't eliminated the need for it. */
1003 result = fold_build2 (FLOOR_MOD_EXPR, operation_type, result,
1004 convert (operation_type, modulus));
1006 if (result_type && result_type != operation_type)
1007 result = convert (result_type, result);
1012 /* Similar, but for unary operations. */
1015 build_unary_op (enum tree_code op_code, tree result_type, tree operand)
1017 tree type = TREE_TYPE (operand);
1018 tree base_type = get_base_type (type);
1019 tree operation_type = result_type;
1021 bool side_effects = false;
1024 && TREE_CODE (operation_type) == RECORD_TYPE
1025 && TYPE_JUSTIFIED_MODULAR_P (operation_type))
1026 operation_type = TREE_TYPE (TYPE_FIELDS (operation_type));
1029 && !AGGREGATE_TYPE_P (operation_type)
1030 && TYPE_EXTRA_SUBTYPE_P (operation_type))
1031 operation_type = get_base_type (operation_type);
1037 if (!operation_type)
1038 result_type = operation_type = TREE_TYPE (type);
1040 gcc_assert (result_type == TREE_TYPE (type));
1042 result = fold_build1 (op_code, operation_type, operand);
1045 case TRUTH_NOT_EXPR:
1046 gcc_assert (result_type == base_type);
1047 result = invert_truthvalue (gnat_truthvalue_conversion (operand));
1050 case ATTR_ADDR_EXPR:
1052 switch (TREE_CODE (operand))
1055 case UNCONSTRAINED_ARRAY_REF:
1056 result = TREE_OPERAND (operand, 0);
1058 /* Make sure the type here is a pointer, not a reference.
1059 GCC wants pointer types for function addresses. */
1061 result_type = build_pointer_type (type);
1066 TREE_TYPE (result) = type = build_pointer_type (type);
1070 case ARRAY_RANGE_REF:
1073 /* If this is for 'Address, find the address of the prefix and
1074 add the offset to the field. Otherwise, do this the normal
1076 if (op_code == ATTR_ADDR_EXPR)
1078 HOST_WIDE_INT bitsize;
1079 HOST_WIDE_INT bitpos;
1081 enum machine_mode mode;
1082 int unsignedp, volatilep;
1084 inner = get_inner_reference (operand, &bitsize, &bitpos, &offset,
1085 &mode, &unsignedp, &volatilep,
1088 /* If INNER is a padding type whose field has a self-referential
1089 size, convert to that inner type. We know the offset is zero
1090 and we need to have that type visible. */
1091 if (TREE_CODE (TREE_TYPE (inner)) == RECORD_TYPE
1092 && TYPE_IS_PADDING_P (TREE_TYPE (inner))
1093 && (CONTAINS_PLACEHOLDER_P
1094 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
1095 (TREE_TYPE (inner)))))))
1096 inner = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (inner))),
1099 /* Compute the offset as a byte offset from INNER. */
1101 offset = size_zero_node;
1103 if (bitpos % BITS_PER_UNIT != 0)
1105 ("taking address of object not aligned on storage unit?",
1108 offset = size_binop (PLUS_EXPR, offset,
1109 size_int (bitpos / BITS_PER_UNIT));
1111 /* Take the address of INNER, convert the offset to void *, and
1112 add then. It will later be converted to the desired result
1114 inner = build_unary_op (ADDR_EXPR, NULL_TREE, inner);
1115 inner = convert (ptr_void_type_node, inner);
1116 offset = convert (ptr_void_type_node, offset);
1117 result = build_binary_op (PLUS_EXPR, ptr_void_type_node,
1119 result = convert (build_pointer_type (TREE_TYPE (operand)),
1126 /* If this is just a constructor for a padded record, we can
1127 just take the address of the single field and convert it to
1128 a pointer to our type. */
1129 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
1131 result = (VEC_index (constructor_elt,
1132 CONSTRUCTOR_ELTS (operand),
1136 result = convert (build_pointer_type (TREE_TYPE (operand)),
1137 build_unary_op (ADDR_EXPR, NULL_TREE, result));
1144 if (AGGREGATE_TYPE_P (type)
1145 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (operand, 0))))
1146 return build_unary_op (ADDR_EXPR, result_type,
1147 TREE_OPERAND (operand, 0));
1149 /* ... fallthru ... */
1151 case VIEW_CONVERT_EXPR:
1152 /* If this just a variant conversion or if the conversion doesn't
1153 change the mode, get the result type from this type and go down.
1154 This is needed for conversions of CONST_DECLs, to eventually get
1155 to the address of their CORRESPONDING_VARs. */
1156 if ((TYPE_MAIN_VARIANT (type)
1157 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (operand, 0))))
1158 || (TYPE_MODE (type) != BLKmode
1159 && (TYPE_MODE (type)
1160 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (operand, 0))))))
1161 return build_unary_op (ADDR_EXPR,
1162 (result_type ? result_type
1163 : build_pointer_type (type)),
1164 TREE_OPERAND (operand, 0));
1168 operand = DECL_CONST_CORRESPONDING_VAR (operand);
1170 /* ... fall through ... */
1175 /* If we are taking the address of a padded record whose field is
1176 contains a template, take the address of the template. */
1177 if (TREE_CODE (type) == RECORD_TYPE
1178 && TYPE_IS_PADDING_P (type)
1179 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (type))) == RECORD_TYPE
1180 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (TYPE_FIELDS (type))))
1182 type = TREE_TYPE (TYPE_FIELDS (type));
1183 operand = convert (type, operand);
1186 if (type != error_mark_node)
1187 operation_type = build_pointer_type (type);
1189 gnat_mark_addressable (operand);
1190 result = fold_build1 (ADDR_EXPR, operation_type, operand);
1193 TREE_CONSTANT (result) = staticp (operand) || TREE_CONSTANT (operand);
1197 /* If we want to refer to an entire unconstrained array,
1198 make up an expression to do so. This will never survive to
1199 the backend. If TYPE is a thin pointer, first convert the
1200 operand to a fat pointer. */
1201 if (TYPE_THIN_POINTER_P (type)
1202 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)))
1205 = convert (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))),
1207 type = TREE_TYPE (operand);
1210 if (TYPE_FAT_POINTER_P (type))
1212 result = build1 (UNCONSTRAINED_ARRAY_REF,
1213 TYPE_UNCONSTRAINED_ARRAY (type), operand);
1214 TREE_READONLY (result) = TREE_STATIC (result)
1215 = TYPE_READONLY (TYPE_UNCONSTRAINED_ARRAY (type));
1217 else if (TREE_CODE (operand) == ADDR_EXPR)
1218 result = TREE_OPERAND (operand, 0);
1222 result = fold_build1 (op_code, TREE_TYPE (type), operand);
1223 TREE_READONLY (result) = TYPE_READONLY (TREE_TYPE (type));
1227 = (!TYPE_FAT_POINTER_P (type) && TYPE_VOLATILE (TREE_TYPE (type)));
1233 tree modulus = ((operation_type
1234 && TREE_CODE (operation_type) == INTEGER_TYPE
1235 && TYPE_MODULAR_P (operation_type))
1236 ? TYPE_MODULUS (operation_type) : 0);
1237 int mod_pow2 = modulus && integer_pow2p (modulus);
1239 /* If this is a modular type, there are various possibilities
1240 depending on the operation and whether the modulus is a
1241 power of two or not. */
1245 gcc_assert (operation_type == base_type);
1246 operand = convert (operation_type, operand);
1248 /* The fastest in the negate case for binary modulus is
1249 the straightforward code; the TRUNC_MOD_EXPR below
1250 is an AND operation. */
1251 if (op_code == NEGATE_EXPR && mod_pow2)
1252 result = fold_build2 (TRUNC_MOD_EXPR, operation_type,
1253 fold_build1 (NEGATE_EXPR, operation_type,
1257 /* For nonbinary negate case, return zero for zero operand,
1258 else return the modulus minus the operand. If the modulus
1259 is a power of two minus one, we can do the subtraction
1260 as an XOR since it is equivalent and faster on most machines. */
1261 else if (op_code == NEGATE_EXPR && !mod_pow2)
1263 if (integer_pow2p (fold_build2 (PLUS_EXPR, operation_type,
1265 convert (operation_type,
1266 integer_one_node))))
1267 result = fold_build2 (BIT_XOR_EXPR, operation_type,
1270 result = fold_build2 (MINUS_EXPR, operation_type,
1273 result = fold_build3 (COND_EXPR, operation_type,
1274 fold_build2 (NE_EXPR,
1279 integer_zero_node)),
1284 /* For the NOT cases, we need a constant equal to
1285 the modulus minus one. For a binary modulus, we
1286 XOR against the constant and subtract the operand from
1287 that constant for nonbinary modulus. */
1289 tree cnst = fold_build2 (MINUS_EXPR, operation_type, modulus,
1290 convert (operation_type,
1294 result = fold_build2 (BIT_XOR_EXPR, operation_type,
1297 result = fold_build2 (MINUS_EXPR, operation_type,
1305 /* ... fall through ... */
1308 gcc_assert (operation_type == base_type);
1309 result = fold_build1 (op_code, operation_type,
1310 convert (operation_type, operand));
1315 TREE_SIDE_EFFECTS (result) = 1;
1316 if (TREE_CODE (result) == INDIRECT_REF)
1317 TREE_THIS_VOLATILE (result) = TYPE_VOLATILE (TREE_TYPE (result));
1320 if (result_type && TREE_TYPE (result) != result_type)
1321 result = convert (result_type, result);
1326 /* Similar, but for COND_EXPR. */
1329 build_cond_expr (tree result_type, tree condition_operand,
1330 tree true_operand, tree false_operand)
1333 bool addr_p = false;
1335 /* The front-end verifies that result, true and false operands have same base
1336 type. Convert everything to the result type. */
1338 true_operand = convert (result_type, true_operand);
1339 false_operand = convert (result_type, false_operand);
1341 /* If the result type is unconstrained, take the address of
1342 the operands and then dereference our result. */
1343 if (TREE_CODE (result_type) == UNCONSTRAINED_ARRAY_TYPE
1344 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (result_type)))
1347 result_type = build_pointer_type (result_type);
1348 true_operand = build_unary_op (ADDR_EXPR, result_type, true_operand);
1349 false_operand = build_unary_op (ADDR_EXPR, result_type, false_operand);
1352 result = fold_build3 (COND_EXPR, result_type, condition_operand,
1353 true_operand, false_operand);
1355 /* If either operand is a SAVE_EXPR (possibly surrounded by
1356 arithmetic, make sure it gets done. */
1357 true_operand = skip_simple_arithmetic (true_operand);
1358 false_operand = skip_simple_arithmetic (false_operand);
1360 if (TREE_CODE (true_operand) == SAVE_EXPR)
1361 result = build2 (COMPOUND_EXPR, result_type, true_operand, result);
1363 if (TREE_CODE (false_operand) == SAVE_EXPR)
1364 result = build2 (COMPOUND_EXPR, result_type, false_operand, result);
1366 /* ??? Seems the code above is wrong, as it may move ahead of the COND
1367 SAVE_EXPRs with side effects and not shared by both arms. */
1370 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1375 /* Similar, but for RETURN_EXPR. If RESULT_DECL is non-zero, build
1376 a RETURN_EXPR around the assignment of RET_VAL to RESULT_DECL.
1377 If RESULT_DECL is zero, build a bare RETURN_EXPR. */
1380 build_return_expr (tree result_decl, tree ret_val)
1386 /* The gimplifier explicitly enforces the following invariant:
1395 As a consequence, type-homogeneity dictates that we use the type
1396 of the RESULT_DECL as the operation type. */
1398 tree operation_type = TREE_TYPE (result_decl);
1400 /* Convert the right operand to the operation type. Note that
1401 it's the same transformation as in the MODIFY_EXPR case of
1402 build_binary_op with the additional guarantee that the type
1403 cannot involve a placeholder, since otherwise the function
1404 would use the "target pointer" return mechanism. */
1406 if (operation_type != TREE_TYPE (ret_val))
1407 ret_val = convert (operation_type, ret_val);
1410 = build2 (MODIFY_EXPR, operation_type, result_decl, ret_val);
1413 result_expr = NULL_TREE;
1415 return build1 (RETURN_EXPR, void_type_node, result_expr);
1418 /* Build a CALL_EXPR to call FUNDECL with one argument, ARG. Return
1422 build_call_1_expr (tree fundecl, tree arg)
1424 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1425 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1427 TREE_SIDE_EFFECTS (call) = 1;
1431 /* Build a CALL_EXPR to call FUNDECL with two arguments, ARG1 & ARG2. Return
1435 build_call_2_expr (tree fundecl, tree arg1, tree arg2)
1437 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1438 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1440 TREE_SIDE_EFFECTS (call) = 1;
1444 /* Likewise to call FUNDECL with no arguments. */
1447 build_call_0_expr (tree fundecl)
1449 /* We rely on build_call_nary to compute TREE_SIDE_EFFECTS. This makes
1450 it possible to propagate DECL_IS_PURE on parameterless functions. */
1451 tree call = build_call_nary (TREE_TYPE (TREE_TYPE (fundecl)),
1452 build_unary_op (ADDR_EXPR, NULL_TREE, fundecl),
1457 /* Call a function that raises an exception and pass the line number and file
1458 name, if requested. MSG says which exception function to call.
1460 GNAT_NODE is the gnat node conveying the source location for which the
1461 error should be signaled, or Empty in which case the error is signaled on
1462 the current ref_file_name/input_line. */
1465 build_call_raise (int msg, Node_Id gnat_node)
1467 tree fndecl = gnat_raise_decls[msg];
1470 = (Debug_Flag_NN || Exception_Locations_Suppressed)
1472 : (gnat_node != Empty)
1473 ? IDENTIFIER_POINTER
1474 (get_identifier (Get_Name_String
1476 (Get_Source_File_Index (Sloc (gnat_node))))))
1479 int len = strlen (str) + 1;
1480 tree filename = build_string (len, str);
1483 = (gnat_node != Empty)
1484 ? Get_Logical_Line_Number (Sloc(gnat_node)) : input_line;
1486 TREE_TYPE (filename)
1487 = build_array_type (char_type_node,
1488 build_index_type (build_int_cst (NULL_TREE, len)));
1491 build_call_2_expr (fndecl,
1492 build1 (ADDR_EXPR, build_pointer_type (char_type_node),
1494 build_int_cst (NULL_TREE, line_number));
1497 /* qsort comparer for the bit positions of two constructor elements
1498 for record components. */
1501 compare_elmt_bitpos (const PTR rt1, const PTR rt2)
1503 tree elmt1 = * (tree *) rt1;
1504 tree elmt2 = * (tree *) rt2;
1506 tree pos_field1 = bit_position (TREE_PURPOSE (elmt1));
1507 tree pos_field2 = bit_position (TREE_PURPOSE (elmt2));
1509 if (tree_int_cst_equal (pos_field1, pos_field2))
1511 else if (tree_int_cst_lt (pos_field1, pos_field2))
1517 /* Return a CONSTRUCTOR of TYPE whose list is LIST. */
1520 gnat_build_constructor (tree type, tree list)
1524 bool allconstant = (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST);
1525 bool side_effects = false;
1528 /* Scan the elements to see if they are all constant or if any has side
1529 effects, to let us set global flags on the resulting constructor. Count
1530 the elements along the way for possible sorting purposes below. */
1531 for (n_elmts = 0, elmt = list; elmt; elmt = TREE_CHAIN (elmt), n_elmts ++)
1533 if (!TREE_CONSTANT (TREE_VALUE (elmt))
1534 || (TREE_CODE (type) == RECORD_TYPE
1535 && DECL_BIT_FIELD (TREE_PURPOSE (elmt))
1536 && TREE_CODE (TREE_VALUE (elmt)) != INTEGER_CST)
1537 || !initializer_constant_valid_p (TREE_VALUE (elmt),
1538 TREE_TYPE (TREE_VALUE (elmt))))
1539 allconstant = false;
1541 if (TREE_SIDE_EFFECTS (TREE_VALUE (elmt)))
1542 side_effects = true;
1544 /* Propagate an NULL_EXPR from the size of the type. We won't ever
1545 be executing the code we generate here in that case, but handle it
1546 specially to avoid the compiler blowing up. */
1547 if (TREE_CODE (type) == RECORD_TYPE
1549 = contains_null_expr (DECL_SIZE (TREE_PURPOSE (elmt))))))
1550 return build1 (NULL_EXPR, type, TREE_OPERAND (result, 0));
1553 /* For record types with constant components only, sort field list
1554 by increasing bit position. This is necessary to ensure the
1555 constructor can be output as static data, which the gimplifier
1556 might force in various circumstances. */
1557 if (allconstant && TREE_CODE (type) == RECORD_TYPE && n_elmts > 1)
1559 /* Fill an array with an element tree per index, and ask qsort to order
1560 them according to what a bitpos comparison function says. */
1562 tree *gnu_arr = (tree *) alloca (sizeof (tree) * n_elmts);
1565 for (i = 0, elmt = list; elmt; elmt = TREE_CHAIN (elmt), i++)
1568 qsort (gnu_arr, n_elmts, sizeof (tree), compare_elmt_bitpos);
1570 /* Then reconstruct the list from the sorted array contents. */
1573 for (i = n_elmts - 1; i >= 0; i--)
1575 TREE_CHAIN (gnu_arr[i]) = list;
1580 result = build_constructor_from_list (type, list);
1581 TREE_CONSTANT (result) = TREE_INVARIANT (result)
1582 = TREE_STATIC (result) = allconstant;
1583 TREE_SIDE_EFFECTS (result) = side_effects;
1584 TREE_READONLY (result) = TYPE_READONLY (type) || allconstant;
1588 /* Return a COMPONENT_REF to access a field that is given by COMPONENT,
1589 an IDENTIFIER_NODE giving the name of the field, or FIELD, a FIELD_DECL,
1590 for the field. Don't fold the result if NO_FOLD_P is true.
1592 We also handle the fact that we might have been passed a pointer to the
1593 actual record and know how to look for fields in variant parts. */
1596 build_simple_component_ref (tree record_variable, tree component,
1597 tree field, bool no_fold_p)
1599 tree record_type = TYPE_MAIN_VARIANT (TREE_TYPE (record_variable));
1602 gcc_assert ((TREE_CODE (record_type) == RECORD_TYPE
1603 || TREE_CODE (record_type) == UNION_TYPE
1604 || TREE_CODE (record_type) == QUAL_UNION_TYPE)
1605 && TYPE_SIZE (record_type)
1606 && (component != 0) != (field != 0));
1608 /* If no field was specified, look for a field with the specified name
1609 in the current record only. */
1611 for (field = TYPE_FIELDS (record_type); field;
1612 field = TREE_CHAIN (field))
1613 if (DECL_NAME (field) == component)
1619 /* If this field is not in the specified record, see if we can find
1620 something in the record whose original field is the same as this one. */
1621 if (DECL_CONTEXT (field) != record_type)
1622 /* Check if there is a field with name COMPONENT in the record. */
1626 /* First loop thru normal components. */
1628 for (new_field = TYPE_FIELDS (record_type); new_field;
1629 new_field = TREE_CHAIN (new_field))
1630 if (field == new_field
1631 || DECL_ORIGINAL_FIELD (new_field) == field
1632 || new_field == DECL_ORIGINAL_FIELD (field)
1633 || (DECL_ORIGINAL_FIELD (field)
1634 && (DECL_ORIGINAL_FIELD (field)
1635 == DECL_ORIGINAL_FIELD (new_field))))
1638 /* Next, loop thru DECL_INTERNAL_P components if we haven't found
1639 the component in the first search. Doing this search in 2 steps
1640 is required to avoiding hidden homonymous fields in the
1644 for (new_field = TYPE_FIELDS (record_type); new_field;
1645 new_field = TREE_CHAIN (new_field))
1646 if (DECL_INTERNAL_P (new_field))
1649 = build_simple_component_ref (record_variable,
1650 NULL_TREE, new_field, no_fold_p);
1651 ref = build_simple_component_ref (field_ref, NULL_TREE, field,
1664 /* If the field's offset has overflowed, do not attempt to access it
1665 as doing so may trigger sanity checks deeper in the back-end.
1666 Note that we don't need to warn since this will be done on trying
1667 to declare the object. */
1668 if (TREE_CODE (DECL_FIELD_OFFSET (field)) == INTEGER_CST
1669 && TREE_OVERFLOW (DECL_FIELD_OFFSET (field)))
1672 /* It would be nice to call "fold" here, but that can lose a type
1673 we need to tag a PLACEHOLDER_EXPR with, so we can't do it. */
1674 ref = build3 (COMPONENT_REF, TREE_TYPE (field), record_variable, field,
1677 if (TREE_READONLY (record_variable) || TREE_READONLY (field))
1678 TREE_READONLY (ref) = 1;
1679 if (TREE_THIS_VOLATILE (record_variable) || TREE_THIS_VOLATILE (field)
1680 || TYPE_VOLATILE (record_type))
1681 TREE_THIS_VOLATILE (ref) = 1;
1683 return no_fold_p ? ref : fold (ref);
1686 /* Like build_simple_component_ref, except that we give an error if the
1687 reference could not be found. */
1690 build_component_ref (tree record_variable, tree component,
1691 tree field, bool no_fold_p)
1693 tree ref = build_simple_component_ref (record_variable, component, field,
1699 /* If FIELD was specified, assume this is an invalid user field so
1700 raise constraint error. Otherwise, we can't find the type to return, so
1703 return build1 (NULL_EXPR, TREE_TYPE (field),
1704 build_call_raise (CE_Discriminant_Check_Failed, Empty));
1707 /* Build a GCC tree to call an allocation or deallocation function.
1708 If GNU_OBJ is nonzero, it is an object to deallocate. Otherwise,
1709 generate an allocator.
1711 GNU_SIZE is the size of the object in bytes and ALIGN is the alignment in
1712 bits. GNAT_PROC, if present, is a procedure to call and GNAT_POOL is the
1713 storage pool to use. If not preset, malloc and free will be used except
1714 if GNAT_PROC is the "fake" value of -1, in which case we allocate the
1715 object dynamically on the stack frame. */
1718 build_call_alloc_dealloc (tree gnu_obj, tree gnu_size, unsigned align,
1719 Entity_Id gnat_proc, Entity_Id gnat_pool,
1722 tree gnu_align = size_int (align / BITS_PER_UNIT);
1724 gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_obj);
1726 if (Present (gnat_proc))
1728 /* The storage pools are obviously always tagged types, but the
1729 secondary stack uses the same mechanism and is not tagged */
1730 if (Is_Tagged_Type (Etype (gnat_pool)))
1732 /* The size is the third parameter; the alignment is the
1734 Entity_Id gnat_size_type
1735 = Etype (Next_Formal (Next_Formal (First_Formal (gnat_proc))));
1736 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
1737 tree gnu_proc = gnat_to_gnu (gnat_proc);
1738 tree gnu_proc_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_proc);
1739 tree gnu_pool = gnat_to_gnu (gnat_pool);
1740 tree gnu_pool_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_pool);
1743 gnu_size = convert (gnu_size_type, gnu_size);
1744 gnu_align = convert (gnu_size_type, gnu_align);
1746 /* The first arg is always the address of the storage pool; next
1747 comes the address of the object, for a deallocator, then the
1748 size and alignment. */
1750 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1751 gnu_proc_addr, 4, gnu_pool_addr,
1752 gnu_obj, gnu_size, gnu_align);
1754 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1755 gnu_proc_addr, 3, gnu_pool_addr,
1756 gnu_size, gnu_align);
1757 TREE_SIDE_EFFECTS (gnu_call) = 1;
1761 /* Secondary stack case. */
1764 /* The size is the second parameter */
1765 Entity_Id gnat_size_type
1766 = Etype (Next_Formal (First_Formal (gnat_proc)));
1767 tree gnu_size_type = gnat_to_gnu_type (gnat_size_type);
1768 tree gnu_proc = gnat_to_gnu (gnat_proc);
1769 tree gnu_proc_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_proc);
1772 gnu_size = convert (gnu_size_type, gnu_size);
1774 /* The first arg is the address of the object, for a
1775 deallocator, then the size */
1777 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1778 gnu_proc_addr, 2, gnu_obj, gnu_size);
1780 gnu_call = build_call_nary (TREE_TYPE (TREE_TYPE (gnu_proc)),
1781 gnu_proc_addr, 1, gnu_size);
1782 TREE_SIDE_EFFECTS (gnu_call) = 1;
1788 return build_call_1_expr (free_decl, gnu_obj);
1790 /* ??? For now, disable variable-sized allocators in the stack since
1791 we can't yet gimplify an ALLOCATE_EXPR. */
1792 else if (gnat_pool == -1
1793 && TREE_CODE (gnu_size) == INTEGER_CST && !flag_stack_check)
1795 /* If the size is a constant, we can put it in the fixed portion of
1796 the stack frame to avoid the need to adjust the stack pointer. */
1797 if (TREE_CODE (gnu_size) == INTEGER_CST && !flag_stack_check)
1800 = build_range_type (NULL_TREE, size_one_node, gnu_size);
1801 tree gnu_array_type = build_array_type (char_type_node, gnu_range);
1803 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
1804 gnu_array_type, NULL_TREE, false, false, false,
1805 false, NULL, gnat_node);
1807 return convert (ptr_void_type_node,
1808 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_decl));
1813 return build2 (ALLOCATE_EXPR, ptr_void_type_node, gnu_size, gnu_align);
1818 if (Nkind (gnat_node) != N_Allocator || !Comes_From_Source (gnat_node))
1819 Check_No_Implicit_Heap_Alloc (gnat_node);
1820 return build_call_1_expr (malloc_decl, gnu_size);
1824 /* Build a GCC tree to correspond to allocating an object of TYPE whose
1825 initial value is INIT, if INIT is nonzero. Convert the expression to
1826 RESULT_TYPE, which must be some type of pointer. Return the tree.
1827 GNAT_PROC and GNAT_POOL optionally give the procedure to call and
1828 the storage pool to use. GNAT_NODE is used to provide an error
1829 location for restriction violations messages. If IGNORE_INIT_TYPE is
1830 true, ignore the type of INIT for the purpose of determining the size;
1831 this will cause the maximum size to be allocated if TYPE is of
1832 self-referential size. */
1835 build_allocator (tree type, tree init, tree result_type, Entity_Id gnat_proc,
1836 Entity_Id gnat_pool, Node_Id gnat_node, bool ignore_init_type)
1838 tree size = TYPE_SIZE_UNIT (type);
1841 /* If the initializer, if present, is a NULL_EXPR, just return a new one. */
1842 if (init && TREE_CODE (init) == NULL_EXPR)
1843 return build1 (NULL_EXPR, result_type, TREE_OPERAND (init, 0));
1845 /* If RESULT_TYPE is a fat or thin pointer, set SIZE to be the sum of the
1846 sizes of the object and its template. Allocate the whole thing and
1847 fill in the parts that are known. */
1848 else if (TYPE_FAT_OR_THIN_POINTER_P (result_type))
1851 = build_unc_object_type_from_ptr (result_type, type,
1852 get_identifier ("ALLOC"));
1853 tree template_type = TREE_TYPE (TYPE_FIELDS (storage_type));
1854 tree storage_ptr_type = build_pointer_type (storage_type);
1856 tree template_cons = NULL_TREE;
1858 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (storage_type),
1861 /* If the size overflows, pass -1 so the allocator will raise
1863 if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size))
1864 size = ssize_int (-1);
1866 storage = build_call_alloc_dealloc (NULL_TREE, size,
1867 TYPE_ALIGN (storage_type),
1868 gnat_proc, gnat_pool, gnat_node);
1869 storage = convert (storage_ptr_type, protect_multiple_eval (storage));
1871 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
1873 type = TREE_TYPE (TYPE_FIELDS (type));
1876 init = convert (type, init);
1879 /* If there is an initializing expression, make a constructor for
1880 the entire object including the bounds and copy it into the
1881 object. If there is no initializing expression, just set the
1885 template_cons = tree_cons (TREE_CHAIN (TYPE_FIELDS (storage_type)),
1887 template_cons = tree_cons (TYPE_FIELDS (storage_type),
1888 build_template (template_type, type,
1894 build2 (COMPOUND_EXPR, storage_ptr_type,
1896 (MODIFY_EXPR, storage_type,
1897 build_unary_op (INDIRECT_REF, NULL_TREE,
1898 convert (storage_ptr_type, storage)),
1899 gnat_build_constructor (storage_type, template_cons)),
1900 convert (storage_ptr_type, storage)));
1904 (COMPOUND_EXPR, result_type,
1906 (MODIFY_EXPR, template_type,
1908 (build_unary_op (INDIRECT_REF, NULL_TREE,
1909 convert (storage_ptr_type, storage)),
1910 NULL_TREE, TYPE_FIELDS (storage_type), 0),
1911 build_template (template_type, type, NULL_TREE)),
1912 convert (result_type, convert (storage_ptr_type, storage)));
1915 /* If we have an initializing expression, see if its size is simpler
1916 than the size from the type. */
1917 if (!ignore_init_type && init && TYPE_SIZE_UNIT (TREE_TYPE (init))
1918 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (init))) == INTEGER_CST
1919 || CONTAINS_PLACEHOLDER_P (size)))
1920 size = TYPE_SIZE_UNIT (TREE_TYPE (init));
1922 /* If the size is still self-referential, reference the initializing
1923 expression, if it is present. If not, this must have been a
1924 call to allocate a library-level object, in which case we use
1925 the maximum size. */
1926 if (CONTAINS_PLACEHOLDER_P (size))
1928 if (!ignore_init_type && init)
1929 size = substitute_placeholder_in_expr (size, init);
1931 size = max_size (size, true);
1934 /* If the size overflows, pass -1 so the allocator will raise
1936 if (TREE_CODE (size) == INTEGER_CST && TREE_OVERFLOW (size))
1937 size = ssize_int (-1);
1939 /* If this is a type whose alignment is larger than the
1940 biggest we support in normal alignment and this is in
1941 the default storage pool, make an "aligning type", allocate
1942 it, point to the field we need, and return that. */
1943 if (TYPE_ALIGN (type) > BIGGEST_ALIGNMENT
1946 tree new_type = make_aligning_type (type, TYPE_ALIGN (type), size);
1948 result = build_call_alloc_dealloc (NULL_TREE, TYPE_SIZE_UNIT (new_type),
1949 BIGGEST_ALIGNMENT, Empty,
1951 result = save_expr (result);
1952 result = convert (build_pointer_type (new_type), result);
1953 result = build_unary_op (INDIRECT_REF, NULL_TREE, result);
1954 result = build_component_ref (result, NULL_TREE,
1955 TYPE_FIELDS (new_type), 0);
1956 result = convert (result_type,
1957 build_unary_op (ADDR_EXPR, NULL_TREE, result));
1960 result = convert (result_type,
1961 build_call_alloc_dealloc (NULL_TREE, size,
1967 /* If we have an initial value, put the new address into a SAVE_EXPR, assign
1968 the value, and return the address. Do this with a COMPOUND_EXPR. */
1972 result = save_expr (result);
1974 = build2 (COMPOUND_EXPR, TREE_TYPE (result),
1976 (MODIFY_EXPR, NULL_TREE,
1977 build_unary_op (INDIRECT_REF,
1978 TREE_TYPE (TREE_TYPE (result)), result),
1983 return convert (result_type, result);
1986 /* Fill in a VMS descriptor for EXPR and return a constructor for it.
1987 GNAT_FORMAL is how we find the descriptor record. */
1990 fill_vms_descriptor (tree expr, Entity_Id gnat_formal)
1992 tree record_type = TREE_TYPE (TREE_TYPE (get_gnu_tree (gnat_formal)));
1994 tree const_list = NULL_TREE;
1996 expr = maybe_unconstrained_array (expr);
1997 gnat_mark_addressable (expr);
1999 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
2002 convert (TREE_TYPE (field),
2003 SUBSTITUTE_PLACEHOLDER_IN_EXPR
2004 (DECL_INITIAL (field), expr)),
2007 return gnat_build_constructor (record_type, nreverse (const_list));
2010 /* Indicate that we need to make the address of EXPR_NODE and it therefore
2011 should not be allocated in a register. Returns true if successful. */
2014 gnat_mark_addressable (tree expr_node)
2017 switch (TREE_CODE (expr_node))
2022 case ARRAY_RANGE_REF:
2025 case VIEW_CONVERT_EXPR:
2027 case NON_LVALUE_EXPR:
2029 expr_node = TREE_OPERAND (expr_node, 0);
2033 TREE_ADDRESSABLE (expr_node) = 1;
2039 TREE_ADDRESSABLE (expr_node) = 1;
2043 TREE_ADDRESSABLE (expr_node) = 1;
2047 return (DECL_CONST_CORRESPONDING_VAR (expr_node)
2048 && (gnat_mark_addressable
2049 (DECL_CONST_CORRESPONDING_VAR (expr_node))));