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