used ir_type instead of type
[libfirm] / ir / tr / mangle.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/mangle.c
4  * Purpose:     Methods to manipulate names.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef HAVE_STDIO_H
17 # include <stdio.h>
18 #endif
19
20 #include "mangle.h"
21 #include "obst.h"
22
23 /* Make types visible to allow most efficient access */
24 #include "entity_t.h"
25 #include "type_t.h"
26 #include "tpop_t.h"
27
28 /** a obstack used for temporary space */
29 static struct obstack mangle_obst;
30
31 /** returned a mangled type name, currently no mangling */
32 static INLINE ident *
33 mangle_type (ir_type *tp)
34 {
35   assert (tp->kind == k_type);
36   return tp->name;
37 }
38
39 ident *
40 mangle_entity (entity *ent)
41 {
42   ident *type_id;
43   char *cp;
44   int len;
45   ident *res;
46
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);
55   return res;
56 }
57
58
59 /* Returns a new ident that represents 'firstscnd'. */
60 ident *mangle (ident *first, ident *scnd) {
61   char *cp;
62   int len;
63   ident *res;
64
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);
71   return res;
72 }
73
74 /** Returns a new ident that represents 'prefixscndsuffix'. */
75 static ident *mangle3(const char *prefix, ident *scnd, const char *suffix) {
76   char *cp;
77   int len;
78   ident *res;
79
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);
87   return res;
88 }
89
90 /** Returns a new ident that represents first<c>scnd. */
91 static ident *mangle_3(ident *first, char c, ident* scnd) {
92   char *cp;
93   int len;
94   ident *res;
95
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);
103   return res;
104 }
105
106 /* Returns a new ident that represents first_scnd. */
107 ident *mangle_u (ident *first, ident* scnd) {
108   return mangle_3(first, '_', scnd);
109 }
110
111 /* Returns a new ident that represents first.scnd. */
112 ident *mangle_dot (ident *first, ident* scnd) {
113   return mangle_3(first, '.', scnd);
114 }
115
116 /* returns a mangled name for a Win32 function using it's calling convention */
117 ident *decorate_win32_c_fkt(entity *ent) {
118   ir_type *tp      = get_entity_type(ent);
119   unsigned cc_mask = get_method_calling_convention(tp);
120   char buf[16];
121   int size, i;
122
123   if (IS_CDECL(cc_mask))
124     return mangle3("_", get_entity_ident(ent), "");
125   else if (IS_STDCALL(cc_mask)) {
126
127     size = 0;
128     for (i = get_method_n_params(tp) - 1; i >= 0; --i) {
129       size += get_type_size_bytes(get_method_param_type(tp, i));
130     }
131
132     snprintf(buf, sizeof(buf), "@%d", size);
133
134     if (cc_mask & cc_reg_param)
135       return mangle3("@", get_entity_ident(ent), buf);
136     else
137       return mangle3("_", get_entity_ident(ent), buf);
138   }
139   return get_entity_ident(ent);
140 }
141
142 void
143 firm_init_mangle (void)
144 {
145   obstack_init(&mangle_obst);
146 }