File: c-call-gen-fn.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (265 lines) | stat: -rw-r--r-- 10,729 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
(* c-call-gen-fn.sml
 *
 * Generate MLRISC code for moving arguments to and from machine locations.
 *)

functor CCallGenFn (
    structure T : MLTREE
    structure C : CELLS
  (* given an offset constant, return an expression that gives an offset from
   * the run-time stack pointer 
   *)
    val offSp : T.I.machine_int -> T.rexp
  (* we assume that the address width is the same *)
    val wordTy : int
  (* extract least significant bits *)
    val lobits : {nBits : int, width : int, e : T.rexp} -> T.rexp
  (* sign extend the expression *)
    val sx : {fromWidth : int, toWidth : int, e : T.rexp} -> T.rexp
  (* float to float conversion (change width) *)
    val f2f : {fromWidth : int, toWidth : int, e : T.fexp} -> T.fexp
		 
    structure SA : STAGED_ALLOCATION
          where type reg_id = T.reg
          where type loc_kind = CLocKind.loc_kind

  ) : C_CALL_GEN = struct

    structure K = CLocKind
    structure T = T
    structure C = C
    structure SA = SA

    fun concatMap f ls = List.concat(List.map f ls)

    datatype c_arg 
      = ARG of T.rexp	
	  (* rexp specifies integer or pointer; if the 
           * corresponding parameter is a C struct, then 
	   * this argument is the address of the struct. 
	   *)
      | FARG of T.fexp
	  (* fexp specifies floating-point argument *)

    fun copyToReg (mty, r, e) = let
	val tmp = C.newReg ()
        in
	    [T.COPY (mty, [r], [tmp]), T.MV (mty, tmp, e)]
        end

    fun copyToFReg (mty, r, e) = let
	val tmp = C.newFreg ()
        in
	    [T.FCOPY (mty, [r], [tmp]), T.FMV (mty, tmp, e)] 
        end

    val stack = T.Region.stack

    fun litInt i = T.I.fromInt(wordTy, i)
    val lit = T.LI o litInt
    val offSp = offSp o litInt

  (* returns any general-purpose register IDs used in a machine location *)
    fun gprsOfLoc (SA.REG (_, K.GPR, r)) = [r]
      | gprsOfLoc (SA.COMBINE (l1, l2)) = gprsOfLoc l1 @ gprsOfLoc l2
      | gprsOfLoc (SA.NARROW (l, _, K.GPR)) = gprsOfLoc l
      | gprsOfLoc _ = []

  (* returns any floating-point register IDs used in a machine location *)
    fun fprsOfLoc (SA.REG (w, K.FPR, r)) = [(w, r)]
      | fprsOfLoc (SA.COMBINE (l1, l2)) = fprsOfLoc l1 @ fprsOfLoc l2
      | fprsOfLoc (SA.NARROW (l, _, K.FPR)) = fprsOfLoc l
      | fprsOfLoc _ = []

  (* eliminate redundant narrows, i.e., narrow.32(r1.32) == r1.32 *)
    fun elimNarrow (loc as SA.NARROW(SA.REG(wr, kr, r), wn, kn)) =
	  if kr = kn andalso wr = wn
	     then SA.REG(wr, kr, r)
	  else loc
      | elimNarrow (loc as SA.NARROW(SA.BLOCK_OFFSET(wb, kb, offset), wn, kn)) =
	  if kb = kn andalso wb = wn
	     then SA.BLOCK_OFFSET(wb, kb, offset)
	  else loc
      | elimNarrow (SA.COMBINE(l1, l2)) = SA.COMBINE(elimNarrow l1, elimNarrow l2)
      | elimNarrow loc = loc

    (* write a C argument (non aggregate) to a machine location
     *   - arg is the argument data
     *   - off is an offset into the argument data
     *   - loc is the machine location
     *   - stms is the accumulator of machine instructions
     *)
    fun writeLoc arg (off, loc, stms) = (
	  case (arg, loc)
	   of (ARG (e as T.REG _), SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) =>
	      (* register to stack (gpr) *)
	      T.STORE(wordTy, offSp offset, e, stack) :: stms
	    | (ARG (e as T.REG _), SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', (K.GPR | K.STK))) =>
	      (* register to stack with width conversion (gpr) *)
	      T.STORE(w, offSp offset, sx{fromWidth=w', toWidth=w, e=e}, stack) :: stms
	    | (ARG (T.LOAD (ty, e, rgn)), SA.REG (w, K.GPR, r)) =>
	      (* memory to register (gpr) *)
	      copyToReg(w, r, T.LOAD (ty, T.ADD(wordTy, e, off), rgn)) @ stms
	    | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.REG (w, K.GPR, r), w', K.GPR)) =>
	      (* memory to register with conversion (gpr) *)
	      copyToReg(w, r, sx{fromWidth=w', toWidth=w, e=T.LOAD (w', T.ADD(wordTy, e, off), rgn)}) @ stms
	    | (ARG e, SA.REG (w, K.GPR, r)) =>
	      (* expression to register *)
	      copyToReg(w, r, e) @ stms
	    | (ARG e, SA.NARROW (SA.REG(w, K.GPR, r), w', K.GPR)) => 
	      (* expression to register with conversion *)
	      copyToReg(w, r, sx{fromWidth=w', toWidth=w, e=e}) @ stms
	    | (ARG (T.LOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) => let
	      (* memory to stack (gpr) *)
		val tmp = C.newReg ()
	        in
		  T.STORE (ty, offSp offset, T.REG (ty, tmp), stack) :: 
		  T.MV (ty, tmp, T.LOAD (ty, T.ADD(wordTy, e, off), rgn)) :: stms
	        end
	    | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', K.GPR)) => let
	      (* memory to stack with conversion (gpr) *)
		val tmp = C.newReg ()
	        in
		  T.STORE (w, offSp offset, T.REG (w, tmp), stack) :: 
		  T.MV (w, tmp, sx{fromWidth=w', toWidth=w, e=T.LOAD (w', T.ADD(wordTy, e, off), rgn)}) :: stms
	        end
	    | (ARG e, SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset)) => let
	      (* expression to stack (gpr) *)
		val tmp = C.newReg ()
	        in
		  T.STORE (w, offSp offset, T.REG (w, tmp), stack) :: T.MV (w, tmp, e) :: stms
	        end
	    | (ARG e, SA.NARROW(SA.BLOCK_OFFSET(w, (K.GPR | K.STK), offset), w', K.GPR)) => let
	      (* expression to stack with conversion (gpr) *)
		val tmp = C.newReg ()
	        in
		  T.STORE (w, offSp offset, T.REG (w, tmp), stack) :: T.MV (w, tmp, sx{fromWidth=w', toWidth=w, e=e}) :: stms
	        end
	    | (FARG (e as T.FREG _), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) =>
	      (* register to stack (fpr) *)
	      T.FSTORE (w, offSp offset, e, stack) :: stms
	    | (FARG e, SA.REG(w, K.FPR, r)) => 
	      (* expression to register (fpr) *)
	      copyToFReg(w, r, e) @ stms
	    | (FARG e, SA.NARROW(SA.REG(w, K.FPR, r), w', K.FPR)) => 
	      (* expression to register with conversion (fpr) *)
	      copyToFReg(w', r, f2f{fromWidth=w, toWidth=w', e=e}) @ stms
	    | (ARG (T.LOAD (ty, e, rgn)), SA.REG(w, K.FPR, r)) =>
	      (* memory to register (fpr) *)
	      copyToFReg(w, r, T.FLOAD (ty, T.ADD(wordTy, e, off), rgn)) @ stms
	    | (ARG (T.LOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
              (* memory to stack (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: 
		  T.FMV (w, tmp, T.FLOAD (ty, T.ADD(wordTy, e, off), rgn)) :: stms
	        end
	    | (ARG (T.LOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
              (* memory to stack with conversion (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: 
		  T.FMV (w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FLOAD (w', T.ADD(wordTy, e, off), rgn)}) :: stms
	        end
	    | (FARG (T.FLOAD (ty, e, rgn)), SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
              (* memory to stack (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: 
		  T.FMV (w, tmp, T.FLOAD (w, T.ADD(wordTy, e, off), rgn)) :: stms
	        end
	    | (FARG (T.FLOAD (ty, e, rgn)), SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
              (* memory to stack with conversion (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: 
		  T.FMV (w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FLOAD (w, T.ADD(wordTy, e, off), rgn)}) :: stms
	        end
	    | (FARG e, SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset)) => let
              (* expression to stack (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w, offSp offset, T.FREG (w, tmp), stack) :: T.FMV (w, tmp, e) :: stms
	        end
	    | (FARG e, SA.NARROW(SA.BLOCK_OFFSET(w, (K.FPR | K.FSTK), offset), w', K.FPR)) => let
              (* expression to stack (fpr) *)
		val tmp = C.newFreg ()
	        in
		  T.FSTORE (w', offSp offset, f2f{fromWidth=w, toWidth=w', e=T.FREG (w, tmp)}, stack) :: T.FMV (w, tmp, e) :: stms
	        end
	    | (FARG _, SA.COMBINE _) => 
	      raise Fail ""
	    | _ => raise Fail "invalid arg / loc pair"
          (* end case *))

  (* write a C argument (possibly an aggregate) to some parameter locations *)
    fun writeLocs' (arg, locs, stms) = let
	  val locs = List.map elimNarrow locs
        (* offsets of the members of the struct *)
	  val membOffs = List.tabulate(List.length locs, fn i => lit(i*8))
          in
	     ListPair.foldl (writeLoc arg) stms (membOffs, locs)
          end

  (* write C arguments to parameter locations; also return any used GPRs and FPRs *)
    fun writeLocs (args, argLocs) = let
	  val gprs = concatMap gprsOfLoc (List.concat argLocs)
	  val fprs = concatMap fprsOfLoc (List.concat argLocs)
	  val instrs = ListPair.foldl writeLocs' [] (args, argLocs)
          in
	     (List.rev instrs, gprs, fprs)
          end

  (* read from a machine location *)
    fun readLoc (loc, (resultRegs, copyResult)) = (
	  case elimNarrow loc
	   of SA.REG(w, K.GPR, r) => let
                (* register (gpr) *)
		val tmpR = C.newReg()
	        in
		  (T.GPR(T.REG(w, tmpR)) :: resultRegs, T.COPY(w, [tmpR], [r]) :: copyResult)
	        end
	    | SA.NARROW(loc, w', K.GPR) => let
                (* conversion (gpr) *)
		val ([resultReg as T.GPR(T.REG(_, tmp))], copyResult') = readLoc(loc, ([], []))
		val w = SA.width loc
	        in
		  (T.GPR(T.REG(w', tmp)) :: resultRegs, 
		   T.MV(w, tmp, lobits{nBits=w', width=w, e=T.REG (w, tmp)}) :: copyResult' @ copyResult)
	        end
	    | SA.REG(w, K.FPR, r) => let
		val resReg = C.newFreg()
	        in
		   (T.FPR(T.FREG(w, resReg)) :: resultRegs, T.FCOPY(w, [resReg], [r]) :: copyResult)
	        end
	    | SA.NARROW(loc, w', K.FPR) => let
                (* conversion (fpr) *)
		val ([resultReg as T.FPR(T.FREG(_, tmp))], copyResult') = readLoc(loc, ([], []))
		val w = SA.width loc
	        in
		   (T.FPR(T.FREG(w', tmp)) :: resultRegs, 
		    T.FMV(w', tmp, f2f{fromWidth=w, toWidth=w', e=T.FREG(w', tmp)}) :: copyResult' @ copyResult)
	        end
	    | SA.COMBINE (l1, l2) => (
	        case (readLoc (l1, ([], [])), readLoc (l2, ([], [])))
		 of ( ([T.GPR e1], instrs1), ([T.GPR e2], instrs2) ) => let
			val w = SA.width loc
			val w' = SA.width l2
			val tmp = C.newReg()
		        in
			   (T.GPR(T.REG(w, tmp)) :: resultRegs, 
			    T.MV(w, tmp, T.ADD(w, T.SLL(w, lit w', e1), e2)) :: 
			    instrs1 @ instrs2 @ copyResult)
			end
 	        (* end case *))
	    | _ => raise Fail "bogus read location"
         (* end case *))

  (* read from some machine locations *)
    fun readLocs locs = let
	  val (resultRegs, copyResult) = List.foldl readLoc ([], []) locs
          in
	      (List.rev resultRegs, List.rev copyResult)
	  end

  end (* CCallGenFn *)