Aha! The programs don't actually run! They say, "print", and SPIM coldly replies, "undefined symbol". What you need is a runtime system.
Tiger programs relies on a small runtime library that includes
functions like print
and initArray
.
The file runtime.c
contains
the Tiger standard library as described in the text book. Here it is
compiled for SPIM; two versions are present, depending on the endianness
of the underlying platform:
Additionally, you need a file with system calls, depending on the specific OS you are targetting:
sysspim.s
--
for targetting MIPS/SPIM
sysposix.c
--
theoretically, code for targetting a MIPS/Unix system, including
IRIX. (We'd love to know if you actually get this working....)
To compile the runtime yourself, you can usually just use the native C compiler. If you are targetting SPIM, however, then there isn't a native C compiler. Instead, you can use gcc on an IRIX box (or gcc with a MIPS target on any box):
gcc -EL -S -mrnames foo.c -o foo.s-raw
And then you can strip out assembly ops that SPIM doesn't understanding using the hackforspim shell script:
hackforspim < foo.s-raw > foo.s
Note: if you run SPIM on a big-endian box, then you need to compile the file with -EB instead of -EL.
The above library names Tiger runtime functions with a prefix of "tig_". The prefix helps avoid conflicts both with C library functions (such as "getchar") and with assembly pseudoops (such as "not"). To deal with this prefix, modify Temp.namedlabel to add it automatically.
Note that both strings and arrays in the above code are assumed to be prefixed by a word indicating their length. For example, this to access element i of an array at A, the memory location (A + 4 + 4*i) is used.
Finally, the MIPS assembly code assumes a calling convention where arguments are passed in a0 and a1, the result is passed in v0, and where a0...a3 and t0...t9 are caller-saves registers and thus may gleefully be trashed.