Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r61efa42 rcf3da24  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Nov  1 21:12:58 2023
    13 // Update Count     : 690
     12// Last Modified On : Thu Sep 28 22:28:45 2023
     13// Update Count     : 687
    1414//
    1515
     
    2929#include <string>                           // for char_traits, operator<<
    3030
     31#include "AST/Convert.hpp"
    3132#include "AST/Pass.hpp"                     // for pass_visitor_stats
    32 #include "AST/Print.hpp"                    // for printAll
    3333#include "AST/TranslationUnit.hpp"          // for TranslationUnit
    3434#include "AST/Util.hpp"                     // for checkInvariants
     
    3939#include "CodeGen/Generate.h"               // for generate
    4040#include "CodeGen/LinkOnce.h"               // for translateLinkOnce
     41#include "CodeTools/TrackLoc.h"             // for fillLocations
    4142#include "Common/CodeLocationTools.hpp"     // for forceFillCodeLocations
    4243#include "Common/DeclStats.hpp"             // for printDeclStats
     
    6465#include "ResolvExpr/EraseWith.hpp"         // for eraseWith
    6566#include "ResolvExpr/Resolver.h"            // for resolve
     67#include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic
     68#include "SynTree/Declaration.h"            // for Declaration
    6669#include "Tuples/Tuples.h"                  // for expandMemberTuples, expan...
    6770#include "Validate/Autogen.hpp"             // for autogenerateRoutines
     
    9194        using namespace Stats::Counters;
    9295        {
    93                 static auto group = build<CounterGroup>( "Pass Visitor Template" );
     96                static auto group = build<CounterGroup>( "Pass Visitor" );
    9497                auto pass = build<CounterGroup>( name, group );
    95                 ast::pass_visitor_stats.depth = 0;
    96                 ast::pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass );
    97                 ast::pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass );
     98                pass_visitor_stats.depth = 0;
     99                pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass );
     100                pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass );
     101        }
     102        {
     103                static auto group = build<CounterGroup>( "Syntax Node" );
     104                auto pass = build<CounterGroup>( name, group );
     105                BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass );
    98106        }
    99107}
     
    124132
    125133static void parse_cmdline( int argc, char * argv[] );
     134static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
    126135static void dump( ast::TranslationUnit && transUnit, ostream & out = cout );
    127136
     
    237246        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
    238247        ostream * output = & cout;
     248        list< Declaration * > translationUnit;
    239249        ast::TranslationUnit transUnit;
    240250
     
    250260
    251261        parse_cmdline( argc, argv );                                            // process command-line arguments
     262        CodeGen::FixMain::setReplaceMain( !nomainp );
    252263
    253264        if ( waiting_for_gdb ) {
     
    279290
    280291                        // Read to gcc builtins, if not generating the cfa library
    281                         FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cfa").c_str(), "r" );
     292                        FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" );
    282293                        assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" );
    283294                        parse( gcc_builtins, ast::Linkage::Compiler );
    284295
    285296                        // read the extra prelude in, if not generating the cfa library
    286                         FILE * extras = fopen( (PreludeDirector + "/extras.cfa").c_str(), "r" );
     297                        FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" );
    287298                        assertf( extras, "cannot open extras.cf\n" );
    288299                        parse( extras, ast::Linkage::BuiltinC );
     
    295306
    296307                                // Read to cfa builtins, if not generating the cfa library
    297                                 FILE * builtins = fopen( (PreludeDirector + "/builtins.cfa").c_str(), "r" );
     308                                FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" );
    298309                                assertf( builtins, "cannot open builtins.cf\n" );
    299310                                parse( builtins, ast::Linkage::BuiltinCFA );
     
    308319
    309320                Stats::Time::StopBlock();
     321
     322                if (Stats::Counters::enabled) {
     323                        ast::pass_visitor_stats.avg = Stats::Counters::build<Stats::Counters::AverageCounter<double>>("Average Depth - New");
     324                        ast::pass_visitor_stats.max = Stats::Counters::build<Stats::Counters::MaxCounter<double>>("Max depth - New");
     325                }
    310326
    311327                PASS( "Hoist Type Decls", Validate::hoistTypeDecls, transUnit );
     
    393409                PASS( "Translate Tries", ControlStruct::translateTries, transUnit );
    394410                PASS( "Gen Waitfor", Concurrency::generateWaitFor, transUnit );
    395                 PASS( "Fix Main Linkage", CodeGen::fixMainLinkage, transUnit, !nomainp );
    396411
    397412                // Needs to happen before tuple types are expanded.
     
    412427                PASS( "Link-Once", CodeGen::translateLinkOnce, transUnit );
    413428
     429                translationUnit = convert( std::move( transUnit ) );
     430
    414431                // Code has been lowered to C, now we can start generation.
    415432
    416                 DUMP( bcodegenp, std::move( transUnit ) );
     433                DUMP( bcodegenp, translationUnit );
    417434
    418435                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
     
    420437                } // if
    421438
    422                 PASS( "Code Gen", CodeGen::generate, transUnit, *output, !genproto, prettycodegenp, true, linemarks, false );
    423                 CodeGen::fixMainInvoke( transUnit, *output, (PreludeDirector + "/bootloader.c").c_str() );
    424 
     439                CodeTools::fillLocations( translationUnit );
     440                PASS( "Code Gen", CodeGen::generate, translationUnit, *output, ! genproto, prettycodegenp, true, linemarks );
     441
     442                CodeGen::FixMain::fix( translationUnit, *output,
     443                                (PreludeDirector + "/bootloader.c").c_str() );
    425444                if ( output != &cout ) {
    426445                        delete output;
     
    429448                if ( errorp ) {
    430449                        cerr << "---AST at error:---" << endl;
    431                         dump( std::move( transUnit ), cerr );
     450                        // We check which section the errors came from without looking at
     451                        // transUnit because std::move means it could look like anything.
     452                        if ( !translationUnit.empty() ) {
     453                                dump( translationUnit, cerr );
     454                        } else {
     455                                dump( std::move( transUnit ), cerr );
     456                        }
    432457                        cerr << endl << "---End of AST, begin error message:---\n" << endl;
    433458                } // if
     
    455480        } // try
    456481
     482        deleteAll( translationUnit );
    457483        Stats::print();
    458484        return EXIT_SUCCESS;
     
    684710} // parse_cmdline
    685711
    686 static bool notPrelude( ast::ptr<ast::Decl> & decl ) {
    687         return !decl->linkage.is_builtin;
    688 }
    689 
    690 static void dump( ast::TranslationUnit && unit, std::ostream & out ) {
    691         // May filter out all prelude declarations.
     712static bool notPrelude( Declaration * decl ) {
     713        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
     714} // notPrelude
     715
     716static void dump( list< Declaration * > & translationUnit, ostream & out ) {
     717        list< Declaration * > decls;
     718
    692719        if ( genproto ) {
    693                 std::list<ast::ptr<ast::Decl>> decls;
    694                 std::copy_if( unit.decls.begin(), unit.decls.end(),
    695                         std::back_inserter( decls ), notPrelude );
    696                 decls.swap( unit.decls );
    697         }
    698 
    699         // May print as full dump or as code generation.
     720                filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
     721        } else {
     722                decls = translationUnit;
     723        } // if
     724
     725        // depending on commandline options, either generate code or dump the AST
    700726        if ( codegenp ) {
    701                 CodeGen::generate( unit, out, !genproto, prettycodegenp, false, false, false );
     727                CodeGen::generate( decls, out, ! genproto, prettycodegenp );
    702728        } else {
    703                 ast::printAll( out, unit.decls );
    704         }
     729                printAll( decls, out );
     730        } // if
     731        deleteAll( translationUnit );
     732} // dump
     733
     734static void dump( ast::TranslationUnit && transUnit, ostream & out ) {
     735        std::list< Declaration * > translationUnit = convert( std::move( transUnit ) );
     736        dump( translationUnit, out );
    705737}
    706738
Note: See TracChangeset for help on using the changeset viewer.