Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Tracker SCM

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

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