Debian Codegraph @

lib/mlrisc-lib/MLRISC/ppc/backpatch/ppcJumps.sml

raw
1 functor PPCJumps
2 (structure Instr: PPCINSTR
3 structure Shuffle : PPCSHUFFLE (* where I = Instr *)
4 where type I.Constant.const = Instr.Constant.const
5 and type I.Region.region = Instr.Region.region
6 and type I.T.Basis.cond = Instr.T.Basis.cond
7 and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
8 and type I.T.Basis.ext = Instr.T.Basis.ext
9 and type I.T.Basis.fcond = Instr.T.Basis.fcond
10 and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
11 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
12 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
13 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
14 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
15 and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
16 and type I.T.ccexp = Instr.T.ccexp
17 and type I.T.fexp = Instr.T.fexp
18 (* and type I.T.labexp = Instr.T.labexp *)
19 and type I.T.mlrisc = Instr.T.mlrisc
20 and type I.T.oper = Instr.T.oper
21 and type I.T.rep = Instr.T.rep
22 and type I.T.rexp = Instr.T.rexp
23 and type I.T.stm = Instr.T.stm
24 and type I.arith = Instr.arith
25 and type I.arithi = Instr.arithi
26 and type I.bit = Instr.bit
27 and type I.bo = Instr.bo
28 and type I.ccarith = Instr.ccarith
29 and type I.cmp = Instr.cmp
30 and type I.ea = Instr.ea
31 and type I.farith = Instr.farith
32 and type I.farith3 = Instr.farith3
33 and type I.fcmp = Instr.fcmp
34 and type I.fload = Instr.fload
35 and type I.fstore = Instr.fstore
36 and type I.funary = Instr.funary
37 and type I.instr = Instr.instr
38 and type I.instruction = Instr.instruction
39 and type I.load = Instr.load
40 and type I.operand = Instr.operand
41 and type I.rotate = Instr.rotate
42 and type I.rotatei = Instr.rotatei
43 and type I.spr = Instr.spr
44 and type I.store = Instr.store
45 and type I.unary = Instr.unary
46 and type I.xerbit = Instr.xerbit
47 structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
48 where type T.Basis.cond = Instr.T.Basis.cond
49 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
50 and type T.Basis.ext = Instr.T.Basis.ext
51 and type T.Basis.fcond = Instr.T.Basis.fcond
52 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
53 and type T.Constant.const = Instr.T.Constant.const
54 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
55 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
56 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
57 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
58 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
59 and type T.Region.region = Instr.T.Region.region
60 and type T.ccexp = Instr.T.ccexp
61 and type T.fexp = Instr.T.fexp
62 (* and type T.labexp = Instr.T.labexp *)
63 and type T.mlrisc = Instr.T.mlrisc
64 and type T.oper = Instr.T.oper
65 and type T.rep = Instr.T.rep
66 and type T.rexp = Instr.T.rexp
67 and type T.stm = Instr.T.stm
68 ) : SDI_JUMPS =
69 struct
70 structure I = Instr
71 structure C = I.C
72 structure Const = I.Constant
73 structure CB = CellsBasis
74
75 fun error msg = MLRiscErrorMsg.error("PPCJumps",msg)
76
77 val warn_long_branch =
78 MLRiscControl.mkFlag ("ppc-warn-long-branch",
79 "whether to warn about long form of branch")
80
81 val branchDelayedArch = false
82
83 fun isSdi(I.ANNOTATION{i,...}) =isSdi i
84 | isSdi(I.LIVE _) = true
85 | isSdi(I.KILL _) = true
86 | isSdi(I.COPY _) = true
87 | isSdi(I.INSTR instr) = let
88 fun operand(I.LabelOp _) = true
89 | operand _ = false
90 in
91 case instr
92 of I.L{d, ...} => operand d
93 | I.LF{d, ...} => operand d
94 | I.ST{d, ...} => operand d
95 | I.STF{d, ...} => operand d
96 | I.ARITHI{im, ...} => operand im
97 | I.ROTATEI{sh, ...} => operand sh
98 | I.COMPARE{rb, ...} => operand rb
99 | I.TW{si, ...} => operand si
100 | I.TD{si, ...} => operand si
101 | I.BC{addr, ...} => operand addr
102 | _ => false
103 end
104
105
106 (* max Size is not used for the PPC span dependency analysis. *)
107 fun maxSize _ = error "maxSize"
108
109 fun minSize(I.LIVE _) = 0
110 | minSize(I.KILL _) = 0
111 | minSize(I.COPY _) = 0
112 | minSize(I.ANNOTATION{i,...}) = minSize i
113 | minSize _ = 4
114
115 fun sdiSize(I.ANNOTATION{i, ...}, labmap, loc) = sdiSize(i, labmap, loc)
116 | sdiSize(I.LIVE _, _, _) = 0
117 | sdiSize(I.KILL _, _, _) = 0
118 | sdiSize(I.COPY{k=CB.GP, src, dst, tmp, ...}, _, _) =
119 4 * length(Shuffle.shuffle{tmp=tmp, dst=dst, src=src})
120 | sdiSize(I.COPY{k=CB.FP, src, dst, tmp, ...}, _, _) =
121 4 * length(Shuffle.shufflefp{src=src, dst=dst, tmp=tmp})
122 | sdiSize(I.INSTR instr, labmap, loc) = let
123 fun signed16 n = ~32768 <= n andalso n < 32768
124 fun signed12 n = ~2048 <= n andalso n < 2048
125 fun signed14 n = ~8192 <= n andalso n < 8192
126 fun unsigned16 n = 0 <= n andalso n < 65536
127 fun unsigned5 n = 0 <=n andalso n < 32
128
129 fun operand(I.LabelOp le, inRange, lo, hi) =
130 if inRange(MLTreeEval.valueOf le) then lo else hi
131 | operand _ = error "sdiSize:operand"
132 in
133 case instr
134 of I.L{ld=(I.LBZ | I.LHZ | I.LHA | I.LWZ),d,...} =>
135 operand(d, signed16, 4, 8)
136 | I.L{d,...} => operand(d, signed12, 4, 8)
137 | I.LF{ld=(I.LFS | I.LFD), d, ...} => operand(d, signed16, 4, 8)
138 | I.LF{d, ...} => operand(d, signed12, 4, 8)
139 | I.ST{st=(I.STB | I.STH | I.STW), d, ...} => operand(d, signed16, 4, 8)
140 | I.ST{d, ...} => operand(d, signed12, 4, 8)
141 | I.STF{st=(I.STFS | I.STFD), d, ...} => operand(d, signed16, 4, 8)
142 | I.STF{d, ...} => operand(d, signed12, 4, 8)
143 | I.ARITHI{oper, im, ...} =>
144 (case oper
145 of I.ADDI => operand(im, signed16, 4, 8)
146 | (I.ADDIS | I.SUBFIC | I.MULLI) => operand(im, signed16, 4, 12)
147 | (I.ANDI_Rc | I.ANDIS_Rc | I.ORI | I.ORIS | I.XORI | I.XORIS) =>
148 operand(im, unsigned16, 4, 12)
149 | (I.SRAWI | I.SRADI) => operand(im, unsigned5, 4, 12)
150 (*esac*))
151 | I.ROTATEI{sh, ...} => error "sdiSize:ROTATE"
152 | I.COMPARE{cmp, rb, ...} =>
153 (case cmp
154 of I.CMP => operand(rb, signed16, 4, 12)
155 | I.CMPL => operand(rb, unsigned16, 4, 12)
156 (*esac*))
157 | I.BC{addr=I.LabelOp lexp, ...} =>
158 if signed14((MLTreeEval.valueOf lexp - loc) div 4) then 4 else 8
159 | _ => error "sdiSize"
160 end
161 | sdiSize _ = error "sdiSize"
162
163 fun valueOf(I.LabelOp lexp) = MLTreeEval.valueOf lexp
164 | valueOf _ = error "valueOf"
165
166 fun split opnd = let
167 val i = valueOf opnd
168 val w = Word.fromInt i
169 val hi = Word.~>>(w, 0w16)
170 val lo = Word.andb(w, 0w65535)
171 val (high,low) =
172 if lo < 0w32768 then (hi, lo) else (hi+0w1, lo-0w65536)
173 in (Word.toIntX high, Word.toIntX low)
174 end
175
176 fun cnv I.ADDI = I.ADD
177 | cnv I.SUBFIC = I.SUBF
178 | cnv I.MULLI = I.MULLW
179 | cnv I.ANDI_Rc = I.AND
180 | cnv I.ORI = I.OR
181 | cnv I.XORI = I.XOR
182 | cnv I.SRAWI = I.SRAW
183 | cnv I.SRADI = I.SRAD
184 | cnv _ = error "cnv"
185
186 fun expand(I.ANNOTATION{i, ...}, size, pos) = expand(i, size, pos)
187 | expand(I.LIVE _, _, _) = []
188 | expand(I.KILL _, _, _) = []
189 | expand(I.COPY{k=CB.GP, src, tmp, dst, ...}, _, _) =
190 Shuffle.shuffle{src=src, dst=dst, tmp=tmp}
191 | expand(I.COPY{k=CB.FP, src, tmp, dst, ...}, _, _) =
192 Shuffle.shufflefp{src=src, dst=dst, tmp=tmp}
193 | expand(instr as I.INSTR i, size, pos) =
194 (case i
195 of I.L{ld, rt, ra, d, mem} =>
196 (case size
197 of 4 => [I.l{ld=ld, rt=rt, ra=ra, d=I.ImmedOp(valueOf d), mem=mem}]
198 | 8 => let
199 val (hi,lo) = split d
200 in
201 [I.arithi{oper=I.ADDIS, rt=C.asmTmpR, ra=ra, im=I.ImmedOp hi},
202 I.l{ld=ld, rt=rt, ra=C.asmTmpR, d=I.ImmedOp lo, mem=mem}]
203 end
204 | _ => error "expand:L"
205 (*esac*))
206 | I.LF{ld, ft, ra, d, mem} =>
207 (case size
208 of 4 => [I.lf{ld=ld, ft=ft, ra=ra, d=I.ImmedOp(valueOf d), mem=mem}]
209 | 8 => let
210 val (hi,lo) = split d
211 in
212 [I.arithi{oper=I.ADDIS, rt=C.asmTmpR, ra=ra, im=I.ImmedOp hi},
213 I.lf{ld=ld, ft=ft, ra=C.asmTmpR, d=I.ImmedOp lo, mem=mem}]
214 end
215 | _ => error "expand:LF"
216 (*esac*))
217 | I.ST{st, rs, ra, d, mem} =>
218 (case size
219 of 4 => [I.st{st=st, rs=rs, ra=ra, d=I.ImmedOp(valueOf d), mem=mem}]
220 | 8 => let
221 val (hi,lo) = split d
222 in
223 [I.arithi{oper=I.ADDIS, rt=C.asmTmpR, ra=ra, im=I.ImmedOp hi},
224 I.st{st=st, rs=rs, ra=C.asmTmpR, d=I.ImmedOp lo, mem=mem}]
225 end
226 | _ => error "expand:ST"
227 (*esac*))
228 | I.STF{st, fs, ra, d, mem} =>
229 (case size
230 of 4 => [I.stf{st=st, fs=fs, ra=ra, d=I.ImmedOp(valueOf d), mem=mem}]
231 | 8 => let
232 val (hi,lo) = split d
233 in
234 [I.arithi{oper=I.ADDIS, rt=C.asmTmpR, ra=ra, im=I.ImmedOp hi},
235 I.stf{st=st, fs=fs, ra=C.asmTmpR, d=I.ImmedOp lo, mem=mem}]
236 end
237 | _ => error "expand:STF"
238 (*esac*))
239 | I.ARITHI{oper, rt, ra, im} =>
240 (case size
241 of 4 => [I.arithi{oper=oper, rt=rt, ra=ra, im=I.ImmedOp(valueOf im)}]
242 | 8 => let val (hi, lo) = split im (* must be ADDI *)
243 in [I.arithi{oper=I.ADDIS, rt=rt, ra=ra, im=I.ImmedOp hi},
244 I.arithi{oper=I.ADDI, rt=rt, ra=rt, im=I.ImmedOp lo}]
245 end
246 | 12 =>
247 let val (hi,lo) = split im
248 in [I.arithi{oper=I.ADDIS, rt=C.asmTmpR, ra=C.Reg CellsBasis.GP 0,
249 im=I.ImmedOp hi},
250 I.arithi{oper=I.ADDI,rt=C.asmTmpR,ra=C.asmTmpR,im=I.ImmedOp lo},
251 I.arith{oper=cnv oper, rt=rt, ra=ra, rb=C.asmTmpR, OE=false,
252 Rc=(oper = I.ANDI_Rc)}]
253 end
254 | _ => error "ARITHI"
255 (*esac*))
256 | I.BC{bo, bf, bit, fall, addr, LK} =>
257 (case size
258 of 4 => [instr]
259 | 8 => let
260 val newBO =
261 (case bo
262 of I.TRUE => I.FALSE
263 | I.FALSE => I.TRUE
264 | I.ALWAYS => error "expand:newBO:BC"
265 | I.COUNTER{eqZero, cond} => error "expand:newBO:COUNTER"
266 (*esac*))
267 in
268 if !warn_long_branch then
269 print("emiting long form of branch" ^ "\n")
270 else ();
271 [I.bc{bo=newBO, bf=bf, bit=bit, addr=fall, fall=fall, LK=false},
272 I.b{addr=addr, LK=LK}]
273 end
274 | _ => error "expand:BC"
275 (*esac*))
276 (* The other span dependent instructions are not generated *)
277 | I.COMPARE _ => error "expand:COMPARE"
278 | _ => error "expand"
279 (*esac*))
280 | expand _ = error "expand"
281 end