feat: implement all runtime errors #32

Merged
jt2622 merged 16 commits from runtime-errors into master 2025-02-28 00:23:13 +00:00
5 changed files with 174 additions and 57 deletions
Showing only changes of commit 3a2af6f95d - Show all commits

View File

@@ -100,5 +100,23 @@ object RuntimeError {
} }
val all: Chain[RuntimeError] = Chain(ZeroDivError, BadChrError, NullPtrError, OverflowError) case object OutOfBoundsError extends RuntimeError {
val strLabel = ".L._errOutOfBounds_str0"
val errStr = "fatal error: array index %d out of bounds"
val errLabel = ".L._errOutOfBounds"
def generateHandler: Chain[AsmLine] = Chain(
LabelDef(OutOfBoundsError.errLabel),
Pop(RSI), // le index
stackAlign,
Load(RDI, IndexAddress(RIP, LabelArg(OutOfBoundsError.strLabel))),
assemblyIR.Call(CLibFunc.PrintF),
Move(RDI, ImmediateVal(255)),
assemblyIR.Call(CLibFunc.Exit)
)
}
val all: Chain[RuntimeError] =
Chain(ZeroDivError, BadChrError, NullPtrError, OverflowError, OutOfBoundsError)
} }

View File

@@ -190,9 +190,13 @@ object asmGenerator {
case ArrayElem(x, i) => case ArrayElem(x, i) =>
chain ++= evalExprOntoStack(rhs) chain ++= evalExprOntoStack(rhs)
chain ++= evalExprOntoStack(i) chain ++= evalExprOntoStack(i)
chain += Compare(stack.head, ImmediateVal(0))
chain += Jump(LabelArg(OutOfBoundsError.errLabel), Cond.Less)
chain ++= evalExprOntoStack(x) chain ++= evalExprOntoStack(x)
chain += stack.pop(RAX) chain += stack.pop(RAX)
chain += stack.pop(RCX) chain += stack.pop(RCX)
// chain += Compare(RAX, RCX)
// chain += Jump(LabelArg(OutOfBoundsError.errLabel), Cond.GreaterEqual)
chain += stack.pop(RDX) chain += stack.pop(RDX)
chain += Move( chain += Move(
@@ -294,6 +298,8 @@ object asmGenerator {
chain ++= evalExprOntoStack(x) chain ++= evalExprOntoStack(x)
chain ++= evalExprOntoStack(i) chain ++= evalExprOntoStack(i)
chain += stack.pop(RCX) chain += stack.pop(RCX)
chain += Compare(RCX, ImmediateVal(0))
chain += Jump(LabelArg(OutOfBoundsError.errLabel), Cond.Less)
chain += stack.pop(RAX) chain += stack.pop(RAX)
// + Int because we store the length of the array at the start // + Int because we store the length of the array at the start
chain += Move( chain += Move(