+ | munch_exp d (T.BINOP(T.MUL, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("mul"))
+ in
+ (munch_exp d e1) @ (munch_exp t1 e2) @ [X.IMUL(d, t1)]
+ end
+ | munch_exp d (T.BINOP(T.DIV, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("div"))
+ in
+ (munch_exp t1 e1) @ (munch_exp d e2) @
+ [X.MOV (X.REG X.EAX, t1), X.CLTD, X.IDIV d, X.MOV (d, X.REG X.EAX)]
+ end
+ | munch_exp d (T.BINOP(T.MOD, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("mod"))
+ in
+ (munch_exp t1 e1) @ (munch_exp d e2) @
+ [X.MOV (X.REG X.EAX, t1), X.CLTD, X.IDIV d, X.MOV (d, X.REG X.EDX)]
+ end
+ | munch_exp d (T.BINOP(T.LSH, e1, T.CONST n)) = (munch_exp d e1) @ [X.SAL (d, X.CONST (n mod 0w32))]
+ | munch_exp d (T.BINOP(T.LSH, e1, T.TEMP t)) = (munch_exp d e1) @ [X.MOV (X.REG X.ECX, X.TEMP t), X.SAL (d, X.REG X.ECX)]
+ | munch_exp d (T.BINOP(T.LSH, e1, e2)) =
+ let
+ val t = X.TEMP (Temp.new ("lsh"))
+ in
+ (munch_exp d e1) @ (munch_exp t e2) @ [X.MOV (X.REG X.ECX, t), X.SAL (d, X.REG X.ECX)]
+ end
+ | munch_exp d (T.BINOP(T.RSH, e1, T.CONST n)) = (munch_exp d e1) @ [X.SAR (d, X.CONST (n mod 0w32))]
+ | munch_exp d (T.BINOP(T.RSH, e1, T.TEMP t)) = (munch_exp d e1) @ [X.MOV (X.REG X.ECX, X.TEMP t), X.SAR (d, X.REG X.ECX)]
+ | munch_exp d (T.BINOP(T.RSH, e1, e2)) =
+ let
+ val t = X.TEMP (Temp.new ("rsh"))
+ in
+ (munch_exp d e1) @ (munch_exp t e2) @ [X.MOV (X.REG X.ECX, t), X.SAR (d, X.REG X.ECX)]
+ end
+ | munch_exp d (T.BINOP(T.BITAND, T.CONST n, e1)) = (munch_exp d e1) @ [X.AND (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITAND, e1, T.CONST n)) = (munch_exp d e1) @ [X.AND (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITAND, T.TEMP t, e1)) = (munch_exp d e1) @ [X.AND (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITAND, e1, T.TEMP t)) = (munch_exp d e1) @ [X.AND (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITAND, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("bitand"))
+ in
+ (munch_exp d e1) @ (munch_exp t1 e2) @ [X.AND(d, t1)]
+ end
+ | munch_exp d (T.BINOP(T.BITOR, T.CONST n, e1)) = (munch_exp d e1) @ [X.OR (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITOR, e1, T.CONST n)) = (munch_exp d e1) @ [X.OR (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITOR, T.TEMP t, e1)) = (munch_exp d e1) @ [X.OR (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITOR, e1, T.TEMP t)) = (munch_exp d e1) @ [X.OR (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITOR, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("bitor"))
+ in
+ (munch_exp d e1) @ (munch_exp t1 e2) @ [X.OR(d, t1)]
+ end
+ | munch_exp d (T.BINOP(T.BITXOR, T.CONST n, e1)) = (munch_exp d e1) @ [X.XOR (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITXOR, e1, T.CONST n)) = (munch_exp d e1) @ [X.XOR (d, X.CONST n)]
+ | munch_exp d (T.BINOP(T.BITXOR, T.TEMP t, e1)) = (munch_exp d e1) @ [X.XOR (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITXOR, e1, T.TEMP t)) = (munch_exp d e1) @ [X.XOR (d, X.TEMP t)]
+ | munch_exp d (T.BINOP(T.BITXOR, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("bitxor"))
+ in
+ (munch_exp d e1) @ (munch_exp t1 e2) @ [X.XOR(d, t1)]
+ end
+ | munch_exp d (a as T.BINOP(T.LOGAND, e1, e2)) =
+ let
+ val (insn1, pos1, neg1) = munch_cond e1
+ val (insn2, pos2, neg2) = munch_cond e2
+ val t1 = X.TEMP (Temp.new("logand 1"))
+ val t2 = X.TEMP (Temp.new("logand 2"))
+ val l = Label.new ()
+ in
+ if (effect e2 orelse (length insn2 > 10))
+ then (insn1) @
+ [X.SETcc(pos1, t1), X.Jcc (neg1, l)] @
+ (insn2) @
+ [X.SETcc(pos2, t1), X.LABEL l, X.MOVZB(d, t1)]
+ else insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.SIZE(X.Byte, X.AND(t1, t2)), X.MOVZB(d, t1)]
+ end
+ | munch_exp d (a as T.BINOP(T.LOGOR, e1, e2)) =
+ let
+ val (insn1, pos1, neg1) = munch_cond e1
+ val (insn2, pos2, neg2) = munch_cond e2
+ val t1 = X.TEMP (Temp.new("logor 1"))
+ val t2 = X.TEMP (Temp.new("logor 2"))
+ val l = Label.new ()
+ in
+ if (effect e2 orelse (length insn2 > 10))
+ then (insn1) @
+ [X.SETcc(pos1, t1), X.Jcc (pos1, l)] @
+ (insn2) @
+ [X.SETcc(pos2, t1), X.LABEL l, X.MOVZB(d, t1)]
+ else insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.SIZE(X.Byte, X.OR(t1, t2)), X.MOVZB(d, t1)]
+ end
+ | munch_exp d (a as T.BINOP(T.EQ, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (a as T.BINOP(T.NEQ, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (a as T.BINOP(T.LE, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (a as T.BINOP(T.LT, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (a as T.BINOP(T.GE, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (a as T.BINOP(T.GT, _, _)) =
+ let val (insns, pos, neg) = munch_cond a in insns @ [X.SETcc (pos, d), X.MOVZB(d, d)] end
+ | munch_exp d (T.UNOP(T.NEG, T.CONST n)) = [X.MOV (d, X.CONST (~n))]
+ | munch_exp d (T.UNOP(T.NEG, e1)) = (munch_exp d e1) @ [X.NEG d]
+ | munch_exp d (T.UNOP(T.BITNOT, T.CONST n)) = [X.MOV (d, X.CONST (Word32.notb n))]
+ | munch_exp d (T.UNOP(T.BITNOT, e1)) = (munch_exp d e1) @ [X.NOT d]
+ | munch_exp d (T.UNOP(T.BANG, T.CONST n)) = if (n = 0w0) then [X.MOV (d, X.CONST 0w1)] else [X.MOV (d, X.CONST 0w0)]
+ | munch_exp d (T.UNOP(T.BANG, e)) =
+ let
+ val (insns, pos, neg) = munch_cond e
+ in
+ insns @ [X.SETcc (neg, d), X.MOVZB(d, d)]
+ end
+ (* munch_cond : T.exp -> X.insn list * X.cond * X.cond
+ * munch_cond stm generates code to set flags, and then returns a conditional
+ * to test if the expression was true and for if it was false.
+ *)
+ and munch_cond (T.UNOP (T.BANG, e)) =
+ let
+ val (insns, pos, neg) = munch_cond e
+ in
+ (insns, neg, pos)
+ end
+ | munch_cond (T.BINOP(T.NEQ, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.NE, X.E)
+ | munch_cond (T.BINOP(T.NEQ, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.NE, X.E)
+ | munch_cond (T.BINOP(T.NEQ, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const neq")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.NE, X.E) end
+ | munch_cond (T.BINOP(T.NEQ, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const neq")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.NE, X.E) end
+ | munch_cond (T.BINOP(T.NEQ, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const neq")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.NE, X.E) end
+ | munch_cond (T.BINOP(T.NEQ, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const neq")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.NE, X.E) end
+ | munch_cond (T.BINOP(T.NEQ, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var neq 1"))
+ val t2 = X.TEMP (Temp.new ("var neq 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.NE, X.E)
+ end
+ | munch_cond (T.BINOP(T.EQ, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.E, X.NE)
+ | munch_cond (T.BINOP(T.EQ, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.E, X.NE)
+ | munch_cond (T.BINOP(T.EQ, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const eq")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.E, X.NE) end
+ | munch_cond (T.BINOP(T.EQ, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const eq")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.E, X.NE) end
+ | munch_cond (T.BINOP(T.EQ, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const eq")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.E, X.NE) end
+ | munch_cond (T.BINOP(T.EQ, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const eq")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.E, X.NE) end
+ | munch_cond (T.BINOP(T.EQ, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var eq 1"))
+ val t2 = X.TEMP (Temp.new ("var eq 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.E, X.NE)
+ end
+ | munch_cond (T.BINOP(T.LE, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.LE, X.G)
+ | munch_cond (T.BINOP(T.LE, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.GE, X.L)
+ | munch_cond (T.BINOP(T.LE, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const le")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.GE, X.L) end
+ | munch_cond (T.BINOP(T.LE, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const le")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.LE, X.G) end
+ | munch_cond (T.BINOP(T.LE, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const le")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.GE, X.L) end
+ | munch_cond (T.BINOP(T.LE, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const le")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.LE, X.G) end
+ | munch_cond (T.BINOP(T.LE, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var le 1"))
+ val t2 = X.TEMP (Temp.new ("var le 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.LE, X.G)
+ end
+ | munch_cond (T.BINOP(T.LT, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.L, X.GE)
+ | munch_cond (T.BINOP(T.LT, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.G, X.LE)
+ | munch_cond (T.BINOP(T.LT, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const lt")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.G, X.LE) end
+ | munch_cond (T.BINOP(T.LT, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const lt")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.L, X.GE) end
+ | munch_cond (T.BINOP(T.LT, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const lt")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.G, X.LE) end
+ | munch_cond (T.BINOP(T.LT, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const lt")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.L, X.GE) end
+ | munch_cond (T.BINOP(T.LT, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var lt 1"))
+ val t2 = X.TEMP (Temp.new ("var lt 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.L, X.GE)
+ end
+ | munch_cond (T.BINOP(T.GT, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.G, X.LE)
+ | munch_cond (T.BINOP(T.GT, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.L, X.GE)
+ | munch_cond (T.BINOP(T.GT, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const gt")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.G, X.LE) end
+ | munch_cond (T.BINOP(T.GT, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const gt")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.L, X.GE) end
+ | munch_cond (T.BINOP(T.GT, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const gt")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.G, X.LE) end
+ | munch_cond (T.BINOP(T.GT, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const gt")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.L, X.GE) end
+ | munch_cond (T.BINOP(T.GT, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var gt 1"))
+ val t2 = X.TEMP (Temp.new ("var gt 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.G, X.LE)
+ end
+ | munch_cond (T.BINOP(T.GE, T.TEMP t, T.CONST n)) = ([X.CMP(X.TEMP t, X.CONST n)], X.GE, X.L)
+ | munch_cond (T.BINOP(T.GE, T.CONST n, T.TEMP t)) = ([X.CMP(X.TEMP t, X.CONST n)], X.LE, X.G)
+ | munch_cond (T.BINOP(T.GE, e1, T.CONST n)) =
+ let val t = X.TEMP (Temp.new ("const ge")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.GE, X.L) end
+ | munch_cond (T.BINOP(T.GE, T.CONST n, e1)) =
+ let val t = X.TEMP (Temp.new ("const ge")) in (munch_exp t e1 @ [X.CMP(t, X.CONST n)], X.LE, X.G) end
+ | munch_cond (T.BINOP(T.GE, e1, T.TEMP t)) =
+ let val t1 = X.TEMP (Temp.new ("const ge")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.GE, X.L) end
+ | munch_cond (T.BINOP(T.GE, T.TEMP t, e1)) =
+ let val t1 = X.TEMP (Temp.new ("const ge")) in (munch_exp t1 e1 @ [X.CMP(t1, X.TEMP t)], X.LE, X.G) end
+ | munch_cond (T.BINOP(T.GE, e1, e2)) =
+ let
+ val t1 = X.TEMP (Temp.new ("var ge 1"))
+ val t2 = X.TEMP (Temp.new ("var ge 2"))
+ in
+ (munch_exp t1 e1 @ munch_exp t2 e2 @
+ [X.CMP(t1, t2)], X.GE, X.L)
+ end
+ | munch_cond (T.BINOP(T.LOGOR, e1, e2)) =
+ let
+ val (insn1, pos1, neg1) = munch_cond e1
+ val (insn2, pos2, neg2) = munch_cond e2
+ val t1 = X.TEMP (Temp.new("logor c 1"))
+ val t2 = X.TEMP (Temp.new("logor c 2"))
+ val l = Label.new ()
+ in
+ if (effect e2 orelse (length insn2 > 10))
+ then ((insn1) @
+ [X.SETcc (pos1, t1), X.Jcc (pos1, l)] @
+ (insn2) @
+ [X.SETcc (pos2, t1), X.LABEL l, X.SIZE (X.Byte, X.TEST (t1, t1))],
+ X.NE, X.E)
+ else (insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.SIZE(X.Byte, X.OR(t1, t2))], X.NE, X.E)
+ end
+ | munch_cond (T.BINOP(T.LOGAND, e1, e2)) =
+ let
+ val (insn1, pos1, neg1) = munch_cond e1
+ val (insn2, pos2, neg2) = munch_cond e2
+ val t1 = X.TEMP (Temp.new("logand c 1"))
+ val t2 = X.TEMP (Temp.new("logand c 2"))
+ val l = Label.new ()
+ in
+ if (effect e2 orelse (length insn2 > 10))
+ then ((insn1) @
+ [X.SETcc (pos1, t1), X.Jcc (neg1, l)] @
+ (insn2) @
+ [X.SETcc (pos2, t1), X.LABEL l, X.SIZE (X.Byte, X.TEST (t1, t1))],
+ X.NE, X.E)
+ else (insn1 @ [X.SETcc (pos1, t1)] @ insn2 @ [X.SETcc (pos2, t2), X.SIZE(X.Byte, X.AND(t1, t2))], X.NE, X.E)
+ end
+ | munch_cond e =
+ let
+ val t = X.TEMP (Temp.new ("munch c"))
+ in
+ (munch_exp t e @ [ X.TEST (t,t) ], X.NE, X.E)
+ end