source: src/SymTab/Mangler.cc @ 9236060

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9236060 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 9.9 KB
Line 
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 : Andrew Beach
12// Last Modified On : Wed Jun 28 15:31:00 2017
13// Update Count     : 21
14//
15#include "Mangler.h"
16
17#include <algorithm>                // for copy, transform
18#include <cassert>                  // for assert, assertf
19#include <functional>               // for const_mem_fun_t, mem_fun
20#include <iterator>                 // for ostream_iterator, back_insert_ite...
21#include <list>                     // for _List_iterator, list, _List_const...
22#include <string>                   // for string, operator<<, basic_string
23
24#include "CodeGen/OperatorTable.h"  // for OperatorInfo, operatorLookup
25#include "Common/utility.h"         // for toString
26#include "Parser/LinkageSpec.h"     // for Spec, isOverridable, AutoGen, Int...
27#include "SynTree/Declaration.h"    // for TypeDecl, DeclarationWithType
28#include "SynTree/Expression.h"     // for TypeExpr, Expression, operator<<
29#include "SynTree/Type.h"           // for Type, ReferenceToType, Type::Fora...
30
31namespace SymTab {
32        std::string Mangler::mangleType( Type *ty ) {
33                Mangler mangler( false, true );
34                maybeAccept( ty, mangler );
35                return mangler.get_mangleName();
36        }
37
38        Mangler::Mangler( bool mangleOverridable, bool typeMode )
39                : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {}
40
41        Mangler::Mangler( const Mangler &rhs ) : mangleName() {
42                varNums = rhs.varNums;
43                nextVarNum = rhs.nextVarNum;
44                isTopLevel = rhs.isTopLevel;
45                mangleOverridable = rhs.mangleOverridable;
46                typeMode = rhs.typeMode;
47        }
48
49        void Mangler::mangleDecl( DeclarationWithType *declaration ) {
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 );
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 && "unknown overrideable linkage" );
75                        } // if
76                }
77                isTopLevel = wasTopLevel;
78        }
79
80        void Mangler::visit( ObjectDecl *declaration ) {
81                mangleDecl( declaration );
82        }
83
84        void Mangler::visit( FunctionDecl *declaration ) {
85                mangleDecl( declaration );
86        }
87
88        void Mangler::visit( VoidType *voidType ) {
89                printQualifiers( voidType );
90                mangleName << "v";
91        }
92
93        void Mangler::visit( BasicType *basicType ) {
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                };
117
118                printQualifiers( basicType );
119                mangleName << btLetter[ basicType->get_kind() ];
120        }
121
122        void Mangler::visit( PointerType *pointerType ) {
123                printQualifiers( pointerType );
124                mangleName << "P";
125                maybeAccept( pointerType->get_base(), *this );
126        }
127
128        void Mangler::visit( ArrayType *arrayType ) {
129                // TODO: encode dimension
130                printQualifiers( arrayType );
131                mangleName << "A0";
132                maybeAccept( arrayType->get_base(), *this );
133        }
134
135        void Mangler::visit( ReferenceType *refType ) {
136                printQualifiers( refType );
137                mangleName << "R";
138                maybeAccept( refType->get_base(), *this );
139        }
140
141        namespace {
142                inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) {
143                        std::list< Type* > ret;
144                        std::transform( decls.begin(), decls.end(), std::back_inserter( ret ),
145                                                        std::mem_fun( &DeclarationWithType::get_type ) );
146                        return ret;
147                }
148        }
149
150        void Mangler::visit( FunctionType *functionType ) {
151                printQualifiers( functionType );
152                mangleName << "F";
153                std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() );
154                acceptAll( returnTypes, *this );
155                mangleName << "_";
156                std::list< Type* > paramTypes = getTypes( functionType->get_parameters() );
157                acceptAll( paramTypes, *this );
158                mangleName << "_";
159        }
160
161        void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) {
162                printQualifiers( refType );
163
164                mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
165        }
166
167        void Mangler::mangleGenericRef( ReferenceToType *refType, std::string prefix ) {
168                printQualifiers( refType );
169
170                std::ostringstream oldName( mangleName.str() );
171                mangleName.clear();
172
173                mangleName << prefix << refType->get_name();
174
175                std::list< Expression* >& params = refType->get_parameters();
176                if ( ! params.empty() ) {
177                        mangleName << "_";
178                        for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) {
179                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
180                                assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str());
181                                maybeAccept( paramType->get_type(), *this );
182                        }
183                        mangleName << "_";
184                }
185
186                oldName << mangleName.str().length() << mangleName.str();
187                mangleName.str( oldName.str() );
188        }
189
190        void Mangler::visit( StructInstType *aggregateUseType ) {
191                if ( typeMode ) mangleGenericRef( aggregateUseType, "s" );
192                else mangleRef( aggregateUseType, "s" );
193        }
194
195        void Mangler::visit( UnionInstType *aggregateUseType ) {
196                if ( typeMode ) mangleGenericRef( aggregateUseType, "u" );
197                else mangleRef( aggregateUseType, "u" );
198        }
199
200        void Mangler::visit( EnumInstType *aggregateUseType ) {
201                mangleRef( aggregateUseType, "e" );
202        }
203
204        void Mangler::visit( TypeInstType *typeInst ) {
205                VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
206                if ( varNum == varNums.end() ) {
207                        mangleRef( typeInst, "t" );
208                } else {
209                        printQualifiers( typeInst );
210                        std::ostringstream numStream;
211                        numStream << varNum->second.first;
212                        switch ( (TypeDecl::Kind )varNum->second.second ) {
213                          case TypeDecl::Any:
214                                mangleName << "t";
215                                break;
216                          case TypeDecl::Dtype:
217                                mangleName << "d";
218                                break;
219                          case TypeDecl::Ftype:
220                                mangleName << "f";
221                                break;
222                                case TypeDecl::Ttype:
223                                mangleName << "tVARGS";
224                                break;
225                                default:
226                                assert( false );
227                        } // switch
228                        mangleName << numStream.str();
229                } // if
230        }
231
232        void Mangler::visit( TupleType *tupleType ) {
233                printQualifiers( tupleType );
234                mangleName << "T";
235                acceptAll( tupleType->get_types(), *this );
236                mangleName << "_";
237        }
238
239        void Mangler::visit( VarArgsType *varArgsType ) {
240                printQualifiers( varArgsType );
241                mangleName << "VARGS";
242        }
243
244        void Mangler::visit( __attribute__((unused)) ZeroType *zeroType ) {
245                mangleName << "Z";
246        }
247
248        void Mangler::visit( __attribute__((unused)) OneType *oneType ) {
249                mangleName << "O";
250        }
251
252        void Mangler::visit( TypeDecl *decl ) {
253                static const char *typePrefix[] = { "BT", "BD", "BF" };
254                mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name();
255        }
256
257        void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) {
258                for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) {
259                        os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl;
260                } // for
261        }
262
263        void Mangler::printQualifiers( Type *type ) {
264                // skip if not including qualifiers
265                if ( typeMode ) return;
266
267                if ( ! type->get_forall().empty() ) {
268                        std::list< std::string > assertionNames;
269                        int tcount = 0, dcount = 0, fcount = 0, vcount = 0;
270                        mangleName << "A";
271                        for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
272                                switch ( (*i)->get_kind() ) {
273                                  case TypeDecl::Any:
274                                        tcount++;
275                                        break;
276                                  case TypeDecl::Dtype:
277                                        dcount++;
278                                        break;
279                                  case TypeDecl::Ftype:
280                                        fcount++;
281                                        break;
282                                  case TypeDecl::Ttype:
283                                        vcount++;
284                                        break;
285                                  default:
286                                        assert( false );
287                                } // switch
288                                varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() );
289                                for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
290                                        Mangler sub_mangler( mangleOverridable, typeMode );
291                                        sub_mangler.nextVarNum = nextVarNum;
292                                        sub_mangler.isTopLevel = false;
293                                        sub_mangler.varNums = varNums;
294                                        (*assert)->accept( sub_mangler );
295                                        assertionNames.push_back( sub_mangler.mangleName.str() );
296                                } // for
297                        } // for
298                        mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_";
299                        std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
300                        mangleName << "_";
301                } // if
302                if ( type->get_const() ) {
303                        mangleName << "C";
304                } // if
305                if ( type->get_volatile() ) {
306                        mangleName << "V";
307                } // if
308                // Removed due to restrict not affecting function compatibility in GCC
309//              if ( type->get_isRestrict() ) {
310//                      mangleName << "E";
311//              } // if
312                if ( type->get_lvalue() ) {
313                        mangleName << "L";
314                } // if
315                if ( type->get_atomic() ) {
316                        mangleName << "A";
317                } // if
318        }
319} // namespace SymTab
320
321// Local Variables: //
322// tab-width: 4 //
323// mode: c++ //
324// compile-command: "make install" //
325// End: //
Note: See TracBrowser for help on using the repository browser.