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