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