[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
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[a08ba92] | 12 | // Last Modified On : Tue May 19 16:50:47 2015
|
---|
| 13 | // Update Count : 3
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 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 {
|
---|
[a08ba92] | 32 | Mangler::Mangler() : nextVarNum( 0 ), isTopLevel( true ) {
|
---|
[0dd3a2f] | 33 | }
|
---|
[51b73452] | 34 |
|
---|
| 35 | //Mangler::Mangler( const Mangler & )
|
---|
| 36 | // : mangleName(), varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( isTopLevel )
|
---|
| 37 | //{
|
---|
| 38 | //}
|
---|
[a08ba92] | 39 | Mangler::Mangler( const Mangler &rhs ) : mangleName() {
|
---|
[0dd3a2f] | 40 | varNums = rhs.varNums;
|
---|
| 41 | nextVarNum = rhs.nextVarNum;
|
---|
| 42 | isTopLevel = rhs.isTopLevel;
|
---|
[a08ba92] | 43 | }
|
---|
[51b73452] | 44 |
|
---|
[a08ba92] | 45 | void Mangler::mangleDecl( DeclarationWithType *declaration ) {
|
---|
[0dd3a2f] | 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;
|
---|
[a08ba92] | 62 | }
|
---|
[51b73452] | 63 |
|
---|
[a08ba92] | 64 | void Mangler::visit( ObjectDecl *declaration ) {
|
---|
[0dd3a2f] | 65 | mangleDecl( declaration );
|
---|
[a08ba92] | 66 | }
|
---|
[51b73452] | 67 |
|
---|
[a08ba92] | 68 | void Mangler::visit( FunctionDecl *declaration ) {
|
---|
[0dd3a2f] | 69 | mangleDecl( declaration );
|
---|
[a08ba92] | 70 | }
|
---|
[51b73452] | 71 |
|
---|
[a08ba92] | 72 | void Mangler::visit( VoidType *voidType ) {
|
---|
[0dd3a2f] | 73 | printQualifiers( voidType );
|
---|
| 74 | mangleName << "v";
|
---|
[a08ba92] | 75 | }
|
---|
[51b73452] | 76 |
|
---|
[a08ba92] | 77 | void Mangler::visit( BasicType *basicType ) {
|
---|
[0dd3a2f] | 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 | };
|
---|
[51b73452] | 101 |
|
---|
[0dd3a2f] | 102 | printQualifiers( basicType );
|
---|
| 103 | mangleName << btLetter[ basicType->get_kind() ];
|
---|
[a08ba92] | 104 | }
|
---|
[51b73452] | 105 |
|
---|
[a08ba92] | 106 | void Mangler::visit( PointerType *pointerType ) {
|
---|
[0dd3a2f] | 107 | printQualifiers( pointerType );
|
---|
| 108 | mangleName << "P";
|
---|
| 109 | maybeAccept( pointerType->get_base(), *this );
|
---|
[a08ba92] | 110 | }
|
---|
[51b73452] | 111 |
|
---|
[a08ba92] | 112 | void Mangler::visit( ArrayType *arrayType ) {
|
---|
[0dd3a2f] | 113 | // TODO: encode dimension
|
---|
| 114 | printQualifiers( arrayType );
|
---|
| 115 | mangleName << "A0";
|
---|
| 116 | maybeAccept( arrayType->get_base(), *this );
|
---|
[a08ba92] | 117 | }
|
---|
[51b73452] | 118 |
|
---|
[a08ba92] | 119 | namespace {
|
---|
[0dd3a2f] | 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 | }
|
---|
[a08ba92] | 126 | }
|
---|
[51b73452] | 127 |
|
---|
[a08ba92] | 128 | void Mangler::visit( FunctionType *functionType ) {
|
---|
[0dd3a2f] | 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 << "_";
|
---|
[a08ba92] | 137 | }
|
---|
[51b73452] | 138 |
|
---|
[a08ba92] | 139 | void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) {
|
---|
[0dd3a2f] | 140 | printQualifiers( refType );
|
---|
| 141 | mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
|
---|
[a08ba92] | 142 | }
|
---|
[51b73452] | 143 |
|
---|
[a08ba92] | 144 | void Mangler::visit( StructInstType *aggregateUseType ) {
|
---|
[0dd3a2f] | 145 | mangleRef( aggregateUseType, "s" );
|
---|
[a08ba92] | 146 | }
|
---|
[51b73452] | 147 |
|
---|
[a08ba92] | 148 | void Mangler::visit( UnionInstType *aggregateUseType ) {
|
---|
[0dd3a2f] | 149 | mangleRef( aggregateUseType, "u" );
|
---|
[a08ba92] | 150 | }
|
---|
[51b73452] | 151 |
|
---|
[a08ba92] | 152 | void Mangler::visit( EnumInstType *aggregateUseType ) {
|
---|
[0dd3a2f] | 153 | mangleRef( aggregateUseType, "e" );
|
---|
[a08ba92] | 154 | }
|
---|
[51b73452] | 155 |
|
---|
[a08ba92] | 156 | void Mangler::visit( TypeInstType *typeInst ) {
|
---|
[0dd3a2f] | 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
|
---|
[a08ba92] | 178 | }
|
---|
[51b73452] | 179 |
|
---|
[a08ba92] | 180 | void Mangler::visit( TupleType *tupleType ) {
|
---|
[0dd3a2f] | 181 | printQualifiers( tupleType );
|
---|
| 182 | mangleName << "T";
|
---|
| 183 | acceptAll( tupleType->get_types(), *this );
|
---|
| 184 | mangleName << "_";
|
---|
[a08ba92] | 185 | }
|
---|
[51b73452] | 186 |
|
---|
[a08ba92] | 187 | void Mangler::visit( TypeDecl *decl ) {
|
---|
[0dd3a2f] | 188 | static const char *typePrefix[] = { "BT", "BD", "BF" };
|
---|
| 189 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name();
|
---|
[a08ba92] | 190 | }
|
---|
[51b73452] | 191 |
|
---|
[a08ba92] | 192 | void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) {
|
---|
[0dd3a2f] | 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
|
---|
[a08ba92] | 196 | }
|
---|
[51b73452] | 197 |
|
---|
[a08ba92] | 198 | void Mangler::printQualifiers( Type *type ) {
|
---|
[0dd3a2f] | 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
|
---|
[a08ba92] | 244 | }
|
---|
[0dd3a2f] | 245 | } // namespace SymTab
|
---|
| 246 |
|
---|
| 247 | // Local Variables: //
|
---|
| 248 | // tab-width: 4 //
|
---|
| 249 | // mode: c++ //
|
---|
| 250 | // compile-command: "make install" //
|
---|
| 251 | // End: //
|
---|