- | transform (X.MOV (dest, src)) =
- let
- val (insns1, realop1 as (_,s1)) = ophit dest
- val (insns2, realop2 as (_,s2)) = ophit src
- in
- if(isspilled dest andalso isspilled src) then
- insns2 @ [X.MOV ((X.REG spillreg2, s2), realop2)] @ insns1 @ [X.MOV (realop1, (X.REG spillreg2, s1))]
- else
- insns1 @ insns2 @ [X.MOV (realop1, realop2)]
- end
- | transform (X.LEA (dest, src)) =
- let
- val (insns1, realop1 as (_,s1)) = ophit dest
- val (insns2, realop2 as (_,s2)) = ophit src
- in
- if(isspilled dest andalso isspilled src) then
- insns2 @ [X.LEA ((X.REG spillreg2, s2), realop2)] @ insns1 @ [X.MOV (realop1, (X.REG spillreg2, s1))]
- else
- insns1 @ insns2 @ [X.LEA (realop1, realop2)]
- end
- | transform (X.SUB (dest, src)) =
- let
- val (insns, realop) = ophit dest
- in
- unspill (src, spillreg2) @ insns @
- [ X.SUB(realop,
- realoper src handle Spilled => (X.REG spillreg2, X.osize realop))]
- end
- | transform (X.IMUL (dest, src)) =
- unspill (dest, spillreg1) @
- [ X.IMUL(
- realoper dest handle Spilled => (X.REG spillreg1, X.osize dest),
- realoper src handle Spilled => stackoper src)] @
- spill (dest, spillreg1)
- | transform (X.IMUL3 (dest, src, const)) =
- unspill (src, spillreg2) @
- [ X.IMUL3(
- realoper dest handle Spilled => (X.REG spillreg1, X.osize dest),
- realoper src handle Spilled => (X.REG spillreg2, X.osize src),
- const)] @
- spill (dest, spillreg1)
- | transform (X.ADD (dest, src)) =
- let
- val (insns, realop) = ophit dest
- in
- unspill (src, spillreg2) @ insns @
- [ X.ADD(realop,
- realoper src handle Spilled => (X.REG spillreg2, X.osize realop))]
- 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 (src, spillreg1) @
- [ X.AND(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => (X.REG spillreg1, X.osize src))]
- | transform (X.OR (dest, src)) =
- unspill (src, spillreg1) @
- [ X.OR(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => (X.REG spillreg1, X.osize src))]
- | transform (X.XOR (dest, src)) =
- unspill (src, spillreg1) @
- [ X.XOR(
- realoper dest handle Spilled => stackoper dest,
- realoper src handle Spilled => (X.REG spillreg1, X.osize src))]
- | transform (X.CMP (op1, op2)) =
- let
- val (insns1, realop1) = ophit op1
- in
- if(isspilled realop1 andalso isspilled op2) then
- unspill (op2, spillreg2) @ insns1 @ [X.CMP (realop1, (X.REG spillreg2, X.osize realop1))]
- else
- insns1 @ [X.CMP (realop1, realoper op2 handle Spilled => stackoper op2)]
- end
- | transform (X.TEST (op1, op2)) =
- unspill (op2, spillreg1) @
- [ X.TEST(
- realoper op1 handle Spilled => stackoper op1,
- realoper op2 handle Spilled => (X.REG spillreg1, X.osize op2))]
- | transform (X.SETcc (c,src)) = [ X.SETcc(c, realoper src handle Spilled => stackoper src)]
- | transform (X.CMOVcc (c, dest, src)) =
- let
- val (insns1, realop1) = ophit dest
- val (insns2, realop2) = ophit src
- in
- if(isspilled dest andalso isspilled src) then
- insns2 @ [X.MOV ((X.REG spillreg2, X.osize src), realop2)] @ insns1 @ [X.CMOVcc (c, realop1, (X.REG spillreg2, X.osize src))]
- else
- insns1 @ insns2 @ [X.CMOVcc (c, realop1, realop2)]
- end
- | transform (X.CALL l) = [ X.CALL l ]
- | transform (X.MOVZB (dest, src)) =
- [ X.MOVZB(
- realoper dest handle Spilled => (X.REG spillreg1, X.osize dest),
- realoper src handle Spilled => stackoper src)]
- @ spill (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)]
- | transform _ = raise ErrorMsg.InternalError "probably movsc: unimplemented"
+
+ (* 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 map (fn i => X.INSN (pred, i)) (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 map (fn i => X.INSN (pred, i)) (spill (op1, spillreg1))
+ else [])
+ | transform (X.INSN (pred, X.MOVSTR (op1, w))) =
+ [ X.INSN (pred, X.MOVSTR (real_op1 op1, w)) ] @
+ (if isspilled op1
+ then map (fn i => X.INSN (pred, i)) (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 map (fn i => X.INSN (pred, i)) (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"*)