Changeset 6250a312 for src/CodeGen
- Timestamp:
- May 10, 2017, 5:00:47 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 8514fe19, dbfb35d
- Parents:
- 0f9bef3 (diff), 29cf9c8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/CodeGen
- Files:
-
- 2 added
- 3 edited
-
CodeGenerator.cc (modified) (18 diffs)
-
Generate.cc (modified) (2 diffs)
-
Generate.h (modified) (1 diff)
-
LineStream.cc (added)
-
LineStream.h (added)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r0f9bef3 r6250a312 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 : T hu Mar 30 16:38:01201713 // Update Count : 48 211 // Last Modified By : Andrew Beach 12 // Last Modified On : Tus May 9 16:50:00 2017 13 // Update Count : 484 14 14 // 15 15 … … 41 41 namespace CodeGen { 42 42 int CodeGenerator::tabsize = 4; 43 44 // Pseudo Function: output << lineDirective(currentNode); 45 struct lineDirective { 46 CodeLocation const & loc; 47 lineDirective(CodeLocation const & location) : loc(location) {} 48 lineDirective(BaseSyntaxNode const * node) : loc(node->location) {} 49 }; 50 std::ostream & operator<<(std::ostream & out, lineDirective const & ld) { 51 if (ld.loc.isSet()) 52 return out << "\n# " << ld.loc.linenumber << " \"" 53 << ld.loc.filename << "\"\n"; 54 return out << "\n// Unset Location\n"; 55 } 43 56 44 57 // the kinds of statements that would ideally be followed by whitespace … … 128 141 129 142 130 // *** Declarations143 // *** Declarations 131 144 void CodeGenerator::visit( FunctionDecl * functionDecl ) { 145 output << lineDirective( functionDecl ); 146 132 147 extension( functionDecl ); 133 148 genAttributes( functionDecl->get_attributes() ); … … 153 168 } 154 169 170 output << lineDirective( objectDecl ); 171 155 172 extension( objectDecl ); 156 173 genAttributes( objectDecl->get_attributes() ); … … 192 209 cur_indent += CodeGenerator::tabsize; 193 210 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 194 output << indent;211 output << lineDirective( *i ) << indent; 195 212 (*i)->accept( *this ); 196 213 output << ";" << endl; … … 204 221 205 222 void CodeGenerator::visit( StructDecl * structDecl ) { 223 output << lineDirective( structDecl ); 224 206 225 extension( structDecl ); 207 226 handleAggregate( structDecl, "struct " ); … … 209 228 210 229 void CodeGenerator::visit( UnionDecl * unionDecl ) { 230 output << lineDirective( unionDecl ); 231 211 232 extension( unionDecl ); 212 233 handleAggregate( unionDecl, "union " ); … … 215 236 void CodeGenerator::visit( EnumDecl * enumDecl ) { 216 237 extension( enumDecl ); 238 output << lineDirective ( enumDecl ); 217 239 output << "enum "; 218 240 genAttributes( enumDecl->get_attributes() ); … … 230 252 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 231 253 assert( obj ); 232 output << indent << mangleName( obj );254 output << lineDirective( obj ) << indent << mangleName( obj ); 233 255 if ( obj->get_init() ) { 234 256 output << " = "; … … 248 270 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 249 271 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 272 output << lineDirective( typeDecl ); 250 273 output << "typedef "; 251 274 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; … … 262 285 } // if 263 286 } else { 264 output << typeDecl->typeString() << " " << typeDecl->get_name(); 287 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); 288 if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) { 289 output << " | sized(" << typeDecl->get_name() << ")"; 290 } 265 291 if ( ! typeDecl->get_assertions().empty() ) { 266 292 output << " | { "; … … 316 342 } 317 343 318 // *** Expressions344 // *** Expressions 319 345 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 320 346 extension( applicationExpr ); … … 719 745 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 720 746 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 721 output << "({" << std::endl;747 output << lineDirective( stmtExpr) << "({" << std::endl; 722 748 cur_indent += CodeGenerator::tabsize; 723 749 unsigned int numStmts = stmts.size(); 724 750 unsigned int i = 0; 725 751 for ( Statement * stmt : stmts ) { 726 output << indent << printLabels( stmt->get_labels() ); 752 output << lineDirective( stmt ) << indent; 753 output << printLabels( stmt->get_labels() ); 727 754 if ( i+1 == numStmts ) { 728 755 // last statement in a statement expression needs to be handled specially - … … 746 773 } 747 774 748 // *** Statements775 // *** Statements 749 776 void CodeGenerator::visit( CompoundStmt * compoundStmt ) { 750 777 std::list<Statement*> ks = compoundStmt->get_kids(); … … 769 796 void CodeGenerator::visit( ExprStmt * exprStmt ) { 770 797 assert( exprStmt ); 771 // cast the top-level expression to void to reduce gcc warnings. 772 Expression * expr = new CastExpr( exprStmt->get_expr() ); 798 Expression * expr = exprStmt->get_expr(); 799 if ( genC ) { 800 // cast the top-level expression to void to reduce gcc warnings. 801 expr = new CastExpr( expr ); 802 } 773 803 expr->accept( *this ); 774 804 output << ";"; … … 807 837 808 838 void CodeGenerator::visit( IfStmt * ifStmt ) { 839 output << lineDirective( ifStmt ); 809 840 output << "if ( "; 810 841 ifStmt->get_condition()->accept( *this ); … … 820 851 821 852 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 853 output << lineDirective( switchStmt ); 822 854 output << "switch ( " ; 823 855 switchStmt->get_condition()->accept( *this ); … … 832 864 833 865 void CodeGenerator::visit( CaseStmt * caseStmt ) { 866 output << lineDirective( caseStmt ); 834 867 output << indent; 835 868 if ( caseStmt->isDefault()) { -
src/CodeGen/Generate.cc
r0f9bef3 r6250a312 22 22 #include "SynTree/Declaration.h" 23 23 #include "CodeGenerator.h" 24 #include "Tuples/Tuples.h" 24 #include "GenType.h" 25 #include "SynTree/SynTree.h" 26 #include "SynTree/Type.h" 27 #include "SynTree/BaseSyntaxNode.h" 28 // #include "Tuples/Tuples.h" 25 29 26 30 using namespace std; … … 39 43 } // for 40 44 } 45 46 void generate( BaseSyntaxNode * node, std::ostream & os ) { 47 if ( Type * type = dynamic_cast< Type * >( node ) ) { 48 os << CodeGen::genPrettyType( type, "" ); 49 } else { 50 CodeGen::CodeGenerator cgv( os, true, false ); 51 node->accept( cgv ); 52 } 53 os << std::endl; 54 } 41 55 } // namespace CodeGen 42 56 -
src/CodeGen/Generate.h
r0f9bef3 r6250a312 25 25 /// Generates code. doIntrinsics determines if intrinsic functions are printed, pretty formats output nicely (e.g., uses unmangled names, etc.), generateC is true when the output must consist only of C code (allows some assertions, etc.) 26 26 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC = false ); 27 28 /// Generate code for a single node -- helpful for debugging in gdb 29 void generate( BaseSyntaxNode * node, std::ostream & os ); 27 30 } // namespace CodeGen 28 31
Note:
See TracChangeset
for help on using the changeset viewer.