OSDN Git Service

Fix 4 execute/va-arg-26.c gcc testsuite failures.
[pf3gnuchains/gcc-fork.git] / gcc / ada / cuintp.c
1 /****************************************************************************
2  *                                                                          *
3  *                        GNAT COMPILER COMPONENTS                          *
4  *                                                                          *
5  *                               C U I N T P                                *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *          Copyright (C) 1992-2003 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 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,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, USA.                                                      *
21  *                                                                          *
22  * GNAT was originally developed  by the GNAT team at  New York University. *
23  * Extensive contributions were provided by Ada Core Technologies Inc.      *
24  *                                                                          *
25  ****************************************************************************/
26
27 /* This file corresponds to the Ada package body Uintp. It was created
28    manually from the files uintp.ads and uintp.adb. */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.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"
44
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.
49
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.
54
55    First define a utility function that operates like build_int_cst for
56    integral types and does a conversion to floating-point for real types.  */
57
58 static tree
59 build_cst_from_int (tree type, HOST_WIDE_INT low)
60 {
61   if (TREE_CODE (type) == REAL_TYPE)
62     return convert (type, build_int_cst (NULL_TREE, low));
63   else
64     return force_fit_type (build_int_cst (type, low), false, false, false);
65 }
66
67 /* Similar to UI_To_Int, but return a GCC INTEGER_CST or REAL_CST node,
68    depending on whether TYPE is an integral or real type.  Overflow is tested
69    by the constant-folding used to build the node.  TYPE is the GCC type of
70    the resulting node.  */
71
72 tree
73 UI_To_gnu (Uint Input, tree type)
74 {
75   tree gnu_ret;
76
77   if (Input <= Uint_Direct_Last)
78     gnu_ret = build_cst_from_int (type, Input - Uint_Direct_Bias);
79   else
80     {
81       Int Idx = Uints_Ptr[Input].Loc;
82       Pos Length = Uints_Ptr[Input].Length;
83       Int First = Udigits_Ptr[Idx];
84       /* Do computations in integer type or TYPE whichever is wider, then
85          convert later.  This avoid overflow if type is short integer.  */
86       tree comp_type
87         = ((TREE_CODE (type) == REAL_TYPE
88             || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
89            ? type : integer_type_node);
90       tree gnu_base = build_cst_from_int (comp_type, Base);
91
92       gcc_assert (Length > 0);
93       gnu_ret = build_cst_from_int (comp_type, First);
94       if (First < 0)
95         for (Idx++, Length--; Length; Idx++, Length--)
96           gnu_ret = fold (build2 (MINUS_EXPR, comp_type,
97                                   fold (build2 (MULT_EXPR, comp_type,
98                                                 gnu_ret, gnu_base)),
99                                   build_cst_from_int (comp_type,
100                                                       Udigits_Ptr[Idx])));
101       else
102         for (Idx++, Length--; Length; Idx++, Length--)
103           gnu_ret = fold (build2 (PLUS_EXPR, comp_type,
104                                   fold (build2 (MULT_EXPR, comp_type,
105                                                 gnu_ret, gnu_base)),
106                                   build_cst_from_int (comp_type,
107                                                       Udigits_Ptr[Idx])));
108     }
109
110   gnu_ret = convert (type, gnu_ret);
111
112   /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET.  */
113   while ((TREE_CODE (gnu_ret) == NOP_EXPR
114           || TREE_CODE (gnu_ret) == NON_LVALUE_EXPR)
115          && TREE_TYPE (TREE_OPERAND (gnu_ret, 0)) == TREE_TYPE (gnu_ret))
116     gnu_ret = TREE_OPERAND (gnu_ret, 0);
117
118   return gnu_ret;
119 }