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