TweetFollow Us on Twitter

Modula-2 Compiler
Volume Number:1
Issue Number:6
Column Tag:Modula-2

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.

MODULE Matmul;

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;

BEGIN
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,
                  WindowPtr(behind),TRUE,WrefCon);
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 
 END
END;
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
 END
END;

WriteString (“Multiplication completed.”); WriteLn;
WriteString (“ C[15,16] = “); GWriteReal (c[15][16],10);
ReadReal(p)

END Matmul.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.