Changes in / [2174191:2da12ae]


Ignore:
Location:
src
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.cpp

    r2174191 r2da12ae  
    2020#include <unordered_map>
    2121
     22#include "CodeGen/FixMain.h"   // for FixMain
    2223#include "Common/Eval.h"       // for eval
    2324
     
    7576        }
    7677        this->type = ftype;
     78        // Hack forcing the function "main" to have Cforall linkage to replace
     79        // main even if it is inside an extern "C", and also makes sure the
     80        // replacing function is always a C function.
     81        if ( name == "main" ) {
     82                this->linkage = CodeGen::FixMain::getMainLinkage();
     83        }
    7784}
    7885
     
    101108        }
    102109        this->type = type;
     110        // See note above about this hack.
     111        if ( name == "main" ) {
     112                this->linkage = CodeGen::FixMain::getMainLinkage();
     113        }
    103114}
    104115
  • src/AST/Type.hpp

    r2174191 r2da12ae  
    3434
    3535namespace ast {
     36
     37template< typename T > class Pass;
    3638
    3739class Type : public Node {
  • src/CodeGen/FixMain.cc

    r2174191 r2da12ae  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // FixMain.cc -- Tools to change a Cforall main into a C main.
     7// FixMain.cc --
    88//
    99// Author           : Thierry Delisle
     
    3333namespace {
    3434
    35 struct FindMainCore final {
     35struct FindMainCore_new {
    3636        ast::FunctionDecl const * main_declaration = nullptr;
    3737
    3838        void previsit( ast::FunctionDecl const * decl ) {
    39                 if ( isMain( decl ) ) {
     39                if ( FixMain::isMain( decl ) ) {
    4040                        if ( main_declaration ) {
    4141                                SemanticError( decl, "Multiple definition of main routine\n" );
     
    106106}
    107107
    108 struct 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 
    118108} // namespace
    119109
    120 bool isMain( const ast::FunctionDecl * decl ) {
     110bool FixMain::isMain( const ast::FunctionDecl * decl ) {
    121111        if ( std::string("main") != decl->name ) {
    122112                return false;
     
    125115}
    126116
    127 void 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 
    134 void fixMainInvoke( ast::TranslationUnit & translationUnit,
     117void FixMain::fix( ast::TranslationUnit & translationUnit,
    135118                std::ostream &os, const char * bootloader_filename ) {
    136119
    137         ast::Pass<FindMainCore> main_finder;
     120        ast::Pass<FindMainCore_new> main_finder;
    138121        ast::accept_all( translationUnit, main_finder );
    139122        if ( nullptr == main_finder.core.main_declaration ) return;
  • src/CodeGen/FixMain.h

    r2174191 r2da12ae  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // FixMain.h -- Tools to change a Cforall main into a C main.
     7// FixMain.h --
    88//
    99// Author           : Thierry Delisle
     
    1717
    1818#include <iosfwd>
     19#include <memory>
     20#include <list>
     21
     22#include "AST/LinkageSpec.hpp"
    1923
    2024namespace ast {
     
    2529namespace CodeGen {
    2630
    27 /// Is this function a program main function?
    28 bool isMain( const ast::FunctionDecl * decl );
     31class FixMain {
     32public :
     33        static inline ast::Linkage::Spec getMainLinkage() {
     34                return replace_main ? ast::Linkage::Cforall : ast::Linkage::C;
     35        }
    2936
    30 /// Adjust the linkage of main functions.
    31 void fixMainLinkage( ast::TranslationUnit & transUnit, bool replaceMain );
     37        static inline void setReplaceMain(bool val) {
     38                replace_main = val;
     39        }
    3240
    33 /// Add a wrapper around to run the Cforall main.
    34 void fixMainInvoke( ast::TranslationUnit & transUnit,
    35                 std::ostream & os, const char * bootloaderFilename );
     41        static bool isMain(const ast::FunctionDecl * decl);
     42
     43        static void fix( ast::TranslationUnit & translationUnit,
     44                        std::ostream &os, const char * bootloader_filename );
     45
     46private:
     47        static bool replace_main;
     48};
    3649
    3750} // namespace CodeGen
  • src/CodeGen/FixNames.cc

    r2174191 r2da12ae  
    2828
    2929namespace CodeGen {
    30 
    31 namespace {
    32 
    3330/// Does work with the main function and scopeLevels.
    34 class FixNames final {
     31class FixNames_new final {
    3532        int scopeLevel = 1;
    3633
     
    4845
    4946        const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) {
    50                 if ( isMain( functionDecl ) ) {
     47                if ( FixMain::isMain( functionDecl ) ) {
    5148                        auto mutDecl = ast::mutate( functionDecl );
    5249
     
    8380};
    8481
    85 } // namespace
    86 
    8782void fixNames( ast::TranslationUnit & translationUnit ) {
    88         ast::Pass<FixNames>::run( translationUnit );
     83        ast::Pass<FixNames_new>::run( translationUnit );
    8984}
    9085
  • src/CodeGen/FixNames.h

    r2174191 r2da12ae  
    1616#pragma once
    1717
     18#include <list>  // for list
     19
     20class Declaration;
    1821namespace ast {
    1922        class TranslationUnit;
     
    2124
    2225namespace CodeGen {
    23 
     26        /// mangles object and function names
     27        void fixNames( std::list< Declaration* > & translationUnit );
    2428/// Sets scope levels and fills in main's default return.
    2529void fixNames( ast::TranslationUnit & translationUnit );
    26 
    2730} // namespace CodeGen
    2831
  • src/CodeGen/module.mk

    r2174191 r2da12ae  
    1818        CodeGen/CodeGeneratorNew.cpp \
    1919        CodeGen/CodeGeneratorNew.hpp \
     20        CodeGen/FixMain2.cc \
     21        CodeGen/FixMain.h \
    2022        CodeGen/GenType.cc \
    2123        CodeGen/GenType.h \
     
    2729        CodeGen/Generate.h \
    2830        CodeGen/FixMain.cc \
    29         CodeGen/FixMain.h \
    3031        CodeGen/FixNames.cc \
    3132        CodeGen/FixNames.h \
  • src/main.cc

    r2174191 r2da12ae  
    250250
    251251        parse_cmdline( argc, argv );                                            // process command-line arguments
     252        CodeGen::FixMain::setReplaceMain( !nomainp );
    252253
    253254        if ( waiting_for_gdb ) {
     
    393394                PASS( "Translate Tries", ControlStruct::translateTries, transUnit );
    394395                PASS( "Gen Waitfor", Concurrency::generateWaitFor, transUnit );
    395                 PASS( "Fix Main Linkage", CodeGen::fixMainLinkage, transUnit, !nomainp );
    396396
    397397                // Needs to happen before tuple types are expanded.
     
    421421
    422422                PASS( "Code Gen", CodeGen::generate, transUnit, *output, !genproto, prettycodegenp, true, linemarks, false );
    423                 CodeGen::fixMainInvoke( transUnit, *output, (PreludeDirector + "/bootloader.c").c_str() );
    424 
     423
     424                CodeGen::FixMain::fix( transUnit, *output, (PreludeDirector + "/bootloader.c").c_str() );
    425425                if ( output != &cout ) {
    426426                        delete output;
Note: See TracChangeset for help on using the changeset viewer.