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