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 : Rob Schluntz |
---|
12 | // Last Modified On : Wed Aug 19 15:52:24 2015 |
---|
13 | // Update Count : 19 |
---|
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 | Mangler::Mangler( bool mangleOverridable ) : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ) { |
---|
33 | } |
---|
34 | |
---|
35 | //Mangler::Mangler( const Mangler & ) |
---|
36 | // : mangleName(), varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( isTopLevel ) |
---|
37 | //{ |
---|
38 | //} |
---|
39 | Mangler::Mangler( const Mangler &rhs ) : mangleName() { |
---|
40 | varNums = rhs.varNums; |
---|
41 | nextVarNum = rhs.nextVarNum; |
---|
42 | isTopLevel = rhs.isTopLevel; |
---|
43 | mangleOverridable = rhs.mangleOverridable; |
---|
44 | } |
---|
45 | |
---|
46 | void Mangler::mangleDecl( DeclarationWithType *declaration ) { |
---|
47 | bool wasTopLevel = isTopLevel; |
---|
48 | if ( isTopLevel ) { |
---|
49 | varNums.clear(); |
---|
50 | nextVarNum = 0; |
---|
51 | isTopLevel = false; |
---|
52 | } // if |
---|
53 | mangleName << "__"; |
---|
54 | CodeGen::OperatorInfo opInfo; |
---|
55 | if ( operatorLookup( declaration->get_name(), opInfo ) ) { |
---|
56 | mangleName << opInfo.outputName; |
---|
57 | } else { |
---|
58 | mangleName << declaration->get_name(); |
---|
59 | } // if |
---|
60 | mangleName << "__"; |
---|
61 | maybeAccept( declaration->get_type(), *this ); |
---|
62 | if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { |
---|
63 | // want to be able to override autogenerated and intrinsic routines, |
---|
64 | // so they need a different name mangling |
---|
65 | if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { |
---|
66 | mangleName << "autogen__"; |
---|
67 | } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
68 | mangleName << "intrinsic__"; |
---|
69 | } else { |
---|
70 | // if we add another kind of overridable function, this has to change |
---|
71 | assert( false ); |
---|
72 | } // if |
---|
73 | } |
---|
74 | isTopLevel = wasTopLevel; |
---|
75 | } |
---|
76 | |
---|
77 | void Mangler::visit( ObjectDecl *declaration ) { |
---|
78 | mangleDecl( declaration ); |
---|
79 | } |
---|
80 | |
---|
81 | void Mangler::visit( FunctionDecl *declaration ) { |
---|
82 | mangleDecl( declaration ); |
---|
83 | } |
---|
84 | |
---|
85 | void Mangler::visit( VoidType *voidType ) { |
---|
86 | printQualifiers( voidType ); |
---|
87 | mangleName << "v"; |
---|
88 | } |
---|
89 | |
---|
90 | void Mangler::visit( BasicType *basicType ) { |
---|
91 | static const char *btLetter[] = { |
---|
92 | "b", // Bool |
---|
93 | "c", // Char |
---|
94 | "Sc", // SignedChar |
---|
95 | "Uc", // UnsignedChar |
---|
96 | "s", // ShortSignedInt |
---|
97 | "Us", // ShortUnsignedInt |
---|
98 | "i", // SignedInt |
---|
99 | "Ui", // UnsignedInt |
---|
100 | "l", // LongSignedInt |
---|
101 | "Ul", // LongUnsignedInt |
---|
102 | "q", // LongLongSignedInt |
---|
103 | "Uq", // LongLongUnsignedInt |
---|
104 | "f", // Float |
---|
105 | "d", // Double |
---|
106 | "r", // LongDouble |
---|
107 | "Xf", // FloatComplex |
---|
108 | "Xd", // DoubleComplex |
---|
109 | "Xr", // LongDoubleComplex |
---|
110 | "If", // FloatImaginary |
---|
111 | "Id", // DoubleImaginary |
---|
112 | "Ir", // LongDoubleImaginary |
---|
113 | }; |
---|
114 | |
---|
115 | printQualifiers( basicType ); |
---|
116 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
117 | } |
---|
118 | |
---|
119 | void Mangler::visit( PointerType *pointerType ) { |
---|
120 | printQualifiers( pointerType ); |
---|
121 | mangleName << "P"; |
---|
122 | maybeAccept( pointerType->get_base(), *this ); |
---|
123 | } |
---|
124 | |
---|
125 | void Mangler::visit( ArrayType *arrayType ) { |
---|
126 | // TODO: encode dimension |
---|
127 | printQualifiers( arrayType ); |
---|
128 | mangleName << "A0"; |
---|
129 | maybeAccept( arrayType->get_base(), *this ); |
---|
130 | } |
---|
131 | |
---|
132 | namespace { |
---|
133 | inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { |
---|
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 Mangler::visit( FunctionType *functionType ) { |
---|
142 | printQualifiers( functionType ); |
---|
143 | mangleName << "F"; |
---|
144 | std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); |
---|
145 | acceptAll( returnTypes, *this ); |
---|
146 | mangleName << "_"; |
---|
147 | std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); |
---|
148 | acceptAll( paramTypes, *this ); |
---|
149 | mangleName << "_"; |
---|
150 | } |
---|
151 | |
---|
152 | void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { |
---|
153 | printQualifiers( refType ); |
---|
154 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); |
---|
155 | } |
---|
156 | |
---|
157 | void Mangler::visit( StructInstType *aggregateUseType ) { |
---|
158 | mangleRef( aggregateUseType, "s" ); |
---|
159 | } |
---|
160 | |
---|
161 | void Mangler::visit( UnionInstType *aggregateUseType ) { |
---|
162 | mangleRef( aggregateUseType, "u" ); |
---|
163 | } |
---|
164 | |
---|
165 | void Mangler::visit( EnumInstType *aggregateUseType ) { |
---|
166 | mangleRef( aggregateUseType, "e" ); |
---|
167 | } |
---|
168 | |
---|
169 | void Mangler::visit( TypeInstType *typeInst ) { |
---|
170 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
171 | if ( varNum == varNums.end() ) { |
---|
172 | mangleRef( typeInst, "t" ); |
---|
173 | } else { |
---|
174 | printQualifiers( typeInst ); |
---|
175 | std::ostringstream numStream; |
---|
176 | numStream << varNum->second.first; |
---|
177 | switch ( (TypeDecl::Kind )varNum->second.second ) { |
---|
178 | case TypeDecl::Any: |
---|
179 | mangleName << "t"; |
---|
180 | break; |
---|
181 | case TypeDecl::Dtype: |
---|
182 | mangleName << "d"; |
---|
183 | break; |
---|
184 | case TypeDecl::Ftype: |
---|
185 | mangleName << "f"; |
---|
186 | break; |
---|
187 | } // switch |
---|
188 | mangleName << numStream.str(); |
---|
189 | } // if |
---|
190 | } |
---|
191 | |
---|
192 | void Mangler::visit( TupleType *tupleType ) { |
---|
193 | printQualifiers( tupleType ); |
---|
194 | mangleName << "T"; |
---|
195 | acceptAll( tupleType->get_types(), *this ); |
---|
196 | mangleName << "_"; |
---|
197 | } |
---|
198 | |
---|
199 | void Mangler::visit( TypeDecl *decl ) { |
---|
200 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
201 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); |
---|
202 | } |
---|
203 | |
---|
204 | void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
205 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
206 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
207 | } // for |
---|
208 | } |
---|
209 | |
---|
210 | void Mangler::printQualifiers( Type *type ) { |
---|
211 | if ( ! type->get_forall().empty() ) { |
---|
212 | std::list< std::string > assertionNames; |
---|
213 | int tcount = 0, dcount = 0, fcount = 0; |
---|
214 | mangleName << "A"; |
---|
215 | for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
216 | switch ( (*i)->get_kind() ) { |
---|
217 | case TypeDecl::Any: |
---|
218 | tcount++; |
---|
219 | break; |
---|
220 | case TypeDecl::Dtype: |
---|
221 | dcount++; |
---|
222 | break; |
---|
223 | case TypeDecl::Ftype: |
---|
224 | fcount++; |
---|
225 | break; |
---|
226 | } // switch |
---|
227 | varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); |
---|
228 | for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
229 | Mangler sub_mangler( mangleOverridable ); |
---|
230 | sub_mangler.nextVarNum = nextVarNum; |
---|
231 | sub_mangler.isTopLevel = false; |
---|
232 | sub_mangler.varNums = varNums; |
---|
233 | (*assert)->accept( sub_mangler ); |
---|
234 | assertionNames.push_back( sub_mangler.mangleName.str() ); |
---|
235 | } // for |
---|
236 | } // for |
---|
237 | mangleName << tcount << "_" << dcount << "_" << fcount << "_"; |
---|
238 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
239 | mangleName << "_"; |
---|
240 | } // if |
---|
241 | if ( type->get_isConst() ) { |
---|
242 | mangleName << "C"; |
---|
243 | } // if |
---|
244 | if ( type->get_isVolatile() ) { |
---|
245 | mangleName << "V"; |
---|
246 | } // if |
---|
247 | if ( type->get_isRestrict() ) { |
---|
248 | mangleName << "R"; |
---|
249 | } // if |
---|
250 | if ( type->get_isLvalue() ) { |
---|
251 | mangleName << "L"; |
---|
252 | } // if |
---|
253 | if ( type->get_isAtomic() ) { |
---|
254 | mangleName << "A"; |
---|
255 | } // if |
---|
256 | } |
---|
257 | } // namespace SymTab |
---|
258 | |
---|
259 | // Local Variables: // |
---|
260 | // tab-width: 4 // |
---|
261 | // mode: c++ // |
---|
262 | // compile-command: "make install" // |
---|
263 | // End: // |
---|