new test main with code directly (fixes) runtime runing

This commit is contained in:
2026-03-24 13:42:32 +00:00
parent 5630f8f119
commit 54f0b4b6ba
10 changed files with 309 additions and 12 deletions
@@ -287,6 +287,10 @@ public final class PbsFrontendCompiler {
if (functionCallableId == null) {
continue;
}
final var localSlotByNameId = new HashMap<NameId, Integer>();
for (var paramIndex = 0; paramIndex < fn.parameters().size(); paramIndex++) {
localSlotByNameId.put(nameTable.register(fn.parameters().get(paramIndex).name()), paramIndex);
}
final var loweringContext = new ExecutableLoweringContext(
normalizedModuleKey,
diagnostics,
@@ -295,7 +299,8 @@ public final class PbsFrontendCompiler {
intrinsicByMethodName,
callableIdsByNameAndArity,
returnSlotsByCallableId,
intrinsicIdTable);
intrinsicIdTable,
localSlotByNameId);
final var terminated = lowerBlock(fn.body(), loweringContext);
final var instructions = loweringContext.instructions();
if (!terminated) {
@@ -618,7 +623,7 @@ public final class PbsFrontendCompiler {
}
switch (expression) {
case PbsAst.CallExpr callExpr -> {
lowerExpression(callExpr.callee(), context);
lowerCallsiteReceiver(callExpr.callee(), context);
for (final var arg : callExpr.arguments()) {
lowerExpression(arg, context);
}
@@ -678,16 +683,27 @@ public final class PbsFrontendCompiler {
}
}
case PbsAst.BlockExpr blockExpr -> lowerBlock(blockExpr.block(), context);
case PbsAst.IdentifierExpr ignored -> {
case PbsAst.IdentifierExpr identifierExpr -> {
final var slot = context.localSlotByNameId().get(context.nameTable().register(identifierExpr.name()));
if (slot != null) {
emitGetLocal(slot, identifierExpr.span(), context);
}
}
case PbsAst.IntLiteralExpr ignored -> {
case PbsAst.IntLiteralExpr intLiteralExpr -> {
if (intLiteralExpr.value() < Integer.MIN_VALUE || intLiteralExpr.value() > Integer.MAX_VALUE) {
reportUnsupportedLowering(
"int literal exceeds i32 lowering range: " + intLiteralExpr.value(),
intLiteralExpr.span(),
context);
return;
}
emitPushI32((int) intLiteralExpr.value(), intLiteralExpr.span(), context);
}
case PbsAst.FloatLiteralExpr ignored -> {
}
case PbsAst.BoundedLiteralExpr ignored -> {
}
case PbsAst.StringLiteralExpr ignored -> {
}
case PbsAst.StringLiteralExpr stringLiteralExpr -> emitPushConst(stringLiteralExpr.value(), stringLiteralExpr.span(), context);
case PbsAst.BoolLiteralExpr ignored -> {
}
case PbsAst.ThisExpr ignored -> {
@@ -701,6 +717,20 @@ public final class PbsFrontendCompiler {
}
}
private void lowerCallsiteReceiver(
final PbsAst.Expression callee,
final ExecutableLoweringContext context) {
if (callee == null) {
return;
}
switch (callee) {
case PbsAst.MemberExpr memberExpr -> lowerExpression(memberExpr.receiver(), context);
case PbsAst.GroupExpr groupExpr -> lowerCallsiteReceiver(groupExpr.expression(), context);
default -> {
}
}
}
private void lowerCallsite(
final PbsAst.CallExpr callExpr,
final ExecutableLoweringContext context) {
@@ -920,6 +950,60 @@ public final class PbsFrontendCompiler {
span));
}
private void emitPushI32(
final int value,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.PUSH_I32,
"",
"",
null,
null,
null,
Integer.toString(value),
"",
null,
null,
span));
}
private void emitPushConst(
final String value,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.PUSH_CONST,
"",
"",
null,
null,
null,
value == null ? "" : value,
"",
null,
null,
span));
}
private void emitGetLocal(
final int slot,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.GET_LOCAL,
"",
"",
null,
null,
null,
"",
"",
slot,
null,
span));
}
private void reportUnsupportedLowering(
final String message,
final p.studio.compiler.source.Span span,
@@ -964,9 +1048,34 @@ public final class PbsFrontendCompiler {
var outHeight = incomingHeightByInstruction.get(index);
switch (instruction.kind()) {
case CALL_FUNC -> outHeight += instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
case CALL_HOST -> outHeight += instruction.hostCall() == null ? 0 : instruction.hostCall().retSlots();
case CALL_INTRINSIC -> outHeight += instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
case PUSH_I32, PUSH_CONST, GET_LOCAL -> outHeight += 1;
case CALL_FUNC -> {
final var argSlots = instruction.expectedArgSlots() == null ? 0 : instruction.expectedArgSlots();
final var retSlots = instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
if (outHeight < argSlots) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at call_func: need=%d have=%d".formatted(argSlots, outHeight));
}
outHeight = outHeight - argSlots + retSlots;
}
case CALL_HOST -> {
final var argSlots = instruction.hostCall() == null ? 0 : instruction.hostCall().argSlots();
final var retSlots = instruction.hostCall() == null ? 0 : instruction.hostCall().retSlots();
if (outHeight < argSlots) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at call_host: need=%d have=%d".formatted(argSlots, outHeight));
}
outHeight = outHeight - argSlots + retSlots;
}
case CALL_INTRINSIC -> {
final var argSlots = instruction.expectedArgSlots() == null ? 0 : instruction.expectedArgSlots();
final var retSlots = instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
if (outHeight < argSlots) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at call_intrinsic: need=%d have=%d".formatted(argSlots, outHeight));
}
outHeight = outHeight - argSlots + retSlots;
}
case HALT, LABEL, JMP, RET -> {
}
case JMP_IF_TRUE, JMP_IF_FALSE -> {
@@ -1040,6 +1149,7 @@ public final class PbsFrontendCompiler {
private final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity;
private final Map<CallableId, Integer> returnSlotsByCallableId;
private final IntrinsicTable intrinsicIdTable;
private final Map<NameId, Integer> localSlotByNameId;
private final ArrayList<IRBackendExecutableFunction.Instruction> instructions = new ArrayList<>();
private final ArrayDeque<LoopTargets> loopTargets = new ArrayDeque<>();
private int nextLabelId = 0;
@@ -1052,7 +1162,8 @@ public final class PbsFrontendCompiler {
final Map<NameId, List<IRReservedMetadata.IntrinsicSurface>> intrinsicByMethodName,
final Map<CallableResolutionKey, List<CallableId>> callableIdsByNameAndArity,
final Map<CallableId, Integer> returnSlotsByCallableId,
final IntrinsicTable intrinsicIdTable) {
final IntrinsicTable intrinsicIdTable,
final Map<NameId, Integer> localSlotByNameId) {
this.moduleKey = moduleKey;
this.diagnostics = diagnostics;
this.nameTable = nameTable;
@@ -1061,6 +1172,7 @@ public final class PbsFrontendCompiler {
this.callableIdsByNameAndArity = callableIdsByNameAndArity;
this.returnSlotsByCallableId = returnSlotsByCallableId;
this.intrinsicIdTable = intrinsicIdTable;
this.localSlotByNameId = localSlotByNameId == null ? Map.of() : localSlotByNameId;
}
private String moduleKey() {
@@ -1095,6 +1207,10 @@ public final class PbsFrontendCompiler {
return intrinsicIdTable;
}
private Map<NameId, Integer> localSlotByNameId() {
return localSlotByNameId;
}
private ArrayList<IRBackendExecutableFunction.Instruction> instructions() {
return instructions;
}
@@ -3,7 +3,9 @@ callables=2
1:::a/0|()->simple:int
intrinsics=0
fn#0:::b id=0
GET_LOCAL calleeId=- intrinsicId=-
RET calleeId=- intrinsicId=-
fn#1:::a id=1
PUSH_I32 calleeId=- intrinsicId=-
CALL_FUNC calleeId=0 intrinsicId=-
RET calleeId=- intrinsicId=-