| 1 |
(* amd64CompInstrExt.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2007 The Fellowship of SML/NJ (http://smlnj.org) |
| 4 |
* |
| 5 |
* emit code for extensions to the amd64 instruction set. |
| 6 |
*) |
| 7 |
|
| 8 |
signature AMD64COMP_INSTR_EXT = sig |
| 9 |
structure I : AMD64INSTR |
| 10 |
structure TS : MLTREE_STREAM (* where T = I.T *) |
| 11 |
where type T.Basis.cond = I.T.Basis.cond |
| 12 |
and type T.Basis.div_rounding_mode = I.T.Basis.div_rounding_mode |
| 13 |
and type T.Basis.ext = I.T.Basis.ext |
| 14 |
and type T.Basis.fcond = I.T.Basis.fcond |
| 15 |
and type T.Basis.rounding_mode = I.T.Basis.rounding_mode |
| 16 |
and type T.Constant.const = I.T.Constant.const |
| 17 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) I.T.Extension.ccx |
| 18 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) I.T.Extension.fx |
| 19 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) I.T.Extension.rx |
| 20 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) I.T.Extension.sx |
| 21 |
and type T.I.div_rounding_mode = I.T.I.div_rounding_mode |
| 22 |
and type T.Region.region = I.T.Region.region |
| 23 |
and type T.ccexp = I.T.ccexp |
| 24 |
and type T.fexp = I.T.fexp |
| 25 |
(* and type T.labexp = I.T.labexp *) |
| 26 |
and type T.mlrisc = I.T.mlrisc |
| 27 |
and type T.oper = I.T.oper |
| 28 |
and type T.rep = I.T.rep |
| 29 |
and type T.rexp = I.T.rexp |
| 30 |
and type T.stm = I.T.stm |
| 31 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = I and P = TS.S.P *) |
| 32 |
where type I.addressing_mode = I.addressing_mode |
| 33 |
and type I.ea = I.ea |
| 34 |
and type I.instr = I.instr |
| 35 |
and type I.instruction = I.instruction |
| 36 |
and type I.operand = I.operand |
| 37 |
where type P.Client.pseudo_op = TS.S.P.Client.pseudo_op |
| 38 |
and type P.T.Basis.cond = TS.S.P.T.Basis.cond |
| 39 |
and type P.T.Basis.div_rounding_mode = TS.S.P.T.Basis.div_rounding_mode |
| 40 |
and type P.T.Basis.ext = TS.S.P.T.Basis.ext |
| 41 |
and type P.T.Basis.fcond = TS.S.P.T.Basis.fcond |
| 42 |
and type P.T.Basis.rounding_mode = TS.S.P.T.Basis.rounding_mode |
| 43 |
and type P.T.Constant.const = TS.S.P.T.Constant.const |
| 44 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) TS.S.P.T.Extension.ccx |
| 45 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) TS.S.P.T.Extension.fx |
| 46 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) TS.S.P.T.Extension.rx |
| 47 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) TS.S.P.T.Extension.sx |
| 48 |
and type P.T.I.div_rounding_mode = TS.S.P.T.I.div_rounding_mode |
| 49 |
and type P.T.Region.region = TS.S.P.T.Region.region |
| 50 |
and type P.T.ccexp = TS.S.P.T.ccexp |
| 51 |
and type P.T.fexp = TS.S.P.T.fexp |
| 52 |
(* and type P.T.labexp = TS.S.P.T.labexp *) |
| 53 |
and type P.T.mlrisc = TS.S.P.T.mlrisc |
| 54 |
and type P.T.oper = TS.S.P.T.oper |
| 55 |
and type P.T.rep = TS.S.P.T.rep |
| 56 |
and type P.T.rexp = TS.S.P.T.rexp |
| 57 |
and type P.T.stm = TS.S.P.T.stm |
| 58 |
|
| 59 |
type reducer = |
| 60 |
(I.instruction, I.C.cellset, I.operand, I.addressing_mode, CFG.cfg) TS.reducer |
| 61 |
|
| 62 |
val compileSext : |
| 63 |
reducer |
| 64 |
-> {stm: (I.T.stm, I.T.rexp, I.T.fexp, I.T.ccexp) AMD64InstrExt.sext, |
| 65 |
an: I.T.an list} |
| 66 |
-> unit |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
functor AMD64CompInstrExt |
| 71 |
( structure I : AMD64INSTR |
| 72 |
structure TS : MLTREE_STREAM (* where T = I.T *) |
| 73 |
where type T.Basis.cond = I.T.Basis.cond |
| 74 |
and type T.Basis.div_rounding_mode = I.T.Basis.div_rounding_mode |
| 75 |
and type T.Basis.ext = I.T.Basis.ext |
| 76 |
and type T.Basis.fcond = I.T.Basis.fcond |
| 77 |
and type T.Basis.rounding_mode = I.T.Basis.rounding_mode |
| 78 |
and type T.Constant.const = I.T.Constant.const |
| 79 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) I.T.Extension.ccx |
| 80 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) I.T.Extension.fx |
| 81 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) I.T.Extension.rx |
| 82 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) I.T.Extension.sx |
| 83 |
and type T.I.div_rounding_mode = I.T.I.div_rounding_mode |
| 84 |
and type T.Region.region = I.T.Region.region |
| 85 |
and type T.ccexp = I.T.ccexp |
| 86 |
and type T.fexp = I.T.fexp |
| 87 |
(* and type T.labexp = I.T.labexp *) |
| 88 |
and type T.mlrisc = I.T.mlrisc |
| 89 |
and type T.oper = I.T.oper |
| 90 |
and type T.rep = I.T.rep |
| 91 |
and type T.rexp = I.T.rexp |
| 92 |
and type T.stm = I.T.stm |
| 93 |
structure CFG : CONTROL_FLOW_GRAPH (* where P = TS.S.P and I = I *) |
| 94 |
where type P.Client.pseudo_op = TS.S.P.Client.pseudo_op |
| 95 |
and type P.T.Basis.cond = TS.S.P.T.Basis.cond |
| 96 |
and type P.T.Basis.div_rounding_mode = TS.S.P.T.Basis.div_rounding_mode |
| 97 |
and type P.T.Basis.ext = TS.S.P.T.Basis.ext |
| 98 |
and type P.T.Basis.fcond = TS.S.P.T.Basis.fcond |
| 99 |
and type P.T.Basis.rounding_mode = TS.S.P.T.Basis.rounding_mode |
| 100 |
and type P.T.Constant.const = TS.S.P.T.Constant.const |
| 101 |
and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) TS.S.P.T.Extension.ccx |
| 102 |
and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) TS.S.P.T.Extension.fx |
| 103 |
and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) TS.S.P.T.Extension.rx |
| 104 |
and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) TS.S.P.T.Extension.sx |
| 105 |
and type P.T.I.div_rounding_mode = TS.S.P.T.I.div_rounding_mode |
| 106 |
and type P.T.Region.region = TS.S.P.T.Region.region |
| 107 |
and type P.T.ccexp = TS.S.P.T.ccexp |
| 108 |
and type P.T.fexp = TS.S.P.T.fexp |
| 109 |
(* and type P.T.labexp = TS.S.P.T.labexp *) |
| 110 |
and type P.T.mlrisc = TS.S.P.T.mlrisc |
| 111 |
and type P.T.oper = TS.S.P.T.oper |
| 112 |
and type P.T.rep = TS.S.P.T.rep |
| 113 |
and type P.T.rexp = TS.S.P.T.rexp |
| 114 |
and type P.T.stm = TS.S.P.T.stm |
| 115 |
where type I.addressing_mode = I.addressing_mode |
| 116 |
and type I.ea = I.ea |
| 117 |
and type I.instr = I.instr |
| 118 |
and type I.instruction = I.instruction |
| 119 |
and type I.operand = I.operand |
| 120 |
) : AMD64COMP_INSTR_EXT = |
| 121 |
struct |
| 122 |
structure CFG = CFG |
| 123 |
structure T = TS.T |
| 124 |
structure I = I |
| 125 |
structure C = I.C |
| 126 |
structure X = AMD64InstrExt |
| 127 |
structure TS = TS |
| 128 |
|
| 129 |
type stm = (T.stm, T.rexp, T.fexp, T.ccexp) X.sext |
| 130 |
|
| 131 |
type reducer = |
| 132 |
(I.instruction, I.C.cellset, I.operand, I.addressing_mode, CFG.cfg) TS.reducer |
| 133 |
|
| 134 |
val rsp = C.rsp |
| 135 |
val rspOpnd = I.Direct(64,rsp) |
| 136 |
|
| 137 |
fun error msg = MLRiscErrorMsg.error("AMD64CompInstrExt", msg) |
| 138 |
|
| 139 |
val stackArea = I.Region.stack |
| 140 |
|
| 141 |
fun compileSext reducer {stm: stm, an:T.an list} = let |
| 142 |
val TS.REDUCER{operand, emit, reduceFexp, instrStream, reduceOperand, |
| 143 |
...} = reducer |
| 144 |
val TS.S.STREAM{emit=emitI, ...} = instrStream |
| 145 |
fun fstp(sz, fstpInstr, fexp) = |
| 146 |
(case fexp |
| 147 |
of T.FREG(sz', f) => |
| 148 |
if sz <> sz' then error "fstp: sz" |
| 149 |
else emitI(I.INSTR(fstpInstr(I.FDirect f))) |
| 150 |
| _ => error "fstp: fexp" |
| 151 |
(*esac*)) |
| 152 |
in |
| 153 |
case stm |
| 154 |
of X.PUSHQ(rexp) => emit(I.push(operand rexp), an) |
| 155 |
| X.POP(rexp) => emit(I.pop(operand rexp), an) |
| 156 |
| X.LEAVE => emit(I.leave, an) |
| 157 |
| X.RET(rexp) => emit(I.ret(SOME(operand rexp)), an) |
| 158 |
| X.LOCK_XADDL (src, dst) => |
| 159 |
emit (I.xadd{ |
| 160 |
(* src must be in a register *) |
| 161 |
lock=true,sz=I.I32, |
| 162 |
src=I.Direct(32,reduceOperand(operand src)), |
| 163 |
dst=operand dst}, |
| 164 |
an) |
| 165 |
| X.LOCK_XADDQ (src, dst) => |
| 166 |
emit (I.xadd{ |
| 167 |
(* src must be in a register *) |
| 168 |
lock=true,sz=I.I64, |
| 169 |
src=I.Direct(64,reduceOperand(operand src)), |
| 170 |
dst=operand dst}, |
| 171 |
an) |
| 172 |
| X.LOCK_CMPXCHGL(src, dst) => |
| 173 |
(* src must be in a register *) |
| 174 |
emit(I.cmpxchg{ |
| 175 |
lock=true,sz=I.I32, |
| 176 |
src=I.Direct(32,reduceOperand(operand src)), |
| 177 |
dst=operand dst |
| 178 |
}, an) |
| 179 |
| X.LOCK_CMPXCHGQ(src, dst) => |
| 180 |
(* src must be in a register *) |
| 181 |
emit(I.cmpxchg{ |
| 182 |
lock=true, sz=I.I64, |
| 183 |
src=I.Direct(64,reduceOperand(operand src)), |
| 184 |
dst=operand dst |
| 185 |
}, an) |
| 186 |
| X.LOCK_XCHGL(src, dst) => |
| 187 |
emit(I.xchg{ |
| 188 |
lock=true,sz=I.I32, |
| 189 |
src=operand src, |
| 190 |
dst=operand dst |
| 191 |
}, an) |
| 192 |
| X.LOCK_XCHGQ(src, dst) => |
| 193 |
emit(I.xchg{ |
| 194 |
lock=true, sz=I.I64, |
| 195 |
src=operand src, |
| 196 |
dst=operand dst |
| 197 |
}, an) |
| 198 |
| X.PAUSE => emit(I.pause, an) |
| 199 |
| X.MFENCE => emit(I.mfence, an) |
| 200 |
| X.LFENCE => emit(I.lfence, an) |
| 201 |
| X.SFENCE => emit(I.sfence, an) |
| 202 |
| X.RDTSC => emit(I.rdtsc, an) |
| 203 |
| X.RDTSCP => emit(I.rdtscp, an) |
| 204 |
(* end case *) |
| 205 |
end |
| 206 |
end |