| 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 : Mon Jun  8 15:12:12 2015
 | 
|---|
| 13 | // Update Count     : 8
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 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 {
 | 
|---|
| 32 |         Mangler::Mangler() : nextVarNum( 0 ), isTopLevel( true ) {
 | 
|---|
| 33 |         }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | //Mangler::Mangler( const Mangler & )
 | 
|---|
| 36 | //  : mangleName(), varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( isTopLevel )
 | 
|---|
| 37 | //{
 | 
|---|
| 38 | //}
 | 
|---|
| 39 |         Mangler::Mangler( const Mangler &rhs ) : mangleName() {
 | 
|---|
| 40 |                 varNums = rhs.varNums;
 | 
|---|
| 41 |                 nextVarNum = rhs.nextVarNum;
 | 
|---|
| 42 |                 isTopLevel = rhs.isTopLevel;
 | 
|---|
| 43 |         }
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         void Mangler::mangleDecl( DeclarationWithType *declaration ) {
 | 
|---|
| 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;
 | 
|---|
| 62 |         }
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         void Mangler::visit( ObjectDecl *declaration ) {
 | 
|---|
| 65 |                 mangleDecl( declaration );
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         void Mangler::visit( FunctionDecl *declaration ) {
 | 
|---|
| 69 |                 mangleDecl( declaration );
 | 
|---|
| 70 |         }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         void Mangler::visit( VoidType *voidType ) {
 | 
|---|
| 73 |                 printQualifiers( voidType );
 | 
|---|
| 74 |                 mangleName << "v";
 | 
|---|
| 75 |         }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         void Mangler::visit( BasicType *basicType ) {
 | 
|---|
| 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 |                 };
 | 
|---|
| 101 |   
 | 
|---|
| 102 |                 printQualifiers( basicType );
 | 
|---|
| 103 |                 mangleName << btLetter[ basicType->get_kind() ];
 | 
|---|
| 104 |         }
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         void Mangler::visit( PointerType *pointerType ) {
 | 
|---|
| 107 |                 printQualifiers( pointerType );
 | 
|---|
| 108 |                 mangleName << "P";
 | 
|---|
| 109 |                 maybeAccept( pointerType->get_base(), *this );
 | 
|---|
| 110 |         }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         void Mangler::visit( ArrayType *arrayType ) {
 | 
|---|
| 113 |                 // TODO: encode dimension
 | 
|---|
| 114 |                 printQualifiers( arrayType );
 | 
|---|
| 115 |                 mangleName << "A0";
 | 
|---|
| 116 |                 maybeAccept( arrayType->get_base(), *this );
 | 
|---|
| 117 |         }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |         namespace {
 | 
|---|
| 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 |                 }
 | 
|---|
| 126 |         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         void Mangler::visit( FunctionType *functionType ) {
 | 
|---|
| 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 << "_";
 | 
|---|
| 137 |         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) {
 | 
|---|
| 140 |                 printQualifiers( refType );
 | 
|---|
| 141 |                 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
 | 
|---|
| 142 |         }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         void Mangler::visit( StructInstType *aggregateUseType ) {
 | 
|---|
| 145 |                 mangleRef( aggregateUseType, "s" );
 | 
|---|
| 146 |         }
 | 
|---|
| 147 | 
 | 
|---|
| 148 |         void Mangler::visit( UnionInstType *aggregateUseType ) {
 | 
|---|
| 149 |                 mangleRef( aggregateUseType, "u" );
 | 
|---|
| 150 |         }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         void Mangler::visit( EnumInstType *aggregateUseType ) {
 | 
|---|
| 153 |                 mangleRef( aggregateUseType, "e" );
 | 
|---|
| 154 |         }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |         void Mangler::visit( TypeInstType *typeInst ) {
 | 
|---|
| 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::ostringstream numStream;
 | 
|---|
| 163 |                         numStream << varNum->second.first;
 | 
|---|
| 164 |                         switch ( (TypeDecl::Kind )varNum->second.second ) {
 | 
|---|
| 165 |                           case TypeDecl::Any:
 | 
|---|
| 166 |                                 mangleName << "t";
 | 
|---|
| 167 |                                 break;
 | 
|---|
| 168 |                           case TypeDecl::Dtype:
 | 
|---|
| 169 |                                 mangleName << "d";
 | 
|---|
| 170 |                                 break;
 | 
|---|
| 171 |                           case TypeDecl::Ftype:
 | 
|---|
| 172 |                                 mangleName << "f";
 | 
|---|
| 173 |                                 break;
 | 
|---|
| 174 |                         } // switch
 | 
|---|
| 175 |                         mangleName << numStream.str();
 | 
|---|
| 176 |                 } // if
 | 
|---|
| 177 |         }
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         void Mangler::visit( TupleType *tupleType ) {
 | 
|---|
| 180 |                 printQualifiers( tupleType );
 | 
|---|
| 181 |                 mangleName << "T";
 | 
|---|
| 182 |                 acceptAll( tupleType->get_types(), *this );
 | 
|---|
| 183 |                 mangleName << "_";
 | 
|---|
| 184 |         }
 | 
|---|
| 185 | 
 | 
|---|
| 186 |         void Mangler::visit( TypeDecl *decl ) {
 | 
|---|
| 187 |                 static const char *typePrefix[] = { "BT", "BD", "BF" };
 | 
|---|
| 188 |                 mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name();
 | 
|---|
| 189 |         }
 | 
|---|
| 190 | 
 | 
|---|
| 191 |         void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) {
 | 
|---|
| 192 |                 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) {
 | 
|---|
| 193 |                         os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl;
 | 
|---|
| 194 |                 } // for
 | 
|---|
| 195 |         }
 | 
|---|
| 196 | 
 | 
|---|
| 197 |         void Mangler::printQualifiers( Type *type ) {
 | 
|---|
| 198 |                 if ( ! type->get_forall().empty() ) {
 | 
|---|
| 199 |                         std::list< std::string > assertionNames;
 | 
|---|
| 200 |                         int tcount = 0, dcount = 0, fcount = 0;
 | 
|---|
| 201 |                         mangleName << "A";
 | 
|---|
| 202 |                         for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
 | 
|---|
| 203 |                                 switch ( (*i)->get_kind() ) {
 | 
|---|
| 204 |                                   case TypeDecl::Any:
 | 
|---|
| 205 |                                         tcount++;
 | 
|---|
| 206 |                                         break;
 | 
|---|
| 207 |                                   case TypeDecl::Dtype:
 | 
|---|
| 208 |                                         dcount++;
 | 
|---|
| 209 |                                         break;
 | 
|---|
| 210 |                                   case TypeDecl::Ftype:
 | 
|---|
| 211 |                                         fcount++;
 | 
|---|
| 212 |                                         break;
 | 
|---|
| 213 |                                 } // switch
 | 
|---|
| 214 |                                 varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() );
 | 
|---|
| 215 |                                 for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
 | 
|---|
| 216 |                                         Mangler sub_mangler;
 | 
|---|
| 217 |                                         sub_mangler.nextVarNum = nextVarNum;
 | 
|---|
| 218 |                                         sub_mangler.isTopLevel = false;
 | 
|---|
| 219 |                                         sub_mangler.varNums = varNums;
 | 
|---|
| 220 |                                         (*assert)->accept( sub_mangler );
 | 
|---|
| 221 |                                         assertionNames.push_back( sub_mangler.mangleName.str() );
 | 
|---|
| 222 |                                 } // for
 | 
|---|
| 223 |                         } // for
 | 
|---|
| 224 |                         mangleName << tcount << "_" << dcount << "_" << fcount << "_";
 | 
|---|
| 225 |                         std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
 | 
|---|
| 226 |                         mangleName << "_";
 | 
|---|
| 227 |                 } // if
 | 
|---|
| 228 |                 if ( type->get_isConst() ) {
 | 
|---|
| 229 |                         mangleName << "C";
 | 
|---|
| 230 |                 } // if
 | 
|---|
| 231 |                 if ( type->get_isVolatile() ) {
 | 
|---|
| 232 |                         mangleName << "V";
 | 
|---|
| 233 |                 } // if
 | 
|---|
| 234 |                 if ( type->get_isRestrict() ) {
 | 
|---|
| 235 |                         mangleName << "R";
 | 
|---|
| 236 |                 } // if
 | 
|---|
| 237 |                 if ( type->get_isLvalue() ) {
 | 
|---|
| 238 |                         mangleName << "L";
 | 
|---|
| 239 |                 } // if
 | 
|---|
| 240 |                 if ( type->get_isAtomic() ) {
 | 
|---|
| 241 |                         mangleName << "A";
 | 
|---|
| 242 |                 } // if
 | 
|---|
| 243 |         }
 | 
|---|
| 244 | } // namespace SymTab
 | 
|---|
| 245 | 
 | 
|---|
| 246 | // Local Variables: //
 | 
|---|
| 247 | // tab-width: 4 //
 | 
|---|
| 248 | // mode: c++ //
 | 
|---|
| 249 | // compile-command: "make install" //
 | 
|---|
| 250 | // End: //
 | 
|---|