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