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