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