Daily bump.
[official-gcc.git] / gcc / ada / cuintp.c
blob49ff6164f291352f363be7c1c37b5307ddaebf32
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * C U I N T P *
6 * *
7 * C Implementation File *
8 * *
9 * $Revision: 1.1 $
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 * GNAT was originally developed by the GNAT team at New York University. *
25 * Extensive contributions were provided by Ada Core Technologies Inc. *
26 * *
27 ****************************************************************************/
29 /* This file corresponds to the Ada package body Uintp. It was created
30 manually from the files uintp.ads and uintp.adb. */
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "ada.h"
36 #include "types.h"
37 #include "uintp.h"
38 #include "atree.h"
39 #include "elists.h"
40 #include "nlists.h"
41 #include "stringt.h"
42 #include "fe.h"
43 #include "gigi.h"
45 /* Universal integers are represented by the Uint type which is an index into
46 the Uints_Ptr table containing Uint_Entry values. A Uint_Entry contains an
47 index and length for getting the "digits" of the universal integer from the
48 Udigits_Ptr table.
50 For efficiency, this method is used only for integer values larger than the
51 constant Uint_Bias. If a Uint is less than this constant, then it contains
52 the integer value itself. The origin of the Uints_Ptr table is adjusted so
53 that a Uint value of Uint_Bias indexes the first element. */
55 /* Similarly to UI_To_Int, but return a GCC INTEGER_CST. Overflow is tested
56 by the constant-folding used to build the node. TYPE is the GCC type of the
57 resulting node. */
59 tree
60 UI_To_gnu (Input, type)
61 Uint Input;
62 tree type;
64 tree gnu_ret;
66 if (Input <= Uint_Direct_Last)
67 gnu_ret = convert (type, build_int_2 (Input - Uint_Direct_Bias,
68 Input < Uint_Direct_Bias ? -1 : 0));
69 else
71 Int Idx = Uints_Ptr[Input].Loc;
72 Pos Length = Uints_Ptr[Input].Length;
73 Int First = Udigits_Ptr[Idx];
74 /* Do computations in integer type or TYPE whichever is wider, then
75 convert later. This avoid overflow if type is short integer. */
76 tree comp_type
77 = (TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)
78 ? type : integer_type_node);
79 tree gnu_base = convert (comp_type, build_int_2 (Base, 0));
81 if (Length <= 0)
82 gigi_abort (601);
84 gnu_ret = convert (comp_type, build_int_2 (First, First < 0 ? -1 : 0));
85 if (First < 0)
86 for (Idx++, Length--; Length; Idx++, Length--)
87 gnu_ret = fold (build (MINUS_EXPR, comp_type,
88 fold (build (MULT_EXPR, comp_type,
89 gnu_ret, gnu_base)),
90 convert (comp_type,
91 build_int_2 (Udigits_Ptr[Idx], 0))));
92 else
93 for (Idx++, Length--; Length; Idx++, Length--)
94 gnu_ret = fold (build (PLUS_EXPR, comp_type,
95 fold (build (MULT_EXPR, comp_type,
96 gnu_ret, gnu_base)),
97 convert (comp_type,
98 build_int_2 (Udigits_Ptr[Idx], 0))));
101 gnu_ret = convert (type, gnu_ret);
103 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET. */
104 while ((TREE_CODE (gnu_ret) == NOP_EXPR
105 || TREE_CODE (gnu_ret) == NON_LVALUE_EXPR)
106 && TREE_TYPE (TREE_OPERAND (gnu_ret, 0)) == TREE_TYPE (gnu_ret))
107 gnu_ret = TREE_OPERAND (gnu_ret, 0);
109 return gnu_ret;