|
|
1.1 root 1: /* Language-level data type conversion for GNU C.
2: Copyright (C) 1987, 1988, 1991 Free Software Foundation, Inc.
3:
4: This file is part of GNU CC.
5:
6: GNU CC is free software; you can redistribute it and/or modify
7: it under the terms of the GNU General Public License as published by
8: the Free Software Foundation; either version 2, or (at your option)
9: any later version.
10:
11: GNU CC is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with GNU CC; see the file COPYING. If not, write to
18: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19:
20:
21: /* This file contains the functions for converting C expressions
22: to different data types. The only entry point is `convert'.
23: Every language front end must have a `convert' function
24: but what kind of conversions it does will depend on the language. */
25:
26: #include "config.h"
27: #include "tree.h"
28: #include "flags.h"
29:
30: /* Change of width--truncation and extension of integers or reals--
31: is represented with NOP_EXPR. Proper functioning of many things
32: assumes that no other conversions can be NOP_EXPRs.
33:
34: Conversion between integer and pointer is represented with CONVERT_EXPR.
35: Converting integer to real uses FLOAT_EXPR
36: and real to integer uses FIX_TRUNC_EXPR.
37:
38: Here is a list of all the functions that assume that widening and
39: narrowing is always done with a NOP_EXPR:
40: In c-convert.c, convert_to_integer.
41: In c-typeck.c, build_binary_op (boolean ops), and truthvalue_conversion.
42: In expr.c: expand_expr, for operands of a MULT_EXPR.
43: In fold-const.c: fold.
44: In tree.c: get_narrower and get_unwidened. */
45:
46: /* Subroutines of `convert'. */
47:
48: static tree
49: convert_to_pointer (type, expr)
50: tree type, expr;
51: {
52: register tree intype = TREE_TYPE (expr);
53: register enum tree_code form = TREE_CODE (intype);
54:
55: if (integer_zerop (expr))
56: {
57: if (type == TREE_TYPE (null_pointer_node))
58: return null_pointer_node;
59: expr = build_int_2 (0, 0);
60: TREE_TYPE (expr) = type;
61: return expr;
62: }
63:
64: if (form == POINTER_TYPE)
65: return build1 (NOP_EXPR, type, expr);
66:
67:
68: if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
69: {
70: if (type_precision (intype) == POINTER_SIZE)
71: return build1 (CONVERT_EXPR, type, expr);
72: expr = convert (type_for_size (POINTER_SIZE, 0), expr);
73: if (TYPE_MODE (TREE_TYPE (expr)) != TYPE_MODE (type))
74: /* There is supposed to be some integral type
75: that is the same width as a pointer. */
76: abort ();
77: return convert_to_pointer (type, expr);
78: }
79:
80: error ("cannot convert to a pointer type");
81:
82: return null_pointer_node;
83: }
84:
85: static tree
86: convert_to_real (type, expr)
87: tree type, expr;
88: {
89: register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
90:
91: if (form == REAL_TYPE)
92: return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
93: type, expr);
94:
95: if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
96: return build1 (FLOAT_EXPR, type, expr);
97:
98: if (form == POINTER_TYPE)
99: error ("pointer value used where a float was expected");
100: else
101: error ("aggregate value used where a float was expected");
102:
103: {
104: register tree tem = make_node (REAL_CST);
105: TREE_TYPE (tem) = type;
106: TREE_REAL_CST (tem) = REAL_VALUE_ATOF ("0.0");
107: return tem;
108: }
109: }
110:
111: /* The result of this is always supposed to be a newly created tree node
112: not in use in any existing structure. */
113:
114: static tree
115: convert_to_integer (type, expr)
116: tree type, expr;
117: {
118: register tree intype = TREE_TYPE (expr);
119: register enum tree_code form = TREE_CODE (intype);
120:
121: if (form == POINTER_TYPE)
122: {
123: if (integer_zerop (expr))
124: expr = integer_zero_node;
125: else
126: expr = fold (build1 (CONVERT_EXPR,
127: type_for_size (POINTER_SIZE, 0), expr));
128: intype = TREE_TYPE (expr);
129: form = TREE_CODE (intype);
130: if (intype == type)
131: return expr;
132: }
133:
134: if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
135: {
136: register unsigned outprec = TYPE_PRECISION (type);
137: register unsigned inprec = TYPE_PRECISION (intype);
138: register enum tree_code ex_form = TREE_CODE (expr);
139:
1.1.1.3 ! root 140: /* If we are widening the type, put in an explicit conversion.
! 141: Similarly if we are not changing the width. However, if this is
! 142: a logical operation that just returns 0 or 1, we can change the
! 143: type of the expression (see below). */
! 144:
! 145: if (TREE_CODE_CLASS (ex_form) == '<'
! 146: || ex_form == TRUTH_AND_EXPR || ex_form == TRUTH_ANDIF_EXPR
! 147: || ex_form == TRUTH_OR_EXPR || ex_form == TRUTH_ORIF_EXPR
! 148: || ex_form == TRUTH_NOT_EXPR)
! 149: {
! 150: TREE_TYPE (expr) = type;
! 151: return expr;
! 152: }
! 153: else if (outprec >= inprec)
1.1 root 154: return build1 (NOP_EXPR, type, expr);
155:
156: /* Here detect when we can distribute the truncation down past some arithmetic.
157: For example, if adding two longs and converting to an int,
158: we can equally well convert both to ints and then add.
159: For the operations handled here, such truncation distribution
160: is always safe.
161: It is desirable in these cases:
162: 1) when truncating down to full-word from a larger size
163: 2) when truncating takes no work.
164: 3) when at least one operand of the arithmetic has been extended
165: (as by C's default conversions). In this case we need two conversions
166: if we do the arithmetic as already requested, so we might as well
167: truncate both and then combine. Perhaps that way we need only one.
168:
169: Note that in general we cannot do the arithmetic in a type
170: shorter than the desired result of conversion, even if the operands
171: are both extended from a shorter type, because they might overflow
172: if combined in that type. The exceptions to this--the times when
173: two narrow values can be combined in their narrow type even to
174: make a wider result--are handled by "shorten" in build_binary_op. */
175:
176: switch (ex_form)
177: {
178: case RSHIFT_EXPR:
179: /* We can pass truncation down through right shifting
1.1.1.3 ! root 180: when the shift count is a nonpositive constant. */
! 181: if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
! 182: && tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_one_node))
! 183: goto trunc1;
! 184: break;
1.1 root 185:
186: case LSHIFT_EXPR:
187: /* We can pass truncation down through left shifting
1.1.1.3 ! root 188: when the shift count is a nonnegative constant. */
! 189: if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
! 190: && ! tree_int_cst_lt (TREE_OPERAND (expr, 1), integer_zero_node)
! 191: && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
! 192: {
! 193: /* If shift count is less than the width of the truncated type,
! 194: really shift. */
! 195: if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
! 196: /* In this case, shifting is like multiplication. */
! 197: goto trunc1;
! 198: else
! 199: /* If it is >= that width, result is zero.
! 200: Handling this with trunc1 would give the wrong result:
! 201: (int) ((long long) a << 32) is well defined (as 0)
! 202: but (int) a << 32 is undefined and would get a warning. */
! 203: return convert_to_integer (type, integer_zero_node);
! 204: }
! 205: break;
1.1 root 206:
207: case MAX_EXPR:
208: case MIN_EXPR:
209: case MULT_EXPR:
210: {
211: tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
212: tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
213:
214: /* Don't distribute unless the output precision is at least as big
215: as the actual inputs. Otherwise, the comparison of the
216: truncated values will be wrong. */
217: if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
218: && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
219: /* If signedness of arg0 and arg1 don't match,
220: we can't necessarily find a type to compare them in. */
221: && (TREE_UNSIGNED (TREE_TYPE (arg0))
222: == TREE_UNSIGNED (TREE_TYPE (arg1))))
223: goto trunc1;
224: break;
225: }
226:
227: case PLUS_EXPR:
228: case MINUS_EXPR:
229: case BIT_AND_EXPR:
230: case BIT_IOR_EXPR:
231: case BIT_XOR_EXPR:
232: case BIT_ANDTC_EXPR:
233: trunc1:
234: {
235: tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
236: tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
237:
238: if (outprec >= BITS_PER_WORD
239: || TRULY_NOOP_TRUNCATION (outprec, inprec)
240: || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
241: || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
242: {
243: /* Do the arithmetic in type TYPEX,
244: then convert result to TYPE. */
245: register tree typex = type;
246:
247: /* Can't do arithmetic in enumeral types
248: so use an integer type that will hold the values. */
249: if (TREE_CODE (typex) == ENUMERAL_TYPE)
250: typex = type_for_size (TYPE_PRECISION (typex),
251: TREE_UNSIGNED (typex));
252:
253: /* But now perhaps TYPEX is as wide as INPREC.
254: In that case, do nothing special here.
255: (Otherwise would recurse infinitely in convert. */
256: if (TYPE_PRECISION (typex) != inprec)
257: {
258: /* Don't do unsigned arithmetic where signed was wanted,
259: or vice versa.
260: Exception: if either of the original operands were
261: unsigned then can safely do the work as unsigned.
262: And we may need to do it as unsigned
263: if we truncate to the original size. */
264: typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
265: || TREE_UNSIGNED (TREE_TYPE (arg0))
266: || TREE_UNSIGNED (TREE_TYPE (arg1)))
267: ? unsigned_type (typex) : signed_type (typex));
268: return convert (type,
269: build_binary_op (ex_form,
270: convert (typex, arg0),
271: convert (typex, arg1),
272: 0));
273: }
274: }
275: }
276: break;
277:
278: case NEGATE_EXPR:
279: case BIT_NOT_EXPR:
280: {
281: register tree typex = type;
282:
283: /* Can't do arithmetic in enumeral types
284: so use an integer type that will hold the values. */
285: if (TREE_CODE (typex) == ENUMERAL_TYPE)
286: typex = type_for_size (TYPE_PRECISION (typex),
287: TREE_UNSIGNED (typex));
288:
289: /* But now perhaps TYPEX is as wide as INPREC.
290: In that case, do nothing special here.
291: (Otherwise would recurse infinitely in convert. */
292: if (TYPE_PRECISION (typex) != inprec)
293: {
294: /* Don't do unsigned arithmetic where signed was wanted,
295: or vice versa. */
296: typex = (TREE_UNSIGNED (TREE_TYPE (expr))
297: ? unsigned_type (typex) : signed_type (typex));
298: return convert (type,
299: build_unary_op (ex_form,
300: convert (typex, TREE_OPERAND (expr, 0)),
301: 1));
302: }
303: }
304:
305: case NOP_EXPR:
306: /* If truncating after truncating, might as well do all at once.
307: If truncating after extending, we may get rid of wasted work. */
308: return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
309:
310: case COND_EXPR:
311: /* Can treat the two alternative values like the operands
312: of an arithmetic expression. */
313: {
314: tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
315: tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
316:
317: if (outprec >= BITS_PER_WORD
318: || TRULY_NOOP_TRUNCATION (outprec, inprec)
319: || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
320: || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
321: {
322: /* Do the arithmetic in type TYPEX,
323: then convert result to TYPE. */
324: register tree typex = type;
325:
326: /* Can't do arithmetic in enumeral types
327: so use an integer type that will hold the values. */
328: if (TREE_CODE (typex) == ENUMERAL_TYPE)
329: typex = type_for_size (TYPE_PRECISION (typex),
330: TREE_UNSIGNED (typex));
331:
332: /* But now perhaps TYPEX is as wide as INPREC.
333: In that case, do nothing special here.
334: (Otherwise would recurse infinitely in convert. */
335: if (TYPE_PRECISION (typex) != inprec)
336: {
337: /* Don't do unsigned arithmetic where signed was wanted,
338: or vice versa. */
339: typex = (TREE_UNSIGNED (TREE_TYPE (expr))
340: ? unsigned_type (typex) : signed_type (typex));
341: return convert (type,
342: fold (build (COND_EXPR, typex,
343: TREE_OPERAND (expr, 0),
344: convert (typex, arg1),
345: convert (typex, arg2))));
346: }
1.1.1.3 ! root 347: else
! 348: /* It is sometimes worthwhile
! 349: to push the narrowing down through the conditional. */
! 350: return fold (build (COND_EXPR, type,
! 351: TREE_OPERAND (expr, 0),
! 352: convert (type, TREE_OPERAND (expr, 1)),
! 353: convert (type, TREE_OPERAND (expr, 2))));
1.1 root 354: }
355: }
356: }
357:
358: return build1 (NOP_EXPR, type, expr);
359: }
360:
361: if (form == REAL_TYPE)
362: return build1 (FIX_TRUNC_EXPR, type, expr);
363:
364: error ("aggregate value used where an integer was expected");
365:
366: {
367: register tree tem = build_int_2 (0, 0);
368: TREE_TYPE (tem) = type;
369: return tem;
370: }
371: }
372:
373: /* Create an expression whose value is that of EXPR,
374: converted to type TYPE. The TREE_TYPE of the value
375: is always TYPE. This function implements all reasonable
376: conversions; callers should filter out those that are
377: not permitted by the language being compiled. */
378:
379: tree
380: convert (type, expr)
381: tree type, expr;
382: {
383: register tree e = expr;
384: register enum tree_code code = TREE_CODE (type);
385:
1.1.1.3 ! root 386: if (type == TREE_TYPE (expr)
! 387: || TREE_CODE (expr) == ERROR_MARK)
1.1 root 388: return expr;
1.1.1.3 ! root 389: if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
! 390: return fold (build1 (NOP_EXPR, type, expr));
1.1 root 391: if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
392: return error_mark_node;
393: if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
394: {
395: error ("void value not ignored as it ought to be");
396: return error_mark_node;
397: }
398: if (code == VOID_TYPE)
399: return build1 (CONVERT_EXPR, type, e);
400: #if 0
401: /* This is incorrect. A truncation can't be stripped this way.
402: Extensions will be stripped by the use of get_unwidened. */
403: if (TREE_CODE (expr) == NOP_EXPR)
404: return convert (type, TREE_OPERAND (expr, 0));
405: #endif
406: if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
407: return fold (convert_to_integer (type, e));
408: if (code == POINTER_TYPE)
409: return fold (convert_to_pointer (type, e));
410: if (code == REAL_TYPE)
411: return fold (convert_to_real (type, e));
412:
413: error ("conversion to non-scalar type requested");
414: return error_mark_node;
415: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.