Goose  Artifact [85f403dcf8]

Artifact 85f403dcf8106e0fbbf76800f7703dfb53ac041902d766d0e960b7d2722040ed:

  • File bs/g0api/compiler.cpp — part of check-in [ec5428614c] at 2021-09-30 19:14:38 on branch trunk — Added a builtin g0 variable that exposes the name of the target OS, just to be able to quickly hack around some rudimentary mac support (user: zlodo size: 8841)

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

using namespace goose;
using namespace goose::compile;

namespace goose::g0api
{
    void SetupApiCompiler( Env& e )
    {
        DefineConstant( e, "TargetOSName"_sid, ValueToEIR( ToValue( string( TARGET_OS_NAME ) ) ) );

        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< 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 ) );
            } );

        // TODO: Clean: this is no longer needed now that we have an api to access the Env
        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
            {
                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
            {
                ProfileZoneScopedN( "#CompileFileToFunction" );

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

                auto filename = *FromValue< string >( filenameVal );
                ProfileZoneName( filename.c_str(), filename.size() );

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

                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( identity,
                    TERM( StringId( Env::NewUniqueId() ) ) );
                c.env()->addVisibilityRule( identity, funcIdentity );

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

                auto func = BuildFunc( localC, *ftype, identity, 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 );
            } );
    }
}