implements PR-05.6
This commit is contained in:
+98
-8
@@ -1,12 +1,14 @@
|
||||
package p.studio.compiler.backend.irvm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.backend.bytecode.BytecodeEmitter;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -23,6 +25,7 @@ class OptimizeIRVMServiceTest {
|
||||
|
||||
assertEquals(validProgram(), output);
|
||||
assertEquals(List.of("A", "B"), order);
|
||||
assertEquals(List.of("A", "B"), service.lastExecutedPassNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -35,6 +38,7 @@ class OptimizeIRVMServiceTest {
|
||||
service.optimize(validProgram());
|
||||
|
||||
assertEquals(List.of("B"), order);
|
||||
assertEquals(List.of("B"), service.lastExecutedPassNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -57,6 +61,72 @@ class OptimizeIRVMServiceTest {
|
||||
assertTrue(thrown.getMessage().contains("vm profile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void optimizeDefaultPassesMustRemoveUnreachableInstructions() {
|
||||
final var service = new OptimizeIRVMService();
|
||||
final var input = singleFunctionProgram(
|
||||
ReadOnlyList.from(
|
||||
new IRVMInstruction(IRVMOp.RET, null),
|
||||
new IRVMInstruction(IRVMOp.HALT, null)),
|
||||
ReadOnlyList.from(
|
||||
BytecodeEmitter.Operation.ret(),
|
||||
BytecodeEmitter.Operation.halt()));
|
||||
|
||||
final var optimized = service.optimize(input);
|
||||
|
||||
assertNotEquals(input, optimized);
|
||||
assertEquals(1, optimized.module().functions().getFirst().instructions().size());
|
||||
assertEquals(IRVMOp.RET, optimized.module().functions().getFirst().instructions().getFirst().op());
|
||||
assertEquals(List.of(
|
||||
"UnreachableInstructionEliminationPass",
|
||||
"NormalizeRedundantJumpTargetsPass",
|
||||
"SimplifyJumpToNextPcPass"), service.lastExecutedPassNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeRedundantJumpTargetsPassMustCollapseJumpChain() {
|
||||
final var service = new OptimizeIRVMService(new IRVMValidator(), List.of(
|
||||
OptimizeIRVMService.normalizeRedundantJumpTargetsPass()));
|
||||
final var input = singleFunctionProgram(
|
||||
ReadOnlyList.from(
|
||||
new IRVMInstruction(IRVMOp.JMP, 6),
|
||||
new IRVMInstruction(IRVMOp.JMP, 12),
|
||||
new IRVMInstruction(IRVMOp.RET, null)),
|
||||
ReadOnlyList.from(
|
||||
BytecodeEmitter.Operation.jmp(6, null),
|
||||
BytecodeEmitter.Operation.jmp(12, null),
|
||||
BytecodeEmitter.Operation.ret()));
|
||||
|
||||
final var optimized = service.optimize(input);
|
||||
final var firstInstruction = optimized.module().functions().getFirst().instructions().getFirst();
|
||||
final var firstOperation = optimized.emissionPlan().functions().getFirst().operations().getFirst();
|
||||
|
||||
assertEquals(IRVMOp.JMP, firstInstruction.op());
|
||||
assertEquals(12, firstInstruction.immediate());
|
||||
assertEquals(BytecodeEmitter.OperationKind.JMP, firstOperation.kind());
|
||||
assertEquals(12, firstOperation.immediate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void simplifyJumpToNextPcPassMustRemoveDirectFallthroughJump() {
|
||||
final var service = new OptimizeIRVMService(new IRVMValidator(), List.of(
|
||||
OptimizeIRVMService.simplifyJumpToNextPcPass()));
|
||||
final var input = singleFunctionProgram(
|
||||
ReadOnlyList.from(
|
||||
new IRVMInstruction(IRVMOp.JMP, 6),
|
||||
new IRVMInstruction(IRVMOp.RET, null)),
|
||||
ReadOnlyList.from(
|
||||
BytecodeEmitter.Operation.jmp(6, null),
|
||||
BytecodeEmitter.Operation.ret()));
|
||||
|
||||
final var optimized = service.optimize(input);
|
||||
|
||||
assertEquals(1, optimized.module().functions().getFirst().instructions().size());
|
||||
assertEquals(IRVMOp.RET, optimized.module().functions().getFirst().instructions().getFirst().op());
|
||||
assertEquals(1, optimized.emissionPlan().functions().getFirst().operations().size());
|
||||
assertEquals(BytecodeEmitter.OperationKind.RET, optimized.emissionPlan().functions().getFirst().operations().getFirst().kind());
|
||||
}
|
||||
|
||||
private OptimizeIRVMService.IRVMPass namedPass(
|
||||
final String name,
|
||||
final List<String> order,
|
||||
@@ -81,14 +151,34 @@ class OptimizeIRVMServiceTest {
|
||||
}
|
||||
|
||||
private IRVMProgram validProgram() {
|
||||
return new IRVMProgram(new IRVMModule(
|
||||
"core-v1",
|
||||
ReadOnlyList.from(new IRVMFunction(
|
||||
"main",
|
||||
return singleFunctionProgram(
|
||||
ReadOnlyList.from(new IRVMInstruction(IRVMOp.HALT, null)),
|
||||
ReadOnlyList.from(BytecodeEmitter.Operation.halt()));
|
||||
}
|
||||
|
||||
private IRVMProgram singleFunctionProgram(
|
||||
final ReadOnlyList<IRVMInstruction> instructions,
|
||||
final ReadOnlyList<BytecodeEmitter.Operation> operations) {
|
||||
return new IRVMProgram(
|
||||
new IRVMModule(
|
||||
"core-v1",
|
||||
ReadOnlyList.from(new IRVMFunction(
|
||||
"main",
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
instructions))),
|
||||
new BytecodeEmitter.EmissionPlan(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
ReadOnlyList.from(new IRVMInstruction(IRVMOp.HALT, null))))));
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.from(new BytecodeEmitter.FunctionPlan(
|
||||
"main",
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
operations))));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user