source: src/CodeGen/FixNames.cc @ 8d7bef2

new-envwith_gc
Last change on this file since 8d7bef2 was 8d7bef2, checked in by Aaron Moss <a3moss@…>, 6 years ago

First compiling build of CFA-CC with GC

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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//
[51b7345]15
16#include "FixNames.h"
[bf2438c]17
18#include <string>                  // for string, operator!=, operator==
19
[c50d54d]20#include "Common/PassVisitor.h"
[bf2438c]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
[51b7345]33namespace CodeGen {
[c50d54d]34        class FixNames : public WithGuards {
[51587aa]35          public:
[c50d54d]36                void postvisit( ObjectDecl *objectDecl );
37                void postvisit( FunctionDecl *functionDecl );
[f326f99]38
[c50d54d]39                void previsit( CompoundStmt *compoundStmt );
[f326f99]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;
[8d7bef2]48                FunctionDecl* mainDecl = new FunctionDecl{ 
49                        "main", Type::StorageClasses(), LinkageSpec::Cforall, 
50                        main_type = new FunctionType{ Type::Qualifiers(), true }, nullptr };
[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
[8d7bef2]55                auto && name = SymTab::Mangler::mangle( mainDecl );
[0270824]56                // std::cerr << name << std::endl;
57                return name;
58        }
59        std::string mangle_main_args() {
60                FunctionType* main_type;
[8d7bef2]61                FunctionDecl* mainDecl = new FunctionDecl{
62                        "main", Type::StorageClasses(), LinkageSpec::Cforall,
63                        main_type = new FunctionType{ Type::Qualifiers(), false }, nullptr };
[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
[fa4c094]68                main_type->get_parameters().push_back(
[68fe077a]69                        new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
[0270824]70                );
71
[fa4c094]72                main_type->get_parameters().push_back(
[bf2438c]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
[8d7bef2]78                auto&& name = SymTab::Mangler::mangle( mainDecl );
[0270824]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
[c50d54d]95        void fixNames( std::list< Declaration* > & translationUnit ) {
96                PassVisitor<FixNames> fixer;
[51587aa]97                acceptAll( translationUnit, fixer );
98        }
99
[c50d54d]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
[c50d54d]109        void FixNames::postvisit( ObjectDecl * objectDecl ) {
[51587aa]110                fixDWT( objectDecl );
111        }
112
[c50d54d]113        void FixNames::postvisit( FunctionDecl * functionDecl ) {
[51587aa]114                fixDWT( functionDecl );
[0270824]115
116                if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
[13de47bc]117                        int nargs = functionDecl->get_functionType()->get_parameters().size();
118                        if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
[a16764a6]119                                SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
[0270824]120                        }
[ba3706f]121                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
[13de47bc]122                        CodeGen::FixMain::registerMain( functionDecl );
[0270824]123                }
[51587aa]124        }
[f326f99]125
[c50d54d]126        void FixNames::previsit( CompoundStmt * ) {
[f326f99]127                scopeLevel++;
[c50d54d]128                GuardAction( [this](){ scopeLevel--; } );
[f326f99]129        }
[51b7345]130} // namespace CodeGen
[51587aa]131
132// Local Variables: //
133// tab-width: 4 //
134// mode: c++ //
135// compile-command: "make install" //
136// End: //
Note: See TracBrowser for help on using the repository browser.