implements PR-034

This commit is contained in:
2026-03-24 13:42:25 +00:00
parent a9304f2515
commit 69da7a5924
3 changed files with 305 additions and 0 deletions
@@ -0,0 +1,127 @@
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class BytecodeEmitterTest {
@Test
void emitMustDeduplicateSyscallsByCanonicalIdentityAndPreserveFirstOccurrenceOrder() {
final var declA = new BytecodeModule.SyscallDecl("gfx", "draw_line", 1, 2, 0);
final var declB = new BytecodeModule.SyscallDecl("audio", "play", 1, 1, 0);
final var emitter = new BytecodeEmitter();
final var module = emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
4,
ReadOnlyList.from(
BytecodeEmitter.Operation.hostcall(declA, 2, 0),
BytecodeEmitter.Operation.hostcall(declA, 2, 0),
BytecodeEmitter.Operation.halt())),
new BytecodeEmitter.FunctionPlan(
"aux",
0,
0,
0,
2,
ReadOnlyList.from(
BytecodeEmitter.Operation.hostcall(declB, 1, 0),
BytecodeEmitter.Operation.ret())))));
assertEquals(2, module.syscalls().size());
assertEquals("gfx", module.syscalls().get(0).module());
assertEquals("audio", module.syscalls().get(1).module());
assertEquals(0x71, readU16(module.code(), 0));
assertEquals(0, readU32(module.code(), 2));
assertEquals(0x71, readU16(module.code(), 6));
assertEquals(0, readU32(module.code(), 8));
assertEquals(0x71, readU16(module.code(), 14));
assertEquals(1, readU32(module.code(), 16));
}
@Test
void emitMustRejectRawSyscallInPreloadPlan() {
final var emitter = new BytecodeEmitter();
final var thrown = assertThrows(BytecodeMarshalingException.class, () -> emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(BytecodeEmitter.Operation.rawSyscall(0x1001)))))));
assertEquals(BytecodeMarshalingErrorCode.MARSHAL_LINKAGE_RAW_SYSCALL_IN_PRELOAD, thrown.code());
}
@Test
void emitMustRejectAbiMismatch() {
final var declA = new BytecodeModule.SyscallDecl("gfx", "draw_line", 1, 2, 0);
final var emitter = new BytecodeEmitter();
final var thrown = assertThrows(BytecodeMarshalingException.class, () -> emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(BytecodeEmitter.Operation.hostcall(declA, 1, 0)))))));
assertEquals(BytecodeMarshalingErrorCode.MARSHAL_LINKAGE_HOST_ABI_MISMATCH, thrown.code());
}
@Test
void emitIntrinsicMustNotCreateSyscalls() {
final var emitter = new BytecodeEmitter();
final var module = emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(
BytecodeEmitter.Operation.intrinsic(0x2000),
BytecodeEmitter.Operation.halt())))));
assertTrue(module.syscalls().isEmpty());
assertEquals(0x72, readU16(module.code(), 0));
assertEquals(0x2000, readU32(module.code(), 2));
}
private static int readU16(final byte[] bytes, final int offset) {
return ByteBuffer.wrap(bytes, offset, 2).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
}
private static int readU32(final byte[] bytes, final int offset) {
return ByteBuffer.wrap(bytes, offset, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
}