Changes in src/CodeGen/CodeGenerator.cc [a5f0529:8a6cf7e]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
ra5f0529 r8a6cf7e 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : T us Jul 25 15:29:00 201713 // Update Count : 48 612 // Last Modified On : Thu Jun 8 16:00:00 2017 13 // Update Count : 485 14 14 // 15 #include "CodeGenerator.h"16 15 17 16 #include <cassert> // for assert, assertf 18 17 #include <list> // for _List_iterator, list, list<>::it... 19 18 19 #include "CodeGenerator.h" 20 20 #include "Common/SemanticError.h" // for SemanticError 21 21 #include "Common/UniqueName.h" // for UniqueName … … 64 64 } // extension 65 65 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 74 66 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { 75 67 labels = &l; … … 109 101 } 110 102 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 ) {} 112 104 113 105 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 190 182 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() ); 191 183 output << ")" << endl; 184 output << indent; 192 185 } 193 186 … … 200 193 output << " {" << endl; 201 194 202 cur_indent += CodeGenerator::tabsize;195 ++indent; 203 196 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 204 197 output << lineDirective( *i ) << indent; … … 207 200 } // for 208 201 209 cur_indent -= CodeGenerator::tabsize;202 --indent; 210 203 211 204 output << indent << "}"; … … 237 230 output << " {" << endl; 238 231 239 cur_indent += CodeGenerator::tabsize;232 ++indent; 240 233 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 241 234 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); … … 249 242 } // for 250 243 251 cur_indent -= CodeGenerator::tabsize;244 --indent; 252 245 253 246 output << indent << "}"; … … 329 322 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){ 330 323 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 << "}"; 333 330 } 334 331 … … 344 341 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 345 342 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 argument356 *arg = addrExpr->get_arg();357 } else {358 // no address-of operator, so must be a pointer - add dereference359 // 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 application361 // 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 } // if369 break;370 }371 372 default:373 // do nothing374 ;375 } // switch376 377 343 switch ( opInfo.type ) { 378 344 case OT_INDEX: … … 594 560 } 595 561 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 604 562 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 605 563 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); … … 703 661 extension( commaExpr ); 704 662 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 } 705 667 commaExpr->get_arg1()->accept( *this ); 706 668 output << " , "; … … 761 723 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 762 724 output << lineDirective( stmtExpr) << "({" << std::endl; 763 cur_indent += CodeGenerator::tabsize;725 ++indent; 764 726 unsigned int numStmts = stmts.size(); 765 727 unsigned int i = 0; 766 728 for ( Statement * stmt : stmts ) { 767 729 output << lineDirective( stmt ) << indent; 768 output << printLabels( stmt->get_labels() );730 output << printLabels( stmt->get_labels() ); 769 731 if ( i+1 == numStmts ) { 770 732 // last statement in a statement expression needs to be handled specially - … … 784 746 ++i; 785 747 } 786 cur_indent -= CodeGenerator::tabsize;748 --indent; 787 749 output << indent << "})"; 788 750 } … … 793 755 output << "{" << endl; 794 756 795 cur_indent += CodeGenerator::tabsize;757 ++indent; 796 758 797 759 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { … … 804 766 } // if 805 767 } // for 806 cur_indent -= CodeGenerator::tabsize;768 --indent; 807 769 808 770 output << indent << "}"; … … 811 773 void CodeGenerator::visit( ExprStmt * exprStmt ) { 812 774 assert( exprStmt ); 813 Expression * expr = exprStmt->get_expr();814 775 if ( genC ) { 815 776 // 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 ); 819 780 output << ";"; 820 781 } … … 872 833 873 834 output << "{" << std::endl; 874 cur_indent += CodeGenerator::tabsize;835 ++indent; 875 836 acceptAll( switchStmt->get_statements(), *this ); 876 cur_indent -= CodeGenerator::tabsize;837 --indent; 877 838 output << indent << "}"; 878 839 } … … 891 852 std::list<Statement *> sts = caseStmt->get_statements(); 892 853 893 cur_indent += CodeGenerator::tabsize;854 ++indent; 894 855 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 895 856 output << indent << printLabels( (*i)->get_labels() ) ; … … 897 858 output << endl; 898 859 } // for 899 cur_indent -= CodeGenerator::tabsize;860 --indent; 900 861 } 901 862
Note:
See TracChangeset
for help on using the changeset viewer.