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