aritmetics

This commit is contained in:
2026-03-24 13:42:34 +00:00
parent 5575c491a1
commit 2bc99cc901
10 changed files with 684 additions and 14 deletions
@@ -388,7 +388,8 @@ public final class PbsFrontendCompiler {
callableSignatureByCallableId,
returnSlotsByCallableId,
intrinsicIdTable,
localSlotByNameId);
localSlotByNameId,
fn.parameters().size());
final var terminated = lowerBlock(fn.body(), loweringContext);
final var instructions = loweringContext.instructions();
if (!terminated) {
@@ -417,6 +418,7 @@ public final class PbsFrontendCompiler {
}
final var start = safeToInt(fn.span().getStart());
final var end = safeToInt(fn.span().getEnd());
final var localSlots = Math.max(0, loweringContext.nextLocalSlot() - fn.parameters().size());
executableFunctions.add(new IRBackendExecutableFunction(
fileId,
normalizedModuleId,
@@ -425,7 +427,7 @@ public final class PbsFrontendCompiler {
start,
end,
fn.parameters().size(),
0,
localSlots,
returnSlots,
maxStackSlots,
ReadOnlyList.wrap(instructions),
@@ -629,8 +631,39 @@ public final class PbsFrontendCompiler {
return false;
}
switch (statement) {
case PbsAst.LetStatement letStatement -> lowerExpression(letStatement.initializer(), context);
case PbsAst.AssignStatement assignStatement -> lowerExpression(assignStatement.value(), context);
case PbsAst.LetStatement letStatement -> {
lowerExpression(letStatement.initializer(), context);
final var localSlot = context.declareLocalSlot(letStatement.name());
emitSetLocal(localSlot, letStatement.span(), context);
}
case PbsAst.AssignStatement assignStatement -> {
final var target = assignStatement.target();
if (target == null || target.rootName() == null || target.rootName().isBlank()) {
reportUnsupportedLowering("assignment target requires a local root name", assignStatement.span(), context);
return false;
}
if (target.pathSegments() != null && !target.pathSegments().isEmpty()) {
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);
return false;
}
switch (assignStatement.operator()) {
case ASSIGN -> {
lowerExpression(assignStatement.value(), context);
emitSetLocal(targetSlot, assignStatement.span(), context);
}
case ADD_ASSIGN, SUB_ASSIGN, MUL_ASSIGN, DIV_ASSIGN, MOD_ASSIGN -> {
emitGetLocal(targetSlot, assignStatement.span(), context);
lowerExpression(assignStatement.value(), context);
emitBinaryOperatorInstruction(compoundAssignBinaryOperator(assignStatement.operator()), assignStatement.span(), context);
emitSetLocal(targetSlot, assignStatement.span(), context);
}
}
}
case PbsAst.ReturnStatement returnStatement -> {
if (returnStatement.value() != null) {
lowerExpression(returnStatement.value(), context);
@@ -741,8 +774,12 @@ public final class PbsFrontendCompiler {
case PbsAst.BinaryExpr binaryExpr -> {
lowerExpression(binaryExpr.left(), context);
lowerExpression(binaryExpr.right(), context);
emitBinaryOperatorInstruction(binaryExpr.operator(), binaryExpr.span(), context);
}
case PbsAst.UnaryExpr unaryExpr -> {
lowerExpression(unaryExpr.expression(), context);
emitUnaryOperatorInstruction(unaryExpr.operator(), unaryExpr.span(), context);
}
case PbsAst.UnaryExpr unaryExpr -> lowerExpression(unaryExpr.expression(), context);
case PbsAst.ElseExpr elseExpr -> {
lowerExpression(elseExpr.optionalExpression(), context);
lowerExpression(elseExpr.fallbackExpression(), context);
@@ -809,8 +846,7 @@ public final class PbsFrontendCompiler {
case PbsAst.BoundedLiteralExpr ignored -> {
}
case PbsAst.StringLiteralExpr stringLiteralExpr -> emitPushConst(stringLiteralExpr.value(), stringLiteralExpr.span(), context);
case PbsAst.BoolLiteralExpr ignored -> {
}
case PbsAst.BoolLiteralExpr boolLiteralExpr -> emitPushBool(boolLiteralExpr.value(), boolLiteralExpr.span(), context);
case PbsAst.ThisExpr ignored -> {
}
case PbsAst.NoneExpr ignored -> {
@@ -890,6 +926,7 @@ public final class PbsFrontendCompiler {
return;
}
final var host = hostCandidates.getFirst();
final var effectiveArgSlots = callExpr.arguments().size() + implicitReceiverArgSlots(callExpr.callee());
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_HOST,
"",
@@ -897,7 +934,7 @@ public final class PbsFrontendCompiler {
host.abiModule(),
host.abiMethod(),
host.abiVersion(),
callExpr.arguments().size(),
effectiveArgSlots,
0),
null,
callExpr.span()));
@@ -939,6 +976,7 @@ public final class PbsFrontendCompiler {
reportUnsupportedLowering("executable lowering resolved callable without signature metadata", callExpr.span(), context);
return;
}
final var effectiveArgSlots = callExpr.arguments().size() + implicitReceiverArgSlots(callExpr.callee());
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
calleeSignature.moduleId(),
@@ -946,7 +984,7 @@ public final class PbsFrontendCompiler {
calleeCallableId,
null,
null,
callExpr.arguments().size(),
effectiveArgSlots,
context.returnSlotsByCallableId().get(calleeCallableId),
callExpr.span()));
}
@@ -1139,6 +1177,24 @@ public final class PbsFrontendCompiler {
span));
}
private void emitPushBool(
final boolean value,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.PUSH_BOOL,
ModuleId.none(),
"",
null,
null,
null,
value ? "true" : "false",
"",
null,
null,
span));
}
private void emitPushConst(
final String value,
final p.studio.compiler.source.Span span,
@@ -1157,6 +1213,102 @@ public final class PbsFrontendCompiler {
span));
}
private void emitSetLocal(
final int slot,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.SET_LOCAL,
ModuleId.none(),
"",
null,
null,
null,
"",
"",
slot,
null,
span));
}
private String compoundAssignBinaryOperator(final PbsAst.AssignOperator operator) {
return switch (operator) {
case ADD_ASSIGN -> "+";
case SUB_ASSIGN -> "-";
case MUL_ASSIGN -> "*";
case DIV_ASSIGN -> "/";
case MOD_ASSIGN -> "%";
case ASSIGN -> "=";
};
}
private void emitUnaryOperatorInstruction(
final String operator,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
final var normalized = operator == null ? "" : operator;
final IRBackendExecutableFunction.InstructionKind instructionKind = switch (normalized) {
case "-", "neg" -> IRBackendExecutableFunction.InstructionKind.NEG;
case "!", "not" -> IRBackendExecutableFunction.InstructionKind.NOT;
default -> null;
};
if (instructionKind == null) {
reportUnsupportedLowering("unsupported unary operator in executable lowering: " + normalized, span, context);
return;
}
context.instructions().add(new IRBackendExecutableFunction.Instruction(
instructionKind,
ModuleId.none(),
"",
null,
null,
null,
"",
"",
null,
null,
span));
}
private void emitBinaryOperatorInstruction(
final String operator,
final p.studio.compiler.source.Span span,
final ExecutableLoweringContext context) {
final var normalized = operator == null ? "" : operator;
final IRBackendExecutableFunction.InstructionKind instructionKind = switch (normalized) {
case "+" -> IRBackendExecutableFunction.InstructionKind.ADD;
case "-" -> IRBackendExecutableFunction.InstructionKind.SUB;
case "*" -> IRBackendExecutableFunction.InstructionKind.MUL;
case "/" -> IRBackendExecutableFunction.InstructionKind.DIV;
case "%" -> IRBackendExecutableFunction.InstructionKind.MOD;
case "==" -> IRBackendExecutableFunction.InstructionKind.EQ;
case "!=" -> IRBackendExecutableFunction.InstructionKind.NEQ;
case "<" -> IRBackendExecutableFunction.InstructionKind.LT;
case "<=" -> IRBackendExecutableFunction.InstructionKind.LTE;
case ">" -> IRBackendExecutableFunction.InstructionKind.GT;
case ">=" -> IRBackendExecutableFunction.InstructionKind.GTE;
case "&&", "and" -> IRBackendExecutableFunction.InstructionKind.AND;
case "||", "or" -> IRBackendExecutableFunction.InstructionKind.OR;
default -> null;
};
if (instructionKind == null) {
reportUnsupportedLowering("unsupported binary operator in executable lowering: " + normalized, span, context);
return;
}
context.instructions().add(new IRBackendExecutableFunction.Instruction(
instructionKind,
ModuleId.none(),
"",
null,
null,
null,
"",
"",
null,
null,
span));
}
private void emitGetLocal(
final int slot,
final p.studio.compiler.source.Span span,
@@ -1219,7 +1371,27 @@ public final class PbsFrontendCompiler {
var outHeight = incomingHeightByInstruction.get(index);
switch (instruction.kind()) {
case PUSH_I32, PUSH_CONST, GET_LOCAL -> outHeight += 1;
case PUSH_I32, PUSH_BOOL, PUSH_CONST, GET_LOCAL -> outHeight += 1;
case POP, SET_LOCAL -> {
if (outHeight <= 0) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at " + instruction.kind().name().toLowerCase() + ": need=1 have=" + outHeight);
}
outHeight -= 1;
}
case ADD, SUB, MUL, DIV, MOD, EQ, NEQ, LT, LTE, GT, GTE, AND, OR -> {
if (outHeight < 2) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at " + instruction.kind().name().toLowerCase() + ": need=2 have=" + outHeight);
}
outHeight -= 1;
}
case NOT, NEG -> {
if (outHeight <= 0) {
throw new ExecutableLoweringAnalysisException(
"stack underflow at " + instruction.kind().name().toLowerCase() + ": need=1 have=" + outHeight);
}
}
case CALL_FUNC -> {
final var argSlots = instruction.expectedArgSlots() == null ? 0 : instruction.expectedArgSlots();
final var retSlots = instruction.expectedRetSlots() == null ? 0 : instruction.expectedRetSlots();
@@ -1327,6 +1499,7 @@ public final class PbsFrontendCompiler {
private final ArrayList<IRBackendExecutableFunction.Instruction> instructions = new ArrayList<>();
private final ArrayDeque<LoopTargets> loopTargets = new ArrayDeque<>();
private int nextLabelId = 0;
private int nextLocalSlot;
private ExecutableLoweringContext(
final ModuleId moduleId,
@@ -1340,7 +1513,8 @@ public final class PbsFrontendCompiler {
final Map<CallableId, CallableSignatureRef> callableSignatureByCallableId,
final Map<CallableId, Integer> returnSlotsByCallableId,
final IntrinsicTable intrinsicIdTable,
final Map<NameId, Integer> localSlotByNameId) {
final Map<NameId, Integer> localSlotByNameId,
final int initialLocalSlot) {
this.moduleId = moduleId == null ? ModuleId.none() : moduleId;
this.diagnostics = diagnostics;
this.nameTable = nameTable;
@@ -1352,7 +1526,8 @@ public final class PbsFrontendCompiler {
this.callableSignatureByCallableId = callableSignatureByCallableId;
this.returnSlotsByCallableId = returnSlotsByCallableId;
this.intrinsicIdTable = intrinsicIdTable;
this.localSlotByNameId = localSlotByNameId == null ? Map.of() : localSlotByNameId;
this.localSlotByNameId = localSlotByNameId == null ? new HashMap<>() : localSlotByNameId;
this.nextLocalSlot = Math.max(0, initialLocalSlot);
}
private ModuleId moduleId() {
@@ -1403,6 +1578,28 @@ public final class PbsFrontendCompiler {
return localSlotByNameId;
}
private Integer resolveLocalSlot(final String localName) {
if (localName == null || localName.isBlank()) {
return null;
}
return localSlotByNameId.get(nameTable.register(localName));
}
private int declareLocalSlot(final String localName) {
final var nameId = nameTable.register(localName);
final var existing = localSlotByNameId.get(nameId);
if (existing != null) {
return existing;
}
final var slot = nextLocalSlot++;
localSlotByNameId.put(nameId, slot);
return slot;
}
private int nextLocalSlot() {
return nextLocalSlot;
}
private ArrayList<IRBackendExecutableFunction.Instruction> instructions() {
return instructions;
}