feat(pbs): align typed values and Pad input facade
Test / Build occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/pr-master There was a failure building this commit
Intrepid/Prometeu/Studio/pipeline/head There was a failure building this commit

This commit is contained in:
2026-10-15 15:48:50 +01:00
parent 603ed01227
commit 13c0ababbb
48 changed files with 1251 additions and 354 deletions
@@ -3,6 +3,7 @@ plugins {
}
dependencies {
testImplementation(project(":prometeu-compiler:frontends:prometeu-frontend-pbs"))
api(project(":prometeu-infra"))
implementation(project(":prometeu-compiler:prometeu-deps"))
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
@@ -94,6 +94,28 @@ public class LowerToIRVMService {
operations.add(BytecodeEmitter.Operation.pushI32(value, sourceSpan));
functionPc += IRVMOp.PUSH_I32.immediateSize() + 2;
}
case PUSH_I64, PUSH_F64, BUILTIN_CONST -> {
final java.util.List<BytecodeModule.ConstantPoolEntry> entries;
if (instr.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST) {
var key = instr.label().split("@", 2);
int dot = key[0].lastIndexOf('.');
entries = IRVMBuiltinConstantDefinition.resolve(key[0].substring(0, dot), key[0].substring(dot + 1), Long.parseLong(key[1]))
.orElseThrow(() -> new IllegalArgumentException("Unknown builtin constant: " + instr.label())).materialize().asList();
} else {
entries = java.util.List.of(instr.kind() == IRBackendExecutableFunction.InstructionKind.PUSH_I64
? new BytecodeModule.Int64Constant(Long.parseLong(instr.label()))
: new BytecodeModule.Float64Constant(Double.parseDouble(instr.label())));
}
if (instr.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST && entries.size() != instr.expectedRetSlots())
throw new IllegalArgumentException("Builtin constant group width differs from declared type: " + instr.label());
for (var value : entries) {
int index = constPool.indexOf(value);
if (index < 0) { index = constPool.size(); constPool.add(value); }
instructions.add(new IRVMInstruction(IRVMOp.PUSH_CONST, index));
operations.add(BytecodeEmitter.Operation.pushConst(index, sourceSpan));
functionPc += IRVMOp.PUSH_CONST.immediateSize() + 2;
}
}
case PUSH_CONST -> {
final var value = instr.label() == null ? "" : instr.label();
final var constPoolIndex = constPoolIndexByString.computeIfAbsent(value, ignored -> {
@@ -0,0 +1,124 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.models.SourceKind;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsRuntimeAlignmentTest {
private static String sdk(String path) throws Exception {
try(var stream = PbsFrontendCompiler.class.getResourceAsStream("/game/stdlib/1/"+path+"/main.pbs")) {
assertNotNull(stream); return new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
}
}
private static BytecodeModule compile(String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();
var fileId = new FileId(0);
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,fileId,diagnostics),fileId,diagnostics,p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(fileId,ast,diagnostics,SourceKind.SDK_INTERFACE,
new p.studio.compiler.source.identifiers.ModuleId(0),p.studio.utilities.structures.ReadOnlyList.empty(),
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(), ()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
var backend=IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new p.studio.compiler.source.identifiers.ModuleId(0)).executableFunctions(file.executableFunctions())
.globals(file.globals()).registry(file.registry()).intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
var program=new LowerToIRVMService().lower(backend);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
Files.write(directory.resolve("pbs-"+name+".pbx"),module.serialize());
return module;
}
@Test void numericValuesAndExplicitConversionsReachProductionPbx() throws Exception {
compile("numeric", """
declare service Probe {
fn widen(value: int) -> long { return value as long; }
fn contextual() -> long { return 1; }
fn receive(value: long) -> long { return value; }
fn main() -> long {
let large: long = 2147483648;
let maximum: long = 9223372036854775807;
Probe.contextual();
Probe.receive(2147483648);
return large + Probe.widen(7);
}
}
""");
compile("narrow", "declare service Probe { fn main() -> int { let value: long = 2147483648; return value as int; } }");
compile("float", "declare service Probe { fn main() -> float { return 1.5 + 2.25; } }");
compile("overflow", "declare service Probe { fn main() -> int { return 2147483647 + 1; } }");
}
@Test void allInputQueriesCompileFromThePublishedFacade() throws Exception {
var input=sdk("sdk/input");
for (var button : new String[]{"Up","Down","Left","Right","A","B","X","Y","L","R","Start","Select"}) {
for(var query:new String[]{"pressed","released","down","hold"}) {
var type=query.equals("hold")?"long":"bool";
compile("pad-"+button.toLowerCase()+"-"+query,input+"\ndeclare service Probe { fn select(button: Button) -> Button { return button; } fn main() -> "+type+" { let button = Probe.select(Button."+button+"); return Pad."+query+"(button); } }");
compile("pad-"+button.toLowerCase()+"-"+query+"-constant", input+"\ndeclare service Probe { fn main() -> "+type+" { return Pad."+query+"(Button."+button+"); } }");
}
}
for(var query:new String[]{"pressed","released","down","hold","x","y"}) {
var type=java.util.Set.of("hold","x","y").contains(query)?"long":"bool";
compile("touch-"+query,input+"\ndeclare service Probe { fn main() -> "+type+" { return Touch."+query+"(); } }");
}
}
@Test void completeBuiltinAndTupleGroupsReachProductionPbx() throws Exception {
compile("vec2", """
[BuiltinType(name = "vec2", version = 1)]
declare builtin type Vector(pub x: float, pub y: float) {}
[BuiltinConst(target = "vec2", name = "zero", version = 1)]
declare const ZERO: Vector;
declare service Probe {
fn echo(value: Vector) -> Vector { return value; }
fn main() -> Vector { let vector = Probe.echo(ZERO); return vector; }
}
""");
compile("tuple", """
declare service Probe {
fn pair() -> (x: int, y: long) { return (x: 7, y: 8 as long); }
fn main() -> (x: int, y: long) { let pair = Probe.pair(); return pair; }
}
""");
compile("named", """
declare global trace: int = 0;
declare service Probe {
fn x() -> int { trace = trace * 10 + 1; return 10; }
fn y() -> int { trace = trace * 10 + 2; return 20; }
fn combine(x: int, y: int) -> int { return x * 100 + y; }
fn main() -> int {
trace = 0;
let value = Probe.combine apply (y: Probe.y(), x: Probe.x());
return trace * 10000 + value;
}
}
""");
compile("color", sdk("core/color") + "declare service Probe { fn echo(value: Color) -> Color { return value; } fn main() -> Color { return Probe.echo(RED); } }");
compile("pixel", sdk("core/color") + """
[BuiltinType(name = "pixel", version = 1)]
declare builtin type Pixel(pub x: long, pub y: long, pub color: Color) {}
declare service Probe {
fn echo(value: Pixel) -> Pixel { return value; }
fn main() -> Pixel { let value = new Pixel(12, -34, RED); return Probe.echo(value); }
}
""");
compile("string", "declare service Probe { fn echo(value: str) -> str { return value; } fn main() -> str { return Probe.echo(\"hello\"); } }");
compile("division", "declare service Probe { fn main() -> int { return 1 / 0; } }");
compile("minimum", "declare service Probe { fn main() -> long { return -9223372036854775808; } }");
}
@Test void wrongNumericAndButtonTypesAreRejected() throws Exception {
for(var expression:new String[]{"1","1 as long","true","Other.A","\"A\""}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),sdk("sdk/input")+"\ndeclare enum Other(A); declare service Probe { fn main() -> bool { return Pad.pressed("+expression+"); } }",diagnostics,SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.hasErrors());
}
for(var source:new String[]{"fn main() -> long { let x: long = 1; let y: int = 1; return x + y; }", "fn main() -> int { return 2147483648; }", "fn main() -> long { return 9223372036854775808; }"}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),source,diagnostics);
assertTrue(diagnostics.hasErrors());
}
}
}