]> gcc.gnu.org Git - gcc.git/blob - gcc/c-common.c
*** empty log message ***
[gcc.git] / gcc / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #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
30 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
31
32 void
33 declare_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 ();
53 decl = build_decl (VAR_DECL, get_identifier ("__FUNCTION__"),
54 char_array_type_node);
55 TREE_STATIC (decl) = 1;
56 TREE_READONLY (decl) = 1;
57 TREE_NO_UNUSED_WARNING (decl) = 1;
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 ();
65 decl = build_decl (VAR_DECL, get_identifier ("__PRETTY_FUNCTION__"),
66 char_array_type_node);
67 TREE_STATIC (decl) = 1;
68 TREE_READONLY (decl) = 1;
69 TREE_NO_UNUSED_WARNING (decl) = 1;
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
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
81 tree
82 combine_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
191 void
192 decl_attributes (decl, attributes)
193 tree decl, attributes;
194 {
195 tree a;
196 for (a = attributes; a; a = TREE_CHAIN (a))
197 if (TREE_VALUE (a) == get_identifier ("packed"))
198 {
199 if (TREE_CODE (decl) == FIELD_DECL)
200 DECL_PACKED (decl) = 1;
201 }
202 else if (TREE_VALUE (a) != 0
203 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
204 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("mode"))
205 {
206 int i;
207 char *specified_name
208 = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (a)));
209
210 /* Give this decl a type with the specified mode. */
211 for (i = 0; i < NUM_MACHINE_MODES; i++)
212 if (!strcmp (specified_name, GET_MODE_NAME (i)))
213 {
214 tree type
215 = type_for_mode (i, TREE_UNSIGNED (TREE_TYPE (decl)));
216 if (type != 0)
217 {
218 TREE_TYPE (decl) = type;
219 DECL_SIZE (decl) = 0;
220 layout_decl (decl);
221 }
222 else
223 error ("no data type for mode `%s'", specified_name);
224 break;
225 }
226 if (i == NUM_MACHINE_MODES)
227 error ("unknown machine mode `%s'", specified_name);
228 }
229 else if (TREE_VALUE (a) != 0
230 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
231 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("aligned"))
232 {
233 int align = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (a)))
234 * BITS_PER_UNIT;
235
236 if (exact_log2 (align) == -1)
237 error_with_decl (decl,
238 "requested alignment of `%s' is not a power of 2");
239 else if (TREE_CODE (decl) != VAR_DECL
240 && TREE_CODE (decl) != FIELD_DECL)
241 error_with_decl (decl,
242 "alignment specified for `%s'");
243 else
244 DECL_ALIGN (decl) = align;
245 }
246 else if (TREE_VALUE (a) != 0
247 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
248 && TREE_PURPOSE (TREE_VALUE (a)) == get_identifier ("format"))
249 {
250 tree list = TREE_VALUE (TREE_VALUE (a));
251 tree format_type = TREE_PURPOSE (list);
252 int format_num = TREE_INT_CST_LOW (TREE_PURPOSE (TREE_VALUE (list)));
253 int first_arg_num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
254 int is_scan;
255
256 if (TREE_CODE (decl) != FUNCTION_DECL)
257 {
258 error_with_decl (decl,
259 "argument format specified for non-function `%s'");
260 return;
261 }
262
263 if (format_type == get_identifier ("printf"))
264 is_scan = 0;
265 else if (format_type == get_identifier ("scanf"))
266 is_scan = 1;
267 else
268 {
269 error_with_decl (decl, "unrecognized format specifier for `%s'");
270 return;
271 }
272
273 if (first_arg_num != 0 && first_arg_num <= format_num)
274 {
275 error_with_decl (decl,
276 "format string arg follows the args to be formatted, for `%s'");
277 return;
278 }
279
280 record_format_info (DECL_NAME (decl), is_scan, format_num,
281 first_arg_num);
282 }
283 }
284 \f
285 void
286 c_expand_expr_stmt (expr)
287 tree expr;
288 {
289 /* Do default conversion if safe and possibly important,
290 in case within ({...}). */
291 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
292 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
293 expr = default_conversion (expr);
294
295 if (TREE_TYPE (expr) != error_mark_node
296 && TYPE_SIZE (TREE_TYPE (expr)) == 0
297 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
298 error ("expression statement has incomplete type");
299
300 expand_expr_stmt (expr);
301 }
302 \f
303 /* Validate the expression after `case' and apply default promotions. */
304
305 tree
306 check_case_value (value)
307 tree value;
308 {
309 if (value == NULL_TREE)
310 return value;
311
312 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
313 if (TREE_CODE (value) == NON_LVALUE_EXPR)
314 value = TREE_OPERAND (value, 0);
315
316 if (TREE_CODE (value) != INTEGER_CST
317 && value != error_mark_node)
318 {
319 error ("case label does not reduce to an integer constant");
320 value = error_mark_node;
321 }
322 else
323 /* Promote char or short to int. */
324 value = default_conversion (value);
325
326 return value;
327 }
328 \f
329 /* Return an integer type with BITS bits of precision,
330 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
331
332 tree
333 type_for_size (bits, unsignedp)
334 unsigned bits;
335 int unsignedp;
336 {
337 if (bits <= TYPE_PRECISION (signed_char_type_node))
338 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
339
340 if (bits <= TYPE_PRECISION (short_integer_type_node))
341 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
342
343 if (bits <= TYPE_PRECISION (integer_type_node))
344 return unsignedp ? unsigned_type_node : integer_type_node;
345
346 if (bits <= TYPE_PRECISION (long_integer_type_node))
347 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
348
349 if (bits <= TYPE_PRECISION (long_long_integer_type_node))
350 return (unsignedp ? long_long_unsigned_type_node
351 : long_long_integer_type_node);
352
353 return 0;
354 }
355
356 /* Return a data type that has machine mode MODE.
357 If the mode is an integer,
358 then UNSIGNEDP selects between signed and unsigned types. */
359
360 tree
361 type_for_mode (mode, unsignedp)
362 enum machine_mode mode;
363 int unsignedp;
364 {
365 if (mode == TYPE_MODE (signed_char_type_node))
366 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
367
368 if (mode == TYPE_MODE (short_integer_type_node))
369 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
370
371 if (mode == TYPE_MODE (integer_type_node))
372 return unsignedp ? unsigned_type_node : integer_type_node;
373
374 if (mode == TYPE_MODE (long_integer_type_node))
375 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
376
377 if (mode == TYPE_MODE (long_long_integer_type_node))
378 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
379
380 if (mode == TYPE_MODE (float_type_node))
381 return float_type_node;
382
383 if (mode == TYPE_MODE (double_type_node))
384 return double_type_node;
385
386 if (mode == TYPE_MODE (long_double_type_node))
387 return long_double_type_node;
388
389 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
390 return build_pointer_type (char_type_node);
391
392 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
393 return build_pointer_type (integer_type_node);
394
395 return 0;
396 }
397 \f
398 /* Print an error message for invalid operands to arith operation CODE.
399 NOP_EXPR is used as a special case (see truthvalue_conversion). */
400
401 void
402 binary_op_error (code)
403 enum tree_code code;
404 {
405 register char *opname;
406 switch (code)
407 {
408 case NOP_EXPR:
409 error ("invalid truth-value expression");
410 return;
411
412 case PLUS_EXPR:
413 opname = "+"; break;
414 case MINUS_EXPR:
415 opname = "-"; break;
416 case MULT_EXPR:
417 opname = "*"; break;
418 case MAX_EXPR:
419 opname = "max"; break;
420 case MIN_EXPR:
421 opname = "min"; break;
422 case EQ_EXPR:
423 opname = "=="; break;
424 case NE_EXPR:
425 opname = "!="; break;
426 case LE_EXPR:
427 opname = "<="; break;
428 case GE_EXPR:
429 opname = ">="; break;
430 case LT_EXPR:
431 opname = "<"; break;
432 case GT_EXPR:
433 opname = ">"; break;
434 case LSHIFT_EXPR:
435 opname = "<<"; break;
436 case RSHIFT_EXPR:
437 opname = ">>"; break;
438 case TRUNC_MOD_EXPR:
439 case FLOOR_MOD_EXPR:
440 opname = "%"; break;
441 case TRUNC_DIV_EXPR:
442 case FLOOR_DIV_EXPR:
443 opname = "/"; break;
444 case BIT_AND_EXPR:
445 opname = "&"; break;
446 case BIT_IOR_EXPR:
447 opname = "|"; break;
448 case TRUTH_ANDIF_EXPR:
449 opname = "&&"; break;
450 case TRUTH_ORIF_EXPR:
451 opname = "||"; break;
452 case BIT_XOR_EXPR:
453 opname = "^"; break;
454 case LROTATE_EXPR:
455 case RROTATE_EXPR:
456 opname = "rotate"; break;
457 }
458 error ("invalid operands to binary %s", opname);
459 }
460 \f
461 /* Subroutine of build_binary_op, used for comparison operations.
462 See if the operands have both been converted from subword integer types
463 and, if so, perhaps change them both back to their original type.
464
465 The arguments of this function are all pointers to local variables
466 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
467 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
468
469 If this function returns nonzero, it means that the comparison has
470 a constant value. What this function returns is an expression for
471 that value. */
472
473 tree
474 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
475 tree *op0_ptr, *op1_ptr;
476 tree *restype_ptr;
477 enum tree_code *rescode_ptr;
478 {
479 register tree type;
480 tree op0 = *op0_ptr;
481 tree op1 = *op1_ptr;
482 int unsignedp0, unsignedp1;
483 int real1, real2;
484 tree primop0, primop1;
485 enum tree_code code = *rescode_ptr;
486
487 /* Throw away any conversions to wider types
488 already present in the operands. */
489
490 primop0 = get_narrower (op0, &unsignedp0);
491 primop1 = get_narrower (op1, &unsignedp1);
492
493 /* Handle the case that OP0 does not *contain* a conversion
494 but it *requires* conversion to FINAL_TYPE. */
495
496 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
497 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
498 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
499 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
500
501 /* If one of the operands must be floated, we cannot optimize. */
502 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
503 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
504
505 /* If first arg is constant, swap the args (changing operation
506 so value is preserved), for canonicalization. */
507
508 if (TREE_CONSTANT (primop0))
509 {
510 register tree tem = primop0;
511 register int temi = unsignedp0;
512 primop0 = primop1;
513 primop1 = tem;
514 tem = op0;
515 op0 = op1;
516 op1 = tem;
517 *op0_ptr = op0;
518 *op1_ptr = op1;
519 unsignedp0 = unsignedp1;
520 unsignedp1 = temi;
521 temi = real1;
522 real1 = real2;
523 real2 = temi;
524
525 switch (code)
526 {
527 case LT_EXPR:
528 code = GT_EXPR;
529 break;
530 case GT_EXPR:
531 code = LT_EXPR;
532 break;
533 case LE_EXPR:
534 code = GE_EXPR;
535 break;
536 case GE_EXPR:
537 code = LE_EXPR;
538 break;
539 }
540 *rescode_ptr = code;
541 }
542
543 /* If comparing an integer against a constant more bits wide,
544 maybe we can deduce a value of 1 or 0 independent of the data.
545 Or else truncate the constant now
546 rather than extend the variable at run time.
547
548 This is only interesting if the constant is the wider arg.
549 Also, it is not safe if the constant is unsigned and the
550 variable arg is signed, since in this case the variable
551 would be sign-extended and then regarded as unsigned.
552 Our technique fails in this case because the lowest/highest
553 possible unsigned results don't follow naturally from the
554 lowest/highest possible values of the variable operand.
555 For just EQ_EXPR and NE_EXPR there is another technique that
556 could be used: see if the constant can be faithfully represented
557 in the other operand's type, by truncating it and reextending it
558 and see if that preserves the constant's value. */
559
560 if (!real1 && !real2
561 && TREE_CODE (primop1) == INTEGER_CST
562 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
563 {
564 int min_gt, max_gt, min_lt, max_lt;
565 tree maxval, minval;
566 /* 1 if comparison is nominally unsigned. */
567 int unsignedp = TREE_UNSIGNED (*restype_ptr);
568 tree val;
569
570 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
571
572 maxval = TYPE_MAX_VALUE (type);
573 minval = TYPE_MIN_VALUE (type);
574
575 if (unsignedp && !unsignedp0)
576 *restype_ptr = signed_type (*restype_ptr);
577
578 if (TREE_TYPE (primop1) != *restype_ptr)
579 primop1 = convert (*restype_ptr, primop1);
580 if (type != *restype_ptr)
581 {
582 minval = convert (*restype_ptr, minval);
583 maxval = convert (*restype_ptr, maxval);
584 }
585
586 if (unsignedp && unsignedp0)
587 {
588 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
589 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
590 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
591 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
592 }
593 else
594 {
595 min_gt = INT_CST_LT (primop1, minval);
596 max_gt = INT_CST_LT (primop1, maxval);
597 min_lt = INT_CST_LT (minval, primop1);
598 max_lt = INT_CST_LT (maxval, primop1);
599 }
600
601 val = 0;
602 /* This used to be a switch, but Genix compiler can't handle that. */
603 if (code == NE_EXPR)
604 {
605 if (max_lt || min_gt)
606 val = integer_one_node;
607 }
608 else if (code == EQ_EXPR)
609 {
610 if (max_lt || min_gt)
611 val = integer_zero_node;
612 }
613 else if (code == LT_EXPR)
614 {
615 if (max_lt)
616 val = integer_one_node;
617 if (!min_lt)
618 val = integer_zero_node;
619 }
620 else if (code == GT_EXPR)
621 {
622 if (min_gt)
623 val = integer_one_node;
624 if (!max_gt)
625 val = integer_zero_node;
626 }
627 else if (code == LE_EXPR)
628 {
629 if (!max_gt)
630 val = integer_one_node;
631 if (min_gt)
632 val = integer_zero_node;
633 }
634 else if (code == GE_EXPR)
635 {
636 if (!min_lt)
637 val = integer_one_node;
638 if (max_lt)
639 val = integer_zero_node;
640 }
641
642 /* If primop0 was sign-extended and unsigned comparison specd,
643 we did a signed comparison above using the signed type bounds.
644 But the comparison we output must be unsigned.
645
646 Also, for inequalities, VAL is no good; but if the signed
647 comparison had *any* fixed result, it follows that the
648 unsigned comparison just tests the sign in reverse
649 (positive values are LE, negative ones GE).
650 So we can generate an unsigned comparison
651 against an extreme value of the signed type. */
652
653 if (unsignedp && !unsignedp0)
654 {
655 if (val != 0)
656 switch (code)
657 {
658 case LT_EXPR:
659 case GE_EXPR:
660 primop1 = TYPE_MIN_VALUE (type);
661 val = 0;
662 break;
663
664 case LE_EXPR:
665 case GT_EXPR:
666 primop1 = TYPE_MAX_VALUE (type);
667 val = 0;
668 break;
669 }
670 type = unsigned_type (type);
671 }
672
673 if (!max_gt && !unsignedp0)
674 {
675 /* This is the case of (char)x >?< 0x80, which people used to use
676 expecting old C compilers to change the 0x80 into -0x80. */
677 if (val == integer_zero_node)
678 warning ("comparison is always 0 due to limited range of data type");
679 if (val == integer_one_node)
680 warning ("comparison is always 1 due to limited range of data type");
681 }
682
683 if (!min_lt && unsignedp0)
684 {
685 /* This is the case of (unsigned char)x >?< -1 or < 0. */
686 if (val == integer_zero_node)
687 warning ("comparison is always 0 due to limited range of data type");
688 if (val == integer_one_node)
689 warning ("comparison is always 1 due to limited range of data type");
690 }
691
692 if (val != 0)
693 {
694 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
695 if (TREE_SIDE_EFFECTS (primop0))
696 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
697 return val;
698 }
699
700 /* Value is not predetermined, but do the comparison
701 in the type of the operand that is not constant.
702 TYPE is already properly set. */
703 }
704 else if (real1 && real2
705 && TYPE_PRECISION (TREE_TYPE (primop0)) == TYPE_PRECISION (TREE_TYPE (primop1)))
706 type = TREE_TYPE (primop0);
707
708 /* If args' natural types are both narrower than nominal type
709 and both extend in the same manner, compare them
710 in the type of the wider arg.
711 Otherwise must actually extend both to the nominal
712 common type lest different ways of extending
713 alter the result.
714 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
715
716 else if (unsignedp0 == unsignedp1 && real1 == real2
717 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
718 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
719 {
720 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
721 type = signed_or_unsigned_type (unsignedp0
722 || TREE_UNSIGNED (*restype_ptr),
723 type);
724 /* Make sure shorter operand is extended the right way
725 to match the longer operand. */
726 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
727 primop0);
728 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
729 primop1);
730 }
731 else
732 {
733 /* Here we must do the comparison on the nominal type
734 using the args exactly as we received them. */
735 type = *restype_ptr;
736 primop0 = op0;
737 primop1 = op1;
738
739 if (!real1 && !real2 && integer_zerop (primop1)
740 && TREE_UNSIGNED (TREE_TYPE (primop0)))
741 {
742 tree value = 0;
743 switch (code)
744 {
745 case GE_EXPR:
746 if (extra_warnings)
747 warning ("unsigned value >= 0 is always 1");
748 value = integer_one_node;
749 break;
750
751 case LT_EXPR:
752 if (extra_warnings)
753 warning ("unsigned value < 0 is always 0");
754 value = integer_zero_node;
755 }
756
757 if (value != 0)
758 {
759 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
760 if (TREE_SIDE_EFFECTS (primop0))
761 return build (COMPOUND_EXPR, TREE_TYPE (value),
762 primop0, value);
763 return value;
764 }
765 }
766 }
767
768 *op0_ptr = convert (type, primop0);
769 *op1_ptr = convert (type, primop1);
770
771 *restype_ptr = integer_type_node;
772
773 return 0;
774 }
775 \f
776 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
777 or validate its data type for an `if' or `while' statement or ?..: exp.
778
779 This preparation consists of taking the ordinary
780 representation of an expression expr and producing a valid tree
781 boolean expression describing whether expr is nonzero. We could
782 simply always do build_binary_op (NE_EXPR, expr, integer_zero_node, 1),
783 but we optimize comparisons, &&, ||, and !.
784
785 The resulting type should always be `integer_type_node'. */
786
787 tree
788 truthvalue_conversion (expr)
789 tree expr;
790 {
791 register enum tree_code code;
792
793 switch (TREE_CODE (expr))
794 {
795 /* It is simpler and generates better code to have only TRUTH_*_EXPR
796 or comparison expressions as truth values at this level. */
797 #if 0
798 case COMPONENT_REF:
799 /* A one-bit unsigned bit-field is already acceptable. */
800 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
801 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
802 return expr;
803 break;
804 #endif
805
806 case EQ_EXPR:
807 /* It is simpler and generates better code to have only TRUTH_*_EXPR
808 or comparison expressions as truth values at this level. */
809 #if 0
810 if (integer_zerop (TREE_OPERAND (expr, 1)))
811 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
812 #endif
813 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
814 case TRUTH_ANDIF_EXPR:
815 case TRUTH_ORIF_EXPR:
816 case TRUTH_AND_EXPR:
817 case TRUTH_OR_EXPR:
818 case ERROR_MARK:
819 return expr;
820
821 case INTEGER_CST:
822 return integer_zerop (expr) ? integer_zero_node : integer_one_node;
823
824 case REAL_CST:
825 return real_zerop (expr) ? integer_zero_node : integer_one_node;
826
827 case ADDR_EXPR:
828 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
829 return build (COMPOUND_EXPR, integer_type_node,
830 TREE_OPERAND (expr, 0), integer_one_node);
831 else
832 return integer_one_node;
833
834 case NEGATE_EXPR:
835 case ABS_EXPR:
836 case FLOAT_EXPR:
837 case FFS_EXPR:
838 /* These don't change whether an object is non-zero or zero. */
839 return truthvalue_conversion (TREE_OPERAND (expr, 0));
840
841 case LROTATE_EXPR:
842 case RROTATE_EXPR:
843 /* These don't change whether an object is zero or non-zero, but
844 we can't ignore them if their second arg has side-effects. */
845 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
846 return build (COMPOUND_EXPR, integer_type_node, TREE_OPERAND (expr, 1),
847 truthvalue_conversion (TREE_OPERAND (expr, 0)));
848 else
849 return truthvalue_conversion (TREE_OPERAND (expr, 0));
850
851 case COND_EXPR:
852 /* Distribute the conversion into the arms of a COND_EXPR. */
853 return fold (build (COND_EXPR, integer_type_node, TREE_OPERAND (expr, 0),
854 truthvalue_conversion (TREE_OPERAND (expr, 1)),
855 truthvalue_conversion (TREE_OPERAND (expr, 2))));
856
857 case CONVERT_EXPR:
858 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
859 since that affects how `default_conversion' will behave. */
860 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
861 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
862 break;
863 /* fall through... */
864 case NOP_EXPR:
865 /* If this is widening the argument, we can ignore it. */
866 if (TYPE_PRECISION (TREE_TYPE (expr))
867 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
868 return truthvalue_conversion (TREE_OPERAND (expr, 0));
869 break;
870
871 case BIT_XOR_EXPR:
872 case MINUS_EXPR:
873 /* These can be changed into a comparison of the two objects. */
874 if (TREE_TYPE (TREE_OPERAND (expr, 0))
875 == TREE_TYPE (TREE_OPERAND (expr, 1)))
876 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
877 TREE_OPERAND (expr, 1), 1);
878 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
879 fold (build1 (NOP_EXPR,
880 TREE_TYPE (TREE_OPERAND (expr, 0)),
881 TREE_OPERAND (expr, 1))), 1);
882 }
883
884 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
885 }
886 \f
887 /* Read the rest of a #-directive from input stream FINPUT.
888 In normal use, the directive name and the white space after it
889 have already been read, so they won't be included in the result.
890 We allow for the fact that the directive line may contain
891 a newline embedded within a character or string literal which forms
892 a part of the directive.
893
894 The value is a string in a reusable buffer. It remains valid
895 only until the next time this function is called. */
896
897 char *
898 get_directive_line (finput)
899 register FILE *finput;
900 {
901 static char *directive_buffer = NULL;
902 static unsigned buffer_length = 0;
903 register char *p;
904 register char *buffer_limit;
905 register int looking_for = 0;
906 register int char_escaped = 0;
907
908 if (buffer_length == 0)
909 {
910 directive_buffer = (char *)xmalloc (128);
911 buffer_length = 128;
912 }
913
914 buffer_limit = &directive_buffer[buffer_length];
915
916 for (p = directive_buffer; ; )
917 {
918 int c;
919
920 /* Make buffer bigger if it is full. */
921 if (p >= buffer_limit)
922 {
923 register unsigned bytes_used = (p - directive_buffer);
924
925 buffer_length *= 2;
926 directive_buffer
927 = (char *)xrealloc (directive_buffer, buffer_length);
928 p = &directive_buffer[bytes_used];
929 buffer_limit = &directive_buffer[buffer_length];
930 }
931
932 c = getc (finput);
933
934 /* Discard initial whitespace. */
935 if ((c == ' ' || c == '\t') && p == directive_buffer)
936 continue;
937
938 /* Detect the end of the directive. */
939 if (c == '\n' && looking_for == 0)
940 {
941 ungetc (c, finput);
942 c = '\0';
943 }
944
945 *p++ = c;
946
947 if (c == 0)
948 return directive_buffer;
949
950 /* Handle string and character constant syntax. */
951 if (looking_for)
952 {
953 if (looking_for == c && !char_escaped)
954 looking_for = 0; /* Found terminator... stop looking. */
955 }
956 else
957 if (c == '\'' || c == '"')
958 looking_for = c; /* Don't stop buffering until we see another
959 another one of these (or an EOF). */
960
961 /* Handle backslash. */
962 char_escaped = (c == '\\' && ! char_escaped);
963 }
964 }
This page took 0.088198 seconds and 6 git commands to generate.