Goose  Artifact [3d70124287]

Artifact 3d70124287530f0d26e828f1d9010f99b1eaca8c607e65011907d1f1165d7bd5:

  • File bs/verify/call.cpp — part of check-in [996d8d9686] at 2021-10-25 19:34:34 on branch trunk — Type predicates: store the expressions as an array of values, which avoids unecessary conversions and results in the inclusion of the CIR from the expressions in the hash (user: zlodo size: 3342)

#include "verify.h"
#include "builtins/builtins.h"
#include "helpers.inl"

using namespace goose::diagnostics;

namespace goose::verify
{
    optional< Z3Val > BuildZ3Op( Builder& b, const Call& instr )
    {
        auto func = FromValue< builtins::Func >( instr.func() );
        if( !func )
            return nullopt;

        const auto& ft = func->type();
        const auto& fvi = ft.verifInfos();
        if( !fvi )
            return nullopt;

        auto retExpr = BuildZ3ConstantFromType( b, ft.returnType(), format( "r{}", b.newUniqueId() ) );

        // Create a temporary builder to construct the z3 expressions out of the
        // function's pre-conditions and postconditions, configured
        // to perform the necessary replacements for arguments and for the
        // return value placeholder.
        Builder cb( b.context(), *b.solver(), b.getPredsDict(), b.remapper() );
        cb.setAssertionHandler( b.getAssertionHandler() );

        // Inject the arguments in the context.
        // They will be picked up by getVars instructions when refered to
        // by the verification conditions.
        uint32_t index = 0;

        ForEachInVectorTerm( instr.args(), [&]( auto&& t )
        {
            auto arg = *EIRToValue( t );

            if( auto zv = BuildZ3ExprFromValue( b, arg ) )
                cb.setVar( index++, move( *zv ) );

            return true;
        } );

        // Asserts the parameter types's predicates.
        uint32_t varId = 0;

        ForEachInVectorTerm( ft.params(), [&]( auto&& param )
        {
            auto result = Decompose( param,
                Vec(
                    Lit( "value"_sid ),
                    Lit( "param"_sid ),
                    SubTerm(),
                    SubTerm(),
                    Val< LocationId >()
                )
            );

            if( !result )
                return true;

            auto&& [type, val, locId] = *result;
            auto paramVal = BuildComputedValue( type, Load( make_shared< Instruction >( VarAddr( varId++ ) ), type ) );

            DiagnosticsContext dc( instr.func().locationId(), "At this call." );
            cb.setLoadMustCheck( true );
            BuildZ3ExprFromValue( cb, paramVal );
            cb.setLoadMustCheck( false );

            return true;
        } );

        // Setup the return value placeholder
        if( retExpr )
            cb.setPlaceholder( "@result"_sid, retExpr->expr );

        // Check preconditions.
        const auto& preConds = fvi->preConditions();
        for( auto&& val : preConds )
        {
            if( auto zv = BuildZ3ExprFromValue( cb, val ) )
            {
                DiagnosticsContext dc( instr.func().locationId(), "At this call." );
                b.checkAssertion( zv->expr, val.locationId() );
            }
        }

        // Add the return type's predicates as assumptions.
        ForEachPredicate( cb, ft.returnType(), retExpr->expr, [&]( auto&& z3expr, auto locId )
        {
            b.assume( z3expr );
        } );

        // Add postconditions as assumptions.
        const auto& postConds = fvi->postConditions();
        for( auto&& val : postConds )
        {
            if( auto zv = BuildZ3ExprFromValue( cb, val ) )
                b.assume( zv->expr );
        }

        return retExpr;
    }
}