Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/sparc/mltree/sparc.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/sparc/mltree/sparc.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 889 - (view) (download)

1 : monnier 245 (*
2 : monnier 411 * This is a new instruction selection module for Sparc,
3 :     * using the new instruction representation and the new MLTREE representation.
4 :     * Support for V9 has been added.
5 : monnier 245 *
6 : monnier 411 * The cc bit in arithmetic op are now embedded within the arithmetic
7 :     * opcode. This should save some space.
8 : monnier 245 *
9 : monnier 411 * -- Allen
10 : monnier 245 *)
11 :    
12 :     functor Sparc
13 :     (structure SparcInstr : SPARCINSTR
14 : monnier 411 structure PseudoInstrs : SPARC_PSEUDO_INSTR
15 : george 555 structure ExtensionComp : MLTREE_EXTENSION_COMP
16 : leunga 775 where I = SparcInstr
17 : monnier 475 sharing PseudoInstrs.I = SparcInstr
18 : monnier 411 (*
19 :     * The client should also specify these parameters.
20 :     * These are the estimated cost of these instructions.
21 :     * The code generator will use alternative sequences that are
22 :     * cheaper when their costs are lower.
23 :     *)
24 : george 545 val muluCost : int ref (* cost of unsigned multiplication in cycles *)
25 : monnier 411 val divuCost : int ref (* cost of unsigned division in cycles *)
26 :     val multCost : int ref (* cost of trapping/signed multiplication in cycles *)
27 :     val divtCost : int ref (* cost of trapping/signed division in cycles *)
28 :    
29 :     (*
30 :     * If you don't want to use register windows at all, set this to false.
31 :     *)
32 :     val registerwindow : bool ref (* should we use register windows? *)
33 :    
34 :     val V9 : bool (* should we use V9 instruction set? *)
35 :     val useBR : bool ref
36 :     (* should we use the BR instruction (when in V9)?
37 :     * I think it is a good idea to use it.
38 :     *)
39 : monnier 245 ) : MLTREECOMP =
40 :     struct
41 : leunga 775 structure I = SparcInstr
42 :     structure T = I.T
43 : monnier 429 structure S = T.Stream
44 : leunga 775 structure R = T.Region
45 : monnier 411 structure C = I.C
46 : george 889 structure CB = CellsBasis
47 : george 545 structure LE = I.LabelExp
48 : monnier 245 structure W = Word32
49 :     structure P = PseudoInstrs
50 : george 545 structure A = MLRiscAnnotations
51 : monnier 245
52 : leunga 744 type instrStream = (I.instruction,C.cellset) T.stream
53 :     type mltreeStream = (T.stm,T.mlrisc list) T.stream
54 : george 545
55 : george 761 val int_0 = T.I.int_0
56 :     fun toInt n = T.I.toInt(32, n)
57 :     fun LI i = T.LI(T.I.fromInt(32, i))
58 :     fun LT (n,m) = T.I.LT(32, n, m)
59 :     fun LE (n,m) = T.I.LE(32, n, m)
60 :    
61 : leunga 624 val intTy = if V9 then 64 else 32
62 : monnier 411 structure Gen = MLTreeGen(structure T = T
63 : leunga 624 val intTy = intTy
64 : monnier 411 val naturalWidths = if V9 then [32,64] else [32]
65 : monnier 429 datatype rep = SE | ZE | NEITHER
66 :     val rep = NEITHER
67 : monnier 411 )
68 : monnier 245
69 : monnier 411 functor Multiply32 = MLTreeMult
70 :     (structure I = I
71 :     structure T = T
72 : george 889 structure CB = CellsBasis
73 :     type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell}
74 :     type argi = {r:CB.cell,i:int,d:CB.cell}
75 : monnier 411
76 :     val intTy = 32
77 :     fun mov{r,d} = I.COPY{dst=[d],src=[r],tmp=NONE,impl=ref NONE}
78 :     fun add{r1,r2,d} = I.ARITH{a=I.ADD,r=r1,i=I.REG r2,d=d}
79 :     fun slli{r,i,d} = [I.SHIFT{s=I.SLL,r=r,i=I.IMMED i,d=d}]
80 :     fun srli{r,i,d} = [I.SHIFT{s=I.SRL,r=r,i=I.IMMED i,d=d}]
81 :     fun srai{r,i,d} = [I.SHIFT{s=I.SRA,r=r,i=I.IMMED i,d=d}]
82 :     )
83 : monnier 245
84 : monnier 411 functor Multiply64 = MLTreeMult
85 :     (structure I = I
86 :     structure T = T
87 : george 889 structure CB = CellsBasis
88 :     type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell}
89 :     type argi = {r:CB.cell,i:int,d:CB.cell}
90 : monnier 411
91 :     val intTy = 64
92 :     fun mov{r,d} = I.COPY{dst=[d],src=[r],tmp=NONE,impl=ref NONE}
93 :     fun add{r1,r2,d} = I.ARITH{a=I.ADD,r=r1,i=I.REG r2,d=d}
94 :     fun slli{r,i,d} = [I.SHIFT{s=I.SLLX,r=r,i=I.IMMED i,d=d}]
95 :     fun srli{r,i,d} = [I.SHIFT{s=I.SRLX,r=r,i=I.IMMED i,d=d}]
96 :     fun srai{r,i,d} = [I.SHIFT{s=I.SRAX,r=r,i=I.IMMED i,d=d}]
97 :     )
98 : monnier 245
99 : monnier 411 (* signed, trapping version of multiply and divide *)
100 :     structure Mult32 = Multiply32
101 :     (val trapping = true
102 :     val multCost = multCost
103 :     fun addv{r1,r2,d} =
104 :     I.ARITH{a=I.ADDCC,r=r1,i=I.REG r2,d=d}::PseudoInstrs.overflowtrap32
105 :     fun subv{r1,r2,d} =
106 :     I.ARITH{a=I.SUBCC,r=r1,i=I.REG r2,d=d}::PseudoInstrs.overflowtrap32
107 :     val sh1addv = NONE
108 :     val sh2addv = NONE
109 :     val sh3addv = NONE
110 :     )
111 : monnier 429 (val signed = true)
112 : monnier 245
113 : monnier 411 (* unsigned, non-trapping version of multiply and divide *)
114 : leunga 657 functor Mul32 = Multiply32
115 : monnier 411 (val trapping = false
116 :     val multCost = muluCost
117 :     fun addv{r1,r2,d} = [I.ARITH{a=I.ADD,r=r1,i=I.REG r2,d=d}]
118 :     fun subv{r1,r2,d} = [I.ARITH{a=I.SUB,r=r1,i=I.REG r2,d=d}]
119 :     val sh1addv = NONE
120 :     val sh2addv = NONE
121 :     val sh3addv = NONE
122 :     )
123 : leunga 657 structure Mulu32 = Mul32(val signed = false)
124 : monnier 245
125 : leunga 657 structure Muls32 = Mul32(val signed = true)
126 :    
127 : monnier 411 (* signed, trapping version of multiply and divide *)
128 :     structure Mult64 = Multiply64
129 :     (val trapping = true
130 :     val multCost = multCost
131 :     fun addv{r1,r2,d} =
132 :     I.ARITH{a=I.ADDCC,r=r1,i=I.REG r2,d=d}::PseudoInstrs.overflowtrap64
133 :     fun subv{r1,r2,d} =
134 :     I.ARITH{a=I.SUBCC,r=r1,i=I.REG r2,d=d}::PseudoInstrs.overflowtrap64
135 :     val sh1addv = NONE
136 :     val sh2addv = NONE
137 :     val sh3addv = NONE
138 :     )
139 : monnier 429 (val signed = true)
140 : monnier 245
141 : monnier 411 (* unsigned, non-trapping version of multiply and divide *)
142 : leunga 657 functor Mul64 = Multiply64
143 : monnier 411 (val trapping = false
144 :     val multCost = muluCost
145 :     fun addv{r1,r2,d} = [I.ARITH{a=I.ADD,r=r1,i=I.REG r2,d=d}]
146 :     fun subv{r1,r2,d} = [I.ARITH{a=I.SUB,r=r1,i=I.REG r2,d=d}]
147 :     val sh1addv = NONE
148 :     val sh2addv = NONE
149 :     val sh3addv = NONE
150 :     )
151 : leunga 657 structure Mulu64 = Mul64(val signed = false)
152 : monnier 245
153 : leunga 657 structure Muls64 = Mul64(val signed = true)
154 :    
155 : monnier 411 datatype commutative = COMMUTE | NOCOMMUTE
156 :     datatype cc = REG (* write to register *)
157 :     | CC (* set condition code *)
158 :     | CC_REG (* do both *)
159 : monnier 245
160 : monnier 411 fun error msg = MLRiscErrorMsg.error("Sparc",msg)
161 : monnier 245
162 : leunga 744
163 :    
164 : monnier 411 fun selectInstructions
165 : george 545 (instrStream as
166 : leunga 815 S.STREAM{emit,defineLabel,entryLabel,pseudoOp,annotation,getAnnotations,
167 : leunga 744 beginCluster,endCluster,exitBlock,comment,...}) =
168 : monnier 411 let
169 :     (* Flags *)
170 :     val useBR = !useBR
171 :     val registerwindow = !registerwindow
172 : monnier 245
173 : leunga 744 val trap32 = PseudoInstrs.overflowtrap32
174 :     val trap64 = PseudoInstrs.overflowtrap64
175 :     val zeroR = C.r0
176 :     val newReg = C.newReg
177 : monnier 411 val newFreg = C.newFreg
178 : george 761 val int_m4096 = T.I.fromInt(32, ~4096)
179 :     val int_4096 = T.I.fromInt(32, 4096)
180 :     fun immed13 n = LE(int_m4096, n) andalso LT(n, int_4096)
181 : monnier 411 fun immed13w w = let val x = W.~>>(w,0w12)
182 :     in x = 0w0 orelse (W.notb x) = 0w0 end
183 :     fun splitw w = {hi=W.toInt(W.>>(w,0w10)),lo=W.toInt(W.andb(w,0wx3ff))}
184 : george 761 fun split n = splitw(T.I.toWord32(32, n))
185 : monnier 245
186 : monnier 411
187 : leunga 744 val zeroOpn = I.REG zeroR (* zero value operand *)
188 : monnier 245
189 : monnier 411 fun cond T.LT = I.BL
190 :     | cond T.LTU = I.BCS
191 :     | cond T.LE = I.BLE
192 :     | cond T.LEU = I.BLEU
193 :     | cond T.EQ = I.BE
194 :     | cond T.NE = I.BNE
195 :     | cond T.GE = I.BGE
196 :     | cond T.GEU = I.BCC
197 :     | cond T.GT = I.BG
198 :     | cond T.GTU = I.BGU
199 : leunga 744 | cond _ = error "cond"
200 : monnier 245
201 : monnier 411 fun rcond T.LT = I.RLZ
202 :     | rcond T.LE = I.RLEZ
203 :     | rcond T.EQ = I.RZ
204 :     | rcond T.NE = I.RNZ
205 :     | rcond T.GE = I.RGEZ
206 :     | rcond T.GT = I.RGZ
207 :     | rcond _ = error "rcond"
208 : monnier 245
209 : monnier 411 fun signedCmp(T.LT | T.LE | T.EQ | T.NE | T.GE | T.GT) = true
210 :     | signedCmp _ = false
211 : monnier 245
212 : monnier 411 fun fcond T.== = I.FBE
213 :     | fcond T.?<> = I.FBNE
214 :     | fcond T.? = I.FBU
215 :     | fcond T.<=> = I.FBO
216 :     | fcond T.> = I.FBG
217 :     | fcond T.>= = I.FBGE
218 :     | fcond T.?> = I.FBUG
219 :     | fcond T.?>= = I.FBUGE
220 :     | fcond T.< = I.FBL
221 :     | fcond T.<= = I.FBLE
222 :     | fcond T.?< = I.FBUL
223 :     | fcond T.?<= = I.FBULE
224 :     | fcond T.<> = I.FBLG
225 :     | fcond T.?= = I.FBUE
226 : george 545 | fcond fc = error("fcond "^T.Basis.fcondToString fc)
227 : monnier 245
228 : monnier 411 fun mark'(i,[]) = i
229 :     | mark'(i,a::an) = mark'(I.ANNOTATION{i=i,a=a},an)
230 : monnier 245
231 : monnier 411 fun mark(i,an) = emit(mark'(i,an))
232 : monnier 245
233 : monnier 411 (* convert an operand into a register *)
234 :     fun reduceOpn(I.REG r) = r
235 : leunga 744 | reduceOpn(I.IMMED 0) = zeroR
236 : monnier 411 | reduceOpn i =
237 :     let val d = newReg()
238 : leunga 744 in emit(I.ARITH{a=I.OR,r=zeroR,i=i,d=d}); d end
239 : monnier 245
240 : monnier 411 (* emit parallel copies *)
241 :     fun copy(dst,src,an) =
242 :     mark(I.COPY{dst=dst,src=src,impl=ref NONE,
243 :     tmp=case dst of [_] => NONE
244 :     | _ => SOME(I.Direct(newReg()))},an)
245 :     fun fcopy(dst,src,an) =
246 :     mark(I.FCOPY{dst=dst,src=src,impl=ref NONE,
247 :     tmp=case dst of [_] => NONE
248 :     | _ => SOME(I.FDirect(newFreg()))},an)
249 : monnier 245
250 : monnier 411 (* move register s to register d *)
251 :     fun move(s,d,an) =
252 : george 889 if CB.sameColor(s,d) orelse CB.registerId d = 0 then ()
253 : monnier 411 else mark(I.COPY{dst=[d],src=[s],tmp=NONE,impl=ref NONE},an)
254 :    
255 :     (* move floating point register s to register d *)
256 :     fun fmoved(s,d,an) =
257 : george 889 if CB.sameColor(s,d) then ()
258 : monnier 411 else mark(I.FCOPY{dst=[d],src=[s],tmp=NONE,impl=ref NONE},an)
259 : monnier 475 fun fmoves(s,d,an) = fmoved(s,d,an) (* error "fmoves" for now!!! XXX *)
260 : monnier 411 fun fmoveq(s,d,an) = error "fmoveq"
261 :    
262 :     (* load immediate *)
263 :     and loadImmed(n,d,cc,an) =
264 :     let val or = if cc <> REG then I.ORCC else I.OR
265 : george 761 in if immed13 n then mark(I.ARITH{a=or,r=zeroR,i=I.IMMED(toInt n),d=d},an)
266 : monnier 411 else let val {hi,lo} = split n
267 :     in if lo = 0 then
268 :     (mark(I.SETHI{i=hi,d=d},an); genCmp0(cc,d))
269 :     else let val t = newReg()
270 :     in emit(I.SETHI{i=hi,d=t});
271 :     mark(I.ARITH{a=or,r=t,i=I.IMMED lo,d=d},an)
272 :     end
273 :     end
274 :     end
275 : monnier 245
276 : monnier 411 (* load label expression *)
277 :     and loadLabel(lab,d,cc,an) =
278 :     let val or = if cc <> REG then I.ORCC else I.OR
279 : leunga 744 in mark(I.ARITH{a=or,r=zeroR,i=I.LAB lab,d=d},an) end
280 : monnier 245
281 : monnier 411 (* emit an arithmetic op *)
282 :     and arith(a,acc,e1,e2,d,cc,comm,trap,an) =
283 :     let val (a,d) = case cc of
284 :     REG => (a,d)
285 : leunga 744 | CC => (acc,zeroR)
286 : monnier 411 | CC_REG => (acc,d)
287 :     in case (opn e1,opn e2,comm) of
288 :     (i,I.REG r,COMMUTE)=> mark(I.ARITH{a=a,r=r,i=i,d=d},an)
289 :     | (I.REG r,i,_) => mark(I.ARITH{a=a,r=r,i=i,d=d},an)
290 :     | (r,i,_) => mark(I.ARITH{a=a,r=reduceOpn r,i=i,d=d},an)
291 :     ;
292 :     case trap of [] => () | _ => app emit trap
293 :     end
294 : monnier 245
295 : monnier 411 (* emit a shift op *)
296 :     and shift(s,e1,e2,d,cc,an) =
297 :     (mark(I.SHIFT{s=s,r=expr e1,i=opn e2,d=d},an);
298 :     genCmp0(cc,d)
299 :     )
300 : monnier 245
301 : monnier 411 (* emit externally defined multiply or division operation (V8) *)
302 :     and extarith(gen,genConst,e1,e2,d,cc,comm) =
303 :     let fun nonconst(e1,e2) =
304 :     case (opn e1,opn e2,comm) of
305 :     (i,I.REG r,COMMUTE) => gen({r=r,i=i,d=d},reduceOpn)
306 :     | (I.REG r,i,_) => gen({r=r,i=i,d=d},reduceOpn)
307 :     | (r,i,_) => gen({r=reduceOpn r,i=i,d=d},reduceOpn)
308 :     fun const(e,i) =
309 :     let val r = expr e
310 : george 761 in genConst{r=r,i=toInt i,d=d}
311 : monnier 411 handle _ => gen({r=r,i=opn(T.LI i),d=d},reduceOpn)
312 :     end
313 :     val instrs =
314 :     case (comm,e1,e2) of
315 :     (_,e1,T.LI i) => const(e1,i)
316 :     | (COMMUTE,T.LI i,e2) => const(e2,i)
317 :     | _ => nonconst(e1,e2)
318 :     in app emit instrs;
319 :     genCmp0(cc,d)
320 :     end
321 : monnier 245
322 : monnier 411 (* emit 64-bit multiply or division operation (V9) *)
323 :     and muldiv64(a,genConst,e1,e2,d,cc,comm,an) =
324 :     let fun nonconst(e1,e2) =
325 :     [mark'(
326 :     case (opn e1,opn e2,comm) of
327 :     (i,I.REG r,COMMUTE) => I.ARITH{a=a,r=r,i=i,d=d}
328 :     | (I.REG r,i,_) => I.ARITH{a=a,r=r,i=i,d=d}
329 :     | (r,i,_) => I.ARITH{a=a,r=reduceOpn r,i=i,d=d},an)
330 :     ]
331 :     fun const(e,i) =
332 :     let val r = expr e
333 : george 761 in genConst{r=r,i=toInt i,d=d}
334 : monnier 411 handle _ => [mark'(I.ARITH{a=a,r=r,i=opn(T.LI i),d=d},an)]
335 :     end
336 :     val instrs =
337 :     case (comm,e1,e2) of
338 :     (_,e1,T.LI i) => const(e1,i)
339 :     | (COMMUTE,T.LI i,e2) => const(e2,i)
340 :     | _ => nonconst(e1,e2)
341 :     in app emit instrs;
342 :     genCmp0(cc,d)
343 :     end
344 :    
345 :     (* divisions *)
346 : george 545 and divu32 x = Mulu32.divide{mode=T.TO_ZERO,stm=doStmt} x
347 : leunga 657 and divs32 x = Muls32.divide{mode=T.TO_ZERO,stm=doStmt} x
348 : george 545 and divt32 x = Mult32.divide{mode=T.TO_ZERO,stm=doStmt} x
349 :     and divu64 x = Mulu64.divide{mode=T.TO_ZERO,stm=doStmt} x
350 : leunga 657 and divs64 x = Muls64.divide{mode=T.TO_ZERO,stm=doStmt} x
351 : george 545 and divt64 x = Mult64.divide{mode=T.TO_ZERO,stm=doStmt} x
352 : monnier 411
353 :     (* emit an unary floating point op *)
354 :     and funary(a,e,d,an) = mark(I.FPop1{a=a,r=fexpr e,d=d},an)
355 :    
356 :     (* emit a binary floating point op *)
357 :     and farith(a,e1,e2,d,an) =
358 :     mark(I.FPop2{a=a,r1=fexpr e1,r2=fexpr e2,d=d},an)
359 :    
360 :     (* convert an expression into an addressing mode *)
361 : blume 841 and addr(T.ADD(ty, (T.ADD (_, e, T.LI n)|
362 :     T.ADD (_, T.LI n, e)), T.LI n')) =
363 :     addr(T.ADD (ty, e, T.LI (T.I.ADD (ty, n, n'))))
364 :     | addr(T.ADD(ty, T.SUB (_, e, T.LI n), T.LI n')) =
365 :     addr(T.ADD (ty, e, T.LI (T.I.SUB (ty, n', n))))
366 :     | addr(T.ADD(_,e,T.LI n)) =
367 : george 761 if immed13 n then (expr e,I.IMMED(toInt n))
368 : monnier 411 else let val d = newReg()
369 :     in loadImmed(n,d,REG,[]); (d,opn e) end
370 : leunga 775 | addr(T.ADD(_,e,x as T.CONST c)) = (expr e,I.LAB x)
371 :     | addr(T.ADD(_,e,x as T.LABEL l)) = (expr e,I.LAB x)
372 :     | addr(T.ADD(_,e,T.LABEXP x)) = (expr e,I.LAB x)
373 : monnier 411 | addr(T.ADD(ty,i as T.LI _,e)) = addr(T.ADD(ty,e,i))
374 : leunga 775 | addr(T.ADD(_,x as T.CONST c,e)) = (expr e,I.LAB x)
375 :     | addr(T.ADD(_,x as T.LABEL l,e)) = (expr e,I.LAB x)
376 :     | addr(T.ADD(_,T.LABEXP x,e)) = (expr e,I.LAB x)
377 : monnier 411 | addr(T.ADD(_,e1,e2)) = (expr e1,I.REG(expr e2))
378 : george 761 | addr(T.SUB(ty,e,T.LI n)) = addr(T.ADD(ty,e,T.LI(T.I.NEG(32,n))))
379 : leunga 775 | addr(x as T.LABEL l) = (zeroR,I.LAB x)
380 :     | addr(T.LABEXP x) = (zeroR,I.LAB x)
381 : monnier 411 | addr a = (expr a,zeroOpn)
382 :    
383 :     (* emit an integer load *)
384 :     and load(l,a,d,mem,cc,an) =
385 :     let val (r,i) = addr a
386 :     in mark(I.LOAD{l=l,r=r,i=i,d=d,mem=mem},an);
387 :     genCmp0(cc,d)
388 :     end
389 :    
390 :     (* emit an integer store *)
391 :     and store(s,a,d,mem,an) =
392 :     let val (r,i) = addr a
393 :     in mark(I.STORE{s=s,r=r,i=i,d=expr d,mem=mem},an) end
394 :    
395 :     (* emit a floating point load *)
396 :     and fload(l,a,d,mem,an) =
397 :     let val (r,i) = addr a
398 :     in mark(I.FLOAD{l=l,r=r,i=i,d=d,mem=mem},an) end
399 :    
400 :     (* emit a floating point store *)
401 :     and fstore(s,a,d,mem,an) =
402 :     let val (r,i) = addr a
403 :     in mark(I.FSTORE{s=s,r=r,i=i,d=fexpr d,mem=mem},an) end
404 :    
405 :     (* emit a jump *)
406 :     and jmp(a,labs,an) =
407 :     let val (r,i) = addr a
408 :     in mark(I.JMP{r=r,i=i,labs=labs,nop=true},an) end
409 :    
410 : george 545 (* convert mlrisc to cellset *)
411 :     and cellset mlrisc =
412 :     let fun g([],set) = set
413 : leunga 744 | g(T.GPR(T.REG(_,r))::regs,set) = g(regs,C.CellSet.add(r,set))
414 :     | g(T.FPR(T.FREG(_,f))::regs,set) = g(regs,C.CellSet.add(f,set))
415 :     | g(T.CCR(T.CC(_,cc))::regs,set) = g(regs,C.CellSet.add(cc,set))
416 : george 545 | g(_::regs, set) = g(regs,set)
417 :     in g(mlrisc, C.empty) end
418 :    
419 : monnier 411 (* emit a function call *)
420 : blume 839 and call(a,flow,defs,uses,mem,cutsTo,an,0) =
421 :     let val (r,i) = addr a
422 :     val defs=cellset(defs)
423 :     val uses=cellset(uses)
424 : george 889 in case (CB.registerId r,i) of
425 : blume 839 (0,I.LAB(T.LABEL l)) =>
426 :     mark(I.CALL{label=l,defs=C.addReg(C.linkReg,defs),uses=uses,
427 :     cutsTo=cutsTo,mem=mem,nop=true},an)
428 :     | _ => mark(I.JMPL{r=r,i=i,d=C.linkReg,defs=defs,uses=uses,
429 :     cutsTo=cutsTo,mem=mem,nop=true},an)
430 :     end
431 :     | call _ = error "pops<>0 not implemented"
432 : monnier 245
433 : monnier 411 (* emit an integer branch instruction *)
434 : leunga 744 and branch(T.CMP(ty,cond,a,b),lab,an) =
435 : monnier 411 let val (cond,a,b) =
436 :     case a of
437 : george 761 (T.LI _ | T.CONST _ | T.LABEL _) =>
438 : george 545 (T.Basis.swapCond cond,b,a)
439 : monnier 411 | _ => (cond,a,b)
440 :     in if V9 then
441 :     branchV9(cond,a,b,lab,an)
442 :     else
443 :     (doExpr(T.SUB(ty,a,b),newReg(),CC,[]); br(cond,lab,an))
444 :     end
445 : leunga 744 | branch(T.CC(cond,r),lab,an) =
446 : george 889 if CB.sameCell(r, C.psr) then br(cond,lab,an)
447 : leunga 744 else (genCmp0(CC,r); br(cond,lab,an))
448 :     | branch(T.FCMP(fty,cond,a,b),lab,an) =
449 : george 545 let val cmp = case fty of
450 :     32 => I.FCMPs
451 :     | 64 => I.FCMPd
452 :     | _ => error "fbranch"
453 :     in emit(I.FCMP{cmp=cmp,r1=fexpr a,r2=fexpr b,nop=true});
454 :     mark(I.FBfcc{b=fcond cond,a=false,label=lab,nop=true},an)
455 :     end
456 : monnier 411 | branch _ = error "branch"
457 : monnier 245
458 : monnier 411 and branchV9(cond,a,b,lab,an) =
459 : leunga 624 let val size = Gen.Size.size a
460 : monnier 411 in if useBR andalso signedCmp cond then
461 :     let val r = newReg()
462 :     in doExpr(T.SUB(size,a,b),r,REG,[]);
463 :     brcond(cond,r,lab,an)
464 :     end
465 :     else
466 :     let val cc = case size of 32 => I.ICC
467 :     | 64 => I.XCC
468 :     | _ => error "branchV9"
469 :     in doExpr(T.SUB(size,a,b),newReg(),CC,[]);
470 :     bp(cond,cc,lab,an)
471 :     end
472 :     end
473 : monnier 245
474 : monnier 411 and br(c,lab,an) = mark(I.Bicc{b=cond c,a=true,label=lab,nop=true},an)
475 : monnier 245
476 : monnier 411 and brcond(c,r,lab,an) =
477 :     mark(I.BR{rcond=rcond c,r=r,p=I.PT,a=true,label=lab,nop=true},an)
478 : monnier 245
479 : monnier 411 and bp(c,cc,lab,an) =
480 :     mark(I.BP{b=cond c,cc=cc,p=I.PT,a=true,label=lab,nop=true},an)
481 : monnier 245
482 : monnier 411 (* generate code for a statement *)
483 :     and stmt(T.MV(_,d,e),an) = doExpr(e,d,REG,an)
484 :     | stmt(T.FMV(_,d,e),an) = doFexpr(e,d,an)
485 :     | stmt(T.CCMV(d,e),an) = doCCexpr(e,d,an)
486 :     | stmt(T.COPY(_,dst,src),an) = copy(dst,src,an)
487 : monnier 475 | stmt(T.FCOPY(_,dst,src),an) = fcopy(dst,src,an)
488 : leunga 775 | stmt(T.JMP(T.LABEL l,_),an) =
489 : monnier 411 mark(I.Bicc{b=I.BA,a=true,label=l,nop=false},an)
490 : leunga 744 | stmt(T.JMP(e,labs),an) = jmp(e,labs,an)
491 : blume 839 | stmt(T.CALL{funct,targets,defs,uses,region,pops,...},an) =
492 :     call(funct,targets,defs,uses,region,[],an,pops)
493 : leunga 796 | stmt(T.FLOW_TO
494 : blume 839 (T.CALL{funct,targets,defs,uses,region,pops,...},cutsTo),an) =
495 :     call(funct,targets,defs,uses,region,cutsTo,an,pops)
496 : george 545 | stmt(T.RET _,an) = mark(I.RET{leaf=not registerwindow,nop=true},an)
497 : monnier 411 | stmt(T.STORE(8,a,d,mem),an) = store(I.STB,a,d,mem,an)
498 :     | stmt(T.STORE(16,a,d,mem),an) = store(I.STH,a,d,mem,an)
499 :     | stmt(T.STORE(32,a,d,mem),an) = store(I.ST,a,d,mem,an)
500 :     | stmt(T.STORE(64,a,d,mem),an) =
501 :     store(if V9 then I.STX else I.STD,a,d,mem,an)
502 :     | stmt(T.FSTORE(32,a,d,mem),an) = fstore(I.STF,a,d,mem,an)
503 :     | stmt(T.FSTORE(64,a,d,mem),an) = fstore(I.STDF,a,d,mem,an)
504 : leunga 744 | stmt(T.BCC(cc,lab),an) = branch(cc,lab,an)
505 : george 545 | stmt(T.DEFINE l,_) = defineLabel l
506 : monnier 411 | stmt(T.ANNOTATION(s,a),an) = stmt(s,a::an)
507 : george 555 | stmt(T.EXT s,an) = ExtensionComp.compileSext(reducer()) {stm=s, an=an}
508 : george 545 | stmt(s,an) = doStmts(Gen.compileStm s)
509 : monnier 245
510 : monnier 411 and doStmt s = stmt(s,[])
511 : monnier 245
512 : george 545 and doStmts ss = app doStmt ss
513 : monnier 245
514 : monnier 411 (* convert an expression into a register *)
515 : george 761 and expr e = let
516 :     fun comp() = let
517 :     val d = newReg()
518 :     in doExpr(e, d, REG, []); d
519 :     end
520 :     in case e
521 :     of T.REG(_,r) => r
522 :     | T.LI z => if T.I.isZero z then zeroR else comp()
523 :     | _ => comp()
524 :     end
525 : monnier 245
526 : monnier 411 (* compute an integer expression and put the result in register d
527 :     * If cc is set then set the condition code with the result.
528 :     *)
529 :     and doExpr(e,d,cc,an) =
530 :     case e of
531 :     T.REG(_,r) => (move(r,d,an); genCmp0(cc,r))
532 :     | T.LI n => loadImmed(n,d,cc,an)
533 : leunga 775 | T.LABEL l => loadLabel(e,d,cc,an)
534 :     | T.CONST c => loadLabel(e,d,cc,an)
535 :     | T.LABEXP x => loadLabel(x,d,cc,an)
536 : monnier 245
537 : monnier 411 (* generic 32/64 bit support *)
538 :     | T.ADD(_,a,b) => arith(I.ADD,I.ADDCC,a,b,d,cc,COMMUTE,[],an)
539 : george 761 | T.SUB(_,a,b) => let
540 :     fun default() = arith(I.SUB,I.SUBCC,a,b,d,cc,NOCOMMUTE,[],an)
541 :     in
542 :     case b
543 :     of T.LI z =>
544 :     if T.I.isZero(z) then doExpr(a,d,cc,an) else default()
545 :     | _ => default()
546 :     (*esac*)
547 :     end
548 :    
549 : monnier 411 | T.ANDB(_,a,T.NOTB(_,b)) =>
550 :     arith(I.ANDN,I.ANDNCC,a,b,d,cc,NOCOMMUTE,[],an)
551 :     | T.ORB(_,a,T.NOTB(_,b)) =>
552 :     arith(I.ORN,I.ORNCC,a,b,d,cc,NOCOMMUTE,[],an)
553 :     | T.XORB(_,a,T.NOTB(_,b)) =>
554 :     arith(I.XNOR,I.XNORCC,a,b,d,cc,COMMUTE,[],an)
555 :     | T.ANDB(_,T.NOTB(_,a),b) =>
556 :     arith(I.ANDN,I.ANDNCC,b,a,d,cc,NOCOMMUTE,[],an)
557 :     | T.ORB(_,T.NOTB(_,a),b) =>
558 :     arith(I.ORN,I.ORNCC,b,a,d,cc,NOCOMMUTE,[],an)
559 :     | T.XORB(_,T.NOTB(_,a),b) =>
560 :     arith(I.XNOR,I.XNORCC,b,a,d,cc,COMMUTE,[],an)
561 :     | T.NOTB(_,T.XORB(_,a,b)) =>
562 :     arith(I.XNOR,I.XNORCC,a,b,d,cc,COMMUTE,[],an)
563 : monnier 245
564 : monnier 411 | T.ANDB(_,a,b) => arith(I.AND,I.ANDCC,a,b,d,cc,COMMUTE,[],an)
565 :     | T.ORB(_,a,b) => arith(I.OR,I.ORCC,a,b,d,cc,COMMUTE,[],an)
566 :     | T.XORB(_,a,b) => arith(I.XOR,I.XORCC,a,b,d,cc,COMMUTE,[],an)
567 : george 761 | T.NOTB(_,a) => arith(I.XNOR,I.XNORCC,a,LI 0,d,cc,COMMUTE,[],an)
568 : monnier 245
569 : monnier 411 (* 32 bit support *)
570 :     | T.SRA(32,a,b) => shift(I.SRA,a,b,d,cc,an)
571 :     | T.SRL(32,a,b) => shift(I.SRL,a,b,d,cc,an)
572 :     | T.SLL(32,a,b) => shift(I.SLL,a,b,d,cc,an)
573 :     | T.ADDT(32,a,b)=>
574 :     arith(I.ADDCC,I.ADDCC,a,b,d,CC_REG,COMMUTE,trap32,an)
575 :     | T.SUBT(32,a,b)=>
576 :     arith(I.SUBCC,I.SUBCC,a,b,d,CC_REG,NOCOMMUTE,trap32,an)
577 : leunga 657 | T.MULU(32,a,b) => extarith(P.umul32,
578 :     Mulu32.multiply,a,b,d,cc,COMMUTE)
579 :     | T.MULS(32,a,b) => extarith(P.smul32,
580 :     Muls32.multiply,a,b,d,cc,COMMUTE)
581 :     | T.MULT(32,a,b) => extarith(P.smul32trap,
582 :     Mult32.multiply,a,b,d,cc,COMMUTE)
583 :     | T.DIVU(32,a,b) => extarith(P.udiv32,divu32,a,b,d,cc,NOCOMMUTE)
584 :     | T.DIVS(32,a,b) => extarith(P.sdiv32,divs32,a,b,d,cc,NOCOMMUTE)
585 :     | T.DIVT(32,a,b) => extarith(P.sdiv32trap,divt32,a,b,d,cc,NOCOMMUTE)
586 : monnier 245
587 : monnier 411 (* 64 bit support *)
588 :     | T.SRA(64,a,b) => shift(I.SRAX,a,b,d,cc,an)
589 :     | T.SRL(64,a,b) => shift(I.SRLX,a,b,d,cc,an)
590 :     | T.SLL(64,a,b) => shift(I.SLLX,a,b,d,cc,an)
591 :     | T.ADDT(64,a,b)=>
592 :     arith(I.ADDCC,I.ADDCC,a,b,d,CC_REG,COMMUTE,trap64,an)
593 :     | T.SUBT(64,a,b)=>
594 :     arith(I.SUBCC,I.SUBCC,a,b,d,CC_REG,NOCOMMUTE,trap64,an)
595 :     | T.MULU(64,a,b) =>
596 :     muldiv64(I.MULX,Mulu64.multiply,a,b,d,cc,COMMUTE,an)
597 : leunga 657 | T.MULS(64,a,b) =>
598 :     muldiv64(I.MULX,Muls64.multiply,a,b,d,cc,COMMUTE,an)
599 : monnier 411 | T.MULT(64,a,b) =>
600 :     (muldiv64(I.MULX,Mult64.multiply,a,b,d,CC_REG,COMMUTE,an);
601 :     app emit trap64)
602 :     | T.DIVU(64,a,b) => muldiv64(I.UDIVX,divu64,a,b,d,cc,NOCOMMUTE,an)
603 : leunga 657 | T.DIVS(64,a,b) => muldiv64(I.SDIVX,divs64,a,b,d,cc,NOCOMMUTE,an)
604 : monnier 411 | T.DIVT(64,a,b) => muldiv64(I.SDIVX,divt64,a,b,d,cc,NOCOMMUTE,an)
605 : monnier 245
606 : monnier 411 (* loads *)
607 :     | T.LOAD(8,a,mem) => load(I.LDUB,a,d,mem,cc,an)
608 : leunga 744 | T.SX(_,_,T.LOAD(8,a,mem)) => load(I.LDSB,a,d,mem,cc,an)
609 : monnier 411 | T.LOAD(16,a,mem) => load(I.LDUH,a,d,mem,cc,an)
610 : leunga 744 | T.SX(_,_,T.LOAD(16,a,mem)) => load(I.LDSH,a,d,mem,cc,an)
611 : monnier 411 | T.LOAD(32,a,mem) => load(I.LD,a,d,mem,cc,an)
612 : george 545 | T.LOAD(64,a,mem) =>
613 :     load(if V9 then I.LDX else I.LDD,a,d,mem,cc,an)
614 : monnier 245
615 : monnier 411 (* conditional expression *)
616 : george 545 | T.COND exp => doStmts (Gen.compileCond{exp=exp,rd=d,an=an})
617 : monnier 411
618 :     (* misc *)
619 : george 545 | T.LET(s,e) => (doStmt s; doExpr(e, d, cc, an))
620 :     | T.MARK(e,A.MARKREG f) => (f d; doExpr(e,d,cc,an))
621 :     | T.MARK(e,a) => doExpr(e,d,cc,a::an)
622 :     | T.PRED(e,c) => doExpr(e,d,cc,A.CTRLUSE c::an)
623 : george 555 | T.REXT e => ExtensionComp.compileRext (reducer()) {e=e, rd=d, an=an}
624 : george 545 | e => doExpr(Gen.compileRexp e,d,cc,an)
625 : monnier 411
626 :     (* generate a comparison with zero *)
627 :     and genCmp0(REG,_) = ()
628 : leunga 744 | genCmp0(_,d) = emit(I.ARITH{a=I.SUBCC,r=d,i=zeroOpn,d=zeroR})
629 : monnier 411
630 :     (* convert an expression into a floating point register *)
631 :     and fexpr(T.FREG(_,r)) = r
632 :     | fexpr e = let val d = newFreg() in doFexpr(e,d,[]); d end
633 :    
634 :     (* compute a floating point expression and put the result in d *)
635 :     and doFexpr(e,d,an) =
636 :     case e of
637 :     (* single precision *)
638 :     T.FREG(32,r) => fmoves(r,d,an)
639 :     | T.FLOAD(32,ea,mem) => fload(I.LDF,ea,d,mem,an)
640 :     | T.FADD(32,a,b) => farith(I.FADDs,a,b,d,an)
641 :     | T.FSUB(32,a,b) => farith(I.FSUBs,a,b,d,an)
642 :     | T.FMUL(32,a,b) => farith(I.FMULs,a,b,d,an)
643 :     | T.FDIV(32,a,b) => farith(I.FDIVs,a,b,d,an)
644 :     | T.FABS(32,a) => funary(I.FABSs,a,d,an)
645 :     | T.FNEG(32,a) => funary(I.FNEGs,a,d,an)
646 :     | T.FSQRT(32,a) => funary(I.FSQRTs,a,d,an)
647 :    
648 :     (* double precision *)
649 :     | T.FREG(64,r) => fmoved(r,d,an)
650 :     | T.FLOAD(64,ea,mem) => fload(I.LDDF,ea,d,mem,an)
651 :     | T.FADD(64,a,b) => farith(I.FADDd,a,b,d,an)
652 :     | T.FSUB(64,a,b) => farith(I.FSUBd,a,b,d,an)
653 :     | T.FMUL(64,a,b) => farith(I.FMULd,a,b,d,an)
654 :     | T.FDIV(64,a,b) => farith(I.FDIVd,a,b,d,an)
655 :     | T.FABS(64,a) => funary(I.FABSd,a,d,an)
656 :     | T.FNEG(64,a) => funary(I.FNEGd,a,d,an)
657 :     | T.FSQRT(64,a) => funary(I.FSQRTd,a,d,an)
658 :    
659 :     (* quad precision *)
660 :     | T.FREG(128,r) => fmoveq(r,d,an)
661 :     | T.FADD(128,a,b) => farith(I.FADDq,a,b,d,an)
662 :     | T.FSUB(128,a,b) => farith(I.FSUBq,a,b,d,an)
663 :     | T.FMUL(128,a,b) => farith(I.FMULq,a,b,d,an)
664 :     | T.FDIV(128,a,b) => farith(I.FDIVq,a,b,d,an)
665 :     | T.FABS(128,a) => funary(I.FABSq,a,d,an)
666 :     | T.FNEG(128,a) => funary(I.FNEGq,a,d,an)
667 :     | T.FSQRT(128,a) => funary(I.FSQRTq,a,d,an)
668 :    
669 :     (* floating point to floating point *)
670 : george 545 | T.CVTF2F(ty,ty',e) =>
671 : monnier 475 (case (ty,ty') of
672 :     (32,32) => doFexpr(e,d,an)
673 :     | (64,32) => funary(I.FsTOd,e,d,an)
674 : monnier 411 | (128,32) => funary(I.FsTOq,e,d,an)
675 : monnier 475 | (32,64) => funary(I.FdTOs,e,d,an)
676 :     | (64,64) => doFexpr(e,d,an)
677 : monnier 411 | (128,64) => funary(I.FdTOq,e,d,an)
678 :     | (32,128) => funary(I.FqTOs,e,d,an)
679 :     | (64,128) => funary(I.FqTOd,e,d,an)
680 :     | (128,128) => doFexpr(e,d,an)
681 :     | _ => error "CVTF2F"
682 :     )
683 :    
684 :     (* integer to floating point *)
685 : george 545 | T.CVTI2F(32,32,e) => app emit (P.cvti2s({i=opn e,d=d},reduceOpn))
686 :     | T.CVTI2F(64,32,e) => app emit (P.cvti2d({i=opn e,d=d},reduceOpn))
687 :     | T.CVTI2F(128,32,e) => app emit (P.cvti2q({i=opn e,d=d},reduceOpn))
688 : monnier 411
689 : george 545 | T.FMARK(e,A.MARKREG f) => (f d; doFexpr(e,d,an))
690 :     | T.FMARK(e,a) => doFexpr(e,d,a::an)
691 :     | T.FPRED(e,c) => doFexpr(e,d,A.CTRLUSE c::an)
692 : george 555 | T.FEXT e => ExtensionComp.compileFext (reducer()) {e=e, fd=d, an=an}
693 : george 545 | e => doFexpr(Gen.compileFexp e,d,an)
694 : monnier 411
695 : leunga 744 and doCCexpr(T.CMP(ty,cond,e1,e2),cc,an) =
696 : george 889 if CB.sameCell(cc,C.psr) then
697 : leunga 744 doExpr(T.SUB(ty,e1,e2),newReg(),CC,an)
698 :     else error "doCCexpr"
699 :     | doCCexpr(T.CC(_,r),d,an) =
700 : george 889 if CB.sameColor(r,C.psr) then error "doCCexpr"
701 : leunga 744 else move(r,d,an)
702 : george 545 | doCCexpr(T.CCMARK(e,A.MARKREG f),d,an) = (f d; doCCexpr(e,d,an))
703 :     | doCCexpr(T.CCMARK(e,a),d,an) = doCCexpr(e,d,a::an)
704 :     | doCCexpr(T.CCEXT e,d,an) =
705 : george 555 ExtensionComp.compileCCext (reducer()) {e=e, ccd=d, an=an}
706 : monnier 411 | doCCexpr e = error "doCCexpr"
707 :    
708 :     and ccExpr e = let val d = newReg() in doCCexpr(e,d,[]); d end
709 :    
710 :     (* convert an expression into an operand *)
711 : leunga 775 and opn(x as T.CONST c) = I.LAB x
712 :     | opn(x as T.LABEL l) = I.LAB x
713 :     | opn(T.LABEXP x) = I.LAB x
714 : george 761 | opn(e as T.LI n) =
715 :     if T.I.isZero(n) then zeroOpn
716 :     else if immed13 n then I.IMMED(toInt n)
717 :     else I.REG(expr e)
718 : monnier 411 | opn e = I.REG(expr e)
719 :    
720 : george 545 and reducer() =
721 :     T.REDUCER{reduceRexp = expr,
722 :     reduceFexp = fexpr,
723 :     reduceCCexp = ccExpr,
724 :     reduceStm = stmt,
725 :     operand = opn,
726 :     reduceOperand = reduceOpn,
727 :     addressOf = addr,
728 :     emit = mark,
729 :     instrStream = instrStream,
730 :     mltreeStream = self()
731 :     }
732 :     and self() =
733 :     S.STREAM
734 : leunga 815 { beginCluster = beginCluster,
735 :     endCluster = endCluster,
736 :     emit = doStmt,
737 :     pseudoOp = pseudoOp,
738 :     defineLabel = defineLabel,
739 :     entryLabel = entryLabel,
740 :     comment = comment,
741 :     annotation = annotation,
742 :     getAnnotations = getAnnotations,
743 :     exitBlock = fn regs => exitBlock(cellset regs)
744 : george 545 }
745 :     in self()
746 : monnier 245 end
747 :    
748 :     end
749 :    
750 : monnier 411 (*
751 :     * Machine code generator for SPARC.
752 : monnier 245 *
753 : monnier 411 * The SPARC architecture has 32 general purpose registers (%g0 is always 0)
754 :     * and 32 single precision floating point registers.
755 : monnier 245 *
756 : monnier 411 * Some Ugliness: double precision floating point registers are
757 :     * register pairs. There are no double precision moves, negation and absolute
758 :     * values. These require two single precision operations. I've created
759 :     * composite instructions FMOVd, FNEGd and FABSd to stand for these.
760 : monnier 245 *
761 : monnier 411 * All integer arithmetic instructions can optionally set the condition
762 :     * code register. We use this to simplify certain comparisons with zero.
763 : monnier 245 *
764 : monnier 411 * Integer multiplication, division and conversion from integer to floating
765 :     * go thru the pseudo instruction interface, since older sparcs do not
766 :     * implement these instructions in hardware.
767 : monnier 245 *
768 : monnier 411 * In addition, the trap instruction for detecting overflow is a parameter.
769 :     * This allows different trap vectors to be used.
770 : monnier 245 *
771 : monnier 411 * -- Allen
772 :     *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0