| 1 |
/**************************************************************************** |
| 2 |
* * |
| 3 |
* GNAT COMPILER COMPONENTS * |
| 4 |
* * |
| 5 |
* C U I N T P * |
| 6 |
* * |
| 7 |
* C Implementation File * |
| 8 |
* * |
| 9 |
* Copyright (C) 1992-2025, Free Software Foundation, Inc. * |
| 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- * |
| 13 |
* ware Foundation; either version 3, 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 along with GCC; see the file COPYING3. If not see * |
| 19 |
* <http://www.gnu.org/licenses/>. * |
| 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 |
/* This file corresponds to the Ada package body Uintp. It was created |
| 27 |
manually from the files uintp.ads and uintp.adb. */ |
| 28 |
|
| 29 |
#include "config.h" |
| 30 |
#include "system.h" |
| 31 |
#include "coretypes.h" |
| 32 |
#include "tm.h" |
| 33 |
#include "vec.h" |
| 34 |
#include "alias.h" |
| 35 |
#include "tree.h" |
| 36 |
#include "inchash.h" |
| 37 |
#include "fold-const.h" |
| 38 |
#include "stor-layout.h" |
| 39 |
|
| 40 |
#include "ada.h" |
| 41 |
#include "types.h" |
| 42 |
#include "uintp.h" |
| 43 |
#include "sinfo.h" |
| 44 |
#include "ada-tree.h" |
| 45 |
#include "gigi.h" |
| 46 |
|
| 47 |
/* Universal integers are represented by the Uint type which is an index into |
| 48 |
the Uints_Ptr table containing Uint_Entry values. A Uint_Entry contains an |
| 49 |
index and length for getting the "digits" of the universal integer from the |
| 50 |
Udigits_Ptr table. |
| 51 |
|
| 52 |
For efficiency, this method is used only for integer values larger than the |
| 53 |
constant Uint_Bias. If a Uint is less than this constant, then it contains |
| 54 |
the integer value itself. |
| 55 |
|
| 56 |
First define a utility function that is build_int_cst for integral types and |
| 57 |
does a conversion for floating-point types. */ |
| 58 |
|
| 59 |
static tree |
| 60 |
build_cst_from_int (tree type, HOST_WIDE_INT low) |
| 61 |
{ |
| 62 |
if (SCALAR_FLOAT_TYPE_P (type)) |
| 63 |
return convert (type, build_int_cst (gnat_type_for_size (32, 0), low)); |
| 64 |
else |
| 65 |
return build_int_cst (type, low); |
| 66 |
} |
| 67 |
|
| 68 |
/* Similar to UI_To_Int, but return a GCC INTEGER_CST or REAL_CST node, |
| 69 |
depending on whether TYPE is an integral or real type. Overflow is tested |
| 70 |
by the constant-folding used to build the node. TYPE is the GCC type of |
| 71 |
the resulting node. If TYPE is NULL, an unsigned integer type wide enough |
| 72 |
to hold the entire constant is selected, and if no such type exists, |
| 73 |
return NULL_TREE. */ |
| 74 |
|
| 75 |
tree |
| 76 |
UI_To_gnu (Uint Input, tree type) |
| 77 |
{ |
| 78 |
/* We might have a TYPE with biased representation and be passed an unbiased |
| 79 |
value that doesn't fit. We always use an unbiased type to be able to hold |
| 80 |
any such possible value for intermediate computations and then rely on a |
| 81 |
conversion back to TYPE to perform the bias adjustment when need be. */ |
| 82 |
tree comp_type |
| 83 |
= (!type ? gnat_type_for_size (32, 1) |
| 84 |
: (TREE_CODE (type) == INTEGER_TYPE |
| 85 |
&& TYPE_BIASED_REPRESENTATION_P (type)) |
| 86 |
? get_base_type (type) : type); |
| 87 |
tree gnu_ret; |
| 88 |
|
| 89 |
if (Input <= Uint_Direct_Last) |
| 90 |
gnu_ret = build_cst_from_int (comp_type, Input - Uint_Direct_Bias); |
| 91 |
else |
| 92 |
{ |
| 93 |
Int Idx = (*Uints_Ptr)[Input - Uint_Table_Start].Loc; |
| 94 |
Pos Length = (*Uints_Ptr)[Input - Uint_Table_Start].Length; |
| 95 |
Int First = (*Udigits_Ptr)[Idx]; |
| 96 |
tree_code code = First < 0 ? MINUS_EXPR : PLUS_EXPR; |
| 97 |
tree gnu_base; |
| 98 |
|
| 99 |
gcc_assert (Length > 0); |
| 100 |
/* The extension of unsigned types we use to try to fit the |
| 101 |
constant only works if we're dealing with nonnegative |
| 102 |
constants, but that's what we expect when !TYPE. */ |
| 103 |
gcc_assert (type || First >= 0); |
| 104 |
|
| 105 |
/* The computations we perform below always require a type at least as |
| 106 |
large as an integer not to overflow. FP types are always fine, but |
| 107 |
INTEGER or ENUMERAL types we are handed may be too short. We use a |
| 108 |
base integer type node for the computations in this case and will |
| 109 |
convert the final result back to the incoming type later on. */ |
| 110 |
if (!SCALAR_FLOAT_TYPE_P (comp_type) && TYPE_PRECISION (comp_type) < 32) |
| 111 |
comp_type = gnat_type_for_size (32, 0); |
| 112 |
|
| 113 |
gnu_base = build_cst_from_int (comp_type, Base); |
| 114 |
|
| 115 |
gnu_ret = build_cst_from_int (comp_type, First); |
| 116 |
for (Idx++, Length--; Length; Idx++, Length--) |
| 117 |
for (;;) |
| 118 |
{ |
| 119 |
tree elt, scaled, next_ret; |
| 120 |
elt = build_cst_from_int (comp_type, (*Udigits_Ptr)[Idx]); |
| 121 |
/* We want to detect overflows with an unsigned type when |
| 122 |
TYPE is not given, but int_const_binop doesn't work for |
| 123 |
e.g. floating-point TYPEs. */ |
| 124 |
if (!type) |
| 125 |
{ |
| 126 |
scaled = int_const_binop (MULT_EXPR, gnu_ret, gnu_base, -1); |
| 127 |
next_ret = int_const_binop (code, scaled, elt, -1); |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
scaled = fold_build2 (MULT_EXPR, comp_type, gnu_ret, gnu_base); |
| 132 |
next_ret = fold_build2 (code, comp_type, scaled, elt); |
| 133 |
} |
| 134 |
if (!TREE_OVERFLOW (next_ret) || type) |
| 135 |
{ |
| 136 |
gnu_ret = next_ret; |
| 137 |
break; |
| 138 |
} |
| 139 |
opt_scalar_int_mode wider_mode |
| 140 |
= GET_MODE_WIDER_MODE (SCALAR_INT_TYPE_MODE |
| 141 |
(comp_type)).require (); |
| 142 |
if (!wider_mode.exists ()) |
| 143 |
/* Signal that we couldn't represent the value. */ |
| 144 |
return NULL_TREE; |
| 145 |
comp_type = make_unsigned_type (GET_MODE_BITSIZE |
| 146 |
(wider_mode.require ())); |
| 147 |
gnu_base = convert (comp_type, gnu_base); |
| 148 |
gnu_ret = convert (comp_type, gnu_ret); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (!type) |
| 153 |
type = comp_type; |
| 154 |
gnu_ret = convert (type, gnu_ret); |
| 155 |
|
| 156 |
/* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET. */ |
| 157 |
while ((TREE_CODE (gnu_ret) == NOP_EXPR |
| 158 |
|| TREE_CODE (gnu_ret) == NON_LVALUE_EXPR) |
| 159 |
&& TREE_TYPE (TREE_OPERAND (gnu_ret, 0)) == TREE_TYPE (gnu_ret)) |
| 160 |
gnu_ret = TREE_OPERAND (gnu_ret, 0); |
| 161 |
|
| 162 |
return gnu_ret; |
| 163 |
} |
| 164 |
|
| 165 |
/* Similar to UI_From_Int, but take a GCC INTEGER_CST. We use UI_From_Int |
| 166 |
when possible, i.e. for a 32-bit signed value, to take advantage of its |
| 167 |
built-in caching mechanism. For values of larger magnitude, we compute |
| 168 |
digits into a vector and call Vector_To_Uint. */ |
| 169 |
|
| 170 |
Uint |
| 171 |
UI_From_gnu (tree Input) |
| 172 |
{ |
| 173 |
tree gnu_type = TREE_TYPE (Input), gnu_base, gnu_temp; |
| 174 |
/* UI_Base is defined so that 5 Uint digits is sufficient to hold the |
| 175 |
largest possible signed 64-bit value. */ |
| 176 |
const int Max_For_Dint = 5; |
| 177 |
int v[Max_For_Dint]; |
| 178 |
Vector_Template temp; |
| 179 |
Int_Vector vec; |
| 180 |
|
| 181 |
#if HOST_BITS_PER_WIDE_INT < 64 |
| 182 |
#error unsupported HOST_BITS_PER_WIDE_INT setting |
| 183 |
#endif |
| 184 |
|
| 185 |
/* On 64-bit hosts, tree_fits_shwi_p tells whether the input fits in |
| 186 |
a signed 64-bit integer. Then a truncation tells whether it fits |
| 187 |
in a signed 32-bit integer. */ |
| 188 |
if (tree_fits_shwi_p (Input)) |
| 189 |
{ |
| 190 |
HOST_WIDE_INT hw_input = tree_to_shwi (Input); |
| 191 |
if (hw_input == (int) hw_input) |
| 192 |
return UI_From_Int (hw_input); |
| 193 |
} |
| 194 |
else |
| 195 |
return No_Uint; |
| 196 |
|
| 197 |
gnu_base = build_int_cst (gnu_type, UI_Base); |
| 198 |
gnu_temp = Input; |
| 199 |
|
| 200 |
for (int i = Max_For_Dint - 1; i >= 0; i--) |
| 201 |
{ |
| 202 |
v[i] = tree_to_shwi (fold_build1 (ABS_EXPR, gnu_type, |
| 203 |
fold_build2 (TRUNC_MOD_EXPR, gnu_type, |
| 204 |
gnu_temp, gnu_base))); |
| 205 |
gnu_temp = fold_build2 (TRUNC_DIV_EXPR, gnu_type, gnu_temp, gnu_base); |
| 206 |
} |
| 207 |
|
| 208 |
temp.Low_Bound = 1; |
| 209 |
temp.High_Bound = Max_For_Dint; |
| 210 |
vec.Bounds = &temp; |
| 211 |
vec.Array = v; |
| 212 |
return Vector_To_Uint (vec, tree_int_cst_sgn (Input) < 0); |
| 213 |
} |