Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rc2ce2350 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 : Wed Apr 27 11:59:36 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:
    241                                   case OT_DTOR:
    242233                                        {
    243234                                                assert( arg != applicationExpr->get_args().end() );
    244235                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    245                                                         // remove & from first assignment/ctor argument
     236               
    246237                                                        *arg = addrExpr->get_arg();
    247238                                                } else {
    248                                                         // no address-of operator, so must be a pointer - add dereference
    249239                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    250240                                                        newExpr->get_args().push_back( *arg );
     
    253243                                                break;
    254244                                        }
    255 
     245             
    256246                                  default:
    257247                                        // do nothing
    258248                                        ;
    259249                                }
    260 
     250           
    261251                                switch ( opInfo.type ) {
    262252                                  case OT_INDEX:
     
    267257                                        output << "]";
    268258                                        break;
    269 
     259             
    270260                                  case OT_CALL:
    271261                                        // there are no intrinsic definitions of the function call operator
    272262                                        assert( false );
    273263                                        break;
    274 
    275                                   case OT_CTOR:
    276                                   case OT_DTOR:
    277                                         if ( applicationExpr->get_args().size() == 1 ) {
    278                                                 // the expression fed into a single parameter constructor or destructor
    279                                                 // may contain side effects - output as a void expression
    280                                                 output << "((void)(";
    281                                                 (*arg++)->accept( *this );
    282                                                 output << ")) /* " << opInfo.inputName << " */";
    283                                         } else if ( applicationExpr->get_args().size() == 2 ) {
    284                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    285                                                 output << "(";
    286                                                 (*arg++)->accept( *this );
    287                                                 output << opInfo.symbol;
    288                                                 (*arg)->accept( *this );
    289                                                 output << ") /* " << opInfo.inputName << " */";
    290                                         } else {
    291                                                 // no constructors with 0 or more than 2 parameters
    292                                                 assert( false );
    293                                         }
    294                                         break;
    295 
     264             
    296265                                  case OT_PREFIX:
    297266                                  case OT_PREFIXASSIGN:
     
    302271                                        output << ")";
    303272                                        break;
    304 
     273             
    305274                                  case OT_POSTFIX:
    306275                                  case OT_POSTFIXASSIGN:
     
    309278                                        output << opInfo.symbol;
    310279                                        break;
    311 
    312280
    313281                                  case OT_INFIX:
     
    320288                                        output << ")";
    321289                                        break;
    322 
     290             
    323291                                  case OT_CONSTANT:
    324292                                  case OT_LABELADDRESS:
     
    339307                } // if
    340308        }
    341 
     309 
    342310        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    343311                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    353321                                        output << "]";
    354322                                        break;
    355 
     323             
    356324                                  case OT_CALL:
    357325                                        assert( false );
    358 
    359 
    360                                   case OT_CTOR:
    361                                   case OT_DTOR:
    362                                         if ( untypedExpr->get_args().size() == 1 ) {
    363                                                 // the expression fed into a single parameter constructor or destructor
    364                                                 // may contain side effects - output as a void expression
    365                                                 output << "((void)(";
    366                                                 (*arg++)->accept( *this );
    367                                                 output << ")) /* " << opInfo.inputName << " */";
    368                                         } else if ( untypedExpr->get_args().size() == 2 ) {
    369                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    370                                                 output << "(";
    371                                                 (*arg++)->accept( *this );
    372                                                 output << opInfo.symbol;
    373                                                 (*arg)->accept( *this );
    374                                                 output << ") /* " << opInfo.inputName << " */";
    375                                         } else {
    376                                                 // no constructors with 0 or more than 2 parameters
    377                                                 assert( false );
    378                                         }
    379                                         break;
    380 
     326                                        break;
     327             
    381328                                  case OT_PREFIX:
    382329                                  case OT_PREFIXASSIGN:
     
    388335                                        output << ")";
    389336                                        break;
    390 
     337             
    391338                                  case OT_POSTFIX:
    392339                                  case OT_POSTFIXASSIGN:
     
    395342                                        output << opInfo.symbol;
    396343                                        break;
    397 
     344 
    398345                                  case OT_INFIX:
    399346                                  case OT_INFIXASSIGN:
     
    405352                                        output << ")";
    406353                                        break;
    407 
     354                                       
    408355                                  case OT_CONSTANT:
    409356                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    423370                } // if
    424371        }
    425 
     372 
    426373        void CodeGenerator::visit( NameExpr *nameExpr ) {
    427374                OperatorInfo opInfo;
     
    433380                } // if
    434381        }
    435 
     382 
    436383        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    437384                output << "(&";
     
    462409                output << ")";
    463410        }
    464 
     411 
    465412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    466413                assert( false );
    467414        }
    468 
     415 
    469416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    470417                memberExpr->get_aggregate()->accept( *this );
    471418                output << "." << mangleName( memberExpr->get_member() );
    472419        }
    473 
     420 
    474421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    475422                OperatorInfo opInfo;
     
    480427                } // if
    481428        }
    482 
     429 
    483430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    484431                assert( constantExpr->get_constant() );
    485432                constantExpr->get_constant()->accept( *this );
    486433        }
    487 
     434 
    488435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    489436                output << "sizeof(";
     
    522469                assert( false && "OffsetPackExpr should not reach code generation" );
    523470        }
    524 
     471 
    525472        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    526473                output << "(";
     
    534481                output << ")";
    535482        }
    536 
     483 
    537484        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    538485                output << "(";
     
    544491                output << ")";
    545492        }
    546 
     493 
    547494        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    548495                output << "(";
     
    552499                output << ")";
    553500        }
    554 
     501 
    555502        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    556 
     503 
    557504        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    558505
     
    585532                        }
    586533                }
    587                 cur_indent -= CodeGenerator::tabsize;
     534                cur_indent -= CodeGenerator::tabsize; 
    588535
    589536                output << indent << "}";
     
    591538
    592539        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    593                 // I don't see why this check is necessary.
    594                 // 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, 
    595542                // with an explanation
    596543                assert( exprStmt );
     
    642589                switchStmt->get_condition()->accept( *this );
    643590                output << " ) ";
    644 
     591               
    645592                output << "{" << std::endl;
    646593                cur_indent += CodeGenerator::tabsize;
     
    662609                } // if
    663610                output << ":\n";
    664 
     611               
    665612                std::list<Statement *> sts = caseStmt->get_statements();
    666613
     
    679626                        if ( ! branchStmt->get_target().empty() )
    680627                                output << "goto " << branchStmt->get_target();
    681                         else {
     628                        else { 
    682629                                if ( branchStmt->get_computedTarget() != 0 ) {
    683630                                        output << "goto *";
     
    730677
    731678        void CodeGenerator::visit( ForStmt *forStmt ) {
    732                 // initialization is always hoisted, so don't
    733                 // bother doing anything with that
     679                // initialization is always hoisted, so don't 
     680                // bother doing anything with that 
    734681                output << "for (;";
    735682
     
    755702        void CodeGenerator::visit( DeclStmt *declStmt ) {
    756703                declStmt->get_decl()->accept( *this );
    757 
     704       
    758705                if ( doSemicolon( declStmt->get_decl() ) ) {
    759706                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.