Debian Codegraph @

lib/mlrisc-lib/MLRISC/hppa/backpatch/hppaJumps.sml

raw
1 (* hppaJumps.sml --- information to resolve jumps.
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 *)
6 functor HppaJumps
7 ( structure Instr:HPPAINSTR
8 structure Shuffle:HPPASHUFFLE (* where I = Instr *)
9 where type I.Constant.const = Instr.Constant.const
10 and type I.Region.region = Instr.Region.region
11 and type I.T.Basis.cond = Instr.T.Basis.cond
12 and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
13 and type I.T.Basis.ext = Instr.T.Basis.ext
14 and type I.T.Basis.fcond = Instr.T.Basis.fcond
15 and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
16 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
17 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
18 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
19 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
20 and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
21 and type I.T.ccexp = Instr.T.ccexp
22 and type I.T.fexp = Instr.T.fexp
23 (* and type I.T.labexp = Instr.T.labexp *)
24 and type I.T.mlrisc = Instr.T.mlrisc
25 and type I.T.oper = Instr.T.oper
26 and type I.T.rep = Instr.T.rep
27 and type I.T.rexp = Instr.T.rexp
28 and type I.T.stm = Instr.T.stm
29 and type I.addressing_mode = Instr.addressing_mode
30 and type I.arith = Instr.arith
31 and type I.arithi = Instr.arithi
32 and type I.bcond = Instr.bcond
33 and type I.bitcond = Instr.bitcond
34 and type I.cmp = Instr.cmp
35 and type I.cmpi = Instr.cmpi
36 and type I.ea = Instr.ea
37 and type I.farith = Instr.farith
38 and type I.fcnv = Instr.fcnv
39 and type I.fcond = Instr.fcond
40 and type I.field_selector = Instr.field_selector
41 and type I.fload = Instr.fload
42 and type I.floadx = Instr.floadx
43 and type I.fmt = Instr.fmt
44 and type I.fstore = Instr.fstore
45 and type I.fstorex = Instr.fstorex
46 and type I.funary = Instr.funary
47 and type I.instr = Instr.instr
48 and type I.instruction = Instr.instruction
49 and type I.load = Instr.load
50 and type I.loadi = Instr.loadi
51 and type I.operand = Instr.operand
52 and type I.scond = Instr.scond
53 and type I.shift = Instr.shift
54 and type I.shiftv = Instr.shiftv
55 and type I.store = Instr.store
56 structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
57 where type T.Basis.cond = Instr.T.Basis.cond
58 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
59 and type T.Basis.ext = Instr.T.Basis.ext
60 and type T.Basis.fcond = Instr.T.Basis.fcond
61 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
62 and type T.Constant.const = Instr.T.Constant.const
63 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
64 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
65 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
66 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
67 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
68 and type T.Region.region = Instr.T.Region.region
69 and type T.ccexp = Instr.T.ccexp
70 and type T.fexp = Instr.T.fexp
71 (* and type T.labexp = Instr.T.labexp *)
72 and type T.mlrisc = Instr.T.mlrisc
73 and type T.oper = Instr.T.oper
74 and type T.rep = Instr.T.rep
75 and type T.rexp = Instr.T.rexp
76 and type T.stm = Instr.T.stm
77 ) : SDI_JUMPS =
78 struct
79 structure I = Instr
80 structure C = Instr.C
81 structure Const = I.Constant
82 structure CB = CellsBasis
83
84 fun error msg = MLRiscErrorMsg.error("HppaJumps",msg)
85
86 val branchDelayedArch = false
87
88 fun minSize(I.INSTR(I.FBRANCH _)) = 12 (* FCMP/FTEST/B *)
89 | minSize(I.INSTR(I.BLR{labs,...})) = 8 + 8 * length labs (* FCMP/FTEST/B *)
90 | minSize(I.ANNOTATION{i,...}) = minSize i
91 | minSize(I.LIVE _) = 0
92 | minSize(I.KILL _) = 0
93 | minSize(I.COPY _) = 0
94 | minSize(I.INSTR(I.COMCLR_LDO _)) = 8
95 | minSize(I.INSTR(I.COMICLR_LDO _)) = 8
96 | minSize _ = 4
97
98 fun maxSize (I.INSTR(I.BCOND _)) = 16 (* BCOND+LONGJUMP *)
99 | maxSize (I.INSTR(I.BCONDI _)) = 16 (* BCONDI+LONGJUMP *)
100 | maxSize (I.INSTR(I.BB _)) = 16 (* BB+LONGJUMP *)
101 | maxSize (I.INSTR(I.B _)) = 12 (* LONGJUMP *)
102 | maxSize (I.INSTR(I.FBRANCH _))= 20
103 | maxSize (I.ANNOTATION{i,...}) = maxSize i
104 | maxSize _ = 4
105
106 fun isSdi(I.ANNOTATION{i,...}) = isSdi i
107 | isSdi(I.LIVE _) = true
108 | isSdi(I.KILL _) = true
109 | isSdi(I.COPY _) = true
110 | isSdi(I.INSTR instr) = let
111
112 fun opnd (I.LabExp _) = true
113 | opnd _ = false
114 in
115 case instr
116 of I.BCOND _ => true
117 | I.BCONDI _ => true
118 | I.BB _ => true
119 | I.B _ => true
120 | I.FBRANCH _ => true
121 | I.BLR _ => true
122 | I.LDO{i, ...} => opnd i
123 | I.STORE{d, ...} => opnd d
124 | I.ARITHI{i, ...} => opnd i
125 | I.LOADI{i, ...} => opnd i
126 | I.COMICLR_LDO{i1, ...} => opnd i1
127 | _ => false
128 end
129
130 fun im11 n = ~1024 <= n andalso n < 1024
131 fun im12 n = ~2048 <= n andalso n < 2048
132 fun im14 n = ~8192 <= n andalso n < 8192
133 fun im17 n = ~65536 <= n andalso n < 65536
134
135 fun sdiSize(I.ANNOTATION{i, ...}, labMap, loc) = sdiSize(i, labMap, loc)
136 | sdiSize(I.LIVE _, _, _) = 0
137 | sdiSize(I.KILL _, _, _) = 0
138 | sdiSize(I.COPY{k=CB.GP, dst, src, tmp, ...}, _, _) =
139 4 * length(Shuffle.shuffle{tmp=tmp, dst=dst, src=src})
140 | sdiSize(I.COPY{k=CB.FP, dst, src, tmp, ...}, _, _) =
141 4 * length(Shuffle.shufflefp{tmp=tmp, dst=dst, src=src})
142 | sdiSize(I.INSTR(instr), labMap, loc) = let
143 fun branchOffset lab = ((labMap lab) - loc - 8) div 4
144 fun branch(lab,nop) = let
145 val offset = branchOffset lab
146 in
147 if im12 offset then
148 if nop then 8 else 4
149 else if im17 offset then 8 else 16
150 end
151 fun memDisp(c, short, long) = if im14(c) then short else long
152 in
153 case instr
154 of I.LDO{i=I.LabExp(lexp, _), ...} => memDisp(MLTreeEval.valueOf lexp, 4, 12)
155 | I.LOADI{i=I.LabExp(lexp, _), ...} => memDisp(MLTreeEval.valueOf lexp, 4, 12)
156 | I.STORE{d=I.LabExp(lexp, _), ...} => memDisp(MLTreeEval.valueOf lexp, 4, 12)
157 | I.COMICLR_LDO{i1=I.LabExp(lexp,_), ...} =>
158 if im11(MLTreeEval.valueOf lexp) then 8 else 16
159 | I.ARITHI{ai, i=I.LabExp(lexp,_), ...} => let
160 fun arithImmed() = if im11(MLTreeEval.valueOf lexp) then 4 else 12
161 in
162 case ai
163 of I.ADDI => arithImmed()
164 | I.ADDIO => arithImmed()
165 | I.SUBI => arithImmed()
166 | I.SUBIO => arithImmed()
167 | _ => error "sdiSize: ARITHI LabelExp"
168 (*esac*)
169 end
170 | I.BCOND{t, nop, ...} => branch(t,nop)
171 | I.BCONDI{t, nop, ...} => branch(t,nop)
172 | I.BB{t, nop, ...} => branch(t,nop)
173 | I.B{lab, ...} => if im17 (branchOffset lab) then 4 else 12
174 | I.FBRANCH{t, ...} => if im17 (branchOffset t) then 12 else 20
175 | I.BLR{labs,...} => let
176 val l = length labs * 8
177 fun badOffsets(t::ts,n) =
178 not(im17(branchOffset t + n)) orelse badOffsets(ts,n+2)
179 | badOffsets([],n) = false
180 in l + (if badOffsets(labs,2) then 20 else 8)
181 end
182 | _ => error "sdiSize"
183 end
184 | sdiSize _ = error "SdiSize"
185
186 (* Note: A better sequence would be to use ADDIL, however
187 * the expansion is done after register allocation and
188 * ADDIL defines %r1.
189 *)
190
191 (*
192 fun longJump{lab, n} = let
193 val baseDisp = LE.MINUS(LE.LABEL lab, LE.INT 8192)
194 val labOpnd = (baseDisp, I.T)
195 val baseptrR = 8
196 in
197 [I.LDIL{i=I.HILabExp labOpnd, t=C.asmTmpR},
198 I.LDO{i=I.LOLabExp labOpnd, b=C.asmTmpR, t=C.asmTmpR},
199 I.ARITH{a=I.ADD, r1=baseptrR, r2=C.asmTmpR, t=C.asmTmpR},
200 I.BV{x=0, labs=[lab], b=C.asmTmpR, n=n}]
201 end
202 *)
203
204 fun longJump{lab, n} =
205 (print "longJump used\n";
206 [I.longjump{lab=lab, tmpLab=Label.anon(), n=n, tmp=C.asmTmpR}]
207 )
208
209 fun split11 n = let
210 val w = Word.fromInt(n)
211 in (Word.toIntX(Word.~>>(w, 0w11)), Word.toIntX(Word.andb(w, 0wx7ff)))
212 end
213
214 fun split11X n = let
215 val w = Word.fromInt(n)
216 val hi' = Word.~>>(w, 0w11)
217 val lo' = Word.andb(w, 0wx7ff)
218 val (hi,lo) =
219 if Word.<=(lo', 0wx3ff) then (hi', lo') else (hi'+0w1, lo'-0wx800)
220 in (Word.toIntX hi, Word.toIntX lo)
221 end
222
223 fun loadIndexed I.LDW = I.LDWX
224 | loadIndexed I.LDH = I.LDHX
225 | loadIndexed I.LDB = I.LDBX
226
227 fun expand(I.ANNOTATION{i,...},size,pos) = expand(i,size,pos)
228 | expand(I.LIVE _, _, _) = []
229 | expand(I.KILL _, _, _) = []
230 | expand(I.COPY{k=CB.GP, dst, src, tmp, ...}, _, _) =
231 Shuffle.shuffle{tmp=tmp, dst=dst, src=src}
232 | expand(I.COPY{k=CB.FP, dst, src, tmp, ...}, _, _) =
233 Shuffle.shufflefp{tmp=tmp, dst=dst, src=src}
234 | expand(instr as I.INSTR(i), size, pos) =
235 (case i
236 of I.LDO{i=I.LabExp lexp, t, b} =>
237 (case size
238 of 4 => [instr]
239 | 12 => [I.ldil{i=I.HILabExp lexp, t=C.asmTmpR},
240 I.ldo{i=I.LOLabExp lexp, b=C.asmTmpR, t=C.asmTmpR},
241 I.arith{a=I.ADD, r1=C.asmTmpR, r2=b, t=t}]
242 | _ => error "LDO"
243 (*esac*))
244 | I.COMICLR_LDO{cc, i1=I.LabExp lexp, r2, t1, b, i2, t2} =>
245 (case size
246 of 8 => [instr]
247 | 16 =>
248 [I.ldil{i=I.HILabExp lexp, t=C.asmTmpR},
249 I.ldo{i=I.LOLabExp lexp, b=C.asmTmpR, t=C.asmTmpR},
250 I.comclr_ldo{cc=cc, r1=C.asmTmpR, r2=r2, t1=t1,
251 b=b, i=i2, t2=t2}
252 ]
253 | _ => error "COMICLR_LDO"
254 (*esac*))
255 | I.STORE{st, d as I.LabExp lexp, b, r, mem} =>
256 (case size
257 of 4 => [instr]
258 | 12 =>
259 [I.ldil{i=I.HILabExp lexp, t=C.asmTmpR},
260 I.arith{a=I.ADD, r1=C.asmTmpR, r2=b, t=C.asmTmpR},
261 I.store{st=st, b=C.asmTmpR, d=I.LOLabExp lexp, r=r, mem=mem}]
262 | _ => error "STORE"
263 (*esac*))
264 | I.STORE _ => error "expand:STORE"
265 | I.ARITHI{ai, r, i=I.LabExp lexp, t} =>
266 (case size
267 of 4 => [instr]
268 | 12 =>
269 (* Note: A better sequence would be to use ADDIL, however
270 * the expansion is done after register allocation and
271 * ADDIL defines %r1.
272 *)
273 [I.ldil{i=I.HILabExp lexp, t=C.asmTmpR},
274 I.ldo{i=I.LOLabExp lexp, b=C.asmTmpR, t=C.asmTmpR},
275 I.arith{
276 a = case ai of I.ADDI => I.ADD | I.ADDIO => I.ADDO
277 | I.SUBI => I.SUB | I.SUBIO => I.SUBO
278 | _ => error "expand: I.ARITHI LabExp",
279 t=t,
280 r1=C.asmTmpR,
281 r2=r}]
282 | _ => error "ARITHI"
283 (*esac*))
284 | I.LOADI{li, r, i=I.LabExp lexp, t, mem} =>
285 (case size
286 of 4 => [instr]
287 | 12 => [I.ldil{i=I.HILabExp lexp, t=C.asmTmpR},
288 I.arith{a=I.ADD, r1=C.asmTmpR, r2=r, t=C.asmTmpR},
289 I.loadi{li=li, r=C.asmTmpR, i=I.LOLabExp lexp, t=t, mem=mem}]
290 | _ => error "LOADI"
291 (*esac*))
292 | I.BCOND{cmp,bc, t, f, r1, r2, n, nop} => let
293 fun rev I.COMBT=I.bcond{cmp=I.COMBF,bc=bc,t=f,f=f,r1=r1,r2=r2,n=true,nop=false}
294 | rev I.COMBF=I.bcond{cmp=I.COMBT,bc=bc,t=f,f=f,r1=r1,r2=r2,n=true,nop=false}
295 in
296 case (size,nop)
297 of (4,false) => [instr]
298 | (8,true) => [instr]
299 | (8,_) => [rev cmp, I.b{lab=t, n=n}]
300 | (16,_) => rev cmp :: longJump{lab=t, n=n}
301 | _ => error "BCOND"
302 (*esac*)
303 end
304 | I.BCONDI{cmpi, bc, t, f, i, r2, n, nop} => let
305 fun rev I.COMIBT=I.bcondi{cmpi=I.COMIBF,bc=bc,i=i,r2=r2,t=f,f=f,n=true,nop=false}
306 | rev I.COMIBF=I.bcondi{cmpi=I.COMIBT,bc=bc,i=i,r2=r2,t=f,f=f,n=true,nop=false}
307 in
308 case (size,nop)
309 of (4,false) => [instr]
310 | (8,true) => [instr]
311 | (8,_) => [rev cmpi, I.b{lab=t, n=n}]
312 | (16,_) => rev cmpi :: longJump{lab=t, n=n}
313 | _ => error "BCONDI"
314 (*esac*)
315 end
316 | I.BB{bc, r, p, t, f, n, nop} => let
317 fun rev I.BSET = I.bb{bc=I.BCLR,r=r,p=p,t=f,f=f,n=true,nop=false}
318 | rev I.BCLR = I.bb{bc=I.BSET,r=r,p=p,t=f,f=f,n=true,nop=false}
319 in case (size,nop) of
320 (4,false) => [instr]
321 | (8,true) => [instr]
322 | (8,_) => [rev bc, I.b{lab=t,n=n}]
323 | (16,_) => rev bc :: longJump{lab=t, n=n}
324 | _ => error "BB"
325 end
326 |I.B{lab=lab, n=n} =>
327 (case size
328 of 4 => [instr]
329 | 12 => longJump{lab=lab, n=n}
330 | _ => error "B"
331 (*esac*))
332 | I.FBRANCH{t, f, n, ...} =>
333 (case size
334 of 12 => [instr]
335 | 20 =>
336 (* lets hope this sequence never gets generated sequence:
337 FTEST
338 allways trapping instruction
339 B (f)
340 longJmp
341 *)
342 error "FBRANCH(20)"
343 | _ => error "FBRANCH"
344 (*esac*))
345 | I.BLR{labs,n,t,x,...} =>
346 (if size = 8 + 8 * length labs then
347 I.blr{labs=[],n=n,t=t,x=x}::
348 I.nop::
349 foldr (fn (l,is) => I.b{lab=l,n=true}::I.nop::is) [] labs
350 else error "BLR"
351 )
352 | _ => error "expand")
353 | expand _ = error "expand"
354 end
355