1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Mangler.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 21:40:29 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Mar 17 09:40:01 2017 |
---|
13 | // Update Count : 20 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | #include <string> |
---|
18 | #include <algorithm> |
---|
19 | #include <iterator> |
---|
20 | #include <functional> |
---|
21 | #include <set> |
---|
22 | |
---|
23 | #include "SynTree/Declaration.h" |
---|
24 | #include "SynTree/Type.h" |
---|
25 | #include "SynTree/Expression.h" |
---|
26 | #include "SynTree/Initializer.h" |
---|
27 | #include "SynTree/Statement.h" |
---|
28 | #include "Mangler.h" |
---|
29 | #include "CodeGen/OperatorTable.h" |
---|
30 | |
---|
31 | namespace SymTab { |
---|
32 | std::string Mangler::mangleType( Type *ty ) { |
---|
33 | Mangler mangler( false, true ); |
---|
34 | maybeAccept( ty, mangler ); |
---|
35 | return mangler.get_mangleName(); |
---|
36 | } |
---|
37 | |
---|
38 | Mangler::Mangler( bool mangleOverridable, bool typeMode ) |
---|
39 | : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {} |
---|
40 | |
---|
41 | Mangler::Mangler( const Mangler &rhs ) : mangleName() { |
---|
42 | varNums = rhs.varNums; |
---|
43 | nextVarNum = rhs.nextVarNum; |
---|
44 | isTopLevel = rhs.isTopLevel; |
---|
45 | mangleOverridable = rhs.mangleOverridable; |
---|
46 | typeMode = rhs.typeMode; |
---|
47 | } |
---|
48 | |
---|
49 | void Mangler::mangleDecl( DeclarationWithType *declaration ) { |
---|
50 | bool wasTopLevel = isTopLevel; |
---|
51 | if ( isTopLevel ) { |
---|
52 | varNums.clear(); |
---|
53 | nextVarNum = 0; |
---|
54 | isTopLevel = false; |
---|
55 | } // if |
---|
56 | mangleName << "__"; |
---|
57 | CodeGen::OperatorInfo opInfo; |
---|
58 | if ( operatorLookup( declaration->get_name(), opInfo ) ) { |
---|
59 | mangleName << opInfo.outputName; |
---|
60 | } else { |
---|
61 | mangleName << declaration->get_name(); |
---|
62 | } // if |
---|
63 | mangleName << "__"; |
---|
64 | maybeAccept( declaration->get_type(), *this ); |
---|
65 | if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { |
---|
66 | // want to be able to override autogenerated and intrinsic routines, |
---|
67 | // so they need a different name mangling |
---|
68 | if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { |
---|
69 | mangleName << "autogen__"; |
---|
70 | } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
71 | mangleName << "intrinsic__"; |
---|
72 | } else { |
---|
73 | // if we add another kind of overridable function, this has to change |
---|
74 | assert( false ); |
---|
75 | } // if |
---|
76 | } |
---|
77 | isTopLevel = wasTopLevel; |
---|
78 | } |
---|
79 | |
---|
80 | void Mangler::visit( ObjectDecl *declaration ) { |
---|
81 | mangleDecl( declaration ); |
---|
82 | } |
---|
83 | |
---|
84 | void Mangler::visit( FunctionDecl *declaration ) { |
---|
85 | mangleDecl( declaration ); |
---|
86 | } |
---|
87 | |
---|
88 | void Mangler::visit( VoidType *voidType ) { |
---|
89 | printQualifiers( voidType ); |
---|
90 | mangleName << "v"; |
---|
91 | } |
---|
92 | |
---|
93 | void Mangler::visit( BasicType *basicType ) { |
---|
94 | static const char *btLetter[] = { |
---|
95 | "b", // Bool |
---|
96 | "c", // Char |
---|
97 | "Sc", // SignedChar |
---|
98 | "Uc", // UnsignedChar |
---|
99 | "s", // ShortSignedInt |
---|
100 | "Us", // ShortUnsignedInt |
---|
101 | "i", // SignedInt |
---|
102 | "Ui", // UnsignedInt |
---|
103 | "l", // LongSignedInt |
---|
104 | "Ul", // LongUnsignedInt |
---|
105 | "q", // LongLongSignedInt |
---|
106 | "Uq", // LongLongUnsignedInt |
---|
107 | "f", // Float |
---|
108 | "d", // Double |
---|
109 | "r", // LongDouble |
---|
110 | "Xf", // FloatComplex |
---|
111 | "Xd", // DoubleComplex |
---|
112 | "Xr", // LongDoubleComplex |
---|
113 | "If", // FloatImaginary |
---|
114 | "Id", // DoubleImaginary |
---|
115 | "Ir", // LongDoubleImaginary |
---|
116 | }; |
---|
117 | |
---|
118 | printQualifiers( basicType ); |
---|
119 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
120 | } |
---|
121 | |
---|
122 | void Mangler::visit( PointerType *pointerType ) { |
---|
123 | printQualifiers( pointerType ); |
---|
124 | mangleName << "P"; |
---|
125 | maybeAccept( pointerType->get_base(), *this ); |
---|
126 | } |
---|
127 | |
---|
128 | void Mangler::visit( ArrayType *arrayType ) { |
---|
129 | // TODO: encode dimension |
---|
130 | printQualifiers( arrayType ); |
---|
131 | mangleName << "A0"; |
---|
132 | maybeAccept( arrayType->get_base(), *this ); |
---|
133 | } |
---|
134 | |
---|
135 | namespace { |
---|
136 | inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { |
---|
137 | std::list< Type* > ret; |
---|
138 | std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), |
---|
139 | std::mem_fun( &DeclarationWithType::get_type ) ); |
---|
140 | return ret; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void Mangler::visit( FunctionType *functionType ) { |
---|
145 | printQualifiers( functionType ); |
---|
146 | mangleName << "F"; |
---|
147 | std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); |
---|
148 | acceptAll( returnTypes, *this ); |
---|
149 | mangleName << "_"; |
---|
150 | std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); |
---|
151 | acceptAll( paramTypes, *this ); |
---|
152 | mangleName << "_"; |
---|
153 | } |
---|
154 | |
---|
155 | void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { |
---|
156 | printQualifiers( refType ); |
---|
157 | |
---|
158 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); |
---|
159 | } |
---|
160 | |
---|
161 | void Mangler::mangleGenericRef( ReferenceToType *refType, std::string prefix ) { |
---|
162 | printQualifiers( refType ); |
---|
163 | |
---|
164 | std::ostringstream oldName( mangleName.str() ); |
---|
165 | mangleName.clear(); |
---|
166 | |
---|
167 | mangleName << prefix << refType->get_name(); |
---|
168 | |
---|
169 | std::list< Expression* >& params = refType->get_parameters(); |
---|
170 | if ( ! params.empty() ) { |
---|
171 | mangleName << "_"; |
---|
172 | for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { |
---|
173 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
174 | assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); |
---|
175 | maybeAccept( paramType->get_type(), *this ); |
---|
176 | } |
---|
177 | mangleName << "_"; |
---|
178 | } |
---|
179 | |
---|
180 | oldName << mangleName.str().length() << mangleName.str(); |
---|
181 | mangleName.str( oldName.str() ); |
---|
182 | } |
---|
183 | |
---|
184 | void Mangler::visit( StructInstType *aggregateUseType ) { |
---|
185 | if ( typeMode ) mangleGenericRef( aggregateUseType, "s" ); |
---|
186 | else mangleRef( aggregateUseType, "s" ); |
---|
187 | } |
---|
188 | |
---|
189 | void Mangler::visit( UnionInstType *aggregateUseType ) { |
---|
190 | if ( typeMode ) mangleGenericRef( aggregateUseType, "u" ); |
---|
191 | else mangleRef( aggregateUseType, "u" ); |
---|
192 | } |
---|
193 | |
---|
194 | void Mangler::visit( EnumInstType *aggregateUseType ) { |
---|
195 | mangleRef( aggregateUseType, "e" ); |
---|
196 | } |
---|
197 | |
---|
198 | void Mangler::visit( TypeInstType *typeInst ) { |
---|
199 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
200 | if ( varNum == varNums.end() ) { |
---|
201 | mangleRef( typeInst, "t" ); |
---|
202 | } else { |
---|
203 | printQualifiers( typeInst ); |
---|
204 | std::ostringstream numStream; |
---|
205 | numStream << varNum->second.first; |
---|
206 | switch ( (TypeDecl::Kind )varNum->second.second ) { |
---|
207 | case TypeDecl::Any: |
---|
208 | mangleName << "t"; |
---|
209 | break; |
---|
210 | case TypeDecl::Dtype: |
---|
211 | mangleName << "d"; |
---|
212 | break; |
---|
213 | case TypeDecl::Ftype: |
---|
214 | mangleName << "f"; |
---|
215 | break; |
---|
216 | case TypeDecl::Ttype: |
---|
217 | mangleName << "tVARGS"; |
---|
218 | break; |
---|
219 | default: |
---|
220 | assert( false ); |
---|
221 | } // switch |
---|
222 | mangleName << numStream.str(); |
---|
223 | } // if |
---|
224 | } |
---|
225 | |
---|
226 | void Mangler::visit( TupleType *tupleType ) { |
---|
227 | printQualifiers( tupleType ); |
---|
228 | mangleName << "T"; |
---|
229 | acceptAll( tupleType->get_types(), *this ); |
---|
230 | mangleName << "_"; |
---|
231 | } |
---|
232 | |
---|
233 | void Mangler::visit( VarArgsType *varArgsType ) { |
---|
234 | printQualifiers( varArgsType ); |
---|
235 | mangleName << "VARGS"; |
---|
236 | } |
---|
237 | |
---|
238 | void Mangler::visit( ZeroType *zeroType ) { |
---|
239 | mangleName << "Z"; |
---|
240 | } |
---|
241 | |
---|
242 | void Mangler::visit( OneType *oneType ) { |
---|
243 | mangleName << "O"; |
---|
244 | } |
---|
245 | |
---|
246 | void Mangler::visit( TypeDecl *decl ) { |
---|
247 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
248 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); |
---|
249 | } |
---|
250 | |
---|
251 | void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
252 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
253 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
254 | } // for |
---|
255 | } |
---|
256 | |
---|
257 | void Mangler::printQualifiers( Type *type ) { |
---|
258 | // skip if not including qualifiers |
---|
259 | if ( typeMode ) return; |
---|
260 | |
---|
261 | if ( ! type->get_forall().empty() ) { |
---|
262 | std::list< std::string > assertionNames; |
---|
263 | int tcount = 0, dcount = 0, fcount = 0, vcount = 0; |
---|
264 | mangleName << "A"; |
---|
265 | for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
266 | switch ( (*i)->get_kind() ) { |
---|
267 | case TypeDecl::Any: |
---|
268 | tcount++; |
---|
269 | break; |
---|
270 | case TypeDecl::Dtype: |
---|
271 | dcount++; |
---|
272 | break; |
---|
273 | case TypeDecl::Ftype: |
---|
274 | fcount++; |
---|
275 | break; |
---|
276 | case TypeDecl::Ttype: |
---|
277 | vcount++; |
---|
278 | break; |
---|
279 | default: |
---|
280 | assert( false ); |
---|
281 | } // switch |
---|
282 | varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); |
---|
283 | for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
284 | Mangler sub_mangler( mangleOverridable, typeMode ); |
---|
285 | sub_mangler.nextVarNum = nextVarNum; |
---|
286 | sub_mangler.isTopLevel = false; |
---|
287 | sub_mangler.varNums = varNums; |
---|
288 | (*assert)->accept( sub_mangler ); |
---|
289 | assertionNames.push_back( sub_mangler.mangleName.str() ); |
---|
290 | } // for |
---|
291 | } // for |
---|
292 | mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; |
---|
293 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
294 | mangleName << "_"; |
---|
295 | } // if |
---|
296 | if ( type->get_const() ) { |
---|
297 | mangleName << "C"; |
---|
298 | } // if |
---|
299 | if ( type->get_volatile() ) { |
---|
300 | mangleName << "V"; |
---|
301 | } // if |
---|
302 | // Removed due to restrict not affecting function compatibility in GCC |
---|
303 | // if ( type->get_isRestrict() ) { |
---|
304 | // mangleName << "R"; |
---|
305 | // } // if |
---|
306 | if ( type->get_lvalue() ) { |
---|
307 | mangleName << "L"; |
---|
308 | } // if |
---|
309 | if ( type->get_atomic() ) { |
---|
310 | mangleName << "A"; |
---|
311 | } // if |
---|
312 | } |
---|
313 | } // namespace SymTab |
---|
314 | |
---|
315 | // Local Variables: // |
---|
316 | // tab-width: 4 // |
---|
317 | // mode: c++ // |
---|
318 | // compile-command: "make install" // |
---|
319 | // End: // |
---|