* MAINTAINERS: (Write After Approval): Add myself.
[official-gcc.git] / gcc / ada / cuintp.c
blob4c192955820fec5ac0c288f484753b24bb25d7fc
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * C U I N T P *
6 * *
7 * C Implementation File *
8 * *
9 * *
10 * Copyright (C) 1992-2001 Free Software Foundation, Inc. *
11 * *
12 * GNAT is free software; you can redistribute it and/or modify it under *
13 * terms of the GNU General Public License as published by the Free Soft- *
14 * ware Foundation; either version 2, or (at your option) any later ver- *
15 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
16 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
18 * for more details. You should have received a copy of the GNU General *
19 * Public License distributed with GNAT; see file COPYING. If not, write *
20 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
21 * MA 02111-1307, USA. *
22 * *
23 * GNAT was originally developed by the GNAT team at New York University. *
24 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
25 * *
26 ****************************************************************************/
28 /* This file corresponds to the Ada package body Uintp. It was created
29 manually from the files uintp.ads and uintp.adb. */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "ada.h"
35 #include "types.h"
36 #include "uintp.h"
37 #include "atree.h"
38 #include "elists.h"
39 #include "nlists.h"
40 #include "stringt.h"
41 #include "fe.h"
42 #include "gigi.h"
44 /* Universal integers are represented by the Uint type which is an index into
45 the Uints_Ptr table containing Uint_Entry values. A Uint_Entry contains an
46 index and length for getting the "digits" of the universal integer from the
47 Udigits_Ptr table.
49 For efficiency, this method is used only for integer values larger than the
50 constant Uint_Bias. If a Uint is less than this constant, then it contains
51 the integer value itself. The origin of the Uints_Ptr table is adjusted so
52 that a Uint value of Uint_Bias indexes the first element. */
54 /* Similarly to UI_To_Int, but return a GCC INTEGER_CST. Overflow is tested
55 by the constant-folding used to build the node. TYPE is the GCC type of the
56 resulting node. */
58 tree
59 UI_To_gnu (Input, type)
60 Uint Input;
61 tree type;
63 tree gnu_ret;
65 if (Input <= Uint_Direct_Last)
66 gnu_ret = convert (type, build_int_2 (Input - Uint_Direct_Bias,
67 Input < Uint_Direct_Bias ? -1 : 0));
68 else
70 Int Idx = Uints_Ptr[Input].Loc;
71 Pos Length = Uints_Ptr[Input].Length;
72 Int First = Udigits_Ptr[Idx];
73 /* Do computations in integer type or TYPE whichever is wider, then
74 convert later. This avoid overflow if type is short integer. */
75 tree comp_type
76 = (TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node)
77 ? type : integer_type_node);
78 tree gnu_base = convert (comp_type, build_int_2 (Base, 0));
80 if (Length <= 0)
81 gigi_abort (601);
83 gnu_ret = convert (comp_type, build_int_2 (First, First < 0 ? -1 : 0));
84 if (First < 0)
85 for (Idx++, Length--; Length; Idx++, Length--)
86 gnu_ret = fold (build (MINUS_EXPR, comp_type,
87 fold (build (MULT_EXPR, comp_type,
88 gnu_ret, gnu_base)),
89 convert (comp_type,
90 build_int_2 (Udigits_Ptr[Idx], 0))));
91 else
92 for (Idx++, Length--; Length; Idx++, Length--)
93 gnu_ret = fold (build (PLUS_EXPR, comp_type,
94 fold (build (MULT_EXPR, comp_type,
95 gnu_ret, gnu_base)),
96 convert (comp_type,
97 build_int_2 (Udigits_Ptr[Idx], 0))));
100 gnu_ret = convert (type, gnu_ret);
102 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET. */
103 while ((TREE_CODE (gnu_ret) == NOP_EXPR
104 || TREE_CODE (gnu_ret) == NON_LVALUE_EXPR)
105 && TREE_TYPE (TREE_OPERAND (gnu_ret, 0)) == TREE_TYPE (gnu_ret))
106 gnu_ret = TREE_OPERAND (gnu_ret, 0);
108 return gnu_ret;