Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r803deb1 r47534159  
    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 : Rob Schluntz
    12 // Last Modified On : Wed Jan 20 12:54:50 2016
    13 // Update Count     : 241
     12// Last Modified On : Thu Sep 17 15:24:08 2015
     13// Update Count     : 231
    1414//
    1515
     
    9898                handleStorageClass( objectDecl );
    9999                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    100 
     100       
    101101                if ( objectDecl->get_init() ) {
    102102                        output << " = ";
     
    112112                if ( aggDecl->get_name() != "" )
    113113                        output << aggDecl->get_name();
    114 
     114       
    115115                std::list< Declaration * > &memb = aggDecl->get_members();
    116116
     
    118118                        output << " {" << endl;
    119119
    120                         cur_indent += CodeGenerator::tabsize;
     120                        cur_indent += CodeGenerator::tabsize; 
    121121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    122                                 output << indent;
     122                                output << indent; 
    123123                                (*i)->accept( *this );
    124124                                output << ";" << endl;
    125125                        }
    126126
    127                         cur_indent -= CodeGenerator::tabsize;
     127                        cur_indent -= CodeGenerator::tabsize; 
    128128
    129129                        output << indent << "}";
     
    140140                handleAggregate( aggregateDecl );
    141141        }
    142 
     142 
    143143        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    144144                output << "enum ";
     
    146146                if ( aggDecl->get_name() != "" )
    147147                        output << aggDecl->get_name();
    148 
     148       
    149149                std::list< Declaration* > &memb = aggDecl->get_members();
    150150
     
    152152                        output << " {" << endl;
    153153
    154                         cur_indent += CodeGenerator::tabsize;
     154                        cur_indent += CodeGenerator::tabsize; 
    155155                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    156156                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    157157                                assert( obj );
    158                                 output << indent << mangleName( obj );
     158                                output << indent << mangleName( obj ); 
    159159                                if ( obj->get_init() ) {
    160160                                        output << " = ";
     
    164164                        } // for
    165165
    166                         cur_indent -= CodeGenerator::tabsize;
     166                        cur_indent -= CodeGenerator::tabsize; 
    167167
    168168                        output << indent << "}";
    169169                } // if
    170170        }
    171 
     171 
    172172        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
    173 
     173 
    174174        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    175175                output << "typedef ";
    176176                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    177177        }
    178 
     178 
    179179        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    180180                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    216216        }
    217217
    218         void CodeGenerator::visit( Constant *constant ) {
     218        void CodeGenerator::visit( Constant *constant ) { 
    219219                output << constant->get_value() ;
    220220        }
     
    233233                                                assert( arg != applicationExpr->get_args().end() );
    234234                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    235 
     235               
    236236                                                        *arg = addrExpr->get_arg();
    237237                                                } else {
     
    242242                                                break;
    243243                                        }
    244 
     244             
    245245                                  default:
    246246                                        // do nothing
    247247                                        ;
    248248                                }
    249 
     249           
    250250                                switch ( opInfo.type ) {
    251251                                  case OT_INDEX:
     
    256256                                        output << "]";
    257257                                        break;
    258 
     258             
    259259                                  case OT_CALL:
    260260                                        // there are no intrinsic definitions of the function call operator
    261261                                        assert( false );
    262262                                        break;
    263 
     263             
    264264                                  case OT_PREFIX:
    265265                                  case OT_PREFIXASSIGN:
     
    270270                                        output << ")";
    271271                                        break;
    272 
     272             
    273273                                  case OT_POSTFIX:
    274274                                  case OT_POSTFIXASSIGN:
     
    287287                                        output << ")";
    288288                                        break;
    289 
     289             
    290290                                  case OT_CONSTANT:
    291291                                  case OT_LABELADDRESS:
     
    306306                } // if
    307307        }
    308 
     308 
    309309        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    310310                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    320320                                        output << "]";
    321321                                        break;
    322 
     322             
    323323                                  case OT_CALL:
    324324                                        assert( false );
    325325                                        break;
    326 
     326             
    327327                                  case OT_PREFIX:
    328328                                  case OT_PREFIXASSIGN:
     
    334334                                        output << ")";
    335335                                        break;
    336 
     336             
    337337                                  case OT_POSTFIX:
    338338                                  case OT_POSTFIXASSIGN:
     
    341341                                        output << opInfo.symbol;
    342342                                        break;
    343 
     343 
    344344                                  case OT_INFIX:
    345345                                  case OT_INFIXASSIGN:
     
    351351                                        output << ")";
    352352                                        break;
    353 
     353                                       
    354354                                  case OT_CONSTANT:
    355355                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    369369                } // if
    370370        }
    371 
     371 
    372372        void CodeGenerator::visit( NameExpr *nameExpr ) {
    373373                OperatorInfo opInfo;
     
    379379                } // if
    380380        }
    381 
     381 
    382382        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    383383                output << "(&";
     
    392392
    393393        void CodeGenerator::visit( CastExpr *castExpr ) {
    394                 output << "(";
    395                 if ( castExpr->get_results().empty() ) {
    396                         output << "(void)" ;
    397                 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
    398                         // at least one result type of cast, but not an lvalue
    399                         output << "(";
    400                         output << genType( castExpr->get_results().front(), "" );
     394                // if the cast is to an lvalue type, then the cast
     395                // should be dropped, since the result of a cast is
     396                // never an lvalue in C
     397                if ( castExpr->get_results().front()->get_isLvalue() ) {
     398                        castExpr->get_arg()->accept( *this );
     399                } else {
     400                        output << "((";
     401                        if ( castExpr->get_results().empty() ) {
     402                                output << "void" ;
     403                        } else {
     404                                output << genType( castExpr->get_results().front(), "" );
     405                        } // if
    401406                        output << ")";
    402                 } else {
    403                         // otherwise, the cast is to an lvalue type, so the cast
    404                         // should be dropped, since the result of a cast is
    405                         // never an lvalue in C
     407                        castExpr->get_arg()->accept( *this );
     408                        output << ")";                 
    406409                }
    407                 castExpr->get_arg()->accept( *this );
    408                 output << ")";
    409         }
    410 
     410        }
     411 
    411412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    412413                assert( false );
    413414        }
    414 
     415 
    415416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    416417                memberExpr->get_aggregate()->accept( *this );
    417418                output << "." << mangleName( memberExpr->get_member() );
    418419        }
    419 
     420 
    420421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    421422                OperatorInfo opInfo;
     
    426427                } // if
    427428        }
    428 
     429 
    429430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    430431                assert( constantExpr->get_constant() );
    431432                constantExpr->get_constant()->accept( *this );
    432433        }
    433 
     434 
    434435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    435436                output << "sizeof(";
     
    452453                output << ")";
    453454        }
    454 
     455 
    455456        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    456457                output << "(";
     
    464465                output << ")";
    465466        }
    466 
     467 
    467468        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    468469                output << "(";
     
    474475                output << ")";
    475476        }
    476 
     477 
    477478        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    478479                output << "(";
     
    482483                output << ")";
    483484        }
    484 
     485 
    485486        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    486 
     487 
    487488        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    488489
     
    515516                        }
    516517                }
    517                 cur_indent -= CodeGenerator::tabsize;
     518                cur_indent -= CodeGenerator::tabsize; 
    518519
    519520                output << indent << "}";
     
    521522
    522523        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    523                 // I don't see why this check is necessary.
    524                 // If this starts to cause problems then put it back in,
     524                // I don't see why this check is necessary. 
     525                // If this starts to cause problems then put it back in, 
    525526                // with an explanation
    526527                assert( exprStmt );
     
    572573                switchStmt->get_condition()->accept( *this );
    573574                output << " ) ";
    574 
     575               
    575576                output << "{" << std::endl;
    576577                cur_indent += CodeGenerator::tabsize;
     
    592593                } // if
    593594                output << ":\n";
    594 
     595               
    595596                std::list<Statement *> sts = caseStmt->get_statements();
    596597
     
    609610                        if ( ! branchStmt->get_target().empty() )
    610611                                output << "goto " << branchStmt->get_target();
    611                         else {
     612                        else { 
    612613                                if ( branchStmt->get_computedTarget() != 0 ) {
    613614                                        output << "goto *";
     
    660661
    661662        void CodeGenerator::visit( ForStmt *forStmt ) {
    662                 // initialization is always hoisted, so don't
    663                 // bother doing anything with that
     663                // initialization is always hoisted, so don't 
     664                // bother doing anything with that 
    664665                output << "for (;";
    665666
     
    685686        void CodeGenerator::visit( DeclStmt *declStmt ) {
    686687                declStmt->get_decl()->accept( *this );
    687 
     688       
    688689                if ( doSemicolon( declStmt->get_decl() ) ) {
    689690                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.