Honors Compilers G22.3130 Spring 2000 Assignment 4 Intermediate Code Generator Due Friday, March 10 The next phase to implement in your compiler is intermediate code generation. The code will be three address code represented as quadruples (very much as described in the Dragon book). Intermediate Code ----------------- The 3 address code language consists of items of the following forms: x := y binop z where binop is one of +, -, *, / x := unop y where unop is - x := y L: where L is a label goto L if x relop y goto L where relop is one of <, <=, =, >=, >, <> param x precedes call operation, passing a parameter. NOTE: these operations should be issued in left-to-right order for pointless. The next phase of your compiler, the assembly-code generator, may decide to generate "push" operations in reverse order, but that is machine dependent. call p procedure call, no return value x := funcall f function call, x is assigned the value returned by f return procedure return funreturn x function return, returning x x := y[i] In this case, y[i] refers to the ith element of array y, and is independent of the size of the elements. Assembly-code generation, the next phase, will convert this to a size-dependent array reference. x[i] := y x := y.b This is a reference to the b field of record y. Note that the b field of y may itself be a record, in which case "x.c" might appear subsequently in the code. Similarly, if "y.b" is an array, "x[i]" can appear subsequently. x.z := y Note that each of the variables x, y, and z, can correspond to either variables in the source program (in particular, local variables of a procedure, formal parameters of a procedure, or global variables in the program), or temporary variables created by the intermediate code generator. Representation of quadruples and of a pointless program ------------------------------------------------------- Quadruples can be represented by a record (i.e. Java object) with four fields, "op", "target", "arg1", and "arg2". The "target", "arg1", and "arg2" fields might refer to variables in the source program or to temporary variables created by the intermediate code generator. If the former, then those fields should point to the node in the AST containing the type information, etc., that your parser & type checker created. Otherwise, as your intermediate code generator generates a temporary variable, it should create a node with the necessary type information, which the field in the quadruple will point to. Each procedure in the pointless program can be represented by an array (or vector) of quadruples. This array should be contained in a Java object which also points to the node giving type and formal parameter information about the procedure. Output of intermediate code generator ------------------------------------- In order to help debug the intermediate code generator, it should be able to print out the array of quadruples representing each procedure in the source program. On the due date of this assignment, Emily will send out a pointless program that your compiler should print the intermediate code for.