1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: Mangler.cc,v 1.13 2005/08/29 20:14:18 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include <cassert> |
---|
9 | #include <string> |
---|
10 | #include <algorithm> |
---|
11 | #include <iterator> |
---|
12 | #include <functional> |
---|
13 | #include <set> |
---|
14 | |
---|
15 | #include "SynTree/Declaration.h" |
---|
16 | #include "SynTree/Type.h" |
---|
17 | #include "SynTree/Expression.h" |
---|
18 | #include "SynTree/Initializer.h" |
---|
19 | #include "SynTree/Statement.h" |
---|
20 | #include "Mangler.h" |
---|
21 | #include "CodeGen/OperatorTable.h" |
---|
22 | |
---|
23 | namespace SymTab { |
---|
24 | |
---|
25 | Mangler::Mangler() |
---|
26 | : nextVarNum( 0 ), isTopLevel( true ) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | //Mangler::Mangler( const Mangler & ) |
---|
31 | // : mangleName(), varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( isTopLevel ) |
---|
32 | //{ |
---|
33 | //} |
---|
34 | Mangler::Mangler( const Mangler &rhs ) |
---|
35 | : mangleName() |
---|
36 | { |
---|
37 | varNums = rhs.varNums; |
---|
38 | nextVarNum = rhs.nextVarNum; |
---|
39 | isTopLevel = rhs.isTopLevel; |
---|
40 | } |
---|
41 | |
---|
42 | void |
---|
43 | Mangler::mangleDecl(DeclarationWithType *declaration) |
---|
44 | { |
---|
45 | bool wasTopLevel = isTopLevel; |
---|
46 | if( isTopLevel ) { |
---|
47 | varNums.clear(); |
---|
48 | nextVarNum = 0; |
---|
49 | isTopLevel = false; |
---|
50 | } |
---|
51 | mangleName << "__"; |
---|
52 | CodeGen::OperatorInfo opInfo; |
---|
53 | if( operatorLookup( declaration->get_name(), opInfo ) ) { |
---|
54 | mangleName << opInfo.outputName; |
---|
55 | } else { |
---|
56 | mangleName << declaration->get_name(); |
---|
57 | } |
---|
58 | mangleName << "__"; |
---|
59 | maybeAccept( declaration->get_type(), *this ); |
---|
60 | isTopLevel = wasTopLevel; |
---|
61 | } |
---|
62 | |
---|
63 | void |
---|
64 | Mangler::visit(ObjectDecl *declaration) |
---|
65 | { |
---|
66 | mangleDecl( declaration ); |
---|
67 | } |
---|
68 | |
---|
69 | void |
---|
70 | Mangler::visit(FunctionDecl *declaration) |
---|
71 | { |
---|
72 | mangleDecl( declaration ); |
---|
73 | } |
---|
74 | |
---|
75 | void |
---|
76 | Mangler::visit(VoidType *voidType) |
---|
77 | { |
---|
78 | printQualifiers( voidType ); |
---|
79 | mangleName << "v"; |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | Mangler::visit(BasicType *basicType) |
---|
84 | { |
---|
85 | static const char *btLetter[] = { |
---|
86 | "b", // Bool |
---|
87 | "c", // Char |
---|
88 | "Sc", // SignedChar |
---|
89 | "Uc", // UnsignedChar |
---|
90 | "s", // ShortSignedInt |
---|
91 | "Us", // ShortUnsignedInt |
---|
92 | "i", // SignedInt |
---|
93 | "Ui", // UnsignedInt |
---|
94 | "l", // LongSignedInt |
---|
95 | "Ul", // LongUnsignedInt |
---|
96 | "q", // LongLongSignedInt |
---|
97 | "Uq", // LongLongUnsignedInt |
---|
98 | "f", // Float |
---|
99 | "d", // Double |
---|
100 | "r", // LongDouble |
---|
101 | "Xf", // FloatComplex |
---|
102 | "Xd", // DoubleComplex |
---|
103 | "Xr", // LongDoubleComplex |
---|
104 | "If", // FloatImaginary |
---|
105 | "Id", // DoubleImaginary |
---|
106 | "Ir", // LongDoubleImaginary |
---|
107 | }; |
---|
108 | |
---|
109 | printQualifiers( basicType ); |
---|
110 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
111 | } |
---|
112 | |
---|
113 | void |
---|
114 | Mangler::visit(PointerType *pointerType) |
---|
115 | { |
---|
116 | printQualifiers( pointerType ); |
---|
117 | mangleName << "P"; |
---|
118 | maybeAccept( pointerType->get_base(), *this ); |
---|
119 | } |
---|
120 | |
---|
121 | void |
---|
122 | Mangler::visit(ArrayType *arrayType) |
---|
123 | { |
---|
124 | // TODO: encode dimension |
---|
125 | printQualifiers( arrayType ); |
---|
126 | mangleName << "A0"; |
---|
127 | maybeAccept( arrayType->get_base(), *this ); |
---|
128 | } |
---|
129 | |
---|
130 | namespace { |
---|
131 | inline std::list< Type* > |
---|
132 | getTypes( const std::list< DeclarationWithType* > decls ) |
---|
133 | { |
---|
134 | std::list< Type* > ret; |
---|
135 | std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), |
---|
136 | std::mem_fun( &DeclarationWithType::get_type ) ); |
---|
137 | return ret; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | void |
---|
142 | Mangler::visit(FunctionType *functionType) |
---|
143 | { |
---|
144 | printQualifiers( functionType ); |
---|
145 | mangleName << "F"; |
---|
146 | std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); |
---|
147 | acceptAll( returnTypes, *this ); |
---|
148 | mangleName << "_"; |
---|
149 | std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); |
---|
150 | acceptAll( paramTypes, *this ); |
---|
151 | mangleName << "_"; |
---|
152 | } |
---|
153 | |
---|
154 | void |
---|
155 | Mangler::mangleRef(ReferenceToType *refType, std::string prefix) |
---|
156 | { |
---|
157 | printQualifiers( refType ); |
---|
158 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); |
---|
159 | } |
---|
160 | |
---|
161 | void |
---|
162 | Mangler::visit(StructInstType *aggregateUseType) |
---|
163 | { |
---|
164 | mangleRef( aggregateUseType, "s" ); |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | Mangler::visit(UnionInstType *aggregateUseType) |
---|
169 | { |
---|
170 | mangleRef( aggregateUseType, "u" ); |
---|
171 | } |
---|
172 | |
---|
173 | void |
---|
174 | Mangler::visit(EnumInstType *aggregateUseType) |
---|
175 | { |
---|
176 | mangleRef( aggregateUseType, "e" ); |
---|
177 | } |
---|
178 | |
---|
179 | void |
---|
180 | Mangler::visit(TypeInstType *typeInst) |
---|
181 | { |
---|
182 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
183 | if( varNum == varNums.end() ) { |
---|
184 | mangleRef( typeInst, "t" ); |
---|
185 | } else { |
---|
186 | printQualifiers( typeInst ); |
---|
187 | std::ostrstream numStream; |
---|
188 | numStream << varNum->second.first; |
---|
189 | mangleName << (numStream.pcount() + 1); |
---|
190 | switch( (TypeDecl::Kind)varNum->second.second ) { |
---|
191 | case TypeDecl::Any: |
---|
192 | mangleName << "t"; |
---|
193 | break; |
---|
194 | case TypeDecl::Dtype: |
---|
195 | mangleName << "d"; |
---|
196 | break; |
---|
197 | case TypeDecl::Ftype: |
---|
198 | mangleName << "f"; |
---|
199 | break; |
---|
200 | } |
---|
201 | mangleName << std::string( numStream.str(), numStream.pcount() ); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void |
---|
206 | Mangler::visit(TupleType *tupleType) |
---|
207 | { |
---|
208 | printQualifiers( tupleType ); |
---|
209 | mangleName << "T"; |
---|
210 | acceptAll( tupleType->get_types(), *this ); |
---|
211 | mangleName << "_"; |
---|
212 | } |
---|
213 | |
---|
214 | void |
---|
215 | Mangler::visit(TypeDecl *decl) |
---|
216 | { |
---|
217 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
218 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); |
---|
219 | } |
---|
220 | |
---|
221 | void |
---|
222 | printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) |
---|
223 | { |
---|
224 | for( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
225 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | void |
---|
230 | Mangler::printQualifiers( Type *type ) |
---|
231 | { |
---|
232 | if( !type->get_forall().empty() ) { |
---|
233 | std::list< std::string > assertionNames; |
---|
234 | int tcount = 0, dcount = 0, fcount = 0; |
---|
235 | mangleName << "A"; |
---|
236 | for( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
237 | switch( (*i)->get_kind() ) { |
---|
238 | case TypeDecl::Any: |
---|
239 | tcount++; |
---|
240 | break; |
---|
241 | case TypeDecl::Dtype: |
---|
242 | dcount++; |
---|
243 | break; |
---|
244 | case TypeDecl::Ftype: |
---|
245 | fcount++; |
---|
246 | break; |
---|
247 | } |
---|
248 | varNums[ (*i)->get_name() ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); |
---|
249 | for( std::list< DeclarationWithType* >::iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
250 | Mangler sub_mangler; |
---|
251 | sub_mangler.nextVarNum = nextVarNum; |
---|
252 | sub_mangler.isTopLevel = false; |
---|
253 | sub_mangler.varNums = varNums; |
---|
254 | (*assert)->accept( sub_mangler ); |
---|
255 | assertionNames.push_back( std::string( sub_mangler.mangleName.str(), sub_mangler.mangleName.pcount() ) ); |
---|
256 | } |
---|
257 | } |
---|
258 | mangleName << tcount << "_" << dcount << "_" << fcount << "_"; |
---|
259 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
260 | mangleName << "_"; |
---|
261 | } |
---|
262 | if( type->get_isConst() ) { |
---|
263 | mangleName << "C"; |
---|
264 | } |
---|
265 | if( type->get_isVolatile() ) { |
---|
266 | mangleName << "V"; |
---|
267 | } |
---|
268 | if( type->get_isRestrict() ) { |
---|
269 | mangleName << "R"; |
---|
270 | } |
---|
271 | if( type->get_isLvalue() ) { |
---|
272 | mangleName << "L"; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | } // namespace SymTab |
---|