Goose  Artifact [66c9327c65]

Artifact 66c9327c659f1f1db7e3f68705f9ed65bd3c0d997a933364296165bd2b2e4d3a:

  • File bs/g0api/compiler.cpp — part of check-in [dd5e98c20d] at 2021-09-19 14:42:21 on branch trunk — g0 api: #CompileFileToFunction takes the identity to use as root as parameter (user: achavasse size: 8994)

#include "g0api/g0api.h"
#include "parse/parse.h"
#include "verify/verify.h"
#include "execute/execute.h"
#include "compile/compiler.h"

using namespace goose;
using namespace goose::compile;

namespace goose::g0api
{
    void SetupApiCompiler( Env& e )
    {
        wptr< Env > pEnv = e.shared_from_this();

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationTraceMode"_sid,
            []( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetTraceMode( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnFailure"_sid,
            []( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetDumpSolverOnFailure( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnSuccess"_sid,
            []( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetDumpSolverOnSuccess( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( uint32_t ) > >( e, "#SetExecutionBudget"_sid,
            []( auto&& c, const Value& budget )
            {
                static bool used = false;

                if( !budget.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( budget.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                if( used )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( 0,
                        "#SetExecutionBudget can only be used once." );
                    return;
                }

                used = true;
                execute::VM::SetExecutionBudget( *FromValue< uint32_t >( budget ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( Value ) > >( e, "#DumpValue"_sid,
            []( auto&& c, const Value& val )
            {
                stringstream s;
                s << val;
                DiagnosticsManager::GetInstance().emitTraceMessage( val.locationId(), s.str() );
            } );

        RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalFunction"_sid,
            []( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return PoisonValue();

                return ToValue( BuildExternalFunc( *ft, symbol ) );
            } );

        RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalVarArgFunction"_sid,
            []( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return PoisonValue();

                return ToValue( BuildExternalFunc( *ft, symbol, true ) );
            } );

        RegisterBuiltinFunc< void ( string, Value ) >( e, "CreateConstant"_sid,
            [pEnv]( const string& name, const Value& v )
            {
                if( !v.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                pEnv.lock()->storeValue(
                    AppendToVectorTerm( RootG0Identity(), StringId( name.c_str() ) ),
                    ANYTERM( _ ), ValueToEIR( v ) );
            } );

        RegisterBuiltinFunc< Intrinsic< uint32_t ( string ) > >( e, "#Include"_sid,
            [pEnv]( auto&& c, const Value& fnameval ) -> Value
            {
                auto filename = *FromValue< string >( fnameval );

                auto identity = TakeVectorTerm( c.identity(), VecSize( c.identity() ) - 1 );
                get< pvec >( identity )->terms().back() = TSID( locvars );

                auto result = Compiler::LoadAndExecuteFile( pEnv.lock(), filename, identity,
                    GetValueType< uint32_t >(), ToValue< uint32_t >( 0 ) );

                if( !result )
                    return ToValue< uint32_t >( 0 );

                return *result;
            } );

        RegisterBuiltinFunc< uint32_t ( string ) >( e, "ExecuteFile"_sid,
            [pEnv]( const string& filename ) -> Value
            {
                // For now generate an unique identity for the file to execute. Later we might want to
                // pass an identity as parameters as a wrapped term.
                auto identity = AppendToVectorTerm( RootG0Identity(),
                    TERM( StringId( Env::NewUniqueId() ) ) );
                auto locVarsIdentity = AppendToVectorTerm( identity, TSID( locvars ) );

                auto env = pEnv.lock();
                env->addVisibilityRule( builtins::RootG0Identity(), identity );
                env->addVisibilityRule( identity, locVarsIdentity );

                auto result = Compiler::LoadAndExecuteFile( env, filename, locVarsIdentity,
                    GetValueType< uint32_t >(), ToValue< uint32_t >( 0 ) );

                if( !result )
                    return ToValue< uint32_t >( 0 );

                return *result;
            } );

        RegisterBuiltinFunc< Intrinsic< Value ( string, TypeWrapper< Term >, Value, Value, Value ) > >( e, "#CompileFileToFunction"_sid,
            [pEnv]( auto&& c, const Value& filenameVal, const Value& identityVal, const Value& rt, const Value& defReturnValue, const Value& params ) -> Value
            {
                if( !filenameVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( filenameVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return PoisonValue();
                }

                if( !identityVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( identityVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return PoisonValue();
                }

                auto filename = *FromValue< string >( filenameVal );
                auto identity = *FromValue< TypeWrapper< Term > >( identityVal );

                // Validate those generic value inputs
                if( !IsType( c, rt ) )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( rt.locationId(),
                        "type expected.", 0 );
                    return PoisonValue();
                }

                if( CheckParamListKind( params ) != ParamListKind::Regular )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( params.locationId(),
                        "template parameter lists are not supported here.", 0 );
                    return PoisonValue();
                }

                sema::Context localC( pEnv.lock(), identity, ValueToEIR( rt ) );
                auto ftype = BuildFuncType( localC, rt, params );
                if( !ftype )
                    return PoisonValue();

                auto funcIdentity = AppendToVectorTerm( builtins::RootG0Identity(),
                    TERM( StringId( Env::NewUniqueId() ) ) );
                c.env()->addVisibilityRule( builtins::RootG0Identity(), funcIdentity );

                auto locVarsIdentity = AppendToVectorTerm( funcIdentity, TSID( locvars ) );
                c.env()->addVisibilityRule( funcIdentity, locVarsIdentity );

                auto func = BuildFunc( localC, *ftype, funcIdentity, params, nullptr, c );
                const auto& pFuncCIR = func.cir();

                auto cfg = Compiler::LoadAndParseFile( pEnv.lock(), filename, locVarsIdentity,
                    ftype->returnType(), defReturnValue );

                if( !cfg )
                    return PoisonValue();

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

                pFuncCIR->body() = cfg;
                return ToValue( func );
            } );
    }
}