Implement mangling of complex and imaginary types.
[cparser] / mangle.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 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 <libfirm/firm.h>
23 #include <string.h>
24
25 #include "entity_t.h"
26 #include "type_t.h"
27 #include "symbol_t.h"
28 #include "mangle.h"
29 #include "diagnostic.h"
30 #include "ast2firm.h"
31 #include "lang_features.h"
32 #include "adt/error.h"
33
34 static struct obstack obst;
35
36 static void mangle_type(type_t *type);
37
38 static char get_atomic_type_mangle(atomic_type_kind_t kind)
39 {
40         switch (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';
58         }
59         panic("invalid atomic type in mangler");
60 }
61
62 static void mangle_atomic_type(const atomic_type_t *type)
63 {
64         obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
65 }
66
67 static void mangle_pointer_type(const pointer_type_t *type)
68 {
69         obstack_1grow(&obst, 'P');
70         mangle_type(type->points_to);
71 }
72
73 static void mangle_parameters(const function_type_t *type)
74 {
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");
79
80         const function_parameter_t *parameter = type->parameters;
81         if (parameter != NULL) {
82                 for ( ; parameter != NULL; parameter = parameter->next) {
83                         mangle_type(parameter->type);
84                 }
85                 if (type->variadic) {
86                         obstack_1grow(&obst, 'z');
87                 }
88         } else {
89                 obstack_1grow(&obst, 'v');
90         }
91 }
92
93 static void mangle_function_type(const function_type_t *type)
94 {
95         obstack_1grow(&obst, 'F');
96         if (type->linkage == LINKAGE_C) {
97                 obstack_1grow(&obst, 'Y');
98         }
99
100         mangle_type(type->return_type);
101         mangle_parameters(type);
102
103         obstack_1grow(&obst, 'E');
104 }
105
106 static void mangle_class_enum_type(const entity_base_t *ent)
107 {
108         const symbol_t *sym = ent->symbol;
109         if (sym != NULL) {
110                 const char *name = sym->string;
111                 obstack_printf(&obst, "%zu%s", strlen(name), name);
112         } else {
113                 /* TODO need the first typedef name here */
114                 panic("mangling of unnamed class/enum types not implemented yet");
115         }
116 }
117
118 static void mangle_array_type(const array_type_t *type)
119 {
120         if (type->is_vla) {
121                 obstack_printf(&obst, "A_");
122         } else if (type->size_constant) {
123                 obstack_printf(&obst, "A%zu_", type->size);
124         } else {
125                 panic("mangling of non-constant sized arrray types not implemented yet");
126         }
127         mangle_type(type->element_type);
128 }
129
130 static void mangle_complex_type(const complex_type_t *type)
131 {
132         obstack_printf(&obst, "C%c", get_atomic_type_mangle(type->akind));
133 }
134
135 static void mangle_imaginary_type(const imaginary_type_t *type)
136 {
137         obstack_printf(&obst, "G%c", get_atomic_type_mangle(type->akind));
138 }
139
140 static void mangle_qualifiers(type_qualifiers_t qualifiers)
141 {
142         if (qualifiers & TYPE_QUALIFIER_RESTRICT)
143                 obstack_1grow(&obst, 'r');
144         if (qualifiers & TYPE_QUALIFIER_VOLATILE)
145                 obstack_1grow(&obst, 'V');
146         if (qualifiers & TYPE_QUALIFIER_CONST)
147                 obstack_1grow(&obst, 'K');
148
149         /* handle MS extended qualifiers? */
150 }
151
152 static void mangle_type(type_t *orig_type)
153 {
154         type_t *type = skip_typeref(orig_type);
155
156         mangle_qualifiers(type->base.qualifiers);
157
158         switch (type->kind) {
159         case TYPE_ATOMIC:
160                 mangle_atomic_type(&type->atomic);
161                 return;
162         case TYPE_POINTER:
163                 mangle_pointer_type(&type->pointer);
164                 return;
165         case TYPE_FUNCTION:
166                 mangle_function_type(&type->function);
167                 return;
168         case TYPE_COMPOUND_STRUCT:
169         case TYPE_COMPOUND_UNION:
170                 mangle_class_enum_type(&type->compound.compound->base);
171                 return;
172         case TYPE_ENUM:
173                 mangle_class_enum_type(&type->enumt.enume->base);
174                 return;
175         case TYPE_ARRAY:
176                 mangle_array_type(&type->array);
177                 return;
178         case TYPE_COMPLEX:
179                 mangle_complex_type(&type->complex);
180                 return;
181         case TYPE_IMAGINARY:
182                 mangle_imaginary_type(&type->imaginary);
183                 return;
184         case TYPE_INVALID:
185                 panic("invalid type encountered while mangling");
186         case TYPE_ERROR:
187                 panic("error type encountered while mangling");
188         case TYPE_BUILTIN:
189         case TYPE_TYPEDEF:
190         case TYPE_TYPEOF:
191                 panic("typeref not resolved while manging?!?");
192
193         case TYPE_BITFIELD:
194                 panic("no mangling for this type implemented yet");
195                 break;
196         }
197         panic("invalid type encountered while mangling");
198 }
199
200 static void mangle_entity(entity_t *entity)
201 {
202         obstack_1grow(&obst, '_');
203         obstack_1grow(&obst, 'Z');
204
205         /* TODO: mangle scope */
206
207         const char *name = entity->base.symbol->string;
208         obstack_printf(&obst, "%zu%s", strlen(name), name);
209
210         if (entity->kind == ENTITY_FUNCTION) {
211                 mangle_parameters(&entity->declaration.type->function);
212         }
213 }
214
215 static ident *make_id_from_obst(void)
216 {
217         size_t  size = obstack_object_size(&obst);
218         char   *str  = obstack_finish(&obst);
219         ident  *id   = new_id_from_chars(str, size);
220         obstack_free(&obst, str);
221         return id;
222 }
223
224 /**
225  * Mangles an entity linker (ld) name for win32 usage.
226  *
227  * @param ent          the entity to be mangled
228  * @param declaration  the declaration
229  */
230 ident *create_name_win32(entity_t *entity)
231 {
232         struct obstack *o = &obst;
233
234         assert(is_declaration(entity));
235
236         if (entity->declaration.modifiers & DM_DLLIMPORT) {
237                 /* add prefix for imported symbols */
238                 obstack_printf(o, "__imp_");
239         }
240
241         if (entity->kind == ENTITY_FUNCTION) {
242                 cc_kind_t cc = entity->declaration.type->function.calling_convention;
243
244                 /* calling convention prefix */
245                 switch (cc) {
246                         case CC_DEFAULT:
247                         case CC_CDECL:
248                         case CC_STDCALL:  obstack_1grow(o, '_'); break;
249                         case CC_FASTCALL: obstack_1grow(o, '@'); break;
250                         default:          panic("unhandled calling convention");
251                 }
252
253                 switch (entity->declaration.type->function.linkage) {
254                         case LINKAGE_INVALID:
255                                 break;
256
257                         case LINKAGE_C:
258                                 obstack_printf(o, "%s", entity->base.symbol->string);
259                                 break;
260
261                         case LINKAGE_CXX:
262                                 mangle_entity(entity);
263                                 break;
264                 }
265
266                 /* calling convention suffix */
267                 switch (cc) {
268                         case CC_DEFAULT:
269                         case CC_CDECL:
270                                 break;
271
272                         case CC_STDCALL:
273                         case CC_FASTCALL: {
274                                 ir_type *irtype = get_ir_type(entity->declaration.type);
275                                 size_t   size   = 0;
276                                 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
277                                         size += get_type_size_bytes(get_method_param_type(irtype, i));
278                                 }
279                                 obstack_printf(o, "@%zu", size);
280                                 break;
281                         }
282
283                         default:
284                                 panic("unhandled calling convention");
285                 }
286         } else {
287                 obstack_printf(o, "_%s", entity->base.symbol->string);
288         }
289
290         return make_id_from_obst();
291 }
292
293 /**
294  * Mangles an entity linker (ld) name for Linux ELF usage.
295  *
296  * @param ent          the entity to be mangled
297  * @param declaration  the declaration
298  */
299 ident *create_name_linux_elf(entity_t *entity)
300 {
301         bool needs_mangling = false;
302
303         if (entity->kind == ENTITY_FUNCTION) {
304                 switch (entity->declaration.type->function.linkage) {
305                         case LINKAGE_INVALID: break;
306                         case LINKAGE_C:       break;
307                         case LINKAGE_CXX:     needs_mangling = true; break;
308                 }
309         }
310
311         if (needs_mangling) {
312                 mangle_entity(entity);
313                 return make_id_from_obst();
314         }
315
316         return new_id_from_str(entity->base.symbol->string);
317 }
318
319 /**
320  * Mangles an entity linker (ld) name for Mach-O usage.
321  *
322  * @param ent          the entity to be mangled
323  * @param declaration  the declaration
324  */
325 ident *create_name_macho(entity_t *entity)
326 {
327         obstack_printf(&obst, "_%s", entity->base.symbol->string);
328         return make_id_from_obst();
329 }
330
331 void init_mangle(void)
332 {
333         obstack_init(&obst);
334 }
335
336 void exit_mangle(void)
337 {
338         obstack_free(&obst, NULL);
339 }