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

Players can take a peek into the design...
It doesn’t matter how much effort developers put into their classes, or how many special little mechanics there are; if there is one that wields two blades, I’m ignoring everything else. Diablo Immortal recently announced such a class in the shape... | Read more »
Android users have a new option in the c...
When you are in the thick of a firefight or trying to pull off a mid-combat parkour flip through a squad of foes, sometimes touchscreen control just won’t do it for you. For those intense sessions, you could benefit from a good mobile controller,... | Read more »
Jagex releases the first of three origin...
At this point, I am sure everyone has heard of Runescape, and or Runescape Classic. It has been going strong for 23 years, with constant content and story coming out. Luckily for fans of the game, or fantasy in general, Jagex has announced an... | Read more »
Watcher of Realms unveils new story and...
Watcher of Realms players are in for quite the feast this month, as Moonton release two powerful new heroes, including one that will burst down even the most mighty of foes. Recruit your new friends, and then burn through the Main Quest expansion... | Read more »
Reverse: 1999 continues its trip down un...
The field trip to Australia continues in Reverse: 1999 as Phase 2 of Revival! The Uluru Games kicks off. You will be able to collect new characters, engage with new events, get hordes of free gifts, and follow the story of a mushroom-based... | Read more »
Ride into the zombie apocalypse in style...
Back in the good old days of Flash games, there were a few staples; Happy Wheels, Stick RPG, and of course the apocalyptic driver Earn to Die. Fans of the running over zombies simulator can rejoice, as the sequel to the legendary game, Earn to Die... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Netflix Games expands its catalogue with...
It is a good time to be a Netflix subscriber this month. I presume there's a good show or two, but we are, of course, talking about their gaming service that seems to be picking up steam lately. May is adding five new titles, and there are some... | Read more »
Pokemon Go takes a step closer to real P...
When Pokemon Go was first announced, one of the best concepts of the whole thing was having your favourite Pokemon follow you in the real world and be able to interact with them. To be frank, the AR Snapshot tool could have done a lot more to help... | Read more »
Seven Knights Idle Adventure drafts in a...
Seven Knights Idle Adventure is opening up more stages, passing the 15k mark, and players may find themselves in need of more help to clear these higher stages. Well, the cavalry has arrived with the introduction of the Legendary Hero Iris, as... | Read more »

Price Scanner via MacPrices.net

New May Verizon promotion: Switch and get a f...
Red Hot Deal Days at Verizon: Switch to Verizon this month, and get the 256GB iPhone 15 Pro for free, with trade-in, when you add a new line of service. Verizon is also offering a free cellular iPad... 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
13-inch M2 MacBook Airs on sale for only $849...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for $150 off Apple’s new MSRP, only $849. Free 1-2 day delivery is available to most US addresses. Their... Read more
13-inch M3 MacBook Airs on sale starting at $...
Amazon has every configuration and color of Apple’s 13″ M3 MacBook Air on sale for $150 off MSRP, now starting at $949 shipped. Their prices are the lowest available for these Airs among Apple’s... Read more
14-inch M3 Pro/Max MacBook Pro available toda...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Apple has the Apple Watch Ultra available for...
Apple has several Certified Refurbished Apple Watch Ultra models available in their online store for $589, or $210 off original MSRP. Each Watch includes Apple’s standard one-year warranty, a new... Read more
M2 Mac minis on sale starting at only $449
B&H Photo has M2-powered Mac minis in stock and on sale today for $100 off Apple’s MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $100 – Mac... Read more
Retailers are clearing out 9th-generation iPa...
With the introduction of new iPad Air and iPad Pros, along with newly discounted 10th-generation iPads, several Apple retailers are clearing out their remaining stock of 9th-generation iPads. Prices... Read more
Apple Studio Display with Standard Glass on s...
Best Buy has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Their price is the lowest available for a Studio Display among Apple’s retailers. Shipping is free... Read more
AirPods Max headphones back on sale for $449,...
Amazon has Apple AirPods Max headphones in stock and on sale for $100 off MSRP, only $449. The sale price is valid for all colors at the time of this post. Shipping is free: – AirPods Max: $449.99 $... Read more

Jobs Board

Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
Remote Secret *Apple* MacOS Workspace ONE A...
Job Description The Apple MacOS Workspace ONE AirWatch Engineer role is primarily responsible for managing a fleet of 400-500 MacBook computers. The ideal candidate Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
*Apple* Software Engineer - HP Inc. (United...
…Mobile, Windows and Mac applications. We are seeking a high energy Senior Apple mobile engineer who can lead and drive application development while also enabling Read more
Pharmacy Technician (Community) - *Apple* H...
Pharmacy Technician (Community) - Apple Hill Pharmacy - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.