| 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 | // | 
|---|
| 7 | // FixNames.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Wed Jun 28 15:26:00 2017 | 
|---|
| 13 | // Update Count     : 20 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "FixNames.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <memory>                  // for unique_ptr | 
|---|
| 19 | #include <string>                  // for string, operator!=, operator== | 
|---|
| 20 |  | 
|---|
| 21 | #include "Common/PassVisitor.h" | 
|---|
| 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 | 
|---|
| 33 |  | 
|---|
| 34 | namespace CodeGen { | 
|---|
| 35 | class FixNames : public WithGuards { | 
|---|
| 36 | public: | 
|---|
| 37 | void postvisit( ObjectDecl *objectDecl ); | 
|---|
| 38 | void postvisit( FunctionDecl *functionDecl ); | 
|---|
| 39 |  | 
|---|
| 40 | void previsit( CompoundStmt *compoundStmt ); | 
|---|
| 41 | private: | 
|---|
| 42 | int scopeLevel = 1; | 
|---|
| 43 |  | 
|---|
| 44 | void fixDWT( DeclarationWithType *dwt ); | 
|---|
| 45 | }; | 
|---|
| 46 |  | 
|---|
| 47 | std::string mangle_main() { | 
|---|
| 48 | FunctionType* main_type; | 
|---|
| 49 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, | 
|---|
| 50 | main_type = new FunctionType( Type::Qualifiers(), true ), nullptr ) | 
|---|
| 51 | }; | 
|---|
| 52 | main_type->get_returnVals().push_back( | 
|---|
| 53 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) | 
|---|
| 54 | ); | 
|---|
| 55 |  | 
|---|
| 56 | auto && name = SymTab::Mangler::mangle( mainDecl.get() ); | 
|---|
| 57 | // std::cerr << name << std::endl; | 
|---|
| 58 | return std::move(name); | 
|---|
| 59 | } | 
|---|
| 60 | std::string mangle_main_args() { | 
|---|
| 61 | FunctionType* main_type; | 
|---|
| 62 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, | 
|---|
| 63 | main_type = new FunctionType( Type::Qualifiers(), false ), nullptr ) | 
|---|
| 64 | }; | 
|---|
| 65 | main_type->get_returnVals().push_back( | 
|---|
| 66 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) | 
|---|
| 67 | ); | 
|---|
| 68 |  | 
|---|
| 69 | main_type->get_parameters().push_back( | 
|---|
| 70 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) | 
|---|
| 71 | ); | 
|---|
| 72 |  | 
|---|
| 73 | main_type->get_parameters().push_back( | 
|---|
| 74 | new ObjectDecl( "", Type::StorageClasses(), 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 std::move(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 |  | 
|---|
| 96 | void fixNames( std::list< Declaration* > & translationUnit ) { | 
|---|
| 97 | PassVisitor<FixNames> fixer; | 
|---|
| 98 | acceptAll( translationUnit, fixer ); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void FixNames::fixDWT( DeclarationWithType * dwt ) { | 
|---|
| 102 | if ( dwt->get_name() != "" ) { | 
|---|
| 103 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) { | 
|---|
| 104 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); | 
|---|
| 105 | dwt->set_scopeLevel( scopeLevel ); | 
|---|
| 106 | } // if | 
|---|
| 107 | } // if | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void FixNames::postvisit( ObjectDecl * objectDecl ) { | 
|---|
| 111 | fixDWT( objectDecl ); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void FixNames::postvisit( FunctionDecl * functionDecl ) { | 
|---|
| 115 | fixDWT( functionDecl ); | 
|---|
| 116 |  | 
|---|
| 117 | if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) { | 
|---|
| 118 | int nargs = functionDecl->get_functionType()->get_parameters().size(); | 
|---|
| 119 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { | 
|---|
| 120 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n"); | 
|---|
| 121 | } | 
|---|
| 122 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); | 
|---|
| 123 | CodeGen::FixMain::registerMain( functionDecl ); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void FixNames::previsit( CompoundStmt * ) { | 
|---|
| 128 | scopeLevel++; | 
|---|
| 129 | GuardAction( [this](){ scopeLevel--; } ); | 
|---|
| 130 | } | 
|---|
| 131 | } // namespace CodeGen | 
|---|
| 132 |  | 
|---|
| 133 | // Local Variables: // | 
|---|
| 134 | // tab-width: 4 // | 
|---|
| 135 | // mode: c++ // | 
|---|
| 136 | // compile-command: "make install" // | 
|---|
| 137 | // End: // | 
|---|