Changeset 0c577f7
- Timestamp:
- Oct 29, 2021, 4:03:07 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- 8e48fca4
- Parents:
- f42fc13
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/FixMain.cc
rf42fc13 r0c577f7 22 22 #include <string> // for operator<< 23 23 24 #include "AST/Decl.hpp" 25 #include "AST/Type.hpp" 24 26 #include "Common/PassVisitor.h" 25 27 #include "Common/SemanticError.h" // for SemanticError … … 105 107 } 106 108 109 bool is_main( const std::string & mangled_name ) { 110 // This breaks if you move it out of the function. 111 static const std::string mangled_mains[] = { 112 mangled_0_argument_main(), 113 mangled_2_argument_main(), 114 //mangled_3_argument_main(), 115 }; 116 117 for ( auto main_name : mangled_mains ) { 118 if ( main_name == mangled_name ) return true; 119 } 120 return false; 121 } 122 107 123 struct FindMainCore { 108 124 void previsit( FunctionDecl * decl ) { … … 116 132 117 133 bool FixMain::isMain( FunctionDecl * decl ) { 118 // This breaks if you move it out of the function.119 static const std::string mangled_mains[] = {120 mangled_0_argument_main(),121 mangled_2_argument_main(),122 //mangled_3_argument_main(),123 };124 125 134 if ( std::string("main") != decl->name ) { 126 135 return false; 127 136 } 128 auto mangled_name = SymTab::Mangler::mangle( decl, true, true ); 129 for ( auto main_name : mangled_mains ) { 130 if ( main_name == mangled_name ) return true; 137 return is_main( SymTab::Mangler::mangle( decl, true, true ) ); 138 } 139 140 bool FixMain::isMain( const ast::FunctionDecl * decl ) { 141 if ( std::string("main") != decl->name ) { 142 return false; 131 143 } 132 return false;144 return is_main( Mangle::mangle( decl, Mangle::Type ) ); 133 145 } 134 146 -
src/CodeGen/FixMain.h
rf42fc13 r0c577f7 10 10 // Created On : Thr Jan 12 14:11:09 2017 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Oct 29 1 1:07:00 202113 // Update Count : 612 // Last Modified On : Fri Oct 29 14:49:00 2021 13 // Update Count : 7 14 14 // 15 15 … … 24 24 class Declaration; 25 25 class FunctionDecl; 26 namespace ast { 27 class FunctionDecl; 28 } 26 29 27 30 namespace CodeGen { … … 31 34 return replace_main ? LinkageSpec::Cforall : LinkageSpec::C; 32 35 } 33 36 34 37 static inline void setReplaceMain(bool val) { 35 38 replace_main = val; … … 38 41 static void registerMain(FunctionDecl* val); 39 42 static bool isMain(FunctionDecl* decl); 43 static bool isMain(const ast::FunctionDecl * decl); 40 44 41 45 static void fix(std::ostream &os, const char* bootloader_filename); -
src/CodeGen/FixNames.cc
rf42fc13 r0c577f7 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Oct 29 1 4:06:00 202113 // Update Count : 2 212 // Last Modified On : Fri Oct 29 15:49:00 2021 13 // Update Count : 23 14 14 // 15 15 … … 19 19 #include <string> // for string, operator!=, operator== 20 20 21 #include "AST/Chain.hpp" 22 #include "AST/Expr.hpp" 23 #include "AST/Pass.hpp" 21 24 #include "Common/PassVisitor.h" 22 25 #include "Common/SemanticError.h" // for SemanticError … … 82 85 GuardAction( [this](){ scopeLevel--; } ); 83 86 } 87 88 /// Does work with the main function and scopeLevels. 89 class FixNames_new : public ast::WithGuards { 90 int scopeLevel = 1; 91 92 bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) { 93 return !dwt->name.empty() && dwt->linkage.is_mangled 94 && dwt->scopeLevel != scopeLevel; 95 } 96 public: 97 const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) { 98 if ( shouldSetScopeLevel( objectDecl ) ) { 99 return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel ); 100 } 101 return objectDecl; 102 } 103 104 const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) { 105 // This store is used to ensure a maximum of one call to mutate. 106 ast::FunctionDecl * mutDecl = nullptr; 107 108 if ( shouldSetScopeLevel( functionDecl ) ) { 109 mutDecl = ast::mutate( functionDecl ); 110 mutDecl->scopeLevel = scopeLevel; 111 } 112 113 if ( FixMain::isMain( functionDecl ) ) { 114 if ( !mutDecl ) { mutDecl = ast::mutate( functionDecl ); } 115 116 int nargs = mutDecl->params.size(); 117 if ( 0 != nargs && 2 != nargs && 3 != nargs ) { 118 SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" ); 119 } 120 ast::chain_mutate( mutDecl->stmts )->kids.push_back( 121 new ast::ReturnStmt( 122 mutDecl->location, 123 ast::ConstantExpr::from_int( mutDecl->location, 0 ) 124 ) 125 ); 126 } 127 return mutDecl ? mutDecl : functionDecl; 128 } 129 130 void previsit( const ast::CompoundStmt * ) { 131 GuardValue( scopeLevel ) += 1; 132 } 133 }; 134 135 void fixNames( ast::TranslationUnit & translationUnit ) { 136 ast::Pass<FixNames_new>::run( translationUnit ); 137 } 138 84 139 } // namespace CodeGen 85 140 -
src/CodeGen/FixNames.h
rf42fc13 r0c577f7 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 : Fri Jul 21 22:17:33 201713 // Update Count : 311 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Oct 26 13:47:00 2021 13 // Update Count : 4 14 14 // 15 15 … … 19 19 20 20 class Declaration; 21 namespace ast { 22 struct TranslationUnit; 23 } 21 24 22 25 namespace CodeGen { 23 26 /// mangles object and function names 24 27 void fixNames( std::list< Declaration* > & translationUnit ); 28 void fixNames( ast::TranslationUnit & translationUnit ); 25 29 } // namespace CodeGen 26 30 -
src/main.cc
rf42fc13 r0c577f7 335 335 PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) ); 336 336 PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); 337 PASS( "Fix Names", CodeGen::fixNames( translationUnit ) );338 337 339 338 CodeTools::fillLocations( translationUnit ); … … 348 347 forceFillCodeLocations( transUnit ); 349 348 349 PASS( "Fix Names", CodeGen::fixNames( transUnit ) ); 350 350 PASS( "Gen Init", InitTweak::genInit( transUnit ) ); 351 351 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( transUnit ) ); … … 383 383 translationUnit = convert( move( transUnit ) ); 384 384 } else { 385 PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); 385 386 PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); 386 387 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) );
Note: See TracChangeset
for help on using the changeset viewer.