Debian Codegraph @

lib/mlrisc-lib/MLRISC/sparc/mltree/sparc.sml

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