1*404b540aSrobert /* Utility routines for data type conversion for GCC.
2*404b540aSrobert Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3*404b540aSrobert 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4*404b540aSrobert
5*404b540aSrobert This file is part of GCC.
6*404b540aSrobert
7*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert the terms of the GNU General Public License as published by the Free
9*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert version.
11*404b540aSrobert
12*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*404b540aSrobert for more details.
16*404b540aSrobert
17*404b540aSrobert You should have received a copy of the GNU General Public License
18*404b540aSrobert along with GCC; see the file COPYING. If not, write to the Free
19*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20*404b540aSrobert 02110-1301, USA. */
21*404b540aSrobert
22*404b540aSrobert
23*404b540aSrobert /* These routines are somewhat language-independent utility function
24*404b540aSrobert intended to be called by the language-specific convert () functions. */
25*404b540aSrobert
26*404b540aSrobert #include "config.h"
27*404b540aSrobert #include "system.h"
28*404b540aSrobert #include "coretypes.h"
29*404b540aSrobert #include "tm.h"
30*404b540aSrobert #include "tree.h"
31*404b540aSrobert #include "flags.h"
32*404b540aSrobert #include "convert.h"
33*404b540aSrobert #include "toplev.h"
34*404b540aSrobert #include "langhooks.h"
35*404b540aSrobert #include "real.h"
36*404b540aSrobert
37*404b540aSrobert /* Convert EXPR to some pointer or reference type TYPE.
38*404b540aSrobert EXPR must be pointer, reference, integer, enumeral, or literal zero;
39*404b540aSrobert in other cases error is called. */
40*404b540aSrobert
41*404b540aSrobert tree
convert_to_pointer(tree type,tree expr)42*404b540aSrobert convert_to_pointer (tree type, tree expr)
43*404b540aSrobert {
44*404b540aSrobert if (TREE_TYPE (expr) == type)
45*404b540aSrobert return expr;
46*404b540aSrobert
47*404b540aSrobert if (integer_zerop (expr))
48*404b540aSrobert {
49*404b540aSrobert tree t = build_int_cst (type, 0);
50*404b540aSrobert if (TREE_OVERFLOW (expr) || TREE_CONSTANT_OVERFLOW (expr))
51*404b540aSrobert t = force_fit_type (t, 0, TREE_OVERFLOW (expr),
52*404b540aSrobert TREE_CONSTANT_OVERFLOW (expr));
53*404b540aSrobert return t;
54*404b540aSrobert }
55*404b540aSrobert
56*404b540aSrobert switch (TREE_CODE (TREE_TYPE (expr)))
57*404b540aSrobert {
58*404b540aSrobert case POINTER_TYPE:
59*404b540aSrobert case REFERENCE_TYPE:
60*404b540aSrobert return fold_build1 (NOP_EXPR, type, expr);
61*404b540aSrobert
62*404b540aSrobert case INTEGER_TYPE:
63*404b540aSrobert case ENUMERAL_TYPE:
64*404b540aSrobert case BOOLEAN_TYPE:
65*404b540aSrobert if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
66*404b540aSrobert expr = fold_build1 (NOP_EXPR,
67*404b540aSrobert lang_hooks.types.type_for_size (POINTER_SIZE, 0),
68*404b540aSrobert expr);
69*404b540aSrobert return fold_build1 (CONVERT_EXPR, type, expr);
70*404b540aSrobert
71*404b540aSrobert
72*404b540aSrobert default:
73*404b540aSrobert error ("cannot convert to a pointer type");
74*404b540aSrobert return convert_to_pointer (type, integer_zero_node);
75*404b540aSrobert }
76*404b540aSrobert }
77*404b540aSrobert
78*404b540aSrobert /* Avoid any floating point extensions from EXP. */
79*404b540aSrobert tree
strip_float_extensions(tree exp)80*404b540aSrobert strip_float_extensions (tree exp)
81*404b540aSrobert {
82*404b540aSrobert tree sub, expt, subt;
83*404b540aSrobert
84*404b540aSrobert /* For floating point constant look up the narrowest type that can hold
85*404b540aSrobert it properly and handle it like (type)(narrowest_type)constant.
86*404b540aSrobert This way we can optimize for instance a=a*2.0 where "a" is float
87*404b540aSrobert but 2.0 is double constant. */
88*404b540aSrobert if (TREE_CODE (exp) == REAL_CST)
89*404b540aSrobert {
90*404b540aSrobert REAL_VALUE_TYPE orig;
91*404b540aSrobert tree type = NULL;
92*404b540aSrobert
93*404b540aSrobert orig = TREE_REAL_CST (exp);
94*404b540aSrobert if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
95*404b540aSrobert && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
96*404b540aSrobert type = float_type_node;
97*404b540aSrobert else if (TYPE_PRECISION (TREE_TYPE (exp))
98*404b540aSrobert > TYPE_PRECISION (double_type_node)
99*404b540aSrobert && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
100*404b540aSrobert type = double_type_node;
101*404b540aSrobert if (type)
102*404b540aSrobert return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
103*404b540aSrobert }
104*404b540aSrobert
105*404b540aSrobert if (TREE_CODE (exp) != NOP_EXPR
106*404b540aSrobert && TREE_CODE (exp) != CONVERT_EXPR)
107*404b540aSrobert return exp;
108*404b540aSrobert
109*404b540aSrobert sub = TREE_OPERAND (exp, 0);
110*404b540aSrobert subt = TREE_TYPE (sub);
111*404b540aSrobert expt = TREE_TYPE (exp);
112*404b540aSrobert
113*404b540aSrobert if (!FLOAT_TYPE_P (subt))
114*404b540aSrobert return exp;
115*404b540aSrobert
116*404b540aSrobert if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
117*404b540aSrobert return exp;
118*404b540aSrobert
119*404b540aSrobert return strip_float_extensions (sub);
120*404b540aSrobert }
121*404b540aSrobert
122*404b540aSrobert
123*404b540aSrobert /* Convert EXPR to some floating-point type TYPE.
124*404b540aSrobert
125*404b540aSrobert EXPR must be float, integer, or enumeral;
126*404b540aSrobert in other cases error is called. */
127*404b540aSrobert
128*404b540aSrobert tree
convert_to_real(tree type,tree expr)129*404b540aSrobert convert_to_real (tree type, tree expr)
130*404b540aSrobert {
131*404b540aSrobert enum built_in_function fcode = builtin_mathfn_code (expr);
132*404b540aSrobert tree itype = TREE_TYPE (expr);
133*404b540aSrobert
134*404b540aSrobert /* Disable until we figure out how to decide whether the functions are
135*404b540aSrobert present in runtime. */
136*404b540aSrobert /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
137*404b540aSrobert if (optimize
138*404b540aSrobert && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
139*404b540aSrobert || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
140*404b540aSrobert {
141*404b540aSrobert switch (fcode)
142*404b540aSrobert {
143*404b540aSrobert #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
144*404b540aSrobert CASE_MATHFN (ACOS)
145*404b540aSrobert CASE_MATHFN (ACOSH)
146*404b540aSrobert CASE_MATHFN (ASIN)
147*404b540aSrobert CASE_MATHFN (ASINH)
148*404b540aSrobert CASE_MATHFN (ATAN)
149*404b540aSrobert CASE_MATHFN (ATANH)
150*404b540aSrobert CASE_MATHFN (CBRT)
151*404b540aSrobert CASE_MATHFN (COS)
152*404b540aSrobert CASE_MATHFN (COSH)
153*404b540aSrobert CASE_MATHFN (ERF)
154*404b540aSrobert CASE_MATHFN (ERFC)
155*404b540aSrobert CASE_MATHFN (EXP)
156*404b540aSrobert CASE_MATHFN (EXP10)
157*404b540aSrobert CASE_MATHFN (EXP2)
158*404b540aSrobert CASE_MATHFN (EXPM1)
159*404b540aSrobert CASE_MATHFN (FABS)
160*404b540aSrobert CASE_MATHFN (GAMMA)
161*404b540aSrobert CASE_MATHFN (J0)
162*404b540aSrobert CASE_MATHFN (J1)
163*404b540aSrobert CASE_MATHFN (LGAMMA)
164*404b540aSrobert CASE_MATHFN (LOG)
165*404b540aSrobert CASE_MATHFN (LOG10)
166*404b540aSrobert CASE_MATHFN (LOG1P)
167*404b540aSrobert CASE_MATHFN (LOG2)
168*404b540aSrobert CASE_MATHFN (LOGB)
169*404b540aSrobert CASE_MATHFN (POW10)
170*404b540aSrobert CASE_MATHFN (SIN)
171*404b540aSrobert CASE_MATHFN (SINH)
172*404b540aSrobert CASE_MATHFN (SQRT)
173*404b540aSrobert CASE_MATHFN (TAN)
174*404b540aSrobert CASE_MATHFN (TANH)
175*404b540aSrobert CASE_MATHFN (TGAMMA)
176*404b540aSrobert CASE_MATHFN (Y0)
177*404b540aSrobert CASE_MATHFN (Y1)
178*404b540aSrobert #undef CASE_MATHFN
179*404b540aSrobert {
180*404b540aSrobert tree arg0 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
181*404b540aSrobert tree newtype = type;
182*404b540aSrobert
183*404b540aSrobert /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
184*404b540aSrobert the both as the safe type for operation. */
185*404b540aSrobert if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
186*404b540aSrobert newtype = TREE_TYPE (arg0);
187*404b540aSrobert
188*404b540aSrobert /* Be careful about integer to fp conversions.
189*404b540aSrobert These may overflow still. */
190*404b540aSrobert if (FLOAT_TYPE_P (TREE_TYPE (arg0))
191*404b540aSrobert && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
192*404b540aSrobert && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
193*404b540aSrobert || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
194*404b540aSrobert {
195*404b540aSrobert tree arglist;
196*404b540aSrobert tree fn = mathfn_built_in (newtype, fcode);
197*404b540aSrobert
198*404b540aSrobert if (fn)
199*404b540aSrobert {
200*404b540aSrobert arglist = build_tree_list (NULL_TREE, fold (convert_to_real (newtype, arg0)));
201*404b540aSrobert expr = build_function_call_expr (fn, arglist);
202*404b540aSrobert if (newtype == type)
203*404b540aSrobert return expr;
204*404b540aSrobert }
205*404b540aSrobert }
206*404b540aSrobert }
207*404b540aSrobert default:
208*404b540aSrobert break;
209*404b540aSrobert }
210*404b540aSrobert }
211*404b540aSrobert if (optimize
212*404b540aSrobert && (((fcode == BUILT_IN_FLOORL
213*404b540aSrobert || fcode == BUILT_IN_CEILL
214*404b540aSrobert || fcode == BUILT_IN_ROUNDL
215*404b540aSrobert || fcode == BUILT_IN_RINTL
216*404b540aSrobert || fcode == BUILT_IN_TRUNCL
217*404b540aSrobert || fcode == BUILT_IN_NEARBYINTL)
218*404b540aSrobert && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
219*404b540aSrobert || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
220*404b540aSrobert || ((fcode == BUILT_IN_FLOOR
221*404b540aSrobert || fcode == BUILT_IN_CEIL
222*404b540aSrobert || fcode == BUILT_IN_ROUND
223*404b540aSrobert || fcode == BUILT_IN_RINT
224*404b540aSrobert || fcode == BUILT_IN_TRUNC
225*404b540aSrobert || fcode == BUILT_IN_NEARBYINT)
226*404b540aSrobert && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
227*404b540aSrobert {
228*404b540aSrobert tree fn = mathfn_built_in (type, fcode);
229*404b540aSrobert
230*404b540aSrobert if (fn)
231*404b540aSrobert {
232*404b540aSrobert tree arg
233*404b540aSrobert = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
234*404b540aSrobert
235*404b540aSrobert /* Make sure (type)arg0 is an extension, otherwise we could end up
236*404b540aSrobert changing (float)floor(double d) into floorf((float)d), which is
237*404b540aSrobert incorrect because (float)d uses round-to-nearest and can round
238*404b540aSrobert up to the next integer. */
239*404b540aSrobert if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
240*404b540aSrobert return
241*404b540aSrobert build_function_call_expr (fn,
242*404b540aSrobert build_tree_list (NULL_TREE,
243*404b540aSrobert fold (convert_to_real (type, arg))));
244*404b540aSrobert }
245*404b540aSrobert }
246*404b540aSrobert
247*404b540aSrobert /* Propagate the cast into the operation. */
248*404b540aSrobert if (itype != type && FLOAT_TYPE_P (type))
249*404b540aSrobert switch (TREE_CODE (expr))
250*404b540aSrobert {
251*404b540aSrobert /* Convert (float)-x into -(float)x. This is safe for
252*404b540aSrobert round-to-nearest rounding mode. */
253*404b540aSrobert case ABS_EXPR:
254*404b540aSrobert case NEGATE_EXPR:
255*404b540aSrobert if (!flag_rounding_math
256*404b540aSrobert && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
257*404b540aSrobert return build1 (TREE_CODE (expr), type,
258*404b540aSrobert fold (convert_to_real (type,
259*404b540aSrobert TREE_OPERAND (expr, 0))));
260*404b540aSrobert break;
261*404b540aSrobert /* Convert (outertype)((innertype0)a+(innertype1)b)
262*404b540aSrobert into ((newtype)a+(newtype)b) where newtype
263*404b540aSrobert is the widest mode from all of these. */
264*404b540aSrobert case PLUS_EXPR:
265*404b540aSrobert case MINUS_EXPR:
266*404b540aSrobert case MULT_EXPR:
267*404b540aSrobert case RDIV_EXPR:
268*404b540aSrobert {
269*404b540aSrobert tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
270*404b540aSrobert tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
271*404b540aSrobert
272*404b540aSrobert if (FLOAT_TYPE_P (TREE_TYPE (arg0))
273*404b540aSrobert && FLOAT_TYPE_P (TREE_TYPE (arg1)))
274*404b540aSrobert {
275*404b540aSrobert tree newtype = type;
276*404b540aSrobert
277*404b540aSrobert if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
278*404b540aSrobert || TYPE_MODE (TREE_TYPE (arg1)) == SDmode)
279*404b540aSrobert newtype = dfloat32_type_node;
280*404b540aSrobert if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
281*404b540aSrobert || TYPE_MODE (TREE_TYPE (arg1)) == DDmode)
282*404b540aSrobert newtype = dfloat64_type_node;
283*404b540aSrobert if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
284*404b540aSrobert || TYPE_MODE (TREE_TYPE (arg1)) == TDmode)
285*404b540aSrobert newtype = dfloat128_type_node;
286*404b540aSrobert if (newtype == dfloat32_type_node
287*404b540aSrobert || newtype == dfloat64_type_node
288*404b540aSrobert || newtype == dfloat128_type_node)
289*404b540aSrobert {
290*404b540aSrobert expr = build2 (TREE_CODE (expr), newtype,
291*404b540aSrobert fold (convert_to_real (newtype, arg0)),
292*404b540aSrobert fold (convert_to_real (newtype, arg1)));
293*404b540aSrobert if (newtype == type)
294*404b540aSrobert return expr;
295*404b540aSrobert break;
296*404b540aSrobert }
297*404b540aSrobert
298*404b540aSrobert if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
299*404b540aSrobert newtype = TREE_TYPE (arg0);
300*404b540aSrobert if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
301*404b540aSrobert newtype = TREE_TYPE (arg1);
302*404b540aSrobert if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
303*404b540aSrobert {
304*404b540aSrobert expr = build2 (TREE_CODE (expr), newtype,
305*404b540aSrobert fold (convert_to_real (newtype, arg0)),
306*404b540aSrobert fold (convert_to_real (newtype, arg1)));
307*404b540aSrobert if (newtype == type)
308*404b540aSrobert return expr;
309*404b540aSrobert }
310*404b540aSrobert }
311*404b540aSrobert }
312*404b540aSrobert break;
313*404b540aSrobert default:
314*404b540aSrobert break;
315*404b540aSrobert }
316*404b540aSrobert
317*404b540aSrobert switch (TREE_CODE (TREE_TYPE (expr)))
318*404b540aSrobert {
319*404b540aSrobert case REAL_TYPE:
320*404b540aSrobert /* Ignore the conversion if we don't need to store intermediate
321*404b540aSrobert results and neither type is a decimal float. */
322*404b540aSrobert return build1 ((flag_float_store
323*404b540aSrobert || DECIMAL_FLOAT_TYPE_P (type)
324*404b540aSrobert || DECIMAL_FLOAT_TYPE_P (itype))
325*404b540aSrobert ? CONVERT_EXPR : NOP_EXPR, type, expr);
326*404b540aSrobert
327*404b540aSrobert case INTEGER_TYPE:
328*404b540aSrobert case ENUMERAL_TYPE:
329*404b540aSrobert case BOOLEAN_TYPE:
330*404b540aSrobert return build1 (FLOAT_EXPR, type, expr);
331*404b540aSrobert
332*404b540aSrobert case COMPLEX_TYPE:
333*404b540aSrobert return convert (type,
334*404b540aSrobert fold_build1 (REALPART_EXPR,
335*404b540aSrobert TREE_TYPE (TREE_TYPE (expr)), expr));
336*404b540aSrobert
337*404b540aSrobert case POINTER_TYPE:
338*404b540aSrobert case REFERENCE_TYPE:
339*404b540aSrobert error ("pointer value used where a floating point value was expected");
340*404b540aSrobert return convert_to_real (type, integer_zero_node);
341*404b540aSrobert
342*404b540aSrobert default:
343*404b540aSrobert error ("aggregate value used where a float was expected");
344*404b540aSrobert return convert_to_real (type, integer_zero_node);
345*404b540aSrobert }
346*404b540aSrobert }
347*404b540aSrobert
348*404b540aSrobert /* Convert EXPR to some integer (or enum) type TYPE.
349*404b540aSrobert
350*404b540aSrobert EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
351*404b540aSrobert vector; in other cases error is called.
352*404b540aSrobert
353*404b540aSrobert The result of this is always supposed to be a newly created tree node
354*404b540aSrobert not in use in any existing structure. */
355*404b540aSrobert
356*404b540aSrobert tree
convert_to_integer(tree type,tree expr)357*404b540aSrobert convert_to_integer (tree type, tree expr)
358*404b540aSrobert {
359*404b540aSrobert enum tree_code ex_form = TREE_CODE (expr);
360*404b540aSrobert tree intype = TREE_TYPE (expr);
361*404b540aSrobert unsigned int inprec = TYPE_PRECISION (intype);
362*404b540aSrobert unsigned int outprec = TYPE_PRECISION (type);
363*404b540aSrobert
364*404b540aSrobert /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
365*404b540aSrobert be. Consider `enum E = { a, b = (enum E) 3 };'. */
366*404b540aSrobert if (!COMPLETE_TYPE_P (type))
367*404b540aSrobert {
368*404b540aSrobert error ("conversion to incomplete type");
369*404b540aSrobert return error_mark_node;
370*404b540aSrobert }
371*404b540aSrobert
372*404b540aSrobert /* Convert e.g. (long)round(d) -> lround(d). */
373*404b540aSrobert /* If we're converting to char, we may encounter differing behavior
374*404b540aSrobert between converting from double->char vs double->long->char.
375*404b540aSrobert We're in "undefined" territory but we prefer to be conservative,
376*404b540aSrobert so only proceed in "unsafe" math mode. */
377*404b540aSrobert if (optimize
378*404b540aSrobert && (flag_unsafe_math_optimizations
379*404b540aSrobert || (long_integer_type_node
380*404b540aSrobert && outprec >= TYPE_PRECISION (long_integer_type_node))))
381*404b540aSrobert {
382*404b540aSrobert tree s_expr = strip_float_extensions (expr);
383*404b540aSrobert tree s_intype = TREE_TYPE (s_expr);
384*404b540aSrobert const enum built_in_function fcode = builtin_mathfn_code (s_expr);
385*404b540aSrobert tree fn = 0;
386*404b540aSrobert
387*404b540aSrobert switch (fcode)
388*404b540aSrobert {
389*404b540aSrobert CASE_FLT_FN (BUILT_IN_CEIL):
390*404b540aSrobert /* Only convert in ISO C99 mode. */
391*404b540aSrobert if (!TARGET_C99_FUNCTIONS)
392*404b540aSrobert break;
393*404b540aSrobert if (outprec < TYPE_PRECISION (long_integer_type_node)
394*404b540aSrobert || (outprec == TYPE_PRECISION (long_integer_type_node)
395*404b540aSrobert && !TYPE_UNSIGNED (type)))
396*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
397*404b540aSrobert else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
398*404b540aSrobert && !TYPE_UNSIGNED (type))
399*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
400*404b540aSrobert break;
401*404b540aSrobert
402*404b540aSrobert CASE_FLT_FN (BUILT_IN_FLOOR):
403*404b540aSrobert /* Only convert in ISO C99 mode. */
404*404b540aSrobert if (!TARGET_C99_FUNCTIONS)
405*404b540aSrobert break;
406*404b540aSrobert if (outprec < TYPE_PRECISION (long_integer_type_node)
407*404b540aSrobert || (outprec == TYPE_PRECISION (long_integer_type_node)
408*404b540aSrobert && !TYPE_UNSIGNED (type)))
409*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
410*404b540aSrobert else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
411*404b540aSrobert && !TYPE_UNSIGNED (type))
412*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
413*404b540aSrobert break;
414*404b540aSrobert
415*404b540aSrobert CASE_FLT_FN (BUILT_IN_ROUND):
416*404b540aSrobert if (outprec < TYPE_PRECISION (long_integer_type_node)
417*404b540aSrobert || (outprec == TYPE_PRECISION (long_integer_type_node)
418*404b540aSrobert && !TYPE_UNSIGNED (type)))
419*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
420*404b540aSrobert else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
421*404b540aSrobert && !TYPE_UNSIGNED (type))
422*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
423*404b540aSrobert break;
424*404b540aSrobert
425*404b540aSrobert CASE_FLT_FN (BUILT_IN_NEARBYINT):
426*404b540aSrobert /* Only convert nearbyint* if we can ignore math exceptions. */
427*404b540aSrobert if (flag_trapping_math)
428*404b540aSrobert break;
429*404b540aSrobert /* ... Fall through ... */
430*404b540aSrobert CASE_FLT_FN (BUILT_IN_RINT):
431*404b540aSrobert if (outprec < TYPE_PRECISION (long_integer_type_node)
432*404b540aSrobert || (outprec == TYPE_PRECISION (long_integer_type_node)
433*404b540aSrobert && !TYPE_UNSIGNED (type)))
434*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
435*404b540aSrobert else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
436*404b540aSrobert && !TYPE_UNSIGNED (type))
437*404b540aSrobert fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
438*404b540aSrobert break;
439*404b540aSrobert
440*404b540aSrobert CASE_FLT_FN (BUILT_IN_TRUNC):
441*404b540aSrobert {
442*404b540aSrobert tree arglist = TREE_OPERAND (s_expr, 1);
443*404b540aSrobert return convert_to_integer (type, TREE_VALUE (arglist));
444*404b540aSrobert }
445*404b540aSrobert
446*404b540aSrobert default:
447*404b540aSrobert break;
448*404b540aSrobert }
449*404b540aSrobert
450*404b540aSrobert if (fn)
451*404b540aSrobert {
452*404b540aSrobert tree arglist = TREE_OPERAND (s_expr, 1);
453*404b540aSrobert tree newexpr = build_function_call_expr (fn, arglist);
454*404b540aSrobert return convert_to_integer (type, newexpr);
455*404b540aSrobert }
456*404b540aSrobert }
457*404b540aSrobert
458*404b540aSrobert switch (TREE_CODE (intype))
459*404b540aSrobert {
460*404b540aSrobert case POINTER_TYPE:
461*404b540aSrobert case REFERENCE_TYPE:
462*404b540aSrobert if (integer_zerop (expr))
463*404b540aSrobert return build_int_cst (type, 0);
464*404b540aSrobert
465*404b540aSrobert /* Convert to an unsigned integer of the correct width first,
466*404b540aSrobert and from there widen/truncate to the required type. */
467*404b540aSrobert expr = fold_build1 (CONVERT_EXPR,
468*404b540aSrobert lang_hooks.types.type_for_size (POINTER_SIZE, 0),
469*404b540aSrobert expr);
470*404b540aSrobert return fold_convert (type, expr);
471*404b540aSrobert
472*404b540aSrobert case INTEGER_TYPE:
473*404b540aSrobert case ENUMERAL_TYPE:
474*404b540aSrobert case BOOLEAN_TYPE:
475*404b540aSrobert /* If this is a logical operation, which just returns 0 or 1, we can
476*404b540aSrobert change the type of the expression. */
477*404b540aSrobert
478*404b540aSrobert if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
479*404b540aSrobert {
480*404b540aSrobert expr = copy_node (expr);
481*404b540aSrobert TREE_TYPE (expr) = type;
482*404b540aSrobert return expr;
483*404b540aSrobert }
484*404b540aSrobert
485*404b540aSrobert /* If we are widening the type, put in an explicit conversion.
486*404b540aSrobert Similarly if we are not changing the width. After this, we know
487*404b540aSrobert we are truncating EXPR. */
488*404b540aSrobert
489*404b540aSrobert else if (outprec >= inprec)
490*404b540aSrobert {
491*404b540aSrobert enum tree_code code;
492*404b540aSrobert tree tem;
493*404b540aSrobert
494*404b540aSrobert /* If the precision of the EXPR's type is K bits and the
495*404b540aSrobert destination mode has more bits, and the sign is changing,
496*404b540aSrobert it is not safe to use a NOP_EXPR. For example, suppose
497*404b540aSrobert that EXPR's type is a 3-bit unsigned integer type, the
498*404b540aSrobert TYPE is a 3-bit signed integer type, and the machine mode
499*404b540aSrobert for the types is 8-bit QImode. In that case, the
500*404b540aSrobert conversion necessitates an explicit sign-extension. In
501*404b540aSrobert the signed-to-unsigned case the high-order bits have to
502*404b540aSrobert be cleared. */
503*404b540aSrobert if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
504*404b540aSrobert && (TYPE_PRECISION (TREE_TYPE (expr))
505*404b540aSrobert != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
506*404b540aSrobert code = CONVERT_EXPR;
507*404b540aSrobert else
508*404b540aSrobert code = NOP_EXPR;
509*404b540aSrobert
510*404b540aSrobert tem = fold_unary (code, type, expr);
511*404b540aSrobert if (tem)
512*404b540aSrobert return tem;
513*404b540aSrobert
514*404b540aSrobert tem = build1 (code, type, expr);
515*404b540aSrobert TREE_NO_WARNING (tem) = 1;
516*404b540aSrobert return tem;
517*404b540aSrobert }
518*404b540aSrobert
519*404b540aSrobert /* If TYPE is an enumeral type or a type with a precision less
520*404b540aSrobert than the number of bits in its mode, do the conversion to the
521*404b540aSrobert type corresponding to its mode, then do a nop conversion
522*404b540aSrobert to TYPE. */
523*404b540aSrobert else if (TREE_CODE (type) == ENUMERAL_TYPE
524*404b540aSrobert || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
525*404b540aSrobert return build1 (NOP_EXPR, type,
526*404b540aSrobert convert (lang_hooks.types.type_for_mode
527*404b540aSrobert (TYPE_MODE (type), TYPE_UNSIGNED (type)),
528*404b540aSrobert expr));
529*404b540aSrobert
530*404b540aSrobert /* Here detect when we can distribute the truncation down past some
531*404b540aSrobert arithmetic. For example, if adding two longs and converting to an
532*404b540aSrobert int, we can equally well convert both to ints and then add.
533*404b540aSrobert For the operations handled here, such truncation distribution
534*404b540aSrobert is always safe.
535*404b540aSrobert It is desirable in these cases:
536*404b540aSrobert 1) when truncating down to full-word from a larger size
537*404b540aSrobert 2) when truncating takes no work.
538*404b540aSrobert 3) when at least one operand of the arithmetic has been extended
539*404b540aSrobert (as by C's default conversions). In this case we need two conversions
540*404b540aSrobert if we do the arithmetic as already requested, so we might as well
541*404b540aSrobert truncate both and then combine. Perhaps that way we need only one.
542*404b540aSrobert
543*404b540aSrobert Note that in general we cannot do the arithmetic in a type
544*404b540aSrobert shorter than the desired result of conversion, even if the operands
545*404b540aSrobert are both extended from a shorter type, because they might overflow
546*404b540aSrobert if combined in that type. The exceptions to this--the times when
547*404b540aSrobert two narrow values can be combined in their narrow type even to
548*404b540aSrobert make a wider result--are handled by "shorten" in build_binary_op. */
549*404b540aSrobert
550*404b540aSrobert switch (ex_form)
551*404b540aSrobert {
552*404b540aSrobert case RSHIFT_EXPR:
553*404b540aSrobert /* We can pass truncation down through right shifting
554*404b540aSrobert when the shift count is a nonpositive constant. */
555*404b540aSrobert if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
556*404b540aSrobert && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
557*404b540aSrobert goto trunc1;
558*404b540aSrobert break;
559*404b540aSrobert
560*404b540aSrobert case LSHIFT_EXPR:
561*404b540aSrobert /* We can pass truncation down through left shifting
562*404b540aSrobert when the shift count is a nonnegative constant and
563*404b540aSrobert the target type is unsigned. */
564*404b540aSrobert if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
565*404b540aSrobert && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
566*404b540aSrobert && TYPE_UNSIGNED (type)
567*404b540aSrobert && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
568*404b540aSrobert {
569*404b540aSrobert /* If shift count is less than the width of the truncated type,
570*404b540aSrobert really shift. */
571*404b540aSrobert if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
572*404b540aSrobert /* In this case, shifting is like multiplication. */
573*404b540aSrobert goto trunc1;
574*404b540aSrobert else
575*404b540aSrobert {
576*404b540aSrobert /* If it is >= that width, result is zero.
577*404b540aSrobert Handling this with trunc1 would give the wrong result:
578*404b540aSrobert (int) ((long long) a << 32) is well defined (as 0)
579*404b540aSrobert but (int) a << 32 is undefined and would get a
580*404b540aSrobert warning. */
581*404b540aSrobert
582*404b540aSrobert tree t = build_int_cst (type, 0);
583*404b540aSrobert
584*404b540aSrobert /* If the original expression had side-effects, we must
585*404b540aSrobert preserve it. */
586*404b540aSrobert if (TREE_SIDE_EFFECTS (expr))
587*404b540aSrobert return build2 (COMPOUND_EXPR, type, expr, t);
588*404b540aSrobert else
589*404b540aSrobert return t;
590*404b540aSrobert }
591*404b540aSrobert }
592*404b540aSrobert break;
593*404b540aSrobert
594*404b540aSrobert case MAX_EXPR:
595*404b540aSrobert case MIN_EXPR:
596*404b540aSrobert case MULT_EXPR:
597*404b540aSrobert {
598*404b540aSrobert tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
599*404b540aSrobert tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
600*404b540aSrobert
601*404b540aSrobert /* Don't distribute unless the output precision is at least as big
602*404b540aSrobert as the actual inputs. Otherwise, the comparison of the
603*404b540aSrobert truncated values will be wrong. */
604*404b540aSrobert if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
605*404b540aSrobert && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
606*404b540aSrobert /* If signedness of arg0 and arg1 don't match,
607*404b540aSrobert we can't necessarily find a type to compare them in. */
608*404b540aSrobert && (TYPE_UNSIGNED (TREE_TYPE (arg0))
609*404b540aSrobert == TYPE_UNSIGNED (TREE_TYPE (arg1))))
610*404b540aSrobert goto trunc1;
611*404b540aSrobert break;
612*404b540aSrobert }
613*404b540aSrobert
614*404b540aSrobert case PLUS_EXPR:
615*404b540aSrobert case MINUS_EXPR:
616*404b540aSrobert case BIT_AND_EXPR:
617*404b540aSrobert case BIT_IOR_EXPR:
618*404b540aSrobert case BIT_XOR_EXPR:
619*404b540aSrobert trunc1:
620*404b540aSrobert {
621*404b540aSrobert tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
622*404b540aSrobert tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
623*404b540aSrobert
624*404b540aSrobert if (outprec >= BITS_PER_WORD
625*404b540aSrobert || TRULY_NOOP_TRUNCATION (outprec, inprec)
626*404b540aSrobert || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
627*404b540aSrobert || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
628*404b540aSrobert {
629*404b540aSrobert /* Do the arithmetic in type TYPEX,
630*404b540aSrobert then convert result to TYPE. */
631*404b540aSrobert tree typex = type;
632*404b540aSrobert
633*404b540aSrobert /* Can't do arithmetic in enumeral types
634*404b540aSrobert so use an integer type that will hold the values. */
635*404b540aSrobert if (TREE_CODE (typex) == ENUMERAL_TYPE)
636*404b540aSrobert typex = lang_hooks.types.type_for_size
637*404b540aSrobert (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
638*404b540aSrobert
639*404b540aSrobert /* But now perhaps TYPEX is as wide as INPREC.
640*404b540aSrobert In that case, do nothing special here.
641*404b540aSrobert (Otherwise would recurse infinitely in convert. */
642*404b540aSrobert if (TYPE_PRECISION (typex) != inprec)
643*404b540aSrobert {
644*404b540aSrobert /* Don't do unsigned arithmetic where signed was wanted,
645*404b540aSrobert or vice versa.
646*404b540aSrobert Exception: if both of the original operands were
647*404b540aSrobert unsigned then we can safely do the work as unsigned.
648*404b540aSrobert Exception: shift operations take their type solely
649*404b540aSrobert from the first argument.
650*404b540aSrobert Exception: the LSHIFT_EXPR case above requires that
651*404b540aSrobert we perform this operation unsigned lest we produce
652*404b540aSrobert signed-overflow undefinedness.
653*404b540aSrobert And we may need to do it as unsigned
654*404b540aSrobert if we truncate to the original size. */
655*404b540aSrobert if (TYPE_UNSIGNED (TREE_TYPE (expr))
656*404b540aSrobert || (TYPE_UNSIGNED (TREE_TYPE (arg0))
657*404b540aSrobert && (TYPE_UNSIGNED (TREE_TYPE (arg1))
658*404b540aSrobert || ex_form == LSHIFT_EXPR
659*404b540aSrobert || ex_form == RSHIFT_EXPR
660*404b540aSrobert || ex_form == LROTATE_EXPR
661*404b540aSrobert || ex_form == RROTATE_EXPR))
662*404b540aSrobert || ex_form == LSHIFT_EXPR
663*404b540aSrobert /* If we have !flag_wrapv, and either ARG0 or
664*404b540aSrobert ARG1 is of a signed type, we have to do
665*404b540aSrobert PLUS_EXPR or MINUS_EXPR in an unsigned
666*404b540aSrobert type. Otherwise, we would introduce
667*404b540aSrobert signed-overflow undefinedness. */
668*404b540aSrobert || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
669*404b540aSrobert || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
670*404b540aSrobert && (ex_form == PLUS_EXPR
671*404b540aSrobert || ex_form == MINUS_EXPR)))
672*404b540aSrobert typex = lang_hooks.types.unsigned_type (typex);
673*404b540aSrobert else
674*404b540aSrobert typex = lang_hooks.types.signed_type (typex);
675*404b540aSrobert return convert (type,
676*404b540aSrobert fold_build2 (ex_form, typex,
677*404b540aSrobert convert (typex, arg0),
678*404b540aSrobert convert (typex, arg1)));
679*404b540aSrobert }
680*404b540aSrobert }
681*404b540aSrobert }
682*404b540aSrobert break;
683*404b540aSrobert
684*404b540aSrobert case NEGATE_EXPR:
685*404b540aSrobert case BIT_NOT_EXPR:
686*404b540aSrobert /* This is not correct for ABS_EXPR,
687*404b540aSrobert since we must test the sign before truncation. */
688*404b540aSrobert {
689*404b540aSrobert tree typex;
690*404b540aSrobert
691*404b540aSrobert /* Don't do unsigned arithmetic where signed was wanted,
692*404b540aSrobert or vice versa. */
693*404b540aSrobert if (TYPE_UNSIGNED (TREE_TYPE (expr)))
694*404b540aSrobert typex = lang_hooks.types.unsigned_type (type);
695*404b540aSrobert else
696*404b540aSrobert typex = lang_hooks.types.signed_type (type);
697*404b540aSrobert return convert (type,
698*404b540aSrobert fold_build1 (ex_form, typex,
699*404b540aSrobert convert (typex,
700*404b540aSrobert TREE_OPERAND (expr, 0))));
701*404b540aSrobert }
702*404b540aSrobert
703*404b540aSrobert case NOP_EXPR:
704*404b540aSrobert /* Don't introduce a
705*404b540aSrobert "can't convert between vector values of different size" error. */
706*404b540aSrobert if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
707*404b540aSrobert && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
708*404b540aSrobert != GET_MODE_SIZE (TYPE_MODE (type))))
709*404b540aSrobert break;
710*404b540aSrobert /* If truncating after truncating, might as well do all at once.
711*404b540aSrobert If truncating after extending, we may get rid of wasted work. */
712*404b540aSrobert return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
713*404b540aSrobert
714*404b540aSrobert case COND_EXPR:
715*404b540aSrobert /* It is sometimes worthwhile to push the narrowing down through
716*404b540aSrobert the conditional and never loses. */
717*404b540aSrobert return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
718*404b540aSrobert convert (type, TREE_OPERAND (expr, 1)),
719*404b540aSrobert convert (type, TREE_OPERAND (expr, 2)));
720*404b540aSrobert
721*404b540aSrobert default:
722*404b540aSrobert break;
723*404b540aSrobert }
724*404b540aSrobert
725*404b540aSrobert return build1 (CONVERT_EXPR, type, expr);
726*404b540aSrobert
727*404b540aSrobert case REAL_TYPE:
728*404b540aSrobert return build1 (FIX_TRUNC_EXPR, type, expr);
729*404b540aSrobert
730*404b540aSrobert case COMPLEX_TYPE:
731*404b540aSrobert return convert (type,
732*404b540aSrobert fold_build1 (REALPART_EXPR,
733*404b540aSrobert TREE_TYPE (TREE_TYPE (expr)), expr));
734*404b540aSrobert
735*404b540aSrobert case VECTOR_TYPE:
736*404b540aSrobert if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
737*404b540aSrobert {
738*404b540aSrobert error ("can't convert between vector values of different size");
739*404b540aSrobert return error_mark_node;
740*404b540aSrobert }
741*404b540aSrobert return build1 (VIEW_CONVERT_EXPR, type, expr);
742*404b540aSrobert
743*404b540aSrobert default:
744*404b540aSrobert error ("aggregate value used where an integer was expected");
745*404b540aSrobert return convert (type, integer_zero_node);
746*404b540aSrobert }
747*404b540aSrobert }
748*404b540aSrobert
749*404b540aSrobert /* Convert EXPR to the complex type TYPE in the usual ways. */
750*404b540aSrobert
751*404b540aSrobert tree
convert_to_complex(tree type,tree expr)752*404b540aSrobert convert_to_complex (tree type, tree expr)
753*404b540aSrobert {
754*404b540aSrobert tree subtype = TREE_TYPE (type);
755*404b540aSrobert
756*404b540aSrobert switch (TREE_CODE (TREE_TYPE (expr)))
757*404b540aSrobert {
758*404b540aSrobert case REAL_TYPE:
759*404b540aSrobert case INTEGER_TYPE:
760*404b540aSrobert case ENUMERAL_TYPE:
761*404b540aSrobert case BOOLEAN_TYPE:
762*404b540aSrobert return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
763*404b540aSrobert convert (subtype, integer_zero_node));
764*404b540aSrobert
765*404b540aSrobert case COMPLEX_TYPE:
766*404b540aSrobert {
767*404b540aSrobert tree elt_type = TREE_TYPE (TREE_TYPE (expr));
768*404b540aSrobert
769*404b540aSrobert if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
770*404b540aSrobert return expr;
771*404b540aSrobert else if (TREE_CODE (expr) == COMPLEX_EXPR)
772*404b540aSrobert return fold_build2 (COMPLEX_EXPR, type,
773*404b540aSrobert convert (subtype, TREE_OPERAND (expr, 0)),
774*404b540aSrobert convert (subtype, TREE_OPERAND (expr, 1)));
775*404b540aSrobert else
776*404b540aSrobert {
777*404b540aSrobert expr = save_expr (expr);
778*404b540aSrobert return
779*404b540aSrobert fold_build2 (COMPLEX_EXPR, type,
780*404b540aSrobert convert (subtype,
781*404b540aSrobert fold_build1 (REALPART_EXPR,
782*404b540aSrobert TREE_TYPE (TREE_TYPE (expr)),
783*404b540aSrobert expr)),
784*404b540aSrobert convert (subtype,
785*404b540aSrobert fold_build1 (IMAGPART_EXPR,
786*404b540aSrobert TREE_TYPE (TREE_TYPE (expr)),
787*404b540aSrobert expr)));
788*404b540aSrobert }
789*404b540aSrobert }
790*404b540aSrobert
791*404b540aSrobert case POINTER_TYPE:
792*404b540aSrobert case REFERENCE_TYPE:
793*404b540aSrobert error ("pointer value used where a complex was expected");
794*404b540aSrobert return convert_to_complex (type, integer_zero_node);
795*404b540aSrobert
796*404b540aSrobert default:
797*404b540aSrobert error ("aggregate value used where a complex was expected");
798*404b540aSrobert return convert_to_complex (type, integer_zero_node);
799*404b540aSrobert }
800*404b540aSrobert }
801*404b540aSrobert
802*404b540aSrobert /* Convert EXPR to the vector type TYPE in the usual ways. */
803*404b540aSrobert
804*404b540aSrobert tree
convert_to_vector(tree type,tree expr)805*404b540aSrobert convert_to_vector (tree type, tree expr)
806*404b540aSrobert {
807*404b540aSrobert switch (TREE_CODE (TREE_TYPE (expr)))
808*404b540aSrobert {
809*404b540aSrobert case INTEGER_TYPE:
810*404b540aSrobert case VECTOR_TYPE:
811*404b540aSrobert if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
812*404b540aSrobert {
813*404b540aSrobert error ("can't convert between vector values of different size");
814*404b540aSrobert return error_mark_node;
815*404b540aSrobert }
816*404b540aSrobert return build1 (VIEW_CONVERT_EXPR, type, expr);
817*404b540aSrobert
818*404b540aSrobert default:
819*404b540aSrobert error ("can't convert value to a vector");
820*404b540aSrobert return error_mark_node;
821*404b540aSrobert }
822*404b540aSrobert }
823