[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 |
---|
[4aa0858] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Wed Aug 19 15:52:24 2015 |
---|
| 13 | // Update Count : 19 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 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 { |
---|
[69911c11] | 32 | std::string Mangler::mangleType( Type *ty ) { |
---|
| 33 | Mangler mangler( false, true ); |
---|
| 34 | maybeAccept( ty, mangler ); |
---|
| 35 | return mangler.get_mangleName(); |
---|
| 36 | } |
---|
[8c49c0e] | 37 | |
---|
[69911c11] | 38 | Mangler::Mangler( bool mangleOverridable, bool typeMode ) |
---|
| 39 | : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {} |
---|
[8c49c0e] | 40 | |
---|
[a08ba92] | 41 | Mangler::Mangler( const Mangler &rhs ) : mangleName() { |
---|
[0dd3a2f] | 42 | varNums = rhs.varNums; |
---|
| 43 | nextVarNum = rhs.nextVarNum; |
---|
| 44 | isTopLevel = rhs.isTopLevel; |
---|
[4aa0858] | 45 | mangleOverridable = rhs.mangleOverridable; |
---|
[69911c11] | 46 | typeMode = rhs.typeMode; |
---|
[a08ba92] | 47 | } |
---|
[51b7345] | 48 | |
---|
[a08ba92] | 49 | void Mangler::mangleDecl( DeclarationWithType *declaration ) { |
---|
[0dd3a2f] | 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 ); |
---|
[4aa0858] | 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 | } |
---|
[0dd3a2f] | 77 | isTopLevel = wasTopLevel; |
---|
[a08ba92] | 78 | } |
---|
[51b7345] | 79 | |
---|
[a08ba92] | 80 | void Mangler::visit( ObjectDecl *declaration ) { |
---|
[0dd3a2f] | 81 | mangleDecl( declaration ); |
---|
[a08ba92] | 82 | } |
---|
[51b7345] | 83 | |
---|
[a08ba92] | 84 | void Mangler::visit( FunctionDecl *declaration ) { |
---|
[0dd3a2f] | 85 | mangleDecl( declaration ); |
---|
[a08ba92] | 86 | } |
---|
[51b7345] | 87 | |
---|
[a08ba92] | 88 | void Mangler::visit( VoidType *voidType ) { |
---|
[0dd3a2f] | 89 | printQualifiers( voidType ); |
---|
| 90 | mangleName << "v"; |
---|
[a08ba92] | 91 | } |
---|
[51b7345] | 92 | |
---|
[a08ba92] | 93 | void Mangler::visit( BasicType *basicType ) { |
---|
[0dd3a2f] | 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 | }; |
---|
[8c49c0e] | 117 | |
---|
[0dd3a2f] | 118 | printQualifiers( basicType ); |
---|
| 119 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
[a08ba92] | 120 | } |
---|
[51b7345] | 121 | |
---|
[a08ba92] | 122 | void Mangler::visit( PointerType *pointerType ) { |
---|
[0dd3a2f] | 123 | printQualifiers( pointerType ); |
---|
| 124 | mangleName << "P"; |
---|
| 125 | maybeAccept( pointerType->get_base(), *this ); |
---|
[a08ba92] | 126 | } |
---|
[51b7345] | 127 | |
---|
[a08ba92] | 128 | void Mangler::visit( ArrayType *arrayType ) { |
---|
[0dd3a2f] | 129 | // TODO: encode dimension |
---|
| 130 | printQualifiers( arrayType ); |
---|
| 131 | mangleName << "A0"; |
---|
| 132 | maybeAccept( arrayType->get_base(), *this ); |
---|
[a08ba92] | 133 | } |
---|
[51b7345] | 134 | |
---|
[a08ba92] | 135 | namespace { |
---|
[0dd3a2f] | 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 | } |
---|
[a08ba92] | 142 | } |
---|
[51b7345] | 143 | |
---|
[a08ba92] | 144 | void Mangler::visit( FunctionType *functionType ) { |
---|
[0dd3a2f] | 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 << "_"; |
---|
[a08ba92] | 153 | } |
---|
[51b7345] | 154 | |
---|
[a08ba92] | 155 | void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { |
---|
[0dd3a2f] | 156 | printQualifiers( refType ); |
---|
[8360977] | 157 | |
---|
[0dd3a2f] | 158 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); |
---|
[a08ba92] | 159 | } |
---|
[51b7345] | 160 | |
---|
[8360977] | 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 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
[69911c11] | 175 | maybeAccept( paramType->get_type(), *this ); |
---|
[8360977] | 176 | } |
---|
| 177 | mangleName << "_"; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | oldName << mangleName.str().length() << mangleName.str(); |
---|
| 181 | mangleName.str( oldName.str() ); |
---|
| 182 | } |
---|
| 183 | |
---|
[a08ba92] | 184 | void Mangler::visit( StructInstType *aggregateUseType ) { |
---|
[69911c11] | 185 | if ( typeMode ) mangleGenericRef( aggregateUseType, "s" ); |
---|
| 186 | else mangleRef( aggregateUseType, "s" ); |
---|
[a08ba92] | 187 | } |
---|
[51b7345] | 188 | |
---|
[a08ba92] | 189 | void Mangler::visit( UnionInstType *aggregateUseType ) { |
---|
[69911c11] | 190 | if ( typeMode ) mangleGenericRef( aggregateUseType, "u" ); |
---|
| 191 | else mangleRef( aggregateUseType, "u" ); |
---|
[a08ba92] | 192 | } |
---|
[51b7345] | 193 | |
---|
[a08ba92] | 194 | void Mangler::visit( EnumInstType *aggregateUseType ) { |
---|
[0dd3a2f] | 195 | mangleRef( aggregateUseType, "e" ); |
---|
[a08ba92] | 196 | } |
---|
[51b7345] | 197 | |
---|
[a08ba92] | 198 | void Mangler::visit( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 199 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
| 200 | if ( varNum == varNums.end() ) { |
---|
| 201 | mangleRef( typeInst, "t" ); |
---|
| 202 | } else { |
---|
| 203 | printQualifiers( typeInst ); |
---|
[5f2f2d7] | 204 | std::ostringstream numStream; |
---|
[0dd3a2f] | 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 | } // switch |
---|
[5f2f2d7] | 217 | mangleName << numStream.str(); |
---|
[0dd3a2f] | 218 | } // if |
---|
[a08ba92] | 219 | } |
---|
[51b7345] | 220 | |
---|
[a08ba92] | 221 | void Mangler::visit( TupleType *tupleType ) { |
---|
[0dd3a2f] | 222 | printQualifiers( tupleType ); |
---|
| 223 | mangleName << "T"; |
---|
| 224 | acceptAll( tupleType->get_types(), *this ); |
---|
| 225 | mangleName << "_"; |
---|
[a08ba92] | 226 | } |
---|
[51b7345] | 227 | |
---|
[44b7088] | 228 | void Mangler::visit( VarArgsType *varArgsType ) { |
---|
[540ddb7d] | 229 | printQualifiers( varArgsType ); |
---|
[44b7088] | 230 | mangleName << "VARGS"; |
---|
| 231 | } |
---|
| 232 | |
---|
[89e6ffc] | 233 | void Mangler::visit( ZeroType *zeroType ) { |
---|
| 234 | mangleName << "Z"; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | void Mangler::visit( OneType *oneType ) { |
---|
| 238 | mangleName << "O"; |
---|
| 239 | } |
---|
| 240 | |
---|
[a08ba92] | 241 | void Mangler::visit( TypeDecl *decl ) { |
---|
[0dd3a2f] | 242 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
| 243 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); |
---|
[a08ba92] | 244 | } |
---|
[51b7345] | 245 | |
---|
[a08ba92] | 246 | void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
[0dd3a2f] | 247 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
| 248 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
| 249 | } // for |
---|
[a08ba92] | 250 | } |
---|
[51b7345] | 251 | |
---|
[a08ba92] | 252 | void Mangler::printQualifiers( Type *type ) { |
---|
[78dd0da] | 253 | // skip if not including qualifiers |
---|
[69911c11] | 254 | if ( typeMode ) return; |
---|
[8c49c0e] | 255 | |
---|
[0dd3a2f] | 256 | if ( ! type->get_forall().empty() ) { |
---|
| 257 | std::list< std::string > assertionNames; |
---|
| 258 | int tcount = 0, dcount = 0, fcount = 0; |
---|
| 259 | mangleName << "A"; |
---|
[8c49c0e] | 260 | for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
[0dd3a2f] | 261 | switch ( (*i)->get_kind() ) { |
---|
| 262 | case TypeDecl::Any: |
---|
| 263 | tcount++; |
---|
| 264 | break; |
---|
| 265 | case TypeDecl::Dtype: |
---|
| 266 | dcount++; |
---|
| 267 | break; |
---|
| 268 | case TypeDecl::Ftype: |
---|
| 269 | fcount++; |
---|
| 270 | break; |
---|
| 271 | } // switch |
---|
| 272 | varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); |
---|
| 273 | for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
[69911c11] | 274 | Mangler sub_mangler( mangleOverridable, typeMode ); |
---|
[0dd3a2f] | 275 | sub_mangler.nextVarNum = nextVarNum; |
---|
| 276 | sub_mangler.isTopLevel = false; |
---|
| 277 | sub_mangler.varNums = varNums; |
---|
| 278 | (*assert)->accept( sub_mangler ); |
---|
[5f2f2d7] | 279 | assertionNames.push_back( sub_mangler.mangleName.str() ); |
---|
[0dd3a2f] | 280 | } // for |
---|
| 281 | } // for |
---|
| 282 | mangleName << tcount << "_" << dcount << "_" << fcount << "_"; |
---|
| 283 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
| 284 | mangleName << "_"; |
---|
| 285 | } // if |
---|
| 286 | if ( type->get_isConst() ) { |
---|
| 287 | mangleName << "C"; |
---|
| 288 | } // if |
---|
| 289 | if ( type->get_isVolatile() ) { |
---|
| 290 | mangleName << "V"; |
---|
| 291 | } // if |
---|
[8884112] | 292 | // Removed due to restrict not affecting function compatibility in GCC |
---|
| 293 | // if ( type->get_isRestrict() ) { |
---|
| 294 | // mangleName << "R"; |
---|
| 295 | // } // if |
---|
[0dd3a2f] | 296 | if ( type->get_isLvalue() ) { |
---|
| 297 | mangleName << "L"; |
---|
| 298 | } // if |
---|
| 299 | if ( type->get_isAtomic() ) { |
---|
| 300 | mangleName << "A"; |
---|
| 301 | } // if |
---|
[a08ba92] | 302 | } |
---|
[0dd3a2f] | 303 | } // namespace SymTab |
---|
| 304 | |
---|
| 305 | // Local Variables: // |
---|
| 306 | // tab-width: 4 // |
---|
| 307 | // mode: c++ // |
---|
| 308 | // compile-command: "make install" // |
---|
| 309 | // End: // |
---|