2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
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.
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.
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
29 #include "lang_features.h"
31 static entity_t *create_builtin_function(builtin_kind_t kind, symbol_t *symbol,
32 type_t *function_type)
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;
41 entity->function.btk = kind;
43 record_entity(entity, /*is_definition=*/false);
47 static symbol_t *finalize_symbol_string(void)
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);
57 static entity_t *create_gnu_builtin(builtin_kind_t kind, const char *name,
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);
66 static entity_t *create_gnu_builtin_firm(ir_builtin_kind kind, const char *name,
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;
76 static entity_t *create_gnu_builtin_libc(const char *name, type_t *type)
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);
85 static entity_t *create_gnu_builtin_chk(const char *name, unsigned chk_arg_pos,
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;
96 void create_gnu_builtins(void)
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));
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));
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));
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));
164 /* TODO: gcc has a LONG list of builtin functions (nearly everything from
165 * C89-C99 and others. Complete this */
168 static entity_t *create_intrinsic_firm(ir_builtin_kind kind, const char *name,
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;
177 static entity_t *create_intrinsic(builtin_kind_t kind, const char *name,
180 symbol_t *symbol = symbol_table_insert(name);
181 entity_t *entity = create_builtin_function(kind, symbol, type);
185 void create_microsoft_intrinsics(void)
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));
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));
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));
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));