feat(pbs): add mutable lists and stable snapshot cursors

Implement SDK list<T> and Cursor<T> with trusted compiler bindings and generic storage publication across IR and PBX. Pin the runtime provider and include specs, conformance coverage, installed examples, and DSC-0090 lessons.
This commit is contained in:
2026-10-23 00:46:39 +01:00
parent 76a8b074e8
commit c410e35001
46 changed files with 2683 additions and 165 deletions
@@ -65,8 +65,9 @@ public class BytecodeEmitter {
switch (op.kind()) {
case REGISTRY -> {
var instruction = Objects.requireNonNull(op.registryInstruction());
writeOpU32(code, instruction.op().opcode(), instruction.immediate());
if (instruction.secondImmediate() != null) code.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(instruction.secondImmediate()).array());
writeOpNoImm(code,instruction.op().opcode());
for(var value:new Integer[]{instruction.immediate(),instruction.secondImmediate(),instruction.thirdImmediate(),instruction.fourthImmediate()})
if(value!=null)code.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array());
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case HALT -> {
@@ -5,11 +5,12 @@ final class BytecodeOpcodeLayout {
static int sizeForOpcode(int opcode) {
return switch(opcode) {
case 0x00,0x01,0x05,0x11,0x12,0x13,0x20,0x21,0x22,0x23,0x24,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80 -> 2;
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80,0xa7 -> 2;
case 0x16 -> 3;
case 0x02,0x03,0x04,0x10,0x17,0x18,0x40,0x41,0x42,0x43,0x50,0x53,0x57,0x70,0x71,0x72,
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6 -> 6;
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> 6;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
case 0xaa -> 18;
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown or retired opcode 0x%04X".formatted(opcode));
};
@@ -29,15 +29,15 @@ public class BytecodePreloadVerifierService {
for(int i=0;i<module.functions().size();i++)verifyFunction(module,registry,i,used);
for(int i=0;i<used.length;i++)check(used[i],"unused syscall declaration "+i);
}
private record Instruction(int pc,int opcode,int first,int second,int next) {}
private record Instruction(int pc,int opcode,int first,int second,int third,int fourth,int next) {}
private void verifyFunction(BytecodeModule m,LinkedRegistry r,int index,boolean[] used) {
var f=m.functions().get(index);byte[] code=m.code();int start=f.codeOffset(),end=start+f.codeLen();
var instructions=new TreeMap<Integer,Instruction>();
for(int pc=start;pc<end;) {
check(pc+2<=end,"truncated opcode");int op=u16(code,pc);
if(op==0x70)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_LINKAGE_RAW_SYSCALL_IN_PRELOAD,"raw SYSCALL in producer image");int size=BytecodeOpcodeLayout.sizeForOpcode(op);check((long)pc+size<=end,"truncated instruction");
int a=size>=6?i32(code,pc+2):0,b=size==10?i32(code,pc+6):0;
var instruction=new Instruction(pc,op,a,b,pc+size);instructions.put(pc,instruction);
int a=size>=6?i32(code,pc+2):0,b=size>=10?i32(code,pc+6):0;
var instruction=new Instruction(pc,op,a,b,size>=18?i32(code,pc+10):0,size>=18?i32(code,pc+14):0,pc+size);instructions.put(pc,instruction);
switch(op) {
case 0x10 -> check(a>=0&&a<m.constPool().size(),"constant index");
case 0x16 -> check(code[pc+2]==0||code[pc+2]==1,"Boolean immediate");
@@ -58,7 +58,8 @@ public class BytecodePreloadVerifierService {
case 0x93,0x94,0x95,0x96 -> {var src=type(r,a).body();var dst=type(r,b).body();check(src instanceof Struct||src instanceof Array||src instanceof Trait,"reference source category");check(op==0x94||op==0x95?dst instanceof Trait:dst instanceof Struct||dst instanceof Array,"reference target category");}
case 0xa0 -> check(type(r,a).body() instanceof Struct,"Struct category");
case 0xa1,0xa2 -> field(r,a,b);
case 0xa3,0xa4,0xa5,0xa6 -> array(r,a);
case 0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> array(r,a);
case 0xaa -> {array(r,a);field(r,b,instruction.third());field(r,b,instruction.fourth());check(instruction.third()!=instruction.fourth(),"publication fields must differ");}
default -> { }
}
pc+=size;
@@ -166,6 +167,21 @@ public class BytecodePreloadVerifierService {
case 0xa4 -> {consume(s,facts(r,type(r,a).identity()),typed);s.stack.add(carrier("i64"));}
case 0xa5 -> {consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,array(r,a).element()),i.pc());}
case 0xa6 -> {consume(s,facts(r,array(r,a).element()),typed);consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);}
case 0xa7 -> consume(s,List.of(carrier("i64"),carrier("i64")),typed);
case 0xa8 -> consume(s,List.of(carrier("i64")),typed);
case 0xa9,0xaa -> {
if(op==0xaa) {
consume(s,facts(r,field(r,b,i.fourth()).type()),typed);
consume(s,facts(r,field(r,b,i.third()).type()),typed);
consume(s,facts(r,type(r,b).identity()),typed);
consume(s,facts(r,array(r,a).element()),typed);
consume(s,List.of(carrier("i64"),carrier("i64")),typed);
}
consume(s,List.of(carrier("i64"),carrier("i64")),typed);
consume(s,facts(r,type(r,a).identity()),typed);
consume(s,List.of(carrier("i64")),typed);
consume(s,facts(r,type(r,a).identity()),typed);
}
case 0x34,0x35 -> {consume(s,List.of(carrier("bool"),carrier("bool")),typed);s.stack.add(carrier("bool"));}
case 0x36 -> {consume(s,List.of(carrier("bool")),typed);s.stack.add(carrier("bool"));}
case 0x3e -> {
@@ -2,34 +2,24 @@ package p.studio.compiler.backend.irvm;
import java.util.Objects;
public record IRVMInstruction(
IRVMOp op,
Integer immediate,
Integer secondImmediate) {
public IRVMInstruction(
IRVMOp op,
Integer immediate) {
this(op, immediate, null);
}
public record IRVMInstruction(IRVMOp op, Integer immediate, Integer secondImmediate,
Integer thirdImmediate, Integer fourthImmediate) {
public IRVMInstruction(IRVMOp op, Integer immediate) { this(op,immediate,null,null,null); }
public IRVMInstruction(IRVMOp op, Integer immediate, Integer secondImmediate) {
this(op,immediate,secondImmediate,null,null);
}
public IRVMInstruction {
Objects.requireNonNull(op, "op");
if (op.name().startsWith("REGISTRY_") && ((op.immediateSize() == 8) != (secondImmediate != null))) {
throw new IllegalArgumentException("registry operand count differs from opcode layout");
}
if (!op.name().startsWith("REGISTRY_") && secondImmediate != null) {
throw new IllegalArgumentException("unexpected second immediate");
}
if (op.immediateSize() > 0 && immediate == null) {
throw new IllegalArgumentException("immediate is required for opcode: " + op.name());
}
if (op.immediateSize() == 0 && immediate != null) {
throw new IllegalArgumentException("immediate is not allowed for opcode: " + op.name());
Objects.requireNonNull(op,"op");
int count=(immediate==null?0:1)+(secondImmediate==null?0:1)+(thirdImmediate==null?0:1)+(fourthImmediate==null?0:1);
if(op.name().startsWith("REGISTRY_")) {
if(count*4!=op.immediateSize() || (thirdImmediate!=null&&secondImmediate==null) || (fourthImmediate!=null&&thirdImmediate==null))
throw new IllegalArgumentException("registry operand count differs from opcode layout");
} else {
if(secondImmediate!=null||thirdImmediate!=null||fourthImmediate!=null)
throw new IllegalArgumentException("unexpected extra immediate");
if((op.immediateSize()>0)!=(immediate!=null))
throw new IllegalArgumentException("immediate differs from opcode layout: "+op.name());
}
}
public int encodedSize() {
return 2 + op.immediateSize();
}
public int encodedSize() { return 2+op.immediateSize(); }
}
@@ -13,7 +13,7 @@ public final class RegistryInstructionLowerer {
public static IRVMInstruction lower(IRRegistryInstruction instruction, LinkedRegistry registry,
IRBackendExecutableFunction current, ReadOnlyList<IRBackendExecutableFunction> functions, Map<CallableId,Integer> functionIds) {
int first=instruction.type()==null?instruction.member():registry.id(instruction.type()).index();
Integer second=null;int pops=0,pushes=0;
Integer second=null,third=null,fourth=null;int pops=0,pushes=0;
var body=instruction.type()==null?null:registry.type(instruction.type()).body();
switch(instruction.kind()) {
case MAKE_CLOSURE -> {
@@ -51,12 +51,25 @@ public final class RegistryInstructionLowerer {
pops=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_SET?1+width:1;pushes=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_GET?width:0;
}
}
case INDEX_GUARD -> pops=2;
case ARRAY_CHECK_LENGTH,ARRAY_COPY,STORAGE_PUBLISH -> {
if(!(body instanceof Array array))throw new IllegalArgumentException("Array category required");
if(instruction.kind()==IRRegistryInstruction.Kind.ARRAY_CHECK_LENGTH)pops=1;
else if(instruction.kind()==IRRegistryInstruction.Kind.ARRAY_COPY)pops=5;
else {
if(!(registry.type(instruction.targetType()).body() instanceof Struct owner))throw new IllegalArgumentException("Struct publication owner required");
if(instruction.member()>=owner.fields().size()||instruction.secondMember()>=owner.fields().size())throw new IllegalArgumentException("publication field index");
second=registry.id(instruction.targetType()).index();third=instruction.member();fourth=instruction.secondMember();
pops=8+registry.width(array.element())+registry.width(owner.fields().get(third).type())+registry.width(owner.fields().get(fourth).type());
}
}
case ARRAY_NEW,ARRAY_LEN,ARRAY_GET,ARRAY_SET -> {
if(!(body instanceof Array array))throw new IllegalArgumentException("Array category required");
int width=registry.width(array.element());
switch(instruction.kind()) {case ARRAY_NEW,ARRAY_LEN->{pops=1;pushes=1;}case ARRAY_GET->{pops=2;pushes=width;}case ARRAY_SET->pops=2+width;default->throw new AssertionError();}
}
}
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),second==null?4:8,pops,pushes,false,false,false),first,second);
int bytes=instruction.kind()==IRRegistryInstruction.Kind.INDEX_GUARD?0:fourth!=null?16:second==null?4:8;
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),bytes,pops,pushes,false,false,false),bytes==0?null:first,second,third,fourth);
}
}
@@ -0,0 +1,67 @@
package p.studio.compiler.backend.registry;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.backend.irvm.*;
import p.studio.compiler.models.*;
import p.studio.compiler.models.registry.*;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.compiler.source.identifiers.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import java.util.ArrayList;
import static p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.*;
import static p.studio.compiler.models.registry.IRRegistryInstruction.Kind.*;
import static org.junit.jupiter.api.Assertions.*;
/** A synthetic frontend qualifies generic storage without a language dependency. */
class StoragePublicationPipelineTest {
private static final TypeRef INTEGER=TypeRef.builtin(0x10),LONG=TypeRef.builtin(0x11),
BUFFER=new TypeRef("synthetic:buffer"),OWNER=new TypeRef("synthetic:state");
private final ArrayList<IRBackendExecutableFunction.Instruction> instructions=new ArrayList<>();
private void literal(IRBackendExecutableFunction.InstructionKind kind,String value) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind,null,null,null,null,null,value,null,null,null,null));
}
private void local(IRBackendExecutableFunction.InstructionKind kind,int index) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind,null,null,null,null,null,null,null,index,null,null));
}
private void typed(IRRegistryInstruction.Kind kind,TypeRef type,int member) {
instructions.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
new IRRegistryInstruction(kind,type,null,null,member)));
}
private void number(long value){literal(PUSH_I64,Long.toString(value));}
@Test void guardsTransfersAndPublicationSurviveBothProducerPaths() throws Exception {
var registry=new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(INTEGER,null,new Builtin(0x10)),new TypeDeclaration(LONG,null,new Builtin(0x11)),
new TypeDeclaration(BUFFER,"Buffer",new Array(INTEGER)),
new TypeDeclaration(OWNER,"State",new Struct(ReadOnlyList.from(new NamedType(LONG,"generation"),new NamedType(INTEGER,"answer"))))),null);
number(3);typed(ARRAY_CHECK_LENGTH,BUFFER,0);
number(3);typed(ARRAY_NEW,BUFFER,0);local(SET_LOCAL,0);
typed(STRUCT_NEW,OWNER,0);local(SET_LOCAL,1);
number(0);number(1);typed(INDEX_GUARD,null,0);
for(int i=0;i<2;i++) {local(GET_LOCAL,0);number(i);literal(PUSH_I32,Integer.toString((i+1)*10));typed(ARRAY_SET,BUFFER,0);}
local(GET_LOCAL,0);number(0);local(GET_LOCAL,0);number(1);number(2);typed(ARRAY_COPY,BUFFER,0);
local(GET_LOCAL,0);number(1);local(GET_LOCAL,0);number(0);number(2);number(2);number(1);
literal(PUSH_I32,"7");local(GET_LOCAL,1);number(9);literal(PUSH_I32,"42");
instructions.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
new IRRegistryInstruction(STORAGE_PUBLISH,BUFFER,OWNER,null,0,1)));
local(GET_LOCAL,1);typed(STRUCT_GET,OWNER,1);literal(RET,null);
var function=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(0),0,0,0,2,1,11,
ReadOnlyList.wrap(instructions),null,new FunctionMetadata(new Signature(null,ReadOnlyList.from(INTEGER)),null,Role.NONE));
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).entryPointCallableName("main")
.registry(registry).executableFunctions(ReadOnlyList.from(function)).build();
for(boolean optimize:new boolean[]{false,true}) {
var program=new LowerToIRVMService().lower(backend);
if(optimize)program=new OptimizeIRVMService().optimize(program);
var publication=program.module().functions().getFirst().instructions().stream()
.filter(i->i.op().opcode()==0xaa).findFirst().orElseThrow();
assertEquals(18,publication.encodedSize());assertEquals(0,publication.thirdImmediate());assertEquals(1,publication.fourthImmediate());
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("common-storage"+(optimize?"-optimized":"")+".pbx"),module.serialize());
}
assertThrows(IllegalArgumentException.class,()->new IRRegistryInstruction(STORAGE_PUBLISH,BUFFER,OWNER,null,1,1));
}
}