CSE 423 Lab #9: Linked Lists of Intermediate Code

Turnin: a .zip on Canvas

This week's lab will consist of practice writing out some linked lists of "intermediate code".

Draft tac.h (raw) 3-address code representation

Here is a starting point. Do we need to add anything?

Supporting tac.c (raw) C code

Lab Exercise: Hello, 3-address Code

For this lab you should write a toy 3-address program, build a linked list of three address instructions in a hardwired dummy main() program, and print it out. One major issue for k0 is that we need a strategy for println() and variables in {curlybrackets}. For k0, we might be able to translate those into calls to printf(). But you are free to devise a better plan for your k0 compiler.
fun main() {
   var i : Int = 5;
   i = i * i + 1;
   println("Variable i is $i.");
}
Write a standalone main() procedure that (manually) builds the linked list of TAC for:
.string 8				; string region size (multiple of 8)
	Variable i is %d.\000		; ""
.code
proc main,0,32
	ASN	loc:0,const:5		; i = 5
	MUL	loc:8,loc:0,loc:0	; t1 = i * i
	ADD	loc:16,loc:8,const:1	; t2 = t1 + 1
	ASN	loc:0,loc:16		; i = t2
	PARAM	loc:0			; push param 2 (i)
	PARAM	str:0			; push param 1 ("Variable i is %d")
	CALL	printf,2,loc:24		; t5 = printf()
	RETURN
Write a tacprint() function that prints out a linked list of three-address instructions to standard out. Use it to approximate the above (minus the assembler comments from ; to the end of various lines).

Note: in real life, what corrections or additions are needed for this example?