Changeset f42fc13 for src/CodeGen
- Timestamp:
- Oct 29, 2021, 2:17:42 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- 0c577f7
- Parents:
- eb9c2dc
- Location:
- src/CodeGen
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/FixMain.cc
reb9c2dc rf42fc13 22 22 #include <string> // for operator<< 23 23 24 #include "Common/PassVisitor.h" 24 25 #include "Common/SemanticError.h" // for SemanticError 25 26 #include "CodeGen/GenType.h" // for GenType … … 65 66 } 66 67 } 68 69 namespace { 70 71 ObjectDecl * signedIntObj() { 72 return new ObjectDecl( 73 "", Type::StorageClasses(), LinkageSpec::Cforall, 0, 74 new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ); 75 } 76 77 ObjectDecl * charStarObj() { 78 return new ObjectDecl( 79 "", Type::StorageClasses(), LinkageSpec::Cforall, 0, 80 new PointerType( Type::Qualifiers(), 81 new PointerType( Type::Qualifiers(), 82 new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), 83 nullptr ); 84 } 85 86 std::string create_mangled_main_function_name( FunctionType * function_type ) { 87 std::unique_ptr<FunctionDecl> decl( new FunctionDecl( 88 "main", Type::StorageClasses(), LinkageSpec::Cforall, 89 function_type, nullptr ) ); 90 return SymTab::Mangler::mangle( decl.get() ); 91 } 92 93 std::string mangled_0_argument_main() { 94 FunctionType* main_type = new FunctionType( Type::Qualifiers(), true ); 95 main_type->get_returnVals().push_back( signedIntObj() ); 96 return create_mangled_main_function_name( main_type ); 97 } 98 99 std::string mangled_2_argument_main() { 100 FunctionType* main_type = new FunctionType( Type::Qualifiers(), false ); 101 main_type->get_returnVals().push_back( signedIntObj() ); 102 main_type->get_parameters().push_back( signedIntObj() ); 103 main_type->get_parameters().push_back( charStarObj() ); 104 return create_mangled_main_function_name( main_type ); 105 } 106 107 struct FindMainCore { 108 void previsit( FunctionDecl * decl ) { 109 if ( FixMain::isMain( decl ) ) { 110 FixMain::registerMain( decl ); 111 } 112 } 67 113 }; 114 115 } // namespace 116 117 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 if ( std::string("main") != decl->name ) { 126 return false; 127 } 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; 131 } 132 return false; 133 } 134 135 void FixMain::findMain( std::list< Declaration * > & translationUnit ) { 136 PassVisitor< FindMainCore > mainFinder; 137 acceptAll( translationUnit, mainFinder ); 138 } 139 140 }; -
src/CodeGen/FixMain.h
reb9c2dc rf42fc13 9 9 // Author : Thierry Delisle 10 10 // Created On : Thr Jan 12 14:11:09 2017 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Feb 16 03:24:32 202013 // Update Count : 511 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Oct 29 11:07:00 2021 13 // Update Count : 6 14 14 // 15 15 … … 18 18 #include <iosfwd> 19 19 #include <memory> 20 #include <list> 20 21 21 22 #include "SynTree/LinkageSpec.h" 22 23 24 class Declaration; 23 25 class FunctionDecl; 24 26 … … 35 37 36 38 static void registerMain(FunctionDecl* val); 39 static bool isMain(FunctionDecl* decl); 37 40 38 41 static void fix(std::ostream &os, const char* bootloader_filename); 42 43 static void findMain( std::list< Declaration * > & decls ); 39 44 40 45 private: -
src/CodeGen/FixNames.cc
reb9c2dc rf42fc13 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 Dec 13 23:39:14 201913 // Update Count : 2 111 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Oct 29 14:06:00 2021 13 // Update Count : 22 14 14 // 15 15 … … 46 46 }; 47 47 48 std::string mangle_main() {49 FunctionType* main_type;50 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,51 main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )52 };53 main_type->get_returnVals().push_back(54 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )55 );56 57 auto && name = SymTab::Mangler::mangle( mainDecl.get() );58 // std::cerr << name << std::endl;59 return std::move(name);60 }61 std::string mangle_main_args() {62 FunctionType* main_type;63 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,64 main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )65 };66 main_type->get_returnVals().push_back(67 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )68 );69 70 main_type->get_parameters().push_back(71 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )72 );73 74 main_type->get_parameters().push_back(75 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0,76 new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),77 nullptr )78 );79 80 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );81 // std::cerr << name << std::endl;82 return std::move(name);83 }84 85 bool is_main(const std::string& name) {86 static std::string mains[] = {87 mangle_main(),88 mangle_main_args()89 };90 91 for(const auto& m : mains) {92 if( name == m ) return true;93 }94 return false;95 }96 97 48 void fixNames( std::list< Declaration* > & translationUnit ) { 98 49 PassVisitor<FixNames> fixer; … … 118 69 fixDWT( functionDecl ); 119 70 120 if (is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {71 if ( FixMain::isMain( functionDecl ) ) { 121 72 int nargs = functionDecl->get_functionType()->get_parameters().size(); 122 73 if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { … … 124 75 } 125 76 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); 126 CodeGen::FixMain::registerMain( functionDecl );127 77 } 128 78 }
Note: See TracChangeset
for help on using the changeset viewer.