Debian Codegraph @

lib/mlrisc-lib/MLRISC/ppc/instructions/ppcProps.sml

raw
1 (* ppcProps.sml
2 *
3 * COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies
4 *)
5
6 functor PPCProps
7 ( structure PPCInstr : PPCINSTR
8 structure MLTreeEval : MLTREE_EVAL (* where T = PPCInstr.T *)
9 where type T.Basis.cond = PPCInstr.T.Basis.cond
10 and type T.Basis.div_rounding_mode = PPCInstr.T.Basis.div_rounding_mode
11 and type T.Basis.ext = PPCInstr.T.Basis.ext
12 and type T.Basis.fcond = PPCInstr.T.Basis.fcond
13 and type T.Basis.rounding_mode = PPCInstr.T.Basis.rounding_mode
14 and type T.Constant.const = PPCInstr.T.Constant.const
15 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) PPCInstr.T.Extension.ccx
16 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) PPCInstr.T.Extension.fx
17 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) PPCInstr.T.Extension.rx
18 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) PPCInstr.T.Extension.sx
19 and type T.I.div_rounding_mode = PPCInstr.T.I.div_rounding_mode
20 and type T.Region.region = PPCInstr.T.Region.region
21 and type T.ccexp = PPCInstr.T.ccexp
22 and type T.fexp = PPCInstr.T.fexp
23 (* and type T.labexp = PPCInstr.T.labexp *)
24 and type T.mlrisc = PPCInstr.T.mlrisc
25 and type T.oper = PPCInstr.T.oper
26 and type T.rep = PPCInstr.T.rep
27 and type T.rexp = PPCInstr.T.rexp
28 and type T.stm = PPCInstr.T.stm
29 structure MLTreeHash : MLTREE_HASH (* where T = PPCInstr.T *)
30 where type T.Basis.cond = PPCInstr.T.Basis.cond
31 and type T.Basis.div_rounding_mode = PPCInstr.T.Basis.div_rounding_mode
32 and type T.Basis.ext = PPCInstr.T.Basis.ext
33 and type T.Basis.fcond = PPCInstr.T.Basis.fcond
34 and type T.Basis.rounding_mode = PPCInstr.T.Basis.rounding_mode
35 and type T.Constant.const = PPCInstr.T.Constant.const
36 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) PPCInstr.T.Extension.ccx
37 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) PPCInstr.T.Extension.fx
38 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) PPCInstr.T.Extension.rx
39 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) PPCInstr.T.Extension.sx
40 and type T.I.div_rounding_mode = PPCInstr.T.I.div_rounding_mode
41 and type T.Region.region = PPCInstr.T.Region.region
42 and type T.ccexp = PPCInstr.T.ccexp
43 and type T.fexp = PPCInstr.T.fexp
44 (* and type T.labexp = PPCInstr.T.labexp *)
45 and type T.mlrisc = PPCInstr.T.mlrisc
46 and type T.oper = PPCInstr.T.oper
47 and type T.rep = PPCInstr.T.rep
48 and type T.rexp = PPCInstr.T.rexp
49 and type T.stm = PPCInstr.T.stm
50 ) : INSN_PROPERTIES =
51 struct
52 structure I = PPCInstr
53 structure C = I.C
54 structure T = I.T
55 structure CB = CellsBasis
56
57 exception NegateConditional
58
59 fun error msg = MLRiscErrorMsg.error("PPCProps",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 (* This stupid architecture doesn't really have a dedicated zero register *)
66 fun zeroR() = C.Reg CB.GP 0
67
68 fun instrKind(I.ANNOTATION{i, ...}) = instrKind i
69 | instrKind(I.COPY _) = IK_COPY
70 | instrKind(I.INSTR instr) = let
71 fun eqTest to = Word.andb(Word.fromInt to, 0w4) <> 0w0
72 fun trapAlways{to, ra, si} =
73 (case si
74 of I.RegOp rb =>
75 if CellsBasis.sameColor(ra,rb) andalso eqTest(to) then IK_JUMP
76 else IK_INSTR
77 | I.ImmedOp 0 =>
78 if CellsBasis.registerId ra = 0 andalso eqTest(to) then IK_JUMP
79 else IK_INSTR
80 | _ => error "trapAlways: neither RegOp nor ImmedOp(0)"
81 (*esac*))
82 in
83 case instr
84 of (I.BC _) => IK_JUMP
85 | (I.BCLR _) => IK_JUMP
86 | (I.B _) => IK_JUMP
87 | (I.TW t) => trapAlways(t)
88 | (I.TD t) => trapAlways(t)
89 | (I.ARITHI{oper=I.ORI, rt, ra, im=I.ImmedOp 0}) =>
90 if CB.registerId rt = 0 andalso CB.registerId ra = 0 then IK_NOP
91 else IK_INSTR
92 | (I.CALL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
93 | (I.CALL _) => IK_CALL
94 | (I.PHI _) => IK_PHI
95 | (I.SOURCE _) => IK_SOURCE
96 | (I.SINK _) => IK_SINK
97 | _ => IK_INSTR
98 (*esac*)
99 end
100 | instrKind _ = error "instrKind"
101
102 fun moveInstr(I.COPY _) = true
103 | moveInstr(I.ANNOTATION{i,...}) = moveInstr i
104 | moveInstr _ = false
105
106 fun nop () = I.arithi{oper=I.ORI, rt=zeroR(), ra=zeroR(), im=I.ImmedOp 0}
107
108 fun moveTmpR(I.COPY{tmp, ...}) =
109 (case tmp
110 of SOME(I.Direct r) => SOME r
111 | SOME(I.FDirect f) => SOME f
112 | _ => NONE
113 (*esac*))
114 | moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i
115 | moveTmpR _ = NONE
116
117 fun moveDstSrc(I.COPY{dst,src,...}) = (dst,src)
118 | moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i
119 | moveDstSrc _ = error "moveDstSrc"
120
121
122 fun branchTargets(I.INSTR(I.BC{bo=I.ALWAYS, addr, ...})) =
123 (case addr
124 of I.LabelOp(T.LABEL lab) => [LABELLED lab]
125 | _ => error "branchTargets:BC:ALWAYS"
126 (*esac*))
127 | branchTargets(I.INSTR(I.BC{addr, ...})) =
128 (case addr
129 of I.LabelOp(T.LABEL lab) => [LABELLED lab, FALLTHROUGH]
130 | _ => error "branchTargets:BC"
131 (*esac*))
132 | branchTargets(I.INSTR(I.BCLR{labels, bo=I.ALWAYS, ...})) =
133 (case labels of [] => [ESCAPES] | _ => map LABELLED labels)
134 | branchTargets(I.INSTR(I.BCLR{labels, ...})) =
135 (case labels of [] => [ESCAPES, FALLTHROUGH] | _ => map LABELLED labels)
136 | branchTargets(I.INSTR(I.B{addr=I.LabelOp(T.LABEL lab), LK})) = [LABELLED lab]
137 | branchTargets(I.INSTR(I.CALL{cutsTo, ...})) = FALLTHROUGH::map LABELLED cutsTo
138 | branchTargets(I.INSTR(I.TD _)) = [ESCAPES]
139 | branchTargets(I.INSTR(I.TW _)) = [ESCAPES]
140 | branchTargets(I.ANNOTATION{i,...}) = branchTargets i
141 | branchTargets _ = error "branchTargets"
142
143 fun labelOp l = I.LabelOp(T.LABEL l)
144
145 fun setJumpTarget(I.ANNOTATION{a,i}, l) = I.ANNOTATION{a=a, i=setJumpTarget(i,l)}
146 | setJumpTarget(I.INSTR(I.BC{bo as I.ALWAYS, bf, bit, addr, fall, LK}), lab) =
147 I.bc{bo=bo, bf=bf, bit=bit, fall=fall, LK=LK, addr=labelOp lab}
148 | setJumpTarget(I.INSTR(I.B{addr, LK}), lab) = I.b{addr=labelOp(lab), LK=LK}
149 | setJumpTarget _ = error "setJumpTarget"
150
151 fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} =
152 I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}}
153 | setBranchTargets{i=I.INSTR(I.BC{bo=I.ALWAYS, bf, bit, addr, fall, LK}), ...} =
154 error "setBranchTargets"
155 | setBranchTargets{i=I.INSTR(I.BC{bo, bf, bit, addr, fall, LK}), t, f} =
156 I.bc{bo=bo, bf=bf, bit=bit, LK=LK, addr=labelOp t, fall=labelOp f}
157 | setBranchTargets _ = error "setBranchTargets"
158
159 fun jump lab = I.b{addr=I.LabelOp(T.LABEL lab), LK=false}
160
161 fun negateConditional(I.ANNOTATION{a,i}, l) =
162 I.ANNOTATION{a=a, i=negateConditional(i, l)}
163 | negateConditional(I.INSTR(I.BC{bo, bf, bit, addr, fall, LK}), lab) = let
164 val bo' = (case bo
165 of I.TRUE => I.FALSE
166 | I.FALSE => I.TRUE
167 | I.ALWAYS => error "negateCondtional: ALWAYS"
168 | I.COUNTER{eqZero, cond=NONE} => I.COUNTER{eqZero=not eqZero, cond=NONE}
169 | I.COUNTER{eqZero, cond=SOME b} => error "negateConditional: COUNTER"
170 (*esac*))
171 in
172 I.bc{bo=bo', bf=bf, bit=bit, addr=labelOp lab, fall=fall, LK=LK}
173 end
174 | negateConditional _ = error "negateConditional"
175
176 val immedRange = {lo= ~32768, hi=32767}
177
178 fun loadImmed{immed,t} =
179 I.arithi
180 {oper=I.ADDI, rt=t, ra=zeroR(),
181 im=if #lo immedRange <= immed andalso immed <= #hi immedRange
182 then I.ImmedOp immed else I.LabelOp(I.T.LI(IntInf.fromInt immed))}
183 fun loadOperand{opn,t} =
184 I.arithi{oper=I.ADDI, rt=t, ra=zeroR(), im=opn}
185
186
187 fun hashOpn(I.RegOp r) = CB.hashCell r
188 | hashOpn(I.ImmedOp i) = Word.fromInt i
189 | hashOpn(I.LabelOp l) = MLTreeHash.hash l
190 fun eqOpn(I.RegOp a,I.RegOp b) = CB.sameColor(a,b)
191 | eqOpn(I.ImmedOp a,I.ImmedOp b) = a = b
192 | eqOpn(I.LabelOp a,I.LabelOp b) = MLTreeEval.==(a,b)
193 | eqOpn _ = false
194
195 fun defUseR instr = let
196 fun ppcDU instr = let
197 fun operand(I.RegOp r,use) = r::use
198 | operand(_,use) = use
199 in
200 case instr
201 of I.L{rt, ra, d, ...} => ([rt], operand(d,[ra]))
202 | I.LF{ra, d, ...} => ([], operand(d,[ra]))
203 | I.ST{rs, ra, d, ...} => ([], operand(d,[rs,ra]))
204 | I.STF{ra, d, ...} => ([], operand(d,[ra]))
205 | I.UNARY{rt, ra, ...} => ([rt], [ra])
206 | I.ARITH{rt, ra, rb, ...} => ([rt], [ra,rb])
207 | I.ARITHI{rt, ra, im, ...} => ([rt], operand(im,[ra]))
208 | I.ROTATE{ra, rs, sh, ...} => ([ra], [rs,sh])
209 | I.ROTATEI{ra, rs, sh, ...} => ([ra], operand(sh,[rs]))
210 | I.COMPARE{ra, rb, ...} => ([], operand(rb,[ra]))
211 | I.MTSPR{rs, ...} => ([], [rs])
212 | I.MFSPR{rt, ...} => ([rt], [])
213 | I.TW{to, ra, si} => ([], operand(si,[ra]))
214 | I.TD{to, ra, si} => ([], operand(si,[ra]))
215 | I.CALL{def, use, ...} => (C.getReg def, C.getReg use)
216 | I.LWARX{rt, ra, rb, ...} => ([rt], [ra, rb])
217 | I.STWCX{rs, ra, rb, ...} => ([], [rs, ra, rb])
218 | _ => ([], [])
219 end
220 in
221 case instr
222 of I.ANNOTATION{i, ...} => defUseR i
223 | I.LIVE{regs, ...} => ([], C.getReg regs)
224 | I.KILL{regs, ...} => (C.getReg regs, [])
225 | I.INSTR(i) => ppcDU(i)
226 | I.COPY{k, dst, src, tmp, ...} => let
227 val (d,u) = case k of CB.GP => (dst, src) | _ => ([], [])
228 in
229 case tmp
230 of SOME(I.Direct r) => (r::d, u)
231 | SOME(I.Displace{base, ...}) => (d, base::u)
232 | _ => (d,u)
233 end
234 end
235
236 fun defUseF instr = let
237 fun ppcDU instr =
238 (case instr
239 of I.LF{ft, ...} => ([ft],[])
240 | I.STF{fs, ...} => ([], [fs])
241 | I.FCOMPARE{fa, fb, ...} => ([], [fa, fb])
242 | I.FUNARY{ft, fb, ...} => ([ft], [fb])
243 | I.FARITH{ft, fa, fb, ...} => ([ft], [fa, fb])
244 | I.FARITH3{ft, fa, fb, fc, ...} => ([ft], [fa, fb, fc])
245 | I.CALL{def, use, ...} => (C.getFreg def,C.getFreg use)
246 | _ => ([], [])
247 (*esac*))
248 in
249 case instr
250 of I.ANNOTATION{i, ...} => defUseF i
251 | I.LIVE{regs, ...} => ([], C.getFreg regs)
252 | I.KILL{regs, ...} => (C.getFreg regs, [])
253 | I.INSTR(i) => ppcDU(i)
254 | I.COPY{k, dst, src, tmp, ...} => let
255 val (d, u) = case k of CB.FP => (dst, src) | _ => ([],[])
256 in
257 case tmp
258 of SOME(I.FDirect f) => (f::d, u)
259 | _ => (d, u)
260 end
261 end
262 fun defUseCC instr = error "defUseCC: not implemented"
263
264 fun defUse CB.GP = defUseR
265 | defUse CB.FP = defUseF
266 | defUse CB.CC = defUseCC
267 | defUse _ = error "defUse"
268
269 (*========================================================================
270 * Annotations
271 *========================================================================*)
272 fun getAnnotations(I.ANNOTATION{i,a}) =
273 let val (i,an) = getAnnotations i in (i,a::an) end
274 | getAnnotations i = (i,[])
275 fun annotate(i,a) = I.ANNOTATION{i=i,a=a}
276
277 (*========================================================================
278 * Replicate an instruction
279 *========================================================================*)
280 fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
281 | replicate(I.COPY{k, sz, tmp=SOME _, dst, src}) =
282 I.COPY{k=k, sz=sz, tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src}
283 | replicate i = i
284 end
285
286