Debian Codegraph @

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

raw
1 (* hppa.sml
2 *
3 * COPYRIGHT (c) 1996 AT&T Bell Laboratories.
4 *
5 * generates machine code from the mltree.
6 *
7 * This new version has been completely rewritten to take (more) advantage
8 * of the new improved instruction set.
9 *
10 * Please see the README.hppa file for details.
11 *
12 * -- Allen
13 *)
14
15 functor Hppa
16 (structure HppaInstr : HPPAINSTR
17 structure ExtensionComp : MLTREE_EXTENSION_COMP (* where I = HppaInstr and T = HppaInstr.T *)
18 where type I.addressing_mode = HppaInstr.addressing_mode
19 and type I.ea = HppaInstr.ea
20 and type I.instr = HppaInstr.instr
21 and type I.instruction = HppaInstr.instruction
22 and type I.operand = HppaInstr.operand
23 where type T.Basis.cond = HppaInstr.T.Basis.cond
24 and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
25 and type T.Basis.ext = HppaInstr.T.Basis.ext
26 and type T.Basis.fcond = HppaInstr.T.Basis.fcond
27 and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
28 and type T.Constant.const = HppaInstr.T.Constant.const
29 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
30 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
31 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
32 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
33 and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
34 and type T.Region.region = HppaInstr.T.Region.region
35 and type T.ccexp = HppaInstr.T.ccexp
36 and type T.fexp = HppaInstr.T.fexp
37 (* and type T.labexp = HppaInstr.T.labexp *)
38 and type T.mlrisc = HppaInstr.T.mlrisc
39 and type T.oper = HppaInstr.T.oper
40 and type T.rep = HppaInstr.T.rep
41 and type T.rexp = HppaInstr.T.rexp
42 and type T.stm = HppaInstr.T.stm
43 structure MilliCode : HPPA_MILLICODE (* where I = HppaInstr *)
44 where type I.Constant.const = HppaInstr.Constant.const
45 and type I.Region.region = HppaInstr.Region.region
46 and type I.T.Basis.cond = HppaInstr.T.Basis.cond
47 and type I.T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
48 and type I.T.Basis.ext = HppaInstr.T.Basis.ext
49 and type I.T.Basis.fcond = HppaInstr.T.Basis.fcond
50 and type I.T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
51 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
52 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
53 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
54 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
55 and type I.T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
56 and type I.T.ccexp = HppaInstr.T.ccexp
57 and type I.T.fexp = HppaInstr.T.fexp
58 (* and type I.T.labexp = HppaInstr.T.labexp *)
59 and type I.T.mlrisc = HppaInstr.T.mlrisc
60 and type I.T.oper = HppaInstr.T.oper
61 and type I.T.rep = HppaInstr.T.rep
62 and type I.T.rexp = HppaInstr.T.rexp
63 and type I.T.stm = HppaInstr.T.stm
64 and type I.addressing_mode = HppaInstr.addressing_mode
65 and type I.arith = HppaInstr.arith
66 and type I.arithi = HppaInstr.arithi
67 and type I.bcond = HppaInstr.bcond
68 and type I.bitcond = HppaInstr.bitcond
69 and type I.cmp = HppaInstr.cmp
70 and type I.cmpi = HppaInstr.cmpi
71 and type I.ea = HppaInstr.ea
72 and type I.farith = HppaInstr.farith
73 and type I.fcnv = HppaInstr.fcnv
74 and type I.fcond = HppaInstr.fcond
75 and type I.field_selector = HppaInstr.field_selector
76 and type I.fload = HppaInstr.fload
77 and type I.floadx = HppaInstr.floadx
78 and type I.fmt = HppaInstr.fmt
79 and type I.fstore = HppaInstr.fstore
80 and type I.fstorex = HppaInstr.fstorex
81 and type I.funary = HppaInstr.funary
82 and type I.instr = HppaInstr.instr
83 and type I.instruction = HppaInstr.instruction
84 and type I.load = HppaInstr.load
85 and type I.loadi = HppaInstr.loadi
86 and type I.operand = HppaInstr.operand
87 and type I.scond = HppaInstr.scond
88 and type I.shift = HppaInstr.shift
89 and type I.shiftv = HppaInstr.shiftv
90 and type I.store = HppaInstr.store
91 structure LabelComp : LABEL_COMP (* where I = HppaInstr and T = HppaInstr.T *)
92 where type I.addressing_mode = HppaInstr.addressing_mode
93 and type I.ea = HppaInstr.ea
94 and type I.instr = HppaInstr.instr
95 and type I.instruction = HppaInstr.instruction
96 and type I.operand = HppaInstr.operand
97 where type T.Basis.cond = HppaInstr.T.Basis.cond
98 and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
99 and type T.Basis.ext = HppaInstr.T.Basis.ext
100 and type T.Basis.fcond = HppaInstr.T.Basis.fcond
101 and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
102 and type T.Constant.const = HppaInstr.T.Constant.const
103 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
104 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
105 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
106 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
107 and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
108 and type T.Region.region = HppaInstr.T.Region.region
109 and type T.ccexp = HppaInstr.T.ccexp
110 and type T.fexp = HppaInstr.T.fexp
111 (* and type T.labexp = HppaInstr.T.labexp *)
112 and type T.mlrisc = HppaInstr.T.mlrisc
113 and type T.oper = HppaInstr.T.oper
114 and type T.rep = HppaInstr.T.rep
115 and type T.rexp = HppaInstr.T.rexp
116 and type T.stm = HppaInstr.T.stm
117 val costOfMultiply : int ref
118 val costOfDivision : int ref
119 ) : MLTREECOMP =
120 struct
121 structure I = HppaInstr
122 structure T = I.T
123 structure TS = ExtensionComp.TS
124 structure C = I.C
125 structure CB = CellsBasis
126 structure MC = MilliCode
127 structure LC = LabelComp
128 structure Region = I.Region
129 structure A = MLRiscAnnotations
130 structure CFG = ExtensionComp.CFG
131
132 type instrStream = (I.instruction, C.cellset, CFG.cfg) TS.stream
133 type mltreeStream = (T.stm, T.mlrisc list, CFG.cfg) TS.stream
134
135 structure Gen = MLTreeGen(structure T = T
136 structure Cells = C
137 val intTy = 32
138 val naturalWidths = [32]
139 datatype rep = SE | ZE | NEITHER
140 val rep = NEITHER
141 )
142 fun mkcopy{dst, src, tmp} =
143 I.COPY{k=CB.GP, sz=32, dst=dst, src=src, tmp=tmp}
144 fun mkfcopy{dst, src, tmp} =
145 I.COPY{k=CB.FP, sz=64, dst=dst, src=src, tmp=tmp}
146 structure W = Word32
147 structure Multiply32 = struct
148 structure I = I
149 structure T = T
150 structure CB = CB
151 val intTy = 32
152 type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell}
153 type argi = {r:CB.cell,i:int,d:CB.cell}
154
155 fun mov{r,d} = mkcopy{dst=[d],src=[r],tmp=NONE}
156 fun add{r1,r2,d} = I.arith{a=I.ADD,r1=r1,r2=r2,t=d}
157 fun slli{r,i,d} = [I.shift{s=I.ZDEP,r=r,p=31-i,len=32-i,t=d}]
158 fun srli{r,i,d} = [I.shift{s=I.EXTRU,r=r,p=31-i,len=32-i,t=d}]
159 fun srai{r,i,d} = [I.shift{s=I.EXTRS,r=r,p=31-i,len=32-i,t=d}]
160 end
161
162 (* signed, trapping version of multiply and divide *)
163 structure Mult32 = MLTreeMult
164 (open Multiply32
165 val trapping = true
166 val multCost = costOfMultiply
167 val divCost = costOfDivision
168 fun addv{r1,r2,d} = [I.arith{a=I.ADDO,r1=r1,r2=r2,t=d}]
169 fun subv{r1,r2,d} = [I.arith{a=I.SUBO,r1=r1,r2=r2,t=d}]
170 val sh1addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH1ADDO,r1=r1,r2=r2,t=d}])
171 val sh2addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH2ADDO,r1=r1,r2=r2,t=d}])
172 val sh3addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH3ADDO,r1=r1,r2=r2,t=d}])
173
174 val signed = true)
175
176 (* unsigned, non-trapping version of multiply and divide *)
177 structure Mulu32 = MLTreeMult
178 (open Multiply32
179 val trapping = false
180 val multCost = costOfMultiply
181 val divCost = costOfDivision
182 fun addv{r1,r2,d} = [I.arith{a=I.ADD,r1=r1,r2=r2,t=d}]
183 fun subv{r1,r2,d} = [I.arith{a=I.SUB,r1=r1,r2=r2,t=d}]
184 val sh1addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH1ADDL,r1=r1,r2=r2,t=d}])
185 val sh2addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH2ADDL,r1=r1,r2=r2,t=d}])
186 val sh3addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH3ADDL,r1=r1,r2=r2,t=d}])
187
188 val signed = false)
189
190 (* signed, non-trapping version of multiply and divide *)
191 structure Muls32 = MLTreeMult
192 (open Multiply32
193 val trapping = false
194 val multCost = costOfMultiply
195 val divCost = costOfDivision
196 fun addv{r1,r2,d} = [I.arith{a=I.ADD,r1=r1,r2=r2,t=d}]
197 fun subv{r1,r2,d} = [I.arith{a=I.SUB,r1=r1,r2=r2,t=d}]
198 val sh1addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH1ADDL,r1=r1,r2=r2,t=d}])
199 val sh2addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH2ADDL,r1=r1,r2=r2,t=d}])
200 val sh3addv = SOME(fn{r1,r2,d} => [I.arith{a=I.SH3ADDL,r1=r1,r2=r2,t=d}])
201
202 val signed = true)
203
204 fun error msg = MLRiscErrorMsg.error("Hppa",msg)
205
206 datatype ea = datatype I.addressing_mode
207
208 datatype times248 = TIMES1 | TIMES2 | TIMES4 | TIMES8
209
210 datatype amode =
211 AMode of I.addressing_mode
212 | DISP of CB.cell * T.I.machine_int
213
214
215 fun LI i = T.LI(T.I.fromInt(32, i))
216 fun toInt mi = T.I.toInt(32, mi)
217 fun toInt32 mi = T.I.toInt32(32, mi)
218 fun toWord mi = T.I.toWord(32, mi)
219 fun toWord32 mi = T.I.toWord32(32, mi)
220 fun EQ(x,y) = T.I.EQ(32, x, y)
221 fun LT(x,y) = T.I.LT(32, x, y)
222 fun GE(x,y) = T.I.GE(32, x, y)
223
224
225 fun selectInstructions
226 (instrStream as
227 TS.S.STREAM{emit=emitInstruction, defineLabel, entryLabel, getAnnotations,
228 beginCluster, endCluster, annotation,
229 exitBlock, pseudoOp, comment, ...}) =
230 let
231 (* operand type and effective addresss *)
232
233 val newReg = C.newReg
234 val newFreg = C.newFreg
235 val CRReg = C.Reg C.CR
236 val zeroR = C.r0
237 val zeroF = C.f0
238 val zeroEA = I.Direct zeroR
239 val zeroT = T.REG(32,zeroR)
240 val zeroImmed = I.IMMED 0
241 val zeroOpn = zeroImmed
242
243 val emit = emitInstruction o I.INSTR
244
245 local
246 fun f(i,[]) = i
247 | f(i, a::an) = f (I.ANNOTATION{i=i, a=a}, an)
248 in
249 fun mark(i, an) = emitInstruction(f(I.INSTR i, an))
250 fun mark'(i, an) = emitInstruction(f(i, an))
251 end
252
253 val ldLabelEA = LC.ldLabelEA emitInstruction
254 val ldLabelOpnd = LC.ldLabelOpnd emitInstruction
255
256 (* Check whether an expression is being multiplied by 2, 4, or 8 *)
257 local
258 fun mul(mi,e, exp) =
259 if EQ(mi, 2) then (TIMES2, e)
260 else if EQ(mi, 4) then (TIMES4, e)
261 else if EQ(mi, 8) then (TIMES8, e)
262 else (TIMES1, exp)
263 in
264 fun times(exp) =
265 (case exp
266 of T.MULU(_, e, T.LI mi) => mul(mi, e, exp)
267 | T.MULU(_, T.LI mi, e) => mul(mi, e, exp)
268 | T.SLL(_, e, T.LI mi) =>
269 if EQ(mi, 1) then (TIMES2, e)
270 else if EQ(mi, 2) then (TIMES4, e)
271 else if EQ(mi, 3) then (TIMES8, e)
272 else (TIMES1, exp)
273 | _ => (TIMES1, exp)
274 (*esac*))
275
276 (* trapping version of the above *)
277 fun timest(exp as T.MULT(_, e, T.LI mi)) = mul(mi, e, exp)
278 | timest(exp as T.MULT(_, T.LI mi, e)) = mul(mi, e, exp)
279 | timest e = (TIMES1, e)
280 end (*local*)
281
282 fun im5 n = LT(n, 16) andalso GE(n, ~16)
283 fun im11 n = LT(n, 1024) andalso GE(n, ~1024)
284 fun im14 n = LT(n, 8192) andalso GE(n, ~8192)
285
286 (* Split values into 11 low bits and 21 high bits *)
287 fun split11w w =
288 {hi = Word32.toIntX(Word32.~>>(w,0w11)),
289 lo = Word32.toIntX(Word32.andb(w,0wx7ff))}
290 fun split11 n = split11w(toWord32 n)
291
292 (* load immediate *)
293 fun loadImmed(n,t,an) =
294 if im14 n
295 then mark(I.LDO{i=I.IMMED(toInt n),b=zeroR,t=t},an)
296 else let val {hi,lo} = split11 n
297 val tmp = newReg()
298 in emit(I.LDIL{i=I.IMMED hi,t=tmp});
299 mark(I.LDO{i=I.IMMED lo,b=tmp,t=t},an)
300 end
301
302 (* generate code to load a immediate constant *)
303 fun immed (n: T.I.machine_int) =
304 let val t = newReg() in loadImmed(n,t,[]); t end
305
306 (* load constant *)
307 fun loadConst(c,t,an) =
308 mark(I.LDO{b=zeroR,i=I.LabExp(c,I.F),t=t},an) (* XXX *)
309
310 (* convert an operand into a register *)
311 fun reduceOpn i =
312 let val t = newReg()
313 in emit(I.LDO{i=i,b=zeroR,t=t}); t end
314
315 (* emit parallel copies *)
316 fun copy(dst,src,an) =
317 mark'(mkcopy{dst=dst,src=src,
318 tmp=case dst of [_] => NONE | _ => SOME(I.Direct(newReg()))},an)
319 fun fcopy(dst,src,an) =
320 mark'(mkfcopy{dst=dst,src=src,
321 tmp=case dst of [_] => NONE | _ => SOME(I.FDirect(newFreg()))},an)
322
323 (* move register s to register t *)
324 fun move(s,t,an) =
325 if CB.sameColor(s,t) orelse CB.registerId t = 0 then ()
326 else if CB.registerId s = 0 then
327 mark(I.LDO{i=zeroImmed,b=zeroR,t=t},an)
328 else mark'(mkcopy{src=[s],dst=[t],tmp=NONE},an)
329
330 (* move floating point register s to register t *)
331 fun fmove(s,t,an) =
332 if CB.sameColor(s,t) then ()
333 else mark'(mkfcopy{src=[s],dst=[t],tmp=NONE},an)
334
335 (* generate millicode function call *)
336 fun milliCall(milliFn, e1, e2, rd) =
337 let val rs = expr e1
338 val rt = expr e2
339 in app emitInstruction (milliFn{rs=rs,rt=rt,rd=rd}) end
340
341 (* emit an arithmetic op with possible immediate mode
342 * The immed operand is the first operand on the HPPA! Arrrrggggghhhh!
343 *)
344 and immedArith(a,ai,e1,e2,t,an) =
345 case (opn e1,expr e2) of
346 (I.REG r1,r2) => mark(I.ARITH{a=a,r1=r1,r2=r2,t=t},an)
347 | (i,r) => mark(I.ARITHI{ai=ai,r=r,i=i,t=t},an)
348
349 (* emit a commutative arithmetic op with immediate mode *)
350 and commImmedArith(a,ai,e1,e2,t,an) =
351 case (opn e1,opn e2) of
352 (I.REG r1,I.REG r2) => mark(I.ARITH{a=a,r1=r1,r2=r2,t=t},an)
353 | (I.REG r,i) => mark(I.ARITHI{ai=ai,r=r,i=i,t=t},an)
354 | (i,I.REG r) => mark(I.ARITHI{ai=ai,r=r,i=i,t=t},an)
355 | (i,j) => mark(I.ARITHI{ai=ai,r=reduceOpn i,i=j,t=t},an)
356
357 (* emit an arithmetic op *)
358 and arith(a,e1,e2,t,an) =
359 mark(I.ARITH{a=a,r1=expr e1,r2=expr e2,t=t},an)
360
361 (* emit an unary floating point op *)
362 and funary(a,e,t,an) = mark(I.FUNARY{fu=a,f=fexpr e,t=t},an)
363
364 (* emit an conversion floating point op *)
365 and fcnv(a,e,t,an) = mark(I.FCNV{fcnv=a,f=fexpr e,t=t},an)
366
367 (* emit a binary floating point op *)
368 and farith(a,e1,e2,t,an) =
369 mark(I.FARITH{fa=a,r1=fexpr e1,r2=fexpr e2,t=t},an)
370
371 (* convert an expression into an addressing mode
372 * scale is the size of the data being addressed.
373 *
374 * Return the addressing mode and an infinite precision immediate
375 * in the case of DISPea.
376 *)
377 and addr(scale,T.ADD(_,e,T.LI n)) = DISP(expr e, n)
378 | addr(scale,T.ADD(_,e,c as T.CONST _)) =
379 AMode(DISPea(expr e,I.LabExp(c,I.F)))
380 | addr(scale,T.ADD(ty,i as T.LI _,e)) = addr(scale,T.ADD(ty,e,i))
381 | addr(scale,T.ADD(_,c as T.CONST _,e)) =
382 AMode(DISPea(expr e,I.LabExp(c,I.F)))
383 | addr(scale,T.ADD(_,e,T.LABEXP le)) =
384 let val rs = expr e
385 val (rt, opnd) = ldLabelEA le
386 in case (CB.registerId rt, opnd) of
387 (0, opnd) => AMode(DISPea(rs,opnd))
388 | (_,I.IMMED 0) => AMode(INDXea(rs,rt))
389 | (_,opnd) =>
390 let val tmp = newReg()
391 in emit(I.ARITH{a=I.ADD,r1=rs,r2=rt,t=tmp});
392 AMode(DISPea(tmp,opnd))
393 end
394 end
395 | addr(scale,T.ADD(t,e1 as T.LABEXP l,e2)) = addr(scale,T.ADD(t,e2,e1))
396 | addr(scale,T.ADD(_,e1,e2)) =
397 let (* check for special multiply add sequence
398 * here, e1 is is scaled
399 *)
400 fun scaleIndexed(actualScale,opcode,e1,e2) =
401 if actualScale = scale then (* can we use scaled indexing mode?*)
402 let val x = expr e1
403 val b = expr e2
404 in AMode(INDXSCALEDea(b,x))
405 end
406 else (* no, use the SHnADD operator, then *)
407 let val tmp = newReg()
408 in emit(I.ARITH{a=opcode,r1=expr e1,r2=expr e2,t=tmp});
409 AMode(DISPea(tmp,zeroImmed))
410 end
411 in case times e1 of
412 (TIMES2,e1) => scaleIndexed(16,I.SH1ADD,e1,e2)
413 | (TIMES4,e1) => scaleIndexed(32,I.SH2ADD,e1,e2)
414 | (TIMES8,e1) => scaleIndexed(64,I.SH3ADD,e1,e2)
415 | _ =>
416 case times e2 of
417 (TIMES2,e2) => scaleIndexed(16,I.SH1ADD,e2,e1)
418 | (TIMES4,e2) => scaleIndexed(32,I.SH2ADD,e2,e1)
419 | (TIMES8,e2) => scaleIndexed(64,I.SH3ADD,e2,e1)
420 | _ => AMode(INDXea(expr e1,expr e2))
421 end
422 | addr(scale,T.SUB(ty,e,T.LI n)) = addr(scale,T.ADD(ty,e,T.LI(T.I.NEGT(32,n))))
423 | addr(scale,T.LABEXP lexp) = AMode(DISPea(ldLabelEA(lexp)))
424 | addr(scale,ea) = AMode(DISPea(expr ea,zeroImmed))
425
426 (* emit an integer load
427 * li - load immediate,
428 * l - load indexed
429 * ls - load indexed with scaling
430 * r1 is base r2 is x
431 *)
432 and load(scale,li,l,ls,ea,t,mem,an) =
433 case addr(scale,ea)
434 of DISP(r, off) =>
435 if im14 off then
436 mark(I.LOADI{li=li,r=r,i=I.IMMED(toInt off),t=t,mem=mem},an)
437 else
438 mark(I.LOAD{l=l,r1=r,r2=immed off,t=t,mem=mem},an)
439 | AMode(DISPea(r,i)) => mark(I.LOADI{li=li,r=r,i=i,t=t,mem=mem},an)
440 | AMode(INDXea(r1,r2)) => mark(I.LOAD{l=l,r1=r1,r2=r2,t=t,mem=mem},an)
441 | AMode(INDXSCALEDea(b,x)) => mark(I.LOAD{l=ls,r1=b,r2=x,t=t,mem=mem},an)
442
443 (* emit an integer store *)
444 and store(st,ea,r,mem,an) =
445 let val (b,d) =
446 case addr(0,ea)
447 of DISP(b, disp) =>
448 if im14 disp then (b,I.IMMED(toInt disp) )
449 else let val {hi,lo} = split11 disp
450 val tmp1 = newReg()
451 val tmp2 = newReg()
452 in emit(I.LDIL{i=I.IMMED hi,t=tmp1});
453 emit(I.ARITH{a=I.ADD,r1=b,r2=tmp1,t=tmp2});
454 (tmp2,I.IMMED lo)
455 end
456 | AMode(DISPea bd) => bd
457 | AMode(INDXea(r1,r2)) =>
458 let val tmp = newReg()
459 in emit(I.ARITH{a=I.ADD,r1=r1,r2=r2,t=tmp});
460 (tmp,I.IMMED 0)
461 end
462 | AMode(INDXSCALEDea _) => error "store"
463 in mark(I.STORE{st=st,b=b,d=d,r=r,mem=mem},an) end
464
465 (* emit a floating point load *)
466 and fload(scale,fl,flx,flxs,ea,t,mem,an) =
467 case addr(scale,ea) of
468 AMode(INDXea(b,x)) => mark(I.FLOADX{flx=flx,b=b,x=x,t=t,mem=mem},an)
469 | AMode(INDXSCALEDea(b,x)) =>
470 mark(I.FLOADX{flx=flxs,b=b,x=x,t=t,mem=mem},an)
471 | AMode(DISPea(b,d)) =>
472 let val tmp = newReg()
473 in emit(I.ARITHI{ai=I.ADDI,r=b,i=d,t=tmp});
474 mark(I.FLOADX{flx=flx,b=tmp,x=zeroR,t=t,mem=mem},an)
475 end
476 | DISP(b, d) =>
477 if im5 d then
478 mark(I.FLOAD{fl=fl,b=b,d=toInt d,t=t,mem=mem},an)
479 else
480 mark(I.FLOADX{flx=flx,b=b,x=immed d,t=t,mem=mem},an)
481
482 (* emit a floating point store *)
483 and fstore(scale,fst,fstx,fstxs,ea,data,mem,an) =
484 let val r = fexpr data
485 in case addr(scale,ea) of
486 DISP(b, d) =>
487 if im5 d then
488 mark(I.FSTORE{fst=fst,b=b,d=(toInt d),r=r,mem=mem},an)
489 else mark(I.FSTOREX{fstx=fstx,b=b,x=immed d,r=r,mem=mem},an)
490 | AMode(DISPea(b,d)) =>
491 let val tmp = newReg()
492 in emit(I.ARITHI{ai=I.ADDI,r=b,i=d,t=tmp});
493 mark(I.FSTORE{fst=I.FSTDS,b=tmp,d=0,r=r,mem=mem},an)
494 end
495 | AMode(INDXea(b,x)) =>
496 mark(I.FSTOREX{fstx=fstx,b=b,x=x,r=r,mem=mem},an)
497 | AMode(INDXSCALEDea(b,x)) =>
498 mark(I.FSTOREX{fstx=fstxs,b=b,x=x,r=r,mem=mem},an)
499 end
500
501 (* emit an integer branch instruction *)
502
503 (* generate a branch *)
504 and branch(T.CMP(ty,cc,T.LI n,e),lab,an) = (* optimize cmp immed *)
505 emitBranchCmpWithImmed(ty,cc,n,e,lab,an)
506 | branch(T.CMP(ty,cc,e1,e2 as T.LI _),lab,an) = (* commute *)
507 branch(T.CMP(ty,T.Basis.swapCond cc,e2,e1),lab,an)
508 | branch(T.CMP(ty,cc,a,b),lab,an) = (* do the usual *)
509 emitBranch(ty,cc,expr a,expr b,lab,an)
510 | branch(T.FCMP(fty,cc,a,b),lab,an) =
511 let val f1 = fexpr a
512 val f2 = fexpr b
513 val fallThrough = Label.anon()
514 fun fcond T.== = I.!=
515 | fcond T.?<> = I.==
516 | fcond T.? = I.<=>
517 | fcond T.<=> = I.?
518 | fcond T.> = I.?<=
519 | fcond T.>= = I.?<
520 | fcond T.?> = I.<=
521 | fcond T.?>= = I.<
522 | fcond T.< = I.?>=
523 | fcond T.<= = I.?>
524 | fcond T.?< = I.>=
525 | fcond T.?<= = I.>
526 | fcond T.<> = I.?=
527 | fcond T.?= = I.<>
528 | fcond _ = error "fcond"
529 in mark(I.FBRANCH{cc=fcond cc,f1=f1,f2=f2,t=lab,f=fallThrough,
530 fmt=getFmt a,n=true,long=false},an);
531 defineLabel fallThrough
532 end
533 | branch(e,lab,an) = error "branch: what is the semantics?"
534
535 (* generate a branch cmp with immed *)
536 and emitBranchCmpWithImmed(ty,cc,n,e2 as T.ANDB(_,e,T.LI mask),t,an) =
537 emitBranchOnBit(ty,cc,n,e2,e,toWord32 mask,t,an)
538 | emitBranchCmpWithImmed(ty,cc,n,e2 as T.ANDB(_,T.LI mask,e),t,an) =
539 emitBranchOnBit(ty,cc,n,e2,e,toWord32 mask,t,an)
540 | emitBranchCmpWithImmed(ty,cc,n,e2,t,an) =
541 emitBranchI(ty,cc,n,e2,t,an)
542
543 (* generate a branch on bit *)
544 and emitBranchOnBit(ty,cc,n,e2,e,mask,t,an) =
545 let fun isPowerOf2 w = W.andb(w,w-0w1) = 0w0
546 fun log w =
547 let fun f(0w1,n) = n
548 | f(w,n) = f(W.>>(w,0w1),n+1)
549 in f(w,0) end
550 val n' = toWord32 n
551 in if (n' = 0w0 orelse n' = mask) andalso
552 (cc = T.EQ orelse cc = T.NE) andalso
553 (mask > 0w0 andalso isPowerOf2 mask) then (* bit test! *)
554 let val bc =
555 case (cc,n') of
556 (T.EQ,0w0) => I.BCLR (* bit is 0 *)
557 | (T.EQ,_) => I.BSET (* bit is 1 *)
558 | (T.NE,0w0) => I.BSET (* bit is 1 *)
559 | (T.NE,_) => I.BCLR (* bit is 0 *)
560 | _ => error "emitBranchOnBit"
561 val f = Label.anon()
562 val bit = 31 - log mask
563 in mark(I.BB{bc=bc,r=expr e,p=bit,t=t,f=f,
564 n=false, nop=true},an);
565 defineLabel f
566 end
567 else
568 emitBranchI(ty,cc,n,e2,t,an)
569 end
570
571 (* generate a branch cmp with immediate *)
572 and emitBranchI(ty,cc,n,e2,t,an) =
573 let val r2 = expr e2
574 in if im5 n then
575 let val f = Label.anon()
576 val (cmpi,bc) =
577 case cc of
578 T.LT => (I.COMIBT, I.LT)
579 | T.LE => (I.COMIBT, I.LE)
580 | T.GT => (I.COMIBF, I.LE)
581 | T.GE => (I.COMIBF, I.LT)
582 | T.EQ => (I.COMIBT, I.EQ)
583 | T.LTU => (I.COMIBT, I.LTU)
584 | T.LEU => (I.COMIBT, I.LEU)
585 | T.GEU => (I.COMIBF, I.LTU)
586 | T.GTU => (I.COMIBF, I.LEU)
587 | T.NE => (I.COMIBF, I.EQ)
588 | _ => error "emitBranchI"
589 in mark(I.BCONDI{cmpi=cmpi,bc=bc,i=toInt(n),r2=r2,t=t,f=f,
590 n=false, nop=true},an);
591 defineLabel f
592 end
593 else emitBranch(ty,cc,immed n,r2,t,an)
594 end
595
596 (* generate a branch *)
597 and emitBranch(ty,cond,r1,r2,t,an) =
598 let val f = Label.anon()
599 val (cmp,bc,r1,r2) =
600 case cond of
601 T.LT => (I.COMBT, I.LT, r1, r2)
602 | T.LE => (I.COMBT, I.LE, r1, r2)
603 | T.GT => (I.COMBT, I.LT, r2, r1)
604 | T.GE => (I.COMBT, I.LE, r2, r1)
605 | T.EQ => (I.COMBT, I.EQ, r1, r2)
606 | T.LTU => (I.COMBT, I.LTU, r1, r2)
607 | T.LEU => (I.COMBT, I.LEU, r1, r2)
608 | T.GEU => (I.COMBT, I.LEU, r2, r1)
609 | T.GTU => (I.COMBT, I.LTU, r2, r1)
610 | T.NE => (I.COMBF, I.EQ, r1, r2)
611 | _ => error "emitBranch"
612 in mark(I.BCOND{cmp=cmp,bc=bc,r1=r1,r2=r2,t=t,f=f,
613 n=false,nop=true},an);
614 defineLabel f
615 end
616
617 and getFmt e =
618 case Gen.Size.fsize e of
619 32 => I.SGL
620 | 64 => I.DBL
621 | 128 => I.QUAD
622 | _ => error "getFmt"
623
624 and goto(l,an) = mark(I.B{lab=l,n=true},an)
625
626 (* generate code for a statement *)
627 and stmt(T.MV(32,t,e),an) = doExpr(e,t,an)
628 | stmt(T.FMV(64,t,e),an) = doFexpr(e,t,an)
629 | stmt(T.CCMV(t,e),an) = doCCexpr(e,t,an)
630 | stmt(T.COPY(32,dst,src),an) = copy(dst,src,an)
631 | stmt(T.FCOPY(64,dst,src),an) = fcopy(dst,src,an)
632 | stmt(T.JMP(T.LABEL l,_),an) = goto(l,an)
633 | stmt(T.JMP(ea,labs),an) = jmp(ea,labs,an)
634 | stmt(s as T.CALL { pops=0, ...},an) = call(s,an)
635 | stmt(T.CALL _, _) = error "pops<>0 not implemented"
636 | stmt(T.RET _,an) =
637 mark(I.BV{labs=[],x=zeroR,b=C.returnPtr,n=true},an)
638 | stmt(T.STORE(8,ea,t,mem),an) = store(I.STB,ea,expr t,mem,an)
639 | stmt(T.STORE(16,ea,t,mem),an) = store(I.STH,ea,expr t,mem,an)
640 | stmt(T.STORE(32,ea,t,mem),an) = store(I.STW,ea,expr t,mem,an)
641 | stmt(T.FSTORE(32,ea,t,mem),an) =
642 fstore(32,I.FSTWS,I.FSTWX,I.FSTWX_S,ea,t,mem,an)
643 | stmt(T.FSTORE(64,ea,t,mem),an) =
644 fstore(64,I.FSTDS,I.FSTDX,I.FSTDX_S,ea,t,mem,an)
645 | stmt(T.BCC(cc,lab),an) = branch(cc,lab,an)
646 | stmt(T.DEFINE l,_) = defineLabel l
647 | stmt(T.LIVE S,an) = mark'(I.LIVE{regs=cellset S,spilled=C.empty},an)
648 | stmt(T.KILL S,an) = mark'(I.KILL{regs=cellset S,spilled=C.empty},an)
649 | stmt(T.ANNOTATION(i,a),an) = stmt(i,a::an)
650 | stmt(T.EXT s,an) =
651 ExtensionComp.compileSext (reducer()) {stm=s, an=an}
652 | stmt(s,_) = doStmts(Gen.compileStm s)
653
654 and doStmt s = stmt(s,[])
655 and doStmts ss = app doStmt ss
656
657 and jmp(e,labs,an) = let
658 fun disp(r, i) = let
659 val b = newReg()
660 in emit(I.ARITHI{ai=I.ADDI, i=i, r=r, t=b});
661 (b, zeroR)
662 end
663
664 val (b,x) =
665 case addr(32,e) of
666 DISP(b, i) =>
667 if i = 0 then (b, zeroR) else disp(b, I.IMMED(toInt i))
668 | AMode(DISPea(r,i)) => disp(r, i)
669 | AMode(INDXea(r1,r2)) => let val b=newReg()
670 in emit(I.ARITH{a=I.ADD,r1=r1,r2=r2,t=b});
671 (b,zeroR)
672 end
673 | AMode(INDXSCALEDea(b,x)) => (b,x)
674 in mark(I.BV{b=b,x=x,n=true,labs=labs},an) end
675
676 and call(s,an) = let val reduce = {stm=doStmt, rexp=expr, emit=emitInstruction}
677 in LC.doCall(reduce,s) end
678
679 (* Optimize addition *)
680 and plus(times,sh1add,sh2add,sh3add,add,addi,a,b,t,an) =
681 case times a of
682 (TIMES2,a) => arith(sh1add,a,b,t,an)
683 | (TIMES4,a) => arith(sh2add,a,b,t,an)
684 | (TIMES8,a) => arith(sh3add,a,b,t,an)
685 | _ =>
686 case times b of
687 (TIMES2,b) => arith(sh1add,b,a,t,an)
688 | (TIMES4,b) => arith(sh2add,b,a,t,an)
689 | (TIMES8,b) => arith(sh3add,b,a,t,an)
690 | _ => commImmedArith(add,addi,a,b,t,an)
691
692 (* Round to zero for division:
693 * d <- r + i
694 * d <- if r >= 0 then r else d
695 *)
696 and divu32 x = Mulu32.divide{mode=T.TO_ZERO,stm=doStmt} x
697 and divs32 x = Muls32.divide{mode=T.TO_ZERO,stm=doStmt} x
698 and divt32 x = Mult32.divide{mode=T.TO_ZERO,stm=doStmt} x
699
700 and muldiv(ty,genConst,milliFn,a,b,t,commute,an) =
701 let fun const(a,i) =
702 let val r = expr a
703 in app emitInstruction (genConst{r=r,i=toInt i,d=t})
704 handle _ => milliCall(milliFn,T.REG(ty,r),T.LI i,t)
705 end
706 in case (commute,a,b) of
707 (_,a,T.LI i) => const(a,i)
708 | (true,T.LI i,a) => const(a,i)
709 | (_,a,b) => milliCall(milliFn,a,b,t)
710 end
711
712 (* compile shift *)
713 and shift(immedShift,varShift,e,T.LI n,t,an) = let
714 val n = toInt n
715 in
716 if n < 0 orelse n > 31 then error "shift"
717 else mark(I.SHIFT{s=immedShift,r=expr e,p=31-n,len=32-n,t=t},an)
718 end
719 | shift(immedShift,varShift,e1,e2,t,an) =
720 let val r1 = expr e1
721 val r2 = expr e2
722 val tmp = newReg()
723 in emit(I.ARITHI{ai=I.SUBI, i=I.IMMED 31, r=r2, t=tmp});
724 emit(I.MTCTL{r=tmp, t=CRReg 11});
725 mark(I.SHIFTV{sv=varShift,r=r1,len=32, t=t},an)
726 end
727
728 (* Generate a COMCLR_LDO/COMICLR_LDO instruction sequence:
729 * COMCLR,cond r1, r2, t1
730 * LDO i(b), t2
731 *
732 * Note:
733 * t <- if cond(r1,r2) then i else 0 can be mapped into:
734 *
735 * COMCLR,cond r1, r2, t
736 * LDO i(0), t
737 *
738 * if cond(r1,r2) then t <- e can be mapped into:
739 *
740 * t' <- e
741 * COMCLR,cond r1, r2, 0
742 * LDO 0(t'), t
743 *
744 * t <- if cond(r1,r2) then e1 else e2 can be mapped into:
745 *
746 * t <- e2
747 * t' <- e1
748 * COMCLR,cond r1, r2, 0
749 * LDO 0(t'), t
750 *)
751 and comclr(cond,x,y,yes,no,t,an) =
752 let val (cond, i1, r2) =
753 case (opn x, opn y) of
754 (x, I.REG r2) => (cond, x, r2)
755 | (I.REG r1, y) => (T.Basis.swapCond cond, y, r1)
756 | (x, y) => (cond, x, reduceOpn y)
757 val cc = case cond of
758 T.LT => I.GE
759 | T.LE => I.GT
760 | T.GT => I.LE
761 | T.GE => I.LT
762 | T.EQ => I.NE
763 | T.LTU => I.GEU
764 | T.LEU => I.GTU
765 | T.GEU => I.LTU
766 | T.GTU => I.LEU
767 | T.NE => I.EQ
768 | _ => error "comclr"
769 val tmp = newReg()
770 val (b,i) =
771 case yes
772 of T.LI n => if im14 n then (zeroR, toInt(n)) else
773 let val {hi,lo} = split11 n
774 val b = newReg()
775 in emit(I.LDIL{i=I.IMMED hi,t=b}); (b,lo) end
776 | e => (expr e, 0)
777 (*esac*)
778
779 val t1 =
780 case no
781 of T.LI z => (* false case is zero *)
782 if z = 0 then tmp else (doExpr(no,tmp,[]); zeroR)
783 | _ => (doExpr(no,tmp,[]); zeroR) (* move false case to tmp *)
784 (*esac*)
785
786 val instr =
787 case i1 of
788 I.REG r1 =>
789 I.COMCLR_LDO{cc=cc,r1=r1,r2=r2,b=b,i=i,t1=t1,t2=tmp}
790 | _ => I.COMICLR_LDO{cc=cc,i1=i1,r2=r2,b=b,i2=i,t1=t1,t2=tmp}
791 in mark(instr, an);
792 move(tmp, t, [])
793 end
794
795 (* convert an expression into a register *)
796 and expr(exp) = let
797 fun comp() = let
798 val t = newReg()
799 in doExpr(exp, t, []); t
800 end
801 in
802 case exp
803 of T.REG(_, r) => r
804 | T.LI z => if z = 0 then zeroR else comp()
805 | _ => comp()
806 end
807
808 (* compute an integer expression and put the result in register t *)
809 and doExpr(e,t,an) =
810 case e of
811 T.REG(_,r) => move(r,t,an)
812 | T.LI n => loadImmed(n,t,an)
813 | T.LABEXP le =>
814 (case ldLabelOpnd{label=le,pref=SOME t} of
815 I.REG r => move(r,t,an)
816 | opnd => mark(I.LDO{i=opnd,b=zeroR,t=t},an)
817 )
818 | T.CONST _ => loadConst(e,t,an)
819 | T.LABEL _ => loadConst(e,t,an)
820 | T.ADD(_,a,b) => plus(times,
821 I.SH1ADDL,I.SH2ADDL,I.SH3ADDL,I.ADD,I.ADDI,
822 a,b,t,an)
823 | T.SUB(_,a,T.LI mi) =>
824 if mi = 0 then doExpr(a,t,an)
825 else commImmedArith(I.ADD,I.ADDI,a,T.LI(T.I.NEGT(32,mi)),t,an)
826 | T.SUB(_,a,b) => immedArith(I.SUB,I.SUBI,a,b,t,an)
827 | T.ADDT(_,a,b) => plus(timest,
828 I.SH1ADDO,I.SH2ADDO,I.SH3ADDO,I.ADDO,I.ADDIO,
829 a,b,t,an)
830 | T.SUBT(_,a,T.LI n) =>
831 commImmedArith(I.ADDO,I.ADDIO,a,T.LI(T.I.NEGT(32,n)),t,an)
832 | T.SUBT(_,a,b) => immedArith(I.SUBO,I.SUBIO,a,b,t,an)
833
834 | T.ANDB(_,a,T.NOTB(_,b)) => arith(I.ANDCM,a,b,t,an)
835 | T.ANDB(_,T.NOTB(_,a),b) => arith(I.ANDCM,b,a,t,an)
836 | T.ANDB(_,a,b) => arith(I.AND,a,b,t,an)
837 | T.ORB(_,a,b) => arith(I.OR,a,b,t,an)
838 | T.XORB(_,a,b) => arith(I.XOR,a,b,t,an)
839
840 | T.SLL(_,a,b) => shift(I.ZDEP,I.ZVDEP,a,b,t,an)
841 | T.SRL(_,a,b) => shift(I.EXTRU,I.VEXTRU,a,b,t,an)
842 | T.SRA(_,a,b) => shift(I.EXTRS,I.VEXTRS,a,b,t,an)
843 | T.MULU(32,a,b) => muldiv(32,Mulu32.multiply,MC.mulu,a,b,t,true,an)
844 | T.MULS(32,a,b) => muldiv(32,Muls32.multiply,MC.mulu,a,b,t,true,an)
845 | T.MULT(32,a,b) => muldiv(32,Mult32.multiply,MC.mulo,a,b,t,true,an)
846 | T.DIVU(32,a,b) => muldiv(32,divu32,MC.divu,a,b,t,false,an)
847 (* FIXME: The following is a hack: We use the trapping div in place of
848 * the non-trapping since we currently expect that the non-trapping
849 * div will only be used where there is some high-level reasoning
850 * that the trapping div would in fact not trap. *)
851 | T.DIVS(T.DIV_TO_ZERO,32,a,b) =>
852 muldiv(32,divs32,MC.divo,a,b,t,false,an)
853 | T.DIVT(T.DIV_TO_ZERO,32,a,b) =>
854 muldiv(32,divt32,MC.divo,a,b,t,false,an)
855
856 | T.LOAD(8,ea,mem) => load(8,I.LDB,I.LDBX,I.LDBX,ea,t,mem,an)
857 | T.LOAD(16,ea,mem) => load(16,I.LDH,I.LDHX,I.LDHX_S,ea,t,mem,an)
858 | T.LOAD(32,ea,mem) => load(32,I.LDW,I.LDWX,I.LDWX_S,ea,t,mem,an)
859
860 | T.COND(_,T.CMP(_,cond,x,y),yes,no) => comclr(cond,x,y,yes,no,t,an)
861 | T.LET(s,e) => (doStmt s; doExpr(e, t, an))
862 | T.MARK(e,A.MARKREG f) => (f t; doExpr(e,t,an))
863 | T.MARK(e,a) => doExpr(e,t,a::an)
864 | T.PRED(e,c) => doExpr(e,t,A.CTRLUSE c::an)
865 | T.REXT e =>
866 ExtensionComp.compileRext (reducer()) {e=e, rd=t, an=an}
867 | e => doExpr(Gen.compileRexp e,t,an)
868
869 (* convert an expression into a floating point register *)
870 and fexpr(T.FREG(_,r)) = r
871 | fexpr e = let val t = newFreg()
872 in doFexpr(e,t,[]); t end
873
874 (* compute a floating point expression and put the result in t *)
875 and doFexpr(e,t,an) =
876 case e of
877 (* single precision *)
878 T.FREG(32,r) => fmove(r,t,an)
879 | T.FLOAD(32,ea,mem) =>
880 fload(32,I.FLDWS,I.FLDWX,I.FLDWX_S,ea,t,mem,an)
881 | T.FADD(32,a,b) => farith(I.FADD_S,a,b,t,an)
882 | T.FSUB(32,a,b) => farith(I.FSUB_S,a,b,t,an)
883 | T.FMUL(32,a,b) => farith(I.FMPY_S,a,b,t,an)
884 | T.FDIV(32,a,b) => farith(I.FDIV_S,a,b,t,an)
885 | T.FABS(32,a) => funary(I.FABS_S,a,t,an)
886 | T.FSQRT(32,a) => funary(I.FSQRT_S,a,t,an)
887
888 (* double precision *)
889 | T.FREG(64,r) => fmove(r,t,an)
890 | T.FLOAD(64,ea,mem) =>
891 fload(64,I.FLDDS,I.FLDDX,I.FLDDX_S,ea,t,mem,an)
892 | T.FADD(64,a,b) => farith(I.FADD_D,a,b,t,an)
893 | T.FSUB(64,a,b) => farith(I.FSUB_D,a,b,t,an)
894 | T.FMUL(64,a,b) => farith(I.FMPY_D,a,b,t,an)
895 | T.FDIV(64,a,b) => farith(I.FDIV_D,a,b,t,an)
896 | T.FABS(64,a) => funary(I.FABS_D,a,t,an)
897 | T.FSQRT(64,a) => funary(I.FSQRT_D,a,t,an)
898
899 (* conversions *)
900 | T.CVTF2F(fty,fty',e) =>
901 (case (fty,fty') of
902 (64,32) => fcnv(I.FCNVFF_SD,e,t,an)
903 | (32,64) => fcnv(I.FCNVFF_DS,e,t,an)
904 | (32,32) => doFexpr(e,t,an)
905 | (64,64) => doFexpr(e,t,an)
906 | _ => error "CVTF2F"
907 )
908 | T.CVTI2F(32,_,e) => app emitInstruction (MilliCode.cvti2s{rs=expr e,fd=t})
909 | T.CVTI2F(64,_,e) => app emitInstruction (MilliCode.cvti2d{rs=expr e,fd=t})
910
911 (* negation is implemented as subtraction *)
912 | T.FNEG(ty,a) => doFexpr(T.FSUB(ty,T.FREG(ty,zeroF),a),t,an)
913
914 | T.FMARK(e,A.MARKREG f) => (f t; doFexpr(e,t,an))
915 | T.FMARK(e,a) => doFexpr(e,t,a::an)
916 | T.FPRED(e,c) => doFexpr(e,t,A.CTRLUSE c::an)
917 | T.FEXT e =>
918 ExtensionComp.compileFext (reducer()) {e=e, fd=t, an=an}
919 | e => error "doFexpr"
920
921 and doCCexpr(T.CC(_,r),t,an) = move(r,t,an)
922 | doCCexpr(T.FCC(_,r),t,an) = move(r,t,an)
923 | doCCexpr(T.CMP(ty,cond,e1,e2),t,an) = error "doCCexpr"
924 | doCCexpr(T.CCMARK(e,A.MARKREG f),t,an) = (f t; doCCexpr(e,t,an))
925 | doCCexpr(T.CCMARK(e,a),t,an) = doCCexpr(e,t,a::an)
926 | doCCexpr(T.CCEXT e,t,an) =
927 ExtensionComp.compileCCext (reducer()) {e=e,ccd=t,an=an}
928 | doCCexpr e = error "doCCexpr"
929
930 and ccExpr(T.CC(_,r)) = r
931 | ccExpr(T.FCC(_,r)) = r
932 | ccExpr e = let val t = newReg() in doCCexpr(e,t,[]); t end
933
934 (* convert an expression into an operand *)
935 and opn(c as T.CONST _) = I.LabExp(c,I.F)
936 | opn(l as T.LABEL _) = I.LabExp(l,I.F)
937 | opn(T.LABEXP le) = ldLabelOpnd{label=le,pref=NONE}
938 | opn(e as T.LI n) = if im11 n then I.IMMED(toInt n)
939 else I.REG(expr e)
940 | opn e = I.REG(expr e)
941
942 and addrOf e =
943 case addr(0, e)
944 of AMode mode => mode
945 | DISP(r, mi) => DISPea(r, I.IMMED(toInt mi))
946
947 and reducer() =
948 TS.REDUCER{reduceRexp = expr,
949 reduceFexp = fexpr,
950 reduceCCexp = ccExpr,
951 reduceStm = stmt,
952 operand = opn,
953 reduceOperand = reduceOpn,
954 addressOf = addrOf,
955 emit = mark',
956 instrStream = instrStream,
957 mltreeStream = self()
958 }
959
960 (* convert mlrisc to cellset:
961 * condition code registers are mapped onto general registers
962 *)
963 and cellset mlrisc =
964 let fun g([],acc) = acc
965 | g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc))
966 | g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc))
967 | g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,C.addReg(cc,acc))
968 | g(_::regs, acc) = g(regs, acc)
969 in g(mlrisc, C.empty) end
970
971 and self() =
972 TS.S.STREAM
973 { beginCluster = beginCluster,
974 endCluster = endCluster,
975 emit = doStmt,
976 pseudoOp = pseudoOp,
977 defineLabel = defineLabel,
978 entryLabel = entryLabel,
979 comment = comment,
980 annotation = annotation,
981 getAnnotations = getAnnotations,
982 exitBlock = fn regs => exitBlock(cellset regs)
983 }
984 in self()
985 end
986
987
988 end