Changeset f42fc13


Ignore:
Timestamp:
Oct 29, 2021, 2:17:42 PM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
0c577f7
Parents:
eb9c2dc
Message:

Reorganize FixNames/FixMain? to avoid storing main_signature for so long.

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/FixMain.cc

    reb9c2dc rf42fc13  
    2222#include <string>                  // for operator<<
    2323
     24#include "Common/PassVisitor.h"
    2425#include "Common/SemanticError.h"  // for SemanticError
    2526#include "CodeGen/GenType.h"       // for GenType
     
    6566                }
    6667        }
     68
     69namespace {
     70
     71ObjectDecl * signedIntObj() {
     72        return new ObjectDecl(
     73                "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
     74                new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
     75}
     76
     77ObjectDecl * charStarObj() {
     78        return new ObjectDecl(
     79                "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
     80                new PointerType( Type::Qualifiers(),
     81                        new PointerType( Type::Qualifiers(),
     82                                new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
     83                nullptr );
     84}
     85
     86std::string create_mangled_main_function_name( FunctionType * function_type ) {
     87        std::unique_ptr<FunctionDecl> decl( new FunctionDecl(
     88                "main", Type::StorageClasses(), LinkageSpec::Cforall,
     89                function_type, nullptr ) );
     90        return SymTab::Mangler::mangle( decl.get() );
     91}
     92
     93std::string mangled_0_argument_main() {
     94        FunctionType* main_type = new FunctionType( Type::Qualifiers(), true );
     95        main_type->get_returnVals().push_back( signedIntObj() );
     96        return create_mangled_main_function_name( main_type );
     97}
     98
     99std::string mangled_2_argument_main() {
     100        FunctionType* main_type = new FunctionType( Type::Qualifiers(), false );
     101        main_type->get_returnVals().push_back( signedIntObj() );
     102        main_type->get_parameters().push_back( signedIntObj() );
     103        main_type->get_parameters().push_back( charStarObj() );
     104        return create_mangled_main_function_name( main_type );
     105}
     106
     107struct FindMainCore {
     108        void previsit( FunctionDecl * decl ) {
     109                if ( FixMain::isMain( decl ) ) {
     110                        FixMain::registerMain( decl );
     111                }
     112        }
    67113};
     114
     115} // namespace
     116
     117bool FixMain::isMain( FunctionDecl * decl ) {
     118        // This breaks if you move it out of the function.
     119        static const std::string mangled_mains[] = {
     120                mangled_0_argument_main(),
     121                mangled_2_argument_main(),
     122                //mangled_3_argument_main(),
     123        };
     124
     125        if ( std::string("main") != decl->name ) {
     126                return false;
     127        }
     128        auto mangled_name = SymTab::Mangler::mangle( decl, true, true );
     129        for ( auto main_name : mangled_mains ) {
     130                if ( main_name == mangled_name ) return true;
     131        }
     132        return false;
     133}
     134
     135void FixMain::findMain( std::list< Declaration * > & translationUnit ) {
     136        PassVisitor< FindMainCore > mainFinder;
     137        acceptAll( translationUnit, mainFinder );
     138}
     139
     140};
  • src/CodeGen/FixMain.h

    reb9c2dc rf42fc13  
    99// Author           : Thierry Delisle
    1010// Created On       : Thr Jan 12 14:11:09 2017
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Feb 16 03:24:32 2020
    13 // Update Count     : 5
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Oct 29 11:07:00 2021
     13// Update Count     : 6
    1414//
    1515
     
    1818#include <iosfwd>
    1919#include <memory>
     20#include <list>
    2021
    2122#include "SynTree/LinkageSpec.h"
    2223
     24class Declaration;
    2325class FunctionDecl;
    2426
     
    3537
    3638                static void registerMain(FunctionDecl* val);
     39                static bool isMain(FunctionDecl* decl);
    3740
    3841                static void fix(std::ostream &os, const char* bootloader_filename);
     42
     43                static void findMain( std::list< Declaration * > & decls );
    3944
    4045          private:
  • src/CodeGen/FixNames.cc

    reb9c2dc rf42fc13  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Dec 13 23:39:14 2019
    13 // Update Count     : 21
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Oct 29 14:06:00 2021
     13// Update Count     : 22
    1414//
    1515
     
    4646        };
    4747
    48         std::string mangle_main() {
    49                 FunctionType* main_type;
    50                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
    51                                                                                                                                    main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )
    52                                 };
    53                 main_type->get_returnVals().push_back(
    54                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    55                 );
    56 
    57                 auto && name = SymTab::Mangler::mangle( mainDecl.get() );
    58                 // std::cerr << name << std::endl;
    59                 return std::move(name);
    60         }
    61         std::string mangle_main_args() {
    62                 FunctionType* main_type;
    63                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
    64                                                                                                                                    main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )
    65                                 };
    66                 main_type->get_returnVals().push_back(
    67                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    68                 );
    69 
    70                 main_type->get_parameters().push_back(
    71                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    72                 );
    73 
    74                 main_type->get_parameters().push_back(
    75                         new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
    76                         new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
    77                         nullptr )
    78                 );
    79 
    80                 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
    81                 // std::cerr << name << std::endl;
    82                 return std::move(name);
    83         }
    84 
    85         bool is_main(const std::string& name) {
    86                 static std::string mains[] = {
    87                         mangle_main(),
    88                         mangle_main_args()
    89                 };
    90 
    91                 for(const auto& m : mains) {
    92                         if( name == m ) return true;
    93                 }
    94                 return false;
    95         }
    96 
    9748        void fixNames( std::list< Declaration* > & translationUnit ) {
    9849                PassVisitor<FixNames> fixer;
     
    11869                fixDWT( functionDecl );
    11970
    120                 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
     71                if ( FixMain::isMain( functionDecl ) ) {
    12172                        int nargs = functionDecl->get_functionType()->get_parameters().size();
    12273                        if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
     
    12475                        }
    12576                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
    126                         CodeGen::FixMain::registerMain( functionDecl );
    12777                }
    12878        }
  • src/main.cc

    reb9c2dc rf42fc13  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Oct 22 16:06:00 2021
    13 // Update Count     : 653
     12// Last Modified On : Fri Oct 29 10:10:00 2021
     13// Update Count     : 654
    1414//
    1515
     
    471471                PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) );
    472472
     473                CodeGen::FixMain::findMain( translationUnit );
    473474                CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() );
    474475                if ( output != &cout ) {
Note: See TracChangeset for help on using the changeset viewer.