Debian Codegraph @

lib/mlrisc-lib/MLRISC/instructions/cells.sml

raw
1 (*
2 * Description of cell and other updatable cells.
3 *
4 * -- Allen.
5 *)
6
7 (*
8 * This functor is applied to create the cells structure for an architecture
9 *)
10 functor Cells
11 (exception Cells
12 val firstPseudo : int
13 val cellKindDescs : (CellsBasis.cellkind * CellsBasis.cellkindDesc) list
14 val cellSize : int
15 ) : CELLS =
16 struct
17
18 open CellsBasis
19
20 exception Cells = Cells
21
22 val i2s = Int.toString
23
24 fun error msg = MLRiscErrorMsg.error(exnName Cells, msg)
25
26 val cellkinds = map (fn (kind,_) => kind) cellKindDescs
27 val firstPseudo = firstPseudo
28 val maxDedicatedCells = 256
29 val firstName = firstPseudo + maxDedicatedCells
30 val name = ref firstName
31 (* val cellCounter = name *)
32
33 val _ = app (fn (_, desc as DESC{physicalRegs, high, low, ...}) =>
34 let val n = high - low + 1
35 in if n <= 0 then ()
36 else let val a = Array.tabulate(n, fn nth =>
37 let val reg = nth + low
38 in CELL{id=reg, col=ref(MACHINE reg),
39 an=ref [], desc=desc}
40 end)
41 in physicalRegs := a
42 end
43 end) cellKindDescs
44
45 fun nextName() = let val id = !name in name := !name + 1; id end
46
47 fun desc(k:cellkind) =
48 let fun loop [] = error("missing info for "^cellkindToString k)
49 | loop((kind,info)::defs) =
50 if kind = k then info else loop defs
51 in loop cellKindDescs end
52
53 val cellkindDesc = desc
54
55 fun cellRange k =
56 let val DESC{low,high,...} = desc k
57 in {low=low,high=high} end
58
59 fun Reg k = let
60 val desc as DESC{low,kind,physicalRegs,...} = desc k
61 in
62 fn nth => (Array.sub(!physicalRegs,nth) handle _ => raise Cells)
63 end
64
65 fun Regs k =
66 let val Reg = Reg k
67 fun loop{from, to, step} =
68 if from > to then []
69 else Reg from :: loop{from=from+step, to=to, step=step}
70 in loop end
71
72 fun Cell k =
73 let val desc as DESC{low,kind,physicalRegs,...} = desc k
74 in fn reg =>
75 Array.sub(!physicalRegs,reg - low) handle _ => raise Cells
76 end
77
78 val GPReg = Reg GP
79 val FPReg = Reg FP
80
81 (* Counters *)
82 fun newCell k =
83 let val desc as DESC{counter,...} = desc k
84 in fn _ =>
85 let val r = !name
86 in name := r + 1;
87 counter := !counter + 1;
88 CELL{id=r, col=ref PSEUDO, an=ref [], desc=desc}
89 end
90 end
91
92 local val desc as DESC{counter, ...} = desc GP
93 in fun newReg _ =
94 let val r = !name
95 in name := r + 1;
96 counter := !counter + 1;
97 CELL{id=r, col=ref PSEUDO, an=ref [], desc=desc}
98 end
99 end
100
101 local val desc as DESC{counter, ...} = desc FP
102 in fun newFreg _ =
103 let val r = !name
104 in name := r + 1;
105 counter := !counter + 1;
106 CELL{id=r, col=ref PSEUDO, an=ref [], desc=desc}
107 end
108 end
109
110 fun newDedicatedCell k =
111 let val desc as DESC{dedicated,...} = desc k
112 in fn _ =>
113 let val d = !dedicated
114 in dedicated := d + 1;
115 if d >= maxDedicatedCells then
116 error "too many dedicated cells"
117 else
118 CELL{id=firstPseudo+d, col=ref PSEUDO, an=ref [], desc=desc}
119 end
120 end
121
122 fun newVar (CELL{desc, an, ...}) =
123 let val r = !name
124 in name := r + 1;
125 CELL{id=r, col=ref PSEUDO, an=ref(!an), desc=desc}
126 end
127
128 fun cloneCell c =
129 let val CELL{desc, an, col, ...} = chase c
130 val r = !name
131 in name := r + 1;
132 CELL{id=r, col=ref(!col), an=ref(!an), desc=desc}
133 end
134
135 fun numCell k = let val DESC{counter, ...} = desc k
136 in fn () => !counter end
137
138 fun maxCell() = !name
139
140 fun reset() =
141 (app (fn (_,DESC{counter, ...}) => counter := 0) cellKindDescs;
142 name := firstName
143 )
144
145 type cellset = CellSet.cellset
146 val empty = CellSet.empty
147 fun getCellsByKind (k : cellkind) = CellSet.get (desc k)
148 fun updateCellsByKind (k : cellkind) = CellSet.update (desc k)
149 val getReg = getCellsByKind GP
150 val getFreg = getCellsByKind FP
151 val addReg = CellSet.add
152 val addFreg = CellSet.add
153 val rmvReg = CellSet.rmv
154 val rmvFreg = CellSet.rmv
155
156 (* Misc *)
157 fun zeroReg k =
158 let val desc as DESC{zeroReg, physicalRegs, low, ...} = desc k
159 in case zeroReg of
160 NONE => NONE
161 | SOME r => SOME(Array.sub(!physicalRegs, r))
162 end
163
164 fun defaultValues k =
165 let val DESC{defaultValues, ...} = desc k
166 in defaultValues end
167
168 (* dummy values for now; these get redefined for each architecture *)
169 val stackptrR = GPReg 0
170 val asmTmpR = GPReg 0
171 val fasmTmp = FPReg 0
172 val cellSize = cellSize
173 end