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

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.