Debian Codegraph @

gcc/m2/gm2-gcc/m2expr.cc

raw
1 /* m2expr.cc provides an interface to GCC expression trees.
2
3 Copyright (C) 2012-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "gcc-consolidation.h"
23
24 #include "../gm2-lang.h"
25 #include "../m2-tree.h"
26 #include "m2convert.h"
27
28 /* Prototypes. */
29
30 #define m2expr_c
31 #include "m2assert.h"
32 #include "m2builtins.h"
33 #include "m2convert.h"
34 #include "m2decl.h"
35 #include "m2expr.h"
36 #include "m2options.h"
37 #include "m2range.h"
38 #include "m2statement.h"
39 #include "m2tree.h"
40 #include "m2treelib.h"
41 #include "m2type.h"
42 #include "m2linemap.h"
43 #include "math.h"
44
45 static void m2expr_checkRealOverflow (location_t location, enum tree_code code,
46 tree result);
47 static tree checkWholeNegateOverflow (location_t location, tree i, tree lowest,
48 tree min, tree max);
49 // static tree m2expr_Build4LogicalAnd (location_t location, tree a, tree b,
50 // tree c, tree d);
51 static tree m2expr_Build4LogicalOr (location_t location, tree a, tree b,
52 tree c, tree d);
53 static tree m2expr_Build4TruthOrIf (location_t location, tree a, tree b,
54 tree c, tree d);
55 static tree m2expr_Build4TruthAndIf (location_t location, tree a, tree b,
56 tree c, tree d);
57
58 static int label_count = 0;
59 static GTY (()) tree set_full_complement;
60
61 /* Return an integer string using base 10 and no padding. The string returned
62 will have been malloc'd. */
63
64 char *
65 m2expr_CSTIntToString (tree t)
66 {
67 char val[100];
68
69 snprintf (val, 100, HOST_WIDE_INT_PRINT_UNSIGNED, TREE_INT_CST_LOW (t));
70 return xstrndup (val, 100);
71 }
72
73 /* Return the char representation of tree t. */
74
75 char
76 m2expr_CSTIntToChar (tree t)
77 {
78 return (char) (TREE_INT_CST_LOW (t));
79 }
80
81 /* CompareTrees returns -1 if e1 < e2, 0 if e1 == e2, and 1 if e1 > e2. */
82
83 int
84 m2expr_CompareTrees (tree e1, tree e2)
85 {
86 return tree_int_cst_compare (m2expr_FoldAndStrip (e1),
87 m2expr_FoldAndStrip (e2));
88 }
89
90 /* FoldAndStrip return expression, t, after it has been folded (if
91 possible). */
92
93 tree
94 m2expr_FoldAndStrip (tree t)
95 {
96 if (t != NULL)
97 {
98 t = fold (t);
99 if (TREE_CODE (t) == CONST_DECL)
100 return m2expr_FoldAndStrip (DECL_INITIAL (t));
101 }
102
103 return t;
104 }
105
106 /* StringLength returns an unsigned int which is the length of, string. */
107
108 unsigned int
109 m2expr_StringLength (tree string)
110 {
111 return TREE_STRING_LENGTH (string);
112 }
113
114 /* BuildCondIfExpression returns a tree containing (condition) ? (left) : right. */
115
116 tree
117 m2expr_BuildCondIfExpression (tree condition, tree type, tree left, tree right)
118 {
119 return fold_build3 (COND_EXPR, type, condition, left, right);
120 }
121
122 /* CheckAddressToCardinal if op is a pointer convert it to the ADDRESS type. */
123
124 static tree
125 CheckAddressToCardinal (location_t location, tree op)
126 {
127 if (m2type_IsAddress (TREE_TYPE (op)))
128 return m2convert_BuildConvert (location, m2type_GetCardinalAddressType (),
129 op, false);
130 return op;
131 }
132
133 /* BuildTruthAndIf return true if a && b. Retain order left to right. */
134
135 static tree
136 m2expr_BuildTruthAndIf (location_t location, tree a, tree b)
137 {
138 return m2expr_build_binary_op (location, TRUTH_ANDIF_EXPR, a, b, false);
139 }
140
141 /* BuildTruthOrIf return true if a || b. Retain order left to right. */
142
143 static tree
144 m2expr_BuildTruthOrIf (location_t location, tree a, tree b)
145 {
146 return m2expr_build_binary_op (location, TRUTH_ORIF_EXPR, a, b, false);
147 }
148
149 /* BuildTruthNotIf inverts the boolean value of expr and returns the result. */
150
151 static tree
152 m2expr_BuildTruthNot (location_t location, tree expr)
153 {
154 return m2expr_build_unary_op (location, TRUTH_NOT_EXPR, expr, false);
155 }
156
157 /* BuildPostInc builds a post increment tree, the second operand is
158 always one. */
159
160 static tree
161 m2expr_BuildPostInc (location_t location, tree op)
162 {
163 return m2expr_BuildAdd (location, op, build_int_cst (TREE_TYPE (op), 1), false);
164 }
165
166 /* BuildPostDec builds a post decrement tree, the second operand is
167 always one. */
168
169 static tree
170 m2expr_BuildPostDec (location_t location, tree op)
171 {
172 return m2expr_BuildSub (location, op, build_int_cst (TREE_TYPE (op), 1), false);
173 }
174
175 /* BuildAddCheck builds an addition tree. */
176
177 tree
178 m2expr_BuildAddCheck (location_t location, tree op1, tree op2, tree lowest,
179 tree min, tree max)
180 {
181 tree t;
182
183 m2assert_AssertLocation (location);
184
185 op1 = m2expr_FoldAndStrip (op1);
186 op2 = m2expr_FoldAndStrip (op2);
187
188 op1 = CheckAddressToCardinal (location, op1);
189 op2 = CheckAddressToCardinal (location, op2);
190
191 t = m2expr_build_binary_op_check (location, PLUS_EXPR, op1, op2, false,
192 lowest, min, max);
193 return m2expr_FoldAndStrip (t);
194 }
195
196 /* BuildAdd builds an addition tree. */
197
198 tree
199 m2expr_BuildAdd (location_t location, tree op1, tree op2, bool needconvert)
200 {
201 tree t;
202
203 m2assert_AssertLocation (location);
204
205 op1 = m2expr_FoldAndStrip (op1);
206 op2 = m2expr_FoldAndStrip (op2);
207
208 op1 = CheckAddressToCardinal (location, op1);
209 op2 = CheckAddressToCardinal (location, op2);
210
211 t = m2expr_build_binary_op (location, PLUS_EXPR, op1, op2, needconvert);
212 return m2expr_FoldAndStrip (t);
213 }
214
215 /* BuildSubCheck builds a subtraction tree. */
216
217 tree
218 m2expr_BuildSubCheck (location_t location, tree op1, tree op2, tree lowest,
219 tree min, tree max)
220 {
221 tree t;
222
223 m2assert_AssertLocation (location);
224
225 op1 = m2expr_FoldAndStrip (op1);
226 op2 = m2expr_FoldAndStrip (op2);
227
228 op1 = CheckAddressToCardinal (location, op1);
229 op2 = CheckAddressToCardinal (location, op2);
230
231 t = m2expr_build_binary_op_check (location, MINUS_EXPR, op1, op2, false,
232 lowest, min, max);
233 return m2expr_FoldAndStrip (t);
234 }
235
236 /* BuildSub builds a subtraction tree. */
237
238 tree
239 m2expr_BuildSub (location_t location, tree op1, tree op2, bool needconvert)
240 {
241 tree t;
242
243 m2assert_AssertLocation (location);
244
245 op1 = m2expr_FoldAndStrip (op1);
246 op2 = m2expr_FoldAndStrip (op2);
247
248 op1 = CheckAddressToCardinal (location, op1);
249 op2 = CheckAddressToCardinal (location, op2);
250
251 t = m2expr_build_binary_op (location, MINUS_EXPR, op1, op2, needconvert);
252 return m2expr_FoldAndStrip (t);
253 }
254
255 /* BuildDivTrunc builds a trunc division tree. */
256
257 tree
258 m2expr_BuildDivTrunc (location_t location, tree op1, tree op2, bool needconvert)
259 {
260 tree t;
261
262 m2assert_AssertLocation (location);
263
264 op1 = m2expr_FoldAndStrip (op1);
265 op2 = m2expr_FoldAndStrip (op2);
266
267 op1 = CheckAddressToCardinal (location, op1);
268 op2 = CheckAddressToCardinal (location, op2);
269
270 t = m2expr_build_binary_op (location, TRUNC_DIV_EXPR, op1, op2, needconvert);
271 return m2expr_FoldAndStrip (t);
272 }
273
274 /* BuildDivTruncCheck builds a trunc division tree. */
275
276 tree
277 m2expr_BuildDivTruncCheck (location_t location, tree op1, tree op2, tree lowest,
278 tree min, tree max)
279 {
280 tree t;
281
282 m2assert_AssertLocation (location);
283
284 op1 = m2expr_FoldAndStrip (op1);
285 op2 = m2expr_FoldAndStrip (op2);
286
287 op1 = CheckAddressToCardinal (location, op1);
288 op2 = CheckAddressToCardinal (location, op2);
289
290 t = m2expr_build_binary_op_check (location, TRUNC_DIV_EXPR, op1, op2, false,
291 lowest, min, max);
292 return m2expr_FoldAndStrip (t);
293 }
294
295 /* BuildModTruncCheck builds a trunc modulus tree. */
296
297 tree
298 m2expr_BuildModTruncCheck (location_t location, tree op1, tree op2, tree lowest,
299 tree min, tree max)
300 {
301 tree t;
302
303 m2assert_AssertLocation (location);
304
305 op1 = m2expr_FoldAndStrip (op1);
306 op2 = m2expr_FoldAndStrip (op2);
307
308 op1 = CheckAddressToCardinal (location, op1);
309 op2 = CheckAddressToCardinal (location, op2);
310
311 t = m2expr_build_binary_op_check (location, TRUNC_MOD_EXPR, op1, op2, false,
312 lowest, min, max);
313 return m2expr_FoldAndStrip (t);
314 }
315
316 /* BuildModTrunc builds a trunc modulus tree. */
317
318 tree
319 m2expr_BuildModTrunc (location_t location, tree op1, tree op2, bool needconvert)
320 {
321 tree t;
322
323 m2assert_AssertLocation (location);
324
325 op1 = m2expr_FoldAndStrip (op1);
326 op2 = m2expr_FoldAndStrip (op2);
327
328 op1 = CheckAddressToCardinal (location, op1);
329 op2 = CheckAddressToCardinal (location, op2);
330
331 t = m2expr_build_binary_op (location, TRUNC_MOD_EXPR, op1, op2, needconvert);
332 return m2expr_FoldAndStrip (t);
333 }
334
335 /* BuildModCeilCheck builds a ceil modulus tree. */
336
337 tree
338 m2expr_BuildModCeilCheck (location_t location, tree op1, tree op2, tree lowest,
339 tree min, tree max)
340 {
341 tree t;
342
343 m2assert_AssertLocation (location);
344
345 op1 = m2expr_FoldAndStrip (op1);
346 op2 = m2expr_FoldAndStrip (op2);
347
348 op1 = CheckAddressToCardinal (location, op1);
349 op2 = CheckAddressToCardinal (location, op2);
350
351 t = m2expr_build_binary_op_check (location, CEIL_MOD_EXPR, op1, op2, false,
352 lowest, min, max);
353 return m2expr_FoldAndStrip (t);
354 }
355
356 /* BuildModFloorCheck builds a trunc modulus tree. */
357
358 tree
359 m2expr_BuildModFloorCheck (location_t location, tree op1, tree op2, tree lowest,
360 tree min, tree max)
361 {
362 tree t;
363
364 m2assert_AssertLocation (location);
365
366 op1 = m2expr_FoldAndStrip (op1);
367 op2 = m2expr_FoldAndStrip (op2);
368
369 op1 = CheckAddressToCardinal (location, op1);
370 op2 = CheckAddressToCardinal (location, op2);
371
372 t = m2expr_build_binary_op_check (location, FLOOR_MOD_EXPR, op1, op2, false,
373 lowest, min, max);
374 return m2expr_FoldAndStrip (t);
375 }
376
377 /* BuildDivCeil builds a ceil division tree. */
378
379 tree
380 m2expr_BuildDivCeil (location_t location, tree op1, tree op2, bool needconvert)
381 {
382 tree t;
383
384 m2assert_AssertLocation (location);
385
386 op1 = m2expr_FoldAndStrip (op1);
387 op2 = m2expr_FoldAndStrip (op2);
388
389 op1 = CheckAddressToCardinal (location, op1);
390 op2 = CheckAddressToCardinal (location, op2);
391
392 t = m2expr_build_binary_op (location, CEIL_DIV_EXPR, op1, op2, needconvert);
393 return m2expr_FoldAndStrip (t);
394 }
395
396 /* BuildDivCeilCheck builds a check ceil division tree. */
397
398 tree
399 m2expr_BuildDivCeilCheck (location_t location, tree op1, tree op2, tree lowest,
400 tree min, tree max)
401 {
402 tree t;
403
404 m2assert_AssertLocation (location);
405
406 op1 = m2expr_FoldAndStrip (op1);
407 op2 = m2expr_FoldAndStrip (op2);
408
409 op1 = CheckAddressToCardinal (location, op1);
410 op2 = CheckAddressToCardinal (location, op2);
411
412 t = m2expr_build_binary_op_check (location, CEIL_DIV_EXPR, op1, op2, false,
413 lowest, min, max);
414 return m2expr_FoldAndStrip (t);
415 }
416
417 /* BuildModCeil builds a ceil modulus tree. */
418
419 tree
420 m2expr_BuildModCeil (location_t location, tree op1, tree op2, bool needconvert)
421 {
422 tree t;
423
424 m2assert_AssertLocation (location);
425
426 op1 = m2expr_FoldAndStrip (op1);
427 op2 = m2expr_FoldAndStrip (op2);
428
429 op1 = CheckAddressToCardinal (location, op1);
430 op2 = CheckAddressToCardinal (location, op2);
431
432 t = m2expr_build_binary_op (location, CEIL_MOD_EXPR, op1, op2, needconvert);
433 return m2expr_FoldAndStrip (t);
434 }
435
436 /* BuildDivFloor builds a floor division tree. */
437
438 tree
439 m2expr_BuildDivFloor (location_t location, tree op1, tree op2, bool needconvert)
440 {
441 tree t;
442
443 m2assert_AssertLocation (location);
444
445 op1 = m2expr_FoldAndStrip (op1);
446 op2 = m2expr_FoldAndStrip (op2);
447
448 op1 = CheckAddressToCardinal (location, op1);
449 op2 = CheckAddressToCardinal (location, op2);
450
451 t = m2expr_build_binary_op (location, FLOOR_DIV_EXPR, op1, op2, needconvert);
452 return m2expr_FoldAndStrip (t);
453 }
454
455 /* BuildDivFloorCheck builds a check floor division tree. */
456
457 tree
458 m2expr_BuildDivFloorCheck (location_t location, tree op1, tree op2, tree lowest,
459 tree min, tree max)
460 {
461 tree t;
462
463 m2assert_AssertLocation (location);
464
465 op1 = m2expr_FoldAndStrip (op1);
466 op2 = m2expr_FoldAndStrip (op2);
467
468 op1 = CheckAddressToCardinal (location, op1);
469 op2 = CheckAddressToCardinal (location, op2);
470
471 t = m2expr_build_binary_op_check (location, FLOOR_DIV_EXPR, op1, op2, false,
472 lowest, min, max);
473 return m2expr_FoldAndStrip (t);
474 }
475
476 /* BuildRDiv builds a division tree (this should only be used for
477 REAL and COMPLEX types and NEVER for integer based types). */
478
479 tree
480 m2expr_BuildRDiv (location_t location, tree op1, tree op2, bool needconvert)
481 {
482 tree t;
483
484 m2assert_AssertLocation (location);
485
486 op1 = m2expr_FoldAndStrip (op1);
487 op2 = m2expr_FoldAndStrip (op2);
488
489 t = m2expr_build_binary_op (location, RDIV_EXPR, op1, op2, needconvert);
490 return m2expr_FoldAndStrip (t);
491 }
492
493 /* BuildModFloor builds a modulus tree. */
494
495 tree
496 m2expr_BuildModFloor (location_t location, tree op1, tree op2, bool needconvert)
497 {
498 tree t;
499
500 m2assert_AssertLocation (location);
501
502 op1 = m2expr_FoldAndStrip (op1);
503 op2 = m2expr_FoldAndStrip (op2);
504
505 op1 = CheckAddressToCardinal (location, op1);
506 op2 = CheckAddressToCardinal (location, op2);
507
508 t = m2expr_build_binary_op (location, FLOOR_MOD_EXPR, op1, op2, needconvert);
509 return m2expr_FoldAndStrip (t);
510 }
511
512 /* BuildLSL builds and returns tree (op1 << op2). */
513
514 tree
515 m2expr_BuildLSL (location_t location, tree op1, tree op2, bool needconvert)
516 {
517 tree t;
518
519 m2assert_AssertLocation (location);
520
521 op1 = m2expr_FoldAndStrip (op1);
522 op2 = m2expr_FoldAndStrip (op2);
523
524 t = m2expr_build_binary_op (location, LSHIFT_EXPR, op1, op2, needconvert);
525 return m2expr_FoldAndStrip (t);
526 }
527
528 /* BuildLSR builds and returns tree (op1 >> op2). */
529
530 tree
531 m2expr_BuildLSR (location_t location, tree op1, tree op2, bool needconvert)
532 {
533 tree t;
534
535 m2assert_AssertLocation (location);
536
537 op1 = m2expr_FoldAndStrip (op1);
538 op2 = m2expr_FoldAndStrip (op2);
539
540 t = m2expr_build_binary_op (location, RSHIFT_EXPR, op1, op2, needconvert);
541 return m2expr_FoldAndStrip (t);
542 }
543
544 /* createUniqueLabel returns a unique label which has been alloc'ed. */
545
546 static char *
547 createUniqueLabel (void)
548 {
549 int size, i;
550 char *label;
551
552 label_count++;
553 i = label_count;
554 size = strlen (".LSHIFT") + 2;
555 while (i > 0)
556 {
557 i /= 10;
558 size++;
559 }
560 label = (char *)ggc_alloc_atomic (size);
561 sprintf (label, ".LSHIFT%d", label_count);
562 return label;
563 }
564
565 /* BuildLogicalShift builds the ISO Modula-2 SHIFT operator for a
566 fundamental data type. */
567
568 void
569 m2expr_BuildLogicalShift (location_t location, tree op1, tree op2, tree op3,
570 tree nBits ATTRIBUTE_UNUSED, bool needconvert)
571 {
572 tree res;
573
574 m2assert_AssertLocation (location);
575 op2 = m2expr_FoldAndStrip (op2);
576 op3 = m2expr_FoldAndStrip (op3);
577 if (TREE_CODE (op3) == INTEGER_CST)
578 {
579 op2 = m2convert_ToWord (location, op2);
580 if (tree_int_cst_sgn (op3) < 0)
581 res = m2expr_BuildLSR (
582 location, op2,
583 m2convert_ToWord (location,
584 m2expr_BuildNegate (location, op3, needconvert)),
585 needconvert);
586 else
587 res = m2expr_BuildLSL (location, op2, m2convert_ToWord (location, op3),
588 needconvert);
589 res = m2convert_BuildConvert (
590 location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
591 m2statement_BuildAssignmentTree (location, op1, res);
592 }
593 else
594 {
595 char *labelElseName = createUniqueLabel ();
596 char *labelEndName = createUniqueLabel ();
597 tree is_less = m2expr_BuildLessThan (location,
598 m2convert_ToInteger (location, op3),
599 m2expr_GetIntegerZero (location));
600
601 m2statement_DoJump (location, is_less, NULL, labelElseName);
602 op2 = m2convert_ToWord (location, op2);
603 op3 = m2convert_ToWord (location, op3);
604 res = m2expr_BuildLSL (location, op2, op3, needconvert);
605 res = m2convert_BuildConvert (
606 location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
607 m2statement_BuildAssignmentTree (location, op1, res);
608 m2statement_BuildGoto (location, labelEndName);
609 m2statement_DeclareLabel (location, labelElseName);
610 res = m2expr_BuildLSR (location, op2,
611 m2expr_BuildNegate (location, op3, needconvert),
612 needconvert);
613 res = m2convert_BuildConvert (
614 location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
615 m2statement_BuildAssignmentTree (location, op1, res);
616 m2statement_DeclareLabel (location, labelEndName);
617 }
618 }
619
620 /* BuildLRL builds and returns tree (op1 rotate left by op2 bits). */
621
622 tree
623 m2expr_BuildLRL (location_t location, tree op1, tree op2, bool needconvert)
624 {
625 tree t;
626
627 m2assert_AssertLocation (location);
628
629 op1 = m2expr_FoldAndStrip (op1);
630 op2 = m2expr_FoldAndStrip (op2);
631
632 t = m2expr_build_binary_op (location, LROTATE_EXPR, op1, op2, needconvert);
633 return m2expr_FoldAndStrip (t);
634 }
635
636 /* BuildLRR builds and returns tree (op1 rotate right by op2 bits). */
637
638 tree
639 m2expr_BuildLRR (location_t location, tree op1, tree op2, bool needconvert)
640 {
641 tree t;
642
643 m2assert_AssertLocation (location);
644
645 op1 = m2expr_FoldAndStrip (op1);
646 op2 = m2expr_FoldAndStrip (op2);
647
648 t = m2expr_build_binary_op (location, RROTATE_EXPR, op1, op2, needconvert);
649 return m2expr_FoldAndStrip (t);
650 }
651
652 /* m2expr_BuildMask returns a tree for the mask of a set of nBits.
653 It assumes nBits is <= TSIZE (WORD). */
654
655 tree
656 m2expr_BuildMask (location_t location, tree nBits, bool needconvert)
657 {
658 tree mask = m2expr_BuildLSL (location, m2expr_GetIntegerOne (location),
659 nBits, needconvert);
660 m2assert_AssertLocation (location);
661 return m2expr_BuildSub (location, mask, m2expr_GetIntegerOne (location),
662 needconvert);
663 }
664
665 /* m2expr_BuildLRotate returns a tree in which op1 has been left
666 rotated by nBits. It assumes nBits is <= TSIZE (WORD). */
667
668 tree
669 m2expr_BuildLRotate (location_t location, tree op1, tree nBits,
670 bool needconvert)
671 {
672 tree t;
673
674 op1 = m2expr_FoldAndStrip (op1);
675 nBits = m2expr_FoldAndStrip (nBits);
676 nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);
677 t = m2expr_build_binary_op (location, LROTATE_EXPR, op1, nBits, needconvert);
678 return m2expr_FoldAndStrip (t);
679 }
680
681 /* m2expr_BuildRRotate returns a tree in which op1 has been left
682 rotated by nBits. It assumes nBits is <= TSIZE (WORD). */
683
684 tree
685 m2expr_BuildRRotate (location_t location, tree op1, tree nBits,
686 bool needconvert)
687 {
688 tree t;
689
690 op1 = m2expr_FoldAndStrip (op1);
691 nBits = m2expr_FoldAndStrip (nBits);
692 nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);
693 t = m2expr_build_binary_op (location, RROTATE_EXPR, op1, nBits, needconvert);
694 return m2expr_FoldAndStrip (t);
695 }
696
697 /* BuildLRLn builds and returns tree (op1 rotate left by op2 bits) it
698 rotates a set of size, nBits. */
699
700 tree
701 m2expr_BuildLRLn (location_t location, tree op1, tree op2, tree nBits,
702 bool needconvert)
703 {
704 tree op2min;
705
706 m2assert_AssertLocation (location);
707
708 /* Ensure we wrap the rotate. */
709
710 op2min = m2expr_BuildModTrunc (
711 location, m2convert_ToCardinal (location, op2),
712 m2convert_ToCardinal (location, nBits), needconvert);
713
714 /* Optimize if we are we going to rotate a TSIZE(BITSET) set. */
715
716 if (m2expr_CompareTrees (
717 m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset ()), nBits)
718 == 0)
719 return m2expr_BuildLRotate (location, op1, op2min, needconvert);
720 else
721 {
722 tree mask = m2expr_BuildMask (location, nBits, needconvert);
723 tree left, right;
724
725 /* Make absolutely sure there are no high order bits lying around. */
726
727 op1 = m2expr_BuildLogicalAnd (location, op1, mask, needconvert);
728 left = m2expr_BuildLSL (location, op1, op2min, needconvert);
729 left = m2expr_BuildLogicalAnd (location, left, mask, needconvert);
730 right = m2expr_BuildLSR (
731 location, op1,
732 m2expr_BuildSub (location, m2convert_ToCardinal (location, nBits),
733 op2min, needconvert),
734 needconvert);
735 return m2expr_BuildLogicalOr (location, left, right, needconvert);
736 }
737 }
738
739 /* BuildLRRn builds and returns tree (op1 rotate right by op2 bits).
740 It rotates a set of size, nBits. */
741
742 tree
743 m2expr_BuildLRRn (location_t location, tree op1, tree op2, tree nBits,
744 bool needconvert)
745 {
746 tree op2min;
747
748 m2assert_AssertLocation (location);
749
750 /* Ensure we wrap the rotate. */
751
752 op2min = m2expr_BuildModTrunc (
753 location, m2convert_ToCardinal (location, op2),
754 m2convert_ToCardinal (location, nBits), needconvert);
755 /* Optimize if we are we going to rotate a TSIZE(BITSET) set. */
756
757 if (m2expr_CompareTrees (
758 m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset ()), nBits)
759 == 0)
760 return m2expr_BuildRRotate (location, op1, op2min, needconvert);
761 else
762 {
763 tree mask = m2expr_BuildMask (location, nBits, needconvert);
764 tree left, right;
765
766 /* Make absolutely sure there are no high order bits lying around. */
767
768 op1 = m2expr_BuildLogicalAnd (location, op1, mask, needconvert);
769 right = m2expr_BuildLSR (location, op1, op2min, needconvert);
770 left = m2expr_BuildLSL (
771 location, op1,
772 m2expr_BuildSub (location, m2convert_ToCardinal (location, nBits),
773 op2min, needconvert),
774 needconvert);
775 left = m2expr_BuildLogicalAnd (location, left, mask, needconvert);
776 return m2expr_BuildLogicalOr (location, left, right, needconvert);
777 }
778 }
779
780 /* BuildLogicalRotate build the ISO Modula-2 ROTATE operator for a
781 fundamental data type. */
782
783 void
784 m2expr_BuildLogicalRotate (location_t location, tree op1, tree op2, tree op3,
785 tree nBits, bool needconvert)
786 {
787 tree res;
788
789 m2assert_AssertLocation (location);
790 op2 = m2expr_FoldAndStrip (op2);
791 op3 = m2expr_FoldAndStrip (op3);
792 if (TREE_CODE (op3) == INTEGER_CST)
793 {
794 if (tree_int_cst_sgn (op3) < 0)
795 res = m2expr_BuildLRRn (
796 location, op2, m2expr_BuildNegate (location, op3, needconvert),
797 nBits, needconvert);
798 else
799 res = m2expr_BuildLRLn (location, op2, op3, nBits, needconvert);
800 m2statement_BuildAssignmentTree (location, op1, res);
801 }
802 else
803 {
804 char *labelElseName = createUniqueLabel ();
805 char *labelEndName = createUniqueLabel ();
806 tree rotateCount = m2convert_ToInteger (location, op3);
807 tree is_less = m2expr_BuildLessThan (location, rotateCount,
808 m2expr_GetIntegerZero (location));
809
810 m2statement_DoJump (location, is_less, NULL, labelElseName);
811 res = m2expr_BuildLRLn (location, op2, rotateCount, nBits, needconvert);
812 m2statement_BuildAssignmentTree (location, op1, res);
813 m2statement_BuildGoto (location, labelEndName);
814 m2statement_DeclareLabel (location, labelElseName);
815 rotateCount = m2expr_BuildNegate (location, rotateCount, needconvert);
816 res = m2expr_BuildLRRn (location, op2, rotateCount, nBits, needconvert);
817 m2statement_BuildAssignmentTree (location, op1, res);
818 m2statement_DeclareLabel (location, labelEndName);
819 }
820 }
821
822 /* buildUnboundedArrayOf construct an unbounded struct and returns
823 the gcc tree. The two fields of the structure are initialized to
824 contentsPtr and high. */
825
826 static tree
827 buildUnboundedArrayOf (tree unbounded, tree contentsPtr, tree high)
828 {
829 tree fields = TYPE_FIELDS (unbounded);
830 tree field_list = NULL_TREE;
831 tree constructor;
832
833 field_list = tree_cons (fields, contentsPtr, field_list);
834 fields = TREE_CHAIN (fields);
835
836 field_list = tree_cons (fields, high, field_list);
837
838 constructor = build_constructor_from_list (unbounded, nreverse (field_list));
839 TREE_CONSTANT (constructor) = 0;
840 TREE_STATIC (constructor) = 0;
841
842 return constructor;
843 }
844
845 /* BuildBinarySetDo if the size of the set is <= TSIZE(WORD) then op1
846 := binop(op2, op3) else call m2rtsprocedure(op1, op2, op3). */
847
848 void
849 m2expr_BuildBinarySetDo (location_t location, tree settype, tree op1, tree op2,
850 tree op3, void (*binop) (location_t, tree, tree, tree,
851 tree, bool),
852 bool is_op1lvalue, bool is_op2lvalue, bool is_op3lvalue,
853 tree nBits, tree unbounded, tree varproc,
854 tree leftproc, tree rightproc)
855 {
856 tree size = m2expr_GetSizeOf (location, settype);
857 bool is_const = false;
858 bool is_left = false;
859
860 m2assert_AssertLocation (location);
861
862 ASSERT_BOOL (is_op1lvalue);
863 ASSERT_BOOL (is_op2lvalue);
864 ASSERT_BOOL (is_op3lvalue);
865
866 if (m2expr_CompareTrees (
867 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
868 <= 0)
869 /* Small set size <= TSIZE(WORD). */
870 (*binop) (location,
871 m2treelib_get_rvalue (location, op1, settype, is_op1lvalue),
872 m2treelib_get_rvalue (location, op2, settype, is_op2lvalue),
873 m2treelib_get_rvalue (location, op3, settype, is_op3lvalue),
874 nBits, false);
875 else
876 {
877 tree result;
878 tree high = m2expr_BuildSub (
879 location,
880 m2convert_ToCardinal (
881 location,
882 m2expr_BuildDivTrunc (
883 location, size,
884 m2expr_GetSizeOf (location, m2type_GetBitsetType ()),
885 false)),
886 m2expr_GetCardinalOne (location), false);
887
888 /* If op3 is constant then make op3 positive and remember which
889 direction we are shifting. */
890
891 op3 = m2tree_skip_const_decl (op3);
892 if (TREE_CODE (op3) == INTEGER_CST)
893 {
894 is_const = true;
895 if (tree_int_cst_sgn (op3) < 0)
896 op3 = m2expr_BuildNegate (location, op3, false);
897 else
898 is_left = true;
899 op3 = m2convert_BuildConvert (location, m2type_GetM2CardinalType (),
900 op3, false);
901 }
902
903 /* These parameters must match the prototypes of the procedures:
904 ShiftLeft, ShiftRight, ShiftVal, RotateLeft, RotateRight, RotateVal
905 inside gm2-iso/SYSTEM.mod. */
906
907 /* Remember we must build the parameters in reverse. */
908
909 /* Parameter 4 amount. */
910 m2statement_BuildParam (
911 location,
912 m2convert_BuildConvert (
913 location, m2type_GetM2IntegerType (),
914 m2treelib_get_rvalue (location, op3,
915 m2tree_skip_type_decl (TREE_TYPE (op3)),
916 is_op3lvalue),
917 false));
918
919 /* Parameter 3 nBits. */
920 m2statement_BuildParam (
921 location,
922 m2convert_BuildConvert (location, m2type_GetM2CardinalType (),
923 m2expr_FoldAndStrip (nBits), false));
924
925 /* Parameter 2 destination set. */
926 m2statement_BuildParam (
927 location,
928 buildUnboundedArrayOf (
929 unbounded,
930 m2treelib_get_set_address (location, op1, is_op1lvalue), high));
931
932 /* Parameter 1 source set. */
933 m2statement_BuildParam (
934 location,
935 buildUnboundedArrayOf (
936 unbounded,
937 m2treelib_get_set_address (location, op2, is_op2lvalue), high));
938
939 /* Now call the appropriate procedure inside SYSTEM.mod. */
940 if (is_const)
941 if (is_left)
942 result = m2statement_BuildProcedureCallTree (location, leftproc,
943 NULL_TREE);
944 else
945 result = m2statement_BuildProcedureCallTree (location, rightproc,
946 NULL_TREE);
947 else
948 result = m2statement_BuildProcedureCallTree (location, varproc,
949 NULL_TREE);
950 add_stmt (location, result);
951 }
952 }
953
954 /* Print a warning if a constant expression had overflow in folding.
955 Invoke this function on every expression that the language requires
956 to be a constant expression. */
957
958 void
959 m2expr_ConstantExpressionWarning (tree value)
960 {
961 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
962 || TREE_CODE (value) == FIXED_CST || TREE_CODE (value) == VECTOR_CST
963 || TREE_CODE (value) == COMPLEX_CST)
964 && TREE_OVERFLOW (value))
965 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
966 }
967
968 /* TreeOverflow return true if the contant expression, t, has caused
969 an overflow. No error message or warning is emitted and no
970 modification is made to, t. */
971
972 bool
973 m2expr_TreeOverflow (tree t)
974 {
975 if ((TREE_CODE (t) == INTEGER_CST
976 || (TREE_CODE (t) == COMPLEX_CST
977 && TREE_CODE (TREE_REALPART (t)) == INTEGER_CST))
978 && TREE_OVERFLOW (t))
979 return true;
980 else if ((TREE_CODE (t) == REAL_CST
981 || (TREE_CODE (t) == COMPLEX_CST
982 && TREE_CODE (TREE_REALPART (t)) == REAL_CST))
983 && TREE_OVERFLOW (t))
984 return true;
985 else
986 return false;
987 }
988
989 /* RemoveOverflow if tree, t, is a constant expression it removes any
990 overflow flag and returns, t. */
991
992 tree
993 m2expr_RemoveOverflow (tree t)
994 {
995 if (TREE_CODE (t) == INTEGER_CST
996 || (TREE_CODE (t) == COMPLEX_CST
997 && TREE_CODE (TREE_REALPART (t)) == INTEGER_CST))
998 TREE_OVERFLOW (t) = 0;
999 else if (TREE_CODE (t) == REAL_CST
1000 || (TREE_CODE (t) == COMPLEX_CST
1001 && TREE_CODE (TREE_REALPART (t)) == REAL_CST))
1002 TREE_OVERFLOW (t) = 0;
1003 return t;
1004 }
1005
1006 /* BuildCoerce return a tree containing the expression, expr, after
1007 it has been coersed to, type. */
1008
1009 tree
1010 m2expr_BuildCoerce (location_t location, tree des, tree type, tree expr)
1011 {
1012 tree copy = copy_node (expr);
1013 TREE_TYPE (copy) = type;
1014
1015 m2assert_AssertLocation (location);
1016
1017 return m2treelib_build_modify_expr (location, des, NOP_EXPR, copy);
1018 }
1019
1020 /* BuildTrunc return an integer expression from a REAL or LONGREAL op1. */
1021
1022 tree
1023 m2expr_BuildTrunc (tree op1)
1024 {
1025 return convert_to_integer (m2type_GetIntegerType (),
1026 m2expr_FoldAndStrip (op1));
1027 }
1028
1029 /* checkUnaryWholeOverflow decide if we can check this unary expression. */
1030
1031 tree
1032 m2expr_checkUnaryWholeOverflow (location_t location, enum tree_code code,
1033 tree arg, tree lowest, tree min, tree max)
1034 {
1035 if (M2Options_GetWholeValueCheck () && (min != NULL))
1036 {
1037 lowest = m2tree_skip_type_decl (lowest);
1038 arg = fold_convert_loc (location, lowest, arg);
1039
1040 switch (code)
1041 {
1042 case NEGATE_EXPR:
1043 return checkWholeNegateOverflow (location, arg, lowest, min, max);
1044 default:
1045 return NULL;
1046 }
1047 }
1048 return NULL;
1049 }
1050
1051 /* build_unary_op return a unary tree node. */
1052
1053 tree
1054 m2expr_build_unary_op_check (location_t location, enum tree_code code,
1055 tree arg, tree lowest, tree min, tree max)
1056 {
1057 tree argtype = TREE_TYPE (arg);
1058 tree result;
1059 tree check = NULL;
1060
1061 m2assert_AssertLocation (location);
1062
1063 arg = m2expr_FoldAndStrip (arg);
1064
1065 if ((TREE_CODE (argtype) != REAL_TYPE) && (min != NULL))
1066 check = m2expr_checkUnaryWholeOverflow (location, code, arg, lowest, min, max);
1067
1068 result = build1 (code, argtype, arg);
1069 protected_set_expr_location (result, location);
1070
1071 if (check != NULL)
1072 result = build2 (COMPOUND_EXPR, argtype, check, result);
1073
1074 if (SCALAR_FLOAT_TYPE_P (argtype))
1075 m2expr_checkRealOverflow (location, code, result);
1076
1077 return m2expr_FoldAndStrip (result);
1078 }
1079
1080 /* build_unary_op return a unary tree node. */
1081
1082 tree
1083 m2expr_build_unary_op (location_t location, enum tree_code code, tree arg,
1084 int flag ATTRIBUTE_UNUSED)
1085 {
1086 tree argtype = TREE_TYPE (arg);
1087 tree result;
1088
1089 m2assert_AssertLocation (location);
1090
1091 arg = m2expr_FoldAndStrip (arg);
1092 result = build1 (code, argtype, arg);
1093 protected_set_expr_location (result, location);
1094
1095 return m2expr_FoldAndStrip (result);
1096 }
1097
1098 /* build_binary_op is a heavily pruned version of the one found in
1099 c-typeck.cc. The Modula-2 expression rules are much more restricted
1100 than C. */
1101
1102 tree
1103 build_binary_op (location_t location, enum tree_code code, tree op1, tree op2,
1104 int convert ATTRIBUTE_UNUSED)
1105 {
1106 tree type1 = TREE_TYPE (op1);
1107 tree result;
1108
1109 m2assert_AssertLocation (location);
1110
1111 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1112 STRIP_TYPE_NOPS (op1);
1113 STRIP_TYPE_NOPS (op2);
1114
1115 op1 = m2expr_FoldAndStrip (op1);
1116 op2 = m2expr_FoldAndStrip (op2);
1117
1118 result = build2 (code, type1, op1, op2);
1119 protected_set_expr_location (result, location);
1120
1121 return m2expr_FoldAndStrip (result);
1122 }
1123
1124 /* BuildLessThanZero - returns a tree containing (< value 0). It
1125 checks the min and max value to ensure that the test can be safely
1126 achieved and will short circuit the result otherwise. */
1127
1128 tree
1129 m2expr_BuildLessThanZero (location_t location, tree value, tree type, tree min,
1130 tree max)
1131 {
1132 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) >= 0)
1133 /* min is greater than or equal to zero therefore value will always
1134 be >= 0. */
1135 return m2type_GetBooleanFalse ();
1136 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) == -1)
1137 /* max is less than zero therefore value will always be < 0. */
1138 return m2type_GetBooleanTrue ();
1139 /* We now know 0 lies in the range min..max so we can safely cast
1140 zero to type. */
1141 return m2expr_BuildLessThan (
1142 location, value,
1143 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1144 }
1145
1146 /* BuildGreaterThanZero - returns a tree containing (> value 0). It
1147 checks the min and max value to ensure that the test can be safely
1148 achieved and will short circuit the result otherwise. */
1149
1150 tree
1151 m2expr_BuildGreaterThanZero (location_t location, tree value, tree type,
1152 tree min, tree max)
1153 {
1154 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
1155 /* min is greater than zero therefore value will always be > 0. */
1156 return m2type_GetBooleanTrue ();
1157 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) <= 0)
1158 /* max is less than or equal to zero therefore value will always be
1159 <= 0. */
1160 return m2type_GetBooleanFalse ();
1161 /* We now know 0 lies in the range min..max so we can safely cast
1162 zero to type. */
1163 return m2expr_BuildGreaterThan (
1164 location, value,
1165 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1166 }
1167
1168 /* BuildEqualToZero - returns a tree containing (= value 0). It
1169 checks the min and max value to ensure that the test can be safely
1170 achieved and will short circuit the result otherwise. */
1171
1172 tree
1173 m2expr_BuildEqualToZero (location_t location, tree value, tree type, tree min,
1174 tree max)
1175 {
1176 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
1177 /* min is greater than zero therefore value will always be > 0. */
1178 return m2type_GetBooleanFalse ();
1179 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
1180 /* max is less than or equal to zero therefore value will always be <
1181 0. */
1182 return m2type_GetBooleanFalse ();
1183 /* We now know 0 lies in the range min..max so we can safely cast
1184 zero to type. */
1185 return m2expr_BuildEqualTo (
1186 location, value,
1187 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1188 }
1189
1190 /* BuildNotEqualToZero - returns a tree containing (# value 0). It
1191 checks the min and max value to ensure that the test can be safely
1192 achieved and will short circuit the result otherwise. */
1193
1194 tree
1195 m2expr_BuildNotEqualToZero (location_t location, tree value, tree type,
1196 tree min, tree max)
1197 {
1198 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
1199 /* min is greater than zero therefore value will always be true. */
1200 return m2type_GetBooleanTrue ();
1201 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
1202 /* max is less than or equal to zero therefore value will always be
1203 true. */
1204 return m2type_GetBooleanTrue ();
1205 /* We now know 0 lies in the range min..max so we can safely cast
1206 zero to type. */
1207 return m2expr_BuildNotEqualTo (
1208 location, value,
1209 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1210 }
1211
1212
1213 /* BuildGreaterThanOrEqualZero - returns a tree containing (>= value 0). It
1214 checks the min and max value to ensure that the test can be safely
1215 achieved and will short circuit the result otherwise. */
1216
1217 tree
1218 m2expr_BuildGreaterThanOrEqualZero (location_t location, tree value, tree type,
1219 tree min, tree max)
1220 {
1221 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) >= 0)
1222 /* min is greater than or equal to zero therefore value will always be >= 0. */
1223 return m2type_GetBooleanTrue ();
1224 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
1225 /* max is less than zero therefore value will always be < 0. */
1226 return m2type_GetBooleanFalse ();
1227 /* We now know 0 lies in the range min..max so we can safely cast
1228 zero to type. */
1229 return m2expr_BuildGreaterThan (
1230 location, value,
1231 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1232 }
1233
1234
1235 /* BuildLessThanOrEqualZero - returns a tree containing (<= value 0). It
1236 checks the min and max value to ensure that the test can be safely
1237 achieved and will short circuit the result otherwise. */
1238
1239 tree
1240 m2expr_BuildLessThanOrEqualZero (location_t location, tree value, tree type,
1241 tree min, tree max)
1242 {
1243 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) > 0)
1244 /* min is greater than zero therefore value will always be > 0. */
1245 return m2type_GetBooleanFalse ();
1246 else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) <= 0)
1247 /* max is less than or equal to zero therefore value will always be <= 0. */
1248 return m2type_GetBooleanTrue ();
1249 /* We now know 0 lies in the range min..max so we can safely cast
1250 zero to type. */
1251 return m2expr_BuildLessThanOrEqual (
1252 location, value,
1253 fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
1254 }
1255
1256
1257 /* get_current_function_name, return the name of the current function if
1258 it currently exists. NULL is returned if we are not inside a function. */
1259
1260 static const char *
1261 get_current_function_name (void)
1262 {
1263 if (current_function_decl != NULL
1264 && (DECL_NAME (current_function_decl) != NULL)
1265 && (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)) != NULL))
1266 return IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
1267 return NULL;
1268 }
1269
1270 /* checkWholeNegateOverflow - check to see whether -arg will overflow
1271 an integer.
1272
1273 PROCEDURE sneg (i: INTEGER) ;
1274 BEGIN
1275 IF i = MIN(INTEGER)
1276 THEN
1277 'integer overflow'
1278 END
1279 END sneg ;
1280
1281 general purpose subrange type, i, is currently legal, min is
1282 MIN(type) and max is MAX(type).
1283
1284 PROCEDURE sneg (i: type) ;
1285 BEGIN
1286 max := MAX (type) ;
1287 min := MIN (type) ;
1288 (* cannot overflow if i is 0 *)
1289 IF (i#0) AND
1290 (* will overflow if entire range is positive. *)
1291 ((min >= 0) OR
1292 (* will overflow if entire range is negative. *)
1293 (max <= 0) OR
1294 (* c7 and c8 and c9 and c10 -> c17 more units positive. *)
1295 ((min < 0) AND (max > 0) AND ((min + max) > 0) AND (i > -min)) OR
1296 (* c11 and c12 and c13 and c14 -> c18 more units negative. *)
1297 ((min < 0) AND (max > 0) AND ((min + max) < 0) AND (i < -max)))
1298 THEN
1299 'type overflow'
1300 END
1301 END sneg ; */
1302
1303 static tree
1304 checkWholeNegateOverflow (location_t location,
1305 tree i, tree type, tree min,
1306 tree max)
1307 {
1308 tree a1
1309 = m2expr_BuildNotEqualToZero (location, i, type, min, max); /* i # 0. */
1310 tree c1 = m2expr_BuildGreaterThanZero (location, min, type, min,
1311 max); /* min > 0. */
1312 tree c2 = m2expr_BuildEqualToZero (location, min, type, min,
1313 max); /* min == 0. */
1314 tree c4 = m2expr_BuildLessThanZero (location, max, type, min,
1315 max); /* max < 0. */
1316 tree c5 = m2expr_BuildEqualToZero (location, max, type, min,
1317 max); /* max == 0. */
1318 tree c7 = m2expr_BuildLessThanZero (location, min, type, min,
1319 max); /* min < 0. */
1320 tree c8 = m2expr_BuildGreaterThanZero (location, max, type, min,
1321 max); /* max > 0. */
1322 tree c9 = m2expr_BuildGreaterThanZero (
1323 location, m2expr_BuildAdd (location, min, max, false), type, min,
1324 max); /* min + max > 0. */
1325 tree c10 = m2expr_BuildGreaterThan (
1326 location, i, m2expr_BuildNegate (location, min, false)); /* i > -min. */
1327 tree c11 = m2expr_BuildLessThanZero (
1328 location, m2expr_BuildAdd (location, min, max, false), type, min,
1329 max); /* min + max < 0. */
1330 tree c12 = m2expr_BuildLessThan (
1331 location, i, m2expr_BuildNegate (location, max, false)); /* i < -max. */
1332
1333 tree b1 = m2expr_BuildTruthOrIf (location, c1, c2);
1334 tree b2 = m2expr_BuildTruthOrIf (location, c8, c5);
1335 tree o1 = m2expr_BuildTruthAndIf (location, b1, b2);
1336
1337 tree b3 = m2expr_BuildTruthOrIf (location, c7, c2);
1338 tree b4 = m2expr_BuildTruthOrIf (location, c4, c5);
1339 tree o2 = m2expr_BuildTruthAndIf (location, b3, b4);
1340
1341 tree o3 = m2expr_Build4TruthAndIf (location, c7, c8, c9, c10);
1342 tree o4 = m2expr_Build4TruthAndIf (location, c7, c8, c11, c12);
1343
1344 tree a2 = m2expr_Build4TruthOrIf (location, o1, o2, o3, o4);
1345 tree condition
1346 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, a1, a2));
1347
1348 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1349 get_current_function_name (),
1350 "whole value unary minus will cause range overflow");
1351 return t;
1352 }
1353
1354 /* checkWholeAddOverflow - check to see whether op1 + op2 will
1355 overflow an integer.
1356
1357 PROCEDURE sadd (i, j: INTEGER) ;
1358 BEGIN
1359 IF ((j>0) AND (i > MAX(INTEGER)-j)) OR ((j<0) AND (i < MIN(INTEGER)-j))
1360 THEN
1361 'signed addition overflow'
1362 END
1363 END sadd. */
1364
1365 static tree
1366 checkWholeAddOverflow (location_t location, tree i, tree j, tree lowest,
1367 tree min, tree max)
1368 {
1369 tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
1370 tree i_gt_max_sub_j = m2expr_BuildGreaterThan (
1371 location, i, m2expr_BuildSub (location, max, j, false));
1372 tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
1373 tree i_lt_min_sub_j = m2expr_BuildLessThan (location, i,
1374 m2expr_BuildSub (location, min, j, false));
1375 tree lhs_or = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, j_gt_zero, i_gt_max_sub_j));
1376 tree rhs_or = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, j_lt_zero, i_lt_min_sub_j));
1377 tree condition
1378 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, lhs_or, rhs_or));
1379 tree result = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1380 get_current_function_name (),
1381 "whole value addition will cause a range overflow");
1382 return result;
1383 }
1384
1385 /* checkWholeSubOverflow - check to see whether op1 - op2 will
1386 overflow an integer.
1387
1388 PROCEDURE ssub (i, j: INTEGER) ;
1389 BEGIN
1390 IF ((j>0) AND (i < MIN(INTEGER)+j)) OR ((j<0) AND (i > MAX(INTEGER)+j))
1391 THEN
1392 'signed subtraction overflow'
1393 END
1394 END ssub. */
1395
1396 static tree
1397 checkWholeSubOverflow (location_t location, tree i, tree j, tree lowest,
1398 tree min, tree max)
1399 {
1400 tree c1 = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
1401 tree c2 = m2expr_BuildLessThan (location, i,
1402 m2expr_BuildAdd (location, min, j, false));
1403 tree c3 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
1404 tree c4 = m2expr_BuildGreaterThan (location, i,
1405 m2expr_BuildAdd (location, max, j, false));
1406 tree c5 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, c1, c2));
1407 tree c6 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, c3, c4));
1408 tree condition
1409 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, c5, c6));
1410 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1411 get_current_function_name (),
1412 "whole value subtraction will cause a range overflow");
1413 return t;
1414 }
1415
1416 /* Build4TruthAndIf - return true if a && b && c && d. Retain order left to
1417 * right. */
1418
1419 static tree
1420 m2expr_Build4TruthAndIf (location_t location, tree a, tree b, tree c, tree d)
1421 {
1422 tree t1 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, a, b));
1423 tree t2 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t1, c));
1424 return m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t2, d));
1425 }
1426
1427 /* Build3TruthAndIf - return true if a && b && c. Retain order left to right.
1428 */
1429
1430 static tree
1431 m2expr_Build3TruthAndIf (location_t location, tree op1, tree op2, tree op3)
1432 {
1433 tree t = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, op1, op2));
1434 return m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t, op3));
1435 }
1436
1437 /* Build3TruthOrIf - return true if a || b || c. Retain order left to right.
1438 */
1439
1440 static tree
1441 m2expr_Build3TruthOrIf (location_t location, tree op1, tree op2, tree op3)
1442 {
1443 tree t = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, op1, op2));
1444 return m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t, op3));
1445 }
1446
1447 /* Build4TruthOrIf - return true if op1 || op2 || op3 || op4. Retain order
1448 left to right. */
1449
1450 static tree
1451 m2expr_Build4TruthOrIf (location_t location, tree op1, tree op2, tree op3,
1452 tree op4)
1453 {
1454 tree t1 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, op1, op2));
1455 tree t2 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t1, op3));
1456 return m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t2, op4));
1457 }
1458
1459 /* Build4LogicalOr - return true if op1 || op2 || op3 || op4. */
1460
1461 static tree
1462 m2expr_Build4LogicalOr (location_t location, tree op1, tree op2, tree op3,
1463 tree op4)
1464 {
1465 tree t1 = m2expr_FoldAndStrip (
1466 m2expr_BuildLogicalOr (location, op1, op2, false));
1467 tree t2
1468 = m2expr_FoldAndStrip (m2expr_BuildLogicalOr (location, t1, op3, false));
1469 return m2expr_FoldAndStrip (
1470 m2expr_BuildLogicalOr (location, t2, op4, false));
1471 }
1472
1473 /* checkWholeMultOverflow - check to see whether i * j will overflow
1474 an integer.
1475
1476 PROCEDURE smult (lhs, rhs: INTEGER) ;
1477 BEGIN
1478 IF ((lhs > 0) AND (rhs > 0) AND (lhs > max DIV rhs)) OR
1479 ((lhs > 0) AND (rhs < 0) AND (rhs < min DIV lhs)) OR
1480 ((lhs < 0) AND (rhs > 0) AND (lhs < min DIV rhs)) OR
1481 ((lhs < 0) AND (rhs < 0) AND (lhs < max DIV rhs))
1482 THEN
1483 error ('signed multiplication overflow')
1484 END
1485 END smult ;
1486
1487 if ((c1 && c3 && c4)
1488 || (c1 && c5 && c6)
1489 || (c2 && c3 && c7)
1490 || (c2 && c5 && c8))
1491 error ('signed subtraction overflow'). */
1492
1493 static tree
1494 testWholeMultOverflow (location_t location, tree lhs, tree rhs,
1495 tree lowest, tree min, tree max)
1496 {
1497 tree c1 = m2expr_BuildGreaterThanZero (location, lhs, lowest, min, max);
1498 tree c2 = m2expr_BuildLessThanZero (location, lhs, lowest, min, max);
1499
1500 tree c3 = m2expr_BuildGreaterThanZero (location, rhs, lowest, min, max);
1501 tree c4 = m2expr_BuildGreaterThan (
1502 location, lhs, m2expr_BuildDivTrunc (location, max, rhs, false));
1503
1504 tree c5 = m2expr_BuildLessThanZero (location, rhs, lowest, min, max);
1505 tree c6 = m2expr_BuildLessThan (
1506 location, rhs, m2expr_BuildDivTrunc (location, min, lhs, false));
1507 tree c7 = m2expr_BuildLessThan (
1508 location, lhs, m2expr_BuildDivTrunc (location, min, rhs, false));
1509 tree c8 = m2expr_BuildLessThan (
1510 location, lhs, m2expr_BuildDivTrunc (location, max, rhs, false));
1511
1512 tree c9 = m2expr_Build3TruthAndIf (location, c1, c3, c4);
1513 tree c10 = m2expr_Build3TruthAndIf (location, c1, c5, c6);
1514 tree c11 = m2expr_Build3TruthAndIf (location, c2, c3, c7);
1515 tree c12 = m2expr_Build3TruthAndIf (location, c2, c5, c8);
1516
1517 tree condition = m2expr_Build4LogicalOr (location, c9, c10, c11, c12);
1518 return condition;
1519 }
1520
1521
1522 static tree
1523 checkWholeMultOverflow (location_t location, tree i, tree j, tree lowest,
1524 tree min, tree max)
1525 {
1526 tree condition = testWholeMultOverflow (location, i, j, lowest, min, max);
1527 tree result = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1528 get_current_function_name (),
1529 "whole value multiplication will cause a range overflow");
1530 return result;
1531 }
1532
1533
1534 static tree
1535 divMinUnderflow (location_t location, tree value, tree lowest, tree min, tree max)
1536 {
1537 tree min2 = m2expr_BuildMult (location, min, min, false);
1538 tree rhs = m2expr_BuildGreaterThanOrEqual (location, value, min2);
1539 tree lhs = testWholeMultOverflow (location, min, min, lowest, min, max);
1540 return m2expr_BuildTruthAndIf (location, lhs, rhs);
1541 }
1542
1543 /*
1544 divexpr - returns true if a DIV_TRUNC b will overflow.
1545 */
1546
1547 /* checkWholeDivOverflow - check to see whether i DIV_TRUNC j will overflow
1548 an integer. The Modula-2 implementation of the GCC trees follows:
1549
1550 PROCEDURE divtruncexpr (a, b: INTEGER) : BOOLEAN ;
1551 BEGIN
1552 (* Firstly catch division by 0. *)
1553 RETURN ((b = 0) OR
1554 (* Case 2 range is always negative. *)
1555 (* In which case a division will be illegal as result will be positive. *)
1556 (max < 0) OR
1557 (* Case 1 both min / max are positive, check for underflow. *)
1558 ((min >= 0) AND (max >= 0) AND (multMinOverflow (b) OR (a < b * min))) OR
1559 (* Case 1 both min / max are positive, check for overflow. *)
1560 ((min >= 0) AND (max >= 0) AND (divMinUnderflow (a) OR (b > a DIV min))) OR
1561 (* Case 3 mixed range, need to check underflow. *)
1562 ((min < 0) AND (max >= 0) AND (a < 0) AND (b < 0) AND (b >= a DIV min)) OR
1563 ((min < 0) AND (max >= 0) AND (a < 0) AND (b > 0) AND (b <= a DIV max)) OR
1564 ((min < 0) AND (max >= 0) AND (a >= 0) AND (b < 0) AND (a DIV b < min)))
1565 END divtruncexpr ;
1566
1567 s1 -> a DIV min
1568 s2 -> a DIV max
1569 s3 -> a DIV b
1570
1571 b4 -> (min >= 0) AND (max >= 0)
1572 b5 -> (min < 0) AND (max >= 0)
1573 a_lt_b_mult_min -> (a < b * min)
1574 b_mult_min_overflow -> testWholeMultOverflow (location, b, min, lowest, min, max)
1575 b6 -> (b_mult_min_overflow OR a_lt_b_mult_min)
1576 b_gt_s1 -> (b > s1)
1577 a_div_min_overflow -> divMinUnderflow (location, a, min, lowest, min, max)
1578 b7 -> (a_div_min_overflow OR b_gt_s1)
1579 b8 -> (a < 0)
1580 b9 -> (b < 0)
1581 b10 -> (b > 0)
1582 b11 -> (b >= s1)
1583 b12 -> (b <= s2)
1584 b13 -> (s3 < min)
1585 b14 -> a >= 0
1586
1587 c1 -> (b = 0)
1588 c2 -> (max < 0)
1589 c3 -> (b4 AND b6)
1590 c4 -> (b4 AND b7)
1591 c5 -> (b5 AND b8 AND b9 AND b11)
1592 c6 -> (b5 AND b8 AND b10 AND b12)
1593 c7 -> (b5 AND b14 AND b9 AND b13)
1594
1595 if (c1 || c2 || c3 || c4 || c5 || c6 || c7)
1596 error ('signed div trunc overflow'). */
1597
1598 static tree
1599 checkWholeDivTruncOverflow (location_t location, tree i, tree j, tree lowest,
1600 tree min, tree max)
1601 {
1602 tree b4a = m2expr_BuildGreaterThanOrEqualZero (location, min, lowest, min, max);
1603 tree b4b = m2expr_BuildGreaterThanOrEqualZero (location, max, lowest, min, max);
1604 tree b4 = m2expr_BuildTruthAndIf (location, b4a, b4b);
1605 tree b5a = m2expr_BuildLessThanZero (location, min, lowest, min, max);
1606 tree b5 = m2expr_BuildTruthAndIf (location, b5a, b4b);
1607 tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
1608 tree c2 = m2expr_BuildLessThanZero (location, max, lowest, min, max);
1609 tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, m2expr_BuildMult (location, j, min, false));
1610 tree j_mult_min_overflow = testWholeMultOverflow (location, j, min, lowest, min, max);
1611 tree b6 = m2expr_BuildTruthOrIf (location, j_mult_min_overflow, i_lt_j_mult_min);
1612 tree c3 = m2expr_BuildTruthAndIf (location, b4, b6);
1613 tree s1 = m2expr_BuildDivTrunc (location, i, min, false);
1614 tree s2 = m2expr_BuildDivTrunc (location, i, max, false);
1615 tree s3 = m2expr_BuildDivTrunc (location, i, j, false);
1616
1617 tree j_gt_s1 = m2expr_BuildGreaterThan (location, j, s1);
1618 tree i_div_min_overflow = divMinUnderflow (location, i, lowest, min, max);
1619 tree b7 = m2expr_BuildTruthOrIf (location, i_div_min_overflow, j_gt_s1);
1620 tree c4 = m2expr_BuildTruthAndIf (location, b4, b7);
1621 tree b8 = m2expr_BuildLessThanZero (location, i, lowest, min, max);
1622 tree b9 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
1623 tree b10 = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
1624 tree b11 = m2expr_BuildGreaterThanOrEqual (location, j, s1);
1625 tree b12 = m2expr_BuildLessThanOrEqual (location, j, s2);
1626 tree b13 = m2expr_BuildLessThan (location, s3, min);
1627 tree b14 = m2expr_BuildGreaterThanOrEqualZero (location, i, lowest, min, max);
1628 tree c5 = m2expr_Build4TruthAndIf (location, b5, b8, b9, b11);
1629 tree c6 = m2expr_Build4TruthAndIf (location, b5, b8, b10, b12);
1630 tree c7 = m2expr_Build4TruthAndIf (location, b5, b14, b9, b13);
1631 tree c8 = m2expr_Build4TruthOrIf (location, c1, c2, c3, c4);
1632 tree condition = m2expr_Build4TruthOrIf (location, c5, c6, c7, c8);
1633 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1634 get_current_function_name (),
1635 "whole value truncated division will cause a range overflow");
1636 return t;
1637 }
1638
1639 #if 0
1640 (*
1641 divexpr - returns true if a DIV_CEIL b will overflow.
1642 *)
1643
1644 (* checkWholeDivCeilOverflow - check to see whether i DIV_CEIL j will overflow
1645 an integer. *)
1646
1647 PROCEDURE divceilexpr (i, j: INTEGER) : BOOLEAN ;
1648 BEGIN
1649 RETURN ((j = 0) OR (* division by zero. *)
1650 (maxT < 0) OR (* both inputs are < 0 and max is < 0,
1651 therefore error. *)
1652 ((i # 0) AND (* first operand is legally zero,
1653 result is also legally zero. *)
1654 divCeilOverflowCases (i, j)))
1655 END divceilexpr ;
1656
1657
1658 (*
1659 divCeilOverflowCases - precondition: i, j are in range values.
1660 postcondition: true is returned if i divceil will
1661 result in an overflow/underflow.
1662 *)
1663
1664 PROCEDURE divCeilOverflowCases (i, j: INTEGER) : BOOLEAN ;
1665 BEGIN
1666 RETURN (((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j)) OR
1667 ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j)) OR
1668 ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j)) OR
1669 ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j)))
1670 END divCeilOverflowCases ;
1671
1672
1673 (*
1674 divCeilOverflowPosPos - precondition: i, j are legal and are both >= 0.
1675 postcondition: true is returned if i divceil will
1676 result in an overflow/underflow.
1677 *)
1678
1679 PROCEDURE divCeilOverflowPosPos (i, j: INTEGER) : BOOLEAN ;
1680 BEGIN
1681 RETURN (((i MOD j = 0) AND (i < j * minT)) OR
1682 (((i MOD j # 0) AND (i < j * minT + 1))))
1683 END divCeilOverflowPosPos ;
1684
1685
1686 (*
1687 divCeilOverflowNegNeg - precondition: i, j are in range values and both < 0.
1688 postcondition: true is returned if i divceil will
1689 result in an overflow/underflow.
1690 *)
1691
1692 PROCEDURE divCeilOverflowNegNeg (i, j: INTEGER) : BOOLEAN ;
1693 BEGIN
1694 RETURN ((maxT <= 0) OR (* signs will cause overflow. *)
1695 (* check for underflow. *)
1696 ((ABS (i) MOD ABS (j) = 0) AND (i >= j * minT)) OR
1697 ((ABS (i) MOD ABS (j) # 0) AND (i >= j * minT - 1)) OR
1698 (* check for overflow. *)
1699 (((ABS (i) MOD maxT) = 0) AND (ABS (i) DIV maxT > ABS (j))) OR
1700 (((ABS (i) MOD maxT) # 0) AND (ABS (i) DIV maxT > ABS (j) + 1)))
1701 END divCeilOverflowNegNeg ;
1702
1703
1704 (*
1705 divCeilOverflowNegPos - precondition: i, j are in range values. i < 0, j >= 0.
1706 postcondition: true is returned if i divceil will
1707 result in an overflow/underflow.
1708 *)
1709
1710 PROCEDURE divCeilOverflowNegPos (i, j: INTEGER) : BOOLEAN ;
1711 BEGIN
1712 (* easier than might be initially expected. We know minT < 0 and maxT > 0.
1713 We know the result will be negative and therefore we only need to test
1714 against minT. *)
1715 RETURN (((ABS (i) MOD j = 0) AND (i < j * minT)) OR
1716 ((ABS (i) MOD j # 0) AND (i < j * minT - 1)))
1717 END divCeilOverflowNegPos ;
1718
1719
1720 (*
1721 divCeilOverflowPosNeg - precondition: i, j are in range values. i >= 0, j < 0.
1722 postcondition: true is returned if i divceil will
1723 result in an overflow/underflow.
1724 *)
1725
1726 PROCEDURE divCeilOverflowPosNeg (i, j: INTEGER) : BOOLEAN ;
1727 BEGIN
1728 (* easier than might be initially expected. We know minT < 0 and maxT > 0.
1729 We know the result will be negative and therefore we only need to test
1730 against minT. *)
1731 RETURN (((i MOD ABS (j) = 0) AND (i > j * minT)) OR
1732 ((i MOD ABS (j) # 0) AND (i > j * minT - 1)))
1733 END divCeilOverflowPosNeg ;
1734 #endif
1735
1736 /* divCeilOverflowPosPos, precondition: lhs, rhs are legal and are both >= 0.
1737 Postcondition: TRUE is returned if lhs divceil rhs will result
1738 in an overflow/underflow.
1739
1740 A handbuilt expression of trees implementing:
1741
1742 RETURN (((lhs MOD rhs = 0) AND (min >= 0) AND (lhs < rhs * min)) OR (* check for underflow, no remainder. *)
1743 lhs_lt_rhs_mult_min
1744 (((lhs MOD rhs # 0) AND (lhs < rhs * min + 1)))) (* check for underflow with remainder. *)
1745 ((lhs > min) AND (lhs - 1 > rhs * min))
1746 lhs_gt_rhs_mult_min
1747
1748 a -> (lhs MOD rhs = 0) AND (lhs < rhs * min)
1749 b -> (lhs MOD rhs # 0) AND (lhs < rhs * min + 1)
1750 RETURN a OR b. */
1751
1752 static tree
1753 divCeilOverflowPosPos (location_t location, tree i, tree j, tree lowest,
1754 tree min, tree max)
1755 {
1756 tree i_mod_j = m2expr_BuildModTrunc (location, i, j, false);
1757 tree i_mod_j_eq_zero = m2expr_BuildEqualToZero (location, i_mod_j, lowest, min, max);
1758 tree i_mod_j_ne_zero = m2expr_BuildNotEqualToZero (location, i_mod_j, lowest, min, max);
1759 tree j_min = m2expr_BuildMult (location, j, min, false);
1760 tree j_min_1 = m2expr_BuildAdd (location, j_min, m2expr_GetIntegerOne (location), false);
1761 tree i_lt_j_min = m2expr_BuildLessThan (location, i, j_min);
1762 tree i_lt_j_min_1 = m2expr_BuildLessThan (location, i, j_min_1);
1763 tree a = m2expr_BuildTruthAndIf (location, i_mod_j_eq_zero, i_lt_j_min);
1764 tree b = m2expr_BuildTruthAndIf (location, i_mod_j_ne_zero, i_lt_j_min_1);
1765 return m2expr_BuildTruthOrIf (location, a, b);
1766 }
1767
1768
1769 /* divCeilOverflowPosNeg precondition: i, j are in range values and i >=0, j < 0.
1770 Postcondition: TRUE is returned if i divceil j will result in an
1771 overflow/underflow.
1772
1773 A handbuilt expression of trees implementing:
1774
1775 RETURN (((i MOD ABS (j) = 0) AND (i > j * min)) OR
1776 ((i MOD ABS (j) # 0) AND (i > j * min - 1)))
1777
1778 abs_j -> (ABS (j))
1779 i_mod_abs_j -> (i MOD abs_j)
1780 i_mod_abs_j_eq_0 -> (i_mod_abs_j = 0)
1781 i_mod_abs_j_ne_0 -> (i_mod_abs_j # 0)
1782 j_mult_min -> (j * min)
1783 j_mult_min_1 -> (j_mult_min - 1)
1784 i_gt_j_mult_min -> (i > j_mult_min)
1785 i_gt_j_mult_min_1 -> (i > j_mult_min_1)
1786 a -> (i_mod_abs_j_eq_0 AND i_gt_j_mult_min)
1787 b -> (i_mod_abs_j_ne_0 AND i_gt_j_mult_min_1)
1788 c -> (a OR b). */
1789
1790 static tree
1791 divCeilOverflowPosNeg (location_t location, tree i, tree j, tree lowest, tree min, tree max)
1792 {
1793 tree abs_j = m2expr_BuildAbs (location, j);
1794 tree i_mod_abs_j = m2expr_BuildModFloor (location, i, abs_j, false);
1795 tree i_mod_abs_j_eq_0 = m2expr_BuildEqualToZero (location, i_mod_abs_j, lowest, min, max);
1796 tree i_mod_abs_j_ne_0 = m2expr_BuildNotEqualToZero (location, i_mod_abs_j, lowest, min, max);
1797 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
1798 tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
1799 tree i_gt_j_mult_min = m2expr_BuildGreaterThan (location, i, j_mult_min);
1800 tree i_gt_j_mult_min_1 = m2expr_BuildGreaterThan (location, i, j_mult_min_1);
1801 tree a = m2expr_BuildTruthAndIf (location, i_mod_abs_j_eq_0, i_gt_j_mult_min);
1802 tree b = m2expr_BuildTruthAndIf (location, i_mod_abs_j_ne_0, i_gt_j_mult_min_1);
1803 tree c = m2expr_BuildTruthOrIf (location, a, b);
1804 return c;
1805 }
1806
1807
1808 /* divCeilOverflowNegPos precondition: i, j are in range values and i < 0, j >= 0.
1809 Postcondition: TRUE is returned if i divceil j will result in an
1810 overflow/underflow.
1811
1812 A handbuilt expression of trees implementing:
1813
1814 RETURN (((ABS (i) MOD j = 0) AND (i < j * min)) OR
1815 ((ABS (i) MOD j # 0) AND (i < j * min - 1)))
1816
1817 abs_i -> (ABS (i))
1818 abs_i_mod_j -> (abs_i MOD j)
1819 abs_i_mod_j_eq_0 -> (abs_i_mod_j = 0)
1820 abs_i_mod_j_ne_0 -> (abs_i_mod_j # 0)
1821 j_mult_min -> (j * min)
1822 j_mult_min_1 -> (j_mult_min - 1)
1823 i_lt_j_mult_min -> (i < j_mult_min)
1824 i_lt_j_mult_min_1 -> (i < j_mult_min_1)
1825 a = (abs_i_mod_j_eq_0 AND i_lt_j_mult_min)
1826 b = (abs_i_mod_j_ne_0 AND i_lt_j_mult_min_1)
1827 c -> (a OR b). */
1828
1829 static tree
1830 divCeilOverflowNegPos (location_t location, tree i, tree j, tree lowest, tree min, tree max)
1831 {
1832 tree abs_i = m2expr_BuildAbs (location, i);
1833 tree abs_i_mod_j = m2expr_BuildModFloor (location, abs_i, j, false);
1834 tree abs_i_mod_j_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_j, lowest, min, max);
1835 tree abs_i_mod_j_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_j, lowest, min, max);
1836 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
1837 tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
1838 tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
1839 tree i_lt_j_mult_min_1 = m2expr_BuildLessThan (location, i, j_mult_min_1);
1840 tree a = m2expr_BuildTruthAndIf (location, abs_i_mod_j_eq_0, i_lt_j_mult_min);
1841 tree b = m2expr_BuildTruthAndIf (location, abs_i_mod_j_ne_0, i_lt_j_mult_min_1);
1842 tree c = m2expr_BuildTruthOrIf (location, a, b);
1843 return c;
1844 }
1845
1846
1847 /* divCeilOverflowNegNeg precondition: i, j are in range values and both < 0.
1848 Postcondition: TRUE is returned if i divceil j will result in an
1849 overflow/underflow.
1850
1851 A handbuilt expression of trees implementing:
1852
1853 RETURN ((max <= 0) OR (* signs will cause overflow. *)
1854 (* check for underflow. *)
1855 ((ABS (i) MOD ABS (j) = 0) AND (i >= j * min)) OR
1856 ((ABS (i) MOD ABS (j) # 0) AND (i >= j * min - 1)) OR
1857 (* check for overflow. *)
1858 (((ABS (i) MOD max) = 0) AND (ABS (i) DIV max > ABS (j))) OR
1859 (((ABS (i) MOD max) # 0) AND (ABS (i) DIV max > ABS (j) + 1)))
1860
1861 max_lte_0 -> (max <= 0)
1862 abs_i -> (ABS (i))
1863 abs_j -> (ABS (j))
1864 abs_i_mod_abs_j -> (abs_i MOD abs_j)
1865 abs_i_mod_abs_j_eq_0 -> (abs_i_mod_abs_j = 0)
1866 abs_i_mod_abs_j_ne_0 -> (abs_i_mod_abs_j # 0)
1867 j_mult_min -> (j * min)
1868 j_mult_min_1 -> (j_mult_min - 1)
1869 i_ge_j_mult_min -> (i >= j_mult_min)
1870 i_ge_j_mult_min_1 -> (i >= j_mult_min_1)
1871 abs_i_mod_max -> (abs_i mod max)
1872 abs_i_div_max -> (abs_i DIVfloor max)
1873 abs_j_1 -> (abs_j + 1)
1874 abs_i_mod_max_eq_0 -> (abs_i_mod_max = 0)
1875 abs_i_mod_max_ne_0 -> (abs_i_mod_max # 0)
1876 abs_i_div_max_gt_abs_j -> (abs_i_div_max > abs_j)
1877 abs_i_div_max_gt_abs_j_1 -> (abs_i_div_max > abs_j_1)
1878
1879 a -> (abs_i_mod_abs_j_eq_0 AND i_ge_j_mult_min)
1880 b -> (abs_i_mod_abs_j_ne_0 AND i_ge_j_mult_min_1)
1881 c -> (abs_i_mod_max_eq_0 AND abs_i_div_max_gt_abs_j)
1882 d -> (abs_i_mod_max_ne_0 AND abs_i_div_max_gt_abs_j_1)
1883 e -> (a OR b OR c OR d)
1884 return max_lte_0 OR e. */
1885
1886 static tree
1887 divCeilOverflowNegNeg (location_t location, tree i, tree j, tree lowest,
1888 tree min, tree max)
1889 {
1890 tree max_lte_0 = m2expr_BuildLessThanOrEqualZero (location, max, lowest, min, max);
1891 tree abs_i = m2expr_BuildAbs (location, i);
1892 tree abs_j = m2expr_BuildAbs (location, j);
1893 tree abs_i_mod_abs_j = m2expr_BuildModFloor (location, abs_i, abs_j, false);
1894 tree abs_i_mod_abs_j_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_abs_j,
1895 lowest, min, max);
1896 tree abs_i_mod_abs_j_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_abs_j,
1897 lowest, min, max);
1898 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
1899 tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
1900 tree i_ge_j_mult_min = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min);
1901 tree i_ge_j_mult_min_1 = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min_1);
1902 tree abs_i_mod_max = m2expr_BuildModFloor (location, abs_i, max, false);
1903 tree abs_i_div_max = m2expr_BuildDivFloor (location, abs_i, max, false);
1904 tree abs_j_1 = m2expr_BuildPostInc (location, abs_j);
1905 tree abs_i_mod_max_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_max, lowest, min, max);
1906 tree abs_i_mod_max_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_max, lowest, min, max);
1907 tree abs_i_div_max_gt_abs_j = m2expr_BuildGreaterThan (location, abs_i_div_max, abs_j);
1908 tree abs_i_div_max_gt_abs_j_1 = m2expr_BuildGreaterThan (location, abs_i_div_max, abs_j_1);
1909
1910 tree a = m2expr_BuildTruthAndIf (location, abs_i_mod_abs_j_eq_0, i_ge_j_mult_min);
1911 tree b = m2expr_BuildTruthAndIf (location, abs_i_mod_abs_j_ne_0, i_ge_j_mult_min_1);
1912 tree c = m2expr_BuildTruthAndIf (location, abs_i_mod_max_eq_0, abs_i_div_max_gt_abs_j);
1913 tree d = m2expr_BuildTruthAndIf (location, abs_i_mod_max_ne_0, abs_i_div_max_gt_abs_j_1);
1914 tree e = m2expr_Build4TruthOrIf (location, a, b, c, d);
1915 return m2expr_BuildTruthOrIf (location, max_lte_0, e);
1916 }
1917
1918
1919 /* divCeilOverflowCases, precondition: i, j are in range values.
1920 Postcondition: TRUE is returned if i divceil will result in an
1921 overflow/underflow.
1922
1923 A handbuilt expression of trees implementing:
1924
1925 RETURN (((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j)) OR
1926 ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j)) OR
1927 ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j)) OR
1928 ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j)))
1929
1930 a -> ((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j))
1931 b -> ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j))
1932 c -> ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j))
1933 d -> ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j))
1934
1935 RETURN a AND b AND c AND d. */
1936
1937 static tree
1938 divCeilOverflowCases (location_t location, tree i, tree j, tree lowest,
1939 tree min, tree max)
1940 {
1941 tree i_gt_zero = m2expr_BuildGreaterThanZero (location, i, lowest, min, max);
1942 tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
1943 tree i_lt_zero = m2expr_BuildLessThanZero (location, i, lowest, min, max);
1944 tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
1945 tree a = m2expr_Build3TruthAndIf (location, i_gt_zero, j_gt_zero,
1946 divCeilOverflowPosPos (location, i, j, lowest, min, max));
1947 tree b = m2expr_Build3TruthAndIf (location, i_lt_zero, j_lt_zero,
1948 divCeilOverflowNegNeg (location, i, j, lowest, min, max));
1949 tree c = m2expr_Build3TruthAndIf (location, i_gt_zero, j_lt_zero,
1950 divCeilOverflowPosNeg (location, i, j, lowest, min, max));
1951 tree d = m2expr_Build3TruthAndIf (location, i_lt_zero, j_gt_zero,
1952 divCeilOverflowNegPos (location, i, j, lowest, min, max));
1953 return m2expr_Build4TruthOrIf (location, a, b, c, d);
1954 }
1955
1956
1957 /* checkWholeDivCeilOverflow check to see whether i DIV_CEIL j will overflow
1958 an integer. A handbuilt expression of trees implementing:
1959
1960 RETURN ((j = 0) OR (* division by zero. *)
1961 (maxT < 0) OR (* both inputs are < 0 and max is < 0,
1962 therefore error. *)
1963 ((i # 0) AND (* first operand is legally zero,
1964 result is also legally zero. *)
1965 divCeilOverflowCases (i, j)))
1966
1967 using the following subexpressions:
1968
1969 j_eq_zero -> (j == 0)
1970 max_lt_zero -> (max < 0)
1971 i_ne_zero -> (i # 0). */
1972
1973 static tree
1974 checkWholeDivCeilOverflow (location_t location, tree i, tree j, tree lowest,
1975 tree min, tree max)
1976 {
1977 tree j_eq_zero = m2expr_BuildEqualToZero (location, j, lowest, min, max);
1978 tree max_lt_zero = m2expr_BuildLessThanZero (location, max, lowest, min, max);
1979 tree i_ne_zero = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
1980 tree j_lt_zero;
1981 tree rhs = m2expr_BuildTruthAndIf (location,
1982 i_ne_zero,
1983 divCeilOverflowCases (location,
1984 i, j, lowest, min, max));
1985
1986 if (M2Options_GetISO ())
1987 j_lt_zero = m2expr_FoldAndStrip (m2expr_BuildLessThanZero (location, j, lowest, min, max));
1988 else
1989 j_lt_zero = m2expr_GetIntegerZero (location);
1990 j_eq_zero = m2expr_FoldAndStrip (j_eq_zero);
1991 max_lt_zero = m2expr_FoldAndStrip (max_lt_zero);
1992 i_ne_zero = m2expr_FoldAndStrip (i_ne_zero);
1993 rhs = m2expr_FoldAndStrip (rhs);
1994
1995 tree condition = m2expr_Build4TruthOrIf (location, j_eq_zero, max_lt_zero, rhs, j_lt_zero);
1996 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
1997 get_current_function_name (),
1998 "whole value ceil division will cause a range overflow");
1999 return t;
2000 }
2001
2002
2003 /* checkWholeModTruncOverflow, the GCC tree.def defines TRUNC_MOD_EXPR to return
2004 the remainder which has the same sign as the dividend. In ISO Modula-2 the
2005 divisor must never be negative (or zero). The pseudo code for implementing these
2006 checks is given below:
2007
2008 IF j = 0
2009 THEN
2010 RETURN TRUE (* division by zero. *)
2011 ELSIF j < 0
2012 THEN
2013 RETURN TRUE (* modulus and division by negative (rhs) not allowed in ISO Modula-2. *)
2014 ELSIF i = 0
2015 THEN
2016 RETURN FALSE (* must be legal as result is same as operand. *)
2017 ELSIF i > 0
2018 THEN
2019 (* test for: i MOD j < minT *)
2020 IF j > i
2021 THEN
2022 RETURN FALSE
2023 END ;
2024 RETURN i - ((i DIV j) * j) < minT
2025 ELSIF i < 0
2026 THEN
2027 (* the result will always be positive and less than i, given that j is less than zero
2028 we know that minT must be < 0 as well and therefore the result of i MOD j will
2029 never underflow. *)
2030 RETURN FALSE
2031 END ;
2032 RETURN FALSE
2033
2034 which can be converted into a large expression:
2035
2036 RETURN (j = 0) OR ((j < 0) AND ISO) OR
2037 ((i # 0) AND (j <= i) AND (i - ((i DIVtrunc j) * j) < minT)
2038
2039 and into GCC trees:
2040
2041 c1 -> (j = 0)
2042 c2 -> (j < 0) (* only called from ISO or PIM4 or -fpositive-mod-floor *)
2043 c3 -> (i # 0)
2044 c4 -> (j <= i)
2045 c6 -> (i DIVtrunc j)
2046 c7 -> (i - (c6 * j))
2047 c5 -> c7 < minT
2048
2049 t -> (c1 OR c2 OR
2050 (c3 AND c4 AND c5)). */
2051
2052 static tree
2053 checkWholeModTruncOverflow (location_t location, tree i, tree j, tree lowest,
2054 tree min, tree max)
2055 {
2056 tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
2057 tree c2 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
2058 tree c3 = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
2059 tree c4 = m2expr_BuildLessThanOrEqual (location, j, i);
2060 tree c6 = m2expr_BuildDivTrunc (location, i, j, false);
2061 tree c7 = m2expr_BuildSub (location, i, m2expr_BuildMult (location, c6, j, false), false);
2062 tree c5 = m2expr_BuildLessThan (location, c7, min);
2063 tree c8 = m2expr_Build3TruthAndIf (location, c3, c4, c5);
2064 tree condition = m2expr_Build3TruthOrIf (location, c1, c2, c8);
2065 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
2066 get_current_function_name (),
2067 "whole value trunc modulus will cause a range overflow");
2068 return t;
2069 }
2070
2071
2072 /* checkWholeModCeilOverflow, the GCC tree.def defines CEIL_MOD_EXPR to return
2073 the remainder which has the same opposite of the divisor. In gm2 this is
2074 only called when the divisor is negative. The pseudo code for implementing
2075 these checks is given below:
2076
2077 IF j = 0
2078 THEN
2079 RETURN TRUE (* division by zero. *)
2080 END ;
2081 t := i - j * divceil (i, j) ;
2082 printf ("t = %d, i = %d, j = %d, %d / %d = %d\n",
2083 t, i, j, i, j, divceil (i, j));
2084 RETURN NOT ((t >= minT) AND (t <= maxT))
2085
2086 which can be converted into the expression:
2087
2088 t := i - j * divceil (i, j) ;
2089 RETURN (j = 0) OR (NOT ((t >= minT) AND (t <= maxT)))
2090
2091 and into GCC trees:
2092
2093 c1 -> (j = 0)
2094 c2 -> (i - j)
2095 c3 -> (i DIVceil j)
2096 t -> (c2 * c3)
2097 c4 -> (t >= minT)
2098 c5 -> (t <= maxT)
2099 c6 -> (c4 AND c5)
2100 c7 -> (NOT c6)
2101 c8 -> (c1 OR c7)
2102 return c8. */
2103
2104 static tree
2105 checkWholeModCeilOverflow (location_t location,
2106 tree i, tree j, tree lowest,
2107 tree min, tree max)
2108 {
2109 tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
2110 tree c2 = m2expr_BuildSub (location, i, j, false);
2111 tree c3 = m2expr_BuildDivCeil (location, i, j, false);
2112 tree t = m2expr_BuildMult (location, c2, c3, false);
2113 tree c4 = m2expr_BuildGreaterThanOrEqual (location, t, min);
2114 tree c5 = m2expr_BuildLessThanOrEqual (location, t, max);
2115 tree c6 = m2expr_BuildTruthAndIf (location, c4, c5);
2116 tree c7 = m2expr_BuildTruthNot (location, c6);
2117 tree condition = m2expr_BuildTruthOrIf (location, c1, c7);
2118 tree s = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
2119 get_current_function_name (),
2120 "whole value ceil modulus will cause a range overflow");
2121 return s;
2122 }
2123
2124
2125 /* checkWholeModFloorOverflow, the GCC tree.def defines FLOOR_MOD_EXPR to return
2126 the remainder which has the same sign as the divisor. In gm2 this is
2127 only called when the divisor is positive. The pseudo code for implementing
2128 these checks is given below:
2129
2130 IF j = 0
2131 THEN
2132 RETURN TRUE (* division by zero. *)
2133 END ;
2134 t := i - j * divfloor (i, j) ;
2135 printf ("t = %d, i = %d, j = %d, %d / %d = %d\n",
2136 t, i, j, i, j, divfloor (i, j));
2137 RETURN NOT ((t >= minT) AND (t <= maxT))
2138
2139 which can be converted into the expression:
2140
2141 t := i - j * divfloor (i, j) ;
2142 RETURN (j = 0) OR (NOT ((t >= minT) AND (t <= maxT)))
2143
2144 and into GCC trees:
2145
2146 c1 -> (j = 0)
2147 c2 -> (i - j)
2148 c3 -> (i DIVfloor j)
2149 t -> (c2 * c3)
2150 c4 -> (t >= minT)
2151 c5 -> (t <= maxT)
2152 c6 -> (c4 AND c5)
2153 c7 -> (NOT c6)
2154 c8 -> (c1 OR c7)
2155 return c8. */
2156
2157 static tree
2158 checkWholeModFloorOverflow (location_t location,
2159 tree i, tree j, tree lowest,
2160 tree min, tree max)
2161 {
2162 tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
2163 tree c2 = m2expr_BuildSub (location, i, j, false);
2164 tree c3 = m2expr_BuildDivFloor (location, i, j, false);
2165 tree t = m2expr_BuildMult (location, c2, c3, false);
2166 tree c4 = m2expr_BuildGreaterThanOrEqual (location, t, min);
2167 tree c5 = m2expr_BuildLessThanOrEqual (location, t, max);
2168 tree c6 = m2expr_BuildTruthAndIf (location, c4, c5);
2169 tree c7 = m2expr_BuildTruthNot (location, c6);
2170 tree condition = m2expr_BuildTruthOrIf (location, c1, c7);
2171 tree s = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
2172 get_current_function_name (),
2173 "whole value floor modulus will cause a range overflow");
2174 return s;
2175 }
2176
2177
2178 #if 0
2179 /* The following is a Modula-2 implementation of the C tree node code
2180 this code has been hand translated into GCC trees. */
2181
2182 (*
2183 divFloorOverflow2 - returns true if an overflow will occur
2184 if i divfloor j is performed.
2185 *)
2186
2187 PROCEDURE divFloorOverflow (i, j: INTEGER) : BOOLEAN ;
2188 BEGIN
2189 RETURN ((j = 0) OR (* division by zero. *)
2190 (maxT < 0) OR (* both inputs are < 0 and max is < 0,
2191 therefore error. *)
2192 (* --fixme-- remember here to also check
2193 if ISO M2 dialect and j < 0
2194 which will also generate an error. *)
2195 ((i # 0) AND (* first operand is legally zero,
2196 result is also legally zero. *)
2197 divFloorOverflowCases (i, j)))
2198 END divFloorOverflow ;
2199
2200
2201 (*
2202 divFloorOverflowCases - precondition: i, j are in range values.
2203 postcondition: true is returned if i divfloor will
2204 result in an overflow/underflow.
2205 *)
2206
2207 PROCEDURE divFloorOverflowCases (i, j: INTEGER) : BOOLEAN ;
2208 BEGIN
2209 RETURN (((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j)) OR
2210 ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j)) OR
2211 ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j)) OR
2212 ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j)))
2213 END divFloorOverflowCases ;
2214
2215
2216 (*
2217 divFloorOverflowPosPos - precondition: lhs, rhs are legal and are both >= 0.
2218 postcondition: true is returned if lhs divfloor rhs will
2219 result in an overflow/underflow.
2220 *)
2221
2222 PROCEDURE divFloorOverflowPosPos (lhs, rhs: INTEGER) : BOOLEAN ;
2223 BEGIN
2224 RETURN multMinOverflow (rhs) OR (lhs < rhs * min)
2225 END divFloorOverflowPosPos ;
2226
2227
2228 (*
2229 divFloorOverflowNegNeg - precondition: i, j are in range values and both < 0.
2230 postcondition: true is returned if i divfloor will
2231 result in an overflow/underflow.
2232 *)
2233
2234 PROCEDURE divFloorOverflowNegNeg (i, j: INTEGER) : BOOLEAN ;
2235 BEGIN
2236 RETURN ((maxT <= 0) OR (* signs will cause overflow. *)
2237 (* check for underflow. *)
2238 (i >= j * minT) OR
2239 (* check for overflow. *)
2240 (ABS (i) DIV maxT > ABS (j)))
2241 END divFloorOverflowNegNeg ;
2242
2243
2244 (*
2245 divFloorOverflowNegPos - precondition: i, j are in range values. i < 0, j >= 0.
2246 postcondition: true is returned if i divfloor will
2247 result in an overflow/underflow.
2248 *)
2249
2250 PROCEDURE divFloorOverflowNegPos (i, j: INTEGER) : BOOLEAN ;
2251 BEGIN
2252 (* easier than might be initially expected. We know minT < 0 and maxT > 0.
2253 We know the result will be negative and therefore we only need to test
2254 against minT. *)
2255 RETURN i < j * minT
2256 END divFloorOverflowNegPos ;
2257
2258
2259 (*
2260 divFloorOverflowPosNeg - precondition: i, j are in range values. i >= 0, j < 0.
2261 postcondition: true is returned if i divfloor will
2262 result in an overflow/underflow.
2263 *)
2264
2265 PROCEDURE divFloorOverflowPosNeg (i, j: INTEGER) : BOOLEAN ;
2266 BEGIN
2267 (* easier than might be initially expected. We know minT < 0 and maxT > 0.
2268 We know the result will be negative and therefore we only need to test
2269 against minT. *)
2270 RETURN i >= j * minT - j (* is safer than i > j * minT -1 *)
2271 END divFloorOverflowPosNeg ;
2272 #endif
2273
2274
2275 /* divFloorOverflowPosPos, precondition: i, j are legal and are both >= 0.
2276 Postcondition: true is returned if i divfloor will result in an overflow/underflow.
2277
2278 A handbuilt expression of trees implementing:
2279
2280 RETURN i < j * min
2281
2282 j_mult_min -> (j * min)
2283 RETURN i < j_mult_min. */
2284
2285 static tree
2286 divFloorOverflowPosPos (location_t location, tree i, tree j, tree min)
2287 {
2288 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
2289 tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
2290 return i_lt_j_mult_min;
2291 }
2292
2293
2294 /* divFloorOverflowNegNeg precondition: i, j are in range values and both < 0.
2295 Postcondition: true is returned if i divfloor j will result in an
2296 overflow/underflow.
2297
2298 A handbuilt expression of trees implementing:
2299
2300 RETURN ((maxT <= 0) OR (* signs will cause overflow. *)
2301 (* check for underflow. *)
2302 (i >= j * min) OR
2303 (* check for overflow. *)
2304 (ABS (i) DIV max > ABS (j)))
2305
2306 max_lte_0 -> (max <= 0)
2307 abs_i -> (ABS (i))
2308 abs_j -> (ABS (j))
2309 j_mult_min -> (j * min)
2310 i_ge_j_mult_min -> (i >= j_mult_min)
2311 abs_i_div_max -> (abs_i divfloor max)
2312 abs_i_div_max_gt_abs_j -> (abs_i_div_max > abs_j)
2313
2314 return max_lte_0 OR
2315 i_ge_j_mult_min OR
2316 abs_i_div_max_gt_abs_j. */
2317
2318 static tree
2319 divFloorOverflowNegNeg (location_t location, tree i, tree j, tree lowest,
2320 tree min, tree max)
2321 {
2322 tree max_lte_0 = m2expr_BuildLessThanOrEqualZero (location, max, lowest, min, max);
2323 tree abs_i = m2expr_BuildAbs (location, i);
2324 tree abs_j = m2expr_BuildAbs (location, j);
2325 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
2326 tree i_ge_j_mult_min = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min);
2327 tree abs_i_div_max = m2expr_BuildDivFloor (location, abs_i, max, false);
2328 tree abs_i_div_max_gt_abs_j = m2expr_BuildGreaterThan (location, abs_i_div_max, abs_j);
2329
2330 return m2expr_Build3TruthOrIf (location, max_lte_0, i_ge_j_mult_min, abs_i_div_max_gt_abs_j);
2331 }
2332
2333
2334 /* divFloorOverflowPosNeg precondition: i, j are in range values and i >=0, j < 0.
2335 Postcondition: true is returned if i divfloor j will result in an
2336 overflow/underflow.
2337
2338 A handbuilt expression of trees implementing:
2339
2340 RETURN i >= j * min - j (* is safer than i > j * min -1 *)
2341
2342 j_mult_min -> (j * min)
2343 j_mult_min_sub_j -> (j_mult_min - j)
2344 i_ge_j_mult_min_sub_j -> (i >= j_mult_min_sub_j)
2345
2346 return i_ge_j_mult_min_sub_j. */
2347
2348 static tree
2349 divFloorOverflowPosNeg (location_t location, tree i, tree j, tree min)
2350 {
2351 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
2352 tree j_mult_min_sub_j = m2expr_BuildSub (location, j_mult_min, j, false);
2353 tree i_ge_j_mult_min_sub_j = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min_sub_j);
2354 return i_ge_j_mult_min_sub_j;
2355 }
2356
2357
2358 /* divFloorOverflowNegPos precondition: i, j are in range values and i < 0, j > 0.
2359 Postcondition: true is returned if i divfloor j will result in an
2360 overflow/underflow.
2361
2362 A handbuilt expression of trees implementing:
2363
2364 RETURN i < j * min
2365
2366 j_mult_min -> (j * min)
2367 RETURN i < j_mult_min. */
2368
2369 static tree
2370 divFloorOverflowNegPos (location_t location, tree i, tree j, tree min)
2371 {
2372 tree j_mult_min = m2expr_BuildMult (location, j, min, false);
2373 tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
2374 return i_lt_j_mult_min;
2375 }
2376
2377
2378 /* divFloorOverflowCases, precondition: i, j are in range values.
2379 Postcondition: true is returned if i divfloor will result in an
2380 overflow/underflow.
2381
2382 A handbuilt expression of trees implementing:
2383
2384 RETURN (((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j)) OR
2385 ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j)) OR
2386 ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j)) OR
2387 ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j)))
2388
2389 a -> ((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j))
2390 b -> ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j))
2391 c -> ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j))
2392 d -> ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j))
2393
2394 RETURN a AND b AND c AND d. */
2395
2396 static tree
2397 divFloorOverflowCases (location_t location, tree i, tree j, tree lowest,
2398 tree min, tree max)
2399 {
2400 tree i_gt_zero = m2expr_BuildGreaterThanZero (location, i, lowest, min, max);
2401 tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
2402 tree i_lt_zero = m2expr_BuildLessThanZero (location, i, lowest, min, max);
2403 tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
2404 tree a = m2expr_Build3TruthAndIf (location, i_gt_zero, j_gt_zero,
2405 divFloorOverflowPosPos (location, i, j, min));
2406 tree b = m2expr_Build3TruthAndIf (location, i_lt_zero, j_lt_zero,
2407 divFloorOverflowNegNeg (location, i, j, lowest, min, max));
2408 tree c = m2expr_Build3TruthAndIf (location, i_gt_zero, j_lt_zero,
2409 divFloorOverflowPosNeg (location, i, j, min));
2410 tree d = m2expr_Build3TruthAndIf (location, i_lt_zero, j_gt_zero,
2411 divFloorOverflowNegPos (location, i, j, min));
2412 return m2expr_Build4TruthOrIf (location, a, b, c, d);
2413 }
2414
2415
2416 /* checkWholeDivFloorOverflow check to see whether i DIV_FLOOR j will overflow
2417 an integer. A handbuilt expression of trees implementing:
2418
2419 RETURN ((j = 0) OR (* division by zero. *)
2420 (maxT < 0) OR (* both inputs are < 0 and max is < 0,
2421 therefore error. *)
2422 (* we also check
2423 if ISO M2 dialect and j < 0
2424 which will also generate an error. *)
2425 ((i # 0) AND (* first operand is legally zero,
2426 result is also legally zero. *)
2427 divFloorOverflowCases (i, j)))
2428
2429 using the following subexpressions:
2430
2431 j_eq_zero -> (j == 0)
2432 max_lt_zero -> (max < 0)
2433 i_ne_zero -> (i # 0). */
2434
2435 static tree
2436 checkWholeDivFloorOverflow (location_t location, tree i, tree j, tree lowest,
2437 tree min, tree max)
2438 {
2439 tree j_eq_zero = m2expr_BuildEqualToZero (location, j, lowest, min, max);
2440 tree max_lt_zero = m2expr_BuildLessThanZero (location, max, lowest, min, max);
2441 tree i_ne_zero = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
2442 tree j_lt_zero;
2443 tree rhs = m2expr_BuildTruthAndIf (location,
2444 i_ne_zero,
2445 divFloorOverflowCases (location,
2446 i, j, lowest, min, max));
2447
2448 if (M2Options_GetISO ())
2449 /* ISO Modula-2 raises an exception if the right hand operand is < 0. */
2450 j_lt_zero = m2expr_FoldAndStrip (m2expr_BuildLessThanZero (location, j, lowest, min, max));
2451 else
2452 j_lt_zero = m2expr_GetIntegerZero (location);
2453 j_eq_zero = m2expr_FoldAndStrip (j_eq_zero);
2454 max_lt_zero = m2expr_FoldAndStrip (max_lt_zero);
2455 i_ne_zero = m2expr_FoldAndStrip (i_ne_zero);
2456 rhs = m2expr_FoldAndStrip (rhs);
2457
2458 tree condition = m2expr_Build4TruthOrIf (location, j_eq_zero, max_lt_zero, rhs, j_lt_zero);
2459 tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
2460 get_current_function_name (),
2461 "whole value floor division will cause a range overflow");
2462 return t;
2463 }
2464
2465 /* checkWholeOverflow check to see if the binary operators will overflow
2466 ordinal types. */
2467
2468 static tree
2469 m2expr_checkWholeOverflow (location_t location, enum tree_code code, tree op1,
2470 tree op2, tree lowest, tree min, tree max)
2471 {
2472 if (M2Options_GetWholeValueCheck () && (min != NULL))
2473 {
2474 lowest = m2tree_skip_type_decl (lowest);
2475 op1 = fold_convert_loc (location, lowest, op1);
2476 op2 = fold_convert_loc (location, lowest, op2);
2477
2478 switch (code)
2479 {
2480 case PLUS_EXPR:
2481 return checkWholeAddOverflow (location, op1, op2, lowest, min, max);
2482 case MINUS_EXPR:
2483 return checkWholeSubOverflow (location, op1, op2, lowest, min, max);
2484 case MULT_EXPR:
2485 return checkWholeMultOverflow (location, op1, op2, lowest, min, max);
2486 case TRUNC_DIV_EXPR:
2487 return checkWholeDivTruncOverflow (location, op1, op2, lowest, min, max);
2488 case CEIL_DIV_EXPR:
2489 return checkWholeDivCeilOverflow (location, op1, op2, lowest, min, max);
2490 case FLOOR_DIV_EXPR:
2491 return checkWholeDivFloorOverflow (location, op1, op2, lowest, min, max);
2492 case TRUNC_MOD_EXPR:
2493 return checkWholeModTruncOverflow (location, op1, op2, lowest, min, max);
2494 case CEIL_MOD_EXPR:
2495 return checkWholeModCeilOverflow (location, op1, op2, lowest, min, max);
2496 case FLOOR_MOD_EXPR:
2497 return checkWholeModFloorOverflow (location, op1, op2, lowest, min, max);
2498 default:
2499 return NULL;
2500 }
2501 }
2502 return NULL;
2503 }
2504
2505 /* checkRealOverflow if we have enabled real value checking then
2506 generate an overflow check appropriate to the tree code being used. */
2507
2508 static void
2509 m2expr_checkRealOverflow (location_t location, enum tree_code code,
2510 tree result)
2511 {
2512 if (M2Options_GetFloatValueCheck ())
2513 {
2514 tree condition = m2expr_BuildEqualTo (
2515 location, m2builtins_BuiltInIsfinite (location, result),
2516 m2expr_GetIntegerZero (location));
2517 switch (code)
2518 {
2519 case PLUS_EXPR:
2520 m2type_AddStatement (location,
2521 M2Range_BuildIfCallRealHandlerLoc (
2522 location, condition,
2523 get_current_function_name (),
2524 "floating point + has caused an overflow"));
2525 break;
2526 case MINUS_EXPR:
2527 m2type_AddStatement (location,
2528 M2Range_BuildIfCallRealHandlerLoc (
2529 location, condition,
2530 get_current_function_name (),
2531 "floating point - has caused an overflow"));
2532 break;
2533 case RDIV_EXPR:
2534 case FLOOR_DIV_EXPR:
2535 case CEIL_DIV_EXPR:
2536 case TRUNC_DIV_EXPR:
2537 m2type_AddStatement (location,
2538 M2Range_BuildIfCallRealHandlerLoc (
2539 location, condition,
2540 get_current_function_name (),
2541 "floating point / has caused an overflow"));
2542 break;
2543 case MULT_EXPR:
2544 m2type_AddStatement (location,
2545 M2Range_BuildIfCallRealHandlerLoc (
2546 location, condition,
2547 get_current_function_name (),
2548 "floating point * has caused an overflow"));
2549 break;
2550 case NEGATE_EXPR:
2551 m2type_AddStatement (
2552 location, M2Range_BuildIfCallRealHandlerLoc (
2553 location, condition,
2554 get_current_function_name (),
2555 "floating point unary - has caused an overflow"));
2556 default:
2557 break;
2558 }
2559 }
2560 }
2561
2562 /* build_binary_op, a wrapper for the lower level build_binary_op
2563 above. */
2564
2565 tree
2566 m2expr_build_binary_op_check (location_t location, enum tree_code code,
2567 tree op1, tree op2, bool needconvert, tree lowest,
2568 tree min, tree max)
2569 {
2570 tree type1, type2, result;
2571 tree check = NULL;
2572
2573 op1 = m2expr_FoldAndStrip (op1);
2574 op2 = m2expr_FoldAndStrip (op2);
2575
2576 type1 = m2tree_skip_type_decl (TREE_TYPE (op1));
2577 type2 = m2tree_skip_type_decl (TREE_TYPE (op2));
2578
2579 m2assert_AssertLocation (location);
2580
2581 if (code == PLUS_EXPR)
2582 {
2583 if (POINTER_TYPE_P (type1))
2584 {
2585 op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
2586 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
2587 op1, op2);
2588 }
2589 else if (POINTER_TYPE_P (type2))
2590 {
2591 op1 = fold_convert_loc (location, sizetype, unshare_expr (op1));
2592 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op2),
2593 op2, op1);
2594 }
2595 }
2596 if (code == MINUS_EXPR)
2597 {
2598 if (POINTER_TYPE_P (type1))
2599 {
2600 op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
2601 op2 = fold_build1_loc (location, NEGATE_EXPR, sizetype, op2);
2602 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
2603 op1, op2);
2604 }
2605 else if (POINTER_TYPE_P (type2))
2606 {
2607 op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
2608 op2 = fold_build1_loc (location, NEGATE_EXPR, sizetype, op2);
2609 op1 = fold_convert_loc (location, sizetype, unshare_expr (op1));
2610 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op2),
2611 op2, op1);
2612 }
2613 }
2614
2615 if ((code != LSHIFT_EXPR) && (code != RSHIFT_EXPR) && (code != LROTATE_EXPR)
2616 && (code == RROTATE_EXPR))
2617 if (type1 != type2)
2618 error_at (location, "not expecting different types to binary operator");
2619
2620 if ((TREE_CODE (type1) != REAL_TYPE) && (min != NULL))
2621 check = m2expr_checkWholeOverflow (location, code, op1, op2, lowest, min, max);
2622
2623 result = build_binary_op (location, code, op1, op2, needconvert);
2624 if (check != NULL)
2625 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), check, result);
2626
2627 if (SCALAR_FLOAT_TYPE_P (type1))
2628 m2expr_checkRealOverflow (location, code, result);
2629 return result;
2630 }
2631
2632 /* build_binary_op, a wrapper for the lower level build_binary_op
2633 above. */
2634
2635 tree
2636 m2expr_build_binary_op (location_t location, enum tree_code code, tree op1,
2637 tree op2, int convert)
2638 {
2639 return m2expr_build_binary_op_check (location, code, op1, op2, convert, NULL,
2640 NULL, NULL);
2641 }
2642
2643 /* BuildAddAddress return an expression op1+op2 where op1 is a
2644 pointer type and op2 is not a pointer type. */
2645
2646 tree
2647 m2expr_BuildAddAddress (location_t location, tree op1, tree op2)
2648 {
2649 tree type1, type2;
2650
2651 op1 = m2expr_FoldAndStrip (op1);
2652 op2 = m2expr_FoldAndStrip (op2);
2653
2654 type1 = m2tree_skip_type_decl (TREE_TYPE (op1));
2655 type2 = m2tree_skip_type_decl (TREE_TYPE (op2));
2656
2657 m2assert_AssertLocation (location);
2658 ASSERT_CONDITION (POINTER_TYPE_P (type1));
2659 ASSERT_CONDITION (!POINTER_TYPE_P (type2));
2660
2661 op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
2662 return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
2663 m2expr_FoldAndStrip (op1),
2664 m2expr_FoldAndStrip (op2));
2665 }
2666
2667 /* BuildNegateCheck builds a negate tree. */
2668
2669 tree
2670 m2expr_BuildNegateCheck (location_t location, tree arg, tree lowest, tree min,
2671 tree max)
2672 {
2673 tree t;
2674
2675 m2assert_AssertLocation (location);
2676
2677 arg = m2expr_FoldAndStrip (arg);
2678 arg = CheckAddressToCardinal (location, arg);
2679
2680 t = m2expr_build_unary_op_check (location, NEGATE_EXPR, arg, lowest, min,
2681 max);
2682 return m2expr_FoldAndStrip (t);
2683 }
2684
2685 /* BuildNegate build a negate expression and returns the tree. */
2686
2687 tree
2688 m2expr_BuildNegate (location_t location, tree op1, bool needconvert)
2689 {
2690 m2assert_AssertLocation (location);
2691 op1 = m2expr_FoldAndStrip (op1);
2692 op1 = CheckAddressToCardinal (location, op1);
2693
2694 return m2expr_build_unary_op (location, NEGATE_EXPR, op1, needconvert);
2695 }
2696
2697 /* BuildSetNegate build a set negate expression and returns the tree. */
2698
2699 tree
2700 m2expr_BuildSetNegate (location_t location, tree op1, bool needconvert)
2701 {
2702 m2assert_AssertLocation (location);
2703
2704 return m2expr_build_binary_op (
2705 location, BIT_XOR_EXPR,
2706 m2convert_BuildConvert (location, m2type_GetWordType (),
2707 m2expr_FoldAndStrip (op1), false),
2708 set_full_complement, needconvert);
2709 }
2710
2711 /* BuildMult build a multiplication tree. */
2712
2713 tree
2714 m2expr_BuildMult (location_t location, tree op1, tree op2, bool needconvert)
2715 {
2716 op1 = m2expr_FoldAndStrip (op1);
2717 op2 = m2expr_FoldAndStrip (op2);
2718
2719 m2assert_AssertLocation (location);
2720
2721 op1 = CheckAddressToCardinal (location, op1);
2722 op2 = CheckAddressToCardinal (location, op2);
2723
2724 return m2expr_build_binary_op (location, MULT_EXPR, op1, op2, needconvert);
2725 }
2726
2727 /* BuildMultCheck builds a multiplication tree. */
2728
2729 tree
2730 m2expr_BuildMultCheck (location_t location, tree op1, tree op2, tree lowest,
2731 tree min, tree max)
2732 {
2733 tree t;
2734
2735 m2assert_AssertLocation (location);
2736
2737 op1 = m2expr_FoldAndStrip (op1);
2738 op2 = m2expr_FoldAndStrip (op2);
2739
2740 op1 = CheckAddressToCardinal (location, op1);
2741 op2 = CheckAddressToCardinal (location, op2);
2742
2743 t = m2expr_build_binary_op_check (location, MULT_EXPR, op1, op2, false,
2744 lowest, min, max);
2745 return m2expr_FoldAndStrip (t);
2746 }
2747
2748 /* testLimits return the number of bits required to represent:
2749 min..max if it matches the, type. Otherwise NULL_TREE is returned. */
2750
2751 static tree
2752 testLimits (location_t location, tree type, tree min, tree max)
2753 {
2754 m2assert_AssertLocation (location);
2755
2756 if ((m2expr_CompareTrees (TYPE_MAX_VALUE (type), max) == 0)
2757 && (m2expr_CompareTrees (TYPE_MIN_VALUE (type), min) == 0))
2758 return m2expr_BuildMult (location, m2expr_GetSizeOf (location, type),
2759 m2decl_BuildIntegerConstant (BITS_PER_UNIT),
2760 false);
2761 return NULL_TREE;
2762 }
2763
2764 /* noBitsRequired return the number of bits required to contain, values. */
2765
2766 static tree
2767 noBitsRequired (tree values)
2768 {
2769 int bits = tree_floor_log2 (values);
2770
2771 return m2decl_BuildIntegerConstant (bits + 1);
2772 }
2773
2774 /* getMax return the result of max (a, b). */
2775
2776 static tree
2777 getMax (tree a, tree b)
2778 {
2779 if (m2expr_CompareTrees (a, b) > 0)
2780 return a;
2781 else
2782 return b;
2783 }
2784
2785 /* calcNbits return the smallest number of bits required to
2786 represent: min..max. */
2787
2788 tree
2789 m2expr_calcNbits (location_t location, tree min, tree max)
2790 {
2791 int negative = false;
2792 tree t = testLimits (location, m2type_GetIntegerType (), min, max);
2793
2794 m2assert_AssertLocation (location);
2795
2796 if (t == NULL)
2797 t = testLimits (location, m2type_GetCardinalType (), min, max);
2798
2799 if (t == NULL)
2800 {
2801 if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) < 0)
2802 {
2803 min = m2expr_BuildAdd (location, min,
2804 m2expr_GetIntegerOne (location), false);
2805 min = fold (m2expr_BuildNegate (location, min, false));
2806 negative = true;
2807 }
2808 if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
2809 {
2810 max = fold (m2expr_BuildNegate (location, max, false));
2811 negative = true;
2812 }
2813 t = noBitsRequired (getMax (min, max));
2814 if (negative)
2815 t = m2expr_BuildAdd (location, t, m2expr_GetIntegerOne (location),
2816 false);
2817 }
2818 return t;
2819 }
2820
2821 /* BuildTBitSize return the minimum number of bits to represent type.
2822 This function is called internally by cc1gm2 to calculate the bits
2823 size of a type and is used to position record fields. */
2824
2825 tree
2826 m2expr_BuildTBitSize (location_t location, tree type)
2827 {
2828 enum tree_code code = TREE_CODE (type);
2829 tree min;
2830 tree max;
2831 m2assert_AssertLocation (location);
2832
2833 switch (code)
2834 {
2835
2836 case TYPE_DECL:
2837 return m2expr_BuildTBitSize (location, TREE_TYPE (type));
2838 case INTEGER_TYPE:
2839 case ENUMERAL_TYPE:
2840 max = m2convert_BuildConvert (location, m2type_GetIntegerType (),
2841 TYPE_MAX_VALUE (type), false);
2842 min = m2convert_BuildConvert (location, m2type_GetIntegerType (),
2843 TYPE_MIN_VALUE (type), false);
2844 return m2expr_calcNbits (location, min, max);
2845 case BOOLEAN_TYPE:
2846 return m2expr_GetIntegerOne (location);
2847 default:
2848 return m2expr_BuildMult (location, m2expr_GetSizeOf (location, type),
2849 m2decl_BuildIntegerConstant (BITS_PER_UNIT),
2850 false);
2851 }
2852 }
2853
2854 /* BuildSystemTBitSize return the minimum number of bits to represent type.
2855 This function is called when evaluating SYSTEM.TBITSIZE. */
2856
2857 tree
2858 m2expr_BuildSystemTBitSize (location_t location, tree type)
2859 {
2860 enum tree_code code = TREE_CODE (type);
2861 m2assert_AssertLocation (location);
2862 if (code == TYPE_DECL)
2863 return m2expr_BuildTBitSize (location, TREE_TYPE (type));
2864 return TYPE_SIZE (type);
2865 }
2866
2867 /* BuildSize build a SIZE function expression and returns the tree. */
2868
2869 tree
2870 m2expr_BuildSize (location_t location, tree op1,
2871 bool needconvert ATTRIBUTE_UNUSED)
2872 {
2873 m2assert_AssertLocation (location);
2874 return m2expr_GetSizeOf (location, op1);
2875 }
2876
2877 /* BuildAddr return an expression which calculates the address of op1
2878 and returns the tree. If use_generic is true then create a generic
2879 pointer type. */
2880
2881 tree
2882 m2expr_BuildAddr (location_t location, tree op1, bool use_generic)
2883 {
2884 tree type = m2tree_skip_type_decl (TREE_TYPE (op1));
2885 tree ptrType = build_pointer_type (type);
2886 tree result;
2887
2888 m2assert_AssertLocation (location);
2889
2890 if (!gm2_mark_addressable (op1))
2891 error_at (location, "cannot take the address of this expression");
2892
2893 if (use_generic)
2894 result = build1 (ADDR_EXPR, m2type_GetPointerType (), op1);
2895 else
2896 result = build1 (ADDR_EXPR, ptrType, op1);
2897 protected_set_expr_location (result, location);
2898 return result;
2899 }
2900
2901 /* BuildOffset1 build and return an expression containing the number
2902 of bytes the field is offset from the start of the record structure.
2903 This function is the same as the above, except that it derives the
2904 record from the field and then calls BuildOffset. */
2905
2906 tree
2907 m2expr_BuildOffset1 (location_t location, tree field,
2908 bool needconvert ATTRIBUTE_UNUSED)
2909 {
2910 m2assert_AssertLocation (location);
2911 return m2expr_BuildOffset (location, DECL_CONTEXT (field), field,
2912 needconvert);
2913 }
2914
2915 /* determinePenultimateField return the field associated with the
2916 DECL_CONTEXT (field) within a record or varient. The record, is a
2917 record/varient but it maybe an outer nested record to the field that
2918 we are searching. Ie:
2919
2920 record = RECORD x: CARDINAL ; y: RECORD field: CARDINAL ; END END ;
2921
2922 determinePenultimateField (record, field) returns, y. We are
2923 assurred that the chain of records leading to field will be unique as
2924 they are built on the fly to implement varient records. */
2925
2926 static tree
2927 determinePenultimateField (tree record, tree field)
2928 {
2929 tree fieldlist = TYPE_FIELDS (record);
2930 tree x, r;
2931
2932 for (x = fieldlist; x; x = TREE_CHAIN (x))
2933 {
2934 if (DECL_CONTEXT (field) == TREE_TYPE (x))
2935 return x;
2936 switch (TREE_CODE (TREE_TYPE (x)))
2937 {
2938 case RECORD_TYPE:
2939 case UNION_TYPE:
2940 r = determinePenultimateField (TREE_TYPE (x), field);
2941 if (r != NULL)
2942 return r;
2943 break;
2944 default:
2945 break;
2946 }
2947 }
2948 return NULL_TREE;
2949 }
2950
2951 /* BuildOffset builds an expression containing the number of bytes
2952 the field is offset from the start of the record structure. The
2953 expression is returned. */
2954
2955 tree
2956 m2expr_BuildOffset (location_t location, tree record, tree field,
2957 bool needconvert ATTRIBUTE_UNUSED)
2958 {
2959 m2assert_AssertLocation (location);
2960
2961 if (DECL_CONTEXT (field) == record)
2962 return m2convert_BuildConvert (
2963 location, m2type_GetIntegerType (),
2964 m2expr_BuildAdd (
2965 location, DECL_FIELD_OFFSET (field),
2966 m2expr_BuildDivTrunc (location, DECL_FIELD_BIT_OFFSET (field),
2967 m2decl_BuildIntegerConstant (BITS_PER_UNIT),
2968 false),
2969 false),
2970 false);
2971 else
2972 {
2973 tree r1 = DECL_CONTEXT (field);
2974 tree r2 = determinePenultimateField (record, field);
2975 return m2convert_BuildConvert (
2976 location, m2type_GetIntegerType (),
2977 m2expr_BuildAdd (
2978 location, m2expr_BuildOffset (location, r1, field, needconvert),
2979 m2expr_BuildOffset (location, record, r2, needconvert), false),
2980 false);
2981 }
2982 }
2983
2984 /* BuildLogicalOrAddress build a logical or expressions and return the tree. */
2985
2986 tree
2987 m2expr_BuildLogicalOrAddress (location_t location, tree op1, tree op2,
2988 bool needconvert)
2989 {
2990 m2assert_AssertLocation (location);
2991 return m2expr_build_binary_op (location, BIT_IOR_EXPR, op1, op2,
2992 needconvert);
2993 }
2994
2995 /* BuildLogicalOr build a logical or expressions and return the tree. */
2996
2997 tree
2998 m2expr_BuildLogicalOr (location_t location, tree op1, tree op2,
2999 bool needconvert)
3000 {
3001 m2assert_AssertLocation (location);
3002 return m2expr_build_binary_op (
3003 location, BIT_IOR_EXPR,
3004 m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
3005 m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
3006 needconvert);
3007 }
3008
3009 /* BuildLogicalAnd build a logical and expression and return the tree. */
3010
3011 tree
3012 m2expr_BuildLogicalAnd (location_t location, tree op1, tree op2,
3013 bool needconvert)
3014 {
3015 m2assert_AssertLocation (location);
3016 return m2expr_build_binary_op (
3017 location, BIT_AND_EXPR,
3018 m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
3019 m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
3020 needconvert);
3021 }
3022
3023 /* BuildSymmetricalDifference build a logical xor expression and return the
3024 * tree. */
3025
3026 tree
3027 m2expr_BuildSymmetricDifference (location_t location, tree op1, tree op2,
3028 bool needconvert)
3029 {
3030 m2assert_AssertLocation (location);
3031 return m2expr_build_binary_op (
3032 location, BIT_XOR_EXPR,
3033 m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
3034 m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
3035 needconvert);
3036 }
3037
3038 /* BuildLogicalDifference build a logical difference expression and
3039 return the tree. (op1 and (not op2)). */
3040
3041 tree
3042 m2expr_BuildLogicalDifference (location_t location, tree op1, tree op2,
3043 bool needconvert)
3044 {
3045 m2assert_AssertLocation (location);
3046 return m2expr_build_binary_op (
3047 location, BIT_AND_EXPR,
3048 m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
3049 m2expr_BuildSetNegate (location, op2, needconvert), needconvert);
3050 }
3051
3052 /* base_type returns the base type of an ordinal subrange, or the
3053 type itself if it is not a subrange. */
3054
3055 static tree
3056 base_type (tree type)
3057 {
3058 if (type == error_mark_node)
3059 return error_mark_node;
3060
3061 /* Check for ordinal subranges. */
3062 if (m2tree_IsOrdinal (type) && TREE_TYPE (type))
3063 type = TREE_TYPE (type);
3064 return TYPE_MAIN_VARIANT (type);
3065 }
3066
3067 /* boolean_enum_to_unsigned convert a BOOLEAN_TYPE, t, or
3068 ENUMERAL_TYPE to an unsigned type. */
3069
3070 static tree
3071 boolean_enum_to_unsigned (location_t location, tree t)
3072 {
3073 tree type = TREE_TYPE (t);
3074
3075 if (TREE_CODE (base_type (type)) == BOOLEAN_TYPE)
3076 return m2convert_BuildConvert (location, unsigned_type_node, t, false);
3077 else if (TREE_CODE (base_type (type)) == ENUMERAL_TYPE)
3078 return m2convert_BuildConvert (location, unsigned_type_node, t, false);
3079 else
3080 return t;
3081 }
3082
3083 /* check_for_comparison check to see if, op, is of type, badType. If
3084 so then it returns op after it has been cast to, goodType. op will
3085 be an array so we take the address and cast the contents. */
3086
3087 static tree
3088 check_for_comparison (location_t location, tree op, tree badType,
3089 tree goodType)
3090 {
3091 m2assert_AssertLocation (location);
3092 if (m2tree_skip_type_decl (TREE_TYPE (op)) == badType)
3093 /* Cannot compare array contents in m2expr_build_binary_op. */
3094 return m2expr_BuildIndirect (
3095 location, m2expr_BuildAddr (location, op, false), goodType);
3096 return op;
3097 }
3098
3099 /* convert_for_comparison return a tree which can be used as an
3100 argument during a comparison. */
3101
3102 static tree
3103 convert_for_comparison (location_t location, tree op)
3104 {
3105 m2assert_AssertLocation (location);
3106 op = boolean_enum_to_unsigned (location, op);
3107
3108 op = check_for_comparison (location, op, m2type_GetISOWordType (),
3109 m2type_GetWordType ());
3110 op = check_for_comparison (location, op, m2type_GetM2Word16 (),
3111 m2type_GetM2Cardinal16 ());
3112 op = check_for_comparison (location, op, m2type_GetM2Word32 (),
3113 m2type_GetM2Cardinal32 ());
3114 op = check_for_comparison (location, op, m2type_GetM2Word64 (),
3115 m2type_GetM2Cardinal64 ());
3116
3117 return op;
3118 }
3119
3120 /* BuildLessThan return a tree which computes <. */
3121
3122 tree
3123 m2expr_BuildLessThan (location_t location, tree op1, tree op2)
3124 {
3125 m2assert_AssertLocation (location);
3126 return m2expr_build_binary_op (
3127 location, LT_EXPR, boolean_enum_to_unsigned (location, op1),
3128 boolean_enum_to_unsigned (location, op2), true);
3129 }
3130
3131 /* BuildGreaterThan return a tree which computes >. */
3132
3133 tree
3134 m2expr_BuildGreaterThan (location_t location, tree op1, tree op2)
3135 {
3136 m2assert_AssertLocation (location);
3137 return m2expr_build_binary_op (
3138 location, GT_EXPR, boolean_enum_to_unsigned (location, op1),
3139 boolean_enum_to_unsigned (location, op2), true);
3140 }
3141
3142 /* BuildLessThanOrEqual return a tree which computes <. */
3143
3144 tree
3145 m2expr_BuildLessThanOrEqual (location_t location, tree op1, tree op2)
3146 {
3147 m2assert_AssertLocation (location);
3148 return m2expr_build_binary_op (
3149 location, LE_EXPR, boolean_enum_to_unsigned (location, op1),
3150 boolean_enum_to_unsigned (location, op2), true);
3151 }
3152
3153 /* BuildGreaterThanOrEqual return a tree which computes >=. */
3154
3155 tree
3156 m2expr_BuildGreaterThanOrEqual (location_t location, tree op1, tree op2)
3157 {
3158 m2assert_AssertLocation (location);
3159 return m2expr_build_binary_op (
3160 location, GE_EXPR, boolean_enum_to_unsigned (location, op1),
3161 boolean_enum_to_unsigned (location, op2), true);
3162 }
3163
3164 /* BuildEqualTo return a tree which computes =. */
3165
3166 tree
3167 m2expr_BuildEqualTo (location_t location, tree op1, tree op2)
3168 {
3169 m2assert_AssertLocation (location);
3170 return m2expr_build_binary_op (location, EQ_EXPR,
3171 convert_for_comparison (location, op1),
3172 convert_for_comparison (location, op2), true);
3173 }
3174
3175 /* BuildEqualNotTo return a tree which computes #. */
3176
3177 tree
3178 m2expr_BuildNotEqualTo (location_t location, tree op1, tree op2)
3179 {
3180 m2assert_AssertLocation (location);
3181 return m2expr_build_binary_op (location, NE_EXPR,
3182 convert_for_comparison (location, op1),
3183 convert_for_comparison (location, op2), true);
3184 }
3185
3186 /* BuildIsSuperset return a tree which computes: op1 & op2 == op2. */
3187
3188 tree
3189 m2expr_BuildIsSuperset (location_t location, tree op1, tree op2)
3190 {
3191 m2assert_AssertLocation (location);
3192 return m2expr_BuildEqualTo (
3193 location, op2, m2expr_BuildLogicalAnd (location, op1, op2, false));
3194 }
3195
3196 /* BuildIsNotSuperset return a tree which computes: op1 & op2 != op2. */
3197
3198 tree
3199 m2expr_BuildIsNotSuperset (location_t location, tree op1, tree op2)
3200 {
3201 m2assert_AssertLocation (location);
3202 return m2expr_BuildNotEqualTo (
3203 location, op2, m2expr_BuildLogicalAnd (location, op1, op2, false));
3204 }
3205
3206 /* BuildIsSubset return a tree which computes: op1 & op2 == op1. */
3207
3208 tree
3209 m2expr_BuildIsSubset (location_t location, tree op1, tree op2)
3210 {
3211 m2assert_AssertLocation (location);
3212 return m2expr_BuildEqualTo (
3213 location, op1, m2expr_BuildLogicalAnd (location, op1, op2, false));
3214 }
3215
3216 /* BuildIsNotSubset return a tree which computes: op1 & op2 != op1. */
3217
3218 tree
3219 m2expr_BuildIsNotSubset (location_t location, tree op1, tree op2)
3220 {
3221 m2assert_AssertLocation (location);
3222 return m2expr_BuildNotEqualTo (
3223 location, op1, m2expr_BuildLogicalAnd (location, op1, op2, false));
3224 }
3225
3226 /* BuildIfConstInVar generates: if constel in varset then goto label. */
3227
3228 void
3229 m2expr_BuildIfConstInVar (location_t location, tree type, tree varset,
3230 tree constel, bool is_lvalue, int fieldno,
3231 char *label)
3232 {
3233 tree size = m2expr_GetSizeOf (location, type);
3234 m2assert_AssertLocation (location);
3235
3236 ASSERT_BOOL (is_lvalue);
3237 if (m2expr_CompareTrees (
3238 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
3239 <= 0)
3240 /* Small set size <= TSIZE(WORD). */
3241 m2treelib_do_jump_if_bit (
3242 location, NE_EXPR,
3243 m2treelib_get_rvalue (location, varset, type, is_lvalue), constel,
3244 label);
3245 else
3246 {
3247 tree fieldlist = TYPE_FIELDS (type);
3248 tree field;
3249
3250 for (field = fieldlist; (field != NULL) && (fieldno > 0);
3251 field = TREE_CHAIN (field))
3252 fieldno--;
3253
3254 m2treelib_do_jump_if_bit (
3255 location, NE_EXPR,
3256 m2treelib_get_set_field_rhs (location, varset, field), constel,
3257 label);
3258 }
3259 }
3260
3261 /* BuildIfConstInVar generates: if not (constel in varset) then goto label. */
3262
3263 void
3264 m2expr_BuildIfNotConstInVar (location_t location, tree type, tree varset,
3265 tree constel, bool is_lvalue, int fieldno,
3266 char *label)
3267 {
3268 tree size = m2expr_GetSizeOf (location, type);
3269
3270 m2assert_AssertLocation (location);
3271
3272 ASSERT_BOOL (is_lvalue);
3273 if (m2expr_CompareTrees (
3274 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
3275 <= 0)
3276 /* Small set size <= TSIZE(WORD). */
3277 m2treelib_do_jump_if_bit (
3278 location, EQ_EXPR,
3279 m2treelib_get_rvalue (location, varset, type, is_lvalue), constel,
3280 label);
3281 else
3282 {
3283 tree fieldlist = TYPE_FIELDS (type);
3284 tree field;
3285
3286 for (field = fieldlist; (field != NULL) && (fieldno > 0);
3287 field = TREE_CHAIN (field))
3288 fieldno--;
3289
3290 m2treelib_do_jump_if_bit (
3291 location, EQ_EXPR,
3292 m2treelib_get_set_field_rhs (location, varset, field), constel,
3293 label);
3294 }
3295 }
3296
3297 /* BuildIfVarInVar generates: if varel in varset then goto label. */
3298
3299 void
3300 m2expr_BuildIfVarInVar (location_t location, tree type, tree varset,
3301 tree varel, bool is_lvalue, tree low,
3302 tree high ATTRIBUTE_UNUSED, char *label)
3303 {
3304 tree size = m2expr_GetSizeOf (location, type);
3305 /* Calculate the index from the first bit, ie bit 0 represents low value. */
3306 tree index = m2expr_BuildSub (
3307 location, m2convert_BuildConvert (location, m2type_GetIntegerType (),
3308 varel, false),
3309 m2convert_BuildConvert (location, m2type_GetIntegerType (), low, false),
3310 false);
3311
3312 m2assert_AssertLocation (location);
3313
3314 if (m2expr_CompareTrees (
3315 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
3316 <= 0)
3317 /* Small set size <= TSIZE(WORD). */
3318 m2treelib_do_jump_if_bit (
3319 location, NE_EXPR,
3320 m2treelib_get_rvalue (location, varset, type, is_lvalue), index,
3321 label);
3322 else
3323 {
3324 tree p1 = m2treelib_get_set_address (location, varset, is_lvalue);
3325 /* Which word do we need to fetch? */
3326 tree word_index = m2expr_FoldAndStrip (m2expr_BuildDivTrunc (
3327 location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
3328 false));
3329 /* Calculate the bit in this word. */
3330 tree offset_into_word = m2expr_FoldAndStrip (m2expr_BuildModTrunc (
3331 location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
3332 false));
3333 tree p2 = m2expr_FoldAndStrip (m2expr_BuildMult (
3334 location, word_index,
3335 m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT), false));
3336
3337 /* Calculate the address of the word we are interested in. */
3338 p1 = m2expr_BuildAddAddress (location,
3339 m2convert_convertToPtr (location, p1), p2);
3340
3341 /* Fetch the word, extract the bit and test for != 0. */
3342 m2treelib_do_jump_if_bit (
3343 location, NE_EXPR,
3344 m2expr_BuildIndirect (location, p1, m2type_GetBitsetType ()),
3345 offset_into_word, label);
3346 }
3347 }
3348
3349 /* BuildIfNotVarInVar generates: if not (varel in varset) then goto label. */
3350
3351 void
3352 m2expr_BuildIfNotVarInVar (location_t location, tree type, tree varset,
3353 tree varel, bool is_lvalue, tree low,
3354 tree high ATTRIBUTE_UNUSED, char *label)
3355 {
3356 tree size = m2expr_GetSizeOf (location, type);
3357 /* Calculate the index from the first bit, ie bit 0 represents low value. */
3358 tree index = m2expr_BuildSub (
3359 location, m2convert_BuildConvert (location, m2type_GetIntegerType (),
3360 m2expr_FoldAndStrip (varel), false),
3361 m2convert_BuildConvert (location, m2type_GetIntegerType (),
3362 m2expr_FoldAndStrip (low), false),
3363 false);
3364
3365 index = m2expr_FoldAndStrip (index);
3366 m2assert_AssertLocation (location);
3367
3368 if (m2expr_CompareTrees (
3369 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
3370 <= 0)
3371 /* Small set size <= TSIZE(WORD). */
3372 m2treelib_do_jump_if_bit (
3373 location, EQ_EXPR,
3374 m2treelib_get_rvalue (location, varset, type, is_lvalue), index,
3375 label);
3376 else
3377 {
3378 tree p1 = m2treelib_get_set_address (location, varset, is_lvalue);
3379 /* Calculate the index from the first bit. */
3380
3381 /* Which word do we need to fetch? */
3382 tree word_index = m2expr_FoldAndStrip (m2expr_BuildDivTrunc (
3383 location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
3384 false));
3385 /* Calculate the bit in this word. */
3386 tree offset_into_word = m2expr_FoldAndStrip (m2expr_BuildModTrunc (
3387 location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
3388 false));
3389 tree p2 = m2expr_FoldAndStrip (m2expr_BuildMult (
3390 location, word_index,
3391 m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT), false));
3392
3393 /* Calculate the address of the word we are interested in. */
3394 p1 = m2expr_BuildAddAddress (location, p1, p2);
3395
3396 /* Fetch the word, extract the bit and test for == 0. */
3397 m2treelib_do_jump_if_bit (
3398 location, EQ_EXPR,
3399 m2expr_BuildIndirect (location, p1, m2type_GetBitsetType ()),
3400 offset_into_word, label);
3401 }
3402 }
3403
3404 /* BuildForeachWordInSetDoIfExpr foreach word in set, type, compute
3405 the expression, expr, and if true goto label. */
3406
3407 void
3408 m2expr_BuildForeachWordInSetDoIfExpr (location_t location, tree type, tree op1,
3409 tree op2, bool is_op1lvalue,
3410 bool is_op2lvalue, bool is_op1const,
3411 bool is_op2const,
3412 tree (*expr) (location_t, tree, tree),
3413 char *label)
3414 {
3415 tree p1 = m2treelib_get_set_address_if_var (location, op1, is_op1lvalue,
3416 is_op1const);
3417 tree p2 = m2treelib_get_set_address_if_var (location, op2, is_op2lvalue,
3418 is_op2const);
3419 unsigned int fieldNo = 0;
3420 tree field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
3421 tree field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
3422
3423 m2assert_AssertLocation (location);
3424 ASSERT_CONDITION (TREE_CODE (TREE_TYPE (op1)) == RECORD_TYPE);
3425 ASSERT_CONDITION (TREE_CODE (TREE_TYPE (op2)) == RECORD_TYPE);
3426
3427 while (field1 != NULL && field2 != NULL)
3428 {
3429 m2statement_DoJump (
3430 location,
3431 (*expr) (location,
3432 m2treelib_get_set_value (location, p1, field1, is_op1const,
3433 is_op1lvalue, op1, fieldNo),
3434 m2treelib_get_set_value (location, p2, field2, is_op2const,
3435 is_op2lvalue, op2, fieldNo)),
3436 NULL, label);
3437 fieldNo++;
3438 field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
3439 field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
3440 }
3441 }
3442
3443 /* BuildIfInRangeGoto returns a tree containing if var is in the
3444 range low..high then goto label. */
3445
3446 void
3447 m2expr_BuildIfInRangeGoto (location_t location, tree var, tree low, tree high,
3448 char *label)
3449 {
3450 m2assert_AssertLocation (location);
3451
3452 if (m2expr_CompareTrees (low, high) == 0)
3453 m2statement_DoJump (location, m2expr_BuildEqualTo (location, var, low),
3454 NULL, label);
3455 else
3456 m2statement_DoJump (
3457 location,
3458 m2expr_build_binary_op (
3459 location, TRUTH_ANDIF_EXPR,
3460 m2expr_BuildGreaterThanOrEqual (location, var, low),
3461 m2expr_BuildLessThanOrEqual (location, var, high), false),
3462 NULL, label);
3463 }
3464
3465 /* BuildIfNotInRangeGoto returns a tree containing if var is not in
3466 the range low..high then goto label. */
3467
3468 void
3469 m2expr_BuildIfNotInRangeGoto (location_t location, tree var, tree low,
3470 tree high, char *label)
3471 {
3472 m2assert_AssertLocation (location);
3473
3474 if (m2expr_CompareTrees (low, high) == 0)
3475 m2statement_DoJump (location, m2expr_BuildNotEqualTo (location, var, low),
3476 NULL, label);
3477 else
3478 m2statement_DoJump (
3479 location, m2expr_build_binary_op (
3480 location, TRUTH_ORIF_EXPR,
3481 m2expr_BuildLessThan (location, var, low),
3482 m2expr_BuildGreaterThan (location, var, high), false),
3483 NULL, label);
3484 }
3485
3486 /* BuildArray - returns a tree which accesses array[index] given,
3487 lowIndice. */
3488
3489 tree
3490 m2expr_BuildArray (location_t location, tree type, tree array, tree index,
3491 tree low_indice)
3492 {
3493 tree array_type = m2tree_skip_type_decl (TREE_TYPE (array));
3494 tree index_type = TYPE_DOMAIN (array_type);
3495 type = m2tree_skip_type_decl (type);
3496 // ASSERT_CONDITION (low_indice == TYPE_MIN_VALUE (index_type));
3497
3498 low_indice
3499 = m2convert_BuildConvert (location, index_type, low_indice, false);
3500 return build4_loc (location, ARRAY_REF, type, array, index, low_indice,
3501 NULL_TREE);
3502 }
3503
3504 /* BuildComponentRef - build a component reference tree which
3505 accesses record.field. If field does not belong to record it
3506 calls BuildComponentRef on the penultimate field. */
3507
3508 tree
3509 m2expr_BuildComponentRef (location_t location, tree record, tree field)
3510 {
3511 tree recordType = m2tree_skip_reference_type (
3512 m2tree_skip_type_decl (TREE_TYPE (record)));
3513
3514 if (DECL_CONTEXT (field) == recordType)
3515 return build3 (COMPONENT_REF, TREE_TYPE (field), record, field, NULL_TREE);
3516 else
3517 {
3518 tree f = determinePenultimateField (recordType, field);
3519 return m2expr_BuildComponentRef (
3520 location, m2expr_BuildComponentRef (location, record, f), field);
3521 }
3522 }
3523
3524 /* BuildIndirect - build: (*target) given that the object to be
3525 copied is of, type. */
3526
3527 tree
3528 m2expr_BuildIndirect (location_t location ATTRIBUTE_UNUSED, tree target,
3529 tree type)
3530 {
3531 /* Note that the second argument to build1 is:
3532
3533 TYPE_QUALS is a list of modifiers such as const or volatile to apply
3534 to the pointer type, represented as identifiers.
3535
3536 it also determines the type of arithmetic and size of the object to
3537 be indirectly moved. */
3538
3539 tree t1 = m2tree_skip_type_decl (type);
3540 tree t2 = build_pointer_type (t1);
3541
3542 m2assert_AssertLocation (location);
3543
3544 return build1 (INDIRECT_REF, t1,
3545 m2convert_BuildConvert (location, t2, target, false));
3546 }
3547
3548 /* IsTrue - returns true if, t, is known to be true. */
3549
3550 bool
3551 m2expr_IsTrue (tree t)
3552 {
3553 return (m2expr_FoldAndStrip (t) == m2type_GetBooleanTrue ());
3554 }
3555
3556 /* IsFalse - returns false if, t, is known to be false. */
3557
3558 bool
3559 m2expr_IsFalse (tree t)
3560 {
3561 return (m2expr_FoldAndStrip (t) == m2type_GetBooleanFalse ());
3562 }
3563
3564 /* AreConstantsEqual - maps onto tree.cc (tree_int_cst_equal). It
3565 returns true if the value of e1 is the same as e2. */
3566
3567 bool
3568 m2expr_AreConstantsEqual (tree e1, tree e2)
3569 {
3570 return tree_int_cst_equal (e1, e2) != 0;
3571 }
3572
3573 /* AreRealOrComplexConstantsEqual - returns true if constants, e1 and
3574 e2 are equal according to IEEE rules. This does not perform bit
3575 equivalence for example IEEE states that -0 == 0 and NaN != NaN. */
3576
3577 bool
3578 m2expr_AreRealOrComplexConstantsEqual (tree e1, tree e2)
3579 {
3580 if (TREE_CODE (e1) == COMPLEX_CST)
3581 return (m2expr_AreRealOrComplexConstantsEqual (TREE_REALPART (e1),
3582 TREE_REALPART (e2))
3583 && m2expr_AreRealOrComplexConstantsEqual (TREE_IMAGPART (e1),
3584 TREE_IMAGPART (e2)));
3585 else
3586 return real_compare (EQ_EXPR, &TREE_REAL_CST (e1), &TREE_REAL_CST (e2));
3587 }
3588
3589 /* DetermineSign, returns -1 if e<0 0 if e==0 1 if e>0
3590 an unsigned constant will never return -1. */
3591
3592 int
3593 m2expr_DetermineSign (tree e)
3594 {
3595 return tree_int_cst_sgn (e);
3596 }
3597
3598 /* Similar to build_int_2 () but allows you to specify the type of
3599 the integer constant that you are creating. */
3600
3601 static tree
3602 build_int_2_type (HOST_WIDE_INT low, HOST_WIDE_INT hi, tree type)
3603 {
3604 tree value;
3605 HOST_WIDE_INT ival[3];
3606
3607 ival[0] = low;
3608 ival[1] = hi;
3609 ival[2] = 0;
3610
3611 widest_int wval = widest_int::from_array (ival, 3);
3612 value = wide_int_to_tree (type, wval);
3613
3614 return value;
3615 }
3616
3617 /* BuildCap - builds the Modula-2 function CAP(t) and returns the
3618 result in a gcc Tree. */
3619
3620 tree
3621 m2expr_BuildCap (location_t location, tree t)
3622 {
3623 tree tt;
3624 tree out_of_range, less_than, greater_than, translated;
3625
3626 m2assert_AssertLocation (location);
3627
3628 t = fold (t);
3629 if (t == error_mark_node)
3630 return error_mark_node;
3631
3632 tt = TREE_TYPE (t);
3633
3634 t = fold (convert (m2type_GetM2CharType (), t));
3635
3636 if (TREE_CODE (tt) == INTEGER_TYPE)
3637 {
3638 less_than = fold (m2expr_build_binary_op (
3639 location, LT_EXPR, t,
3640 build_int_2_type ('a', 0, m2type_GetM2CharType ()), 0));
3641 greater_than = fold (m2expr_build_binary_op (
3642 location, GT_EXPR, t,
3643 build_int_2_type ('z', 0, m2type_GetM2CharType ()), 0));
3644 out_of_range = fold (m2expr_build_binary_op (
3645 location, TRUTH_ORIF_EXPR, less_than, greater_than, 0));
3646
3647 translated = fold (convert (
3648 m2type_GetM2CharType (),
3649 m2expr_build_binary_op (
3650 location, MINUS_EXPR, t,
3651 build_int_2_type ('a' - 'A', 0, m2type_GetM2CharType ()), 0)));
3652
3653 return fold_build3 (COND_EXPR, m2type_GetM2CharType (), out_of_range, t,
3654 translated);
3655 }
3656
3657 error_at (location,
3658 "argument to CAP is not a constant or variable of type CHAR");
3659 return error_mark_node;
3660 }
3661
3662 /* BuildDivM2 if iso or pim4 then all modulus results are positive
3663 and the results from the division are rounded to the floor otherwise
3664 use BuildDivTrunc. */
3665
3666 tree
3667 m2expr_BuildDivM2 (location_t location, tree op1, tree op2,
3668 bool needsconvert)
3669 {
3670 op1 = m2expr_FoldAndStrip (op1);
3671 op2 = m2expr_FoldAndStrip (op2);
3672 ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
3673 /* If iso or pim4 then build and return ((op2 < 0) ? (op1
3674 divceil op2) : (op1 divfloor op2)) otherwise use divtrunc. */
3675 if (M2Options_GetPIM4 () || M2Options_GetISO ()
3676 || M2Options_GetPositiveModFloor ())
3677 return fold_build3 (
3678 COND_EXPR, TREE_TYPE (op1),
3679 m2expr_BuildLessThan (
3680 location, op2,
3681 m2convert_BuildConvert (location, TREE_TYPE (op2),
3682 m2expr_GetIntegerZero (location), false)),
3683 m2expr_BuildDivCeil (location, op1, op2, needsconvert),
3684 m2expr_BuildDivFloor (location, op1, op2, needsconvert));
3685 else
3686 return m2expr_BuildDivTrunc (location, op1, op2, needsconvert);
3687 }
3688
3689 /* BuildDivM2Check - build and
3690 return ((op2 < 0) ? (op1 divtrunc op2) : (op1 divfloor op2))
3691 when -fiso, -fpim4 or -fpositive-mod-floor-div is present else
3692 return op1 div trunc op2. Use the checking div equivalents. */
3693
3694 tree
3695 m2expr_BuildDivM2Check (location_t location, tree op1, tree op2,
3696 tree lowest, tree min, tree max)
3697 {
3698 op1 = m2expr_FoldAndStrip (op1);
3699 op2 = m2expr_FoldAndStrip (op2);
3700 ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
3701 if (M2Options_GetISO ()
3702 || M2Options_GetPIM4 () || M2Options_GetPositiveModFloor ())
3703 return fold_build3 (
3704 COND_EXPR, TREE_TYPE (op1),
3705 m2expr_BuildLessThan (
3706 location, op2,
3707 m2convert_BuildConvert (location, TREE_TYPE (op2),
3708 m2expr_GetIntegerZero (location), false)),
3709 m2expr_BuildDivCeilCheck (location, op1, op2, lowest, min, max),
3710 m2expr_BuildDivFloorCheck (location, op1, op2, lowest, min, max));
3711 else
3712 return m2expr_BuildDivTruncCheck (location, op1, op2, lowest, min, max);
3713 }
3714
3715 static
3716 tree
3717 m2expr_BuildISOModM2Check (location_t location,
3718 tree op1, tree op2, tree lowest, tree min, tree max)
3719 {
3720 tree cond = m2expr_BuildLessThan (location, op2,
3721 m2convert_BuildConvert (location, TREE_TYPE (op2),
3722 m2expr_GetIntegerZero (location), false));
3723
3724 /* Return the result of the modulus. */
3725 return fold_build3 (COND_EXPR, TREE_TYPE (op1), cond,
3726 /* op2 < 0. */
3727 m2expr_BuildModCeilCheck (location, op1, op2, lowest, min, max),
3728 /* op2 >= 0. */
3729 m2expr_BuildModFloorCheck (location, op1, op2, lowest, min, max));
3730 }
3731
3732
3733 /* BuildModM2Check if iso or pim4 then build and return ((op2 < 0) ? (op1
3734 modceil op2) : (op1 modfloor op2)) otherwise use modtrunc.
3735 Use the checking mod equivalents. */
3736
3737 tree
3738 m2expr_BuildModM2Check (location_t location, tree op1, tree op2,
3739 tree lowest, tree min, tree max)
3740 {
3741 op1 = m2expr_FoldAndStrip (op1);
3742 op2 = m2expr_FoldAndStrip (op2);
3743 ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
3744 if (M2Options_GetPIM4 () || M2Options_GetISO ()
3745 || M2Options_GetPositiveModFloor ())
3746 return m2expr_BuildISOModM2Check (location, op1, op2, lowest, min, max);
3747 else
3748 return m2expr_BuildModTruncCheck (location, op1, op2, lowest, min, max);
3749 }
3750
3751 /* BuildModM2 if iso or pim4 then build and return ((op2 < 0) ? (op1
3752 modceil op2) : (op1 modfloor op2)) otherwise use modtrunc. */
3753
3754 tree
3755 m2expr_BuildModM2 (location_t location, tree op1, tree op2,
3756 bool needsconvert)
3757 {
3758 op1 = m2expr_FoldAndStrip (op1);
3759 op2 = m2expr_FoldAndStrip (op2);
3760 ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
3761 if (M2Options_GetPIM4 () || M2Options_GetISO ()
3762 || M2Options_GetPositiveModFloor ())
3763 return fold_build3 (
3764 COND_EXPR, TREE_TYPE (op1),
3765 m2expr_BuildLessThan (
3766 location, op2,
3767 m2convert_BuildConvert (location, TREE_TYPE (op2),
3768 m2expr_GetIntegerZero (location), false)),
3769 m2expr_BuildModCeil (location, op1, op2, needsconvert),
3770 m2expr_BuildModFloor (location, op1, op2, needsconvert));
3771 else
3772 return m2expr_BuildModTrunc (location, op1, op2, needsconvert);
3773 }
3774
3775 /* BuildAbs build the Modula-2 function ABS(t) and return the result
3776 in a gcc Tree. */
3777
3778 tree
3779 m2expr_BuildAbs (location_t location, tree t)
3780 {
3781 m2assert_AssertLocation (location);
3782
3783 return m2expr_build_unary_op (location, ABS_EXPR, t, 0);
3784 }
3785
3786 /* BuildRe build an expression for the function RE. */
3787
3788 tree
3789 m2expr_BuildRe (tree op1)
3790 {
3791 op1 = m2expr_FoldAndStrip (op1);
3792 if (TREE_CODE (op1) == COMPLEX_CST)
3793 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
3794 else
3795 return build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
3796 }
3797
3798 /* BuildIm build an expression for the function IM. */
3799
3800 tree
3801 m2expr_BuildIm (tree op1)
3802 {
3803 op1 = m2expr_FoldAndStrip (op1);
3804 if (TREE_CODE (op1) == COMPLEX_CST)
3805 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
3806 else
3807 return build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
3808 }
3809
3810 /* BuildCmplx build an expression for the function CMPLX. */
3811
3812 tree
3813 m2expr_BuildCmplx (location_t location, tree type, tree real, tree imag)
3814 {
3815 tree scalor;
3816 real = m2expr_FoldAndStrip (real);
3817 imag = m2expr_FoldAndStrip (imag);
3818 type = m2tree_skip_type_decl (type);
3819 scalor = TREE_TYPE (type);
3820
3821 if (scalor != TREE_TYPE (real))
3822 real = m2convert_BuildConvert (location, scalor, real, false);
3823 if (scalor != TREE_TYPE (imag))
3824 imag = m2convert_BuildConvert (location, scalor, imag, false);
3825
3826 if ((TREE_CODE (real) == REAL_CST) && (TREE_CODE (imag) == REAL_CST))
3827 return build_complex (type, real, imag);
3828 else
3829 return build2 (COMPLEX_EXPR, type, real, imag);
3830 }
3831
3832 /* BuildBinaryForeachWordDo implements the large set operators. Each
3833 word of the set can be calculated by binop. This function runs along
3834 each word of the large set invoking the binop. */
3835
3836 void
3837 m2expr_BuildBinaryForeachWordDo (location_t location, tree type, tree op1,
3838 tree op2, tree op3,
3839 tree (*binop) (location_t, tree, tree, bool),
3840 bool is_op1lvalue, bool is_op2lvalue,
3841 bool is_op3lvalue, bool is_op1const,
3842 bool is_op2const, bool is_op3const)
3843 {
3844 tree size = m2expr_GetSizeOf (location, type);
3845
3846 m2assert_AssertLocation (location);
3847
3848 ASSERT_BOOL (is_op1lvalue);
3849 ASSERT_BOOL (is_op2lvalue);
3850 ASSERT_BOOL (is_op3lvalue);
3851 ASSERT_BOOL (is_op1const);
3852 ASSERT_BOOL (is_op2const);
3853 ASSERT_BOOL (is_op3const);
3854 if (m2expr_CompareTrees (
3855 size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
3856 <= 0)
3857 /* Small set size <= TSIZE(WORD). */
3858 m2statement_BuildAssignmentTree (
3859 location, m2treelib_get_rvalue (location, op1, type, is_op1lvalue),
3860 (*binop) (
3861 location, m2treelib_get_rvalue (location, op2, type, is_op2lvalue),
3862 m2treelib_get_rvalue (location, op3, type, is_op3lvalue), false));
3863 else
3864 {
3865 /* Large set size > TSIZE(WORD). */
3866
3867 tree p2 = m2treelib_get_set_address_if_var (location, op2, is_op2lvalue,
3868 is_op2const);
3869 tree p3 = m2treelib_get_set_address_if_var (location, op3, is_op3lvalue,
3870 is_op3const);
3871 unsigned int fieldNo = 0;
3872 tree field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
3873 tree field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
3874 tree field3 = m2treelib_get_field_no (type, op3, is_op3const, fieldNo);
3875
3876 if (is_op1const)
3877 m2linemap_internal_error_at (
3878 location,
3879 "not expecting operand1 to be a constant set");
3880
3881 while (field1 != NULL && field2 != NULL && field3 != NULL)
3882 {
3883 m2statement_BuildAssignmentTree (
3884 location, m2treelib_get_set_field_des (location, op1, field1),
3885 (*binop) (
3886 location,
3887 m2treelib_get_set_value (location, p2, field2, is_op2const,
3888 is_op2lvalue, op2, fieldNo),
3889 m2treelib_get_set_value (location, p3, field3, is_op3const,
3890 is_op3lvalue, op3, fieldNo),
3891 false));
3892 fieldNo++;
3893 field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
3894 field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
3895 field3 = m2treelib_get_field_no (type, op3, is_op3const, fieldNo);
3896 }
3897 }
3898 }
3899
3900
3901 /* OverflowZType returns true if the ZTYPE str will exceed the
3902 internal representation. This routine is much faster (at
3903 least 2 orders of magnitude faster) than the char at a time overflow
3904 detection used in ToWideInt and so it should be
3905 used to filter out erroneously large constants before calling ToWideInt
3906 allowing a quick fail. */
3907
3908 bool
3909 m2expr_OverflowZType (location_t location, const char *str, unsigned int base,
3910 bool issueError)
3911 {
3912 int length = strlen (str);
3913 bool overflow = false;
3914
3915 switch (base)
3916 {
3917 case 2:
3918 overflow = ((length -1) > WIDE_INT_MAX_PRECISION);
3919 break;
3920 case 8:
3921 overflow = (((length -1) * 3) > WIDE_INT_MAX_PRECISION);
3922 break;
3923 case 10:
3924 {
3925 int str_log10 = length;
3926 int bits_str = (int) (((float) (str_log10)) / log10f (2.0)) + 1;
3927 overflow = (bits_str > WIDE_INT_MAX_PRECISION);
3928 }
3929 break;
3930 case 16:
3931 overflow = (((length -1) * 4) > WIDE_INT_MAX_PRECISION);
3932 break;
3933 default:
3934 gcc_unreachable ();
3935 }
3936 if (issueError && overflow)
3937 error_at (location,
3938 "constant literal %qs exceeds internal ZTYPE range", str);
3939 return overflow;
3940 }
3941
3942
3943 /* ToWideInt converts a ZTYPE str value into result. */
3944
3945 static
3946 bool
3947 ToWideInt (location_t location, const char *str, unsigned int base,
3948 widest_int &result, bool issueError)
3949 {
3950 tree type = m2type_GetM2ZType ();
3951 unsigned int i = 0;
3952 wi::overflow_type overflow = wi::OVF_NONE;
3953 widest_int wbase = wi::to_widest (m2decl_BuildIntegerConstant (base));
3954 unsigned int digit = 0;
3955 result = wi::to_widest (m2decl_BuildIntegerConstant (0));
3956 bool base_specifier = false;
3957
3958 while (((str[i] != (char)0) && (overflow == wi::OVF_NONE))
3959 && (! base_specifier))
3960 {
3961 char ch = str[i];
3962
3963 switch (base)
3964 {
3965 /* GNU m2 extension allows 'A' to represent binary literals. */
3966 case 2:
3967 if (ch == 'A')
3968 base_specifier = true;
3969 else if ((ch < '0') || (ch > '1'))
3970 {
3971 if (issueError)
3972 error_at (location,
3973 "constant literal %qs contains %qc, expected 0 or 1",
3974 str, ch);
3975 return true;
3976 }
3977 else
3978 digit = (unsigned int) (ch - '0');
3979 break;
3980 case 8:
3981 /* An extension of 'B' indicates octal ZTYPE and 'C' octal character. */
3982 if ((ch == 'B') || (ch == 'C'))
3983 base_specifier = true;
3984 else if ((ch < '0') || (ch > '7'))
3985 {
3986 if (issueError)
3987 error_at (location,
3988 "constant literal %qs contains %qc, expected %qs",
3989 str, ch, "0..7");
3990 return true;
3991 }
3992 else
3993 digit = (unsigned int) (ch - '0');
3994 break;
3995 case 10:
3996 if ((ch < '0') || (ch > '9'))
3997 {
3998 if (issueError)
3999 error_at (location,
4000 "constant literal %qs contains %qc, expected %qs",
4001 str, ch, "0..9");
4002 return true;
4003 }
4004 else
4005 digit = (unsigned int) (ch - '0');
4006 break;
4007 case 16:
4008 /* An extension of 'H' indicates hexidecimal ZTYPE. */
4009 if (ch == 'H')
4010 base_specifier = true;
4011 else if ((ch >= '0') && (ch <= '9'))
4012 digit = (unsigned int) (ch - '0');
4013 else if ((ch >= 'A') && (ch <= 'F'))
4014 digit = ((unsigned int) (ch - 'A')) + 10;
4015 else
4016 {
4017 if (issueError)
4018 error_at (location,
4019 "constant literal %qs contains %qc, expected %qs or %qs",
4020 str, ch, "0..9", "A..F");
4021 return true;
4022 }
4023 break;
4024 default:
4025 gcc_unreachable ();
4026 }
4027
4028 if (! base_specifier)
4029 {
4030 widest_int wdigit = wi::to_widest (m2decl_BuildIntegerConstant (digit));
4031 result = wi::umul (result, wbase, &overflow);
4032 if (overflow == wi::OVF_NONE)
4033 result = wi::add (result, wdigit, UNSIGNED, &overflow);
4034 }
4035 i++;
4036 }
4037 if (overflow == wi::OVF_NONE)
4038 {
4039 tree value = wide_int_to_tree (type, result);
4040 if (m2expr_TreeOverflow (value))
4041 {
4042 if (issueError)
4043 error_at (location,
4044 "constant literal %qs exceeds internal ZTYPE range", str);
4045 return true;
4046 }
4047 return false;
4048 }
4049 else
4050 {
4051 if (issueError)
4052 error_at (location,
4053 "constant literal %qs exceeds internal ZTYPE range", str);
4054 return true;
4055 }
4056 }
4057
4058
4059 /* StrToWideInt return true if an overflow occurs when attempting to convert
4060 str to an unsigned ZTYPE the value is contained in the widest_int result.
4061 The value result is undefined if true is returned. */
4062
4063 bool
4064 m2expr_StrToWideInt (location_t location, const char *str, unsigned int base,
4065 widest_int &result, bool issueError)
4066 {
4067 if (m2expr_OverflowZType (location, str, base, issueError))
4068 return true;
4069 return ToWideInt (location, str, base, result, issueError);
4070 }
4071
4072
4073 /* GetSizeOfInBits return the number of bits used to contain, type. */
4074
4075 tree
4076 m2expr_GetSizeOfInBits (tree type)
4077 {
4078 enum tree_code code = TREE_CODE (type);
4079
4080 if (code == FUNCTION_TYPE)
4081 return m2expr_GetSizeOfInBits (ptr_type_node);
4082
4083 if (code == VOID_TYPE)
4084 {
4085 error ("%qs applied to a void type", "sizeof");
4086 return size_one_node;
4087 }
4088
4089 if (code == VAR_DECL)
4090 return m2expr_GetSizeOfInBits (TREE_TYPE (type));
4091
4092 if (code == PARM_DECL)
4093 return m2expr_GetSizeOfInBits (TREE_TYPE (type));
4094
4095 if (code == TYPE_DECL)
4096 return m2expr_GetSizeOfInBits (TREE_TYPE (type));
4097
4098 if (code == COMPONENT_REF)
4099 return m2expr_GetSizeOfInBits (TREE_TYPE (type));
4100
4101 if (code == ERROR_MARK)
4102 return size_one_node;
4103
4104 if (!COMPLETE_TYPE_P (type))
4105 {
4106 error ("%qs applied to an incomplete type", "sizeof");
4107 return size_zero_node;
4108 }
4109
4110 return m2decl_BuildIntegerConstant (TYPE_PRECISION (type));
4111 }
4112
4113 /* GetSizeOf taken from c-typeck.cc (c_sizeof). */
4114
4115 tree
4116 m2expr_GetSizeOf (location_t location, tree type)
4117 {
4118 enum tree_code code = TREE_CODE (type);
4119 m2assert_AssertLocation (location);
4120
4121 if (code == FUNCTION_TYPE)
4122 return m2expr_GetSizeOf (location, m2type_GetPointerType ());
4123
4124 if (code == VOID_TYPE)
4125 return size_one_node;
4126
4127 if (code == VAR_DECL)
4128 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4129
4130 if (code == PARM_DECL)
4131 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4132
4133 if (code == TYPE_DECL)
4134 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4135
4136 if (code == ERROR_MARK)
4137 return size_one_node;
4138
4139 if (code == CONSTRUCTOR)
4140 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4141
4142 if (code == FIELD_DECL)
4143 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4144
4145 if (code == COMPONENT_REF)
4146 return m2expr_GetSizeOf (location, TREE_TYPE (type));
4147
4148 if (!COMPLETE_TYPE_P (type))
4149 {
4150 error_at (location, "%qs applied to an incomplete type", "SIZE");
4151 return size_zero_node;
4152 }
4153
4154 /* Convert in case a char is more than one unit. */
4155 return size_binop_loc (
4156 location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4157 size_int (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT));
4158 }
4159
4160 tree
4161 m2expr_GetIntegerZero (location_t location ATTRIBUTE_UNUSED)
4162 {
4163 return integer_zero_node;
4164 }
4165
4166 tree
4167 m2expr_GetIntegerOne (location_t location ATTRIBUTE_UNUSED)
4168 {
4169 return integer_one_node;
4170 }
4171
4172 tree
4173 m2expr_GetCardinalOne (location_t location)
4174 {
4175 return m2convert_ToCardinal (location, integer_one_node);
4176 }
4177
4178 tree
4179 m2expr_GetCardinalZero (location_t location)
4180 {
4181 return m2convert_ToCardinal (location, integer_zero_node);
4182 }
4183
4184 tree
4185 m2expr_GetWordZero (location_t location)
4186 {
4187 return m2convert_ToWord (location, integer_zero_node);
4188 }
4189
4190 tree
4191 m2expr_GetWordOne (location_t location)
4192 {
4193 return m2convert_ToWord (location, integer_one_node);
4194 }
4195
4196 tree
4197 m2expr_GetPointerZero (location_t location)
4198 {
4199 return m2convert_convertToPtr (location, integer_zero_node);
4200 }
4201
4202 tree
4203 m2expr_GetPointerOne (location_t location)
4204 {
4205 return m2convert_convertToPtr (location, integer_one_node);
4206 }
4207
4208 /* build_set_full_complement return a word size value with all bits
4209 set to one. */
4210
4211 static tree
4212 build_set_full_complement (location_t location)
4213 {
4214 tree value = integer_zero_node;
4215 int i;
4216
4217 m2assert_AssertLocation (location);
4218
4219 for (i = 0; i < SET_WORD_SIZE; i++)
4220 {
4221 value = m2expr_BuildLogicalOr (
4222 location, value,
4223 m2expr_BuildLSL (
4224 location, m2expr_GetWordOne (location),
4225 m2convert_BuildConvert (location, m2type_GetWordType (),
4226 m2decl_BuildIntegerConstant (i), false),
4227 false),
4228 false);
4229 }
4230 return value;
4231 }
4232
4233
4234 /* GetCstInteger return the integer value of the cst tree. */
4235
4236 int
4237 m2expr_GetCstInteger (tree cst)
4238 {
4239 return TREE_INT_CST_LOW (cst);
4240 }
4241
4242
4243 /* init initialise this module. */
4244
4245 void
4246 m2expr_init (location_t location)
4247 {
4248 m2assert_AssertLocation (location);
4249
4250 set_full_complement = build_set_full_complement (location);
4251 }
4252
4253 #include "gt-m2-m2expr.h"