[0dd3a2f] | 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 | // |
---|
[8c49c0e] | 7 | // Mangler.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:40:29 2015 |
---|
[fa4805f] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Jun 28 15:31:00 2017 |
---|
| 13 | // Update Count : 21 |
---|
[0dd3a2f] | 14 | // |
---|
[30f9072] | 15 | #include "Mangler.h" |
---|
[0dd3a2f] | 16 | |
---|
[30f9072] | 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... |
---|
[c3acf0aa] | 22 | #include <string> // for string, char_traits, operator<< |
---|
[51b7345] | 23 | |
---|
[30f9072] | 24 | #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
[c3acf0aa] | 25 | #include "Common/SemanticError.h" // for SemanticError |
---|
[30f9072] | 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... |
---|
[51b7345] | 31 | |
---|
| 32 | namespace SymTab { |
---|
[69911c11] | 33 | std::string Mangler::mangleType( Type *ty ) { |
---|
| 34 | Mangler mangler( false, true ); |
---|
| 35 | maybeAccept( ty, mangler ); |
---|
| 36 | return mangler.get_mangleName(); |
---|
| 37 | } |
---|
[8c49c0e] | 38 | |
---|
[69911c11] | 39 | Mangler::Mangler( bool mangleOverridable, bool typeMode ) |
---|
| 40 | : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {} |
---|
[8c49c0e] | 41 | |
---|
[a08ba92] | 42 | Mangler::Mangler( const Mangler &rhs ) : mangleName() { |
---|
[0dd3a2f] | 43 | varNums = rhs.varNums; |
---|
| 44 | nextVarNum = rhs.nextVarNum; |
---|
| 45 | isTopLevel = rhs.isTopLevel; |
---|
[4aa0858] | 46 | mangleOverridable = rhs.mangleOverridable; |
---|
[69911c11] | 47 | typeMode = rhs.typeMode; |
---|
[a08ba92] | 48 | } |
---|
[51b7345] | 49 | |
---|
[a08ba92] | 50 | void Mangler::mangleDecl( DeclarationWithType *declaration ) { |
---|
[0dd3a2f] | 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 ); |
---|
[4aa0858] | 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 |
---|
[fa4805f] | 75 | assert( false && "unknown overrideable linkage" ); |
---|
[4aa0858] | 76 | } // if |
---|
| 77 | } |
---|
[0dd3a2f] | 78 | isTopLevel = wasTopLevel; |
---|
[a08ba92] | 79 | } |
---|
[51b7345] | 80 | |
---|
[a08ba92] | 81 | void Mangler::visit( ObjectDecl *declaration ) { |
---|
[0dd3a2f] | 82 | mangleDecl( declaration ); |
---|
[a08ba92] | 83 | } |
---|
[51b7345] | 84 | |
---|
[a08ba92] | 85 | void Mangler::visit( FunctionDecl *declaration ) { |
---|
[0dd3a2f] | 86 | mangleDecl( declaration ); |
---|
[a08ba92] | 87 | } |
---|
[51b7345] | 88 | |
---|
[a08ba92] | 89 | void Mangler::visit( VoidType *voidType ) { |
---|
[0dd3a2f] | 90 | printQualifiers( voidType ); |
---|
| 91 | mangleName << "v"; |
---|
[a08ba92] | 92 | } |
---|
[51b7345] | 93 | |
---|
[a08ba92] | 94 | void Mangler::visit( BasicType *basicType ) { |
---|
[0dd3a2f] | 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 | }; |
---|
[8c49c0e] | 118 | |
---|
[0dd3a2f] | 119 | printQualifiers( basicType ); |
---|
| 120 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
[a08ba92] | 121 | } |
---|
[51b7345] | 122 | |
---|
[a08ba92] | 123 | void Mangler::visit( PointerType *pointerType ) { |
---|
[0dd3a2f] | 124 | printQualifiers( pointerType ); |
---|
| 125 | mangleName << "P"; |
---|
| 126 | maybeAccept( pointerType->get_base(), *this ); |
---|
[a08ba92] | 127 | } |
---|
[51b7345] | 128 | |
---|
[a08ba92] | 129 | void Mangler::visit( ArrayType *arrayType ) { |
---|
[0dd3a2f] | 130 | // TODO: encode dimension |
---|
| 131 | printQualifiers( arrayType ); |
---|
| 132 | mangleName << "A0"; |
---|
| 133 | maybeAccept( arrayType->get_base(), *this ); |
---|
[a08ba92] | 134 | } |
---|
[51b7345] | 135 | |
---|
[25bd9074] | 136 | void Mangler::visit( ReferenceType *refType ) { |
---|
| 137 | printQualifiers( refType ); |
---|
| 138 | mangleName << "R"; |
---|
| 139 | maybeAccept( refType->get_base(), *this ); |
---|
| 140 | } |
---|
| 141 | |
---|
[a08ba92] | 142 | namespace { |
---|
[0dd3a2f] | 143 | inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { |
---|
| 144 | std::list< Type* > ret; |
---|
| 145 | std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), |
---|
| 146 | std::mem_fun( &DeclarationWithType::get_type ) ); |
---|
| 147 | return ret; |
---|
| 148 | } |
---|
[a08ba92] | 149 | } |
---|
[51b7345] | 150 | |
---|
[a08ba92] | 151 | void Mangler::visit( FunctionType *functionType ) { |
---|
[0dd3a2f] | 152 | printQualifiers( functionType ); |
---|
| 153 | mangleName << "F"; |
---|
| 154 | std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); |
---|
| 155 | acceptAll( returnTypes, *this ); |
---|
| 156 | mangleName << "_"; |
---|
| 157 | std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); |
---|
| 158 | acceptAll( paramTypes, *this ); |
---|
| 159 | mangleName << "_"; |
---|
[a08ba92] | 160 | } |
---|
[51b7345] | 161 | |
---|
[a08ba92] | 162 | void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { |
---|
[0dd3a2f] | 163 | printQualifiers( refType ); |
---|
[8360977] | 164 | |
---|
[0dd3a2f] | 165 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); |
---|
[a08ba92] | 166 | } |
---|
[51b7345] | 167 | |
---|
[8360977] | 168 | void Mangler::mangleGenericRef( ReferenceToType *refType, std::string prefix ) { |
---|
| 169 | printQualifiers( refType ); |
---|
| 170 | |
---|
| 171 | std::ostringstream oldName( mangleName.str() ); |
---|
| 172 | mangleName.clear(); |
---|
| 173 | |
---|
| 174 | mangleName << prefix << refType->get_name(); |
---|
| 175 | |
---|
| 176 | std::list< Expression* >& params = refType->get_parameters(); |
---|
| 177 | if ( ! params.empty() ) { |
---|
| 178 | mangleName << "_"; |
---|
| 179 | for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 180 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
[064cb18] | 181 | assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); |
---|
[69911c11] | 182 | maybeAccept( paramType->get_type(), *this ); |
---|
[8360977] | 183 | } |
---|
| 184 | mangleName << "_"; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | oldName << mangleName.str().length() << mangleName.str(); |
---|
| 188 | mangleName.str( oldName.str() ); |
---|
| 189 | } |
---|
| 190 | |
---|
[a08ba92] | 191 | void Mangler::visit( StructInstType *aggregateUseType ) { |
---|
[69911c11] | 192 | if ( typeMode ) mangleGenericRef( aggregateUseType, "s" ); |
---|
| 193 | else mangleRef( aggregateUseType, "s" ); |
---|
[a08ba92] | 194 | } |
---|
[51b7345] | 195 | |
---|
[a08ba92] | 196 | void Mangler::visit( UnionInstType *aggregateUseType ) { |
---|
[69911c11] | 197 | if ( typeMode ) mangleGenericRef( aggregateUseType, "u" ); |
---|
| 198 | else mangleRef( aggregateUseType, "u" ); |
---|
[a08ba92] | 199 | } |
---|
[51b7345] | 200 | |
---|
[a08ba92] | 201 | void Mangler::visit( EnumInstType *aggregateUseType ) { |
---|
[0dd3a2f] | 202 | mangleRef( aggregateUseType, "e" ); |
---|
[a08ba92] | 203 | } |
---|
[51b7345] | 204 | |
---|
[a08ba92] | 205 | void Mangler::visit( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 206 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
| 207 | if ( varNum == varNums.end() ) { |
---|
| 208 | mangleRef( typeInst, "t" ); |
---|
| 209 | } else { |
---|
| 210 | printQualifiers( typeInst ); |
---|
[5f2f2d7] | 211 | std::ostringstream numStream; |
---|
[0dd3a2f] | 212 | numStream << varNum->second.first; |
---|
| 213 | switch ( (TypeDecl::Kind )varNum->second.second ) { |
---|
| 214 | case TypeDecl::Any: |
---|
| 215 | mangleName << "t"; |
---|
| 216 | break; |
---|
| 217 | case TypeDecl::Dtype: |
---|
| 218 | mangleName << "d"; |
---|
| 219 | break; |
---|
| 220 | case TypeDecl::Ftype: |
---|
| 221 | mangleName << "f"; |
---|
| 222 | break; |
---|
[8bf784a] | 223 | case TypeDecl::Ttype: |
---|
| 224 | mangleName << "tVARGS"; |
---|
| 225 | break; |
---|
| 226 | default: |
---|
| 227 | assert( false ); |
---|
[0dd3a2f] | 228 | } // switch |
---|
[5f2f2d7] | 229 | mangleName << numStream.str(); |
---|
[0dd3a2f] | 230 | } // if |
---|
[a08ba92] | 231 | } |
---|
[51b7345] | 232 | |
---|
[a08ba92] | 233 | void Mangler::visit( TupleType *tupleType ) { |
---|
[0dd3a2f] | 234 | printQualifiers( tupleType ); |
---|
| 235 | mangleName << "T"; |
---|
| 236 | acceptAll( tupleType->get_types(), *this ); |
---|
| 237 | mangleName << "_"; |
---|
[a08ba92] | 238 | } |
---|
[51b7345] | 239 | |
---|
[44b7088] | 240 | void Mangler::visit( VarArgsType *varArgsType ) { |
---|
[540ddb7d] | 241 | printQualifiers( varArgsType ); |
---|
[44b7088] | 242 | mangleName << "VARGS"; |
---|
| 243 | } |
---|
| 244 | |
---|
[af397ef8] | 245 | void Mangler::visit( __attribute__((unused)) ZeroType *zeroType ) { |
---|
[89e6ffc] | 246 | mangleName << "Z"; |
---|
| 247 | } |
---|
| 248 | |
---|
[af397ef8] | 249 | void Mangler::visit( __attribute__((unused)) OneType *oneType ) { |
---|
[89e6ffc] | 250 | mangleName << "O"; |
---|
| 251 | } |
---|
| 252 | |
---|
[a08ba92] | 253 | void Mangler::visit( TypeDecl *decl ) { |
---|
[0dd3a2f] | 254 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
| 255 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); |
---|
[a08ba92] | 256 | } |
---|
[51b7345] | 257 | |
---|
[a08ba92] | 258 | void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
[0dd3a2f] | 259 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
| 260 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
| 261 | } // for |
---|
[a08ba92] | 262 | } |
---|
[51b7345] | 263 | |
---|
[a08ba92] | 264 | void Mangler::printQualifiers( Type *type ) { |
---|
[78dd0da] | 265 | // skip if not including qualifiers |
---|
[69911c11] | 266 | if ( typeMode ) return; |
---|
[8c49c0e] | 267 | |
---|
[0dd3a2f] | 268 | if ( ! type->get_forall().empty() ) { |
---|
| 269 | std::list< std::string > assertionNames; |
---|
[8bf784a] | 270 | int tcount = 0, dcount = 0, fcount = 0, vcount = 0; |
---|
[0dd3a2f] | 271 | mangleName << "A"; |
---|
[8c49c0e] | 272 | for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
[0dd3a2f] | 273 | switch ( (*i)->get_kind() ) { |
---|
| 274 | case TypeDecl::Any: |
---|
| 275 | tcount++; |
---|
| 276 | break; |
---|
| 277 | case TypeDecl::Dtype: |
---|
| 278 | dcount++; |
---|
| 279 | break; |
---|
| 280 | case TypeDecl::Ftype: |
---|
| 281 | fcount++; |
---|
| 282 | break; |
---|
[8bf784a] | 283 | case TypeDecl::Ttype: |
---|
| 284 | vcount++; |
---|
| 285 | break; |
---|
| 286 | default: |
---|
| 287 | assert( false ); |
---|
[0dd3a2f] | 288 | } // switch |
---|
| 289 | varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); |
---|
| 290 | for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
[69911c11] | 291 | Mangler sub_mangler( mangleOverridable, typeMode ); |
---|
[0dd3a2f] | 292 | sub_mangler.nextVarNum = nextVarNum; |
---|
| 293 | sub_mangler.isTopLevel = false; |
---|
| 294 | sub_mangler.varNums = varNums; |
---|
| 295 | (*assert)->accept( sub_mangler ); |
---|
[5f2f2d7] | 296 | assertionNames.push_back( sub_mangler.mangleName.str() ); |
---|
[0dd3a2f] | 297 | } // for |
---|
| 298 | } // for |
---|
[8bf784a] | 299 | mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; |
---|
[0dd3a2f] | 300 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
| 301 | mangleName << "_"; |
---|
| 302 | } // if |
---|
[615a096] | 303 | if ( type->get_const() ) { |
---|
[0dd3a2f] | 304 | mangleName << "C"; |
---|
| 305 | } // if |
---|
[615a096] | 306 | if ( type->get_volatile() ) { |
---|
[0dd3a2f] | 307 | mangleName << "V"; |
---|
| 308 | } // if |
---|
[8884112] | 309 | // Removed due to restrict not affecting function compatibility in GCC |
---|
| 310 | // if ( type->get_isRestrict() ) { |
---|
[25bd9074] | 311 | // mangleName << "E"; |
---|
[8884112] | 312 | // } // if |
---|
[615a096] | 313 | if ( type->get_lvalue() ) { |
---|
[0dd3a2f] | 314 | mangleName << "L"; |
---|
| 315 | } // if |
---|
[615a096] | 316 | if ( type->get_atomic() ) { |
---|
[0dd3a2f] | 317 | mangleName << "A"; |
---|
| 318 | } // if |
---|
[a08ba92] | 319 | } |
---|
[0dd3a2f] | 320 | } // namespace SymTab |
---|
| 321 | |
---|
| 322 | // Local Variables: // |
---|
| 323 | // tab-width: 4 // |
---|
| 324 | // mode: c++ // |
---|
| 325 | // compile-command: "make install" // |
---|
| 326 | // End: // |
---|