add + improve comparison codegen
This commit is contained in:
@ -82,6 +82,15 @@ public final class CodeGenerator {
|
|||||||
return Collections.unmodifiableMap(varMap);
|
return Collections.unmodifiableMap(varMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String genComparisonInst(String inst, String labelPre, int currentLabel) {
|
||||||
|
return inst + " " + labelPre + "true" + currentLabel // If not equal jump to NEtrue
|
||||||
|
+ "\n\t\tldc 0" // If false load 0
|
||||||
|
+ "\n\t\tgoto " + labelPre + "end" + currentLabel // If false skip true branch
|
||||||
|
+ "\n" + labelPre + "true" + currentLabel + ":"
|
||||||
|
+ "\n\t\tldc 1" // if true load 1
|
||||||
|
+ "\n" + labelPre + "end" + currentLabel + ":";
|
||||||
|
}
|
||||||
|
|
||||||
private void generateHeader(String source) {
|
private void generateHeader(String source) {
|
||||||
System.out.println(" - Generating Jasmin Assembler...");
|
System.out.println(" - Generating Jasmin Assembler...");
|
||||||
final String clazz = this.tree.getRoot().getChildren().get(1).getValue();
|
final String clazz = this.tree.getRoot().getChildren().get(1).getValue();
|
||||||
@ -280,18 +289,12 @@ public final class CodeGenerator {
|
|||||||
inst = switch (node.getValue()) {
|
inst = switch (node.getValue()) {
|
||||||
case "AND" -> "iand"; // Boolean
|
case "AND" -> "iand"; // Boolean
|
||||||
case "OR" -> "ior";
|
case "OR" -> "ior";
|
||||||
case "EQUAL" -> cmpeq + " EQtrue" + currentLabel // If equal jump to EQtrue
|
case "EQUAL" -> genComparisonInst(cmpeq, "EQ", currentLabel);
|
||||||
+ "\n\t\tldc 0" // If false load 0
|
case "NOT_EQUAL" -> genComparisonInst(cmpne, "NE", currentLabel);
|
||||||
+ "\n\t\tgoto EQend" + currentLabel // If false skip true branch
|
case "LESS" -> genComparisonInst("if_icmplt", "LT", currentLabel);
|
||||||
+ "\nEQtrue" + currentLabel + ":"
|
case "LESS_EQUAL" -> genComparisonInst("if_icmple", "LE", currentLabel);
|
||||||
+ "\n\t\tldc 1" // if true load 1
|
case "GREATER" -> genComparisonInst("if_icmpgt", "GT", currentLabel);
|
||||||
+ "\nEQend" + currentLabel + ":";
|
case "GREATER_EQUAL" -> genComparisonInst("if_icmpge", "GE", currentLabel);
|
||||||
case "NOT_EQUAL" -> cmpne + " NEtrue" + currentLabel // If not equal jump to NEtrue
|
|
||||||
+ "\n\t\tldc 0" // If false load 0
|
|
||||||
+ "\n\t\tgoto NEend" + currentLabel // If false skip true branch
|
|
||||||
+ "\nNEtrue" + currentLabel + ":"
|
|
||||||
+ "\n\t\tldc 1" // if true load 1
|
|
||||||
+ "\nNEend" + currentLabel + ":";
|
|
||||||
default -> throw new IllegalStateException("Unexpected value: " + node.getValue());
|
default -> throw new IllegalStateException("Unexpected value: " + node.getValue());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user