- Timestamp:
- Jan 12, 2017, 11:31:57 AM (8 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:
- 3fe34ae
- Parents:
- 075734f
- Location:
- src
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/FixNames.cc
r075734f r0270824 14 14 // 15 15 16 #include <memory> 17 16 18 #include "FixNames.h" 17 19 #include "SynTree/Declaration.h" … … 20 22 #include "SymTab/Mangler.h" 21 23 #include "OperatorTable.h" 24 25 extern std::unique_ptr<FunctionDecl> translation_unit_main_signature; 22 26 23 27 namespace CodeGen { … … 28 32 29 33 virtual void visit( CompoundStmt *compoundStmt ); 30 31 34 private: 32 35 int scopeLevel = 1; … … 34 37 void fixDWT( DeclarationWithType *dwt ); 35 38 }; 39 40 std::string mangle_main() { 41 FunctionType* main_type; 42 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( 43 "main", 44 DeclarationNode::NoStorageClass, 45 LinkageSpec::Cforall, 46 main_type = new FunctionType( Type::Qualifiers(), true ), 47 nullptr, false, false 48 ) }; 49 main_type->get_returnVals().push_back( 50 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) 51 ); 52 53 auto&& name = SymTab::Mangler::mangle( mainDecl.get() ); 54 // std::cerr << name << std::endl; 55 return name; 56 } 57 std::string mangle_main_args() { 58 FunctionType* main_type; 59 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( 60 "main", 61 DeclarationNode::NoStorageClass, 62 LinkageSpec::Cforall, 63 main_type = new FunctionType( Type::Qualifiers(), false ), 64 nullptr, false, false 65 ) }; 66 main_type->get_returnVals().push_back( 67 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) 68 ); 69 70 mainDecl->get_functionType()->get_parameters().push_back( 71 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) 72 ); 73 74 mainDecl->get_functionType()->get_parameters().push_back( 75 new ObjectDecl( "", DeclarationNode::NoStorageClass, 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 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 } 36 96 37 97 void fixNames( std::list< Declaration* > translationUnit ) { … … 57 117 Visitor::visit( functionDecl ); 58 118 fixDWT( functionDecl ); 119 120 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) { 121 if(translation_unit_main_signature) { 122 throw SemanticError("Multiple definition of main routine\n", functionDecl); 123 } 124 125 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) ); 126 translation_unit_main_signature.reset( functionDecl->clone() ); 127 } 59 128 } 60 129 -
src/SymTab/Mangler.h
r075734f r0270824 27 27 /// Mangle syntax tree object; primary interface to clients 28 28 template< typename SynTreeClass > 29 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true );29 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool typeMode = false ); 30 30 /// Mangle a type name; secondary interface 31 31 static std::string mangleType( Type* ty ); … … 70 70 71 71 template< typename SynTreeClass > 72 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable ) {73 Mangler mangler( mangleOverridable, false );72 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool typeMode ) { 73 Mangler mangler( mangleOverridable, typeMode ); 74 74 maybeAccept( decl, mangler ); 75 75 return mangler.get_mangleName(); -
src/SynTree/FunctionDecl.cc
r075734f r0270824 27 27 set_isInline( isInline ); 28 28 set_isNoreturn( isNoreturn ); 29 // this is a brazen hack to force the function "main" to have C linkage 29 // this is a brazen hack to force the function "main" to have Cforall linkage 30 // because we want to replace the main even if it is inside an extern 30 31 if ( name == "main" ) { 31 set_linkage( LinkageSpec::C );32 set_linkage( LinkageSpec::Cforall ); 32 33 } // if 33 34 } -
src/main.cc
r075734f r0270824 14 14 // 15 15 16 #include <memory> 16 17 #include <iostream> 17 18 #include <fstream> … … 78 79 static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); 79 80 81 std::unique_ptr<FunctionDecl> translation_unit_main_signature = nullptr; 82 80 83 static void backtrace( int start ) { // skip first N stack frames 81 84 enum { Frames = 50 }; … … 299 302 300 303 CodeGen::generate( translationUnit, *output, ! noprotop ); 304 305 if( translation_unit_main_signature ) { 306 *output << "int main(int argc, char** argv) { return "; 307 308 *output << translation_unit_main_signature->get_scopedMangleName() << "("; 309 if(translation_unit_main_signature->get_functionType()->get_parameters().size() != 0){ 310 *output << "argc, argv"; 311 } 312 *output << ");"; 313 314 *output << " }\n"; 315 } 301 316 302 317 if ( output != &cout ) { -
src/tests/.expect/32/declarationSpecifier.txt
r075734f r0270824 621 621 static inline volatile const short __f47__FCVs___1(); 622 622 static inline volatile const short __f48__FCVs___1(); 623 int main(int __argc__i_1, const char **__argv__PPCc_1){623 int __main__Fi_iPPCc__1(int __argc__i_1, const char **__argv__PPCc_1){ 624 624 int ___retval_main__i_1; 625 625 ((void)(___retval_main__i_1=((int )0)) /* ?{} */); 626 626 return ((int )___retval_main__i_1); 627 } 627 ((void)(___retval_main__i_1=0) /* ?{} */); 628 return ((int )___retval_main__i_1); 629 } 630 int main(int argc, char** argv) { return __main__Fi_iPPCc__1(argc, argv); } 631 -
src/tests/.expect/32/gccExtensions.txt
r075734f r0270824 6 6 extern int printf(const char *__restrict __format, ...); 7 7 extern int __x__i_1 asm ( "xx" ); 8 int main(int __argc__i_1, const char **__argv__PPCc_1){8 int __main__Fi_iPPCc__1(int __argc__i_1, const char **__argv__PPCc_1){ 9 9 int ___retval_main__i_1; 10 10 asm ( "nop" : : : ); … … 162 162 ((void)(___retval_main__i_1=((int )0)) /* ?{} */); 163 163 return ((int )___retval_main__i_1); 164 ((void)(___retval_main__i_1=0) /* ?{} */); 165 return ((int )___retval_main__i_1); 164 166 } 167 int main(int argc, char** argv) { return __main__Fi_iPPCc__1(argc, argv); } -
src/tests/.expect/64/declarationSpecifier.txt
r075734f r0270824 621 621 static inline volatile const short __f47__FCVs___1(); 622 622 static inline volatile const short __f48__FCVs___1(); 623 int main(int __argc__i_1, const char **__argv__PPCc_1){623 int __main__Fi_iPPCc__1(int __argc__i_1, const char **__argv__PPCc_1){ 624 624 int ___retval_main__i_1; 625 625 ((void)(___retval_main__i_1=((int )0)) /* ?{} */); 626 626 return ((int )___retval_main__i_1); 627 } 627 ((void)(___retval_main__i_1=0) /* ?{} */); 628 return ((int )___retval_main__i_1); 629 } 630 int main(int argc, char** argv) { return __main__Fi_iPPCc__1(argc, argv); } -
src/tests/.expect/64/gccExtensions.txt
r075734f r0270824 6 6 extern int printf(const char *__restrict __format, ...); 7 7 extern int __x__i_1 asm ( "xx" ); 8 int main(int __argc__i_1, const char **__argv__PPCc_1){8 int __main__Fi_iPPCc__1(int __argc__i_1, const char **__argv__PPCc_1){ 9 9 int ___retval_main__i_1; 10 10 asm ( "nop" : : : ); … … 162 162 ((void)(___retval_main__i_1=((int )0)) /* ?{} */); 163 163 return ((int )___retval_main__i_1); 164 ((void)(___retval_main__i_1=0) /* ?{} */); 165 return ((int )___retval_main__i_1); 164 166 } 167 int main(int argc, char** argv) { return __main__Fi_iPPCc__1(argc, argv); }
Note: See TracChangeset
for help on using the changeset viewer.