| 1 |
(* AMD64Jumps.sml --- information to resolve jumps for runtime code generation. |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 1997 Bell Laboratories. |
| 4 |
*) |
| 5 |
|
| 6 |
functor AMD64Jumps |
| 7 |
(structure Instr : AMD64INSTR |
| 8 |
structure Eval : MLTREE_EVAL (* where T = Instr.T *) |
| 9 |
where type T.Basis.cond = Instr.T.Basis.cond |
| 10 |
and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode |
| 11 |
and type T.Basis.ext = Instr.T.Basis.ext |
| 12 |
and type T.Basis.fcond = Instr.T.Basis.fcond |
| 13 |
and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode |
| 14 |
and type T.Constant.const = Instr.T.Constant.const |
| 15 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx |
| 16 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx |
| 17 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx |
| 18 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx |
| 19 |
and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode |
| 20 |
and type T.Region.region = Instr.T.Region.region |
| 21 |
and type T.ccexp = Instr.T.ccexp |
| 22 |
and type T.fexp = Instr.T.fexp |
| 23 |
(* and type T.labexp = Instr.T.labexp *) |
| 24 |
and type T.mlrisc = Instr.T.mlrisc |
| 25 |
and type T.oper = Instr.T.oper |
| 26 |
and type T.rep = Instr.T.rep |
| 27 |
and type T.rexp = Instr.T.rexp |
| 28 |
and type T.stm = Instr.T.stm |
| 29 |
structure Shuffle : AMD64SHUFFLE (* where I = Instr *) |
| 30 |
where type I.Constant.const = Instr.Constant.const |
| 31 |
and type I.Region.region = Instr.Region.region |
| 32 |
and type I.T.Basis.cond = Instr.T.Basis.cond |
| 33 |
and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode |
| 34 |
and type I.T.Basis.ext = Instr.T.Basis.ext |
| 35 |
and type I.T.Basis.fcond = Instr.T.Basis.fcond |
| 36 |
and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode |
| 37 |
and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx |
| 38 |
and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx |
| 39 |
and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx |
| 40 |
and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx |
| 41 |
and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode |
| 42 |
and type I.T.ccexp = Instr.T.ccexp |
| 43 |
and type I.T.fexp = Instr.T.fexp |
| 44 |
(* and type I.T.labexp = Instr.T.labexp *) |
| 45 |
and type I.T.mlrisc = Instr.T.mlrisc |
| 46 |
and type I.T.oper = Instr.T.oper |
| 47 |
and type I.T.rep = Instr.T.rep |
| 48 |
and type I.T.rexp = Instr.T.rexp |
| 49 |
and type I.T.stm = Instr.T.stm |
| 50 |
(* and type I.addressing_mode = Instr.addressing_mode *) |
| 51 |
and type I.binaryOp = Instr.binaryOp |
| 52 |
and type I.bitOp = Instr.bitOp |
| 53 |
and type I.cond = Instr.cond |
| 54 |
and type I.fbin_op = Instr.fbin_op |
| 55 |
and type I.fcom_op = Instr.fcom_op |
| 56 |
and type I.fmove_op = Instr.fmove_op |
| 57 |
and type I.fsize = Instr.fsize |
| 58 |
and type I.instr = Instr.instr |
| 59 |
and type I.instruction = Instr.instruction |
| 60 |
and type I.isize = Instr.isize |
| 61 |
and type I.move = Instr.move |
| 62 |
and type I.multDivOp = Instr.multDivOp |
| 63 |
and type I.operand = Instr.operand |
| 64 |
and type I.shiftOp = Instr.shiftOp |
| 65 |
and type I.unaryOp = Instr.unaryOp |
| 66 |
structure MCEmitter : MC_EMIT (* where I = Instr *) |
| 67 |
where type I.addressing_mode = Instr.addressing_mode |
| 68 |
and type I.ea = Instr.ea |
| 69 |
and type I.instr = Instr.instr |
| 70 |
and type I.instruction = Instr.instruction |
| 71 |
and type I.operand = Instr.operand |
| 72 |
) : SDI_JUMPS = |
| 73 |
struct |
| 74 |
structure I = Instr |
| 75 |
structure C = I.C |
| 76 |
structure Const = I.Constant |
| 77 |
|
| 78 |
fun error msg = MLRiscErrorMsg.error("AMD64Jumps",msg) |
| 79 |
|
| 80 |
val esp = 4 |
| 81 |
val ebp = 5 |
| 82 |
val branchDelayedArch = false |
| 83 |
|
| 84 |
fun imm8 i = ~128 <= i andalso i < 128 |
| 85 |
|
| 86 |
fun isSdi(I.ANNOTATION{i, ...}) = isSdi i |
| 87 |
| isSdi(I.LIVE _) = true |
| 88 |
| isSdi(I.KILL _) = true |
| 89 |
| isSdi(I.COPY _) = false |
| 90 |
| isSdi(I.INSTR instr) = let |
| 91 |
fun operand(I.ImmedLabel _) = true |
| 92 |
| operand(I.LabelEA _) = true |
| 93 |
| operand(I.Displace{disp, ...}) = operand disp |
| 94 |
| operand(I.Indexed{disp, ...}) = operand disp |
| 95 |
| operand _ = false |
| 96 |
fun cmptest{lsrc, rsrc} = operand lsrc orelse operand rsrc |
| 97 |
in |
| 98 |
case instr |
| 99 |
of I.JMP(opnd, _) => operand opnd |
| 100 |
| I.JCC{opnd, ...} => operand opnd |
| 101 |
| I.BINARY{src, dst, ...} => operand src orelse operand dst |
| 102 |
| I.MOVE{src, dst, ...} => operand src orelse operand dst |
| 103 |
| I.LEAL{addr, ...} => operand addr |
| 104 |
| I.LEAQ{addr, ...} => operand addr |
| 105 |
| ( I.CMPQ arg | I.CMPL arg | I.CMPW arg | I.CMPB arg |
| 106 |
| I.TESTQ arg | I.TESTL arg | I.TESTW arg | I.TESTB arg) => cmptest arg |
| 107 |
| I.MULTDIV{src, ...} => operand src |
| 108 |
| I.MUL3{src1, ...} => operand src1 |
| 109 |
| I.MULQ3{src1, ...} => operand src1 |
| 110 |
| I.UNARY{opnd, ...} => operand opnd |
| 111 |
| I.SET{opnd, ...} => operand opnd |
| 112 |
| I.CMOV{src, dst, ...} => operand src |
| 113 |
| I.PUSH opnd => operand opnd |
| 114 |
| I.POP opnd => operand opnd |
| 115 |
| _ => false |
| 116 |
end |
| 117 |
|
| 118 |
fun minSize(I.ANNOTATION{i, ...}) = minSize i |
| 119 |
| minSize(I.LIVE _) = 0 |
| 120 |
| minSize(I.KILL _) = 0 |
| 121 |
| minSize(I.INSTR i) = |
| 122 |
(case i |
| 123 |
of I.JMP _ => 2 |
| 124 |
| I.JCC _ => 2 |
| 125 |
| I.LEAL _ => 2 |
| 126 |
| _ => 1) |
| 127 |
| minSize _ = error"minSize" |
| 128 |
|
| 129 |
|
| 130 |
fun maxSize _ = 12 |
| 131 |
|
| 132 |
(* value of span-dependent operand *) |
| 133 |
fun operand(I.ImmedLabel le) = Eval.valueOf le |
| 134 |
| operand(I.LabelEA le) = Eval.valueOf le |
| 135 |
| operand _ = error "operand" |
| 136 |
|
| 137 |
val encode = MCEmitter.emitInstr |
| 138 |
|
| 139 |
fun sdiSize(I.ANNOTATION{i, ...}, labmap, loc) = sdiSize(i, labmap, loc) |
| 140 |
| sdiSize(I.LIVE _, _, _) = 0 |
| 141 |
| sdiSize(I.KILL _, _, _) = 0 |
| 142 |
|
| 143 |
| sdiSize(I.INSTR instr, labmap, loc) = let |
| 144 |
fun branch(opnd, short, long) = let |
| 145 |
val offset = operand opnd - loc |
| 146 |
in if imm8(offset - 2) then short else long |
| 147 |
end |
| 148 |
in |
| 149 |
case instr |
| 150 |
of I.JMP(opnd, _) => branch(opnd, 2, 5) |
| 151 |
| I.JCC{opnd, ...} => branch(opnd, 2, 6) |
| 152 |
| _ => Word8Vector.length(encode(I.INSTR instr)) |
| 153 |
end (*sdiSize*) |
| 154 |
| sdiSize _ = error "sdiSize" |
| 155 |
|
| 156 |
fun expand(I.ANNOTATION{i,...}, size, loc) = expand(i, size, loc) |
| 157 |
| expand(I.LIVE _, _, _) = [] |
| 158 |
| expand(I.KILL _, _, _) = [] |
| 159 |
| expand(I.INSTR instr, size, loc) = |
| 160 |
(case instr |
| 161 |
of I.JMP(opnd, labs) => [I.jmp(I.Relative(operand opnd-loc), labs)] |
| 162 |
| I.JCC{cond, opnd} => |
| 163 |
[I.jcc{cond=cond, opnd=I.Relative(operand opnd-loc)}] |
| 164 |
| opnd => [I.INSTR opnd]) |
| 165 |
| expand _ = error "expand" |
| 166 |
end |
| 167 |
|