Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r5b2f5bb re8032b0  
    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 Mar 30 14:39:30 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
     
    2121#include "Parser/ParseNode.h"
    2222
    23 #include "SynTree/Type.h"
     23#include "SynTree/Declaration.h"
    2424#include "SynTree/Expression.h"
    2525#include "SynTree/Initializer.h"
    2626#include "SynTree/Statement.h"
     27#include "SynTree/Type.h"
    2728
    2829#include "Common/utility.h"
     
    9899                handleStorageClass( objectDecl );
    99100                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    100 
     101       
    101102                if ( objectDecl->get_init() ) {
    102103                        output << " = ";
     
    112113                if ( aggDecl->get_name() != "" )
    113114                        output << aggDecl->get_name();
    114 
     115       
    115116                std::list< Declaration * > &memb = aggDecl->get_members();
    116117
     
    118119                        output << " {" << endl;
    119120
    120                         cur_indent += CodeGenerator::tabsize;
     121                        cur_indent += CodeGenerator::tabsize; 
    121122                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    122                                 output << indent;
     123                                output << indent; 
    123124                                (*i)->accept( *this );
    124125                                output << ";" << endl;
    125126                        }
    126127
    127                         cur_indent -= CodeGenerator::tabsize;
     128                        cur_indent -= CodeGenerator::tabsize; 
    128129
    129130                        output << indent << "}";
     
    140141                handleAggregate( aggregateDecl );
    141142        }
    142 
     143 
    143144        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    144145                output << "enum ";
     
    146147                if ( aggDecl->get_name() != "" )
    147148                        output << aggDecl->get_name();
    148 
     149       
    149150                std::list< Declaration* > &memb = aggDecl->get_members();
    150151
     
    152153                        output << " {" << endl;
    153154
    154                         cur_indent += CodeGenerator::tabsize;
     155                        cur_indent += CodeGenerator::tabsize; 
    155156                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    156157                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    157158                                assert( obj );
    158                                 output << indent << mangleName( obj );
     159                                output << indent << mangleName( obj ); 
    159160                                if ( obj->get_init() ) {
    160161                                        output << " = ";
     
    164165                        } // for
    165166
    166                         cur_indent -= CodeGenerator::tabsize;
     167                        cur_indent -= CodeGenerator::tabsize; 
    167168
    168169                        output << indent << "}";
    169170                } // if
    170171        }
    171 
    172         void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
    173 
     172 
     173        void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
     174 
    174175        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    175176                output << "typedef ";
    176177                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    177178        }
    178 
     179 
    179180        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    180181                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    212213                printDesignators( init->get_designators() );
    213214                output << "{ ";
    214                 if ( init->begin_initializers() == init->end_initializers() ) {
    215                         // illegal to leave initializer list empty for scalar initializers,
    216                         // but always legal to have 0
    217                         output << "0";
    218                 } else {
    219                         genCommaList( init->begin_initializers(), init->end_initializers() );
    220                 }
     215                genCommaList( init->begin_initializers(), init->end_initializers() );
    221216                output << " }";
    222217        }
    223218
    224         void CodeGenerator::visit( Constant *constant ) {
     219        void CodeGenerator::visit( Constant *constant ) { 
    225220                output << constant->get_value() ;
    226221        }
     
    239234                                                assert( arg != applicationExpr->get_args().end() );
    240235                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    241 
     236               
    242237                                                        *arg = addrExpr->get_arg();
    243238                                                } else {
     
    248243                                                break;
    249244                                        }
    250 
     245             
    251246                                  default:
    252247                                        // do nothing
    253248                                        ;
    254249                                }
    255 
     250           
    256251                                switch ( opInfo.type ) {
    257252                                  case OT_INDEX:
     
    262257                                        output << "]";
    263258                                        break;
    264 
     259             
    265260                                  case OT_CALL:
    266                                         // there are no intrinsic definitions of the function call operator or constructors or destructors
     261                                        // there are no intrinsic definitions of the function call operator
    267262                                        assert( false );
    268263                                        break;
    269 
    270                                   case OT_CTOR:
    271                                   // it's just an optimization to disallow this, so for now let it through
    272                                   // since it makes autogenerating constructors a lot easier
    273                                 varExpr->accept( *this );
    274                                         output << "(";
    275                                         genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
    276                                         output << ")";
    277 
    278                                   // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
    279                                   // assert(false);
    280                                   break;
    281 
    282                                   case OT_DTOR:
    283                                   // intrinsic destructors do nothing - don't generate any code
    284                                   output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */";
    285                                   break;
    286 
     264             
    287265                                  case OT_PREFIX:
    288266                                  case OT_PREFIXASSIGN:
     
    293271                                        output << ")";
    294272                                        break;
    295 
     273             
    296274                                  case OT_POSTFIX:
    297275                                  case OT_POSTFIXASSIGN:
     
    300278                                        output << opInfo.symbol;
    301279                                        break;
    302 
    303280
    304281                                  case OT_INFIX:
     
    311288                                        output << ")";
    312289                                        break;
    313 
     290             
    314291                                  case OT_CONSTANT:
    315292                                  case OT_LABELADDRESS:
     
    330307                } // if
    331308        }
    332 
     309 
    333310        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    334311                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    344321                                        output << "]";
    345322                                        break;
    346 
     323             
    347324                                  case OT_CALL:
    348325                                        assert( false );
    349 
    350                                         case OT_CTOR:
    351                                         case OT_DTOR:
    352                                         // intrinsic constructors should never be called
    353                                         // intrinsic destructors do nothing
    354                                         break;
    355 
     326                                        break;
     327             
    356328                                  case OT_PREFIX:
    357329                                  case OT_PREFIXASSIGN:
     
    363335                                        output << ")";
    364336                                        break;
    365 
     337             
    366338                                  case OT_POSTFIX:
    367339                                  case OT_POSTFIXASSIGN:
     
    370342                                        output << opInfo.symbol;
    371343                                        break;
    372 
     344 
    373345                                  case OT_INFIX:
    374346                                  case OT_INFIXASSIGN:
     
    380352                                        output << ")";
    381353                                        break;
    382 
     354                                       
    383355                                  case OT_CONSTANT:
    384356                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    398370                } // if
    399371        }
    400 
     372 
    401373        void CodeGenerator::visit( NameExpr *nameExpr ) {
    402374                OperatorInfo opInfo;
     
    408380                } // if
    409381        }
    410 
     382 
    411383        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    412384                output << "(&";
     
    437409                output << ")";
    438410        }
    439 
     411 
    440412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    441413                assert( false );
    442414        }
    443 
     415 
    444416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    445417                memberExpr->get_aggregate()->accept( *this );
    446418                output << "." << mangleName( memberExpr->get_member() );
    447419        }
    448 
     420 
    449421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    450422                OperatorInfo opInfo;
     
    455427                } // if
    456428        }
    457 
     429 
    458430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    459431                assert( constantExpr->get_constant() );
    460432                constantExpr->get_constant()->accept( *this );
    461433        }
    462 
     434 
    463435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    464436                output << "sizeof(";
     
    493465                output << ")";
    494466        }
    495 
     467 
    496468        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    497469                output << "(";
     
    505477                output << ")";
    506478        }
    507 
     479 
    508480        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    509481                output << "(";
     
    515487                output << ")";
    516488        }
    517 
     489 
    518490        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    519491                output << "(";
     
    523495                output << ")";
    524496        }
    525 
     497 
    526498        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    527 
     499 
    528500        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    529501
     
    556528                        }
    557529                }
    558                 cur_indent -= CodeGenerator::tabsize;
     530                cur_indent -= CodeGenerator::tabsize; 
    559531
    560532                output << indent << "}";
     
    562534
    563535        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    564                 // I don't see why this check is necessary.
    565                 // If this starts to cause problems then put it back in,
     536                // I don't see why this check is necessary. 
     537                // If this starts to cause problems then put it back in, 
    566538                // with an explanation
    567539                assert( exprStmt );
     
    613585                switchStmt->get_condition()->accept( *this );
    614586                output << " ) ";
    615 
     587               
    616588                output << "{" << std::endl;
    617589                cur_indent += CodeGenerator::tabsize;
     
    633605                } // if
    634606                output << ":\n";
    635 
     607               
    636608                std::list<Statement *> sts = caseStmt->get_statements();
    637609
     
    650622                        if ( ! branchStmt->get_target().empty() )
    651623                                output << "goto " << branchStmt->get_target();
    652                         else {
     624                        else { 
    653625                                if ( branchStmt->get_computedTarget() != 0 ) {
    654626                                        output << "goto *";
     
    701673
    702674        void CodeGenerator::visit( ForStmt *forStmt ) {
    703                 // initialization is always hoisted, so don't
    704                 // bother doing anything with that
     675                // initialization is always hoisted, so don't 
     676                // bother doing anything with that 
    705677                output << "for (;";
    706678
     
    726698        void CodeGenerator::visit( DeclStmt *declStmt ) {
    727699                declStmt->get_decl()->accept( *this );
    728 
     700       
    729701                if ( doSemicolon( declStmt->get_decl() ) ) {
    730702                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.