implements PR-06.1
This commit is contained in:
+27
-1
@@ -2,16 +2,42 @@ package p.studio.compiler.backend.irvm;
|
||||
|
||||
public class IRVMLoweringException extends RuntimeException {
|
||||
private final IRVMLoweringErrorCode code;
|
||||
private final Integer fileId;
|
||||
private final Integer start;
|
||||
private final Integer end;
|
||||
|
||||
public IRVMLoweringException(
|
||||
final IRVMLoweringErrorCode code,
|
||||
final String message) {
|
||||
this(code, message, null, null, null);
|
||||
}
|
||||
|
||||
public IRVMLoweringException(
|
||||
final IRVMLoweringErrorCode code,
|
||||
final String message,
|
||||
final Integer fileId,
|
||||
final Integer start,
|
||||
final Integer end) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.fileId = fileId;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public IRVMLoweringErrorCode code() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public Integer fileId() {
|
||||
return fileId;
|
||||
}
|
||||
|
||||
public Integer start() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Integer end() {
|
||||
return end;
|
||||
}
|
||||
}
|
||||
|
||||
+63
-8
@@ -47,7 +47,8 @@ public class LowerToIRVMService {
|
||||
for (var i = 0; i < ordered.size(); i++) {
|
||||
final var fn = ordered.get(i);
|
||||
if (funcIdByCallableId.putIfAbsent(fn.callableId(), i) != null) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
|
||||
"duplicate callable id in executable set: " + fn.callableId());
|
||||
}
|
||||
@@ -78,20 +79,26 @@ public class LowerToIRVMService {
|
||||
}
|
||||
case CALL_FUNC -> {
|
||||
if (instr.calleeCallableId() == null) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
|
||||
"missing callee callable id");
|
||||
}
|
||||
final var calleeId = funcIdByCallableId.get(instr.calleeCallableId());
|
||||
if (calleeId == null) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
|
||||
"missing callee function for callable_id=" + instr.calleeCallableId());
|
||||
}
|
||||
final var calleeFunction = ordered.get(calleeId);
|
||||
if (instr.expectedArgSlots() != null
|
||||
&& instr.expectedArgSlots() != calleeFunction.paramSlots()) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH,
|
||||
"call arg_slots mismatch for %s. expected=%d actual=%d".formatted(
|
||||
instr.calleeCallableName(),
|
||||
@@ -100,7 +107,9 @@ public class LowerToIRVMService {
|
||||
}
|
||||
if (instr.expectedRetSlots() != null
|
||||
&& instr.expectedRetSlots() != calleeFunction.returnSlots()) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_CALL_RET_SLOTS_MISMATCH,
|
||||
"call ret_slots mismatch for %s. expected=%d actual=%d".formatted(
|
||||
instr.calleeCallableName(),
|
||||
@@ -130,7 +139,9 @@ public class LowerToIRVMService {
|
||||
final var intrinsic = instr.intrinsicCall();
|
||||
final var intrinsicIndex = intrinsic.intrinsicId().getIndex();
|
||||
if (intrinsicIndex < 0 || intrinsicIndex >= backend.getIntrinsicPool().size()) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_INVALID_INTRINSIC_ID,
|
||||
"invalid intrinsic id: " + intrinsic.intrinsicId());
|
||||
}
|
||||
@@ -145,7 +156,9 @@ public class LowerToIRVMService {
|
||||
case LABEL -> {
|
||||
final var label = instr.label();
|
||||
if (labelToPc.putIfAbsent(label, functionPc) != null) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_JUMP_TARGET,
|
||||
"duplicate label in function '" + fn.callableName() + "': " + label);
|
||||
}
|
||||
@@ -173,7 +186,8 @@ public class LowerToIRVMService {
|
||||
for (final var patch : jumpPatches) {
|
||||
final var targetPc = labelToPc.get(patch.targetLabel());
|
||||
if (targetPc == null) {
|
||||
throw new IRVMLoweringException(
|
||||
throw loweringError(
|
||||
fn,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_JUMP_TARGET,
|
||||
"missing jump target label '" + patch.targetLabel() + "' in function '" + fn.callableName() + "'");
|
||||
}
|
||||
@@ -268,6 +282,47 @@ public class LowerToIRVMService {
|
||||
return (int) value;
|
||||
}
|
||||
|
||||
private IRVMLoweringException loweringError(
|
||||
final IRBackendExecutableFunction function,
|
||||
final IRVMLoweringErrorCode code,
|
||||
final String message) {
|
||||
return new IRVMLoweringException(
|
||||
code,
|
||||
message,
|
||||
function.fileId().getId(),
|
||||
function.sourceStart(),
|
||||
function.sourceEnd());
|
||||
}
|
||||
|
||||
private IRVMLoweringException loweringError(
|
||||
final IRBackendExecutableFunction function,
|
||||
final IRBackendExecutableFunction.Instruction instruction,
|
||||
final IRVMLoweringErrorCode code,
|
||||
final String message) {
|
||||
return loweringError(function, instruction == null ? Span.none() : instruction.span(), code, message);
|
||||
}
|
||||
|
||||
private IRVMLoweringException loweringError(
|
||||
final IRBackendExecutableFunction function,
|
||||
final Span span,
|
||||
final IRVMLoweringErrorCode code,
|
||||
final String message) {
|
||||
final var effectiveSpan = span == null ? Span.none() : span;
|
||||
final var fileId = effectiveSpan.getFileId() == null || effectiveSpan.getFileId().isNone()
|
||||
? function.fileId().getId()
|
||||
: effectiveSpan.getFileId().getId();
|
||||
final int start;
|
||||
final int end;
|
||||
if (effectiveSpan.getFileId() == null || effectiveSpan.getFileId().isNone()) {
|
||||
start = function.sourceStart();
|
||||
end = function.sourceEnd();
|
||||
} else {
|
||||
start = safeToInt(effectiveSpan.getStart());
|
||||
end = safeToInt(effectiveSpan.getEnd());
|
||||
}
|
||||
return new IRVMLoweringException(code, message, fileId, start, end);
|
||||
}
|
||||
|
||||
private record JumpPatch(
|
||||
int instructionIndex,
|
||||
int operationIndex,
|
||||
|
||||
+3
@@ -39,6 +39,9 @@ public class LowerToIRVMPipelineStage implements PipelineStage {
|
||||
.error(true)
|
||||
.phase("BACKEND_LOWER_TO_IRVM")
|
||||
.code(e.code().name())
|
||||
.fileId(e.fileId())
|
||||
.start(e.start())
|
||||
.end(e.end())
|
||||
.message("[BUILD]: lower to irvm failed: " + e.getMessage())
|
||||
.exception(e));
|
||||
} catch (IRVMValidationException e) {
|
||||
|
||||
Reference in New Issue
Block a user