[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 |
---|
[f326f99] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Mon Apr 11 15:38:10 2016 |
---|
[51587aa] | 13 | // Update Count : 1 |
---|
| 14 | // |
---|
[51b7345] | 15 | |
---|
[0270824] | 16 | #include <memory> |
---|
| 17 | |
---|
[51b7345] | 18 | #include "FixNames.h" |
---|
| 19 | #include "SynTree/Declaration.h" |
---|
| 20 | #include "SynTree/Expression.h" |
---|
| 21 | #include "SynTree/Visitor.h" |
---|
| 22 | #include "SymTab/Mangler.h" |
---|
| 23 | #include "OperatorTable.h" |
---|
[13de47bc] | 24 | #include "FixMain.h" |
---|
[0270824] | 25 | |
---|
[51b7345] | 26 | namespace CodeGen { |
---|
[51587aa] | 27 | class FixNames : public Visitor { |
---|
| 28 | public: |
---|
| 29 | virtual void visit( ObjectDecl *objectDecl ); |
---|
| 30 | virtual void visit( FunctionDecl *functionDecl ); |
---|
[f326f99] | 31 | |
---|
| 32 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 33 | private: |
---|
| 34 | int scopeLevel = 1; |
---|
| 35 | |
---|
| 36 | void fixDWT( DeclarationWithType *dwt ); |
---|
[51587aa] | 37 | }; |
---|
| 38 | |
---|
[0270824] | 39 | std::string mangle_main() { |
---|
| 40 | FunctionType* main_type; |
---|
| 41 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( |
---|
| 42 | "main", |
---|
| 43 | DeclarationNode::NoStorageClass, |
---|
| 44 | LinkageSpec::Cforall, |
---|
| 45 | main_type = new FunctionType( Type::Qualifiers(), true ), |
---|
| 46 | nullptr, false, false |
---|
| 47 | ) }; |
---|
| 48 | main_type->get_returnVals().push_back( |
---|
| 49 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
| 50 | ); |
---|
| 51 | |
---|
| 52 | auto&& name = SymTab::Mangler::mangle( mainDecl.get() ); |
---|
| 53 | // std::cerr << name << std::endl; |
---|
| 54 | return name; |
---|
| 55 | } |
---|
| 56 | std::string mangle_main_args() { |
---|
| 57 | FunctionType* main_type; |
---|
| 58 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( |
---|
| 59 | "main", |
---|
| 60 | DeclarationNode::NoStorageClass, |
---|
| 61 | LinkageSpec::Cforall, |
---|
| 62 | main_type = new FunctionType( Type::Qualifiers(), false ), |
---|
| 63 | nullptr, false, false |
---|
| 64 | ) }; |
---|
| 65 | main_type->get_returnVals().push_back( |
---|
| 66 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
| 67 | ); |
---|
| 68 | |
---|
| 69 | mainDecl->get_functionType()->get_parameters().push_back( |
---|
| 70 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
| 71 | ); |
---|
| 72 | |
---|
| 73 | mainDecl->get_functionType()->get_parameters().push_back( |
---|
| 74 | new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, |
---|
| 75 | new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), |
---|
| 76 | nullptr ) |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | auto&& name = SymTab::Mangler::mangle( mainDecl.get() ); |
---|
| 80 | // std::cerr << name << std::endl; |
---|
| 81 | return name; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | bool is_main(const std::string& name) { |
---|
| 85 | static std::string mains[] = { |
---|
| 86 | mangle_main(), |
---|
| 87 | mangle_main_args() |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | for(const auto& m : mains) { |
---|
| 91 | if( name == m ) return true; |
---|
| 92 | } |
---|
| 93 | return false; |
---|
| 94 | } |
---|
| 95 | |
---|
[51587aa] | 96 | void fixNames( std::list< Declaration* > translationUnit ) { |
---|
| 97 | FixNames fixer; |
---|
| 98 | acceptAll( translationUnit, fixer ); |
---|
| 99 | } |
---|
| 100 | |
---|
[f326f99] | 101 | void FixNames::fixDWT( DeclarationWithType *dwt ) { |
---|
[51587aa] | 102 | if ( dwt->get_name() != "" ) { |
---|
| 103 | if ( LinkageSpec::isDecoratable( dwt->get_linkage() ) ) { |
---|
| 104 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); |
---|
[f326f99] | 105 | dwt->set_scopeLevel( scopeLevel ); |
---|
[51587aa] | 106 | } // if |
---|
| 107 | } // if |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void FixNames::visit( ObjectDecl *objectDecl ) { |
---|
| 111 | Visitor::visit( objectDecl ); |
---|
| 112 | fixDWT( objectDecl ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void FixNames::visit( FunctionDecl *functionDecl ) { |
---|
| 116 | Visitor::visit( functionDecl ); |
---|
| 117 | fixDWT( functionDecl ); |
---|
[0270824] | 118 | |
---|
| 119 | if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) { |
---|
[13de47bc] | 120 | int nargs = functionDecl->get_functionType()->get_parameters().size(); |
---|
| 121 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { |
---|
[aaa1a99a] | 122 | throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", functionDecl); |
---|
[0270824] | 123 | } |
---|
| 124 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) ); |
---|
[13de47bc] | 125 | CodeGen::FixMain::registerMain( functionDecl ); |
---|
[0270824] | 126 | } |
---|
[51587aa] | 127 | } |
---|
[f326f99] | 128 | |
---|
| 129 | void FixNames::visit( CompoundStmt *compoundStmt ) { |
---|
| 130 | scopeLevel++; |
---|
| 131 | Visitor::visit( compoundStmt ); |
---|
| 132 | scopeLevel--; |
---|
| 133 | } |
---|
[51b7345] | 134 | } // namespace CodeGen |
---|
[51587aa] | 135 | |
---|
| 136 | // Local Variables: // |
---|
| 137 | // tab-width: 4 // |
---|
| 138 | // mode: c++ // |
---|
| 139 | // compile-command: "make install" // |
---|
| 140 | // End: // |
---|