| 1 |
@c gm2-internals.texi describes the internals of gm2. |
| 2 |
@c Copyright @copyright{} 2000-2023 Free Software Foundation, Inc. |
| 3 |
@c |
| 4 |
@c This is part of the GM2 manual. |
| 5 |
@c For copying conditions, see the file gcc/doc/include/fdl.texi. |
| 6 |
|
| 7 |
@chapter GNU Modula-2 Internals |
| 8 |
|
| 9 |
This document is a small step in the long journey of documenting the GNU |
| 10 |
Modula-2 compiler and how it integrates with GCC. |
| 11 |
The document is still in it's infancy. |
| 12 |
|
| 13 |
@menu |
| 14 |
* History:: How GNU Modula-2 came about. |
| 15 |
* Overview:: Overview of the structure of GNU Modula-2. |
| 16 |
* Integrating:: How the front end integrates with gcc. |
| 17 |
* Passes:: What gets processed during each pass. |
| 18 |
* Run time:: Integration of run time modules with the compiler. |
| 19 |
* Scope rules:: Clarification of some the scope rules. |
| 20 |
* Done list:: Progression of the GNU Modula-2 project. |
| 21 |
* To do list:: Outstanding issues. |
| 22 |
@end menu |
| 23 |
|
| 24 |
@node History, Overview, , Internals |
| 25 |
@section History |
| 26 |
|
| 27 |
This document is out of date and needs to be rewritten. |
| 28 |
|
| 29 |
The Modula-2 compiler sources have come from the m2f compiler which |
| 30 |
runs under GNU/Linux. The original m2f compiler was written in Modula-2 |
| 31 |
and was bootstrapped via a modified version of p2c 1.20. The m2f |
| 32 |
compiler was a recursive descent which generated quadruples as |
| 33 |
intermediate code. It also used C style calling convention wherever |
| 34 |
possible and utilized a C structure for dynamic arrays. |
| 35 |
|
| 36 |
@node Overview, Integrating, History, Internals |
| 37 |
@section Overview |
| 38 |
|
| 39 |
GNU Modula-2 uses flex and a machine generated recursive descent |
| 40 |
parser. Most of the source code is written in Modula-2 and |
| 41 |
bootstrapping is achieved via a modified version of p2c-1.20. |
| 42 |
The modified p2c-1.20 is contained in the GNU Modula-2 source |
| 43 |
tree as are a number of other tools necessary for bootstrapping. |
| 44 |
|
| 45 |
The changes to p2c include: |
| 46 |
|
| 47 |
@itemize @bullet |
| 48 |
@item |
| 49 |
allowing @code{DEFINITION MODULE FOR "C"} |
| 50 |
@item |
| 51 |
fixes to abstract data types. |
| 52 |
@item |
| 53 |
making p2c understand the 2nd Edition dialect of Modula-2. |
| 54 |
@item |
| 55 |
introducing the @code{UNQUALIFIED} keyword. |
| 56 |
@item |
| 57 |
allowing varargs (@code{...}) inside @code{DEFINITION MODULE FOR "C"} modules. |
| 58 |
@item |
| 59 |
fixing the parser to understand commented @code{FORWARD} prototypes, |
| 60 |
which are ignored by GNU Modula-2. |
| 61 |
@item |
| 62 |
fixes to the @code{CASE} syntax for 2nd Edition Modula-2. |
| 63 |
@item |
| 64 |
fixes to a @code{FOR} loop counting down to zero using a @code{CARDINAL}. |
| 65 |
@item |
| 66 |
introducing an initialization section for each implementation module. |
| 67 |
@item |
| 68 |
various porting improvements and general tidying up so that |
| 69 |
it compiles with the gcc option @code{-Wall}. |
| 70 |
@end itemize |
| 71 |
|
| 72 |
GNU Modula-2 comes with PIM and ISO style libraries. The compiler |
| 73 |
is built using PIM libraries and the source of the compiler |
| 74 |
complies with the PIM dialect together with a few @code{C} |
| 75 |
library calling extensions. |
| 76 |
|
| 77 |
The compiler is a four pass compiler. The first pass tokenizes |
| 78 |
the source code, creates scope and enumeration type symbols. |
| 79 |
All tokens are placed into a dynamic buffer and subsequent passes reread |
| 80 |
tokens and build types, quadruples and resolve hidden types. |
| 81 |
@xref{Passes, , ,}. |
| 82 |
|
| 83 |
GNU Modula-2 uses a technique of double book keeping @footnote{See the |
| 84 |
excellent tutorial by Joachim Nadler translated by Tim Josling}. |
| 85 |
@xref{Back end Access to Symbol Table, , , gcc}. |
| 86 |
The front end builds a complete symbol table and a list of quadruples. |
| 87 |
Each symbol is translated into a @code{gcc} equivalent after which |
| 88 |
each quadruple is translated into a @code{gcc} @code{tree}. |
| 89 |
|
| 90 |
@node Integrating, Passes, Overview, Internals |
| 91 |
@section How the front end integrates with gcc |
| 92 |
|
| 93 |
The M2Base and M2System |
| 94 |
modules contain base types and system types respectively they |
| 95 |
map onto GCC back-end data types. |
| 96 |
|
| 97 |
@node Passes, Run time, Integrating, Internals |
| 98 |
@section Passes |
| 99 |
|
| 100 |
This section describes the general actions of each pass. The key to |
| 101 |
building up the symbol table correctly is to ensure that the symbols |
| 102 |
are only created in the scope where they were declared. This may seem |
| 103 |
obvious (and easy) but it is complicated by two issues: firstly GNU |
| 104 |
Modula-2 does not generate @code{.sym} files and so all imported |
| 105 |
definition modules are parsed after the module is parsed; secondly the |
| 106 |
import/export rules might mean that you can see and use a symbol |
| 107 |
before it is declared in a completely different scope. |
| 108 |
|
| 109 |
Here is a brief description of the lists of symbols maintained within |
| 110 |
@code{DefImp} and @code{Module} symbols. It is these lists and actions |
| 111 |
at each pass which manipulate these lists which solve the scoping and |
| 112 |
visability of all symbols. |
| 113 |
|
| 114 |
The @code{DefImp} symbol maintains the: @code{ExportQualified}, |
| 115 |
@code{ExportUnQualified}, @code{ExportRequest}, @code{IncludeList}, |
| 116 |
@code{ImportTree}, @code{ExportUndeclared}, |
| 117 |
@code{NeedToBeImplemented}, @code{LocalSymbols}, |
| 118 |
@code{EnumerationScopeList}, @code{Unresolved}, @code{ListOfVars}, |
| 119 |
@code{ListOfProcs} and @code{ListOfModules} lists. |
| 120 |
|
| 121 |
The @code{Module} symbol maintains the: @code{LocalSymbols}, |
| 122 |
@code{ExportTree}, @code{IncludeList}, @code{ImportTree}, |
| 123 |
@code{ExportUndeclared}, @code{EnumerationScopeList}, |
| 124 |
@code{Unresolved}, @code{ListOfVars}, @code{ListOfProcs} and |
| 125 |
@code{ListOfModules} lists. |
| 126 |
|
| 127 |
Initially we discuss the lists which are common to both @code{DefImp} |
| 128 |
and @code{Module} symbols, thereafter the lists peculiar to @code{DefImp} |
| 129 |
and @code{Module} symbols are discussed. |
| 130 |
|
| 131 |
The @code{ListOfVars}, @code{ListOfProcs} and @code{ListOfModules} |
| 132 |
lists (common to both symbols) and simply contain a list of |
| 133 |
variables, procedures and inner modules which are declared with this |
| 134 |
definition/implementation or program module. |
| 135 |
|
| 136 |
The @code{LocalSymbols} list (common to both symbols) contains a |
| 137 |
complete list of symbols visible in this modules scope. The symbols in |
| 138 |
this list may have been imported or exported from an inner module. |
| 139 |
|
| 140 |
The @code{EnumerationScope} list (common to both symbols) defines all |
| 141 |
visible enumeration symbols. When this module is parsed the contents |
| 142 |
of these enumeration types are marked as visible. Internally to GNU |
| 143 |
Modula-2 these form a pseudo scope (rather like a @code{WITH} |
| 144 |
statement which temporarily makes the fields of the record visible). |
| 145 |
|
| 146 |
The @code{ExportUndeclared} list (common to both symbols) contains a |
| 147 |
list of all symbols marked as exported but are as yet undeclared. |
| 148 |
|
| 149 |
The @code{IncludeList} is (common to both symbols) contains a list of |
| 150 |
all modules imported by the @code{IMPORT modulename ;} construct. |
| 151 |
|
| 152 |
The @code{ImportTree} (common to both symbols) contains a tree of all |
| 153 |
imported identifiers. |
| 154 |
|
| 155 |
The @code{ExportQualified} and @code{ExportUnQualified} trees (only |
| 156 |
present in the @code{DefImp} symbol) contain identifiers which are |
| 157 |
marked as @code{EXPORT QUALIFIED} and @code{EXPORT UNQUALIFIED} |
| 158 |
respectively. |
| 159 |
|
| 160 |
The @code{NeedToBeImplemented} list (only present in the @code{DefImp} |
| 161 |
symbol) and contains a list of all unresolved symbols which are exported. |
| 162 |
|
| 163 |
@subsection Pass 1 |
| 164 |
|
| 165 |
During pass 1 each @code{DefImp} and @code{Module} symbol is |
| 166 |
created. These are also placed into a list of outstanding sources to |
| 167 |
be parsed. The import and export lists are recorded and each object |
| 168 |
imported is created in the module from whence it is exported and added |
| 169 |
into the imported list of the current module. Any exported objects are |
| 170 |
placed into the export list and marked as qualified or unqualified. |
| 171 |
|
| 172 |
Inner module symbols are also created and their import and export |
| 173 |
lists are also processed. An import list will result in a symbol being |
| 174 |
fetched (or created if it does not exist) from the outer scope and |
| 175 |
placed into the scope of the inner module. An export list results in |
| 176 |
each symbol being fetched or created in the current inner scope and |
| 177 |
added to the outer scope. If the symbol has not yet been declared then |
| 178 |
it is added to the current modules @code{ExportUndeclared} list. |
| 179 |
|
| 180 |
Procedure symbols are created (the parameters are parsed but no more |
| 181 |
symbols are created). Enumerated types are created, hidden types in |
| 182 |
the definition modules are marked as such. All the rest of the Modula-2 |
| 183 |
syntax is parsed but no symbols are created. |
| 184 |
|
| 185 |
@subsection Pass 2 |
| 186 |
|
| 187 |
This section discuss varient records and their representation within |
| 188 |
the front end @file{gm2/gm2-compiler/SymbolTable.mod}. Records and |
| 189 |
varient records are declared in pass 2. |
| 190 |
|
| 191 |
Ordinary records are represented by the following symbol table entries: |
| 192 |
|
| 193 |
@example |
| 194 |
TYPE |
| 195 |
this = RECORD |
| 196 |
foo: CARDINAL ; |
| 197 |
bar: CHAR ; |
| 198 |
END ; |
| 199 |
|
| 200 |
|
| 201 |
SymRecord [1] |
| 202 |
+-------------+ |
| 203 |
| Name = this | SymRecordField [2] |
| 204 |
| ListOfSons | +-------------------+ |
| 205 |
| +--------| | Name = foo | |
| 206 |
| | [2] [3]| | Parent = [1] | |
| 207 |
+-------------+ | Type = [Cardinal] | |
| 208 |
| LocalSymbols| +-------------------+ |
| 209 |
| +-----------+ |
| 210 |
| | foo bar | |
| 211 |
| +-----------+ |
| 212 |
+-------------+ |
| 213 |
|
| 214 |
|
| 215 |
SymRecordField [3] |
| 216 |
+-------------------+ |
| 217 |
| Name = bar | |
| 218 |
| Parent = [1] | |
| 219 |
| Type = [Cardinal] | |
| 220 |
+-------------------+ |
| 221 |
@end example |
| 222 |
|
| 223 |
Whereas varient records are represented by the following symbol table |
| 224 |
entries: |
| 225 |
|
| 226 |
@example |
| 227 |
TYPE |
| 228 |
this = RECORD |
| 229 |
CASE tag: CHAR OF |
| 230 |
'a': foo: CARDINAL ; |
| 231 |
bar: CHAR | |
| 232 |
'b': an: REAL | |
| 233 |
ELSE |
| 234 |
END |
| 235 |
END ; |
| 236 |
|
| 237 |
|
| 238 |
SymRecord [1] |
| 239 |
+-------------+ |
| 240 |
| Name = this | SymRecordField [2] |
| 241 |
| ListOfSons | +-------------------+ |
| 242 |
| +--------| | Name = tag | |
| 243 |
| | [2] [3]| | Parent = [1] | |
| 244 |
| +--------+ | Type = [CHAR] | |
| 245 |
| LocalSymbols| +-------------------+ |
| 246 |
| +-----------+ |
| 247 |
| | tag foo | |
| 248 |
| | bar an | |
| 249 |
| +-----------+ |
| 250 |
+-------------+ |
| 251 |
|
| 252 |
SymVarient [3] SymFieldVarient [4] |
| 253 |
+-------------------+ +-------------------+ |
| 254 |
| Parent = [1] | | Parent = [1] | |
| 255 |
| ListOfSons | | ListOfSons | |
| 256 |
| +--------------| | +--------------| |
| 257 |
| | [4] [5] | | | [6] [7] | |
| 258 |
+-------------------+ +-------------------+ |
| 259 |
|
| 260 |
SymFieldVarient [5] |
| 261 |
+-------------------+ |
| 262 |
| Parent = [1] | |
| 263 |
| ListOfSons | |
| 264 |
| +--------------| |
| 265 |
| | [8] | |
| 266 |
+-------------------+ |
| 267 |
|
| 268 |
SymRecordField [6] SymRecordField [7] |
| 269 |
+-------------------+ +-------------------+ |
| 270 |
| Name = foo | | Name = bar | |
| 271 |
| Parent = [1] | | Parent = [1] | |
| 272 |
| Type = [CARDINAL] | | Type = [CHAR] | |
| 273 |
+-------------------+ +-------------------+ |
| 274 |
|
| 275 |
SymRecordField [8] |
| 276 |
+-------------------+ |
| 277 |
| Name = an | |
| 278 |
| Parent = [1] | |
| 279 |
| Type = [REAL] | |
| 280 |
+-------------------+ |
| 281 |
@end example |
| 282 |
|
| 283 |
Varient records which have nested @code{CASE} statements are |
| 284 |
represented by the following symbol table entries: |
| 285 |
|
| 286 |
@example |
| 287 |
TYPE |
| 288 |
this = RECORD |
| 289 |
CASE tag: CHAR OF |
| 290 |
'a': foo: CARDINAL ; |
| 291 |
CASE bar: BOOLEAN OF |
| 292 |
TRUE : bt: INTEGER | |
| 293 |
FALSE: bf: CARDINAL |
| 294 |
END | |
| 295 |
'b': an: REAL | |
| 296 |
ELSE |
| 297 |
END |
| 298 |
END ; |
| 299 |
|
| 300 |
|
| 301 |
SymRecord [1] |
| 302 |
+-------------+ |
| 303 |
| Name = this | SymRecordField [2] |
| 304 |
| ListOfSons | +-------------------+ |
| 305 |
| +--------| | Name = tag | |
| 306 |
| | [2] [3]| | Parent = [1] | |
| 307 |
| +--------+ | Type = [CHAR] | |
| 308 |
| LocalSymbols| +-------------------+ |
| 309 |
| +-----------+ |
| 310 |
| | tag foo | |
| 311 |
| | bar bt bf | |
| 312 |
| | an | |
| 313 |
| +-----------+ |
| 314 |
+-------------+ |
| 315 |
|
| 316 |
('1st CASE') ('a' selector) |
| 317 |
SymVarient [3] SymFieldVarient [4] |
| 318 |
+-------------------+ +-------------------+ |
| 319 |
| Parent = [1] | | Parent = [1] | |
| 320 |
| ListOfSons | | ListOfSons | |
| 321 |
| +--------------| | +--------------| |
| 322 |
| | [4] [5] | | | [6] [7] [8] | |
| 323 |
+-------------------+ +-------------------+ |
| 324 |
|
| 325 |
('b' selector) |
| 326 |
SymFieldVarient [5] |
| 327 |
+-------------------+ |
| 328 |
| Parent = [1] | |
| 329 |
| ListOfSons | |
| 330 |
| +--------------| |
| 331 |
| | [9] | |
| 332 |
+-------------------+ |
| 333 |
|
| 334 |
SymRecordField [6] SymRecordField [7] |
| 335 |
+-------------------+ +-------------------+ |
| 336 |
| Name = foo | | Name = bar | |
| 337 |
| Parent = [1] | | Parent = [1] | |
| 338 |
| Type = [CARDINAL] | | Type = [BOOLEAN] | |
| 339 |
+-------------------+ +-------------------+ |
| 340 |
|
| 341 |
('2nd CASE') |
| 342 |
SymVarient [8] |
| 343 |
+-------------------+ |
| 344 |
| Parent = [1] | |
| 345 |
| ListOfSons | |
| 346 |
| +--------------| |
| 347 |
| | [12] [13] | |
| 348 |
+-------------------+ |
| 349 |
|
| 350 |
SymRecordField [9] |
| 351 |
+-------------------+ |
| 352 |
| Name = an | |
| 353 |
| Parent = [1] | |
| 354 |
| Type = [REAL] | |
| 355 |
+-------------------+ |
| 356 |
|
| 357 |
SymRecordField [10] SymRecordField [11] |
| 358 |
+-------------------+ +-------------------+ |
| 359 |
| Name = bt | | Name = bf | |
| 360 |
| Parent = [1] | | Parent = [1] | |
| 361 |
| Type = [REAL] | | Type = [REAL] | |
| 362 |
+-------------------+ +-------------------+ |
| 363 |
|
| 364 |
(TRUE selector) (FALSE selector) |
| 365 |
SymFieldVarient [12] SymFieldVarient [13] |
| 366 |
+-------------------+ +-------------------+ |
| 367 |
| Parent = [1] | | Parent = [1] | |
| 368 |
| ListOfSons | | ListOfSons | |
| 369 |
| +--------------| | +--------------| |
| 370 |
| | [10] | | | [11] | |
| 371 |
+-------------------+ +-------------------+ |
| 372 |
@end example |
| 373 |
|
| 374 |
@subsection Pass 3 |
| 375 |
|
| 376 |
To do |
| 377 |
|
| 378 |
@subsection Pass H |
| 379 |
|
| 380 |
To do |
| 381 |
|
| 382 |
@subsection Declaration ordering |
| 383 |
|
| 384 |
This section gives a few stress testing examples and walks though |
| 385 |
the mechanics of the passes and how the lists of symbols are created. |
| 386 |
|
| 387 |
The first example contains a nested module in which an enumeration |
| 388 |
type is created and exported. A procedure declared before the nested |
| 389 |
module uses the enumeration type. |
| 390 |
|
| 391 |
@example |
| 392 |
MODULE colour ; |
| 393 |
|
| 394 |
PROCEDURE make (VAR c: colours) ; |
| 395 |
BEGIN |
| 396 |
c := yellow |
| 397 |
END make ; |
| 398 |
|
| 399 |
MODULE inner ; |
| 400 |
EXPORT colours ; |
| 401 |
|
| 402 |
TYPE |
| 403 |
colours = (red, blue, yellow, white) ; |
| 404 |
END inner ; |
| 405 |
|
| 406 |
VAR |
| 407 |
g: colours |
| 408 |
BEGIN |
| 409 |
make(g) |
| 410 |
END colour. |
| 411 |
@end example |
| 412 |
|
| 413 |
@node Run time, Scope rules, Passes, Internals |
| 414 |
@section Run time |
| 415 |
|
| 416 |
This section describes how the GNU Modula-2 compiler interfaces with |
| 417 |
the run time system. The modules which must be common to all library |
| 418 |
collections are @code{M2RTS} and @code{SYSTEM}. In the PIM library |
| 419 |
collection an implementation of @code{M2RTS} and @code{SYSTEM} exist; |
| 420 |
likewise in the ISO library and ULM library collection these modules |
| 421 |
also exist. |
| 422 |
|
| 423 |
The @code{M2RTS} module contains many of the base runtime features |
| 424 |
required by the GNU Modula-2 compiler. For example @code{M2RTS} |
| 425 |
contains the all the low level exception handling routines. These |
| 426 |
include exception handlers for run time range checks for: assignments, |
| 427 |
increments, decrements, static array access, dynamic array access, for |
| 428 |
loop begin, for loop to, for loop increment, pointer via nil, function |
| 429 |
without return, case value not specified and no exception. The |
| 430 |
@code{M2RTS} module also contains the @code{HALT} and @code{LENGTH} |
| 431 |
procedure. The ISO @code{SYSTEM} module contains a number of |
| 432 |
@code{SHIFT} and @code{ROTATE} procedures which GNU Modula-2 will call |
| 433 |
when wishing to shift and rotate multi-word set types. |
| 434 |
|
| 435 |
@subsection Exception handling |
| 436 |
|
| 437 |
This section describes how exception handling is implemented in GNU |
| 438 |
Modula-2. We begin by including a simple Modula-2 program which uses |
| 439 |
exception handling and provide the same program written in C++. The |
| 440 |
compiler will translate the Modula-2 into the equivalent trees, just |
| 441 |
like the C++ frontend. This ensures that the Modula-2 frontend will |
| 442 |
not do anything that the middle and backend cannot process, which |
| 443 |
ensures that migration through the later gcc releases will be smooth. |
| 444 |
|
| 445 |
Here is an example of Modula-2 using exception handling: |
| 446 |
|
| 447 |
@example |
| 448 |
MODULE except ; |
| 449 |
|
| 450 |
FROM libc IMPORT printf ; |
| 451 |
FROM Storage IMPORT ALLOCATE, DEALLOCATE ; |
| 452 |
|
| 453 |
PROCEDURE fly ; |
| 454 |
BEGIN |
| 455 |
printf("fly main body\n") ; |
| 456 |
IF 4 DIV ip^ = 4 |
| 457 |
THEN |
| 458 |
printf("yes it worked\n") |
| 459 |
ELSE |
| 460 |
printf("no it failed\n") |
| 461 |
END |
| 462 |
END fly ; |
| 463 |
|
| 464 |
PROCEDURE tryFlying ; |
| 465 |
BEGIN |
| 466 |
printf("tryFlying main body\n"); |
| 467 |
fly ; |
| 468 |
EXCEPT |
| 469 |
printf("inside tryFlying exception routine\n") ; |
| 470 |
IF (ip#NIL) AND (ip^=0) |
| 471 |
THEN |
| 472 |
ip^ := 1 ; |
| 473 |
RETRY |
| 474 |
END |
| 475 |
END tryFlying ; |
| 476 |
|
| 477 |
PROCEDURE keepFlying ; |
| 478 |
BEGIN |
| 479 |
printf("keepFlying main body\n") ; |
| 480 |
tryFlying ; |
| 481 |
EXCEPT |
| 482 |
printf("inside keepFlying exception routine\n") ; |
| 483 |
IF ip=NIL |
| 484 |
THEN |
| 485 |
NEW(ip) ; |
| 486 |
ip^ := 0 ; |
| 487 |
RETRY |
| 488 |
END |
| 489 |
END keepFlying ; |
| 490 |
|
| 491 |
VAR |
| 492 |
ip: POINTER TO INTEGER ; |
| 493 |
BEGIN |
| 494 |
ip := NIL ; |
| 495 |
keepFlying ; |
| 496 |
printf("all done\n") |
| 497 |
END except. |
| 498 |
@end example |
| 499 |
|
| 500 |
Now the same program implemented in GNU C++ |
| 501 |
|
| 502 |
@example |
| 503 |
#include <stdio.h> |
| 504 |
#include <stdlib.h> |
| 505 |
|
| 506 |
// a c++ example of Modula-2 exception handling |
| 507 |
|
| 508 |
static int *ip = NULL; |
| 509 |
|
| 510 |
void fly (void) |
| 511 |
@{ |
| 512 |
printf("fly main body\n") ; |
| 513 |
if (ip == NULL) |
| 514 |
throw; |
| 515 |
if (*ip == 0) |
| 516 |
throw; |
| 517 |
if (4 / (*ip) == 4) |
| 518 |
printf("yes it worked\n"); |
| 519 |
else |
| 520 |
printf("no it failed\n"); |
| 521 |
@} |
| 522 |
|
| 523 |
/* |
| 524 |
* a C++ version of the Modula-2 example given in the ISO standard. |
| 525 |
*/ |
| 526 |
|
| 527 |
void tryFlying (void) |
| 528 |
@{ |
| 529 |
again_tryFlying: |
| 530 |
printf("tryFlying main body\n"); |
| 531 |
try @{ |
| 532 |
fly() ; |
| 533 |
@} |
| 534 |
catch (...) @{ |
| 535 |
printf("inside tryFlying exception routine\n") ; |
| 536 |
if ((ip != NULL) && ((*ip) == 0)) @{ |
| 537 |
*ip = 1; |
| 538 |
// retry |
| 539 |
goto again_tryFlying; |
| 540 |
@} |
| 541 |
printf("did't handle exception here so we will call the next exception routine\n") ; |
| 542 |
throw; // unhandled therefore call previous exception handler |
| 543 |
@} |
| 544 |
@} |
| 545 |
|
| 546 |
void keepFlying (void) |
| 547 |
@{ |
| 548 |
again_keepFlying: |
| 549 |
printf("keepFlying main body\n") ; |
| 550 |
try @{ |
| 551 |
tryFlying(); |
| 552 |
@} |
| 553 |
catch (...) @{ |
| 554 |
printf("inside keepFlying exception routine\n"); |
| 555 |
if (ip == NULL) @{ |
| 556 |
ip = (int *)malloc(sizeof(int)); |
| 557 |
*ip = 0; |
| 558 |
goto again_keepFlying; |
| 559 |
@} |
| 560 |
throw; // unhandled therefore call previous exception handler |
| 561 |
@} |
| 562 |
@} |
| 563 |
|
| 564 |
main () |
| 565 |
@{ |
| 566 |
keepFlying(); |
| 567 |
printf("all done\n"); |
| 568 |
@} |
| 569 |
@end example |
| 570 |
|
| 571 |
The equivalent program in GNU C is given below. However the |
| 572 |
use of @code{setjmp} and @code{longjmp} in creating an exception |
| 573 |
handler mechanism is not used used by GNU C++ and GNU Java. |
| 574 |
The GNU exception handling ABI uses @code{TRY_CATCH_EXPR} tree |
| 575 |
nodes. Thus GNU Modula-2 generates trees which model the C++ |
| 576 |
code above, rather than the C code shown below. The code here |
| 577 |
serves as a mental model (for readers who are familiar with C |
| 578 |
but not of C++) of what is happening in the C++ code above. |
| 579 |
|
| 580 |
@example |
| 581 |
#include <setjmp.h> |
| 582 |
#include <malloc.h> |
| 583 |
#include <stdio.h> |
| 584 |
|
| 585 |
typedef enum jmpstatus @{ |
| 586 |
jmp_normal, |
| 587 |
jmp_retry, |
| 588 |
jmp_exception, |
| 589 |
@} jmp_status; |
| 590 |
|
| 591 |
struct setjmp_stack @{ |
| 592 |
jmp_buf env; |
| 593 |
struct setjmp_stack *next; |
| 594 |
@} *head = NULL; |
| 595 |
|
| 596 |
void pushsetjmp (void) |
| 597 |
@{ |
| 598 |
struct setjmp_stack *p = (struct setjmp_stack *) |
| 599 |
malloc (sizeof (struct setjmp_stack)); |
| 600 |
|
| 601 |
p->next = head; |
| 602 |
head = p; |
| 603 |
@} |
| 604 |
|
| 605 |
void exception (void) |
| 606 |
@{ |
| 607 |
printf("invoking exception handler\n"); |
| 608 |
longjmp (head->env, jmp_exception); |
| 609 |
@} |
| 610 |
|
| 611 |
void retry (void) |
| 612 |
@{ |
| 613 |
printf("retry\n"); |
| 614 |
longjmp (head->env, jmp_retry); |
| 615 |
@} |
| 616 |
|
| 617 |
void popsetjmp (void) |
| 618 |
@{ |
| 619 |
struct setjmp_stack *p = head; |
| 620 |
|
| 621 |
head = head->next; |
| 622 |
free (p); |
| 623 |
@} |
| 624 |
|
| 625 |
static int *ip = NULL; |
| 626 |
|
| 627 |
void fly (void) |
| 628 |
@{ |
| 629 |
printf("fly main body\n"); |
| 630 |
if (ip == NULL) @{ |
| 631 |
printf("ip == NULL\n"); |
| 632 |
exception(); |
| 633 |
@} |
| 634 |
if ((*ip) == 0) @{ |
| 635 |
printf("*ip == 0\n"); |
| 636 |
exception(); |
| 637 |
@} |
| 638 |
if ((4 / (*ip)) == 4) |
| 639 |
printf("yes it worked\n"); |
| 640 |
else |
| 641 |
printf("no it failed\n"); |
| 642 |
@} |
| 643 |
|
| 644 |
void tryFlying (void) |
| 645 |
@{ |
| 646 |
void tryFlying_m2_exception () @{ |
| 647 |
printf("inside tryFlying exception routine\n"); |
| 648 |
if ((ip != NULL) && ((*ip) == 0)) @{ |
| 649 |
(*ip) = 1; |
| 650 |
retry(); |
| 651 |
@} |
| 652 |
@} |
| 653 |
|
| 654 |
int t; |
| 655 |
|
| 656 |
pushsetjmp (); |
| 657 |
do @{ |
| 658 |
t = setjmp (head->env); |
| 659 |
@} while (t == jmp_retry); |
| 660 |
|
| 661 |
if (t == jmp_exception) @{ |
| 662 |
/* exception called */ |
| 663 |
tryFlying_m2_exception (); |
| 664 |
/* exception has not been handled, invoke previous handler */ |
| 665 |
printf("exception not handled here\n"); |
| 666 |
popsetjmp(); |
| 667 |
exception(); |
| 668 |
@} |
| 669 |
|
| 670 |
printf("tryFlying main body\n"); |
| 671 |
fly(); |
| 672 |
popsetjmp(); |
| 673 |
@} |
| 674 |
|
| 675 |
void keepFlying (void) |
| 676 |
@{ |
| 677 |
void keepFlying_m2_exception () @{ |
| 678 |
printf("inside keepFlying exception routine\n"); |
| 679 |
if (ip == NULL) @{ |
| 680 |
ip = (int *)malloc (sizeof (int)); |
| 681 |
*ip = 0; |
| 682 |
retry(); |
| 683 |
@} |
| 684 |
@} |
| 685 |
int t; |
| 686 |
|
| 687 |
pushsetjmp (); |
| 688 |
do @{ |
| 689 |
t = setjmp (head->env); |
| 690 |
@} while (t == jmp_retry); |
| 691 |
|
| 692 |
if (t == jmp_exception) @{ |
| 693 |
/* exception called */ |
| 694 |
keepFlying_m2_exception (); |
| 695 |
/* exception has not been handled, invoke previous handler */ |
| 696 |
popsetjmp(); |
| 697 |
exception(); |
| 698 |
@} |
| 699 |
printf("keepFlying main body\n"); |
| 700 |
tryFlying(); |
| 701 |
popsetjmp(); |
| 702 |
@} |
| 703 |
|
| 704 |
main () |
| 705 |
@{ |
| 706 |
keepFlying(); |
| 707 |
printf("all done\n"); |
| 708 |
@} |
| 709 |
@end example |
| 710 |
|
| 711 |
@node Scope rules, Done list, Run time, Internals |
| 712 |
@section Scope rules |
| 713 |
|
| 714 |
This section describes my understanding of the Modula-2 scope rules |
| 715 |
with respect to enumerated types. If they are incorrect please |
| 716 |
correct me by email @email{gaius@@gnu.org}. They also serve to |
| 717 |
document the behaviour of GNU Modula-2 in these cirumstances. |
| 718 |
|
| 719 |
In GNU Modula-2 the syntax for a type declaration is defined as: |
| 720 |
|
| 721 |
@example |
| 722 |
TypeDeclaration := Ident "=" Type =: |
| 723 |
|
| 724 |
Type := SimpleType | ArrayType |
| 725 |
| RecordType |
| 726 |
| SetType |
| 727 |
| PointerType |
| 728 |
| ProcedureType |
| 729 |
=: |
| 730 |
|
| 731 |
SimpleType := Qualident | Enumeration | SubrangeType =: |
| 732 |
|
| 733 |
@end example |
| 734 |
|
| 735 |
If the @code{TypeDeclaration} rule is satisfied by |
| 736 |
@code{SimpleType} and @code{Qualident} ie: |
| 737 |
|
| 738 |
@example |
| 739 |
TYPE |
| 740 |
foo = bar ; |
| 741 |
@end example |
| 742 |
|
| 743 |
then @code{foo} is said to be equivalent to @code{bar}. Thus |
| 744 |
variables, parameters and record fields declared with either type will |
| 745 |
be compatible with each other. |
| 746 |
|
| 747 |
If, however, the @code{TypeDeclaration} rule is satisfied by any |
| 748 |
alternative clause @code{ArrayType}, @code{RecordType}, |
| 749 |
@code{SetType}, @code{PointerType}, @code{ProcedureType}, |
| 750 |
@code{Enumeration} or @code{SubrangeType} then in these cases a new |
| 751 |
type is created which is distinct from all other types. It will be |
| 752 |
incompatible with all other user defined types. |
| 753 |
|
| 754 |
It also has furthur consequences in that if bar was defined as an |
| 755 |
enumerated type and foo is imported by another module then the |
| 756 |
enumerated values are also visible in this module. |
| 757 |
|
| 758 |
Consider the following modules: |
| 759 |
|
| 760 |
@example |
| 761 |
DEFINITION MODULE impc ; |
| 762 |
|
| 763 |
TYPE |
| 764 |
C = (red, blue, green) ; |
| 765 |
|
| 766 |
END impc. |
| 767 |
@end example |
| 768 |
|
| 769 |
@example |
| 770 |
DEFINITION MODULE impb ; |
| 771 |
|
| 772 |
IMPORT impc ; |
| 773 |
|
| 774 |
TYPE |
| 775 |
C = impc.C ; |
| 776 |
|
| 777 |
END impb. |
| 778 |
@end example |
| 779 |
|
| 780 |
@example |
| 781 |
MODULE impa ; |
| 782 |
|
| 783 |
FROM impb IMPORT C ; |
| 784 |
|
| 785 |
VAR |
| 786 |
a: C ; |
| 787 |
BEGIN |
| 788 |
a := red |
| 789 |
END impa. |
| 790 |
@end example |
| 791 |
|
| 792 |
Here we see that the type @code{C} defined in module @code{impb} is |
| 793 |
equivalent to the type @code{C} in module @code{impc}. Module |
| 794 |
@code{impa} imports the type @code{C} from module @code{impb} |
| 795 |
and at that point the enumeration values @code{red, blue, green} |
| 796 |
(declared in module @code{impc}) are also visible. |
| 797 |
|
| 798 |
The ISO Standand (p.41) in section 6.1.8 Import Lists states: |
| 799 |
|
| 800 |
``Following the module heading, a module may have a sequence of import |
| 801 |
lists. An import list includes a list of the identifiers that are to |
| 802 |
be explicitly imported into the module. Explicit import of an |
| 803 |
enumeration type identifier implicitly imports the enumeration |
| 804 |
constant identifiers of the enumeration type. |
| 805 |
|
| 806 |
Imported identifiers are introduced into the module, thus extending |
| 807 |
their scope, but they have a defining occurrence that appears elsewhere. |
| 808 |
|
| 809 |
Every kind of module may include a sequence of import lists, whether it |
| 810 |
is a program module, a definition module, an implementation module or |
| 811 |
a local module. In the case of any other kind of module, the imported |
| 812 |
identifiers may be used in the block of the module.'' |
| 813 |
|
| 814 |
These statements confirm that the previous example is legal. But it |
| 815 |
prompts the question, what about implicit imports othersise known |
| 816 |
as qualified references. |
| 817 |
|
| 818 |
In section 6.10 Implicit Import and Export of the ISO Modula-2 standard |
| 819 |
it says: |
| 820 |
|
| 821 |
``The set of identifiers that is imported or exported if an identifier |
| 822 |
is explicitly imported or exported is called the (import and export) |
| 823 |
closure of that identifier. Normally, the closure includes only the |
| 824 |
explicitly imported or exported identifier. However, in the case |
| 825 |
of the explicit import or export of an identifier of an enumeration |
| 826 |
type, the closure also includes the identifiers of the values of that |
| 827 |
type. |
| 828 |
|
| 829 |
Implicit export applies to the identifiers that are exported (qualified) |
| 830 |
from separate modules, by virtue of their being the subject of a |
| 831 |
definition module, as well as to export from a local module that |
| 832 |
uses an export list.'' |
| 833 |
|
| 834 |
Clearly this means that the following is legal: |
| 835 |
|
| 836 |
@example |
| 837 |
MODULE impd ; |
| 838 |
|
| 839 |
IMPORT impc ; |
| 840 |
|
| 841 |
VAR |
| 842 |
a: impc.C ; |
| 843 |
BEGIN |
| 844 |
a := impc.red |
| 845 |
END impd. |
| 846 |
@end example |
| 847 |
|
| 848 |
It also means that the following code is legal: |
| 849 |
|
| 850 |
@example |
| 851 |
MODULE impe ; |
| 852 |
|
| 853 |
IMPORT impb ; |
| 854 |
|
| 855 |
VAR |
| 856 |
a: impb.C ; |
| 857 |
BEGIN |
| 858 |
a := impb.red |
| 859 |
END impe. |
| 860 |
@end example |
| 861 |
|
| 862 |
And also this code is legal: |
| 863 |
|
| 864 |
@example |
| 865 |
MODULE impf ; |
| 866 |
|
| 867 |
FROM impb IMPORT C ; |
| 868 |
|
| 869 |
VAR |
| 870 |
a: C ; |
| 871 |
BEGIN |
| 872 |
a := red |
| 873 |
END impf. |
| 874 |
@end example |
| 875 |
|
| 876 |
And also that this code is legal: |
| 877 |
|
| 878 |
@example |
| 879 |
DEFINITION MODULE impg ; |
| 880 |
|
| 881 |
IMPORT impc; |
| 882 |
|
| 883 |
TYPE |
| 884 |
C = impc.C ; |
| 885 |
|
| 886 |
END impg. |
| 887 |
@end example |
| 888 |
|
| 889 |
@example |
| 890 |
IMPLEMENTATION MODULE impg ; |
| 891 |
|
| 892 |
VAR |
| 893 |
t: C ; |
| 894 |
BEGIN |
| 895 |
t := red |
| 896 |
END impg. |
| 897 |
@end example |
| 898 |
|
| 899 |
Furthermore the following code is also legal as the new type, @code{C} |
| 900 |
is declared and exported. Once exported all its enumerated fields |
| 901 |
are also exported. |
| 902 |
|
| 903 |
@example |
| 904 |
DEFINITION MODULE imph; |
| 905 |
|
| 906 |
IMPORT impc; |
| 907 |
TYPE |
| 908 |
C = impc.C; |
| 909 |
|
| 910 |
END imph. |
| 911 |
@end example |
| 912 |
|
| 913 |
Here we see that the current scope is populated with the enumeration |
| 914 |
fields @code{red, blue, green} and also it is possible to reference |
| 915 |
these values via a qualified identifier. |
| 916 |
|
| 917 |
@example |
| 918 |
IMPLEMENTATION MODULE imph; |
| 919 |
|
| 920 |
IMPORT impc; |
| 921 |
|
| 922 |
VAR |
| 923 |
a: C ; |
| 924 |
b: impc.C ; |
| 925 |
BEGIN |
| 926 |
a := impc.red ; |
| 927 |
b := red ; |
| 928 |
a := b ; |
| 929 |
b := a |
| 930 |
END imph. |
| 931 |
@end example |
| 932 |
|
| 933 |
|
| 934 |
@node Done list, To do list, Scope rules, Internals |
| 935 |
@section Done list |
| 936 |
|
| 937 |
What has been done: |
| 938 |
|
| 939 |
@itemize @bullet |
| 940 |
|
| 941 |
@item |
| 942 |
Coroutines have been implemented. The @code{SYSTEM} module in |
| 943 |
PIM-[234] now includes @code{TRANSFER}, @code{IOTRANSFER} and |
| 944 |
@code{NEWPROCESS}. This module is available in the directory |
| 945 |
@file{gm2/gm2-libs-coroutines}. Users of this module also have to |
| 946 |
link with GNU Pthreads @code{-lpth}. |
| 947 |
|
| 948 |
@item |
| 949 |
GM2 now works on the @code{opteron} 64 bit architecture. @code{make |
| 950 |
gm2.paranoid} and @code{make check-gm2} pass. |
| 951 |
|
| 952 |
@item |
| 953 |
GM2 can now be built as a cross compiler to the MinGW platform under |
| 954 |
GNU/Linux i386. |
| 955 |
|
| 956 |
@item |
| 957 |
GM2 now works on the @code{sparc} architecture. @code{make |
| 958 |
gm2.paranoid} and @code{make check-gm2} pass. |
| 959 |
|
| 960 |
@item |
| 961 |
converted the regression test suite into the GNU dejagnu format. |
| 962 |
In turn this can be grafted onto the GCC testsuite and can be |
| 963 |
invoked as @code{make check-gm2}. GM2 should now pass all |
| 964 |
regression tests. |
| 965 |
|
| 966 |
@item |
| 967 |
provided access to a few compiler built-in constants |
| 968 |
and twenty seven built-in C functions. |
| 969 |
|
| 970 |
@item |
| 971 |
definition modules no longer have to @code{EXPORT QUALIFIED} |
| 972 |
objects (as per PIM-3, PIM-4 and ISO). |
| 973 |
|
| 974 |
@item |
| 975 |
implemented ISO Modula-2 sets. Large sets are now allowed, |
| 976 |
no limits imposed. The comparison operators |
| 977 |
@code{# = <= >= < >} all behave as per ISO standard. |
| 978 |
The obvious use for large sets is |
| 979 |
@code{SET OF CHAR}. These work well with gdb once it has been |
| 980 |
patched to understand Modula-2 sets. |
| 981 |
|
| 982 |
@item |
| 983 |
added @code{DEFINITION MODULE FOR "C"} method of linking |
| 984 |
to C. Also added varargs handling in C definition modules. |
| 985 |
|
| 986 |
@item |
| 987 |
cpp can be run on definition and implementation modules. |
| 988 |
|
| 989 |
@item |
| 990 |
@samp{-fmakell} generates a temporary @code{Makefile} and |
| 991 |
will build all dependant modules. |
| 992 |
|
| 993 |
@item |
| 994 |
compiler will bootstrap itself and three generations of the |
| 995 |
compiler all produce the same code. |
| 996 |
|
| 997 |
@item |
| 998 |
the back end will generate code and assembly declarations for |
| 999 |
modules containing global variables of all types. Procedure |
| 1000 |
prologue/epilogue is created. |
| 1001 |
|
| 1002 |
@item |
| 1003 |
all loop constructs, if then else, case statements and expressions. |
| 1004 |
|
| 1005 |
@item |
| 1006 |
nested module initialization. |
| 1007 |
|
| 1008 |
@item |
| 1009 |
pointers, arrays, procedure calls, nested procedures. |
| 1010 |
|
| 1011 |
@item |
| 1012 |
front end @samp{gm2} can now compile and link modules. |
| 1013 |
|
| 1014 |
@item |
| 1015 |
the ability to insert gnu asm statements within GNU Modula-2. |
| 1016 |
|
| 1017 |
@item |
| 1018 |
inbuilt functions, @code{SIZE}, @code{ADR}, @code{TSIZE}, @code{HIGH} etc |
| 1019 |
|
| 1020 |
@item |
| 1021 |
block becomes and complex procedure parameters (unbounded arrays, strings). |
| 1022 |
|
| 1023 |
@item |
| 1024 |
the front end now utilizes GCC tree constants and types and is no |
| 1025 |
longer tied to a 32 bit architecture, but reflects the 'configure' |
| 1026 |
target machine description. |
| 1027 |
|
| 1028 |
@item |
| 1029 |
fixed all C compiler warnings when gcc compiles the p2c generated C |
| 1030 |
with -Wall. |
| 1031 |
|
| 1032 |
@item |
| 1033 |
built a new parser which implements error recovery. |
| 1034 |
|
| 1035 |
@item |
| 1036 |
added mechanism to invoke cpp to support conditional compilation if required. |
| 1037 |
|
| 1038 |
@item |
| 1039 |
all @samp{Makefile}s are generated via @samp{./configure} |
| 1040 |
|
| 1041 |
@end itemize |
| 1042 |
|
| 1043 |
@node To do list, , Done list, Internals |
| 1044 |
@section To do list |
| 1045 |
|
| 1046 |
What needs to be done: |
| 1047 |
|
| 1048 |
@itemize @bullet |
| 1049 |
|
| 1050 |
@item |
| 1051 |
ISO library implementation needs to be completed and debugged. |
| 1052 |
|
| 1053 |
@item |
| 1054 |
Easy access to other libraries using @code{-flibs=} so that libraries |
| 1055 |
can be added into the @file{/usr/.../gcc-lib/gm2/...} structure. |
| 1056 |
|
| 1057 |
@item |
| 1058 |
improve documentation, specifically this document which should |
| 1059 |
also include a synopsis of 2nd Edition Modula-2. |
| 1060 |
|
| 1061 |
@item |
| 1062 |
modifying @file{SymbolTable.mod} to make all the data structures dynamic. |
| 1063 |
|
| 1064 |
@item |
| 1065 |
testing and fixing bugs |
| 1066 |
|
| 1067 |
@end itemize |