| 1 |
(* M2LangDump.mod provides support routines for the -flang-dump. |
| 2 |
|
| 3 |
Copyright (C) 2024-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 |
IMPLEMENTATION MODULE M2LangDump ; |
| 23 |
|
| 24 |
FROM SYSTEM IMPORT ADDRESS ; |
| 25 |
|
| 26 |
FROM DynamicStrings IMPORT String, Length, InitString, Mark, Slice, EqualArray, |
| 27 |
InitStringCharStar, ConCatChar, ConCat, KillString, |
| 28 |
Dup, string, char, Index, ReverseIndex, RIndex, Equal, |
| 29 |
PushAllocation, PopAllocationExemption ; |
| 30 |
|
| 31 |
FROM SymbolTable IMPORT NulSym, |
| 32 |
GetSymName, GetLibName, |
| 33 |
GetScope, GetModuleScope, GetMainModule, GetDeclaredMod, |
| 34 |
GetLocalSym, FinalSymbol, |
| 35 |
IsInnerModule, |
| 36 |
IsVar, |
| 37 |
IsProcedure, |
| 38 |
IsModule, IsDefImp, |
| 39 |
IsExportQualified, IsExportUnQualified, |
| 40 |
IsExported, IsPublic, IsExtern, IsMonoName, |
| 41 |
IsDefinitionForC ; |
| 42 |
|
| 43 |
FROM M2Options IMPORT GetM2DumpFilter, GetDumpDir, GetDumpQuadFilename, |
| 44 |
GetDumpDeclFilename, GetDumpGimpleFilename ; |
| 45 |
|
| 46 |
FROM M2AsmUtil IMPORT GetFullSymName ; |
| 47 |
FROM M2GCCDeclare IMPORT IncludeDumpSymbol ; |
| 48 |
FROM FormatStrings IMPORT Sprintf0, Sprintf1 ; |
| 49 |
FROM NameKey IMPORT Name, GetKey, MakeKey, makekey, KeyToCharStar, NulName ; |
| 50 |
FROM SymbolConversion IMPORT Gcc2Mod, Mod2Gcc, GccKnowsAbout ; |
| 51 |
FROM M2AsmUtil IMPORT GetFullScopeAsmName ; |
| 52 |
FROM M2LexBuf IMPORT FindFileNameFromToken ; |
| 53 |
FROM M2Printf IMPORT fprintf0, fprintf1, printf0, printf1, printf2 ; |
| 54 |
FROM M2Error IMPORT InternalError ; |
| 55 |
FROM M2Batch IMPORT Get ; |
| 56 |
FROM m2misc IMPORT warning_m2_dump_filter ; |
| 57 |
FROM StrLib IMPORT StrLen ; |
| 58 |
FROM libc IMPORT printf ; |
| 59 |
|
| 60 |
IMPORT FIO, SFIO, DynamicStrings, StdIO ; |
| 61 |
|
| 62 |
|
| 63 |
CONST |
| 64 |
Debugging = FALSE ; |
| 65 |
|
| 66 |
VAR |
| 67 |
outputFile : FIO.File ; |
| 68 |
declActive, |
| 69 |
quadActive, |
| 70 |
mustClose : BOOLEAN ; |
| 71 |
NoOfQuadDumps, |
| 72 |
NoOfDeclDumps: CARDINAL ; |
| 73 |
|
| 74 |
|
| 75 |
(* |
| 76 |
Assert - call InternalError is NOT value. |
| 77 |
*) |
| 78 |
|
| 79 |
PROCEDURE Assert (value: BOOLEAN) ; |
| 80 |
BEGIN |
| 81 |
IF NOT value |
| 82 |
THEN |
| 83 |
InternalError ('assert failed in M2LangDump') |
| 84 |
END |
| 85 |
END Assert ; |
| 86 |
|
| 87 |
|
| 88 |
(* |
| 89 |
DumpWrite - writes a single ch to the dump output. |
| 90 |
*) |
| 91 |
|
| 92 |
PROCEDURE DumpWrite (ch: CHAR) ; |
| 93 |
BEGIN |
| 94 |
FIO.WriteChar (outputFile, ch) |
| 95 |
END DumpWrite ; |
| 96 |
|
| 97 |
|
| 98 |
(* |
| 99 |
CloseDump - close the dump file and pop the default write procedure. |
| 100 |
*) |
| 101 |
|
| 102 |
PROCEDURE CloseDump ; |
| 103 |
BEGIN |
| 104 |
IF mustClose |
| 105 |
THEN |
| 106 |
FIO.Close (outputFile) ; |
| 107 |
mustClose := FALSE |
| 108 |
ELSE |
| 109 |
FIO.FlushBuffer (outputFile) |
| 110 |
END ; |
| 111 |
StdIO.PopOutput ; |
| 112 |
outputFile := FIO.StdOut ; |
| 113 |
END CloseDump ; |
| 114 |
|
| 115 |
|
| 116 |
(* |
| 117 |
OpenDump - open filename as a dump file. The filename '-' is treated as stdout. |
| 118 |
It pushes a write procedure to StdIO. |
| 119 |
*) |
| 120 |
|
| 121 |
PROCEDURE OpenDump (filename: String; no: CARDINAL) ; |
| 122 |
BEGIN |
| 123 |
IF DynamicStrings.EqualArray (filename, '-') |
| 124 |
THEN |
| 125 |
mustClose := FALSE ; |
| 126 |
outputFile := FIO.StdOut |
| 127 |
ELSE |
| 128 |
filename := Sprintf1 (filename, no) ; |
| 129 |
outputFile := SFIO.OpenToWrite (filename) ; |
| 130 |
mustClose := FIO.IsNoError (outputFile) |
| 131 |
END ; |
| 132 |
filename := KillString (filename) ; |
| 133 |
StdIO.PushOutput (DumpWrite) |
| 134 |
END OpenDump ; |
| 135 |
|
| 136 |
|
| 137 |
(* |
| 138 |
CloseDumpDecl - close the dump output file. |
| 139 |
*) |
| 140 |
|
| 141 |
PROCEDURE CloseDumpDecl ; |
| 142 |
BEGIN |
| 143 |
IF declActive |
| 144 |
THEN |
| 145 |
CloseDump ; |
| 146 |
declActive := FALSE |
| 147 |
END |
| 148 |
END CloseDumpDecl ; |
| 149 |
|
| 150 |
|
| 151 |
(* |
| 152 |
AddRuleTextDump - filter on the textual name given to GCC. |
| 153 |
*) |
| 154 |
|
| 155 |
PROCEDURE AddRuleTextDump (rule: String) ; |
| 156 |
VAR |
| 157 |
sym : CARDINAL ; |
| 158 |
key : Name ; |
| 159 |
seenMatch: BOOLEAN ; |
| 160 |
BEGIN |
| 161 |
sym := 1 ; |
| 162 |
seenMatch := FALSE ; |
| 163 |
key := makekey (string (rule)) ; |
| 164 |
WHILE sym <= FinalSymbol () DO |
| 165 |
IF IsProcedure (sym) AND (key = GetFullSymName (sym)) |
| 166 |
THEN |
| 167 |
IncludeDumpSymbol (sym) ; |
| 168 |
seenMatch := TRUE |
| 169 |
END ; |
| 170 |
INC (sym) |
| 171 |
END ; |
| 172 |
CheckRuleMatch (seenMatch, rule) |
| 173 |
END AddRuleTextDump ; |
| 174 |
|
| 175 |
|
| 176 |
(* |
| 177 |
AddRuleScopeQualidentDump - |
| 178 |
*) |
| 179 |
|
| 180 |
PROCEDURE AddRuleScopeQualidentDump (rule: String; dot: INTEGER; modsym: CARDINAL) ; |
| 181 |
VAR |
| 182 |
modstr, |
| 183 |
idstr : String ; |
| 184 |
start : INTEGER ; |
| 185 |
sym : CARDINAL ; |
| 186 |
BEGIN |
| 187 |
start := dot + 1 ; |
| 188 |
dot := Index (rule, '.', start) ; |
| 189 |
WHILE dot > 0 DO |
| 190 |
modstr := Slice (rule, start, dot) ; |
| 191 |
modsym := GetLocalSym (modsym, makekey (string (modstr))) ; |
| 192 |
IF (modsym # NulSym) AND IsModule (modsym) |
| 193 |
THEN |
| 194 |
start := dot + 1 ; |
| 195 |
dot := Index (rule, '.', start) |
| 196 |
ELSE |
| 197 |
modstr := KillString (modstr) ; |
| 198 |
RETURN |
| 199 |
END |
| 200 |
END ; |
| 201 |
idstr := Slice (rule, start, 0) ; |
| 202 |
sym := GetLocalSym (modsym, makekey (string (idstr))) ; |
| 203 |
IF sym = NulSym |
| 204 |
THEN |
| 205 |
CheckRuleMatch (FALSE, rule) |
| 206 |
ELSE |
| 207 |
IncludeDumpSymbol (sym) |
| 208 |
END |
| 209 |
END AddRuleScopeQualidentDump ; |
| 210 |
|
| 211 |
|
| 212 |
(* |
| 213 |
AddRuleScopeDump - |
| 214 |
*) |
| 215 |
|
| 216 |
PROCEDURE AddRuleScopeDump (rule: String) ; |
| 217 |
VAR |
| 218 |
modsym: CARDINAL ; |
| 219 |
libstr, |
| 220 |
modstr: String ; |
| 221 |
start, |
| 222 |
dot : INTEGER ; |
| 223 |
BEGIN |
| 224 |
dot := Index (rule, '.', 0) ; |
| 225 |
Assert (dot # -1) ; |
| 226 |
libstr := NIL ; |
| 227 |
modstr := Slice (rule, 0, dot) ; |
| 228 |
modsym := Get (makekey (string (modstr))) ; |
| 229 |
IF modsym = NulSym |
| 230 |
THEN |
| 231 |
libstr := modstr ; |
| 232 |
start := dot + 1 ; |
| 233 |
dot := Index (rule, '.', start) ; |
| 234 |
IF dot > 0 |
| 235 |
THEN |
| 236 |
modstr := Slice (rule, start, dot) ; |
| 237 |
modsym := Get (makekey (string (modstr))) ; |
| 238 |
IF (modsym # NulSym) AND (makekey (string (libstr)) = GetLibName (modsym)) |
| 239 |
THEN |
| 240 |
AddRuleScopeQualidentDump (rule, dot, modsym) |
| 241 |
END |
| 242 |
END |
| 243 |
ELSE |
| 244 |
AddRuleScopeQualidentDump (rule, dot, modsym) |
| 245 |
END |
| 246 |
END AddRuleScopeDump ; |
| 247 |
|
| 248 |
|
| 249 |
(* |
| 250 |
GenQualidentSymString - returns the qualified sym string (including |
| 251 |
any nested procedure and modules). |
| 252 |
*) |
| 253 |
|
| 254 |
PROCEDURE GenQualidentSymString (sym: CARDINAL) : String ; |
| 255 |
VAR |
| 256 |
identstr, |
| 257 |
qualidentstr: String ; |
| 258 |
BEGIN |
| 259 |
qualidentstr := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ; |
| 260 |
WHILE GetScope (sym) # NulSym DO |
| 261 |
sym := GetScope (sym) ; |
| 262 |
identstr := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ; |
| 263 |
identstr := ConCatChar (identstr, '.') ; |
| 264 |
qualidentstr := ConCat (identstr, Mark (qualidentstr)) |
| 265 |
END ; |
| 266 |
RETURN qualidentstr |
| 267 |
END GenQualidentSymString ; |
| 268 |
|
| 269 |
|
| 270 |
(* |
| 271 |
IdentQualidentMatch - return TRUE if sym name matches symstr. |
| 272 |
*) |
| 273 |
|
| 274 |
PROCEDURE IdentQualidentMatch (sym: CARDINAL; symstr: String) : BOOLEAN ; |
| 275 |
VAR |
| 276 |
success : BOOLEAN ; |
| 277 |
qualidentstr: String ; |
| 278 |
BEGIN |
| 279 |
qualidentstr := GenQualidentSymString (sym) ; |
| 280 |
success := Equal (qualidentstr, symstr) ; |
| 281 |
qualidentstr := KillString (qualidentstr) ; |
| 282 |
RETURN success |
| 283 |
END IdentQualidentMatch ; |
| 284 |
|
| 285 |
|
| 286 |
(* |
| 287 |
IsRuleFilenameMatch - return TRUE if rule matches sym. |
| 288 |
*) |
| 289 |
|
| 290 |
PROCEDURE IsRuleFilenameMatch (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 291 |
VAR |
| 292 |
fname, |
| 293 |
modstr, |
| 294 |
symstr, |
| 295 |
filename: String ; |
| 296 |
tokenno : CARDINAL ; |
| 297 |
dot, |
| 298 |
colon : INTEGER ; |
| 299 |
success : BOOLEAN ; |
| 300 |
BEGIN |
| 301 |
tokenno := GetDeclaredMod (sym) ; |
| 302 |
filename := FindFileNameFromToken (tokenno, 0) ; |
| 303 |
fname := NIL ; |
| 304 |
symstr := NIL ; |
| 305 |
modstr := NIL ; |
| 306 |
success := FALSE ; |
| 307 |
colon := Index (rule, ':', 0) ; |
| 308 |
IF colon > 0 |
| 309 |
THEN |
| 310 |
fname := Slice (rule, 0, colon) ; |
| 311 |
IF Equal (fname, filename) |
| 312 |
THEN |
| 313 |
IF INTEGER (Length (rule)) > colon |
| 314 |
THEN |
| 315 |
symstr := Slice (rule, colon + 1, 0) ; |
| 316 |
dot := ReverseIndex (symstr, '.', -1) ; |
| 317 |
IF dot >= 0 |
| 318 |
THEN |
| 319 |
success := IdentQualidentMatch (sym, symstr) |
| 320 |
ELSE |
| 321 |
success := GetSymName (sym) = makekey (string (symstr)) |
| 322 |
END |
| 323 |
END |
| 324 |
END |
| 325 |
END ; |
| 326 |
fname := KillString (fname) ; |
| 327 |
modstr := KillString (modstr) ; |
| 328 |
symstr := KillString (symstr) ; |
| 329 |
RETURN success |
| 330 |
END IsRuleFilenameMatch ; |
| 331 |
|
| 332 |
|
| 333 |
(* |
| 334 |
CheckRuleMatch - issue a warning if seenMatch is FALSE and this is the first time |
| 335 |
the rule is matched. |
| 336 |
*) |
| 337 |
|
| 338 |
PROCEDURE CheckRuleMatch (seenMatch: BOOLEAN; rule: String) ; |
| 339 |
VAR |
| 340 |
message: String ; |
| 341 |
BEGIN |
| 342 |
IF (NoOfDeclDumps = 1) AND (NOT seenMatch) |
| 343 |
THEN |
| 344 |
message := InitString ("no symbol matching: %qs has been found when applying the dump filter") ; |
| 345 |
warning_m2_dump_filter (string (message), string (rule)); |
| 346 |
END |
| 347 |
END CheckRuleMatch ; |
| 348 |
|
| 349 |
|
| 350 |
(* |
| 351 |
AddRuleFilenameDump - |
| 352 |
*) |
| 353 |
|
| 354 |
PROCEDURE AddRuleFilenameDump (rule: String) ; |
| 355 |
VAR |
| 356 |
sym : CARDINAL ; |
| 357 |
seenMatch: BOOLEAN ; |
| 358 |
BEGIN |
| 359 |
sym := 1 ; |
| 360 |
seenMatch := FALSE ; |
| 361 |
WHILE sym <= FinalSymbol () DO |
| 362 |
IF IsProcedure (sym) AND IsRuleFilenameMatch (rule, sym) |
| 363 |
THEN |
| 364 |
IncludeDumpSymbol (sym) ; |
| 365 |
seenMatch := TRUE |
| 366 |
END ; |
| 367 |
INC (sym) |
| 368 |
END ; |
| 369 |
CheckRuleMatch (seenMatch, rule) |
| 370 |
END AddRuleFilenameDump ; |
| 371 |
|
| 372 |
|
| 373 |
(* |
| 374 |
AddRuleSymToDump - call appropriate sub rule. FileName if : present. |
| 375 |
Scope if . present otherwise assume a text rule. |
| 376 |
*) |
| 377 |
|
| 378 |
PROCEDURE AddRuleSymToDump (rule: String) ; |
| 379 |
BEGIN |
| 380 |
IF Index (rule, ':', 0) # -1 |
| 381 |
THEN |
| 382 |
(* Filename and scope rule. *) |
| 383 |
AddRuleFilenameDump (rule) |
| 384 |
ELSIF Index (rule, '.', 0) # -1 |
| 385 |
THEN |
| 386 |
(* Modula-2 scoping tests. *) |
| 387 |
AddRuleScopeDump (rule) |
| 388 |
ELSE |
| 389 |
(* Text decl tests. *) |
| 390 |
AddRuleTextDump (rule) |
| 391 |
END |
| 392 |
END AddRuleSymToDump ; |
| 393 |
|
| 394 |
|
| 395 |
(* |
| 396 |
AddFilterListToDumpWatch - |
| 397 |
*) |
| 398 |
|
| 399 |
PROCEDURE AddFilterListToDumpWatch (filter: ADDRESS) ; |
| 400 |
VAR |
| 401 |
rule, |
| 402 |
full : String ; |
| 403 |
start, |
| 404 |
i : INTEGER ; |
| 405 |
BEGIN |
| 406 |
full := InitStringCharStar (filter) ; |
| 407 |
start := 0 ; |
| 408 |
REPEAT |
| 409 |
i := Index (full, ',', start) ; |
| 410 |
IF i = -1 |
| 411 |
THEN |
| 412 |
rule := Slice (full, start, 0) |
| 413 |
ELSE |
| 414 |
rule := Slice (full, start, i) |
| 415 |
END ; |
| 416 |
AddRuleSymToDump (rule) ; |
| 417 |
rule := KillString (rule) ; |
| 418 |
start := i+1 ; |
| 419 |
UNTIL i = -1 ; |
| 420 |
full := KillString (full) ; |
| 421 |
END AddFilterListToDumpWatch ; |
| 422 |
|
| 423 |
|
| 424 |
(* |
| 425 |
CreateDumpTitle - creates the underlined title. |
| 426 |
*) |
| 427 |
|
| 428 |
PROCEDURE CreateDumpTitle (title: ARRAY OF CHAR) ; |
| 429 |
VAR |
| 430 |
len, |
| 431 |
text, |
| 432 |
i : CARDINAL ; |
| 433 |
s : String ; |
| 434 |
BEGIN |
| 435 |
s := Sprintf0 (Mark (InitString (title))) ; |
| 436 |
s := KillString (SFIO.WriteS (GetDumpFile (), s)) ; |
| 437 |
len := StrLen (title) ; |
| 438 |
i := 0 ; |
| 439 |
text := 0 ; |
| 440 |
WHILE i < len DO |
| 441 |
IF title[i] = '\' |
| 442 |
THEN |
| 443 |
INC (i, 2) |
| 444 |
ELSE |
| 445 |
INC (i) ; |
| 446 |
INC (text) |
| 447 |
END |
| 448 |
END ; |
| 449 |
s := DynamicStrings.Mult (Mark (InitString ('=')), text) ; |
| 450 |
s := KillString (SFIO.WriteS (GetDumpFile (), s)) ; |
| 451 |
fprintf0 (GetDumpFile (), '\n'); |
| 452 |
END CreateDumpTitle ; |
| 453 |
|
| 454 |
|
| 455 |
(* |
| 456 |
CreateDumpDecl - create the dump file for a decl dump. |
| 457 |
*) |
| 458 |
|
| 459 |
PROCEDURE CreateDumpDecl (title: ARRAY OF CHAR) ; |
| 460 |
BEGIN |
| 461 |
IF GetM2DumpFilter () # NIL |
| 462 |
THEN |
| 463 |
Assert (NOT declActive) ; |
| 464 |
Assert (NOT quadActive) ; |
| 465 |
declActive := TRUE ; |
| 466 |
INC (NoOfDeclDumps) ; |
| 467 |
OpenDump (MakeDeclTemplate (), NoOfDeclDumps) ; |
| 468 |
CreateDumpTitle (title) ; |
| 469 |
AddFilterListToDumpWatch (GetM2DumpFilter ()) |
| 470 |
END |
| 471 |
END CreateDumpDecl ; |
| 472 |
|
| 473 |
|
| 474 |
(* |
| 475 |
CloseDumpQuad - close the dump output file. |
| 476 |
*) |
| 477 |
|
| 478 |
PROCEDURE CloseDumpQuad ; |
| 479 |
BEGIN |
| 480 |
CloseDump ; |
| 481 |
quadActive := FALSE |
| 482 |
END CloseDumpQuad ; |
| 483 |
|
| 484 |
|
| 485 |
(* |
| 486 |
CreateDumpQuad - create the dump file for a quad dump. |
| 487 |
*) |
| 488 |
|
| 489 |
PROCEDURE CreateDumpQuad (title: ARRAY OF CHAR) ; |
| 490 |
BEGIN |
| 491 |
Assert (NOT declActive) ; |
| 492 |
Assert (NOT quadActive) ; |
| 493 |
quadActive := TRUE ; |
| 494 |
INC (NoOfQuadDumps) ; |
| 495 |
OpenDump (MakeQuadTemplate (), NoOfQuadDumps) ; |
| 496 |
CreateDumpTitle (title) |
| 497 |
END CreateDumpQuad ; |
| 498 |
|
| 499 |
|
| 500 |
(* |
| 501 |
GetDumpFile - return the dump output file. |
| 502 |
*) |
| 503 |
|
| 504 |
PROCEDURE GetDumpFile () : File ; |
| 505 |
BEGIN |
| 506 |
RETURN outputFile |
| 507 |
END GetDumpFile ; |
| 508 |
|
| 509 |
|
| 510 |
(* |
| 511 |
IsDumpRequiredTree - return TRUE if the gcc tree should be dumped. |
| 512 |
*) |
| 513 |
|
| 514 |
PROCEDURE IsDumpRequiredTree (gcctree: tree; default: BOOLEAN) : BOOLEAN ; |
| 515 |
VAR |
| 516 |
sym: CARDINAL ; |
| 517 |
BEGIN |
| 518 |
sym := Gcc2Mod (gcctree) ; |
| 519 |
IF sym = NulSym |
| 520 |
THEN |
| 521 |
RETURN default |
| 522 |
ELSE |
| 523 |
RETURN IsDumpRequired (sym, default) |
| 524 |
END |
| 525 |
END IsDumpRequiredTree ; |
| 526 |
|
| 527 |
|
| 528 |
(* |
| 529 |
IsDumpRequired - return TRUE if symbol sym should be dumped |
| 530 |
according to the rules of the filter. |
| 531 |
No filter specified will always return default. |
| 532 |
The filter is a comma separated list. Each element |
| 533 |
of the list can specify a symbol three ways. |
| 534 |
Firstly by DECL name for example: m2pim_NumberIO_HexToStr |
| 535 |
Secondly by qualified scope: [pathname.]NumberIO.HexToStr |
| 536 |
Thirdly by filename and scope: NumberIO.mod:HexToStr |
| 537 |
*) |
| 538 |
|
| 539 |
PROCEDURE IsDumpRequired (sym: CARDINAL; default: BOOLEAN) : BOOLEAN ; |
| 540 |
VAR |
| 541 |
filter: String ; |
| 542 |
BEGIN |
| 543 |
filter := GetM2DumpFilter () ; |
| 544 |
IF filter = NIL |
| 545 |
THEN |
| 546 |
RETURN default |
| 547 |
ELSE |
| 548 |
RETURN Match (filter, sym) |
| 549 |
END |
| 550 |
END IsDumpRequired ; |
| 551 |
|
| 552 |
|
| 553 |
(* |
| 554 |
Match - return TRUE if sym matches any of the filter rules. |
| 555 |
*) |
| 556 |
|
| 557 |
PROCEDURE Match (filter: ADDRESS; sym: CARDINAL) : BOOLEAN ; |
| 558 |
VAR |
| 559 |
result: BOOLEAN ; |
| 560 |
rule, |
| 561 |
full : String ; |
| 562 |
start, |
| 563 |
i : INTEGER ; |
| 564 |
BEGIN |
| 565 |
full := InitStringCharStar (filter) ; |
| 566 |
start := 0 ; |
| 567 |
REPEAT |
| 568 |
i := Index (full, ',', start) ; |
| 569 |
IF i = -1 |
| 570 |
THEN |
| 571 |
rule := Slice (full, start, 0) |
| 572 |
ELSE |
| 573 |
rule := Slice (full, start, i) |
| 574 |
END ; |
| 575 |
result := MatchRule (rule, sym) ; |
| 576 |
rule := KillString (rule) ; |
| 577 |
IF result |
| 578 |
THEN |
| 579 |
full := KillString (full) ; |
| 580 |
RETURN TRUE |
| 581 |
END ; |
| 582 |
start := i+1 ; |
| 583 |
UNTIL i = -1 ; |
| 584 |
full := KillString (full) ; |
| 585 |
RETURN FALSE |
| 586 |
END Match ; |
| 587 |
|
| 588 |
|
| 589 |
(* |
| 590 |
MatchRule - return TRUE if rule matches sym. |
| 591 |
*) |
| 592 |
|
| 593 |
PROCEDURE MatchRule (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 594 |
BEGIN |
| 595 |
IF Index (rule, ':', 0) # -1 |
| 596 |
THEN |
| 597 |
(* Filename and scope qualification tests. *) |
| 598 |
RETURN MatchRuleFilenameScope (rule, sym) |
| 599 |
ELSIF Index (rule, '.', 0) # -1 |
| 600 |
THEN |
| 601 |
(* Modula-2 scoping tests. *) |
| 602 |
RETURN MatchRuleScope (rule, sym) |
| 603 |
ELSE |
| 604 |
(* Text decl tests. *) |
| 605 |
RETURN MatchRuleText (rule, sym) |
| 606 |
END |
| 607 |
END MatchRule ; |
| 608 |
|
| 609 |
|
| 610 |
(* |
| 611 |
MatchRuleFilenameScope - returns TRUE if rule contains filename.ext:qualident |
| 612 |
and it matches sym. |
| 613 |
*) |
| 614 |
|
| 615 |
PROCEDURE MatchRuleFilenameScope (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 616 |
VAR |
| 617 |
rulefile, |
| 618 |
symfile, |
| 619 |
subrule : String ; |
| 620 |
BEGIN |
| 621 |
rulefile := Slice (rule, 0, Index (rule, ':', 0)) ; |
| 622 |
(* Do not deallocate symfile. *) |
| 623 |
symfile := FindFileNameFromToken (GetDeclaredMod (sym), 0) ; |
| 624 |
IF TextMatch (rulefile, symfile) |
| 625 |
THEN |
| 626 |
subrule := Slice (rule, Index (rule, ':', 0) + 1, 0) ; |
| 627 |
IF MatchRuleScope (subrule, sym) |
| 628 |
THEN |
| 629 |
subrule := KillString (subrule) ; |
| 630 |
RETURN TRUE |
| 631 |
END |
| 632 |
END ; |
| 633 |
rulefile := KillString (rulefile) ; |
| 634 |
RETURN FALSE |
| 635 |
END MatchRuleFilenameScope ; |
| 636 |
|
| 637 |
|
| 638 |
(* |
| 639 |
MatchRuleScope - returns TRUE if rule contains a [libname.]qualified.ident |
| 640 |
and it matches sym. |
| 641 |
*) |
| 642 |
|
| 643 |
PROCEDURE MatchRuleScope (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 644 |
VAR |
| 645 |
i : INTEGER ; |
| 646 |
name: Name ; |
| 647 |
BEGIN |
| 648 |
IF Debugging |
| 649 |
THEN |
| 650 |
name := GetSymName (sym) ; |
| 651 |
printf2 ("MatchRuleScope (%s, %a)\n", rule, name) |
| 652 |
END ; |
| 653 |
(* Compare qualident right to left. *) |
| 654 |
i := RIndex (rule, '.', 0) ; |
| 655 |
IF i = -1 |
| 656 |
THEN |
| 657 |
(* No qualification, just the ident. *) |
| 658 |
RETURN MatchRuleIdent (rule, sym) |
| 659 |
ELSE |
| 660 |
RETURN MatchRuleQualident (rule, Slice (rule, i+1, 0), i, sym) |
| 661 |
END |
| 662 |
END MatchRuleScope ; |
| 663 |
|
| 664 |
|
| 665 |
(* |
| 666 |
MatchRuleQualident - returns TRUE if rule matches qualified sym. |
| 667 |
PostCondition: subrule will be deallocated upon exit. |
| 668 |
TRUE is returned if rule matches qualified sym. |
| 669 |
*) |
| 670 |
|
| 671 |
PROCEDURE MatchRuleQualident (rule, subrule: String; i: INTEGER; sym: CARDINAL) : BOOLEAN ; |
| 672 |
VAR |
| 673 |
scope: CARDINAL ; |
| 674 |
BEGIN |
| 675 |
IF TextCompareName (subrule, GetSymName (sym)) |
| 676 |
THEN |
| 677 |
IF NOT QualifiedScope (rule, sym, i, scope) |
| 678 |
THEN |
| 679 |
RETURN FALSE |
| 680 |
END ; |
| 681 |
IF OptionalLibname (rule, sym, i, scope) |
| 682 |
THEN |
| 683 |
RETURN TRUE |
| 684 |
END |
| 685 |
END ; |
| 686 |
subrule := KillString (subrule) ; |
| 687 |
IF Debugging |
| 688 |
THEN |
| 689 |
printf0 ("MatchRuleQualident FALSE\n") |
| 690 |
END ; |
| 691 |
RETURN FALSE |
| 692 |
END MatchRuleQualident ; |
| 693 |
|
| 694 |
|
| 695 |
(* |
| 696 |
QualifiedScope - PostCondition: true is returned is rule matches a qualified sym. |
| 697 |
i is -1 if no more qualifications or libname is found. |
| 698 |
scope will be the set to the last outer scope seen. |
| 699 |
*) |
| 700 |
|
| 701 |
PROCEDURE QualifiedScope (rule: String; sym: CARDINAL; VAR i: INTEGER; VAR scope: CARDINAL) : BOOLEAN ; |
| 702 |
VAR |
| 703 |
subrule: String ; |
| 704 |
j : INTEGER ; |
| 705 |
name : Name ; |
| 706 |
BEGIN |
| 707 |
IF Debugging |
| 708 |
THEN |
| 709 |
name := GetSymName (sym) ; |
| 710 |
printf2 ("seen ident name, QualifiedScope (rule = %s, %a)\n", rule, name) |
| 711 |
END ; |
| 712 |
scope := sym ; |
| 713 |
subrule := NIL ; |
| 714 |
REPEAT |
| 715 |
j := i ; |
| 716 |
scope := GetScope (scope) ; |
| 717 |
i := ReverseIndex (rule, '.', j - 1) ; |
| 718 |
IF Debugging |
| 719 |
THEN |
| 720 |
printf2 (" reverseindex (rule = %s, '.', j = %d)\n", rule, j); |
| 721 |
printf1 (" returns i = %d\n", i) |
| 722 |
END ; |
| 723 |
IF scope # NulSym |
| 724 |
THEN |
| 725 |
subrule := KillString (subrule) ; |
| 726 |
subrule := Slice (rule, i + 1, j) ; |
| 727 |
IF Debugging |
| 728 |
THEN |
| 729 |
name := GetSymName (scope) ; |
| 730 |
printf2 ("QualifiedScope (subrule = %s, %a)\n", subrule, name) |
| 731 |
END ; |
| 732 |
IF NOT TextCompareName (subrule, GetSymName (scope)) |
| 733 |
THEN |
| 734 |
subrule := KillString (subrule) ; |
| 735 |
IF Debugging |
| 736 |
THEN |
| 737 |
printf0 ("QualifiedScope FALSE\n") |
| 738 |
END ; |
| 739 |
RETURN FALSE |
| 740 |
END |
| 741 |
END |
| 742 |
UNTIL (i <= 0) OR IsDefImp (scope) OR IsModule (scope) ; |
| 743 |
subrule := KillString (subrule) ; |
| 744 |
RETURN TRUE |
| 745 |
END QualifiedScope ; |
| 746 |
|
| 747 |
|
| 748 |
(* |
| 749 |
OptionalLibname - returns TRUE if rule[0..dot] matches syms libname or |
| 750 |
if there is no libname the scope is a module or defimp |
| 751 |
symbol. |
| 752 |
*) |
| 753 |
|
| 754 |
PROCEDURE OptionalLibname (rule: String; sym: CARDINAL; |
| 755 |
dot: INTEGER; scope: CARDINAL) : BOOLEAN ; |
| 756 |
VAR |
| 757 |
subrule: String ; |
| 758 |
BEGIN |
| 759 |
IF dot > 0 |
| 760 |
THEN |
| 761 |
(* Check for optional libname. *) |
| 762 |
subrule := Slice (rule, 0, dot) ; |
| 763 |
IF Debugging |
| 764 |
THEN |
| 765 |
printf2 ("checking for optional libname (subrule = %s, '.', dot = %d)\n", |
| 766 |
rule, dot) |
| 767 |
END ; |
| 768 |
IF TextCompareName (subrule, GetLibName (GetModuleScope (sym))) |
| 769 |
THEN |
| 770 |
subrule := KillString (subrule) ; |
| 771 |
IF Debugging |
| 772 |
THEN |
| 773 |
printf0 ("OptionalLibname TRUE\n") |
| 774 |
END ; |
| 775 |
RETURN TRUE |
| 776 |
END ; |
| 777 |
subrule := KillString (subrule) |
| 778 |
ELSIF (scope # NulSym) AND (IsModule (scope) OR IsDefImp (scope)) |
| 779 |
THEN |
| 780 |
IF Debugging |
| 781 |
THEN |
| 782 |
printf0 ("OptionalLibname TRUE\n") |
| 783 |
END ; |
| 784 |
RETURN TRUE |
| 785 |
END ; |
| 786 |
RETURN FALSE |
| 787 |
END OptionalLibname ; |
| 788 |
|
| 789 |
|
| 790 |
(* |
| 791 |
MatchRuleIdent - return TRUE if ident sym matches rule. |
| 792 |
The ident must be in a module or defimp scope. |
| 793 |
*) |
| 794 |
|
| 795 |
PROCEDURE MatchRuleIdent (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 796 |
VAR |
| 797 |
scope: CARDINAL ; |
| 798 |
BEGIN |
| 799 |
IF TextCompareName (rule, GetSymName (sym)) |
| 800 |
THEN |
| 801 |
scope := GetScope (sym) ; |
| 802 |
RETURN IsModule (scope) OR IsDefImp (scope) |
| 803 |
END ; |
| 804 |
RETURN FALSE |
| 805 |
END MatchRuleIdent ; |
| 806 |
|
| 807 |
|
| 808 |
(* |
| 809 |
MatchRuleText - returns TRUE if rule matches sym. |
| 810 |
*) |
| 811 |
|
| 812 |
PROCEDURE MatchRuleText (rule: String; sym: CARDINAL) : BOOLEAN ; |
| 813 |
BEGIN |
| 814 |
RETURN TextCompareName (rule, GetFullScopeAsmName (sym)) |
| 815 |
END MatchRuleText ; |
| 816 |
|
| 817 |
|
| 818 |
(* |
| 819 |
TextCompareName - return TRUE if rule matches name. |
| 820 |
*) |
| 821 |
|
| 822 |
PROCEDURE TextCompareName (rule: String; name: Name) : BOOLEAN ; |
| 823 |
VAR |
| 824 |
result: BOOLEAN ; |
| 825 |
text : String ; |
| 826 |
BEGIN |
| 827 |
text := InitStringCharStar (KeyToCharStar (name)) ; |
| 828 |
result := TextMatch (rule, text) ; |
| 829 |
text := KillString (text) ; |
| 830 |
RETURN result |
| 831 |
END TextCompareName ; |
| 832 |
|
| 833 |
|
| 834 |
(* |
| 835 |
TextMatch - returns TRUE if rule matches text. Currently this |
| 836 |
is a simple string compare, but could be extended |
| 837 |
to implement regexp (seen in the rule). |
| 838 |
*) |
| 839 |
|
| 840 |
PROCEDURE TextMatch (rule, text: String) : BOOLEAN ; |
| 841 |
BEGIN |
| 842 |
IF Debugging |
| 843 |
THEN |
| 844 |
printf2 ("TextMatch (%s, %s)\n", rule, text) |
| 845 |
END ; |
| 846 |
RETURN Equal (rule, text) |
| 847 |
END TextMatch ; |
| 848 |
|
| 849 |
|
| 850 |
(* |
| 851 |
CreateTemplate - create and return a template filename with extension. |
| 852 |
If the user has specified "-" then "-" is returned otherwise |
| 853 |
a template is formed from "dumpdir + filename + .%03dl.extension". |
| 854 |
*) |
| 855 |
|
| 856 |
PROCEDURE CreateTemplate (filename, extension: String) : String ; |
| 857 |
BEGIN |
| 858 |
IF filename = NIL |
| 859 |
THEN |
| 860 |
(* User has not specified a file. *) |
| 861 |
IF GetDumpDir () = NIL |
| 862 |
THEN |
| 863 |
filename := InitStringCharStar (KeyToCharStar (GetSymName (GetMainModule ()))) |
| 864 |
ELSE |
| 865 |
filename := Dup (GetDumpDir ()) ; |
| 866 |
filename := ConCat (filename, Mark (InitStringCharStar (KeyToCharStar (GetSymName (GetMainModule ()))))) |
| 867 |
END ; |
| 868 |
filename := ConCat (filename, Mark (InitString ('.mod'))) |
| 869 |
ELSE |
| 870 |
(* We need to duplicate the filename to create a new string before ConCat |
| 871 |
is used later on. *) |
| 872 |
filename := Dup (filename) |
| 873 |
END ; |
| 874 |
IF NOT EqualArray (filename, '-') |
| 875 |
THEN |
| 876 |
filename := ConCat (ConCat (filename, InitString ('.%03dl.')), extension) |
| 877 |
END ; |
| 878 |
RETURN filename |
| 879 |
END CreateTemplate ; |
| 880 |
|
| 881 |
|
| 882 |
(* |
| 883 |
MakeQuadTemplate - return a template for the quad dump file. |
| 884 |
*) |
| 885 |
|
| 886 |
PROCEDURE MakeQuadTemplate () : String ; |
| 887 |
BEGIN |
| 888 |
RETURN CreateTemplate (GetDumpQuadFilename (), InitString ('quad')) |
| 889 |
END MakeQuadTemplate ; |
| 890 |
|
| 891 |
|
| 892 |
(* |
| 893 |
MakeDeclTemplate - return a template for the decl dump file. |
| 894 |
*) |
| 895 |
|
| 896 |
PROCEDURE MakeDeclTemplate () : String ; |
| 897 |
BEGIN |
| 898 |
RETURN CreateTemplate (GetDumpDeclFilename (), InitString ('decl')) |
| 899 |
END MakeDeclTemplate ; |
| 900 |
|
| 901 |
|
| 902 |
(* |
| 903 |
MakeGimpleTemplate - return a template for the gimple dump file and assign |
| 904 |
len to the max number of characters required to complete |
| 905 |
a template (including a nul terminator). |
| 906 |
*) |
| 907 |
|
| 908 |
PROCEDURE MakeGimpleTemplate (VAR len: CARDINAL) : String ; |
| 909 |
VAR |
| 910 |
filename: String ; |
| 911 |
BEGIN |
| 912 |
filename := CreateTemplate (GetDumpGimpleFilename (), InitString ('gimple')) ; |
| 913 |
len := Length (filename) ; (* This is a short cut based on '%03d' format |
| 914 |
specifier used above. *) |
| 915 |
RETURN filename |
| 916 |
END MakeGimpleTemplate ; |
| 917 |
|
| 918 |
|
| 919 |
(* |
| 920 |
Init - initialize the module global variables. |
| 921 |
*) |
| 922 |
|
| 923 |
PROCEDURE Init ; |
| 924 |
BEGIN |
| 925 |
NoOfQuadDumps := 0 ; |
| 926 |
NoOfDeclDumps := 0 ; |
| 927 |
declActive := FALSE ; |
| 928 |
quadActive := FALSE ; |
| 929 |
mustClose := FALSE ; |
| 930 |
outputFile := FIO.StdOut |
| 931 |
END Init ; |
| 932 |
|
| 933 |
|
| 934 |
BEGIN |
| 935 |
Init |
| 936 |
END M2LangDump. |