| [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 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
 | 16 | #include "FixNames.h"
 | 
|---|
| [bf2438c] | 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
 | 
|---|
| [0270824] | 32 | 
 | 
|---|
| [51b73452] | 33 | namespace CodeGen {
 | 
|---|
| [51587aa] | 34 |         class FixNames : public Visitor {
 | 
|---|
 | 35 |           public:
 | 
|---|
 | 36 |                 virtual void visit( ObjectDecl *objectDecl );
 | 
|---|
 | 37 |                 virtual void visit( FunctionDecl *functionDecl );
 | 
|---|
| [f326f99] | 38 | 
 | 
|---|
 | 39 |                 virtual void visit( CompoundStmt *compoundStmt );
 | 
|---|
 | 40 |           private:
 | 
|---|
 | 41 |                 int scopeLevel = 1;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |                 void fixDWT( DeclarationWithType *dwt );
 | 
|---|
| [51587aa] | 44 |         };
 | 
|---|
 | 45 | 
 | 
|---|
| [0270824] | 46 |         std::string mangle_main() {
 | 
|---|
 | 47 |                 FunctionType* main_type;
 | 
|---|
| [68fe077a] | 48 |                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
 | 
|---|
| [a7c90d4] | 49 |                                                                                                                                    main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )
 | 
|---|
 | 50 |                                 };
 | 
|---|
| [bf2438c] | 51 |                 main_type->get_returnVals().push_back(
 | 
|---|
| [68fe077a] | 52 |                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
 | 
|---|
| [0270824] | 53 |                 );
 | 
|---|
 | 54 | 
 | 
|---|
| [dd020c0] | 55 |                 auto && name = SymTab::Mangler::mangle( mainDecl.get() );
 | 
|---|
| [0270824] | 56 |                 // std::cerr << name << std::endl;
 | 
|---|
 | 57 |                 return name;
 | 
|---|
 | 58 |         }
 | 
|---|
 | 59 |         std::string mangle_main_args() {
 | 
|---|
 | 60 |                 FunctionType* main_type;
 | 
|---|
| [bf2438c] | 61 |                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
 | 
|---|
| [a7c90d4] | 62 |                                                                                                                                    main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )
 | 
|---|
 | 63 |                                 };
 | 
|---|
| [bf2438c] | 64 |                 main_type->get_returnVals().push_back(
 | 
|---|
| [68fe077a] | 65 |                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
 | 
|---|
| [0270824] | 66 |                 );
 | 
|---|
 | 67 | 
 | 
|---|
| [bf2438c] | 68 |                 mainDecl->get_functionType()->get_parameters().push_back(
 | 
|---|
| [68fe077a] | 69 |                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
 | 
|---|
| [0270824] | 70 |                 );
 | 
|---|
 | 71 | 
 | 
|---|
| [bf2438c] | 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 ) ) ),
 | 
|---|
| [0270824] | 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) {
 | 
|---|
| [bf2438c] | 84 |                 static std::string mains[] = {
 | 
|---|
 | 85 |                         mangle_main(),
 | 
|---|
| [0270824] | 86 |                         mangle_main_args()
 | 
|---|
 | 87 |                 };
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |                 for(const auto& m : mains) {
 | 
|---|
 | 90 |                         if( name == m ) return true;
 | 
|---|
 | 91 |                 }
 | 
|---|
 | 92 |                 return false;
 | 
|---|
 | 93 |         }
 | 
|---|
 | 94 | 
 | 
|---|
| [51587aa] | 95 |         void fixNames( std::list< Declaration* > translationUnit ) {
 | 
|---|
 | 96 |                 FixNames fixer;
 | 
|---|
 | 97 |                 acceptAll( translationUnit, fixer );
 | 
|---|
 | 98 |         }
 | 
|---|
 | 99 | 
 | 
|---|
| [f326f99] | 100 |         void FixNames::fixDWT( DeclarationWithType *dwt ) {
 | 
|---|
| [51587aa] | 101 |                 if ( dwt->get_name() != "" ) {
 | 
|---|
| [fa4805f] | 102 |                         if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
 | 
|---|
| [51587aa] | 103 |                                 dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
 | 
|---|
| [f326f99] | 104 |                                 dwt->set_scopeLevel( scopeLevel );
 | 
|---|
| [51587aa] | 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 );
 | 
|---|
| [0270824] | 117 | 
 | 
|---|
 | 118 |                 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
 | 
|---|
| [13de47bc] | 119 |                         int nargs = functionDecl->get_functionType()->get_parameters().size();
 | 
|---|
 | 120 |                         if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
 | 
|---|
| [bf2438c] | 121 |                                 throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", functionDecl);
 | 
|---|
| [0270824] | 122 |                         }
 | 
|---|
| [d56e5bc] | 123 |                         functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant::from_int( 0 ) ) ) );
 | 
|---|
| [13de47bc] | 124 |                         CodeGen::FixMain::registerMain( functionDecl );
 | 
|---|
| [0270824] | 125 |                 }
 | 
|---|
| [51587aa] | 126 |         }
 | 
|---|
| [f326f99] | 127 | 
 | 
|---|
 | 128 |         void FixNames::visit( CompoundStmt *compoundStmt ) {
 | 
|---|
 | 129 |                 scopeLevel++;
 | 
|---|
 | 130 |                 Visitor::visit( compoundStmt );
 | 
|---|
 | 131 |                 scopeLevel--;
 | 
|---|
 | 132 |         }
 | 
|---|
| [51b73452] | 133 | } // namespace CodeGen
 | 
|---|
| [51587aa] | 134 | 
 | 
|---|
 | 135 | // Local Variables: //
 | 
|---|
 | 136 | // tab-width: 4 //
 | 
|---|
 | 137 | // mode: c++ //
 | 
|---|
 | 138 | // compile-command: "make install" //
 | 
|---|
 | 139 | // End: //
 | 
|---|