- don't use obstack_printf() when not necessary
[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 symbol_t       *sym_stdcall;
38 static struct obstack  obst;
39
40 static void mangle_type(type_t *type);
41
42 static char get_atomic_type_mangle(atomic_type_kind_t kind)
43 {
44         switch (kind) {
45         case ATOMIC_TYPE_INVALID: break;
46         case ATOMIC_TYPE_VOID:        return 'v';
47         case ATOMIC_TYPE_BOOL:        return 'b';
48         case ATOMIC_TYPE_CHAR:        return 'c';
49         case ATOMIC_TYPE_SCHAR:       return 'a';
50         case ATOMIC_TYPE_UCHAR:       return 'h';
51         case ATOMIC_TYPE_INT:         return 'i';
52         case ATOMIC_TYPE_UINT:        return 'j';
53         case ATOMIC_TYPE_SHORT:       return 's';
54         case ATOMIC_TYPE_USHORT:      return 't';
55         case ATOMIC_TYPE_LONG:        return 'l';
56         case ATOMIC_TYPE_ULONG:       return 'm';
57         case ATOMIC_TYPE_LONGLONG:    return 'x';
58         case ATOMIC_TYPE_ULONGLONG:   return 'y';
59         case ATOMIC_TYPE_LONG_DOUBLE: return 'e';
60         case ATOMIC_TYPE_FLOAT:       return 'f';
61         case ATOMIC_TYPE_DOUBLE:      return 'd';
62         }
63         panic("invalid atomic type in mangler");
64 }
65
66 static void mangle_atomic_type(const atomic_type_t *type)
67 {
68         obstack_1grow(&obst, get_atomic_type_mangle(type->akind));
69 }
70
71 static void mangle_pointer_type(const pointer_type_t *type)
72 {
73         obstack_1grow(&obst, 'P');
74         mangle_type(type->points_to);
75 }
76
77 static void mangle_function_type(const function_type_t *type)
78 {
79         obstack_1grow(&obst, 'F');
80         if (type->linkage == sym_C) {
81                 obstack_1grow(&obst, 'Y');
82         }
83
84         mangle_type(type->return_type);
85
86         function_parameter_t *parameter = type->parameters;
87         for ( ; parameter != NULL; parameter = parameter->next) {
88                 mangle_type(parameter->type);
89         }
90         if (type->variadic) {
91                 obstack_1grow(&obst, 'z');
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_CONST)
104                 obstack_1grow(&obst, 'K');
105         if (qualifiers & TYPE_QUALIFIER_VOLATILE)
106                 obstack_1grow(&obst, 'V');
107         if (qualifiers & TYPE_QUALIFIER_RESTRICT)
108                 obstack_1grow(&obst, 'r');
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
152 /**
153  * Mangles an entity linker (ld) name for win32 usage.
154  *
155  * @param ent          the entity to be mangled
156  * @param declaration  the declaration
157  */
158 ident *create_name_win32(entity_t *entity)
159 {
160         ident *id = new_id_from_str(entity->base.symbol->string);
161
162         if (entity->kind == ENTITY_FUNCTION) {
163                 if (entity->declaration.type->function.linkage == sym_stdcall) {
164                         char     buf[16];
165                         ir_type *irtype = get_ir_type(entity->declaration.type);
166                         size_t   size   = 0;
167                         for (int i = get_method_n_params(irtype) - 1; i >= 0; --i) {
168                                 size += get_type_size_bytes(get_method_param_type(irtype, i));
169                         }
170
171                         snprintf(buf, sizeof(buf), "@%d", size);
172                         return id_mangle3("_", id, buf);
173                 }
174         } else {
175                 /* always add an underscore in win32 */
176                 id = id_mangle(id_underscore, id);
177         }
178
179         assert(is_declaration(entity));
180         decl_modifiers_t decl_modifiers = entity->declaration.modifiers;
181         if (decl_modifiers & DM_DLLIMPORT) {
182                 /* add prefix for imported symbols */
183                 id = id_mangle(id_imp, id);
184         }
185         return id;
186 }
187
188 static void mangle_entity(entity_t *entity)
189 {
190         assert(obstack_object_size(&obst) == 0);
191         obstack_1grow(&obst, '_');
192         obstack_1grow(&obst, 'Z');
193
194         /* TODO: mangle scope */
195
196         symbol_t *symbol = entity->base.symbol;
197         obstack_printf(&obst, "%u%s", strlen(symbol->string), symbol->string);
198
199         if (entity->kind == ENTITY_FUNCTION) {
200                 mangle_type(entity->declaration.type);
201         }
202 }
203
204 /**
205  * Mangles an entity linker (ld) name for Linux ELF usage.
206  *
207  * @param ent          the entity to be mangled
208  * @param declaration  the declaration
209  */
210 ident *create_name_linux_elf(entity_t *entity)
211 {
212         bool needs_mangling = false;
213
214         if (entity->kind == ENTITY_FUNCTION && (c_mode & _CXX)) {
215                 symbol_t *linkage = entity->declaration.type->function.linkage;
216
217                 if (linkage == NULL) {
218                         needs_mangling = true;
219                 } else if (linkage != sym_C) {
220                         errorf(&entity->base.source_position,
221                                "Unknown linkage type \"%Y\" found\n", linkage);
222                 }
223         }
224
225         if (needs_mangling) {
226                 mangle_entity(entity);
227                 obstack_1grow(&obst, '\0');
228                 char *str = obstack_finish(&obst);
229
230                 ident *id = new_id_from_str(str);
231                 obstack_free(&obst, str);
232
233                 return id;
234         }
235
236         return new_id_from_str(entity->base.symbol->string);
237 }
238
239 /**
240  * Mangles an entity linker (ld) name for Mach-O usage.
241  *
242  * @param ent          the entity to be mangled
243  * @param declaration  the declaration
244  */
245 ident *create_name_macho(entity_t *entity)
246 {
247         ident *id = new_id_from_str(entity->base.symbol->string);
248         return id_mangle(id_underscore, id);
249 }
250
251 void init_mangle(void)
252 {
253         id_underscore = new_id_from_chars("_", 1);
254         id_imp        = new_id_from_chars("__imp_", 6);
255         sym_C         = symbol_table_insert("C");
256         sym_stdcall   = symbol_table_insert("stdcall");
257
258         obstack_init(&obst);
259 }
260
261 void exit_mangle(void)
262 {
263         obstack_free(&obst, NULL);
264 }