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