feat(pbs): add Set and reusable identity hashing
This commit is contained in:
+2
-2
@@ -8,8 +8,8 @@ final class BytecodeOpcodeLayout {
|
||||
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80 -> 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,0xa9 -> 6;
|
||||
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
|
||||
0x90,0x91,0x92,0x97,0xa0,0xa3,0xa4,0xa5,0xa6,0xa9 -> 6;
|
||||
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0x98,0xa1,0xa2 -> 10;
|
||||
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
|
||||
"unknown or retired opcode 0x%04X".formatted(opcode));
|
||||
};
|
||||
|
||||
+4
@@ -56,6 +56,7 @@ public class BytecodePreloadVerifierService {
|
||||
case 0x58 -> method(r,a,b);
|
||||
case 0x90,0x91,0x92 -> type(r,a);
|
||||
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 0x97,0x98 -> {identitySource(r,a);if(op==0x98)identitySource(r,b);}
|
||||
case 0xa0 -> check(type(r,a).body() instanceof Struct,"Struct category");
|
||||
case 0xa1,0xa2 -> field(r,a,b);
|
||||
case 0xa3,0xa4,0xa5,0xa6,0xa9 -> array(r,a);
|
||||
@@ -159,6 +160,8 @@ public class BytecodePreloadVerifierService {
|
||||
case 0x91 -> {dynamic(pop(s,2),typed);produce(s,facts(r,type(r,a).identity()),i.pc());}
|
||||
case 0x92 -> {dynamic(pop(s,2),typed);s.stack.add(carrier("bool"));}
|
||||
case 0x93,0x94,0x95,0x96 -> {consume(s,facts(r,type(r,a).identity()),typed);if(op==0x93||op==0x94)s.stack.add(carrier("bool"));else produce(s,facts(r,type(r,b).identity()),i.pc());}
|
||||
case 0x97 -> {consume(s,facts(r,type(r,a).identity()),typed);s.stack.add(carrier("i64"));}
|
||||
case 0x98 -> {var expected=new ArrayList<>(facts(r,type(r,a).identity()));expected.addAll(facts(r,type(r,b).identity()));consume(s,expected,typed);s.stack.add(carrier("bool"));}
|
||||
case 0xa0 -> produce(s,facts(r,type(r,a).identity()),i.pc());
|
||||
case 0xa1 -> {consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,field(r,a,b).type()),i.pc());}
|
||||
case 0xa2 -> {consume(s,facts(r,field(r,a,b).type()),typed);consume(s,facts(r,type(r,a).identity()),typed);}
|
||||
@@ -266,6 +269,7 @@ public class BytecodePreloadVerifierService {
|
||||
check((mask(actual.get(i+1).carrier())&64)!=0,"invalid null group pair");
|
||||
}
|
||||
}
|
||||
private static void identitySource(LinkedRegistry r,int id){var body=type(r,id).body();check(body instanceof Struct||body instanceof Array||body instanceof Fn||body instanceof Trait||body instanceof Builtin builtin&&builtin.id()==0x16,"identity source category");}
|
||||
private static TypeDeclaration type(LinkedRegistry r,int id){check(id>=0&&id<r.types().size(),"TypeId");return r.types().get(id);}
|
||||
private static Fn fn(LinkedRegistry r,int id){check(type(r,id).body() instanceof Fn,"Fn category");return (Fn)type(r,id).body();}
|
||||
private static Method method(LinkedRegistry r,int id,int member){check(type(r,id).body() instanceof Trait,"Trait category");var t=(Trait)type(r,id).body();check(member>=0&&member<t.methods().size(),"method index");return t.methods().get(member);}
|
||||
|
||||
+13
@@ -42,6 +42,15 @@ public final class RegistryInstructionLowerer {
|
||||
if(trait?!(target instanceof Trait):!(target instanceof Struct||target instanceof Array))throw new IllegalArgumentException("reference target category");
|
||||
second=registry.id(instruction.targetType()).index();pops=registry.width(instruction.type());pushes=instruction.kind()==IRRegistryInstruction.Kind.REF_TO_TRAIT?2:1;
|
||||
}
|
||||
case REF_IDENTITY_HASH,REF_IDENTITY_EQ -> {
|
||||
if(!identitySource(body))throw new IllegalArgumentException("identity source category");
|
||||
pops=registry.width(instruction.type());pushes=1;
|
||||
if(instruction.kind()==IRRegistryInstruction.Kind.REF_IDENTITY_EQ) {
|
||||
if(!identitySource(registry.type(instruction.targetType()).body()))throw new IllegalArgumentException("identity right category");
|
||||
pops=Math.addExact(pops,registry.width(instruction.targetType()));
|
||||
second=registry.id(instruction.targetType()).index();
|
||||
}
|
||||
}
|
||||
case STRUCT_NEW,STRUCT_GET,STRUCT_SET -> {
|
||||
if(!(body instanceof Struct struct))throw new IllegalArgumentException("Struct category required");
|
||||
if(instruction.kind()==IRRegistryInstruction.Kind.STRUCT_NEW)pushes=1;
|
||||
@@ -64,4 +73,8 @@ public final class RegistryInstructionLowerer {
|
||||
int bytes=second==null?4:8;
|
||||
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),bytes,pops,pushes,false,false,false),first,second);
|
||||
}
|
||||
private static boolean identitySource(TypeBody body) {
|
||||
return body instanceof Struct||body instanceof Array||body instanceof Fn||body instanceof Trait
|
||||
||body instanceof Builtin builtin&&builtin.id()==0x16;
|
||||
}
|
||||
}
|
||||
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
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.*;
|
||||
import static p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.*;
|
||||
import static p.studio.compiler.models.registry.IRRegistryInstruction.Kind.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Neutral producer: no PBS type, spelling or collection binding reaches the backend. */
|
||||
class IdentityHashingPipelineTest {
|
||||
private static final TypeRef LONG=TypeRef.builtin(0x11),BOOL=TypeRef.builtin(0x13),
|
||||
OWNER=new TypeRef("neutral:object"),VIEW=new TypeRef("neutral:view");
|
||||
private final List<IRBackendExecutableFunction.Instruction> code=new ArrayList<>();
|
||||
private void emit(IRBackendExecutableFunction.InstructionKind op,String label,Integer index) {
|
||||
code.add(new IRBackendExecutableFunction.Instruction(op,null,null,null,null,null,label,null,index,null,null));
|
||||
}
|
||||
private void typed(IRRegistryInstruction.Kind op,TypeRef left,TypeRef right,int field) {
|
||||
code.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
|
||||
new IRRegistryInstruction(op,left,right,null,field)));
|
||||
}
|
||||
@Test void completeTraitViewsAndConcreteAliasesUseBothTypedQueries() throws Exception {
|
||||
var registry=new IRRegistry(ReadOnlyList.from(
|
||||
new TypeDeclaration(LONG,null,new Builtin(0x11)),new TypeDeclaration(BOOL,null,new Builtin(0x13)),
|
||||
new TypeDeclaration(OWNER,"Object",new Struct(ReadOnlyList.from(new NamedType(LONG,"value")))),
|
||||
new TypeDeclaration(VIEW,"View",new Trait(null))),
|
||||
ReadOnlyList.from(new Implementation("neutral:implementation",OWNER,VIEW,null)));
|
||||
typed(STRUCT_NEW,OWNER,null,0);emit(SET_LOCAL,null,0);
|
||||
emit(GET_LOCAL,null,0);typed(REF_IDENTITY_HASH,OWNER,null,0);emit(SET_LOCAL,null,1);
|
||||
emit(GET_LOCAL,null,0);emit(PUSH_I64,"42",null);typed(STRUCT_SET,OWNER,null,0);
|
||||
emit(GET_LOCAL,null,0);typed(REF_TO_TRAIT,OWNER,VIEW,0);typed(REF_IDENTITY_HASH,VIEW,null,0);
|
||||
emit(GET_LOCAL,null,1);emit(EQ,null,null);
|
||||
emit(GET_LOCAL,null,0);emit(GET_LOCAL,null,0);typed(REF_TO_TRAIT,OWNER,VIEW,0);
|
||||
typed(REF_IDENTITY_EQ,OWNER,VIEW,0);emit(AND,null,null);
|
||||
emit(GET_LOCAL,null,0);typed(STRUCT_NEW,OWNER,null,0);typed(REF_IDENTITY_EQ,OWNER,OWNER,0);
|
||||
emit(NOT,null,null);emit(AND,null,null);emit(RET,null,null);
|
||||
var f=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(0),0,0,0,2,1,5,
|
||||
ReadOnlyList.wrap(code),null,FunctionMetadata.ordinary(new Signature(null,ReadOnlyList.from(BOOL))));
|
||||
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).entryPointCallableName("main")
|
||||
.registry(registry).executableFunctions(ReadOnlyList.from(f)).build();
|
||||
for(boolean optimized:new boolean[]{false,true}) {
|
||||
var program=new LowerToIRVMService().lower(backend);
|
||||
if(optimized)program=new OptimizeIRVMService().optimize(program);
|
||||
var queries=program.module().functions().getFirst().instructions().stream()
|
||||
.filter(i->i.op().opcode()==0x97||i.op().opcode()==0x98).toList();
|
||||
assertEquals(4,queries.size());
|
||||
assertEquals(List.of(6,6,10,10),queries.stream().map(IRVMInstruction::encodedSize).toList());
|
||||
assertEquals(List.of(1,2,3,2),queries.stream().map(i->i.op().pops()).toList());
|
||||
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-identity"+(optimized?"-optimized":"")+".pbx"),module.serialize());
|
||||
int pc=0,hashPc=-1,eqPc=-1;
|
||||
for(var instruction:program.module().functions().getFirst().instructions()) {
|
||||
if(hashPc<0&&instruction.op().opcode()==0x97)hashPc=pc;
|
||||
if(eqPc<0&&instruction.op().opcode()==0x98)eqPc=pc;
|
||||
pc+=instruction.encodedSize();
|
||||
}
|
||||
int boolId=LinkedRegistry.link(registry).id(BOOL).index();
|
||||
for(var negative:new String[]{"bad-category","bad-right","dead-bad-category","bad-typeid"}) {
|
||||
byte[] bad=module.code().clone();var buffer=java.nio.ByteBuffer.wrap(bad).order(java.nio.ByteOrder.LITTLE_ENDIAN);
|
||||
if(negative.equals("bad-right"))buffer.putInt(eqPc+6,boolId);
|
||||
else buffer.putInt(hashPc+2,negative.equals("bad-typeid")?-1:boolId);
|
||||
if(negative.startsWith("dead"))for(int offset=0;offset<6;offset+=2)buffer.putShort(offset,(short)1);
|
||||
var malformed=new BytecodeModule(module.version(),module.constPool(),module.functions(),bad,module.debugInfo(),module.exports(),module.syscalls(),module.registry(),module.intrinsicSignatures());
|
||||
assertThrows(BytecodeMarshalingException.class,()->new BytecodePreloadVerifierService().verify(malformed),negative);
|
||||
Files.write(directory.resolve("common-identity-"+negative+(optimized?"-optimized":"")+".pbx"),malformed.serialize());
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test void neutralUnknownInputsKeepRuntimeValidationOfTheCompleteRightView()throws Exception {
|
||||
var registry=new IRRegistry(ReadOnlyList.from(
|
||||
new TypeDeclaration(LONG,null,new Builtin(0x11)),new TypeDeclaration(BOOL,null,new Builtin(0x13)),
|
||||
new TypeDeclaration(OWNER,"Object",new Struct(null)),new TypeDeclaration(VIEW,"View",new Trait(null))),null);
|
||||
var linked=LinkedRegistry.link(registry);int owner=linked.id(OWNER).index(),view=linked.id(VIEW).index();
|
||||
for(var name:new String[]{"null-views","right-after-null","right-after-distinct","partial","truncated"}) {
|
||||
var bytes=new java.io.ByteArrayOutputStream();
|
||||
if(name.equals("truncated")){word(bytes,0x97);}
|
||||
else {
|
||||
if(name.equals("right-after-distinct"))instruction(bytes,0xa0,owner);else instruction(bytes,0x10,0);
|
||||
if(name.startsWith("right")) {instruction(bytes,0x40,0);instruction(bytes,0x40,1);}
|
||||
else {instruction(bytes,0x10,0);if(!name.equals("partial"))instruction(bytes,0x10,0);}
|
||||
instruction(bytes,0x98,owner);integer(bytes,view);word(bytes,0x51);
|
||||
}
|
||||
byte[] raw=bytes.toByteArray();
|
||||
var module=new BytecodeModule(0,ReadOnlyList.from(new BytecodeModule.NullConstant()),
|
||||
ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,raw.length,0,0,1,3,
|
||||
FunctionMetadata.ordinary(new Signature(null,ReadOnlyList.from(BOOL))))),raw,null,ReadOnlyList.empty(),ReadOnlyList.empty(),registry);
|
||||
if(name.equals("partial")||name.equals("truncated"))assertThrows(BytecodeMarshalingException.class,()->new BytecodePreloadVerifierService().verify(module));
|
||||
else new BytecodePreloadVerifierService().verify(module);
|
||||
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
|
||||
Files.write(directory.resolve("common-identity-"+name+".pbx"),module.serialize());
|
||||
}
|
||||
}
|
||||
private static void word(java.io.ByteArrayOutputStream out,int n){out.write(n);out.write(n>>>8);}
|
||||
private static void integer(java.io.ByteArrayOutputStream out,int n){for(int i=0;i<4;i++)out.write(n>>>(8*i));}
|
||||
private static void instruction(java.io.ByteArrayOutputStream out,int op,int n){word(out,op);integer(out,n);}
|
||||
@Test void identityEqualityRequiresTwoTypeOperands() {
|
||||
assertThrows(NullPointerException.class,()->new IRRegistryInstruction(REF_IDENTITY_EQ,OWNER,null,null,0));
|
||||
assertThrows(IllegalArgumentException.class,()->new IRRegistryInstruction(REF_IDENTITY_HASH,OWNER,VIEW,null,0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user