source: translator/SymTab/Mangler.cc @ 42dcae7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 42dcae7 was ea3eb06, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

formatting changes

  • Property mode set to 100644
File size: 6.5 KB
Line 
1#include <cassert>
2#include <string>
3#include <algorithm>
4#include <iterator>
5#include <functional>
6#include <set>
7
8#include "SynTree/Declaration.h"
9#include "SynTree/Type.h"
10#include "SynTree/Expression.h"
11#include "SynTree/Initializer.h"
12#include "SynTree/Statement.h"
13#include "Mangler.h"
14#include "CodeGen/OperatorTable.h"
15
16namespace SymTab {
17    Mangler::Mangler() : nextVarNum( 0 ), isTopLevel( true )
18    {}
19
20//Mangler::Mangler( const Mangler & )
21//  : mangleName(), varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( isTopLevel )
22//{
23//}
24    Mangler::Mangler( const Mangler &rhs ) : mangleName() {
25        varNums = rhs.varNums;
26        nextVarNum = rhs.nextVarNum;
27        isTopLevel = rhs.isTopLevel;
28    }
29
30    void Mangler::mangleDecl( DeclarationWithType *declaration ) {
31        bool wasTopLevel = isTopLevel;
32        if ( isTopLevel ) {
33            varNums.clear();
34            nextVarNum = 0;
35            isTopLevel = false;
36        }
37        mangleName << "__";
38        CodeGen::OperatorInfo opInfo;
39        if ( operatorLookup( declaration->get_name(), opInfo ) ) {
40            mangleName << opInfo.outputName;
41        } else {
42            mangleName << declaration->get_name();
43        }
44        mangleName << "__";
45        maybeAccept( declaration->get_type(), *this );
46        isTopLevel = wasTopLevel;
47    }
48
49    void Mangler::visit( ObjectDecl *declaration ) {
50        mangleDecl( declaration );
51    }
52
53    void Mangler::visit( FunctionDecl *declaration ) {
54        mangleDecl( declaration );
55    }
56
57    void Mangler::visit( VoidType *voidType ) {
58        printQualifiers( voidType );
59        mangleName << "v";
60    }
61
62    void Mangler::visit( BasicType *basicType ) {
63        static const char *btLetter[] = {
64            "b",        // Bool
65            "c",        // Char
66            "Sc",       // SignedChar
67            "Uc",       // UnsignedChar
68            "s",        // ShortSignedInt
69            "Us",       // ShortUnsignedInt
70            "i",        // SignedInt
71            "Ui",       // UnsignedInt
72            "l",        // LongSignedInt
73            "Ul",       // LongUnsignedInt
74            "q",        // LongLongSignedInt
75            "Uq",       // LongLongUnsignedInt
76            "f",        // Float
77            "d",        // Double
78            "r",        // LongDouble
79            "Xf",       // FloatComplex
80            "Xd",       // DoubleComplex
81            "Xr",       // LongDoubleComplex
82            "If",       // FloatImaginary
83            "Id",       // DoubleImaginary
84            "Ir",       // LongDoubleImaginary
85        };
86 
87        printQualifiers( basicType );
88        mangleName << btLetter[ basicType->get_kind() ];
89    }
90
91    void Mangler::visit( PointerType *pointerType ) {
92        printQualifiers( pointerType );
93        mangleName << "P";
94        maybeAccept( pointerType->get_base(), *this );
95    }
96
97    void Mangler::visit( ArrayType *arrayType ) {
98        // TODO: encode dimension
99        printQualifiers( arrayType );
100        mangleName << "A0";
101        maybeAccept( arrayType->get_base(), *this );
102    }
103
104    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        }
113    }
114
115    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 << "_";
124    }
125
126    void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) {
127        printQualifiers( refType );
128        mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
129    }
130
131    void Mangler::visit( StructInstType *aggregateUseType ) {
132        mangleRef( aggregateUseType, "s" );
133    }
134
135    void Mangler::visit( UnionInstType *aggregateUseType ) {
136        mangleRef( aggregateUseType, "u" );
137    }
138
139    void Mangler::visit( EnumInstType *aggregateUseType ) {
140        mangleRef( aggregateUseType, "e" );
141    }
142
143    void Mangler::visit( TypeInstType *typeInst ) {
144        VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
145        if ( varNum == varNums.end() ) {
146            mangleRef( typeInst, "t" );
147        } else {
148            printQualifiers( typeInst );
149            std::ostrstream numStream;
150            numStream << varNum->second.first;
151            mangleName << (numStream.pcount() + 1);
152            switch ( (TypeDecl::Kind )varNum->second.second ) {
153              case TypeDecl::Any:
154                mangleName << "t";
155                break;
156              case TypeDecl::Dtype:
157                mangleName << "d";
158                break;
159              case TypeDecl::Ftype:
160                mangleName << "f";
161                break;
162            }
163            mangleName << std::string( numStream.str(), numStream.pcount() );
164        }
165    }
166
167    void Mangler::visit( TupleType *tupleType ) {
168        printQualifiers( tupleType );
169        mangleName << "T";
170        acceptAll( tupleType->get_types(), *this );
171        mangleName << "_";
172    }
173
174    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();
177    }
178
179    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            os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl;
182        }
183    }
184
185    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    }
229} // SymTab
Note: See TracBrowser for help on using the repository browser.