Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    re8032b0 r5b2f5bb  
    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 : Peter A. Buhr
    12 // Last Modified On : Wed Mar  2 17:32:16 2016
    13 // Update Count     : 243
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Mar 30 14:39:30 2016
     13// Update Count     : 255
    1414//
    1515
     
    2121#include "Parser/ParseNode.h"
    2222
    23 #include "SynTree/Declaration.h"
     23#include "SynTree/Type.h"
    2424#include "SynTree/Expression.h"
    2525#include "SynTree/Initializer.h"
    2626#include "SynTree/Statement.h"
    27 #include "SynTree/Type.h"
    2827
    2928#include "Common/utility.h"
     
    9998                handleStorageClass( objectDecl );
    10099                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    101        
     100
    102101                if ( objectDecl->get_init() ) {
    103102                        output << " = ";
     
    113112                if ( aggDecl->get_name() != "" )
    114113                        output << aggDecl->get_name();
    115        
     114
    116115                std::list< Declaration * > &memb = aggDecl->get_members();
    117116
     
    119118                        output << " {" << endl;
    120119
    121                         cur_indent += CodeGenerator::tabsize; 
     120                        cur_indent += CodeGenerator::tabsize;
    122121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    123                                 output << indent; 
     122                                output << indent;
    124123                                (*i)->accept( *this );
    125124                                output << ";" << endl;
    126125                        }
    127126
    128                         cur_indent -= CodeGenerator::tabsize; 
     127                        cur_indent -= CodeGenerator::tabsize;
    129128
    130129                        output << indent << "}";
     
    141140                handleAggregate( aggregateDecl );
    142141        }
    143  
     142
    144143        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    145144                output << "enum ";
     
    147146                if ( aggDecl->get_name() != "" )
    148147                        output << aggDecl->get_name();
    149        
     148
    150149                std::list< Declaration* > &memb = aggDecl->get_members();
    151150
     
    153152                        output << " {" << endl;
    154153
    155                         cur_indent += CodeGenerator::tabsize; 
     154                        cur_indent += CodeGenerator::tabsize;
    156155                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    157156                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    158157                                assert( obj );
    159                                 output << indent << mangleName( obj ); 
     158                                output << indent << mangleName( obj );
    160159                                if ( obj->get_init() ) {
    161160                                        output << " = ";
     
    165164                        } // for
    166165
    167                         cur_indent -= CodeGenerator::tabsize; 
     166                        cur_indent -= CodeGenerator::tabsize;
    168167
    169168                        output << indent << "}";
    170169                } // if
    171170        }
    172  
    173         void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
    174  
     171
     172        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
     173
    175174        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    176175                output << "typedef ";
    177176                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    178177        }
    179  
     178
    180179        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    181180                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    213212                printDesignators( init->get_designators() );
    214213                output << "{ ";
    215                 genCommaList( init->begin_initializers(), init->end_initializers() );
     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                }
    216221                output << " }";
    217222        }
    218223
    219         void CodeGenerator::visit( Constant *constant ) { 
     224        void CodeGenerator::visit( Constant *constant ) {
    220225                output << constant->get_value() ;
    221226        }
     
    234239                                                assert( arg != applicationExpr->get_args().end() );
    235240                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    236                
     241
    237242                                                        *arg = addrExpr->get_arg();
    238243                                                } else {
     
    243248                                                break;
    244249                                        }
    245              
     250
    246251                                  default:
    247252                                        // do nothing
    248253                                        ;
    249254                                }
    250            
     255
    251256                                switch ( opInfo.type ) {
    252257                                  case OT_INDEX:
     
    257262                                        output << "]";
    258263                                        break;
    259              
     264
    260265                                  case OT_CALL:
    261                                         // there are no intrinsic definitions of the function call operator
     266                                        // there are no intrinsic definitions of the function call operator or constructors or destructors
    262267                                        assert( false );
    263268                                        break;
    264              
     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
    265287                                  case OT_PREFIX:
    266288                                  case OT_PREFIXASSIGN:
     
    271293                                        output << ")";
    272294                                        break;
    273              
     295
    274296                                  case OT_POSTFIX:
    275297                                  case OT_POSTFIXASSIGN:
     
    278300                                        output << opInfo.symbol;
    279301                                        break;
     302
    280303
    281304                                  case OT_INFIX:
     
    288311                                        output << ")";
    289312                                        break;
    290              
     313
    291314                                  case OT_CONSTANT:
    292315                                  case OT_LABELADDRESS:
     
    307330                } // if
    308331        }
    309  
     332
    310333        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    311334                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    321344                                        output << "]";
    322345                                        break;
    323              
     346
    324347                                  case OT_CALL:
    325348                                        assert( false );
    326                                         break;
    327              
     349
     350                                        case OT_CTOR:
     351                                        case OT_DTOR:
     352                                        // intrinsic constructors should never be called
     353                                        // intrinsic destructors do nothing
     354                                        break;
     355
    328356                                  case OT_PREFIX:
    329357                                  case OT_PREFIXASSIGN:
     
    335363                                        output << ")";
    336364                                        break;
    337              
     365
    338366                                  case OT_POSTFIX:
    339367                                  case OT_POSTFIXASSIGN:
     
    342370                                        output << opInfo.symbol;
    343371                                        break;
    344  
     372
    345373                                  case OT_INFIX:
    346374                                  case OT_INFIXASSIGN:
     
    352380                                        output << ")";
    353381                                        break;
    354                                        
     382
    355383                                  case OT_CONSTANT:
    356384                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    370398                } // if
    371399        }
    372  
     400
    373401        void CodeGenerator::visit( NameExpr *nameExpr ) {
    374402                OperatorInfo opInfo;
     
    380408                } // if
    381409        }
    382  
     410
    383411        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    384412                output << "(&";
     
    409437                output << ")";
    410438        }
    411  
     439
    412440        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    413441                assert( false );
    414442        }
    415  
     443
    416444        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    417445                memberExpr->get_aggregate()->accept( *this );
    418446                output << "." << mangleName( memberExpr->get_member() );
    419447        }
    420  
     448
    421449        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    422450                OperatorInfo opInfo;
     
    427455                } // if
    428456        }
    429  
     457
    430458        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    431459                assert( constantExpr->get_constant() );
    432460                constantExpr->get_constant()->accept( *this );
    433461        }
    434  
     462
    435463        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    436464                output << "sizeof(";
     
    465493                output << ")";
    466494        }
    467  
     495
    468496        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    469497                output << "(";
     
    477505                output << ")";
    478506        }
    479  
     507
    480508        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    481509                output << "(";
     
    487515                output << ")";
    488516        }
    489  
     517
    490518        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    491519                output << "(";
     
    495523                output << ")";
    496524        }
    497  
     525
    498526        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    499  
     527
    500528        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    501529
     
    528556                        }
    529557                }
    530                 cur_indent -= CodeGenerator::tabsize; 
     558                cur_indent -= CodeGenerator::tabsize;
    531559
    532560                output << indent << "}";
     
    534562
    535563        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    536                 // I don't see why this check is necessary. 
    537                 // If this starts to cause problems then put it back in, 
     564                // I don't see why this check is necessary.
     565                // If this starts to cause problems then put it back in,
    538566                // with an explanation
    539567                assert( exprStmt );
     
    585613                switchStmt->get_condition()->accept( *this );
    586614                output << " ) ";
    587                
     615
    588616                output << "{" << std::endl;
    589617                cur_indent += CodeGenerator::tabsize;
     
    605633                } // if
    606634                output << ":\n";
    607                
     635
    608636                std::list<Statement *> sts = caseStmt->get_statements();
    609637
     
    622650                        if ( ! branchStmt->get_target().empty() )
    623651                                output << "goto " << branchStmt->get_target();
    624                         else { 
     652                        else {
    625653                                if ( branchStmt->get_computedTarget() != 0 ) {
    626654                                        output << "goto *";
     
    673701
    674702        void CodeGenerator::visit( ForStmt *forStmt ) {
    675                 // initialization is always hoisted, so don't 
    676                 // bother doing anything with that 
     703                // initialization is always hoisted, so don't
     704                // bother doing anything with that
    677705                output << "for (;";
    678706
     
    698726        void CodeGenerator::visit( DeclStmt *declStmt ) {
    699727                declStmt->get_decl()->accept( *this );
    700        
     728
    701729                if ( doSemicolon( declStmt->get_decl() ) ) {
    702730                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.