implements PR-05.2
This commit is contained in:
parent
541ada32a1
commit
9c294774db
@ -183,14 +183,18 @@ public final class PbsFrontendCompiler {
|
|||||||
final NameTable nameTable,
|
final NameTable nameTable,
|
||||||
final DiagnosticSink diagnostics) {
|
final DiagnosticSink diagnostics) {
|
||||||
final var normalizedModuleKey = moduleKey == null ? "" : moduleKey;
|
final var normalizedModuleKey = moduleKey == null ? "" : moduleKey;
|
||||||
final var hostByMethodName = new HashMap<String, IRReservedMetadata.HostMethodBinding>();
|
final var hostByMethodName = new HashMap<NameId, List<IRReservedMetadata.HostMethodBinding>>();
|
||||||
for (final var hostBinding : reservedMetadata.hostMethodBindings()) {
|
for (final var hostBinding : reservedMetadata.hostMethodBindings()) {
|
||||||
hostByMethodName.put(hostBinding.sourceMethodName(), hostBinding);
|
hostByMethodName
|
||||||
|
.computeIfAbsent(nameTable.register(hostBinding.sourceMethodName()), ignored -> new ArrayList<>())
|
||||||
|
.add(hostBinding);
|
||||||
}
|
}
|
||||||
final var intrinsicByMethodName = new HashMap<String, IRReservedMetadata.IntrinsicSurface>();
|
final var intrinsicByMethodName = new HashMap<NameId, List<IRReservedMetadata.IntrinsicSurface>>();
|
||||||
for (final var builtinType : reservedMetadata.builtinTypeSurfaces()) {
|
for (final var builtinType : reservedMetadata.builtinTypeSurfaces()) {
|
||||||
for (final var intrinsicSurface : builtinType.intrinsics()) {
|
for (final var intrinsicSurface : builtinType.intrinsics()) {
|
||||||
intrinsicByMethodName.put(intrinsicSurface.sourceMethodName(), intrinsicSurface);
|
intrinsicByMethodName
|
||||||
|
.computeIfAbsent(nameTable.register(intrinsicSurface.sourceMethodName()), ignored -> new ArrayList<>())
|
||||||
|
.add(intrinsicSurface);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final var callableIdTable = new CallableTable();
|
final var callableIdTable = new CallableTable();
|
||||||
@ -591,13 +595,41 @@ public final class PbsFrontendCompiler {
|
|||||||
private void lowerCallsite(
|
private void lowerCallsite(
|
||||||
final PbsAst.CallExpr callExpr,
|
final PbsAst.CallExpr callExpr,
|
||||||
final ExecutableLoweringContext context) {
|
final ExecutableLoweringContext context) {
|
||||||
final var calleeName = extractSimpleCalleeName(callExpr.callee());
|
final var calleeIdentity = resolveCalleeIdentity(callExpr.callee(), context.nameTable());
|
||||||
if (calleeName == null || calleeName.isBlank()) {
|
if (calleeIdentity == null) {
|
||||||
reportUnsupportedLowering("executable lowering requires resolvable callee identity", callExpr.span(), context);
|
reportUnsupportedLowering("executable lowering requires resolvable callee identity", callExpr.span(), context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final var host = context.hostByMethodName().get(calleeName);
|
final var callableCandidates = context.callableIdsByNameAndArity().get(
|
||||||
if (host != null) {
|
new CallableResolutionKey(calleeIdentity.nameId(), callExpr.arguments().size()));
|
||||||
|
final var hostCandidates = context.hostByMethodName().getOrDefault(calleeIdentity.nameId(), List.of());
|
||||||
|
final var intrinsicCandidates = context.intrinsicByMethodName().getOrDefault(calleeIdentity.nameId(), List.of());
|
||||||
|
|
||||||
|
var categoryCount = 0;
|
||||||
|
if (callableCandidates != null && !callableCandidates.isEmpty()) {
|
||||||
|
categoryCount++;
|
||||||
|
}
|
||||||
|
if (!hostCandidates.isEmpty()) {
|
||||||
|
categoryCount++;
|
||||||
|
}
|
||||||
|
if (!intrinsicCandidates.isEmpty()) {
|
||||||
|
categoryCount++;
|
||||||
|
}
|
||||||
|
if (categoryCount == 0) {
|
||||||
|
reportUnsupportedLowering("executable lowering requires resolvable callee identity", callExpr.span(), context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (categoryCount > 1) {
|
||||||
|
reportUnsupportedLowering("executable lowering found ambiguous callsite category", callExpr.span(), context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hostCandidates.isEmpty()) {
|
||||||
|
if (hostCandidates.size() > 1) {
|
||||||
|
reportUnsupportedLowering("executable lowering found ambiguous host binding identity", callExpr.span(), context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final var host = hostCandidates.getFirst();
|
||||||
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
||||||
IRBackendExecutableFunction.InstructionKind.CALL_HOST,
|
IRBackendExecutableFunction.InstructionKind.CALL_HOST,
|
||||||
"",
|
"",
|
||||||
@ -613,8 +645,12 @@ public final class PbsFrontendCompiler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var intrinsic = context.intrinsicByMethodName().get(calleeName);
|
if (!intrinsicCandidates.isEmpty()) {
|
||||||
if (intrinsic != null) {
|
if (intrinsicCandidates.size() > 1) {
|
||||||
|
reportUnsupportedLowering("executable lowering found ambiguous intrinsic identity", callExpr.span(), context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final var intrinsic = intrinsicCandidates.getFirst();
|
||||||
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
||||||
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
|
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
|
||||||
"",
|
"",
|
||||||
@ -628,21 +664,19 @@ public final class PbsFrontendCompiler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var candidateCallableIds = context.callableIdsByNameAndArity().get(
|
if (callableCandidates == null || callableCandidates.isEmpty()) {
|
||||||
new CallableResolutionKey(context.nameTable().register(calleeName), callExpr.arguments().size()));
|
reportUnsupportedLowering("executable lowering requires resolvable callable identity", callExpr.span(), context);
|
||||||
if (candidateCallableIds == null || candidateCallableIds.isEmpty()) {
|
|
||||||
reportUnsupportedLowering("executable lowering requires resolvable callee identity", callExpr.span(), context);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (candidateCallableIds.size() > 1) {
|
if (callableCandidates.size() > 1) {
|
||||||
reportUnsupportedLowering("executable lowering found ambiguous callable identity", callExpr.span(), context);
|
reportUnsupportedLowering("executable lowering found ambiguous callable identity", callExpr.span(), context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final var calleeCallableId = candidateCallableIds.getFirst();
|
final var calleeCallableId = callableCandidates.getFirst();
|
||||||
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
||||||
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
|
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
|
||||||
context.moduleKey(),
|
context.moduleKey(),
|
||||||
calleeName,
|
calleeIdentity.displayName(),
|
||||||
calleeCallableId,
|
calleeCallableId,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
@ -651,6 +685,21 @@ public final class PbsFrontendCompiler {
|
|||||||
callExpr.span()));
|
callExpr.span()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CalleeIdentity resolveCalleeIdentity(
|
||||||
|
final PbsAst.Expression callee,
|
||||||
|
final NameTable nameTable) {
|
||||||
|
return switch (callee) {
|
||||||
|
case PbsAst.IdentifierExpr identifierExpr ->
|
||||||
|
new CalleeIdentity(nameTable.register(identifierExpr.name()), identifierExpr.name());
|
||||||
|
case PbsAst.MemberExpr memberExpr ->
|
||||||
|
new CalleeIdentity(nameTable.register(memberExpr.memberName()), memberExpr.memberName());
|
||||||
|
case PbsAst.BindExpr bindExpr ->
|
||||||
|
new CalleeIdentity(nameTable.register(bindExpr.functionName()), bindExpr.functionName());
|
||||||
|
case PbsAst.GroupExpr groupExpr -> resolveCalleeIdentity(groupExpr.expression(), nameTable);
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private void emitJump(
|
private void emitJump(
|
||||||
final IRBackendExecutableFunction.InstructionKind jumpKind,
|
final IRBackendExecutableFunction.InstructionKind jumpKind,
|
||||||
final String targetLabel,
|
final String targetLabel,
|
||||||
@ -701,16 +750,6 @@ public final class PbsFrontendCompiler {
|
|||||||
span);
|
span);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractSimpleCalleeName(final PbsAst.Expression callee) {
|
|
||||||
return switch (callee) {
|
|
||||||
case PbsAst.IdentifierExpr identifierExpr -> identifierExpr.name();
|
|
||||||
case PbsAst.MemberExpr memberExpr -> memberExpr.memberName();
|
|
||||||
case PbsAst.BindExpr bindExpr -> bindExpr.functionName();
|
|
||||||
case PbsAst.GroupExpr groupExpr -> extractSimpleCalleeName(groupExpr.expression());
|
|
||||||
default -> null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private int analyzeMaxStackSlots(
|
private int analyzeMaxStackSlots(
|
||||||
final ReadOnlyList<IRBackendExecutableFunction.Instruction> instructions,
|
final ReadOnlyList<IRBackendExecutableFunction.Instruction> instructions,
|
||||||
final int returnSlots) {
|
final int returnSlots) {
|
||||||
@ -812,8 +851,8 @@ public final class PbsFrontendCompiler {
|
|||||||
private final String moduleKey;
|
private final String moduleKey;
|
||||||
private final DiagnosticSink diagnostics;
|
private final DiagnosticSink diagnostics;
|
||||||
private final NameTable nameTable;
|
private final NameTable nameTable;
|
||||||
private final Map<String, IRReservedMetadata.HostMethodBinding> hostByMethodName;
|
private final Map<NameId, List<IRReservedMetadata.HostMethodBinding>> hostByMethodName;
|
||||||
private final Map<String, IRReservedMetadata.IntrinsicSurface> intrinsicByMethodName;
|
private final Map<NameId, List<IRReservedMetadata.IntrinsicSurface>> intrinsicByMethodName;
|
||||||
private final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity;
|
private final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity;
|
||||||
private final Map<CallableId, Integer> returnSlotsByCallableId;
|
private final Map<CallableId, Integer> returnSlotsByCallableId;
|
||||||
private final IntrinsicTable intrinsicIdTable;
|
private final IntrinsicTable intrinsicIdTable;
|
||||||
@ -825,8 +864,8 @@ public final class PbsFrontendCompiler {
|
|||||||
final String moduleKey,
|
final String moduleKey,
|
||||||
final DiagnosticSink diagnostics,
|
final DiagnosticSink diagnostics,
|
||||||
final NameTable nameTable,
|
final NameTable nameTable,
|
||||||
final Map<String, IRReservedMetadata.HostMethodBinding> hostByMethodName,
|
final Map<NameId, List<IRReservedMetadata.HostMethodBinding>> hostByMethodName,
|
||||||
final Map<String, IRReservedMetadata.IntrinsicSurface> intrinsicByMethodName,
|
final Map<NameId, List<IRReservedMetadata.IntrinsicSurface>> intrinsicByMethodName,
|
||||||
final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity,
|
final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity,
|
||||||
final Map<CallableId, Integer> returnSlotsByCallableId,
|
final Map<CallableId, Integer> returnSlotsByCallableId,
|
||||||
final IntrinsicTable intrinsicIdTable) {
|
final IntrinsicTable intrinsicIdTable) {
|
||||||
@ -852,11 +891,11 @@ public final class PbsFrontendCompiler {
|
|||||||
return nameTable;
|
return nameTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, IRReservedMetadata.HostMethodBinding> hostByMethodName() {
|
private Map<NameId, List<IRReservedMetadata.HostMethodBinding>> hostByMethodName() {
|
||||||
return hostByMethodName;
|
return hostByMethodName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, IRReservedMetadata.IntrinsicSurface> intrinsicByMethodName() {
|
private Map<NameId, List<IRReservedMetadata.IntrinsicSurface>> intrinsicByMethodName() {
|
||||||
return intrinsicByMethodName;
|
return intrinsicByMethodName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -911,6 +950,11 @@ public final class PbsFrontendCompiler {
|
|||||||
int arity) {
|
int arity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record CalleeIdentity(
|
||||||
|
NameId nameId,
|
||||||
|
String displayName) {
|
||||||
|
}
|
||||||
|
|
||||||
private static final class ExecutableLoweringAnalysisException extends RuntimeException {
|
private static final class ExecutableLoweringAnalysisException extends RuntimeException {
|
||||||
private ExecutableLoweringAnalysisException(final String message) {
|
private ExecutableLoweringAnalysisException(final String message) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user