Debian Codegraph @

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

raw
1 (* c-test-gen.sml
2 *
3 * This module tests C calls for staged allocation. We generate tests for a variety
4 * of calls. To verify the test, we generate a dummy C function that prints out its
5 * parameters in order.
6 *
7 * The example code below passes arguments x and y to the MLRISC-generated C call. The output
8 * of main.c and sanity.c should be identical.
9
10 /* glue.c */
11 #define MAX_SZ 16
12
13 int target (int x, float y)
14 {
15 printf("%d %f", x, y);
16 return 23432;
17 }
18
19 void glueCode()
20 {
21 void* arr0[4096];
22 void** arr = arr0;
23 /* initialize arguments */
24 int x = 23423;
25 memcpy(arr, &x, sizeof(int));
26 arr += MAX_SZ;
27 float y = 1024.013f;
28 memcpy(arr, &y, sizeof(float));
29 arr += MAX_SZ;
30 /* call into the MLRISC glue code that will make the C call */
31 int tmp = mlriscGlue(arr0);
32 printf("%d", tmp);
33 }
34
35 /* main.c */
36 int main ()
37 {
38 glueCode();
39 return 0;
40 }
41
42 /* sanity.c */
43 int main ()
44 {
45 int x = 23432;
46 float y = 1024.013f;
47 int tmp = target(x, y);
48 printf ("%d", tmp);
49 return 0;
50 }
51
52 * We also generate the mlriscGlue code in MLRISC. This code grabs the arguments from
53 * arr0 and then passes them to target using Staged Allocation.
54 *
55 *)
56
57 functor GenTestFn(
58 structure T : MLTREE
59 where type Region.region = unit
60 structure Cells : CELLS
61 structure CCall : C_CALL
62 where T = T
63 val codegen : (string * string * CType.c_proto * T.stm list * CCall.c_arg list) -> unit
64 val param0 : T.rexp
65 val wordTy : int
66 ) =
67 struct
68
69 structure CTy = CType
70
71 val retValVar = "retVal"
72
73 val mem = ()
74
75 val wordSzB = wordTy div 8
76
77 fun zip3 (ls1, ls2, ls3) = let
78 fun f ([], _, _, xs) = List.rev xs
79 | f (x1 ::xs1, x2::xs2, x3::xs3, xs) = f(xs1, xs2, xs3, (x1, x2, x3) :: xs)
80 in
81 f(ls1, ls2, ls3, [])
82 end
83
84 datatype c_argument =
85 INT of int
86 | SHORT of int
87 | CHAR of int
88 | FLOAT of real
89 | DOUBLE of real
90 | POINTER of int
91 | STRUCT of c_argument list
92
93 fun flattenArg cArg = (case cArg
94 of STRUCT args => List.concat (List.map flattenArg args)
95 | cArg => [cArg])
96
97 fun cArgToString (cArg) = (case cArg
98 of INT i => Int.toString i
99 | SHORT i => Int.toString i
100 | CHAR c => Int.toString c
101 | FLOAT f => Real.toString f^"f"
102 | DOUBLE f => Real.toString f
103 | POINTER p => "(void*)0x"^Int.fmt StringCvt.HEX p
104 | STRUCT args => "{"^String.concatWith ", " (List.map cArgToString args)^"}"
105 (* end case *))
106
107 fun tyToFormatString (ty) = (case ty
108 of CTy.C_signed CTy.I_char => "%c"
109 | CTy.C_unsigned CTy.I_char => "%c"
110 | CTy.C_unsigned _ => "%u"
111 | CTy.C_signed _ => "%d"
112 | CTy.C_float => "%f"
113 | CTy.C_double => "%f"
114 | CTy.C_PTR => "%p"
115 | CTy.C_STRUCT cTys => String.concatWith " " (List.map tyToFormatString cTys)
116 (* end case *))
117
118 fun cTyToName cTy = (case cTy
119 of CTy.C_unsigned CTy.I_int => "u"
120 | CTy.C_signed CTy.I_int => "i"
121 | CTy.C_signed CTy.I_char => "c"
122 | CTy.C_unsigned CTy.I_char => "c"
123 | CTy.C_signed CTy.I_short => "i"
124 | CTy.C_unsigned CTy.I_short => "u"
125 | CTy.C_float => "f"
126 | CTy.C_double => "d"
127 | CTy.C_PTR => "vs"
128 | CTy.C_STRUCT cTys => "s"^String.concat (List.map cTyToName cTys)
129 (* end case *))
130
131 fun cTyToString (ty) = (case ty
132 of CTy.C_unsigned CTy.I_int => "unsigned int"
133 | CTy.C_signed CTy.I_int => "int"
134 | CTy.C_unsigned CTy.I_char => "char"
135 | CTy.C_signed CTy.I_char => "char"
136 | CTy.C_signed CTy.I_short => "short"
137 | CTy.C_unsigned CTy.I_short => "short"
138 | CTy.C_float => "float"
139 | CTy.C_double => "double"
140 | CTy.C_PTR => "void*"
141 | CTy.C_void => "void"
142 | CTy.C_STRUCT cTys => "struct "^cTyToName ty
143 (* end case *))
144
145 val i = ref 0
146 fun freshName () = (
147 i := (!i) + 1;
148 "x"^Int.toString (!i))
149
150 fun cTyDecl' cTy = (case cTy
151 of CTy.C_STRUCT cTys => cTyToString cTy^"{ "^(String.concatWith " " (List.map cTyDecl' cTys))^"}"^freshName()^";"
152 | cTy => cTyToString cTy^" "^freshName()^";"
153 (* end case *))
154
155 fun cTyDecl cTy = let
156 val td = cTyDecl' cTy
157 in
158 i := 0;
159 td
160 end
161
162 fun cTyNames' prefix (cTy) = (case cTy
163 of CTy.C_STRUCT cTys => List.concat (List.map (cTyNames' (prefix^"."^freshName())) cTys)
164 | cTy => [prefix^"."^freshName()]
165 (* end case *))
166
167 fun cTyNames prefix cTy = let
168 val td = (case cTy
169 of CTy.C_STRUCT cTys => List.concat (List.map (cTyNames' prefix) cTys)
170 | cTy => [prefix])
171 in
172 i := 0;
173 td
174 end
175
176 fun genParamName i =
177 " a"^Int.toString i
178
179 (* generate parameter names for a list of types *)
180 fun genParamNames tys =
181 List.rev(#2 (List.foldl (fn (ty, (i, params)) => (i+1, genParamName(i) :: params)) (0, []) tys))
182
183 (* generate formal parameters for a list of types and variables *)
184 fun genFormals (paramTys, vars) = let
185 fun f (ty, name) = cTyToString ty^name
186 in
187 List.map f (ListPair.zip (paramTys, vars))
188 end
189
190 fun genTargetPrintfArgs (vars, paramTys) = let
191 fun f (prefix, ty) = cTyNames prefix ty
192 in
193 List.concat (ListPair.map f (vars, paramTys))
194 end
195
196 fun genPrintf (formatString, args) =
197 "printf("^String.concatWith "," (formatString :: args)^");"
198
199 fun genFormatString (elts) =
200 "\"" ^ String.concatWith " " elts ^ "\\n\""
201
202 fun fmtVar (arg, ty) = arg^"="^tyToFormatString ty^"\\n"
203
204 (* construct a format string printing the parameters of a proto *)
205 fun protoToFormatString ({conv, retTy, paramTys}, args) =
206 genFormatString (ListPair.map fmtVar (args, paramTys))
207
208 fun protoToPrintf (proto, args) = genPrintf(protoToFormatString(proto, args), args)
209
210 (* generate a dummy target function that prints its parameters *)
211 fun targetFun (targetName, proto as {conv, retTy, paramTys}, retVal) = let
212 val vars = genParamNames paramTys
213 val params = genFormals(paramTys, vars)
214 val printfArgs = genTargetPrintfArgs(vars, paramTys)
215 in
216 cTyToString retTy ^ " " ^ targetName ^ "(" ^ (String.concatWith ", " params) ^ ")" ^
217 "{" ^
218 protoToPrintf(proto, printfArgs) ^
219 "return "^(String.concat (List.map cArgToString retVal))^";"^
220 "}"
221 end
222
223 fun genMLRISCGlueHdr (mlriscGlue, proto as {conv, retTy, paramTys}) =
224 cTyToString retTy ^ " " ^ mlriscGlue ^ "(void* arr0);"
225
226 fun genAssignArg (ty, var, arg) = cTyToString(ty)^" "^var^" = "^cArgToString(arg)^";"
227
228 (* generate C code that initializes an argument in arr0 *)
229 fun genInitArr0 (ty, arg) =
230 String.concatWith "\t" [
231 "{",
232 genAssignArg(ty, "tmp", arg),
233 "memcpy(arr, &tmp, sizeof("^cTyToString(ty)^"));",
234 "arr += MAX_SZ;",
235 "}\n"
236 ]
237
238 (* generate C code that calls the MLRISC-generated function *)
239 fun genCGlueCode (mlriscGlue, proto as {conv, retTy, paramTys}, args) = let
240 val stms = List.rev (ListPair.map genInitArr0 (paramTys, args))
241 val glueCall = if retTy <> CTy.C_void
242 then cTyToString retTy ^ " " ^retValVar^" = " ^ mlriscGlue^"(arr0);\n\t"^
243 genPrintf(genFormatString([tyToFormatString retTy]), [retValVar])
244 else mlriscGlue^"(arr0);"
245 in
246 String.concatWith "\n\t" [
247 "void glueCode(){",
248 (* initialize arguments *)
249 "void* arr0[4096];",
250 "void** arr = arr0;",
251 String.concatWith "\t " (List.rev stms),
252 glueCall,
253 "}"
254 ]
255 end
256
257 (* generate C code that calls the MLRISC-generated function *)
258 fun genTestCode (proto as {conv, retTy, paramTys}, args) = let
259 val paramNames = genParamNames paramTys
260 val stms = List.map genAssignArg (zip3(paramTys, paramNames, args))
261 val callTarget = "target("^String.concatWith ", " paramNames^");"
262 val glueCall = if retTy <> CTy.C_void
263 then cTyToString retTy ^ " " ^retValVar^" = " ^ callTarget^"\n\t"^
264 genPrintf(genFormatString([tyToFormatString retTy]), [retValVar])
265 else callTarget
266 in
267 String.concatWith "\n\t" [
268 "void testCode(){",
269 String.concatWith "\t " (List.rev stms),
270 glueCall,
271 "}"
272 ]
273 end
274
275 val cIncludes = String.concatWith "\n" [
276 "#include <stdio.h>",
277 "#include <stdlib.h>",
278 "#include <string.h>\n"
279 ]
280
281 fun isStruct (CTy.C_STRUCT _) = true
282 | isStruct _ = false
283
284 (* number of bytes to represent ty *)
285 fun szOfTy ty = if ty = CTy.C_void then 0 else #sz (CSizes.sizeOfTy ty)
286
287 (* returns the maximum size type for a C prototype *)
288 fun maxSzOfProto ({conv, paramTys, retTy}) =
289 List.foldl Int.max 0 (List.map szOfTy (retTy :: paramTys))
290
291 fun genGlue (target, mlriscGlue, proto, args, retVal) = String.concatWith "\n" [
292 cIncludes,
293 "#define MAX_SZ "^Int.toString(Int.max(1,(maxSzOfProto proto) div wordSzB)),
294 (* tyep declarations for structs *)
295 String.concatWith "\n" (List.map cTyDecl (List.filter isStruct (#paramTys proto))),
296 (* C prototype for the MLRISC assembly stub *)
297 genMLRISCGlueHdr(mlriscGlue, proto),
298 (* target function *)
299 targetFun(target, proto, retVal),
300 (* C glue code for calling into the MLRISC assembly stub *)
301 genCGlueCode(mlriscGlue, proto, args),
302 (* C test code that directly calls the target function *)
303 genTestCode(proto, args)
304 ]
305
306 fun genCMain () = "int main () { glueCode(); return 0; }"
307
308 fun genSanityCheck (proto, args, retVal) = "int main () { testCode(); return 0; }"
309
310 fun offset szB arr0 i = T.ADD(wordTy, arr0, T.LI (T.I.fromInt (wordTy, i*szB)))
311
312 (* generate the ith argument to the MLRISC code *)
313 fun genGlueArg szB arr0 (ty, (i, args)) = (i+1,
314 (case ty
315 of CTy.C_signed CTy.I_int => CCall.ARG (T.LOAD(32, offset szB arr0 i, mem))
316 | CTy.C_unsigned CTy.I_int => CCall.ARG (T.LOAD(32, offset szB arr0 i, mem))
317 | CTy.C_unsigned CTy.I_char => CCall.ARG (T.LOAD(8, offset szB arr0 i, mem))
318 | CTy.C_signed CTy.I_char => CCall.ARG (T.LOAD(8, offset szB arr0 i, mem))
319 | CTy.C_PTR => CCall.ARG (T.LOAD(wordTy, offset szB arr0 i, mem))
320 | CTy.C_STRUCT _ => CCall.ARG (T.LOAD(wordTy, offset szB arr0 i, mem))
321 | CTy.C_float => CCall.FARG (T.FLOAD(32, offset szB arr0 i, mem))
322 | CTy.C_double => CCall.FARG (T.FLOAD(64, offset szB arr0 i, mem))
323 (* end case *)) :: args)
324
325 val rand = Random.rand (0, 255)
326
327 fun genRandArg ty = (case ty
328 of CTy.C_float => FLOAT (Random.randReal(rand))
329 | CTy.C_double => DOUBLE(Random.randReal(rand))
330 | CTy.C_unsigned CTy.I_int => INT (Random.randNat(rand))
331 | CTy.C_signed CTy.I_int => INT (Random.randNat(rand))
332 | CTy.C_signed CTy.I_char => CHAR (Random.randNat rand mod 255)
333 | CTy.C_PTR => POINTER(Random.randNat(rand))
334 | CTy.C_STRUCT cTys => STRUCT(List.map genRandArg cTys)
335 (* end case *))
336
337 fun output (strm, s) = TextIO.output(strm, s^"\n")
338
339 (* test parameter types *)
340 val pty1 = [CTy.C_double, CTy.C_unsigned CTy.I_int, CTy.C_PTR, CTy.C_double,
341 CTy.C_float, CTy.C_PTR, CTy.C_float, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR,
342 CTy.C_signed CTy.I_int,
343 CTy.C_double, CTy.C_double, CTy.C_double, CTy.C_double, CTy.C_double,
344 CTy.C_double, CTy.C_double]
345 val pty2 = [CTy.C_STRUCT [CTy.C_float]]
346 val pty3 = [CTy.C_STRUCT [CTy.C_float,CTy.C_float]]
347 val pty3 = [CTy.C_STRUCT [CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float]]
348 val pty4 = [CTy.C_STRUCT [CTy.C_PTR,CTy.C_float,CTy.C_float,CTy.C_float]]
349 val pty5 = [CTy.C_double, CTy.C_unsigned CTy.I_int, CTy.C_PTR]
350 val pty6 = [CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR]
351 val pty7 = [CTy.C_float]
352 val pty7 = [CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float,CTy.C_float]
353 val pty8 = [CTy.C_PTR]
354 val pty9 = [CTy.C_signed CTy.I_int]
355 val pty10 = [CTy.C_signed CTy.I_int, CTy.C_signed CTy.I_int]
356 val pty11 = [CTy.C_signed CTy.I_int, CTy.C_signed CTy.I_char, CTy.C_float]
357 val pty12 = [CTy.C_STRUCT [CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR, CTy.C_PTR]]
358 val pty13 = [CTy.C_float, CTy.C_double]
359
360 fun main _ = (*BackTrace.monitor (fn () => *) (let
361 (* choose the prototype to test *)
362 val retTy = CTy.C_void
363 val paramTys = pty5
364
365 val cArgs = List.map genRandArg paramTys
366 val retVal = if retTy <> CTy.C_void then [genRandArg retTy] else []
367 val proto = {conv="ccall", retTy=retTy, paramTys=paramTys}
368 val mlriscGlue = "mlriscGlue"
369 val target = "target"
370
371 (* output C code that glues to the MLRISC code *)
372 val cOutStrm = TextIO.openOut "glue.c"
373 val cCode = genGlue(target, mlriscGlue, proto, cArgs, retVal)
374 val _ = output(cOutStrm, cCode)
375 val _ = TextIO.closeOut cOutStrm
376
377 (* output C code for santity check *)
378 val cOutStrm = TextIO.openOut "sanity.c"
379 val cCode = genSanityCheck(proto, cArgs, retVal)
380 val _ = output(cOutStrm, cCode)
381 val _ = TextIO.closeOut cOutStrm
382
383 (* output main *)
384 val cMainOutStrm = TextIO.openOut "main.c"
385 val cMain = genCMain()
386 val _ = output(cMainOutStrm, cMain)
387 val _ = TextIO.closeOut cMainOutStrm
388
389 (* output MLRISC code *)
390 val tmpReg = Cells.newReg()
391 val tmpR = T.REG(wordTy, tmpReg)
392 val szB = Int.max(wordSzB, maxSzOfProto proto)
393 val (_, glueArgs) = List.foldl (genGlueArg szB tmpR) (0, []) paramTys
394 val asmOutStrm = TextIO.openOut "mlrisc.s"
395 fun doit () = codegen(mlriscGlue, target, proto, [T.MV(32, tmpReg, param0)], List.rev glueArgs)
396 val _ = AsmStream.withStream asmOutStrm doit ()
397 val _ = TextIO.closeOut asmOutStrm
398 in
399 0
400 end)
401
402 end