Debian Codegraph @

lib/mlrisc-lib/MLRISC/hppa/instructions/hppaProps.sml

raw
1 (* hppaProps.sml
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 *)
6
7 functor HppaProps
8 ( structure HppaInstr : HPPAINSTR
9 structure MLTreeEval : MLTREE_EVAL (* where T = HppaInstr.T *)
10 where type T.Basis.cond = HppaInstr.T.Basis.cond
11 and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
12 and type T.Basis.ext = HppaInstr.T.Basis.ext
13 and type T.Basis.fcond = HppaInstr.T.Basis.fcond
14 and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
15 and type T.Constant.const = HppaInstr.T.Constant.const
16 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
17 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
18 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
19 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
20 and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
21 and type T.Region.region = HppaInstr.T.Region.region
22 and type T.ccexp = HppaInstr.T.ccexp
23 and type T.fexp = HppaInstr.T.fexp
24 (* and type T.labexp = HppaInstr.T.labexp *)
25 and type T.mlrisc = HppaInstr.T.mlrisc
26 and type T.oper = HppaInstr.T.oper
27 and type T.rep = HppaInstr.T.rep
28 and type T.rexp = HppaInstr.T.rexp
29 and type T.stm = HppaInstr.T.stm
30 structure MLTreeHash : MLTREE_HASH (* where T = HppaInstr.T *)
31 where type T.Basis.cond = HppaInstr.T.Basis.cond
32 and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
33 and type T.Basis.ext = HppaInstr.T.Basis.ext
34 and type T.Basis.fcond = HppaInstr.T.Basis.fcond
35 and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
36 and type T.Constant.const = HppaInstr.T.Constant.const
37 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
38 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
39 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
40 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
41 and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
42 and type T.Region.region = HppaInstr.T.Region.region
43 and type T.ccexp = HppaInstr.T.ccexp
44 and type T.fexp = HppaInstr.T.fexp
45 (* and type T.labexp = HppaInstr.T.labexp *)
46 and type T.mlrisc = HppaInstr.T.mlrisc
47 and type T.oper = HppaInstr.T.oper
48 and type T.rep = HppaInstr.T.rep
49 and type T.rexp = HppaInstr.T.rexp
50 and type T.stm = HppaInstr.T.stm
51 ) : INSN_PROPERTIES =
52 struct
53 structure I = HppaInstr
54 structure C = HppaInstr.C
55 structure CB = CellsBasis
56
57 exception NegateConditional
58
59 fun error msg = MLRiscErrorMsg.error("HppaProps",msg)
60
61 datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL
62 | IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK
63 datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES
64
65 val zeroR = Option.valOf(C.zeroReg CB.GP)
66 val r31 = C.Reg CB.GP 31
67
68 (*========================================================================
69 * Instruction Kinds
70 *========================================================================*)
71 (* Note: BLE and BL used to implement calls are not view as branches *)
72 fun instrKind(I.ANNOTATION{i, ...}) = instrKind i
73 | instrKind(I.COPY _) = IK_COPY
74 | instrKind(I.INSTR instr) =
75 (case instr
76 of (I.BCOND _) => IK_JUMP
77 | (I.BCONDI _) => IK_JUMP
78 | (I.BB _) => IK_JUMP
79 | (I.B _) => IK_JUMP
80 | (I.BE _) => IK_JUMP
81 | (I.FBRANCH _)=> IK_JUMP
82 | (I.BV _) => IK_JUMP
83 | (I.BLR _) => IK_JUMP
84 | (I.BREAK _) => IK_JUMP
85 | (I.NOP) => IK_NOP
86 | (I.BL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
87 | (I.BL _) => IK_CALL
88 | (I.BLE{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
89 | (I.BLE _) => IK_CALL
90 | (I.PHI _) => IK_PHI
91 | (I.SOURCE _) => IK_SOURCE
92 | (I.SINK _) => IK_SINK
93 | _ => IK_INSTR)
94 | instrKind _ = error "instrKind"
95
96 fun moveInstr(I.COPY _) = true
97 | moveInstr(I.ANNOTATION{i,...}) = moveInstr i
98 | moveInstr _ = false
99
100 fun nop() = I.nop
101
102 (*========================================================================
103 * Parallel Move
104 *========================================================================*)
105 fun moveTmpR(I.COPY{tmp, ...}) =
106 (case tmp
107 of SOME(I.Direct r) => SOME r
108 | SOME(I.FDirect f) => SOME f
109 | _ => NONE
110 (*esac*))
111 | moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i
112 | moveTmpR _ = NONE
113
114 fun moveDstSrc(I.COPY{dst, src, ...}) = (dst, src)
115 | moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i
116 | moveDstSrc _ = error "moveDstSrc"
117
118 (*========================================================================
119 * Branches and Calls/Returns
120 *========================================================================*)
121 fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i
122 | branchTargets(I.INSTR instr) =
123 (case instr
124 of (I.BCOND{t, ...}) => [LABELLED t, FALLTHROUGH]
125 | (I.BCONDI{t, ...}) => [LABELLED t, FALLTHROUGH]
126 | (I.BB{t, ...}) => [LABELLED t, FALLTHROUGH]
127 | (I.B{lab, ...}) => [LABELLED lab]
128 | (I.FBRANCH{t,...}) => [LABELLED t, FALLTHROUGH]
129 | (I.BE{labs=[],...}) => [ESCAPES]
130 | (I.BE{labs,...}) => map LABELLED labs
131 | (I.BV{labs=[],...}) => [ESCAPES]
132 | (I.BV{labs,...}) => map LABELLED labs
133 | (I.BLR{labs,...}) => map LABELLED labs
134 | (I.BL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
135 | (I.BLE{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
136 | (I.BREAK _) => [ESCAPES]
137 | _ => error "branchTargets"
138 (*easc*))
139 | branchTargets _ = error "branchTargets"
140
141 fun jump label = I.b{lab=label,n=true}
142
143 val immedRange = {lo= ~8192, hi=8191}
144 fun loadImmed{immed,t} =
145 I.ldo{i=if #lo immedRange <= immed andalso immed <= #hi immedRange
146 then I.IMMED immed
147 else I.LabExp(I.T.LI(I.T.I.fromInt(32,immed)),I.F),b=zeroR,t=t}
148 fun loadOperand{opn,t} = I.ldo{i=opn,b=zeroR,t=t}
149
150 fun setJumpTarget(I.ANNOTATION{a,i}, l) = I.ANNOTATION{a=a, i=setJumpTarget(i,l)}
151 | setJumpTarget(I.INSTR(I.B{n,...}), L) = I.b{lab=L,n=n}
152 | setJumpTarget _ = error "setJumpTarget"
153
154 fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} =
155 I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}}
156 | setBranchTargets{i=I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop}), t=T, f=F} =
157 I.bcond{cmp=cmp,bc=bc,r1=r1,r2=r2,t=T,f=F,n=n,nop=nop}
158 | setBranchTargets{i=I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop=nop}),t=T, f=F} =
159 I.bcondi{cmpi=cmpi,bc=bc,i=i,r2=r2,t=T,f=F,n=n,nop=nop}
160 | setBranchTargets{i=I.INSTR(I.BB{bc,r,p,t,f,n,nop}), t=T, f=F} =
161 I.bb{bc=bc,r=r,p=p,t=T,f=F,n=n,nop=nop}
162 | setBranchTargets{i=I.INSTR(I.FBRANCH{cc,fmt,n,long,f1,f2,...}), t=T, f=F} =
163 I.fbranch{cc=cc,fmt=fmt,t=T,f=F,n=n,long=long,f1=f1,f2=f2}
164 | setBranchTargets _ = error "setBranchTargets"
165
166
167 (* negate the branch. Since the HPPA instruction representation tracks both
168 * the true and false target labels, we set the false label to be the
169 * old true label and set the true label to be the argument label.
170 *)
171 fun negateConditional (br, lab) = let
172 fun revFcond I.? = I.!?
173 | revFcond I.!<=> = I.<=>
174 | revFcond I.== = I.!=
175 | revFcond I.?= = I.!?=
176 | revFcond I.!<> = I.<>
177 | revFcond I.!?>= = I.?>=
178 | revFcond I.< = I.!<
179 | revFcond I.?< = I.!?<
180 | revFcond I.!>= = I.>=
181 | revFcond I.!?> = I.?>
182 | revFcond I.<= = I.!<=
183 | revFcond I.?<= = I.!?<=
184 | revFcond I.!> = I.>
185 | revFcond I.!?<= = I.?<=
186 | revFcond I.> = I.!>
187 | revFcond I.?> = I.!?>
188 | revFcond I.!<= = I.<=
189 | revFcond I.!?< = I.?<
190 | revFcond I.>= = I.!>=
191 | revFcond I.?>= = I.!?>=
192 | revFcond I.!< = I.<
193 | revFcond I.!?= = I.?=
194 | revFcond I.<> = I.!<>
195 | revFcond I.!= = I.==
196 | revFcond I.!? = I.?
197 | revFcond I.<=> = I.!<=>
198 | revFcond _ = error "revFcond"
199 fun negate (I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop})) = I.bcond{
200 bc=bc, r1=r1, r2=r2, t=lab, f=t, n=n, nop=nop,
201 cmp=case cmp of I.COMBT => I.COMBF | I.COMBF => I.COMBT
202 }
203 | negate (I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop})) = I.bcondi{
204 bc=bc, i=i, r2=r2, t=lab, f=t, n=n, nop=nop,
205 cmpi=case cmpi of I.COMIBT => I.COMIBF | I.COMIBF => I.COMIBT
206 }
207 | negate (I.INSTR(I.BB{bc,r,p,t,f,n,nop})) = I.bb{
208 bc=case bc of I.BSET => I.BCLR | I.BCLR => I.BSET,
209 r=r,p=p,t=lab,f=t,n=n,nop=nop
210 }
211 | negate (I.INSTR(I.FBRANCH{cc,fmt,f1,f2,t,f,n,long})) =
212 I.fbranch{cc=revFcond cc,fmt=fmt,f1=f1,f2=f2,t=lab,f=t,n=n,long=long}
213 | negate (I.ANNOTATION{i,a}) = I.ANNOTATION{i=negate i,a=a}
214 | negate _ = raise NegateConditional
215 in
216 negate br
217 end
218
219 (*========================================================================
220 * Equality and hashing for operands
221 *========================================================================*)
222 fun hashFieldSel I.F = 0w0
223 | hashFieldSel I.S = 0w1
224 | hashFieldSel I.D = 0w2
225 | hashFieldSel I.R = 0w3
226 | hashFieldSel I.T = 0w4
227 | hashFieldSel I.P = 0w5
228 fun hashOpn(I.IMMED i) = Word.fromInt i
229 | hashOpn(I.LabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f
230 | hashOpn(I.HILabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w10000
231 | hashOpn(I.LOLabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w20000
232 | hashOpn(I.REG r) = CB.hashCell r
233 fun eqOpn(I.IMMED i,I.IMMED j) = i = j
234 | eqOpn(I.REG x,I.REG y) = CB.sameColor(x,y)
235 | eqOpn(I.LabExp(a,b),I.LabExp(c,d)) =
236 b = d andalso MLTreeEval.==(a,c)
237 | eqOpn(I.HILabExp(a,b),I.HILabExp(c,d)) =
238 b = d andalso MLTreeEval.==(a,c)
239 | eqOpn(I.LOLabExp(a,b),I.LOLabExp(c,d)) =
240 b = d andalso MLTreeEval.==(a,c)
241 | eqOpn _ = false
242
243
244 (*========================================================================
245 * Definition and use (for register allocation mainly)
246 *========================================================================*)
247 fun defUseR instr = let
248 fun hppaDU instr = let
249 fun trap((I.ADDO | I.SUBO | I.SH1ADDO), d, u) = (d, u)
250 | trap(_, d, u) = (d, u)
251 fun trapi((I.ADDIO | I.SUBIO), d, u) = (d, u)
252 | trapi(_, d, u) = (d, u)
253 in
254 case instr
255 of I.STORE {b, r,...} => ([], [b,r])
256 | I.LOAD {l, r1, r2, t, ...} => ([t], [r1,r2])
257 | I.LOADI {li, r, t, ...} => ([t], [r])
258 | I.ARITH {a, r1, r2, t, ...} => trap(a, [t], [r1,r2])
259 | I.ARITHI {ai, r, t, ...} => trapi(ai, [t], [r])
260 | I.COMCLR_LDO{r1, r2, b, t1, t2, ...}=>
261 if CB.sameColor(t1,t2) then ([t1], [b, r1, r2])
262 else ([t1, t2], [b, r1, r2, t2])
263 | I.COMICLR_LDO{i1, r2, b, t1, t2, ...}=>
264 if CB.sameColor(t1,t2) then ([t1], [b, r2])
265 else ([t1, t2], [b, r2, t2])
266 | I.SHIFTV {r, t, ...} => ([t], [r])
267 | I.SHIFT {r, t, ...} => ([t], [r])
268 | I.BCOND {r1, r2, ...} => ([], [r1,r2])
269 | I.BCONDI {r2, ...} => ([], [r2])
270 | I.BB {r, ...} => ([], [r])
271 | I.BV {x, b, ...} => ([], [x,b])
272 | I.BE {b, ...} => ([], [b])
273 | I.BLR{x, t, ...} => ([t], [x])
274 | I.BL{defs, uses, ...} => (C.getReg defs, C.getReg uses)
275 | I.BLE{t, b, defs, uses, ...}=>
276 (r31 :: t :: C.getReg defs, b :: C.getReg uses)
277 | I.LDIL{i, t} => ([t], [])
278 | I.LDO{b, t, ...} => ([t], [b])
279 | I.MTCTL{r, t} => ([], [r])
280 | I.FSTORE {b, ...} => ([], [b])
281 | I.FSTOREX {b, x, ...} => ([], [b,x])
282 | I.FLOAD {b, ...} => ([], [b])
283 | I.FLOADX{b, x, ...} => ([], [b,x])
284 | _ => ([],[])
285 end
286 in
287 case instr
288 of I.ANNOTATION{i, ...} => defUseR i
289 | I.LIVE{regs, ...} => ([], C.getReg regs)
290 | I.KILL{regs, ...} => (C.getReg regs, [])
291 | I.INSTR(i) => hppaDU(i)
292 | I.COPY{k, dst, src, tmp, ...} => let
293 val (d,u) = case k of CB.GP => (dst, src) | _ => ([], [])
294 in
295 case tmp
296 of SOME(I.Direct r) => (r::d, u)
297 | SOME(I.Displace{base, ...}) => (* (d, base::u) *) (d, u)
298 | _ => (d,u)
299 end
300 end
301
302 fun defUseF instr = let
303 fun hppaDU instr =
304 case instr
305 of I.FSTORE {r, ...} => ([], [r])
306 | I.FSTOREX{r, ...} => ([], [r])
307 | I.FLOAD{t, ...} => ([t], [])
308 | I.FLOADX{t, ...} => ([t], [])
309 | I.FARITH {r1, r2, t, ...} => ([t], [r1,r2])
310 | I.FUNARY {f, t, ...} => ([t], [f])
311 | I.FCNV {f, t, ...} => ([t], [f])
312 | I.FBRANCH{f1, f2,...} => ([], [f1, f2])
313 | I.BL{defs, uses, ...} => (C.getFreg defs, C.getFreg uses)
314 | I.BLE{defs, uses, ...} => (C.getFreg defs, C.getFreg uses)
315 | _ => ([],[])
316 in
317 case instr
318 of I.ANNOTATION{i, ...} => defUseF i
319 | I.INSTR(i) => hppaDU(i)
320 | I.LIVE{regs, ...} => ([], C.getFreg regs)
321 | I.KILL{regs, ...} => (C.getFreg regs, [])
322 | I.COPY{k, dst, src, tmp, ...} => let
323 val (d, u) = case k of CB.FP => (dst, src) | _ => ([],[])
324 in
325 case tmp
326 of SOME(I.FDirect f) => (f::d, u)
327 | _ => (d, u)
328 end
329 end
330
331 fun defUse CB.GP = defUseR
332 | defUse CB.FP = defUseF
333 | defUse _ = error "defUse"
334
335 (*========================================================================
336 * Annotations
337 *========================================================================*)
338 fun getAnnotations(I.ANNOTATION{i,a}) =
339 let val (i,an) = getAnnotations i in (i,a::an) end
340 | getAnnotations i = (i,[])
341 fun annotate(i,a) = I.ANNOTATION{i=i,a=a}
342
343 (*========================================================================
344 * Replicate an instruction
345 *========================================================================*)
346 fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
347 | replicate(I.COPY{k, sz, tmp=SOME _, dst, src}) = let
348 val tmp = case k of CB.GP => C.newReg()
349 | CB.FP => C.newFreg()
350 | _ => error "replicate: neither GP nor FP"
351 in
352 I.COPY{k=k, sz=sz, tmp=SOME(I.Direct(tmp)), dst=dst, src=src}
353 end
354 | replicate i = i
355 end
356
357
358