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