Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    ra5f0529 r8a6cf7e  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tus Jul 25 15:29:00 2017
    13 // Update Count     : 486
     12// Last Modified On : Thu Jun  8 16:00:00 2017
     13// Update Count     : 485
    1414//
    15 #include "CodeGenerator.h"
    1615
    1716#include <cassert>                   // for assert, assertf
    1817#include <list>                      // for _List_iterator, list, list<>::it...
    1918
     19#include "CodeGenerator.h"
    2020#include "Common/SemanticError.h"    // for SemanticError
    2121#include "Common/UniqueName.h"       // for UniqueName
     
    6464        } // extension
    6565
    66         ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
    67           return output << string( cg.cur_indent, ' ' );
    68         }
    69 
    70         ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
    71                 return indent( output );
    72         }
    73 
    7466        CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
    7567                labels = &l;
     
    109101        }
    110102
    111         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
     103        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    112104
    113105        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    190182                        genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
    191183                        output << ")" << endl;
     184                        output << indent;
    192185                }
    193186
     
    200193                        output << " {" << endl;
    201194
    202                         cur_indent += CodeGenerator::tabsize;
     195                        ++indent;
    203196                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    204197                                output << lineDirective( *i ) << indent;
     
    207200                        } // for
    208201
    209                         cur_indent -= CodeGenerator::tabsize;
     202                        --indent;
    210203
    211204                        output << indent << "}";
     
    237230                        output << " {" << endl;
    238231
    239                         cur_indent += CodeGenerator::tabsize;
     232                        ++indent;
    240233                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    241234                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
     
    249242                        } // for
    250243
    251                         cur_indent -= CodeGenerator::tabsize;
     244                        --indent;
    252245
    253246                        output << indent << "}";
     
    329322        void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
    330323                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
    331                 // xxx - generate something reasonable for constructor/destructor pairs
    332                 output << "<ctorinit>";
     324                // pseudo-output for constructor/destructor pairs
     325                output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
     326                maybeAccept( init->get_ctor(), *this );
     327                output << ", " << std::endl << indent << "dtor: ";
     328                maybeAccept( init->get_dtor(), *this );
     329                output << std::endl << --indent << "}";
    333330        }
    334331
     
    344341                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
    345342                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
    346                                 switch ( opInfo.type ) {
    347                                   case OT_PREFIXASSIGN:
    348                                   case OT_POSTFIXASSIGN:
    349                                   case OT_INFIXASSIGN:
    350                                   case OT_CTOR:
    351                                   case OT_DTOR:
    352                                         {
    353                                                 assert( arg != applicationExpr->get_args().end() );
    354                                                 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    355                                                         // remove & from first assignment/ctor argument
    356                                                         *arg = addrExpr->get_arg();
    357                                                 } else {
    358                                                         // no address-of operator, so must be a pointer - add dereference
    359                                                         // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
    360                                                         // Since its arguments are modified here, this assertion most commonly triggers when the application
    361                                                         // is visited multiple times.
    362                                                         UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    363                                                         newExpr->get_args().push_back( *arg );
    364                                                         Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
    365                                                         assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
    366                                                         newExpr->set_result( type->clone() );
    367                                                         *arg = newExpr;
    368                                                 } // if
    369                                                 break;
    370                                         }
    371 
    372                                   default:
    373                                         // do nothing
    374                                         ;
    375                                 } // switch
    376 
    377343                                switch ( opInfo.type ) {
    378344                                  case OT_INDEX:
     
    594560        }
    595561
    596         void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
    597                 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
    598                 extension( castExpr );
    599                 output << "(virtual ";
    600                 castExpr->get_arg()->accept( *this );
    601                 output << ")";
    602         }
    603 
    604562        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    605563                assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
     
    703661                extension( commaExpr );
    704662                output << "(";
     663                if ( genC ) {
     664                        // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
     665                        commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
     666                }
    705667                commaExpr->get_arg1()->accept( *this );
    706668                output << " , ";
     
    761723                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    762724                output << lineDirective( stmtExpr) << "({" << std::endl;
    763                 cur_indent += CodeGenerator::tabsize;
     725                ++indent;
    764726                unsigned int numStmts = stmts.size();
    765727                unsigned int i = 0;
    766728                for ( Statement * stmt : stmts ) {
    767729                        output << lineDirective( stmt ) << indent;
    768             output << printLabels( stmt->get_labels() );
     730                        output << printLabels( stmt->get_labels() );
    769731                        if ( i+1 == numStmts ) {
    770732                                // last statement in a statement expression needs to be handled specially -
     
    784746                        ++i;
    785747                }
    786                 cur_indent -= CodeGenerator::tabsize;
     748                --indent;
    787749                output << indent << "})";
    788750        }
     
    793755                output << "{" << endl;
    794756
    795                 cur_indent += CodeGenerator::tabsize;
     757                ++indent;
    796758
    797759                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
     
    804766                        } // if
    805767                } // for
    806                 cur_indent -= CodeGenerator::tabsize;
     768                --indent;
    807769
    808770                output << indent << "}";
     
    811773        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    812774                assert( exprStmt );
    813                 Expression * expr = exprStmt->get_expr();
    814775                if ( genC ) {
    815776                        // cast the top-level expression to void to reduce gcc warnings.
    816                         expr = new CastExpr( expr );
    817                 }
    818                 expr->accept( *this );
     777                        exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
     778                }
     779                exprStmt->get_expr()->accept( *this );
    819780                output << ";";
    820781        }
     
    872833
    873834                output << "{" << std::endl;
    874                 cur_indent += CodeGenerator::tabsize;
     835                ++indent;
    875836                acceptAll( switchStmt->get_statements(), *this );
    876                 cur_indent -= CodeGenerator::tabsize;
     837                --indent;
    877838                output << indent << "}";
    878839        }
     
    891852                std::list<Statement *> sts = caseStmt->get_statements();
    892853
    893                 cur_indent += CodeGenerator::tabsize;
     854                ++indent;
    894855                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    895856                        output << indent << printLabels( (*i)->get_labels() )  ;
     
    897858                        output << endl;
    898859                } // for
    899                 cur_indent -= CodeGenerator::tabsize;
     860                --indent;
    900861        }
    901862
Note: See TracChangeset for help on using the changeset viewer.