Debian Codegraph @

lib/mlrisc-lib/MLRISC/c-call/gen/c-call-gen-fn.sml

raw
1 (* c-call-gen-fn.sml
2 *
3 * Generate MLRISC code for moving arguments to and from machine locations.
4 *)
5
6 functor CCallGenFn (
7 structure T : MLTREE
8 structure C : CELLS
9 (* given an offset constant, return an expression that gives an offset from
10 * the run-time stack pointer
11 *)
12 val offSp : T.I.machine_int -> T.rexp
13 (* we assume that the address width is the same *)
14 val wordTy : int
15 (* extract least significant bits *)
16 val lobits : {nBits : int, width : int, e : T.rexp} -> T.rexp
17 (* sign extend the expression *)
18 val sx : {fromWidth : int, toWidth : int, e : T.rexp} -> T.rexp
19 (* float to float conversion (change width) *)
20 val f2f : {fromWidth : int, toWidth : int, e : T.fexp} -> T.fexp
21
22 structure SA : STAGED_ALLOCATION
23 where type reg_id = T.reg
24 where type loc_kind = CLocKind.loc_kind
25
26 ) : C_CALL_GEN = struct
27
28 structure K = CLocKind
29 structure T = T
30 structure C = C
31 structure SA = SA
32
33 fun concatMap f ls = List.concat(List.map f ls)
34
35 datatype c_arg
36 = ARG of T.rexp
37 (* rexp specifies integer or pointer; if the
38 * corresponding parameter is a C struct, then
39 * this argument is the address of the struct.
40 *)
41 | FARG of T.fexp
42 (* fexp specifies floating-point argument *)
43
44 fun copyToReg (mty, r, e) = let
45 val tmp = C.newReg ()
46 in
47 [T.COPY (mty, [r], [tmp]), T.MV (mty, tmp, e)]
48 end
49
50 fun copyToFReg (mty, r, e) = let
51 val tmp = C.newFreg ()
52 in
53 [T.FCOPY (mty, [r], [tmp]), T.FMV (mty, tmp, e)]
54 end
55
56 val stack = T.Region.stack
57
58 fun litInt i = T.I.fromInt(wordTy, i)
59 val lit = T.LI o litInt
60 val offSp = offSp o litInt
61
62 (* returns any general-purpose register IDs used in a machine location *)
63 fun gprsOfLoc (SA.REG (_, K.GPR, r)) = [r]
64 | gprsOfLoc (SA.COMBINE (l1, l2)) = gprsOfLoc l1 @ gprsOfLoc l2
65 | gprsOfLoc (SA.NARROW (l, _, K.GPR)) = gprsOfLoc l
66 | gprsOfLoc _ = []
67
68 (* returns any floating-point register IDs used in a machine location *)
69 fun fprsOfLoc (SA.REG (w, K.FPR, r)) = [(w, r)]
70 | fprsOfLoc (SA.COMBINE (l1, l2)) = fprsOfLoc l1 @ fprsOfLoc l2
71 | fprsOfLoc (SA.NARROW (l, _, K.FPR)) = fprsOfLoc l
72 | fprsOfLoc _ = []
73
74 (* eliminate redundant narrows, i.e., narrow.32(r1.32) == r1.32 *)
75 fun elimNarrow (loc as SA.NARROW(SA.REG(wr, kr, r), wn, kn)) =
76 if kr = kn andalso wr = wn
77 then SA.REG(wr, kr, r)
78 else loc
79 | elimNarrow (loc as SA.NARROW(SA.BLOCK_OFFSET(wb, kb, offset), wn, kn)) =
80 if kb = kn andalso wb = wn
81 then SA.BLOCK_OFFSET(wb, kb, offset)
82 else loc
83 | elimNarrow (SA.COMBINE(l1, l2)) = SA.COMBINE(elimNarrow l1, elimNarrow l2)
84 | elimNarrow loc = loc
85
86 (* write a C argument (non aggregate) to a machine location
87 * - arg is the argument data
88 * - off is an offset into the argument data
89 * - loc is the machine location
90 * - stms is the accumulator of machine instructions
91 *)
92 fun writeLoc arg (off, loc, stms) = (
93 case (arg, loc)
94 of (ARG (e as T.REG _), SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) =>
95 (* register to stack (gpr) *)
96 T.STORE(wordTy, offSp offset, e, stack) :: stms
97 | (ARG (e as T.REG _), SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', (K.GPR | K.STK))) =>
98 (* register to stack with width conversion (gpr) *)
99 T.STORE(w, offSp offset, sx{fromWidth=w', toWidth=w, e=e}, stack) :: stms
100 | (ARG (T.LOAD (ty, e, rgn)), SA.REG (w, K.GPR, r)) =>
101 (* memory to register (gpr) *)
102 copyToReg(w, r, T.LOAD (ty, T.ADD(wordTy, e, off), rgn)) @ stms
103 | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.REG (w, K.GPR, r), w', K.GPR)) =>
104 (* memory to register with conversion (gpr) *)
105 copyToReg(w, r, sx{fromWidth=w', toWidth=w, e=T.LOAD (w', T.ADD(wordTy, e, off), rgn)}) @ stms
106 | (ARG e, SA.REG (w, K.GPR, r)) =>
107 (* expression to register *)
108 copyToReg(w, r, e) @ stms
109 | (ARG e, SA.NARROW (SA.REG(w, K.GPR, r), w', K.GPR)) =>
110 (* expression to register with conversion *)
111 copyToReg(w, r, sx{fromWidth=w', toWidth=w, e=e}) @ stms
112 | (ARG (T.LOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) => let
113 (* memory to stack (gpr) *)
114 val tmp = C.newReg ()
115 in
116 T.STORE (ty, offSp offset, T.REG (ty, tmp), stack) ::
117 T.MV (ty, tmp, T.LOAD (ty, T.ADD(wordTy, e, off), rgn)) :: stms
118 end
119 | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', K.GPR)) => let
120 (* memory to stack with conversion (gpr) *)
121 val tmp = C.newReg ()
122 in
123 T.STORE (w, offSp offset, T.REG (w, tmp), stack) ::
124 T.MV (w, tmp, sx{fromWidth=w', toWidth=w, e=T.LOAD (w', T.ADD(wordTy, e, off), rgn)}) :: stms
125 end
126 | (ARG e, SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) => let
127 (* expression to stack (gpr) *)
128 val tmp = C.newReg ()
129 in
130 T.STORE (w, offSp offset, T.REG (w, tmp), stack) :: T.MV (w, tmp, e) :: stms
131 end
132 | (ARG e, SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', K.GPR)) => let
133 (* expression to stack with conversion (gpr) *)
134 val tmp = C.newReg ()
135 in
136 T.STORE (w, offSp offset, T.REG (w, tmp), stack) :: T.MV (w, tmp, sx{fromWidth=w', toWidth=w, e=e}) :: stms
137 end
138 | (FARG (e as T.FREG _), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) =>
139 (* register to stack (fpr) *)
140 T.FSTORE (w, offSp offset, e, stack) :: stms
141 | (FARG e, SA.REG(w, K.FPR, r)) =>
142 (* expression to register (fpr) *)
143 copyToFReg(w, r, e) @ stms
144 | (FARG e, SA.NARROW(SA.REG(w, K.FPR, r), w', K.FPR)) =>
145 (* expression to register with conversion (fpr) *)
146 copyToFReg(w', r, f2f{fromWidth=w, toWidth=w', e=e}) @ stms
147 | (ARG (T.LOAD (ty, e, rgn)), SA.REG(w, K.FPR, r)) =>
148 (* memory to register (fpr) *)
149 copyToFReg(w, r, T.FLOAD (ty, T.ADD(wordTy, e, off), rgn)) @ stms
150 | (ARG (T.LOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
151 (* memory to stack (fpr) *)
152 val tmp = C.newFreg ()
153 in
154 T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) ::
155 T.FMV (w, tmp, T.FLOAD (ty, T.ADD(wordTy, e, off), rgn)) :: stms
156 end
157 | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
158 (* memory to stack with conversion (fpr) *)
159 val tmp = C.newFreg ()
160 in
161 T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) ::
162 T.FMV (w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FLOAD (w', T.ADD(wordTy, e, off), rgn)}) :: stms
163 end
164 | (FARG (T.FLOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
165 (* memory to stack (fpr) *)
166 val tmp = C.newFreg ()
167 in
168 T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) ::
169 T.FMV (w, tmp, T.FLOAD (w, T.ADD(wordTy, e, off), rgn)) :: stms
170 end
171 | (FARG (T.FLOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
172 (* memory to stack with conversion (fpr) *)
173 val tmp = C.newFreg ()
174 in
175 T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) ::
176 T.FMV (w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FLOAD (w, T.ADD(wordTy, e, off), rgn)}) :: stms
177 end
178 | (FARG e, SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
179 (* expression to stack (fpr) *)
180 val tmp = C.newFreg ()
181 in
182 T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: T.FMV (w, tmp, e) :: stms
183 end
184 | (FARG e, SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
185 (* expression to stack (fpr) *)
186 val tmp = C.newFreg ()
187 in
188 T.FSTORE (w', offSp offset, f2f{fromWidth=w, toWidth=w', e=T.FREG (w, tmp)}, stack) :: T.FMV (w, tmp, e) :: stms
189 end
190 | (FARG _, SA.COMBINE _) =>
191 raise Fail ""
192 | _ => raise Fail "invalid arg / loc pair"
193 (* end case *))
194
195 (* write a C argument (possibly an aggregate) to some parameter locations *)
196 fun writeLocs' (arg, locs, stms) = let
197 val locs = List.map elimNarrow locs
198 (* offsets of the members of the struct *)
199 val membOffs = List.tabulate(List.length locs, fn i => lit(i*8))
200 in
201 ListPair.foldl (writeLoc arg) stms (membOffs, locs)
202 end
203
204 (* write C arguments to parameter locations; also return any used GPRs and FPRs *)
205 fun writeLocs (args, argLocs) = let
206 val gprs = concatMap gprsOfLoc (List.concat argLocs)
207 val fprs = concatMap fprsOfLoc (List.concat argLocs)
208 val instrs = ListPair.foldl writeLocs' [] (args, argLocs)
209 in
210 (List.rev instrs, gprs, fprs)
211 end
212
213 (* read from a machine location *)
214 fun readLoc (loc, (resultRegs, copyResult)) = (
215 case elimNarrow loc
216 of SA.REG(w, K.GPR, r) => let
217 (* register (gpr) *)
218 val tmpR = C.newReg()
219 in
220 (T.GPR(T.REG(w, tmpR)) :: resultRegs, T.COPY(w, [tmpR], [r]) :: copyResult)
221 end
222 | SA.NARROW(loc, w', K.GPR) => let
223 (* conversion (gpr) *)
224 val ([resultReg as T.GPR(T.REG(_, tmp))], copyResult') = readLoc(loc, ([], []))
225 val w = SA.width loc
226 in
227 (T.GPR(T.REG(w', tmp)) :: resultRegs,
228 T.MV(w, tmp, lobits{nBits=w', width=w, e=T.REG (w, tmp)}) :: copyResult' @ copyResult)
229 end
230 | SA.REG(w, K.FPR, r) => let
231 val resReg = C.newFreg()
232 in
233 (T.FPR(T.FREG(w, resReg)) :: resultRegs, T.FCOPY(w, [resReg], [r]) :: copyResult)
234 end
235 | SA.NARROW(loc, w', K.FPR) => let
236 (* conversion (fpr) *)
237 val ([resultReg as T.FPR(T.FREG(_, tmp))], copyResult') = readLoc(loc, ([], []))
238 val w = SA.width loc
239 in
240 (T.FPR(T.FREG(w', tmp)) :: resultRegs,
241 T.FMV(w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FREG(w', tmp)}) :: copyResult' @ copyResult)
242 end
243 | SA.COMBINE (l1, l2) => (
244 case (readLoc (l1, ([], [])), readLoc (l2, ([], [])))
245 of ( ([T.GPR e1], instrs1), ([T.GPR e2], instrs2) ) => let
246 val w = SA.width loc
247 val w' = SA.width l2
248 val tmp = C.newReg()
249 in
250 (T.GPR(T.REG(w, tmp)) :: resultRegs,
251 T.MV(w, tmp, T.ADD(w, T.SLL(w, lit w', e1), e2)) ::
252 instrs1 @ instrs2 @ copyResult)
253 end
254 (* end case *))
255 | _ => raise Fail "bogus read location"
256 (* end case *))
257
258 (* read from some machine locations *)
259 fun readLocs locs = let
260 val (resultRegs, copyResult) = List.foldl readLoc ([], []) locs
261 in
262 (List.rev resultRegs, List.rev copyResult)
263 end
264
265 end (* CCallGenFn *)