3 * File name: ir/tr/mangle.c
4 * Purpose: Methods to manipulate names.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 /* Make types visible to allow most efficient access */
28 /** a obstack used for temporary space */
29 static struct obstack mangle_obst;
31 /** returned a mangled type name, currently no mangling */
33 mangle_type (ir_type *tp)
35 assert (tp->kind == k_type);
40 mangle_entity (ir_entity *ent)
47 type_id = mangle_type(ent->owner);
48 obstack_grow(&mangle_obst, get_id_str(type_id), get_id_strlen(type_id));
49 obstack_1grow(&mangle_obst,'_');
50 obstack_grow(&mangle_obst,get_id_str(ent->name),get_id_strlen(ent->name));
51 len = obstack_object_size (&mangle_obst);
52 cp = obstack_finish (&mangle_obst);
53 res = new_id_from_chars(cp, len);
54 obstack_free (&mangle_obst, cp);
59 /* Returns a new ident that represents 'firstscnd'. */
60 ident *mangle (ident *first, ident *scnd) {
65 obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
66 obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
67 len = obstack_object_size (&mangle_obst);
68 cp = obstack_finish (&mangle_obst);
69 res = new_id_from_chars (cp, len);
70 obstack_free (&mangle_obst, cp);
74 /** Returns a new ident that represents 'prefixscndsuffix'. */
75 static ident *mangle3(const char *prefix, ident *scnd, const char *suffix) {
80 obstack_grow(&mangle_obst, prefix, strlen(prefix));
81 obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
82 obstack_grow(&mangle_obst, suffix, strlen(suffix));
83 len = obstack_object_size (&mangle_obst);
84 cp = obstack_finish (&mangle_obst);
85 res = new_id_from_chars (cp, len);
86 obstack_free (&mangle_obst, cp);
90 /** Returns a new ident that represents first<c>scnd. */
91 static ident *mangle_3(ident *first, char c, ident* scnd) {
96 obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
97 obstack_1grow(&mangle_obst, c);
98 obstack_grow(&mangle_obst,get_id_str(scnd),get_id_strlen(scnd));
99 len = obstack_object_size (&mangle_obst);
100 cp = obstack_finish (&mangle_obst);
101 res = new_id_from_chars (cp, len);
102 obstack_free (&mangle_obst, cp);
106 /* Returns a new ident that represents first_scnd. */
107 ident *mangle_u (ident *first, ident* scnd) {
108 return mangle_3(first, '_', scnd);
111 /* Returns a new ident that represents first.scnd. */
112 ident *mangle_dot (ident *first, ident* scnd) {
113 return mangle_3(first, '.', scnd);
116 /* returns a mangled name for a Win32 function using it's calling convention */
117 ident *decorate_win32_c_fkt(ir_entity *ent) {
118 ir_type *tp = get_entity_type(ent);
119 unsigned cc_mask = get_method_calling_convention(tp);
123 if (IS_CDECL(cc_mask))
124 return mangle3("_", get_entity_ident(ent), "");
125 else if (IS_STDCALL(cc_mask)) {
128 for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
129 size += get_type_size_bytes(get_method_param_type(tp, i));
132 snprintf(buf, sizeof(buf), "@%d", size);
134 if (cc_mask & cc_reg_param)
135 return mangle3("@", get_entity_ident(ent), buf);
137 return mangle3("_", get_entity_ident(ent), buf);
139 return get_entity_ident(ent);
143 firm_init_mangle (void)
145 obstack_init(&mangle_obst);