Empty parameter lists are mangled as "v".
[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_function_type(const function_type_t *type)
74 {
75         obstack_1grow(&obst, 'F');
76         if (type->linkage == LINKAGE_C) {
77                 obstack_1grow(&obst, 'Y');
78         }
79
80         mangle_type(type->return_type);
81
82         function_parameter_t *parameter = type->parameters;
83         if (parameter != NULL) {
84                 for ( ; parameter != NULL; parameter = parameter->next) {
85                         mangle_type(parameter->type);
86                 }
87                 if (type->variadic) {
88                         obstack_1grow(&obst, 'z');
89                 }
90         } else {
91                 obstack_1grow(&obst, 'v');
92         }
93         if (type->unspecified_parameters)
94                 panic("can't mangle unspecified parameter types");
95         if (type->kr_style_parameters)
96                 panic("can't mangle kr_style_parameters type");
97
98         obstack_1grow(&obst, 'E');
99 }
100
101 static void mangle_qualifiers(type_qualifiers_t qualifiers)
102 {
103         if (qualifiers & TYPE_QUALIFIER_RESTRICT)
104                 obstack_1grow(&obst, 'r');
105         if (qualifiers & TYPE_QUALIFIER_VOLATILE)
106                 obstack_1grow(&obst, 'V');
107         if (qualifiers & TYPE_QUALIFIER_CONST)
108                 obstack_1grow(&obst, 'K');
109
110         /* handle MS extended qualifiers? */
111 }
112
113 static void mangle_type(type_t *orig_type)
114 {
115         type_t *type = skip_typeref(orig_type);
116
117         mangle_qualifiers(type->base.qualifiers);
118
119         switch (type->kind) {
120         case TYPE_ATOMIC:
121                 mangle_atomic_type(&type->atomic);
122                 return;
123         case TYPE_POINTER:
124                 mangle_pointer_type(&type->pointer);
125                 return;
126         case TYPE_FUNCTION:
127                 mangle_function_type(&type->function);
128                 return;
129         case TYPE_INVALID:
130                 panic("invalid type encountered while mangling");
131         case TYPE_ERROR:
132                 panic("error type encountered while mangling");
133         case TYPE_BUILTIN:
134         case TYPE_TYPEDEF:
135         case TYPE_TYPEOF:
136                 panic("typeref not resolved while manging?!?");
137
138         case TYPE_BITFIELD:
139         case TYPE_COMPLEX:
140         case TYPE_IMAGINARY:
141         case TYPE_COMPOUND_STRUCT:
142         case TYPE_COMPOUND_UNION:
143         case TYPE_ENUM:
144         case TYPE_ARRAY:
145                 panic("no mangling for this type implemented yet");
146                 break;
147         }
148         panic("invalid type encountered while mangling");
149 }
150
151 static void mangle_entity(entity_t *entity)
152 {
153         obstack_1grow(&obst, '_');
154         obstack_1grow(&obst, 'Z');
155
156         /* TODO: mangle scope */
157
158         symbol_t *symbol = entity->base.symbol;
159         obstack_printf(&obst, "%u%s", strlen(symbol->string), symbol->string);
160
161         if (entity->kind == ENTITY_FUNCTION) {
162                 mangle_type(entity->declaration.type);
163         }
164 }
165
166 static ident *make_id_from_obst(void)
167 {
168         size_t  size = obstack_object_size(&obst);
169         char   *str  = obstack_finish(&obst);
170         ident  *id   = new_id_from_chars(str, size);
171         obstack_free(&obst, str);
172         return id;
173 }
174
175 /**
176  * Mangles an entity linker (ld) name for win32 usage.
177  *
178  * @param ent          the entity to be mangled
179  * @param declaration  the declaration
180  */
181 ident *create_name_win32(entity_t *entity)
182 {
183         struct obstack *o = &obst;
184
185         assert(is_declaration(entity));
186
187         if (entity->declaration.modifiers & DM_DLLIMPORT) {
188                 /* add prefix for imported symbols */
189                 obstack_printf(o, "__imp_");
190         }
191
192         if (entity->kind == ENTITY_FUNCTION) {
193                 cc_kind_t cc = entity->declaration.type->function.calling_convention;
194
195                 /* calling convention prefix */
196                 switch (cc) {
197                         case CC_DEFAULT:
198                         case CC_CDECL:
199                         case CC_STDCALL:  obstack_1grow(o, '_'); break;
200                         case CC_FASTCALL: obstack_1grow(o, '@'); break;
201                         default:          panic("unhandled calling convention");
202                 }
203
204                 switch (entity->declaration.type->function.linkage) {
205                         case LINKAGE_INVALID:
206                                 break;
207
208                         case LINKAGE_C:
209                                 obstack_printf(o, "%s", entity->base.symbol->string);
210                                 break;
211
212                         case LINKAGE_CXX:
213                                 mangle_entity(entity);
214                                 break;
215                 }
216
217                 /* calling convention suffix */
218                 switch (cc) {
219                         case CC_DEFAULT:
220                         case CC_CDECL:
221                                 break;
222
223                         case CC_STDCALL:
224                         case CC_FASTCALL: {
225                                 ir_type *irtype = get_ir_type(entity->declaration.type);
226                                 size_t   size   = 0;
227                                 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
228                                         size += get_type_size_bytes(get_method_param_type(irtype, i));
229                                 }
230                                 obstack_printf(o, "@%zu\n", size);
231                                 break;
232                         }
233
234                         default:
235                                 panic("unhandled calling convention");
236                 }
237         } else {
238                 obstack_printf(o, "_%s", entity->base.symbol->string);
239         }
240
241         return make_id_from_obst();
242 }
243
244 /**
245  * Mangles an entity linker (ld) name for Linux ELF usage.
246  *
247  * @param ent          the entity to be mangled
248  * @param declaration  the declaration
249  */
250 ident *create_name_linux_elf(entity_t *entity)
251 {
252         bool needs_mangling = false;
253
254         if (entity->kind == ENTITY_FUNCTION) {
255                 switch (entity->declaration.type->function.linkage) {
256                         case LINKAGE_INVALID: break;
257                         case LINKAGE_C:       break;
258                         case LINKAGE_CXX:     needs_mangling = true; break;
259                 }
260         }
261
262         if (needs_mangling) {
263                 mangle_entity(entity);
264                 return make_id_from_obst();
265         }
266
267         return new_id_from_str(entity->base.symbol->string);
268 }
269
270 /**
271  * Mangles an entity linker (ld) name for Mach-O usage.
272  *
273  * @param ent          the entity to be mangled
274  * @param declaration  the declaration
275  */
276 ident *create_name_macho(entity_t *entity)
277 {
278         obstack_printf(&obst, "_%s", entity->base.symbol->string);
279         return make_id_from_obst();
280 }
281
282 void init_mangle(void)
283 {
284         obstack_init(&obst);
285 }
286
287 void exit_mangle(void)
288 {
289         obstack_free(&obst, NULL);
290 }