[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[f326f99] | 7 | // FixNames.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[fa4805f] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Jun 28 15:26:00 2017 |
---|
| 13 | // Update Count : 20 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "FixNames.h" |
---|
[bf2438c] | 17 | |
---|
| 18 | #include <memory> // for unique_ptr |
---|
| 19 | #include <string> // for string, operator!=, operator== |
---|
| 20 | |
---|
[c50d54d] | 21 | #include "Common/PassVisitor.h" |
---|
[bf2438c] | 22 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 23 | #include "FixMain.h" // for FixMain |
---|
| 24 | #include "Parser/LinkageSpec.h" // for Cforall, isMangled |
---|
| 25 | #include "SymTab/Mangler.h" // for Mangler |
---|
| 26 | #include "SynTree/Constant.h" // for Constant |
---|
| 27 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declarat... |
---|
| 28 | #include "SynTree/Expression.h" // for ConstantExpr |
---|
| 29 | #include "SynTree/Label.h" // for Label, noLabels |
---|
| 30 | #include "SynTree/Statement.h" // for ReturnStmt, CompoundStmt |
---|
| 31 | #include "SynTree/Type.h" // for Type, BasicType, Type::Qualifiers |
---|
| 32 | #include "SynTree/Visitor.h" // for Visitor, acceptAll |
---|
[0270824] | 33 | |
---|
[51b7345] | 34 | namespace CodeGen { |
---|
[c50d54d] | 35 | class FixNames : public WithGuards { |
---|
[51587aa] | 36 | public: |
---|
[c50d54d] | 37 | void postvisit( ObjectDecl *objectDecl ); |
---|
| 38 | void postvisit( FunctionDecl *functionDecl ); |
---|
[f326f99] | 39 | |
---|
[c50d54d] | 40 | void previsit( CompoundStmt *compoundStmt ); |
---|
[f326f99] | 41 | private: |
---|
| 42 | int scopeLevel = 1; |
---|
| 43 | |
---|
| 44 | void fixDWT( DeclarationWithType *dwt ); |
---|
[51587aa] | 45 | }; |
---|
| 46 | |
---|
[0270824] | 47 | std::string mangle_main() { |
---|
| 48 | FunctionType* main_type; |
---|
[68fe077a] | 49 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, |
---|
[a7c90d4] | 50 | main_type = new FunctionType( Type::Qualifiers(), true ), nullptr ) |
---|
| 51 | }; |
---|
[bf2438c] | 52 | main_type->get_returnVals().push_back( |
---|
[68fe077a] | 53 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
[0270824] | 54 | ); |
---|
| 55 | |
---|
[dd020c0] | 56 | auto && name = SymTab::Mangler::mangle( mainDecl.get() ); |
---|
[0270824] | 57 | // std::cerr << name << std::endl; |
---|
[6d611fb] | 58 | return std::move(name); |
---|
[0270824] | 59 | } |
---|
| 60 | std::string mangle_main_args() { |
---|
| 61 | FunctionType* main_type; |
---|
[bf2438c] | 62 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, |
---|
[a7c90d4] | 63 | main_type = new FunctionType( Type::Qualifiers(), false ), nullptr ) |
---|
| 64 | }; |
---|
[bf2438c] | 65 | main_type->get_returnVals().push_back( |
---|
[68fe077a] | 66 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
[0270824] | 67 | ); |
---|
| 68 | |
---|
[fa4c094] | 69 | main_type->get_parameters().push_back( |
---|
[68fe077a] | 70 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
[0270824] | 71 | ); |
---|
| 72 | |
---|
[fa4c094] | 73 | main_type->get_parameters().push_back( |
---|
[bf2438c] | 74 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, |
---|
| 75 | new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), |
---|
[0270824] | 76 | nullptr ) |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | auto&& name = SymTab::Mangler::mangle( mainDecl.get() ); |
---|
| 80 | // std::cerr << name << std::endl; |
---|
[6d611fb] | 81 | return std::move(name); |
---|
[0270824] | 82 | } |
---|
| 83 | |
---|
| 84 | bool is_main(const std::string& name) { |
---|
[bf2438c] | 85 | static std::string mains[] = { |
---|
| 86 | mangle_main(), |
---|
[0270824] | 87 | mangle_main_args() |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | for(const auto& m : mains) { |
---|
| 91 | if( name == m ) return true; |
---|
| 92 | } |
---|
| 93 | return false; |
---|
| 94 | } |
---|
| 95 | |
---|
[c50d54d] | 96 | void fixNames( std::list< Declaration* > & translationUnit ) { |
---|
| 97 | PassVisitor<FixNames> fixer; |
---|
[51587aa] | 98 | acceptAll( translationUnit, fixer ); |
---|
| 99 | } |
---|
| 100 | |
---|
[c50d54d] | 101 | void FixNames::fixDWT( DeclarationWithType * dwt ) { |
---|
[51587aa] | 102 | if ( dwt->get_name() != "" ) { |
---|
[fa4805f] | 103 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) { |
---|
[51587aa] | 104 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); |
---|
[f326f99] | 105 | dwt->set_scopeLevel( scopeLevel ); |
---|
[51587aa] | 106 | } // if |
---|
| 107 | } // if |
---|
| 108 | } |
---|
| 109 | |
---|
[c50d54d] | 110 | void FixNames::postvisit( ObjectDecl * objectDecl ) { |
---|
[51587aa] | 111 | fixDWT( objectDecl ); |
---|
| 112 | } |
---|
| 113 | |
---|
[c50d54d] | 114 | void FixNames::postvisit( FunctionDecl * functionDecl ) { |
---|
[51587aa] | 115 | fixDWT( functionDecl ); |
---|
[0270824] | 116 | |
---|
| 117 | if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) { |
---|
[13de47bc] | 118 | int nargs = functionDecl->get_functionType()->get_parameters().size(); |
---|
| 119 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { |
---|
[a16764a6] | 120 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n"); |
---|
[0270824] | 121 | } |
---|
[ba3706f] | 122 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); |
---|
[13de47bc] | 123 | CodeGen::FixMain::registerMain( functionDecl ); |
---|
[0270824] | 124 | } |
---|
[51587aa] | 125 | } |
---|
[f326f99] | 126 | |
---|
[c50d54d] | 127 | void FixNames::previsit( CompoundStmt * ) { |
---|
[f326f99] | 128 | scopeLevel++; |
---|
[c50d54d] | 129 | GuardAction( [this](){ scopeLevel--; } ); |
---|
[f326f99] | 130 | } |
---|
[51b7345] | 131 | } // namespace CodeGen |
---|
[51587aa] | 132 | |
---|
| 133 | // Local Variables: // |
---|
| 134 | // tab-width: 4 // |
---|
| 135 | // mode: c++ // |
---|
| 136 | // compile-command: "make install" // |
---|
| 137 | // End: // |
---|