Debian Codegraph @

lib/mlrisc-lib/MLRISC/demo/demo-backend.sml

raw
1 (*
2 * This functor factors out the machine independent part of the register
3 * allocator. This works well for RISC machines; not applicable to x86.
4 *)
5 functor BackEnd
6 (structure MLTreeComp : MLTREECOMP (* instruction selection *)
7 structure Flowgraph : FLOWGRAPH
8 structure InsnProps : INSN_PROPERTIES
9 structure Asm : INSTRUCTION_EMITTER
10 structure RA : CLUSTER_OPTIMIZATION
11 sharing InsnProps.I = Flowgraph.I = Asm.I = MLTreeComp.I
12 sharing Asm.P = Flowgraph.P = MLTreeComp.T.PseudoOp
13 sharing RA.F = Flowgraph
14 ) : sig
15 structure T : MLTREE
16 structure C : CELLS
17 val codegen : Label.label * T.stm list -> unit
18 end =
19 struct
20
21 structure F = Flowgraph
22 structure T = MLTreeComp.T
23 structure I = F.I
24 structure C = I.C
25 structure Stream = T.Stream
26
27 (* Flowgraph generation *)
28 structure FlowGraphGen =
29 ClusterGen(structure Flowgraph = F
30 structure InsnProps = InsnProps
31 structure MLTree = MLTreeComp.T
32 )
33
34 (* Assembly output *)
35 structure Assembler =
36 ClusterEmit(structure F = F
37 structure E = Asm)
38
39 fun error msg = MLRiscErrorMsg.error("BackEnd",msg)
40
41 (* How to compile a cluster *)
42 fun compile cluster =
43 let val cluster = RA.run cluster (* just run register allocation *)
44 in Assembler.asmEmit cluster (* and output the assembly *)
45 end
46
47 fun codegen(functionName, mltreeStms) =
48 let val _ =
49 (* initialize all hidden states first *)
50 Label.reset(); (* okay, just the label counters *)
51 (*
52 * Extract the methods from a stream
53 *)
54 val stream as Stream.STREAM
55 { beginCluster, (* start a cluster *)
56 endCluster, (* end a cluster *)
57 emit, (* emit MLTREE stm *)
58 defineLabel, (* define a local label *)
59 entryLabel, (* define an external entry *)
60 exitBlock, (* mark the end of a procedure *)
61 pseudoOp, (* emit a pseudo op *)
62 annotation, (* add an annotation *)
63 ... } =
64 MLTreeComp.selectInstructions
65 (FlowGraphGen.newStream{compile=compile, flowgraph=NONE})
66 in beginCluster 0; (* start a new cluster *)
67 entryLabel functionName; (* define the entry label *)
68 app emit mltreeStms; (* emit all the statements *)
69 (* IMPORTANT: normally you'll have to call the other methods too *)
70 endCluster [] (* end the cluster *)
71 end
72 end