summaryrefslogtreecommitdiff
path: root/cparser/GCC.ml
blob: 17b718b424e05bfbf3ba395a9d95ff7486415ea3 (plain)
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
(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(* GCC built-ins *)

open C
open Cutil

(* Code adapted from CIL *)

let voidType = TVoid []
let charType = TInt(IChar, [])
let intType = TInt(IInt, [])
let uintType = TInt(IUInt, [])
let longType = TInt(ILong, [])
let ulongType = TInt(IULong, [])
let ulongLongType = TInt(IULongLong, [])
let floatType = TFloat(FFloat, [])
let doubleType = TFloat(FDouble, [])
let longDoubleType = TFloat (FLongDouble, [])
let voidPtrType = TPtr(TVoid [], [])
let voidConstPtrType = TPtr(TVoid [AConst], [])
let charPtrType = TPtr(TInt(IChar, []), [])
let charConstPtrType = TPtr(TInt(IChar, [AConst]), [])
let intPtrType = TPtr(TInt(IInt, []), [])
let sizeType = TInt(size_t_ikind, [])

let builtins = {
  Builtins.typedefs = [
  "__builtin_va_list", voidPtrType
];
  Builtins.functions = [
  "__builtin___fprintf_chk",  (intType, [ voidPtrType; intType; charConstPtrType ], true) (* first argument is really FILE*, not void*, but we don't want to build in the definition for FILE *);
  "__builtin___memcpy_chk",  (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  "__builtin___memmove_chk",  (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  "__builtin___mempcpy_chk",  (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType; sizeType ], false);
  "__builtin___memset_chk",  (voidPtrType, [ voidPtrType; intType; sizeType; sizeType ], false);
  "__builtin___printf_chk",  (intType, [ intType; charConstPtrType ], true);
  "__builtin___snprintf_chk",  (intType, [ charPtrType; sizeType; intType; sizeType; charConstPtrType ], true);
  "__builtin___sprintf_chk",  (intType, [ charPtrType; intType; sizeType; charConstPtrType ], true);
  "__builtin___stpcpy_chk",  (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  "__builtin___strcat_chk",  (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  "__builtin___strcpy_chk",  (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  "__builtin___strncat_chk",  (charPtrType, [ charPtrType; charConstPtrType; sizeType; sizeType ], false);
  "__builtin___strncpy_chk",  (charPtrType, [ charPtrType; charConstPtrType; sizeType; sizeType ], false);
  "__builtin___vfprintf_chk",  (intType, [ voidPtrType; intType; charConstPtrType; voidPtrType ], false) (* first argument is really FILE*, not void*, but we don't want to build in the definition for FILE *);
  "__builtin___vprintf_chk",  (intType, [ intType; charConstPtrType; voidPtrType ], false);
  "__builtin___vsnprintf_chk",  (intType, [ charPtrType; sizeType; intType; sizeType; charConstPtrType; voidPtrType ], false);
  "__builtin___vsprintf_chk",  (intType, [ charPtrType; intType; sizeType; charConstPtrType; voidPtrType ], false);

  "__builtin_acos",  (doubleType, [ doubleType ], false);
  "__builtin_acosf",  (floatType, [ floatType ], false);
  "__builtin_acosl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_alloca",  (voidPtrType, [ uintType ], false);
  
  "__builtin_asin",  (doubleType, [ doubleType ], false);
  "__builtin_asinf",  (floatType, [ floatType ], false);
  "__builtin_asinl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_atan",  (doubleType, [ doubleType ], false);
  "__builtin_atanf",  (floatType, [ floatType ], false);
  "__builtin_atanl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_atan2",  (doubleType, [ doubleType; doubleType ], false);
  "__builtin_atan2f",  (floatType, [ floatType; floatType ], false);
  "__builtin_atan2l",  (longDoubleType, [ longDoubleType; 
                                                longDoubleType ], false);

  "__builtin_ceil",  (doubleType, [ doubleType ], false);
  "__builtin_ceilf",  (floatType, [ floatType ], false);
  "__builtin_ceill",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_cos",  (doubleType, [ doubleType ], false);
  "__builtin_cosf",  (floatType, [ floatType ], false);
  "__builtin_cosl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_cosh",  (doubleType, [ doubleType ], false);
  "__builtin_coshf",  (floatType, [ floatType ], false);
  "__builtin_coshl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_clz",  (intType, [ uintType ], false);
  "__builtin_clzl",  (intType, [ ulongType ], false);
  "__builtin_clzll",  (intType, [ ulongLongType ], false);
  "__builtin_constant_p",  (intType, [ intType ], false);
  "__builtin_ctz",  (intType, [ uintType ], false);
  "__builtin_ctzl",  (intType, [ ulongType ], false);
  "__builtin_ctzll",  (intType, [ ulongLongType ], false);

  "__builtin_exp",  (doubleType, [ doubleType ], false);
  "__builtin_expf",  (floatType, [ floatType ], false);
  "__builtin_expl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_expect",  (longType, [ longType; longType ], false);

  "__builtin_fabs",  (doubleType, [ doubleType ], false);
  "__builtin_fabsf",  (floatType, [ floatType ], false);
  "__builtin_fabsl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_ffs",  (intType, [ uintType ], false);
  "__builtin_ffsl",  (intType, [ ulongType ], false);
  "__builtin_ffsll",  (intType, [ ulongLongType ], false);
  "__builtin_frame_address",  (voidPtrType, [ uintType ], false);

  "__builtin_floor",  (doubleType, [ doubleType ], false);
  "__builtin_floorf",  (floatType, [ floatType ], false);
  "__builtin_floorl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_huge_val",  (doubleType, [], false);
  "__builtin_huge_valf",  (floatType, [], false);
  "__builtin_huge_vall",  (longDoubleType, [], false);
  "__builtin_inf",  (doubleType, [], false);
  "__builtin_inff",  (floatType, [], false);
  "__builtin_infl",  (longDoubleType, [], false);
  "__builtin_memcpy",  (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType ], false);
  "__builtin_mempcpy",  (voidPtrType, [ voidPtrType; voidConstPtrType; sizeType ], false);

  "__builtin_fmod",  (doubleType, [ doubleType ], false);
  "__builtin_fmodf",  (floatType, [ floatType ], false);
  "__builtin_fmodl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_frexp",  (doubleType, [ doubleType; intPtrType ], false);
  "__builtin_frexpf",  (floatType, [ floatType; intPtrType  ], false);
  "__builtin_frexpl",  (longDoubleType, [ longDoubleType; 
                                                intPtrType  ], false);

  "__builtin_ldexp",  (doubleType, [ doubleType; intType ], false);
  "__builtin_ldexpf",  (floatType, [ floatType; intType  ], false);
  "__builtin_ldexpl",  (longDoubleType, [ longDoubleType; 
                                                intType  ], false);

  "__builtin_log",  (doubleType, [ doubleType ], false);
  "__builtin_logf",  (floatType, [ floatType ], false);
  "__builtin_logl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_log10",  (doubleType, [ doubleType ], false);
  "__builtin_log10f",  (floatType, [ floatType ], false);
  "__builtin_log10l",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_modff",  (floatType, [ floatType; 
                                          TPtr(floatType,[]) ], false);
  "__builtin_modfl",  (longDoubleType, [ longDoubleType; 
                                               TPtr(longDoubleType, []) ], 
                             false);

  "__builtin_nan",  (doubleType, [ charConstPtrType ], false);
  "__builtin_nanf",  (floatType, [ charConstPtrType ], false);
  "__builtin_nanl",  (longDoubleType, [ charConstPtrType ], false);
  "__builtin_nans",  (doubleType, [ charConstPtrType ], false);
  "__builtin_nansf",  (floatType, [ charConstPtrType ], false);
  "__builtin_nansl",  (longDoubleType, [ charConstPtrType ], false);
  "__builtin_next_arg",  (voidPtrType, [], false);
  "__builtin_object_size",  (sizeType, [ voidPtrType; intType ], false);

  "__builtin_parity",  (intType, [ uintType ], false);
  "__builtin_parityl",  (intType, [ ulongType ], false);
  "__builtin_parityll",  (intType, [ ulongLongType ], false);

  "__builtin_popcount",  (intType, [ uintType ], false);
  "__builtin_popcountl",  (intType, [ ulongType ], false);
  "__builtin_popcountll",  (intType, [ ulongLongType ], false);

  "__builtin_powi",  (doubleType, [ doubleType; intType ], false);
  "__builtin_powif",  (floatType, [ floatType; intType ], false);
  "__builtin_powil",  (longDoubleType, [ longDoubleType; intType ], false);
  "__builtin_prefetch",  (voidType, [ voidConstPtrType ], true);
  "__builtin_return",  (voidType, [ voidConstPtrType ], false);
  "__builtin_return_address",  (voidPtrType, [ uintType ], false);

  "__builtin_sin",  (doubleType, [ doubleType ], false);
  "__builtin_sinf",  (floatType, [ floatType ], false);
  "__builtin_sinl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_sinh",  (doubleType, [ doubleType ], false);
  "__builtin_sinhf",  (floatType, [ floatType ], false);
  "__builtin_sinhl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_sqrt",  (doubleType, [ doubleType ], false);
  "__builtin_sqrtf",  (floatType, [ floatType ], false);
  "__builtin_sqrtl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_stpcpy",  (charPtrType, [ charPtrType; charConstPtrType ], false);
  "__builtin_strchr",  (charPtrType, [ charPtrType; charType ], false);
  "__builtin_strcmp",  (intType, [ charConstPtrType; charConstPtrType ], false);
  "__builtin_strcpy",  (charPtrType, [ charPtrType; charConstPtrType ], false);
  "__builtin_strcspn",  (uintType, [ charConstPtrType; charConstPtrType ], false);
  "__builtin_strncat",  (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  "__builtin_strncmp",  (intType, [ charConstPtrType; charConstPtrType; sizeType ], false);
  "__builtin_strncpy",  (charPtrType, [ charPtrType; charConstPtrType; sizeType ], false);
  "__builtin_strspn",  (intType, [ charConstPtrType; charConstPtrType ], false);
  "__builtin_strpbrk",  (charPtrType, [ charConstPtrType; charConstPtrType ], false);
  (* When we parse builtin_types_compatible_p, we change its interface *)
  "__builtin_types_compatible_p", 
                              (intType, [ uintType; (* Sizeof the type *)
                                          uintType  (* Sizeof the type *) ],
                               false);
  "__builtin_tan",  (doubleType, [ doubleType ], false);
  "__builtin_tanf",  (floatType, [ floatType ], false);
  "__builtin_tanl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_tanh",  (doubleType, [ doubleType ], false);
  "__builtin_tanhf",  (floatType, [ floatType ], false);
  "__builtin_tanhl",  (longDoubleType, [ longDoubleType ], false);

  "__builtin_va_end",  (voidType, [ voidPtrType ], false);
  "__builtin_varargs_start",  
    (voidType, [ voidPtrType ], false);
  (* When we elaborate builtin_stdarg_start/builtin_va_start,
     second argument is passed by address *)
  "__builtin_va_start",  (voidType, [ voidPtrType; voidPtrType ], false);
  "__builtin_stdarg_start",  (voidType, [ voidPtrType ], false);
  (* When we parse builtin_va_arg, type argument becomes sizeof type *)
  "__builtin_va_arg",  (voidType, [ voidPtrType; sizeType ], false);
  "__builtin_va_copy",  (voidType, [ voidPtrType;
                                           voidPtrType ],
                              false)
]
}