]> gcc.gnu.org Git - gcc.git/blame - gcc/c-common.c
(fold): Handle complex constants as "winning".
[gcc.git] / gcc / c-common.c
CommitLineData
b30f223b
RS
1/* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config.h"
21#include "tree.h"
22#include "c-lex.h"
23#include "c-tree.h"
24#include "flags.h"
25#include <stdio.h>
26
0b73773c
NH
27extern struct obstack permanent_obstack;
28
b032c74c 29/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2
RS
30
31void
32declare_function_name ()
33{
97d17ac2 34 tree decl, type, init;
7da551a2 35 char *name, *printable_name;
97d17ac2 36 int len;
7da551a2
RS
37
38 if (current_function_decl == NULL)
39 {
40 name = "";
41 printable_name = "top level";
42 }
43 else
44 {
45 char *kind = "function";
46 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
47 kind = "method";
6152f876
RS
48 /* Allow functions to be nameless (such as artificial ones). */
49 if (DECL_NAME (current_function_decl))
50 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
51 else
52 name = "";
7da551a2
RS
53 printable_name = (*decl_printable_name) (current_function_decl, &kind);
54 }
55
97d17ac2
RK
56 /* If the default size of char arrays isn't big enough for the name,
57 make a bigger one. */
58 len = strlen (name) + 1;
59 type = char_array_type_node;
60 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (char_array_type_node)))
61 < len)
62 type = build_array_type (char_type_node,
63 build_index_type (build_int_2 (len, 0)));
64
7da551a2 65 push_obstacks_nochange ();
97d17ac2 66 decl = build_decl (VAR_DECL, get_identifier ("__FUNCTION__"), type);
7da551a2
RS
67 TREE_STATIC (decl) = 1;
68 TREE_READONLY (decl) = 1;
b9a24ad4 69 DECL_SOURCE_LINE (decl) = 0;
176e81eb 70 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 71 DECL_IGNORED_P (decl) = 1;
97d17ac2
RK
72 init = build_string (len, name);
73 TREE_TYPE (init) = type;
7da551a2
RS
74 DECL_INITIAL (decl) = init;
75 finish_decl (pushdecl (decl), init, NULL_TREE);
76
97d17ac2
RK
77 len = strlen (printable_name) + 1;
78 type = char_array_type_node;
79 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (char_array_type_node)))
80 < len)
81 type = build_array_type (char_type_node,
82 build_index_type (build_int_2 (len, 0)));
83
7da551a2 84 push_obstacks_nochange ();
97d17ac2 85 decl = build_decl (VAR_DECL, get_identifier ("__PRETTY_FUNCTION__"), type);
7da551a2
RS
86 TREE_STATIC (decl) = 1;
87 TREE_READONLY (decl) = 1;
b9a24ad4 88 DECL_SOURCE_LINE (decl) = 0;
176e81eb 89 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 90 DECL_IGNORED_P (decl) = 1;
97d17ac2
RK
91 init = build_string (len, printable_name);
92 TREE_TYPE (init) = type;
7da551a2
RS
93 DECL_INITIAL (decl) = init;
94 finish_decl (pushdecl (decl), init, NULL_TREE);
95}
96
b30f223b
RS
97/* Given a chain of STRING_CST nodes,
98 concatenate them into one STRING_CST
99 and give it a suitable array-of-chars data type. */
100
101tree
102combine_strings (strings)
103 tree strings;
104{
105 register tree value, t;
106 register int length = 1;
107 int wide_length = 0;
108 int wide_flag = 0;
109 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
110 int nchars;
111
112 if (TREE_CHAIN (strings))
113 {
114 /* More than one in the chain, so concatenate. */
115 register char *p, *q;
116
117 /* Don't include the \0 at the end of each substring,
118 except for the last one.
119 Count wide strings and ordinary strings separately. */
120 for (t = strings; t; t = TREE_CHAIN (t))
121 {
122 if (TREE_TYPE (t) == wchar_array_type_node)
123 {
124 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
125 wide_flag = 1;
126 }
127 else
128 length += (TREE_STRING_LENGTH (t) - 1);
129 }
130
131 /* If anything is wide, the non-wides will be converted,
132 which makes them take more space. */
133 if (wide_flag)
134 length = length * wchar_bytes + wide_length;
135
136 p = savealloc (length);
137
138 /* Copy the individual strings into the new combined string.
139 If the combined string is wide, convert the chars to ints
140 for any individual strings that are not wide. */
141
142 q = p;
143 for (t = strings; t; t = TREE_CHAIN (t))
144 {
145 int len = (TREE_STRING_LENGTH (t)
146 - ((TREE_TYPE (t) == wchar_array_type_node)
147 ? wchar_bytes : 1));
148 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
149 {
150 bcopy (TREE_STRING_POINTER (t), q, len);
151 q += len;
152 }
153 else
154 {
155 int i;
156 for (i = 0; i < len; i++)
157 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
158 q += len * wchar_bytes;
159 }
160 }
161 if (wide_flag)
162 {
163 int i;
164 for (i = 0; i < wchar_bytes; i++)
165 *q++ = 0;
166 }
167 else
168 *q = 0;
169
170 value = make_node (STRING_CST);
171 TREE_STRING_POINTER (value) = p;
172 TREE_STRING_LENGTH (value) = length;
173 TREE_CONSTANT (value) = 1;
174 }
175 else
176 {
177 value = strings;
178 length = TREE_STRING_LENGTH (value);
179 if (TREE_TYPE (value) == wchar_array_type_node)
180 wide_flag = 1;
181 }
182
183 /* Compute the number of elements, for the array type. */
184 nchars = wide_flag ? length / wchar_bytes : length;
185
186 /* Create the array type for the string constant.
187 -Wwrite-strings says make the string constant an array of const char
188 so that copying it to a non-const pointer will get a warning. */
189 if (warn_write_strings
190 && (! flag_traditional && ! flag_writable_strings))
191 {
192 tree elements
193 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
194 1, 0);
195 TREE_TYPE (value)
196 = build_array_type (elements,
197 build_index_type (build_int_2 (nchars - 1, 0)));
198 }
199 else
200 TREE_TYPE (value)
201 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
202 build_index_type (build_int_2 (nchars - 1, 0)));
203 TREE_CONSTANT (value) = 1;
204 TREE_STATIC (value) = 1;
205 return value;
206}
207\f
208/* Process the attributes listed in ATTRIBUTES
209 and install them in DECL. */
210
211void
212decl_attributes (decl, attributes)
213 tree decl, attributes;
214{
215 tree a;
216 for (a = attributes; a; a = TREE_CHAIN (a))
2525c782
RS
217 if (TREE_VALUE (a) == get_identifier ("packed"))
218 {
219 if (TREE_CODE (decl) == FIELD_DECL)
220 DECL_PACKED (decl) = 1;
9b4e97b0 221 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
1394aabd 222 used for DECL_REGISTER. It wouldn't mean anything anyway. */
2525c782
RS
223 }
224 else if (TREE_VALUE (a) != 0
f3ee8ad1
RS
225 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
226 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
2525c782
RS
227 {
228 int i;
229 char *specified_name
230 = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
231
232 /* Give this decl a type with the specified mode. */
233 for (i = 0; i < NUM_MACHINE_MODES; i++)
234 if (!strcmp (specified_name, GET_MODE_NAME (i)))
235 {
236 tree type
9a631e8e 237 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
2525c782
RS
238 if (type != 0)
239 {
240 TREE_TYPE (decl) = type;
241 DECL_SIZE (decl) = 0;
ef57b1e9 242 layout_decl (decl, 0);
2525c782
RS
243 }
244 else
245 error ("no data type for mode `%s'", specified_name);
9a631e8e 246 break;
2525c782
RS
247 }
248 if (i == NUM_MACHINE_MODES)
249 error ("unknown machine mode `%s'", specified_name);
250 }
251 else if (TREE_VALUE (a) != 0
252 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
253 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
b30f223b
RS
254 {
255 int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
256 * BITS_PER_UNIT;
257
258 if (exact_log2 (align) == -1)
9a631e8e
RS
259 error_with_decl (decl,
260 "requested alignment of `%s' is not a power of 2");
b30f223b
RS
261 else if (TREE_CODE (decl) != VAR_DECL
262 && TREE_CODE (decl) != FIELD_DECL)
9a631e8e 263 error_with_decl (decl,
1e307bd8 264 "alignment specified for `%s'");
b30f223b
RS
265 else
266 DECL_ALIGN (decl) = align;
267 }
268 else if (TREE_VALUE (a) != 0
269 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
2525c782 270 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
b30f223b
RS
271 {
272 tree list = TREE_VALUE (TREE_VALUE (a));
273 tree format_type = TREE_PURPOSE (list);
274 int format_num = TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list)));
275 int first_arg_num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
276 int is_scan;
277
278 if (TREE_CODE (decl) != FUNCTION_DECL)
279 {
9a631e8e
RS
280 error_with_decl (decl,
281 "argument format specified for non-function `%s'");
b30f223b
RS
282 return;
283 }
284
285 if (format_type == get_identifier ("printf"))
286 is_scan = 0;
287 else if (format_type == get_identifier ("scanf"))
288 is_scan = 1;
289 else
290 {
9a631e8e 291 error_with_decl (decl, "unrecognized format specifier for `%s'");
b30f223b
RS
292 return;
293 }
294
295 if (first_arg_num != 0 && first_arg_num <= format_num)
296 {
9a631e8e 297 error_with_decl (decl,
b30f223b
RS
298 "format string arg follows the args to be formatted, for `%s'");
299 return;
300 }
301
302 record_format_info (DECL_NAME (decl), is_scan, format_num,
303 first_arg_num);
304 }
305}
306\f
d74154d5
RS
307/* Print a warning if a constant expression had overflow in folding.
308 Invoke this function on every expression that the language
309 requires to be a constant expression.
310 Note the ANSI C standard says it is erroneous for a
311 constant expression to overflow. */
96571883
BK
312
313void
314constant_expression_warning (value)
315 tree value;
316{
317 if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
d74154d5
RS
318 {
319 pedwarn ("overflow in constant expression");
320 /* Suppress duplicate warnings. */
321 TREE_CONSTANT_OVERFLOW (value) = 0;
322 }
323}
324
325/* Print a warning if an expression had overflow in folding.
326 Invoke this function on every expression that
327 (1) appears in the source code, and
328 (2) might be a constant expression that overflowed, and
329 (3) is not already checked by convert_and_check;
330 however, do not invoke this function on operands of explicit casts. */
331
332void
333overflow_warning (value)
334 tree value;
335{
336 if (TREE_CODE (value) == INTEGER_CST && TREE_CONSTANT_OVERFLOW (value))
337 {
338 pedwarn ("integer overflow in expression");
339 TREE_CONSTANT_OVERFLOW (value) = 0;
340 }
341}
342
343/* Print a warning if a large constant is truncated to unsigned,
344 or if -Wconversion is used and a constant < 0 is converted to unsigned.
345 Invoke this function on every expression that might be implicitly
346 converted to an unsigned type. */
347
348void
349unsigned_conversion_warning (result, operand)
350 tree result, operand;
351{
352 if (TREE_CODE (operand) == INTEGER_CST
353 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
354 && TREE_UNSIGNED (TREE_TYPE (result))
355 && !int_fits_type_p (operand, TREE_TYPE (result)))
356 {
357 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
358 /* This detects cases like converting -129 or 256 to unsigned char. */
359 pedwarn ("large integer implicitly truncated to unsigned type");
360 else if (warn_conversion)
361 pedwarn ("negative integer implicitly converted to unsigned type");
362 }
363}
364
365/* Convert EXPR to TYPE, warning about conversion problems with constants.
366 Invoke this function on every expression that is converted implicitly,
367 i.e. because of language rules and not because of an explicit cast. */
368
369tree
370convert_and_check (type, expr)
371 tree type, expr;
372{
373 tree t = convert (type, expr);
374 if (TREE_CODE (t) == INTEGER_CST)
375 {
376 if (TREE_CONSTANT_OVERFLOW (t))
377 {
378 pedwarn ("overflow in implicit constant conversion");
379 TREE_CONSTANT_OVERFLOW (t) = 0;
380 }
381 else
382 unsigned_conversion_warning (t, expr);
383 }
384 return t;
96571883
BK
385}
386\f
b30f223b
RS
387void
388c_expand_expr_stmt (expr)
389 tree expr;
390{
391 /* Do default conversion if safe and possibly important,
392 in case within ({...}). */
393 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
394 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
395 expr = default_conversion (expr);
396
397 if (TREE_TYPE (expr) != error_mark_node
398 && TYPE_SIZE (TREE_TYPE (expr)) == 0
399 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
400 error ("expression statement has incomplete type");
401
402 expand_expr_stmt (expr);
403}
404\f
405/* Validate the expression after `case' and apply default promotions. */
406
407tree
408check_case_value (value)
409 tree value;
410{
411 if (value == NULL_TREE)
412 return value;
413
414 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 415 STRIP_TYPE_NOPS (value);
b30f223b
RS
416
417 if (TREE_CODE (value) != INTEGER_CST
418 && value != error_mark_node)
419 {
420 error ("case label does not reduce to an integer constant");
421 value = error_mark_node;
422 }
423 else
424 /* Promote char or short to int. */
425 value = default_conversion (value);
426
bc690db1
RS
427 constant_expression_warning (value);
428
b30f223b
RS
429 return value;
430}
431\f
432/* Return an integer type with BITS bits of precision,
433 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
434
435tree
436type_for_size (bits, unsignedp)
437 unsigned bits;
438 int unsignedp;
439{
3fc7e390 440 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
441 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
442
3fc7e390 443 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
444 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
445
3fc7e390 446 if (bits == TYPE_PRECISION (integer_type_node))
b30f223b
RS
447 return unsignedp ? unsigned_type_node : integer_type_node;
448
3fc7e390 449 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
450 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
451
3fc7e390 452 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
453 return (unsignedp ? long_long_unsigned_type_node
454 : long_long_integer_type_node);
455
3fc7e390
RS
456 if (bits <= TYPE_PRECISION (intQI_type_node))
457 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
458
459 if (bits <= TYPE_PRECISION (intHI_type_node))
460 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
461
462 if (bits <= TYPE_PRECISION (intSI_type_node))
463 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
464
465 if (bits <= TYPE_PRECISION (intDI_type_node))
466 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
467
b30f223b
RS
468 return 0;
469}
470
471/* Return a data type that has machine mode MODE.
472 If the mode is an integer,
473 then UNSIGNEDP selects between signed and unsigned types. */
474
475tree
476type_for_mode (mode, unsignedp)
477 enum machine_mode mode;
478 int unsignedp;
479{
480 if (mode == TYPE_MODE (signed_char_type_node))
481 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
482
483 if (mode == TYPE_MODE (short_integer_type_node))
484 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
485
486 if (mode == TYPE_MODE (integer_type_node))
487 return unsignedp ? unsigned_type_node : integer_type_node;
488
489 if (mode == TYPE_MODE (long_integer_type_node))
490 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
491
492 if (mode == TYPE_MODE (long_long_integer_type_node))
493 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
494
3fc7e390
RS
495 if (mode == TYPE_MODE (intQI_type_node))
496 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
497
498 if (mode == TYPE_MODE (intHI_type_node))
499 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
500
501 if (mode == TYPE_MODE (intSI_type_node))
502 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
503
504 if (mode == TYPE_MODE (intDI_type_node))
505 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
506
b30f223b
RS
507 if (mode == TYPE_MODE (float_type_node))
508 return float_type_node;
509
510 if (mode == TYPE_MODE (double_type_node))
511 return double_type_node;
512
513 if (mode == TYPE_MODE (long_double_type_node))
514 return long_double_type_node;
515
516 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
517 return build_pointer_type (char_type_node);
518
519 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
520 return build_pointer_type (integer_type_node);
521
522 return 0;
523}
524\f
525/* Print an error message for invalid operands to arith operation CODE.
526 NOP_EXPR is used as a special case (see truthvalue_conversion). */
527
528void
529binary_op_error (code)
530 enum tree_code code;
531{
532 register char *opname;
533 switch (code)
534 {
535 case NOP_EXPR:
536 error ("invalid truth-value expression");
537 return;
538
539 case PLUS_EXPR:
540 opname = "+"; break;
541 case MINUS_EXPR:
542 opname = "-"; break;
543 case MULT_EXPR:
544 opname = "*"; break;
545 case MAX_EXPR:
546 opname = "max"; break;
547 case MIN_EXPR:
548 opname = "min"; break;
549 case EQ_EXPR:
550 opname = "=="; break;
551 case NE_EXPR:
552 opname = "!="; break;
553 case LE_EXPR:
554 opname = "<="; break;
555 case GE_EXPR:
556 opname = ">="; break;
557 case LT_EXPR:
558 opname = "<"; break;
559 case GT_EXPR:
560 opname = ">"; break;
561 case LSHIFT_EXPR:
562 opname = "<<"; break;
563 case RSHIFT_EXPR:
564 opname = ">>"; break;
565 case TRUNC_MOD_EXPR:
047de90b 566 case FLOOR_MOD_EXPR:
b30f223b
RS
567 opname = "%"; break;
568 case TRUNC_DIV_EXPR:
047de90b 569 case FLOOR_DIV_EXPR:
b30f223b
RS
570 opname = "/"; break;
571 case BIT_AND_EXPR:
572 opname = "&"; break;
573 case BIT_IOR_EXPR:
574 opname = "|"; break;
575 case TRUTH_ANDIF_EXPR:
576 opname = "&&"; break;
577 case TRUTH_ORIF_EXPR:
578 opname = "||"; break;
579 case BIT_XOR_EXPR:
580 opname = "^"; break;
047de90b
RS
581 case LROTATE_EXPR:
582 case RROTATE_EXPR:
583 opname = "rotate"; break;
b30f223b
RS
584 }
585 error ("invalid operands to binary %s", opname);
586}
587\f
588/* Subroutine of build_binary_op, used for comparison operations.
589 See if the operands have both been converted from subword integer types
590 and, if so, perhaps change them both back to their original type.
591
592 The arguments of this function are all pointers to local variables
593 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
594 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
595
596 If this function returns nonzero, it means that the comparison has
597 a constant value. What this function returns is an expression for
598 that value. */
599
600tree
601shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
602 tree *op0_ptr, *op1_ptr;
603 tree *restype_ptr;
604 enum tree_code *rescode_ptr;
605{
606 register tree type;
607 tree op0 = *op0_ptr;
608 tree op1 = *op1_ptr;
609 int unsignedp0, unsignedp1;
610 int real1, real2;
611 tree primop0, primop1;
612 enum tree_code code = *rescode_ptr;
613
614 /* Throw away any conversions to wider types
615 already present in the operands. */
616
617 primop0 = get_narrower (op0, &unsignedp0);
618 primop1 = get_narrower (op1, &unsignedp1);
619
620 /* Handle the case that OP0 does not *contain* a conversion
621 but it *requires* conversion to FINAL_TYPE. */
622
623 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
624 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
625 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
626 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
627
628 /* If one of the operands must be floated, we cannot optimize. */
629 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
630 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
631
632 /* If first arg is constant, swap the args (changing operation
633 so value is preserved), for canonicalization. */
634
635 if (TREE_CONSTANT (primop0))
636 {
637 register tree tem = primop0;
638 register int temi = unsignedp0;
639 primop0 = primop1;
640 primop1 = tem;
641 tem = op0;
642 op0 = op1;
643 op1 = tem;
644 *op0_ptr = op0;
645 *op1_ptr = op1;
646 unsignedp0 = unsignedp1;
647 unsignedp1 = temi;
648 temi = real1;
649 real1 = real2;
650 real2 = temi;
651
652 switch (code)
653 {
654 case LT_EXPR:
655 code = GT_EXPR;
656 break;
657 case GT_EXPR:
658 code = LT_EXPR;
659 break;
660 case LE_EXPR:
661 code = GE_EXPR;
662 break;
663 case GE_EXPR:
664 code = LE_EXPR;
665 break;
666 }
667 *rescode_ptr = code;
668 }
669
670 /* If comparing an integer against a constant more bits wide,
671 maybe we can deduce a value of 1 or 0 independent of the data.
672 Or else truncate the constant now
673 rather than extend the variable at run time.
674
675 This is only interesting if the constant is the wider arg.
676 Also, it is not safe if the constant is unsigned and the
677 variable arg is signed, since in this case the variable
678 would be sign-extended and then regarded as unsigned.
679 Our technique fails in this case because the lowest/highest
680 possible unsigned results don't follow naturally from the
681 lowest/highest possible values of the variable operand.
682 For just EQ_EXPR and NE_EXPR there is another technique that
683 could be used: see if the constant can be faithfully represented
684 in the other operand's type, by truncating it and reextending it
685 and see if that preserves the constant's value. */
686
687 if (!real1 && !real2
688 && TREE_CODE (primop1) == INTEGER_CST
689 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
690 {
691 int min_gt, max_gt, min_lt, max_lt;
692 tree maxval, minval;
693 /* 1 if comparison is nominally unsigned. */
694 int unsignedp = TREE_UNSIGNED (*restype_ptr);
695 tree val;
696
697 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
698
699 maxval = TYPE_MAX_VALUE (type);
700 minval = TYPE_MIN_VALUE (type);
701
702 if (unsignedp && !unsignedp0)
703 *restype_ptr = signed_type (*restype_ptr);
704
705 if (TREE_TYPE (primop1) != *restype_ptr)
706 primop1 = convert (*restype_ptr, primop1);
707 if (type != *restype_ptr)
708 {
709 minval = convert (*restype_ptr, minval);
710 maxval = convert (*restype_ptr, maxval);
711 }
712
713 if (unsignedp && unsignedp0)
714 {
715 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
716 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
717 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
718 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
719 }
720 else
721 {
722 min_gt = INT_CST_LT (primop1, minval);
723 max_gt = INT_CST_LT (primop1, maxval);
724 min_lt = INT_CST_LT (minval, primop1);
725 max_lt = INT_CST_LT (maxval, primop1);
726 }
727
728 val = 0;
729 /* This used to be a switch, but Genix compiler can't handle that. */
730 if (code == NE_EXPR)
731 {
732 if (max_lt || min_gt)
733 val = integer_one_node;
734 }
735 else if (code == EQ_EXPR)
736 {
737 if (max_lt || min_gt)
738 val = integer_zero_node;
739 }
740 else if (code == LT_EXPR)
741 {
742 if (max_lt)
743 val = integer_one_node;
744 if (!min_lt)
745 val = integer_zero_node;
746 }
747 else if (code == GT_EXPR)
748 {
749 if (min_gt)
750 val = integer_one_node;
751 if (!max_gt)
752 val = integer_zero_node;
753 }
754 else if (code == LE_EXPR)
755 {
756 if (!max_gt)
757 val = integer_one_node;
758 if (min_gt)
759 val = integer_zero_node;
760 }
761 else if (code == GE_EXPR)
762 {
763 if (!min_lt)
764 val = integer_one_node;
765 if (max_lt)
766 val = integer_zero_node;
767 }
768
769 /* If primop0 was sign-extended and unsigned comparison specd,
770 we did a signed comparison above using the signed type bounds.
771 But the comparison we output must be unsigned.
772
773 Also, for inequalities, VAL is no good; but if the signed
774 comparison had *any* fixed result, it follows that the
775 unsigned comparison just tests the sign in reverse
776 (positive values are LE, negative ones GE).
777 So we can generate an unsigned comparison
778 against an extreme value of the signed type. */
779
780 if (unsignedp && !unsignedp0)
781 {
782 if (val != 0)
783 switch (code)
784 {
785 case LT_EXPR:
786 case GE_EXPR:
787 primop1 = TYPE_MIN_VALUE (type);
788 val = 0;
789 break;
790
791 case LE_EXPR:
792 case GT_EXPR:
793 primop1 = TYPE_MAX_VALUE (type);
794 val = 0;
795 break;
796 }
797 type = unsigned_type (type);
798 }
799
1e276c4a 800 if (!max_gt && !unsignedp0)
b30f223b
RS
801 {
802 /* This is the case of (char)x >?< 0x80, which people used to use
803 expecting old C compilers to change the 0x80 into -0x80. */
804 if (val == integer_zero_node)
805 warning ("comparison is always 0 due to limited range of data type");
806 if (val == integer_one_node)
807 warning ("comparison is always 1 due to limited range of data type");
808 }
809
1e276c4a 810 if (!min_lt && unsignedp0)
b30f223b 811 {
1e276c4a 812 /* This is the case of (unsigned char)x >?< -1 or < 0. */
b30f223b
RS
813 if (val == integer_zero_node)
814 warning ("comparison is always 0 due to limited range of data type");
815 if (val == integer_one_node)
816 warning ("comparison is always 1 due to limited range of data type");
817 }
818
819 if (val != 0)
820 {
821 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
822 if (TREE_SIDE_EFFECTS (primop0))
823 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
824 return val;
825 }
826
827 /* Value is not predetermined, but do the comparison
828 in the type of the operand that is not constant.
829 TYPE is already properly set. */
830 }
831 else if (real1 && real2
832 && TYPE_PRECISION (TREE_TYPE (primop0)) == TYPE_PRECISION (TREE_TYPE (primop1)))
833 type = TREE_TYPE (primop0);
834
835 /* If args' natural types are both narrower than nominal type
836 and both extend in the same manner, compare them
837 in the type of the wider arg.
838 Otherwise must actually extend both to the nominal
839 common type lest different ways of extending
840 alter the result.
841 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
842
843 else if (unsignedp0 == unsignedp1 && real1 == real2
844 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
845 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
846 {
847 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
848 type = signed_or_unsigned_type (unsignedp0
849 || TREE_UNSIGNED (*restype_ptr),
850 type);
851 /* Make sure shorter operand is extended the right way
852 to match the longer operand. */
853 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
854 primop0);
855 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
856 primop1);
857 }
858 else
859 {
860 /* Here we must do the comparison on the nominal type
861 using the args exactly as we received them. */
862 type = *restype_ptr;
863 primop0 = op0;
864 primop1 = op1;
865
866 if (!real1 && !real2 && integer_zerop (primop1)
867 && TREE_UNSIGNED (TREE_TYPE (primop0)))
868 {
869 tree value = 0;
870 switch (code)
871 {
872 case GE_EXPR:
873 if (extra_warnings)
874 warning ("unsigned value >= 0 is always 1");
875 value = integer_one_node;
876 break;
877
878 case LT_EXPR:
879 if (extra_warnings)
880 warning ("unsigned value < 0 is always 0");
881 value = integer_zero_node;
882 }
883
884 if (value != 0)
885 {
886 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
887 if (TREE_SIDE_EFFECTS (primop0))
888 return build (COMPOUND_EXPR, TREE_TYPE (value),
889 primop0, value);
890 return value;
891 }
892 }
893 }
894
895 *op0_ptr = convert (type, primop0);
896 *op1_ptr = convert (type, primop1);
897
898 *restype_ptr = integer_type_node;
899
900 return 0;
901}
902\f
903/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
904 or validate its data type for an `if' or `while' statement or ?..: exp.
905
906 This preparation consists of taking the ordinary
907 representation of an expression expr and producing a valid tree
908 boolean expression describing whether expr is nonzero. We could
909 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
910 but we optimize comparisons, &&, ||, and !.
911
912 The resulting type should always be `integer_type_node'. */
913
914tree
915truthvalue_conversion (expr)
916 tree expr;
917{
918 register enum tree_code code;
919
920 switch (TREE_CODE (expr))
921 {
922 /* It is simpler and generates better code to have only TRUTH_*_EXPR
923 or comparison expressions as truth values at this level. */
924#if 0
925 case COMPONENT_REF:
926 /* A one-bit unsigned bit-field is already acceptable. */
927 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
928 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
929 return expr;
930 break;
931#endif
932
933 case EQ_EXPR:
934 /* It is simpler and generates better code to have only TRUTH_*_EXPR
935 or comparison expressions as truth values at this level. */
936#if 0
937 if (integer_zerop (TREE_OPERAND (expr, 1)))
938 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
939#endif
940 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
941 case TRUTH_ANDIF_EXPR:
942 case TRUTH_ORIF_EXPR:
943 case TRUTH_AND_EXPR:
944 case TRUTH_OR_EXPR:
9379fac9 945 case TRUTH_XOR_EXPR:
b30f223b
RS
946 case ERROR_MARK:
947 return expr;
948
949 case INTEGER_CST:
950 return integer_zerop (expr) ? integer_zero_node : integer_one_node;
951
952 case REAL_CST:
953 return real_zerop (expr) ? integer_zero_node : integer_one_node;
954
955 case ADDR_EXPR:
956 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
957 return build (COMPOUND_EXPR, integer_type_node,
958 TREE_OPERAND (expr, 0), integer_one_node);
959 else
960 return integer_one_node;
961
962 case NEGATE_EXPR:
963 case ABS_EXPR:
964 case FLOAT_EXPR:
965 case FFS_EXPR:
966 /* These don't change whether an object is non-zero or zero. */
967 return truthvalue_conversion (TREE_OPERAND (expr, 0));
968
969 case LROTATE_EXPR:
970 case RROTATE_EXPR:
971 /* These don't change whether an object is zero or non-zero, but
972 we can't ignore them if their second arg has side-effects. */
973 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
974 return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
975 truthvalue_conversion (TREE_OPERAND (expr, 0)));
976 else
977 return truthvalue_conversion (TREE_OPERAND (expr, 0));
978
979 case COND_EXPR:
980 /* Distribute the conversion into the arms of a COND_EXPR. */
981 return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
982 truthvalue_conversion (TREE_OPERAND (expr, 1)),
983 truthvalue_conversion (TREE_OPERAND (expr, 2))));
984
985 case CONVERT_EXPR:
986 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
987 since that affects how `default_conversion' will behave. */
988 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
989 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
990 break;
991 /* fall through... */
992 case NOP_EXPR:
993 /* If this is widening the argument, we can ignore it. */
994 if (TYPE_PRECISION (TREE_TYPE (expr))
995 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
996 return truthvalue_conversion (TREE_OPERAND (expr, 0));
997 break;
998
999 case BIT_XOR_EXPR:
1000 case MINUS_EXPR:
1001 /* These can be changed into a comparison of the two objects. */
1002 if (TREE_TYPE (TREE_OPERAND (expr, 0))
1003 == TREE_TYPE (TREE_OPERAND (expr, 1)))
1004 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1005 TREE_OPERAND (expr, 1), 1);
1006 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1007 fold (build1 (NOP_EXPR,
1008 TREE_TYPE (TREE_OPERAND (expr, 0)),
1009 TREE_OPERAND (expr, 1))), 1);
e2aab13d
RS
1010
1011 case MODIFY_EXPR:
1012 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1013 warning ("suggest parentheses around assignment used as truth value");
1014 break;
b30f223b
RS
1015 }
1016
1017 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1018}
1019\f
1020/* Read the rest of a #-directive from input stream FINPUT.
1021 In normal use, the directive name and the white space after it
1022 have already been read, so they won't be included in the result.
1023 We allow for the fact that the directive line may contain
1024 a newline embedded within a character or string literal which forms
1025 a part of the directive.
1026
1027 The value is a string in a reusable buffer. It remains valid
1028 only until the next time this function is called. */
1029
1030char *
1031get_directive_line (finput)
1032 register FILE *finput;
1033{
1034 static char *directive_buffer = NULL;
1035 static unsigned buffer_length = 0;
1036 register char *p;
1037 register char *buffer_limit;
1038 register int looking_for = 0;
1039 register int char_escaped = 0;
1040
1041 if (buffer_length == 0)
1042 {
1043 directive_buffer = (char *)xmalloc (128);
1044 buffer_length = 128;
1045 }
1046
1047 buffer_limit = &directive_buffer[buffer_length];
1048
1049 for (p = directive_buffer; ; )
1050 {
1051 int c;
1052
1053 /* Make buffer bigger if it is full. */
1054 if (p >= buffer_limit)
1055 {
1056 register unsigned bytes_used = (p - directive_buffer);
1057
1058 buffer_length *= 2;
1059 directive_buffer
1060 = (char *)xrealloc (directive_buffer, buffer_length);
1061 p = &directive_buffer[bytes_used];
1062 buffer_limit = &directive_buffer[buffer_length];
1063 }
1064
1065 c = getc (finput);
1066
1067 /* Discard initial whitespace. */
1068 if ((c == ' ' || c == '\t') && p == directive_buffer)
1069 continue;
1070
1071 /* Detect the end of the directive. */
1072 if (c == '\n' && looking_for == 0)
1073 {
1074 ungetc (c, finput);
1075 c = '\0';
1076 }
1077
1078 *p++ = c;
1079
1080 if (c == 0)
1081 return directive_buffer;
1082
1083 /* Handle string and character constant syntax. */
1084 if (looking_for)
1085 {
1086 if (looking_for == c && !char_escaped)
1087 looking_for = 0; /* Found terminator... stop looking. */
1088 }
1089 else
1090 if (c == '\'' || c == '"')
1091 looking_for = c; /* Don't stop buffering until we see another
1092 another one of these (or an EOF). */
1093
1094 /* Handle backslash. */
1095 char_escaped = (c == '\\' && ! char_escaped);
1096 }
1097}
0b73773c
NH
1098\f
1099/* Make a variant type in the proper way for C/C++, propagating qualifiers
1100 down to the element type of an array. */
1101
1102tree
1103c_build_type_variant (type, constp, volatilep)
1104 tree type;
1105 int constp, volatilep;
1106{
1107 if (TREE_CODE (type) == ARRAY_TYPE)
1108 {
1109 tree real_main_variant = TYPE_MAIN_VARIANT (type);
1110 int permanent = TREE_PERMANENT (type);
1111
1112 if (permanent)
1113 push_obstacks (&permanent_obstack, &permanent_obstack);
1114 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
1115 constp, volatilep),
1116 TYPE_DOMAIN (type));
1117 TYPE_MAIN_VARIANT (type) = real_main_variant;
1118 if (permanent)
1119 pop_obstacks ();
1120 }
1121 return build_type_variant (type, constp, volatilep);
1122}
This page took 0.182941 seconds and 5 git commands to generate.