home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / M2V11-1.LHA / modula / docs / EXTENSIONS.DOC < prev    next >
Encoding:
Text File  |  1994-09-28  |  15.9 KB  |  483 lines

  1.              Compiler Extensions
  2.              ===================
  3.  
  4.     The compiler has been extended with extra features. Most of which are
  5.     designed to make interfacing with the amiga operating system easier.
  6.     Other features are designed to make programming in Modula-2 less annoying.
  7.     Avoid using these extensions if you wish to develop portable modules.
  8.  
  9. Underscore
  10. ==========
  11.  
  12.     The underscore is a valid 'letter'.
  13.     eg, '_Turbo_Modula_2' is a legal identifier.
  14.  
  15. SHORTCARD, SHORTINT AND SHORTREAL
  16. =================================
  17.  
  18.     The types SHORTINT, SHORTCARD & SHORTREAL are not normally
  19.     predeclared in other Modula-2 compilers:
  20.  
  21.     SHORTCARD is predeclared as [0..255]    (* byte sized cardinal *)
  22.     SHORTINT  is predeclared as [-128..127] (* byte sized integer  *)
  23.     SHORTREAL Motorola fast floating point.
  24.  
  25. Conversion Rules
  26. ================
  27.  
  28.     Signed (integer) and unsigned(cardinal) numeric subrange types may be freely
  29.     mixed in expressions. The following conversion rules apply:
  30.  
  31.     VAR
  32.       shortint  : SHORTINT  ;
  33.       shortcard : SHORTCARD ;
  34.       integer   : INTEGER ;
  35.       ...
  36.  
  37.     shortint+shortcard   => VAL(INTEGER,shortint)+VAL(INTEGER,shortcard)
  38.     shortint+cardinal    => VAL(LONGINT,shortint)+VAL(LONGINT,cardinal)
  39.  
  40.     shortint+integer     => VAL(INTEGER,shortint)+integer
  41.     shortint+longint     => VAL(LONGINT,shortint)+longint
  42.  
  43.     shortcard+cardinal     => VAL(CARDINAL,shortcard)+cardinal
  44.     shortcard+integer     => VAL(INTEGER,shortcard)+integer
  45.     shortcard+longint    => VAL(LONGINT,shortcard)+longint
  46.  
  47.     integer+cardinal     => VAL(LONGINT,integer)+VAL(LONGINT,cardinal)
  48.  
  49.     integer+longint     => VAL(LONGINT,integer)+longint
  50.  
  51.     cardinal+longint     => VAL(LONGINT,cardinal)+longint
  52.  
  53.     Where '+' could be any binary operator.
  54.     The result type in each conversion should be obvious.
  55.  
  56. Qualified Import aliasing
  57. =========================
  58.  
  59.     An imported global module name may be aliased.
  60.  
  61.     IMPORT Graphics , Intuition ;
  62.  
  63.     VAR
  64.       rp  : Graphics.RastPortPtr ;
  65.       win : Intuition.WindowPtr ;
  66.  
  67.     Can be rewritten:
  68.  
  69.     IMPORT G := Graphics , I := Intuition ;
  70.  
  71.     VAR
  72.       rp  : G.RastPortPtr ;
  73.       win : I.WindowPtr ;
  74.  
  75. Imported library versions
  76. =========================
  77.  
  78.     When importing an Amiga library module ( Dos, Intuition, Graphics etc ),
  79.     an optional version number can be specified:
  80.  
  81.     IMPORT Intuition{33} ;
  82.  
  83.     FROM Dos{36} IMPORT System ;
  84.  
  85.     The values 33 & 36 will be passed to Exec.OpenLibrary by Intuition.o & Dos.o
  86.     If no version number is specified, zero is assumed.
  87.  
  88.     Intuition.o, Dos.o etc will cache the highest version opened so far
  89.     in order to reduce calls to Exec.OpenLibrary.
  90.  
  91.     The version number must be a decimal literal (not an arbitrary expression)
  92.     this simplifies the parser in programs like M2B.
  93.  
  94. Structure assignment
  95. ====================
  96.  
  97.     It is possible to assign to an array or record structure in a single
  98.     statement, the right hand side being a list of bracketed expressions.
  99.  
  100.     The syntax of the assignment statement is extended to:
  101.  
  102.     $ assignment  =  designator ":=" ass_rhs .
  103.     $ ass_rhs  = "["[asslist]"]" | expression.
  104.     $ asslist = ass_rhs{,ass_rhs}
  105.  
  106.     VAR x : ARRAY [0..6] OF INTEGER ;
  107.  
  108.     x := [f(y),2,4,a,0,0,0] ; (* The expressions need NOT be constant *)
  109.  
  110.     Trailing elements/fields need not be assigned to,in which case they will
  111.     be zeroed.Any automatically inserted compiler padding fields will also be 0.
  112.  
  113.     x := [f(y),2,4,a] ; (* same as above *)
  114.  
  115.     If a record contains variant fields then the compiler assumes (and checks
  116.     against) the first variant.In other words you can only assign to the
  117.     first variant field(s).
  118.  
  119.     examples:
  120.  
  121.     VAR
  122.       matrix  = ARRAY [0..3],[0..2],[0..2] OF LONGINT  ;
  123.  
  124.     PROCEDURE InitMatrix ;
  125.     BEGIN
  126.       matrix:=[[[7FFFH,   0,   0],[    0,7FFFH,   0],[    0,    0,7FFFH]],
  127.            [[32642,   0,2856],[    0,7FFFH,   0],[-2856,    0,32642]],
  128.            [[32642,2856,   0],[-2856,32642,   0],[    0,    0,7FFFH]],
  129.            [[7FFFH,   0,   0],[    0,32642,2856],[    0,-2856,32642]]] ;
  130.     END InitMatrix ;
  131.  
  132.     TYPE
  133.       NewMenu = RECORD (* Gadtools structure *)
  134.         nm_Type         : SHORTCARD ;
  135.         CASE : INTEGER OF
  136.         | 0 : nm_Label     : STRING   ;
  137.         | 1 : nm_Image     : ImagePtr ;
  138.         END ;
  139.         nm_CommKey     : STRING  ;
  140.         nm_Flags     : BITSET  ;
  141.         nm_MutualExclude : LONGINT ;
  142.         nm_UserData     : ADDRESS ;
  143.       END ;
  144.  
  145.     VAR
  146.       demomenu : ARRAY [0..11] OF NewMenu ;
  147.  
  148.     BEGIN
  149.       demomenu :=
  150.          [
  151.           [ NM_TITLE,"Project" ],
  152.       [ NM_ITEM, "Run",               "R", {}, 0, MENU_RUN   ],
  153.       [ NM_ITEM, "Step",              "S", {}, 0, MENU_STEP  ],
  154.       [ NM_ITEM, NM_BARLABEL ],
  155.       [ NM_ITEM, "Slower Horizontal", "1", {}, 0, MENU_HSLOW ],
  156.       [ NM_ITEM, "Faster Horizontal", "2", {}, 0, MENU_HFAST ],
  157.       [ NM_ITEM, "Slower Vertical",   "3", {}, 0, MENU_VSLOW ],
  158.       [ NM_ITEM, "Faster Vertical",   "4", {}, 0, MENU_VFAST ],
  159.       [ NM_ITEM, NM_BARLABEL ],
  160.       [ NM_ITEM, "Quit",              "Q", {}, 0, MENU_QUIT  ],
  161.        [ NM_END ]
  162.       ] ;
  163.  
  164. Open array heap variables
  165. =========================
  166.  
  167.     In Modula-2 it is possible to declare open arrays only as formal parameters.
  168.     The compiler allows open arrays to be declared as pointer base types.
  169.  
  170.     VAR
  171.       p : POINTER TO ARRAY OF CHAR ;
  172.  
  173.     Like formal parameters, the open array must be 1 dimensional.
  174.  
  175.     The NEW substitution mechanism has to be extended to allow declarations
  176.     of such heap variables.
  177.  
  178.     NEW(p,exp) ; where exp is an integer/cardinal expression.
  179.  
  180.     This expands to ALLOCATE( p , exp*SIZE(p^[0]));
  181.  
  182.     There is no bounds checking associated with this type of array.
  183.     It is not possible to do any operation on the open array,only individual
  184.     elements may be accessed:
  185.  
  186.       The designator p^ may never appear on its own, the '^' must always be
  187.       followed by a '[' eg p^[10].
  188.  
  189.     If the follow declarations exist:
  190.  
  191.       VAR
  192.         x : POINTER TO ARRAY OF type ;
  193.         y : ARRAY [low..hi] OF type ; (* normal array *)
  194.  
  195.     Then the assignment
  196.  
  197.      x := y ; is allowed. This is equivalent to
  198.      x := ADR( y ) ;
  199.  
  200.      If the base type of the open array(x^[0]) is CHAR then,
  201.  
  202.      x := "help" ;      is allowed and is equivalent to
  203.      x := ADR("help") ;
  204.  
  205.      The SYSTEM.STRING type is predeclared as POINTER TO ARRAY OF CHAR.
  206.      The array pointed to by a STRING variable should always be 0C terminated.
  207.  
  208.  
  209.      This kind of pointer occurs in the C language, and so is quite useful
  210.      when programming with the amiga OS and the Ansi C libraries. However it is
  211.      not true to Modula's strong typing philosophy and therefore its
  212.      indiscriminate use should be avoided.
  213.  
  214. Pointer casting in selector lists
  215. =================================
  216.  
  217.     It is possible to insert type casts in variable selector lists:
  218.  
  219.     foo := bar(NewMenuPtr)^.nm_Label(ImagePtr) ;
  220.  
  221.     The syntax of the designator is extended to:
  222.  
  223.     $ designator = qualident {"."ident|"["ExpList"]"|"("qualident")"| "^" }.
  224.  
  225.     This method of casting is restricted to (and from) pointer types.
  226.     It is very useful when using Intuitions BOOPSI system.
  227.  
  228. Variable length argument lists
  229. ==============================
  230.  
  231.     A procedure may be declared to take a variable number of arguments.
  232.     To declare such a procedure, the formal parameter list must end with '..':
  233.  
  234.     PROCEDURE VarArgsProc( x : INTEGER ; .. ) ;
  235.     (* There must be at least 1 normal formal parameter *)
  236.  
  237.     There is no standard way of accessing the unknown parameters from Modula-2.
  238.     This kind of declaration normally occur in Amiga OS & C interface modules.
  239.     However you can declare them in implementation & program modules:
  240.  
  241.     PROCEDURE CreateGad( kind:LONGINT; VAR ng:GT.NewGadget; tag1:LONGINT; .. ) ;
  242.     (* topazAttr, visualInfo, window, lastAdded are global variables *)
  243.     BEGIN
  244.       ng.ng_TextAttr    := ADR( topazAttr ) ;
  245.       ng.ng_VisualInfo  := visualInfo ;
  246.       INC( ng.ng_LeftEdge, window^.BorderLeft ) ;
  247.       INC( ng.ng_TopEdge , window^.BorderTop  ) ;
  248.  
  249.       lastAdded := GT.CreateGadgetA( kind, lastAdded, ng, ADR( tag1 ) )
  250.     END CreateGad ;
  251.  
  252.     If a SHORTREAL or REAL actual corresponds to a '..' formal
  253.     then the parameter is converted to a LONGREAL before being passed.
  254.  
  255. Newline and tab characters
  256. ==========================
  257.  
  258.     String literals may contain newline (\n) & tab(\t) characters.
  259.  
  260.     InOut.WriteString("Hello\tworld\n");
  261.  
  262.     If '\' in a string is not followed by one of 't' 'n' '\' then the compiler
  263.     will report an error.
  264.     To represent '\' use '\\'.
  265.  
  266.     "\n" & "\t" are also legal character constants as well as strings.
  267.  
  268. Exit codes
  269. ==========
  270.  
  271.     A return statement inside the initialization statement sequence of the root
  272.     program module may contain an optional integer expression. This expression,
  273.     if executed, will represent the return code for the program.
  274.     If no expression is supplied, or if execution falls through, 0 is returned.
  275.  
  276.     MODULE foo ;
  277.      ...
  278.     BEGIN
  279.       RETURN 20
  280.     END foo.
  281.  
  282.     The standard AmigaDOS return codes are:
  283.  
  284.     00: program was successful.
  285.     10: warning (non fatal 'error').
  286.     20: fatal error.
  287.  
  288.     Alternatively StdLib.exit(X), will terminate the program with return code X.
  289.  
  290. Coroutines
  291. ==========
  292.  
  293.     The pseudo module SYSTEM does not contain any coroutine facilities,
  294.     instead the library module 'Coroutines' should be used.
  295.  
  296. SHORTSET BITSET & LONGSET constants
  297. ===================================
  298.  
  299.     The compiler predefines 3 set types.
  300.  
  301.     SHORTSET = SET OF [0..07] ; SIZE = 1 Byte
  302.     BITSET   = SET OF [0..15] ; SIZE = 2 Byte
  303.     LONGSET  = SET OF [0..31] ; SIZE = 4 Byte
  304.  
  305.     The type of an unqualified set constant has been extended to support these
  306.     types.
  307.  
  308.        {}, {0}, {0,1} ... {0..7} are compatible with SHORTSET,BITSET & LONGSET
  309.        {8}, {8,9} ... {8..15} are compatible with BITSET & LONGSET
  310.        {16} .. {16..31} are only LONGSETs
  311.  
  312.        By compatible I mean both expression compatible & assignment compatible.
  313.  
  314.     VAR
  315.       shortset : SHORTSET ; bitset : BITSET ; longset  : LONGSET  ;
  316.  
  317.     shortset:= {1} (*valid*); shortset:= {8} (*wrong*); shortset:= {16}(*wrong*)
  318.     bitset  := {1} (*valid*); bitset  := {8} (*valid*); bitset  := {16}(*wrong*)
  319.     longset := {1} (*valid*); longset := {8} (*valid*); longset := {16}(*valid*)
  320.  
  321. INCL & EXCL
  322. ===========
  323.  
  324.     As well as the normal definitions of the standard procedures INCL/EXCL
  325.     the second parameter of these procedures may be a set expression.
  326.  
  327.     example:
  328.  
  329.       INCL(bitset,{1,2}) is identical to bitset := bitset+{1,2}.
  330.       EXCL(bitset,{1,2}) is identical to bitset := bitset-{1,2}.
  331.  
  332.       However the INCL/EXCL versions generate atomic code.
  333.  
  334. BCPL pointers
  335. =============
  336.  
  337.     The DOS operating subsystem was partly implemented in the BCPL programming
  338.     language, which used long word pointers,as opposed to normal byte pointers.
  339.     It is possible to declare a BCPL long word pointer in Turbo Modula-2
  340.  
  341.     TYPE
  342.       FileLockPtr = BCPL POINTER TO FileLock ; (* BCPL is a reserved keyword *)
  343.  
  344.     Instances of this pointer can be dereferenced as normal.
  345.  
  346.     The type SYSTEM.BADDRESS exists and is analogous to the normal
  347.     SYSTEM.ADDRESS type.
  348.  
  349.     If an assignment to a BCPL pointer from an ADDRESS typed expression occurs
  350.     then the compiler will automatically perform the necessary conversion:
  351.  
  352.     VAR lock : FileLockPtr ; v : FileLock ;
  353.  
  354.       lock := ADR(v) ; (* will work, but only if v is long word aligned *)
  355.  
  356.     The reverse assignment from a BADDRESS expression to a normal pointer will
  357.     also be converted properly.
  358.  
  359.     You must be careful not to cast between the 2 different kinds of pointers
  360.     as no conversion will take place. Instead there are 2 functions in the
  361.     SYSTEM module that can be used.
  362.  
  363.     PROCEDURE SYSTEM.BTOA( bp : BADDRESS ) : ADDRESS ;
  364.     PROCEDURE SYSTEM.ATOB(  p : ADDRESS  ) : BADDRESS ;
  365.  
  366.     The base variable of a BCPL pointer should always be long word aligned.
  367.     Modula-2 global variables are always longword aligned as is the memory
  368.     allocated using the StdLib.malloc & Storage.ALLOCATE functions.
  369.  
  370. SHORTFLOAT & LONGFLOAT
  371. ======================
  372.  
  373.     As well as the normal FLOAT conversion function, which always converts
  374.     to REAL, there are 2 other integer/cardinal->real conversion functions:
  375.  
  376.     SHORTFLOAT, converts an integer/cardinal into a SHORTREAL
  377.     LONGFLOAT , converts an integer/cardinal into a LONGREAL
  378.  
  379. Psuedo Module System
  380. ====================
  381.  
  382.     The following types are declared in SYSTEM:
  383.  
  384.     ADDRESS
  385.     BADDRESS    see above
  386.     BYTE
  387.     WORD
  388.     LONGWORD    like BYTE & WORD except 32-bits long
  389.  
  390.     SHORTSET    (also pervasive)
  391.     BITSET        (also pervasive)
  392.     LONGSET        (also pervasive)
  393.     SHORTREAL    (also pervasive)
  394.     FFP        Motoral Fast Floating Point (FFP=SHORTREAL)
  395.     STRING        see above
  396.  
  397.     Functions declared in SYSTEM:
  398.  
  399.     ADR
  400.     TSIZE
  401.     BTOA        see above
  402.     ATOB        see above
  403.  
  404.     OFFSET        OFFSET( recordType , field ) : LONGINT
  405.             Returns the byte offset of the field in the record.
  406.  
  407.     CAST        CAST( TYPEid,expression) : TYPEid
  408.             Performs a typecast: same as to TYPEid(expression).
  409.             eg str := CAST(STRING,98)
  410.                str := STRING(98)
  411.  
  412.     MAKEID        MAKEID( STRING ) : LONGINT
  413.             String should <= 4 characters.
  414.  
  415. Comment Options
  416. ===============
  417.  
  418.     Unnested comments in your source code can be used to override command line
  419.     options. The format is (* @X+ *) to enable an option or (* @X- *) to disable
  420.     it. These comments should normally be placed at the top of your code.
  421.  
  422.     The options are:
  423.  
  424.     @C(+/-)    Enable/Disable Large Code option.
  425.     @D(+/-)    Enable/Disable Large Data option.
  426.     @B(+/-)    Enable/Disable Array Bounds Checking.
  427.     @P(+/-)    Enable/Disable Pointer Checking.
  428.     @A(+/-)    Enable/Disable Alternative BSS naming convention.
  429.  
  430.     There are also two other options which relate to individual procedures:
  431.  
  432.     @G See examples/src/Hook.mod
  433.     @O See ansi-c/SetJmp.def
  434.  
  435. DEFINITION FOR C MODULE'S
  436. =========================
  437.  
  438.     To make it easy to interface with code written in the 'C' language it
  439.     is possible to declare non standard definition modules.
  440.     There are 3 closely related kinds:
  441.  
  442.     1.DEFINITION FOR C MODULE (eg StdLib.def)
  443.  
  444.       There is no corresponding implementation module.
  445.       This kind of definition module normally contains a list of declarations
  446.       which are defined in some C library or object file.
  447.       A normal Modula-2 module that wishes to use the functions/variables
  448.       declared in this kind of module simply imports them as normal.
  449.       To enable correct linking however, the library or object files must be
  450.       specified on the command line
  451.  
  452.       >m2b CallsX.mod X.o X.lib
  453.  
  454.       See examples/for_c/ for an example of calling a 'C' function.
  455.  
  456.       The library (c.lib) which contains all the function declared in
  457.       StdLib.def,CType.def etc is automatically included by DCC,
  458.       so we dont have to specify it on the command line.
  459.  
  460.     2.DEFINITION FOR AMIGALIB MODULE (eg Console.def)
  461.  
  462.       Mainly used to define AmigaDos devices (functions in amiga.lib).
  463.       Like FOR C except all variable and strings declarations declared in the
  464.       definition module are expected to be found in the coressponding Modula-2
  465.       object file. For the compiler to generate such an object file there must
  466.       be a corressonding implementation module,which is normally empty
  467.       eg Console.mod or opens the device).
  468.       The implementation module may contain procedure declaration which
  469.       should only impelement macros not coded in the object file or library
  470.       eg StdIO.mod .
  471.  
  472.     3.DEFINITION FOR LIBRARY MODULE (eg GadTools.def)
  473.  
  474.       Used to open and define AmigaDos libraries.
  475.       Identical to FOR AMIGALIB except when such a module is imported an
  476.       optional library version number can be specified (see line 75 ).
  477.       This version number can be read using the pervasive cardinal variable
  478.       'VERSION', for an example see GadTools.mod, and is noramlly passed to
  479.       to function OpenLib, which inturn calls Exec.OpenLibrary.
  480.       The initialisation body of an implementaion for library module must be
  481.       capable of being called more than once ie with different versions
  482.       arguments.
  483.