forked from facebookarchive/pfff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_minic.ml
274 lines (239 loc) · 9.01 KB
/
parse_minic.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
(* Yoann Padioleau
*
* Copyright (C) 2014 Facebook
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* file license.txt for more details.
*)
open Common
open Ast_cpp
open Ast_c
module C = Ast_c
module M = Ast_minic
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
* Just a small wrapper around the C parser (itself a wrapper over the C++
* parser).
*)
(*****************************************************************************)
(* Types *)
(*****************************************************************************)
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
let error s tok =
failwith (spf "%s at %s" s (Parse_info.string_of_info tok))
let error_any s any =
let v = Meta_ast_c.vof_any any in
let str = Ocaml.string_of_v v in
failwith (spf "%s on %s" s str)
(* dupe of graph_code.ml, but avoid deps *)
let cnt = ref 0
let gensym s =
incr cnt;
spf "%s__%d" s !cnt
(*****************************************************************************)
(* Conversion *)
(*****************************************************************************)
let rec program xs =
xs +> List.map toplevel +> List.flatten
and toplevel t =
match t with
| Define (name, _body) -> error "use enum not define" (snd name)
(* | Undef name -> error "use enum not define" (snd name) *)
| Macro (name, _args, _body) -> error "use function not macros" (snd name)
(* this is ok, so that gcc can compile the file, but our linking phase
* do not require it
*)
| Include _ -> []
| Prototype _ -> []
| StructDef def -> [M.StructDef (struct_def def)]
| TypeDef (name, _type) -> error "typedefs not supported" (snd name)
| EnumDef (_name, xs) ->
xs +> List.map (fun (name, eopt) ->
(match eopt with
| Some (Int _) -> ()
| None -> ()
| _ -> error "complex expr for enum not supported" (snd name)
);
M.Constant name
)
| Global v ->
(* this can be useful for gcc to compile, but our linking phase skip it *)
if v.v_storage = Extern
then []
else [M.Global (var_decl_global v)]
| FuncDef def -> [M.FuncDef (func_def def)]
and var_decl_global v =
match v.v_storage with
| Extern -> error "extern not supported, use one file" (snd v.v_name)
| Static -> error "static not supported, use regular globals" (snd v.v_name)
| DefaultStorage ->
(match v.v_init with
| Some _ ->
error "toplevel initializer not supported, use main" (snd v.v_name)
| None ->
{ M.v_name = v.v_name;
v_type = type_ v.v_type
}
)
and type_ x =
match x with
| TBase name -> M.TBase name
| TPointer t -> M.TPointer (type_ t)
| TArray (_, t) -> M.TArray (type_ t)
| TFunction (t, params) -> M.TFunction (function_type (t, params))
| TStructName (kind, name) ->
(match kind with
| Struct -> M.TStructName name
| Union -> error "union not supported" (snd name)
)
| TEnumName name -> error "enum type not supported, use int" (snd name)
| TTypeName name -> error "typedef not supported, inline" (snd name)
and function_type (t, params) =
(type_ t, params +> List.map parameter)
and parameter p =
match p.p_name with
| None -> error_any "give a name to parameter" (Type p.p_type)
| Some name -> { M.v_name = name; v_type = type_ p.p_type }
and struct_def def =
match def.s_kind with
| Union -> error "union not supported" (snd def.s_name)
| Struct ->
{ M.s_name = def.s_name;
s_flds = def.s_flds +> List.map field_def
}
and field_def def =
match def.fld_name with
| None -> error_any "unnamed field not supported" (Type def.fld_type)
| Some name ->
{ M.v_name = name;
v_type = type_ def.fld_type;
}
and func_def def =
{ M.f_name = def.f_name;
f_type = function_type def.f_type;
f_body = stmts def.f_body;
}
and stmts xs = xs +> List.map stmt +> List.flatten
and stmt st =
match st with
| ExprSt (Call (Id ("printf", _tok), _xs)) -> []
(* foo(...) => void local_void_1 = foo(...); *)
| ExprSt (Call (Id fname, es)) ->
let name = gensym "local_void", snd fname in
let typ = M.TBase ("void", snd fname) in
(M.Local { M.v_name = name; v_type = typ })::
stmt (ExprSt (Assign ((SimpleAssign, snd name),
Id name, (Call (Id fname, es)))))
| ExprSt e -> [M.Instr (expr_for_instr e)]
| Block xs -> stmts xs
| If (e, st1, st2) -> [M.If (expr_for_var e, stmt st1, stmt st2)]
| Switch (e, _xs) -> error_any "switch not supported, use if" (Expr e)
| While (e, st) -> [M.While (expr_for_var e, stmt st)]
| DoWhile (_, e) -> error_any "dowhile not supported, use while" (Expr e)
| For (_, _, _, st) -> error_any "for not supported, use while" (Stmt st)
| Continue | Break -> error "continue or break not supported"
(failwith "noTok")
| Return (Some e) -> [M.Return (expr_for_var e)]
| Return None -> error "empty return not supported" (failwith "noTok")
| Label (name, _) | Goto name -> error "label not supported" (snd name)
| Vars xs -> xs +> List.map var_decl +> List.flatten
| Asm xs -> error_any "asm not supported" (Expr (List.hd xs))
and var_decl v =
match v.v_storage with
| Extern -> error "extern not supported, use one file" (snd v.v_name)
| Static -> error "static not supported, use globals" (snd v.v_name)
| DefaultStorage ->
let decl =
M.Local { M.v_name = v.v_name;
v_type = type_ v.v_type
}
in
decl ::
(match v.v_init with
(* this is ok here *)
| Some e -> [M.Instr (expr_for_instr
(Assign ((SimpleAssign, snd v.v_name),
Id v.v_name, e)))]
| None -> []
)
and expr_for_instr e =
match e with
| Assign((SimpleAssign, _),
RecordPtAccess(Id name, fld), Id name2) ->
M.AssignField (name, fld, name2)
| Assign((SimpleAssign, _), ArrayAccess(Id name, Id idx), Id name2) ->
M.AssignArray (name, idx, name2)
| Assign((SimpleAssign, _), Id name, Unary(Id name2, (GetRef, _))) ->
M.AssignAddress (name, name2)
| Assign((SimpleAssign, _), Id name,
Unary(RecordPtAccess(Id name2, fld), (GetRef, _))) ->
M.AssignFieldAddress (name, name2, fld)
| Assign((SimpleAssign, _), Id name,
Unary(ArrayAccess(Id name2, Id idx), (GetRef, _)))->
M.AssignIndexAddress (name, name2, idx)
| Assign((SimpleAssign, _), Unary(Id name, (DeRef, _)), Id name2) ->
M.AssignDeref (name, name2)
| Assign ((SimpleAssign,_), Id name, e) ->
M.Assign (name, expr e)
| _ -> error_any "expected a simple instr, not a C expr" (Expr e)
and expr_for_var e =
match e with
| Id name -> name
| _ -> error_any "expected a var, not a regular expr" (Expr e)
and expr e =
match e with
| Int s -> M.Int s
| String s -> M.String s
| Float (_, tok) | Char (_, tok) -> error "float/char not supported" tok
| Id name -> M.Id name
| Call (Id ("malloc", _), xs) ->
(match xs with
| [SizeOf(Right(t))] ->
M.Alloc (type_ t)
| [Binary(Id(var), (Arith(Mul), _), SizeOf(Right(t)))] ->
M.AllocArray(var, type_ t)
| _ -> error_any "malloc form not supported" (Expr e)
)
| RecordPtAccess(Id name, name2) ->
M.ObjField (name, name2)
| ArrayAccess(Id(name1), Id(name2)) ->
M.ArrayAccess (name1, name2)
| Unary(Id(name), (DeRef, _)) ->
M.DeRef name
| Call (Id ("builtin", tok), xs) ->
let name = ("builtin", tok) in
M.BuiltinCall (name, xs +> List.map expr_for_var)
| Call (Id name, xs) ->
M.StaticCall (name, xs +> List.map expr_for_var)
| Call(Unary(Id(name), (DeRef, _)), xs) ->
M.DynamicCall (name, xs +> List.map expr_for_var)
(* should be handled in caller in expr_for_instr *)
| Assign _
(* more general case, not handled *)
| Call _ | ArrayAccess _ | RecordPtAccess _
(* advanced features not supported *)
| Cast _
| Postfix _ | Infix _ | Unary _ | Binary _
| CondExpr _ | Sequence _
(* handled only in malloc call context *)
| SizeOf _
| ArrayInit _ | RecordInit _ | GccConstructor _
-> error_any "expected an expr, not a C expr" (Expr e)
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let parse file =
let (ast_opt, _toks), _stat = Parse_c.parse file in
match ast_opt with
| None -> failwith (spf "parsing error on %s" file)
| Some ast -> program ast