Debian Codegraph @

lib/mlrisc-lib/MLRISC/sparc/backpatch/sparcJumps.sml

raw
1 (* sparcJumps.sml --- information to resolve jumps.
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 *)
6 functor SparcJumps
7 (structure Instr:SPARCINSTR
8 structure Shuffle:SPARCSHUFFLE (* 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.arith = Instr.arith
30 and type I.branch = Instr.branch
31 and type I.cc = Instr.cc
32 and type I.ea = Instr.ea
33 and type I.farith1 = Instr.farith1
34 and type I.farith2 = Instr.farith2
35 and type I.fbranch = Instr.fbranch
36 and type I.fcmp = Instr.fcmp
37 and type I.fload = Instr.fload
38 and type I.fsize = Instr.fsize
39 and type I.fstore = Instr.fstore
40 and type I.instr = Instr.instr
41 and type I.instruction = Instr.instruction
42 and type I.load = Instr.load
43 and type I.operand = Instr.operand
44 and type I.prediction = Instr.prediction
45 and type I.rcond = Instr.rcond
46 and type I.shift = Instr.shift
47 and type I.store = Instr.store
48 structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
49 where type T.Basis.cond = Instr.T.Basis.cond
50 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
51 and type T.Basis.ext = Instr.T.Basis.ext
52 and type T.Basis.fcond = Instr.T.Basis.fcond
53 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
54 and type T.Constant.const = Instr.T.Constant.const
55 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
56 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
57 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
58 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
59 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
60 and type T.Region.region = Instr.T.Region.region
61 and type T.ccexp = Instr.T.ccexp
62 and type T.fexp = Instr.T.fexp
63 (* and type T.labexp = Instr.T.labexp *)
64 and type T.mlrisc = Instr.T.mlrisc
65 and type T.oper = Instr.T.oper
66 and type T.rep = Instr.T.rep
67 and type T.rexp = Instr.T.rexp
68 and type T.stm = Instr.T.stm
69 ) : SDI_JUMPS =
70 struct
71 structure I = Instr
72 structure C = Instr.C
73 structure Const = I.Constant
74 structure CB = CellsBasis
75
76 fun error msg = MLRiscErrorMsg.error("SparcJumps",msg)
77
78 val branchDelayedArch = true
79
80
81 fun isSdi(I.ANNOTATION{i,...}) = isSdi i
82 | isSdi(I.LIVE _) = true
83 | isSdi(I.KILL _) = true
84 | isSdi(I.COPY _) = true
85 | isSdi(I.INSTR instr) = let
86 fun oper(I.IMMED n) = false
87 | oper(I.REG _) = false
88 | oper(I.HI _) = false
89 | oper(I.LO _) = false
90 | oper(I.LAB _) = true
91 in case instr of
92 I.ARITH{i,...} => oper i
93 | I.SHIFT{i,...} => oper i
94 | I.LOAD{i,...} => oper i
95 | I.STORE{i,...} => oper i
96 | I.FLOAD{i,...} => oper i
97 | I.FSTORE{i,...} => oper i
98 | I.JMPL{i,...} => oper i
99 | I.JMP{i,...} => oper i
100 | I.MOVicc{i,...} => oper i
101 | I.MOVfcc{i,...} => oper i
102 | I.MOVR{i,...} => oper i
103 | I.CALL _ => true
104 | I.Bicc _ => true
105 | I.FBfcc _ => true
106 | I.BR _ => true
107 | I.BP _ => true
108 | I.Ticc{i,...} => oper i
109 | I.WRY{i,...} => oper i
110 | I.SAVE{i,...} => oper i
111 | I.RESTORE{i,...} => oper i
112 (* The following is only true of Version 8 *)
113 | I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd), ...} => true
114 | _ => false
115 end
116
117 fun minSize(I.ANNOTATION{i,...}) = minSize i
118 | minSize(I.LIVE _) = 0
119 | minSize(I.KILL _) = 0
120 | minSize (I.COPY _) = 0 (* ? *)
121 | minSize(I.INSTR instr) =
122 (case instr
123 of (I.Bicc{nop=true,...}) => 8
124 | (I.FBfcc{nop=true,...}) => 8
125 | (I.JMP{nop=true,...}) => 8
126 | (I.JMPL{nop=true,...}) => 8
127 | (I.CALL{nop=true,...}) => 8
128 | (I.BR{nop=true,...}) => 8
129 | (I.BP{nop=true,...}) => 8
130 | (I.RET{nop=true,...}) => 8
131 | (I.FCMP{nop=true,...}) => 8
132 | (I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...}) => 8
133 | _ => 4
134 (*esac*))
135
136 fun maxSize (I.INSTR(I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...})) = 8
137 | maxSize (I.ANNOTATION{i,...}) = maxSize i
138 | maxSize _ = 4
139
140 fun immed13 n = ~4096 <= n andalso n < 4096
141 fun immed22 n = ~0x200000 <= n andalso n < 0x1fffff
142 fun immed16 n = ~0x8000 <= n andalso n < 0x8000
143 fun immed19 n = ~0x40000 <= n andalso n < 0x40000
144 fun immed30 n = ~0x4000000 <= n andalso n < 0x3ffffff
145
146 fun instrLength([],n) = n
147 | instrLength(I.INSTR(I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...})::is,n) =
148 instrLength(is,n+8)
149 | instrLength(_::is,n) = instrLength(is,n+4)
150
151 fun sdiSize(I.ANNOTATION{i, ...}, labmap, loc) = sdiSize(i, labmap, loc)
152 | sdiSize(I.LIVE _, _, _) = 0
153 | sdiSize(I.KILL _, _, _) = 0
154 | sdiSize(I.COPY{k=CB.GP, src, dst, tmp, ...}, _, _) =
155 4 * length(Shuffle.shuffle{tmp=tmp, dst=dst, src=src})
156 | sdiSize(I.COPY{k=CB.FP, src, dst, tmp, ...}, _, _) = let
157 val instrs = Shuffle.shufflefp{src=src, dst=dst, tmp=tmp}
158 in instrLength(instrs, 0)
159 end
160 | sdiSize(instr as I.INSTR i, labMap, loc) = let
161 fun oper(I.IMMED n,_) = 4
162 | oper(I.REG _,_) = 4
163 | oper(I.HI _,_) = 4
164 | oper(I.LO _,_) = 4
165 | oper(I.LAB lexp,hi) = if immed13(MLTreeEval.valueOf lexp) then 4 else hi
166 fun displacement lab = ((labMap lab) - loc) div 4
167 fun branch22 lab = if immed22(displacement lab) then 4 else 16
168 fun branch19 lab = if immed19(displacement lab) then 4 else 16
169 fun branch16 lab = if immed16(displacement lab) then 4 else 16
170 fun call lab = if immed30(displacement lab) then 4 else 20
171 fun delaySlot false = 0
172 | delaySlot true = 4
173 in
174 case i
175 of I.ARITH{a=I.OR,r,i,...} =>
176 if CellsBasis.cellId r = 0 then oper(i,8) else oper(i,12)
177 | I.ARITH{i,...} => oper(i,12)
178 | I.SHIFT{i,...} => oper(i,12)
179 | I.LOAD{i,...} => oper(i,12)
180 | I.STORE{i,...} => oper(i,12)
181 | I.FLOAD{i,...} => oper(i,12)
182 | I.FSTORE{i,...} => oper(i,12)
183 | I.Ticc{i,...} => oper(i,12)
184 | I.SAVE{i,...} => oper(i,12)
185 | I.RESTORE{i,...} => oper(i,12)
186 | I.MOVicc{i,...} => oper(i,12)
187 | I.MOVfcc{i,...} => oper(i,12)
188 | I.MOVR{i,...} => oper(i,12)
189 | I.JMPL{i,nop,...} => oper(i,12) + delaySlot nop
190 | I.JMP{i,nop,...} => oper(i,12) + delaySlot nop
191 | I.Bicc{label,nop,...} => branch22 label + delaySlot nop
192 | I.FBfcc{label,nop,...} => branch22 label + delaySlot nop
193 | I.BR{label,nop,...} => branch16 label + delaySlot nop
194 | I.BP{label,nop,...} => branch19 label + delaySlot nop
195 | I.CALL{label,...} => call label
196 | I.WRY{i,...} => oper(i,12)
197 | I.FPop1{a=(I.FMOVd | I.FNEGd | I.FABSd),...} => 8
198 | _ => error "sdiSize"
199 end
200 | sdiSize _ = error "sdiSize"
201
202 fun split22_10 n =
203 let val w = Word32.fromInt n
204 in {hi=Word32.toInt(Word32.>>(w,0w10)),
205 lo=Word32.toInt(Word32.andb(w,0wx3ff))
206 }
207 end
208
209 fun split(I.LAB lexp) = split22_10(MLTreeEval.valueOf lexp)
210 | split _ = error "split"
211
212 (* Expand the immediate constant into two instructions *)
213 fun expandImm(immed,instr) =
214 let val {lo,hi} = split immed
215 in
216 [I.sethi{i=hi,d=C.asmTmpR},
217 I.arith{a=I.OR,r=C.asmTmpR,i=I.IMMED lo,d=C.asmTmpR},
218 I.INSTR instr
219 ]
220 end
221
222 (* Expand a span dependent instruction *)
223 fun expand(I.ANNOTATION{i, ...}, size, pos) = expand(i, size, pos)
224 | expand(I.LIVE _, _, _) = []
225 | expand(I.KILL _, _, _) = []
226 | expand(I.COPY{k=CB.GP, src, tmp, dst, ...}, _, _) =
227 Shuffle.shuffle{src=src, dst=dst, tmp=tmp}
228 | expand(I.COPY{k=CB.FP, src, tmp, dst, ...}, _, _) =
229 Shuffle.shufflefp{src=src, dst=dst, tmp=tmp}
230 | expand(instr as (I.INSTR i), size, pos) =
231 (case (i,size)
232 of (_,4) => [instr]
233 | (I.ARITH{a=I.OR,r,i,d},8) =>
234 if CellsBasis.cellId r = 0 then
235 let val {lo,hi} = split i
236 in [I.sethi{i=hi,d=C.asmTmpR},
237 I.arith{a=I.OR,r=C.asmTmpR,i=I.IMMED lo,d=d}
238 ]
239 end
240 else error "ARITH"
241 | (I.ARITH{a,r,i,d},12) =>
242 expandImm(i,I.ARITH{a=a,r=r,i=I.REG C.asmTmpR,d=d})
243 | (I.SHIFT{s,r,i,d},12) =>
244 expandImm(i,I.SHIFT{s=s,r=r,i=I.REG C.asmTmpR,d=d})
245 | (I.SAVE{r,i,d},12) =>
246 expandImm(i,I.SAVE{r=r,i=I.REG C.asmTmpR,d=d})
247 | (I.RESTORE{r,i,d},12) =>
248 expandImm(i,I.RESTORE{r=r,i=I.REG C.asmTmpR,d=d})
249 | (I.LOAD{l,r,i,d,mem},12) =>
250 expandImm(i,I.LOAD{l=l,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
251 | (I.STORE{s,r,i,d,mem},12) =>
252 expandImm(i,I.STORE{s=s,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
253 | (I.FLOAD{l,r,i,d,mem},12) =>
254 expandImm(i,I.FLOAD{l=l,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
255 | (I.FSTORE{s,r,i,d,mem},12) =>
256 expandImm(i,I.FSTORE{s=s,r=r,i=I.REG C.asmTmpR,d=d,mem=mem})
257 | (I.MOVicc{b,i,d},12) =>
258 expandImm(i,I.MOVicc{b=b,i=I.REG C.asmTmpR,d=d})
259 | (I.MOVfcc{b,i,d},12) =>
260 expandImm(i,I.MOVfcc{b=b,i=I.REG C.asmTmpR,d=d})
261 | (I.MOVR{rcond,r,i,d},12) =>
262 expandImm(i,I.MOVR{rcond=rcond,r=r,i=I.REG C.asmTmpR,d=d})
263 | (I.JMPL _,8) => [instr]
264 | (I.JMP _,8) => [instr]
265 | (I.Bicc _,8) => [instr]
266 | (I.FBfcc _,8) => [instr]
267 | (I.BR _,8) => [instr]
268 | (I.BP _,8) => [instr]
269 | (I.JMPL{r,i,d,defs,uses,cutsTo,nop,mem},(12 | 16)) =>
270 expandImm(i,I.JMPL{r=r,i=I.REG C.asmTmpR,d=d,defs=defs,uses=uses,
271 cutsTo=cutsTo,nop=nop,mem=mem})
272 | (I.JMP{r,i,labs,nop},(12 | 16)) =>
273 expandImm(i,I.JMP{r=r,i=I.REG C.asmTmpR,labs=labs,nop=nop})
274 | (I.Ticc{t,cc,r,i},12) =>
275 expandImm(i,I.Ticc{t=t,cc=cc,r=r,i=I.REG C.asmTmpR})
276 (*
277 * The sparc uses 22bits signed extended displacement offsets
278 * Let's hope it's enough
279 *)
280 | (I.Bicc{b,a,label,nop},_) => error "Bicc"
281 | (I.FBfcc{b,a,label,nop},_) => error "FBfcc"
282 | (I.FPop1{a, r, d}, _) => let
283 fun nextRegNum c = C.FPReg(CellsBasis.registerNum c + 1)
284 (* Note: if r=d then the move is not required.
285 * This needs to be factored into the size before it
286 * can be done here.
287 *)
288 fun doDouble(oper) =
289 [I.fpop1{a=oper, r=r, d=d},
290 I.fpop1{a=I.FMOVs, r=nextRegNum r, d=nextRegNum d}]
291 in
292 case a
293 of I.FMOVd => doDouble(I.FMOVs)
294 | I.FNEGd => doDouble(I.FNEGs)
295 | I.FABSd => doDouble(I.FABSs)
296 | _ => error "expand: FPop1"
297 end
298 | (I.WRY{r,i},12) => expandImm(i,I.WRY{r=r,i=I.REG C.asmTmpR})
299 | _ => error "expand"
300 (*esac*))
301 | expand _ = error "expand"
302
303 end
304