Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r4b2589a rafc1045  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // CodeGenerator.cc --
     7// CodeGenerator.cc -- 
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun  9 13:21:00 2016
    13 // Update Count     : 256
     12// Last Modified On : Wed Mar  2 17:32:16 2016
     13// Update Count     : 243
    1414//
    1515
     
    2626#include "SynTree/Statement.h"
    2727#include "SynTree/Type.h"
    28 #include "SynTree/Attribute.h"
    2928
    3029#include "Common/utility.h"
     
    3433#include "OperatorTable.h"
    3534#include "GenType.h"
    36 
    37 #include "InitTweak/InitTweak.h"
    3835
    3936using namespace std;
     
    4845        }
    4946
    50         ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
     47        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
    5148          return output << string( cg.cur_indent, ' ' );
    5249        }
    5350
    54         ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
     51        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
    5552                return indent( output );
    5653        }
    5754
    58         CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
    59                 labels = &l;
    60                 return *this;
    61         }
    62 
    63         ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter &printLabels ) {
    64                 std::list< Label > & labs = *printLabels.labels;
    65                 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
    66                 for ( Label & l : labs ) {
    67                         output << l.get_name() + ": ";
    68                         printLabels.cg.genAttributes( l.get_attributes() );
    69                 }
    70                 return output;
    71         }
    72 
    73         CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) { }
     55        CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
    7456
    7557        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
    76                         : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
     58                        : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    7759                //output << std::string( init );
    7860        }
    7961
    8062        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
    81                         : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
     63                        : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    8264                //output << std::string( init );
    8365        }
     
    8567        string mangleName( DeclarationWithType *decl ) {
    8668                if ( decl->get_mangleName() != "" ) {
    87                         // need to incorporate scope level in order to differentiate names for destructors
    88                         return decl->get_scopedMangleName();
     69                        return decl->get_mangleName();
    8970                } else {
    9071                        return decl->get_name();
    9172                } // if
    9273        }
    93 
    94         void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) {
    95                 if ( ! attributes.empty() ) {
    96                         output << "__attribute__ ((";
    97                         for ( Attribute *& attr : attributes ) {
    98                                 if ( ! attr->empty() ) {
    99                                         output << attr->get_name() << "(";
    100                                         genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
    101                                         output << ")";
    102                                 }
    103                                 output << ",";
    104                         }
    105                         output << ")) ";
    106                 }
    107         }
    108 
    10974
    11075        //*** Declarations
    11176        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
    112                 genAttributes( functionDecl->get_attributes() );
    113 
    11477                handleStorageClass( functionDecl );
    11578                if ( functionDecl->get_isInline() ) {
     
    13699                handleStorageClass( objectDecl );
    137100                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    138 
     101       
    139102                if ( objectDecl->get_init() ) {
    140103                        output << " = ";
     
    150113                if ( aggDecl->get_name() != "" )
    151114                        output << aggDecl->get_name();
    152 
     115       
    153116                std::list< Declaration * > &memb = aggDecl->get_members();
    154117
     
    156119                        output << " {" << endl;
    157120
    158                         cur_indent += CodeGenerator::tabsize;
     121                        cur_indent += CodeGenerator::tabsize; 
    159122                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    160                                 output << indent;
     123                                output << indent; 
    161124                                (*i)->accept( *this );
    162125                                output << ";" << endl;
    163126                        }
    164127
    165                         cur_indent -= CodeGenerator::tabsize;
     128                        cur_indent -= CodeGenerator::tabsize; 
    166129
    167130                        output << indent << "}";
     
    178141                handleAggregate( aggregateDecl );
    179142        }
    180 
     143 
    181144        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    182145                output << "enum ";
     
    184147                if ( aggDecl->get_name() != "" )
    185148                        output << aggDecl->get_name();
    186 
     149       
    187150                std::list< Declaration* > &memb = aggDecl->get_members();
    188151
     
    190153                        output << " {" << endl;
    191154
    192                         cur_indent += CodeGenerator::tabsize;
     155                        cur_indent += CodeGenerator::tabsize; 
    193156                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    194157                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    195158                                assert( obj );
    196                                 output << indent << mangleName( obj );
     159                                output << indent << mangleName( obj ); 
    197160                                if ( obj->get_init() ) {
    198161                                        output << " = ";
     
    202165                        } // for
    203166
    204                         cur_indent -= CodeGenerator::tabsize;
     167                        cur_indent -= CodeGenerator::tabsize; 
    205168
    206169                        output << indent << "}";
    207170                } // if
    208171        }
    209 
     172 
    210173        void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
    211 
     174 
    212175        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    213176                output << "typedef ";
    214177                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    215178        }
    216 
     179 
    217180        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    218181                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    250213                printDesignators( init->get_designators() );
    251214                output << "{ ";
    252                 if ( init->begin_initializers() == init->end_initializers() ) {
    253                         // illegal to leave initializer list empty for scalar initializers,
    254                         // but always legal to have 0
    255                         output << "0";
    256                 } else {
    257                         genCommaList( init->begin_initializers(), init->end_initializers() );
    258                 }
     215                genCommaList( init->begin_initializers(), init->end_initializers() );
    259216                output << " }";
    260217        }
    261218
    262         void CodeGenerator::visit( Constant *constant ) {
     219        void CodeGenerator::visit( Constant *constant ) { 
    263220                output << constant->get_value() ;
    264221        }
     
    266223        //*** Expressions
    267224        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
    268                 extension( applicationExpr );
    269225                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    270226                        OperatorInfo opInfo;
     
    275231                                  case OT_POSTFIXASSIGN:
    276232                                  case OT_INFIXASSIGN:
    277                                   case OT_CTOR:
    278                                   case OT_DTOR:
    279233                                        {
    280234                                                assert( arg != applicationExpr->get_args().end() );
    281235                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    282                                                         // remove & from first assignment/ctor argument
     236               
    283237                                                        *arg = addrExpr->get_arg();
    284238                                                } else {
    285                                                         // no address-of operator, so must be a pointer - add dereference
    286239                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    287240                                                        newExpr->get_args().push_back( *arg );
    288                                                         assert( (*arg)->get_results().size() == 1 );
    289                                                         Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() );
    290                                                         assert( type );
    291                                                         newExpr->get_results().push_back( type );
    292241                                                        *arg = newExpr;
    293242                                                } // if
    294243                                                break;
    295244                                        }
    296 
     245             
    297246                                  default:
    298247                                        // do nothing
    299248                                        ;
    300249                                }
    301 
     250           
    302251                                switch ( opInfo.type ) {
    303252                                  case OT_INDEX:
     
    308257                                        output << "]";
    309258                                        break;
    310 
     259             
    311260                                  case OT_CALL:
    312261                                        // there are no intrinsic definitions of the function call operator
    313262                                        assert( false );
    314263                                        break;
    315 
    316                                   case OT_CTOR:
    317                                   case OT_DTOR:
    318                                         if ( applicationExpr->get_args().size() == 1 ) {
    319                                                 // the expression fed into a single parameter constructor or destructor
    320                                                 // may contain side effects, so must still output this expression
    321                                                 output << "(";
    322                                                 (*arg++)->accept( *this );
    323                                                 output << ") /* " << opInfo.inputName << " */";
    324                                         } else if ( applicationExpr->get_args().size() == 2 ) {
    325                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    326                                                 output << "(";
    327                                                 (*arg++)->accept( *this );
    328                                                 output << opInfo.symbol;
    329                                                 (*arg)->accept( *this );
    330                                                 output << ") /* " << opInfo.inputName << " */";
    331                                         } else {
    332                                                 // no constructors with 0 or more than 2 parameters
    333                                                 assert( false );
    334                                         }
    335                                         break;
    336 
     264             
    337265                                  case OT_PREFIX:
    338266                                  case OT_PREFIXASSIGN:
     
    343271                                        output << ")";
    344272                                        break;
    345 
     273             
    346274                                  case OT_POSTFIX:
    347275                                  case OT_POSTFIXASSIGN:
     
    350278                                        output << opInfo.symbol;
    351279                                        break;
    352 
    353280
    354281                                  case OT_INFIX:
     
    361288                                        output << ")";
    362289                                        break;
    363 
     290             
    364291                                  case OT_CONSTANT:
    365292                                  case OT_LABELADDRESS:
     
    380307                } // if
    381308        }
    382 
     309 
    383310        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    384                 extension( untypedExpr );
    385311                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    386312                        OperatorInfo opInfo;
     
    395321                                        output << "]";
    396322                                        break;
    397 
     323             
    398324                                  case OT_CALL:
    399325                                        assert( false );
    400 
    401 
    402                                   case OT_CTOR:
    403                                   case OT_DTOR:
    404                                         if ( untypedExpr->get_args().size() == 1 ) {
    405                                                 // the expression fed into a single parameter constructor or destructor
    406                                                 // may contain side effects, so must still output this expression
    407                                                 output << "(";
    408                                                 (*arg++)->accept( *this );
    409                                                 output << ") /* " << opInfo.inputName << " */";
    410                                         } else if ( untypedExpr->get_args().size() == 2 ) {
    411                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    412                                                 output << "(";
    413                                                 (*arg++)->accept( *this );
    414                                                 output << opInfo.symbol;
    415                                                 (*arg)->accept( *this );
    416                                                 output << ") /* " << opInfo.inputName << " */";
    417                                         } else {
    418                                                 // no constructors with 0 or more than 2 parameters
    419                                                 assert( false );
    420                                         }
    421                                         break;
    422 
     326                                        break;
     327             
    423328                                  case OT_PREFIX:
    424329                                  case OT_PREFIXASSIGN:
     
    430335                                        output << ")";
    431336                                        break;
    432 
     337             
    433338                                  case OT_POSTFIX:
    434339                                  case OT_POSTFIXASSIGN:
     
    437342                                        output << opInfo.symbol;
    438343                                        break;
    439 
     344 
    440345                                  case OT_INFIX:
    441346                                  case OT_INFIXASSIGN:
     
    447352                                        output << ")";
    448353                                        break;
    449 
     354                                       
    450355                                  case OT_CONSTANT:
    451356                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    465370                } // if
    466371        }
    467 
     372 
    468373        void CodeGenerator::visit( NameExpr *nameExpr ) {
    469                 extension( nameExpr );
    470374                OperatorInfo opInfo;
    471375                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     
    476380                } // if
    477381        }
    478 
     382 
    479383        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    480                 extension( addressExpr );
    481384                output << "(&";
    482385                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
     
    490393
    491394        void CodeGenerator::visit( CastExpr *castExpr ) {
    492                 extension( castExpr );
    493395                output << "(";
    494396                if ( castExpr->get_results().empty() ) {
     
    507409                output << ")";
    508410        }
    509 
     411 
    510412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    511413                assert( false );
    512414        }
    513 
     415 
    514416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    515                 extension( memberExpr );
    516417                memberExpr->get_aggregate()->accept( *this );
    517418                output << "." << mangleName( memberExpr->get_member() );
    518419        }
    519 
     420 
    520421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    521                 extension( variableExpr );
    522422                OperatorInfo opInfo;
    523423                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
     
    527427                } // if
    528428        }
    529 
     429 
    530430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    531431                assert( constantExpr->get_constant() );
    532                 extension( constantExpr );
    533432                constantExpr->get_constant()->accept( *this );
    534433        }
    535 
     434 
    536435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    537                 extension( sizeofExpr );
    538436                output << "sizeof(";
    539437                if ( sizeofExpr->get_isType() ) {
     
    546444
    547445        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
    548                 extension( alignofExpr );
    549446                // use GCC extension to avoid bumping std to C11
    550447                output << "__alignof__(";
     
    562459
    563460        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
    564                 extension( offsetofExpr );
    565461                // use GCC builtin
    566462                output << "__builtin_offsetof(";
     
    573469                assert( false && "OffsetPackExpr should not reach code generation" );
    574470        }
    575 
     471 
    576472        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    577                 extension( logicalExpr );
    578473                output << "(";
    579474                logicalExpr->get_arg1()->accept( *this );
     
    586481                output << ")";
    587482        }
    588 
     483 
    589484        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    590                 extension( conditionalExpr );
    591485                output << "(";
    592486                conditionalExpr->get_arg1()->accept( *this );
     
    597491                output << ")";
    598492        }
    599 
     493 
    600494        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    601                 extension( commaExpr );
    602495                output << "(";
    603496                commaExpr->get_arg1()->accept( *this );
     
    606499                output << ")";
    607500        }
    608 
     501 
    609502        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    610 
     503 
    611504        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    612505
    613506        void CodeGenerator::visit( AsmExpr *asmExpr ) {
    614                 extension( asmExpr );
    615507                if ( asmExpr->get_inout() ) {
    616508                        output << "[ ";
     
    640532                        }
    641533                }
    642                 cur_indent -= CodeGenerator::tabsize;
     534                cur_indent -= CodeGenerator::tabsize; 
    643535
    644536                output << indent << "}";
     
    646538
    647539        void CodeGenerator::visit( ExprStmt *exprStmt ) {
     540                // I don't see why this check is necessary.
     541                // If this starts to cause problems then put it back in,
     542                // with an explanation
    648543                assert( exprStmt );
    649                 // cast the top-level expression to void to reduce gcc warnings.
    650                 Expression * expr = new CastExpr( exprStmt->get_expr() );
    651                 expr->accept( *this );
    652                 output << ";";
     544
     545                // if ( exprStmt != 0 ) {
     546                exprStmt->get_expr()->accept( *this );
     547                output << ";" ;
     548                // } // if
    653549        }
    654550
     
    693589                switchStmt->get_condition()->accept( *this );
    694590                output << " ) ";
    695 
     591               
    696592                output << "{" << std::endl;
    697593                cur_indent += CodeGenerator::tabsize;
     
    713609                } // if
    714610                output << ":\n";
    715 
     611               
    716612                std::list<Statement *> sts = caseStmt->get_statements();
    717613
     
    730626                        if ( ! branchStmt->get_target().empty() )
    731627                                output << "goto " << branchStmt->get_target();
    732                         else {
     628                        else { 
    733629                                if ( branchStmt->get_computedTarget() != 0 ) {
    734630                                        output << "goto *";
     
    750646        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
    751647                output << "return ";
    752                 maybeAccept( returnStmt->get_expr(), *this );
     648
     649                // xxx -- check for null expression;
     650                if ( returnStmt->get_expr() ) {
     651                        returnStmt->get_expr()->accept( *this );
     652                } // if
    753653                output << ";";
    754654        }
    755655
    756656        void CodeGenerator::visit( WhileStmt *whileStmt ) {
    757                 if ( whileStmt->get_isDoWhile() ) {
     657                if ( whileStmt->get_isDoWhile() )
    758658                        output << "do" ;
    759                 } else {
     659                else {
    760660                        output << "while (" ;
    761661                        whileStmt->get_condition()->accept( *this );
     
    777677
    778678        void CodeGenerator::visit( ForStmt *forStmt ) {
    779                 // initialization is always hoisted, so don't
    780                 // bother doing anything with that
     679                // initialization is always hoisted, so don't 
     680                // bother doing anything with that 
    781681                output << "for (;";
    782682
    783                 if ( forStmt->get_condition() != 0 ) {
     683                if ( forStmt->get_condition() != 0 )
    784684                        forStmt->get_condition()->accept( *this );
    785                 }
    786685                output << ";";
    787686
    788                 if ( forStmt->get_increment() != 0 ) {
    789                         // cast the top-level expression to void to reduce gcc warnings.
    790                         Expression * expr = new CastExpr( forStmt->get_increment() );
    791                         expr->accept( *this );
    792                 }
     687                if ( forStmt->get_increment() != 0 )
     688                        forStmt->get_increment()->accept( *this );
    793689                output << ") ";
    794690
     
    806702        void CodeGenerator::visit( DeclStmt *declStmt ) {
    807703                declStmt->get_decl()->accept( *this );
    808 
     704       
    809705                if ( doSemicolon( declStmt->get_decl() ) ) {
    810706                        output << ";";
    811707                } // if
     708        }
     709
     710        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
     711                std::string str( "" );
     712                l.unique(); // assumes a sorted list. Why not use set?
     713
     714                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
     715                        str += *i + ": ";
     716
     717                return str;
    812718        }
    813719
Note: See TracChangeset for help on using the changeset viewer.