Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r356189a 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
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Thu Apr 14 17:10:21 2016
    13 // Update Count     : 255
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Mar  2 17:32:16 2016
     13// Update Count     : 243
    1414//
    1515
     
    6767        string mangleName( DeclarationWithType *decl ) {
    6868                if ( decl->get_mangleName() != "" ) {
    69                         // need to incorporate scope level in order to differentiate names for destructors
    70                         return decl->get_scopedMangleName();
     69                        return decl->get_mangleName();
    7170                } else {
    7271                        return decl->get_name();
     
    10099                handleStorageClass( objectDecl );
    101100                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    102 
     101       
    103102                if ( objectDecl->get_init() ) {
    104103                        output << " = ";
     
    114113                if ( aggDecl->get_name() != "" )
    115114                        output << aggDecl->get_name();
    116 
     115       
    117116                std::list< Declaration * > &memb = aggDecl->get_members();
    118117
     
    120119                        output << " {" << endl;
    121120
    122                         cur_indent += CodeGenerator::tabsize;
     121                        cur_indent += CodeGenerator::tabsize; 
    123122                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    124                                 output << indent;
     123                                output << indent; 
    125124                                (*i)->accept( *this );
    126125                                output << ";" << endl;
    127126                        }
    128127
    129                         cur_indent -= CodeGenerator::tabsize;
     128                        cur_indent -= CodeGenerator::tabsize; 
    130129
    131130                        output << indent << "}";
     
    142141                handleAggregate( aggregateDecl );
    143142        }
    144 
     143 
    145144        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    146145                output << "enum ";
     
    148147                if ( aggDecl->get_name() != "" )
    149148                        output << aggDecl->get_name();
    150 
     149       
    151150                std::list< Declaration* > &memb = aggDecl->get_members();
    152151
     
    154153                        output << " {" << endl;
    155154
    156                         cur_indent += CodeGenerator::tabsize;
     155                        cur_indent += CodeGenerator::tabsize; 
    157156                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    158157                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    159158                                assert( obj );
    160                                 output << indent << mangleName( obj );
     159                                output << indent << mangleName( obj ); 
    161160                                if ( obj->get_init() ) {
    162161                                        output << " = ";
     
    166165                        } // for
    167166
    168                         cur_indent -= CodeGenerator::tabsize;
     167                        cur_indent -= CodeGenerator::tabsize; 
    169168
    170169                        output << indent << "}";
    171170                } // if
    172171        }
    173 
     172 
    174173        void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
    175 
     174 
    176175        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    177176                output << "typedef ";
    178177                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    179178        }
    180 
     179 
    181180        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    182181                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    214213                printDesignators( init->get_designators() );
    215214                output << "{ ";
    216                 if ( init->begin_initializers() == init->end_initializers() ) {
    217                         // illegal to leave initializer list empty for scalar initializers,
    218                         // but always legal to have 0
    219                         output << "0";
    220                 } else {
    221                         genCommaList( init->begin_initializers(), init->end_initializers() );
    222                 }
     215                genCommaList( init->begin_initializers(), init->end_initializers() );
    223216                output << " }";
    224217        }
    225218
    226         void CodeGenerator::visit( Constant *constant ) {
     219        void CodeGenerator::visit( Constant *constant ) { 
    227220                output << constant->get_value() ;
    228221        }
     
    238231                                  case OT_POSTFIXASSIGN:
    239232                                  case OT_INFIXASSIGN:
    240                                   case OT_CTOR:
    241233                                        {
    242234                                                assert( arg != applicationExpr->get_args().end() );
    243235                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    244                                                         // remove & from first assignment/ctor argument
     236               
    245237                                                        *arg = addrExpr->get_arg();
    246238                                                } else {
    247                                                         // no address-of operator, so must be a pointer - add dereference
    248239                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    249240                                                        newExpr->get_args().push_back( *arg );
     
    252243                                                break;
    253244                                        }
    254 
     245             
    255246                                  default:
    256247                                        // do nothing
    257248                                        ;
    258249                                }
    259 
     250           
    260251                                switch ( opInfo.type ) {
    261252                                  case OT_INDEX:
     
    266257                                        output << "]";
    267258                                        break;
    268 
     259             
    269260                                  case OT_CALL:
    270261                                        // there are no intrinsic definitions of the function call operator
    271262                                        assert( false );
    272263                                        break;
    273 
    274                                   case OT_CTOR:
    275                                         if ( applicationExpr->get_args().size() == 1 ) {
    276                                                 // the expression fed into a single parameter constructor may contain
    277                                                 // side effects - output as a void expression
    278                                                 output << "((void)(";
    279                                                 (*arg++)->accept( *this );
    280                                                 output << ")) /* ?{} */";
    281                                         } else if ( applicationExpr->get_args().size() == 2 ) {
    282                                                 // intrinsic constructors are essentially bitwise assignment
    283                                                 output << "(";
    284                                                 (*arg++)->accept( *this );
    285                                                 output << opInfo.symbol;
    286                                                 (*arg)->accept( *this );
    287                                                 output << ") /* ?{} */";
    288                                         } else {
    289                                                 // not constructors with 0 or more than 2 parameters
    290                                                 assert( false );
    291                                         }
    292                                         break;
    293 
    294                                   case OT_DTOR:
    295                                   // intrinsic destructors do nothing - don't generate any code
    296                                   output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */";
    297                                   break;
    298 
     264             
    299265                                  case OT_PREFIX:
    300266                                  case OT_PREFIXASSIGN:
     
    305271                                        output << ")";
    306272                                        break;
    307 
     273             
    308274                                  case OT_POSTFIX:
    309275                                  case OT_POSTFIXASSIGN:
     
    312278                                        output << opInfo.symbol;
    313279                                        break;
    314 
    315280
    316281                                  case OT_INFIX:
     
    323288                                        output << ")";
    324289                                        break;
    325 
     290             
    326291                                  case OT_CONSTANT:
    327292                                  case OT_LABELADDRESS:
     
    342307                } // if
    343308        }
    344 
     309 
    345310        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    346311                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    356321                                        output << "]";
    357322                                        break;
    358 
     323             
    359324                                  case OT_CALL:
    360325                                        assert( false );
    361 
    362                                         case OT_CTOR:
    363                                         case OT_DTOR:
    364                                         // intrinsic constructors should never be called
    365                                         // intrinsic destructors do nothing
    366                                         break;
    367 
     326                                        break;
     327             
    368328                                  case OT_PREFIX:
    369329                                  case OT_PREFIXASSIGN:
     
    375335                                        output << ")";
    376336                                        break;
    377 
     337             
    378338                                  case OT_POSTFIX:
    379339                                  case OT_POSTFIXASSIGN:
     
    382342                                        output << opInfo.symbol;
    383343                                        break;
    384 
     344 
    385345                                  case OT_INFIX:
    386346                                  case OT_INFIXASSIGN:
     
    392352                                        output << ")";
    393353                                        break;
    394 
     354                                       
    395355                                  case OT_CONSTANT:
    396356                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    410370                } // if
    411371        }
    412 
     372 
    413373        void CodeGenerator::visit( NameExpr *nameExpr ) {
    414374                OperatorInfo opInfo;
     
    420380                } // if
    421381        }
    422 
     382 
    423383        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    424384                output << "(&";
     
    449409                output << ")";
    450410        }
    451 
     411 
    452412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    453413                assert( false );
    454414        }
    455 
     415 
    456416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    457417                memberExpr->get_aggregate()->accept( *this );
    458418                output << "." << mangleName( memberExpr->get_member() );
    459419        }
    460 
     420 
    461421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    462422                OperatorInfo opInfo;
     
    467427                } // if
    468428        }
    469 
     429 
    470430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    471431                assert( constantExpr->get_constant() );
    472432                constantExpr->get_constant()->accept( *this );
    473433        }
    474 
     434 
    475435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    476436                output << "sizeof(";
     
    509469                assert( false && "OffsetPackExpr should not reach code generation" );
    510470        }
    511 
     471 
    512472        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    513473                output << "(";
     
    521481                output << ")";
    522482        }
    523 
     483 
    524484        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    525485                output << "(";
     
    531491                output << ")";
    532492        }
    533 
     493 
    534494        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    535495                output << "(";
     
    539499                output << ")";
    540500        }
    541 
     501 
    542502        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    543 
     503 
    544504        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    545505
     
    572532                        }
    573533                }
    574                 cur_indent -= CodeGenerator::tabsize;
     534                cur_indent -= CodeGenerator::tabsize; 
    575535
    576536                output << indent << "}";
     
    578538
    579539        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    580                 // I don't see why this check is necessary.
    581                 // If this starts to cause problems then put it back in,
     540                // I don't see why this check is necessary. 
     541                // If this starts to cause problems then put it back in, 
    582542                // with an explanation
    583543                assert( exprStmt );
     
    629589                switchStmt->get_condition()->accept( *this );
    630590                output << " ) ";
    631 
     591               
    632592                output << "{" << std::endl;
    633593                cur_indent += CodeGenerator::tabsize;
     
    649609                } // if
    650610                output << ":\n";
    651 
     611               
    652612                std::list<Statement *> sts = caseStmt->get_statements();
    653613
     
    666626                        if ( ! branchStmt->get_target().empty() )
    667627                                output << "goto " << branchStmt->get_target();
    668                         else {
     628                        else { 
    669629                                if ( branchStmt->get_computedTarget() != 0 ) {
    670630                                        output << "goto *";
     
    717677
    718678        void CodeGenerator::visit( ForStmt *forStmt ) {
    719                 // initialization is always hoisted, so don't
    720                 // bother doing anything with that
     679                // initialization is always hoisted, so don't 
     680                // bother doing anything with that 
    721681                output << "for (;";
    722682
     
    742702        void CodeGenerator::visit( DeclStmt *declStmt ) {
    743703                declStmt->get_decl()->accept( *this );
    744 
     704       
    745705                if ( doSemicolon( declStmt->get_decl() ) ) {
    746706                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.