Changeset 0dd3a2f for translator/SymTab/Mangler.cc
- Timestamp:
- May 18, 2015, 11:20:23 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 51587aa
- Parents:
- a32b204
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/SymTab/Mangler.cc
ra32b204 r0dd3a2f 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 : Peter A. Buhr 12 // Last Modified On : Sun May 17 21:43:49 2015 13 // Update Count : 2 14 // 15 1 16 #include <cassert> 2 17 #include <string> … … 15 30 16 31 namespace SymTab { 17 Mangler::Mangler() : nextVarNum( 0 ), isTopLevel( true ) 18 {}32 Mangler::Mangler() : nextVarNum( 0 ), isTopLevel( true ) { 33 } 19 34 20 35 //Mangler::Mangler( const Mangler & ) … … 23 38 //} 24 39 Mangler::Mangler( const Mangler &rhs ) : mangleName() { 25 varNums = rhs.varNums;26 nextVarNum = rhs.nextVarNum;27 isTopLevel = rhs.isTopLevel;40 varNums = rhs.varNums; 41 nextVarNum = rhs.nextVarNum; 42 isTopLevel = rhs.isTopLevel; 28 43 } 29 44 30 45 void Mangler::mangleDecl( DeclarationWithType *declaration ) { 31 bool wasTopLevel = isTopLevel;32 if ( isTopLevel ) {33 34 35 36 }37 mangleName << "__";38 CodeGen::OperatorInfo opInfo;39 if ( operatorLookup( declaration->get_name(), opInfo ) ) {40 41 } else {42 43 }44 mangleName << "__";45 maybeAccept( declaration->get_type(), *this );46 isTopLevel = wasTopLevel;46 bool wasTopLevel = isTopLevel; 47 if ( isTopLevel ) { 48 varNums.clear(); 49 nextVarNum = 0; 50 isTopLevel = false; 51 } // if 52 mangleName << "__"; 53 CodeGen::OperatorInfo opInfo; 54 if ( operatorLookup( declaration->get_name(), opInfo ) ) { 55 mangleName << opInfo.outputName; 56 } else { 57 mangleName << declaration->get_name(); 58 } // if 59 mangleName << "__"; 60 maybeAccept( declaration->get_type(), *this ); 61 isTopLevel = wasTopLevel; 47 62 } 48 63 49 64 void Mangler::visit( ObjectDecl *declaration ) { 50 mangleDecl( declaration );65 mangleDecl( declaration ); 51 66 } 52 67 53 68 void Mangler::visit( FunctionDecl *declaration ) { 54 mangleDecl( declaration );69 mangleDecl( declaration ); 55 70 } 56 71 57 72 void Mangler::visit( VoidType *voidType ) { 58 printQualifiers( voidType );59 mangleName << "v";73 printQualifiers( voidType ); 74 mangleName << "v"; 60 75 } 61 76 62 77 void Mangler::visit( BasicType *basicType ) { 63 static const char *btLetter[] = {64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 };78 static const char *btLetter[] = { 79 "b", // Bool 80 "c", // Char 81 "Sc", // SignedChar 82 "Uc", // UnsignedChar 83 "s", // ShortSignedInt 84 "Us", // ShortUnsignedInt 85 "i", // SignedInt 86 "Ui", // UnsignedInt 87 "l", // LongSignedInt 88 "Ul", // LongUnsignedInt 89 "q", // LongLongSignedInt 90 "Uq", // LongLongUnsignedInt 91 "f", // Float 92 "d", // Double 93 "r", // LongDouble 94 "Xf", // FloatComplex 95 "Xd", // DoubleComplex 96 "Xr", // LongDoubleComplex 97 "If", // FloatImaginary 98 "Id", // DoubleImaginary 99 "Ir", // LongDoubleImaginary 100 }; 86 101 87 printQualifiers( basicType );88 mangleName << btLetter[ basicType->get_kind() ];102 printQualifiers( basicType ); 103 mangleName << btLetter[ basicType->get_kind() ]; 89 104 } 90 105 91 106 void Mangler::visit( PointerType *pointerType ) { 92 printQualifiers( pointerType );93 mangleName << "P";94 maybeAccept( pointerType->get_base(), *this );107 printQualifiers( pointerType ); 108 mangleName << "P"; 109 maybeAccept( pointerType->get_base(), *this ); 95 110 } 96 111 97 112 void Mangler::visit( ArrayType *arrayType ) { 98 // TODO: encode dimension99 printQualifiers( arrayType );100 mangleName << "A0";101 maybeAccept( arrayType->get_base(), *this );113 // TODO: encode dimension 114 printQualifiers( arrayType ); 115 mangleName << "A0"; 116 maybeAccept( arrayType->get_base(), *this ); 102 117 } 103 118 104 119 namespace { 105 inline std::list< Type* > 106 getTypes( const std::list< DeclarationWithType* > decls ) 107 { 108 std::list< Type* > ret; 109 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 110 std::mem_fun( &DeclarationWithType::get_type ) ); 111 return ret; 112 } 120 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { 121 std::list< Type* > ret; 122 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 123 std::mem_fun( &DeclarationWithType::get_type ) ); 124 return ret; 125 } 113 126 } 114 127 115 128 void Mangler::visit( FunctionType *functionType ) { 116 printQualifiers( functionType );117 mangleName << "F";118 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() );119 acceptAll( returnTypes, *this );120 mangleName << "_";121 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() );122 acceptAll( paramTypes, *this );123 mangleName << "_";129 printQualifiers( functionType ); 130 mangleName << "F"; 131 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); 132 acceptAll( returnTypes, *this ); 133 mangleName << "_"; 134 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); 135 acceptAll( paramTypes, *this ); 136 mangleName << "_"; 124 137 } 125 138 126 139 void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { 127 printQualifiers( refType );128 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();140 printQualifiers( refType ); 141 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 129 142 } 130 143 131 144 void Mangler::visit( StructInstType *aggregateUseType ) { 132 mangleRef( aggregateUseType, "s" );145 mangleRef( aggregateUseType, "s" ); 133 146 } 134 147 135 148 void Mangler::visit( UnionInstType *aggregateUseType ) { 136 mangleRef( aggregateUseType, "u" );149 mangleRef( aggregateUseType, "u" ); 137 150 } 138 151 139 152 void Mangler::visit( EnumInstType *aggregateUseType ) { 140 mangleRef( aggregateUseType, "e" );153 mangleRef( aggregateUseType, "e" ); 141 154 } 142 155 143 156 void Mangler::visit( TypeInstType *typeInst ) { 144 VarMapType::iterator varNum = varNums.find( typeInst->get_name() );145 if ( varNum == varNums.end() ) {146 147 } else {148 149 150 151 152 153 154 mangleName << "t";155 break;156 157 mangleName << "d";158 break;159 160 mangleName << "f";161 break;162 }163 164 }157 VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); 158 if ( varNum == varNums.end() ) { 159 mangleRef( typeInst, "t" ); 160 } else { 161 printQualifiers( typeInst ); 162 std::ostrstream numStream; 163 numStream << varNum->second.first; 164 mangleName << (numStream.pcount() + 1); 165 switch ( (TypeDecl::Kind )varNum->second.second ) { 166 case TypeDecl::Any: 167 mangleName << "t"; 168 break; 169 case TypeDecl::Dtype: 170 mangleName << "d"; 171 break; 172 case TypeDecl::Ftype: 173 mangleName << "f"; 174 break; 175 } // switch 176 mangleName << std::string( numStream.str(), numStream.pcount() ); 177 } // if 165 178 } 166 179 167 180 void Mangler::visit( TupleType *tupleType ) { 168 printQualifiers( tupleType );169 mangleName << "T";170 acceptAll( tupleType->get_types(), *this );171 mangleName << "_";181 printQualifiers( tupleType ); 182 mangleName << "T"; 183 acceptAll( tupleType->get_types(), *this ); 184 mangleName << "_"; 172 185 } 173 186 174 187 void Mangler::visit( TypeDecl *decl ) { 175 static const char *typePrefix[] = { "BT", "BD", "BF" };176 mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name();188 static const char *typePrefix[] = { "BT", "BD", "BF" }; 189 mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name(); 177 190 } 178 191 179 192 void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { 180 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) {181 182 }193 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { 194 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; 195 } // for 183 196 } 184 197 185 198 void Mangler::printQualifiers( Type *type ) { 186 if ( ! type->get_forall().empty() ) { 187 std::list< std::string > assertionNames; 188 int tcount = 0, dcount = 0, fcount = 0; 189 mangleName << "A"; 190 for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { 191 switch ( (*i)->get_kind() ) { 192 case TypeDecl::Any: 193 tcount++; 194 break; 195 case TypeDecl::Dtype: 196 dcount++; 197 break; 198 case TypeDecl::Ftype: 199 fcount++; 200 break; 201 } 202 varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); 203 for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { 204 Mangler sub_mangler; 205 sub_mangler.nextVarNum = nextVarNum; 206 sub_mangler.isTopLevel = false; 207 sub_mangler.varNums = varNums; 208 (*assert)->accept( sub_mangler ); 209 assertionNames.push_back( std::string( sub_mangler.mangleName.str(), sub_mangler.mangleName.pcount() ) ); 210 } 211 } 212 mangleName << tcount << "_" << dcount << "_" << fcount << "_"; 213 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 214 mangleName << "_"; 215 } 216 if ( type->get_isConst() ) { 217 mangleName << "C"; 218 } 219 if ( type->get_isVolatile() ) { 220 mangleName << "V"; 221 } 222 if ( type->get_isRestrict() ) { 223 mangleName << "R"; 224 } 225 if ( type->get_isLvalue() ) { 226 mangleName << "L"; 227 } 228 if ( type->get_isAtomic() ) { 229 mangleName << "A"; 230 } 231 } 232 } // SymTab 199 if ( ! type->get_forall().empty() ) { 200 std::list< std::string > assertionNames; 201 int tcount = 0, dcount = 0, fcount = 0; 202 mangleName << "A"; 203 for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { 204 switch ( (*i)->get_kind() ) { 205 case TypeDecl::Any: 206 tcount++; 207 break; 208 case TypeDecl::Dtype: 209 dcount++; 210 break; 211 case TypeDecl::Ftype: 212 fcount++; 213 break; 214 } // switch 215 varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); 216 for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { 217 Mangler sub_mangler; 218 sub_mangler.nextVarNum = nextVarNum; 219 sub_mangler.isTopLevel = false; 220 sub_mangler.varNums = varNums; 221 (*assert)->accept( sub_mangler ); 222 assertionNames.push_back( std::string( sub_mangler.mangleName.str(), sub_mangler.mangleName.pcount() ) ); 223 } // for 224 } // for 225 mangleName << tcount << "_" << dcount << "_" << fcount << "_"; 226 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 227 mangleName << "_"; 228 } // if 229 if ( type->get_isConst() ) { 230 mangleName << "C"; 231 } // if 232 if ( type->get_isVolatile() ) { 233 mangleName << "V"; 234 } // if 235 if ( type->get_isRestrict() ) { 236 mangleName << "R"; 237 } // if 238 if ( type->get_isLvalue() ) { 239 mangleName << "L"; 240 } // if 241 if ( type->get_isAtomic() ) { 242 mangleName << "A"; 243 } // if 244 } 245 } // namespace SymTab 246 247 // Local Variables: // 248 // tab-width: 4 // 249 // mode: c++ // 250 // compile-command: "make install" // 251 // End: //
Note: See TracChangeset
for help on using the changeset viewer.