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