feat: implements outofmemoryerror handling

This commit is contained in:
Guy C
2025-02-28 11:58:38 +00:00
parent f2a1eaf24c
commit b733d233b0
2 changed files with 24 additions and 3 deletions

View File

@@ -117,6 +117,22 @@ object RuntimeError {
)
}
case object OutOfMemoryError extends RuntimeError {
val strLabel = ".L._errOutOfMemory_str0"
val errStr = "fatal error: out of memory"
val errLabel = ".L._errOutOfMemory"
def generateHandler: Chain[AsmLine] = Chain(
LabelDef(OutOfMemoryError.errLabel),
stackAlign,
Load(RDI, IndexAddress(RIP, LabelArg(OutOfMemoryError.strLabel))),
assemblyIR.Call(CLibFunc.PrintF),
Move(RDI, ImmediateVal(255)),
assemblyIR.Call(CLibFunc.Exit)
)
}
val all: Chain[RuntimeError] =
Chain(ZeroDivError, BadChrError, NullPtrError, OverflowError, OutOfBoundsError)
Chain(ZeroDivError, BadChrError, NullPtrError, OverflowError, OutOfBoundsError,
OutOfMemoryError)
}

View File

@@ -141,8 +141,13 @@ object asmGenerator {
chain ++= wrapBuiltinFunc(
labelGenerator.getLabel(Builtin.Malloc),
Chain(stackAlign, assemblyIR.Call(CLibFunc.Malloc))
// Out of memory check is optional
Chain(
stackAlign,
assemblyIR.Call(CLibFunc.Malloc),
// Out of memory check
Compare(RAX, ImmediateVal(0)),
Jump(LabelArg(OutOfMemoryError.errLabel), Cond.Equal)
)
)
chain ++= wrapBuiltinFunc(