rs6000: use li;x?oris to build constant
[official-gcc.git] / gcc / doc / gm2.texi
blob513fdd3ec7f1c2f1ab00bd4677a0ccd6401b9619
1 \input texinfo
2 @c -*-texinfo-*-
3 @c Copyright (C) 2001-2022 Free Software Foundation, Inc.
4 @c This is part of the GM2 manual.
6 @c User level documentation for GNU Modula-2
7 @c
8 @c header
10 @setfilename gm2.info
11 @settitle The GNU Modula-2 Compiler
13 @set version-python  3.5
15 @include gcc-common.texi
17 @c Copyright years for this manual.
18 @set copyrights-gm2 1999-2022
20 @copying
21 @c man begin COPYRIGHT
22 Copyright @copyright{} @value{copyrights-gm2} Free Software Foundation, Inc.
24 Permission is granted to copy, distribute and/or modify this document
25 under the terms of the GNU Free Documentation License, Version 1.3 or
26 any later version published by the Free Software Foundation; with no
27 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
28 A copy of the license is included in the
29 @c man end
30 section entitled ``GNU Free Documentation License''.
31 @ignore
32 @c man begin COPYRIGHT
33 man page gfdl(7).
34 @c man end
35 @end ignore
36 @end copying
38 @ifinfo
39 @format
40 @dircategory Software development
41 @direntry
42 * gm2: (gm2).               A GCC-based compiler for the Modula-2 language
43 @end direntry
44 @end format
46 @insertcopying
47 @end ifinfo
49 @titlepage
50 @title The GNU Modula-2 Compiler
51 @versionsubtitle
52 @author Gaius Mulley
54 @page
55 @vskip 0pt plus 1filll
56 Published by the Free Software Foundation @*
57 51 Franklin Street, Fifth Floor@*
58 Boston, MA 02110-1301, USA@*
59 @sp 1
60 @insertcopying
61 @end titlepage
62 @contents
63 @page
65 @c `Top' Node and Master Menu
67 @node Top, Overview, (dir), (dir)
68 @top Introduction
70 @menu
71 * Overview::         What is GNU Modula-2.
72 * Using::            Using GNU Modula-2.
73 * License::          License of GNU Modula-2
74 * Copying::          GNU Public License V3.
75 * Contributing::     Contributing to GNU Modula-2
76 * Internals::        GNU Modula-2 internals.
77 * EBNF::             EBNF of GNU Modula-2
78 * Libraries::        PIM and ISO library definitions.
79 * Indices::          Document and function indices.
80 @end menu
82 @node Overview, Using, Top, Top
83 @chapter Overview of GNU Modula-2
85 @menu
86 * What is GNU Modula-2::  Brief description of GNU Modula-2.
87 * Why use GNU Modula-2::  Advantages of GNU Modula-2.
88 * Development::           How to get source code using git.
89 * Features::              GNU Modula-2 Features
90 @end menu
92 @node What is GNU Modula-2, Why use GNU Modula-2, , Using
93 @section What is GNU Modula-2
95 GNU Modula-2 is a @uref{http://gcc.gnu.org/frontends.html, front end}
96 for the GNU Compiler Collection (@uref{http://gcc.gnu.org/, GCC}).
97 The GNU Modula-2 compiler is compliant with the PIM2, PIM3, PIM4 and
98 ISO dialects.  Also implemented are a complete set of free ISO
99 libraries and PIM libraries.
101 @footnote{The four Modula-2 dialects supported are defined in the following
102 references:
104 PIM2: 'Programming in Modula-2', 2nd Edition, Springer Verlag, 1982,
105 1983 by Niklaus Wirth (PIM2).
107 PIM3: 'Programming in Modula-2', 3rd Corrected Edition, Springer Verlag,
108 1985 (PIM3).
110 PIM4: 'Programming in Modula-2', 4th Edition, Springer Verlag, 1988
111 (@uref{http://freepages.modula2.org/report4/modula-2.html, PIM4}).
113 ISO: the ISO Modula-2 language as defined in 'ISO/IEC Information
114 technology - programming languages - part 1: Modula-2 Language,
115 ISO/IEC 10514-1 (1996)'
118 @node Why use GNU Modula-2, Release map, What is GNU Modula-2, Using
119 @section Why use GNU Modula-2
121 There are a number of advantages of using GNU Modula-2 rather than
122 translate an existing project into another language.
124 The first advantage is of maintainability of the original sources
125 and the ability to debug the original project source code using a
126 combination of gm2 and gdb.
128 The second advantage is that gcc runs on many processors and
129 platforms.  gm2 builds and runs on powerpc64le, amd64, i386, aarch64
130 to name but a few processors.
132 gm2 can produce swig interface headers to allow access from Python and
133 other scripting languages.  It can also be used with C/C++ and
134 generate shared libraries.
136 The compiler provides semantic analysis and run time checking (full ISO
137 Modula-2 checking is implemented) and there is a plugin which can,
138 under certain conditions, detect run time errors at compile time.
140 The compiler supports PIM2, PIM3, PIM4 and ISO dialects of Modula-2,
141 work is underway to implement M2R10.  Many of the GCC builtins are
142 available and access to assembly programming is achieved using the
143 same syntax as that used by GCC.
145 The gm2 driver allows third party libraries to be installed alongside
146 gm2 libraries.  For example if the user specifies library @code{foo}
147 using @code{-flibs=foo} the driver will check the standard GCC install
148 directory for a sub directory @code{foo} containing the library
149 contents.  The library module search path is altered accordingly
150 for compile and link.
152 @node Release map, Development, Why use GNU Modula-2, Using
153 @section Release map
155 GNU Modula-2 is now part of GCC and therefore will adopt the GCC
156 release schedule.  It is intended that GNU Modula-2 implement more of
157 the GCC builtins (vararg access) and GCC features.
159 There is an intention to implement the ISO generics and the M2R10
160 dialect of Modula-2.  It will also implement all language changes.  If
161 you wish to see something different please email
162 @email{gm2@@nongnu.org} with your ideas.
164 @node Development, Features, Release map, Using
165 @section How to get source code using git
167 GNU Modula-2 is now in the @url{https://gcc.gnu.org/git.html, GCC git
168 tree}.
170 @node Features, Documentation, Development, Using
171 @section GNU Modula-2 Features
173 @itemize @bullet
175 @item
176 the compiler currently complies with Programming in Modula-2 Edition
177 2, 3, 4 and ISO Modula-2.  Users can switch on specific language
178 features by using: @samp{-fpim}, @samp{-fpim2}, @samp{-fpim3},
179 @samp{-fpim4} or @samp{-fiso}.
181 @item
182 the option @samp{-fswig} will automatically create a swig interface
183 file which corresponds to the definition module of the file being
184 compiled.
186 @item
187 exception handling is compatible with C++ and swig.  Modula-2 code can
188 be used with C or C++ code.
190 @item
191 Python can call GNU Modula-2 modules via swig.
193 @item
194 shared libraries can be built.
196 @item
197 fixed sized types are now available from @samp{SYSTEM}.
199 @c @item
200 @c support for dynamic @code{ARRAY}s has been added into @samp{gdb}.
202 @item
203 variables can be declared at addresses.
205 @item
206 much better dwarf-2 debugging support and when used with
207 @samp{gdb} the programmer can display @code{RECORD}s,
208 @code{ARRAY}s, @code{SET}s, subranges and constant char literals
209 in Modula-2 syntax.
211 @item
212 supports sets of any ordinal size (memory permitting).
214 @item
215 easy interface to C, and varargs can be passed to C routines.
217 @item
218 many Logitech libraries have been implemented and can be accessed via:
219 @samp{-flibs=m2log,m2pim,m2iso}.
221 @item
222 coroutines have been implemented in the PIM style and these are
223 accessible from SYSTEM.  A number of supporting libraries (executive
224 and file descriptor mapping to interrupt vector libraries are
225 available through the @samp{-flibs=m2iso,m2pim} switch).
227 @item
228 can be built as a cross compiler (for embedded microprocessors
229 such as the AVR and the ARM).
231 @end itemize
233 @node Documentation, Regression tests, Features, Using
234 @section Documentation
236 The GNU Modula-2 documentation is available on line
237 @url{https://www.nongnu.org/gm2/homepage.html,at the gm2 homepage}
238 or in the pdf, info, html file format.
240 @node Regression tests, Limitations, Documentation, Using
241 @section Regression tests for gm2 in the repository
243 The regression testsuite can be run from the gcc build directory:
245 @example
246 $ cd build-gcc
247 $ make check -j 24
248 @end example
250 which runs the complete testsuite for all compilers using 24 parallel
251 invocations of the compiler.  Individual language testsuites can be
252 run by specifying the language, for example the Modula-2 testsuite can
253 be run using:
255 @example
256 $ cd build-gcc
257 $ make check-m2 -j 24
258 @end example
260 Finally the results of the testsuite can be emailed to the
261 @url{https://gcc.gnu.org/lists.html, gcc-testresults} list using the
262 @file{test_summary} script found in the gcc source tree:
264 @example
265 $ @samp{directory to the sources}/contrib/test_summary
266 @end example
268 @node Limitations, Objectives, Regression tests, Using
269 @section Limitations
271 Logitech compatibility library is incomplete.  The principle modules
272 for this platform exist however for a comprehensive list of completed
273 modules please check the documentation
274 @url{gm2.html}.
276 @node Objectives, FAQ, , Using
277 @section Objectives
279 @itemize @bullet
281 @item
282 The intention of GNU Modula-2 is to provide a production Modula-2
283 front end to GCC.
285 @item
286 It should support all Niklaus Wirth PIM Dialects [234] and also ISO
287 Modula-2 including a re-implementation of all the ISO modules.
289 @item
290 There should be an easy interface to C.
292 @item
293 Exploit the features of GCC.
295 @item
296 Listen to the requests of the users.
297 @end itemize
299 @node FAQ, Community, Objectives, Using
300 @section FAQ
302 @subsection Why use the C++ exception mechanism in GCC, rather than a bespoke Modula-2 mechanism?
304 The C++ mechanism is tried and tested, it also provides GNU Modula-2
305 with the ability to link with C++ modules and via swig it can raise
306 Python exceptions.
308 @node Community, Other languages, FAQ, Using
309 @section Community
311 You can subscribe to the GNU Modula-2 mailing by sending an
312 email to:
313 @email{gm2-subscribe@@nongnu.org}
314 or by
315 @url{http://lists.nongnu.org/mailman/listinfo/gm2}.
316 The mailing list contents can be viewed
317 @url{http://lists.gnu.org/archive/html/gm2}.
319 @node Other languages, , Community, Using
320 @section Other languages for GCC
322 These exist and can be found on the frontends web page on the
323 @uref{http://gcc.gnu.org/frontends.html, gcc web site}.
325 @node Using, , Community, Top
326 @chapter Using GNU Modula-2
328 @menu
329 * Example usage::         Example compile and link.
330 * Compiler options::      GNU Modula-2 compiler options.
331 * Linking::               Linking options in more detail.
332 * Elementary data types:: Data types supported by GNU Modula-2.
333 * Standard procedures::   Permanently accessible base procedures.
334 * Dialect::               GNU Modula-2 supported dialects.
335 * Exceptions::            Exception implementation
336 * Semantic checking::     How to detect run time problems at compile time.
337 * Extensions::            GNU Modula-2 language extensions.
338 * Type compatibility::    Data type compatibility.
339 * Unbounded by reference::Explanation of a language optimization.
340 * Building a shared library:: How to build a shared library.
341 * Interface for Python::  How to produce swig interface files.
342 * Producing a Python module::  How to produce a Python module.
343 * Interface to C::        Interfacing GNU Modula-2 to C.
344 * Assembly language::     Interface to assembly language.
345 * Alignment::             Data type alignment.
346 * Packed::                Packing data types.
347 * Built-ins::             Accessing GNU Modula-2 Built-ins.
348 * The PIM system module:: SYSTEM data types and procedures.
349 * The ISO system module:: SYSTEM data types, procedures and run time.
350 * Other languages::       Other languages for GCC.
351 * What is GNU Modula-2::  Brief description of GNU Modula-2.
352 * Why use GNU Modula-2::  Advantages of GNU Modula-2.
353 @ifnothtml
354 @c omit these nodes if generating gm2 webpage as these are hand written.
355 * Release map:: Release map.
356 * Development:: Development.
357 * Features:: Features of the implementation.
358 * Documentation:: Placeholder for how to access the documentation online.
359 * Regression tests:: How to run the testsuite.
360 * Limitations:: Current limitations.
361 * Objectives:: Objectives of the implementation.
362 * FAQ:: Frequently asked questions.
363 * Community:: How to join the community.
364 @end ifnothtml
365 @end menu
367 This document contains the user and design issues relevant to the
368 Modula-2 front end to gcc.
370 @node Example usage, Compiler options, Using, Using
371 @section Example compile and link
373 @ignore
374 @c man begin SYNOPSIS gm2
375 gm2 [@option{-c}|@option{-S}] [@option{-g}] [@option{-pg}]
376     [@option{-O}@var{level}] [@option{-W}@var{warn}@dots{}]
377     [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
378     [@option{-f}@var{option}@dots{}] [@option{-m}@var{machine-option}@dots{}]
379     [@option{-o} @var{outfile}] [@@@var{file}] @var{infile}@dots{}
381 Only the most useful options are listed here; see below for the
382 remainder.
383 @c man end
384 @c man begin SEEALSO
385 gpl(7), gfdl(7), fsf-funding(7), gcc(1)
386 and the Info entries for @file{gm2} and @file{gcc}.
387 @c man end
388 @end ignore
390 @c man begin DESCRIPTION gm2
392 The @command{gm2} command is the GNU compiler for the Modula-2 language and
393 supports many of the same options as @command{gcc}.  @xref{Option Summary, ,
394 Option Summary, gcc, Using the GNU Compiler Collection (GCC)}.
395 This manual only documents the options specific to @command{gm2}.
397 @c man end
399 This section describes how to compile and link a simple hello world
400 program.  It provides a few examples of using the different options
401 mentioned in @pxref{Compiler options, , ,gm2}.  Assuming that you have
402 a file called @file{hello.mod} in your current directory which
403 contains:
405 @example
406 MODULE hello ;
408 FROM StrIO IMPORT WriteString, WriteLn ;
410 BEGIN
411    WriteString ('hello world') ; WriteLn
412 END hello.
413 @end example
415 You can compile and link it by: @samp{gm2 -g hello.mod}.
416 The result will be an @samp{a.out} file created in your directory.
418 You can split this command into two steps if you prefer.  The compile
419 step can be achieved by: @samp{gm2 -g -c -fscaffold-main hello.mod}
420 and the link via: @samp{gm2 -g hello.o}.
422 @footnote{To see all the compile actions taken by @samp{gm2} users can also
423 add the @samp{-v} flag at the command line, for example:
425 @samp{gm2 -v -g -I. hello.mod}
427 This displays the sub processes initiated by @samp{gm2} which can be useful
428 when trouble shooting.}
430 @node Compiler options, Elementary data types, Example usage, Using
431 @section Compiler options
433 This section describes the compiler options specific to GNU Modula-2
434 for generic flags details @xref{Invoking GCC, , ,gcc}.
436 @c man begin OPTIONS
438 For any given input file, the file name suffix determines what kind of
439 compilation is done.  The following kinds of input file names are supported:
441 @table @gcctabopt
442 @item @var{file}.mod
443 Modula-2 implementation or program source files.  See the
444 @samp{-fmod=} option if you wish to compile a project which uses a
445 different source file extension.
446 @item @var{file}.def
447 Modula-2 definition module source files.  Definition modules are not
448 compiled separately, in GNU Modula-2 definition modules are parsed as
449 required when program or implementation modules are compiled.  See the
450 @samp{-fdef=} option if you wish to compile a project which uses a
451 different source file extension.
452 @end table
454 You can specify more than one input file on the @command{gm2} command line,
456 @table @code
458 @item -g
459 create debugging information so that debuggers such as @file{gdb}
460 can inspect and control executable.
462 @item -I
463 used to specify the search path for definition and implementation
464 modules.  An example is:  @code{gm2 -g -c -I.:../../libs foo.mod}.
465 If this option is not specified then the default path is added
466 which consists of the current directory followed by the appropriate
467 language dialect library directories.
469 @c ordered list of options from here.
471 @item -fauto-init
472 turns on auto initialization of pointers to NIL.  Whenever a block is
473 created all pointers declared within this scope will have their
474 addresses assigned to NIL.
476 @item -fbounds
477 turns on run time subrange, array index and indirection via @code{NIL}
478 pointer checking.
480 @item -fcase
481 turns on compile time checking to check whether a @code{CASE}
482 statement requires an @code{ELSE} clause when on was not specified.
484 @item -fcpp
485 preprocess the source with @samp{cpp -lang-asm -traditional-cpp}
486 For further details about these options @xref{Invocation, , ,cpp}.
487 If @samp{-fcpp} is supplied then all definition modules and
488 implementation modules which are parsed will be prepossessed by
489 @samp{cpp}.
491 @c fcpp-end
492 @c Modula-2
493 @c passed to the preprocessor if -fcpp is used (internal switch)
495 @c fcpp-begin
496 @c Modula-2
497 @c passed to the preprocessor if -fcpp is used (internal switch)
499 @item -fdebug-builtins
500 call a real function, rather than the builtin equivalent.  This can
501 be useful for debugging parameter values to a builtin function as
502 it allows users to single step code into a real function.
504 @c fd
505 @c Modula-2
506 @c turn on internal debugging of the compiler (internal switch)
508 @c fdebug-trace-quad
509 @c Modula-2
510 @c turn on quadruple tracing (internal switch)
512 @c fdebug-trace-api
513 @c Modula-2
514 @c turn on the Modula-2 api tracing (internal switch)
516 @c fdebug-function-line-numbers
517 @c Modula-2
518 @c turn on the Modula-2 function line number generation (internal switch)
520 @item -fdef=
521 recognize the specified suffix as a definition module filename.
522 The default implementation and module filename suffix is @file{.def}.
523 If this option is used GNU Modula-2 will still fall back to this
524 default if a requested definition module is not found.
526 @item -fdump-system-exports
527 display all inbuilt system items.
528 This is an internal command line option.
530 @item -fexceptions
531 turn on exception handling code.  By default this option is on.
532 Exception handling can be disabled by @samp{-fno-exceptions}
533 and no references are made to the run time exception libraries.
535 @item -fextended-opaque
536 allows opaque types to be implemented as any type.  This is a GNU
537 Modula-2 extension and it requires that the implementation module
538 defining the opaque type is available so that it can be resolved when
539 compiling the module which imports the opaque type.
541 @item -ffloatvalue
542 turns on run time checking to check whether a floating point number is
543 about to exceed range.
545 @item -fgen-module-list=@file{filename}
546 attempt to find all modules when linking and generate a module list.
547 If the @file{filename} is @samp{-} then the contents are not written
548 and only used to force the linking of all module ctors.
549 This option cannot be used if @samp{-fuse-list=} is enabled.
551 @item -findex
552 generate code to check whether array index values are out of bounds.
553 Array index checking can be disabled via @samp{-fno-index}.
555 @item -fiso
556 turn on ISO standard features.  Currently this enables the ISO
557 @code{SYSTEM} module and alters the default library search path so
558 that the ISO libraries are searched before the PIM libraries.  It also
559 effects the behavior of @code{DIV} and @code{MOD} operators.
560 @xref{Dialect, , ,gm2}.
562 @item -flibs=
563 modifies the default library search path.  The libraries supplied are:
564 m2pim, m2iso, m2min, m2log and m2cor.  These map onto the
565 Programming in Modula-2 base libraries, ISO standard libraries, minimal
566 library support, Logitech compatible library and Programming in
567 Modula-2 with coroutines.
568 Multiple libraries can be specified and are comma separated with precedence
569 going to the first in the list.  It is not necessary to use -flibs=m2pim or
570 -flibs=m2iso if you also specify -fpim, -fpim2, -fpim3, -fpim4 or
571 -fiso.  Unless you are using -flibs=m2min you should include m2pim as
572 the they provide the base modules which all other dialects utilize.
573 The option @samp{-fno-libs=-} disables the @samp{gm2} driver from
574 modifying the search and library paths.
576 @c flocation=
577 @c Modula-2 Joined
578 @c set all location values to a specific value (internal switch)
580 @item -fm2-g
581 improve the debugging experience for new programmers at the expense
582 of generating @code{nop} instructions if necessary to ensure single
583 stepping precision over all code related keywords.  An example
584 of this is in termination of a list of nested @code{IF} statements
585 where multiple @code{END} keywords are mapped onto a sequence of
586 @code{nop} instructions.
588 @item -fm2-lower-case
589 render keywords in error messages using lower case.
591 @item -fm2-plugin
592 insert plugin to identify run time errors at compile time (default on).
594 @item -fm2-statistics
595 generates quadruple information: number of quadruples generated,
596 number of quadruples remaining after optimization and number of source
597 lines compiled.
599 @item -fm2-strict-type
600 experimental flag to turn on the new strict type checker.
602 @item -fm2-whole-program
603 compile all implementation modules and program module at once.  Notice
604 that you need to take care if you are compiling different dialect
605 modules (particularly with the negative operands to modulus).  But
606 this option, when coupled together with @code{-O3}, can deliver huge
607 performance improvements.
609 @item -fmod=
610 recognize the specified suffix as implementation and module filenames.
611 The default implementation and module filename suffix is @file{.mod}.
612 If this option is used GNU Modula-2 will still fall back to this
613 default if it needs to read an implementation module and the specified
614 suffixed filename does not exist.
616 @item -fnil
617 generate code to detect accessing data through a @code{NIL} value
618 pointer.  Dereferencing checking through a @code{NIL} pointer can be
619 disabled by @samp{-fno-nil}.
621 @item -fpim
622 turn on PIM standard features.  Currently this enables the PIM
623 @code{SYSTEM} module and determines which identifiers are pervasive
624 (declared in the base module).  If no other @samp{-fpim[234]} switch is
625 used then division and modulus operators behave as defined in PIM4.
626 @xref{Dialect, , ,gm2}.
628 @item -fpim2
629 turn on PIM-2 standard features.  Currently this removes @code{SIZE}
630 from being a pervasive identifier (declared in the base module).  It
631 places @code{SIZE} in the @code{SYSTEM} module.  It also effects the
632 behavior of @code{DIV} and @code{MOD} operators.
633 @xref{Dialect, , ,gm2}.
635 @item -fpim3
636 turn on PIM-3 standard features.  Currently this only effects the
637 behavior of @code{DIV} and @code{MOD} operators.
638 @xref{Dialect, , ,gm2}.
640 @item -fpim4
641 turn on PIM-4 standard features.  Currently this only effects the
642 behavior of @code{DIV} and @code{MOD} operators.
643 @xref{Dialect, , ,gm2}.
645 @item -fpositive-mod-floor-div
646 forces the @code{DIV} and @code{MOD} operators to behave as defined by PIM4.
647 All modulus results are positive and the results from the division are
648 rounded to the floor.
649 @xref{Dialect, , ,gm2}.
651 @item -fpthread
652 link against the pthread library.  By default this option is on.  It
653 can be disabled by @samp{-fno-pthread}.  GNU Modula-2 uses the GCC
654 pthread libraries to implement coroutines (see the SYSTEM
655 implementation module).
657 @c -fq
658 @c -Modula-2
659 @c -internal compiler debugging information, dump the list of quadruples
661 @item -frange
662 generate code to check the assignment range, return value range
663 set range and constructor range.  Range checking can be disabled
664 via @samp{-fno-range}.
666 @item -freturn
667 generate code to check that functions always exit with a @code{RETURN}
668 and do not fall out at the end.  Return checking can be disabled
669 via @samp{-fno-return}.
671 @item -fruntime-modules=
672 specify, using a comma separated list, the run time modules and their
673 order.  These modules will initialized first before any other modules
674 in the application dependency.  By default the run time modules list is
675 set to @code{Storage,SYSTEM,M2RTS,RTExceptions,IOLink}.  Note that
676 these modules will only be linked into your executable if they are
677 required.  So adding a long list of dependent modules will not effect
678 the size of the executable it merely states the initialization order
679 should they be required.
681 @item -fscaffold-dynamic
682 the option ensures that @samp{gm2} will generate a dynamic scaffold
683 infrastructure when compiling implementation and program modules.
684 By default this option is on.  Use @samp{-fno-scaffold-dynamic}
685 to turn it off or select @samp{-fno-scaffold-static}.
687 @item -fscaffold-c
688 generate a C source scaffold for the current module being compiled.
690 @item -fscaffold-c++
691 generate a C++ source scaffold for the current module being compiled.
693 @item -fscaffold-main
694 force the generation of the @samp{main} function.  This is not
695 necessary if the @samp{-c} is omitted.
697 @item -fscaffold-static
698 the option ensures that @samp{gm2} will generate a static scaffold
699 within the program module.  The static scaffold consists of sequences
700 of calls to all dependent module initialization and finalization
701 procedures.  The static scaffold is useful for debugging and single
702 stepping the initialization blocks of implementation modules.
704 @item -fshared
705 generate a shared library from the module.
707 @item -fsoft-check-all
708 turns on all run time checks.  This is the same as invoking
709 GNU Modula-2 using the command options
710 @code{-fnil} @code{-frange} @code{-findex}
711 @code{-fwholevalue}
712 @code{-fwholediv} @code{-fcase} @code{-freturn}.
714 @item -fsources
715 displays the path to the source of each module.  This option
716 can be used at compile time to check the correct definition module
717 is being used.
719 @item -fswig
720 generate a swig interface file.
722 @item -funbounded-by-reference
723 enable optimization of unbounded parameters by attempting to pass non
724 @code{VAR} unbounded parameters by reference.  This optimization
725 avoids the implicit copy inside the callee procedure.  GNU Modula-2
726 will only allow unbounded parameters to be passed by reference if,
727 inside the callee procedure, they are not written to, no address is
728 calculated on the array and it is not passed as a @code{VAR}
729 parameter.  Note that it is possible to write code to break this
730 optimization, therefore this option should be used carefully.
731 For example it would be possible to take the address of an array, pass
732 the address and the array to a procedure, read from the array in
733 the procedure and write to the location using the address parameter.
735 Due to the dangerous nature of this option it is not enabled
736 when the @samp{-O} option is specified.
738 @item -fuse-list=@file{filename}
739 if @samp{-fscaffold-static} is enabled then use the file
740 @file{filename} for the initialization order of modules.  Whereas if
741 @samp{-fscaffold-dynamic} is enabled then use this file to force
742 linking of all module ctors.
743 This option cannot be used if @samp{-fgen-module-list=} is enabled.
745 @item -fwholediv
746 generate code to detect whole number division by zero or modulus by
747 zero.
749 @item -fwholevalue
750 generate code to detect whole number overflow and underflow.
752 @c the following warning options are complete but need to be
753 @c regression tested against all other front ends
754 @c to ensure the options do not conflict.
756 @c @item -Wall
757 @c turn on all Modula-2 warnings.
759 @c @item -Wpedantic
760 @c forces the compiler to reject nested @code{WITH} statements
761 @c referencing the same record type.  Does not allow multiple imports of
762 @c the same item from a module.  It also checks that: procedure variables
763 @c are written to before being read; variables are not only written to
764 @c but read from; variables are declared and used.  If the compiler
765 @c encounters a variable being read before written it will terminate with
766 @c a message.  It will check that @code{FOR} loop indices are not used
767 @c outside the end of this loop without being reset.
769 @c @item -Wpedantic-cast
770 @c warns if the ISO system function is used and if the size of
771 @c the variable is different from that of the type.  This is legal
772 @c in ISO Modula-2, however it can be dangerous.  Some users may prefer
773 @c to use @code{VAL} instead in these situations and use @code{CAST}
774 @c exclusively for changes in type on objects which have the same size.
776 @c @item -Wpedantic-param-names
777 @c procedure parameter names are checked in the definition module
778 @c against their implementation module counterpart.  This is not
779 @c necessary in ISO or PIM versions of Modula-2.
781 @c @item -Wstyle
782 @c checks for poor programming style.  This option is aimed at new users of
783 @c Modula-2 in that it checks for situations which might cause confusion
784 @c and thus mistakes.  It checks whether variables of the same name are
785 @c declared in different scopes and whether variables look like keywords.
786 @c Experienced users might find this option too aggressive.
788 @c @item -Wunused-variable
789 @c warns if a variable has been declared and it not used.
791 @c @item -Wunused-parameter
792 @c warns if a parameter has been declared and it not used.
794 @c @item -Wverbose-unbounded
795 @c inform the user which non @code{VAR} unbounded parameters will be
796 @c passed by reference.  This only produces output if the option
797 @c @samp{-funbounded-by-reference} is also supplied on the command line.
799 @end table
801 @c man end
803 @node Elementary data types, Standard procedures, Compiler options, Using
804 @section Elementary data types
806 This section describes the elementary data types supported by GNU
807 Modula-2.  It also describes the relationship between these data types
808 and the equivalent C data types.
810 The following data types are supported: @code{INTEGER},
811 @code{LONGINT}, @code{SHORTINT}, @code{CARDINAL}, @code{LONGCARD},
812 @code{SHORTCARD}, @code{BOOLEAN}, @code{REAL}, @code{LONGREAL},
813 @code{SHORTREAL}, @code{COMPLEX}, @code{LONGCOMPLEX},
814 @code{SHORTCOMPLEX} and @code{CHAR}.
816 An equivalence table is given below:
818 @example
819 GNU Modula-2              GNU C
820 ======================================
821 INTEGER                   int
822 LONGINT                   long long int
823 SHORTINT                  short int
824 CARDINAL                  unsigned int
825 LONGCARD                  long long unsigned int
826 SHORTCARD                 short unsigned int
827 BOOLEAN                   int
828 REAL                      double
829 LONGREAL                  long double
830 SHORTREAL                 float
831 CHAR                      char
832 SHORTCOMPLEX              complex float
833 COMPLEX                   complex double
834 LONGCOMPLEX               complex long double
835 @end example
837 Note that GNU Modula-2 also supports fixed sized data types which are
838 exported from the @code{SYSTEM} module.
839 @xref{The PIM system module, , ,gm2}.
840 @xref{The ISO system module, , ,gm2}.
842 @node Standard procedures, Dialect, Elementary data types, Using
843 @section Permanently accessible base procedures.
845 This section describes the procedures and functions which are
846 always visible.
848 @subsection Standard procedures and functions common to PIM and ISO
850 The following procedures are implemented and conform with Programming
851 in Modula-2 and ISO Modula-2: @code{NEW}, @code{DISPOSE}, @code{INC},
852 @code{DEC}, @code{INCL}, @code{EXCL} and @code{HALT}.  The standard
853 functions are: @code{ABS}, @code{CAP}, @code{CHR}, @code{FLOAT},
854 @code{HIGH}, @code{LFLOAT}, @code{LTRUNC}, @code{MIN}, @code{MAX},
855 @code{ODD}, @code{SFLOAT}, @code{STRUNC} @code{TRUNC} and
856 @code{VAL}.  All these functions and procedures (except @code{HALT},
857 @code{NEW}, @code{DISPOSE} and, under non constant conditions,
858 @code{LENGTH}) generate in-line code for efficiency.
860 @example
863    ABS - returns the positive value of i.
866 @findex ABS
867 PROCEDURE ABS (i: <any signed type>) : <any signed type> ;
869 @end example
871 @example
874    CAP - returns the capital of character ch providing
875          ch lies within the range 'a'..'z'.  Otherwise ch
876          is returned unaltered.
879 @findex CAP
880 PROCEDURE CAP (ch: CHAR) : CHAR ;
882 @end example
884 @example
887    CHR - converts a value of a <whole number type> into a CHAR.
888          CHR(x) is shorthand for VAL(CHAR, x).
891 @findex CHR
892 PROCEDURE CHR (x: <whole number type>) : CHAR ;
894 @end example
896 @example
899    DISPOSE - the procedure DISPOSE is replaced by:
900              DEALLOCATE(p, TSIZE(p^)) ;
901              The user is expected to import the procedure DEALLOCATE
902              (normally found in the module, Storage.)
904              In:  a variable p: of any pointer type which has been
905                   initialized by a call to NEW.
906              Out: the area of memory
907                   holding p^ is returned to the system.
908                   Note that the underlying procedure DEALLOCATE
909                   procedure in module Storage will assign p to NIL.
912 @findex DISPOSE
913 PROCEDURE DISPOSE (VAR p:<any pointer type>) ;
914 @end example
916 @example
919    DEC - can either take one or two parameters.  If supplied
920          with one parameter then on the completion of the call to
921          DEC, v will have its predecessor value.  If two
922          parameters are supplied then the value v will have its
923          n'th predecessor.  For these reasons the value of n
924          must be >=0.
927 @findex DEC
928 PROCEDURE DEC (VAR v: <any base type>; [n: <any base type> = 1]) ;
929 @end example
931 @example
934    EXCL - excludes bit element e from a set type s.
937 @findex EXCL
938 PROCEDURE EXCL (VAR s: <any set type>; e: <element of set type s>) ;
939 @end example
941 @example
944    FLOAT - will return a REAL number whose value is the same as o.
947 @findex FLOAT
948 PROCEDURE FLOAT (o: <any whole number type>) : REAL ;
949 @end example
951 @example
954    FLOATS - will return a SHORTREAL number whose value is the same as o.
957 @findex FLOATS
958 PROCEDURE FLOATS (o: <any whole number type>) : REAL ;
959 @end example
961 @example
964    FLOATL - will return a LONGREAL number whose value is the same as o.
967 @findex FLOATL
968 PROCEDURE FLOATL (o: <any whole number type>) : REAL ;
969 @end example
971 @example
974    HALT - will call the HALT procedure inside the module M2RTS.
975           Users can replace M2RTS.
978 @findex HALT
979 PROCEDURE HALT ;
980 @end example
982 @example
985    HIGH - returns the last accessible index of an parameter declared as
986           ARRAY OF CHAR.  Thus
988           PROCEDURE foo (a: ARRAY OF CHAR) ;
989           VAR
990              c: CARDINAL ;
991           BEGIN
992              c := HIGH(a)
993           END foo ;
995           BEGIN
996              foo('hello')
997           END
999           will cause the local variable c to contain the value 4
1002 @findex HIGH
1003 PROCEDURE HIGH (a: ARRAY OF CHAR) : CARDINAL ;
1004 @end example
1006 @example
1009    INC - can either take one or two parameters.  If supplied
1010          with one parameter then on the completion of the call to
1011          INC, v will have its successor value.  If two
1012          parameters are supplied then the value v will have its
1013          n'th successor.  For these reasons the value of n
1014          must be >=0.
1017 @findex INC
1018 PROCEDURE INC (VAR v: <any base type>; [n: <any base type> = 1]) ;
1019 @end example
1021 @example
1024    INCL - includes bit element e to a set type s.
1027 @findex INCL
1028 PROCEDURE INCL (VAR s: <any set type>; e: <element of set type s>) ;
1029 @end example
1031 @example
1034    LFLOAT - will return a LONGREAL number whose value is the same as o.
1037 @findex LFLOAT
1038 PROCEDURE LFLOAT (o: <any whole number type>) : LONGREAL ;
1039 @end example
1041 @example
1044    LTRUNC - will return a LONG<type> number whose value is the
1045             same as o.  PIM2, PIM3 and ISO Modula-2 will return
1046             a LONGCARD whereas PIM4 returns LONGINT.
1049 @findex LTRUNC
1050 PROCEDURE LTRUNC (o: <any floating point type>) : LONG<type> ;
1051 @end example
1053 @example
1056    MIN - returns the lowest legal value of an ordinal type.
1059 @findex MIN
1060 PROCEDURE MIN (t: <ordinal type>) : <ordinal type> ;
1062 @end example
1064 @example
1067    MAX - returns the largest legal value of an ordinal type.
1070 @findex MAX
1071 PROCEDURE MAX (t: <ordinal type>) : <ordinal type> ;
1073 @end example
1075 @example
1078    NEW - the procedure NEW is replaced by:
1079          ALLOCATE(p, TSIZE(p^)) ;
1080          The user is expected to import the procedure ALLOCATE
1081          (normally found in the module, Storage.)
1083          In:  a variable p: of any pointer type.
1084          Out: variable p is set to some allocated memory
1085               which is large enough to hold all the contents of p^.
1088 @findex NEW
1089 PROCEDURE NEW (VAR p:<any pointer type>) ;
1090 @end example
1092 @example
1095    ODD - returns TRUE if the value is not divisible by 2.
1098 @findex ODD
1099 PROCEDURE ODD (x: <whole number type>) : BOOLEAN ;
1101 @end example
1103 @example
1106    SFLOAT - will return a SHORTREAL number whose value is the same
1107             as o.
1110 @findex SFLOAT
1111 PROCEDURE SFLOAT (o: <any whole number type>) : SHORTREAL ;
1112 @end example
1114 @example
1117    STRUNC - will return a SHORT<type> number whose value is the same
1118             as o.  PIM2, PIM3 and ISO Modula-2 will return a
1119             SHORTCARD whereas PIM4 returns SHORTINT.
1122 @findex STRUNC
1123 PROCEDURE STRUNC (o: <any floating point type>) : SHORT<type> ;
1124 @end example
1126 @example
1129    TRUNC - will return a <type> number whose value is the same as o.
1130            PIM2, PIM3 and ISO Modula-2 will return a CARDINAL
1131            whereas PIM4 returns INTEGER.
1134 @findex TRUNC
1135 PROCEDURE TRUNC (o: <any floating point type>) : <type> ;
1136 @end example
1138 @example
1141    TRUNCS - will return a <type> number whose value is the same
1142             as o.  PIM2, PIM3 and ISO Modula-2 will return a
1143             SHORTCARD whereas PIM4 returns SHORTINT.
1146 @findex TRUNCS
1147 PROCEDURE TRUNCS (o: <any floating point type>) : <type> ;
1148 @end example
1150 @example
1153    TRUNCL - will return a <type> number whose value is the same
1154             as o.  PIM2, PIM3 and ISO Modula-2 will return a
1155             LONGCARD whereas PIM4 returns LONGINT.
1158 @findex TRUNCL
1159 PROCEDURE TRUNCL (o: <any floating point type>) : <type> ;
1160 @end example
1162 @example
1165    VAL - converts data i of <any simple data type 2> to
1166          <any simple data type 1> and returns this value.
1167          No range checking is performed during this conversion.
1170 @findex VAL
1171 PROCEDURE VAL (<any simple data type 1>,
1172                i: <any simple data type 2>) : <any simple data type 1> ;
1174 @end example
1176 @subsection ISO specific standard procedures and functions
1178 The standard function @code{LENGTH} is specific to ISO Modula-2 and
1179 is defined as:
1181 @example
1184    IM - returns the imaginary component of a complex type.
1185         The return value will the same type as the imaginary field
1186         within the complex type.
1189 @findex IM
1190 PROCEDURE IM (c: <any complex type>) : <floating point type> ;
1191 @end example
1193 @example
1196    INT - returns an INTEGER value which has the same value as v.
1197          This function is equivalent to: VAL(INTEGER, v).
1200 @findex INT
1201 PROCEDURE INT (v: <any ordinal type>) : INTEGER ;
1202 @end example
1204 @example
1207    LENGTH - returns the length of string a.
1210 @findex LENGTH
1211 PROCEDURE LENGTH (a: ARRAY OF CHAR) : CARDINAL ;
1212 @end example
1214 This function is evaluated at compile time, providing that string
1215 @code{a} is a constant.  If @code{a} cannot be evaluated then a call is
1216 made to @code{M2RTS.Length}.
1218 @example
1221    ODD - returns a BOOLEAN indicating whether the whole number
1222          value, v, is odd.
1225 @findex ODD
1226 PROCEDURE ODD (v: <any whole number type>) : BOOLEAN ;
1227 @end example
1229 @example
1232    RE - returns the real component of a complex type.
1233         The return value will the same type as the real field
1234         within the complex type.
1237 @findex RE
1238 PROCEDURE RE (c: <any complex type>) : <floating point type> ;
1239 @end example
1241 @node Dialect, Exceptions, Standard procedures, Using
1242 @section GNU Modula-2 supported dialects
1244 This section describes the dialects understood by GNU Modula-2.
1245 It also describes the differences between the dialects and
1246 any command line switches which determine dialect behaviour.
1248 The GNU Modula-2 compiler is compliant with four dialects of Modula-2.
1249 The language as defined in 'Programming in Modula-2' 2nd Edition,
1250 Springer Verlag, 1982, 1983 by Niklaus Wirth (PIM2), 'Programming in
1251 Modula-2', 3rd Corrected Edition, Springer Verlag, 1985 (PIM3) and
1252 'Programming in Modula-2', 4th Edition, Springer Verlag, 1988 (PIM4)
1253 @uref{http://freepages.modula2.org/report4/modula-2.html} and the ISO
1254 Modula-2 language as defined in ISO/IEC Information technology -
1255 programming languages - part 1: Modula-2 Language, ISO/IEC 10514-1
1256 (1996) (ISO).
1258 The command line switches @samp{-fpim2}, @samp{-fpim3}, @samp{-fpim4}
1259 and @samp{-fiso} can be used to force mutually exclusive
1260 features.  However by default the compiler will not aggressively fail
1261 if a non mutually exclusive feature is used from another dialect.  For
1262 example it is possible to specify @samp{-fpim2} and still utilize
1263 @samp{DEFINITION} @samp{MODULES} which have no export list.
1265 Some dialect differences will force a compile time error, for example
1266 in PIM2 the user must @code{IMPORT} @code{SIZE} from the module
1267 @code{SYSTEM}, whereas in PIM3 and PIM4 @code{SIZE} is a pervasive
1268 function.  Thus compiling PIM4 source code with the @samp{-fpim2}
1269 switch will cause a compile time error.  This can be fixed quickly
1270 with an additional @code{IMPORT} or alternatively by compiling with
1271 the @samp{-fpim4} switch.
1273 However there are some very important differences between the dialects
1274 which are mutually exclusive and therefore it is vital that users
1275 choose the dialects with care when these language features are used.
1277 @subsection Integer division, remainder and modulus
1279 The most dangerous set of mutually exclusive features found in the
1280 four dialects supported by GNU Modula-2 are the @code{INTEGER}
1281 division, remainder and modulus arithmetic operators.  It is important
1282 to note that the same source code can be compiled to give different
1283 run time results depending upon these switches!  The reference manual
1284 for the various dialects of Modula-2 are quite clear about this
1285 behavior and sadly there are three distinct definitions.
1287 The table below illustrates the problem when a negative operand is
1288 used.
1290 @example
1291                   Pim2/3          Pim4                ISO
1292                -----------    -----------    ----------------------
1293 lval    rval   DIV     MOD    DIV     MOD    DIV    MOD    /    REM
1294  31      10      3       1      3       1      3      1     3     1
1295 -31      10     -3      -1     -4       9     -4      9    -3    -1
1296  31     -10     -3       1     -3       1     Exception    -3     1
1297 -31     -10      3      -1      4       9     Exception     3    -1
1298 @end example
1300 See also P24 of PIM2, P27 of PIM3, P29 of PIM4 and P201 of the ISO
1301 Standard.  At present all dialect division, remainder and modulus are
1302 implemented as above, apart from the exception calling in the ISO
1303 dialect.  Instead of exception handling the results are the same as the
1304 PIM4 dialect.  This is a temporary implementation situation.
1306 @node Exceptions, Semantic checking, Dialect, Using
1307 @section Exception implementation
1309 This section describes how exceptions are implemented in GNU Modula-2
1310 and how command line switches affect their behavior.  The option
1311 @samp{-fsoft-check-all} enables all software checking of nil
1312 dereferences, division by zero etc.  Additional code is produced to
1313 check these conditions and exception handlers are invoked if the
1314 conditions prevail.
1316 Without @samp{-fsoft-check-all} these exceptions will be caught by
1317 hardware (assuming the hardware support exists) and a signal handler
1318 is invoked.  The signal handler will in turn @code{THROW} an exception
1319 which will be caught by the appropriate Modula-2 handler.  However the
1320 action of throwing an exception from within a signal handler is
1321 implementation defined (according to the C++ documentation).  For
1322 example on the x86_64 architecture this works whereas on the i686
1323 architecture it does not.  Therefore to ensure portability it is
1324 recommended to use @samp{-fsoft-check-all}.
1326 @footnote{@samp{-fsoft-check-all} can be effectively combined with
1327 @samp{-O2} to semantically analyze source code for possible run time
1328 errors at compile time.}
1330 @node Semantic checking, Extensions, Exceptions, Using
1331 @section How to detect run time problems at compile time
1333 Consider the following program:
1335 @example
1336 MODULE assignvalue ;  (*!m2iso+gm2*)
1338 PROCEDURE bad () : INTEGER ;
1340    i: INTEGER ;
1341 BEGIN
1342    i := -1 ;
1343    RETURN i
1344 END bad ;
1347    foo: CARDINAL ;
1348 BEGIN
1349    (* The m2rte plugin will detect this as an error, post
1350       optimization.  *)
1351    foo := bad ()
1352 END assignvalue.
1353 @end example
1355 here we see that the programmer has overlooked that the return value
1356 from @samp{bad} will cause an overflow to @samp{foo}.  If we compile
1357 the code with the following options:
1359 @example
1360 $ gm2 -g -fsoft-check-all -O2 -c assignvalue.mod
1361 assignvalue.mod:16:0:inevitable that this error will occur at run time,
1362 assignment will result in an overflow
1363 @end example
1365 The gm2 semantic plugin is automatically run and will generate a
1366 warning message for every exception call which is known as reachable.
1367 It is highly advised to run the optimizer (@samp{-O2} or @samp{-O3})
1368 with @samp{-fsoft-check-all} so that the compiler is able to run the
1369 optimizer and perform variable and flow analysis before the semantic
1370 plugin is invoked.
1372 @node Extensions, Type compatibility, Semantic checking, Using
1373 @section GNU Modula-2 language extensions
1375 This section introduces the GNU Modula-2 language extensions.
1376 The GNU Modula-2 compiler allows abstract data types to be any type,
1377 not just restricted to a pointer type providing the
1378 @samp{-fextended-opaque} option is supplied
1379 @xref{Compiler options, , ,gm2}.
1381 Declarations can be made in any order, whether they are
1382 types, constants, procedures, nested modules or variables.
1383 @c (@xref{Passes, , ,}.)
1385 GNU Modula-2 also allows programmers to interface to @code{C} and
1386 assembly language.
1388 GNU Modula-2 provides support for the special tokens @code{__LINE__},
1389 @code{__FILE__}, @code{__FUNCTION__} and @code{__DATE__}.  Support for
1390 these tokens will occur even if the @samp{-fcpp} option is not
1391 supplied.  A table of these identifiers and their data type and values
1392 is given below:
1394 @example
1395 Scope       GNU Modula-2 token      Data type and example value
1397 anywhere    __LINE__                Constant Literal compatible
1398                                     with CARDINAL, INTEGER and WORD.
1399                                     Example 1234
1401 anywhere    __FILE__                Constant string compatible
1402                                     with parameter ARRAY OF CHAR or
1403                                     an ARRAY whose SIZE is >= string
1404                                     length.  Example
1405                                     "hello.mod"
1407 procedure   __FUNCTION__            Constant string compatible
1408                                     with parameter ARRAY OF CHAR or
1409                                     an ARRAY whose SIZE is >= string
1410                                     length.  Example
1411                                     "calc"
1413 module      __FUNCTION__            Example
1414                                     "module hello initialization"
1416 anywhere    __DATE__                Constant string compatible
1417                                     with parameter ARRAY OF CHAR or
1418                                     an ARRAY whose SIZE is >= string
1419                                     length.  Example
1420                                     "Thu Apr 29 10:07:16 BST 2004"
1422 anywhere   __COLUMN__               Gives a constant literal number
1423                                     determining the left hand column
1424                                     where the first _ appears in
1425                                     __COLUMN__.  The left most column
1426                                     is 1.
1428 @end example
1430 The preprocessor @samp{cpp} can be invoked via the @samp{-fcpp}
1431 command line option.  This in turn invokes @samp{cpp} with the
1432 following arguments @samp{-traditional -lang-asm}.  These options
1433 preserve comments and all quotations.  @samp{gm2} treats a @samp{#}
1434 character in the first column as a preprocessor directive.
1436 For example here is a module which calls @code{FatalError}
1437 via the macro @code{ERROR}.
1439 @example
1440 MODULE cpp ;
1442 FROM SYSTEM IMPORT ADR, SIZE ;
1443 FROM libc IMPORT exit, printf, malloc ;
1445 PROCEDURE FatalError (a, file: ARRAY OF CHAR;
1446                          line: CARDINAL;
1447                          func: ARRAY OF CHAR) ;
1448 BEGIN
1449    printf ("%s:%d:fatal error, %s, in %s\n",
1450             ADR (file), line, ADR (a), ADR (func)) ;
1451    exit (1)
1452 END FatalError ;
1454 #define ERROR(X)  FatalError(X, __FILE__, __LINE__, __FUNCTION__)
1457    pc: POINTER TO CARDINAL;
1458 BEGIN
1459    pc := malloc (SIZE (CARDINAL)) ;
1460    IF pc = NIL
1461    THEN
1462       ERROR ('out of memory')
1463    END
1464 END cpp.
1465 @end example
1467 Another use for the C preprocessor in Modula-2 might be to turn on
1468 debugging code.  For example the library module
1469 @file{FormatStrings.mod} uses procedures from @file{DynamicStrings.mod}
1470 and to track down memory leaks it was useful to track the source file
1471 and line where each string was created.  Here is a section of
1472 @file{FormatStrings.mod} which shows how the debugging code was
1473 enabled and disabled by adding @code{-fcpp} to the command line.
1475 @example
1476 FROM DynamicStrings IMPORT String, InitString, InitStringChar, Mark,
1477                            ConCat, Slice, Index, char,
1478                            Assign, Length, Mult, Dup, ConCatChar,
1479                            PushAllocation, PopAllocationExemption,
1480                            InitStringDB, InitStringCharStarDB,
1481                            InitStringCharDB, MultDB, DupDB, SliceDB ;
1484 #define InitString(X) InitStringDB(X, __FILE__, __LINE__)
1485 #define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, \
1486                                                    __LINE__)
1487 #define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__)
1488 #define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__)
1489 #define Dup(X) DupDB(X, __FILE__, __LINE__)
1490 #define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__)
1493 PROCEDURE doDSdbEnter ;
1494 BEGIN
1495    PushAllocation
1496 END doDSdbEnter ;
1498 PROCEDURE doDSdbExit (s: String) ;
1499 BEGIN
1500    s := PopAllocationExemption (TRUE, s)
1501 END doDSdbExit ;
1503 PROCEDURE DSdbEnter ;
1504 BEGIN
1505 END DSdbEnter ;
1507 PROCEDURE DSdbExit (s: String) ;
1508 BEGIN
1509 END DSdbExit ;
1512 #define DBsbEnter doDBsbEnter
1513 #define DBsbExit  doDBsbExit
1516 PROCEDURE Sprintf1 (s: String; w: ARRAY OF BYTE) : String ;
1517 BEGIN
1518    DSdbEnter ;
1519    s := FormatString (HandleEscape (s), w) ;
1520    DSdbExit (s) ;
1521    RETURN s
1522 END Sprintf1 ;
1523 @end example
1525 It is worth noting that the overhead of this code once @code{-fcpp} is
1526 not present and -O2 is used will be zero since the local empty
1527 procedures @code{DSdbEnter} and @code{DSdbExit} will be thrown away by
1528 the optimization passes of the GCC backend.
1530 @subsection Optional procedure parameter
1532 GNU Modula-2 allows the last parameter to a procedure or function
1533 parameter to be optional.  For example in the ISO library
1534 @file{COROUTINES.def} the procedure @code{NEWCOROUTINE} is defined as
1535 having an optional fifth argument (@code{initProtection}) which, if
1536 absent, is automatically replaced by @code{NIL}.
1538 @example
1539 @findex NEWCOROUTINE
1540 PROCEDURE NEWCOROUTINE (procBody: PROC; workspace: SYSTEM.ADDRESS;
1541                         size: CARDINAL; VAR cr: COROUTINE;
1542                         [initProtection: PROTECTION = NIL]);
1544   (* Creates a new coroutine whose body is given by procBody,
1545      and returns the identity of the coroutine in cr.
1546      workspace is a pointer to the work space allocated to
1547      the coroutine; size specifies the size of this workspace
1548      in terms of SYSTEM.LOC.
1550      The optional fifth argument may contain a single parameter
1551      which specifies the initial protection level of the coroutine.
1552   *)
1553 @end example
1555 The implementation module @file{COROUTINES.mod} implements this
1556 procedure using the following syntax:
1558 @example
1559 PROCEDURE NEWCOROUTINE (procBody: PROC; workspace: SYSTEM.ADDRESS;
1560                         size: CARDINAL; VAR cr: COROUTINE;
1561                         [initProtection: PROTECTION]);
1562 BEGIN
1564 END NEWCOROUTINE ;
1565 @end example
1567 Note that it is illegal for this declaration to contain an initializer
1568 value for @code{initProtection}.  However it is necessary to surround
1569 this parameter with the brackets @code{[} and @code{]}.  This serves to
1570 remind the programmer that the last parameter was declared as optional
1571 in the definition module.
1573 Local procedures can be declared to have an optional final parameter
1574 in which case the initializer is mandatory in the implementation or
1575 program module.
1577 GNU Modula-2 also provides additional fixed sized data types which
1578 are all exported from the @code{SYSTEM} module.
1579 @xref{The PIM system module, , ,gm2}.
1580 @xref{The ISO system module, , ,gm2}.
1582 @node Type compatibility, Unbounded by reference, Extensions, Using
1583 @section Type compatibility
1585 This section discuss the issues surrounding assignment, expression
1586 and parameter compatibility, their effect of the additional
1587 fixed sized datatypes and also their effect of run time checking.
1588 The data types supported by the compiler are:
1590 @example
1591 GNU Modula-2              scope      switches
1592 =============================================
1593 INTEGER                   pervasive
1594 LONGINT                   pervasive
1595 SHORTINT                  pervasive
1596 CARDINAL                  pervasive
1597 LONGCARD                  pervasive
1598 SHORTCARD                 pervasive
1599 BOOLEAN                   pervasive
1600 BITSET                    pervasive
1601 REAL                      pervasive
1602 LONGREAL                  pervasive
1603 SHORTREAL                 pervasive
1604 CHAR                      pervasive
1605 SHORTCOMPLEX              pervasive
1606 COMPLEX                   pervasive
1607 LONGCOMPLEX               pervasive
1609 LOC                       SYSTEM     -fiso
1610 BYTE                      SYSTEM
1611 WORD                      SYSTEM
1612 ADDRESS                   SYSTEM
1614 The following extensions are supported for
1615 most architectures (please check SYSTEM.def).
1616 =============================================
1617 INTEGER8                  SYSTEM
1618 INTEGER16                 SYSTEM
1619 INTEGER32                 SYSTEM
1620 INTEGER64                 SYSTEM
1621 CARDINAL8                 SYSTEM
1622 CARDINAL16                SYSTEM
1623 CARDINAL32                SYSTEM
1624 CARDINAL64                SYSTEM
1625 BITSET8                   SYSTEM
1626 BITSET16                  SYSTEM
1627 BITSET32                  SYSTEM
1628 WORD16                    SYSTEM
1629 WORD32                    SYSTEM
1630 WORD64                    SYSTEM
1631 REAL32                    SYSTEM
1632 REAL64                    SYSTEM
1633 REAL96                    SYSTEM
1634 REAL128                   SYSTEM
1635 COMPLEX32                 SYSTEM
1636 COMPLEX64                 SYSTEM
1637 COMPLEX96                 SYSTEM
1638 COMPLEX128                SYSTEM
1639 @end example
1641 The Modula-2 language categorizes compatibility between entities of
1642 possibly differing types into three sub components: expressions,
1643 assignments, and parameters.  Parameter compatibility is further
1644 divided into two sections for pass by reference and pass by value
1645 compatibility.
1647 For more detail on the Modula-2 type compatibility see the Modula-2
1648 ISO standard BS ISO/IEC 10514-1:1996 page 121-125.  For detail on the
1649 PIM type compatibility see Programming in Modula-2 Edition 4 page 29,
1650 (Elementary Data Types).
1652 @subsection Expression compatibility
1654 Modula-2 restricts the types of expressions to the same type.
1655 Expression compatibility is a symmetric relation.
1657 For example two sub expressions of @code{INTEGER} and @code{CARDINAL}
1658 are not expression compatible
1659 (@uref{http://freepages.modula2.org/report4/modula-2.html} and ISO
1660 Modula-2).
1662 In GNU Modula-2 this rule is also extended across all fixed sized data
1663 types (imported from SYSTEM).
1665 @subsection Assignment compatibility
1667 This section discusses the assignment issues surrounding assignment
1668 compatibility of elementary types (@code{INTEGER}, @code{CARDINAL},
1669 @code{REAL} and @code{CHAR} for example).  The information here is
1670 found in more detail in the Modula-2 ISO standard BS ISO/IEC
1671 10514-1:1996 page 122.
1673 Assignment compatibility exists between the same sized elementary
1674 types.
1676 Same type family of different sizes are
1677 also compatible as long as the @code{MAX(}type@code{)} and
1678 @code{MIN(}type@code{)} is known.  So for example this includes the
1679 @code{INTEGER} family, @code{CARDINAL} family and the @code{REAL}
1680 family.
1682 The reason for this is that when the assignment is performed
1683 the compiler will check to see that the expression (on the right of
1684 the @code{:=}) lies within the range of the designator type (on the
1685 left hand side of the @code{:=}).  Thus these ordinal types can be
1686 assignment compatible.  However it does mean that @code{WORD32} is not
1687 compatible with @code{WORD16} as @code{WORD32} does not have a minimum
1688 or maximum value and therefore cannot be checked.  The compiler does
1689 not know which of the two bytes from @code{WORD32} should be copied
1690 into @code{WORD16} and which two should be ignored.  Currently the
1691 types @code{BITSET8}, @code{BITSET16} and @code{BITSET32} are
1692 assignment incompatible.  However this restriction maybe lifted when
1693 further run time checking is achieved.
1695 Modula-2 does allow @code{INTEGER} to be assignment compatible with
1696 @code{WORD} as they are the same size.  Likewise GNU Modula-2 allows
1697 @code{INTEGER16} to be compatible with @code{WORD16} and the same for
1698 the other fixed sized types and their sized equivalent in either
1699 @code{WORD}n, @code{BYTE} or @code{LOC} types.  However it prohibits
1700 assignment between @code{WORD} and @code{WORD32} even though on many
1701 systems these sizes will be the same.  The reasoning behind this rule
1702 is that the extended fixed sized types are meant to be used by
1703 applications requiring fixed sized data types and it is more portable
1704 to forbid the blurring of the boundaries between fixed sized and
1705 machine dependent sized types.
1707 Intermediate code run time checking is always generated by the front
1708 end.  However this intermediate code is only translated into actual
1709 code if the appropriate command line switches are specified.  This
1710 allows the compiler to perform limited range checking at compile time.
1711 In the future it will allow the extensive GCC optimizations to
1712 propagate constant values through to the range checks which if they
1713 are found to exceed the type range will result in a compile time
1714 error message.
1716 @subsection Parameter compatibility
1718 Parameter compatibility is divided into two areas, pass by value and
1719 pass by reference (@code{VAR}).  In the case of pass by value the
1720 rules are exactly the same as assignment.  However in the second case,
1721 pass by reference, the actual parameter and formal parameter must be
1722 the same size and family.  Furthermore @code{INTEGER} and
1723 @code{CARDINAL}s are not treated as compatible in the pass by
1724 reference case.
1726 The types @code{BYTE}, @code{LOC}, @code{WORD} and @code{WORD}n
1727 derivatives are assignment and parameter compatible with any data type
1728 of the same size.
1730 @node Unbounded by reference, Building a shared library, Type compatibility, Using
1731 @section Unbounded by reference
1733 This section documents a GNU Modula-2 compiler switch which implements
1734 a language optimization surrounding the implementation of unbounded
1735 arrays.  In GNU Modula-2 the unbounded array is implemented by
1736 utilizing an internal structure @code{struct @{dataType *address,
1737 unsigned int high@}}.  So given the Modula-2 procedure declaration:
1739 @example
1740 PROCEDURE foo (VAR a: ARRAY OF dataType) ;
1741 BEGIN
1742    IF a[2]= (* etc *)
1743 END foo ;
1744 @end example
1746 it is translated into GCC @code{tree}s, which can be represented
1747 in their C form thus:
1749 @example
1750 void foo (struct @{dataType *address, unsigned int high@} a)
1752    if (a.address[2] == /* etc */
1754 @end example
1756 Whereas if the procedure @code{foo} was declared as:
1758 @example
1759 PROCEDURE foo (a: ARRAY OF dataType) ;
1760 BEGIN
1761    IF a[2]= (* etc *)
1762 END foo ;
1763 @end example
1765 then it is implemented by being translated into the following
1766 GCC @code{tree}s, which can be represented in their C form thus:
1768 @example
1769 void foo (struct @{dataType *address, unsigned int high@} a)
1771    dataType *copyContents = (dataType *)alloca (a.high+1);
1772    memcpy(copyContents, a.address, a.high+1);
1773    a.address = copyContents;
1775    if (a.address[2] == /* etc */
1777 @end example
1779 This implementation works, but it makes a copy of each non VAR
1780 unbounded array when a procedure is entered.  If the unbounded array
1781 is not changed during procedure @code{foo} then this implementation
1782 will be very inefficient.  In effect Modula-2 lacks the @code{REF}
1783 keyword of Ada.  Consequently the programmer maybe tempted to
1784 sacrifice semantic clarity for greater efficiency by declaring the
1785 parameter using the @code{VAR} keyword in place of @code{REF}.
1787 The @code{-funbounded-by-reference} switch instructs the compiler to
1788 check and see if the programmer is modifying the content of any
1789 unbounded array.  If it is modified then a copy will be made upon
1790 entry into the procedure.  Conversely if the content is only read and
1791 never modified then this non @code{VAR} unbounded array is a candidate
1792 for being passed by reference.  It is only a candidate as it is still
1793 possible that passing this parameter by reference could alter the
1794 meaning of the source code.  For example consider the following case:
1796 @example
1797 PROCEDURE StrConCat (VAR a: ARRAY OF CHAR; b, c: ARRAY OF CHAR) ;
1798 BEGIN
1799    (* code which performs string a := b + c *)
1800 END StrConCat ;
1802 PROCEDURE foo ;
1804    a: ARRAY [0..3] OF CHAR ;
1805 BEGIN
1806    a := 'q' ;
1807    StrConCat(a, a, a)
1808 END foo ;
1809 @end example
1811 In the code above we see that the same parameter, @code{a}, is being
1812 passed three times to @code{StrConCat}.  Clearly even though parameters
1813 @code{b} and @code{c} are never modified it would be incorrect to
1814 implement them as pass by reference.  Therefore the compiler checks to
1815 see if any non @code{VAR} parameter is type compatible with any
1816 @code{VAR} parameter and if so it generates run time procedure entry
1817 checks to determine whether the contents of parameters @code{b} or
1818 @code{c} matches the contents of @code{a}.  If a match is detected
1819 then a copy is made and the @code{address} in the unbounded
1820 @code{struct}ure is modified.
1822 The compiler will check the address range of each candidate against
1823 the address range of any @code{VAR} parameter, providing they are type
1824 compatible.  For example consider:
1826 @example
1827 PROCEDURE foo (a: ARRAY OF BYTE; VAR f: REAL) ;
1828 BEGIN
1829    f := 3.14 ;
1830    IF a[0]=BYTE(0)
1831    THEN
1832       (* etc *)
1833    END
1834 END foo ;
1836 PROCEDURE bar ;
1837 BEGIN
1838    r := 2.0 ;
1839    foo(r, r)
1840 END bar ;
1841 @end example
1843 Here we see that although parameter, @code{a}, is a candidate for the
1844 passing by reference, it would be incorrect to use this
1845 transformation.  Thus the compiler detects that parameters, @code{a}
1846 and @code{f} are type compatible and will produce run time checking
1847 code to test whether the address range of their respective contents
1848 intersect.
1850 @node Linking, Building a shared library, Unbounded by reference, Using
1852 This section describes the linking related options.  There are three
1853 linking strategies available which are dynamic scaffold, static
1854 scaffold and user defined.  The dynamic scaffold is enabled by default
1855 and each module will register itself to the run time @samp{M2RTS} via
1856 a constructor.  The static scaffold mechanism will invoke each modules
1857 @samp{_init} and @samp{_finish} function in turn via a sequence of
1858 calls from within @samp{main}.  Lastly the user defined strategy
1859 can be implemented by turning off the dynamic and static options via
1860 @samp{-fno-scaffold-dynamic} and @samp{-fno-scaffold-static}.
1862 In the simple test below:
1864 @example
1865 $ gm2 hello.mod
1866 @end example
1868 the driver will add the options @samp{-fscaffold-dynamic} and
1869 @samp{-fgen-module-list=-} which generate a list of application
1870 modules and also creates the @samp{main} function with calls to
1871 @samp{M2RTS}.  It can be useful to add the option @samp{-fsources}
1872 which displays the source files as they are parsed and summarizes
1873 whether the source file is required for compilation or linking.
1875 If you wish to split the above command line into a compile and link
1876 then you could use these steps:
1878 @example
1879 $ gm2 -c -fscaffold-main hello.mod
1880 $ gm2 hello.o
1881 @end example
1883 The @samp{-fscaffold-main} informs the compiler to generate the
1884 @samp{main} function and scaffold.  You can enable the environment
1885 variable @samp{GCC_M2LINK_RTFLAG} to trace the construction and
1886 destruction of the application.  The values for
1887 @samp{GCC_M2LINK_RTFLAG} are shown in the table below:
1889 @example
1890 value   | meaning
1891 =================
1892 all     | turn on all flags below
1893 module  | trace modules as they register themselves
1894 pre     | generate module list prior to dependency resolution
1895 dep     | trace module dependency resolution
1896 post    | generate module list after dependency resolution
1897 force   | generate a module list after dependency and forced
1898         | ordering is complete
1899 @end example
1901 The values can be combined using a comma separated list.
1903 One of the advantages of the dynamic scaffold is that the driver
1904 behaves in a similar way to the other front end drivers.
1905 For example consider a small project consisting of 4 definition
1906 implementation modules (@samp{a.def}, @samp{a.mod}, @samp{b.def},
1907 @samp{b.mod}, @samp{c.def}, @samp{c.mod}, @samp{d.def}, @samp{d.mod})
1908 and a program module @samp{program.mod}.
1910 To link this project we could:
1912 @example
1913 $ gm2 -g -c a.mod
1914 $ gm2 -g -c b.mod
1915 $ gm2 -g -c c.mod
1916 $ gm2 -g -c d.mod
1917 $ gm2 -g program.mod a.o b.o c.o d.o
1918 @end example
1920 The module initialization sequence is defined by the ISO standard to
1921 follow the import graph traversal.  The initialization order is the
1922 order in which the corresponding separate modules finish the
1923 processing of their import lists.
1925 However, if required, you can override this using
1926 @samp{-fruntime-modules=a,b,c,d} for example which forces the
1927 initialization sequence to @samp{a}, @samp{b}, @samp{c} and @samp{d}.
1929 @node Building a shared library, Interface for Python, Unbounded by reference, Using
1930 @section Building a shared library
1932 This section describes building a tiny shared library implemented in
1933 Modula-2 and built with @file{libtool}.  Suppose a project consists of
1934 two definition modules and two implementation modules and a program
1935 module @file{a.def}, @file{a.mod}, @file{b.def}, @file{b.mod} and
1936 @file{c.mod}.  The first step is to compile the modules using position
1937 independent code.  This can be achieved by the following three
1938 commands:
1940 @example
1941 libtool --tag=CC --mode=compile gm2 -g -c a.mod -o a.lo
1942 libtool --tag=CC --mode=compile gm2 -g -c b.mod -o b.lo
1943 libtool --tag=CC --mode=compile gm2 -g -c c.mod -o c.lo
1944 @end example
1946 The second step is to generate the shared library initialization and
1947 finalization routines.  We can do this by asking gm2 to generate a
1948 list of dependent modules and then use this to generate the scaffold.
1949 We also must compile the scaffold.
1951 @example
1952 gm2 -c -g -fmakelist c.mod
1953 gm2 -c -g -fmakeinit -fshared c.mod
1954 libtool --tag=CC --mode=compile g++ -g -c c_m2.cpp -o c_m2.lo
1955 @end example
1957 The third step is to link all these @file{.lo} files.
1959 @example
1960 libtool --mode=link gcc -g c_m2.lo a.lo b.lo c.lo \
1961         -L$(prefix)/lib64 \
1962         -rpath `pwd` -lgm2 -lstdc++ -lm -o libabc.la
1963 @end example
1965 At this point the shared library @file{libabc.so} will have been
1966 created inside the directory @file{.libs}.
1968 @node Interface for Python, Producing a Python module, Building a shared library, Using
1969 @section How to produce swig interface files
1971 This section describes how Modula-2 implementation modules can be
1972 called from Python (and other scripting languages such as TCL and
1973 Perl).  GNU Modula-2 can be instructed to create a swig interface when
1974 it is compiling an implementation module.  Swig then uses the
1975 interface file to generate all the necessary wrapping to that the
1976 desired scripting language may access the implementation module.
1978 Here is an example of how you might call upon the services of the
1979 Modula-2 library module @code{NumberIO} from Python3.
1981 The following commands can be used to generate the Python3 module:
1983 @example
1984 export src=@samp{directory to the sources}
1985 export prefix=@samp{directory to where the compiler is installed}
1986 gm2 -I$@{src@} -c -g -fswig $@{src@}/../../../gm2-libs/NumberIO.mod
1987 gm2 -I$@{src@} -c -g -fmakelist $@{src@}/../../../gm2-libs/NumberIO.mod
1989 gm2 -I$@{src@} -c -g -fmakeinit -fshared \
1990    $@{src@}/../../../gm2-libs/NumberIO.mod
1992 swig -c++ -python3 NumberIO.i
1994 libtool --mode=compile g++ -g -c -I$@{src@} NumberIO_m2.cpp \
1995   -o NumberIO_m2.lo
1997 libtool --tag=CC --mode=compile gm2 -g -c \
1998   -I$@{src@}../../../gm2-libs \
1999   $@{src@}/../../../gm2-libs/NumberIO.mod -o NumberIO.lo
2001 libtool --tag=CC --mode=compile g++ -g -c NumberIO_wrap.cxx \
2002   -I/usr/include/python3 -o NumberIO_wrap.lo
2004 libtool --mode=link gcc -g NumberIO_m2.lo NumberIO_wrap.lo \
2005    -L$@{prefix@}/lib64 \
2006    -rpath `pwd` -lgm2 -lstdc++ -lm -o libNumberIO.la
2008 cp .libs/libNumberIO.so _NumberIO.so
2009 @end example
2011 The first four commands, generate the swig interface file
2012 @file{NumberIO.i} and python wrap files @file{NumberIO_wrap.cxx} and
2013 @file{NumberIO.py}.  The next three @file{libtool} commnads compile
2014 the C++ and Modula-2 source code into @file{.lo} objects.  The last
2015 @file{libtool} command links all the @file{.lo} files into a
2016 @file{.la} file and includes all shared library dependencies.
2018 Now it is possible to run the following Python script
2019 (called @file{testnum.py}):
2021 @example
2022 import NumberIO
2024 print ("1234 x 2 =", NumberIO.NumberIO_StrToInt("1234")*2)
2025 @end example
2027 like this:
2029 @example
2030 $ python3 testnum.py
2031 1234 x 2 = 2468
2032 @end example
2034 @xref{Producing a Python module, , ,gm2} for another example which
2035 uses the @code{UNQUALIFIED} keyword to reduce the module name clutter
2036 from the viewport of Python3.
2038 @subsection Limitations of automatic generated of Swig files
2040 This section discusses the limitations of automatically generating
2041 swig files.  From the previous example we see that the module
2042 @code{NumberIO} had a swig interface file @file{NumberIO.i}
2043 automatically generated by the compiler.  If we consider three of the
2044 procedure definitions in @file{NumberIO.def} we can see the
2045 success and limitations of the automatic interface generation.
2047 @example
2048 PROCEDURE StrToHex (a: ARRAY OF CHAR; VAR x: CARDINAL) ;
2049 PROCEDURE StrToInt (a: ARRAY OF CHAR; VAR x: INTEGER) ;
2050 PROCEDURE ReadInt (VAR x: CARDINAL) ;
2051 @end example
2053 Below are the swig interface prototypes:
2055 @example
2056 extern void NumberIO_StrToHex (char *_m2_address_a,
2057                                int _m2_high_a, unsigned int *OUTPUT);
2058 /*  parameters: x is known to be an OUTPUT */
2059 extern void NumberIO_StrToInt (char *_m2_address_a,
2060                                int _m2_high_a, int *OUTPUT);
2061 /*  parameters: x is guessed to be an OUTPUT */
2062 extern void NumberIO_ReadInt (int *x);
2063 /*  parameters: x is unknown */
2064 @end example
2066 In the case of @code{StrToHex} it can be seen that the compiler
2067 detects that the last parameter is an output.  It explicitly tells
2068 swig this by using the parameter name @code{OUTPUT} and in the
2069 following comment it informs the user that it knows this to be an
2070 output parameter.  In the second procedure @code{StrToInt} it marks
2071 the final parameter as an output, but it tells the user that this is
2072 only a guess.  Finally in @code{ReadInt} it informs the user that
2073 it does not know whether the parameter, @code{x}, is an output, input
2074 or an inout parameter.
2076 The compiler decides whether to mark a parameter as either:
2077 @code{INPUT}, @code{OUTPUT} or @code{INOUT} if it is read before
2078 written or visa versa in the first basic block.  At this point
2079 it will write output that the parameter is known.  If it is not
2080 read or written in the first basic block then subsequent basic blocks
2081 are searched and the result is commented as a guess.  Finally if
2082 no read or write occurs then the parameter is commented as unknown.
2083 However, clearly it is possible to fool this mechanism.  Nevertheless
2084 automatic generation of implementation module into swig interface files
2085 was thought sufficiently useful despite these limitations.
2087 In conclusion it would be wise to check all parameters in any
2088 automatically generated swig interface file.  Furthermore you can
2089 force the automatic mechanism to generate correct interface files by
2090 reading or writing to the @code{VAR} parameter in the first basic
2091 block of a procedure.
2093 @node Producing a Python module, Interface to C, Interface for Python, Using
2094 @section How to produce a Python module
2096 This section describes how it is possible to produce a Python module
2097 from Modula-2 code.  There are a number of advantages to this
2098 approach, it ensures your code reaches a wider audience, maybe it is
2099 easier to initialize your application in Python.
2101 The example application here is a pedagogical two dimensional gravity
2102 next event simulation.  The Python module needs to have a clear API
2103 which should be placed in a single definition module.  Furthermore the
2104 API should only use fundamental pervasive data types and strings.
2105 Below the API is contained in the file @file{twoDsim.def}:
2107 @example
2108 DEFINITION MODULE twoDsim ;
2110 EXPORT UNQUALIFIED gravity, box, poly3, poly5, poly6, mass,
2111                    fix, circle, pivot, velocity, accel, fps,
2112                    replayRate, simulateFor ;
2114    gravity - turn on gravity at: g m^2
2117 PROCEDURE gravity (g: REAL) ;
2121    box - place a box in the world at (x0,y0),(x0+i,y0+j)
2124 PROCEDURE box (x0, y0, i, j: REAL) : CARDINAL ;
2128    poly3 - place a triangle in the world at:
2129            (x0,y0),(x1,y1),(x2,y2)
2132 PROCEDURE poly3 (x0, y0, x1, y1, x2, y2: REAL) : CARDINAL ;
2136    poly5 - place a pentagon in the world at:
2137            (x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4)
2140 PROCEDURE poly5 (x0, y0, x1, y1,
2141                  x2, y2, x3, y3, x4, y4: REAL) : CARDINAL ;
2145    poly6 - place a hexagon in the world at:
2146            (x0,y0),(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)
2149 PROCEDURE poly6 (x0, y0, x1, y1,
2150                  x2, y2, x3, y3,
2151                  x4, y4, x5, y5: REAL) : CARDINAL ;
2155    mass - specify the mass of an object and return the, id.
2158 PROCEDURE mass (id: CARDINAL; m: REAL) : CARDINAL ;
2162    fix - fix the object to the world.
2165 PROCEDURE fix (id: CARDINAL) : CARDINAL ;
2169    circle - adds a circle to the world.  Center
2170             defined by: x0, y0 radius, r.
2173 PROCEDURE circle (x0, y0, r: REAL) : CARDINAL ;
2177    velocity - give an object, id, a velocity, vx, vy.
2180 PROCEDURE velocity (id: CARDINAL; vx, vy: REAL) : CARDINAL ;
2184    accel - give an object, id, an acceleration, ax, ay.
2187 PROCEDURE accel (id: CARDINAL; ax, ay: REAL) : CARDINAL ;
2191    fps - set frames per second.
2194 PROCEDURE fps (f: REAL) ;
2198    replayRate - set frames per second during replay.
2201 PROCEDURE replayRate (f: REAL) ;
2205    simulateFor - render for, t, seconds.
2208 PROCEDURE simulateFor (t: REAL) ;
2211 END twoDsim.
2212 @end example
2214 The keyword @code{UNQUALIFIED} can be used to ensure that the
2215 compiler will provide externally accessible functions
2216 @code{gravity}, @code{box}, @code{poly3}, @code{poly5}, @code{poly6},
2217 @code{mass}, @code{fix}, @code{circle}, @code{pivot}, @code{velocity},
2218 @code{accel}, @code{fps}, @code{replayRate}, @code{simulateFor}
2219 rather than name mangled alternatives.
2220 Hence in our Python3 application we could write:
2222 @example
2223 #!/usr/bin/env python3
2225 from twoDsim import *
2227 b = box (0.0, 0.0, 1.0, 1.0)
2228 b = fix (b)
2229 c1 = circle (0.7, 0.7, 0.05)
2230 c1 = mass (c1, 0.01)
2231 c2 = circle (0.7, 0.1, 0.05)
2232 c2 = mass (c2, 0.01)
2233 c2 = fix (c2)
2234 gravity (-9.81)
2235 fps (24.0*4.0)
2236 replayRate (24.0)
2237 print ("creating frames")
2238 try:
2239     simulateFor (1.0)
2240     print ("all done")
2241 except:
2242     print ("exception raised")
2243 @end example
2245 which accesses the various functions defined and implemented by the
2246 module @code{twoDsim}.  The Modula-2 source code is compiled via:
2248 @example
2249 $ gm2 -g -fiso -c -fswig twoDsim.mod
2250 $ gm2 -g -fiso -c -fmakelist twoDsim.mod
2251 $ gm2 -g -fiso -c -fmakeinit twoDsim.mod
2252 @end example
2254 The first command both compiles the source file creating
2255 @file{twoDsim.o} and produces a swig interface file @file{swig.i}.  We
2256 now use @code{swig} and @code{g++} to produce and compile the
2257 interface wrappers:
2259 @example
2260 $ libtool --mode=compile g++ -g -c twoDsim_m2.cpp -o twoDsim_m2.lo
2261 $ swig -c++ -python3 twoDsim.i
2262 $ libtool --mode=compile g++ -c -fPIC twoDsim_wrap.cxx \
2263    -I/usr/include/python3 -o twoDsim_wrap.lo
2264 $ libtool --mode=compile gm2 -g -fPIC -fiso -c deviceGnuPic.mod
2265 $ libtool --mode=compile gm2 -g -fPIC -fiso -c roots.mod
2266 $ libtool --mode=compile gm2 -g -fPIC -fiso -c -fswig \
2267    twoDsim.mod -o twoDsim.lo
2268 @end example
2270 Finally the application is linked into a shared library:
2272 @example
2273 $ libtool --mode=link gcc -g twoDsim_m2.lo twoDsim_wrap.lo \
2274   roots.lo deviceGnuPic.lo \
2275    -L$@{prefix@}/lib64 \
2276    -rpath `pwd` -lgm2 -lstdc++ -lm -o libtwoDsim.la
2277 cp .libs/libtwoDsim.so _twoDsim.so
2278 @end example
2280 The library name must start with @code{_} to comply with the Python3
2281 module naming scheme.
2283 @node Interface to C, Assembly language, Producing a Python module, Using
2284 @section Interfacing GNU Modula-2 to C
2286 The GNU Modula-2 compiler tries to use the C calling convention
2287 wherever possible however some parameters have no C equivalent and
2288 thus a language specific method is used.  For example unbounded arrays
2289 are passed as a @code{struct @{void *address, unsigned int high@}} and
2290 the contents of these arrays are copied by callee functions when they
2291 are declared as non @code{VAR} parameters.  The @code{VAR} equivalent
2292 unbounded array parameters need no copy, but still use the
2293 @code{struct} representation.
2295 The recommended method of interfacing GNU Modula-2 to C is by telling
2296 the definition module that the implementation is in the C language.
2297 This is achieved by using the tokens @code{DEFINITION MODULE FOR "C"}.
2298 Here is an example @file{libprintf.def}.
2300 @example
2301 DEFINITION MODULE FOR "C" libprintf ;
2303 EXPORT UNQUALIFIED printf ;
2305 PROCEDURE printf (a: ARRAY OF CHAR; ...) : [ INTEGER ] ;
2307 END libprintf.
2308 @end example
2310 the @code{UNQUALIFIED} keyword in the definition module informs
2311 GNU Modula-2 not to prefix the module name to exported references
2312 in the object file.
2314 The @code{printf} declaration states that the first parameter
2315 semantically matches @code{ARRAY OF CHAR} but since the module is for
2316 the C language it will be mapped onto @code{char *}.  The token
2317 @code{...} indicates a variable number of arguments (varargs) and all
2318 parameters passed here are mapped onto their C equivalents.  Arrays and
2319 constant strings are passed as pointers.  Lastly @code{[ INTEGER ]}
2320 states that the caller can ignore the function return result if desired.
2322 The hello world program can be rewritten as:
2324 @example
2325 MODULE hello ;
2327 FROM libprintf IMPORT printf ;
2329 BEGIN
2330    printf ("hello world\n")
2331 END hello.
2332 @end example
2334 and it can be compiled by:
2336 @samp{gm2 -g hello.mod -lc}
2338 In reality the @samp{-lc} is redundant as libc is always included in the
2339 linking process.  It is shown here to emphasize that the C library or
2340 object file containing @code{printf} must be present.  The search path
2341 for modules can be changed by using @samp{-I}.
2343 If a procedure function is declared using varargs then some parameter
2344 values are converted.  The table below summarizes the default conversions
2345 and default types used.
2347 @example
2348 Actual Parameter       |  Default conversion  |   Type of actual
2349                        |                      |   value passed
2350 ===============================================================
2351 123                    |  none                |   long long int
2352 "hello world"          |  none                |   const char *
2353 a: ARRAY OF CHAR       |  ADR (a)             |   char *
2354 a: ARRAY [0..5] OF CHAR|  ADR (a)             |   char *
2355 3.14                   |  none                |   long double
2356 @end example
2358 If you wish to pass @code{int} values then you should explicitly
2359 convert the constants using one of the conversion mechanisms.
2360 For example:  @code{INTEGER(10)} or @code{VAL(INTEGER, 10)} or
2361 @code{CAST(INTEGER, 10)}.
2363 @node Assembly language, Alignment, Interface to C, Using
2364 @section Interface to assembly language
2366 The interface for GNU Modula-2 to assembly language is almost
2367 identical to GNU C.  The only alterations are that the keywords
2368 @code{asm} and @code{volatile} are in capitals, following the Modula-2
2369 convention.
2371 A simple, but highly non optimal, example is given below.  Here we want
2372 to add the two @code{CARDINAL}s @code{foo} and @code{bar} together and
2373 return the result.  The target processor is assumed to be executing
2374 the x86_64 instruction set.
2376 @example
2377 PROCEDURE Example (foo, bar: CARDINAL) : CARDINAL ;
2379    myout: CARDINAL ;
2380 BEGIN
2381    ASM VOLATILE ("movq %1,%%rax; addq %2,%%rax; movq %%rax,%0"
2382       : "=rm" (myout)            (* outputs *)
2383       : "rm" (foo), "rm" (bar)   (* inputs  *)
2384       : "rax") ;                 (* we trash *)
2385    RETURN( myout )
2386 END Example ;
2387 @end example
2389 For a full description of this interface we refer the reader to the GNU C manual.
2391 @xref{Extended Asm, ,Extensions to the C Language Family,gcc}.
2393 The same example can be written using the newer extensions of naming
2394 the operands rather than using numbered arguments.
2396 @example
2397 PROCEDURE Example (foo, bar: CARDINAL) : CARDINAL ;
2399    myout: CARDINAL ;
2400 BEGIN
2401    ASM VOLATILE (
2402     "movq %[left],%%rax; addq %[right],%%rax; movq %%rax,%[output]"
2403       : [output] "=rm" (myout)                  (* outputs *)
2404       : [left] "rm" (foo), [right] "rm" (bar)   (* inputs  *)
2405       : "rax") ;                                (* we trash *)
2406    RETURN( myout )
2407 END Example ;
2408 @end example
2410 Both examples generate exactly the same code.  It is worth noting that
2411 the specifier ``rm'' indicates that the operand can be either a
2412 register or memory.  Of course you must choose an instruction which
2413 can take either, but this allows the compiler to take make more
2414 efficient choices depending upon the optimization level given to the
2415 compiler.
2417 @node Alignment, Packed, Assembly language, Using
2418 @section Data type alignment
2420 GNU Modula-2 allows you to specify alignment for types and variables.
2421 The syntax for alignment is to use the ISO pragma directives @code{<*}
2422 @code{bytealignment (} expression @code{)} and @code{*>}.  These directives
2423 can be used after type and variable declarations.
2425 The ebnf of the alignment production is:
2427 @example
2428 Alignment := [ ByteAlignment ] =:
2429 ByteAlignment := '<*' AttributeExpression '*>' =:
2430 AlignmentExpression := "(" ConstExpression ")" =:
2431 @end example
2433 The @code{Alignment} ebnf statement may be used during construction of
2434 types, records, record fields, arrays, pointers and variables.  Below
2435 is an example of aligning a type so that the variable @code{bar} is
2436 aligned on a 1024 address.
2438 @example
2439 MODULE align ;
2441 TYPE
2442    foo = INTEGER <* bytealignment(1024) *> ;
2445    z  : INTEGER ;
2446    bar: foo ;
2447 BEGIN
2448 END align.
2449 @end example
2451 The next example aligns a variable on a 1024 byte boundary.
2453 @example
2454 MODULE align2 ;
2457    x  : CHAR ;
2458    z  : ARRAY [0..255] OF INTEGER <* bytealignment(1024) *> ;
2459 BEGIN
2460 END align2.
2461 @end example
2463 Here the example aligns a pointer on a 1024 byte boundary.
2465 @example
2466 MODULE align4 ;
2468 FROM SYSTEM IMPORT ADR ;
2469 FROM libc IMPORT exit ;
2472    x  : CHAR ;
2473    z  : POINTER TO INTEGER <* bytealignment(1024) *> ;
2474 BEGIN
2475    IF ADR(z) MOD 1024=0
2476    THEN
2477       exit(0)
2478    ELSE
2479       exit(1)
2480    END
2481 END align4.
2482 @end example
2484 In example @code{align5} record field @code{y} is aligned on a 1024
2485 byte boundary.
2487 @example
2488 MODULE align5 ;
2490 FROM SYSTEM IMPORT ADR ;
2491 FROM libc IMPORT exit ;
2493 TYPE
2494    rec = RECORD
2495             x: CHAR ;
2496             y: CHAR <* bytealignment(1024) *> ;
2497          END ;
2499    r: rec ;
2500 BEGIN
2501    IF ADR(r.y) MOD 1024=0
2502    THEN
2503       exit(0)
2504    ELSE
2505       exit(1)
2506    END
2507 END align5.
2508 @end example
2510 In the example below module @code{align6} declares @code{foo} as an
2511 array of 256 @code{INTEGER}s.  The array @code{foo} is aligned on a
2512 1024 byte boundary.
2514 @example
2515 MODULE align6 ;
2517 FROM SYSTEM IMPORT ADR ;
2518 FROM libc IMPORT exit ;
2520 TYPE
2521    foo = ARRAY [0..255] OF INTEGER <* bytealignment(1024) *> ;
2524    x  : CHAR ;
2525    z  : foo ;
2526 BEGIN
2527    IF ADR(z) MOD 1024=0
2528    THEN
2529       exit(0)
2530    ELSE
2531       exit(1)
2532    END
2533 END align6.
2534 @end example
2536 @node Packed, Built-ins, Alignment, Using
2537 @section Packing data types
2539 The pragma @code{<* bytealignment(0) *>} can be used to specify that
2540 the fields within a @code{RECORD} are to be packed.  Currently this
2541 only applies to fields which are declared as subranges, ordinal types
2542 and enumerated types.  Here is an example of how two subranges might
2543 be packed into a byte.
2545 @example
2546 TYPE
2547    bits3c =  [0..7] ;
2548    bits3i = [-4..3] ;
2550    byte = RECORD
2551               <* bytealignment(0) *>
2552               x: bits3c ;
2553               <* bitsunused(2) *>
2554               y: bits3i ;
2555           END ;
2556 @end example
2558 Notice that the user has specified that in between fields @code{x} and
2559 @code{y} there are two bits unused.
2561 Now the user wishes to create a record with byte numbers zero and one
2562 occupied and then an @code{INTEGER32} field which is four byte
2563 aligned.  In this case byte numbers two and three will be unused.  The
2564 pragma @code{bytealignment} can be issued at the start of the record
2565 indicating the default alignment for the whole record and this can be
2566 overridden by individual fields if necessary.
2568 @example
2569    rec = RECORD
2570             <* bytealignment (1) *> ;
2571             a, b: byte ;
2572             x: INTEGER32 <* bytealignment(4) *> ;
2573          END ;
2574 @end example
2576 In the following example the user has specified that a record has two
2577 fields @code{p} and @code{q} but that there are three bytes unused between
2578 these fields.
2580 @example
2581    header = RECORD
2582                <* bytealignment(1) *>
2583                p: byte ;
2584                <* bytesunused(3) *>
2585                q: byte ;
2586             END ;
2587 @end example
2589 The pragma @code{<* bytesunused(x) *>} can only be used if the current
2590 field is on a byte boundary.  There is also a @code{SYSTEM} pseudo
2591 procedure function @code{TBITSIZE(T)} which returns the minimum number of
2592 bits necessary to represent type @code{T}.
2594 Another example of packing record bit fields is given below:
2596 @example
2597 MODULE align21 ;
2599 FROM libc IMPORT exit ;
2601 TYPE
2602    colour = (red, blue, green, purple, white, black) ;
2604    soc = PACKEDSET OF colour ;
2606    rec = RECORD
2607             <* bytealignment(0) *>
2608             x: soc ;
2609             y: [-1..1] ;
2610          END ;
2613    r: rec ;
2614    v: CARDINAL ;
2615 BEGIN
2616    v := SIZE(r) ;
2617    IF SIZE(r)#1
2618    THEN
2619       exit(1)
2620    END ;
2621    r.x := soc@{blue@} ;
2622    IF r.x#soc@{blue@}
2623    THEN
2624       exit(2)
2625    END
2626 END align21.
2627 @end example
2629 Here we see that the total size of this record is one byte and consists
2630 of a six bit set type followed by a 2 bit integer subrange.
2632 @node Built-ins, The PIM system module, Packed, Using
2633 @section Accessing GNU Modula-2 Built-ins
2635 This section describes the built-in constants and functions defined in
2636 GNU Modula-2.  The following compiler constants can be accessed using
2637 the @code{__ATTRIBUTE__} @code{__BUILTIN__} keywords.  These are not
2638 part of the Modula-2 language and they may differ depending upon the
2639 target architecture but they provide a method whereby common
2640 libraries can interface to a different underlying architecture.
2642 The built-in constants are: @code{BITS_PER_UNIT}, @code{BITS_PER_WORD},
2643 @code{BITS_PER_CHAR} and @code{UNITS_PER_WORD}.  They are integrated into
2644 GNU Modula-2 by an extension to the @code{ConstFactor} rule:
2646 @example
2647 ConstFactor := ConstQualidentOrSet | Number | ConstString |
2648                "(" ConstExpression ")" | "NOT" ConstFactor |
2649                ConstAttribute =:
2651 ConstAttribute := "__ATTRIBUTE__" "__BUILTIN__" "(" "(" Ident ")" ")" =:
2652 @end example
2654 Here is an example taken from the ISO library @code{SYSTEM.def}:
2656 @example
2657 CONST
2658    BITSPERLOC    = __ATTRIBUTE__ __BUILTIN__ ((BITS_PER_UNIT)) ;
2659    LOCSPERWORD   = __ATTRIBUTE__ __BUILTIN__ ((UNITS_PER_WORD)) ;
2660 @end example
2662 Built-in functions are transparent to the end user.  All built-in
2663 functions are declared in @code{DEFINITION MODULE}s and are imported
2664 as and when required.  Built-in functions are declared in definition
2665 modules by using the @code{__BUILTIN__} keyword.  Here is a section of
2666 the ISO library @code{LongMath.def} which demonstrates this feature.
2668 @example
2669 PROCEDURE __BUILTIN__ sqrt (x: LONGREAL): LONGREAL;
2670   (* Returns the square root of x *)
2671 @end example
2673 This indicates that the function @code{sqrt} will be implemented using
2674 the gcc built-in maths library.  If gcc cannot utilize the built-in
2675 function (for example if the programmer requested the address of
2676 @code{sqrt}) then code is generated to call the alternative function
2677 implemented in the @code{IMPLEMENTATION} @code{MODULE}.
2679 Sometimes a function exported from the @code{DEFINITION} @code{MODULE}
2680 will have a different name from the built-in function within gcc.  In
2681 such cases the mapping between the GNU Modula-2 function name and the
2682 gcc name is expressed using the keywords @code{__ATTRIBUTE__}
2683 @code{__BUILTIN__} @code{((Ident))}.  For example the function
2684 @code{sqrt} in @code{LongMath.def} maps onto the gcc built-in function
2685 @code{sqrtl} and this is expressed as:
2687 @example
2688 PROCEDURE __ATTRIBUTE__ __BUILTIN__ ((sqrtl)) sqrt
2689                                     (x: LONGREAL) : LONGREAL;
2690   (* Returns the positive square root of x *)
2691 @end example
2693 The following module @code{Builtins.def} enumerates the list of
2694 built-in functions which can be accessed in GNU Modula-2.  It also
2695 serves to define the parameter and return value for each function:
2697 @include m2/Builtins.texi
2699 Although this module exists and will result in the generation of
2700 in-line code if optimization flags are passed to GNU Modula-2, users
2701 are advised to utilize the same functions from more generic libraries.
2702 The built-in mechanism will be applied to these generic
2703 libraries where appropriate.  Note for the mathematical routines to
2704 be in-lined you need to specify the @samp{-ffast-math -O} options.
2706 @node The PIM system module, The ISO system module, Built-ins, Using
2707 @section The PIM system module
2709 @include m2/SYSTEM-pim.texi
2711 The different dialects of Modula-2 PIM-[234] and ISO Modula-2 declare
2712 the function @code{SIZE} in different places.  PIM-[34] and ISO
2713 Modula-2 declare @code{SIZE} as a pervasive function (declared in the
2714 base module).  PIM-2 defined @code{SIZE} in the @code{SYSTEM} module
2715 (as shown above).
2717 GNU Modula-2 allows users to specify the dialect of Modula-2 by using
2718 the @code{-fiso} and @code{-fpim2} command line switches.
2720 The data types @code{CSIZE_T} and @code{CSSIZE_T} are also exported from
2721 the @code{SYSTEM} module.  The type @code{CSIZE_T} is unsigned and is
2722 mapped onto the target C data type @code{size_t} whereas the type
2723 @code{CSSIZE_T} is mapped onto the signed C data type @code{ssize_t}.
2725 It is anticipated that these should only be used to provide cross
2726 platform definition modules for C libraries.
2728 There are also a variety of fixed sized @code{INTEGER} and
2729 @code{CARDINAL} types.  The variety of the fixed sized types will
2730 depend upon the target architecture.
2732 @node The ISO system module, , The PIM system module, Using
2733 @section The ISO system module
2735 @include m2/SYSTEM-iso.texi
2737 The data types @code{CSIZE_T} and @code{CSSIZE_T} are also exported from
2738 the @code{SYSTEM} module.  The type @code{CSIZE_T} is unsigned and is
2739 mapped onto the target C data type @code{size_t} whereas the type
2740 @code{CSSIZE_T} is mapped onto the signed C data type @code{ssize_t}.
2742 It is anticipated that these should only be used to provide cross
2743 platform definition modules for C libraries.
2745 There are also a variety of fixed sized @code{INTEGER} and
2746 @code{CARDINAL} types.  The variety of the fixed sized types will
2747 depend upon the target architecture.
2749 @node License, Copying, The ISO system module, Top
2750 @section License of GNU Modula-2
2752 GNU Modula-2 is free software, the compiler is held under the GPL v3
2753 @uref{http://www.gnu.org/licenses/gpl.txt},
2754 its libraries (pim, iso and Logitech compatible) are under the
2755 GPL v3 with the GCC run time library exception clause.
2757 Under Section 7 of GPL version 3, you are granted additional
2758 permissions described in the GCC Runtime Library Exception, version
2759 3.1, as published by the Free Software Foundation.
2761 You should have received a copy of the GNU General Public License and
2762 a copy of the GCC Runtime Library Exception along with this program;
2763 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2764 <http://www.gnu.org/licenses/>.
2766 More information on how these licenses work is available
2767 @uref{http://www.gnu.org/licenses/licenses.html} on the GNU web site.
2769 @c Copying node is inside the gpl_v3.texi
2770 @include gpl_v3.texi
2772 @node Contributing, Internals, Copying, Top
2773 @section Contributing to GNU Modula-2
2775 Please do and please read the GNU Emacs info under
2777 @example
2778 * Standards: (standards).       GNU coding standards.
2779 * Intellectual Property::       Keeping Free Software Free
2780 * Reading Non-Free Code::       Referring to Proprietary Programs
2781 * Contributions::               Accepting Contributions
2782 @end example
2784 You might consider joining the GM2 Mailing list before you start
2785 coding.  The mailing list may be subscribed via a web interface
2786 @uref{http://lists.nongnu.org/mailman/listinfo/gm2} or via email
2787 @email{gm2-subscribe@@nongnu.org}.
2789 Many thanks and enjoy your coding!
2791 @node Internals, EBNF, Contributing, Top
2793 This section is still being written.
2794 @c @include gm2-internals.texi
2796 @node EBNF, Libraries, Internals, Top
2797 @chapter EBNF of GNU Modula-2
2799 This chapter contains the EBNF of GNU Modula-2.  This grammar currently
2800 supports both PIM and ISO dialects.  The rules here are automatically
2801 extracted from the crammer files in GNU Modula-2 and serve to document
2802 the syntax of the extensions described earlier and how they fit in
2803 with the base language.
2805 Note that the first six productions are built into the lexical analysis
2806 phase.
2808 @include m2/gm2-ebnf.texi
2810 @node Libraries, Indices, EBNF, Top
2811 @chapter PIM and ISO library definitions
2813 This chapter contains M2F, PIM and ISO libraries.
2815 @include m2/gm2-libs.texi
2817 @node Indices, , Libraries, Top
2818 @section Indices
2820 @ifhtml
2821 @menu
2822 * Contents::    Section and subsections.
2823 * Functions::   Function, constants, types, ebnf indices.
2824 @end menu
2826 @node Contents, Functions, ,
2827 @section Section and subsections
2828 @printindex cp
2830 @node Functions, , Contents,
2831 @section Function, constants, types, ebnf indices.
2832 @end ifhtml
2834 @printindex fn
2836 @summarycontents
2837 @contents
2838 @bye