Ignore:
Timestamp:
Nov 14, 2023, 12:19:09 PM (23 months ago)
Author:
caparson <caparson@…>
Branches:
master
Children:
1ccae59, 89a8bab
Parents:
df8ba61a (diff), 5625427 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/FixMain.cc

    rdf8ba61a r8d182b1  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // FixMain.cc --
     7// FixMain.cc -- Tools to change a Cforall main into a C main.
    88//
    99// Author           : Thierry Delisle
     
    2525#include "AST/Type.hpp"
    2626#include "AST/Vector.hpp"
    27 #include "Common/PassVisitor.h"
    2827#include "Common/SemanticError.h"  // for SemanticError
    2928#include "CodeGen/GenType.h"       // for GenType
    30 #include "SynTree/Declaration.h"   // for FunctionDecl, operator<<
    31 #include "SynTree/Type.h"          // for FunctionType
    3229#include "SymTab/Mangler.h"
    3330
     
    3633namespace {
    3734
    38 struct FindMainCore {
    39         FunctionDecl * main_signature = nullptr;
    40 
    41         void previsit( FunctionDecl * decl ) {
    42                 if ( FixMain::isMain( decl ) ) {
    43                         if ( main_signature ) {
    44                                 SemanticError( decl, "Multiple definition of main routine\n" );
    45                         }
    46                         main_signature = decl;
    47                 }
    48         }
    49 };
    50 
    51 struct FindMainCore_new {
     35struct FindMainCore final {
    5236        ast::FunctionDecl const * main_declaration = nullptr;
    5337
    5438        void previsit( ast::FunctionDecl const * decl ) {
    55                 if ( FixMain::isMain( decl ) ) {
     39                if ( isMain( decl ) ) {
    5640                        if ( main_declaration ) {
    5741                                SemanticError( decl, "Multiple definition of main routine\n" );
     
    6549        return genType( types[at], "", Options( false, false, false, false ) );
    6650}
    67 
    68 }
    69 
    70         template<typename container>
    71         std::string genTypeAt(const container& p, size_t idx) {
    72                 return genType((*std::next(p.begin(), idx))->get_type(), "");
    73         }
    74 
    75         void FixMain::fix( std::list< Declaration * > & translationUnit,
    76                         std::ostream &os, const char* bootloader_filename ) {
    77                 PassVisitor< FindMainCore > main_finder;
    78                 acceptAll( translationUnit, main_finder );
    79                 FunctionDecl * main_signature = main_finder.pass.main_signature;
    80 
    81                 if( main_signature ) {
    82                         os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
    83                         main_signature->mangleName = SymTab::Mangler::mangle(main_signature);
    84 
    85                         os << main_signature->get_scopedMangleName() << "(";
    86                         const auto& params = main_signature->get_functionType()->get_parameters();
    87                         switch(params.size()) {
    88                                 case 3: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv, (" << genTypeAt(params, 2) << ")envp"; break;
    89                                 case 2: os << "(" << genTypeAt(params, 0) << ")argc, (" << genTypeAt(params, 1) << ")argv"; break;
    90                                 case 0: break;
    91                                 default : assert(false);
    92                         }
    93                         os << "); }\n";
    94 
    95                         std::ifstream bootloader(bootloader_filename, std::ios::in);
    96                         assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
    97                         os << bootloader.rdbuf();
    98                 }
    99         }
    100 
    101 namespace {
    10251
    10352ast::ObjectDecl * makeIntObj(){
     
    157106}
    158107
     108struct FixLinkageCore final {
     109        ast::Linkage::Spec const spec;
     110        FixLinkageCore( ast::Linkage::Spec spec ) : spec( spec ) {}
     111
     112        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ) {
     113                if ( decl->name != "main" ) return decl;
     114                return ast::mutate_field( decl, &ast::FunctionDecl::linkage, spec );
     115        }
     116};
     117
    159118} // namespace
    160119
    161 bool FixMain::isMain( FunctionDecl * decl ) {
    162         if ( std::string("main") != decl->name ) {
    163                 return false;
    164         }
    165         return is_main( SymTab::Mangler::mangle( decl, true, true ) );
    166 }
    167 
    168 bool FixMain::isMain( const ast::FunctionDecl * decl ) {
     120bool isMain( const ast::FunctionDecl * decl ) {
    169121        if ( std::string("main") != decl->name ) {
    170122                return false;
     
    173125}
    174126
    175 void FixMain::fix( ast::TranslationUnit & translationUnit,
     127void fixMainLinkage( ast::TranslationUnit & translationUnit,
     128                bool replace_main ) {
     129        ast::Linkage::Spec const spec =
     130                ( replace_main ) ? ast::Linkage::Cforall : ast::Linkage::C;
     131        ast::Pass<FixLinkageCore>::run( translationUnit, spec );
     132}
     133
     134void fixMainInvoke( ast::TranslationUnit & translationUnit,
    176135                std::ostream &os, const char * bootloader_filename ) {
    177136
    178         ast::Pass<FindMainCore_new> main_finder;
     137        ast::Pass<FindMainCore> main_finder;
    179138        ast::accept_all( translationUnit, main_finder );
    180139        if ( nullptr == main_finder.core.main_declaration ) return;
Note: See TracChangeset for help on using the changeset viewer.