implements PR-O3.2

This commit is contained in:
2026-03-24 13:42:27 +00:00
parent a38fedc591
commit 0df936e36f
2 changed files with 88 additions and 2 deletions
@@ -6,6 +6,7 @@ import p.studio.utilities.structures.ReadOnlyList;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -167,6 +168,60 @@ class BytecodeEmitterTest {
assertEquals(2, readU32(module.code(), 8));
}
@Test
void emitMustDeduplicateConstPoolAndExportsByFirstOccurrence() {
final var emitter = new BytecodeEmitter();
final var module = emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.from(
new BytecodeModule.Int32Constant(7),
new BytecodeModule.Int32Constant(7),
new BytecodeModule.StringConstant("abc"),
new BytecodeModule.StringConstant("abc")),
ReadOnlyList.from(
new BytecodeModule.Export("main", 0),
new BytecodeModule.Export("main", 0),
new BytecodeModule.Export("aux", 1)),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(BytecodeEmitter.Operation.ret())))));
assertEquals(2, module.constPool().size());
assertEquals(2, module.exports().size());
assertEquals("main", module.exports().get(0).symbol());
assertEquals("aux", module.exports().get(1).symbol());
}
@Test
void emitMustRemainDeterministicAfterInterning() {
final var emitter = new BytecodeEmitter();
final var plan = new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.from(
new BytecodeModule.Int32Constant(1),
new BytecodeModule.Int32Constant(1)),
ReadOnlyList.from(
new BytecodeModule.Export("main", 0),
new BytecodeModule.Export("main", 0)),
ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
1,
ReadOnlyList.from(BytecodeEmitter.Operation.halt()))));
final var first = emitter.emit(plan).serialize();
final var second = emitter.emit(plan).serialize();
assertArrayEquals(first, second);
}
private static int readU16(final byte[] bytes, final int offset) {
return ByteBuffer.wrap(bytes, offset, 2).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
}