implements PLN-0007
This commit is contained in:
+102
@@ -143,6 +143,7 @@ final class PbsExecutableBodyLowerer {
|
||||
case PbsAst.WhileStatement whileStatement -> lowerWhileStatement(whileStatement, context);
|
||||
case PbsAst.ExpressionStatement expressionStatement -> {
|
||||
lowerExpression(expressionStatement.expression(), context);
|
||||
discardIgnoredValueSlots(expressionStatement.expression(), context);
|
||||
yield false;
|
||||
}
|
||||
case PbsAst.BreakStatement breakStatement -> lowerBreakStatement(breakStatement, context);
|
||||
@@ -426,6 +427,21 @@ final class PbsExecutableBodyLowerer {
|
||||
}
|
||||
}
|
||||
|
||||
private void discardIgnoredValueSlots(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var slots = materializedValueSlots(expression, context);
|
||||
for (int i = 0; i < slots; i++) {
|
||||
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.POP,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
expression == null ? p.studio.compiler.source.Span.none() : expression.span()));
|
||||
}
|
||||
}
|
||||
|
||||
private void lowerMemberExpression(
|
||||
final PbsAst.MemberExpr memberExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
@@ -508,6 +524,92 @@ final class PbsExecutableBodyLowerer {
|
||||
return receiverPath + "." + memberExpr.memberName();
|
||||
}
|
||||
|
||||
private int materializedValueSlots(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if (expression == null) {
|
||||
return 0;
|
||||
}
|
||||
return switch (expression) {
|
||||
case PbsAst.CallExpr callExpr -> callsiteEmitter.returnSlotsOf(callExpr, context);
|
||||
case PbsAst.ApplyExpr ignored -> 0;
|
||||
case PbsAst.BinaryExpr ignored -> 1;
|
||||
case PbsAst.UnaryExpr ignored -> 1;
|
||||
case PbsAst.ElseExpr elseExpr ->
|
||||
Math.max(
|
||||
materializedValueSlots(elseExpr.optionalExpression(), context),
|
||||
materializedValueSlots(elseExpr.fallbackExpression(), context));
|
||||
case PbsAst.IfExpr ifExpr ->
|
||||
Math.max(
|
||||
blockValueSlots(ifExpr.thenBlock(), context),
|
||||
materializedValueSlots(ifExpr.elseExpression(), context));
|
||||
case PbsAst.SwitchExpr ignored -> 0;
|
||||
case PbsAst.HandleExpr ignored -> 0;
|
||||
case PbsAst.AsExpr asExpr -> materializedValueSlots(asExpr.expression(), context);
|
||||
case PbsAst.MemberExpr memberExpr -> {
|
||||
final var assetReference = flattenAssetReference(memberExpr);
|
||||
if (assetReference != null && context.resolveAssetId(assetReference) != null) {
|
||||
yield 1;
|
||||
}
|
||||
yield materializedValueSlots(memberExpr.receiver(), context);
|
||||
}
|
||||
case PbsAst.PropagateExpr propagateExpr -> materializedValueSlots(propagateExpr.expression(), context);
|
||||
case PbsAst.GroupExpr groupExpr -> materializedValueSlots(groupExpr.expression(), context);
|
||||
case PbsAst.NewExpr newExpr -> expressionListValueSlots(newExpr.arguments(), context);
|
||||
case PbsAst.BindExpr bindExpr -> materializedValueSlots(bindExpr.contextExpression(), context);
|
||||
case PbsAst.SomeExpr someExpr -> materializedValueSlots(someExpr.value(), context);
|
||||
case PbsAst.OkExpr okExpr -> materializedValueSlots(okExpr.value(), context);
|
||||
case PbsAst.TupleExpr tupleExpr -> {
|
||||
var slots = 0;
|
||||
for (final var item : tupleExpr.items()) {
|
||||
slots += materializedValueSlots(item.expression(), context);
|
||||
}
|
||||
yield slots;
|
||||
}
|
||||
case PbsAst.BlockExpr blockExpr -> blockValueSlots(blockExpr.block(), context);
|
||||
case PbsAst.IdentifierExpr identifierExpr -> producesIdentifierValue(identifierExpr, context) ? 1 : 0;
|
||||
case PbsAst.IntLiteralExpr ignored -> 1;
|
||||
case PbsAst.StringLiteralExpr ignoredString -> 1;
|
||||
case PbsAst.BoolLiteralExpr ignoredBool -> 1;
|
||||
case PbsAst.FloatLiteralExpr ignoredFloat -> 0;
|
||||
case PbsAst.BoundedLiteralExpr ignoredBounded -> 0;
|
||||
case PbsAst.ThisExpr ignoredThis -> 0;
|
||||
case PbsAst.NoneExpr ignoredNone -> 0;
|
||||
case PbsAst.ErrExpr ignoredErr -> 0;
|
||||
case PbsAst.UnitExpr ignoredUnit -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
private int blockValueSlots(
|
||||
final PbsAst.Block block,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if (block == null || block.tailExpression() == null) {
|
||||
return 0;
|
||||
}
|
||||
return materializedValueSlots(block.tailExpression(), context);
|
||||
}
|
||||
|
||||
private int expressionListValueSlots(
|
||||
final ReadOnlyList<PbsAst.Expression> expressions,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
var slots = 0;
|
||||
for (final var expression : expressions) {
|
||||
slots += materializedValueSlots(expression, context);
|
||||
}
|
||||
return slots;
|
||||
}
|
||||
|
||||
private boolean producesIdentifierValue(
|
||||
final PbsAst.IdentifierExpr identifierExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if ("assets".equals(identifierExpr.name())) {
|
||||
return false;
|
||||
}
|
||||
return context.resolveLocalSlot(identifierExpr.name()) != null
|
||||
|| context.resolveGlobalSlot(identifierExpr.name()) != null
|
||||
|| context.resolveConstDecl(identifierExpr.name()) != null;
|
||||
}
|
||||
|
||||
private void lowerConstExpression(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
|
||||
+23
@@ -16,6 +16,29 @@ import java.util.Map;
|
||||
import static p.studio.compiler.pbs.lowering.PbsExecutableLoweringModels.*;
|
||||
|
||||
final class PbsExecutableCallsiteEmitter {
|
||||
int returnSlotsOf(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var calleeIdentity = resolveCalleeIdentity(callExpr.callee(), context.nameTable());
|
||||
if (calleeIdentity == null) {
|
||||
return 0;
|
||||
}
|
||||
final var candidates = resolveCallsiteCandidates(callExpr, calleeIdentity, context);
|
||||
final var resolvedCategory = resolveCallsiteCategory(candidates, callExpr, context);
|
||||
if (resolvedCategory == null) {
|
||||
return 0;
|
||||
}
|
||||
return switch (resolvedCategory) {
|
||||
case HOST -> candidates.hostCandidates().getFirst().retSlots();
|
||||
case INTRINSIC -> candidates.intrinsicCandidates().getFirst().retSlots();
|
||||
case CALLABLE -> {
|
||||
if (candidates.callableCandidates().size() != 1) {
|
||||
yield 0;
|
||||
}
|
||||
yield context.returnSlotsByCallableId().getOrDefault(candidates.callableCandidates().getFirst(), 0);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void emitCallsite(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
|
||||
+8
-2
@@ -172,7 +172,7 @@ final class PbsFlowStatementAnalyzer {
|
||||
return;
|
||||
}
|
||||
if (statement instanceof PbsAst.ExpressionStatement expressionStatement) {
|
||||
expressionAnalyzer.analyzeExpression(
|
||||
final var expressionType = expressionAnalyzer.analyzeExpression(
|
||||
expressionStatement.expression(),
|
||||
scope,
|
||||
null,
|
||||
@@ -183,7 +183,13 @@ final class PbsFlowStatementAnalyzer {
|
||||
diagnostics,
|
||||
ExprUse.VALUE,
|
||||
false,
|
||||
this::analyzeNestedBlock);
|
||||
this::analyzeNestedBlock).type();
|
||||
if (typeOps.isMaterializedValue(expressionType)) {
|
||||
Diagnostics.warning(diagnostics,
|
||||
PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name(),
|
||||
"Ignored materialized value in expression statement",
|
||||
expressionStatement.span());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (statement instanceof PbsAst.AssignStatement assignStatement) {
|
||||
|
||||
+10
@@ -139,6 +139,16 @@ final class PbsFlowTypeOps {
|
||||
return type.kind() == Kind.STR || type.kind() == Kind.UNKNOWN;
|
||||
}
|
||||
|
||||
boolean isMaterializedValue(final TypeView type) {
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
return switch (type.kind()) {
|
||||
case UNKNOWN, UNIT, ASSET_NAMESPACE -> false;
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean tupleCompatible(final TypeView actual, final TypeView expected) {
|
||||
if (actual.tupleFields().size() != expected.tupleFields().size()) {
|
||||
return false;
|
||||
|
||||
+1
@@ -69,4 +69,5 @@ public enum PbsSemanticsErrors {
|
||||
E_SEM_HANDLE_ERROR_MISMATCH,
|
||||
E_SEM_HANDLE_ARM_TERMINAL_INVALID,
|
||||
E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE,
|
||||
W_SEM_IGNORED_VALUE,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user