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
22 #include <libfirm/firm.h>
29 #include "diagnostic.h"
31 #include "lang_features.h"
32 #include "adt/error.h"
34 static struct obstack obst;
36 static void mangle_type(type_t *type);
38 static char get_atomic_type_mangle(atomic_type_kind_t kind)
41 case ATOMIC_TYPE_VOID: return 'v';
42 case ATOMIC_TYPE_WCHAR_T: return 'w';
43 case ATOMIC_TYPE_BOOL: return 'b';
44 case ATOMIC_TYPE_CHAR: return 'c';
45 case ATOMIC_TYPE_SCHAR: return 'a';
46 case ATOMIC_TYPE_UCHAR: return 'h';
47 case ATOMIC_TYPE_INT: return 'i';
48 case ATOMIC_TYPE_UINT: return 'j';
49 case ATOMIC_TYPE_SHORT: return 's';
50 case ATOMIC_TYPE_USHORT: return 't';
51 case ATOMIC_TYPE_LONG: return 'l';
52 case ATOMIC_TYPE_ULONG: return 'm';
53 case ATOMIC_TYPE_LONGLONG: return 'x';
54 case ATOMIC_TYPE_ULONGLONG: return 'y';
55 case ATOMIC_TYPE_LONG_DOUBLE: return 'e';
56 case ATOMIC_TYPE_FLOAT: return 'f';
57 case ATOMIC_TYPE_DOUBLE: return 'd';
59 panic("invalid atomic type");
62 static void mangle_atomic_type(const atomic_type_t *type)
64 obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
67 static void mangle_pointer_type(const pointer_type_t *type)
69 obstack_1grow(&obst, 'P');
70 mangle_type(type->points_to);
73 static void mangle_reference_type(const reference_type_t *type)
75 obstack_1grow(&obst, 'R');
76 mangle_type(type->refers_to);
79 static void mangle_parameters(const function_type_t *type)
81 if (type->unspecified_parameters)
82 panic("can't mangle unspecified parameter types");
83 if (type->kr_style_parameters)
84 panic("can't mangle kr_style_parameters type");
86 const function_parameter_t *parameter = type->parameters;
87 if (parameter != NULL) {
88 for ( ; parameter != NULL; parameter = parameter->next) {
89 mangle_type(parameter->type);
92 obstack_1grow(&obst, 'z');
95 obstack_1grow(&obst, 'v');
99 static void mangle_function_type(const function_type_t *type)
101 obstack_1grow(&obst, 'F');
102 if (type->linkage == LINKAGE_C) {
103 obstack_1grow(&obst, 'Y');
106 mangle_type(type->return_type);
107 mangle_parameters(type);
109 obstack_1grow(&obst, 'E');
112 static void print_name(const char* name)
114 obstack_printf(&obst, "%u%s", (unsigned)strlen(name), name);
117 static void mangle_class_type(const compound_type_t *type)
119 const symbol_t *sym = type->compound->base.symbol;
121 if (type->compound->alias == NULL)
122 panic("mangling anonymous type");
123 sym = type->compound->alias->base.symbol;
125 print_name(sym->string);
128 static void mangle_enum_type(const enum_type_t *type)
130 const symbol_t *sym = type->enume->base.symbol;
132 if (type->enume->alias == NULL)
133 panic("mangling anonymous type");
134 sym = type->enume->alias->base.symbol;
136 print_name(sym->string);
139 static void mangle_array_type(const array_type_t *type)
142 obstack_1grow(&obst, 'A');
143 obstack_1grow(&obst, '_');
144 } else if (type->size_constant) {
145 obstack_printf(&obst, "A%u_", (unsigned) type->size);
147 panic("mangling of non-constant sized array types not implemented yet");
149 mangle_type(type->element_type);
152 static void mangle_complex_type(const atomic_type_t *type)
154 obstack_1grow(&obst, 'C');
155 obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
158 static void mangle_imaginary_type(const atomic_type_t *type)
160 obstack_1grow(&obst, 'G');
161 obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
164 static void mangle_qualifiers(type_qualifiers_t qualifiers)
166 #if 0 /* Do not mangle restrict qualifiers. GCC doesn't either */
167 if (qualifiers & TYPE_QUALIFIER_RESTRICT)
168 obstack_1grow(&obst, 'r');
170 if (qualifiers & TYPE_QUALIFIER_VOLATILE)
171 obstack_1grow(&obst, 'V');
172 if (qualifiers & TYPE_QUALIFIER_CONST)
173 obstack_1grow(&obst, 'K');
175 /* handle MS extended qualifiers? */
178 static void mangle_type(type_t *orig_type)
180 type_t *type = skip_typeref(orig_type);
182 mangle_qualifiers(type->base.qualifiers);
184 switch (type->kind) {
186 mangle_atomic_type(&type->atomic);
189 mangle_pointer_type(&type->pointer);
192 mangle_reference_type(&type->reference);
195 mangle_function_type(&type->function);
197 case TYPE_COMPOUND_STRUCT:
198 case TYPE_COMPOUND_UNION:
199 mangle_class_type(&type->compound);
202 mangle_enum_type(&type->enumt);
205 mangle_array_type(&type->array);
208 mangle_complex_type(&type->atomic);
211 mangle_imaginary_type(&type->atomic);
214 panic("error type encountered while mangling");
217 panic("typeref not resolved while manging?!?");
219 panic("invalid type encountered while mangling");
222 static void mangle_namespace(entity_t *entity)
224 for (entity_t *e = entity->base.parent_entity; e != NULL;
225 e = e->base.parent_entity) {
226 /* TODO: we need something similar (or the same?) for classes */
227 if (e->kind == ENTITY_NAMESPACE) {
229 print_name(e->base.symbol->string);
235 static void mangle_entity(entity_t *entity)
237 obstack_1grow(&obst, '_');
238 obstack_1grow(&obst, 'Z');
240 if (entity->base.parent_entity != NULL) {
241 obstack_1grow(&obst, 'N');
242 mangle_namespace(entity);
245 print_name(entity->base.symbol->string);
247 if (entity->kind == ENTITY_FUNCTION) {
248 mangle_parameters(&entity->declaration.type->function);
252 static ident *make_id_from_obst(void)
254 size_t size = obstack_object_size(&obst);
255 char *str = obstack_finish(&obst);
256 ident *id = new_id_from_chars(str, size);
257 obstack_free(&obst, str);
262 * Mangles an entity linker (ld) name for win32 usage.
264 * @param ent the entity to be mangled
265 * @param declaration the declaration
267 ident *create_name_win32(entity_t *entity)
269 struct obstack *o = &obst;
271 assert(is_declaration(entity));
273 if (entity->kind == ENTITY_FUNCTION) {
274 type_t *type = skip_typeref(entity->declaration.type);
275 assert(is_type_function(type));
277 if (entity->declaration.modifiers & DM_DLLIMPORT)
278 /* add prefix for imported symbols */
279 obstack_printf(o, "__imp_");
281 cc_kind_t cc = type->function.calling_convention;
283 /* calling convention prefix */
287 case CC_STDCALL: obstack_1grow(o, '_'); break;
288 case CC_FASTCALL: obstack_1grow(o, '@'); break;
289 default: panic("unhandled calling convention");
292 switch (type->function.linkage) {
294 obstack_printf(o, "%s", entity->base.symbol->string);
298 mangle_entity(entity);
302 /* calling convention suffix */
310 ir_type *irtype = get_ir_type(entity->declaration.type);
312 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
313 size += get_type_size_bytes(get_method_param_type(irtype, i));
315 obstack_printf(o, "@%u", size);
320 panic("unhandled calling convention");
323 obstack_printf(o, "_%s", entity->base.symbol->string);
326 return make_id_from_obst();
330 * Mangles an entity linker (ld) name for Linux ELF usage.
332 * @param ent the entity to be mangled
333 * @param declaration the declaration
335 ident *create_name_linux_elf(entity_t *entity)
337 const char *name = entity->base.symbol->string;
339 if (entity->kind == ENTITY_FUNCTION) {
340 type_t *type = skip_typeref(entity->declaration.type);
341 assert(is_type_function(type));
342 switch (type->function.linkage) {
344 if (entity->function.actual_name != NULL)
345 name = entity->function.actual_name->string;
348 // TODO What about __REDIRECT/actual_name with mangling?
349 mangle_entity(entity);
350 return make_id_from_obst();
354 return new_id_from_str(name);
358 * Mangles an entity linker (ld) name for Mach-O usage.
360 * @param ent the entity to be mangled
361 * @param declaration the declaration
363 ident *create_name_macho(entity_t *entity)
365 if (entity->kind == ENTITY_FUNCTION) {
366 type_t *type = skip_typeref(entity->declaration.type);
367 assert(is_type_function(type));
369 switch (type->function.linkage) {
371 if (entity->function.actual_name != NULL)
372 return new_id_from_str(entity->function.actual_name->string);
377 obstack_printf(&obst, "_%s", entity->base.symbol->string);
378 return make_id_from_obst();
381 void init_mangle(void)
386 void exit_mangle(void)
388 obstack_free(&obst, NULL);