source: src/CodeGen/GenType.cc @ 8f16086

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 8f16086 was 5f642e38, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

more work on codegen output

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[51587aa]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//
[486341f]7// GenType.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[1db21619]11// Last Modified By : Peter A. Buhr
[615a096]12// Last Modified On : Fri Mar 17 09:02:28 2017
13// Update Count     : 22
[51587aa]14//
15
[5f2f2d7]16#include <sstream>
[51b7345]17#include <cassert>
18
19#include "GenType.h"
[a61fea9a]20#include "CodeGenerator.h"
[e8032b0]21
22#include "SynTree/Declaration.h"
[51b7345]23#include "SynTree/Expression.h"
[e8032b0]24#include "SynTree/Type.h"
25#include "SynTree/Visitor.h"
[51b7345]26
27namespace CodeGen {
[51587aa]28        class GenType : public Visitor {
29          public:
[e39241b]30                GenType( const std::string &typeString, bool pretty = false, bool genC = false );
[51587aa]31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
[486341f]33
[51587aa]34                virtual void visit( FunctionType *funcType );
35                virtual void visit( VoidType *voidType );
36                virtual void visit( BasicType *basicType );
37                virtual void visit( PointerType *pointerType );
38                virtual void visit( ArrayType *arrayType );
39                virtual void visit( StructInstType *structInst );
40                virtual void visit( UnionInstType *unionInst );
41                virtual void visit( EnumInstType *enumInst );
42                virtual void visit( TypeInstType *typeInst );
[6d4d1a6]43                virtual void visit( TupleType * tupleType );
[44b7088]44                virtual void visit( VarArgsType *varArgsType );
[89e6ffc]45                virtual void visit( ZeroType *zeroType );
46                virtual void visit( OneType *oneType );
[486341f]47
[51587aa]48          private:
49                void handleQualifiers( Type *type );
[e39241b]50                std::string handleGeneric( ReferenceToType * refType );
[51587aa]51                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
[486341f]52
[51587aa]53                std::string typeString;
[35b1bf4]54                bool pretty = false; // pretty print
[e39241b]55                bool genC = false;   // generating C code?
[51587aa]56        };
57
[e39241b]58        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC ) {
59                GenType gt( baseString, pretty, genC );
[c0aa336]60                std::ostringstream os;
[35b1bf4]61
[c0aa336]62                if ( ! type->get_attributes().empty() ) {
[e39241b]63                        CodeGenerator cg( os, pretty, genC );
[c0aa336]64                        cg.genAttributes( type->get_attributes() );
65                } // if
66
[51587aa]67                type->accept( gt );
[c0aa336]68                return os.str() + gt.get_typeString();
[51587aa]69        }
70
[35b1bf4]71  std::string genPrettyType( Type * type, const std::string & baseString ) {
[e39241b]72        return genType( type, baseString, true, false );
[35b1bf4]73  }
74
[e39241b]75        GenType::GenType( const std::string &typeString, bool pretty, bool genC ) : typeString( typeString ), pretty( pretty ), genC( genC ) {}
[51587aa]76
77        void GenType::visit( VoidType *voidType ) {
78                typeString = "void " + typeString;
79                handleQualifiers( voidType );
80        }
81
82        void GenType::visit( BasicType *basicType ) {
83                BasicType::Kind kind = basicType->get_kind();
84                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
85                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
86                handleQualifiers( basicType );
87        }
88
89        void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
[5f2f2d7]90                std::ostringstream os;
[51587aa]91                if ( typeString != "" ) {
92                        if ( typeString[ 0 ] == '*' ) {
93                                os << "(" << typeString << ")";
94                        } else {
95                                os << typeString;
96                        } // if
97                } // if
98                os << "[";
99
100                if ( isStatic ) {
101                        os << "static ";
102                } // if
[615a096]103                if ( qualifiers.is_const ) {
[51587aa]104                        os << "const ";
105                } // if
[615a096]106                if ( qualifiers.is_volatile ) {
[51587aa]107                        os << "volatile ";
108                } // if
[615a096]109                if ( qualifiers.is_restrict ) {
[51587aa]110                        os << "__restrict ";
111                } // if
[615a096]112                if ( qualifiers.is_atomic ) {
[51587aa]113                        os << "_Atomic ";
114                } // if
115                if ( dimension != 0 ) {
[e39241b]116                        CodeGenerator cg( os, pretty, genC );
[51587aa]117                        dimension->accept( cg );
[71bd8c6]118                } else if ( isVarLen ) {
[1db21619]119                        // no dimension expression on a VLA means it came in with the * token
[71bd8c6]120                        os << "*";
[51587aa]121                } // if
122                os << "]";
123
[5f2f2d7]124                typeString = os.str();
[486341f]125
[51587aa]126                base->accept( *this );
127        }
128
129        void GenType::visit( PointerType *pointerType ) {
130                assert( pointerType->get_base() != 0);
131                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
132                        genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
133                } else {
134                        handleQualifiers( pointerType );
135                        if ( typeString[ 0 ] == '?' ) {
136                                typeString = "* " + typeString;
137                        } else {
138                                typeString = "*" + typeString;
139                        } // if
140                        pointerType->get_base()->accept( *this );
141                } // if
142        }
143
[a08ba92]144        void GenType::visit( ArrayType *arrayType ) {
[51587aa]145                genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
146        }
147
148        void GenType::visit( FunctionType *funcType ) {
[5f2f2d7]149                std::ostringstream os;
[51587aa]150
151                if ( typeString != "" ) {
152                        if ( typeString[ 0 ] == '*' ) {
153                                os << "(" << typeString << ")";
154                        } else {
155                                os << typeString;
156                        } // if
157                } // if
[486341f]158
[51587aa]159                /************* parameters ***************/
160
161                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
162
163                if ( pars.empty() ) {
164                        if ( funcType->get_isVarArgs() ) {
165                                os << "()";
166                        } else {
167                                os << "(void)";
168                        } // if
169                } else {
[e39241b]170                        CodeGenerator cg( os, pretty, genC );
[51587aa]171                        os << "(" ;
172
173                        cg.genCommaList( pars.begin(), pars.end() );
174
[a08ba92]175                        if ( funcType->get_isVarArgs() ) {
[51587aa]176                                os << ", ...";
177                        } // if
178                        os << ")";
179                } // if
[486341f]180
[5f2f2d7]181                typeString = os.str();
[51587aa]182
183                if ( funcType->get_returnVals().size() == 0 ) {
184                        typeString = "void " + typeString;
185                } else {
186                        funcType->get_returnVals().front()->get_type()->accept( *this );
187                } // if
[5f642e38]188
189                // add forall
190                if( ! funcType->get_forall().empty() && ! genC ) {
191                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
192                        std::ostringstream os;
193                        CodeGenerator cg( os, pretty, genC );
194                        os << "forall(";
195                        cg.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
196                        os << ")" << std::endl;
197                        typeString = os.str() + typeString;
198                }
[51587aa]199        }
200
[e39241b]201        std::string GenType::handleGeneric( ReferenceToType * refType ) {
202                if ( ! refType->get_parameters().empty() ) {
203                        std::ostringstream os;
204                        CodeGenerator cg( os, pretty, genC );
205                        os << "(";
206                        cg.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );
207                        os << ") ";
208                        return os.str();
209                }
210                return "";
211        }
212
[51587aa]213        void GenType::visit( StructInstType *structInst )  {
[e39241b]214                typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
215                if ( genC ) typeString = "struct " + typeString;
[51587aa]216                handleQualifiers( structInst );
217        }
218
219        void GenType::visit( UnionInstType *unionInst ) {
[e39241b]220                typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
221                if ( genC ) typeString = "union " + typeString;
[51587aa]222                handleQualifiers( unionInst );
223        }
224
225        void GenType::visit( EnumInstType *enumInst ) {
[e39241b]226                typeString = enumInst->get_name() + " " + typeString;
227                if ( genC ) typeString = "enum " + typeString;
[51587aa]228                handleQualifiers( enumInst );
229        }
230
231        void GenType::visit( TypeInstType *typeInst ) {
232                typeString = typeInst->get_name() + " " + typeString;
233                handleQualifiers( typeInst );
234        }
235
[6d4d1a6]236        void GenType::visit( TupleType * tupleType ) {
[e39241b]237                assertf( ! genC, "Tuple types should not reach code generation." );
[6d4d1a6]238                Visitor::visit( tupleType );
[35b1bf4]239                unsigned int i = 0;
240                std::ostringstream os;
241                os << "[";
242                for ( Type * t : *tupleType ) {
243                        i++;
[e39241b]244                        os << genType( t, "", pretty, genC ) << (i == tupleType->size() ? "" : ", ");
[35b1bf4]245                }
246                os << "]";
247                typeString = os.str() + typeString;
[6d4d1a6]248        }
249
[44b7088]250        void GenType::visit( VarArgsType *varArgsType ) {
251                typeString = "__builtin_va_list " + typeString;
[540ddb7d]252                handleQualifiers( varArgsType );
[44b7088]253        }
254
[89e6ffc]255        void GenType::visit( ZeroType *zeroType ) {
256                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
[35b1bf4]257                typeString = (pretty ? "zero_t " : "long int ") + typeString;
[89e6ffc]258                handleQualifiers( zeroType );
259        }
260
261        void GenType::visit( OneType *oneType ) {
262                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
[35b1bf4]263                typeString = (pretty ? "one_t " : "long int ") + typeString;
[89e6ffc]264                handleQualifiers( oneType );
265        }
266
[51587aa]267        void GenType::handleQualifiers( Type *type ) {
[615a096]268                if ( type->get_const() ) {
[51587aa]269                        typeString = "const " + typeString;
270                } // if
[615a096]271                if ( type->get_volatile() ) {
[51587aa]272                        typeString = "volatile " + typeString;
273                } // if
[615a096]274                if ( type->get_restrict() ) {
[51587aa]275                        typeString = "__restrict " + typeString;
276                } // if
[615a096]277                if ( type->get_atomic() ) {
[51587aa]278                        typeString = "_Atomic " + typeString;
279                } // if
280        }
[51b7345]281} // namespace CodeGen
[51587aa]282
283// Local Variables: //
284// tab-width: 4 //
285// mode: c++ //
286// compile-command: "make install" //
287// End: //
Note: See TracBrowser for help on using the repository browser.