summary refs log tree commit diff
path: root/lisc/tools/abi.ml
blob: a5ad2503ed9375f0bd127db688838a869137d0ce (plain) (blame)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
(* fuzzer *)

type _ bty =
  | Char: int bty
  | Short: int bty
  | Int: int bty
  | Long: int bty
  | Float: float bty
  | Double: float bty

type _ sty =
  | Field: 'a bty * 'b sty -> ('a * 'b) sty
  | Empty: unit sty

type _ aty =
  | Base: 'a bty -> 'a aty
  | Struct: 'a sty -> 'a aty

type anyb = AB: _ bty -> anyb (* kinda boring... *)
type anys = AS: _ sty -> anys
type anya = AA: _ aty -> anya
type test = T: 'a aty * 'a -> test


let btysize: type a. a bty -> int = function
  | Char -> 1
  | Short -> 2
  | Int -> 4
  | Long -> 8
  | Float -> 4
  | Double -> 8

let btyalign = btysize

let styempty: type a. a sty -> bool = function
  | Field _ -> false
  | Empty -> true


(* Generate types and test vectors. *)
module Gen = struct
  module R = Random

  let init = function
    | None ->
      let f = open_in "/dev/urandom" in
      let seed =
        Char.code (input_char f) lsl 8 +
        Char.code (input_char f) in
      close_in f;
      R.init seed;
      seed
    | Some seed ->
      R.init seed;
      seed

  let int sz =
    let bound = 1 lsl (8 * min sz 3 - 1) in
    let i = R.int bound in
    if R.bool () then - i else i

  let float () =
    let f = R.float 1000. in
    if R.bool () then -. f else f

  let testv: type a. a aty -> a =
    let tb: type a. a bty -> a = function (* eh, dry... *)
      | Float  -> float ()
      | Double -> float ()
      | Char   -> int (btysize Char)
      | Short  -> int (btysize Short)
      | Int    -> int (btysize Int)
      | Long   -> int (btysize Long) in
    let rec ts: type a. a sty -> a = function
      | Field (b, s) -> (tb b, ts s)
      | Empty -> () in
    function
    | Base b -> tb b
    | Struct s -> ts s

  let b () = (* uniform *)
    match R.int 6 with
    | 0 -> AB Char
    | 1 -> AB Short
    | 2 -> AB Int
    | 3 -> AB Long
    | 4 -> AB Float
    | _ -> AB Double

  let smax = 5      (* max elements in structs *)
  let structp = 0.3 (* odds of having a struct type *)
  let amax = 8      (* max function arguments *)

  let s () =
    let rec f n =
      if n = 0 then AS Empty else
      let AB bt = b () in
      let AS st = f (n-1) in
      AS (Field (bt, st)) in
    f (1 + R.int (smax-1))

  let a () =
    if R.float 1.0 > structp then
      let AB bt = b () in
      AA (Base bt)
    else
      let AB bt = b () in
      let AS st = s () in
      AA (Struct (Field (bt, st)))

  let test () =
    let AA ty = a () in
    let t = testv ty in
    T (ty, t)

  let tests () =
    let rec f n =
      if n = 0 then [] else
      test () :: f (n-1) in
    f (R.int amax)

end

module type OUT = sig
  val extension: string
  val comment: out_channel -> string -> unit
  val caller: out_channel -> test -> test list -> unit
  val callee: out_channel -> test -> test list -> unit
end

(* Code generation for C *)
module OutC = struct
  open Printf

  let ctypelong oc name =
    let cb: type a. a bty -> unit = function
      | Char   -> fprintf oc "char"
      | Short  -> fprintf oc "short"
      | Int    -> fprintf oc "int"
      | Long   -> fprintf oc "long"
      | Float  -> fprintf oc "float"
      | Double -> fprintf oc "double" in
    let rec cs: type a. int -> a sty -> unit =
      fun i -> function
      | Field (b, s) ->
        cb b;
        fprintf oc " f%d; " i;
        cs (i+1) s;
      | Empty -> () in
    function
    | Base b ->
      cb b;
    | Struct s ->
      fprintf oc "struct %s { " name;
      cs 1 s;
      fprintf oc "}";
      ()

  let ctype: type a. out_channel -> string -> a aty -> unit =
    fun oc name -> function
    | Struct _ -> fprintf oc "struct %s" name
    | t -> ctypelong oc "" t

  let base: type a. out_channel -> a bty * a -> unit =
    fun oc -> function
    | Char, i   -> fprintf oc "%d" i
    | Short, i  -> fprintf oc "%d" i
    | Int, i    -> fprintf oc "%d" i
    | Long, i   -> fprintf oc "%d" i
    | Float, f  -> fprintf oc "%ff" f
    | Double, f -> fprintf oc "%f" f

  let init oc name (T (ty, t)) =
    let inits s =
      let rec f: type a. a sty * a -> unit = function
        | Field (b, s), (tb, ts) ->
          base oc (b, tb);
          fprintf oc ", ";
          f (s, ts)
        | Empty, () -> () in
      fprintf oc "{ ";
      f s;
      fprintf oc "}"; in
    ctype oc name ty;
    fprintf oc " %s = " name;
    begin match (ty, t) with
    | Base b, tb -> base oc (b, tb)
    | Struct s, ts -> inits (s, ts)
    end;
    fprintf oc ";\n";
    ()

  let extension = ".c"

  let comment oc s =
    fprintf oc "/* %s */\n" s

  let prelude oc = List.iter (fprintf oc "%s\n")
    [ "#include <stdio.h>"
    ; "#include <stdlib.h>"
    ; ""
    ; "static void fail(char *chk)"
    ; "{"
    ; "\tfprintf(stderr, \"fail: checking %s\\n\", chk);"
    ; "\tabort();"
    ; "}"
    ; ""
    ]

  let typedef oc name = function
    | T (Struct ts, _) ->
      ctypelong oc name (Struct ts);
      fprintf oc ";\n";
    | _ -> ()

  let check oc name =
    let chkbase: type a. string -> a bty * a -> unit =
      fun name t ->
        fprintf oc "\tif (%s != " name;
        base oc t;
        fprintf oc ")\n\t\tfail(%S);\n" name; in
    function
    | T (Base b, tb) -> chkbase name (b, tb)
    | T (Struct s, ts) ->
      let rec f: type a. int -> a sty * a -> unit =
        fun i -> function
        | Field (b, s), (tb, ts) ->
          chkbase (Printf.sprintf "%s.f%d" name i) (b, tb);
          f (i+1) (s, ts);
        | Empty, () -> () in
      f 1 (s, ts)

  let argname i = "arg" ^ string_of_int (i+1)

  let proto oc (T (tret, _)) args =
    ctype oc "ret" tret;
    fprintf oc " f(";
    let narg = List.length args in
    List.iteri (fun i (T (targ, _)) ->
      ctype oc (argname i) targ;
      fprintf oc " %s" (argname i);
      if i <> narg-1 then
        fprintf oc ", ";
    ) args;
    fprintf oc ")";
    ()

  let caller oc ret args =
    let narg = List.length args in
    prelude oc;
    typedef oc "ret" ret;
    List.iteri (fun i arg ->
      typedef oc (argname i) arg;
    ) args;
    proto oc ret args;
    fprintf oc ";\n\nint main()\n{\n";
    List.iteri (fun i arg ->
      fprintf oc "\t";
      init oc (argname i) arg;
    ) args;
    fprintf oc "\t";
    let T (tret, _) = ret in
    ctype oc "ret" tret;
    fprintf oc " ret;\n\n";
    fprintf oc "\tret = f(";
    List.iteri (fun i _ ->
      fprintf oc "%s" (argname i);
      if i <> narg-1 then
        fprintf oc ", ";
    ) args;
    fprintf oc ");\n";
    check oc "ret" ret;
    fprintf oc "\n\treturn 0;\n}\n";
    ()

  let callee oc ret args =
    prelude oc;
    typedef oc "ret" ret;
    List.iteri (fun i arg ->
      typedef oc (argname i) arg;
    ) args;
    fprintf oc "\n";
    proto oc ret args;
    fprintf oc "\n{\n\t";
    init oc "ret" ret;
    fprintf oc "\n";
    List.iteri (fun i arg ->
      check oc (argname i) arg;
    ) args;
    fprintf oc "\n\treturn ret;\n}\n";
    ()

end

(* Code generation for QBE *)
module OutIL = struct
  open Printf

  let ctypelong oc name =
    let cb: type a. a bty -> unit = function
      | Char   -> fprintf oc "char"
      | Short  -> fprintf oc "short"
      | Int    -> fprintf oc "int"
      | Long   -> fprintf oc "long"
      | Float  -> fprintf oc "float"
      | Double -> fprintf oc "double" in
    let rec cs: type a. int -> a sty -> unit =
      fun i -> function
      | Field (b, s) ->
        cb b;
        fprintf oc " f%d; " i;
        cs (i+1) s;
      | Empty -> () in
    function
    | Base b ->
      cb b;
    | Struct s ->
      fprintf oc "struct %s { " name;
      cs 1 s;
      fprintf oc "}";
      ()

  let ctype: type a. out_channel -> string -> a aty -> unit =
    fun oc name -> function
    | Struct _ -> fprintf oc "struct %s" name
    | t -> ctypelong oc "" t

  let init oc name (T (ty, t)) =
    let inits s =
      let rec f: type a. a sty * a -> unit = function
        | Field (b, s), (tb, ts) ->
          base oc (b, tb);
          fprintf oc ", ";
          f (s, ts)
        | Empty, () -> () in
      fprintf oc "{ ";
      f s;
      fprintf oc "}"; in
    ctype oc name ty;
    fprintf oc " %s = " name;
    begin match (ty, t) with
    | Base b, tb -> base oc (b, tb)
    | Struct s, ts -> inits (s, ts)
    end;
    fprintf oc ";\n";
    ()


  let comment oc s =
    fprintf oc "# %s\n" s

  let check oc name =
    let chkbase: type a. string -> a bty * a -> unit =
      fun name t ->
        fprintf oc "\tif (%s != " name;
        base oc t;
        fprintf oc ")\n\t\tfail(%S);\n" name; in
    function
    | T (Base b, tb) -> chkbase name (b, tb)
    | T (Struct s, ts) ->
      let rec f: type a. int -> a sty * a -> unit =
        fun i -> function
        | Field (b, s), (tb, ts) ->
          chkbase (Printf.sprintf "%s.f%d" name i) (b, tb);
          f (i+1) (s, ts);
        | Empty, () -> () in
      f 1 (s, ts)

  let tmp =
    let next = ref 0 in
    fun () ->
      incr next;
      "%t" ^ (string_of_int !next)

  (* NEW NEW NEW *)

  let base: type a. out_channel -> a bty * a -> unit =
    fun oc -> function
    | Char, i   -> fprintf oc "%d" i
    | Short, i  -> fprintf oc "%d" i
    | Int, i    -> fprintf oc "%d" i
    | Long, i   -> fprintf oc "%d" i
    | Float, f  -> fprintf oc "s_%f" f
    | Double, f -> fprintf oc "d_%f" f

  let extension = ".ssa"

  let argname i = "arg" ^ string_of_int (i+1)

  let btype: type a. a bty -> string = function
    | Char   -> "w"
    | Short  -> "w"
    | Int    -> "w"
    | Long   -> "l"
    | Float  -> "s"
    | Double -> "d"

  let ttype name = function
    | T (Base b, _)   -> btype b
    | T (Struct _, _) -> ":" ^ name

  let typedef oc name =
    let rec f: type a. a sty -> unit = function
      | Field (b, s) ->
        fprintf oc "%s" (btype b);
        if not (styempty s) then
          fprintf oc ", ";
        f s;
      | Empty -> () in
    function
    | T (Struct ts, _) ->
      fprintf oc "type :%s = { " name;
      f ts;
      fprintf oc " }\n";
    | _ -> ()

  let callee oc ret args =
    let narg = List.length args in
    List.iteri (fun i arg ->
      typedef oc (argname i) arg;
    ) args;
    typedef oc "ret" ret;
    fprintf oc "\nfunction %s $f(" (ttype "ret" ret);
    List.iteri (fun i arg ->
      let a = argname i in
      fprintf oc "%s %%%s" (ttype a arg) a;
      if i <> narg-1 then
        fprintf oc ", ";
    ) args;
    fprintf oc ") {\n";

    fprintf oc "}\n";
    ()

end

let _ =
  let module O = OutIL in
  let seed = Gen.init None in
  let tret = Gen.test () in
  let targs = Gen.tests () in
  let oc = stdout in
  O.comment oc (Printf.sprintf "seed %d" seed);
  (* O.caller oc tret targs; *)
  O.callee oc tret targs;
  ()