refactor: merge MemLocation with IndexedAddress

This commit is contained in:
2025-02-28 16:26:40 +00:00
parent 61643a49eb
commit 1a39950a7b
5 changed files with 62 additions and 40 deletions

View File

@@ -112,8 +112,8 @@ object asmGenerator {
Builtin.PrintCharArray,
Chain(
stackAlign,
Load(RDX, IndexAddress(RSI, KnownType.Int.size.toInt)),
Move(Register(D32, SI), MemLocation(RSI, D32)),
Load(RDX, MemLocation(RSI, KnownType.Int.size.toInt)),
Move(Register(D32, SI), MemLocation(RSI, opSize = Some(D32))),
assemblyIR.Call(CLibFunc.PrintF),
Xor(RDI, RDI),
assemblyIR.Call(CLibFunc.Fflush)
@@ -148,7 +148,7 @@ object asmGenerator {
stackAlign,
Subtract(Register(Q64, SP), ImmediateVal(8)),
Push(RSI),
Load(RSI, MemLocation(Register(Q64, SP), Q64)),
Load(RSI, MemLocation(Register(Q64, SP), opSize = Some(Q64))),
assemblyIR.Call(CLibFunc.Scanf),
Pop(RAX)
)
@@ -167,9 +167,10 @@ object asmGenerator {
lhs match {
case ident: Ident =>
if (!stack.contains(ident)) asm += stack.reserve(ident)
val dest = Register(ident.ty.size, AX)
asm ++= evalExprOntoStack(rhs)
asm += stack.pop(RAX)
asm += Move(stack.accessVar(ident), RAX)
asm += Move(stack.accessVar(ident), dest)
case ArrayElem(x, i) =>
asm ++= evalExprOntoStack(rhs)
asm ++= evalExprOntoStack(i)
@@ -182,12 +183,12 @@ object asmGenerator {
asm += stack.pop(RCX)
asm += Compare(EAX, ImmediateVal(0))
asm += Jump(labelGenerator.getLabelArg(NullPtrError), Cond.Equal)
asm += Compare(MemLocation(RAX, D32), ECX)
asm += Compare(MemLocation(RAX, opSize = Some(D32)), ECX)
asm += Jump(labelGenerator.getLabelArg(OutOfBoundsError), Cond.LessEqual)
asm += stack.pop(RDX)
asm += Move(
IndexAddress(RAX, KnownType.Int.size.toInt, RCX, x.ty.elemSize.toInt),
MemLocation(RAX, KnownType.Int.size.toInt, (RCX, x.ty.elemSize.toInt)),
Register(x.ty.elemSize, DX)
)
}
@@ -248,13 +249,17 @@ object asmGenerator {
expr match {
case IntLiter(v) => asm += stack.push(KnownType.Int.size, ImmediateVal(v))
case CharLiter(v) => asm += stack.push(KnownType.Char.size, ImmediateVal(v.toInt))
case ident: Ident => asm += stack.push(ident.ty.size, stack.accessVar(ident))
case ident: Ident =>
val location = stack.accessVar(ident)
// items in stack are guaranteed to be in Q64 slots,
// so we are safe to wipe the opSize from the memory location
asm += stack.push(ident.ty.size, location.copy(opSize = None))
case array @ ArrayLiter(elems) =>
expr.ty match {
case KnownType.String =>
val str = elems.collect { case CharLiter(v) => v }.mkString
asm += Load(RAX, IndexAddress(RIP, labelGenerator.getLabelArg(str)))
asm += Load(RAX, MemLocation(RIP, labelGenerator.getLabelArg(str)))
asm += stack.push(Q64, RAX)
case ty =>
asm ++= generateCall(
@@ -263,12 +268,12 @@ object asmGenerator {
)
asm += stack.push(Q64, RAX)
// Store the length of the array at the start
asm += Move(MemLocation(RAX, D32), ImmediateVal(elems.size))
asm += Move(MemLocation(RAX, opSize = Some(D32)), ImmediateVal(elems.size))
elems.zipWithIndex.foldMap { (elem, i) =>
asm ++= evalExprOntoStack(elem)
asm += stack.pop(RCX)
asm += stack.pop(RAX)
asm += Move(IndexAddress(RAX, 4 + i * ty.elemSize.toInt), Register(ty.elemSize, CX))
asm += Move(MemLocation(RAX, 4 + i * ty.elemSize.toInt), Register(ty.elemSize, CX))
asm += stack.push(Q64, RAX)
}
}
@@ -289,12 +294,12 @@ object asmGenerator {
asm += stack.pop(RAX)
asm += Compare(EAX, ImmediateVal(0))
asm += Jump(labelGenerator.getLabelArg(NullPtrError), Cond.Equal)
asm += Compare(MemLocation(RAX, D32), ECX)
asm += Compare(MemLocation(RAX, opSize = Some(D32)), ECX)
asm += Jump(labelGenerator.getLabelArg(OutOfBoundsError), Cond.LessEqual)
// + Int because we store the length of the array at the start
asm += Move(
Register(x.ty.elemSize, AX),
IndexAddress(RAX, KnownType.Int.size.toInt, RCX, x.ty.elemSize.toInt)
MemLocation(RAX, KnownType.Int.size.toInt, (RCX, x.ty.elemSize.toInt))
)
asm += stack.push(x.ty.elemSize, RAX)
case UnaryOp(x, op) =>
@@ -308,7 +313,7 @@ object asmGenerator {
case UnaryOperator.Ord => // No op needed
case UnaryOperator.Len =>
asm += stack.pop(RAX)
asm += Move(EAX, MemLocation(RAX, D32))
asm += Move(EAX, MemLocation(RAX, opSize = Some(D32)))
asm += stack.push(D32, RAX)
case UnaryOperator.Negate =>
asm += Xor(EAX, EAX)
@@ -376,7 +381,7 @@ object asmGenerator {
stack.size == stackSizeStart + 1,
"Sanity check: ONLY the evaluated expression should have been pushed onto the stack"
)
asm ++= zeroRest(MemLocation(stack.head.pointer, Q64), expr.ty.size)
asm ++= zeroRest(MemLocation(base = stack.head.base, opSize = Some(Q64)), expr.ty.size)
asm
}