Debian Codegraph @

lib/mlrisc-lib/MLRISC/x86/backpatch/x86Jumps.sml

raw
1 (* X86Jumps.sml --- information to resolve jumps for runtime code generation.
2 *
3 * COPYRIGHT (c) 1997 Bell Laboratories.
4 *)
5
6 functor X86Jumps
7 (structure Instr : X86INSTR
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 : X86SHUFFLE (* 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.fbinOp = Instr.fbinOp
55 and type I.fenvOp = Instr.fenvOp
56 and type I.fibinOp = Instr.fibinOp
57 and type I.fsize = Instr.fsize
58 and type I.funOp = Instr.funOp
59 and type I.instr = Instr.instr
60 and type I.instruction = Instr.instruction
61 and type I.isize = Instr.isize
62 and type I.move = Instr.move
63 and type I.multDivOp = Instr.multDivOp
64 and type I.operand = Instr.operand
65 and type I.shiftOp = Instr.shiftOp
66 and type I.unaryOp = Instr.unaryOp
67 structure MCEmitter : MC_EMIT (* where I = Instr *)
68 where type I.addressing_mode = Instr.addressing_mode
69 and type I.ea = Instr.ea
70 and type I.instr = Instr.instr
71 and type I.instruction = Instr.instruction
72 and type I.operand = Instr.operand
73 ) : SDI_JUMPS =
74 struct
75 structure I = Instr
76 structure C = I.C
77 structure Const = I.Constant
78
79 fun error msg = MLRiscErrorMsg.error("X86Jumps",msg)
80
81 val esp = 4
82 val ebp = 5
83 val branchDelayedArch = false
84
85 fun imm8 i = ~128 <= i andalso i < 128
86
87 fun isSdi(I.ANNOTATION{i, ...}) = isSdi i
88 | isSdi(I.LIVE _) = true
89 | isSdi(I.KILL _) = true
90 | isSdi(I.COPY _) = false
91 | isSdi(I.INSTR instr) = let
92 fun operand(I.ImmedLabel _) = true
93 | operand(I.LabelEA _) = true
94 | operand(I.Displace{disp, ...}) = operand disp
95 | operand(I.Indexed{disp, ...}) = operand disp
96 | operand _ = false
97 fun cmptest{lsrc, rsrc} = operand lsrc orelse operand rsrc
98 in
99 case instr
100 of I.JMP(opnd, _) => operand opnd
101 | I.JCC{opnd, ...} => operand opnd
102 | I.BINARY{src, dst, ...} => operand src orelse operand dst
103 | I.MOVE{src, dst, ...} => operand src orelse operand dst
104 | I.LEA{addr, ...} => operand addr
105 | ( I.CMPL arg | I.CMPW arg | I.CMPB arg
106 | 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.UNARY{opnd, ...} => operand opnd
110 | I.SET{opnd, ...} => operand opnd
111 | I.CMOV{src, dst, ...} => operand src
112 | (I.PUSHL opnd | I.PUSHW opnd | I.PUSHB opnd) => operand opnd
113 | I.POP opnd => operand opnd
114 | I.FSTPT opnd => operand opnd
115 | I.FSTPL opnd => operand opnd
116 | I.FSTPS opnd => operand opnd
117 | I.FSTL opnd => operand opnd
118 | I.FSTS opnd => operand opnd
119 | I.FLDT opnd => operand opnd
120 | I.FLDL opnd => operand opnd
121 | I.FLDS opnd => operand opnd
122 | I.FBINARY{src, dst, ...} => operand src orelse operand dst
123 | I.FIBINARY{src, ...} => operand src
124 | I.FILD opnd => operand opnd
125 | I.FILDL opnd => operand opnd
126 | I.FILDLL opnd => operand opnd
127 | _ => false
128 end
129
130 fun minSize(I.ANNOTATION{i, ...}) = minSize i
131 | minSize(I.LIVE _) = 0
132 | minSize(I.KILL _) = 0
133 | minSize(I.INSTR i) =
134 (case i
135 of I.JMP _ => 2
136 | I.JCC _ => 2
137 | I.LEA _ => 2
138 | _ => 1)
139 | minSize _ = error"minSize"
140
141
142 fun maxSize _ = 12
143
144 (* value of span-dependent operand *)
145 fun operand(I.ImmedLabel le) = Eval.valueOf le
146 | operand(I.LabelEA le) = Eval.valueOf le
147 | operand _ = error "operand"
148
149 val encode = MCEmitter.emitInstr
150
151 fun sdiSize(I.ANNOTATION{i, ...}, labmap, loc) = sdiSize(i, labmap, loc)
152 | sdiSize(I.LIVE _, _, _) = 0
153 | sdiSize(I.KILL _, _, _) = 0
154
155 | sdiSize(I.INSTR instr, labmap, loc) = let
156 fun branch(opnd, short, long) = let
157 val offset = operand opnd - loc
158 in if imm8(offset - 2) then short else long
159 end
160 in
161 case instr
162 of I.JMP(opnd, _) => branch(opnd, 2, 5)
163 | I.JCC{opnd, ...} => branch(opnd, 2, 6)
164 | _ => Word8Vector.length(encode(I.INSTR instr))
165 end (*sdiSize*)
166 | sdiSize _ = error "sdiSize"
167
168 fun expand(I.ANNOTATION{i,...}, size, loc) = expand(i, size, loc)
169 | expand(I.LIVE _, _, _) = []
170 | expand(I.KILL _, _, _) = []
171 | expand(I.INSTR instr, size, loc) =
172 (case instr
173 of I.JMP(opnd, labs) => [I.jmp(I.Relative(operand opnd-loc), labs)]
174 | I.JCC{cond, opnd} =>
175 [I.jcc{cond=cond, opnd=I.Relative(operand opnd-loc)}]
176 | opnd => [I.INSTR opnd])
177 | expand _ = error "expand"
178 end
179