implements PR-08.2
This commit is contained in:
+98
-4
@@ -4,14 +4,18 @@ import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
import p.studio.compiler.source.identifiers.IntrinsicId;
|
||||
import p.studio.compiler.source.identifiers.ModuleId;
|
||||
import p.studio.compiler.source.tables.CallableSignatureRef;
|
||||
import p.studio.compiler.source.tables.CallableTable;
|
||||
import p.studio.compiler.source.tables.IntrinsicReference;
|
||||
import p.studio.compiler.source.tables.IntrinsicTable;
|
||||
import p.studio.compiler.source.tables.ModuleReference;
|
||||
import p.studio.compiler.source.tables.ModuleTable;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
@@ -23,6 +27,8 @@ public class IRBackend {
|
||||
@Builder.Default
|
||||
private final ReadOnlyList<IRBackendExecutableFunction> executableFunctions = ReadOnlyList.empty();
|
||||
@Builder.Default
|
||||
private final ReadOnlyList<ModuleReference> modulePool = ReadOnlyList.empty();
|
||||
@Builder.Default
|
||||
private final ReadOnlyList<CallableSignatureRef> callableSignatures = ReadOnlyList.empty();
|
||||
@Builder.Default
|
||||
private final ReadOnlyList<IntrinsicReference> intrinsicPool = ReadOnlyList.empty();
|
||||
@@ -37,6 +43,7 @@ public class IRBackend {
|
||||
private String entryPointCallableName;
|
||||
private final ArrayList<IRFunction> functions = new ArrayList<>();
|
||||
private final ArrayList<IRBackendExecutableFunction> executableFunctions = new ArrayList<>();
|
||||
private final ModuleTable moduleTable = new ModuleTable();
|
||||
private final CallableTable callableTable = new CallableTable();
|
||||
private final IntrinsicTable intrinsicTable = new IntrinsicTable();
|
||||
private final ArrayList<IRReservedMetadata.HostMethodBinding> hostMethodBindings = new ArrayList<>();
|
||||
@@ -49,10 +56,11 @@ public class IRBackend {
|
||||
return;
|
||||
}
|
||||
functions.addAll(backendFile.functions().asList());
|
||||
final var callableRemap = reindexCallables(backendFile.callableSignatures());
|
||||
final var moduleRemap = reindexModules(backendFile.modulePool());
|
||||
final var callableRemap = reindexCallables(backendFile.callableSignatures(), moduleRemap);
|
||||
final var intrinsicRemap = reindexIntrinsics(backendFile.intrinsicPool());
|
||||
for (final var function : backendFile.executableFunctions()) {
|
||||
executableFunctions.add(remapExecutableFunction(function, callableRemap, intrinsicRemap));
|
||||
executableFunctions.add(remapExecutableFunction(function, moduleRemap, callableRemap, intrinsicRemap));
|
||||
}
|
||||
final var metadata = backendFile.reservedMetadata();
|
||||
hostMethodBindings.addAll(metadata.hostMethodBindings().asList());
|
||||
@@ -68,22 +76,52 @@ public class IRBackend {
|
||||
return this;
|
||||
}
|
||||
|
||||
private CallableId[] reindexCallables(final ReadOnlyList<CallableSignatureRef> localCallableSignatures) {
|
||||
private ModuleId[] reindexModules(final ReadOnlyList<ModuleReference> localModulePool) {
|
||||
if (localModulePool == null || localModulePool.isEmpty()) {
|
||||
return new ModuleId[0];
|
||||
}
|
||||
final var remap = new ModuleId[localModulePool.size()];
|
||||
for (var i = 0; i < localModulePool.size(); i++) {
|
||||
remap[i] = moduleTable.register(localModulePool.get(i));
|
||||
}
|
||||
return remap;
|
||||
}
|
||||
|
||||
private CallableId[] reindexCallables(
|
||||
final ReadOnlyList<CallableSignatureRef> localCallableSignatures,
|
||||
final ModuleId[] moduleRemap) {
|
||||
if (localCallableSignatures == null || localCallableSignatures.isEmpty()) {
|
||||
return new CallableId[0];
|
||||
}
|
||||
final var remap = new CallableId[localCallableSignatures.size()];
|
||||
for (var i = 0; i < localCallableSignatures.size(); i++) {
|
||||
remap[i] = callableTable.register(localCallableSignatures.get(i));
|
||||
final var signature = localCallableSignatures.get(i);
|
||||
final var remappedModuleId = remapModuleId(
|
||||
signature.moduleId(),
|
||||
signature.moduleKey(),
|
||||
moduleRemap,
|
||||
"callable signature");
|
||||
remap[i] = callableTable.register(
|
||||
remappedModuleId,
|
||||
signature.moduleKey(),
|
||||
signature.callableName(),
|
||||
signature.arity(),
|
||||
signature.typeShape());
|
||||
}
|
||||
return remap;
|
||||
}
|
||||
|
||||
private IRBackendExecutableFunction remapExecutableFunction(
|
||||
final IRBackendExecutableFunction function,
|
||||
final ModuleId[] moduleRemap,
|
||||
final CallableId[] callableRemap,
|
||||
final IntrinsicId[] intrinsicRemap) {
|
||||
final var remappedInstructions = new ArrayList<IRBackendExecutableFunction.Instruction>(function.instructions().size());
|
||||
final var remappedFunctionModuleId = remapModuleId(
|
||||
function.moduleId(),
|
||||
function.moduleKey(),
|
||||
moduleRemap,
|
||||
"function");
|
||||
for (final var instruction : function.instructions()) {
|
||||
final CallableId remappedCallee;
|
||||
if (instruction.calleeCallableId() == null) {
|
||||
@@ -101,8 +139,14 @@ public class IRBackend {
|
||||
} else {
|
||||
remappedIntrinsic = instruction.intrinsicCall();
|
||||
}
|
||||
final var remappedCalleeModuleId = remapModuleId(
|
||||
instruction.calleeModuleId(),
|
||||
instruction.calleeModuleKey(),
|
||||
moduleRemap,
|
||||
"callee");
|
||||
remappedInstructions.add(new IRBackendExecutableFunction.Instruction(
|
||||
instruction.kind(),
|
||||
remappedCalleeModuleId,
|
||||
instruction.calleeModuleKey(),
|
||||
instruction.calleeCallableName(),
|
||||
remappedCallee,
|
||||
@@ -116,6 +160,7 @@ public class IRBackend {
|
||||
}
|
||||
return new IRBackendExecutableFunction(
|
||||
function.fileId(),
|
||||
remappedFunctionModuleId,
|
||||
function.moduleKey(),
|
||||
function.callableName(),
|
||||
remapCallableId(function.callableId(), callableRemap, "function"),
|
||||
@@ -129,6 +174,45 @@ public class IRBackend {
|
||||
function.span());
|
||||
}
|
||||
|
||||
private ModuleId remapModuleId(
|
||||
final ModuleId localModuleId,
|
||||
final String moduleKey,
|
||||
final ModuleId[] moduleRemap,
|
||||
final String label) {
|
||||
if (localModuleId != null && !localModuleId.isNone()) {
|
||||
final var localIndex = localModuleId.getIndex();
|
||||
if (localIndex < 0 || localIndex >= moduleRemap.length) {
|
||||
throw new IllegalArgumentException("invalid local " + label + " module id: " + localModuleId);
|
||||
}
|
||||
return moduleRemap[localIndex];
|
||||
}
|
||||
final var fallbackReference = moduleReferenceFromKey(moduleKey);
|
||||
if (fallbackReference != null) {
|
||||
return moduleTable.register(fallbackReference);
|
||||
}
|
||||
return ModuleId.none();
|
||||
}
|
||||
|
||||
private ModuleReference moduleReferenceFromKey(final String moduleKey) {
|
||||
if (moduleKey == null || moduleKey.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
final var separator = moduleKey.indexOf(':');
|
||||
if (separator <= 0) {
|
||||
return null;
|
||||
}
|
||||
final var project = moduleKey.substring(0, separator).trim();
|
||||
final var rawPath = moduleKey.substring(separator + 1).trim();
|
||||
if (project.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
if (rawPath.isBlank()) {
|
||||
return new ModuleReference(project, ReadOnlyList.empty());
|
||||
}
|
||||
final var segments = List.of(rawPath.split("/"));
|
||||
return new ModuleReference(project, ReadOnlyList.wrap(segments));
|
||||
}
|
||||
|
||||
private CallableId remapCallableId(
|
||||
final CallableId localCallableId,
|
||||
final CallableId[] callableRemap,
|
||||
@@ -169,6 +253,14 @@ public class IRBackend {
|
||||
return ReadOnlyList.wrap(signatures);
|
||||
}
|
||||
|
||||
private ReadOnlyList<ModuleReference> emitModulePool() {
|
||||
final var modules = new ArrayList<ModuleReference>(moduleTable.size());
|
||||
for (final var moduleId : moduleTable.identifiers()) {
|
||||
modules.add(moduleTable.get(moduleId));
|
||||
}
|
||||
return ReadOnlyList.wrap(modules);
|
||||
}
|
||||
|
||||
private ReadOnlyList<IntrinsicReference> emitIntrinsicPool() {
|
||||
final var pool = new ArrayList<IntrinsicReference>(intrinsicTable.size());
|
||||
for (final var intrinsicId : intrinsicTable.identifiers()) {
|
||||
@@ -183,6 +275,7 @@ public class IRBackend {
|
||||
.entryPointCallableName(resolveEntryPointCallableName())
|
||||
.functions(ReadOnlyList.wrap(functions))
|
||||
.executableFunctions(ReadOnlyList.wrap(executableFunctions))
|
||||
.modulePool(emitModulePool())
|
||||
.callableSignatures(emitCallableSignatures())
|
||||
.intrinsicPool(emitIntrinsicPool())
|
||||
.reservedMetadata(new IRReservedMetadata(
|
||||
@@ -206,6 +299,7 @@ public class IRBackend {
|
||||
sb.append("IRBackend{entrypoint=").append(entryPointCallableName)
|
||||
.append(", functions=").append(functions.size())
|
||||
.append(", executableFunctions=").append(executableFunctions.size())
|
||||
.append(", modulePool=").append(modulePool.size())
|
||||
.append(", callableSignatures=").append(callableSignatures.size())
|
||||
.append(", intrinsicPool=").append(intrinsicPool.size())
|
||||
.append(", hostBindings=").append(reservedMetadata.hostMethodBindings().size())
|
||||
|
||||
+109
-4
@@ -4,12 +4,14 @@ import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.IntrinsicId;
|
||||
import p.studio.compiler.source.identifiers.ModuleId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record IRBackendExecutableFunction(
|
||||
FileId fileId,
|
||||
ModuleId moduleId,
|
||||
String moduleKey,
|
||||
String callableName,
|
||||
CallableId callableId,
|
||||
@@ -22,8 +24,41 @@ public record IRBackendExecutableFunction(
|
||||
ReadOnlyList<Instruction> instructions,
|
||||
Span span) {
|
||||
|
||||
public IRBackendExecutableFunction(
|
||||
final FileId fileId,
|
||||
final String moduleKey,
|
||||
final String callableName,
|
||||
final CallableId callableId,
|
||||
final int sourceStart,
|
||||
final int sourceEnd,
|
||||
final int paramSlots,
|
||||
final int localSlots,
|
||||
final int returnSlots,
|
||||
final int maxStackSlots,
|
||||
final ReadOnlyList<Instruction> instructions,
|
||||
final Span span) {
|
||||
this(
|
||||
fileId,
|
||||
ModuleId.none(),
|
||||
moduleKey,
|
||||
callableName,
|
||||
callableId,
|
||||
sourceStart,
|
||||
sourceEnd,
|
||||
paramSlots,
|
||||
localSlots,
|
||||
returnSlots,
|
||||
maxStackSlots,
|
||||
instructions,
|
||||
span);
|
||||
}
|
||||
|
||||
public IRBackendExecutableFunction {
|
||||
fileId = Objects.requireNonNull(fileId, "fileId");
|
||||
moduleId = moduleId == null ? ModuleId.none() : moduleId;
|
||||
if (!moduleId.isNone()) {
|
||||
moduleId.getIndex();
|
||||
}
|
||||
moduleKey = moduleKey == null ? "" : moduleKey;
|
||||
callableName = Objects.requireNonNull(callableName, "callableName");
|
||||
if (callableName.isBlank()) {
|
||||
@@ -52,6 +87,7 @@ public record IRBackendExecutableFunction(
|
||||
|
||||
public record Instruction(
|
||||
InstructionKind kind,
|
||||
ModuleId calleeModuleId,
|
||||
String calleeModuleKey,
|
||||
String calleeCallableName,
|
||||
CallableId calleeCallableId,
|
||||
@@ -64,13 +100,78 @@ public record IRBackendExecutableFunction(
|
||||
Span span) {
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final ModuleId calleeModuleId,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final CallableId calleeCallableId,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
this(kind, calleeModuleId, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final ModuleId calleeModuleId,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final CallableId calleeCallableId,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Integer expectedArgSlots,
|
||||
final Integer expectedRetSlots,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleId, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final ModuleId calleeModuleId,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleId, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final ModuleId calleeModuleId,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Integer expectedArgSlots,
|
||||
final Integer expectedRetSlots,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleId, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final CallableId calleeCallableId,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final String label,
|
||||
final String targetLabel,
|
||||
final Integer expectedArgSlots,
|
||||
final Integer expectedRetSlots,
|
||||
final Span span) {
|
||||
this(kind, ModuleId.none(), calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, label, targetLabel, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
final InstructionKind kind,
|
||||
final String calleeModuleKey,
|
||||
final String calleeCallableName,
|
||||
final CallableId calleeCallableId,
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Span span) {
|
||||
this(kind, ModuleId.none(), calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
@@ -83,7 +184,7 @@ public record IRBackendExecutableFunction(
|
||||
final Integer expectedArgSlots,
|
||||
final Integer expectedRetSlots,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
this(kind, ModuleId.none(), calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
@@ -93,7 +194,7 @@ public record IRBackendExecutableFunction(
|
||||
final HostCallMetadata hostCall,
|
||||
final IntrinsicCallMetadata intrinsicCall,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
this(kind, ModuleId.none(), calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, null, null, span);
|
||||
}
|
||||
|
||||
public Instruction(
|
||||
@@ -105,12 +206,16 @@ public record IRBackendExecutableFunction(
|
||||
final Integer expectedArgSlots,
|
||||
final Integer expectedRetSlots,
|
||||
final Span span) {
|
||||
this(kind, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
this(kind, ModuleId.none(), calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public Instruction {
|
||||
Objects.requireNonNull(kind, "kind");
|
||||
span = span == null ? Span.none() : span;
|
||||
calleeModuleId = calleeModuleId == null ? ModuleId.none() : calleeModuleId;
|
||||
if (!calleeModuleId.isNone()) {
|
||||
calleeModuleId.getIndex();
|
||||
}
|
||||
calleeModuleKey = calleeModuleKey == null ? "" : calleeModuleKey;
|
||||
calleeCallableName = calleeCallableName == null ? "" : calleeCallableName;
|
||||
label = label == null ? "" : label;
|
||||
|
||||
+44
-3
@@ -3,6 +3,7 @@ package p.studio.compiler.models;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.tables.CallableSignatureRef;
|
||||
import p.studio.compiler.source.tables.IntrinsicReference;
|
||||
import p.studio.compiler.source.tables.ModuleReference;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.Objects;
|
||||
@@ -12,6 +13,7 @@ public record IRBackendFile(
|
||||
ReadOnlyList<IRFunction> functions,
|
||||
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
|
||||
IRReservedMetadata reservedMetadata,
|
||||
ReadOnlyList<ModuleReference> modulePool,
|
||||
ReadOnlyList<CallableSignatureRef> callableSignatures,
|
||||
ReadOnlyList<IntrinsicReference> intrinsicPool) {
|
||||
public IRBackendFile {
|
||||
@@ -19,6 +21,7 @@ public record IRBackendFile(
|
||||
functions = functions == null ? ReadOnlyList.empty() : functions;
|
||||
executableFunctions = executableFunctions == null ? ReadOnlyList.empty() : executableFunctions;
|
||||
reservedMetadata = reservedMetadata == null ? IRReservedMetadata.empty() : reservedMetadata;
|
||||
modulePool = modulePool == null ? ReadOnlyList.empty() : modulePool;
|
||||
callableSignatures = callableSignatures == null ? ReadOnlyList.empty() : callableSignatures;
|
||||
intrinsicPool = intrinsicPool == null ? ReadOnlyList.empty() : intrinsicPool;
|
||||
}
|
||||
@@ -26,17 +29,55 @@ public record IRBackendFile(
|
||||
public IRBackendFile(
|
||||
final FileId fileId,
|
||||
final ReadOnlyList<IRFunction> functions) {
|
||||
this(fileId, functions, ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty(), ReadOnlyList.empty());
|
||||
this(
|
||||
fileId,
|
||||
functions,
|
||||
ReadOnlyList.empty(),
|
||||
IRReservedMetadata.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty());
|
||||
}
|
||||
|
||||
public static IRBackendFile empty(final FileId fileId) {
|
||||
return new IRBackendFile(fileId, ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty(), ReadOnlyList.empty());
|
||||
return new IRBackendFile(
|
||||
fileId,
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
IRReservedMetadata.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty());
|
||||
}
|
||||
|
||||
public IRBackendFile(
|
||||
final FileId fileId,
|
||||
final ReadOnlyList<IRFunction> functions,
|
||||
final IRReservedMetadata reservedMetadata) {
|
||||
this(fileId, functions, ReadOnlyList.empty(), reservedMetadata, ReadOnlyList.empty(), ReadOnlyList.empty());
|
||||
this(
|
||||
fileId,
|
||||
functions,
|
||||
ReadOnlyList.empty(),
|
||||
reservedMetadata,
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty());
|
||||
}
|
||||
|
||||
public IRBackendFile(
|
||||
final FileId fileId,
|
||||
final ReadOnlyList<IRFunction> functions,
|
||||
final ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
|
||||
final IRReservedMetadata reservedMetadata,
|
||||
final ReadOnlyList<CallableSignatureRef> callableSignatures,
|
||||
final ReadOnlyList<IntrinsicReference> intrinsicPool) {
|
||||
this(
|
||||
fileId,
|
||||
functions,
|
||||
executableFunctions,
|
||||
reservedMetadata,
|
||||
ReadOnlyList.empty(),
|
||||
callableSignatures,
|
||||
intrinsicPool);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user