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