forked from ThinkOpenly/riscvdecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscvdecode.ml
433 lines (403 loc) · 15.5 KB
/
riscvdecode.ml
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
open Libsail
open Ast
open Ast_defs
open Ast_util
let types = Hashtbl.create 997
let sigs = Hashtbl.create 997
let operands = Hashtbl.create 997
let encodings = Hashtbl.create 997
let assembly = Hashtbl.create 997
let functions = Hashtbl.create 997
let op_functions = Hashtbl.create 997
let string_of_arg = function
| E_aux (E_id id, _) -> "\"" ^ string_of_id id ^ "\""
| exp -> ("exp " ^ string_of_exp exp)
let rec parse_exp e = match e with
| E_aux (E_app (f, args), _) ->
print_endline ("E_app \"" ^ string_of_id f ^ "\" [" ^ Util.string_of_list ", " string_of_arg args ^ "]")
| _ -> print_endline ("parse_exp other" ^ string_of_exp e)
let rec parse_mpat x = match x with
| MP_aux (MP_lit ( l ), _) -> print_endline ("MP_lit " ^ string_of_lit l)
| MP_aux (MP_id ( i ), _) -> print_endline ("MP_id " ^ string_of_id i)
| MP_aux (MP_app ( i, pl ), _) ->
print_endline ("MP_app " ^ (string_of_id i) ^ " -->");
List.iter parse_mpat pl;
print_endline ("<-- MP_app " ^ (string_of_id i))
| MP_aux (MP_vector_concat ( mpl ), _) ->
print_endline "MP_vector_concat";
List.iter parse_mpat mpl
| MP_aux (MP_string_append ( pl ), _) ->
print_endline "MP_string_append";
List.iter parse_mpat pl
| MP_aux (MP_typ ( mp, at ), _) ->
print_endline "MP_typ";
parse_mpat mp
| _ -> print_endline "mpat other"
let rec string_list_of_mpat x = match x with
| MP_aux (MP_lit ( l ), _) ->
print_endline ("MP_lit " ^ string_of_lit l);
[ string_of_lit l ]
| MP_aux (MP_id ( i ), _) ->
print_endline ("MP_id " ^ string_of_id i);
[ string_of_id i ]
| MP_aux (MP_app ( i, pl ), _) ->
print_endline ("MP_app " ^ string_of_id i);
begin match string_of_id i with
| "spc" | "sep" -> []
| _ -> let b = List.concat (List.map string_list_of_mpat pl) in
begin
print_endline ("<-- MP_app" ^ string_of_id i);
[ (string_of_id i) ^ "(" ^ (String.concat "," b) ^ ")" ]
end
end
| MP_aux (MP_vector_concat ( mpl ), _) ->
print_endline "MP_vector_concat";
List.concat (List.map string_list_of_mpat mpl)
| MP_aux (MP_string_append ( pl ), _) ->
print_endline "MP_string_append";
List.concat (List.map string_list_of_mpat pl)
| MP_aux (MP_typ ( mp, at ), _) ->
print_endline "MP_typ";
string_list_of_mpat mp
| _ -> assert false
let parse_MPat_aux p = match p with
| MPat_aux ( MPat_pat (p), _ ) ->
print_endline ("MPat_pat " ^ string_of_mpat p);
parse_mpat p
| MPat_aux ( MPat_when (p, e), _ ) ->
print_endline ("MPat_when " ^ (string_of_mpat p) ^ " when " ^ (string_of_exp e));
parse_mpat p;
parse_exp e
| _ -> print_endline "MCL_bidir other"
let string_lists_of_MPat_aux p = match p with
| MPat_aux ( MPat_pat (p), _ ) ->
(* print_endline ("MPat_pat " ^ string_of_mpat p); *)
(string_list_of_mpat p, None)
| MPat_aux ( MPat_when (p, e), _ ) ->
(* print_endline ("MPat_when " ^ (string_of_mpat p) ^ " when " ^ (string_of_exp e)); *)
(string_list_of_mpat p, Some (string_of_exp e))
| _ -> assert false
let parse_encdec_mpat mp pb = match mp with
| MP_aux (MP_app ( app_id, mpl ), _) ->
print_endline ("MP_app " ^ string_of_id app_id);
let operandl = List.concat (List.map string_list_of_mpat mpl) in
begin
List.iter print_endline operandl;
Hashtbl.add operands (string_of_id app_id) operandl;
print_endline "MCL_bidir (right part)";
match pb with
| MPat_aux ( MPat_pat (p), _ ) ->
print_endline ("MPat_pat ");
List.iter print_endline (string_list_of_mpat p);
Hashtbl.add encodings (string_of_id app_id) (string_list_of_mpat p)
| MPat_aux ( MPat_when (p, e), _ ) ->
print_endline ("MPat_when ");
List.iter print_endline (string_list_of_mpat p);
Hashtbl.add encodings (string_of_id app_id) (string_list_of_mpat p)
| _ ->
print_endline ("assert ");
assert false
end
| _ -> assert false
let parse_encdec i mc = match mc with
| MCL_aux ( MCL_bidir ( pa, pb ), _ ) ->
print_endline "MCL_bidir (left part)";
begin match pa with
| MPat_aux ( MPat_pat (p), _ ) ->
print_endline ("MPat_pat ");
parse_encdec_mpat p pb
| MPat_aux ( MPat_when (p, e), _ ) ->
print_endline ("MPat_when ");
parse_encdec_mpat p pb
| _ ->
print_endline ("assert ");
assert false
end
| _ -> assert false
let add_assembly app_id p =
let x = string_list_of_mpat p in
begin
(* We only support "simple" assembly at the moment,
where the quoted literal mnemonic is in the statement. *)
if String.get (List.hd x) 0 = '"' then begin
print_endline ("assembly.add " ^ string_of_id app_id ^ " : " ^ List.hd x);
Hashtbl.add assembly (string_of_id app_id) x
end
end
let parse_assembly_mpat mp pb = match mp with
| MP_aux (MP_app ( app_id, mpl ), _) ->
print_endline ("MP_app " ^ string_of_id app_id);
let operandl = List.concat (List.map string_list_of_mpat mpl) in
begin
List.iter print_endline operandl;
Hashtbl.add operands (string_of_id app_id) operandl;
print_endline "MCL_bidir (right part)";
match pb with
| MPat_aux ( MPat_pat (p), _ ) ->
print_endline ("MPat_pat assembly");
add_assembly app_id p
| MPat_aux ( MPat_when (p, e), _ ) ->
print_endline ("MPat_when assembly");
add_assembly app_id p
| _ ->
print_endline ("assert ");
assert false
end
| _ -> assert false
let parse_assembly i mc = match mc with
| MCL_aux ( MCL_bidir ( pa, pb ), _ ) ->
print_endline "MCL_bidir";
begin match pa with
| MPat_aux ( MPat_pat (p), _ ) ->
print_endline ("MPat_pat ");
parse_assembly_mpat p pb
| MPat_aux ( MPat_when (p, e), _ ) ->
print_endline ("MPat_when ");
parse_assembly_mpat p pb
| _ ->
print_endline ("assert ");
assert false
end
| _ -> assert false
let parse_SD_mapcl i mc =
print_endline ("SD_mapcl " ^ string_of_id i);
match string_of_id i with
"encdec" ->
print_endline "ENCDEC!";
parse_encdec i mc
| "assembly" ->
print_endline "ASSEMBLY!";
parse_assembly i mc
| _ -> ();
match mc with
| MCL_aux ( MCL_bidir ( pa, pb ), _ ) ->
parse_MPat_aux pa;
parse_MPat_aux pb
| _ -> print_endline "mapcl other"
let parse_execute p e =
let x = match p with
P_aux ( P_app (i, pl), _ ) ->
print_endline ("P_app " ^ string_of_id i);
string_of_id i
| _ -> raise (Failure "pat other")
in begin
print_endline "<- pat";
print_endline "exp -> ";
print_endline (string_of_exp e);
print_endline "<- exp";
Hashtbl.add functions x (string_of_exp e)
end
let parse_SD_funcl fcl =
print_endline "SD_funcl";
match fcl with
| FCL_aux ( FCL_Funcl ( i, Pat_aux ( j, _ ) ), _ ) ->
print_endline ("FCL_Funcl " ^ string_of_id i);
if (string_of_id i) = "execute" then begin
match j with
| Pat_exp ( p, e ) -> (* parse_exp e *)
print_endline "Pat_exp";
print_endline (string_of_pat p);
print_endline "pat -> ";
begin match p with
P_aux ( P_app (x, pl), _ ) ->
print_endline ("P_app " ^ string_of_id x);
| _ -> print_endline "pat other"
end;
print_endline "<- pat";
print_endline "exp -> ";
print_endline (string_of_exp e);
print_endline "<- exp";
parse_execute p e
| Pat_when ( p, e, w ) ->
print_endline "Pat_when";
print_endline (string_of_pat p);
print_endline (string_of_exp e);
print_endline (string_of_exp w);
parse_execute p e
| _ -> raise (Failure "FCL_Funcl other")
end
| _ -> raise (Failure "SD_funcl other")
let parse_SD_unioncl i ucl =
print_endline ("SD_unioncl " ^ string_of_id i);
match ucl with
| Tu_aux ( Tu_ty_id ( c, d ), _ ) ->
print_string ("Tu_ty_id " ^ string_of_id d ^ "(");
(* print_endline (string_of_typ c); *)
begin match c with
| Typ_aux ( Typ_tup ( x ), _ ) ->
(* Typ_tuple in later versions of sail *)
List.iter (fun x0 ->
let type_name = string_of_typ x0 in
let type_type = try Hashtbl.find types (string_of_typ x0)
with Not_found -> "None"
in print_string (type_name ^ ":" ^ type_type ^ " ")
) x;
let l = List.map string_of_typ x in
Hashtbl.add sigs (string_of_id d) l;
| _ -> print_endline "Tu_ty_id other"
end;
print_endline ")"
| _ -> print_endline "SD_unioncl other"
let parse_DEF_type def =
print_endline "DEF_type";
match def with
| TD_aux (TD_abbrev (d, e, f), _) ->
print_endline ( "TD_abbrev " ^ string_of_id d ^ ":" ^ string_of_typ_arg f);
Hashtbl.add types (string_of_id d) (string_of_typ_arg f);
(* print_endline ( "TD_abbrev " ^ string_of_typquant e ); *)
(* print_endline ( "TD_abbrev " ^ string_of_typ_arg f ); *)
(*
begin match e with
| TypQ_aux ( x, _ ) ->
print_endline "TypQ_aux";
begin match x with
| TypQ_tq ( y ) ->
print_endline "TypQ_tq";
List.iter (fun y0 -> begin match y0 with
| QI_aux ( z, _ ) ->
print_endline "QI_aux";
begin match z with
| QI_id ( a ) -> print_endline "QI_id";
| QI_constraint ( a ) -> print_endline "QI_constraint"
| _ -> print_endline "QI_aux other";
end
| _ -> print_endline "TypQ_tq other"
end
) y
| TypQ_no_forall -> print_endline "TypQ_no_forall"
| _ -> print_endline "TypQ_aux other";
end
| _ -> print_endline "typquant other"
end
*)
| TD_aux ( TD_record (d, e, f, g), _) -> print_endline ( "TD_record " ^ string_of_id d )
| TD_aux ( TD_variant (d, e, f, g), _) -> print_endline ( "TD_variant " ^ string_of_id d )
| TD_aux ( TD_enum (d, e, f), _) -> print_endline ( "TD_enum " ^ string_of_id d )
| TD_aux ( TD_bitfield (d, e, f), _) -> print_endline ( "TD_bitfield " ^ string_of_id d )
| _ -> print_endline "DEF_type other"
let json_of_operand op = "\"" ^ op ^ "\""
let json_of_key_operand key op t =
"\n{\n" ^
" \"name\": \"" ^ op ^ "\", \"type\": \"" ^ t ^ "\"\n" ^
"}"
let json_of_operands k =
let ops = Hashtbl.find_opt operands k
and types = Hashtbl.find_opt sigs k in
match (ops, types) with
(Some opslist, Some typeslist) ->
let fk = json_of_key_operand k in
String.concat ", " (List.map2 fk opslist typeslist)
| (_, _) -> ""
let rec basetype t =
match Hashtbl.find_opt types t with
None -> t
| Some bt -> basetype bt
let string_of_sizeof_field k f =
if String.starts_with ~prefix:"0b" f then string_of_int (String.length f - 2)
else if String.contains f '(' then
let op_func = List.hd (String.split_on_char '(' f) in
Hashtbl.find op_functions op_func
else begin
print_endline ("sizeof " ^ k ^ " " ^ f);
let opmap = List.combine (Hashtbl.find operands k) (Hashtbl.find sigs k) in
begin match List.assoc_opt f opmap with
Some t ->
let bt = basetype t in
if bt = "bool" then
"1"
else if String.starts_with ~prefix:"bits(" bt then
List.hd (String.split_on_char ')' (List.hd (List.tl ((String.split_on_char '(' bt)))))
else begin
print_endline ("unfamiliar base type " ^ bt);
"72"
end
| None ->
print_endline ("not found " ^ f);
"0"
end
end
let json_of_field k f =
"{ \"field\": \"" ^ f ^ "\", \"size\": " ^ string_of_sizeof_field k f ^ " }"
let json_of_fields k =
match Hashtbl.find_opt encodings k with
None -> ""
| Some (fields) -> String.concat ", " (List.map (fun f -> json_of_field k f) fields)
let json_of_function k =
match Hashtbl.find_opt functions k with
None -> ""
| Some (f) -> String.escaped f
let json_of_instruction k =
let m = Hashtbl.find assembly k in
"{\n" ^
" \"mnemonic\": " ^ List.hd m ^ ",\n" ^
" \"name\": " ^ "\"TBD\"" ^ ",\n" ^
" \"operands\": [ " ^ (json_of_operands k) ^ " ],\n" ^
" \"fields\": [ " ^ (json_of_fields k) ^ " ],\n" ^
" \"function\": \"" ^ (json_of_function k) ^ "\",\n" ^
" \"description\": " ^ "\"TBD\"" ^ "\n" ^
"}"
let rec parse_typ name t = match t with
Typ_aux (Typ_bidir (tl, tr), _) ->
print_endline "Typ_bidir";
parse_typ name tl; parse_typ name tr
| Typ_aux (Typ_app (id, args), _) -> print_endline (string_of_id id);
print_endline (string_of_id id ^ "(" ^ (String.concat ", " (List.map string_of_typ_arg args)) ^ ")");
begin match string_of_id id with
"bitvector" ->
print_endline (string_of_typ_arg (List.hd args));
Hashtbl.add op_functions name (string_of_typ_arg (List.hd args))
| _ -> print_endline "Typ_app other"
end
| _ -> print_endline "typ other"
let parse_typschm name ts = match ts with
TypSchm_aux ( TypSchm_ts ( _, x ), _ ) ->
parse_typ name x (* This compiles as if x is a 'typ' instead of 'atyp' (?) *)
| _ -> assert false
let riscv_decode_info ast env =
List.iter (fun def ->
match def with
DEF_type ( def ) -> parse_DEF_type def
| DEF_spec ( vs ) ->
print_endline "DEF_spec";
begin match vs with
VS_aux ( VS_val_spec (ts, i, _, _), _) ->
parse_typschm (string_of_id i) ts
end
| DEF_scattered ( def ) ->
begin match def with
| SD_aux ( SD_funcl ( fcl ), _ ) -> parse_SD_funcl fcl
| SD_aux ( SD_function ( _, _, _ ), _ ) -> print_endline "SD_function"
| SD_aux ( SD_variant ( _, _ ), _ ) -> print_endline "SD_variant"
| SD_aux ( SD_unioncl ( i, ucl ), _ ) -> parse_SD_unioncl i ucl
| SD_aux ( SD_mapping ( _, _ ), _ ) -> print_endline "SD_mapping"
| SD_aux ( SD_mapcl ( i, mc ), _ ) -> parse_SD_mapcl i mc
| _ -> print_endline "b"
end
| _ -> print_string ""
) ast.defs;
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ v)) types;
print_endline "sigs";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) sigs;
print_endline "operands";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) operands;
print_endline "encodings";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) encodings;
print_endline "assembly";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ Util.string_of_list ", " (fun x -> x) v)) assembly;
print_endline "functions";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ v)) functions;
print_endline "op_functions";
Hashtbl.iter (fun k v -> print_endline (k ^ ":" ^ v)) op_functions;
print_endline "{";
print_endline " \"instructions\": [";
(* Filter out keys which have no match in 'assembly'. *)
Hashtbl.iter (fun k v -> if Hashtbl.find_opt assembly k == None then Hashtbl.remove sigs k) sigs;
print_endline (String.concat ",\n" (List.map json_of_instruction (Hashtbl.fold (fun k v init -> k :: init) sigs [])));
print_endline " ]";
print_endline "}";
exit 0
let _ =
Target.register
~name:"riscv_decode"
~pre_descatter_hook:riscv_decode_info
(fun _ _ _ _ _ -> ())