Changes in / [de62360d:94e0864d]
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rde62360d r94e0864d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Jun 23 16:16:57201513 // Update Count : 1 3311 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:11:41 2015 13 // Update Count : 143 14 14 // 15 15 … … 44 44 } 45 45 46 CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { } 47 48 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp ) 49 : cur_indent( indent ), insideFunction( infunp ), output( os ) { 46 ostream & CodeGenerator::Indenter::operator()( ostream & output ) { 47 return output << string( cg.cur_indent, ' ' ); 48 } 49 50 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) { 51 return indent( output ); 52 } 53 54 CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { } 55 56 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp ) 57 : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) { 50 58 //output << std::string( init ); 51 59 } 52 60 53 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent , bool infunp )54 : cur_indent( indent), insideFunction( infunp ), output( os ) {61 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp ) 62 : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) { 55 63 //output << std::string( init ); 56 64 } … … 63 71 } // if 64 72 } 65 73 66 74 //*** Declarations 67 75 void CodeGenerator::visit( FunctionDecl *functionDecl ) { … … 108 116 109 117 if ( ! memb.empty() ) { 110 output << endl << string( cur_indent, ' ' ) << "{" << endl;118 output << " {" << endl; 111 119 112 120 cur_indent += CodeGenerator::tabsize; 113 121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 114 output << string( cur_indent, ' ' );122 output << indent; 115 123 (*i)->accept(*this ); 116 124 output << ";" << endl; … … 119 127 cur_indent -= CodeGenerator::tabsize; 120 128 121 output << string( cur_indent, ' ' )<< "}";129 output << indent << "}"; 122 130 } // if 123 131 } … … 142 150 143 151 if ( ! memb.empty() ) { 144 output << endl << "{" << endl;152 output << " {" << endl; 145 153 146 154 cur_indent += CodeGenerator::tabsize; … … 148 156 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 149 157 assert( obj ); 150 output << string( cur_indent, ' ' )<< mangleName( obj );158 output << indent << mangleName( obj ); 151 159 if ( obj->get_init() ) { 152 160 output << " = "; … … 158 166 cur_indent -= CodeGenerator::tabsize; 159 167 160 output << "}" << endl;168 output << indent << "}"; 161 169 } // if 162 170 } … … 449 457 450 458 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { 451 output << string( cur_indent, ' ' )<< printLabels( (*i)->get_labels() );459 output << indent << printLabels( (*i)->get_labels() ); 452 460 (*i)->accept(*this ); 453 461 … … 459 467 cur_indent -= CodeGenerator::tabsize; 460 468 461 output << string( cur_indent, ' ' )<< "}";469 output << indent << "}"; 462 470 } 463 471 … … 499 507 cur_indent -= CodeGenerator::tabsize; 500 508 501 output << string( cur_indent, ' ' )<< "}";509 output << indent << "}"; 502 510 } 503 511 504 512 void CodeGenerator::visit( CaseStmt *caseStmt ) { 505 output << string( cur_indent, ' ' );513 output << indent; 506 514 if ( caseStmt->isDefault()) { 507 515 output << "default"; … … 516 524 cur_indent += CodeGenerator::tabsize; 517 525 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 518 output << string( cur_indent, ' ' )<< printLabels( (*i)->get_labels() ) ;526 output << indent << printLabels( (*i)->get_labels() ) ; 519 527 (*i)->accept(*this ); 520 528 output << endl; … … 569 577 whileStmt->get_body()->accept( *this ); 570 578 571 output << string( cur_indent, ' ' );579 output << indent; 572 580 573 581 if ( whileStmt->get_isDoWhile() ) { … … 601 609 602 610 void CodeGenerator::visit( NullStmt *nullStmt ) { 603 //output << string( cur_indent, ' ' )<< CodeGenerator::printLabels( nullStmt->get_labels() );611 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 604 612 output << "/* null statement */ ;"; 605 613 } -
src/CodeGen/CodeGenerator.h
rde62360d r94e0864d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon Jun 8 14:34:43 201513 // Update Count : 1511 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Jun 11 13:24:23 2015 13 // Update Count : 23 14 14 // 15 15 … … 80 80 81 81 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 82 83 struct Indenter { 84 Indenter(CodeGenerator &cg) : cg(cg) {} 85 CodeGenerator & cg; 86 std::ostream& operator()(std::ostream & os); 87 }; 82 88 private: 89 90 Indenter indent; 83 91 int cur_indent; 84 92 bool insideFunction; -
src/Parser/ExpressionNode.cc
rde62360d r94e0864d 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Jun 23 17:46:23201513 // Update Count : 15 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:10:47 2015 13 // Update Count : 154 14 14 // 15 15 … … 252 252 new ConstantExpr( 253 253 Constant( new BasicType( q, BasicType::UnsignedInt ), 254 toString( value.size() + 1 ) ) ), // account for '\0'254 toString( value.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' 255 255 false, false ); 256 256 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) ); -
src/ResolvExpr/Resolver.cc
rde62360d r94e0864d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:17:01 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed Jun 24 1 5:47:16201513 // Update Count : 5011 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:08:49 2015 13 // Update Count : 155 14 14 // 15 15 … … 38 38 virtual void visit( TypeDecl *typeDecl ); 39 39 40 virtual void visit( ArrayType * at ); 41 40 42 virtual void visit( ExprStmt *exprStmt ); 41 43 virtual void visit( IfStmt *ifStmt ); … … 51 53 virtual void visit( ListInit *listInit ); 52 54 private: 55 typedef std::list< Initializer * >::iterator InitIterator; 56 57 void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); 58 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); 59 53 60 std::list< Type * > functionReturn; 54 61 Type *initContext; … … 158 165 initContext = new_type; 159 166 SymTab::Indexer::visit( objectDecl ); 160 161 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){ 162 if ( at->get_dimension() ) { 163 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 164 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 165 Expression *newExpr = findSingleExpression( castExpr, *this ); 166 delete at->get_dimension(); 167 at->set_dimension( newExpr ); 168 } 169 } 170 } 171 167 } 168 169 void Resolver::visit( ArrayType * at ) { 170 if ( at->get_dimension() ) { 171 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 172 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 173 Expression *newExpr = findSingleExpression( castExpr, *this ); 174 delete at->get_dimension(); 175 at->set_dimension( newExpr ); 176 } 177 Visitor::visit( at ); 178 } 179 172 180 void Resolver::visit( TypeDecl *typeDecl ) { 173 181 if ( typeDecl->get_base() ) { … … 177 185 SymTab::Indexer::visit( typeDecl ); 178 186 } 179 187 180 188 void Resolver::visit( FunctionDecl *functionDecl ) { 181 189 #if 0 … … 284 292 returnStmt->set_expr( newExpr ); 285 293 } // if 294 } 295 296 template< typename T > 297 bool isCharType( T t ) { 298 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { 299 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || 300 bt->get_kind() == BasicType::UnsignedChar; 301 } 302 return false; 286 303 } 287 304 … … 310 327 delete castExpr; 311 328 singleInit->set_value( newExpr ); 329 330 // check if initializing type is char[] 331 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 332 if ( isCharType( at->get_base() ) ) { 333 // check if the resolved type is char * 334 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { 335 if ( isCharType( pt->get_base() ) ) { 336 // strip cast if we're initializing a char[] with a char *, e.g. 337 // char x[] = "hello"; 338 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); 339 singleInit->set_value( ce->get_arg() ); 340 ce->set_arg( NULL ); 341 delete ce; 342 } 343 } 344 } 345 } 312 346 } // if 313 347 // singleInit->get_value()->accept( *this ); 314 348 } 315 349 316 void Resolver::visit( ListInit *listInit ) { 317 Visitor::visit(listInit); 350 void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { 351 DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); 352 assert( dt ); 353 initContext = dt->get_type(); 354 try { 355 if ( init == initEnd ) return; // stop when there are no more initializers 356 (*init)->accept( *this ); 357 ++init; // made it past an initializer 358 } catch( SemanticError & ) { 359 // need to delve deeper, if you can 360 if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { 361 resolveAggrInit( sit->get_baseStruct(), init, initEnd ); 362 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { 363 resolveAggrInit( uit->get_baseUnion(), init, initEnd ); 364 } else { 365 // might need to rethink what is being thrown 366 throw; 367 } // if 368 } 369 } 370 371 void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { 372 if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { 373 // want to resolve each initializer to the members of the struct, 374 // but if there are more initializers than members we should stop 375 list< Declaration * >::iterator it = st->get_members().begin(); 376 for ( ; it != st->get_members().end(); ++it) { 377 resolveSingleAggrInit( *it, init, initEnd ); 378 } 379 } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { 380 // only resolve to the first member of a union 381 resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); 382 } // if 383 } 384 385 void Resolver::visit( ListInit * listInit ) { 386 InitIterator iter = listInit->begin_initializers(); 387 InitIterator end = listInit->end_initializers(); 388 389 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 390 // resolve each member to the base type of the array 391 for ( ; iter != end; ++iter ) { 392 initContext = at->get_base(); 393 (*iter)->accept( *this ); 394 } // for 395 } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { 396 resolveAggrInit( st->get_baseStruct(), iter, end ); 397 } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { 398 resolveAggrInit( st->get_baseUnion(), iter, end ); 399 } else { 400 // basic types are handled here 401 Visitor::visit( listInit ); 402 } 403 318 404 #if 0 319 405 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { -
src/SynTree/Constant.cc
rde62360d r94e0864d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Jun 7 08:45:30201513 // Update Count : 511 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 10 14:41:03 2015 13 // Update Count : 8 14 14 // 15 15 … … 27 27 28 28 void Constant::print( std::ostream &os ) const { 29 os << value;29 os << "(" << value; 30 30 if ( type ) { 31 os << " ";31 os << ": "; 32 32 type->print( os ); 33 33 } // if 34 os << ")"; 34 35 } 35 36
Note:
See TracChangeset
for help on using the changeset viewer.