Goose  Diff

Differences From Artifact [78a8e40e06]:

  • File bs/verify/value.cpp — part of check-in [5b069c9677] at 2023-11-30 18:29:55 on branch trunk — Handful of fixes and improvements from an abandoned refactor of inline funcs, will go for a different solution (user: zlodo size: 24282)

To Artifact [8e69978cd1]:

  • File bs/verify/value.cpp — part of check-in [1793989d05] at 2024-02-09 18:05:28 on branch trunk — Lowering: completely reworked all lowering to happen in the same two unified extension points upstream of all three CIR consumers (user: zlodo size: 24061)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "verify.h"
#include "builtins/builtins.h"
#include "helpers.inl"

using namespace goose::diagnostics;

namespace goose::verify
{
    Z3Val BuildZ3ValFromConstant( Builder& b, const Value& val )
    {
        optional< TypeInfo > tinfo;

        auto loweredVal = LowerConstantForVerification( b.context(), val );
        if( loweredVal && !loweredVal->isPoison() )
            tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), loweredVal->type() );

        if( !tinfo )
            tinfo = TypeCache::GetInstance().uninterpretedTypeInfo();

        auto zexpr = tinfo->build( b, *loweredVal );
        return Z3Val { move( zexpr ), *EIRToValue( val.type() ), val.locationId() };
    }

    optional< Z3Val > TryBuildZ3ValFromConstant( Builder& b, const Value& val )
    {
        auto loweredVal = LowerConstantForVerification( b.context(), val );
        if( !loweredVal || loweredVal->isPoison() )
            return nullopt;

        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), loweredVal->type() );
        if( !tinfo )
            return nullopt;

        auto zexpr = tinfo->build( b, *loweredVal );
        return Z3Val { move( zexpr ), *EIRToValue( val.type() ), val.locationId() };
    }

    Z3Val BuildZ3ConstantFromType( Builder& b, const Value& type, const string& name )
    {
        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), ValueToEIR( type ) );
        if( !tinfo )












<
<
|




|





<
<
<
<
|



|







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


13
14
15
16
17
18
19
20
21
22
23




24
25
26
27
28
29
30
31
32
33
34
35
#include "verify.h"
#include "builtins/builtins.h"
#include "helpers.inl"

using namespace goose::diagnostics;

namespace goose::verify
{
    Z3Val BuildZ3ValFromConstant( Builder& b, const Value& val )
    {
        optional< TypeInfo > tinfo;



        tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), val.type() );

        if( !tinfo )
            tinfo = TypeCache::GetInstance().uninterpretedTypeInfo();

        auto zexpr = tinfo->build( b, val );
        return Z3Val { move( zexpr ), *EIRToValue( val.type() ), val.locationId() };
    }

    optional< Z3Val > TryBuildZ3ValFromConstant( Builder& b, const Value& val )
    {




        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), val.type() );
        if( !tinfo )
            return nullopt;

        auto zexpr = tinfo->build( b, val );
        return Z3Val { move( zexpr ), *EIRToValue( val.type() ), val.locationId() };
    }

    Z3Val BuildZ3ConstantFromType( Builder& b, const Value& type, const string& name )
    {
        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), ValueToEIR( type ) );
        if( !tinfo )
244
245
246
247
248
249
250

251
252
253
254
255
256
257
        {
            assert( rhs->expr.is_int() );
            b.push( Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), *EIRToValue( GetValueType< bool >() ), instr.locationId() } );
            return true;
        }
        else
        {

            assert( lhs->expr.is_int() );
            b.push( Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ), instr.locationId() } );
            return true;
        }

        return false;
    }







>







238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        {
            assert( rhs->expr.is_int() );
            b.push( Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), *EIRToValue( GetValueType< bool >() ), instr.locationId() } );
            return true;
        }
        else
        {
            cout << lhs->expr << endl;
            assert( lhs->expr.is_int() );
            b.push( Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ), instr.locationId() } );
            return true;
        }

        return false;
    }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
    }

    bool BuildZ3Op( Builder& b, const Load& instr )
    {
        optional< Z3Val > zv;

        if( auto cstAddr = b.pop< Address >() )
            zv = LoadFromAddress( b, *cstAddr, instr.type() );
        else if( auto gfa = b.pop< GhostFuncApplication >() )
            zv = LoadFromAddress( b, *gfa );
        else if( auto symAddr = b.pop< Z3Val >() )
            zv = BuildZ3ConstantFromType( b, instr.type(), format( "val{}", b.newUniqueId() ) );

        if( !zv )
            return false;

        if( b.mustLoadAssume() )
            ForEachPredicate( b, instr.type(), zv->expr, [&]( auto&& z3expr, auto locId )
            {
                b.assume( z3expr );
            } );

        zv->loc = instr.locationId();
        b.push( move( *zv ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const Store& instr )
    {
        auto addr = b.pop< Stack::Slot >();
        if( !addr )
            return false;

        auto zv = b.pop();
        if( !zv )
            return false;

        ForEachPredicate( b, instr.type(), zv->expr, [&]( auto&& z3expr, auto locId )
        {
            if( !instr.srcLocId().invalid() && !instr.destLocId().invalid() )
            {
                DiagnosticsContext dc( instr.destLocId(), "...to this." );
                DiagnosticsContext dc2( instr.srcLocId(), "When assigning this..." );
                b.checkAssertion( z3expr, locId );
            }







|



|





|



















|







314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    }

    bool BuildZ3Op( Builder& b, const Load& instr )
    {
        optional< Z3Val > zv;

        if( auto cstAddr = b.pop< Address >() )
            zv = LoadFromAddress( b, *cstAddr, instr.type().get() );
        else if( auto gfa = b.pop< GhostFuncApplication >() )
            zv = LoadFromAddress( b, *gfa );
        else if( auto symAddr = b.pop< Z3Val >() )
            zv = BuildZ3ConstantFromType( b, instr.type().get(), format( "val{}", b.newUniqueId() ) );

        if( !zv )
            return false;

        if( b.mustLoadAssume() )
            ForEachPredicate( b, instr.type().get(), zv->expr, [&]( auto&& z3expr, auto locId )
            {
                b.assume( z3expr );
            } );

        zv->loc = instr.locationId();
        b.push( move( *zv ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const Store& instr )
    {
        auto addr = b.pop< Stack::Slot >();
        if( !addr )
            return false;

        auto zv = b.pop();
        if( !zv )
            return false;

        ForEachPredicate( b, instr.type().get(), zv->expr, [&]( auto&& z3expr, auto locId )
        {
            if( !instr.srcLocId().invalid() && !instr.destLocId().invalid() )
            {
                DiagnosticsContext dc( instr.destLocId(), "...to this." );
                DiagnosticsContext dc2( instr.srcLocId(), "When assigning this..." );
                b.checkAssertion( z3expr, locId );
            }
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

        b.push( move( *zv ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const AllocVar& instr )
    {
        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), ValueToEIR( instr.type() ) );
        if( !tinfo )
            tinfo = TypeCache::GetInstance().uninterpretedTypeInfo();

        b.setVar( instr.index(), Z3Val{ tinfo->undefined( b ), instr.type(), instr.locationId() } );
        return true;
    }

    // Implemented in phi.cpp
    bool BuildZ3Op( Builder& b, const Phi& instr );

    bool BuildZ3Op( Builder& b, const Not& instr )







|



|







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

        b.push( move( *zv ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const AllocVar& instr )
    {
        auto tinfo = TypeCache::GetInstance().getTypeInfo( b.context(), instr.type().get() );
        if( !tinfo )
            tinfo = TypeCache::GetInstance().uninterpretedTypeInfo();

        b.setVar( instr.index(), Z3Val{ tinfo->undefined( b ), *EIRToValue( instr.type().get() ), instr.locationId() } );
        return true;
    }

    // Implemented in phi.cpp
    bool BuildZ3Op( Builder& b, const Phi& instr );

    bool BuildZ3Op( Builder& b, const Not& instr )
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    }

    bool BuildZ3Op( Builder& b, const Placeholder& instr )
    {
        const auto* expr = b.retrievePlaceholder( instr.name() );
        if( expr )
        {
            b.push( Z3Val{ *expr, *EIRToValue( instr.type() ), instr.locationId() } );
            return true;
        }

        auto result = BuildZ3ConstantFromType( b, instr.type(), format( "p{}", instr.name().str() ) );
        b.push( move( result ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const PHOverrideSet& instr )
    {
        auto phExpr = b.pop();







|



|







604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
    }

    bool BuildZ3Op( Builder& b, const Placeholder& instr )
    {
        const auto* expr = b.retrievePlaceholder( instr.name() );
        if( expr )
        {
            b.push( Z3Val{ *expr, *EIRToValue( instr.type().get() ), instr.locationId() } );
            return true;
        }

        auto result = BuildZ3ConstantFromType( b, instr.type().get(), format( "p{}", instr.name().str() ) );
        b.push( move( result ) );
        return true;
    }

    bool BuildZ3Op( Builder& b, const PHOverrideSet& instr )
    {
        auto phExpr = b.pop();
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    {
        b.unsetPlaceholder( instr.name() );
        return true;
    }

    bool BuildZ3Op( Builder& b, const PushConstant& instr )
    {
        auto v = TryBuildZ3ExprFromValue( b, instr.value() );
        if( v )
            b.push( move( *v ) );
        else
            b.push( instr.value() );
        return true;
    }

    bool BuildZ3Op( Builder& b, const VarAddr& instr )
    {
        b.push( Address( {}, Address::Origin::Stack, instr.varIndex(), instr.locationId() ) );
        return true;







|



|







630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
    {
        b.unsetPlaceholder( instr.name() );
        return true;
    }

    bool BuildZ3Op( Builder& b, const PushConstant& instr )
    {
        auto v = TryBuildZ3ExprFromValue( b, instr.value().get() );
        if( v )
            b.push( move( *v ) );
        else
            b.push( instr.value().get() );
        return true;
    }

    bool BuildZ3Op( Builder& b, const VarAddr& instr )
    {
        b.push( Address( {}, Address::Origin::Stack, instr.varIndex(), instr.locationId() ) );
        return true;