[comp.lang.modula2] Initialisation code: When is it called?

warwick@batserver.cs.uq.oz.au (Warwick Allison) (01/29/91)

"A.def" exports a procedure "Do":

PROCEDURE Do(x:PROC);
(* Adds the given procedure to a list of procedures, all of which
   will be executed when user does... *)

"B.def" contains nothing.

"B.mod" has the following initialisation code:

BEGIN
	Do(SayHello);
END B.


Should a Modula-2 compiler know to include this module (I guess
it would have to be explicitly linked), and when would the init
code be executed?

Thanks very much, in advance.
--
________________________________________________________

	This .signature intentionally left blank
________________________________________________________

RW_GRIFFITHS@VAX.ACS.OPEN.AC.UK (01/29/91)

The importation of a module causes the imported module to be initialized as
the program (that imports the module) begins to execute. Since many different
modules may be imported by many others (in a large program etc), it is necessary
 to give an ordering for the initialization. This order is the one in which
they are textually listed in the import statement. The compilere works out the
orderuing


 by examing the main program module first and then works backwards. IF
there is a circularity in the initialization order ( ie two modules importing
from each other) then strictly speaking the order is undefined; but most
 compilers can resolve this problem. For example TopSpeed M2 initializes in an
 order
staring with the last module in the circular reference list, before repetitio
n occurs. Eg. in the case of A importing B, B importing C and C importing A,
the initialization order would be C, then B then A. However remember that
this will not be portable to other compilers.
Finally there is another rare case
hen compilers can produce different
initialization orders for the same program. This occurs when a module is
 imported into a definition module and then not used in the corresponding
 implementationmodule. In such cases some compilers do not initialize the
 imported module.

Rob Griffiths
The Open University

news@m2xenix.psg.com (Randy Bush) (01/31/91)

RW_GRIFFITHS@VAX.ACS.OPEN.AC.UK writes:

> Finally there is another rare case hen compilers can produce different
> initialization orders for the same program. This occurs when a module is
> imported into a definition module and then not used in the corresponding
> implementationmodule. In such cases some compilers do not initialize the
> imported module.

Given the following example, should we consider such programs Modula-2
compilers?

DEFINITION MODULE A;
EXPORT QUALIFIED p;
PROCEDURE p () : CARDINAL;
END A.

IMPLEMENTATION MODULE A;
VAR v : CARDINAL;
PROCEDURE p () : CARDINAL;
BEGIN
   RETURN (v)
   END p;
BEGIN
   v := 42
   END A.

DEFINITION MODULE B;
IMPORT A;
EXPORT p;
CONST p = A.p;
END B.

IMPLEMENTATION MODULE B;
END B.

MODULE Test;
IMPORT B;
BEGIN
   REPEAT
      UNTIL B.p() = 42
   END Test.

-- 
..!{uunet,qiclab,intelhf,bucket}!m2xenix!news