implements PLN-0008
This commit is contained in:
+12
@@ -15,6 +15,7 @@ public final class PbsReservedMetadataExtractor {
|
||||
private static final String ATTR_BUILTIN_TYPE = "BuiltinType";
|
||||
private static final String ATTR_INTRINSIC_CALL = "IntrinsicCall";
|
||||
private static final String ATTR_BUILTIN_CONST = "BuiltinConst";
|
||||
private static final String ATTR_ASSET_LOWERING = "AssetLowering";
|
||||
|
||||
public IRReservedMetadata extract(
|
||||
final PbsAst.File ast,
|
||||
@@ -68,6 +69,7 @@ public final class PbsReservedMetadataExtractor {
|
||||
final var capability = capabilityAttribute
|
||||
.flatMap(attr -> stringArgument(attr, "name"))
|
||||
.orElse("");
|
||||
final var assetLoweringAttribute = firstAttributeNamed(signature.attributes(), ATTR_ASSET_LOWERING);
|
||||
hostMethodBindings.add(new IRReservedMetadata.HostMethodBinding(
|
||||
hostDecl.name(),
|
||||
signature.name(),
|
||||
@@ -78,6 +80,9 @@ public final class PbsReservedMetadataExtractor {
|
||||
case INFERRED_UNIT, EXPLICIT_UNIT -> 0;
|
||||
case PLAIN, RESULT -> 1;
|
||||
},
|
||||
assetLoweringAttribute.isPresent()
|
||||
? safeToInteger(longArgument(assetLoweringAttribute.get(), "param").orElse(-1L))
|
||||
: null,
|
||||
capabilityAttribute.isPresent(),
|
||||
capability,
|
||||
signature.span()));
|
||||
@@ -261,4 +266,11 @@ public final class PbsReservedMetadataExtractor {
|
||||
}
|
||||
return ReadOnlyList.wrap(required.stream().toList());
|
||||
}
|
||||
|
||||
private Integer safeToInteger(final long value) {
|
||||
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
|
||||
return null;
|
||||
}
|
||||
return (int) value;
|
||||
}
|
||||
}
|
||||
|
||||
+39
-1
@@ -415,10 +415,28 @@ final class PbsExecutableBodyLowerer {
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
lowerCallsiteReceiver(callExpr.callee(), context);
|
||||
lowerExpressionList(callExpr.arguments(), context);
|
||||
lowerCallArguments(callExpr, context);
|
||||
callsiteEmitter.emitCallsite(callExpr, context);
|
||||
}
|
||||
|
||||
private void lowerCallArguments(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var hostBinding = callsiteEmitter.resolveUniqueHostBinding(callExpr, context);
|
||||
if (hostBinding == null || hostBinding.assetLoweringParam() == null) {
|
||||
lowerExpressionList(callExpr.arguments(), context);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < callExpr.arguments().size(); i++) {
|
||||
final var argument = callExpr.arguments().get(i);
|
||||
if (i == hostBinding.assetLoweringParam()) {
|
||||
lowerAssetFacingArgument(argument, hostBinding, context);
|
||||
continue;
|
||||
}
|
||||
lowerExpression(argument, context);
|
||||
}
|
||||
}
|
||||
|
||||
private void lowerExpressionList(
|
||||
final ReadOnlyList<PbsAst.Expression> expressions,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
@@ -461,6 +479,26 @@ final class PbsExecutableBodyLowerer {
|
||||
lowerExpression(memberExpr.receiver(), context);
|
||||
}
|
||||
|
||||
private void lowerAssetFacingArgument(
|
||||
final PbsAst.Expression expression,
|
||||
final p.studio.compiler.models.IRReservedMetadata.HostMethodBinding hostBinding,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var assetReference = flattenAssetReference(expression);
|
||||
if (assetReference == null) {
|
||||
lowerExpression(expression, context);
|
||||
return;
|
||||
}
|
||||
final var assetId = context.resolveAssetId(assetReference);
|
||||
if (assetId == null) {
|
||||
reportUnsupportedLowering(
|
||||
"asset-facing host parameter does not resolve in backend-owned surface: " + assetReference,
|
||||
expression.span(),
|
||||
context);
|
||||
return;
|
||||
}
|
||||
emitPushI32(assetId, expression.span(), context);
|
||||
}
|
||||
|
||||
private void lowerIfExpression(
|
||||
final PbsAst.IfExpr ifExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
|
||||
+17
@@ -16,6 +16,23 @@ import java.util.Map;
|
||||
import static p.studio.compiler.pbs.lowering.PbsExecutableLoweringModels.*;
|
||||
|
||||
final class PbsExecutableCallsiteEmitter {
|
||||
IRReservedMetadata.HostMethodBinding resolveUniqueHostBinding(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var calleeIdentity = resolveCalleeIdentity(callExpr.callee(), context.nameTable());
|
||||
if (calleeIdentity == null) {
|
||||
return null;
|
||||
}
|
||||
final var candidates = resolveCallsiteCandidates(callExpr, calleeIdentity, context);
|
||||
if (!candidates.callableCandidates().isEmpty() || !candidates.intrinsicCandidates().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (candidates.hostCandidates().size() != 1) {
|
||||
return null;
|
||||
}
|
||||
return candidates.hostCandidates().getFirst();
|
||||
}
|
||||
|
||||
int returnSlotsOf(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
|
||||
+55
-2
@@ -18,6 +18,7 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
private static final String ATTR_BUILTIN_CONST = "BuiltinConst";
|
||||
private static final String ATTR_INTRINSIC_CALL = "IntrinsicCall";
|
||||
private static final String ATTR_INIT_ALLOWED = "InitAllowed";
|
||||
private static final String ATTR_ASSET_LOWERING = "AssetLowering";
|
||||
|
||||
private static final Set<String> RESERVED_ATTRIBUTES = Set.of(
|
||||
ATTR_HOST,
|
||||
@@ -25,7 +26,8 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
ATTR_BUILTIN_TYPE,
|
||||
ATTR_BUILTIN_CONST,
|
||||
ATTR_INTRINSIC_CALL,
|
||||
ATTR_INIT_ALLOWED);
|
||||
ATTR_INIT_ALLOWED,
|
||||
ATTR_ASSET_LOWERING);
|
||||
|
||||
private final NameTable nameTable;
|
||||
private final PbsConstSemanticsValidator constSemanticsValidator = new PbsConstSemanticsValidator();
|
||||
@@ -403,13 +405,15 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
final DiagnosticSink diagnostics) {
|
||||
final var hostAttributes = attributesNamed(signature.attributes(), ATTR_HOST);
|
||||
final var initAllowedAttributes = attributesNamed(signature.attributes(), ATTR_INIT_ALLOWED);
|
||||
final var assetLoweringAttributes = attributesNamed(signature.attributes(), ATTR_ASSET_LOWERING);
|
||||
for (final var attribute : signature.attributes()) {
|
||||
if (!isReservedAttribute(attribute.name())) {
|
||||
continue;
|
||||
}
|
||||
if (ATTR_HOST.equals(attribute.name())
|
||||
|| ATTR_CAPABILITY.equals(attribute.name())
|
||||
|| ATTR_INIT_ALLOWED.equals(attribute.name())) {
|
||||
|| ATTR_INIT_ALLOWED.equals(attribute.name())
|
||||
|| ATTR_ASSET_LOWERING.equals(attribute.name())) {
|
||||
continue;
|
||||
}
|
||||
reportInvalidReservedAttributeTarget(
|
||||
@@ -438,10 +442,19 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
}
|
||||
}
|
||||
|
||||
if (assetLoweringAttributes.size() > 1) {
|
||||
for (int i = 1; i < assetLoweringAttributes.size(); i++) {
|
||||
reportDuplicateReservedAttribute(assetLoweringAttributes.get(i), ATTR_ASSET_LOWERING, diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
validateHostAttributeShape(hostAttributes.getFirst(), diagnostics);
|
||||
if (!initAllowedAttributes.isEmpty()) {
|
||||
validateInitAllowedAttributeShape(initAllowedAttributes.getFirst(), diagnostics);
|
||||
}
|
||||
if (!assetLoweringAttributes.isEmpty()) {
|
||||
validateAssetLoweringAttributeShape(assetLoweringAttributes.getFirst(), signature, diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateBuiltinTypeAttribute(
|
||||
@@ -587,6 +600,39 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateAssetLoweringAttributeShape(
|
||||
final PbsAst.Attribute attribute,
|
||||
final PbsAst.FunctionSignature signature,
|
||||
final DiagnosticSink diagnostics) {
|
||||
final var args = validateNamedArguments(attribute, Set.of("param"), Set.of(), diagnostics);
|
||||
if (args == null) {
|
||||
return;
|
||||
}
|
||||
validateRequiredIntArgument(attribute, args, "param", false, diagnostics);
|
||||
final var value = args.get("param");
|
||||
if (!(value instanceof PbsAst.AttributeIntValue intValue)) {
|
||||
return;
|
||||
}
|
||||
final var paramIndex = intValue.value();
|
||||
if (paramIndex < 0 || paramIndex >= signature.parameters().size()) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name(),
|
||||
"AssetLowering param index %d is out of range for host signature '%s'".formatted(paramIndex, signature.name()),
|
||||
attribute.span());
|
||||
return;
|
||||
}
|
||||
final var parameter = signature.parameters().get((int) paramIndex);
|
||||
final var parameterType = unwrapGroup(parameter.typeRef());
|
||||
if (parameterType == null
|
||||
|| parameterType.kind() != PbsAst.TypeRefKind.SIMPLE
|
||||
|| !"Addressable".equals(parameterType.name())) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name(),
|
||||
"AssetLowering target parameter '%s' must have Addressable type".formatted(parameter.name()),
|
||||
attribute.span());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, PbsAst.AttributeValue> validateNamedArguments(
|
||||
final PbsAst.Attribute attribute,
|
||||
final Set<String> requiredNames,
|
||||
@@ -690,6 +736,13 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
return trimmed.isEmpty();
|
||||
}
|
||||
|
||||
private PbsAst.TypeRef unwrapGroup(final PbsAst.TypeRef typeRef) {
|
||||
if (typeRef == null || typeRef.kind() != PbsAst.TypeRefKind.GROUP) {
|
||||
return typeRef;
|
||||
}
|
||||
return unwrapGroup(typeRef.inner());
|
||||
}
|
||||
|
||||
private List<PbsAst.Attribute> attributesNamed(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final String name) {
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
|
||||
+65
-2
@@ -9,12 +9,18 @@ import p.studio.compiler.models.IRGlobalVisibility;
|
||||
import p.studio.compiler.models.IRSyntheticCallableKind;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.lexer.LexErrors;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticPhase;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.ModuleId;
|
||||
import p.studio.compiler.source.tables.NameTable;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -433,7 +439,8 @@ class PbsFrontendCompilerTest {
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -462,7 +469,8 @@ class PbsFrontendCompilerTest {
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& h.abiModule().equals("asset")
|
||||
&& h.abiMethod().equals("load")
|
||||
&& h.abiVersion() == 1));
|
||||
&& h.abiVersion() == 1
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -493,6 +501,61 @@ class PbsFrontendCompilerTest {
|
||||
&& "37".equals(i.label())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRewriteAssetLoweringHostParameterToAssetId() {
|
||||
final var interfaceSource = """
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
}
|
||||
""";
|
||||
final var source = """
|
||||
fn boot() -> (status: int, loading_handle: int) {
|
||||
return LowAssets.load(assets.ui.panel, 2);
|
||||
}
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var interfaceBackend = compiler.compileFile(new FileId(16), interfaceSource, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
final var sourceFileId = new FileId(17);
|
||||
final var sourceTokens = PbsLexer.lex(source, sourceFileId, diagnostics);
|
||||
final var sourceAst = PbsParser.parse(sourceTokens, sourceFileId, diagnostics);
|
||||
final var interfaceTokens = PbsLexer.lex(interfaceSource, new FileId(18), diagnostics);
|
||||
final var interfaceAst = PbsParser.parse(interfaceTokens, new FileId(18), diagnostics, PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
final var fileBackend = compiler.compileParsedFile(
|
||||
sourceFileId,
|
||||
sourceAst,
|
||||
diagnostics,
|
||||
SourceKind.PROJECT,
|
||||
ModuleId.none(),
|
||||
ReadOnlyList.empty(),
|
||||
HostAdmissionContext.permissiveDefault(),
|
||||
new FESurfaceContext(ReadOnlyList.from(new Addressable("assets.ui.panel", 55))),
|
||||
new NameTable(),
|
||||
interfaceAst.topDecls(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
interfaceBackend.reservedMetadata(),
|
||||
Map.of());
|
||||
|
||||
assertEquals(0, diagnostics.errorCount(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
final var executableBoot = fileBackend.executableFunctions().stream()
|
||||
.filter(fn -> fn.callableName().equals("boot"))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertTrue(executableBoot.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_I32
|
||||
&& "55".equals(i.label())));
|
||||
assertTrue(executableBoot.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_HOST
|
||||
&& i.hostCall() != null
|
||||
&& "asset".equals(i.hostCall().module())
|
||||
&& "load".equals(i.hostCall().name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectUnresolvedBackendOwnedAssetReference() {
|
||||
final var source = """
|
||||
|
||||
+4
-2
@@ -332,7 +332,8 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -361,8 +362,9 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
assertEquals("asset", backend.reservedMetadata().requiredCapabilities().getFirst());
|
||||
assertTrue(backend.reservedMetadata().hostMethodBindings().stream()
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)
|
||||
&& h.abiModule().equals("asset")
|
||||
&& h.abiMethod().equals("cancel")
|
||||
&& h.abiMethod().equals("load")
|
||||
&& h.abiVersion() == 1));
|
||||
}
|
||||
|
||||
|
||||
+20
-1
@@ -126,7 +126,8 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -140,6 +141,24 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectMalformedAssetLoweringOnHostSurface() {
|
||||
final var source = """
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
[AssetLowering(param = 1)]
|
||||
fn load(addressable: Addressable) -> int;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(5), source, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectBuiltinFieldWithNonAdmissibleLayoutType() {
|
||||
final var source = """
|
||||
|
||||
+2
-1
@@ -69,7 +69,8 @@ class InterfaceModuleLoaderTest {
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
}
|
||||
"""))),
|
||||
"pub host LowAssets;");
|
||||
|
||||
+2
-1
@@ -950,7 +950,8 @@ class PBSFrontendPhaseServiceTest {
|
||||
assertTrue(irBackend.getReservedMetadata().hostMethodBindings().stream()
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& h.sourceMethodName().equals("load")
|
||||
&& h.abiModule().equals("asset")));
|
||||
&& h.abiModule().equals("asset")
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user