Debian Codegraph @

lib/mlrisc-lib/MLRISC/c-call/test/c-sparc-test.sml

raw
1 local
2
3 (*---------------------------------------------------------------------------
4 * First, some front-end dependent stuff. Typically, you only need
5 * one instance of these things for each source language.
6 *---------------------------------------------------------------------------*)
7
8 (*
9 * User defined constant type. Dummy for now.
10 * In practice, you'll want to use this type to implement constants with
11 * values that cannot be determined until final code generation, e.g.
12 * stack frame offset.
13 *)
14 structure UserConst =
15 struct
16 type const = int
17 val toString = Int.toString
18 fun hash _ = 0w0
19 fun valueOf _ = 0
20 fun == _ = true
21 end
22
23 (*
24 * Instantiate label expressions with respect to user defined constants.
25 * This type is somewhat misnamed; it is used to represent constant
26 * expressions.
27 *)
28 (* structure LabelExp = LabelExp(UserConst) *)
29
30 (*
31 * User defined datatype for representing aliasing. Dummy for now.
32 * You'll need this to represent aliasing information.
33 *)
34 structure UserRegion =
35 struct
36 type region = unit
37 fun toString () = ""
38 val memory = ()
39 val stack = ()
40 val readonly = ()
41 val spill = ()
42 end
43
44 (*
45 * User defined datatype for representing pseudo assembly operators.
46 * Dummy for now.
47 *
48 * You'll need this to represent assembler directives.
49 *)
50 structure UserPseudoOps =
51 struct
52 type pseudo_op = unit
53 fun toString () = ""
54 fun emitValue _ = ()
55 fun sizeOf _ = 0
56 fun adjustLabels _ = true
57 end
58
59
60 (*
61 * Instruction stream datatype.
62 * This is just a simple record type used by MLRISC to represent
63 * instruction streams.
64 *)
65 (*structure Stream = InstructionStream(UserPseudoOps)*)
66
67 (*
68 * Client defined extensions. None for now.
69 * You'll need this only if you need to extend the set of MLTREE operators
70 *)
71 structure UserExtension =
72 struct
73
74 type ('s,'r,'f,'c) sx = ('s,'r,'f,'c) SparcInstrExt.sext
75 type ('s,'r,'f,'c) rx = unit
76 type ('s,'r,'f,'c) fx = unit
77 type ('s,'r,'f,'c) ccx = unit
78
79 end
80
81 structure SparcMLTree =
82 MLTreeF (structure Constant = UserConst
83 structure Region = UserRegion
84 structure Extension = UserExtension)
85
86
87 (*---------------------------------------------------------------------------
88 * Backend specific stuff. You'll need one instance of these things
89 * for each architecture.
90 *---------------------------------------------------------------------------*)
91
92 (*
93 * The Sparc instruction set, specialized with respect to the
94 * user constant and region types.
95 *)
96 structure SparcInstr = SparcInstr
97 (SparcMLTree
98 )
99
100 (*
101 * How to serialize parallel copies
102 *)
103 structure SparcShuffle = SparcShuffle(SparcInstr)
104
105 structure SparcMLTreeEval =
106 MLTreeEval (structure T = SparcMLTree
107 fun eq _ _ = false
108 val eqRext = eq val eqFext = eq
109 val eqCCext = eq val eqSext = eq)
110
111 functor SparcPseudoOpsFn (
112 structure T : MLTREE
113 structure MLTreeEval : MLTREE_EVAL where T = T
114 ) : PSEUDO_OPS_BASIS = SparcGasPseudoOps (
115 structure T = SparcMLTree
116 structure MLTreeEval = SparcMLTreeEval)
117
118 structure SparcPseudoOps = SparcPseudoOpsFn(
119 structure T = SparcMLTree
120 structure MLTreeEval = SparcMLTreeEval)
121
122 structure PseudoOps =
123 struct
124
125 structure Client =
126 struct
127 structure AsmPseudoOps = SparcPseudoOps
128 type pseudo_op = unit
129
130 fun toString () = ""
131
132 fun emitValue _ = raise Fail "todo"
133 fun sizeOf _ = raise Fail "todo"
134 fun adjustLabels _ = raise Fail "todo"
135 end (* Client *)
136
137 structure PseudoOps = PseudoOps (structure Client = Client)
138 end
139
140 structure SparcStream = InstructionStream(PseudoOps.PseudoOps)
141 structure SparcMLTreeStream =
142 MLTreeStream
143 (structure T = SparcMLTree
144 structure S = SparcStream)
145
146 (*
147 * The assembler
148 *)
149 structure SparcAsm = SparcAsmEmitter
150 (structure Instr = SparcInstr
151 structure Stream = SparcStream
152 structure Shuffle = SparcShuffle
153 structure S = SparcStream
154 structure MLTreeEval = SparcMLTreeEval
155 val V9 = false (* we'll generate V8 instructions for now *)
156 )
157
158 structure SparcPseudoInstrs =
159 struct
160 structure I = SparcInstr
161 structure C = I.C
162
163 type format1 =
164 {r:CellsBasis.cell, i:I.operand, d:CellsBasis.cell} *
165 (I.operand -> CellsBasis.cell) -> I.instruction list
166
167 type format2 =
168 {i:I.operand, d:CellsBasis.cell} *
169 (I.operand -> CellsBasis.cell) -> I.instruction list
170
171 fun error msg = MLRiscErrorMsg.impossible ("SparcPseudoInstrs."^msg)
172
173 val delta = 0 (*SparcSpec.framesize*) (* initial value of %fp - %sp *)
174
175 (* runtime system dependent; the numbers are relative to %sp but
176 * we need offsets relative to %fp, hence the adjustment by delta *)
177 val floatTmpOffset = I.IMMED (88 - delta)
178 val umulOffset = I.IMMED (80 - delta)
179 val smulOffset = I.IMMED (72 - delta)
180 val udivOffset = I.IMMED (84 - delta)
181 val sdivOffset = I.IMMED (76 - delta)
182
183 val stack = () (*CPSRegions.stack*)
184
185 val native = true (* use native versions of the instructions? *)
186
187 fun umul_native({r, i, d}, reduceOpnd) =
188 [I.arith{a=I.UMUL,r=r,i=i,d=d}]
189
190 val TNE = I.ticc{t=I.BNE,cc=I.ICC,r=C.r0,i=I.IMMED 7}
191 val TVS = I.ticc{t=I.BVS,cc=I.ICC,r=C.r0,i=I.IMMED 7}
192
193 (* overflows iff Y != (d ~>> 31) *)
194 fun smult_native({r, i, d}, reduceOpnd) =
195 let val t1 = C.newReg()
196 val t2 = C.newReg()
197 in [I.arith{a=I.SMUL,r=r,i=i,d=d},
198 I.shift{s=I.SRA,r=d,i=I.IMMED 31,d=t1},
199 I.rdy{d=t2},
200 I.arith{a=I.SUBCC,r=t1,i=I.REG t2,d=C.r0},
201 TNE
202 ]
203 end
204
205 fun smul_native({r, i, d}, reduceOpnd) =
206 [I.arith{a=I.SMUL,r=r,i=i,d=d}]
207
208 fun udiv_native({r,i,d},reduceOpnd) =
209 [I.wry{r=C.r0,i=I.REG C.r0},
210 I.arith{a=I.UDIV,r=r,i=i,d=d}]
211
212 (* May overflow if MININT div -1 *)
213 fun sdivt_native({r,i,d},reduceOpnd) =
214 let val t1 = C.newReg()
215 in [I.shift{s=I.SRA,r=r,i=I.IMMED 31,d=t1},
216 I.wry{r=t1,i=I.REG C.r0},
217 I.arith{a=I.SDIVCC,r=r,i=i,d=d},
218 TVS
219 ]
220 end
221
222 fun sdiv_native({r,i,d},reduceOpnd) =
223 let val t1 = C.newReg()
224 in [I.shift{s=I.SRA,r=r,i=I.IMMED 31,d=t1},
225 I.wry{r=t1,i=I.REG C.r0},
226 I.arith{a=I.SDIV,r=r,i=i,d=d}
227 ]
228 end
229
230 (*
231 * Registers %o2, %o3 are used to pass arguments to ml_mul and ml_div
232 * Result is returned in %o2.
233 *)
234 val r10 = C.GPReg 10
235 val r11 = C.GPReg 11
236
237 fun callRoutine(offset,reduceOpnd,r,i,d) =
238 let val addr = C.newReg()
239 val defs = C.addReg(r10,C.empty)
240 val uses = C.addReg(r10,C.addReg(r11,C.empty))
241 fun copy{dst, src, tmp} =
242 I.COPY{k=CellsBasis.GP, sz=32, dst=dst, src=src, tmp=tmp}
243 in
244 [copy{src=[r,reduceOpnd i],dst=[r10,r11],tmp=SOME(I.Direct(C.newReg()))},
245 I.load{l=I.LD,r=C.frameptrR,i=offset,d=addr,mem=stack},
246 I.jmpl{r=addr,i=I.IMMED 0,d=C.linkReg,defs=defs,uses=uses,
247 cutsTo=[],nop=true,mem=stack},
248 copy{src=[r10],dst=[d],tmp=NONE}
249 ]
250 end
251
252 fun umul({r, i, d}, reduceOpnd) = callRoutine(umulOffset,reduceOpnd,r,i,d)
253 fun smultrap({r, i, d}, reduceOpnd) = callRoutine(smulOffset,reduceOpnd,r,i,d)
254 fun udiv({r, i, d}, reduceOpnd) = callRoutine(udivOffset,reduceOpnd,r,i,d)
255 fun sdivtrap({r, i, d}, reduceOpnd) = callRoutine(sdivOffset,reduceOpnd,r,i,d)
256
257 fun cvti2d({i, d}, reduceOpnd) =
258 [I.store{s=I.ST,r=C.frameptrR,i=floatTmpOffset,d=reduceOpnd i,mem=stack},
259 I.fload{l=I.LDF,r=C.frameptrR,i=floatTmpOffset,d=d,mem=stack},
260 I.fpop1{a=I.FiTOd,r=d,d=d}
261 ]
262 fun cvti2s _ = error "cvti2s"
263 fun cvti2q _ = error "cvti2q"
264
265 (* Generate native versions of the instructions *)
266 val umul32 = if native then umul_native else umul
267 val smul32 : format1 =
268 if native then smul_native else (fn _ => error "smul32")
269 val smul32trap = if native then smult_native else smultrap
270 val udiv32 = if native then udiv_native else udiv
271 val sdiv32 : format1 =
272 if native then sdiv_native else (fn _ => error "sdiv32")
273 val sdiv32trap = if native then sdivt_native else sdivtrap
274
275 val overflowtrap32 = (* tvs 0x7 *)
276 [I.ticc{t=I.BVS,cc=I.ICC,r=C.r0,i=I.IMMED 7}]
277 val overflowtrap64 = [] (* not needed *)
278
279 fun save (r, opnd :SparcInstr.operand, d) = [I.save{r=r, i=opnd, d=d}]
280 fun restore (r, opnd :SparcInstr.operand, d) = [I.restore{r=r, i=opnd, d=d}]
281
282 end
283
284 structure SparcMLTreeHash =
285 MLTreeHash
286 (structure T = SparcMLTree
287 fun h _ _ = 0w0
288 val hashRext = h val hashFext = h
289 val hashCCext = h val hashSext = h)
290
291 structure SparcProps =
292 SparcProps
293 (structure SparcInstr = SparcInstr
294 structure MLTreeEval = SparcMLTreeEval
295 structure MLTreeHash = SparcMLTreeHash)
296
297 structure SparcAsmEmitter =
298 SparcAsmEmitter(structure Instr=SparcInstr
299 structure Shuffle=SparcShuffle
300 structure S = SparcStream
301 structure MLTreeEval=SparcMLTreeEval
302 val V9 = false)
303
304
305 structure SparcCFG =
306 ControlFlowGraph
307 (structure I = SparcInstr
308 structure PseudoOps = SparcPseudoOps
309 structure GraphImpl = DirectedGraph
310 structure InsnProps = SparcProps
311 structure Asm = SparcAsmEmitter)
312
313 structure SparcFlowGraph = BuildFlowgraph
314 (structure Props = SparcProps
315 structure Stream = SparcStream
316 structure CFG = SparcCFG)
317
318 structure SparcExpand = CFGExpandCopies (structure CFG=SparcCFG
319 structure Shuffle = SparcShuffle)
320 structure SparcBlockPlacement = DefaultBlockPlacement(SparcCFG)
321
322 structure SparcEmit = CFGEmit (
323 structure CFG = SparcCFG
324 structure E = SparcAsmEmitter)
325
326 structure SparcCCall = SparcCCallFn (
327 structure T = SparcMLTree
328 fun ix x = raise Fail "")
329
330 (*
331 * This module controls how we handle user extensions. Since we don't
332 * have any yet. This is just a bunch of dummy routines.
333 *)
334 structure SparcMLTreeExtComp : MLTREE_EXTENSION_COMP =
335 struct
336 structure TS = SparcMLTreeStream
337 structure I = SparcInstr
338 structure T = SparcMLTree
339 structure C = I.C
340 structure Ext = UserExtension
341 structure CFG = SparcCFG
342 structure SparcCompInstrExt =
343 SparcCompInstrExt(structure I = I structure CFG = CFG structure TS=SparcMLTreeStream)
344
345 type reducer =
346 (I.instruction,C.cellset,I.operand,I.addressing_mode, CFG.cfg) TS.reducer
347 fun unimplemented _ = MLRiscErrorMsg.impossible "SparcMLTreeExtComp"
348
349 val compileSext = SparcCompInstrExt.compileSext
350 val compileRext = unimplemented
351 val compileCCext = unimplemented
352 val compileFext = unimplemented
353 end
354
355 structure MLTreeComp=
356 Sparc(structure SparcInstr = SparcInstr
357 structure SparcMLTree = SparcMLTree
358 structure PseudoInstrs = SparcPseudoInstrs
359 structure ExtensionComp = SparcMLTreeExtComp
360 val V9 = false
361 val muluCost = ref 5
362 val multCost = ref 3
363 val divuCost = ref 5
364 val divtCost = ref 5
365 val registerwindow = ref false
366 val useBR = ref false
367 )
368
369
370 structure InsnProps = SparcProps
371
372 structure RA =
373 RISC_RA
374 (structure I = SparcInstr
375 structure C = CellsBasis
376 structure T = SparcMLTree
377 structure CFG = SparcCFG
378 structure InsnProps = InsnProps
379 structure Rewrite = SparcRewrite(SparcInstr)
380 structure SpillInstr= SparcSpillInstr(SparcInstr)
381 structure Asm = SparcAsmEmitter
382 structure SpillHeur = ChaitinSpillHeur
383 structure Spill = RASpill(structure InsnProps = InsnProps
384 structure Asm = SparcAsmEmitter)
385
386 structure SpillTable = SpillTable(val initialSpillOffset = 0 (* This is probably wrong!!!!! *)
387 val spillAreaSz = 4000
388 val architecture = "Sparc" )
389 val fp = I.C.frameptrR
390 val spill = UserRegion.spill
391 datatype spillOperandKind = SPILL_LOC | CONST_VAL
392 type spill_info = unit
393 fun beforeRA _ = SpillTable.beginRA()
394
395 val architecture = "Sparc"
396
397 fun pure(I.ANNOTATION{i,...}) = pure i
398 | pure(I.INSTR(I.LOAD _)) = true
399 | pure(I.INSTR(I.FLOAD _)) = true
400 | pure(I.INSTR(I.SETHI _)) = true
401 | pure(I.INSTR(I.SHIFT _)) = true
402 | pure(I.INSTR(I.FPop1 _)) = true
403 | pure(I.INSTR(I.FPop2 _)) = true
404 | pure _ = false
405
406 (* make copy *)
407 structure Int =
408 struct
409 val dedicated = [I.C.stackptrR, I.C.GPReg 0]
410 val avail =
411 C.SortedCells.return
412 (C.SortedCells.difference(
413 C.SortedCells.uniq(
414 SparcCells.Regs C.GP {from=0, to=31, step=1}),
415 C.SortedCells.uniq dedicated)
416 )
417
418 fun mkDisp loc = T.LI(T.I.fromInt(32, SpillTable.get loc))
419 fun spillLoc{info, an, cell, id} =
420 {opnd=I.Displace{base=fp, disp=mkDisp(RAGraph.FRAME id), mem=spill},
421 kind=SPILL_LOC}
422
423 val mode = RACore.NO_OPTIMIZATION
424 end
425
426 structure Float =
427 struct
428 fun fromto(n, m, inc) = if n>m then [] else n :: fromto(n+inc, m, inc)
429 val avail = SparcCells.Regs C.FP {from=0, to=30, step=2}
430 val dedicated = []
431
432 fun mkDisp loc = T.LI(T.I.fromInt(32, SpillTable.getF loc))
433
434 fun spillLoc(S, an, loc) =
435 I.Displace{base=fp, disp=mkDisp(RAGraph.FRAME loc), mem=spill}
436
437 val mode = RACore.NO_OPTIMIZATION
438 end
439 )
440
441 structure Cells = SparcInstr.C
442 structure T = SparcMLTree
443 structure CFG = SparcCFG
444 structure FlowGraph = SparcFlowGraph
445 val wordTy = 32
446
447
448 val GP = SparcCells.GPReg
449 val FP = SparcCells.FPReg
450
451 fun greg r = GP r
452 fun oreg r = GP (r + 8)
453 fun ireg r = GP (r + 24)
454 fun freg r = FP r
455 fun reg32 r = T.REG (32, r)
456 fun freg64 r = T.FREG (64, r)
457 fun LI i = T.LI (T.I.fromInt (32, i))
458
459
460
461 in
462
463 structure SparcMLTree = SparcMLTree
464 structure SparcCCall = SparcCCall
465
466 structure SparcMLRISCGen =
467 struct
468
469 fun gen (functionName, stms, result) = let
470 val insnStrm = FlowGraph.build()
471 val stream as SparcStream.STREAM
472 { beginCluster, (* start a cluster *)
473 endCluster, (* end a cluster *)
474 emit, (* emit MLTREE stm *)
475 defineLabel, (* define a local label *)
476 entryLabel, (* define an external entry *)
477 exitBlock, (* mark the end of a procedure *)
478 pseudoOp, (* emit a pseudo op *)
479 annotation, (* add an annotation *)
480 ... } =
481 MLTreeComp.selectInstructions insnStrm
482 fun doit () = (
483 beginCluster 0; (* start a new cluster *)
484 pseudoOp PseudoOpsBasisTyp.TEXT;
485 pseudoOp (PseudoOpsBasisTyp.EXPORT [functionName]);
486 entryLabel functionName; (* define the entry label *)
487 List.app emit stms; (* emit all the statements *)
488 exitBlock result;
489 endCluster [])
490 val cfg = doit ()
491 val cfg = RA.run cfg
492 val cfg = SparcExpand.run cfg
493 in
494 (cfg, stream) (* end the cluster *)
495 end
496
497 fun dumpOutput (cfg, stream) = let
498 val (cfg as Graph.GRAPH graph, blocks) =
499 SparcBlockPlacement.blockPlacement cfg
500 val CFG.INFO{annotations=an, data, decls, ...} = #graph_info graph
501 in
502 SparcEmit.asmEmit (cfg, blocks)
503 end (* dumpOutput *)
504
505 val GP = SparcCells.GPReg
506 val FP = SparcCells.FPReg
507 fun greg r = GP r
508 fun oreg r = GP (r + 8)
509 fun ireg r = GP (r + 24)
510 fun freg r = FP r
511 fun reg32 r = T.REG (32, r)
512 fun freg64 r = T.FREG (64, r)
513 fun LI i = T.LI (T.I.fromInt (32, i))
514 val sp = oreg 6
515
516 fun codegen (functionName, target, proto, initStms, args) = let
517 val _ = Label.reset()
518
519 val [functionName, target] = List.map Label.global [functionName, target]
520
521 (* construct the C call *)
522 val {result, callseq} = SparcCCall.genCall {
523 name=T.LABEL target,
524 paramAlloc=fn _ => false,
525 (* FIXME *)
526 structRet=fn _ => T.REG(32, SparcCells.GPReg 0),
527 saveRestoreDedicated=fn _ => {save=[], restore=[]},
528 callComment=NONE,
529 proto=proto,
530 args=args}
531
532 fun wordLit i = T.LI (T.I.fromInt (wordTy, i))
533
534 fun offp i = T.ADD(32, T.REG (32, ireg 6), LI i)
535
536 val stms = List.concat [
537 [T.EXT(SparcInstrExt.SAVE(T.REG(32, sp), LI(~112), T.REG(32, sp)))],
538 initStms,
539 callseq,
540 [T.EXT(SparcInstrExt.RESTORE(T.REG(32, greg 0), T.REG(32, greg 0), T.REG(32, greg 0)))],
541 [T.JMP(T.ADD(32, T.REG(32, oreg 7), LI 8), [])]
542 ]
543
544 (* val _ = List.all (fn stm => ChkTy.check stm
545 orelse raise Fail ("typechecking error: "^SparcMTC.SparcMLTreeUtils.stmToString stm))
546 stms
547 *)
548
549 in
550 dumpOutput(gen (functionName, stms, result))
551 end
552
553 end
554
555 structure SparcTest = GenTestFn (
556 structure T = SparcMLTree
557 structure CCall = SparcCCall
558 structure Cells = SparcCells
559 val codegen = SparcMLRISCGen.codegen
560 val param0 = reg32(ireg 0)
561 val wordTy = 32)
562 end