CS 440
Program 4
due: November 19, 2015
The Program
You will finish code generation for all of PAXI except for subprograms
(procedures), local variables, and parameters. Your compiler will
generate programs having only one subprogram (main) with no local
variables and no procedure calls but otherwise will compile the entire
language.
The pieces you will add for this program are the
writestr statement for quoted strings, Boolean
expressions, and control structures. You will also add some code, a
loader, to your PVM interpreter (Program 1).
Boolean Expressions
Boolean expressions are described in productions 39 through 43 of the
PAXI Language Definition. They involve the relational operators (= ,
< , <=, >, >=, and #) and Boolean operators (and,
or, not).
A Boolean atom such as (var1 < var2) should leave a value
0 or 1 on the stack. Each of var1 and
var2 in the above example will result in a value being left
on the stack (by arithmetic_expression). You will generate code to
subtract these two values, test the result (perhaps using
blt in this example), and push either 0 or 1 onto the
stack. The particular branching instruction used will depend on the
relational operator which appears in the Boolean atom.
Boolean expressions (using operators and,
or, and not) are implemented in a manner
similar to arithmetic expressions. Here you will leave integer values
on the stack as the values of Boolean expressions, Boolean terms, and
Boolean factors: 0 will be treated as false and 1 will be treated as
true.
Control Structures
The only control structures are if
... then ... else
... endif, while
... endwhile, and do
... endo which are described in productions 19
through 23 of the PAXI Language Definition. Each of these control
structures involves evaluating a Boolean expression and transferring
control to a corresponding point in the program. Transfer of control
is done with a branching instruction.
Sometimes generating the branching instruction will involve
backpatching. For example, implementing a while loop
involves a branch to the end (exit point) of the loop but the
instruction is generated before the location of the exit point is
known. The problem is resolved by generating a blank instruction
(e.g. 0, 0, 0), noting its code store location, and writing this
instruction in its proper place after the body of the loop has been
generated.
The Writestr Statement
The only code generated for the writestr statement is
a PUTS instruction with the (data store) address of the
null-terminated string to be displayed. In the case of a literal
string (second right hand side of production 32) you must allocate
memory for the string (literal strings will be stored consecutively in
the data store immediately following the variables and arrays). When
the compiler writes its output it will first write a header (see
below), the instructions from the code store, and then write the
strings (as ASCII codes) to the output file. For
writestr to display a string stored in an array
(third right hand side of production 32) you must get the argument for
the PUTS instruction from the symbol table entry for the array.
The Header and the Loader
A further change in this program is that a four-integer
header must now begin the compiler's output file. The first
integer is the size of the code store, i.e. the number of integers in
the PVM file which will be loaded into the code store array. The
second is the position in the data store where the strings will begin
(the first data store location past the space allocated for
variables and arrays). The third is the amount of data store used for
strings. The fourth is the program entry point (the code store
address of main) -- for Program 4 this will be 0.
The loader is a piece of code added to the PVM
interpreter. It will read the header and use the information it finds
there to allocate memory for the code store and data store, place the
instructions into the code store, the strings into the data store, and
prepare to start interpreter execution at the program's entry point.
To Hand In
You will hand in a source listing and a sample terminal session. The
source listing should include your yacc file and any relevant source
files (you need not turn in your lex file or additions to Program 1).
Sample data will be supplied later.