Changes in src/CodeGen/CodeGenerator.cc [0a81c3f:bf2438c]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r0a81c3f rbf2438c 14 14 // 15 15 16 #include <algorithm> 17 #include <iostream> 18 #include <cassert> 19 #include <list> 20 21 #include "Parser/ParseNode.h" 22 23 #include "SynTree/Declaration.h" 24 #include "SynTree/Expression.h" 25 #include "SynTree/Initializer.h" 26 #include "SynTree/Statement.h" 27 #include "SynTree/Type.h" 28 #include "SynTree/Attribute.h" 29 30 #include "Common/utility.h" 31 #include "Common/UnimplementedError.h" 16 #include <cassert> // for assert, assertf 17 #include <list> // for _List_iterator, list, list<>::it... 32 18 33 19 #include "CodeGenerator.h" 34 #include "OperatorTable.h" 35 #include "GenType.h" 36 37 #include "InitTweak/InitTweak.h" 20 #include "Common/SemanticError.h" // for SemanticError 21 #include "Common/UniqueName.h" // for UniqueName 22 #include "Common/utility.h" // for CodeLocation, toString 23 #include "GenType.h" // for genType 24 #include "InitTweak/InitTweak.h" // for getPointerBase 25 #include "OperatorTable.h" // for OperatorInfo, operatorLookup 26 #include "Parser/LinkageSpec.h" // for Spec, Intrinsic 27 #include "SynTree/Attribute.h" // for Attribute 28 #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode 29 #include "SynTree/Constant.h" // for Constant 30 #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl 31 #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica... 32 #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation 33 #include "SynTree/Label.h" // for Label, operator<< 34 #include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt 35 #include "SynTree/Type.h" // for Type, Type::StorageClasses, Func... 38 36 39 37 using namespace std; … … 65 63 } // if 66 64 } // extension 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 } 67 73 68 74 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { … … 103 109 } 104 110 105 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 ) {}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 ) {} 106 112 107 113 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 194 200 output << " {" << endl; 195 201 196 ++indent;202 cur_indent += CodeGenerator::tabsize; 197 203 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 198 204 output << lineDirective( *i ) << indent; … … 201 207 } // for 202 208 203 --indent;209 cur_indent -= CodeGenerator::tabsize; 204 210 205 211 output << indent << "}"; … … 231 237 output << " {" << endl; 232 238 233 ++indent;239 cur_indent += CodeGenerator::tabsize; 234 240 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 235 241 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); … … 243 249 } // for 244 250 245 --indent;251 cur_indent -= CodeGenerator::tabsize; 246 252 247 253 output << indent << "}"; … … 338 344 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 339 345 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 340 377 switch ( opInfo.type ) { 341 378 case OT_INDEX: … … 716 753 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 717 754 output << lineDirective( stmtExpr) << "({" << std::endl; 718 ++indent;755 cur_indent += CodeGenerator::tabsize; 719 756 unsigned int numStmts = stmts.size(); 720 757 unsigned int i = 0; … … 739 776 ++i; 740 777 } 741 --indent;778 cur_indent -= CodeGenerator::tabsize; 742 779 output << indent << "})"; 743 780 } … … 748 785 output << "{" << endl; 749 786 750 ++indent;787 cur_indent += CodeGenerator::tabsize; 751 788 752 789 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { … … 759 796 } // if 760 797 } // for 761 --indent;798 cur_indent -= CodeGenerator::tabsize; 762 799 763 800 output << indent << "}"; … … 827 864 828 865 output << "{" << std::endl; 829 ++indent;866 cur_indent += CodeGenerator::tabsize; 830 867 acceptAll( switchStmt->get_statements(), *this ); 831 --indent;868 cur_indent -= CodeGenerator::tabsize; 832 869 output << indent << "}"; 833 870 } … … 846 883 std::list<Statement *> sts = caseStmt->get_statements(); 847 884 848 ++indent;885 cur_indent += CodeGenerator::tabsize; 849 886 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 850 887 output << indent << printLabels( (*i)->get_labels() ) ; … … 852 889 output << endl; 853 890 } // for 854 --indent;891 cur_indent -= CodeGenerator::tabsize; 855 892 } 856 893
Note:
See TracChangeset
for help on using the changeset viewer.