- Timestamp:
- Nov 8, 2021, 5:28:21 PM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- 36a05d7
- Parents:
- 949339b (diff), 5ee153d (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
- Files:
-
- 9 added
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.hpp
r949339b rcc287800 131 131 // declared type, derived from parameter declarations 132 132 ptr<FunctionType> type; 133 /// Null for the forward declaration of a function. 133 134 ptr<CompoundStmt> stmts; 134 135 std::vector< ptr<Expr> > withExprs; -
src/AST/Pass.hpp
r949339b rcc287800 348 348 349 349 /// When this node is finished being visited, restore the value of a variable 350 /// You may assign to the return value to set the new value in the same statement. 350 351 template< typename T > 351 voidGuardValue( T& val ) {352 T& GuardValue( T& val ) { 352 353 at_cleanup( [ val ]( void * newVal ) { 353 354 * static_cast< T * >( newVal ) = val; 354 355 }, static_cast< void * >( & val ) ); 356 return val; 355 357 } 356 358 … … 394 396 }; 395 397 398 /// Used to get a pointer to the wrapping TranslationUnit. 399 struct WithConstTranslationUnit { 400 const TranslationUnit * translationUnit = nullptr; 401 402 const TranslationUnit & transUnit() const { 403 assertf( translationUnit, "WithConstTranslationUnit not set-up." ); 404 return *translationUnit; 405 } 406 }; 407 396 408 } 397 409 -
src/AST/Pass.impl.hpp
r949339b rcc287800 420 420 template< typename core_t > 421 421 inline void ast::accept_all( ast::TranslationUnit & unit, ast::Pass< core_t > & visitor ) { 422 return ast::accept_all( unit.decls, visitor ); 422 if ( auto ptr = __pass::translation_unit::get_cptr( visitor.core, 0 ) ) { 423 ValueGuard<const TranslationUnit *> guard( *ptr ); 424 *ptr = &unit; 425 return ast::accept_all( unit.decls, visitor ); 426 } else { 427 return ast::accept_all( unit.decls, visitor ); 428 } 423 429 } 424 430 -
src/AST/Pass.proto.hpp
r949339b rcc287800 426 426 } // namespace forall 427 427 428 // For passes that need access to the global context. Sreaches `translationUnit` 429 namespace translation_unit { 430 template<typename core_t> 431 static inline auto get_cptr( core_t & core, int ) 432 -> decltype( &core.translationUnit ) { 433 return &core.translationUnit; 434 } 435 436 template<typename core_t> 437 static inline const TranslationUnit ** get_cptr( core_t &, long ) { 438 return nullptr; 439 } 440 } 441 428 442 template<typename core_t> 429 443 static inline auto get_result( core_t & core, char ) -> decltype( core.result() ) { -
src/AST/Stmt.hpp
r949339b rcc287800 175 175 class CaseStmt final : public Stmt { 176 176 public: 177 /// Null for the default label. 177 178 ptr<Expr> cond; 178 179 std::vector<ptr<Stmt>> stmts; -
src/AST/TranslationUnit.hpp
r949339b rcc287800 26 26 std::list< ptr< Decl > > decls; 27 27 28 struct Global s{28 struct Global { 29 29 std::map< UniqueId, Decl * > idMap; 30 30 31 const Type *sizeType;31 ptr<Type> sizeType; 32 32 const FunctionDecl * dereference; 33 33 const StructDecl * dtorStruct; -
src/AST/porting.md
r949339b rcc287800 98 98 * `Initializer` => `ast::Init` 99 99 * `Statement` => `ast::Stmt` 100 * `ReferenceToType` => `ast::BaseInstType` 100 101 * any field names should follow a similar renaming 101 102 * because they don't really belong to `Type` (and for consistency with `Linkage::Spec`): -
src/CodeGen/FixMain.cc
r949339b rcc287800 22 22 #include <string> // for operator<< 23 23 24 #include "AST/Decl.hpp" 25 #include "AST/Type.hpp" 26 #include "Common/PassVisitor.h" 24 27 #include "Common/SemanticError.h" // for SemanticError 25 28 #include "CodeGen/GenType.h" // for GenType … … 29 32 30 33 namespace CodeGen { 34 35 namespace { 36 37 struct FindMainCore { 38 FunctionDecl * main_signature = nullptr; 39 40 void previsit( FunctionDecl * decl ) { 41 if ( FixMain::isMain( decl ) ) { 42 if ( main_signature ) { 43 SemanticError( decl, "Multiple definition of main routine\n" ); 44 } 45 main_signature = decl; 46 } 47 } 48 }; 49 50 } 51 31 52 bool FixMain::replace_main = false; 32 std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr;33 53 34 54 template<typename container> … … 37 57 } 38 58 39 void FixMain::registerMain(FunctionDecl* functionDecl) 40 { 41 if(main_signature) { 42 SemanticError(functionDecl, "Multiple definition of main routine\n"); 43 } 44 main_signature.reset( functionDecl->clone() ); 45 } 59 void FixMain::fix( std::list< Declaration * > & translationUnit, 60 std::ostream &os, const char* bootloader_filename ) { 61 PassVisitor< FindMainCore > main_finder; 62 acceptAll( translationUnit, main_finder ); 63 FunctionDecl * main_signature = main_finder.pass.main_signature; 46 64 47 void FixMain::fix(std::ostream &os, const char* bootloader_filename) {48 65 if( main_signature ) { 49 66 os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return "; 50 main_signature->mangleName = SymTab::Mangler::mangle(main_signature .get());67 main_signature->mangleName = SymTab::Mangler::mangle(main_signature); 51 68 52 69 os << main_signature->get_scopedMangleName() << "("; … … 65 82 } 66 83 } 84 85 namespace { 86 87 ObjectDecl * signedIntObj() { 88 return new ObjectDecl( 89 "", Type::StorageClasses(), LinkageSpec::Cforall, 0, 90 new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ); 91 } 92 93 ObjectDecl * charStarObj() { 94 return new ObjectDecl( 95 "", Type::StorageClasses(), LinkageSpec::Cforall, 0, 96 new PointerType( Type::Qualifiers(), 97 new PointerType( Type::Qualifiers(), 98 new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), 99 nullptr ); 100 } 101 102 std::string create_mangled_main_function_name( FunctionType * function_type ) { 103 std::unique_ptr<FunctionDecl> decl( new FunctionDecl( 104 "main", Type::StorageClasses(), LinkageSpec::Cforall, 105 function_type, nullptr ) ); 106 return SymTab::Mangler::mangle( decl.get() ); 107 } 108 109 std::string mangled_0_argument_main() { 110 FunctionType* main_type = new FunctionType( Type::Qualifiers(), true ); 111 main_type->get_returnVals().push_back( signedIntObj() ); 112 return create_mangled_main_function_name( main_type ); 113 } 114 115 std::string mangled_2_argument_main() { 116 FunctionType* main_type = new FunctionType( Type::Qualifiers(), false ); 117 main_type->get_returnVals().push_back( signedIntObj() ); 118 main_type->get_parameters().push_back( signedIntObj() ); 119 main_type->get_parameters().push_back( charStarObj() ); 120 return create_mangled_main_function_name( main_type ); 121 } 122 123 bool is_main( const std::string & mangled_name ) { 124 // This breaks if you move it out of the function. 125 static const std::string mangled_mains[] = { 126 mangled_0_argument_main(), 127 mangled_2_argument_main(), 128 //mangled_3_argument_main(), 129 }; 130 131 for ( auto main_name : mangled_mains ) { 132 if ( main_name == mangled_name ) return true; 133 } 134 return false; 135 } 136 137 } // namespace 138 139 bool FixMain::isMain( FunctionDecl * decl ) { 140 if ( std::string("main") != decl->name ) { 141 return false; 142 } 143 return is_main( SymTab::Mangler::mangle( decl, true, true ) ); 144 } 145 146 bool FixMain::isMain( const ast::FunctionDecl * decl ) { 147 if ( std::string("main") != decl->name ) { 148 return false; 149 } 150 return is_main( Mangle::mangle( decl, Mangle::Type ) ); 151 } 152 67 153 }; -
src/CodeGen/FixMain.h
r949339b rcc287800 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 16:20:00 2021 13 // Update Count : 8 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; 26 namespace ast { 27 class FunctionDecl; 28 } 24 29 25 30 namespace CodeGen { 26 class FixMain {27 public :28 static inline LinkageSpec::Spec mainLinkage() {29 return replace_main ? LinkageSpec::Cforall : LinkageSpec::C;30 }31 32 static inline void setReplaceMain(bool val) {33 replace_main = val;34 }35 31 36 static void registerMain(FunctionDecl* val); 32 class FixMain { 33 public : 34 static inline LinkageSpec::Spec mainLinkage() { 35 return replace_main ? LinkageSpec::Cforall : LinkageSpec::C; 36 } 37 37 38 static void fix(std::ostream &os, const char* bootloader_filename); 38 static inline void setReplaceMain(bool val) { 39 replace_main = val; 40 } 39 41 40 private: 41 static bool replace_main; 42 static std::unique_ptr<FunctionDecl> main_signature; 43 }; 42 static bool isMain(FunctionDecl* decl); 43 static bool isMain(const ast::FunctionDecl * decl); 44 45 static void fix( std::list< Declaration * > & decls, 46 std::ostream &os, const char* bootloader_filename ); 47 48 private: 49 static bool replace_main; 50 }; 51 44 52 } // namespace CodeGen -
src/CodeGen/FixNames.cc
r949339b rcc287800 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 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 … … 46 49 }; 47 50 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 51 void fixNames( std::list< Declaration* > & translationUnit ) { 98 52 PassVisitor<FixNames> fixer; … … 118 72 fixDWT( functionDecl ); 119 73 120 if (is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {74 if ( FixMain::isMain( functionDecl ) ) { 121 75 int nargs = functionDecl->get_functionType()->get_parameters().size(); 122 76 if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { … … 124 78 } 125 79 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); 126 CodeGen::FixMain::registerMain( functionDecl );127 80 } 128 81 } … … 132 85 GuardAction( [this](){ scopeLevel--; } ); 133 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 134 139 } // namespace CodeGen 135 140 -
src/CodeGen/FixNames.h
r949339b rcc287800 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/CodeTools/DeclStats.cc
r949339b rcc287800 156 156 /// number of counting bins for linkages 157 157 static const unsigned n_named_specs = 8; 158 /// map from total number of specs to bins 159 static const unsigned ind_for_linkage[16]; 158 /// Mapping function from linkage to bin. 159 static unsigned linkage_index( LinkageSpec::Spec spec ) { 160 switch ( spec ) { 161 case LinkageSpec::Intrinsic: return 0; 162 case LinkageSpec::C: return 1; 163 case LinkageSpec::Cforall: return 2; 164 case LinkageSpec::AutoGen: return 3; 165 case LinkageSpec::Compiler: return 4; 166 case LinkageSpec::BuiltinCFA: return 5; 167 case LinkageSpec::BuiltinC: return 6; 168 default: return 7; 169 } 170 } 160 171 161 172 Stats for_linkage[n_named_specs]; ///< Stores separate stats per linkage … … 366 377 const std::string& mangleName = decl->get_mangleName().empty() ? decl->name : decl->get_mangleName(); 367 378 if ( seen_names.insert( mangleName ).second ) { 368 Stats& stats = for_linkage[ ind_for_linkage[ decl->linkage ]];379 Stats& stats = for_linkage[ linkage_index( decl->linkage ) ]; 369 380 370 381 ++stats.n_decls; … … 527 538 }; 528 539 529 const unsigned DeclStats::ind_for_linkage[]530 = { 7, 7, 2, 1, 7, 7, 7, 3, 4, 7, 6, 5, 7, 7, 7, 0 };531 532 540 void printDeclStats( std::list< Declaration * > &translationUnit ) { 533 541 PassVisitor<DeclStats> stats; -
src/Common/module.mk
r949339b rcc287800 22 22 Common/CompilerError.h \ 23 23 Common/Debug.h \ 24 Common/DeclStats.hpp \ 25 Common/DeclStats.cpp \ 24 26 Common/ErrorObjects.h \ 25 27 Common/Eval.cc \ … … 33 35 Common/PassVisitor.proto.h \ 34 36 Common/PersistentMap.h \ 37 Common/ResolvProtoDump.hpp \ 38 Common/ResolvProtoDump.cpp \ 35 39 Common/ScopedMap.h \ 36 40 Common/SemanticError.cc \ -
src/ControlStruct/ExceptTranslate.cc
r949339b rcc287800 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Except Visitor.cc --7 // ExceptTranslate.cc -- Conversion of exception control flow structures. 8 8 // 9 9 // Author : Andrew Beach -
src/ControlStruct/ExceptTranslate.h
r949339b rcc287800 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // ExceptTranslate.h -- 7 // ExceptTranslate.h -- Conversion of exception control flow structures. 8 8 // 9 9 // Author : Andrew Beach 10 10 // Created On : Tus Jun 06 10:13:00 2017 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tus May 19 11:47:00 202013 // Update Count : 512 // Last Modified On : Mon Nov 8 11:43:00 2020 13 // Update Count : 6 14 14 // 15 15 … … 19 19 20 20 class Declaration; 21 namespace ast { 22 class TranslationUnit; 23 } 21 24 22 25 namespace ControlStruct { 23 26 void translateThrows( std::list< Declaration *> & translationUnit ); 27 void translateThrows( ast::TranslationUnit & transUnit ); 24 28 /* Replaces all throw & throwResume statements with function calls. 25 29 * These still need to be resolved, so call this before the reslover. -
src/ControlStruct/LabelGenerator.cc
r949339b rcc287800 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon Mar 11 22:23:20 201913 // Update Count : 1 511 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Nov 8 10:18:00 2021 13 // Update Count : 17 14 14 // 15 15 … … 19 19 20 20 #include "LabelGenerator.h" 21 22 #include "AST/Attribute.hpp" 23 #include "AST/Label.hpp" 24 #include "AST/Stmt.hpp" 21 25 #include "SynTree/Attribute.h" // for Attribute 22 26 #include "SynTree/Label.h" // for Label, operator<< … … 24 28 25 29 namespace ControlStruct { 26 LabelGenerator * LabelGenerator::labelGenerator = 0; 30 31 int LabelGenerator::current = 0; 32 LabelGenerator * LabelGenerator::labelGenerator = nullptr; 27 33 28 34 LabelGenerator * LabelGenerator::getGenerator() { … … 43 49 return l; 44 50 } 51 52 ast::Label LabelGenerator::newLabel( 53 const std::string & suffix, const ast::Stmt * stmt ) { 54 assert( stmt ); 55 56 std::ostringstream os; 57 os << "__L" << current++ << "__" << suffix; 58 if ( stmt && !stmt->labels.empty() ) { 59 os << "_" << stmt->labels.front() << "__"; 60 } 61 ast::Label ret_label( stmt->location, os.str() ); 62 ret_label.attributes.push_back( new ast::Attribute( "unused" ) ); 63 return ret_label; 64 } 65 45 66 } // namespace ControlStruct 46 67 -
src/ControlStruct/LabelGenerator.h
r949339b rcc287800 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat Jul 22 09:20:14 201713 // Update Count : 611 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Nov 8 10:16:00 2021 13 // Update Count : 8 14 14 // 15 15 … … 21 21 22 22 class Statement; 23 namespace ast { 24 class Stmt; 25 class Label; 26 } 23 27 24 28 namespace ControlStruct { 25 class LabelGenerator { 26 public: 27 static LabelGenerator *getGenerator(); 28 Label newLabel(std::string suffix, Statement * stmt = nullptr); 29 void reset() { current = 0; } 30 void rewind() { current--; } 31 protected: 32 LabelGenerator(): current(0) {} 33 private: 34 int current; 35 static LabelGenerator *labelGenerator; 36 }; 29 30 class LabelGenerator { 31 static int current; 32 static LabelGenerator *labelGenerator; 33 protected: 34 LabelGenerator() {} 35 public: 36 static LabelGenerator *getGenerator(); 37 static Label newLabel(std::string suffix, Statement * stmt = nullptr); 38 static ast::Label newLabel( const std::string&, const ast::Stmt * ); 39 static void reset() { current = 0; } 40 static void rewind() { current--; } 41 }; 42 37 43 } // namespace ControlStruct 38 44 -
src/ControlStruct/module.mk
r949339b rcc287800 18 18 ControlStruct/ExceptDecl.cc \ 19 19 ControlStruct/ExceptDecl.h \ 20 ControlStruct/FixLabels.cpp \ 21 ControlStruct/FixLabels.hpp \ 20 22 ControlStruct/ForExprMutator.cc \ 21 23 ControlStruct/ForExprMutator.h \ … … 26 28 ControlStruct/MLEMutator.cc \ 27 29 ControlStruct/MLEMutator.h \ 30 ControlStruct/MultiLevelExit.cpp \ 31 ControlStruct/MultiLevelExit.hpp \ 28 32 ControlStruct/Mutate.cc \ 29 33 ControlStruct/Mutate.h 30 34 31 SRC += $(SRC_CONTROLSTRUCT) ControlStruct/ExceptTranslate.cc ControlStruct/ExceptTranslate.h 35 SRC += $(SRC_CONTROLSTRUCT) \ 36 ControlStruct/ExceptTranslateNew.cpp \ 37 ControlStruct/ExceptTranslate.cc \ 38 ControlStruct/ExceptTranslate.h 39 32 40 SRCDEMANGLE += $(SRC_CONTROLSTRUCT) 33 41 -
src/InitTweak/GenInit.cc
r949339b rcc287800 9 9 // Author : Rob Schluntz 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:15:10 201913 // Update Count : 18 411 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Oct 25 13:53:00 2021 13 // Update Count : 186 14 14 // 15 15 #include "GenInit.h" … … 24 24 #include "AST/Decl.hpp" 25 25 #include "AST/Init.hpp" 26 #include "AST/Pass.hpp" 26 27 #include "AST/Node.hpp" 27 28 #include "AST/Stmt.hpp" … … 294 295 } 295 296 297 namespace { 298 299 # warning Remove the _New suffix after the conversion is complete. 300 struct HoistArrayDimension_NoResolve_New final : 301 public ast::WithDeclsToAdd<>, public ast::WithShortCircuiting, 302 public ast::WithGuards, public ast::WithConstTranslationUnit, 303 public ast::WithVisitorRef<HoistArrayDimension_NoResolve_New> { 304 void previsit( const ast::ObjectDecl * decl ); 305 const ast::DeclWithType * postvisit( const ast::ObjectDecl * decl ); 306 // Do not look for objects inside there declarations (and type). 307 void previsit( const ast::AggregateDecl * ) { visit_children = false; } 308 void previsit( const ast::NamedTypeDecl * ) { visit_children = false; } 309 void previsit( const ast::FunctionType * ) { visit_children = false; } 310 311 const ast::Type * hoist( const ast::Type * type ); 312 313 ast::Storage::Classes storageClasses; 314 }; 315 316 void HoistArrayDimension_NoResolve_New::previsit( 317 const ast::ObjectDecl * decl ) { 318 GuardValue( storageClasses ) = decl->storage; 319 } 320 321 const ast::DeclWithType * HoistArrayDimension_NoResolve_New::postvisit( 322 const ast::ObjectDecl * objectDecl ) { 323 return mutate_field( objectDecl, &ast::ObjectDecl::type, 324 hoist( objectDecl->type ) ); 325 } 326 327 const ast::Type * HoistArrayDimension_NoResolve_New::hoist( 328 const ast::Type * type ) { 329 static UniqueName dimensionName( "_array_dim" ); 330 331 if ( !isInFunction() || storageClasses.is_static ) { 332 return type; 333 } 334 335 if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { 336 if ( nullptr == arrayType->dimension ) { 337 return type; 338 } 339 340 if ( !Tuples::maybeImpure( arrayType->dimension ) ) { 341 return type; 342 } 343 344 ast::ptr<ast::Type> dimType = transUnit().global.sizeType; 345 assert( dimType ); 346 add_qualifiers( dimType, ast::CV::Qualifiers( ast::CV::Const ) ); 347 348 ast::ObjectDecl * arrayDimension = new ast::ObjectDecl( 349 arrayType->dimension->location, 350 dimensionName.newName(), 351 dimType, 352 new ast::SingleInit( 353 arrayType->dimension->location, 354 arrayType->dimension 355 ) 356 ); 357 358 ast::ArrayType * mutType = ast::mutate( arrayType ); 359 mutType->dimension = new ast::VariableExpr( 360 arrayDimension->location, arrayDimension ); 361 declsToAddBefore.push_back( arrayDimension ); 362 363 mutType->base = hoist( mutType->base ); 364 return mutType; 365 } 366 return type; 367 } 368 369 struct ReturnFixer_New final : 370 public ast::WithStmtsToAdd<>, ast::WithGuards { 371 void previsit( const ast::FunctionDecl * decl ); 372 const ast::ReturnStmt * previsit( const ast::ReturnStmt * stmt ); 373 private: 374 const ast::FunctionDecl * funcDecl = nullptr; 375 }; 376 377 void ReturnFixer_New::previsit( const ast::FunctionDecl * decl ) { 378 GuardValue( funcDecl ) = decl; 379 } 380 381 const ast::ReturnStmt * ReturnFixer_New::previsit( 382 const ast::ReturnStmt * stmt ) { 383 auto & returns = funcDecl->returns; 384 assert( returns.size() < 2 ); 385 // Hands off if the function returns a reference. 386 // Don't allocate a temporary if the address is returned. 387 if ( stmt->expr && 1 == returns.size() ) { 388 ast::ptr<ast::DeclWithType> retDecl = returns.front(); 389 if ( isConstructable( retDecl->get_type() ) ) { 390 // Explicitly construct the return value using the return 391 // expression and the retVal object. 392 assertf( "" != retDecl->name, 393 "Function %s has unnamed return value.\n", 394 funcDecl->name.c_str() ); 395 396 auto retVal = retDecl.strict_as<ast::ObjectDecl>(); 397 if ( auto varExpr = stmt->expr.as<ast::VariableExpr>() ) { 398 // Check if the return statement is already set up. 399 if ( varExpr->var == retVal ) return stmt; 400 } 401 ast::ptr<ast::Stmt> ctorStmt = genCtorDtor( 402 retVal->location, "?{}", retVal, stmt->expr ); 403 assertf( ctorStmt, 404 "ReturnFixer: genCtorDtor returned nllptr: %s / %s", 405 toString( retVal ).c_str(), 406 toString( stmt->expr ).c_str() ); 407 stmtsToAddBefore.push_back( ctorStmt ); 408 409 // Return the retVal object. 410 ast::ReturnStmt * mutStmt = ast::mutate( stmt ); 411 mutStmt->expr = new ast::VariableExpr( 412 stmt->location, retDecl ); 413 return mutStmt; 414 } 415 } 416 return stmt; 417 } 418 419 } // namespace 420 421 void genInit( ast::TranslationUnit & transUnit ) { 422 ast::Pass<HoistArrayDimension_NoResolve_New>::run( transUnit ); 423 ast::Pass<ReturnFixer_New>::run( transUnit ); 424 } 425 296 426 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { 297 427 PassVisitor<CtorDtor> ctordtor; -
src/InitTweak/GenInit.h
r949339b rcc287800 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat Jul 22 09:31:19 201713 // Update Count : 411 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Oct 22 16:08:00 2021 13 // Update Count : 6 14 14 // 15 15 … … 27 27 /// Adds return value temporaries and wraps Initializers in ConstructorInit nodes 28 28 void genInit( std::list< Declaration * > & translationUnit ); 29 void genInit( ast::TranslationUnit & translationUnit ); 29 30 30 31 /// Converts return statements into copy constructor calls on the hidden return variable -
src/Parser/parser.yy
r949339b rcc287800 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Sep 11 08:20:44202113 // Update Count : 5 04012 // Last Modified On : Fri Oct 15 09:20:17 2021 13 // Update Count : 5163 14 14 // 15 15 … … 31 31 // from ANSI90 to ANSI11 C are marked with the comment "C99/C11". 32 32 33 // This grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions All of the33 // This grammar also has two levels of extensions. The first extensions cover most of the GCC C extensions. All of the 34 34 // syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall (CFA), which 35 35 // fixes several of C's outstanding problems and extends C with many modern language concepts. All of the syntactic … … 69 69 // 2. String encodings are transformed into canonical form (one encoding at start) so the encoding can be found 70 70 // without searching the string, e.g.: "abc" L"def" L"ghi" => L"abc" "def" "ghi". Multiple encodings must match, 71 // i.e., u"a" U"b" L"c" is disallowed.71 // e.g., u"a" U"b" L"c" is disallowed. 72 72 73 73 if ( from[0] != '"' ) { // encoding ? … … 310 310 %token ATassign // @= 311 311 312 %type<tok> identifier 313 %type<tok> identifier_or_type_name attr_name 312 %type<tok> identifier identifier_at identifier_or_type_name attr_name 314 313 %type<tok> quasi_keyword 315 314 %type<constant> string_literal … … 327 326 %type<en> conditional_expression constant_expression assignment_expression assignment_expression_opt 328 327 %type<en> comma_expression comma_expression_opt 329 %type<en> argument_expression_list_opt argument_expression default_initializer_opt328 %type<en> argument_expression_list_opt argument_expression_list argument_expression default_initializer_opt 330 329 %type<ifctl> if_control_expression 331 330 %type<fctl> for_control_expression for_control_expression_list … … 559 558 IDENTIFIER 560 559 | quasi_keyword 560 ; 561 562 identifier_at: 563 identifier 561 564 | '@' // CFA 562 565 { Token tok = { new string( DeclarationNode::anonymous.newName() ), yylval.tok.loc }; $$ = tok; } … … 693 696 // empty 694 697 { $$ = nullptr; } 695 | argument_expression 698 | argument_expression_list 699 ; 700 701 argument_expression_list: 702 argument_expression 696 703 | argument_expression_list_opt ',' argument_expression 697 704 { $$ = (ExpressionNode *)($1->set_last( $3 )); } … … 731 738 | FLOATINGconstant fraction_constants_opt 732 739 { $$ = new ExpressionNode( build_field_name_fraction_constants( build_field_name_FLOATINGconstant( *$1 ), $2 ) ); } 733 | identifier fraction_constants_opt740 | identifier_at fraction_constants_opt // CFA, allow anonymous fields 734 741 { 735 742 $$ = new ExpressionNode( build_field_name_fraction_constants( build_varref( $1 ), $2 ) ); … … 1084 1091 comma_expression_opt ';' 1085 1092 { $$ = new StatementNode( build_expr( $1 ) ); } 1093 | MUTEX '(' ')' comma_expression ';' 1094 { $$ = new StatementNode( build_mutex( nullptr, new StatementNode( build_expr( $4 ) ) ) ); } 1095 // { SemanticError( yylloc, "Mutex expression is currently unimplemented." ); $$ = nullptr; } 1086 1096 ; 1087 1097 … … 1182 1192 1183 1193 iteration_statement: 1184 WHILE '(' push if_control_expression ')' statement pop 1185 { $$ = new StatementNode( build_while( $4, maybe_build_compound( $6 ) ) ); } 1186 | WHILE '(' ')' statement // CFA => while ( 1 ) 1194 WHILE '(' ')' statement // CFA => while ( 1 ) 1187 1195 { $$ = new StatementNode( build_while( new IfCtrl( nullptr, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ), maybe_build_compound( $4 ) ) ); } 1188 | DO statement WHILE '(' comma_expression ')' ';' 1189 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ) ) ); } 1196 | WHILE '(' if_control_expression ')' statement %prec THEN 1197 { $$ = new StatementNode( build_while( $3, maybe_build_compound( $5 ) ) ); } 1198 | WHILE '(' if_control_expression ')' statement ELSE statement // CFA 1199 { SemanticError( yylloc, "Loop default block is currently unimplemented." ); $$ = nullptr; } 1190 1200 | DO statement WHILE '(' ')' ';' // CFA => do while( 1 ) 1191 1201 { $$ = new StatementNode( build_do_while( new ExpressionNode( build_constantInteger( *new string( "1" ) ) ), maybe_build_compound( $2 ) ) ); } 1192 | FOR '(' push for_control_expression_list ')' statement pop 1193 { $$ = new StatementNode( build_for( $4, maybe_build_compound( $6 ) ) ); } 1202 | DO statement WHILE '(' comma_expression ')' ';' %prec THEN 1203 { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ) ) ); } 1204 | DO statement WHILE '(' comma_expression ')' ELSE statement // CFA 1205 { SemanticError( yylloc, "Loop default block is currently unimplemented." ); $$ = nullptr; } 1194 1206 | FOR '(' ')' statement // CFA => for ( ;; ) 1195 1207 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); } 1208 | FOR '(' for_control_expression_list ')' statement %prec THEN 1209 { $$ = new StatementNode( build_for( $3, maybe_build_compound( $5 ) ) ); } 1210 | FOR '(' for_control_expression_list ')' statement ELSE statement // CFA 1211 { SemanticError( yylloc, "Loop default block is currently unimplemented." ); $$ = nullptr; } 1196 1212 ; 1197 1213 … … 1339 1355 with_statement: 1340 1356 WITH '(' tuple_expression_list ')' statement 1341 { 1342 $$ = new StatementNode( build_with( $3, $5 ) ); 1343 } 1357 { $$ = new StatementNode( build_with( $3, $5 ) ); } 1344 1358 ; 1345 1359 1346 1360 // If MUTEX becomes a general qualifier, there are shift/reduce conflicts, so change syntax to "with mutex". 1347 1361 mutex_statement: 1348 MUTEX '(' argument_expression_list _opt')' statement1362 MUTEX '(' argument_expression_list ')' statement 1349 1363 { $$ = new StatementNode( build_mutex( $3, $5 ) ); } 1350 1364 ; … … 2475 2489 designation: 2476 2490 designator_list ':' // C99, CFA uses ":" instead of "=" 2477 | identifier ':' // GCC, field name2491 | identifier_at ':' // GCC, field name 2478 2492 { $$ = new ExpressionNode( build_varref( $1 ) ); } 2479 2493 ; … … 2487 2501 2488 2502 designator: 2489 '.' identifier 2503 '.' identifier_at // C99, field name 2490 2504 { $$ = new ExpressionNode( build_varref( $2 ) ); } 2491 2505 | '[' push assignment_expression pop ']' // C99, single array element … … 2919 2933 2920 2934 paren_identifier: 2921 identifier 2935 identifier_at 2922 2936 { $$ = DeclarationNode::newName( $1 ); } 2923 2937 | '(' paren_identifier ')' // redundant parenthesis -
src/main.cc
r949339b rcc287800 9 9 // Author : Peter Buhr and Rob Schluntz 10 10 // Created On : Fri May 15 23:12:02 2015 11 // Last Modified By : Henry Xue12 // Last Modified On : Mon Aug 23 15:42:08202113 // Update Count : 65 011 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Nov 8 11:42:00 2021 13 // Update Count : 656 14 14 // 15 15 … … 43 43 #include "Common/CodeLocationTools.hpp" // for forceFillCodeLocations 44 44 #include "Common/CompilerError.h" // for CompilerError 45 #include "Common/DeclStats.hpp" // for printDeclStats 46 #include "Common/ResolvProtoDump.hpp" // for dumpAsResolverProto 45 47 #include "Common/Stats.h" 46 48 #include "Common/PassVisitor.h" … … 51 53 #include "ControlStruct/ExceptDecl.h" // for translateExcept 52 54 #include "ControlStruct/ExceptTranslate.h" // for translateEHM 55 #include "ControlStruct/FixLabels.hpp" // for fixLabels 53 56 #include "ControlStruct/Mutate.h" // for mutate 54 57 #include "GenPoly/Box.h" // for box … … 331 334 } // if 332 335 333 PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) );334 PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) );335 PASS( "Fix Names", CodeGen::fixNames( translationUnit ) );336 PASS( "Gen Init", InitTweak::genInit( translationUnit ) );337 338 if ( libcfap ) {339 // generate the bodies of cfa library functions340 LibCfa::makeLibCfa( translationUnit );341 } // if342 343 if ( declstatsp ) {344 CodeTools::printDeclStats( translationUnit );345 deleteAll( translationUnit );346 return EXIT_SUCCESS;347 } // if348 349 if ( bresolvep ) {350 dump( translationUnit );351 return EXIT_SUCCESS;352 } // if353 354 336 CodeTools::fillLocations( translationUnit ); 355 356 if ( resolvprotop ) {357 CodeTools::dumpAsResolvProto( translationUnit );358 return EXIT_SUCCESS;359 } // if360 337 361 338 if( useNewAST ) { … … 366 343 auto transUnit = convert( move( translationUnit ) ); 367 344 345 forceFillCodeLocations( transUnit ); 346 347 PASS( "Translate Throws", ControlStruct::translateThrows( transUnit ) ); 348 PASS( "Fix Labels", ControlStruct::fixLabels( transUnit ) ); 349 PASS( "Fix Names", CodeGen::fixNames( transUnit ) ); 350 PASS( "Gen Init", InitTweak::genInit( transUnit ) ); 368 351 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( transUnit ) ); 369 352 353 if ( libcfap ) { 354 // Generate the bodies of cfa library functions. 355 LibCfa::makeLibCfa( transUnit ); 356 } // if 357 358 if ( declstatsp ) { 359 printDeclStats( transUnit ); 360 return EXIT_SUCCESS; 361 } // if 362 363 if ( bresolvep ) { 364 dump( move( transUnit ) ); 365 return EXIT_SUCCESS; 366 } // if 367 368 if ( resolvprotop ) { 369 dumpAsResolverProto( transUnit ); 370 return EXIT_SUCCESS; 371 } // if 372 370 373 PASS( "Resolve", ResolvExpr::resolve( transUnit ) ); 371 374 if ( exprp ) { … … 377 380 378 381 PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary())); 382 379 383 translationUnit = convert( move( transUnit ) ); 380 384 } else { 385 PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) ); 386 PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); 387 PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); 388 PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); 381 389 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) ); 390 391 if ( libcfap ) { 392 // Generate the bodies of cfa library functions. 393 LibCfa::makeLibCfa( translationUnit ); 394 } // if 395 396 if ( declstatsp ) { 397 CodeTools::printDeclStats( translationUnit ); 398 deleteAll( translationUnit ); 399 return EXIT_SUCCESS; 400 } // if 401 402 if ( bresolvep ) { 403 dump( translationUnit ); 404 return EXIT_SUCCESS; 405 } // if 406 407 CodeTools::fillLocations( translationUnit ); 408 409 if ( resolvprotop ) { 410 CodeTools::dumpAsResolvProto( translationUnit ); 411 return EXIT_SUCCESS; 412 } // if 382 413 383 414 PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); … … 443 474 PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) ); 444 475 445 CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() ); 476 CodeGen::FixMain::fix( translationUnit, *output, 477 (PreludeDirector + "/bootloader.c").c_str() ); 446 478 if ( output != &cout ) { 447 479 delete output;
Note:
See TracChangeset
for help on using the changeset viewer.