ast2firm: Factorise code to convert a value to its storage type.
[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 "adt/strutil.h"
23 #include "type_t.h"
24 #include "types.h"
25 #include "entity_t.h"
26 #include "ast_t.h"
27 #include "symbol_t.h"
28 #include "parser.h"
29 #include "builtins.h"
30 #include "lang_features.h"
31
32 static entity_t *create_builtin_function(builtin_kind_t kind, symbol_t *symbol,
33                                          type_t *function_type)
34 {
35         entity_t *const entity = allocate_entity_zero(ENTITY_FUNCTION, NAMESPACE_NORMAL, symbol, &builtin_position);
36         entity->declaration.storage_class          = STORAGE_CLASS_EXTERN;
37         entity->declaration.declared_storage_class = STORAGE_CLASS_EXTERN;
38         entity->declaration.type                   = function_type;
39         entity->declaration.implicit               = true;
40         entity->function.btk                       = kind;
41
42         record_entity(entity, /*is_definition=*/false);
43         return entity;
44 }
45
46 static symbol_t *finalize_symbol_string(void)
47 {
48         obstack_1grow(&symbol_obstack, '\0');
49         char *string = obstack_finish(&symbol_obstack);
50         symbol_t *symbol = symbol_table_insert(string);
51         if (symbol->string != string)
52                 obstack_free(&symbol_obstack, string);
53         return symbol;
54 }
55
56 static entity_t *create_gnu_builtin(builtin_kind_t kind, const char *name,
57                                     type_t *type)
58 {
59         obstack_printf(&symbol_obstack, "__builtin_%s", name);
60         symbol_t *symbol = finalize_symbol_string();
61         entity_t *entity = create_builtin_function(kind, symbol, type);
62         return entity;
63 }
64
65 static entity_t *create_gnu_builtin_firm(ir_builtin_kind kind, const char *name,
66                                          type_t *type)
67 {
68         obstack_printf(&symbol_obstack, "__builtin_%s", name);
69         symbol_t *symbol = finalize_symbol_string();
70         entity_t *entity = create_builtin_function(BUILTIN_FIRM, symbol, type);
71         entity->function.b.firm_builtin_kind = kind;
72         return entity;
73 }
74
75 static entity_t *create_gnu_builtin_libc(const char *name, type_t *type)
76 {
77         obstack_printf(&symbol_obstack, "__builtin_%s", name);
78         symbol_t *symbol = finalize_symbol_string();
79         entity_t *entity = create_builtin_function(BUILTIN_LIBC, symbol, type);
80         entity->function.actual_name = symbol_table_insert(name);
81         return entity;
82 }
83
84 static entity_t *create_gnu_builtin_chk(const char *name, unsigned chk_arg_pos,
85                                         type_t *type)
86 {
87         obstack_printf(&symbol_obstack, "__builtin___%s_chk", name);
88         symbol_t *symbol = finalize_symbol_string();
89         entity_t *entity = create_builtin_function(BUILTIN_LIBC_CHECK, symbol, type);
90         entity->function.actual_name   = symbol_table_insert(name);
91         entity->function.b.chk_arg_pos = chk_arg_pos;
92         return entity;
93 }
94
95 void create_gnu_builtins(void)
96 {
97         entity_t *(*b)(builtin_kind_t,const char*,type_t*) = create_gnu_builtin;
98         b(BUILTIN_ALLOCA,      "alloca",         make_function_1_type(type_void_ptr, type_size_t, DM_NONE));
99         b(BUILTIN_INF,         "huge_val",       make_function_0_type(type_double, DM_CONST));
100         b(BUILTIN_INF,         "huge_valf",      make_function_0_type(type_float, DM_CONST));
101         b(BUILTIN_INF,         "huge_vall",      make_function_0_type(type_long_double, DM_CONST));
102         b(BUILTIN_INF,         "inf",            make_function_0_type(type_double, DM_CONST));
103         b(BUILTIN_INF,         "inff",           make_function_0_type(type_float, DM_CONST));
104         b(BUILTIN_INF,         "infl",           make_function_0_type(type_long_double, DM_CONST));
105         b(BUILTIN_NAN,         "nan",            make_function_1_type(type_double, type_char_ptr, DM_CONST));
106         b(BUILTIN_NAN,         "nanf",           make_function_1_type(type_float, type_char_ptr, DM_CONST));
107         b(BUILTIN_NAN,         "nanl",           make_function_1_type(type_long_double, type_char_ptr, DM_CONST));
108         b(BUILTIN_VA_END,      "va_end",         make_function_1_type(type_void, type_valist, DM_NONE));
109         b(BUILTIN_EXPECT,      "expect",         make_function_2_type(type_long, type_long, type_long, DM_CONST));
110         b(BUILTIN_OBJECT_SIZE, "object_size",    make_function_2_type(type_size_t, type_void_ptr, type_int, DM_CONST));
111
112         entity_t *(*f)(ir_builtin_kind,const char*,type_t*)
113                 = create_gnu_builtin_firm;
114         f(ir_bk_bswap,          "bswap32",        make_function_1_type(type_int32_t, type_int32_t, DM_CONST));
115         f(ir_bk_bswap,          "bswap64",        make_function_1_type(type_int64_t, type_int64_t, DM_CONST));
116         f(ir_bk_clz,            "clz",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
117         f(ir_bk_clz,            "clzl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
118         f(ir_bk_clz,            "clzll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
119         f(ir_bk_ctz,            "ctz",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
120         f(ir_bk_ctz,            "ctzl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
121         f(ir_bk_ctz,            "ctzll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
122         f(ir_bk_ffs,            "ffs",            make_function_1_type(type_int, type_unsigned_int, DM_CONST));
123         f(ir_bk_ffs,            "ffsl",           make_function_1_type(type_int, type_unsigned_long, DM_CONST));
124         f(ir_bk_ffs,            "ffsll",          make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
125         f(ir_bk_frame_address,  "frame_address",  make_function_1_type(type_void_ptr, type_unsigned_int, DM_NONE));
126         f(ir_bk_parity,         "parity",         make_function_1_type(type_int, type_unsigned_int, DM_CONST));
127         f(ir_bk_parity,         "parityl",        make_function_1_type(type_int, type_unsigned_long, DM_CONST));
128         f(ir_bk_parity,         "parityll",       make_function_1_type(type_int, type_unsigned_long_long, DM_CONST));
129         f(ir_bk_popcount,       "popcount",       make_function_1_type(type_int, type_unsigned_int, DM_CONST));
130         f(ir_bk_popcount,       "popcountl",      make_function_1_type(type_int, type_unsigned_long, DM_CONST));
131         f(ir_bk_popcount,       "popcountll",     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_return_address, "return_address", make_function_1_type(type_void_ptr, type_unsigned_int, DM_NONE));
134         f(ir_bk_trap,           "trap",           make_function_type(type_void, 0, NULL, DM_NORETURN));
135
136         entity_t *(*l)(const char*,type_t*) = create_gnu_builtin_libc;
137         l("abort",   make_function_type(type_void, 0, NULL, DM_NORETURN));
138         l("abs",     make_function_type(type_int, 1, (type_t *[]) { type_int }, DM_CONST));
139         l("fabs",    make_function_type(type_double, 1, (type_t *[]) { type_double }, DM_CONST));
140         l("fabsf",   make_function_type(type_float, 1, (type_t *[]) { type_float }, DM_CONST));
141         l("fabsl",   make_function_type(type_long_double, 1, (type_t *[]) { type_long_double }, DM_CONST));
142         l("labs",    make_function_type(type_long, 1, (type_t *[]) { type_long }, DM_CONST));
143         l("llabs",   make_function_type(type_long_long, 1, (type_t *[]) { type_long_long }, DM_CONST));
144         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));
145         l("memcmp",  make_function_type(type_int, 3, (type_t *[]) { type_const_void_ptr, type_const_void_ptr, type_size_t }, DM_PURE));
146         l("memset",  make_function_type(type_void_ptr, 3, (type_t *[]) { type_void_ptr, type_int, type_size_t }, DM_NONE));
147         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));
148         l("strcat",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
149         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));
150         l("strlen",  make_function_type(type_size_t, 1, (type_t *[]) { type_const_char_ptr }, DM_PURE));
151         l("strcmp",  make_function_type(type_int, 2, (type_t *[]) { type_const_char_ptr, type_const_char_ptr }, DM_PURE));
152         l("strcpy",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
153         l("stpcpy",  make_function_type(type_char_ptr, 2, (type_t *[]) { type_char_ptr_restrict, type_const_char_ptr_restrict }, DM_NONE));
154         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));
155         l("exit",    make_function_type(type_void, 1, (type_t *[]) { type_int }, DM_NORETURN));
156         l("malloc",  make_function_type(type_void_ptr, 1, (type_t *[]) { type_size_t }, DM_MALLOC));
157
158         entity_t *(*c)(const char*,unsigned,type_t*) = create_gnu_builtin_chk;
159         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));
160         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));
161         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));
162         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));
163         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));
164         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));
165         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));
166         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));
167
168         /* TODO: gcc has a LONG list of builtin functions (nearly everything from
169          * C89-C99 and others. Complete this */
170 }
171
172 static entity_t *create_intrinsic_firm(ir_builtin_kind kind, const char *name,
173                                        type_t *type)
174 {
175         symbol_t *symbol = symbol_table_insert(name);
176         entity_t *entity = create_builtin_function(BUILTIN_FIRM, symbol, type);
177         entity->function.b.firm_builtin_kind = kind;
178         return entity;
179 }
180
181 static entity_t *create_intrinsic(builtin_kind_t kind, const char *name,
182                                   type_t *type)
183 {
184         symbol_t *symbol = symbol_table_insert(name);
185         entity_t *entity = create_builtin_function(kind, symbol, type);
186         return entity;
187 }
188
189 void create_microsoft_intrinsics(void)
190 {
191         entity_t *(*i)(builtin_kind_t,const char*,type_t*) = create_intrinsic;
192         entity_t *(*f)(ir_builtin_kind,const char*,type_t*) = create_intrinsic_firm;
193         /* intrinsics for all architectures */
194         i(BUILTIN_ROTL,   "_rotl",                make_function_2_type(type_unsigned_int,   type_unsigned_int, type_int, DM_CONST));
195         i(BUILTIN_ROTL,   "_rotl64",              make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int, DM_CONST));
196         i(BUILTIN_ROTR,   "_rotr",                make_function_2_type(type_unsigned_int,   type_unsigned_int, type_int, DM_CONST));
197         i(BUILTIN_ROTR,   "_rotr64",              make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int, DM_CONST));
198
199         f(ir_bk_bswap,    "_byteswap_ushort",     make_function_1_type(type_unsigned_short, type_unsigned_short, DM_CONST));
200         f(ir_bk_bswap,    "_byteswap_ulong",      make_function_1_type(type_unsigned_long,  type_unsigned_long, DM_CONST));
201         f(ir_bk_bswap,    "_byteswap_uint64",     make_function_1_type(type_unsigned_int64, type_unsigned_int64, DM_CONST));
202
203         f(ir_bk_debugbreak,     "__debugbreak",   make_function_0_type(type_void, DM_NONE));
204         f(ir_bk_return_address, "_ReturnAddress", make_function_0_type(type_void_ptr, DM_NONE));
205         f(ir_bk_popcount,       "__popcount",     make_function_1_type(type_unsigned_int, type_unsigned_int, DM_CONST));
206
207         /* x86/x64 only */
208         f(ir_bk_inport,   "__inbyte",             make_function_1_type(type_unsigned_char, type_unsigned_short, DM_NONE));
209         f(ir_bk_inport,   "__inword",             make_function_1_type(type_unsigned_short, type_unsigned_short, DM_NONE));
210         f(ir_bk_inport,   "__indword",            make_function_1_type(type_unsigned_long, type_unsigned_short, DM_NONE));
211         f(ir_bk_outport,  "__outbyte",            make_function_2_type(type_void, type_unsigned_short, type_unsigned_char, DM_NONE));
212         f(ir_bk_outport,  "__outword",            make_function_2_type(type_void, type_unsigned_short, type_unsigned_short, DM_NONE));
213         f(ir_bk_outport,  "__outdword",           make_function_2_type(type_void, type_unsigned_short, type_unsigned_long, DM_NONE));
214         f(ir_bk_trap,     "__ud2",                make_function_type(type_void, 0, NULL, DM_NORETURN));
215 }
216
217 static type_t *add_type_modifier(type_t *orig_type, decl_modifiers_t modifiers)
218 {
219         type_t *type = skip_typeref(orig_type);
220
221         assert(type->kind == TYPE_FUNCTION);
222         if ((type->function.modifiers & modifiers) == modifiers)
223                 return orig_type;
224
225         type_t *new_type = duplicate_type(type);
226         new_type->function.modifiers |= modifiers;
227         return identify_new_type(new_type);
228 }
229
230 void adapt_special_functions(function_t *function)
231 {
232         symbol_t *symbol = function->base.base.symbol;
233         if (symbol == NULL)
234                 return;
235         const char *name = symbol->string;
236
237         /* the following list of names is taken from gcc (calls.c) */
238
239         /* Disregard prefix _, __, __x or __builtin_.  */
240         if (name[0] == '_') {
241                 if (strstart(name + 1, "_builtin_"))
242                         name += 10;
243                 else if (name[1] == '_' && name[2] == 'x')
244                         name += 3;
245                 else if (name[1] == '_')
246                         name += 2;
247                 else
248                         name += 1;
249         }
250
251         if (name[0] == 's') {
252                 if ((name[1] == 'e' && (streq(name, "setjmp")
253                                      || streq(name, "setjmp_syscall")))
254                     || (name[1] == 'i' && streq(name, "sigsetjmp"))
255                     || (name[1] == 'a' && streq(name, "savectx"))) {
256                         function->base.type
257                                 = add_type_modifier(function->base.type, DM_RETURNS_TWICE);
258                 } else if (name[1] == 'i' && streq(name, "siglongjmp")) {
259                         function->base.type
260                                 = add_type_modifier(function->base.type, DM_NORETURN);
261                 }
262         } else if ((name[0] == 'q' && streq(name, "qsetjmp"))
263                    || (name[0] == 'v' && streq(name, "vfork"))
264                    || (name[0] == 'g' && streq(name, "getcontext"))) {
265                 function->base.type
266                         = add_type_modifier(function->base.type, DM_RETURNS_TWICE);
267         } else if (name[0] == 'l' && streq(name, "longjmp")) {
268                 function->base.type
269                         = add_type_modifier(function->base.type, DM_NORETURN);
270         }
271 }