2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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_INVALID: break;
42 case ATOMIC_TYPE_VOID: return 'v';
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 in mangler");
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_parameters(const function_type_t *type)
75 if (type->unspecified_parameters)
76 panic("can't mangle unspecified parameter types");
77 if (type->kr_style_parameters)
78 panic("can't mangle kr_style_parameters type");
80 const function_parameter_t *parameter = type->parameters;
81 if (parameter != NULL) {
82 for ( ; parameter != NULL; parameter = parameter->next) {
83 mangle_type(parameter->type);
86 obstack_1grow(&obst, 'z');
89 obstack_1grow(&obst, 'v');
93 static void mangle_function_type(const function_type_t *type)
95 obstack_1grow(&obst, 'F');
96 if (type->linkage == LINKAGE_C) {
97 obstack_1grow(&obst, 'Y');
100 mangle_type(type->return_type);
101 mangle_parameters(type);
103 obstack_1grow(&obst, 'E');
106 static void print_name(const char* name)
108 obstack_printf(&obst, "%u%s", (unsigned)strlen(name), name);
111 static void mangle_class_type(const compound_type_t *type)
113 const symbol_t *sym = type->compound->base.symbol;
115 if (type->compound->alias == NULL)
116 panic("mangling anonymous type");
117 sym = type->compound->alias->base.symbol;
119 print_name(sym->string);
122 static void mangle_enum_type(const enum_type_t *type)
124 const symbol_t *sym = type->enume->base.symbol;
126 if (type->enume->alias == NULL)
127 panic("mangling anonymous type");
128 sym = type->enume->alias->base.symbol;
130 print_name(sym->string);
133 static void mangle_array_type(const array_type_t *type)
136 obstack_1grow(&obst, 'A');
137 obstack_1grow(&obst, '_');
138 } else if (type->size_constant) {
139 obstack_printf(&obst, "A%u_", (unsigned) type->size);
141 panic("mangling of non-constant sized array types not implemented yet");
143 mangle_type(type->element_type);
146 static void mangle_complex_type(const complex_type_t *type)
148 obstack_1grow(&obst, 'C');
149 obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
152 static void mangle_imaginary_type(const imaginary_type_t *type)
154 obstack_1grow(&obst, 'G');
155 obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
158 static void mangle_qualifiers(type_qualifiers_t qualifiers)
160 #if 0 /* Do not mangle restrict qualifiers. GCC doesn't either */
161 if (qualifiers & TYPE_QUALIFIER_RESTRICT)
162 obstack_1grow(&obst, 'r');
164 if (qualifiers & TYPE_QUALIFIER_VOLATILE)
165 obstack_1grow(&obst, 'V');
166 if (qualifiers & TYPE_QUALIFIER_CONST)
167 obstack_1grow(&obst, 'K');
169 /* handle MS extended qualifiers? */
172 static void mangle_type(type_t *orig_type)
174 type_t *type = skip_typeref(orig_type);
176 mangle_qualifiers(type->base.qualifiers);
178 switch (type->kind) {
180 mangle_atomic_type(&type->atomic);
183 mangle_pointer_type(&type->pointer);
186 mangle_function_type(&type->function);
188 case TYPE_COMPOUND_STRUCT:
189 case TYPE_COMPOUND_UNION:
190 mangle_class_type(&type->compound);
193 mangle_enum_type(&type->enumt);
196 mangle_array_type(&type->array);
199 mangle_complex_type(&type->complex);
202 mangle_imaginary_type(&type->imaginary);
205 panic("invalid type encountered while mangling");
207 panic("error type encountered while mangling");
211 panic("typeref not resolved while manging?!?");
214 panic("no mangling for this type implemented yet");
217 panic("invalid type encountered while mangling");
220 static void mangle_entity(entity_t *entity)
222 obstack_1grow(&obst, '_');
223 obstack_1grow(&obst, 'Z');
225 /* TODO: mangle scope */
227 print_name(entity->base.symbol->string);
229 if (entity->kind == ENTITY_FUNCTION) {
230 mangle_parameters(&entity->declaration.type->function);
234 static ident *make_id_from_obst(void)
236 size_t size = obstack_object_size(&obst);
237 char *str = obstack_finish(&obst);
238 ident *id = new_id_from_chars(str, size);
239 obstack_free(&obst, str);
244 * Mangles an entity linker (ld) name for win32 usage.
246 * @param ent the entity to be mangled
247 * @param declaration the declaration
249 ident *create_name_win32(entity_t *entity)
251 struct obstack *o = &obst;
253 assert(is_declaration(entity));
255 if (entity->declaration.modifiers & DM_DLLIMPORT) {
256 /* add prefix for imported symbols */
257 obstack_printf(o, "__imp_");
260 if (entity->kind == ENTITY_FUNCTION) {
261 cc_kind_t cc = entity->declaration.type->function.calling_convention;
263 /* calling convention prefix */
267 case CC_STDCALL: obstack_1grow(o, '_'); break;
268 case CC_FASTCALL: obstack_1grow(o, '@'); break;
269 default: panic("unhandled calling convention");
272 switch (entity->declaration.type->function.linkage) {
273 case LINKAGE_INVALID:
277 obstack_printf(o, "%s", entity->base.symbol->string);
281 mangle_entity(entity);
285 /* calling convention suffix */
293 ir_type *irtype = get_ir_type(entity->declaration.type);
295 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
296 size += get_type_size_bytes(get_method_param_type(irtype, i));
298 obstack_printf(o, "@%u", size);
303 panic("unhandled calling convention");
306 obstack_printf(o, "_%s", entity->base.symbol->string);
309 return make_id_from_obst();
313 * Mangles an entity linker (ld) name for Linux ELF usage.
315 * @param ent the entity to be mangled
316 * @param declaration the declaration
318 ident *create_name_linux_elf(entity_t *entity)
320 bool needs_mangling = false;
322 if (entity->kind == ENTITY_FUNCTION) {
323 switch (entity->declaration.type->function.linkage) {
324 case LINKAGE_INVALID: break;
325 case LINKAGE_C: break;
326 case LINKAGE_CXX: needs_mangling = true; break;
330 if (needs_mangling) {
331 mangle_entity(entity);
332 return make_id_from_obst();
335 return new_id_from_str(entity->base.symbol->string);
339 * Mangles an entity linker (ld) name for Mach-O usage.
341 * @param ent the entity to be mangled
342 * @param declaration the declaration
344 ident *create_name_macho(entity_t *entity)
346 obstack_printf(&obst, "_%s", entity->base.symbol->string);
347 return make_id_from_obst();
350 void init_mangle(void)
355 void exit_mangle(void)
357 obstack_free(&obst, NULL);