Goose  Artifact [e8b70cc119]

Artifact e8b70cc119a26cc87faaf146ecdb11e07fb3bb47fa1fdb09b580214f22fce701:

  • File bs/builtins/operators/logic.cpp — part of check-in [011df3c4d3] at 2019-08-04 12:53:20 on branch trunk — logic ops: forbid xor between runtime integers of different signedness. (user: achavasse size: 5763)

#include "builtins/builtins.h"
#include "precedence.h"
#include "helpers.h"

using namespace empathy;
using namespace empathy::ir;
using namespace empathy::llr;
using namespace empathy::parse;

namespace empathy::builtins
{
    void SetupLogicOps( Env& e )
    {
        CreatePrefixOp( e, "!"_sid, "operator_not"_sid, precedence::UnaryOps,
            ForType< bool >( []( auto&& operand ) -> Value
            {
                return BuildComputedValue( GetValueType< bool >(),
                    Xor( operand, ToValue( true ) ) );
            } )
        );

        CreateLeftAssInfixOp( e, "^"_sid, "operator_xor"_sid, precedence::OrOp,
            // Logical xor
            ForType< bool >( []( auto&& lhs, auto&& rhs ) -> Value
            {
                return BuildComputedValue( GetValueType< bool >(),
                    Xor( lhs, rhs ) );
            } ),

            // ct_int xor
            ForType< APSInt >( []( auto&& lhs, auto&& rhs ) -> Value
            {
                return BuildComputedValue( GetValueType< APSInt >(),
                    Xor( lhs, rhs ) );
            } ),

            // runtime integer xor, defined to work for any two integers of same
            // bit size and signedness.
            ForType< CustomPattern< RTInteger, RTInteger::Pattern > >(
            []( auto&& lhs, auto&& rhs ) -> Value
            {
                return BuildComputedValue( lhs.type(),
                    Xor( lhs, rhs ) );
            } )
        );

        CreateLeftAssInfixOp( e, "|"_sid, "operator_or"_sid, precedence::OrOp,
            ForType< bool >( []( auto&& lhs, auto&& rhs ) -> Value
            {
                // Build a CFG that implements the control flow for
                // shortcut evaluation.
                auto cfg = make_shared< CFG >();
                auto pLhsBB = cfg->entryBB();
                auto pRhsBB = cfg->createBB();
                auto pEndBB = cfg->createBB();

                // If the lhs is true, skip to the end directly.
                // Otherwise, jump to the BB that computes rhs.
                pLhsBB->setTerminator( CondBranch( lhs, pEndBB, pRhsBB ) );

                auto rhsIndex = cfg->getNewTemporaryIndex();
                pRhsBB->emplace_back( CreateTemporary( cfg->uniqueId(), rhsIndex, rhs ) );
                pRhsBB->setTerminator( Branch( pEndBB ) );

                auto resultIndex = cfg->getNewTemporaryIndex();

                // Build the Phi instruction that will collect the final result.
                auto phi = Phi( *ValueFromIRExpr( GetValueType< bool >() ),
                    2, cfg->uniqueId(), resultIndex );

                // If coming directly from the lhs BB, we know the result is true.
                phi.setIncoming( pLhsBB, ToValue( true ) );

                // Otherwise, the result is whatever was computed by the rhs block.
                phi.setIncoming( pRhsBB, BuildComputedValue( GetValueType< bool >(),
                    GetTemporary( cfg->uniqueId(), rhsIndex ) ) );

                pEndBB->emplace_back( move( phi ) );

                // Build the result val which pulls the temporary created by the
                // cfg above.
                auto resultVal = BuildComputedValue( GetValueType< bool >(),
                    GetTemporary( cfg->uniqueId(), resultIndex ) );

                // Return it
                pEndBB->setTerminator( Ret( move( resultVal ) ) );

                // Pachage our cfg in a value with an inline CFG instruction.
                return BuildComputedValue( GetValueType< bool >(), move( cfg ) );
            } )
        );

        CreateLeftAssInfixOp( e, "&"_sid, "operator_and"_sid, precedence::AndOp,
            ForType< bool >( []( auto&& lhs, auto&& rhs ) -> Value
            {
                // Build a CFG that implements the control flow for
                // shortcut evaluation.
                auto cfg = make_shared< CFG >();
                auto pLhsBB = cfg->entryBB();
                auto pRhsBB = cfg->createBB();
                auto pEndBB = cfg->createBB();

                // If the lhs is false, skip to the end directly.
                // Otherwise, jump to the BB that computes rhs.
                pLhsBB->setTerminator( CondBranch( lhs, pRhsBB, pEndBB ) );

                auto rhsIndex = cfg->getNewTemporaryIndex();
                pRhsBB->emplace_back( CreateTemporary( cfg->uniqueId(), rhsIndex, rhs ) );
                pRhsBB->setTerminator( Branch( pEndBB ) );

                auto resultIndex = cfg->getNewTemporaryIndex();

                // Build the Phi instruction that will collect the final result.
                auto phi = Phi( *ValueFromIRExpr( GetValueType< bool >() ),
                    2, cfg->uniqueId(), resultIndex );

                // If coming directly from the lhs BB, we know the result is false.
                phi.setIncoming( pLhsBB, ToValue( false ) );

                // Otherwise, the result is whatever was computed by the rhs block.
                phi.setIncoming( pRhsBB, BuildComputedValue( GetValueType< bool >(),
                    GetTemporary( cfg->uniqueId(), rhsIndex ) ) );

                pEndBB->emplace_back( move( phi ) );

                // Build the result val which pulls the temporary created by the
                // cfg above.
                auto resultVal = BuildComputedValue( GetValueType< bool >(),
                    GetTemporary( cfg->uniqueId(), resultIndex ) );

                // Return it
                pEndBB->setTerminator( Ret( move( resultVal ) ) );

                // Pachage our cfg in a value with an inline CFG instruction.
                return BuildComputedValue( GetValueType< bool >(), move( cfg ) );
            } )
        );
    }
}