| 1 |
(* M2Range.def exports procedures which maintain the range checking. |
| 2 |
|
| 3 |
Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. *) |
| 21 |
|
| 22 |
DEFINITION MODULE M2Range ; |
| 23 |
|
| 24 |
(* |
| 25 |
Title : M2Range |
| 26 |
Author : Gaius Mulley |
| 27 |
System : GNU Modula-2 |
| 28 |
Date : Thu Feb 28 10:18:55 2008 |
| 29 |
Revision : $Version$ |
| 30 |
Description: exports procedures which maintain the range checking |
| 31 |
state which is explored once all the subrange values |
| 32 |
have been resolved by the front end (once |
| 33 |
M2GCCDeclare has completed its task). We cannot |
| 34 |
perform this activity during M2Quads, as we dont |
| 35 |
know the subrange values and also we can do so much |
| 36 |
more once optimization has occurred. It should be |
| 37 |
possible to detect simple overflow errors at compile |
| 38 |
time, post optimization. |
| 39 |
*) |
| 40 |
|
| 41 |
FROM CDataTypes IMPORT ConstCharStar ; |
| 42 |
FROM gcctypes IMPORT location_t, tree ; |
| 43 |
FROM DynamicStrings IMPORT String ; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
(* |
| 48 |
InitAssignmentRangeCheck - returns a range check node which |
| 49 |
remembers the information necessary |
| 50 |
so that a range check for d := e |
| 51 |
can be generated later on. |
| 52 |
*) |
| 53 |
|
| 54 |
PROCEDURE InitAssignmentRangeCheck (tokno: CARDINAL; |
| 55 |
des, expr: CARDINAL; |
| 56 |
destok, exprtok: CARDINAL) : CARDINAL ; |
| 57 |
|
| 58 |
|
| 59 |
(* |
| 60 |
InitReturnRangeCheck - returns a range check node which |
| 61 |
remembers the information necessary |
| 62 |
so that a range check for RETURN e |
| 63 |
from procedure, d, can be generated later on. |
| 64 |
*) |
| 65 |
|
| 66 |
PROCEDURE InitReturnRangeCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 67 |
|
| 68 |
|
| 69 |
(* |
| 70 |
InitSubrangeRangeCheck - returns a range check node which |
| 71 |
remembers the information necessary |
| 72 |
so that a range check for d := e |
| 73 |
can be generated later on. |
| 74 |
*) |
| 75 |
|
| 76 |
PROCEDURE InitSubrangeRangeCheck (d, e: CARDINAL) : CARDINAL ; |
| 77 |
|
| 78 |
|
| 79 |
(* |
| 80 |
InitStaticArraySubscriptRangeCheck - returns a range check node which |
| 81 |
remembers the information necessary |
| 82 |
so that a range check for d[e] |
| 83 |
can be generated later on. |
| 84 |
*) |
| 85 |
|
| 86 |
PROCEDURE InitStaticArraySubscriptRangeCheck (d, e, dim: CARDINAL) : CARDINAL ; |
| 87 |
|
| 88 |
|
| 89 |
(* |
| 90 |
InitDynamicArraySubscriptRangeCheck - returns a range check node which |
| 91 |
remembers the information necessary |
| 92 |
so that a range check for d[e] |
| 93 |
can be generated later on. |
| 94 |
*) |
| 95 |
|
| 96 |
PROCEDURE InitDynamicArraySubscriptRangeCheck (d, e, dim: CARDINAL) : CARDINAL ; |
| 97 |
|
| 98 |
|
| 99 |
(* |
| 100 |
InitIncRangeCheck - returns a range check node which |
| 101 |
remembers the information necessary |
| 102 |
so that a range check for INC(d, e) |
| 103 |
can be generated later on. |
| 104 |
*) |
| 105 |
|
| 106 |
PROCEDURE InitIncRangeCheck (d, e: CARDINAL) : CARDINAL ; |
| 107 |
|
| 108 |
|
| 109 |
(* |
| 110 |
InitDecRangeCheck - returns a range check node which |
| 111 |
remembers the information necessary |
| 112 |
so that a range check for DEC(d, e) |
| 113 |
can be generated later on. |
| 114 |
*) |
| 115 |
|
| 116 |
PROCEDURE InitDecRangeCheck (d, e: CARDINAL) : CARDINAL ; |
| 117 |
|
| 118 |
|
| 119 |
(* |
| 120 |
InitForLoopBeginRangeCheck - returns a range check node which |
| 121 |
remembers the information necessary |
| 122 |
so that a range check for |
| 123 |
FOR des := expr1 TO expr2 DO |
| 124 |
can be generated later on. expr2 is |
| 125 |
only used to type check with des. |
| 126 |
*) |
| 127 |
|
| 128 |
PROCEDURE InitForLoopBeginRangeCheck (des, destok, |
| 129 |
expr1, expr1tok, |
| 130 |
expr2, expr2tok, |
| 131 |
byconst, byconsttok: CARDINAL) : CARDINAL ; |
| 132 |
|
| 133 |
|
| 134 |
(* |
| 135 |
PutRangeForIncrement - places incrementquad into the range record. |
| 136 |
*) |
| 137 |
|
| 138 |
PROCEDURE PutRangeForIncrement (range: CARDINAL; incrementquad: CARDINAL) ; |
| 139 |
|
| 140 |
|
| 141 |
(* |
| 142 |
InitForLoopToRangeCheck - returns a range check node which |
| 143 |
remembers the information necessary |
| 144 |
so that a range check for the final value |
| 145 |
implied by ... e1 TO e2 BY e3 DO |
| 146 |
can be generated later on. |
| 147 |
*) |
| 148 |
|
| 149 |
PROCEDURE InitForLoopToRangeCheck (d, e: CARDINAL) : CARDINAL ; |
| 150 |
|
| 151 |
|
| 152 |
(* |
| 153 |
InitForLoopEndRangeCheck - returns a range check node which |
| 154 |
remembers the information necessary |
| 155 |
so that a range check for |
| 156 |
INC or DEC(d, e) |
| 157 |
can be generated later on. |
| 158 |
*) |
| 159 |
|
| 160 |
PROCEDURE InitForLoopEndRangeCheck (d, e: CARDINAL) : CARDINAL ; |
| 161 |
|
| 162 |
|
| 163 |
(* |
| 164 |
InitPointerRangeCheck - creates a pointer # NIL check. |
| 165 |
*) |
| 166 |
|
| 167 |
PROCEDURE InitPointerRangeCheck (tokno: CARDINAL; |
| 168 |
d: CARDINAL; isLeft: BOOLEAN) : CARDINAL ; |
| 169 |
|
| 170 |
|
| 171 |
(* |
| 172 |
InitNoReturnRangeCheck - creates a check held in the function |
| 173 |
to detect the absence of a RETURN |
| 174 |
statement at runtime. |
| 175 |
*) |
| 176 |
|
| 177 |
PROCEDURE InitNoReturnRangeCheck () : CARDINAL ; |
| 178 |
|
| 179 |
|
| 180 |
(* |
| 181 |
InitNoElseRangeCheck - creates a check held at the end of |
| 182 |
a CASE statement without an ELSE |
| 183 |
clause to detect its absence |
| 184 |
at runtime. |
| 185 |
*) |
| 186 |
|
| 187 |
PROCEDURE InitNoElseRangeCheck () : CARDINAL ; |
| 188 |
|
| 189 |
|
| 190 |
(* |
| 191 |
InitWholeNonPosDivCheck - creates a check expression for non positive |
| 192 |
or zero 2nd operand to division. |
| 193 |
*) |
| 194 |
|
| 195 |
PROCEDURE InitWholeNonPosDivCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 196 |
|
| 197 |
|
| 198 |
(* |
| 199 |
InitWholeNonPosModCheck - creates a check expression for non positive |
| 200 |
or zero 2nd operand to modulus. |
| 201 |
*) |
| 202 |
|
| 203 |
PROCEDURE InitWholeNonPosModCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 204 |
|
| 205 |
|
| 206 |
(* |
| 207 |
InitWholeZeroDivisionCheck - creates a check expression for zero 2nd |
| 208 |
operand for division. |
| 209 |
*) |
| 210 |
|
| 211 |
PROCEDURE InitWholeZeroDivisionCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 212 |
|
| 213 |
|
| 214 |
(* |
| 215 |
InitWholeZeroRemainderCheck - creates a check expression for zero 2nd |
| 216 |
operand for remainder. |
| 217 |
*) |
| 218 |
|
| 219 |
PROCEDURE InitWholeZeroRemainderCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 220 |
|
| 221 |
|
| 222 |
(* |
| 223 |
InitInclCheck - checks to see that bit, e, is type compatible with |
| 224 |
d and also in range. |
| 225 |
*) |
| 226 |
|
| 227 |
PROCEDURE InitInclCheck (d, e: CARDINAL) : CARDINAL ; |
| 228 |
|
| 229 |
|
| 230 |
(* |
| 231 |
InitExclCheck - checks to see that bit, e, is type compatible with |
| 232 |
d and also in range. |
| 233 |
*) |
| 234 |
|
| 235 |
PROCEDURE InitExclCheck (d, e: CARDINAL) : CARDINAL ; |
| 236 |
|
| 237 |
|
| 238 |
(* |
| 239 |
InitShiftCheck - checks to see that bit, e, is type compatible with |
| 240 |
d and also in range. |
| 241 |
*) |
| 242 |
|
| 243 |
PROCEDURE InitShiftCheck (d, e: CARDINAL) : CARDINAL ; |
| 244 |
|
| 245 |
|
| 246 |
(* |
| 247 |
InitRotateCheck - checks to see that bit, e, is type compatible with |
| 248 |
d and also in range. |
| 249 |
*) |
| 250 |
|
| 251 |
PROCEDURE InitRotateCheck (d, e: CARDINAL) : CARDINAL ; |
| 252 |
|
| 253 |
|
| 254 |
(* |
| 255 |
InitTypesAssignmentCheck - checks to see that the types of, d, and, e, |
| 256 |
are assignment compatible. |
| 257 |
*) |
| 258 |
|
| 259 |
PROCEDURE InitTypesAssignmentCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 260 |
|
| 261 |
|
| 262 |
(* |
| 263 |
InitTypesParameterCheck - checks to see that the types of, d, and, e, |
| 264 |
are parameter compatible. |
| 265 |
*) |
| 266 |
|
| 267 |
PROCEDURE InitTypesParameterCheck (tokno: CARDINAL; |
| 268 |
proc: CARDINAL; paramno: CARDINAL; |
| 269 |
formal, actual: CARDINAL; |
| 270 |
depRangeId: CARDINAL) : CARDINAL ; |
| 271 |
|
| 272 |
|
| 273 |
(* |
| 274 |
InitParameterRangeCheck - checks to see that the types of, d, and, e, |
| 275 |
are parameter compatible. |
| 276 |
*) |
| 277 |
|
| 278 |
PROCEDURE InitParameterRangeCheck (tokno: CARDINAL; |
| 279 |
proc: CARDINAL; paramno: CARDINAL; |
| 280 |
formal, actual: CARDINAL; |
| 281 |
parentRangeId: CARDINAL) : CARDINAL ; |
| 282 |
|
| 283 |
|
| 284 |
(* |
| 285 |
InitTypesExpressionCheck - checks to see that the types of, d, and, e, |
| 286 |
are expression compatible. |
| 287 |
*) |
| 288 |
|
| 289 |
PROCEDURE InitTypesExpressionCheck (tokno: CARDINAL; d, e: CARDINAL; |
| 290 |
strict, isin: BOOLEAN) : CARDINAL ; |
| 291 |
|
| 292 |
|
| 293 |
(* |
| 294 |
InitTypesIndrXCheck - checks to see that the types of d and e |
| 295 |
are assignment compatible. The type checking |
| 296 |
will dereference *e during the type check. |
| 297 |
d = *e. |
| 298 |
*) |
| 299 |
|
| 300 |
PROCEDURE InitTypesIndrXCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ; |
| 301 |
|
| 302 |
|
| 303 |
(* |
| 304 |
InitTypesReturnTypeCheck - checks to see that the type of val can |
| 305 |
be returned from func. |
| 306 |
*) |
| 307 |
|
| 308 |
PROCEDURE InitTypesReturnTypeCheck (tokno: CARDINAL; func, val: CARDINAL) : CARDINAL ; |
| 309 |
|
| 310 |
|
| 311 |
(* |
| 312 |
InitCaseBounds - creates a case bound range check. |
| 313 |
*) |
| 314 |
|
| 315 |
PROCEDURE InitCaseBounds (b: CARDINAL) : CARDINAL ; |
| 316 |
|
| 317 |
|
| 318 |
(* |
| 319 |
CodeRangeCheck - creates a sequence of Trees representing the code for a |
| 320 |
range test defined by, r. |
| 321 |
*) |
| 322 |
|
| 323 |
PROCEDURE CodeRangeCheck (r: CARDINAL; function: String) ; |
| 324 |
|
| 325 |
|
| 326 |
(* |
| 327 |
FoldRangeCheck - attempts to resolve the range check. |
| 328 |
*) |
| 329 |
|
| 330 |
PROCEDURE FoldRangeCheck (tokenno: CARDINAL; quad: CARDINAL; range: CARDINAL) ; |
| 331 |
|
| 332 |
|
| 333 |
(* |
| 334 |
CodeErrorCheck - returns a Tree calling the approprate exception handler. |
| 335 |
*) |
| 336 |
|
| 337 |
PROCEDURE CodeErrorCheck (r: CARDINAL; function, message: String) : tree ; |
| 338 |
|
| 339 |
|
| 340 |
(* |
| 341 |
CheckRangeAddVariableRead - ensures that any references to reading |
| 342 |
variables used by this range check, r, |
| 343 |
at this, quadNo, are recorded in the |
| 344 |
symbol table. |
| 345 |
*) |
| 346 |
|
| 347 |
(* PROCEDURE CheckRangeAddVariableRead (r: CARDINAL; quadNo: CARDINAL) ; *) |
| 348 |
|
| 349 |
|
| 350 |
(* |
| 351 |
CheckRangeRemoveVariableRead - ensures that any references to reading |
| 352 |
variable at this quadNo are removed from |
| 353 |
the symbol table. |
| 354 |
*) |
| 355 |
|
| 356 |
(* PROCEDURE CheckRangeRemoveVariableRead (r: CARDINAL; quadNo: CARDINAL) ; *) |
| 357 |
|
| 358 |
|
| 359 |
(* |
| 360 |
WriteRangeCheck - displays debugging information about range, r. |
| 361 |
*) |
| 362 |
|
| 363 |
PROCEDURE WriteRangeCheck (r: CARDINAL) ; |
| 364 |
|
| 365 |
|
| 366 |
(* |
| 367 |
OverlapsRange - returns TRUE if a1..a2 overlaps with b1..b2. |
| 368 |
*) |
| 369 |
|
| 370 |
PROCEDURE OverlapsRange (a1, a2, b1, b2: tree) : BOOLEAN ; |
| 371 |
|
| 372 |
|
| 373 |
(* |
| 374 |
IsEqual - returns TRUE if a=b. |
| 375 |
*) |
| 376 |
|
| 377 |
PROCEDURE IsEqual (a, b: tree) : BOOLEAN ; |
| 378 |
|
| 379 |
|
| 380 |
(* |
| 381 |
IsGreaterOrEqual - returns TRUE if a>=b. |
| 382 |
*) |
| 383 |
|
| 384 |
PROCEDURE IsGreaterOrEqual (a, b: tree) : BOOLEAN ; |
| 385 |
|
| 386 |
|
| 387 |
(* |
| 388 |
IsGreater - returns TRUE if a>b. |
| 389 |
*) |
| 390 |
|
| 391 |
PROCEDURE IsGreater (a, b: tree) : BOOLEAN ; |
| 392 |
|
| 393 |
|
| 394 |
(* |
| 395 |
BuildIfCallWholeHandlerLoc - return a Tree containing a runtime test whether, condition, is true. |
| 396 |
*) |
| 397 |
|
| 398 |
PROCEDURE BuildIfCallWholeHandlerLoc (location: location_t; condition: tree; |
| 399 |
scope, message: ConstCharStar) : tree ; |
| 400 |
|
| 401 |
|
| 402 |
(* |
| 403 |
BuildIfCallRealHandlerLoc - return a Tree containing a runtime test whether, condition, is true. |
| 404 |
*) |
| 405 |
|
| 406 |
PROCEDURE BuildIfCallRealHandlerLoc (location: location_t; condition: tree; |
| 407 |
scope, message: ConstCharStar) : tree ; |
| 408 |
|
| 409 |
|
| 410 |
(* |
| 411 |
GetMinMax - returns TRUE if we know the max and min of a type, t. |
| 412 |
*) |
| 413 |
|
| 414 |
PROCEDURE GetMinMax (tokenno: CARDINAL; type: CARDINAL; VAR min, max: tree) : BOOLEAN ; |
| 415 |
|
| 416 |
|
| 417 |
END M2Range. |