implements PR-O2.1

This commit is contained in:
2026-03-24 13:42:26 +00:00
parent d915e39511
commit 5f78df750d
7 changed files with 283 additions and 3 deletions
@@ -0,0 +1,77 @@
package p.studio.compiler.backend.bytecode;
import org.junit.jupiter.api.Test;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class BytecodeLinkPrecheckServiceTest {
private final BytecodeLinkPrecheckService service = new BytecodeLinkPrecheckService();
@Test
void validateMustAcceptWellFormedModule() {
final var module = new BytecodeEmitter().emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeModule.Export("main", 0)),
ReadOnlyList.from(new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(BytecodeEmitter.Operation.ret())))));
assertDoesNotThrow(() -> service.validate(module));
}
@Test
void validateMustRejectInvalidExportFunctionIndex() {
final var module = new BytecodeModule(
0,
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeModule.FunctionMeta(0, 2, 0, 0, 0, 1)),
codeRet(),
null,
ReadOnlyList.from(new BytecodeModule.Export("main", 2)),
ReadOnlyList.empty());
final var thrown = assertThrows(BytecodeMarshalingException.class, () -> service.validate(module));
assertEquals(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_EXPORT_FUNC_INDEX_INVALID, thrown.code());
}
@Test
void validateMustRejectHostcallIndexOutOfBounds() {
final var module = new BytecodeModule(
0,
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeModule.FunctionMeta(0, 8, 0, 0, 0, 1)),
codeHostcall(3),
null,
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeModule.SyscallDecl("gfx", "draw_pixel", 1, 0, 0)));
final var thrown = assertThrows(BytecodeMarshalingException.class, () -> service.validate(module));
assertEquals(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_HOSTCALL_INDEX_INVALID, thrown.code());
}
private byte[] codeHostcall(final int index) {
final var out = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
out.putShort((short) 0x71);
out.putInt(index);
out.putShort((short) 0x51);
return out.array();
}
private byte[] codeRet() {
final var out = ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN);
out.putShort((short) 0x51);
return out.array();
}
}
@@ -3,6 +3,7 @@ package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import p.studio.compiler.workspaces.stages.EmitBytecodePipelineStage;
import p.studio.compiler.workspaces.stages.FrontendPhasePipelineStage;
import p.studio.compiler.workspaces.stages.LinkBytecodePipelineStage;
import p.studio.compiler.workspaces.stages.LoadSourcesPipelineStage;
import p.studio.compiler.workspaces.stages.LowerToIRVMPipelineStage;
import p.studio.compiler.workspaces.stages.OptimizeIRVMPipelineStage;
@@ -31,8 +32,8 @@ class BuilderPipelineServiceOrderTest {
FrontendPhasePipelineStage.class,
LowerToIRVMPipelineStage.class,
OptimizeIRVMPipelineStage.class,
EmitBytecodePipelineStage.class),
EmitBytecodePipelineStage.class,
LinkBytecodePipelineStage.class),
stageTypes);
}
}
@@ -0,0 +1,58 @@
package p.studio.compiler.workspaces.stages;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.BytecodeModule;
import p.studio.compiler.messages.BuilderPipelineConfig;
import p.studio.compiler.models.BuilderPipelineContext;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LinkBytecodePipelineStageTest {
@Test
void runMustFailWhenBytecodeModuleIsMissing() {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
final var stage = new LinkBytecodePipelineStage();
final var issues = stage.run(ctx, LogAggregator.empty());
final var firstIssue = issues.asCollection().iterator().next();
assertTrue(issues.hasErrors());
assertEquals("BACKEND_LINK_PRECHECK", firstIssue.getPhase());
assertEquals("MARSHAL_FORMAT_STAGE_INPUT_MISSING", firstIssue.getCode());
}
@Test
void runMustReportHostcallIndexPrecheckFailure() {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.bytecodeModule = new BytecodeModule(
0,
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeModule.FunctionMeta(0, 8, 0, 0, 0, 1)),
codeHostcall(1),
null,
ReadOnlyList.empty(),
ReadOnlyList.empty());
final var stage = new LinkBytecodePipelineStage();
final var issues = stage.run(ctx, LogAggregator.empty());
final var firstIssue = issues.asCollection().iterator().next();
assertTrue(issues.hasErrors());
assertEquals("MARSHAL_VERIFY_PRECHECK_HOSTCALL_INDEX_INVALID", firstIssue.getCode());
}
private byte[] codeHostcall(final int index) {
final var out = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
out.putShort((short) 0x71);
out.putInt(index);
out.putShort((short) 0x51);
return out.array();
}
}