Goose  Diff

Differences From Artifact [20422b0474]:

  • File bs/compiler.cpp — part of check-in [baf9721752] at 2019-08-19 13:43:31 on branch trunk —
    • Fixed many issues with compile time versus run time function invocations.
    • Improved some error messages.
    (user: achavasse size: 2494)

To Artifact [c47b171239]:

  • File bs/compiler.cpp — part of check-in [0c5641877d] at 2019-08-24 13:42:23 on branch trunk — Factored out the common code of Compiler::execute(), ExecuteFile() and #CompileFileToFunction(). (user: achavasse size: 3279)

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
#include "compiler.h"
#include "builtins/builtins.h"
#include "builtins/helpers.h"
#include "sema/sema.h"
#include "execute/execute.h"
#include "diagnostics/diagnostics.h"

#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"

using namespace empathy;
using namespace empathy::util;
using namespace empathy::ir;

using namespace empathy::diagnostics;

Compiler::Compiler( int argc, char** argv ) :
    m_pEnv( make_shared< sema::Env >() )
{
    llvm::InitLLVM( argc, argv );














>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "compiler.h"
#include "builtins/builtins.h"
#include "builtins/helpers.h"
#include "sema/sema.h"
#include "execute/execute.h"
#include "diagnostics/diagnostics.h"

#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"

using namespace empathy;
using namespace empathy::util;
using namespace empathy::ir;
using namespace empathy::sema;
using namespace empathy::diagnostics;

Compiler::Compiler( int argc, char** argv ) :
    m_pEnv( make_shared< sema::Env >() )
{
    llvm::InitLLVM( argc, argv );

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96






















































    llvm::InitializeAllTargetInfos();
    llvm::InitializeAllTargets();
    llvm::InitializeAllTargetMCs();
    llvm::InitializeAllAsmParsers();
    llvm::InitializeAllAsmPrinters();
}

uint32_t Compiler::execute( istream& source, const string& filename )
{
    auto identity = sema::InjectDomainIntoIdentity( builtins::RootIdentity(), sema::DomainCompileTime() );
    sema::Context c( m_pEnv, identity, GetValueType< uint32_t >() );

    auto r = make_shared< parse::Resolver >( make_shared< lex::Lexer >( source, filename ), c );
    parse::Parser p( r );

    auto cfg = make_shared< llr::CFG >();
    p.setCFG( cfg );
    p.cfg()->setCurrentBB( cfg->entryBB() );

    VerbosityContext vc( Verbosity::Normal, true );

    p.parseSequence();

    if( cfg->isPoisoned() )
        return 0;

    if( !r->eos() )
    {
        DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
            r->getCurrentLocation(), "syntax error." );
        return 0;
    }

    if( !cfg->entryBB()->canBeExecuted() )
    {
        DiagnosticsManager::GetInstance().emitErrorMessage( 0,
            format( "{}: can not be executed.", filename ) );
        return 0;
    }

    execute::VM vm;
    auto result = vm.execute( cfg->entryBB() );

    if( DiagnosticsManager::GetInstance().errorsWereEmitted() )
        return 0;

    if( !result )
        return 1;

    if( result->isPoison() )
        return 0;

    return *FromValue< uint32_t >( *result );
}





























































|


<

<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<












>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
44
45
46
47
48
49
50
51
52
53

54


55



























56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    llvm::InitializeAllTargetInfos();
    llvm::InitializeAllTargets();
    llvm::InitializeAllTargetMCs();
    llvm::InitializeAllAsmParsers();
    llvm::InitializeAllAsmPrinters();
}

uint32_t Compiler::execute( const string& filename )
{
    auto identity = sema::InjectDomainIntoIdentity( builtins::RootIdentity(), sema::DomainCompileTime() );




    auto result = LoadAndExecuteFile( m_pEnv, filename, identity, GetValueType< uint32_t >() );




























    if( DiagnosticsManager::GetInstance().errorsWereEmitted() )
        return 0;

    if( !result )
        return 1;

    if( result->isPoison() )
        return 0;

    return *FromValue< uint32_t >( *result );
}

optional< Value > Compiler::LoadAndExecuteFile( const ptr< Env >& e, const string& filename, const Term& identity, optional< Term > returnType )
{
    auto cfg = LoadAndParseFile( e, filename, identity, returnType );
    if( !cfg )
        return PoisonValue();

    if( !cfg->entryBB()->canBeExecuted() )
    {
        DiagnosticsManager::GetInstance().emitErrorMessage( 0,
            format( "{}: can not be executed.", filename ) );
        return PoisonValue();
    }

    execute::VM vm;
    return vm.execute( cfg->entryBB() );
}


ptr< llr::CFG > Compiler::LoadAndParseFile( const ptr< Env >& e, const string& filename, const Term& identity, optional< Term > returnType )
{
    ifstream sourcefile( filename.c_str() );
    if( !sourcefile.good() )
    {
        DiagnosticsManager::GetInstance().emitErrorMessage( 0,
            format( "can't open '{}'.", filename ) );
        return nullptr;
    }

    sema::Context c( e, identity, returnType );
    DiagnosticsContext dc( 0, true );
    VerbosityContext vc( Verbosity::Normal, true );

    auto r = make_shared< parse::Resolver >(
        make_shared< lex::Lexer >( sourcefile, filename ), c );
    parse::Parser p( r );

    auto cfg = make_shared< llr::CFG >();
    p.setCFG( cfg );
    p.cfg()->setCurrentBB( cfg->entryBB() );

    p.parseSequence();

    if( cfg->isPoisoned() )
        return cfg;

    if( !r->eos() )
    {
        DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
            r->getCurrentLocation(), "syntax error." );
    }

    return cfg;
}