Changeset 8d182b1 for src/CodeGen/FixMain.cc
- Timestamp:
- Nov 14, 2023, 12:19:09 PM (23 months ago)
- Branches:
- master
- Children:
- 1ccae59, 89a8bab
- Parents:
- df8ba61a (diff), 5625427 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/FixMain.cc
rdf8ba61a r8d182b1 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // FixMain.cc -- 7 // FixMain.cc -- Tools to change a Cforall main into a C main. 8 8 // 9 9 // Author : Thierry Delisle … … 25 25 #include "AST/Type.hpp" 26 26 #include "AST/Vector.hpp" 27 #include "Common/PassVisitor.h"28 27 #include "Common/SemanticError.h" // for SemanticError 29 28 #include "CodeGen/GenType.h" // for GenType 30 #include "SynTree/Declaration.h" // for FunctionDecl, operator<<31 #include "SynTree/Type.h" // for FunctionType32 29 #include "SymTab/Mangler.h" 33 30 … … 36 33 namespace { 37 34 38 struct FindMainCore { 39 FunctionDecl * main_signature = nullptr; 40 41 void previsit( FunctionDecl * decl ) { 42 if ( FixMain::isMain( decl ) ) { 43 if ( main_signature ) { 44 SemanticError( decl, "Multiple definition of main routine\n" ); 45 } 46 main_signature = decl; 47 } 48 } 49 }; 50 51 struct FindMainCore_new { 35 struct FindMainCore final { 52 36 ast::FunctionDecl const * main_declaration = nullptr; 53 37 54 38 void previsit( ast::FunctionDecl const * decl ) { 55 if ( FixMain::isMain( decl ) ) {39 if ( isMain( decl ) ) { 56 40 if ( main_declaration ) { 57 41 SemanticError( decl, "Multiple definition of main routine\n" ); … … 65 49 return genType( types[at], "", Options( false, false, false, false ) ); 66 50 } 67 68 }69 70 template<typename container>71 std::string genTypeAt(const container& p, size_t idx) {72 return genType((*std::next(p.begin(), idx))->get_type(), "");73 }74 75 void FixMain::fix( std::list< Declaration * > & translationUnit,76 std::ostream &os, const char* bootloader_filename ) {77 PassVisitor< FindMainCore > main_finder;78 acceptAll( translationUnit, main_finder );79 FunctionDecl * main_signature = main_finder.pass.main_signature;80 81 if( main_signature ) {82 os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";83 main_signature->mangleName = SymTab::Mangler::mangle(main_signature);84 85 os << main_signature->get_scopedMangleName() << "(";86 const auto& params = main_signature->get_functionType()->get_parameters();87 switch(params.size()) {88 case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;89 case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;90 case 0: break;91 default : assert(false);92 }93 os << "); }\n";94 95 std::ifstream bootloader(bootloader_filename, std::ios::in);96 assertf( bootloader.is_open(), "cannot open bootloader.c\n" );97 os << bootloader.rdbuf();98 }99 }100 101 namespace {102 51 103 52 ast::ObjectDecl * makeIntObj(){ … … 157 106 } 158 107 108 struct FixLinkageCore final { 109 ast::Linkage::Spec const spec; 110 FixLinkageCore( ast::Linkage::Spec spec ) : spec( spec ) {} 111 112 ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ) { 113 if ( decl->name != "main" ) return decl; 114 return ast::mutate_field( decl, &ast::FunctionDecl::linkage, spec ); 115 } 116 }; 117 159 118 } // namespace 160 119 161 bool FixMain::isMain( FunctionDecl * decl ) { 162 if ( std::string("main") != decl->name ) { 163 return false; 164 } 165 return is_main( SymTab::Mangler::mangle( decl, true, true ) ); 166 } 167 168 bool FixMain::isMain( const ast::FunctionDecl * decl ) { 120 bool isMain( const ast::FunctionDecl * decl ) { 169 121 if ( std::string("main") != decl->name ) { 170 122 return false; … … 173 125 } 174 126 175 void FixMain::fix( ast::TranslationUnit & translationUnit, 127 void fixMainLinkage( ast::TranslationUnit & translationUnit, 128 bool replace_main ) { 129 ast::Linkage::Spec const spec = 130 ( replace_main ) ? ast::Linkage::Cforall : ast::Linkage::C; 131 ast::Pass<FixLinkageCore>::run( translationUnit, spec ); 132 } 133 134 void fixMainInvoke( ast::TranslationUnit & translationUnit, 176 135 std::ostream &os, const char * bootloader_filename ) { 177 136 178 ast::Pass<FindMainCore _new> main_finder;137 ast::Pass<FindMainCore> main_finder; 179 138 ast::accept_all( translationUnit, main_finder ); 180 139 if ( nullptr == main_finder.core.main_declaration ) return;
Note:
See TracChangeset
for help on using the changeset viewer.