new test main with code directly (fixes)

This commit is contained in:
2026-03-24 13:42:32 +00:00
parent 14f9b5dd26
commit 5630f8f119
63 changed files with 1677 additions and 198 deletions
@@ -68,7 +68,7 @@ class GoldenArtifactsTest {
"",
"",
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata("core.color.pack", 1, new IntrinsicId(0)),
new IRBackendExecutableFunction.IntrinsicCallMetadata("input.pad", 1, new IntrinsicId(0)),
0,
0,
Span.none()),
@@ -80,7 +80,7 @@ class GoldenArtifactsTest {
null,
Span.none())),
Span.none())))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.build();
}
@@ -204,7 +204,7 @@ class IRVMValidatorTest {
}
@Test
void validateProgramMustApplyHostcallStackEffectsFromMetadata() {
void validateProgramMustRejectHostcallWhenDeclaredArgsAreMissingOnStack() {
final var module = new IRVMModule(
"core-v1",
ReadOnlyList.from(new IRVMFunction(
@@ -36,15 +36,34 @@ class LowerToIRVMServiceTest {
assertEquals(0, lowered.module().functions().get(1).instructions().get(0).immediate());
}
@Test
void lowerMustUseFrontendDeclaredEntrypointCallable() {
final var backend = IRBackend.builder()
.entryPointCallableName("frame")
.executableFunctions(ReadOnlyList.from(
fn("aux", "app", 11, ReadOnlyList.from(
callFunc("app", "frame", 10),
ret())),
fn("frame", "app", 10, ReadOnlyList.from(
ret()))))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
assertEquals("frame", lowered.module().functions().get(0).name());
assertEquals("aux", lowered.module().functions().get(1).name());
assertEquals(0, lowered.module().functions().get(1).instructions().get(0).immediate());
}
@Test
void lowerMustMapHostAndIntrinsicCallsites() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
callHost("gfx", "draw_pixel", 1, 0, 0),
callIntrinsic("core.color.pack", 1, 0),
callIntrinsic("input.pad", 1, 0),
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
@@ -52,7 +71,7 @@ class LowerToIRVMServiceTest {
final var instructions = lowered.module().functions().getFirst().instructions();
assertEquals(IRVMOp.HOSTCALL, instructions.get(0).op());
assertEquals(IRVMOp.INTRINSIC, instructions.get(1).op());
assertEquals(0, instructions.get(1).immediate());
assertEquals(0x2000, instructions.get(1).immediate());
final var emissionOps = lowered.emissionPlan().functions().getFirst().operations();
assertEquals(p.studio.compiler.backend.bytecode.BytecodeEmitter.OperationKind.HOSTCALL, emissionOps.get(0).kind());
assertEquals(p.studio.compiler.backend.bytecode.BytecodeEmitter.OperationKind.INTRINSIC, emissionOps.get(1).kind());
@@ -84,6 +103,21 @@ class LowerToIRVMServiceTest {
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE, thrown.code());
}
@Test
void lowerMustRejectUnknownCanonicalIntrinsicIdentity() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
callIntrinsic("core.color.pack", 1, 0),
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1)))
.build();
final var thrown = assertThrows(IRVMLoweringException.class, () -> new LowerToIRVMService().lower(backend));
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_INVALID_INTRINSIC_ID, thrown.code());
assertTrue(thrown.getMessage().contains("unknown VM-owned intrinsic identity"));
}
@Test
void lowerMustRejectCallArgSlotMismatch() {
final var backend = IRBackend.builder()
@@ -141,6 +175,18 @@ class LowerToIRVMServiceTest {
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_ENTRYPOINT_MISSING, thrown.code());
}
@Test
void lowerMustRejectWhenEntrypointDeclarationIsMissing() {
final var backend = IRBackend.builder()
.entryPointCallableName(" ")
.executableFunctions(ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(ret()))))
.build();
final var thrown = assertThrows(IRVMLoweringException.class, () -> new LowerToIRVMService().lower(backend));
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_ENTRYPOINT_DECLARATION_MISSING, thrown.code());
}
@Test
void lowerMustRejectWhenEntrypointIsAmbiguous() {
final var backend = IRBackend.builder()
@@ -31,9 +31,9 @@ class OptimizeIRVMEquivalenceHarnessTest {
.executableFunctions(ReadOnlyList.from(
fn("main", 1, ReadOnlyList.from(
callHost("gfx", "draw_pixel", 1, 0, 0),
callIntrinsic("core.color.pack", 1, 0, 0, 0),
callIntrinsic("input.pad", 1, 0, 0, 0),
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
@@ -145,9 +145,9 @@ class BackendGateIIntegrationTest {
void gateI_validIntrinsicPath() {
final var module = emitFromBackend(backendWithSingleFunction(
fn("main", ReadOnlyList.from(
callIntrinsic("core.color.pack", 1, 0),
callIntrinsic("input.pad", 1, 0),
ret())),
ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1))));
ReadOnlyList.from(new IntrinsicReference("input.pad", 1))));
final var check = compatibilityAdapter.check(module);
assertEquals(CompatibilityError.NONE, check.error());
@@ -9,6 +9,7 @@ import p.studio.compiler.workspaces.stages.LowerToIRVMPipelineStage;
import p.studio.compiler.workspaces.stages.OptimizeIRVMPipelineStage;
import p.studio.compiler.workspaces.stages.ResolveDepsPipelineStage;
import p.studio.compiler.workspaces.stages.VerifyBytecodePipelineStage;
import p.studio.compiler.workspaces.stages.WriteBytecodeArtifactPipelineStage;
import java.lang.reflect.Field;
import java.util.List;
@@ -35,7 +36,8 @@ class BuilderPipelineServiceOrderTest {
OptimizeIRVMPipelineStage.class,
EmitBytecodePipelineStage.class,
LinkBytecodePipelineStage.class,
VerifyBytecodePipelineStage.class),
VerifyBytecodePipelineStage.class,
WriteBytecodeArtifactPipelineStage.class),
stageTypes);
}
}
@@ -141,4 +141,67 @@ class LowerToIRVMPipelineStageTest {
assertEquals(12, firstIssue.getStart());
assertEquals(24, firstIssue.getEnd());
}
@Test
void runMustRejectCallWhenStackDoesNotProvideDeclaredArgs() {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.irBackend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
new IRBackendExecutableFunction(
new FileId(0),
"app",
"callee",
new CallableId(2),
0,
10,
1,
0,
0,
1,
ReadOnlyList.from(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
"",
"",
null,
null,
Span.none())),
Span.none()),
new IRBackendExecutableFunction(
new FileId(0),
"app",
"main",
new CallableId(1),
0,
10,
0,
0,
0,
1,
ReadOnlyList.from(
new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
"app",
"callee",
new CallableId(2),
null,
null,
1,
0,
Span.none()),
new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
"",
"",
null,
null,
Span.none())),
Span.none())))
.build();
final var issues = new LowerToIRVMPipelineStage().run(ctx, LogAggregator.empty());
final var firstIssue = issues.asCollection().iterator().next();
assertTrue(issues.hasErrors());
assertEquals("MARSHAL_VERIFY_PRECHECK_STACK_UNDERFLOW", firstIssue.getCode());
}
}
@@ -0,0 +1,86 @@
package p.studio.compiler.workspaces.stages;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
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.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class WriteBytecodeArtifactPipelineStageTest {
@TempDir
Path tempDir;
@Test
void runMustFailWhenRootProjectPathIsMissing() {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.bytecodeBytes = new byte[] { 0x01 };
final var issues = new WriteBytecodeArtifactPipelineStage().run(ctx, LogAggregator.empty());
final var firstIssue = issues.asCollection().iterator().next();
assertTrue(issues.hasErrors());
assertEquals("BACKEND_WRITE_ARTIFACT", firstIssue.getPhase());
assertEquals("MARSHAL_FORMAT_STAGE_INPUT_MISSING", firstIssue.getCode());
}
@Test
void runMustFailWhenBytecodeInputIsMissing() {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.rootProjectPathCanon = tempDir;
final var issues = new WriteBytecodeArtifactPipelineStage().run(ctx, LogAggregator.empty());
final var firstIssue = issues.asCollection().iterator().next();
assertTrue(issues.hasErrors());
assertEquals("BACKEND_WRITE_ARTIFACT", firstIssue.getPhase());
assertEquals("MARSHAL_FORMAT_STAGE_INPUT_MISSING", firstIssue.getCode());
}
@Test
void runMustWriteBuildProgramPbxFromByteArray() throws Exception {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.rootProjectPathCanon = tempDir;
ctx.bytecodeBytes = new byte[] { 0x50, 0x42, 0x58, 0x00 };
final var issues = new WriteBytecodeArtifactPipelineStage().run(ctx, LogAggregator.empty());
final var outputPath = tempDir.resolve("build").resolve("program.pbx");
assertFalse(issues.hasErrors());
assertTrue(Files.exists(outputPath));
assertArrayEquals(ctx.bytecodeBytes, Files.readAllBytes(outputPath));
assertEquals(outputPath, ctx.bytecodeArtifactPath);
}
@Test
void runMustWriteBuildProgramPbxFromModuleWhenByteArrayIsMissing() throws Exception {
final var ctx = BuilderPipelineContext.compilerContext(new BuilderPipelineConfig(false, "."));
ctx.rootProjectPathCanon = tempDir;
ctx.bytecodeModule = new BytecodeModule(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
new byte[] { (byte) 0x51, 0x00 },
null,
ReadOnlyList.empty(),
ReadOnlyList.empty());
final var issues = new WriteBytecodeArtifactPipelineStage().run(ctx, LogAggregator.empty());
final var outputPath = tempDir.resolve("build").resolve("program.pbx");
assertFalse(issues.hasErrors());
assertTrue(Files.exists(outputPath));
assertArrayEquals(ctx.bytecodeModule.serialize(), Files.readAllBytes(outputPath));
assertEquals(outputPath, ctx.bytecodeArtifactPath);
}
}