Partially implement mangling of compound 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_compound_type(const compound_type_t *type)
107 {
108         const symbol_t *sym = type->compound->base.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 compound types not implemented yet");
115         }
116 }
117
118 static void mangle_qualifiers(type_qualifiers_t qualifiers)
119 {
120         if (qualifiers & TYPE_QUALIFIER_RESTRICT)
121                 obstack_1grow(&obst, 'r');
122         if (qualifiers & TYPE_QUALIFIER_VOLATILE)
123                 obstack_1grow(&obst, 'V');
124         if (qualifiers & TYPE_QUALIFIER_CONST)
125                 obstack_1grow(&obst, 'K');
126
127         /* handle MS extended qualifiers? */
128 }
129
130 static void mangle_type(type_t *orig_type)
131 {
132         type_t *type = skip_typeref(orig_type);
133
134         mangle_qualifiers(type->base.qualifiers);
135
136         switch (type->kind) {
137         case TYPE_ATOMIC:
138                 mangle_atomic_type(&type->atomic);
139                 return;
140         case TYPE_POINTER:
141                 mangle_pointer_type(&type->pointer);
142                 return;
143         case TYPE_FUNCTION:
144                 mangle_function_type(&type->function);
145                 return;
146         case TYPE_COMPOUND_STRUCT:
147         case TYPE_COMPOUND_UNION:
148                 mangle_compound_type(&type->compound);
149                 return;
150         case TYPE_INVALID:
151                 panic("invalid type encountered while mangling");
152         case TYPE_ERROR:
153                 panic("error type encountered while mangling");
154         case TYPE_BUILTIN:
155         case TYPE_TYPEDEF:
156         case TYPE_TYPEOF:
157                 panic("typeref not resolved while manging?!?");
158
159         case TYPE_BITFIELD:
160         case TYPE_COMPLEX:
161         case TYPE_IMAGINARY:
162         case TYPE_ENUM:
163         case TYPE_ARRAY:
164                 panic("no mangling for this type implemented yet");
165                 break;
166         }
167         panic("invalid type encountered while mangling");
168 }
169
170 static void mangle_entity(entity_t *entity)
171 {
172         obstack_1grow(&obst, '_');
173         obstack_1grow(&obst, 'Z');
174
175         /* TODO: mangle scope */
176
177         const char *name = entity->base.symbol->string;
178         obstack_printf(&obst, "%zu%s", strlen(name), name);
179
180         if (entity->kind == ENTITY_FUNCTION) {
181                 mangle_parameters(&entity->declaration.type->function);
182         }
183 }
184
185 static ident *make_id_from_obst(void)
186 {
187         size_t  size = obstack_object_size(&obst);
188         char   *str  = obstack_finish(&obst);
189         ident  *id   = new_id_from_chars(str, size);
190         obstack_free(&obst, str);
191         return id;
192 }
193
194 /**
195  * Mangles an entity linker (ld) name for win32 usage.
196  *
197  * @param ent          the entity to be mangled
198  * @param declaration  the declaration
199  */
200 ident *create_name_win32(entity_t *entity)
201 {
202         struct obstack *o = &obst;
203
204         assert(is_declaration(entity));
205
206         if (entity->declaration.modifiers & DM_DLLIMPORT) {
207                 /* add prefix for imported symbols */
208                 obstack_printf(o, "__imp_");
209         }
210
211         if (entity->kind == ENTITY_FUNCTION) {
212                 cc_kind_t cc = entity->declaration.type->function.calling_convention;
213
214                 /* calling convention prefix */
215                 switch (cc) {
216                         case CC_DEFAULT:
217                         case CC_CDECL:
218                         case CC_STDCALL:  obstack_1grow(o, '_'); break;
219                         case CC_FASTCALL: obstack_1grow(o, '@'); break;
220                         default:          panic("unhandled calling convention");
221                 }
222
223                 switch (entity->declaration.type->function.linkage) {
224                         case LINKAGE_INVALID:
225                                 break;
226
227                         case LINKAGE_C:
228                                 obstack_printf(o, "%s", entity->base.symbol->string);
229                                 break;
230
231                         case LINKAGE_CXX:
232                                 mangle_entity(entity);
233                                 break;
234                 }
235
236                 /* calling convention suffix */
237                 switch (cc) {
238                         case CC_DEFAULT:
239                         case CC_CDECL:
240                                 break;
241
242                         case CC_STDCALL:
243                         case CC_FASTCALL: {
244                                 ir_type *irtype = get_ir_type(entity->declaration.type);
245                                 size_t   size   = 0;
246                                 for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
247                                         size += get_type_size_bytes(get_method_param_type(irtype, i));
248                                 }
249                                 obstack_printf(o, "@%zu", size);
250                                 break;
251                         }
252
253                         default:
254                                 panic("unhandled calling convention");
255                 }
256         } else {
257                 obstack_printf(o, "_%s", entity->base.symbol->string);
258         }
259
260         return make_id_from_obst();
261 }
262
263 /**
264  * Mangles an entity linker (ld) name for Linux ELF usage.
265  *
266  * @param ent          the entity to be mangled
267  * @param declaration  the declaration
268  */
269 ident *create_name_linux_elf(entity_t *entity)
270 {
271         bool needs_mangling = false;
272
273         if (entity->kind == ENTITY_FUNCTION) {
274                 switch (entity->declaration.type->function.linkage) {
275                         case LINKAGE_INVALID: break;
276                         case LINKAGE_C:       break;
277                         case LINKAGE_CXX:     needs_mangling = true; break;
278                 }
279         }
280
281         if (needs_mangling) {
282                 mangle_entity(entity);
283                 return make_id_from_obst();
284         }
285
286         return new_id_from_str(entity->base.symbol->string);
287 }
288
289 /**
290  * Mangles an entity linker (ld) name for Mach-O usage.
291  *
292  * @param ent          the entity to be mangled
293  * @param declaration  the declaration
294  */
295 ident *create_name_macho(entity_t *entity)
296 {
297         obstack_printf(&obst, "_%s", entity->base.symbol->string);
298         return make_id_from_obst();
299 }
300
301 void init_mangle(void)
302 {
303         obstack_init(&obst);
304 }
305
306 void exit_mangle(void)
307 {
308         obstack_free(&obst, NULL);
309 }