Function code generation
Description
An example
Description
Modify the parser to generate assembly-like code for entire programs.
The specific requirements are
-
As in previous projects, your parser should ignore (but copy) "meta-statements".
-
The output program uses the same grammar as the input program as in the
previous
assignment except that a generated program cannot have any
function other than main, and it cannot have any variables other
than array mem[N], where N is a constant. However,
you can define and use macros such as #define stackTop mem[0].
-
Parameters are passed by value.
-
As with the previous assignment, the output program should be compilable
by a C compiler and have the same behavior as the input program.
-
You may use a jump table to implement parameterized gotos as in this
example. Or you may use a while loop, as in another
example.
-
Finish above requirements before implementing this one. A
type error happens when a variable is not operated in an appropriate manner.
A memory error (i.e. segmentation fault) happens when a program accesses
a memory location that is outside its legal data space. Augment your
compiler so that its generated programs are guarantteed to be free of type
and memory error, in other words, make them type and memory safe.
Programs that are not safe should be rejected at either compile time or
at run time. The extra amount of coding is trivial for CS 254 and
very modest for CS 454. Think about the solution carefully before
programming. You are encouraged to discuss it with classmates, TA,
or instructor.
Polymorphism extension (for CS 454)
-
Your compiler should allow integer variables be declared as either decimal
(dint) or octal (oint). A 'dint' variable or parameter can
hold only decimal values, and a 'bint' variable or parameter can hold only
octal values. However, a 'int' variable or parameter can hold both
decimal and octal values. The operations such as '+', '-', etc are implicitly
polymorphic on decimal and octal values. You can use the same operators
for different types of values except input and output operations.
For decimal values, use "readdint" and "writedint". For octal values,
use "readoint" and "writeoint". When the type of a value in a 'int' variable
is unspecified, read and write it as a decimal value.
-
Your compiler should pass all test cases where only 'int' type is used.
It should also support polymorphism in programs where 'oint' and 'dint'
are used.
An example
original
program
program
after intra-function code generation
program
after function code generation