Due: 5 pm., Friday, December 11, 1998
Your assignment is to develop a compiler from Tiger (as specified in one of Appel's Tiger books) to a ``straight-line intermediate-representation code'' (hereafter referred to as IRT) which is described in the next section. IRT is a simplified assembly code, which is quite similar to the intermediate representation trees given in the Appel book.
In order to complete the translation, your program should:
You will then interpret the textual instructions using an IRT interpreter that I will write (in Perl, for portability).
It is not clear when all of the helpers will be ready, so you should be prepared to go ahead using only ``stubs'' for the various parts. I will definitely have the various things ready Monday after Thanksgiving.
You need not implement the built-in functions.
A few forms of memory locations are accepted:
R(num): register number num
M(num): memory location num
M(R(num): the memory location given by the contents of register number num
L(label): the address associated with a particular label
The valid commands are:
CALL label mem1 mem2 ... memn
BUILTIN name mem1 mem2 ... memn
MOVE memdest memsource
JUMP mem label-list
CJUMP op mem mem label
BINOP op mem mem
R(0): the value 0.
R(1): the program counter. You can also (and should) use
R(2): the stack pointer. You can also (and should) use
R(3): the frame pointer. You can also (and should) use
R(4): the heap pointer. You can also (and should) use
R(5): the accumulator. You can also (and should) use
R(6): the return value from function calls. You can also (and should) use
R(7): the return address for function calls. You can also (and should) use
R(8): the arguments for a function call. Function calls with more than ten arguments are not allowed. You can also (and should) use
and R(9): reserved for future use
R(maxint): for program use.
CALL instruction copies all of the values to the
appropriate registers and then branches to the appropriate label.
It is the callee's responsibility to move values from those registers
to other registers or to locations in memory. (The code given by
IRTFrame will handle the parameters, but not the return address.)
If your functions call other functions, you will need to handle the
code for storing values in possibly-changed registers within the current
frame. The IRTFrame module will provide an
method that lists all registers used within the module.
You can call any of the built-in functions given as part of the Tiger
standard library by using the name given in the Tiger language description.
In addition to the given functions, there is also a
function for printing integers.
Here is a program that multiplies two and three and prints the result.
BINOP MUL 2 3 BUILTIN printInt ACC END
Here is a program that that prints the string "Hello".
BUILTIN print L(L123) END LABEL L123 STRING Hello
Here is a program that reads two characters and prints the one with the larger ordinal value.
BUILTIN getchar MOVE R(21) RV BUILTIN ord R(21) MOVE R(22) RV BUILTIN getchar MOVE R(23) RV BUILTIN ord R(23) MOVE R(24) RV CJUMP GT R(22) R(24) L101 LABEL L100 BUILTIN print R23 JUMP 1 L102 LABEL L101 BUILTIN print R21 LABEL L102 END
Here is a program fragment that multiplies the contents of register 22 by 3
BINOP MUL R(22) 3 BUILTIN printInt ACC END
Here is a program the prints numbers from 1 to ???
# Loop forever, printing numbers from 1 to maxint BINOP PLUS 0 1 LABEL loop BUILTIN printInt ACC BUILTIN print L(cr) BINOP PLUS 1 ACC JUMP L(loop) END LABEL cr STRING \n
More programs forthcoming.
Forthcoming. Please send me some of your own suggestions.
You can obtain extra credit by doing any or all of the following:
RETURN. Clarified separation of arguments to functions. Added register 0.
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Fri Dec 11 10:09:08 1998.
This page generated on Fri Dec 11 10:11:28 1998 by SiteWeaver.
Contact our webmaster at firstname.lastname@example.org