- | transform (X.IMUL (dest, src)) =
- unspill X.Long (dest, spillreg1) @
- [ X.IMUL(
- realoper dest handle Spilled => X.REG spillreg1,
- realoper src handle Spilled => stackoper src)] @
- spill X.Long (dest, spillreg1)
- | transform (X.IMUL3 (dest, src, const)) =
- unspill X.Long ((X.stripsize src), spillreg2) @
- [ X.IMUL3(
- realoper dest handle Spilled => X.REG spillreg1,
- realoper src handle Spilled => X.REG spillreg2,
- const)] @
- spill X.Long (dest, spillreg1)
- | transform (X.ADD (dest, src)) =
- let
- val (insns, realop) = ophit dest
- in
- unspill X.Long (src, spillreg2) @ insns @
- [ X.ADD(realop,
- realoper src handle Spilled => X.REG spillreg2)]
- end
- | transform (X.IDIV (src)) = [ X.IDIV(realoper src handle Spilled => stackoper src)]
- | transform (X.NEG (src)) = [ X.NEG(realoper src handle Spilled => stackoper src)]
- | transform (X.NOT (src)) = [ X.NOT(realoper src handle Spilled => stackoper src)]
- | transform (X.SAL (dest, shft)) =
- [ X.SAL (
- realoper dest handle Spilled => stackoper dest,
- shft)]
- | transform (X.SAR (dest, shft)) =
- [ X.SAR (
- realoper dest handle Spilled => stackoper dest,
- shft)]
- | transform (X.CLTD) = [ X.CLTD ]
- | transform (X.AND (dest, src)) =
- unspill X.Long (src, spillreg1) @
- [ X.AND(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.OR (dest, src)) =
- unspill X.Long (src, spillreg1) @
- [ X.OR(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.XOR (dest, src)) =
- unspill X.Long (src, spillreg1) @
- [ X.XOR(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => X.REG spillreg1)]
- | transform (X.CMP (op1, op2)) =
- unspill X.Long (op2, spillreg1) @
- [ X.CMP(
- realoper op1 handle Spilled => stackoper op1,
- realoper op2 handle Spilled => X.REG spillreg1)]
- | transform (X.TEST (op1, op2)) =
- unspill X.Long (op2, spillreg1) @
- [ X.TEST(
- realoper op1 handle Spilled => stackoper op1,
- realoper op2 handle Spilled => X.REG spillreg1)]
- | transform (X.SETcc (c,src)) = [ X.SETcc(c, realoper src handle Spilled => stackoper src)]
- | transform (X.CALL l) = [ X.CALL l ]
- | transform (X.MOVZB (dest, src)) =
- [ X.MOVZB(
- realoper dest handle Spilled => X.REG spillreg1,
- realoper src handle Spilled => stackoper src)]
- @ spill X.Long (dest, spillreg1)
- | transform (X.RET) = if nsave < 2 then (epilogue @ [X.RET]) else [X.JMP endlbl]
- | transform (X.LABEL l) = [ X.LABEL l ]
- | transform (X.JMP l) = [ X.JMP l ]
- | transform (X.Jcc (c,l)) = [X.Jcc (c,l)]
+ | unspill _ = nil
+
+ fun unspill_ops (op1, op2) =
+ case (isspilled op1, isspilled op2)
+ of (false, false) => []
+ | (true, false) => unspill (op1, spillreg1)
+ | (false, true) => unspill (op2, spillreg2)
+ | (true, true) => unspill (op1, spillreg1) @ unspill (op2, spillreg2)
+
+ fun respill_ops (op1, op2) =
+ case (isspilled op1, isspilled op2) (* no instruction writes back to op2 *)
+ of (false, _) => []
+ | (true, _) => spill (op1, spillreg1)
+
+ fun real_op1 op1 =
+ case op1
+ of (X.TEMP temp) => if isspilled op1
+ then (X.REG spillreg1)
+ else X.REG (temptoreg temp)
+ | (X.STACKARG arg) => X.REG spillreg1
+ | r => r
+
+ fun real_ops (op1, op2) =
+ (case op1
+ of (X.TEMP temp) => if isspilled op1
+ then (X.REG spillreg1)
+ else X.REG (temptoreg temp)
+ | (X.STACKARG arg) => X.REG spillreg1
+ | r => r,
+ case op2
+ of (X.TEMP temp) => if isspilled op2
+ then (X.REG spillreg2)
+ else X.REG (temptoreg temp)
+ | (X.STACKARG arg) => X.REG spillreg2
+ | r => r)
+
+ fun whack_insn (pred, insn, op1, op2) =
+ (map (fn i => X.INSN (pred, i)) (unspill_ops (op1, op2))) @
+ [ X.INSN (pred, insn (real_ops (op1, op2))) ] @
+ (map (fn i => X.INSN (pred, i)) (respill_ops (op1, op2)))
+
+ fun transform (X.DIRECTIVE s) = [X.DIRECTIVE s]
+ | transform (X.COMMENT s) = [X.COMMENT s]
+ | transform (X.LABEL l) = [X.LABEL l]
+ | transform (X.LIVEIGN a) = transform a
+
+ (* god the special cases are going to suck *)
+ | transform (X.INSN (pred, X.MOVLIT (op1, w))) =
+ [ X.INSN (pred, X.MOVLIT (real_op1 op1, w)) ] @
+ (if isspilled op1
+ then spill (op1, spillreg1)
+ else [])
+ | transform (X.INSN (pred, X.MOVSYM (op1, w))) =
+ [ X.INSN (pred, X.MOVSYM (real_op1 op1, w)) ] @
+ (if isspilled op1
+ then spill (op1, spillreg1)
+ else [])
+ | transform (X.INSN (pred, X.MOVLBL (op1, w))) =
+ [ X.INSN (pred, X.MOVLBL (real_op1 op1, w)) ] @
+ (if isspilled op1
+ then spill (op1, spillreg1)
+ else [])
+
+ (* and here comes the boilerplate *)
+ | transform (X.INSN (pred, X.LDR (op1, op2))) = whack_insn (pred, X.LDR, op1, op2)
+ | transform (X.INSN (pred, X.STO (op1, op2))) = whack_insn (pred, X.STO, op1, op2)
+ | transform (X.INSN (pred, X.MOV (op1, op2))) = whack_insn (pred, X.MOV, op1, op2)
+ | transform (X.INSN (pred, X.MOVS (op1, op2))) = whack_insn (pred, X.MOVS, op1, op2)
+ | transform (X.INSN (pred, X.ADD (op1, op2))) = whack_insn (pred, X.ADD, op1, op2)
+ | transform (X.INSN (pred, X.ADDS (op1, op2))) = whack_insn (pred, X.ADDS, op1, op2)
+ | transform (X.INSN (pred, X.SUB (op1, op2))) = whack_insn (pred, X.SUB, op1, op2)
+ | transform (X.INSN (pred, X.SUBS (op1, op2))) = whack_insn (pred, X.SUBS, op1, op2)
+ | transform (X.INSN (pred, X.AND (op1, op2))) = whack_insn (pred, X.AND, op1, op2)
+ | transform (X.INSN (pred, X.ANDS (op1, op2))) = whack_insn (pred, X.ANDS, op1, op2)
+ | transform (X.INSN (pred, X.NOT (op1, op2))) = whack_insn (pred, X.NOT, op1, op2)
+ | transform (X.INSN (pred, X.NOTS (op1, op2))) = whack_insn (pred, X.NOTS, op1, op2)
+ | transform (X.INSN (pred, X.PUSH (op1, op2))) = if isspilled op2
+ then raise ErrorMsg.InternalError "PUSH on spilled op2 is not possible"
+ else [ X.INSN (pred, X.PUSH (real_ops (op1, op2))) ]
+ | transform (X.INSN (pred, X.POP (X.REG X.SP, X.REG X.PC))) = [ X.INSN (pred, X.MOVLBL (X.REG X.PC, endlbl)) ] (* optimize epilogue? *)
+ | transform (X.INSN (pred, X.POP (op1, op2))) = if isspilled op2
+ then raise ErrorMsg.InternalError "POP on spilled op2 is not possible"
+ else [ X.INSN (pred, X.POP (real_ops (op1, op2))) ]
+ | transform (X.INSN (pred, X.CALL (op1, op2, i))) = if isspilled op2
+ then raise ErrorMsg.InternalError "CALL on spilled op2 is not possible"
+ else [ X.INSN (pred, X.CALL ((fn (x, y) => (x, y, i)) (real_ops (op1, op2)))) ]
+ | transform (X.INSN (pred, X.SHR (op1, op2))) = whack_insn (pred, X.SHR, op1, op2)
+ | transform (X.INSN (pred, X.SHL (op1, op2))) = whack_insn (pred, X.SHL, op1, op2)
+ (*| transform _ = raise ErrorMsg.InternalError "unimplemented"*)