1 /* Build expressions with type checking for CHILL compiler.
2 Copyright (C) 1992, 93, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
21 /* This file is part of the CHILL front end.
22 It contains routines to build C expressions given their operands,
23 including computing the modes of the result, C-specific error checks,
24 and some optimization.
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
40 extern tree intQI_type_node;
41 extern tree intHI_type_node;
42 extern tree intSI_type_node;
43 extern tree intDI_type_node;
44 #if HOST_BITS_PER_WIDE_INT >= 64
45 extern tree intTI_type_node;
48 extern tree unsigned_intQI_type_node;
49 extern tree unsigned_intHI_type_node;
50 extern tree unsigned_intSI_type_node;
51 extern tree unsigned_intDI_type_node;
52 #if HOST_BITS_PER_WIDE_INT >= 64
53 extern tree unsigned_intTI_type_node;
56 /* forward declarations */
57 static int chill_l_equivalent PROTO((tree, tree, struct mode_chain*));
58 static tree extract_constant_from_buffer PROTO((tree, unsigned char *, int));
59 static int expand_constant_to_buffer PROTO((tree, unsigned char *, int));
62 * This function checks an array access.
63 * It calls error (ERROR_MESSAGE) if the condition (index <= domain max value
64 * index >= domain min value)
65 * is not met at compile time,
66 * If a runtime test is required and permitted,
67 * check_expression is used to do so.
68 * the global RANGE_CHECKING flags controls the
69 * generation of runtime checking code.
72 valid_array_index_p (array, idx, error_message, is_varying_lhs)
77 tree cond, low_limit, high_cond, atype, domain;
78 tree orig_index = idx;
79 enum chill_tree_code condition;
81 if (array == NULL_TREE || TREE_CODE (array) == ERROR_MARK
82 || idx == NULL_TREE || TREE_CODE (idx) == ERROR_MARK)
83 return error_mark_node;
85 if (TREE_CODE (idx) == TYPE_DECL
86 || TREE_CODE_CLASS (TREE_CODE (idx)) == 't')
88 error ("array or string index is a mode (instead of a value)");
89 return error_mark_node;
92 atype = TREE_TYPE (array);
94 if (chill_varying_type_p (atype))
96 domain = TYPE_DOMAIN (CH_VARYING_ARRAY_TYPE (atype));
97 high_cond = build_component_ref (array, var_length_id);
98 if (chill_varying_string_type_p (atype))
110 domain = TYPE_DOMAIN (atype);
111 high_cond = TYPE_MAX_VALUE (domain);
115 if (CH_STRING_TYPE_P (atype))
117 if (! CH_SIMILAR (TREE_TYPE (orig_index), integer_type_node))
119 error ("index is not an integer expression");
120 return error_mark_node;
125 if (! CH_COMPATIBLE (orig_index, domain))
127 error ("index not compatible with index mode");
128 return error_mark_node;
132 /* Convert BOOLS(1) to BOOL and CHARS(1) to CHAR. */
133 if (flag_old_strings)
135 idx = convert_to_discrete (idx);
136 if (idx == NULL) /* should never happen */
137 error ("index is not discrete");
140 /* we know we'll refer to this value twice */
142 idx = save_expr (idx);
144 low_limit = TYPE_MIN_VALUE (domain);
145 high_cond = build_compare_discrete_expr (condition, idx, high_cond);
147 /* an invalid index expression meets this condition */
148 cond = fold (build (TRUTH_ORIF_EXPR, boolean_type_node,
149 build_compare_discrete_expr (LT_EXPR, idx, low_limit),
152 /* strip a redundant NOP_EXPR */
153 if (TREE_CODE (cond) == NOP_EXPR
154 && TREE_TYPE (cond) == boolean_type_node
155 && TREE_CODE (TREE_OPERAND (cond, 0)) == INTEGER_CST)
156 cond = TREE_OPERAND (cond, 0);
158 idx = convert (CH_STRING_TYPE_P (atype) ? integer_type_node : domain,
161 if (TREE_CODE (cond) == INTEGER_CST)
163 if (tree_int_cst_equal (cond, boolean_false_node))
164 return idx; /* condition met at compile time */
165 error (error_message); /* condition failed at compile time */
166 return error_mark_node;
168 else if (range_checking)
170 /* FIXME: often, several of these conditions will
171 be generated for the same source file and line number.
172 A great optimization would be to share the
173 cause_exception function call among them rather
174 than generating a cause_exception call for each. */
175 return check_expression (idx, cond,
176 ridpointers[(int) RID_RANGEFAIL]);
179 return idx; /* don't know at compile time */
183 * Extract a slice from an array, which could look like a
184 * SET_TYPE if it's a bitstring. The array could also be VARYING
185 * if the element type is CHAR. The min_value and length values
186 * must have already been checked with valid_array_index_p. No
187 * checking is done here.
190 build_chill_slice (array, min_value, length)
191 tree array, min_value, length;
194 tree array_type = TREE_TYPE (array);
196 if (!CH_REFERABLE (array) && TREE_CODE (array) != SAVE_EXPR
197 && (TREE_CODE (array) != COMPONENT_REF
198 || TREE_CODE (TREE_OPERAND (array, 0)) != SAVE_EXPR))
200 if (!TREE_CONSTANT (array))
201 warning ("possible internal error - slice argument is neither referable nor constant");
205 NOTE: This could mean multiple identical copies of
206 the same constant. FIXME. */
207 tree mydecl = decl_temp1 (get_unique_identifier("SLICEE"),
208 array_type, 1, array, 0, 0);
209 TREE_READONLY (mydecl) = 1;
210 /* mark_addressable (mydecl); FIXME: necessary? */
216 The code-generation which uses a slice tree needs not only to
217 know the dynamic upper and lower limits of that slice, but the
218 original static allocation, to use to build temps where one or both
219 of the dynamic limits must be calculated at runtime.. We pass the
220 dynamic size by building a new array_type whose limits are the
221 min_value and min_value + length values passed to us.
223 The static allocation info is passed by using the parent array's
224 limits to compute a temp_size, which is passed in the lang_specific
225 field of the slice_type.
228 if (TREE_CODE (array_type) == ARRAY_TYPE)
230 tree domain_type = TYPE_DOMAIN (array_type);
231 tree domain_min = TYPE_MIN_VALUE (domain_type);
232 tree domain_max = fold (build (PLUS_EXPR, domain_type,
234 size_binop (MINUS_EXPR,
235 length, integer_one_node)));
236 tree index_type = build_chill_range_type (TYPE_DOMAIN (array_type),
240 tree element_type = TREE_TYPE (array_type);
241 tree slice_type = build_simple_array_type (element_type, index_type, NULL_TREE);
242 tree slice_pointer_type;
245 if (CH_CHARS_TYPE_P (array_type))
246 MARK_AS_STRING_TYPE (slice_type);
248 TYPE_PACKED (slice_type) = TYPE_PACKED (array_type);
250 SET_CH_NOVELTY (slice_type, CH_NOVELTY (array_type));
252 if (TREE_CONSTANT (array) && TREE_CODE (min_value) == INTEGER_CST
253 && TREE_CODE (length) == INTEGER_CST)
255 int type_size = int_size_in_bytes (array_type);
256 unsigned char *buffer = (unsigned char*) alloca (type_size);
257 int delta = int_size_in_bytes (element_type)
258 * (TREE_INT_CST_LOW (min_value) - TREE_INT_CST_LOW (domain_min));
259 bzero (buffer, type_size);
260 if (expand_constant_to_buffer (array, buffer, type_size))
262 result = extract_constant_from_buffer (slice_type,
270 /* Kludge used by case CONCAT_EXPR in chill_expand_expr.
271 Set TYPE_ARRAY_MAX_SIZE to a constant upper bound on the
273 max_size = size_in_bytes (slice_type);
274 if (TREE_CODE (max_size) != INTEGER_CST)
276 max_size = TYPE_ARRAY_MAX_SIZE (array_type);
277 if (max_size == NULL_TREE)
278 max_size = size_in_bytes (array_type);
280 TYPE_ARRAY_MAX_SIZE (slice_type) = max_size;
282 mark_addressable (array);
283 /* Contruct a SLICE_EXPR to represent a slice of a packed array of bits. */
284 if (TYPE_PACKED (array_type))
286 if (pass == 2 && TREE_CODE (length) != INTEGER_CST)
288 sorry ("bit array slice with non-constant length");
289 return error_mark_node;
291 if (domain_min && ! integer_zerop (domain_min))
292 min_value = size_binop (MINUS_EXPR, min_value,
293 convert (sizetype, domain_min));
294 result = build (SLICE_EXPR, slice_type, array, min_value, length);
295 TREE_READONLY (result)
296 = TREE_READONLY (array) | TYPE_READONLY (TREE_TYPE (array_type));
300 slice_pointer_type = build_chill_pointer_type (slice_type);
301 if (TREE_CODE (min_value) == INTEGER_CST
302 && domain_min && TREE_CODE (domain_min) == INTEGER_CST
303 && compare_int_csts (EQ_EXPR, min_value, domain_min))
304 result = fold (build1 (ADDR_EXPR, slice_pointer_type, array));
307 min_value = convert (sizetype, min_value);
308 if (domain_min && ! integer_zerop (domain_min))
309 min_value = size_binop (MINUS_EXPR, min_value,
310 convert (sizetype, domain_min));
311 min_value = size_binop (MULT_EXPR, min_value,
312 size_in_bytes (element_type));
313 result = fold (build (PLUS_EXPR, slice_pointer_type,
314 build1 (ADDR_EXPR, slice_pointer_type,
316 convert (slice_pointer_type, min_value)));
318 /* Return the final array value. */
319 result = fold (build1 (INDIRECT_REF, slice_type, result));
320 TREE_READONLY (result)
321 = TREE_READONLY (array) | TYPE_READONLY (element_type);
324 else if (TREE_CODE (array_type) == SET_TYPE) /* actually a bitstring */
326 if (pass == 2 && TREE_CODE (length) != INTEGER_CST)
328 sorry ("bitstring slice with non-constant length");
329 return error_mark_node;
331 result = build (SLICE_EXPR, build_bitstring_type (length),
332 array, min_value, length);
333 TREE_READONLY (result)
334 = TREE_READONLY (array) | TYPE_READONLY (TREE_TYPE (array_type));
337 else if (chill_varying_type_p (array_type))
338 return build_chill_slice (varying_to_slice (array), min_value, length);
341 error ("slice operation on non-array, non-bitstring value not supported");
342 return error_mark_node;
347 build_empty_string (type)
350 int orig_pass = pass;
353 range = build_chill_range_type (type, integer_zero_node,
354 integer_minus_one_node);
355 result = build_chill_array_type (type,
356 tree_cons (NULL_TREE, range, NULL_TREE), 0, NULL_TREE);
358 range = build_chill_range_type (type, integer_zero_node,
359 integer_minus_one_node);
360 result = build_chill_array_type (type,
361 tree_cons (NULL_TREE, range, NULL_TREE), 0, NULL_TREE);
364 return decl_temp1 (get_unique_identifier ("EMPTY_STRING"),
365 result, 0, NULL_TREE, 0, 0);
368 /* We build the runtime range-checking as a separate list
369 * rather than making a compound_expr with min_value
370 * (for example), to control when that comparison gets
371 * generated. We cannot allow it in a TYPE_MAX_VALUE or
372 * TYPE_MIN_VALUE expression, for instance, because that code
373 * will get generated when the slice is laid out, which would
374 * put it outside the scope of an exception handler for the
375 * statement we're generating. I.e. we would be generating
376 * cause_exception calls which might execute before the
377 * necessary ch_link_handler call.
380 build_chill_slice_with_range (array, min_value, max_value)
381 tree array, min_value, max_value;
383 if (array == NULL_TREE || TREE_CODE (array) == ERROR_MARK
384 || min_value == NULL_TREE || TREE_CODE(min_value) == ERROR_MARK
385 || max_value == NULL_TREE || TREE_CODE(max_value) == ERROR_MARK)
386 return error_mark_node;
388 if (TREE_TYPE (array) == NULL_TREE
389 || (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
390 && TREE_CODE (TREE_TYPE (array)) != SET_TYPE
391 && !chill_varying_type_p (TREE_TYPE (array))))
393 error ("can only take slice of array or string");
394 return error_mark_node;
397 array = save_if_needed (array);
399 /* FIXME: test here for max_value >= min_value, except
400 for max_value == -1, min_value == 0 (empty string) */
401 min_value = valid_array_index_p (array, min_value,
402 "slice lower limit out-of-range", 0);
403 if (TREE_CODE (min_value) == ERROR_MARK)
406 /* FIXME: suppress this test if max_value is the LENGTH of a
407 varying array, which has presumably already been checked. */
408 max_value = valid_array_index_p (array, max_value,
409 "slice upper limit out-of-range", 0);
410 if (TREE_CODE (max_value) == ERROR_MARK)
411 return error_mark_node;
413 if (TREE_CODE (min_value) == INTEGER_CST
414 && TREE_CODE (max_value) == INTEGER_CST
415 && tree_int_cst_lt (max_value, min_value))
416 return build_empty_string (TREE_TYPE (TREE_TYPE (array)));
418 return build_chill_slice (array, min_value,
419 save_expr (size_binop (PLUS_EXPR,
420 size_binop (MINUS_EXPR, max_value, min_value),
426 build_chill_slice_with_length (array, min_value, length)
427 tree array, min_value, length;
430 tree cond, high_cond, atype;
432 if (array == NULL_TREE || TREE_CODE (array) == ERROR_MARK
433 || min_value == NULL_TREE || TREE_CODE(min_value) == ERROR_MARK
434 || length == NULL_TREE || TREE_CODE(length) == ERROR_MARK)
435 return error_mark_node;
437 if (TREE_TYPE (array) == NULL_TREE
438 || (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
439 && TREE_CODE (TREE_TYPE (array)) != SET_TYPE
440 && !chill_varying_type_p (TREE_TYPE (array))))
442 error ("can only take slice of array or string");
443 return error_mark_node;
446 if (TREE_CONSTANT (length)
447 && tree_int_cst_lt (length, integer_zero_node))
448 return build_empty_string (TREE_TYPE (TREE_TYPE (array)));
450 array = save_if_needed (array);
451 min_value = save_expr (min_value);
452 length = save_expr (length);
454 if (! CH_SIMILAR (TREE_TYPE (length), integer_type_node))
456 error ("slice length is not an integer");
457 length = integer_one_node;
460 max_index = size_binop (MINUS_EXPR,
461 size_binop (PLUS_EXPR, length, min_value),
463 max_index = convert_to_class (chill_expr_class (min_value), max_index);
465 min_value = valid_array_index_p (array, min_value,
466 "slice start index out-of-range", 0);
467 if (TREE_CODE (min_value) == ERROR_MARK)
468 return error_mark_node;
470 atype = TREE_TYPE (array);
472 if (chill_varying_type_p (atype))
473 high_cond = build_component_ref (array, var_length_id);
475 high_cond = TYPE_MAX_VALUE (TYPE_DOMAIN (atype));
477 /* an invalid index expression meets this condition */
478 cond = fold (build (TRUTH_ORIF_EXPR, boolean_type_node,
479 build_compare_discrete_expr (LT_EXPR,
480 length, integer_zero_node),
481 build_compare_discrete_expr (GT_EXPR,
482 max_index, high_cond)));
484 if (TREE_CODE (cond) == INTEGER_CST)
486 if (! tree_int_cst_equal (cond, boolean_false_node))
488 error ("slice length out-of-range");
489 return error_mark_node;
493 else if (range_checking)
495 min_value = check_expression (min_value, cond,
496 ridpointers[(int) RID_RANGEFAIL]);
499 return build_chill_slice (array, min_value, length);
503 build_chill_array_ref (array, indexlist)
504 tree array, indexlist;
508 if (array == NULL_TREE || TREE_CODE (array) == ERROR_MARK)
509 return error_mark_node;
510 if (indexlist == NULL_TREE || TREE_CODE (indexlist) == ERROR_MARK)
511 return error_mark_node;
513 idx = TREE_VALUE (indexlist); /* handle first index */
515 idx = valid_array_index_p (array, idx,
516 "array index out-of-range", 0);
517 if (TREE_CODE (idx) == ERROR_MARK)
518 return error_mark_node;
520 array = build_chill_array_ref_1 (array, idx);
522 if (array && TREE_CODE (array) != ERROR_MARK
523 && TREE_CHAIN (indexlist))
525 /* Z.200 (1988) section 4.2.8 says that:
526 <array> '(' <expression {',' <expression> }* ')'
527 is derived syntax (i.e. syntactic sugar) for:
528 <array> '(' <expression ')' { '(' <expression> ')' }*
529 The intent is clear if <array> has mode: ARRAY (...) ARRAY (...) XXX.
530 But what if <array> has mode: ARRAY (...) CHARS (N)
531 or: ARRAY (...) BOOLS (N).
532 Z.200 doesn't explicitly prohibit it, but the intent is unclear.
533 We'll allow it, since it seems reasonable and useful.
534 However, we won't allow it if <array> is:
535 ARRAY (...) PROC (...).
536 (The latter would make sense if we allowed general
537 Currying, which Chill doesn't.) */
538 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
539 || chill_varying_type_p (TREE_TYPE (array))
540 || CH_BOOLS_TYPE_P (TREE_TYPE (array)))
541 array = build_generalized_call (array, TREE_CHAIN (indexlist));
543 error ("too many index expressions");
549 * Don't error check the index in here. It's supposed to be
550 * checked by the caller.
553 build_chill_array_ref_1 (array, idx)
560 if (array == NULL_TREE || TREE_CODE (array) == ERROR_MARK
561 || idx == NULL_TREE || TREE_CODE (idx) == ERROR_MARK)
562 return error_mark_node;
564 if (chill_varying_type_p (TREE_TYPE (array)))
565 array = varying_to_slice (array);
567 domain = TYPE_DOMAIN (TREE_TYPE (array));
570 if (! integer_zerop (TYPE_MIN_VALUE (domain)))
572 /* The C part of the compiler doesn't understand how to do
573 arithmetic with dissimilar enum types. So we check compatability
574 here, and perform the math in INTEGER_TYPE. */
575 if (TREE_CODE (TREE_TYPE (idx)) == ENUMERAL_TYPE
576 && chill_comptypes (TREE_TYPE (idx), domain, 0))
577 idx = convert (TREE_TYPE (TYPE_MIN_VALUE (domain)), idx);
578 idx = build_binary_op (MINUS_EXPR, idx, TYPE_MIN_VALUE (domain), 0);
582 if (CH_STRING_TYPE_P (TREE_TYPE (array)))
584 /* Could be bitstring or char string. */
585 if (TREE_TYPE (TREE_TYPE (array)) == boolean_type_node)
587 rval = build (SET_IN_EXPR, boolean_type_node, idx, array);
588 TREE_READONLY (rval) = TREE_READONLY (array);
593 if (!discrete_type_p (TREE_TYPE (idx)))
595 error ("array index is not discrete");
596 return error_mark_node;
599 /* An array that is indexed by a non-constant
600 cannot be stored in a register; we must be able to do
601 address arithmetic on its address.
602 Likewise an array of elements of variable size. */
603 if (TREE_CODE (idx) != INTEGER_CST
604 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
605 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
607 if (mark_addressable (array) == 0)
608 return error_mark_node;
611 type = TREE_TYPE (TREE_TYPE (array));
613 /* Do constant folding */
614 if (TREE_CODE (idx) == INTEGER_CST && TREE_CONSTANT (array))
616 struct ch_class class;
617 class.kind = CH_VALUE_CLASS;
620 if (TREE_CODE (array) == CONSTRUCTOR)
622 tree list = CONSTRUCTOR_ELTS (array);
623 for ( ; list != NULL_TREE; list = TREE_CHAIN (list))
625 if (tree_int_cst_equal (TREE_PURPOSE (list), idx))
626 return convert_to_class (class, TREE_VALUE (list));
629 else if (TREE_CODE (array) == STRING_CST
630 && CH_CHARS_TYPE_P (TREE_TYPE (array)))
632 HOST_WIDE_INT i = TREE_INT_CST_LOW (idx);
633 if (i >= 0 && i < TREE_STRING_LENGTH (array))
635 char ch = TREE_STRING_POINTER (array) [i];
636 return convert_to_class (class,
637 build_int_2 ((unsigned char)ch, 0));
642 if (TYPE_PACKED (TREE_TYPE (array)))
643 rval = build (PACKED_ARRAY_REF, type, array, idx);
645 rval = build (ARRAY_REF, type, array, idx);
647 /* Array ref is const/volatile if the array elements are
648 or if the array is. */
649 TREE_READONLY (rval) = TREE_READONLY (array) | TYPE_READONLY (type);
650 TREE_SIDE_EFFECTS (rval)
651 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
652 | TREE_SIDE_EFFECTS (array));
653 TREE_THIS_VOLATILE (rval)
654 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
655 /* This was added by rms on 16 Nov 91.
656 It fixes vol struct foo *a; a->elts[1]
657 in an inline function.
658 Hope it doesn't break something else. */
659 | TREE_THIS_VOLATILE (array));
664 build_chill_bitref (bitstring, indexlist)
665 tree bitstring, indexlist;
667 if (TREE_CODE (bitstring) == ERROR_MARK)
669 if (TREE_CODE (indexlist) == ERROR_MARK)
672 if (TREE_CHAIN (indexlist) != NULL_TREE)
674 error ("invalid compound index for bitstring mode");
675 return error_mark_node;
678 if (TREE_CODE (indexlist) == TREE_LIST)
680 tree result = build (SET_IN_EXPR, boolean_type_node,
681 TREE_VALUE (indexlist), bitstring);
682 TREE_READONLY (result) = TREE_READONLY (bitstring);
690 discrete_type_p (type)
693 return INTEGRAL_TYPE_P (type);
696 /* Checks that EXP has discrete type, or can be converted to discrete.
697 Otherwise, returns NULL_TREE.
698 Normally returns the (possibly-converted) EXP. */
701 convert_to_discrete (exp)
704 if (! discrete_type_p (TREE_TYPE (exp)))
706 if (flag_old_strings)
708 if (CH_CHARS_ONE_P (TREE_TYPE (exp)))
709 return convert (char_type_node, exp);
710 if (CH_BOOLS_ONE_P (TREE_TYPE (exp)))
711 return convert (boolean_type_node, exp);
718 /* Write into BUFFER the target-machine representation of VALUE.
719 Returns 1 on success, or 0 on failure. (Either the VALUE was
720 not constant, or we don't know how to do the conversion.) */
723 expand_constant_to_buffer (value, buffer, buf_size)
725 unsigned char *buffer;
728 tree type = TREE_TYPE (value);
729 int size = int_size_in_bytes (type);
731 if (size < 0 || size > buf_size)
733 switch (TREE_CODE (value))
737 HOST_WIDE_INT lo = TREE_INT_CST_LOW (value);
738 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (value);
739 for (i = 0; i < size; i++)
741 /* Doesn't work if host and target BITS_PER_UNIT differ. */
742 unsigned char byte = lo & ((1 << BITS_PER_UNIT) - 1);
743 if (BYTES_BIG_ENDIAN)
744 buffer[size - i - 1] = byte;
747 rshift_double (lo, hi, BITS_PER_UNIT, BITS_PER_UNIT * size,
754 size = TREE_STRING_LENGTH (value);
757 bcopy (TREE_STRING_POINTER (value), buffer, size);
761 if (TREE_CODE (type) == ARRAY_TYPE)
763 tree element_type = TREE_TYPE (type);
764 int element_size = int_size_in_bytes (element_type);
765 tree list = CONSTRUCTOR_ELTS (value);
766 HOST_WIDE_INT next_index;
767 HOST_WIDE_INT min_index = 0;
768 if (element_size < 0)
771 if (TYPE_DOMAIN (type) != 0)
773 tree min_val = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
776 if (TREE_CODE (min_val) != INTEGER_CST)
779 min_index = TREE_INT_CST_LOW (min_val);
783 next_index = min_index;
785 for (; list != NULL_TREE; list = TREE_CHAIN (list))
787 HOST_WIDE_INT offset;
788 HOST_WIDE_INT last_index;
789 tree purpose = TREE_PURPOSE (list);
792 if (TREE_CODE (purpose) == INTEGER_CST)
793 last_index = next_index = TREE_INT_CST_LOW (purpose);
794 else if (TREE_CODE (purpose) == RANGE_EXPR)
796 next_index = TREE_INT_CST_LOW (TREE_OPERAND(purpose, 0));
797 last_index = TREE_INT_CST_LOW (TREE_OPERAND(purpose, 1));
803 last_index = next_index;
804 for ( ; next_index <= last_index; next_index++)
806 offset = (next_index - min_index) * element_size;
807 if (!expand_constant_to_buffer (TREE_VALUE (list),
815 else if (TREE_CODE (type) == RECORD_TYPE)
817 tree list = CONSTRUCTOR_ELTS (value);
818 for (; list != NULL_TREE; list = TREE_CHAIN (list))
820 tree field = TREE_PURPOSE (list);
821 HOST_WIDE_INT offset;
822 if (field == NULL_TREE || TREE_CODE (field) != FIELD_DECL)
824 if (DECL_BIT_FIELD (field))
826 offset = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
828 if (!expand_constant_to_buffer (TREE_VALUE (list),
835 else if (TREE_CODE (type) == SET_TYPE)
837 if (get_set_constructor_bytes (value, buffer, buf_size)
848 /* Given that BUFFER contains a target-machine representation of
849 a value of type TYPE, return that value as a tree.
850 Returns NULL_TREE on failure. (E.g. the TYPE might be variable size,
851 or perhaps we don't know how to do the conversion.) */
854 extract_constant_from_buffer (type, buffer, buf_size)
856 unsigned char *buffer;
860 int size = int_size_in_bytes (type);
862 if (size < 0 || size > buf_size)
864 switch (TREE_CODE (type))
872 HOST_WIDE_INT lo = 0, hi = 0;
873 /* Accumulate (into (lo,hi) the bytes (from buffer). */
874 for (i = size; --i >= 0; )
877 /* Get next byte (in big-endian order). */
878 if (BYTES_BIG_ENDIAN)
879 byte = buffer[size - i - 1];
882 lshift_double (lo, hi, BITS_PER_UNIT, TYPE_PRECISION (type),
884 add_double (lo, hi, byte, 0, &lo, &hi);
886 value = build_int_2 (lo, hi);
887 TREE_TYPE (value) = type;
892 tree element_type = TREE_TYPE (type);
893 int element_size = int_size_in_bytes (element_type);
894 tree list = NULL_TREE;
895 HOST_WIDE_INT min_index = 0, max_index, cur_index;
896 if (element_size == 1 && CH_CHARS_TYPE_P (type))
898 value = build_string (size, buffer);
899 CH_DERIVED_FLAG (value) = 1;
900 TREE_TYPE (value) = type;
903 if (TYPE_DOMAIN (type) == 0)
905 value = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
908 if (TREE_CODE (value) != INTEGER_CST)
911 min_index = TREE_INT_CST_LOW (value);
913 value = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
914 if (value == NULL_TREE || TREE_CODE (value) != INTEGER_CST)
917 max_index = TREE_INT_CST_LOW (value);
918 for (cur_index = max_index; cur_index >= min_index; cur_index--)
920 HOST_WIDE_INT offset = (cur_index - min_index) * element_size;
921 value = extract_constant_from_buffer (element_type,
924 if (value == NULL_TREE)
926 list = tree_cons (build_int_2 (cur_index, 0), value, list);
928 value = build (CONSTRUCTOR, type, NULL_TREE, list);
929 TREE_CONSTANT (value) = 1;
930 TREE_STATIC (value) = 1;
935 tree list = NULL_TREE;
936 tree field = TYPE_FIELDS (type);
937 for (; field != NULL_TREE; field = TREE_CHAIN (field))
940 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)) / BITS_PER_UNIT;
941 if (DECL_BIT_FIELD (field))
943 value = extract_constant_from_buffer (TREE_TYPE (field),
946 if (value == NULL_TREE)
948 list = tree_cons (field, value, list);
950 value = build (CONSTRUCTOR, type, NULL_TREE, nreverse (list));
951 TREE_CONSTANT (value) = 1;
952 TREE_STATIC (value) = 1;
958 tree longest_variant = NULL_TREE;
959 int longest_size = 0;
960 tree field = TYPE_FIELDS (type);
962 /* This is a kludge. We assume that converting the data to te
963 longest variant will provide valid data for the "correct"
964 variant. This is usually the case, but is not guaranteed.
965 For example, the longest variant may include holes.
966 Also incorrect interpreting the given value as the longest
967 variant may confuse the compiler if that should happen
968 to yield invalid values. ??? */
970 for (; field != NULL_TREE; field = TREE_CHAIN (field))
972 int size = TREE_INT_CST_LOW (size_in_bytes (TREE_TYPE (field)));
974 if (size > longest_size)
977 longest_variant = field;
980 if (longest_variant == NULL_TREE)
982 return extract_constant_from_buffer (TREE_TYPE (longest_variant), buffer, buf_size);
987 tree list = NULL_TREE;
989 HOST_WIDE_INT min_index, max_index;
990 if (TYPE_DOMAIN (type) == 0)
992 value = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
993 if (value == NULL_TREE)
995 else if (TREE_CODE (value) != INTEGER_CST)
998 min_index = TREE_INT_CST_LOW (value);
999 value = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1000 if (value == NULL_TREE)
1002 else if (TREE_CODE (value) != INTEGER_CST)
1005 max_index = TREE_INT_CST_LOW (value);
1006 for (i = max_index + 1 - min_index; --i >= 0; )
1008 unsigned char byte = (unsigned char)buffer[i / BITS_PER_UNIT];
1009 unsigned bit_pos = (unsigned)i % (unsigned)BITS_PER_UNIT;
1010 if (BYTES_BIG_ENDIAN
1011 ? (byte & (1 << (BITS_PER_UNIT - 1 - bit_pos)))
1012 : (byte & (1 << bit_pos)))
1013 list = tree_cons (NULL_TREE,
1014 build_int_2 (i + min_index, 0), list);
1016 value = build (CONSTRUCTOR, type, NULL_TREE, list);
1017 TREE_CONSTANT (value) = 1;
1018 TREE_STATIC (value) = 1;
1028 build_chill_cast (type, expr)
1034 int type_is_discrete;
1035 int expr_type_is_discrete;
1037 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
1038 return error_mark_node;
1039 if (expr == NULL_TREE || TREE_CODE (expr) == ERROR_MARK)
1040 return error_mark_node;
1042 /* if expression was untyped because of its context (an
1043 if_expr or case_expr in a tuple, perhaps) just apply
1045 expr_type = TREE_TYPE (expr);
1046 if (expr_type == NULL_TREE
1047 || TREE_CODE (expr_type) == ERROR_MARK)
1048 return convert (type, expr);
1050 if (expr_type == type)
1053 expr_type_size = int_size_in_bytes (expr_type);
1054 type_size = int_size_in_bytes (type);
1056 if (expr_type_size == -1)
1058 error ("conversions from variable_size value");
1059 return error_mark_node;
1061 if (type_size == -1)
1063 error ("conversions to variable_size mode");
1064 return error_mark_node;
1067 /* FIXME: process REAL ==> INT && INT ==> REAL && REAL ==> REAL. I hope this is correct. */
1068 if ((TREE_CODE (expr_type) == INTEGER_TYPE && TREE_CODE (type) == REAL_TYPE) ||
1069 (TREE_CODE (expr_type) == REAL_TYPE && TREE_CODE (type) == INTEGER_TYPE) ||
1070 (TREE_CODE (expr_type) == REAL_TYPE && TREE_CODE (type) == REAL_TYPE))
1071 return convert (type, expr);
1073 /* FIXME: Don't know if this is correct */
1074 /* Don't allow conversions to or from REAL with others then integer */
1075 if (TREE_CODE (type) == REAL_TYPE)
1077 error ("cannot convert to float");
1078 return error_mark_node;
1080 else if (TREE_CODE (expr_type) == REAL_TYPE)
1082 error ("cannot convert float to this mode");
1083 return error_mark_node;
1086 if (expr_type_size == type_size && CH_REFERABLE (expr))
1087 goto do_location_conversion;
1090 = discrete_type_p (type) || TREE_CODE (type) == POINTER_TYPE;
1091 expr_type_is_discrete
1092 = discrete_type_p (expr_type) || TREE_CODE (expr_type) == POINTER_TYPE;
1093 if (expr_type_is_discrete && type_is_discrete)
1095 /* do an overflow check
1096 FIXME: is this always neccessary ??? */
1097 /* FIXME: don't do range chacking when target type is PTR.
1098 PTR doesn't have MIN and MAXVALUE. result is sigsegv. */
1099 if (range_checking && type != ptr_type_node)
1104 if (TREE_CONSTANT (tmp) && TREE_CODE (tmp) != ADDR_EXPR)
1106 if (compare_int_csts (LT_EXPR, tmp, TYPE_MIN_VALUE (type)) ||
1107 compare_int_csts (GT_EXPR, tmp, TYPE_MAX_VALUE (type)))
1109 error ("OVERFLOW in expression conversion");
1110 return error_mark_node;
1115 int cond1 = tree_int_cst_lt (TYPE_SIZE (type),
1116 TYPE_SIZE (expr_type));
1117 int cond2 = TREE_UNSIGNED (type) && (! TREE_UNSIGNED (expr_type));
1118 int cond3 = (! TREE_UNSIGNED (type))
1119 && TREE_UNSIGNED (expr_type)
1120 && tree_int_cst_equal (TYPE_SIZE (type),
1121 TYPE_SIZE (expr_type));
1122 int cond4 = TREE_TYPE (type) && type_is_discrete;
1124 if (cond1 || cond2 || cond3 || cond4)
1126 tree type_min = TYPE_MIN_VALUE (type);
1127 tree type_max = TYPE_MAX_VALUE (type);
1129 expr = save_if_needed (expr);
1130 if (expr && type_min && type_max)
1132 tree check = test_range (expr, type_min, type_max);
1133 if (!integer_zerop (check))
1135 if (current_function_decl == NULL_TREE)
1137 if (TREE_CODE (check) == INTEGER_CST)
1138 error ("overflow (not inside function)");
1140 warning ("possible overflow (not inside function)");
1144 if (TREE_CODE (check) == INTEGER_CST)
1145 warning ("expression will always cause OVERFLOW");
1146 expr = check_expression (expr, check,
1147 ridpointers[(int) RID_OVERFLOW]);
1154 return convert (type, expr);
1157 if (TREE_CODE (expr) == INTEGER_CST && expr_type_size != type_size)
1159 /* There should probably be a pedwarn here ... */
1160 tree itype = type_for_size (type_size * BITS_PER_UNIT, 1);
1163 expr = convert (itype, expr);
1164 expr_type = TREE_TYPE (expr);
1165 expr_type_size= type_size;
1169 /* If expr is a constant of the right size, use it to to
1170 initialize a static variable. */
1171 if (expr_type_size == type_size && TREE_CONSTANT (expr) && !pedantic)
1173 unsigned char *buffer = (unsigned char*) alloca (type_size);
1175 bzero (buffer, type_size);
1176 if (!expand_constant_to_buffer (expr, buffer, type_size))
1178 error ("not implemented: constant conversion from that kind of expression");
1179 return error_mark_node;
1181 value = extract_constant_from_buffer (type, buffer, type_size);
1182 if (value == NULL_TREE)
1184 error ("not implemented: constant conversion to that kind of mode");
1185 return error_mark_node;
1190 if (!CH_REFERABLE (expr) && expr_type_size == type_size)
1192 tree temp = decl_temp1 (get_unique_identifier ("CAST"),
1193 TREE_TYPE (expr), 0, 0, 0, 0);
1194 tree convert1 = build_chill_modify_expr (temp, expr);
1195 pedwarn ("non-standard, non-portable value conversion");
1196 return build (COMPOUND_EXPR, type, convert1,
1197 build_chill_cast (type, temp));
1200 if (CH_REFERABLE (expr) && expr_type_size != type_size)
1201 error ("location conversion between differently-sized modes");
1203 error ("unsupported value conversion");
1204 return error_mark_node;
1206 do_location_conversion:
1207 /* To avoid confusing other parts of gcc,
1208 represent this as the C expression: *(TYPE*)EXPR. */
1209 mark_addressable (expr);
1210 expr = build1 (INDIRECT_REF, type,
1211 build1 (NOP_EXPR, build_pointer_type (type),
1212 build1 (ADDR_EXPR, build_pointer_type (expr_type),
1214 TREE_READONLY (expr) = TYPE_READONLY (type);
1219 * given a set_type, build an integer array from it that C will grok.
1222 build_array_from_set (type)
1225 tree bytespint, bit_array_size, int_array_count;
1227 if (type == NULL_TREE || type == error_mark_node || TREE_CODE (type) != SET_TYPE)
1228 return error_mark_node;
1230 bytespint = build_int_2 (HOST_BITS_PER_INT / HOST_BITS_PER_CHAR, 0);
1231 bit_array_size = size_in_bytes (type);
1232 int_array_count = fold (size_binop (TRUNC_DIV_EXPR, bit_array_size,
1234 if (integer_zerop (int_array_count))
1235 int_array_count = size_one_node;
1236 type = build_array_type (integer_type_node,
1237 build_index_type (int_array_count));
1243 build_chill_bin_type (size)
1249 if (TREE_CODE (size) != INTEGER_CST
1250 || (isize = TREE_INT_CST_LOW (size), isize <= 0))
1252 error ("operand to bin must be a non-negative integer literal");
1253 return error_mark_node;
1255 if (isize <= TYPE_PRECISION (unsigned_char_type_node))
1256 return unsigned_char_type_node;
1257 if (isize <= TYPE_PRECISION (short_unsigned_type_node))
1258 return short_unsigned_type_node;
1259 if (isize <= TYPE_PRECISION (unsigned_type_node))
1260 return unsigned_type_node;
1261 if (isize <= TYPE_PRECISION (long_unsigned_type_node))
1262 return long_unsigned_type_node;
1263 if (isize <= TYPE_PRECISION (long_long_unsigned_type_node))
1264 return long_long_unsigned_type_node;
1265 error ("size %d of BIN too big - no such integer mode", isize);
1266 return error_mark_node;
1272 bintype = make_node (INTEGER_TYPE);
1273 TREE_TYPE (bintype) = ridpointers[(int) RID_BIN];
1274 TYPE_MIN_VALUE (bintype) = size;
1275 TYPE_MAX_VALUE (bintype) = size;
1279 error ("BIN in pass 2");
1280 return error_mark_node;
1286 chill_expand_tuple (type, constructor)
1287 tree type, constructor;
1290 tree nonreft = type;
1292 if (TYPE_NAME (type) != NULL_TREE)
1294 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1295 name = IDENTIFIER_POINTER (TYPE_NAME (type));
1297 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1302 /* get to actual underlying type for digest_init */
1303 while (nonreft && TREE_CODE (nonreft) == REFERENCE_TYPE)
1304 nonreft = TREE_TYPE (nonreft);
1306 if (TREE_CODE (nonreft) == ARRAY_TYPE
1307 || TREE_CODE (nonreft) == RECORD_TYPE
1308 || TREE_CODE (nonreft) == SET_TYPE)
1309 return convert (nonreft, constructor);
1312 error ("mode of tuple is neither ARRAY, STRUCT, nor POWERSET");
1313 return error_mark_node;
1317 /* This function classifies an expr into the Null class,
1318 the All class, the M-Value, the M-derived, or the M-reference class.
1319 It probably has some inaccuracies. */
1322 chill_expr_class (expr)
1325 struct ch_class class;
1326 /* The Null class contains the NULL pointer constant (only). */
1327 if (expr == null_pointer_node)
1329 class.kind = CH_NULL_CLASS;
1330 class.mode = NULL_TREE;
1334 /* The All class contains the <undefined value> "*". */
1335 if (TREE_CODE (expr) == UNDEFINED_EXPR)
1337 class.kind = CH_ALL_CLASS;
1338 class.mode = NULL_TREE;
1342 if (CH_DERIVED_FLAG (expr))
1344 class.kind = CH_DERIVED_CLASS;
1345 class.mode = TREE_TYPE (expr);
1349 /* The M-Reference contains <references location> (address-of) expressions.
1350 Note that something that's been converted to a reference doesn't count. */
1351 if (TREE_CODE (expr) == ADDR_EXPR
1352 && TREE_CODE (TREE_TYPE (expr)) != REFERENCE_TYPE)
1354 class.kind = CH_REFERENCE_CLASS;
1355 class.mode = TREE_TYPE (TREE_TYPE (expr));
1359 /* The M-Value class contains expressions with a known, specific mode M. */
1360 class.kind = CH_VALUE_CLASS;
1361 class.mode = TREE_TYPE (expr);
1365 /* Returns >= 1 iff REF is a location. Return 2 if it is referable. */
1367 int chill_location (ref)
1370 register enum tree_code code = TREE_CODE (ref);
1377 case PACKED_ARRAY_REF:
1379 case NOP_EXPR: /* RETYPE_EXPR */
1380 return chill_location (TREE_OPERAND (ref, 0));
1382 return chill_location (TREE_OPERAND (ref, 1));
1386 /* A bit-string slice is nor referable. */
1387 return chill_location (TREE_OPERAND (ref, 0)) == 0 ? 0 : 1;
1398 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
1399 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
1410 chill_referable (val)
1413 return chill_location (val) > 1;
1416 /* Make a copy of MODE, but with the given NOVELTY. */
1419 copy_novelty (novelty, mode)
1422 if (CH_NOVELTY (mode) != novelty)
1424 mode = copy_node (mode);
1425 TYPE_MAIN_VARIANT (mode) = mode;
1426 TYPE_NEXT_VARIANT (mode) = 0;
1427 TYPE_POINTER_TO (mode) = 0;
1428 TYPE_REFERENCE_TO (mode) = 0;
1429 SET_CH_NOVELTY (mode, novelty);
1437 struct mode_chain *prev;
1441 /* Tests if MODE1 and MODE2 are SIMILAR.
1442 This is more or less as defined in the Blue Book, though
1443 see FIXME for parts that are unfinished.
1444 CHAIN is used to catch infinite recursion: It is a list of pairs
1445 of mode arguments to calls to chill_similar "outer" to this call. */
1448 chill_similar (mode1, mode2, chain)
1450 struct mode_chain *chain;
1452 int varying1, varying2;
1454 struct mode_chain *link, node;
1455 if (mode1 == NULL_TREE || mode2 == NULL_TREE)
1458 while (TREE_CODE (mode1) == REFERENCE_TYPE)
1459 mode1 = TREE_TYPE (mode1);
1460 while (TREE_CODE (mode2) == REFERENCE_TYPE)
1461 mode2 = TREE_TYPE (mode2);
1463 /* Range modes are similar to their parent types. */
1464 while (TREE_CODE (mode1) == INTEGER_TYPE && TREE_TYPE (mode1) != NULL_TREE)
1465 mode1 = TREE_TYPE (mode1);
1466 while (TREE_CODE (mode2) == INTEGER_TYPE && TREE_TYPE (mode2) != NULL_TREE)
1467 mode2 = TREE_TYPE (mode2);
1470 /* see Z.200 sections 12.1.2.2 and 13.2 - all integer precisions
1471 are similar to INT and to each other */
1472 if (mode1 == mode2 ||
1473 (TREE_CODE (mode1) == INTEGER_TYPE && TREE_CODE (mode2) == INTEGER_TYPE))
1476 /* This guards against certain kinds of recursion.
1478 SYNMODE a = STRUCT ( next REF a );
1479 SYNMODE b = STRUCT ( next REF b );
1480 These moes are similar, but will get an infite recursion trying
1481 to prove that. So, if we are recursing, assume the moes are similar.
1482 If they are not, we'll find some other discrepancy. */
1483 for (link = chain; link != NULL; link = link->prev)
1485 if (link->mode1 == mode1 && link->mode2 == mode2)
1493 varying1 = chill_varying_type_p (mode1);
1494 varying2 = chill_varying_type_p (mode2);
1495 /* FIXME: This isn't quite strict enough. */
1496 if ((varying1 && varying2)
1497 || (varying1 && TREE_CODE (mode2) == ARRAY_TYPE)
1498 || (varying2 && TREE_CODE (mode1) == ARRAY_TYPE))
1501 if (TREE_CODE(mode1) != TREE_CODE(mode2))
1503 if (flag_old_strings)
1505 /* The recursion is to handle varying strings. */
1506 if ((TREE_CODE (mode1) == CHAR_TYPE
1507 && CH_SIMILAR (mode2, string_one_type_node))
1508 || (TREE_CODE (mode2) == CHAR_TYPE
1509 && CH_SIMILAR (mode1, string_one_type_node)))
1511 if ((TREE_CODE (mode1) == BOOLEAN_TYPE
1512 && CH_SIMILAR (mode2, bitstring_one_type_node))
1513 || (TREE_CODE (mode2) == BOOLEAN_TYPE
1514 && CH_SIMILAR (mode1, bitstring_one_type_node)))
1517 if (TREE_CODE (mode1) == FUNCTION_TYPE
1518 && TREE_CODE (mode2) == POINTER_TYPE
1519 && TREE_CODE (TREE_TYPE (mode2)) == FUNCTION_TYPE)
1520 mode2 = TREE_TYPE (mode2);
1521 else if (TREE_CODE (mode2) == FUNCTION_TYPE
1522 && TREE_CODE (mode1) == POINTER_TYPE
1523 && TREE_CODE (TREE_TYPE (mode1)) == FUNCTION_TYPE)
1524 mode1 = TREE_TYPE (mode1);
1529 if (CH_IS_BUFFER_MODE (mode1) && CH_IS_BUFFER_MODE (mode2))
1531 tree len1 = max_queue_size (mode1);
1532 tree len2 = max_queue_size (mode2);
1533 return tree_int_cst_equal (len1, len2);
1535 else if (CH_IS_EVENT_MODE (mode1) && CH_IS_EVENT_MODE (mode2))
1537 tree len1 = max_queue_size (mode1);
1538 tree len2 = max_queue_size (mode2);
1539 return tree_int_cst_equal (len1, len2);
1541 else if (CH_IS_ACCESS_MODE (mode1) && CH_IS_ACCESS_MODE (mode2))
1543 tree index1 = access_indexmode (mode1);
1544 tree index2 = access_indexmode (mode2);
1545 tree record1 = access_recordmode (mode1);
1546 tree record2 = access_recordmode (mode2);
1547 if (! chill_read_compatible (index1, index2))
1549 return chill_read_compatible (record1, record2);
1551 switch ((enum chill_tree_code)TREE_CODE (mode1))
1558 if (TYPE_VALUES (mode1) == TYPE_VALUES (mode2))
1562 /* FIXME: This is more strict than z.200, which seems to
1563 allow the elements to be reordered, as long as they
1564 have the same values. */
1566 tree field1 = TYPE_VALUES (mode1);
1567 tree field2 = TYPE_VALUES (mode2);
1569 while (field1 != NULL_TREE && field2 != NULL_TREE)
1571 tree value1, value2;
1572 /* Check that the names are equal. */
1573 if (TREE_PURPOSE (field1) != TREE_PURPOSE (field2))
1576 value1 = TREE_VALUE (field1);
1577 value2 = TREE_VALUE (field2);
1578 /* This isn't quite sufficient in general, but will do ... */
1579 /* Note that proclaim_decl can cause the SET modes to be
1580 compared BEFORE they are satisfied, but otherwise
1581 chill_similar is mostly called after satisfaction. */
1582 if (TREE_CODE (value1) == CONST_DECL)
1583 value1 = DECL_INITIAL (value1);
1584 if (TREE_CODE (value2) == CONST_DECL)
1585 value2 = DECL_INITIAL (value2);
1586 /* Check that the values are equal or both NULL. */
1587 if (!(value1 == NULL_TREE && value2 == NULL_TREE)
1588 && (value1 == NULL_TREE || value2 == NULL_TREE
1589 || ! tree_int_cst_equal (value1, value2)))
1591 field1 = TREE_CHAIN (field1);
1592 field2 = TREE_CHAIN (field2);
1594 return field1 == NULL_TREE && field2 == NULL_TREE;
1597 /* check for bit strings */
1598 if (CH_BOOLS_TYPE_P (mode1))
1599 return CH_BOOLS_TYPE_P (mode2);
1600 if (CH_BOOLS_TYPE_P (mode2))
1601 return CH_BOOLS_TYPE_P (mode1);
1602 /* both are powerset modes */
1603 return CH_EQUIVALENT (TYPE_DOMAIN (mode1), TYPE_DOMAIN (mode2));
1606 /* Are the referenced modes equivalent? */
1607 return !integer_zerop (chill_equivalent (TREE_TYPE (mode1),
1612 /* char for char strings */
1613 if (CH_CHARS_TYPE_P (mode1))
1614 return CH_CHARS_TYPE_P (mode2);
1615 if (CH_CHARS_TYPE_P (mode2))
1616 return CH_CHARS_TYPE_P (mode1);
1618 if (CH_V_EQUIVALENT (TYPE_DOMAIN (mode1), TYPE_DOMAIN (mode2))
1619 /* Are the elements modes equivalent? */
1620 && !integer_zerop (chill_equivalent (TREE_TYPE (mode1),
1624 /* FIXME: Check that element layouts are equivalent */
1626 tree count1 = fold (build (MINUS_EXPR, sizetype,
1627 TYPE_MAX_VALUE (TYPE_DOMAIN (mode1)),
1628 TYPE_MIN_VALUE (TYPE_DOMAIN (mode1))));
1629 tree count2 = fold (build (MINUS_EXPR, sizetype,
1630 TYPE_MAX_VALUE (TYPE_DOMAIN (mode2)),
1631 TYPE_MIN_VALUE (TYPE_DOMAIN (mode2))));
1632 tree cond = build_compare_discrete_expr (EQ_EXPR, count1, count2);
1633 if (TREE_CODE (cond) == INTEGER_CST)
1634 return !integer_zerop (cond);
1638 extern int ignoring;
1641 && current_function_decl)
1651 for (t1 = TYPE_FIELDS (mode1), t2 = TYPE_FIELDS (mode2);
1652 t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1654 if (TREE_CODE (t1) != TREE_CODE (t2))
1656 /* Are the field modes equivalent? */
1657 if (integer_zerop (chill_equivalent (TREE_TYPE (t1),
1665 if (!chill_l_equivalent (TREE_TYPE (mode1), TREE_TYPE (mode2), &node))
1667 for (t1 = TYPE_ARG_TYPES (mode1), t2 = TYPE_ARG_TYPES (mode2);
1668 t1 != NULL_TREE && t2 != NULL_TREE;
1669 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1671 tree attr1 = TREE_PURPOSE (t1)
1672 ? TREE_PURPOSE (t1) : ridpointers[(int) RID_IN];
1673 tree attr2 = TREE_PURPOSE (t2)
1674 ? TREE_PURPOSE (t2) : ridpointers[(int) RID_IN];
1677 if (!chill_l_equivalent (TREE_VALUE (t1), TREE_VALUE (t2), &node))
1680 if (t1 != t2) /* Both NULL_TREE */
1682 /* check list of exception names */
1683 t1 = TYPE_RAISES_EXCEPTIONS (mode1);
1684 t2 = TYPE_RAISES_EXCEPTIONS (mode2);
1685 if (t1 == NULL_TREE && t2 != NULL_TREE)
1687 if (t1 != NULL_TREE && t2 == NULL_TREE)
1689 if (list_length (t1) != list_length (t2))
1691 while (t1 != NULL_TREE)
1693 if (value_member (TREE_VALUE (t1), t2) == NULL_TREE)
1695 t1 = TREE_CHAIN (t1);
1697 /* FIXME: Should also check they have the same RECURSIVITY */
1703 /* Need to handle row modes, instance modes,
1704 association modes, access modes, text modes,
1705 duration modes, absolute time modes, structure modes,
1706 parameterized structure modes */
1712 /* Return a node that is true iff MODE1 and MODE2 are equivalent.
1713 This is normally boolean_true_node or boolean_false_node,
1714 but can be dynamic for dynamic types.
1715 CHAIN is as for chill_similar. */
1718 chill_equivalent (mode1, mode2, chain)
1720 struct mode_chain *chain;
1722 int varying1, varying2;
1723 int is_string1, is_string2;
1724 tree base_mode1, base_mode2;
1726 /* Are the modes v-equivalent? */
1728 if (!chill_similar (mode1, mode2, chain)
1729 || CH_NOVELTY(mode1) != CH_NOVELTY(mode2))
1730 return boolean_false_node;
1732 if (!chill_similar (mode1, mode2, chain))
1733 return boolean_false_node;
1734 else if (TREE_CODE (mode2) == FUNCTION_TYPE
1735 && TREE_CODE (mode1) == POINTER_TYPE
1736 && TREE_CODE (TREE_TYPE (mode1)) == FUNCTION_TYPE)
1737 /* don't check novelty in this case to avoid error in case of
1738 NEWMODE'd proceduremode gets assigned a function */
1739 return boolean_true_node;
1740 else if (CH_NOVELTY(mode1) != CH_NOVELTY(mode2))
1741 return boolean_false_node;
1743 varying1 = chill_varying_type_p (mode1);
1744 varying2 = chill_varying_type_p (mode2);
1746 if (varying1 != varying2)
1747 return boolean_false_node;
1748 base_mode1 = varying1 ? CH_VARYING_ARRAY_TYPE (mode1) : mode1;
1749 base_mode2 = varying2 ? CH_VARYING_ARRAY_TYPE (mode2) : mode2;
1750 is_string1 = CH_STRING_TYPE_P (base_mode1);
1751 is_string2 = CH_STRING_TYPE_P (base_mode2);
1752 if (is_string1 || is_string2)
1754 if (is_string1 != is_string2)
1755 return boolean_false_node;
1756 return fold (build (EQ_EXPR, boolean_type_node,
1757 TYPE_SIZE (base_mode1),
1758 TYPE_SIZE (base_mode2)));
1761 /* && some more stuff FIXME! */
1762 if (TREE_CODE(mode1) == INTEGER_TYPE || TREE_CODE(mode2) == INTEGER_TYPE)
1764 if (TREE_CODE(mode1) != INTEGER_TYPE || TREE_CODE(mode2) != INTEGER_TYPE)
1765 return boolean_false_node;
1766 /* If one is a range, the other has to be a range. */
1767 if ((TREE_TYPE (mode1) != NULL_TREE) != (TREE_TYPE (mode2) != NULL_TREE))
1768 return boolean_false_node;
1769 if (TYPE_PRECISION (mode1) != TYPE_PRECISION (mode2))
1770 return boolean_false_node;
1771 if (!tree_int_cst_equal (TYPE_MIN_VALUE (mode1), TYPE_MIN_VALUE (mode2)))
1772 return boolean_false_node;
1773 if (!tree_int_cst_equal (TYPE_MAX_VALUE (mode1), TYPE_MAX_VALUE (mode2)))
1774 return boolean_false_node;
1776 return boolean_true_node;
1780 chill_l_equivalent (mode1, mode2, chain)
1782 struct mode_chain *chain;
1784 /* Are the modes equivalent? */
1785 if (integer_zerop (chill_equivalent (mode1, mode2, chain)))
1787 if (TYPE_READONLY (mode1) != TYPE_READONLY (mode2))
1790 ... other conditions ...;
1795 /* See Z200 12.1.2.12 */
1798 chill_read_compatible (modeM, modeN)
1801 while (TREE_CODE (modeM) == REFERENCE_TYPE)
1802 modeM = TREE_TYPE (modeM);
1803 while (TREE_CODE (modeN) == REFERENCE_TYPE)
1804 modeN = TREE_TYPE (modeN);
1806 if (!CH_EQUIVALENT (modeM, modeN))
1808 if (TYPE_READONLY (modeN))
1810 if (!TYPE_READONLY (modeM))
1812 if (CH_IS_BOUND_REFERENCE_MODE (modeM)
1813 && CH_IS_BOUND_REFERENCE_MODE (modeN))
1815 return chill_l_equivalent (TREE_TYPE (modeM), TREE_TYPE (modeN), 0);
1824 /* Tests if MODE is compatible with the class of EXPR.
1825 Cfr. Chill Blue Book 12.1.2.15. */
1828 chill_compatible (expr, mode)
1831 struct ch_class class;
1833 if (expr == NULL_TREE || TREE_CODE (expr) == ERROR_MARK)
1835 if (mode == NULL_TREE || TREE_CODE (mode) == ERROR_MARK)
1838 while (TREE_CODE (mode) == REFERENCE_TYPE)
1839 mode = TREE_TYPE (mode);
1841 if (TREE_TYPE (expr) == NULL_TREE)
1843 if (TREE_CODE (expr) == CONSTRUCTOR)
1844 return TREE_CODE (mode) == RECORD_TYPE
1845 || ((TREE_CODE (mode) == SET_TYPE || TREE_CODE (mode) == ARRAY_TYPE)
1846 && ! TYPE_STRING_FLAG (mode));
1848 return TREE_CODE (expr) == CASE_EXPR || TREE_CODE (expr) == COND_EXPR;
1851 class = chill_expr_class (expr);
1857 return CH_IS_REFERENCE_MODE (mode) || CH_IS_PROCEDURE_MODE (mode)
1858 || CH_IS_INSTANCE_MODE (mode);
1859 case CH_VALUE_CLASS:
1860 if (CH_HAS_REFERENCING_PROPERTY (mode))
1861 return CH_RESTRICTABLE_TO(mode, class.mode);
1863 return CH_V_EQUIVALENT(mode, class.mode);
1864 case CH_DERIVED_CLASS:
1865 return CH_SIMILAR (class.mode, mode);
1866 case CH_REFERENCE_CLASS:
1867 if (!CH_IS_REFERENCE_MODE (mode))
1871 if (class.mode is a row mode)
1873 else if (class.mode is not a static mode)
1874 return 0; /* is this possible? FIXME */
1876 return !CH_IS_BOUND_REFERENCE_MODE(mode)
1877 || CH_READ_COMPATIBLE (TREE_TYPE (mode), class.mode);
1879 return 0; /* ERROR! */
1882 /* Tests if the class of of EXPR1 and EXPR2 are compatible.
1883 Cfr. Chill Blue Book 12.1.2.16. */
1886 chill_compatible_classes (expr1, expr2)
1889 struct ch_class temp;
1890 struct ch_class class1, class2;
1891 class1 = chill_expr_class (expr1);
1892 class2 = chill_expr_class (expr2);
1894 switch (class1.kind)
1899 switch (class2.kind)
1903 case CH_REFERENCE_CLASS:
1905 case CH_VALUE_CLASS:
1906 case CH_DERIVED_CLASS:
1909 case CH_REFERENCE_CLASS:
1910 switch (class2.kind)
1915 case CH_REFERENCE_CLASS:
1916 return CH_EQUIVALENT (class1.mode, class2.mode);
1917 case CH_VALUE_CLASS:
1919 case CH_DERIVED_CLASS:
1922 case CH_DERIVED_CLASS:
1923 switch (class2.kind)
1927 case CH_VALUE_CLASS:
1928 case CH_DERIVED_CLASS:
1929 return CH_SIMILAR (class1.mode, class2.mode);
1933 case CH_REFERENCE_CLASS:
1936 case CH_VALUE_CLASS:
1937 switch (class2.kind)
1941 case CH_DERIVED_CLASS:
1942 return CH_SIMILAR (class1.mode, class2.mode);
1943 case CH_VALUE_CLASS:
1944 return CH_V_EQUIVALENT (class1.mode, class2.mode);
1948 case CH_REFERENCE_CLASS:
1949 temp = class1; class1 = class2; class2 = temp;
1954 /* The Null class is Compatible with the M-derived class or M-value class
1955 if and only if M is a reference mdoe, procedure mode or instance mode.*/
1956 return CH_IS_REFERENCE_MODE (class2.mode)
1957 || CH_IS_PROCEDURE_MODE (class2.mode)
1958 || CH_IS_INSTANCE_MODE (class2.mode);
1961 /* The M-reference class is compatible with the N-value class if and
1962 only if N is a reference mode and ... */
1963 if (!CH_IS_REFERENCE_MODE (class2.mode))
1965 if (1) /* If M is a static mode - FIXME */
1967 if (!CH_IS_BOUND_REFERENCE_MODE (class2.mode))
1969 if (CH_EQUIVALENT (TREE_TYPE (class2.mode), class1.mode))
1972 /* If N is a row mode whose .... FIXME */
1976 /* Cfr. Blue Book 12.1.1.6, with some "extensions." */
1979 chill_root_mode (mode)
1982 /* Reference types are not user-visible types.
1983 This seems like a good place to get rid of them. */
1984 if (TREE_CODE (mode) == REFERENCE_TYPE)
1985 mode = TREE_TYPE (mode);
1987 while (TREE_CODE (mode) == INTEGER_TYPE && TREE_TYPE (mode) != NULL_TREE)
1988 mode = TREE_TYPE (mode); /* a sub-range */
1990 /* This extension in not in the Blue Book - which only has a
1991 single Integer type.
1992 We should probably use chill_integer_type_node rather
1993 than integer_type_node, but that is likely to bomb.
1994 At some point, these will become the same, I hope. FIXME */
1995 if (TREE_CODE (mode) == INTEGER_TYPE
1996 && TYPE_PRECISION (mode) < TYPE_PRECISION (integer_type_node)
1997 && CH_NOVELTY (mode) == NULL_TREE)
1998 mode = integer_type_node;
2000 if (TREE_CODE (mode) == FUNCTION_TYPE)
2001 return build_pointer_type (mode);
2006 /* Cfr. Blue Book 12.1.1.7. */
2009 chill_resulting_mode (mode1, mode2)
2012 mode1 = CH_ROOT_MODE (mode1);
2013 mode2 = CH_ROOT_MODE (mode2);
2014 if (chill_varying_type_p (mode1))
2016 if (chill_varying_type_p (mode2))
2021 /* Cfr. Blue Book (z200, 1988) 12.1.1.7 Resulting class. */
2024 chill_resulting_class (class1, class2)
2025 struct ch_class class1, class2;
2027 struct ch_class class;
2028 switch (class1.kind)
2030 case CH_VALUE_CLASS:
2031 switch (class2.kind)
2033 case CH_DERIVED_CLASS:
2035 class.kind = CH_VALUE_CLASS;
2036 class.mode = CH_ROOT_MODE (class1.mode);
2038 case CH_VALUE_CLASS:
2039 class.kind = CH_VALUE_CLASS;
2041 = CH_ROOT_MODE (CH_RESULTING_MODE (class1.mode, class2.mode));
2047 case CH_DERIVED_CLASS:
2048 switch (class2.kind)
2050 case CH_VALUE_CLASS:
2051 class.kind = CH_VALUE_CLASS;
2052 class.mode = CH_ROOT_MODE (class2.mode);
2054 case CH_DERIVED_CLASS:
2055 class.kind = CH_DERIVED_CLASS;
2056 class.mode = CH_RESULTING_MODE (class1.mode, class2.mode);
2059 class.kind = CH_DERIVED_CLASS;
2060 class.mode = CH_ROOT_MODE (class1.mode);
2067 switch (class2.kind)
2069 case CH_VALUE_CLASS:
2070 class.kind = CH_VALUE_CLASS;
2071 class.mode = CH_ROOT_MODE (class2.mode);
2074 class.kind = CH_ALL_CLASS;
2075 class.mode = NULL_TREE;
2077 case CH_DERIVED_CLASS:
2078 class.kind = CH_DERIVED_CLASS;
2079 class.mode = CH_ROOT_MODE (class2.mode);
2088 error ("internal error in chill_root_resulting_mode");
2089 class.kind = CH_VALUE_CLASS;
2090 class.mode = CH_ROOT_MODE (class1.mode);
2096 * See Z.200, section 6.3, static conditions. This function
2097 * returns bool_false_node if the condition is not met at compile time,
2098 * bool_true_node if the condition is detectably met at compile time
2099 * an expression if a runtime check would be required or was generated.
2100 * It should only be called with string modes and values.
2103 string_assignment_condition (lhs_mode, rhs_value)
2104 tree lhs_mode, rhs_value;
2106 tree lhs_size, rhs_size, cond;
2107 tree rhs_mode = TREE_TYPE (rhs_value);
2108 int lhs_varying = chill_varying_type_p (lhs_mode);
2111 lhs_size = size_in_bytes (CH_VARYING_ARRAY_TYPE (lhs_mode));
2112 else if (CH_BOOLS_TYPE_P (lhs_mode))
2113 lhs_size = TYPE_MAX_VALUE (TYPE_DOMAIN (lhs_mode));
2115 lhs_size = size_in_bytes (lhs_mode);
2116 lhs_size = convert (chill_unsigned_type_node, lhs_size);
2118 if (rhs_mode && TREE_CODE (rhs_mode) == REFERENCE_TYPE)
2119 rhs_mode = TREE_TYPE (rhs_mode);
2120 if (rhs_mode == NULL_TREE)
2122 /* actually, count constructor's length */
2125 else if (chill_varying_type_p (rhs_mode))
2126 rhs_size = build_component_ref (rhs_value, var_length_id);
2127 else if (CH_BOOLS_TYPE_P (rhs_mode))
2128 rhs_size = TYPE_MAX_VALUE (TYPE_DOMAIN (rhs_mode));
2130 rhs_size = size_in_bytes (rhs_mode);
2131 rhs_size = convert (chill_unsigned_type_node, rhs_size);
2133 /* validity condition */
2134 cond = fold (build (lhs_varying ? GE_EXPR : EQ_EXPR,
2135 boolean_type_node, lhs_size, rhs_size));
2140 * take a basic CHILL type and wrap it in a VARYING structure.
2141 * Be sure the length field is initialized. Return the wrapper.
2144 build_varying_struct (type)
2147 tree decl1, decl2, result;
2149 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
2150 return error_mark_node;
2152 decl1 = build_decl (FIELD_DECL, var_length_id, chill_integer_type_node);
2153 decl2 = build_decl (FIELD_DECL, var_data_id, type);
2154 TREE_CHAIN (decl1) = decl2;
2155 TREE_CHAIN (decl2) = NULL_TREE;
2156 result = build_chill_struct_type (decl1);
2158 /* mark this so we don't complain about missing initializers.
2159 It's fine for a VARYING array to be partially initialized.. */
2160 C_TYPE_VARIABLE_SIZE(type) = 1;
2166 * This is the struct type that forms the runtime initializer
2167 * list. There's at least one of these generated per module.
2168 * It's attached to the global initializer list by the module's
2169 * 'constructor' code. Should only be called in pass 2.
2172 build_init_struct ()
2174 tree decl1, decl2, result;
2175 /* We temporarily reset the maximum_field_alignment to zero so the
2176 compiler's init data structures can be compatible with the
2177 run-time system, even when we're compiling with -fpack. */
2178 extern int maximum_field_alignment;
2179 int save_maximum_field_alignment = maximum_field_alignment;
2180 maximum_field_alignment = 0;
2182 decl1 = build_decl (FIELD_DECL, get_identifier ("__INIT_ENTRY"),
2183 build_chill_pointer_type (
2184 build_function_type (void_type_node, NULL_TREE)));
2186 decl2 = build_decl (FIELD_DECL, get_identifier ("__INIT_NEXT"),
2187 build_chill_pointer_type (void_type_node));
2189 TREE_CHAIN (decl1) = decl2;
2190 TREE_CHAIN (decl2) = NULL_TREE;
2191 result = build_chill_struct_type (decl1);
2192 maximum_field_alignment = save_maximum_field_alignment;
2198 * Return 1 if the given type is a single-bit boolean set,
2199 * in which the domain's min and max values
2201 * 0 if not. This can become a macro later..
2204 ch_singleton_set (type)
2207 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
2209 if (TREE_CODE (type) != SET_TYPE)
2211 if (TREE_TYPE (type) == NULL_TREE
2212 || TREE_CODE (TREE_TYPE (type)) != BOOLEAN_TYPE)
2214 if (TYPE_DOMAIN (type) == NULL_TREE)
2216 if (! tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (type)),
2219 if (! tree_int_cst_equal (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
2225 /* return non-zero if TYPE is a compiler-generated VARYING
2226 array of some base type */
2228 chill_varying_type_p (type)
2231 if (type == NULL_TREE)
2233 if (TREE_CODE (type) != RECORD_TYPE)
2235 if (TYPE_FIELDS (type) == NULL_TREE
2236 || TREE_CHAIN (TYPE_FIELDS (type)) == NULL_TREE)
2238 if (DECL_NAME (TYPE_FIELDS (type)) != var_length_id)
2240 if (DECL_NAME (TREE_CHAIN (TYPE_FIELDS (type))) != var_data_id)
2242 if (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))) != NULL_TREE)
2247 /* return non-zero if TYPE is a compiler-generated VARYING
2250 chill_varying_string_type_p (type)
2255 if (!chill_varying_type_p (type))
2258 var_data_type = CH_VARYING_ARRAY_TYPE (type);
2259 return CH_CHARS_TYPE_P (var_data_type);
2262 /* swiped from c-typeck.c */
2263 /* Build an assignment expression of lvalue LHS from value RHS. */
2266 build_chill_modify_expr (lhs, rhs)
2269 register tree result;
2272 tree lhstype = TREE_TYPE (lhs);
2274 /* Avoid duplicate error messages from operands that had errors. */
2275 if (lhs == NULL_TREE || TREE_CODE (lhs) == ERROR_MARK || rhs == NULL_TREE || TREE_CODE (rhs) == ERROR_MARK)
2276 return error_mark_node;
2278 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2279 /* Do not use STRIP_NOPS here. We do not want an enumerator
2280 whose value is 0 to count as a null pointer constant. */
2281 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
2282 rhs = TREE_OPERAND (rhs, 0);
2285 /* Handle a cast used as an "lvalue".
2286 We have already performed any binary operator using the value as cast.
2287 Now convert the result to the cast type of the lhs,
2288 and then true type of the lhs and store it there;
2289 then convert result back to the cast type to be the value
2290 of the assignment. */
2292 switch (TREE_CODE (lhs))
2297 case FIX_TRUNC_EXPR:
2298 case FIX_FLOOR_EXPR:
2299 case FIX_ROUND_EXPR:
2302 tree inner_lhs = TREE_OPERAND (lhs, 0);
2304 result = build_chill_modify_expr (inner_lhs,
2305 convert (TREE_TYPE (inner_lhs),
2306 convert (lhstype, rhs)));
2307 pedantic_lvalue_warning (CONVERT_EXPR);
2308 return convert (TREE_TYPE (lhs), result);
2312 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
2313 Reject anything strange now. */
2315 if (!lvalue_or_else (lhs, "assignment"))
2316 return error_mark_node;
2318 /* FIXME: need to generate a RANGEFAIL if the RHS won't
2319 fit into the LHS. */
2321 if (TREE_CODE (lhs) != VAR_DECL
2322 && ((TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE &&
2323 (TREE_TYPE (rhs) && TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE)) ||
2324 chill_varying_type_p (TREE_TYPE (lhs)) ||
2325 chill_varying_type_p (TREE_TYPE (rhs))))
2327 int lhs_varying = chill_varying_type_p (TREE_TYPE (lhs));
2328 int rhs_varying = chill_varying_type_p (TREE_TYPE (rhs));
2330 /* point at actual RHS data's type */
2331 tree rhs_data_type = rhs_varying ?
2332 CH_VARYING_ARRAY_TYPE (TREE_TYPE (rhs)) :
2335 /* point at actual LHS data's type */
2336 tree lhs_data_type = lhs_varying ?
2337 CH_VARYING_ARRAY_TYPE (TREE_TYPE (lhs)) :
2340 int lhs_bytes = int_size_in_bytes (lhs_data_type);
2341 int rhs_bytes = int_size_in_bytes (rhs_data_type);
2343 /* if both sides not varying, and sizes not dynamically
2344 computed, sizes must *match* */
2345 if (! lhs_varying && ! rhs_varying && lhs_bytes != rhs_bytes
2346 && lhs_bytes > 0 && rhs_bytes > 0)
2348 error ("string lengths not equal");
2349 return error_mark_node;
2351 /* Must have enough space on LHS for static size of RHS */
2353 if (lhs_bytes > 0 && rhs_bytes > 0
2354 && lhs_bytes < rhs_bytes)
2358 /* FIXME: generate runtime test for room */
2363 error ("can't do ARRAY assignment - too large");
2364 return error_mark_node;
2369 /* now we know the RHS will fit in LHS, build trees for the
2370 emit_block_move parameters */
2373 rhs = convert (TREE_TYPE (lhs), rhs);
2377 rhs = build_component_ref (rhs, var_data_id);
2379 if (! mark_addressable (rhs))
2381 error ("rhs of array assignment is not addressable");
2382 return error_mark_node;
2385 lhs = force_addr_of (lhs);
2386 rhs = build1 (ADDR_EXPR, const_ptr_type_node, rhs);
2388 build_chill_function_call (lookup_name (get_identifier ("memmove")),
2389 tree_cons (NULL_TREE, lhs,
2390 tree_cons (NULL_TREE, rhs,
2391 tree_cons (NULL_TREE, size_in_bytes (rhs_data_type),
2396 result = build (MODIFY_EXPR, lhstype, lhs, rhs);
2397 TREE_SIDE_EFFECTS (result) = 1;
2402 /* Constructors for pointer, array and function types.
2403 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
2404 constructed by language-dependent code, not here.) */
2406 /* Construct, lay out and return the type of pointers to TO_TYPE.
2407 If such a type has already been constructed, reuse it. */
2410 make_chill_pointer_type (to_type, code)
2412 enum tree_code code; /* POINTER_TYPE or REFERENCE_TYPE */
2414 extern struct obstack *current_obstack;
2415 extern struct obstack *saveable_obstack;
2416 extern struct obstack permanent_obstack;
2418 register struct obstack *ambient_obstack = current_obstack;
2419 register struct obstack *ambient_saveable_obstack = saveable_obstack;
2421 /* If TO_TYPE is permanent, make this permanent too. */
2422 if (TREE_PERMANENT (to_type))
2424 current_obstack = &permanent_obstack;
2425 saveable_obstack = &permanent_obstack;
2428 t = make_node (code);
2429 TREE_TYPE (t) = to_type;
2431 current_obstack = ambient_obstack;
2432 saveable_obstack = ambient_saveable_obstack;
2438 build_chill_pointer_type (to_type)
2441 int is_type_node = TREE_CODE_CLASS (TREE_CODE (to_type)) == 't';
2442 register tree t = is_type_node ? TYPE_POINTER_TO (to_type) : NULL_TREE;
2444 /* First, if we already have a type for pointers to TO_TYPE, use it. */
2449 /* We need a new one. */
2450 t = make_chill_pointer_type (to_type, POINTER_TYPE);
2452 /* Lay out the type. This function has many callers that are concerned
2453 with expression-construction, and this simplifies them all.
2454 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
2455 if ((is_type_node && (TYPE_SIZE (to_type) != NULL_TREE))
2458 /* Record this type as the pointer to TO_TYPE. */
2459 TYPE_POINTER_TO (to_type) = t;
2467 build_chill_reference_type (to_type)
2470 int is_type_node = TREE_CODE_CLASS (TREE_CODE (to_type)) == 't';
2471 register tree t = is_type_node ? TYPE_REFERENCE_TO (to_type) : NULL_TREE;
2473 /* First, if we already have a type for references to TO_TYPE, use it. */
2478 /* We need a new one. */
2479 t = make_chill_pointer_type (to_type, REFERENCE_TYPE);
2481 /* Lay out the type. This function has many callers that are concerned
2482 with expression-construction, and this simplifies them all.
2483 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
2484 if ((is_type_node && (TYPE_SIZE (to_type) != NULL_TREE))
2487 /* Record this type as the reference to TO_TYPE. */
2488 TYPE_REFERENCE_TO (to_type) = t;
2490 CH_NOVELTY (t) = CH_NOVELTY (to_type);
2497 make_chill_range_type (type, lowval, highval)
2498 tree type, lowval, highval;
2500 register tree itype = make_node (INTEGER_TYPE);
2501 TREE_TYPE (itype) = type;
2502 TYPE_MIN_VALUE (itype) = lowval;
2503 TYPE_MAX_VALUE (itype) = highval;
2508 layout_chill_range_type (rangetype, must_be_const)
2512 tree type = TREE_TYPE (rangetype);
2513 tree lowval = TYPE_MIN_VALUE (rangetype);
2514 tree highval = TYPE_MAX_VALUE (rangetype);
2517 if (TYPE_SIZE (rangetype) != NULL_TREE)
2521 if (type == ridpointers[(int) RID_BIN])
2525 /* make a range out of it */
2526 if (TREE_CODE (highval) != INTEGER_CST)
2528 error ("non-constant expression for BIN");
2529 return error_mark_node;
2531 binsize = TREE_INT_CST_LOW (highval);
2534 error ("expression for BIN must not be negative");
2535 return error_mark_node;
2539 error ("cannot process BIN (>32)");
2540 return error_mark_node;
2542 type = ridpointers [(int) RID_RANGE];
2543 lowval = integer_zero_node;
2544 highval = build_int_2 ((1 << binsize) - 1, 0);
2547 if (TREE_CODE (lowval) == ERROR_MARK ||
2548 TREE_CODE (highval) == ERROR_MARK)
2549 return error_mark_node;
2551 if (!CH_COMPATIBLE_CLASSES (lowval, highval))
2553 error ("bounds of range are not compatible");
2554 return error_mark_node;
2557 if (type == string_index_type_dummy)
2559 if (TREE_CODE (highval) == INTEGER_CST
2560 && compare_int_csts (LT_EXPR, highval, integer_minus_one_node))
2562 error ("negative string length");
2563 highval = integer_minus_one_node;
2565 if (compare_int_csts (EQ_EXPR, highval, integer_minus_one_node))
2566 type = integer_type_node;
2569 TREE_TYPE (rangetype) = type;
2571 else if (type == ridpointers[(int) RID_RANGE])
2573 /* This isn't 100% right, since the Blue Book definition
2574 uses Resulting Class, rather than Resulting Mode,
2575 but it's close enough. */
2576 type = CH_ROOT_RESULTING_CLASS (lowval, highval).mode;
2578 /* The default TYPE is the type of the constants -
2579 except if the constants are integers, we choose an
2580 integer type that fits. */
2581 if (TREE_CODE (type) == INTEGER_TYPE
2582 && TREE_CODE (lowval) == INTEGER_CST
2583 && TREE_CODE (highval) == INTEGER_CST)
2585 /* The logic of this code has been copied from finish_enum
2586 in c-decl.c. FIXME duplication! */
2588 HOST_WIDE_INT maxvalue = TREE_INT_CST_LOW (highval);
2589 HOST_WIDE_INT minvalue = TREE_INT_CST_LOW (lowval);
2590 if (TREE_INT_CST_HIGH (lowval) >= 0
2591 ? tree_int_cst_lt (TYPE_MAX_VALUE (unsigned_type_node), highval)
2592 : (tree_int_cst_lt (lowval, TYPE_MIN_VALUE (integer_type_node))
2593 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), highval)))
2594 precision = TYPE_PRECISION (long_long_integer_type_node);
2598 precision = floor_log2 (maxvalue) + 1;
2601 /* Compute number of bits to represent magnitude of a
2602 negative value. Add one to MINVALUE since range of
2603 negative numbers includes the power of two. */
2604 int negprecision = floor_log2 (-minvalue - 1) + 1;
2605 if (negprecision > precision)
2606 precision = negprecision;
2607 precision += 1; /* room for sign bit */
2613 type = type_for_size (precision, minvalue >= 0);
2616 TREE_TYPE (rangetype) = type;
2620 if (!CH_COMPATIBLE (lowval, type))
2622 error ("range's lower bound and parent mode don't match");
2623 return integer_type_node; /* an innocuous fake */
2625 if (!CH_COMPATIBLE (highval, type))
2627 error ("range's upper bound and parent mode don't match");
2628 return integer_type_node; /* an innocuous fake */
2632 if (TREE_CODE (type) == ERROR_MARK)
2634 else if (TREE_CODE_CLASS (TREE_CODE (type)) != 't')
2636 error ("making range from non-mode");
2637 return error_mark_node;
2640 if (TREE_CODE (lowval) == REAL_CST || TREE_CODE (highval) == REAL_CST)
2642 sorry ("floating point ranges");
2643 return integer_type_node; /* another fake */
2646 if (TREE_CODE (lowval) != INTEGER_CST || TREE_CODE (highval) != INTEGER_CST)
2650 error ("range mode has non-constant limits");
2654 else if (tree_int_cst_equal (lowval, integer_zero_node)
2655 && tree_int_cst_equal (highval, integer_minus_one_node))
2656 ; /* do nothing - this is the index type for an empty string */
2657 else if (compare_int_csts (LT_EXPR, highval, TYPE_MIN_VALUE (type)))
2659 error ("range's high bound < mode's low bound");
2662 else if (compare_int_csts (GT_EXPR, highval, TYPE_MAX_VALUE (type)))
2664 error ("range's high bound > mode's high bound");
2667 else if (compare_int_csts (LT_EXPR, highval, lowval))
2669 error ("range mode high bound < range mode low bound");
2672 else if (compare_int_csts (LT_EXPR, lowval, TYPE_MIN_VALUE (type)))
2674 error ("range's low bound < mode's low bound");
2677 else if (compare_int_csts (GT_EXPR, lowval, TYPE_MAX_VALUE (type)))
2679 error ("range's low bound > mode's high bound");
2685 lowval = TYPE_MIN_VALUE (type);
2689 highval = convert (type, highval);
2690 lowval = convert (type, lowval);
2691 TYPE_MIN_VALUE (rangetype) = lowval;
2692 TYPE_MAX_VALUE (rangetype) = highval;
2693 TYPE_PRECISION (rangetype) = TYPE_PRECISION (type);
2694 TYPE_MODE (rangetype) = TYPE_MODE (type);
2695 TYPE_SIZE (rangetype) = TYPE_SIZE (type);
2696 TYPE_SIZE_UNIT (rangetype) = TYPE_SIZE_UNIT (type);
2697 TYPE_ALIGN (rangetype) = TYPE_ALIGN (type);
2698 TREE_UNSIGNED (rangetype) = TREE_UNSIGNED (type);
2699 CH_NOVELTY (rangetype) = CH_NOVELTY (type);
2703 /* Build a _TYPE node that has range bounds associated with its values.
2704 TYPE is the base type for the range type. */
2706 build_chill_range_type (type, lowval, highval)
2707 tree type, lowval, highval;
2711 if (type == NULL_TREE)
2712 type = ridpointers[(int) RID_RANGE];
2713 else if (TREE_CODE (type) == ERROR_MARK)
2714 return error_mark_node;
2716 rangetype = make_chill_range_type (type, lowval, highval);
2718 rangetype = layout_chill_range_type (rangetype, 0);
2723 /* Build a CHILL array type, but with minimal checking etc. */
2726 build_simple_array_type (type, idx, layout)
2727 tree type, idx, layout;
2729 tree array_type = make_node (ARRAY_TYPE);
2730 TREE_TYPE (array_type) = type;
2731 TYPE_DOMAIN (array_type) = idx;
2732 TYPE_ATTRIBUTES (array_type) = layout;
2734 array_type = layout_chill_array_type (array_type);
2739 apply_chill_array_layout (array_type)
2742 tree layout, temp, what, element_type;
2743 int stepsize=0, word, start_bit=0, length, natural_length;
2744 int stepsize_specified;
2745 int start_bit_error = 0;
2746 int length_error = 0;
2748 layout = TYPE_ATTRIBUTES (array_type);
2749 if (layout == NULL_TREE)
2752 if (layout == integer_zero_node) /* NOPACK */
2754 TYPE_PACKED (array_type) = 0;
2758 /* Allow for the packing of 1 bit discrete modes at the bit level. */
2759 element_type = TREE_TYPE (array_type);
2760 if (discrete_type_p (element_type)
2761 && get_type_precision (TYPE_MIN_VALUE (element_type),
2762 TYPE_MAX_VALUE (element_type)) == 1)
2765 natural_length = TREE_INT_CST_LOW (TYPE_SIZE (element_type));
2767 if (layout == integer_one_node) /* PACK */
2769 if (natural_length == 1)
2770 TYPE_PACKED (array_type) = 1;
2774 /* The layout is a STEP (...).
2775 The current implementation restricts STEP specifications to be of the form
2776 STEP(POS(0,0,n),n) where n is the natural size of the element mode. */
2777 stepsize_specified = 0;
2778 temp = TREE_VALUE (layout);
2779 if (TREE_VALUE (temp) != NULL_TREE)
2781 if (TREE_CODE (TREE_VALUE (temp)) != INTEGER_CST)
2782 error ("Stepsize in STEP must be an integer constant");
2785 stepsize = TREE_INT_CST_LOW (TREE_VALUE (temp));
2787 error ("Stepsize in STEP must be > 0");
2789 stepsize_specified = 1;
2791 if (stepsize != natural_length)
2792 sorry ("Stepsize in STEP must be the natural width of "
2793 "the array element mode");
2797 temp = TREE_PURPOSE (temp);
2798 if (TREE_CODE (TREE_PURPOSE (temp)) != INTEGER_CST)
2799 error ("Starting word in POS must be an integer constant");
2802 word = TREE_INT_CST_LOW (TREE_PURPOSE (temp));
2804 error ("Starting word in POS must be >= 0");
2806 sorry ("Starting word in POS within STEP must be 0");
2809 length = natural_length;
2810 temp = TREE_VALUE (temp);
2811 if (temp != NULL_TREE)
2813 int wordsize = TYPE_PRECISION (chill_integer_type_node);
2814 if (TREE_CODE (TREE_PURPOSE (temp)) != INTEGER_CST)
2816 error ("Starting bit in POS must be an integer constant");
2817 start_bit_error = 1;
2821 start_bit = TREE_INT_CST_LOW (TREE_PURPOSE (temp));
2823 sorry ("Starting bit in POS within STEP must be 0");
2826 error ("Starting bit in POS must be >= 0");
2828 start_bit_error = 1;
2830 else if (start_bit >= wordsize)
2832 error ("Starting bit in POS must be < the width of a word");
2834 start_bit_error = 1;
2838 temp = TREE_VALUE (temp);
2839 if (temp != NULL_TREE)
2841 what = TREE_PURPOSE (temp);
2842 if (what == integer_zero_node)
2844 if (TREE_CODE (TREE_VALUE (temp)) != INTEGER_CST)
2846 error ("Length in POS must be an integer constant");
2851 length = TREE_INT_CST_LOW (TREE_VALUE (temp));
2853 error ("Length in POS must be > 0");
2858 if (TREE_CODE (TREE_VALUE (temp)) != INTEGER_CST)
2860 error ("End bit in POS must be an integer constant");
2865 int end_bit = TREE_INT_CST_LOW (TREE_VALUE (temp));
2866 if (end_bit < start_bit)
2868 error ("End bit in POS must be >= the start bit");
2869 end_bit = wordsize - 1;
2872 else if (end_bit >= wordsize)
2874 error ("End bit in POS must be < the width of a word");
2875 end_bit = wordsize - 1;
2878 else if (start_bit_error)
2881 length = end_bit - start_bit + 1;
2884 if (! length_error && length != natural_length)
2886 sorry ("The length specified on POS within STEP must be "
2887 "the natural length of the array element type");
2892 if (! length_error && stepsize_specified && stepsize < length)
2893 error ("Step size in STEP must be >= the length in POS");
2896 TYPE_PACKED (array_type) = 1;
2900 layout_chill_array_type (array_type)
2904 tree element_type = TREE_TYPE (array_type);
2906 if (TREE_CODE (element_type) == ARRAY_TYPE
2907 && TYPE_SIZE (element_type) == 0)
2908 layout_chill_array_type (element_type);
2910 itype = TYPE_DOMAIN (array_type);
2912 if (TREE_CODE (itype) == ERROR_MARK
2913 || TREE_CODE (element_type) == ERROR_MARK)
2914 return error_mark_node;
2916 /* do a lower/upper bound check. */
2917 if (TREE_CODE (itype) == INTEGER_CST)
2919 error ("array index must be a range, not a single integer");
2920 return error_mark_node;
2922 if (TREE_CODE_CLASS (TREE_CODE (itype)) != 't'
2923 || !discrete_type_p (itype))
2925 error ("array index is not a discrete mode");
2926 return error_mark_node;
2929 /* apply the array layout, if specified. */
2930 apply_chill_array_layout (array_type);
2931 TYPE_ATTRIBUTES (array_type) = NULL_TREE;
2933 /* Make sure TYPE_POINTER_TO (element_type) is filled in. */
2934 build_pointer_type (element_type);
2936 if (TYPE_SIZE (array_type) == 0)
2937 layout_type (array_type);
2939 if (TYPE_READONLY_PROPERTY (element_type))
2940 TYPE_FIELDS_READONLY (array_type) = 1;
2942 TYPE_ARRAY_MAX_SIZE (array_type) = size_in_bytes (array_type);
2946 /* Build a CHILL array type.
2948 TYPE is the element type of the array.
2949 IDXLIST is the list of dimensions of the array.
2950 VARYING_P is non-zero if the array is a varying array.
2951 LAYOUT is (NULL_TREE, integer_one_node, integer_zero_node, tree_list),
2952 meaning (default, pack, nopack, STEP (...) ). */
2954 build_chill_array_type (type, idxlist, varying_p, layouts)
2959 tree array_type = type;
2961 if (type == NULL_TREE || TREE_CODE (type) == ERROR_MARK)
2962 return error_mark_node;
2963 if (idxlist == NULL_TREE || TREE_CODE (idxlist) == ERROR_MARK)
2964 return error_mark_node;
2966 /* We have to walk down the list of index decls, building inner
2967 array types as we go. We need to reverse the list of layouts so that the
2968 first layout applies to the last index etc. */
2969 layouts = nreverse (layouts);
2970 for ( ; idxlist; idxlist = TREE_CHAIN (idxlist))
2972 if (layouts != NULL_TREE)
2974 type = build_simple_array_type (
2975 type, TREE_VALUE (idxlist), TREE_VALUE (layouts));
2976 layouts = TREE_CHAIN (layouts);
2979 type = build_simple_array_type (type, TREE_VALUE (idxlist), NULL_TREE);
2983 array_type = build_varying_struct (array_type);
2987 /* Function to help qsort sort FIELD_DECLs by name order. */
2990 field_decl_cmp (x, y)
2993 return (long)DECL_NAME (*x) - (long)DECL_NAME (*y);
2997 make_chill_struct_type (fieldlist)
3001 if (TREE_UNION_ELEM (fieldlist))
3002 t = make_node (UNION_TYPE);
3004 t = make_node (RECORD_TYPE);
3005 /* Install struct as DECL_CONTEXT of each field decl. */
3006 for (x = fieldlist; x; x = TREE_CHAIN (x))
3008 DECL_CONTEXT (x) = t;
3009 DECL_FIELD_SIZE (x) = 0;
3012 /* Delete all duplicate fields from the fieldlist */
3013 for (x = fieldlist; x && TREE_CHAIN (x);)
3014 /* Anonymous fields aren't duplicates. */
3015 if (DECL_NAME (TREE_CHAIN (x)) == 0)
3019 register tree y = fieldlist;
3023 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
3029 if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
3031 error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
3032 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3034 else x = TREE_CHAIN (x);
3037 TYPE_FIELDS (t) = fieldlist;
3042 /* decl is a FIELD_DECL.
3043 DECL_INIT (decl) is (NULL_TREE, integer_one_node, integer_zero_node, tree_list),
3044 meaning (default, pack, nopack, POS (...) ).
3045 The return value is a boolean: 1 if POS specified, 0 if not */
3047 apply_chill_field_layout (decl, next_struct_offset)
3049 int* next_struct_offset;
3051 tree layout, type, temp, what;
3052 int word, wordsize, start_bit, offset, length, natural_length;
3056 type = TREE_TYPE (decl);
3057 is_discrete = discrete_type_p (type);
3059 natural_length = get_type_precision (TYPE_MIN_VALUE (type), TYPE_MAX_VALUE (type));
3061 natural_length = TREE_INT_CST_LOW (TYPE_SIZE (type));
3063 layout = DECL_INITIAL (decl);
3064 if (layout == integer_zero_node) /* NOPACK */
3066 DECL_PACKED (decl) = 0;
3067 *next_struct_offset += natural_length;
3068 return 0; /* not POS */
3071 if (layout == integer_one_node) /* PACK */
3074 DECL_BIT_FIELD (decl) = 1;
3077 DECL_BIT_FIELD (decl) = 0;
3078 DECL_ALIGN (decl) = BITS_PER_UNIT;
3080 DECL_PACKED (decl) = 1;
3081 DECL_FIELD_SIZE (decl) = natural_length;
3082 *next_struct_offset += natural_length;
3083 return 0; /* not POS */
3086 /* The layout is a POS (...). The current implementation restricts the use
3087 of POS to monotonically increasing fields whose width must be the
3088 natural width of the underlying type. */
3089 temp = TREE_PURPOSE (layout);
3091 if (TREE_CODE (TREE_PURPOSE (temp)) != INTEGER_CST)
3093 error ("Starting word in POS must be an integer constant");
3098 word = TREE_INT_CST_LOW (TREE_PURPOSE (temp));
3101 error ("Starting word in POS must be >= 0");
3107 wordsize = TYPE_PRECISION (chill_integer_type_node);
3108 offset = word * wordsize;
3109 length = natural_length;
3111 temp = TREE_VALUE (temp);
3112 if (temp != NULL_TREE)
3114 if (TREE_CODE (TREE_PURPOSE (temp)) != INTEGER_CST)
3116 error ("Starting bit in POS must be an integer constant");
3117 start_bit = *next_struct_offset - offset;
3122 start_bit = TREE_INT_CST_LOW (TREE_PURPOSE (temp));
3125 error ("Starting bit in POS must be >= 0");
3126 start_bit = *next_struct_offset - offset;
3129 else if (start_bit >= wordsize)
3131 error ("Starting bit in POS must be < the width of a word");
3132 start_bit = *next_struct_offset - offset;
3137 temp = TREE_VALUE (temp);
3138 if (temp != NULL_TREE)
3140 what = TREE_PURPOSE (temp);
3141 if (what == integer_zero_node)
3143 if (TREE_CODE (TREE_VALUE (temp)) != INTEGER_CST)
3145 error ("Length in POS must be an integer constant");
3150 length = TREE_INT_CST_LOW (TREE_VALUE (temp));
3153 error ("Length in POS must be > 0");
3154 length = natural_length;
3161 if (TREE_CODE (TREE_VALUE (temp)) != INTEGER_CST)
3163 error ("End bit in POS must be an integer constant");
3168 int end_bit = TREE_INT_CST_LOW (TREE_VALUE (temp));
3169 if (end_bit < start_bit)
3171 error ("End bit in POS must be >= the start bit");
3174 else if (end_bit >= wordsize)
3176 error ("End bit in POS must be < the width of a word");
3180 length = end_bit - start_bit + 1;
3183 if (length != natural_length && ! pos_error)
3185 sorry ("The length specified on POS must be the natural length "
3186 "of the field type");
3187 length = natural_length;
3191 offset += start_bit;
3194 if (offset != *next_struct_offset && ! pos_error)
3195 sorry ("STRUCT fields must be layed out in monotonically increasing order");
3197 DECL_PACKED (decl) = 1;
3198 DECL_BIT_FIELD (decl) = is_discrete;
3199 DECL_FIELD_SIZE (decl) = length;
3200 *next_struct_offset += natural_length;
3202 return 1; /* was POS */
3206 layout_chill_struct_type (t)
3209 tree fieldlist = TYPE_FIELDS (t);
3215 int next_struct_offset;
3217 old_momentary = suspend_momentary ();
3219 /* Process specified field sizes.
3220 Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3221 The specified size is found in the DECL_INITIAL.
3222 Store 0 there, except for ": 0" fields (so we can find them
3223 and delete them, below). */
3225 next_struct_offset = 0;
3226 for (x = fieldlist; x; x = TREE_CHAIN (x))
3228 /* An EVENT or BUFFER mode is implemented as a RECORD_TYPE
3229 which may contain a CONST_DECL for the maximum queue size. */
3230 if (TREE_CODE (x) == CONST_DECL)
3233 /* If any field is const, the structure type is pseudo-const. */
3234 /* A field that is pseudo-const makes the structure likewise. */
3235 if (TREE_READONLY (x) || TYPE_READONLY_PROPERTY (TREE_TYPE (x)))
3236 TYPE_FIELDS_READONLY (t) = 1;
3238 /* Any field that is volatile means variables of this type must be
3239 treated in some ways as volatile. */
3240 if (TREE_THIS_VOLATILE (x))
3241 C_TYPE_FIELDS_VOLATILE (t) = 1;
3243 if (DECL_INITIAL (x) != NULL_TREE)
3245 was_pos = apply_chill_field_layout (x, &next_struct_offset);
3246 DECL_INITIAL (x) = NULL_TREE;
3250 unsigned int min_align = TYPE_ALIGN (TREE_TYPE (x));
3251 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
3254 if ((! was_pos && pos_seen) || (was_pos && ! pos_seen && x != fieldlist))
3256 pos_seen |= was_pos;
3260 error ("If one field has a POS layout, then all fields must have a POS layout");
3262 /* Now DECL_INITIAL is null on all fields. */
3266 /* Now we have the truly final field list.
3267 Store it in this type and in the variants. */
3269 TYPE_FIELDS (t) = fieldlist;
3271 /* If there are lots of fields, sort so we can look through them fast.
3272 We arbitrarily consider 16 or more elts to be "a lot". */
3276 for (x = fieldlist; x; x = TREE_CHAIN (x))
3287 len += list_length (x);
3288 /* Use the same allocation policy here that make_node uses, to
3289 ensure that this lives as long as the rest of the struct decl.
3290 All decls in an inline function need to be saved. */
3291 if (allocation_temporary_p ())
3292 space = savealloc (sizeof (struct lang_type) + len * sizeof (tree));
3294 space = oballoc (sizeof (struct lang_type) + len * sizeof (tree));
3296 TYPE_LANG_SPECIFIC (t) = (struct lang_type *) space;
3297 TYPE_LANG_SPECIFIC (t)->foo.rec.len = len;
3299 field_array = &TYPE_LANG_SPECIFIC (t)->foo.rec.elts[0];
3301 for (x = fieldlist; x; x = TREE_CHAIN (x))
3302 field_array[len++] = x;
3304 qsort (field_array, len, sizeof (tree), field_decl_cmp);
3308 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
3310 TYPE_FIELDS (x) = TYPE_FIELDS (t);
3311 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
3312 TYPE_ALIGN (x) = TYPE_ALIGN (t);
3315 resume_momentary (old_momentary);
3320 /* Given a list of fields, FIELDLIST, return a structure
3321 type that contains these fields. The returned type is
3322 always a new type. */
3324 build_chill_struct_type (fieldlist)
3329 if (fieldlist == NULL_TREE || TREE_CODE (fieldlist) == ERROR_MARK)
3330 return error_mark_node;
3332 t = make_chill_struct_type (fieldlist);
3334 t = layout_chill_struct_type (t);
3336 /* pushtag (NULL_TREE, t); */
3341 /* Fix a LANG_TYPE. These are used for three different uses:
3342 - representing a 'READ M' (in which case TYPE_READONLY is set);
3343 - for a NEWMODE or SYNMODE (CH_NOVELTY is set for a NEWMODE); and
3344 - for a parameterised type (TREE_TYPE points to base type,
3345 while TYPE_DOMAIN is the parameter or parameter list).
3346 Called from satisfy. */
3348 smash_dummy_type (type)
3351 /* Save fields that we don't want to copy from ORIGIN. */
3352 tree origin = TREE_TYPE (type);
3353 tree main_tree = TYPE_MAIN_VARIANT (origin);
3354 int save_uid = TYPE_UID (type);
3355 struct obstack *save_obstack = TYPE_OBSTACK (type);
3356 tree save_name = TYPE_NAME (type);
3357 int save_permanent = TREE_PERMANENT (type);
3358 int save_readonly = TYPE_READONLY (type);
3359 tree save_novelty = CH_NOVELTY (type);
3360 tree save_domain = TYPE_DOMAIN (type);
3362 if (origin == NULL_TREE)
3367 if (TREE_CODE (save_domain) == ERROR_MARK)
3368 return error_mark_node;
3369 if (origin == char_type_node)
3370 { /* Old-fashioned CHAR(N) declaration. */
3371 origin = build_string_type (origin, save_domain);
3374 { /* Handle parameterised modes. */
3375 int is_varying = chill_varying_type_p (origin);
3376 tree new_max = save_domain;
3377 tree origin_novelty = CH_NOVELTY (origin);
3379 origin = CH_VARYING_ARRAY_TYPE (origin);
3380 if (CH_STRING_TYPE_P (origin))
3382 tree oldindex = TYPE_DOMAIN (origin);
3383 new_max = check_range (new_max, new_max, NULL_TREE,
3384 size_binop (PLUS_EXPR,
3385 TYPE_MAX_VALUE (oldindex),
3387 origin = build_string_type (TREE_TYPE (origin), new_max);
3389 else if (TREE_CODE (origin) == ARRAY_TYPE)
3391 tree oldindex = TYPE_DOMAIN (origin);
3392 tree upper = check_range (new_max, new_max, NULL_TREE,
3393 TYPE_MAX_VALUE (oldindex));
3395 = build_chill_range_type (TREE_TYPE (oldindex),
3396 TYPE_MIN_VALUE (oldindex), upper);
3397 origin = build_simple_array_type (TREE_TYPE (origin), newindex, NULL_TREE);
3399 else if (TREE_CODE (origin) == RECORD_TYPE)
3401 error ("parameterised structures not implemented");
3402 return error_mark_node;
3406 error ("invalid parameterised type");
3407 return error_mark_node;
3410 SET_CH_NOVELTY (origin, origin_novelty);
3413 origin = build_varying_struct (origin);
3414 SET_CH_NOVELTY (origin, origin_novelty);
3417 save_domain = NULL_TREE;
3420 if (TREE_CODE (origin) == ERROR_MARK)
3421 return error_mark_node;
3423 *(struct tree_type*)type = *(struct tree_type*)origin;
3424 /* The following is so that the debug code for
3425 the copy is different from the original type.
3426 The two statements usually duplicate each other
3427 (because they clear fields of the same union),
3428 but the optimizer should catch that. */
3429 TYPE_SYMTAB_POINTER (type) = 0;
3430 TYPE_SYMTAB_ADDRESS (type) = 0;
3432 /* Restore fields that we didn't want copied from ORIGIN. */
3433 TYPE_UID (type) = save_uid;
3434 TYPE_OBSTACK (type) = save_obstack;
3435 TREE_PERMANENT (type) = save_permanent;
3436 TYPE_NAME (type) = save_name;
3438 TREE_CHAIN (type) = NULL_TREE;
3439 TYPE_VOLATILE (type) = 0;
3440 TYPE_POINTER_TO (type) = 0;
3441 TYPE_REFERENCE_TO (type) = 0;
3444 { /* TYPE is READ ORIGIN.
3445 Add this type to the chain of variants of TYPE. */
3446 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (main_tree);
3447 TYPE_NEXT_VARIANT (main_tree) = type;
3448 TYPE_READONLY (type) = save_readonly;
3452 /* TYPE is the copy of the RHS in a NEWMODE or SYNMODE.
3453 We also get here after old-fashioned CHAR(N) declaration (see above). */
3454 TYPE_MAIN_VARIANT (type) = type;
3455 TYPE_NEXT_VARIANT (type) = NULL_TREE;
3457 DECL_ORIGINAL_TYPE (save_name) = origin;
3459 if (save_novelty != NULL_TREE) /* A NEWMODE declaration. */
3461 CH_NOVELTY (type) = save_novelty;
3463 /* Z.200: "If the DEFINING mode of the NEWMODE name is a range mode,
3464 then the virtual mode &name is introduced as the PARENT mode
3465 of the NEWMODE name. The DEFINING mode of &name is the PARENT
3466 mode of the range mode, and the NOVELTY of &name is that of
3467 the NEWMODE name." */
3469 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type))
3472 /* PARENT is the virtual mode &name mentioned above. */
3473 push_obstacks_nochange ();
3474 end_temporary_allocation ();
3475 parent = copy_novelty (save_novelty,TREE_TYPE (type));
3478 TREE_TYPE (type) = parent;
3479 TYPE_MIN_VALUE (type) = convert (parent, TYPE_MIN_VALUE (type));
3480 TYPE_MAX_VALUE (type) = convert (parent, TYPE_MAX_VALUE (type));
3487 /* This generates a LANG_TYPE node that represents 'READ TYPE'. */
3490 build_readonly_type (type)
3493 tree node = make_node (LANG_TYPE);
3494 TREE_TYPE (node) = type;
3495 TYPE_READONLY (node) = 1;
3497 node = smash_dummy_type (node);
3502 /* Return an unsigned type the same as TYPE in other respects. */
3505 unsigned_type (type)
3508 tree type1 = TYPE_MAIN_VARIANT (type);
3509 if (type1 == signed_char_type_node || type1 == char_type_node)
3510 return unsigned_char_type_node;
3511 if (type1 == integer_type_node)
3512 return unsigned_type_node;
3513 if (type1 == short_integer_type_node)
3514 return short_unsigned_type_node;
3515 if (type1 == long_integer_type_node)
3516 return long_unsigned_type_node;
3517 if (type1 == long_long_integer_type_node)
3518 return long_long_unsigned_type_node;
3520 return signed_or_unsigned_type (1, type);
3523 /* Return a signed type the same as TYPE in other respects. */
3529 tree type1 = TYPE_MAIN_VARIANT (type);
3530 while (TREE_CODE (type1) == INTEGER_TYPE && TREE_TYPE (type1) != NULL_TREE)
3531 type1 = TREE_TYPE (type1);
3532 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3533 return signed_char_type_node;
3534 if (type1 == unsigned_type_node)
3535 return integer_type_node;
3536 if (type1 == short_unsigned_type_node)
3537 return short_integer_type_node;
3538 if (type1 == long_unsigned_type_node)
3539 return long_integer_type_node;
3540 if (type1 == long_long_unsigned_type_node)
3541 return long_long_integer_type_node;
3542 if (TYPE_PRECISION (type1) == 1)
3543 return signed_boolean_type_node;
3545 return signed_or_unsigned_type (0, type);
3548 /* Return a type the same as TYPE except unsigned or
3549 signed according to UNSIGNEDP. */
3552 signed_or_unsigned_type (unsignedp, type)
3556 if (! INTEGRAL_TYPE_P (type)
3557 || TREE_UNSIGNED (type) == unsignedp)
3560 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
3561 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3562 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
3563 return unsignedp ? unsigned_type_node : integer_type_node;
3564 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
3565 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3566 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
3567 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3568 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
3569 return (unsignedp ? long_long_unsigned_type_node
3570 : long_long_integer_type_node);
3574 /* Mark EXP saying that we need to be able to take the
3575 address of it; it should not be allocated in a register.
3576 Value is 1 if successful. */
3579 mark_addressable (exp)
3582 register tree x = exp;
3584 switch (TREE_CODE (x))
3591 x = TREE_OPERAND (x, 0);
3594 case TRUTH_ANDIF_EXPR:
3595 case TRUTH_ORIF_EXPR:
3597 x = TREE_OPERAND (x, 1);
3601 return mark_addressable (TREE_OPERAND (x, 1))
3602 & mark_addressable (TREE_OPERAND (x, 2));
3605 TREE_ADDRESSABLE (x) = 1;
3609 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
3610 incompatibility problems. Handle this case by marking FOO. */
3611 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
3612 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
3614 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
3617 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
3619 x = TREE_OPERAND (x, 0);
3628 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3629 && DECL_NONLOCAL (x))
3631 if (TREE_PUBLIC (x))
3633 error ("global register variable `%s' used in nested function",
3634 IDENTIFIER_POINTER (DECL_NAME (x)));
3637 pedwarn ("register variable `%s' used in nested function",
3638 IDENTIFIER_POINTER (DECL_NAME (x)));
3640 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3642 if (TREE_PUBLIC (x))
3644 error ("address of global register variable `%s' requested",
3645 IDENTIFIER_POINTER (DECL_NAME (x)));
3649 /* If we are making this addressable due to its having
3650 volatile components, give a different error message. Also
3651 handle the case of an unnamed parameter by not trying
3652 to give the name. */
3654 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3656 error ("cannot put object with volatile field into register");
3660 pedwarn ("address of register variable `%s' requested",
3661 IDENTIFIER_POINTER (DECL_NAME (x)));
3663 put_var_into_stack (x);
3667 TREE_ADDRESSABLE (x) = 1;
3668 #if 0 /* poplevel deals with this now. */
3669 if (DECL_CONTEXT (x) == 0)
3670 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3678 /* Return nonzero if VALUE is a valid constant-valued expression
3679 for use in initializing a static variable; one that can be an
3680 element of a "constant" initializer.
3682 Return null_pointer_node if the value is absolute;
3683 if it is relocatable, return the variable that determines the relocation.
3684 We assume that VALUE has been folded as much as possible;
3685 therefore, we do not need to check for such things as
3686 arithmetic-combinations of integers. */
3689 initializer_constant_valid_p (value, endtype)
3693 switch (TREE_CODE (value))
3696 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3697 && TREE_CONSTANT (value))
3699 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
3702 return TREE_STATIC (value) ? null_pointer_node : 0;
3708 return null_pointer_node;
3711 return TREE_OPERAND (value, 0);
3713 case NON_LVALUE_EXPR:
3714 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3718 /* Allow conversions between pointer types. */
3719 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
3720 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
3721 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3723 /* Allow conversions between real types. */
3724 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
3725 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
3726 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3728 /* Allow length-preserving conversions between integer types. */
3729 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
3730 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
3731 && (TYPE_PRECISION (TREE_TYPE (value))
3732 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3733 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3735 /* Allow conversions between other integer types only if
3737 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
3738 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
3740 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3742 if (inner == null_pointer_node)
3743 return null_pointer_node;
3747 /* Allow (int) &foo provided int is as wide as a pointer. */
3748 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
3749 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
3750 && (TYPE_PRECISION (TREE_TYPE (value))
3751 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3752 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3755 /* Likewise conversions from int to pointers. */
3756 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
3757 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
3758 && (TYPE_PRECISION (TREE_TYPE (value))
3759 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3760 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3763 /* Allow conversions to union types if the value inside is okay. */
3764 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
3765 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3770 if (TREE_CODE (endtype) == INTEGER_TYPE
3771 && TYPE_PRECISION (endtype) < POINTER_SIZE)
3774 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3776 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3778 /* If either term is absolute, use the other terms relocation. */
3779 if (valid0 == null_pointer_node)
3781 if (valid1 == null_pointer_node)
3787 if (TREE_CODE (endtype) == INTEGER_TYPE
3788 && TYPE_PRECISION (endtype) < POINTER_SIZE)
3791 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3793 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3795 /* Win if second argument is absolute. */
3796 if (valid1 == null_pointer_node)
3798 /* Win if both arguments have the same relocation.
3799 Then the value is absolute. */
3800 if (valid0 == valid1)
3801 return null_pointer_node;
3809 /* Return an integer type with BITS bits of precision,
3810 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3813 type_for_size (bits, unsignedp)
3817 if (bits == TYPE_PRECISION (integer_type_node))
3818 return unsignedp ? unsigned_type_node : integer_type_node;
3820 if (bits == TYPE_PRECISION (signed_char_type_node))
3821 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3823 if (bits == TYPE_PRECISION (short_integer_type_node))
3824 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3826 if (bits == TYPE_PRECISION (long_integer_type_node))
3827 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3829 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3830 return (unsignedp ? long_long_unsigned_type_node
3831 : long_long_integer_type_node);
3833 if (bits <= TYPE_PRECISION (intQI_type_node))
3834 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3836 if (bits <= TYPE_PRECISION (intHI_type_node))
3837 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3839 if (bits <= TYPE_PRECISION (intSI_type_node))
3840 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3842 if (bits <= TYPE_PRECISION (intDI_type_node))
3843 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3845 #if HOST_BITS_PER_WIDE_INT >= 64
3846 if (bits <= TYPE_PRECISION (intTI_type_node))
3847 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3853 /* Return a data type that has machine mode MODE.
3854 If the mode is an integer,
3855 then UNSIGNEDP selects between signed and unsigned types. */
3858 type_for_mode (mode, unsignedp)
3859 enum machine_mode mode;
3862 if (mode == TYPE_MODE (integer_type_node))
3863 return unsignedp ? unsigned_type_node : integer_type_node;
3865 if (mode == TYPE_MODE (signed_char_type_node))
3866 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3868 if (mode == TYPE_MODE (short_integer_type_node))
3869 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3871 if (mode == TYPE_MODE (long_integer_type_node))
3872 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3874 if (mode == TYPE_MODE (long_long_integer_type_node))
3875 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3877 if (mode == TYPE_MODE (intQI_type_node))
3878 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3880 if (mode == TYPE_MODE (intHI_type_node))
3881 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3883 if (mode == TYPE_MODE (intSI_type_node))
3884 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3886 if (mode == TYPE_MODE (intDI_type_node))
3887 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3889 #if HOST_BITS_PER_WIDE_INT >= 64
3890 if (mode == TYPE_MODE (intTI_type_node))
3891 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3894 if (mode == TYPE_MODE (float_type_node))
3895 return float_type_node;
3897 if (mode == TYPE_MODE (double_type_node))
3898 return double_type_node;
3900 if (mode == TYPE_MODE (long_double_type_node))
3901 return long_double_type_node;
3903 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3904 return build_pointer_type (char_type_node);
3906 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3907 return build_pointer_type (integer_type_node);