Debian Codegraph @

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

raw
1 (*
2 * The Alpha instruction set, specialized with respect to the
3 * user constant and region types.
4 *)
5
6 structure AlphaMLTree =
7 MLTreeF
8 (structure Constant = UserConstant
9 structure Region = UserRegion
10 structure Extension = UserExtension
11 )
12
13 structure AlphaInstr = AlphaInstr(AlphaMLTree)
14
15 structure AlphaMLTreeEval =
16 MLTreeEval
17 (structure T = AlphaMLTree
18 fun eq _ _ = false
19 val eqRext = eq val eqFext = eq
20 val eqCCext = eq val eqSext = eq
21 )
22
23 structure AlphaMLTreeHash =
24 MLTreeHash
25 (structure T = AlphaMLTree
26 fun h _ _ = 0w0
27 val hashRext = eq val hashFext = eq
28 val hashCCext = eq val hashSext = eq
29 )
30
31 (*
32 * How to serialize parallel copies
33 *)
34 structure AlphaShuffle = AlphaShuffle(AlphaInstr)
35
36 (*
37 * The assembler
38 *)
39 structure AlphaAsm = AlphaAsmEmitter
40 (structure Instr = AlphaInstr
41 structure Stream = Stream
42 structure Shuffle = AlphaShuffle
43 val V9 = false (* we'll generate V8 instructions for now *)
44 )
45
46 (*
47 * The flowgraph (cluster) representation specialized to the sparc instruction
48 * set.
49 *)
50 structure AlphaFlowGraph =
51 FlowGraph(structure I = AlphaInstr
52 structure P = UserPseudoOps
53 )
54 (*
55 * Alpha has no integer division. So they have to be handled specially.
56 * The following is stolen from Fermin's C-- source code.
57 *)
58 structure AlphaPseudoInstrs : ALPHA_PSEUDO_INSTR =
59 struct
60
61 fun error msg = MLRiscErrorMsg.error ("AlphaPseudoInstrs", msg)
62
63 structure I = AlphaInstr
64 structure T = MLTree
65 structure C = I.C
66
67 type reduceOpnd = I.operand -> C.cell
68
69 (* reduceOpnd moves the operand to a register if it's not in one
70 already (handy).
71 div*, rem* are assembler macros. The alpha/osf assembler accepts
72 divl $1, 7, $1
73 but the alpha/linux assembler insists that the operand be a register
74 Sigh ...
75 *)
76
77 val temps = foldr C.addReg C.empty (map C.GPReg [23, 24, 25, 26, 28])
78
79 fun pseudoArith instr ({ra, rb, rc}, reduceOpnd) =
80 [I.PSEUDOARITH{oper=instr, ra=ra, rb=I.REGop(reduceOpnd rb), rc=rc, tmps=temps}]
81
82 fun divl operands = pseudoArith I.DIVL operands
83 fun divlu operands = pseudoArith I.DIVLU operands
84 fun divq operands = pseudoArith I.DIVQ operands
85 fun divqu operands = pseudoArith I.DIVQU operands
86 fun divlv _ = error "divlv"
87 fun divqv _ = error "divqv"
88
89 fun reml operands = pseudoArith I.REML operands
90 fun remlu operands = pseudoArith I.REMLU operands
91 fun remq operands = pseudoArith I.REMQ operands
92 fun remqu operands = pseudoArith I.REMQU operands
93 fun remlv _ = error "remlv"
94 fun remqv _ = error "remqv"
95
96 val stack = I.Region.stack
97 val sp = C.stackptrR
98
99 val push16 = I.LDA{r=sp, b=sp, d=I.IMMop (~16)}
100 val pop16 = I.LDA{r=sp, b=sp, d=I.IMMop 16}
101
102 (**** int to float ****)
103
104 (* i32 -> f32 *)
105 fun cvtls({opnd, fd}, reduceOpnd) =
106 let val ra = reduceOpnd opnd
107 in
108 [push16,
109 I.STORE{stOp=I.STQ, r=ra, b=sp, d=I.IMMop 0, mem=stack},
110 I.FLOAD{ldOp=I.LDT, r=fd, b=sp, d=I.IMMop 0, mem=stack},
111 pop16,
112 I.FUNARY{oper=I.CVTQS, fb=fd, fc=fd}]
113 end
114
115 (* i32 -> f64 *)
116 fun cvtlt({opnd, fd}, reduceOpnd) =
117 let val ra = reduceOpnd opnd
118 in
119 [push16,
120 I.STORE{stOp=I.STQ, r=ra, b=sp, d=I.IMMop 0, mem=stack},
121 I.FLOAD{ldOp=I.LDT, r=fd, b=sp, d=I.IMMop 0, mem=stack},
122 pop16,
123 I.FUNARY{oper=I.CVTQT, fb=fd, fc=fd}]
124 end
125
126 (* i64 -> f32 *)
127 val cvtqs = cvtls
128
129 (* i64 -> f64 *)
130 val cvtqt = cvtlt
131
132 (**** float to int ****)
133
134 (* TODO: These should really look at the rounding mode, and not generate
135 CVTTQ_C blindly *)
136
137 (* f32 -> i32 *)
138 fun cvtsl({mode, fs, rd}) = let
139 val ftmp = AlphaCells.newFreg()
140 in
141 [I.FUNARY{oper=I.CVTTQC, fb=fs, fc=ftmp},
142 push16,
143 I.FSTORE{stOp=I.STT, r=ftmp, b=sp, d=I.IMMop 0, mem=stack},
144 I.LOAD {ldOp=I.LDL, r=rd, b=sp, d=I.IMMop 0, mem=stack},
145 pop16
146 ]
147 end
148
149 (* f64 -> i32 *)
150 val cvttl= cvtsl
151
152
153 (* f32 -> i64 *)
154 fun cvtsq({mode, fs, rd}) = let
155 val ftmp = AlphaCells.newFreg()
156 in
157 [I.FUNARY{oper=I.CVTTQC, fb=fs, fc=ftmp},
158 push16,
159 I.FSTORE{stOp=I.STT, r=ftmp, b=sp, d=I.IMMop 0, mem=stack},
160 I.LOAD {ldOp=I.LDQ, r=rd, b=sp, d=I.IMMop 0, mem=stack},
161 pop16
162 ]
163 end
164
165 (* f64 -> i64 *)
166 val cvttq = cvtsq
167
168
169 end (* AlphaPseudoInstrs *)
170
171 (*
172 * Instruction selection module for Alpha.
173 *)
174 structure AlphaMLTreeComp =
175 Alpha(structure AlphaInstr = AlphaInstr
176 structure AlphaMLTree = MLTree
177 structure PseudoInstrs = AlphaPseudoInstrs
178 structure ExtensionComp = UserMLTreeExtComp
179 (structure I = AlphaInstr
180 structure T = AlphaMLTree
181 )
182 (* Some alpha specific parameters *)
183 val mode32bit = false (* simulate 32 bit mode *)
184 val multCost = ref 8 (* just guessing *)
185 val useMultByConst = ref false (* just guessing *)
186 val byteWordLoadStores = ref false
187 val SMLNJfloatingPoint = false (* must be true for SML/NJ *)
188 )
189
190
191 (*
192 * Alpha specific backend
193 *)
194 structure AlphaBackEnd =
195 BackEnd
196 (structure I = AlphaInstr
197 structure Flowgraph = AlphaFlowGraph
198 structure InsnProps = AlphaProps(AlphaInstr)
199 structure Asm = AlphaAsm
200 structure MLTreeComp = AlphaMLTreeComp
201
202 val sp = I.C.stackptrR
203 val spill = UserRegion.spill
204
205 (* I'm assuming only r31 and the stack pointer is dedicated *)
206 structure RA =
207 RISC_RA
208 (structure I = I
209 structure C = I.C
210 structure Flowgraph = Flowgraph
211 structure Asm = Asm
212 structure Rewrite = AlphaRewrite(AlphaInstr)
213 structure InsnProps = InsnProps
214 structure Spill = RASpill(structure Asm = Asm
215 structure InsnProps = InsnProps)
216 structure SpillHeur = ChaitinSpillHeur
217 structure SpillTable =
218 SpillTable
219 (val initialSpillOffset = 0 (* This is probably wrong!!!!! *)
220 val spillAreaSz = 4000
221 val architecture = "Alpha"
222 )
223
224 open SpillTable
225
226 fun pure _ = false
227
228 (* make copies *)
229 structure Int =
230 struct
231 val dedicated = [I.C.stackptrR, I.C.GPReg 31]
232 val avail =
233 C.SortedCells.return(
234 C.SortedCells.difference(
235 C.SortedCells.uniq(
236 C.Regs C.GP {from=0, to=31, step=1}),
237 C.SortedCells.uniq dedicated))
238
239 fun copy((rds as [_], rss as [_]), _) =
240 I.COPY{dst=rds, src=rss, impl=ref NONE, tmp=NONE}
241 | copy((rds, rss), I.COPY{tmp, ...}) =
242 I.COPY{dst=rds, src=rss, impl=ref NONE, tmp=tmp}
243 (* spill register *)
244 fun spillInstr{an,src,spilledCell,spillLoc} =
245 [I.STORE{stOp=I.STL, b=sp, d=I.IMMop(get spillLoc),
246 r=src, mem=spill}]
247
248 (* spill copy temp *)
249 fun spillCopyTmp(_,I.COPY{k,tmp,dst,src,impl},loc) =
250 I.COPY{k=k,tmp=SOME(I.Displace{base=sp, disp=get loc}),
251 dst=dst,src=src,impl=impl}
252
253 (* reload register *)
254 fun reloadInstr{an,dst,spilledCell,spillLoc} =
255 [I.LOAD{ldOp=I.LDL, b=sp, d=I.IMMop(get spillLoc), r=dst,
256 mem=spill}]
257 end
258
259 structure Float =
260 struct
261 val dedicated = [I.C.FPReg 31]
262 val avail = C.Regs C.FP {from=0, to=30, step=1}
263
264 fun copy((fds as [_], fss as [_]), _) =
265 I.FCOPY{dst=fds, src=fss, impl=ref NONE, tmp=NONE}
266 | copy((fds, fss), I.FCOPY{tmp, ...}) =
267 I.FCOPY{dst=fds, src=fss, impl=ref NONE, tmp=tmp}
268
269 fun spillCopyTmp(_,I.FCOPY{tmp,dst,src,impl},loc) =
270 I.FCOPY{tmp=SOME(I.Displace{base=sp, disp=getF loc}),
271 dst=dst,src=src,impl=impl}
272 fun spillInstr(_,r,loc) =
273 [I.FSTORE{stOp=I.STT, b=sp, d=I.IMMop(getF loc), r=r, mem=spill}]
274
275 fun reloadInstr(_,r,loc) =
276 [I.FLOAD{ldOp=I.LDT, b=sp, d=I.IMMop(getF loc), r=r, mem=spill}]
277 end
278 )
279 )
280