Correct checking for well known functions: Properly check for __builtin_ prefix.
[cparser] / builtins.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include "config.h"
21
22 #include "type_t.h"
23 #include "types.h"
24 #include "entity_t.h"
25 #include "ast_t.h"
26 #include "symbol_t.h"
27 #include "parser.h"
28 #include "builtins.h"
29 #include "lang_features.h"
30
31 static entity_t *create_builtin_function(builtin_kind_t kind, symbol_t *symbol,
32                                          type_t *function_type)
33 {
34         entity_t *const entity = allocate_entity_zero(ENTITY_FUNCTION, NAMESPACE_NORMAL, symbol);
35         entity->declaration.storage_class          = STORAGE_CLASS_EXTERN;
36         entity->declaration.declared_storage_class = STORAGE_CLASS_EXTERN;
37         entity->declaration.type                   = function_type;
38         entity->declaration.implicit               = true;
39         entity->base.source_position               = builtin_source_position;
40
41         entity->function.btk                       = kind;
42
43         record_entity(entity, /*is_definition=*/false);
44         return entity;
45 }
46
47 static symbol_t *finalize_symbol_string(void)
48 {
49         obstack_1grow(&symbol_obstack, '\0');
50         char *string = obstack_finish(&symbol_obstack);
51         symbol_t *symbol = symbol_table_insert(string);
52         if (symbol->string != string)
53                 obstack_free(&symbol_obstack, string);
54         return symbol;
55 }
56
57 static entity_t *create_gnu_builtin(builtin_kind_t kind, const char *name,
58                                     type_t *type)
59 {
60         obstack_printf(&symbol_obstack, "__builtin_%s", name);
61         symbol_t *symbol = finalize_symbol_string();
62         entity_t *entity = create_builtin_function(kind, symbol, type);
63         return entity;
64 }
65
66 static entity_t *create_gnu_builtin_firm(ir_builtin_kind kind, const char *name,
67                                          type_t *type)
68 {
69         obstack_printf(&symbol_obstack, "__builtin_%s", name);
70         symbol_t *symbol = finalize_symbol_string();
71         entity_t *entity = create_builtin_function(BUILTIN_FIRM, symbol, type);
72         entity->function.b.firm_builtin_kind = kind;
73         return entity;
74 }
75
76 static entity_t *create_gnu_builtin_libc(const char *name, type_t *type)
77 {
78         obstack_printf(&symbol_obstack, "__builtin_%s", name);
79         symbol_t *symbol = finalize_symbol_string();
80         entity_t *entity = create_builtin_function(BUILTIN_LIBC, symbol, type);
81         entity->function.actual_name = symbol_table_insert(name);
82         return entity;
83 }
84
85 static entity_t *create_gnu_builtin_chk(const char *name, unsigned chk_arg_pos,
86                                         type_t *type)
87 {
88         obstack_printf(&symbol_obstack, "__builtin___%s_chk", name);
89         symbol_t *symbol = finalize_symbol_string();
90         entity_t *entity = create_builtin_function(BUILTIN_LIBC_CHECK, symbol, type);
91         entity->function.actual_name   = symbol_table_insert(name);
92         entity->function.b.chk_arg_pos = chk_arg_pos;
93         return entity;
94 }
95
96 void create_gnu_builtins(void)
97 {
98         entity_t *(*b)(builtin_kind_t,const char*,type_t*) = create_gnu_builtin;
99         b(BUILTIN_ALLOCA,      "alloca",         make_function_1_type(type_void_ptr, type_size_t, DM_NONE));
100         b(BUILTIN_INF,         "huge_val",       make_function_0_type(type_double, DM_CONST));
101         b(BUILTIN_INF,         "huge_valf",      make_function_0_type(type_float, DM_CONST));
102         b(BUILTIN_INF,         "huge_vall",      make_function_0_type(type_long_double, DM_CONST));
103         b(BUILTIN_INF,         "inf",            make_function_0_type(type_double, DM_CONST));
104         b(BUILTIN_INF,         "inff",           make_function_0_type(type_float, DM_CONST));
105         b(BUILTIN_INF,         "infl",           make_function_0_type(type_long_double, DM_CONST));
106         b(BUILTIN_NAN,         "nan",            make_function_1_type(type_double, type_char_ptr, DM_CONST));
107         b(BUILTIN_NAN,         "nanf",           make_function_1_type(type_float, type_char_ptr, DM_CONST));
108         b(BUILTIN_NAN,         "nanl",           make_function_1_type(type_long_double, type_char_ptr, DM_CONST));
109         b(BUILTIN_VA_END,      "va_end",         make_function_1_type(type_void, type_valist, DM_NONE));
110         b(BUILTIN_EXPECT,      "expect",         make_function_2_type(type_long, type_long, type_long, DM_CONST));
111         b(BUILTIN_OBJECT_SIZE, "object_size",    make_function_2_type(type_size_t, type_void_ptr, type_int, DM_CONST));
112
113         entity_t *(*f)(ir_builtin_kind,const char*,type_t*)
114                 = create_gnu_builtin_firm;
115         f(ir_bk_return_address, "return_address", make_function_1_type(type_void_ptr, type_unsigned_int, DM_NONE));
116         f(ir_bk_frame_address,  "frame_address",  make_function_1_type(type_void_ptr, type_unsigned_int, DM_NONE));
117         f(ir_bk_ffs,            "ffs",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
118         f(ir_bk_ffs,            "ffsl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
119         f(ir_bk_ffs,            "ffsll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
120         f(ir_bk_clz,            "clz",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
121         f(ir_bk_clz,            "clzl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
122         f(ir_bk_clz,            "clzll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
123         f(ir_bk_ctz,            "ctz",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
124         f(ir_bk_ctz,            "ctzl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
125         f(ir_bk_ctz,            "ctzll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
126         f(ir_bk_popcount,       "popcount",       make_function_1_type(type_int, type_unsigned_int, DM_CONST));
127         f(ir_bk_popcount,       "popcountl",      make_function_1_type(type_int, type_unsigned_long, DM_CONST));
128         f(ir_bk_popcount,       "popcountll",     make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
129         f(ir_bk_parity,         "parity",         make_function_1_type(type_int, type_unsigned_int, DM_CONST));
130         f(ir_bk_parity,         "parityl",        make_function_1_type(type_int, type_unsigned_long, DM_CONST));
131         f(ir_bk_parity,         "parityll",       make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
132         f(ir_bk_prefetch,       "prefetch",       make_function_1_type_variadic(type_float, type_void_ptr, DM_NONE));
133         f(ir_bk_trap,           "trap",           make_function_type(type_void, 0, NULL, DM_NORETURN));
134
135         entity_t *(*l)(const char*,type_t*) = create_gnu_builtin_libc;
136         l("abort",   make_function_type(type_void, 0, NULL, DM_NORETURN));
137         l("abs",     make_function_type(type_int, 1, (type_t *[]) { type_int }, DM_CONST));
138         l("labs",    make_function_type(type_long, 1, (type_t *[]) { type_long }, DM_CONST));
139         l("llabs",   make_function_type(type_long_long, 1, (type_t *[]) { type_long_long }, DM_CONST));
140         l("memcpy",  make_function_type(type_void_ptr, 3, (type_t *[]) { type_void_ptr_restrict, type_const_void_ptr_restrict, type_size_t }, DM_NONE));
141         l("memcmp",  make_function_type(type_int, 3, (type_t *[]) { type_const_void_ptr, type_const_void_ptr, type_size_t }, DM_PURE));
142         l("memset",  make_function_type(type_void_ptr, 3, (type_t *[]) { type_void_ptr, type_int, type_size_t }, DM_NONE));
143         l("memmove", make_function_type(type_void_ptr, 3, (type_t *[]) { type_void_ptr_restrict, type_const_void_ptr_restrict, type_size_t }, DM_NONE));
144         l("strcat",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
145         l("strncat", make_function_type(type_char_ptr, 3, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t }, DM_NONE));
146         l("strlen",  make_function_type(type_size_t, 1, (type_t *[]) { type_const_char_ptr }, DM_PURE));
147         l("strcmp",  make_function_type(type_int, 2, (type_t *[]) { type_const_char_ptr, type_const_char_ptr }, DM_PURE));
148         l("strcpy",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
149         l("stpcpy",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
150         l("strncpy", make_function_type(type_char_ptr, 3, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t }, DM_NONE));
151         l("exit",    make_function_type(type_void, 1, (type_t *[]) { type_int }, DM_NORETURN));
152         l("malloc",  make_function_type(type_void_ptr, 1, (type_t *[]) { type_size_t }, DM_MALLOC));
153
154         entity_t *(*c)(const char*,unsigned,type_t*) = create_gnu_builtin_chk;
155         c("memcpy",  3, make_function_type(type_void_ptr, 4, (type_t *[]) { type_void_ptr_restrict, type_const_void_ptr_restrict, type_size_t, type_size_t}, DM_NONE));
156         c("memset",  3, make_function_type(type_void_ptr, 4, (type_t *[]) { type_void_ptr, type_int, type_size_t, type_size_t }, DM_NONE));
157         c("memmove", 3, make_function_type(type_void_ptr, 4, (type_t *[]) { type_void_ptr_restrict, type_const_void_ptr_restrict, type_size_t, type_size_t }, DM_NONE));
158         c("strcat",  2, make_function_type(type_char_ptr, 3, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t }, DM_NONE));
159         c("strncat", 3, make_function_type(type_char_ptr, 4, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t, type_size_t }, DM_NONE));
160         c("strcpy",  2, make_function_type(type_char_ptr, 3, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t }, DM_NONE));
161         c("stpcpy",  2, make_function_type(type_char_ptr, 3, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t }, DM_NONE));
162         c("strncpy", 3, make_function_type(type_char_ptr, 4, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict, type_size_t, type_size_t }, DM_NONE));
163
164         /* TODO: gcc has a LONG list of builtin functions (nearly everything from
165          * C89-C99 and others. Complete this */
166 }
167
168 static entity_t *create_intrinsic_firm(ir_builtin_kind kind, const char *name,
169                                        type_t *type)
170 {
171         symbol_t *symbol = symbol_table_insert(name);
172         entity_t *entity = create_builtin_function(BUILTIN_FIRM, symbol, type);
173         entity->function.b.firm_builtin_kind = kind;
174         return entity;
175 }
176
177 static entity_t *create_intrinsic(builtin_kind_t kind, const char *name,
178                                   type_t *type)
179 {
180         symbol_t *symbol = symbol_table_insert(name);
181         entity_t *entity = create_builtin_function(kind, symbol, type);
182         return entity;
183 }
184
185 void create_microsoft_intrinsics(void)
186 {
187         entity_t *(*i)(builtin_kind_t,const char*,type_t*) = create_intrinsic;
188         entity_t *(*f)(ir_builtin_kind,const char*,type_t*) = create_intrinsic_firm;
189         /* intrinsics for all architectures */
190         i(BUILTIN_ROTL,   "_rotl",                make_function_2_type(type_unsigned_int,   type_unsigned_int, type_int, DM_CONST));
191         i(BUILTIN_ROTL,   "_rotl64",              make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int, DM_CONST));
192         i(BUILTIN_ROTR,   "_rotr",                make_function_2_type(type_unsigned_int,   type_unsigned_int, type_int, DM_CONST));
193         i(BUILTIN_ROTR,   "_rotr64",              make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int, DM_CONST));
194
195         f(ir_bk_bswap,    "_byteswap_ushort",     make_function_1_type(type_unsigned_short, type_unsigned_short, DM_CONST));
196         f(ir_bk_bswap,    "_byteswap_ulong",      make_function_1_type(type_unsigned_long,  type_unsigned_long, DM_CONST));
197         f(ir_bk_bswap,    "_byteswap_uint64",     make_function_1_type(type_unsigned_int64, type_unsigned_int64, DM_CONST));
198
199         f(ir_bk_debugbreak,     "__debugbreak",   make_function_0_type(type_void, DM_NONE));
200         f(ir_bk_return_address, "_ReturnAddress", make_function_0_type(type_void_ptr, DM_NONE));
201         f(ir_bk_popcount,       "__popcount",     make_function_1_type(type_unsigned_int, type_unsigned_int, DM_CONST));
202
203         /* x86/x64 only */
204         f(ir_bk_inport,   "__inbyte",             make_function_1_type(type_unsigned_char, type_unsigned_short, DM_NONE));
205         f(ir_bk_inport,   "__inword",             make_function_1_type(type_unsigned_short, type_unsigned_short, DM_NONE));
206         f(ir_bk_inport,   "__indword",            make_function_1_type(type_unsigned_long, type_unsigned_short, DM_NONE));
207         f(ir_bk_outport,  "__outbyte",            make_function_2_type(type_void, type_unsigned_short, type_unsigned_char, DM_NONE));
208         f(ir_bk_outport,  "__outword",            make_function_2_type(type_void, type_unsigned_short, type_unsigned_short, DM_NONE));
209         f(ir_bk_outport,  "__outdword",           make_function_2_type(type_void, type_unsigned_short, type_unsigned_long, DM_NONE));
210         f(ir_bk_trap,     "__ud2",                make_function_type(type_void, 0, NULL, DM_NORETURN));
211 }
212
213 static type_t *add_type_modifier(type_t *orig_type, decl_modifiers_t modifiers)
214 {
215         type_t *type = skip_typeref(orig_type);
216
217         assert(type->kind == TYPE_FUNCTION);
218         if ((type->function.modifiers & modifiers) == modifiers)
219                 return orig_type;
220
221         type_t *new_type = duplicate_type(type);
222         new_type->function.modifiers |= modifiers;
223         return identify_new_type(new_type);
224 }
225
226 void adapt_special_functions(function_t *function)
227 {
228         symbol_t *symbol = function->base.base.symbol;
229         if (symbol == NULL)
230                 return;
231         const char *name = symbol->string;
232
233         /* the following list of names is taken from gcc (calls.c) */
234
235         /* Disregard prefix _, __, __x or __builtin_.  */
236         if (name[0] == '_') {
237                 if (!strncmp(name + 1, "_builtin_", 9))
238                         name += 10;
239                 else if (name[1] == '_' && name[2] == 'x')
240                         name += 3;
241                 else if (name[1] == '_')
242                         name += 2;
243                 else
244                         name += 1;
245         }
246
247         if (name[0] == 's') {
248                 if ((name[1] == 'e' && (!strcmp(name, "setjmp")
249                                      || !strcmp(name, "setjmp_syscall")))
250                     || (name[1] == 'i' && !strcmp(name, "sigsetjmp"))
251                     || (name[1] == 'a' && !strcmp(name, "savectx"))) {
252                         function->base.type
253                                 = add_type_modifier(function->base.type, DM_RETURNS_TWICE);
254                 } else if (name[1] == 'i' && !strcmp(name, "siglongjmp")) {
255                         function->base.type
256                                 = add_type_modifier(function->base.type, DM_NORETURN);
257                 }
258         } else if ((name[0] == 'q' && !strcmp(name, "qsetjmp"))
259                    || (name[0] == 'v' && !strcmp(name, "vfork"))
260                    || (name[0] == 'g' && !strcmp(name, "getcontext"))) {
261                 function->base.type
262                         = add_type_modifier(function->base.type, DM_RETURNS_TWICE);
263         } else if (name[0] == 'l' && !strcmp(name, "longjmp")) {
264                 function->base.type
265                         = add_type_modifier(function->base.type, DM_NORETURN);
266         }
267 }