Modula-2 Compiler by Modula Corporation
By Jörg Langowski
The almost released beta version of the Modula-2 compiler by Modula Corporation has been around for some time now on several peoples desks, including mine. I feel it is time to report some of my experiences with this product.
Modula 2 is in some way the successor of Pascal, created by the same author, Niklaus Wirth from Zürich. Simple Modula 2 programs almost look like Pascal programs. Supposedly some of the inconveniences of Pascal have been straightened out in Modula 2, e.g., the rigid order in which declarations are made in Pascal has been relaxed. The most important new concept is that of modular compilation, which means that external (library) definitions can be imported from separately compiled modules at compile time and checked for correct passing of parameters, type mismatches etc
But I do not want to talk about language differences here. There has been a whole issue of BYTE dealing with Modula recently, and if you plan to use the Modula-2 compiler, you will have to get the standard reference book, Programming in Modula-2, by Niklaus Wirth, anyway.
The Modula-2 version that I received from Modula Corporation came on one Macintosh disk with a three-ring binder manual. The disk contains:
- The Modula-2 compiler and linker. Both are compiled Modula-2 programs, which means (for this system) that they are not native 68000 code, but rather M-code for a virtual machine, which has to be interpreted.
- The interpreter. The disk contains two versions, one of which is used to run the compiler and linker, the other one to run your compiled Modula-2 program.
- System libraries with precompiled modules for I/O, file handling, number conversion, string manipulation and - most important of all - toolbox access.
- a Decode utility to disassemble M-code. I found this of little use since I had no definition of the M-code instructions available.
- source code of a Graphics Demo and the Edit sample application that is also part of the IM manual.
- Edit, the same editor as in the Asm/Edit system.
- Rmaker, the (in)famous resource maker.
The manual gives a rather concise introduction on how to use the compiler and linker if you want to write a simple program or create a separately compiled module. Beside these instructions, it contains a listing of all the predefined modules that come with the system.
If you want to write a program that does just simple I/O and do not care about the user interface, you can use the standard GrafPort that the system comes up with when you start your application. Unfortunately, this is the full screen, not a nicely set up window like some other compilers have. You merely get a blank screen terminal emulation; if you want to use a window, you will have to define it through toolbox calls. You might want to take a look at the sample program in Listing 1 now.
This is a short matrix multiplication routine that initializes two matrices and determines the product matrix, then types out one element of the product as a check. You see that the actual calculation comprises about 25% of the total program, the rest is definitions and setting up of the window. Having read some of the other examples in MacTutor, you might not be surprised at that kind of overhead. The main parts of the program are:
- the IMPORT declarations, which get routines out of precompiled modules; note that not even simple I/O is defined by default, you have to import procedures for any kind of I/O that you want to do.
- some TYPE and VAR declarations - similar to Pascal. Integers (signed) and Cardinals (unsigned) are 16 bit by default. If you need a 32 bit integer, you have to use the LongCard TYPE, which is a record with the fields h and l, both CARDINAL. You have to assign them separately.
- toolbox PROCEDURE declarations. Unfortunately, there is no way to access a toolbox routine through its trap number directly; the routines are called (presumably) through numbers in another module, and the definitions that you have to use are given in the manual. Most of the toolbox routines (no - not all of them) are included. The definitions make as much sense to me as they make to you, but they seem to work.
- the actual program. You will notice there that the string format has to be converted from Modula (zero byte at the end) to Macintosh (character count in front) format.
The program is edited using the well-known Edit program, then passed as a file with the extension .MOD to the compiler. Compilation of this sample program takes 2 min 58 secs, add to that 25 secs to bring up the compiler. The reason for the system being so slow is of course that the compiler is written in M-code, too, and is interpreted. You quickly learn to examine your code very carefully for errors before you call up the compiler! One rather annoying feature of Modula-2 did not make things easier, either: Upper and lower case are distinguished, and it makes a difference whether you write WindowPtr or windowptr. Oh well Debugging the example, after it almost ran, took me one hour, most of that time waiting for the compiler to finish.
The compiled program is then linked with the library modules (no linker errors here, since external references are already checked for completeness by the compiler) and you get a .LOD file, which, when double clicked, is executed by the Modula runtime interpreter.
Even though the compiler is slow, the generated code is rather fast. The example takes 21 seconds to execute. This is faster as you can ever do with the built-in 80-bit floating point package (judged from a similar program in Forth, which took over 40 seconds, most of that being floating point time). Modula-2 has its own 32-bit floating point package built in, which is precise enough for most applications. The Sieve runs 9 seconds per iteration, making a total of 90 secs for the standard 10 iterations. This, in turn, is slower than Forth (probably because the ratio M-code to built-in 68000 routines is higher in this case).
The timing results, in my opinion, make the Modula-2 system a very satisfactory choice for scientific and engineering applications. If Modula Corporation could come up with a compiler written in machine code, program development would be much more fun, too.
FROM InOut IMPORT ReadCard, WriteCard, WriteString, WriteLn;
FROM Terminal IMPORT ClearScreen;
FROM RealInOut IMPORT ReadReal, GWriteReal;
FROM QuickDrawTypes IMPORT Rect, GrafPtr, Str255, LongCard;
FROM ToolBoxTypes IMPORT WindowPtr, WindowRecord;
FROM Strings IMPORT StrModToMac;
FROM SYSTEM IMPORT ADDRESS;
CONST CX = 355B; QuickDrawModNum = 1; ToolBoxModNum = 2;
TYPE Ptr = ADDRESS;
VAR i,j,k : CARDINAL;
sum,p,q : REAL;
a : ARRAY [1..20] OF ARRAY [1..30] OF REAL;
b : ARRAY [1..30] OF ARRAY [1..20] OF REAL;
c : ARRAY [1..30] OF ARRAY [1..30] OF REAL;
Wbounds : Rect;
Wtitle : Str255;
Wnew : WindowPtr;
WrefCon, behind, OnHeap: LongCard;
PROCEDURE SetPort (port : WindowPtr);
CODE CX; QuickDrawModNum; 4 END SetPort;
PROCEDURE PortSize (width, height : INTEGER);
CODE CX; QuickDrawModNum; 8 END PortSize;
PROCEDURE TextFont (font : INTEGER);
CODE CX; QuickDrawModNum; 32 END TextFont;
PROCEDURE TextSize (size : INTEGER);
CODE CX; QuickDrawModNum; 35 END TextSize;
PROCEDURE NewWindow (wStorage : Ptr; boundsRect : Rect;
title : Str255; visible : BOOLEAN;
theProc:INTEGER; behind : WindowPtr;
goAwayFlag:BOOLEAN; refCon:LongCard) : WindowPtr;
CODE CX; ToolBoxModNum; 50 END NewWindow;
PROCEDURE DrawGrowIcon (theWindow : WindowPtr);
CODE CX; ToolBoxModNum; 64 END DrawGrowIcon;
PROCEDURE SelectWindow (theWindow : WindowPtr);
CODE CX; ToolBoxModNum; 56 END SelectWindow;
WITH WrefCon DO h := 0; l := 0 END;
WITH behind DO h := 65535; l := 65535 END;
WITH OnHeap DO h := 0; l := 0 END;
WITH Wbounds DO top := 50; left := 20; bottom := 300; right := 500 END;
StrModToMac (Wtitle, Demo Window); TextFont(0); TextSize(12);
Wnew := NewWindow(Ptr(OnHeap),Wbounds,Wtitle,TRUE,0,
SetPort(Wnew); SelectWindow(Wnew); DrawGrowIcon(Wnew);
PortSize(464,234); ClearScreen; TextFont(3); TextSize(9);
WriteString(Initialization values for a: ); ReadReal(p);
WriteString(, and b: ); ReadReal(q); WriteLn;
FOR i:= 1 TO 20 DO
FOR j:= 1 TO 30 DO
a[i][j] := p; b[j][i] := q
WriteString (Initialization done.); WriteLn;
FOR i:= 1 TO 30 DO
FOR j:= 1 TO 30 DO
sum := 0.0;
FOR k := 1 TO 20 DO sum:=sum + a[k][i]*b[j][k] END;
c[i][j] := sum
WriteString (Multiplication completed.); WriteLn;
WriteString ( C[15,16] = ); GWriteReal (c,10);