G22.2130 Compiler Construction
Fall 2004
Final Project : Code Generation
Due: Dec. 24
To complete the project, you will generate assembly code for the machine
for which you wrote assignment 0.
You must be able to generate assembly for basic control structures (loops
and conditionals) for function calls, and for standard arithmetic. A reasonable
test would include the following simple functions, which you can declare as
static functions of some class:
int even (int x ) {
-- This could also be written more compactly:
-- return ((x / 2) * 2 == x);
if (x / 2) * 2 == x) {
return true;
}
else {
return false;
}
};
int pow (int base, int exponent) {
int temp;
if (Exponent == 0) {
return 1;
}
else if (exponent == 1) {
return base;
}
else if (even (exponent)) {
temp = pow (base, exponent / 2);
return temp * temp;
}
else {
return base * pow (base, exponent -1);
}
};
int slow_pow (int base, int exponent) {
int result;
result = 1;
for (int j = 1; j <= exponent; j++) {
result = result * base;
};
return result;
}
Ideally, your code should be executable, and you could write a driver in
C or in assembly, that calls pow and slow_pow for assorted inputs and prints
the result.
Short of that, producing correct assembly for your chosen target is fine.
You can generate the assembly directly from the tree, or by generating
quadruples first, and transforming them into assembly in a separate pass.