Using recursion, complete the body of the following static method. The code generated by generateCodeForStatement must match the code-generation patterns described in Code Generation (slides 39-47). Use the code for the case IF as a guide for the other cases.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
Generates the sequence of virtual machine instructions ("byte codes")
corresponding to {@code s} and appends it at the end of {@code cp}.
@param s
code
the {@code Statement} for which to generate code
@param context
code
the {@code Context} in which to find user defined instructions
@param cp
code
the {@code Sequence} containing the generated code
@updates cp
@ensures
if [all instructions called in s are either primitive or
code
defined in context] and
[context does not include any calling cycles, i.e., recursion] then
cp = #cp * [sequence of virtual machine "byte codes" corresponding to s]
else
[reports an appropriate error message to the console and terminates client]
final int dummy = 0;
switch (s.kind()) {
case BLOCK: {
// TODO - fill in case
break;
}
case IF: {
Statement b = s.newInstance();
Condition c = s.disassembleIf(b);
cp.add(cp.length(), conditionalJump(c).byteCode());
int jump = cp.length();
cp.add(cp.length(), dummy);
generateCodeForStatement(b, context, cp);
cp.replaceEntry(jump, cp.length());
s.assembleIf(c, b);
break;
}
case IF_ELSE: {
// TODO - fill in case
break;
}
case WHILE: {
// TODO - fill in case
break;
}
// remaining case CALL goes here
}
Assistant
}
In lab you will be given an implementation for the following static method needed to implement generateCodeForStatement.
/**
Converts {@code Condition} into corresponding conditional jump
instruction byte code.
@param c
code
the {@code Condition} to be converted
@return the conditional jump instruction byte code corresponding to
code
{@code c}
@ensures
conditionalJump =
[conditional jump instruction byte code corresponding to c]
*/
private static Instruction conditionalJump(Condition c) {...}
To complete the generateCodeForStatement method, we need to fill in the cases for BLOCK, IF_ELSE, and WHILE. The code should recursively generate the appropriate byte codes for different kinds of statements provided as part of the Statement enumeration (which is assumed but not shown). Keep in mind that this code snippet is illustrative and won't work "as is" because it uses hypothetical Statement, Condition, Context, and Sequence classes that are not part of standard Java libraries.
Please note that in the IF case, conditionalJump(c).byteCode() is assumed to be a way to get the byte code for the conditional jump, which matches the expected interface for the placeholder method conditionalJump(Condition c) provided at the end.
Here's how you could complete the generateCodeForStatement method:
java
private static void generateCodeForStatement(Statement s, Map<String, Statement> context, Sequence<Integer> cp) {
final int dummy = 0; // Used as a placeholder for jump addresses that will be back-filled later
switch (s.kind()) {
case BLOCK: {
Block b = s.disassembleBlock();
for (Statement statement : b) {
generateCodeForStatement(statement, context, cp);
}
s.assembleBlock(b);
break;
}
case IF: {
// Existing code for IF case
Statement b = s.newInstance();
Condition c = s.disassembleIf(b);
cp.add(cp.length(), conditionalJump(c).byteCode());
int jump = cp.length();
cp.add(cp.length(), dummy);
generateCodeForStatement(b, context, cp);
cp.replaceEntry(jump, cp.length());
s.assembleIf(c, b);
break;
}
case IF_ELSE: {
Statement tBranch = s.newInstance();
Statement fBranch = s.newInstance();
Condition c = s.disassembleIfElse(tBranch, fBranch);
cp.add(cp.length(), conditionalJump(c).byteCode());
int jumpToElse = cp.length();
cp.add(cp.length(), dummy);
generateCodeForStatement(tBranch, context, cp);
int jumpToEnd = cp.length();
cp.add(cp.length(), dummy); // This will jump over the else branch once the if branch is executed
cp.replaceEntry(jumpToElse, cp.length());
generateCodeForStatement(fBranch, context, cp);
cp.replaceEntry(jumpToEnd, cp.length());
s.assembleIfElse(c, tBranch, fBranch);
break;
}
case WHILE: {
int startWhile = cp.length();
Statement b = s.newInstance();
Condition c = s.disassembleWhile(b);
cp.add(cp.length(), conditionalJump(c).byteCode());
int jump = cp.length();
cp.add(cp.length(), dummy);
generateCodeForStatement(b, context, cp);
cp.add(cp.length(), unconditionalJump().byteCode()); // Jump back to start of while
cp.add(cp.length(), startWhile);
cp.replaceEntry(jump, cp.length());
s.assembleWhile(c, b);
break;
}
// additional cases (e.g., CALL) would go here
}
}
private static Instruction unconditionalJump() {
// Returns an instance of Instruction representing an unconditional jump
// The specifics of how we get this Instruction are not provided.
// This method is a placeholder and does not represent actual Java code.
return new Instruction() {
public int byteCode() {
// This would be the actual byte code for unconditional jump
return 0; // Placeholder value
}
};
}
For the BLOCK case, the method iterates over all statements within a block and generates code for each statement. After generating code, the block is reassembled.
The IF_ELSE case generates code for both the true branch and the false branch of the statement. The code first makes a conditional jump to the else branch if the condition is false. After generating code for the true branch, it adds an unconditional jump to skip over the else branch. The addresses of jumps are updated after generating the corresponding branches.
The WHILE case generates code for the loop. It first sets a conditional jump that will exit the loop if the condition is false. Then, code for the body of the loop is generated, followed by an unconditional jump back to the beginning of the loop condition check. The address for the conditional jump is updated after the loop body.
The unconditionalJump method provided here is a placeholder to return an Instruction representing an unconditional jump.
This code assumes the existence of many methods (disassembleBlock, disassembleIf, disassembleIfElse, assembleIf, assembleIfElse, disassembleWhile, assembleWhile) that are supposed to convert the Statement objects to and from their constituent parts, and a way to manipulate the cp (code sequence) like add, replaceEntry, etc., which are also not standard Java methods and expected to be provided in the context of this hypothetical scenario.