Tiger Runtime System

Okay, so you've been compiling like mad. You've lexed, parsed, type-checked, and translated; you've generated code and allocated registers. Maybe you've optimized some trees or folded some constants along the way. Even after all of this work, however, there is probably a lingering feeling that something is missing. What could it be....

Aha! The programs don't actually run! They say, "print", and SPIM coldly replies, "undefined symbol". What you need is a runtime system.

The Runtime Library Itself

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:

Compiling the Runtime

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.

Linking Conventions

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.

Other Pages

Andrew Appel has a web page on targetting SPIM. The information here is based on that, but has been extended.