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