refactor: extract Stack, proper register naming and sizes

This commit is contained in:
2025-02-27 01:49:22 +00:00
parent 808a59f58a
commit 58df1d7bb9
3 changed files with 181 additions and 54 deletions

View File

@@ -0,0 +1,29 @@
package wacc
object sizeExtensions {
import microWacc._
import types._
import assemblyIR.Size
extension (expr: Expr) {
/** Calculate the size (bytes) of the heap required for the expression. */
def heapSize: Int = (expr, expr.ty) match {
case (ArrayLiter(elems), KnownType.Array(KnownType.Char)) =>
KnownType.Int.size.toInt + elems.size.toInt * KnownType.Char.size.toInt
case (ArrayLiter(elems), _) =>
KnownType.Int.size.toInt + elems.map(_.ty.size.toInt).sum
case _ => expr.ty.size.toInt
}
}
extension (ty: SemType) {
/** Calculate the size (bytes) of a type in a register. */
def size: Size = ty match {
case KnownType.Int => Size.D32
case KnownType.Bool | KnownType.Char => Size.B8
case KnownType.String | KnownType.Array(_) | KnownType.Pair(_, _) | ? => Size.Q64
}
}
}