feat(compiler): implement typed registry and production PBX conformance (AGD76)
This commit is contained in:
+15
-2
@@ -1,6 +1,7 @@
|
||||
package p.studio.compiler.models;
|
||||
|
||||
import lombok.Builder;
|
||||
import p.studio.compiler.models.registry.IRRegistry;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
import p.studio.compiler.source.identifiers.IntrinsicId;
|
||||
@@ -34,6 +35,8 @@ public class IRBackend implements IRBackendReader {
|
||||
private final ReadOnlyList<IntrinsicReference> intrinsicPool = ReadOnlyList.empty();
|
||||
@Builder.Default
|
||||
private final IRReservedMetadata reservedMetadata = IRReservedMetadata.empty();
|
||||
@Builder.Default
|
||||
private final IRRegistry registry = IRRegistry.empty();
|
||||
private final IRLifecycleDeclaration lifecycleDeclaration;
|
||||
|
||||
public static IRBackendAggregator aggregator() {
|
||||
@@ -41,6 +44,8 @@ public class IRBackend implements IRBackendReader {
|
||||
}
|
||||
|
||||
public static final class IRBackendAggregator {
|
||||
private final ArrayList<IRRegistry.TypeDeclaration> registryTypes = new ArrayList<>();
|
||||
private final ArrayList<IRRegistry.Implementation> registryImplementations = new ArrayList<>();
|
||||
private String entryPointCallableName;
|
||||
private ModuleId entryPointModuleId = ModuleId.none();
|
||||
private final ArrayList<IRFunction> functions = new ArrayList<>();
|
||||
@@ -90,6 +95,11 @@ public class IRBackend implements IRBackendReader {
|
||||
}
|
||||
final var callableRemap = reindexCallables(backendFile.callableSignatures(), moduleRemap);
|
||||
final var intrinsicRemap = reindexIntrinsics(backendFile.intrinsicPool());
|
||||
registryTypes.addAll(backendFile.registry().types().asList());
|
||||
for (final var impl : backendFile.registry().implementations()) {
|
||||
registryImplementations.add(new IRRegistry.Implementation(impl.declarationKey(), impl.owner(), impl.trait(),
|
||||
ReadOnlyList.wrap(impl.methods().stream().map(id -> remapCallableId(id, callableRemap, "implementation")).toList())));
|
||||
}
|
||||
for (final var function : backendFile.executableFunctions()) {
|
||||
executableFunctions.add(remapExecutableFunction(function, moduleRemap, callableRemap, intrinsicRemap));
|
||||
}
|
||||
@@ -200,7 +210,9 @@ public class IRBackend implements IRBackendReader {
|
||||
instruction.targetLabel(),
|
||||
instruction.expectedArgSlots(),
|
||||
instruction.expectedRetSlots(),
|
||||
instruction.span()));
|
||||
instruction.span(), instruction.registryInstruction() == null ? null :
|
||||
new p.studio.compiler.models.registry.IRRegistryInstruction(instruction.registryInstruction().kind(), instruction.registryInstruction().type(), instruction.registryInstruction().targetType(),
|
||||
instruction.registryInstruction().function() == null ? null : remapCallableId(instruction.registryInstruction().function(), callableRemap, "Registry instruction"), instruction.registryInstruction().member())));
|
||||
}
|
||||
return new IRBackendExecutableFunction(
|
||||
function.fileId(),
|
||||
@@ -214,7 +226,7 @@ public class IRBackend implements IRBackendReader {
|
||||
function.returnSlots(),
|
||||
function.maxStackSlots(),
|
||||
ReadOnlyList.wrap(remappedInstructions),
|
||||
function.span());
|
||||
function.span(), function.metadata());
|
||||
}
|
||||
|
||||
private IRLifecycleFileDeclaration remapLifecycleFileDeclaration(
|
||||
@@ -367,6 +379,7 @@ public class IRBackend implements IRBackendReader {
|
||||
public IRBackend emit() {
|
||||
return IRBackend
|
||||
.builder()
|
||||
.registry(new IRRegistry(ReadOnlyList.wrap(registryTypes), ReadOnlyList.wrap(registryImplementations)))
|
||||
.entryPointCallableName(resolveEntryPointCallableName())
|
||||
.entryPointModuleId(resolveEntryPointModuleId())
|
||||
.functions(ReadOnlyList.wrap(functions))
|
||||
|
||||
+38
@@ -10,6 +10,20 @@ import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.util.Objects;
|
||||
|
||||
public record IRBackendExecutableFunction(
|
||||
FileId fileId,
|
||||
ModuleId moduleId,
|
||||
String callableName,
|
||||
CallableId callableId,
|
||||
int sourceStart,
|
||||
int sourceEnd,
|
||||
int paramSlots,
|
||||
int localSlots,
|
||||
int returnSlots,
|
||||
int maxStackSlots,
|
||||
ReadOnlyList<Instruction> instructions,
|
||||
Span span,
|
||||
p.studio.compiler.models.registry.IRRegistry.FunctionMetadata metadata) {
|
||||
public IRBackendExecutableFunction(
|
||||
FileId fileId,
|
||||
ModuleId moduleId,
|
||||
String callableName,
|
||||
@@ -22,6 +36,9 @@ public record IRBackendExecutableFunction(
|
||||
int maxStackSlots,
|
||||
ReadOnlyList<Instruction> instructions,
|
||||
Span span) {
|
||||
this(fileId, moduleId, callableName, callableId, sourceStart, sourceEnd, paramSlots, localSlots, returnSlots, maxStackSlots, instructions, span, paramSlots == 0 && returnSlots == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null);
|
||||
}
|
||||
|
||||
|
||||
public IRBackendExecutableFunction(
|
||||
final FileId fileId,
|
||||
@@ -82,6 +99,19 @@ public record IRBackendExecutableFunction(
|
||||
}
|
||||
|
||||
public record Instruction(
|
||||
InstructionKind kind,
|
||||
ModuleId calleeModuleId,
|
||||
String calleeCallableName,
|
||||
CallableId calleeCallableId,
|
||||
HostCallMetadata hostCall,
|
||||
IntrinsicCallMetadata intrinsicCall,
|
||||
String label,
|
||||
String targetLabel,
|
||||
Integer expectedArgSlots,
|
||||
Integer expectedRetSlots,
|
||||
Span span,
|
||||
p.studio.compiler.models.registry.IRRegistryInstruction registryInstruction) {
|
||||
public Instruction(
|
||||
InstructionKind kind,
|
||||
ModuleId calleeModuleId,
|
||||
String calleeCallableName,
|
||||
@@ -93,6 +123,9 @@ public record IRBackendExecutableFunction(
|
||||
Integer expectedArgSlots,
|
||||
Integer expectedRetSlots,
|
||||
Span span) {
|
||||
this(kind, calleeModuleId, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, label, targetLabel, expectedArgSlots, expectedRetSlots, span, null);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final ModuleId calleeModuleId,
|
||||
@@ -217,7 +250,11 @@ public record IRBackendExecutableFunction(
|
||||
if (expectedRetSlots != null && expectedRetSlots < 0) {
|
||||
throw new IllegalArgumentException("expectedRetSlots must be non-negative");
|
||||
}
|
||||
if ((kind == InstructionKind.REGISTRY) != (registryInstruction != null)) throw new IllegalArgumentException("Registry operand mismatch");
|
||||
switch (kind) {
|
||||
case REGISTRY -> {
|
||||
if (hostCall != null || intrinsicCall != null || calleeCallableId != null || !label.isEmpty() || !targetLabel.isEmpty()) throw new IllegalArgumentException("unexpected Registry instruction metadata");
|
||||
}
|
||||
case PUSH_I32 -> {
|
||||
if (label.isBlank()) {
|
||||
throw new IllegalArgumentException("PUSH_I32 requires immediate label payload");
|
||||
@@ -387,6 +424,7 @@ public record IRBackendExecutableFunction(
|
||||
}
|
||||
|
||||
public enum InstructionKind {
|
||||
REGISTRY,
|
||||
PUSH_I32,
|
||||
PUSH_BOOL,
|
||||
PUSH_CONST,
|
||||
|
||||
+18
@@ -7,8 +7,22 @@ import p.studio.compiler.source.tables.ModuleReference;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.Objects;
|
||||
import p.studio.compiler.models.registry.IRRegistry;
|
||||
|
||||
public record IRBackendFile(
|
||||
FileId fileId,
|
||||
ReadOnlyList<IRFunction> functions,
|
||||
ReadOnlyList<IRSyntheticFunction> syntheticFunctions,
|
||||
ReadOnlyList<IRGlobal> globals,
|
||||
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
|
||||
IRReservedMetadata reservedMetadata,
|
||||
ReadOnlyList<ModuleReference> modulePool,
|
||||
ReadOnlyList<CallableSignatureRef> callableSignatures,
|
||||
ReadOnlyList<IntrinsicReference> intrinsicPool,
|
||||
IRLifecycleFileDeclaration lifecycleDeclaration,
|
||||
IRRegistry registry) {
|
||||
|
||||
public IRBackendFile(
|
||||
FileId fileId,
|
||||
ReadOnlyList<IRFunction> functions,
|
||||
ReadOnlyList<IRSyntheticFunction> syntheticFunctions,
|
||||
@@ -19,7 +33,11 @@ public record IRBackendFile(
|
||||
ReadOnlyList<CallableSignatureRef> callableSignatures,
|
||||
ReadOnlyList<IntrinsicReference> intrinsicPool,
|
||||
IRLifecycleFileDeclaration lifecycleDeclaration) {
|
||||
this(fileId, functions, syntheticFunctions, globals, executableFunctions, reservedMetadata, modulePool, callableSignatures, intrinsicPool, lifecycleDeclaration, IRRegistry.empty());
|
||||
}
|
||||
|
||||
public IRBackendFile {
|
||||
registry = registry == null ? IRRegistry.empty() : registry;
|
||||
fileId = Objects.requireNonNull(fileId, "fileId");
|
||||
functions = functions == null ? ReadOnlyList.empty() : functions;
|
||||
syntheticFunctions = syntheticFunctions == null ? ReadOnlyList.empty() : syntheticFunctions;
|
||||
|
||||
+1
@@ -7,6 +7,7 @@ import p.studio.compiler.source.tables.ModuleReference;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
public interface IRBackendReader {
|
||||
p.studio.compiler.models.registry.IRRegistry getRegistry();
|
||||
String getEntryPointCallableName();
|
||||
ModuleId getEntryPointModuleId();
|
||||
ReadOnlyList<IRFunction> getFunctions();
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package p.studio.compiler.models.registry;
|
||||
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Frontend-neutral declarations. References are stable keys, never Java object identity or image IDs. */
|
||||
public record IRRegistry(ReadOnlyList<TypeDeclaration> types, ReadOnlyList<Implementation> implementations) {
|
||||
public IRRegistry {
|
||||
types = copy(types);
|
||||
implementations = copy(implementations);
|
||||
}
|
||||
public static IRRegistry empty() { return new IRRegistry(null, null); }
|
||||
private static <T> ReadOnlyList<T> copy(ReadOnlyList<T> values) {
|
||||
return values == null ? ReadOnlyList.empty() : ReadOnlyList.wrap(values.asList());
|
||||
}
|
||||
public record TypeRef(String declarationKey) {
|
||||
public TypeRef {
|
||||
Objects.requireNonNull(declarationKey, "declarationKey");
|
||||
if (declarationKey.isBlank()) throw new IllegalArgumentException("empty type declaration key");
|
||||
}
|
||||
public static TypeRef builtin(int id) { return new TypeRef("builtin:" + id); }
|
||||
}
|
||||
public record TypeDeclaration(TypeRef identity, String name, TypeBody body) {
|
||||
public TypeDeclaration { Objects.requireNonNull(identity); Objects.requireNonNull(body); }
|
||||
}
|
||||
public sealed interface TypeBody permits Builtin, Struct, Array, Trait, Fn {}
|
||||
public record Builtin(int id) implements TypeBody {
|
||||
public Builtin {
|
||||
switch (id) {
|
||||
case 1, 2, 3, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 -> { }
|
||||
default -> throw new IllegalArgumentException("unsupported BuiltinId: " + id);
|
||||
}
|
||||
}
|
||||
}
|
||||
public record Struct(ReadOnlyList<NamedType> fields) implements TypeBody {
|
||||
public Struct { fields = copy(fields); }
|
||||
}
|
||||
public record Array(TypeRef element) implements TypeBody {
|
||||
public Array { Objects.requireNonNull(element); }
|
||||
}
|
||||
public record Trait(ReadOnlyList<Method> methods) implements TypeBody {
|
||||
public Trait { methods = copy(methods); }
|
||||
}
|
||||
public record Fn(Signature signature) implements TypeBody {
|
||||
public Fn { Objects.requireNonNull(signature); }
|
||||
}
|
||||
public record NamedType(TypeRef type, String name) {
|
||||
public NamedType { Objects.requireNonNull(type); }
|
||||
}
|
||||
public record Signature(ReadOnlyList<NamedType> parameters, ReadOnlyList<TypeRef> results) {
|
||||
public Signature { parameters = copy(parameters); results = copy(results); }
|
||||
public static Signature empty() { return new Signature(null, null); }
|
||||
}
|
||||
public record Method(String name, Signature signature) {
|
||||
public Method { Objects.requireNonNull(signature); }
|
||||
}
|
||||
public record Implementation(String declarationKey, TypeRef owner, TypeRef trait, ReadOnlyList<CallableId> methods) {
|
||||
public Implementation {
|
||||
Objects.requireNonNull(declarationKey);
|
||||
if (declarationKey.isBlank()) throw new IllegalArgumentException("empty implementation key");
|
||||
Objects.requireNonNull(owner); Objects.requireNonNull(trait);
|
||||
methods = copy(methods);
|
||||
}
|
||||
}
|
||||
public enum Role {
|
||||
NONE(0), BEFORE_PAUSE(1), BEFORE_RESUME(2), MEMCARD_BOOT(3);
|
||||
private final int wire;
|
||||
Role(int wire) { this.wire = wire; }
|
||||
public int wire() { return wire; }
|
||||
}
|
||||
public record FunctionMetadata(Signature signature, ReadOnlyList<TypeRef> captures, Role role, String name) {
|
||||
public FunctionMetadata(Signature signature, ReadOnlyList<TypeRef> captures, Role role) {
|
||||
this(signature, captures, role, null);
|
||||
}
|
||||
public FunctionMetadata {
|
||||
Objects.requireNonNull(signature); captures = copy(captures); Objects.requireNonNull(role);
|
||||
}
|
||||
public static FunctionMetadata ordinary(Signature signature) { return new FunctionMetadata(signature, null, Role.NONE); }
|
||||
public static FunctionMetadata empty() { return ordinary(Signature.empty()); }
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package p.studio.compiler.models.registry;
|
||||
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Semantic Registry operands before image linking. No frontend syntax is exposed. */
|
||||
public record IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegistry.TypeRef targetType,
|
||||
CallableId function, int member) {
|
||||
public enum Kind {
|
||||
MAKE_CLOSURE(0x52), CALL_CLOSURE(0x53), GET_CAPTURE(0x57), CALL_TRAIT(0x58),
|
||||
DYN_PACK(0x90), DYN_UNPACK(0x91), DYN_CAN_UNPACK(0x92),
|
||||
REF_IS_EXACT(0x93), REF_IMPLEMENTS(0x94), REF_TO_TRAIT(0x95), REF_CAST(0x96),
|
||||
STRUCT_NEW(0xa0), STRUCT_GET(0xa1), STRUCT_SET(0xa2),
|
||||
ARRAY_NEW(0xa3), ARRAY_LEN(0xa4), ARRAY_GET(0xa5), ARRAY_SET(0xa6);
|
||||
private final int opcode;
|
||||
Kind(int opcode) { this.opcode=opcode; }
|
||||
public int opcode(){return opcode;}
|
||||
}
|
||||
public IRRegistryInstruction {
|
||||
Objects.requireNonNull(kind);
|
||||
if(member<0)throw new IllegalArgumentException("negative member index");
|
||||
if(kind!=Kind.GET_CAPTURE)Objects.requireNonNull(type,"type");
|
||||
if(kind==Kind.MAKE_CLOSURE) {Objects.requireNonNull(function,"function");function.getIndex();}
|
||||
else if(function!=null)throw new IllegalArgumentException("unexpected function operand");
|
||||
boolean pair=kind.opcode()>=0x93&&kind.opcode()<=0x96;
|
||||
if(pair)Objects.requireNonNull(targetType,"targetType");
|
||||
else if(targetType!=null)throw new IllegalArgumentException("unexpected target type");
|
||||
if(kind==Kind.GET_CAPTURE&&type!=null)throw new IllegalArgumentException("capture index is not a TypeId");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user