(fixes) test-projects main have to run

This commit is contained in:
2026-03-27 05:29:24 +00:00
parent e582527937
commit e905055cb0
15 changed files with 391 additions and 121 deletions
@@ -173,23 +173,36 @@ final class PbsExecutableBodyLowerer {
reportUnsupportedLowering("path assignment lowering is not supported in executable lowering v1", assignStatement.span(), context);
return false;
}
final var targetSlot = context.resolveLocalSlot(target.rootName());
if (targetSlot == null) {
reportUnsupportedLowering("assignment target is not a known local: " + target.rootName(), assignStatement.span(), context);
final var targetLocalSlot = context.resolveLocalSlot(target.rootName());
final var targetGlobalSlot = targetLocalSlot == null ? context.resolveGlobalSlot(target.rootName()) : null;
if (targetLocalSlot == null && targetGlobalSlot == null) {
reportUnsupportedLowering("assignment target is not a known local or global: " + target.rootName(), assignStatement.span(), context);
return false;
}
if (assignStatement.operator() == PbsAst.AssignOperator.ASSIGN) {
final var localOwner = callsiteEmitter.resolveExpressionOwnerForLowering(assignStatement.value(), context);
lowerExpression(assignStatement.value(), context);
context.bindLocalOwner(target.rootName(), localOwner);
emitSetLocal(targetSlot, assignStatement.span(), context);
if (targetLocalSlot != null) {
context.bindLocalOwner(target.rootName(), localOwner);
emitSetLocal(targetLocalSlot, assignStatement.span(), context);
} else {
emitSetGlobal(targetGlobalSlot, assignStatement.span(), context);
}
return false;
}
emitGetLocal(targetSlot, assignStatement.span(), context);
if (targetLocalSlot != null) {
emitGetLocal(targetLocalSlot, assignStatement.span(), context);
} else {
emitGetGlobal(targetGlobalSlot, assignStatement.span(), context);
}
lowerExpression(assignStatement.value(), context);
emitBinaryOperatorInstruction(compoundAssignBinaryOperator(assignStatement.operator()), assignStatement.span(), context);
context.bindLocalOwner(target.rootName(), "");
emitSetLocal(targetSlot, assignStatement.span(), context);
if (targetLocalSlot != null) {
context.bindLocalOwner(target.rootName(), "");
emitSetLocal(targetLocalSlot, assignStatement.span(), context);
} else {
emitSetGlobal(targetGlobalSlot, assignStatement.span(), context);
}
return false;
}
@@ -446,6 +459,53 @@ final class PbsExecutableBodyLowerer {
final var slot = context.localSlotByNameId().get(context.nameTable().register(identifierExpr.name()));
if (slot != null) {
emitGetLocal(slot, identifierExpr.span(), context);
return;
}
final var globalSlot = context.resolveGlobalSlot(identifierExpr.name());
if (globalSlot != null) {
emitGetGlobal(globalSlot, identifierExpr.span(), context);
return;
}
final var constDecl = context.resolveConstDecl(identifierExpr.name());
if (constDecl != null && constDecl.initializer() != null) {
lowerConstExpression(constDecl.initializer(), context);
}
}
private void lowerConstExpression(
final PbsAst.Expression expression,
final PbsExecutableLoweringContext context) {
if (expression == null) {
return;
}
switch (expression) {
case PbsAst.IntLiteralExpr intLiteralExpr -> lowerIntLiteral(intLiteralExpr, context);
case PbsAst.BoundedLiteralExpr boundedLiteralExpr ->
lowerIntLiteral(new PbsAst.IntLiteralExpr(boundedLiteralExpr.value(), boundedLiteralExpr.span()), context);
case PbsAst.StringLiteralExpr stringLiteralExpr -> emitPushConst(stringLiteralExpr.value(), stringLiteralExpr.span(), context);
case PbsAst.BoolLiteralExpr boolLiteralExpr -> emitPushBool(boolLiteralExpr.value(), boolLiteralExpr.span(), context);
case PbsAst.GroupExpr groupExpr -> lowerConstExpression(groupExpr.expression(), context);
case PbsAst.UnaryExpr unaryExpr -> {
lowerConstExpression(unaryExpr.expression(), context);
emitUnaryOperatorInstruction(unaryExpr.operator(), unaryExpr.span(), context);
}
case PbsAst.BinaryExpr binaryExpr -> {
lowerConstExpression(binaryExpr.left(), context);
lowerConstExpression(binaryExpr.right(), context);
emitBinaryOperatorInstruction(binaryExpr.operator(), binaryExpr.span(), context);
}
case PbsAst.IdentifierExpr identifierExpr -> {
final var referenced = context.resolveConstDecl(identifierExpr.name());
if (referenced != null && referenced.initializer() != null) {
lowerConstExpression(referenced.initializer(), context);
} else {
reportUnsupportedLowering("const identifier is not lowerable: " + identifierExpr.name(), identifierExpr.span(), context);
}
}
default -> reportUnsupportedLowering(
"const expression is not lowerable in executable lowering v1",
expression.span(),
context);
}
}
@@ -585,6 +645,24 @@ final class PbsExecutableBodyLowerer {
span));
}
private void emitSetGlobal(
final int slot,
final p.studio.compiler.source.Span span,
final PbsExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.SET_GLOBAL,
ModuleId.none(),
"",
null,
null,
null,
"",
"",
slot,
null,
span));
}
private String compoundAssignBinaryOperator(final PbsAst.AssignOperator operator) {
return switch (operator) {
case ADD_ASSIGN -> "+";
@@ -681,6 +759,24 @@ final class PbsExecutableBodyLowerer {
span));
}
private void emitGetGlobal(
final int slot,
final p.studio.compiler.source.Span span,
final PbsExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.GET_GLOBAL,
ModuleId.none(),
"",
null,
null,
null,
"",
"",
slot,
null,
span));
}
private void reportUnsupportedLowering(
final String message,
final p.studio.compiler.source.Span span,
@@ -361,7 +361,9 @@ final class PbsExecutableCallsiteEmitter {
final PbsExecutableLoweringContext context) {
return switch (receiver) {
case PbsAst.CallExpr ignored -> true;
case PbsAst.IdentifierExpr identifierExpr -> context.resolveLocalSlot(identifierExpr.name()) != null;
case PbsAst.IdentifierExpr identifierExpr ->
context.resolveLocalSlot(identifierExpr.name()) != null
|| context.resolveGlobalSlot(identifierExpr.name()) != null;
case PbsAst.GroupExpr groupExpr -> receiverProducesRuntimeValue(groupExpr.expression(), context);
default -> false;
};
@@ -58,6 +58,14 @@ final class PbsExecutableLoweringContext {
return metadataIndex.builtinConstOwnerByNameId();
}
Map<NameId, Integer> globalSlotByNameId() {
return metadataIndex.globalSlotByNameId();
}
Map<NameId, p.studio.compiler.pbs.ast.PbsAst.ConstDecl> constDeclByNameId() {
return metadataIndex.constDeclByNameId();
}
Map<String, String> builtinCanonicalBySourceType() {
return metadataIndex.builtinCanonicalBySourceType();
}
@@ -118,6 +126,20 @@ final class PbsExecutableLoweringContext {
return localSlotByNameId.get(nameTable.register(localName));
}
Integer resolveGlobalSlot(final String globalName) {
if (globalName == null || globalName.isBlank()) {
return null;
}
return globalSlotByNameId().get(nameTable.register(globalName));
}
p.studio.compiler.pbs.ast.PbsAst.ConstDecl resolveConstDecl(final String constName) {
if (constName == null || constName.isBlank()) {
return null;
}
return constDeclByNameId().get(nameTable.register(constName));
}
int declareLocalSlot(final String localName) {
final var nameId = nameTable.register(localName);
final var existing = localSlotByNameId.get(nameId);
@@ -44,6 +44,8 @@ public class PbsExecutableLoweringModels {
Map<NameId, List<IRReservedMetadata.HostMethodBinding>> hostByMethodName,
Map<String, String> builtinCanonicalBySourceType,
Map<NameId, String> builtinConstOwnerByNameId,
Map<NameId, Integer> globalSlotByNameId,
Map<NameId, PbsAst.ConstDecl> constDeclByNameId,
Map<PbsIntrinsicOwnerMethodKey, List<IRReservedMetadata.IntrinsicSurface>> intrinsicByOwnerAndMethod,
Map<PbsIntrinsicCanonicalKey, String> intrinsicReturnOwnerByCanonical) {
}
@@ -27,6 +27,8 @@ final class PbsExecutableMetadataIndexFactory {
final var hostByMethodName = indexHostBindingsByMethodName(reservedMetadata, nameTable);
final var builtinCanonicalBySourceType = indexBuiltinCanonicalBySourceType(reservedMetadata);
final var builtinConstOwnerByNameId = indexBuiltinConstOwners(reservedMetadata, nameTable);
final var globalSlotByNameId = indexGlobalSlots(ast, nameTable);
final var constDeclByNameId = indexConstDecls(ast, nameTable);
final var builtinSignatureByOwnerAndMethod = indexBuiltinSignatures(
ast,
supplementalTopDecls,
@@ -44,6 +46,8 @@ final class PbsExecutableMetadataIndexFactory {
hostByMethodName,
builtinCanonicalBySourceType,
builtinConstOwnerByNameId,
globalSlotByNameId,
constDeclByNameId,
intrinsicByOwnerAndMethod,
intrinsicReturnOwnerByCanonical);
}
@@ -81,6 +85,33 @@ final class PbsExecutableMetadataIndexFactory {
return builtinConstOwnerByNameId;
}
private Map<NameId, Integer> indexGlobalSlots(
final PbsAst.File ast,
final NameTable nameTable) {
final var globalSlotByNameId = new HashMap<NameId, Integer>();
var nextSlot = 0;
for (final var topDecl : ast.topDecls()) {
if (!(topDecl instanceof PbsAst.GlobalDecl globalDecl)) {
continue;
}
globalSlotByNameId.putIfAbsent(nameTable.register(globalDecl.name()), nextSlot++);
}
return globalSlotByNameId;
}
private Map<NameId, PbsAst.ConstDecl> indexConstDecls(
final PbsAst.File ast,
final NameTable nameTable) {
final var constDeclByNameId = new HashMap<NameId, PbsAst.ConstDecl>();
for (final var topDecl : ast.topDecls()) {
if (!(topDecl instanceof PbsAst.ConstDecl constDecl)) {
continue;
}
constDeclByNameId.putIfAbsent(nameTable.register(constDecl.name()), constDecl);
}
return constDeclByNameId;
}
private Map<PbsIntrinsicOwnerMethodKey, PbsAst.FunctionSignature> indexBuiltinSignatures(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
@@ -38,8 +38,8 @@ final class PbsExecutableStackAnalyzer {
var outHeight = incomingHeightByInstruction.get(index);
switch (instruction.kind()) {
case PUSH_I32, PUSH_BOOL, PUSH_CONST, GET_LOCAL -> outHeight += 1;
case POP, SET_LOCAL -> {
case PUSH_I32, PUSH_BOOL, PUSH_CONST, GET_LOCAL, GET_GLOBAL -> outHeight += 1;
case POP, SET_LOCAL, SET_GLOBAL -> {
if (outHeight <= 0) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at " + instruction.kind().name().toLowerCase() + ": need=1 have=" + outHeight);
@@ -108,6 +108,11 @@ final class PbsFlowAssignmentAnalyzer {
return new AssignmentTargetResolution(localType, true);
}
final var globalType = model.globalTypes.get(rootName);
if (globalType != null) {
return new AssignmentTargetResolution(globalType, true);
}
if (isKnownNonAssignableRoot(rootName, model)) {
Diagnostics.error(diagnostics,
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_NOT_ASSIGNABLE.name(),
@@ -138,6 +143,8 @@ final class PbsFlowAssignmentAnalyzer {
final var localType = scope.resolve(rootName);
if (localType != null) {
currentType = localType;
} else if (model.globalTypes.containsKey(rootName)) {
currentType = model.globalTypes.get(rootName);
} else if (model.serviceSingletons.containsKey(rootName)) {
currentType = model.serviceSingletons.get(rootName);
} else if (model.constTypes.containsKey(rootName)) {
@@ -253,6 +253,8 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
callableSignatures.add(new CallableSignatureRef(entryPointModuleId, entryPointCallableName, 0, "() -> unit"));
final var instructions = new ArrayList<IRBackendExecutableFunction.Instruction>();
instructions.add(getGlobalInstruction(bootGuardSlot, frameExecutable.span()));
instructions.add(pushBoolInstruction(true, frameExecutable.span()));
instructions.add(eqInstruction(frameExecutable.span()));
instructions.add(jumpIfTrueInstruction("bootstrap_done", frameExecutable.span()));
for (final var moduleId : sortedModuleIds) {
final var moduleInitCallableId = moduleInitCallableIds.get(moduleId);
@@ -297,7 +299,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
0,
0,
0,
1,
2,
ReadOnlyList.wrap(instructions),
frameExecutable.span()));
@@ -459,6 +461,16 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
span);
}
private IRBackendExecutableFunction.Instruction eqInstruction(final Span span) {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.EQ,
"",
null,
null,
null,
span);
}
private IRBackendExecutableFunction.Instruction labelInstruction(
final String label,
final Span span) {
@@ -134,6 +134,59 @@ class PbsFrontendCompilerTest {
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_FUNC));
}
@Test
void shouldLowerGlobalReadsAndWritesInExecutableBodies() {
final var source = """
declare global tile_id: int = 0;
fn tick() -> int {
tile_id = tile_id + 1;
if (tile_id > 7) {
tile_id = 0;
}
return tile_id;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(46), source, diagnostics);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var executableTick = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("tick"))
.findFirst()
.orElseThrow();
assertTrue(executableTick.instructions().stream().anyMatch(i ->
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.GET_GLOBAL));
assertTrue(executableTick.instructions().stream().anyMatch(i ->
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.SET_GLOBAL));
}
@Test
void shouldLowerUserConstIdentifiersInExecutableBodies() {
final var source = """
declare const MAX_FRAMES: int = 10;
fn tick(frame_counter: int) -> bool {
return frame_counter > MAX_FRAMES;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(47), source, diagnostics);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var executableTick = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("tick"))
.findFirst()
.orElseThrow();
assertTrue(executableTick.instructions().stream().anyMatch(i ->
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_I32
&& "10".equals(i.label())));
}
@Test
void shouldClassifyCallableCallEvenWhenNameLooksHostLike() {
final var source = """
@@ -42,6 +42,28 @@ class PbsSemanticsAssignmentTest {
d.getCode().equals(PbsSemanticsErrors.E_SEM_FIELD_WRITE_ACCESS_DENIED.name())));
}
@Test
void shouldAllowGlobalAssignments() {
final var source = """
declare global tile_id: int = 0;
fn tick(next: int) -> int {
tile_id = tile_id + next;
return tile_id;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
assertFalse(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_UNRESOLVED.name())));
assertFalse(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_NOT_ASSIGNABLE.name())));
assertFalse(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_ASSIGN_TYPE_MISMATCH.name())));
}
@Test
void shouldRejectUnresolvedImmutableAndTypeMismatchedAssignments() {
final var source = """
@@ -381,16 +381,18 @@ class PBSFrontendPhaseServiceTest {
.filter(function -> irBackend.getEntryPointCallableName().equals(function.callableName()))
.findFirst()
.orElseThrow();
assertEquals(9, wrapper.instructions().size());
assertEquals(11, wrapper.instructions().size());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.GET_GLOBAL, wrapper.instructions().get(0).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP_IF_TRUE, wrapper.instructions().get(1).kind());
assertTrue(wrapper.instructions().get(2).calleeCallableName().startsWith("__pbs.module_init$m"));
assertTrue(wrapper.instructions().get(3).calleeCallableName().startsWith("__pbs.project_init$m"));
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_BOOL, wrapper.instructions().get(4).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.SET_GLOBAL, wrapper.instructions().get(5).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.LABEL, wrapper.instructions().get(6).kind());
assertEquals("frame", wrapper.instructions().get(7).calleeCallableName());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.RET, wrapper.instructions().get(8).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_BOOL, wrapper.instructions().get(1).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.EQ, wrapper.instructions().get(2).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP_IF_TRUE, wrapper.instructions().get(3).kind());
assertTrue(wrapper.instructions().get(4).calleeCallableName().startsWith("__pbs.module_init$m"));
assertTrue(wrapper.instructions().get(5).calleeCallableName().startsWith("__pbs.project_init$m"));
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_BOOL, wrapper.instructions().get(6).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.SET_GLOBAL, wrapper.instructions().get(7).kind());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.LABEL, wrapper.instructions().get(8).kind());
assertEquals("frame", wrapper.instructions().get(9).calleeCallableName());
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.RET, wrapper.instructions().get(10).kind());
}
@Test
@@ -87,6 +87,8 @@ class LowerToIRVMServiceTest {
fn("frame", "app", 20, ReadOnlyList.from(ret())),
fn("__pbs.frame_wrapper$m0", "app", 40, ReadOnlyList.from(
getGlobal(0),
pushBool(true),
eq(),
jmpIfTrue("bootstrap_done"),
callFunc("app", "__pbs.module_init$m0", 31),
callFunc("app", "__pbs.project_init$m0", 32),
@@ -101,13 +103,15 @@ class LowerToIRVMServiceTest {
assertEquals("__pbs.frame_wrapper$m0", lowered.module().functions().get(0).name());
assertEquals(IRVMOp.GET_GLOBAL, lowered.module().functions().get(0).instructions().get(0).op());
assertEquals(IRVMOp.JMP_IF_TRUE, lowered.module().functions().get(0).instructions().get(1).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(2).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(3).op());
assertEquals(IRVMOp.PUSH_BOOL, lowered.module().functions().get(0).instructions().get(4).op());
assertEquals(IRVMOp.SET_GLOBAL, lowered.module().functions().get(0).instructions().get(5).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(6).op());
assertEquals(IRVMOp.RET, lowered.module().functions().get(0).instructions().get(7).op());
assertEquals(IRVMOp.PUSH_BOOL, lowered.module().functions().get(0).instructions().get(1).op());
assertEquals(IRVMOp.EQ, lowered.module().functions().get(0).instructions().get(2).op());
assertEquals(IRVMOp.JMP_IF_TRUE, lowered.module().functions().get(0).instructions().get(3).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(4).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(5).op());
assertEquals(IRVMOp.PUSH_BOOL, lowered.module().functions().get(0).instructions().get(6).op());
assertEquals(IRVMOp.SET_GLOBAL, lowered.module().functions().get(0).instructions().get(7).op());
assertEquals(IRVMOp.CALL, lowered.module().functions().get(0).instructions().get(8).op());
assertEquals(IRVMOp.RET, lowered.module().functions().get(0).instructions().get(9).op());
assertEquals(IRVMOp.RET, lowered.module().functions().stream()
.filter(function -> "frame".equals(function.name()))
.findFirst()
@@ -645,4 +649,14 @@ class LowerToIRVMServiceTest {
null,
Span.none());
}
private static IRBackendExecutableFunction.Instruction eq() {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.EQ,
"",
null,
null,
null,
Span.none());
}
}