source: src/CodeGen/CodeGenerator.cc @ 907eccb

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 907eccb was 907eccb, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

added UntypedTupleExpr? to better differentiate typed and untyped contexts, simplifying some code

  • Property mode set to 100644
File size: 27.6 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//
[71f4e4f]7// CodeGenerator.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[8688ce1]11// Last Modified By : Peter A. Buhr
[58dd019]12// Last Modified On : Tue Dec 13 14:51:27 2016
13// Update Count     : 362
[51587aa]14//
15
[51b7345]16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
[68cd1ce]21#include "Parser/ParseNode.h"
22
[e8032b0]23#include "SynTree/Declaration.h"
[51b7345]24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
[68cd1ce]26#include "SynTree/Statement.h"
[e8032b0]27#include "SynTree/Type.h"
[7baed7d]28#include "SynTree/Attribute.h"
[51b7345]29
[d3b7937]30#include "Common/utility.h"
31#include "Common/UnimplementedError.h"
[51b7345]32
[a61fea9a]33#include "CodeGenerator.h"
[51b7345]34#include "OperatorTable.h"
35#include "GenType.h"
36
[10a7775]37#include "InitTweak/InitTweak.h"
38
[51b7345]39using namespace std;
40
41namespace CodeGen {
[6c4ff37]42        int CodeGenerator::tabsize = 4;
[51587aa]43
[145f1fc]44        // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]45        bool wantSpacing( Statement * stmt) {
46                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
[08061589]47                        dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
[2b6c1e0]48        }
49
[8688ce1]50        void CodeGenerator::extension( Expression * expr ) {
[8e9cbb2]51                if ( expr->get_extension() ) {
52                        output << "__extension__ ";
53                } // if
54        } // extension
55
[8688ce1]56        void CodeGenerator::extension( Declaration * decl ) {
[8e9cbb2]57                if ( decl->get_extension() ) {
58                        output << "__extension__ ";
59                } // if
60        } // extension
61
[58dd019]62        void CodeGenerator::asmName( DeclarationWithType * decl ) {
63                if ( ConstantExpr * asmName = decl->get_asmName() ) {
64                        output << " asm ( " << asmName->get_constant()->get_value() << " )";
65                } // if
66        } // extension
67
[888cbe4]68        ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
[cda48b6]69          return output << string( cg.cur_indent, ' ' );
70        }
71
[888cbe4]72        ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
[cda48b6]73                return indent( output );
74        }
[51587aa]75
[888cbe4]76        CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
77                labels = &l;
78                return *this;
79        }
80
[8688ce1]81        ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
[888cbe4]82                std::list< Label > & labs = *printLabels.labels;
83                // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
84                for ( Label & l : labs ) {
85                        output << l.get_name() + ": ";
86                        printLabels.cg.genAttributes( l.get_attributes() );
[8688ce1]87                } // for
[888cbe4]88                return output;
89        }
90
[486341f]91        CodeGenerator::CodeGenerator( std::ostream & os, bool mangle ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), mangle( mangle ) {}
[cda48b6]92
[8688ce1]93        CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )
[888cbe4]94                        : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
[6c4ff37]95                //output << std::string( init );
[51587aa]96        }
97
[8688ce1]98        CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp )
[888cbe4]99                        : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
[6c4ff37]100                //output << std::string( init );
[51587aa]101        }
102
[486341f]103        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
104                if ( ! mangle ) return decl->get_name();
[51587aa]105                if ( decl->get_mangleName() != "" ) {
[f326f99]106                        // need to incorporate scope level in order to differentiate names for destructors
107                        return decl->get_scopedMangleName();
[51587aa]108                } else {
109                        return decl->get_name();
110                } // if
111        }
[94b4364]112
[7baed7d]113        void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) {
114                if ( ! attributes.empty() ) {
115                        output << "__attribute__ ((";
116                        for ( Attribute *& attr : attributes ) {
117                                if ( ! attr->empty() ) {
118                                        output << attr->get_name() << "(";
119                                        genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
120                                        output << ")";
[8688ce1]121                                } // if
[7baed7d]122                                output << ",";
[8688ce1]123                        } // for
[7baed7d]124                        output << ")) ";
[8688ce1]125                } // if
[7baed7d]126        }
127
128
129        //*** Declarations
[8688ce1]130        void CodeGenerator::visit( FunctionDecl * functionDecl ) {
[8e9cbb2]131                extension( functionDecl );
[7baed7d]132                genAttributes( functionDecl->get_attributes() );
133
[51587aa]134                handleStorageClass( functionDecl );
[f38c8d9]135                if ( functionDecl->get_isInline() ) {
[6c4ff37]136                        output << "inline ";
[f38c8d9]137                } // if
[de62360d]138                if ( functionDecl->get_isNoreturn() ) {
139                        output << "_Noreturn ";
140                } // if
[6c4ff37]141                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
[51587aa]142
143                // how to get this to the Functype?
144                std::list< Declaration * > olds = functionDecl->get_oldDecls();
145                if ( ! olds.empty() ) {
[6c4ff37]146                        output << " /* function has old declaration */";
[51587aa]147                } // if
148
[58dd019]149                asmName( functionDecl );
150
[51587aa]151                // acceptAll( functionDecl->get_oldDecls(), *this );
152                if ( functionDecl->get_statements() ) {
[7f5566b]153                        functionDecl->get_statements()->accept( *this );
[51587aa]154                } // if
155        }
156
[8688ce1]157        void CodeGenerator::visit( ObjectDecl * objectDecl ) {
[8e9cbb2]158                extension( objectDecl );
[f9cebb5]159                genAttributes( objectDecl->get_attributes() );
160
[51587aa]161                handleStorageClass( objectDecl );
[6c4ff37]162                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
[71f4e4f]163
[58dd019]164                asmName( objectDecl );
165
[51587aa]166                if ( objectDecl->get_init() ) {
[6c4ff37]167                        output << " = ";
[51587aa]168                        objectDecl->get_init()->accept( *this );
169                } // if
[3778cb2]170
[51587aa]171                if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]172                        output << ":";
[51587aa]173                        objectDecl->get_bitfieldWidth()->accept( *this );
174                } // if
175        }
176
[8688ce1]177        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
[51587aa]178                if ( aggDecl->get_name() != "" )
[6c4ff37]179                        output << aggDecl->get_name();
[71f4e4f]180
[2c57025]181                // std::list< Declaration * > & memb = aggDecl->get_members();
182                // if ( ! memb.empty() ) {
183                if ( aggDecl->has_body() ) {
184                        std::list< Declaration * > & memb = aggDecl->get_members();
[94b4364]185                        output << " {" << endl;
[51587aa]186
[71f4e4f]187                        cur_indent += CodeGenerator::tabsize;
[3778cb2]188                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
[71f4e4f]189                                output << indent;
[7f5566b]190                                (*i)->accept( *this );
[6c4ff37]191                                output << ";" << endl;
[3778cb2]192                        } // for
[51587aa]193
[71f4e4f]194                        cur_indent -= CodeGenerator::tabsize;
[51587aa]195
[cda48b6]196                        output << indent << "}";
[17cd4eb]197                } // if
[51587aa]198        }
[17cd4eb]199
[8688ce1]200        void CodeGenerator::visit( StructDecl * structDecl ) {
[8e9cbb2]201                extension( structDecl );
[6c4ff37]202                output << "struct ";
[51587aa]203                handleAggregate( structDecl );
204        }
[17cd4eb]205
[8688ce1]206        void CodeGenerator::visit( UnionDecl * unionDecl ) {
[8e9cbb2]207                extension( unionDecl );
[6c4ff37]208                output << "union ";
[8e9cbb2]209                handleAggregate( unionDecl );
[51587aa]210        }
[71f4e4f]211
[8688ce1]212        void CodeGenerator::visit( EnumDecl * enumDecl ) {
[8e9cbb2]213                extension( enumDecl );
[6c4ff37]214                output << "enum ";
[51587aa]215
[8e9cbb2]216                if ( enumDecl->get_name() != "" )
217                        output << enumDecl->get_name();
[71f4e4f]218
[8e9cbb2]219                std::list< Declaration* > &memb = enumDecl->get_members();
[51587aa]220
221                if ( ! memb.empty() ) {
[cda48b6]222                        output << " {" << endl;
[51587aa]223
[71f4e4f]224                        cur_indent += CodeGenerator::tabsize;
[51587aa]225                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
[8688ce1]226                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
[51587aa]227                                assert( obj );
[71f4e4f]228                                output << indent << mangleName( obj );
[51587aa]229                                if ( obj->get_init() ) {
[6c4ff37]230                                        output << " = ";
[7f5566b]231                                        obj->get_init()->accept( *this );
[51587aa]232                                } // if
[6c4ff37]233                                output << "," << endl;
[51587aa]234                        } // for
235
[71f4e4f]236                        cur_indent -= CodeGenerator::tabsize;
[51587aa]237
[cda48b6]238                        output << indent << "}";
[51587aa]239                } // if
240        }
[71f4e4f]241
[8688ce1]242        void CodeGenerator::visit( TraitDecl * traitDecl ) {}
[71f4e4f]243
[8688ce1]244        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
[8e9cbb2]245                assert( false && "Typedefs are removed and substituted in earlier passes." );
246                //output << "typedef ";
247                //output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]248        }
[71f4e4f]249
[8688ce1]250        void CodeGenerator::visit( TypeDecl * typeDecl ) {
[51587aa]251                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
252                // still to be done
[8e9cbb2]253                extension( typeDecl );
[6c4ff37]254                output << "extern unsigned long " << typeDecl->get_name();
[51587aa]255                if ( typeDecl->get_base() ) {
[6c4ff37]256                        output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
[51587aa]257                } // if
258        }
259
[e45215c]260        void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
261                typedef std::list< Expression * > DesignatorList;
262                if ( designators.size() == 0 ) return;
263                for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
[8a4da06]264                        if ( dynamic_cast< NameExpr * >( *iter ) ) {
[f32c7f4]265                                // if expression is a name, then initializing aggregate member
266                                output << ".";
267                                (*iter)->accept( *this );
268                        } else {
269                                // if not a simple name, it has to be a constant expression, i.e. an array designator
[e45215c]270                                output << "[";
271                                (*iter)->accept( *this );
272                                output << "]";
[3778cb2]273                        } // if
274                } // for
[e45215c]275                output << " = ";
276        }
277
[8688ce1]278        void CodeGenerator::visit( SingleInit * init ) {
[e45215c]279                printDesignators( init->get_designators() );
[51587aa]280                init->get_value()->accept( *this );
281        }
282
[8688ce1]283        void CodeGenerator::visit( ListInit * init ) {
[e45215c]284                printDesignators( init->get_designators() );
[6c4ff37]285                output << "{ ";
[4d2434a]286                if ( init->begin() == init->end() ) {
[8e9cbb2]287                        // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
[5b40f30]288                        output << "0";
289                } else {
[4d2434a]290                        genCommaList( init->begin(), init->end() );
[8688ce1]291                } // if
[6c4ff37]292                output << " }";
[51587aa]293        }
294
[fc638d2]295        void CodeGenerator::visit( ConstructorInit * init ){
296                assertf( false, "ConstructorInit nodes should not make it to CodeGen." );
297        }
298
[8688ce1]299        void CodeGenerator::visit( Constant * constant ) {
[6c4ff37]300                output << constant->get_value() ;
[51587aa]301        }
302
303        //*** Expressions
[8688ce1]304        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
[e04ef3a]305                extension( applicationExpr );
[8688ce1]306                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
[51587aa]307                        OperatorInfo opInfo;
308                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
309                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
310                                switch ( opInfo.type ) {
311                                  case OT_PREFIXASSIGN:
312                                  case OT_POSTFIXASSIGN:
313                                  case OT_INFIXASSIGN:
[356189a]314                                  case OT_CTOR:
[c2ce2350]315                                  case OT_DTOR:
[51587aa]316                                        {
317                                                assert( arg != applicationExpr->get_args().end() );
[8688ce1]318                                                if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
[356189a]319                                                        // remove & from first assignment/ctor argument
[51587aa]320                                                        *arg = addrExpr->get_arg();
321                                                } else {
[356189a]322                                                        // no address-of operator, so must be a pointer - add dereference
[066d77a]323                                                        // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
324                                                        // Since its arguments are modified here, this assertion most commonly triggers when the application
325                                                        // is visited multiple times.
[8688ce1]326                                                        UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
[51587aa]327                                                        newExpr->get_args().push_back( *arg );
[906e24d]328                                                        Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
[066d77a]329                                                        assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
[906e24d]330                                                        newExpr->set_result( type->clone() );
[51587aa]331                                                        *arg = newExpr;
332                                                } // if
333                                                break;
334                                        }
[71f4e4f]335
[51587aa]336                                  default:
337                                        // do nothing
338                                        ;
[3778cb2]339                                } // switch
[71f4e4f]340
[51587aa]341                                switch ( opInfo.type ) {
342                                  case OT_INDEX:
343                                        assert( applicationExpr->get_args().size() == 2 );
344                                        (*arg++)->accept( *this );
[6c4ff37]345                                        output << "[";
[51587aa]346                                        (*arg)->accept( *this );
[6c4ff37]347                                        output << "]";
[51587aa]348                                        break;
[71f4e4f]349
[51587aa]350                                  case OT_CALL:
[356189a]351                                        // there are no intrinsic definitions of the function call operator
[51587aa]352                                        assert( false );
353                                        break;
[71f4e4f]354
[f1e012b]355                                  case OT_CTOR:
[c2ce2350]356                                  case OT_DTOR:
[356189a]357                                        if ( applicationExpr->get_args().size() == 1 ) {
[8e9cbb2]358                                                // the expression fed into a single parameter constructor or destructor may contain side
359                                                // effects, so must still output this expression
[64071c2]360                                                output << "(";
[356189a]361                                                (*arg++)->accept( *this );
[64071c2]362                                                output << ") /* " << opInfo.inputName << " */";
[356189a]363                                        } else if ( applicationExpr->get_args().size() == 2 ) {
[c2ce2350]364                                                // intrinsic two parameter constructors are essentially bitwise assignment
[356189a]365                                                output << "(";
366                                                (*arg++)->accept( *this );
367                                                output << opInfo.symbol;
368                                                (*arg)->accept( *this );
[c2ce2350]369                                                output << ") /* " << opInfo.inputName << " */";
[356189a]370                                        } else {
[c2ce2350]371                                                // no constructors with 0 or more than 2 parameters
[356189a]372                                                assert( false );
[8688ce1]373                                        } // if
[356189a]374                                        break;
[f1e012b]375
[51587aa]376                                  case OT_PREFIX:
377                                  case OT_PREFIXASSIGN:
378                                        assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]379                                        output << "(";
380                                        output << opInfo.symbol;
[51587aa]381                                        (*arg)->accept( *this );
[6c4ff37]382                                        output << ")";
[51587aa]383                                        break;
[71f4e4f]384
[51587aa]385                                  case OT_POSTFIX:
386                                  case OT_POSTFIXASSIGN:
387                                        assert( applicationExpr->get_args().size() == 1 );
388                                        (*arg)->accept( *this );
[6c4ff37]389                                        output << opInfo.symbol;
[51587aa]390                                        break;
391
[f1e012b]392
[51587aa]393                                  case OT_INFIX:
394                                  case OT_INFIXASSIGN:
395                                        assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]396                                        output << "(";
[51587aa]397                                        (*arg++)->accept( *this );
[6c4ff37]398                                        output << opInfo.symbol;
[51587aa]399                                        (*arg)->accept( *this );
[6c4ff37]400                                        output << ")";
[51587aa]401                                        break;
[71f4e4f]402
[51587aa]403                                  case OT_CONSTANT:
[721f17a]404                                  case OT_LABELADDRESS:
405                                        // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]406                                        assert( false );
[3778cb2]407                                } // switch
[17cd4eb]408                        } else {
[51587aa]409                                varExpr->accept( *this );
[6c4ff37]410                                output << "(";
[51587aa]411                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]412                                output << ")";
[17cd4eb]413                        } // if
[51587aa]414                } else {
415                        applicationExpr->get_function()->accept( *this );
[6c4ff37]416                        output << "(";
[51587aa]417                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]418                        output << ")";
[51587aa]419                } // if
420        }
[71f4e4f]421
[8688ce1]422        void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
[e04ef3a]423                extension( untypedExpr );
[8688ce1]424                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
[51587aa]425                        OperatorInfo opInfo;
426                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
427                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
428                                switch ( opInfo.type ) {
429                                  case OT_INDEX:
430                                        assert( untypedExpr->get_args().size() == 2 );
431                                        (*arg++)->accept( *this );
[6c4ff37]432                                        output << "[";
[51587aa]433                                        (*arg)->accept( *this );
[6c4ff37]434                                        output << "]";
[51587aa]435                                        break;
[71f4e4f]436
[51587aa]437                                  case OT_CALL:
[f1e012b]438                                        assert( false );
439
[c2ce2350]440                                  case OT_CTOR:
441                                  case OT_DTOR:
442                                        if ( untypedExpr->get_args().size() == 1 ) {
[8e9cbb2]443                                                // the expression fed into a single parameter constructor or destructor may contain side
444                                                // effects, so must still output this expression
[64071c2]445                                                output << "(";
[c2ce2350]446                                                (*arg++)->accept( *this );
[64071c2]447                                                output << ") /* " << opInfo.inputName << " */";
[c2ce2350]448                                        } else if ( untypedExpr->get_args().size() == 2 ) {
449                                                // intrinsic two parameter constructors are essentially bitwise assignment
450                                                output << "(";
451                                                (*arg++)->accept( *this );
452                                                output << opInfo.symbol;
453                                                (*arg)->accept( *this );
454                                                output << ") /* " << opInfo.inputName << " */";
455                                        } else {
456                                                // no constructors with 0 or more than 2 parameters
457                                                assert( false );
[3778cb2]458                                        } // if
[51587aa]459                                        break;
[71f4e4f]460
[51587aa]461                                  case OT_PREFIX:
462                                  case OT_PREFIXASSIGN:
[de62360d]463                                  case OT_LABELADDRESS:
[51587aa]464                                        assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]465                                        output << "(";
466                                        output << opInfo.symbol;
[51587aa]467                                        (*arg)->accept( *this );
[6c4ff37]468                                        output << ")";
[51587aa]469                                        break;
[71f4e4f]470
[51587aa]471                                  case OT_POSTFIX:
472                                  case OT_POSTFIXASSIGN:
473                                        assert( untypedExpr->get_args().size() == 1 );
474                                        (*arg)->accept( *this );
[6c4ff37]475                                        output << opInfo.symbol;
[51587aa]476                                        break;
[71f4e4f]477
[51587aa]478                                  case OT_INFIX:
479                                  case OT_INFIXASSIGN:
480                                        assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]481                                        output << "(";
[51587aa]482                                        (*arg++)->accept( *this );
[6c4ff37]483                                        output << opInfo.symbol;
[51587aa]484                                        (*arg)->accept( *this );
[6c4ff37]485                                        output << ")";
[51587aa]486                                        break;
[71f4e4f]487
[51587aa]488                                  case OT_CONSTANT:
489                                        // there are no intrinsic definitions of 0 or 1 as functions
490                                        assert( false );
[3778cb2]491                                } // switch
[51587aa]492                        } else {
[8688ce1]493                                if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
[ac911f4]494                                        assert( untypedExpr->get_args().size() == 2 );
495                                        (*untypedExpr->get_args().begin())->accept( *this );
496                                        output << " ... ";
497                                        (*--untypedExpr->get_args().end())->accept( *this );
[057b34f]498                                } else {                                                                // builtin routines
499                                        nameExpr->accept( *this );
500                                        output << "(";
501                                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
502                                        output << ")";
[66d12f7]503                                } // if
[51587aa]504                        } // if
505                } else {
506                        untypedExpr->get_function()->accept( *this );
[6c4ff37]507                        output << "(";
[51587aa]508                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]509                        output << ")";
[51587aa]510                } // if
511        }
[71f4e4f]512
[064e3ff]513        void CodeGenerator::visit( RangeExpr * rangeExpr ) {
514                rangeExpr->get_low()->accept( *this );
515                output << " ... ";
516                rangeExpr->get_high()->accept( *this );
517        }
518
[8688ce1]519        void CodeGenerator::visit( NameExpr * nameExpr ) {
[e04ef3a]520                extension( nameExpr );
[51587aa]521                OperatorInfo opInfo;
522                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
523                        assert( opInfo.type == OT_CONSTANT );
[6c4ff37]524                        output << opInfo.symbol;
[51587aa]525                } else {
[6c4ff37]526                        output << nameExpr->get_name();
[51587aa]527                } // if
528        }
[71f4e4f]529
[8688ce1]530        void CodeGenerator::visit( AddressExpr * addressExpr ) {
[e04ef3a]531                extension( addressExpr );
[6c4ff37]532                output << "(&";
[51587aa]533                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
[8688ce1]534                if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]535                        output << mangleName( variableExpr->get_var() );
[51587aa]536                } else {
537                        addressExpr->get_arg()->accept( *this );
538                } // if
[6c4ff37]539                output << ")";
[51587aa]540        }
541
[8688ce1]542        void CodeGenerator::visit( CastExpr * castExpr ) {
[e04ef3a]543                extension( castExpr );
[803deb1]544                output << "(";
[906e24d]545                if ( castExpr->get_result()->isVoid() ) {
[803deb1]546                        output << "(void)" ;
[906e24d]547                } else if ( ! castExpr->get_result()->get_isLvalue() ) {
[803deb1]548                        // at least one result type of cast, but not an lvalue
549                        output << "(";
[906e24d]550                        output << genType( castExpr->get_result(), "" );
[71f4e4f]551                        output << ")";
[803deb1]552                } else {
[8e9cbb2]553                        // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
[803deb1]554                        // never an lvalue in C
[3778cb2]555                } // if
[803deb1]556                castExpr->get_arg()->accept( *this );
557                output << ")";
[51587aa]558        }
[71f4e4f]559
[8688ce1]560        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
[51587aa]561                assert( false );
562        }
[71f4e4f]563
[8688ce1]564        void CodeGenerator::visit( MemberExpr * memberExpr ) {
[e04ef3a]565                extension( memberExpr );
[51587aa]566                memberExpr->get_aggregate()->accept( *this );
[6c4ff37]567                output << "." << mangleName( memberExpr->get_member() );
[51587aa]568        }
[71f4e4f]569
[8688ce1]570        void CodeGenerator::visit( VariableExpr * variableExpr ) {
[e04ef3a]571                extension( variableExpr );
[51587aa]572                OperatorInfo opInfo;
573                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]574                        output << opInfo.symbol;
[51587aa]575                } else {
[6c4ff37]576                        output << mangleName( variableExpr->get_var() );
[51587aa]577                } // if
578        }
[71f4e4f]579
[8688ce1]580        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
[51587aa]581                assert( constantExpr->get_constant() );
[e04ef3a]582                extension( constantExpr );
[51587aa]583                constantExpr->get_constant()->accept( *this );
584        }
[71f4e4f]585
[8688ce1]586        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
[e04ef3a]587                extension( sizeofExpr );
[6c4ff37]588                output << "sizeof(";
[51587aa]589                if ( sizeofExpr->get_isType() ) {
[6c4ff37]590                        output << genType( sizeofExpr->get_type(), "" );
[51587aa]591                } else {
592                        sizeofExpr->get_expr()->accept( *this );
593                } // if
[6c4ff37]594                output << ")";
[51587aa]595        }
[47534159]596
[8688ce1]597        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
[47534159]598                // use GCC extension to avoid bumping std to C11
[8e9cbb2]599                extension( alignofExpr );
[47534159]600                output << "__alignof__(";
[25a054f]601                if ( alignofExpr->get_isType() ) {
602                        output << genType( alignofExpr->get_type(), "" );
[47534159]603                } else {
[25a054f]604                        alignofExpr->get_expr()->accept( *this );
[47534159]605                } // if
606                output << ")";
607        }
[71f4e4f]608
[8688ce1]609        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
[8e9cbb2]610                assert( false && "UntypedOffsetofExpr should not reach code generation." );
[2a4b088]611        }
612
[8688ce1]613        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
[25a054f]614                // use GCC builtin
615                output << "__builtin_offsetof(";
616                output << genType( offsetofExpr->get_type(), "" );
[e551c69]617                output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]618                output << ")";
619        }
[d63eeb0]620
[8688ce1]621        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
[8e9cbb2]622                assert( false && "OffsetPackExpr should not reach code generation." );
[afc1045]623        }
[70a06f6]624
[8688ce1]625        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
[e04ef3a]626                extension( logicalExpr );
[6c4ff37]627                output << "(";
[51587aa]628                logicalExpr->get_arg1()->accept( *this );
629                if ( logicalExpr->get_isAnd() ) {
[6c4ff37]630                        output << " && ";
[51587aa]631                } else {
[6c4ff37]632                        output << " || ";
[51587aa]633                } // if
634                logicalExpr->get_arg2()->accept( *this );
[6c4ff37]635                output << ")";
[51587aa]636        }
[71f4e4f]637
[8688ce1]638        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]639                extension( conditionalExpr );
[6c4ff37]640                output << "(";
[51587aa]641                conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]642                output << " ? ";
[51587aa]643                conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]644                output << " : ";
[51587aa]645                conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]646                output << ")";
[51587aa]647        }
[71f4e4f]648
[8688ce1]649        void CodeGenerator::visit( CommaExpr * commaExpr ) {
[e04ef3a]650                extension( commaExpr );
[6c4ff37]651                output << "(";
[51587aa]652                commaExpr->get_arg1()->accept( *this );
[6c4ff37]653                output << " , ";
[51587aa]654                commaExpr->get_arg2()->accept( *this );
[6c4ff37]655                output << ")";
[51587aa]656        }
[71f4e4f]657
[907eccb]658        void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { assertf( false, "UntypedTupleExpr should not make it to Code Gen" ); }
659
660        void CodeGenerator::visit( TupleExpr * tupleExpr ) { assertf( false, "TupleExpr should not make it to Code Gen" ); }
[71f4e4f]661
[8688ce1]662        void CodeGenerator::visit( TypeExpr * typeExpr ) {}
[2b6c1e0]663
[8688ce1]664        void CodeGenerator::visit( AsmExpr * asmExpr ) {
[7f5566b]665                if ( asmExpr->get_inout() ) {
666                        output << "[ ";
667                        asmExpr->get_inout()->accept( *this );
668                        output << " ] ";
669                } // if
670                asmExpr->get_constraint()->accept( *this );
671                output << " ( ";
672                asmExpr->get_operand()->accept( *this );
673                output << " )";
674        }
675
[3c13c03]676        void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
677                assert( compLitExpr->get_type() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
678                output << "(" << genType( compLitExpr->get_type(), "" ) << ")";
679                compLitExpr->get_initializer()->accept( *this );
680        }
681
[6eb8948]682        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
[3c13c03]683                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
684                output << "({" << std::endl;
685                cur_indent += CodeGenerator::tabsize;
686                unsigned int numStmts = stmts.size();
687                unsigned int i = 0;
688                for ( Statement * stmt : stmts ) {
689                        output << indent << printLabels( stmt->get_labels() );
690                        if ( i+1 == numStmts ) {
691                                // last statement in a statement expression needs to be handled specially -
692                                // cannot cast to void, otherwise the expression statement has no value
693                                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
694                                        exprStmt->get_expr()->accept( *this );
695                                        output << ";" << endl;
696                                        ++i;
697                                        break;
698                                }
699                        }
700                        stmt->accept( *this );
701                        output << endl;
702                        if ( wantSpacing( stmt ) ) {
703                                output << endl;
704                        } // if
705                        ++i;
706                }
707                cur_indent -= CodeGenerator::tabsize;
708                output << indent << "})";
[6eb8948]709        }
710
[51587aa]711        //*** Statements
[8688ce1]712        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
[51587aa]713                std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]714                output << "{" << endl;
[51587aa]715
[2b6c1e0]716                cur_indent += CodeGenerator::tabsize;
[51587aa]717
[7f5566b]718                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
[cda48b6]719                        output << indent << printLabels( (*i)->get_labels() );
[7f5566b]720                        (*i)->accept( *this );
[2b6c1e0]721
[6c4ff37]722                        output << endl;
[2b6c1e0]723                        if ( wantSpacing( *i ) ) {
724                                output << endl;
[3778cb2]725                        } // if
[8688ce1]726                } // for
[71f4e4f]727                cur_indent -= CodeGenerator::tabsize;
[51587aa]728
[cda48b6]729                output << indent << "}";
[51587aa]730        }
731
[8688ce1]732        void CodeGenerator::visit( ExprStmt * exprStmt ) {
[6c4ff37]733                assert( exprStmt );
[321a2481]734                // cast the top-level expression to void to reduce gcc warnings.
735                Expression * expr = new CastExpr( exprStmt->get_expr() );
736                expr->accept( *this );
737                output << ";";
[51587aa]738        }
739
[8688ce1]740        void CodeGenerator::visit( AsmStmt * asmStmt ) {
[7f5566b]741                output << "asm ";
742                if ( asmStmt->get_voltile() ) output << "volatile ";
743                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
744                output << "( ";
745                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
746                output << " : ";
747                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
748                output << " : ";
749                genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
750                output << " : ";
751                genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
752                if ( ! asmStmt->get_gotolabels().empty() ) {
753                        output << " : ";
754                        for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
755                                output << *begin++;
756                                if ( begin == asmStmt->get_gotolabels().end() ) break;
757                                output << ", ";
758                        } // for
759                } // if
760                output << " );" ;
761        }
762
[8688ce1]763        void CodeGenerator::visit( IfStmt * ifStmt ) {
[7f5566b]764                output << "if ( ";
765                ifStmt->get_condition()->accept( *this );
766                output << " ) ";
[51587aa]767
[7f5566b]768                ifStmt->get_thenPart()->accept( *this );
[51587aa]769
770                if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]771                        output << " else ";
[7f5566b]772                        ifStmt->get_elsePart()->accept( *this );
[51587aa]773                } // if
774        }
775
[8688ce1]776        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
[7f5566b]777                output << "switch ( " ;
778                switchStmt->get_condition()->accept( *this );
779                output << " ) ";
[71f4e4f]780
[2b6c1e0]781                output << "{" << std::endl;
[6c4ff37]782                cur_indent += CodeGenerator::tabsize;
[8688ce1]783                acceptAll( switchStmt->get_statements(), *this );
[6c4ff37]784                cur_indent -= CodeGenerator::tabsize;
[cda48b6]785                output << indent << "}";
[51587aa]786        }
787
[8688ce1]788        void CodeGenerator::visit( CaseStmt * caseStmt ) {
[cda48b6]789                output << indent;
[eb3261f]790                if ( caseStmt->isDefault()) {
[2b6c1e0]791                        output << "default";
[eb3261f]792                } else {
[2b6c1e0]793                        output << "case ";
[7f5566b]794                        caseStmt->get_condition()->accept( *this );
[17cd4eb]795                } // if
[6c4ff37]796                output << ":\n";
[71f4e4f]797
[51587aa]798                std::list<Statement *> sts = caseStmt->get_statements();
799
[6c4ff37]800                cur_indent += CodeGenerator::tabsize;
[51587aa]801                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
[cda48b6]802                        output << indent << printLabels( (*i)->get_labels() )  ;
[7f5566b]803                        (*i)->accept( *this );
[6c4ff37]804                        output << endl;
[3778cb2]805                } // for
[6c4ff37]806                cur_indent -= CodeGenerator::tabsize;
[51587aa]807        }
808
[8688ce1]809        void CodeGenerator::visit( BranchStmt * branchStmt ) {
[51587aa]810                switch ( branchStmt->get_type()) {
811                  case BranchStmt::Goto:
812                        if ( ! branchStmt->get_target().empty() )
[6c4ff37]813                                output << "goto " << branchStmt->get_target();
[71f4e4f]814                        else {
[51587aa]815                                if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]816                                        output << "goto *";
[51587aa]817                                        branchStmt->get_computedTarget()->accept( *this );
818                                } // if
819                        } // if
820                        break;
821                  case BranchStmt::Break:
[6c4ff37]822                        output << "break";
[51587aa]823                        break;
824                  case BranchStmt::Continue:
[6c4ff37]825                        output << "continue";
[51587aa]826                        break;
[3778cb2]827                } // switch
[2b6c1e0]828                output << ";";
[51587aa]829        }
830
831
[8688ce1]832        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
[6c4ff37]833                output << "return ";
[4b2589a]834                maybeAccept( returnStmt->get_expr(), *this );
[6c4ff37]835                output << ";";
[51587aa]836        }
837
[8688ce1]838        void CodeGenerator::visit( WhileStmt * whileStmt ) {
[321a2481]839                if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]840                        output << "do" ;
[321a2481]841                } else {
[6c4ff37]842                        output << "while (" ;
[7f5566b]843                        whileStmt->get_condition()->accept( *this );
[6c4ff37]844                        output << ")";
[51587aa]845                } // if
[2b6c1e0]846                output << " ";
[51587aa]847
[2b6c1e0]848                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]849                whileStmt->get_body()->accept( *this );
850
[cda48b6]851                output << indent;
[51587aa]852
853                if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]854                        output << " while (" ;
[7f5566b]855                        whileStmt->get_condition()->accept( *this );
[6c4ff37]856                        output << ");";
[51587aa]857                } // if
858        }
859
[8688ce1]860        void CodeGenerator::visit( ForStmt * forStmt ) {
[8e9cbb2]861                // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]862                output << "for (;";
[51587aa]863
[321a2481]864                if ( forStmt->get_condition() != 0 ) {
[51587aa]865                        forStmt->get_condition()->accept( *this );
[3778cb2]866                } // if
[6c4ff37]867                output << ";";
[51587aa]868
[321a2481]869                if ( forStmt->get_increment() != 0 ) {
870                        // cast the top-level expression to void to reduce gcc warnings.
871                        Expression * expr = new CastExpr( forStmt->get_increment() );
872                        expr->accept( *this );
[3778cb2]873                } // if
[2b6c1e0]874                output << ") ";
[51587aa]875
876                if ( forStmt->get_body() != 0 ) {
[2b6c1e0]877                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]878                        forStmt->get_body()->accept( *this );
879                } // if
880        }
881
[8688ce1]882        void CodeGenerator::visit( NullStmt * nullStmt ) {
[cda48b6]883                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]884                output << "/* null statement */ ;";
[51587aa]885        }
886
[8688ce1]887        void CodeGenerator::visit( DeclStmt * declStmt ) {
[51587aa]888                declStmt->get_decl()->accept( *this );
[71f4e4f]889
[51587aa]890                if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]891                        output << ";";
[51587aa]892                } // if
893        }
894
[8688ce1]895        void CodeGenerator::handleStorageClass( Declaration * decl ) {
[51587aa]896                switch ( decl->get_storageClass() ) {
[68cd1ce]897                  case DeclarationNode::Extern:
[6c4ff37]898                        output << "extern ";
[51587aa]899                        break;
[68cd1ce]900                  case DeclarationNode::Static:
[6c4ff37]901                        output << "static ";
[51587aa]902                        break;
[68cd1ce]903                  case DeclarationNode::Auto:
[51587aa]904                        // silently drop storage class
905                        break;
[68cd1ce]906                  case DeclarationNode::Register:
[6c4ff37]907                        output << "register ";
[51587aa]908                        break;
[68cd1ce]909                  case DeclarationNode::Inline:
[de62360d]910                        output << "inline ";
[f38c8d9]911                        break;
[68cd1ce]912                  case DeclarationNode::Fortran:
913                        output << "fortran ";
914                        break;
915                  case DeclarationNode::Noreturn:
916                        output << "_Noreturn ";
917                        break;
918                  case DeclarationNode::Threadlocal:
919                        output << "_Thread_local ";
920                        break;
921                  case DeclarationNode::NoStorageClass:
[f38c8d9]922                        break;
[843054c2]923                } // switch
[51587aa]924        }
[9facf3b]925
926        std::string genName( DeclarationWithType * decl ) {
927                CodeGen::OperatorInfo opInfo;
928                if ( operatorLookup( decl->get_name(), opInfo ) ) {
929                        return opInfo.outputName;
930                } else {
931                        return decl->get_name();
932                } // if
933        }
[51b7345]934} // namespace CodeGen
[51587aa]935
936// Local Variables: //
937// tab-width: 4 //
938// mode: c++ //
939// compile-command: "make install" //
940// End: //
Note: See TracBrowser for help on using the repository browser.