Cosc 4P75


The compiler which is written in Cosc 4P75 generates a set of pseudo instructions, or assembly instructions. These are high level "assembly" type instructions which dictate how a program will run and are an abstraction of a "more" real environment. Thus, the code generated must  be translated to a platform which in turn implements the virtual machine. Only then will execution be possible.

The original version of the course back in the 1980s targeted the assembly pseudo instructions toward a stack architecture. This was run on a Vax 11-780 system. The compiler code that was generated, was linked with a set of Vax macros which provide the proper architecture. The resulting file was in effect Vax assembly instructions which then ran on the Vax architecture. These files where given the extension ".mar" which stood for macro.  Since the 1980s the Vax has been retired and the departments computing power replaced by SGI servers. These servers were MIPS Risc based unlike the CISC based Vax. A problem with MIPS is that direct MIPS assembly files must deal with the multi-threading, multi-processing and timing idiosyncrasies which are inherent in the architecture. Since serious MIPS assembly is not required, a simplistic solution was to use a MIPS simulator.

The solution was SPIM, a fully functional R2000/R3000 MIPS simulator. MIPS assembly language code (MAL) would run under SPIM which provides a nice safe environment.

To get MAL code I took the existing Vax virtual machine, reverse engineer the macro instructions, and produced equivalent MAL code based on the MIPS architecture. The program which takes the pseudo assembly instructions and converts them to run under SPIM is called a translator. This translator was named "marmalaid" to reflect the process of translating Vax .mar code to the current MAL code.
 

The Translator


Marmalaid is written in  'C'. It accepts the compiler output (assembly pseudo instructions) files and produces 1 target MIPS assembly file. The input files are read using file I/O, however the output is simply streamed to the consol. The following syntax is used to translate the .spm files which the compiler generates to .mal files which SPIM can execute.
 

marmalaid <file1> <file2> etc  > output.mal


Example: 

marmalaid  Employee.spm    Main.spm    >   Payroll.mal


Example code:


Here is the sample Payroll code generated from the compiler. Translate the code using the example above. Try running it under spim using the syntax as described in the SPIM section below.

Compiling marmalaid

Current version of marmalaid is 2.07 Nov. 18 2005.

Marmalaid was written in a standard ansi 'C', using no special functions. It has been compiled and run under UNIX on the departments SGIs, and under MSDOS. It has not been tested under linux, however, I don't see why it wouldn't work.

To compile for use on sandcastle. Down load the source code, "marmalaid.c".  Compile using:
 

cc marmalaid.c -o marmalaid
The MSDOS version was compiled using gcc under Cygwin. Compile using
 
gcc marmalaid.c -o marmalaid

Alternately you may download the  MSDOS executable. You will also need to download the cygwin1.dll file and place it in the same directory as the DOS version of marmalaid. The file you download is a self extracting zip file. Run cygwin.exe to extract cygwin1.dll.
 

SPIM

The .mal code which marmalaid produces will execute directly under SPIM. This has been tested using ver 6.5 on the SGIs, 6.0 MSDOS and 7.2 pcspim (Windows version). It is suggested you use the command line version since I/O can be redirected as needed. For example, here are some variation on executing the Payroll.mal program produced above.
 

spim -file Payroll.mal


Input from the keyboard, output to the consol.
 

spim -file Payroll.mal < Payroll.dat


Input comes from the data file Payroll.dat, output is sent to the consol.
 

spim -file Payroll.mal < Payroll.dat > Myoutput.txt


Input comes from the data file Payroll.dat, output is sent to the file Myoutput.txt
 

Obtaining SPIM.


Spim is available on sandcastle by typing "spim". Other platforms can be downloaded from the SPIM web site.
 

Known Bugs

Every effort has been made to ensure that marmalaid produces correct code. This does not mean that marmalaid is not buggy. As bugs are found or reported, marmalaid will be patched and new versions released. The current version of marmalaid will be posted above. You can determine your version number by typing marmalaid without any parameter.

Since marmalaid only translates what it is given, then it is a slave to the garbage in garbage out syndrome. This means that it will produce valid MAL code which crashes quite nicely under SPIM if it was given buggy .spm files to work with.

If your code crashes (Normally Bad addresses under SPIM) then check to ensure you are generating the correct pseudo assembly instructions. The translator has been tested with Dave Hughes's compiler test files and produces working .mal files.

If you find a bug and can verify it as a bug then report it to me bockusd@brocku.ca

Ver 2.07

  1. Just some cosmetic changes to the code.

Ver 2.06

  1. Get char (gchr) now reads exactly 1 char
  2. Get String (gstr) will now properly get a variable length string
  3. Get Boolean (gbln) will now read and parse a boolean literal (true, false), generates an error if it can not successfully read. Read is case sensitive.
  4. Put Boolean (pbln) will now output a boolean literal (true, false)and not a 0 or 1 as before.
  5. The var command has been procedurized to reduce the amount of code generated by marmalaid.

Ver 2.05

    1.     Marmalaid now generates internal labels in the form _ML###.

Ver 2.04
 

  1.  Minor bug fix in ndx command. Was missing a linefeed preventing an instruction from being produced as ndx was expanded.


Ver 2.03
 

  1. gbln has been modified to return 1 for integer values greater or equal to 1, 0 otherwise
  2. not has been fixed to only flip the right hand bit


Ver 2.02
 

  1. ndx r2, l, s, r1 is now fixed to correspond to r1 = r1 + (r2-l)*s.
  2. Reported bug that logical not does not produce the correct result. Currently still a bug.


Ver 2.01
 

  1. gchr r1 caused a bad address. "r1" specified an address location, this has now been changed to read the character directly into r1.


Ver 2.0
 

  1. The compiler should not generate labels in the format ML###. These labels are generated by marmalaid for use in branching.


Good Luck!