Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Implemented the shift operators. |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
2fdf1d19299ad4ade25d0a3e876b2b72 |
| User & Date: | achavasse 2019-08-05 21:04:06.033 |
Context
|
2019-08-05
| ||
| 21:14 | Fixed shift operators precedence. check-in: 4fe0527143 user: achavasse tags: trunk | |
| 21:04 | Implemented the shift operators. check-in: 2fdf1d1929 user: achavasse tags: trunk | |
| 20:07 | Implemented the bitwise not, bitwise and and bitwise or operators. check-in: c7acd1eba0 user: achavasse tags: trunk | |
Changes
Changes to bs/builtins/operators/helpers.h.
| ︙ | ︙ | |||
75 76 77 78 79 80 81 |
}
template< typename T, typename I >
auto ForType()
{
return []< typename tag >( auto&& e, auto&& pOvlSet, tag t )
{
| < < < < < < < < < < < < | | | | | | < | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
}
template< typename T, typename I >
auto ForType()
{
return []< typename tag >( auto&& e, auto&& pOvlSet, tag t )
{
using intrinsicType = Intrinsic< Value ( T, T ) >;
auto intrinsicFunc = []( const Value& lhs, const Value& rhs )
{
return BuildComputedValue( GetValueType< T >(), I( lhs, rhs ) );
};
pOvlSet->add( e, ToValue< intrinsicType >( move( intrinsicFunc ) ) );
};
}
template< typename T, typename F >
auto ForType( F&& func )
{
return [&]< typename tag >( auto&& e, auto&& pOvlSet, tag t )
|
| ︙ | ︙ |
Changes to bs/builtins/operators/logic.cpp.
| ︙ | ︙ | |||
32 33 34 35 36 37 38 |
)
) );
} )
);
CreateLeftAssInfixOp( e, "^"_sid, "operator_xor"_sid, precedence::OrOp,
// Logical xor
| | | < < < < | < < < < | < < < < | 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
)
) );
} )
);
CreateLeftAssInfixOp( e, "^"_sid, "operator_xor"_sid, precedence::OrOp,
// Logical xor
ForType< bool, Xor >(),
// ct_int xor
ForType< BigInt, Xor >(),
// 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,
// ct_int or
ForType< BigInt, Or >(),
// runtime integer or, 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(),
|
| ︙ | ︙ | |||
120 121 122 123 124 125 126 |
// 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,
// ct_int and
| | < < < < | | 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
// 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,
// ct_int and
ForType< BigInt, And >(),
// runtime integer and, 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(),
And( lhs, rhs ) );
} ),
// bool and
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();
|
| ︙ | ︙ | |||
180 181 182 183 184 185 186 |
// Return it
pEndBB->setTerminator( Ret( move( resultVal ) ) );
// Pachage our cfg in a value with an inline CFG instruction.
return BuildComputedValue( GetValueType< bool >(), move( cfg ) );
} )
);
| | > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
// 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_shift_left"_sid, precedence::OrOp,
// ct_int left shift
ForType< BigInt, Shl >(),
// runtime integer left shift, 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(),
Shl( lhs, rhs ) );
} )
);
CreateLeftAssInfixOp( e, ">>"_sid, "operator_shift_right"_sid, precedence::OrOp,
// ct_int right shift
ForType< BigInt, AShr >(),
// runtime signed integer right shift, defined to work for any two integers of same
// bit size.
ForType< CustomPattern< RTInteger, RTInteger::PatternSigned > >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
return BuildComputedValue( lhs.type(),
AShr( lhs, rhs ) );
} ),
// runtime unsigned integer right shift, defined to work for any two integers of same
// bit size.
ForType< CustomPattern< RTInteger, RTInteger::PatternUnsigned > >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
return BuildComputedValue( lhs.type(),
LShr( lhs, rhs ) );
} )
);
}
}
|
Changes to bs/builtins/types/runtime/basic.cpp.
| ︙ | ︙ | |||
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
const Term& RTInteger::Pattern::GetPattern()
{
static auto pattern = ValueToIRExpr( Value( TypeType(), TVEC( TSID( rt_type ),
MkHole( "llvmType"_sid ),
TSID( rt_integer ), MkHole( "size"_sid ), MkHole( "signedness"_sid ) ) ) );
return pattern;
}
}
namespace empathy::ir
{
//// Half
| > > > > > > > > > > > > > > > > > > | 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
const Term& RTInteger::Pattern::GetPattern()
{
static auto pattern = ValueToIRExpr( Value( TypeType(), TVEC( TSID( rt_type ),
MkHole( "llvmType"_sid ),
TSID( rt_integer ), MkHole( "size"_sid ), MkHole( "signedness"_sid ) ) ) );
return pattern;
}
const Term& RTInteger::PatternSigned::GetPattern()
{
static auto pattern = ValueToIRExpr( Value( TypeType(), TVEC( TSID( rt_type ),
MkHole( "llvmType"_sid ),
TSID( rt_integer ), MkHole( "size"_sid ), TERM( 1ULL ) ) ) );
return pattern;
}
const Term& RTInteger::PatternUnsigned::GetPattern()
{
static auto pattern = ValueToIRExpr( Value( TypeType(), TVEC( TSID( rt_type ),
MkHole( "llvmType"_sid ),
TSID( rt_integer ), MkHole( "size"_sid ), TERM( 0ULL ) ) ) );
return pattern;
}
}
namespace empathy::ir
{
//// Half
|
| ︙ | ︙ |
Changes to bs/builtins/types/runtime/basic.h.
| ︙ | ︙ | |||
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
{}
uint32_t m_numBits = 1;
bool m_signed = false;
// Helpers to provide generic param patterns for builtin funcs
struct Pattern
{
static const Term& GetPattern();
};
};
}
namespace empathy::ir
| > > > > > > > > > > | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
{}
uint32_t m_numBits = 1;
bool m_signed = false;
// Helpers to provide generic param patterns for builtin funcs
struct Pattern
{
static const Term& GetPattern();
};
struct PatternSigned
{
static const Term& GetPattern();
};
struct PatternUnsigned
{
static const Term& GetPattern();
};
};
}
namespace empathy::ir
|
| ︙ | ︙ |
Changes to bs/codegen/binaryops.cpp.
| ︙ | ︙ | |||
50 51 52 53 54 55 56 |
if( !lhs )
return nullptr;
auto rhs = buildValue( c, inf, bo.rhs() );
if( !rhs )
return nullptr;
| | | 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
if( !lhs )
return nullptr;
auto rhs = buildValue( c, inf, bo.rhs() );
if( !rhs )
return nullptr;
return m_llvmBuilder.CreateShl( lhs, rhs );
}
llvm::Value* Module::buildInstruction( const Context& c, Infos& inf, const llr::LShr& bo )
{
auto lhs = buildValue( c, inf, bo.lhs() );
if( !lhs )
return nullptr;
|
| ︙ | ︙ |
Changes to tests/bitwiseops.em.
1 2 3 4 5 6 7 8 9 10 11 12 |
ExecuteFile( "tests/helpers.em" )
if !assert( equals( 0xdeadbeef & 0xabadfeed, 0x8aadbeed ), "bitwise and test" )
return 0
if !assert( equals( 0xdeadbeef | 0xabadfeed, 0xffadfeef ), "bitwise or test" )
return 0
if !assert( equals( 0b110011 ^ 0x2a, 0b011001 ), "bitwise xor test" )
return 0
return 1
| > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
ExecuteFile( "tests/helpers.em" )
if !assert( equals( 0xdeadbeef & 0xabadfeed, 0x8aadbeed ), "bitwise and test" )
return 0
if !assert( equals( 0xdeadbeef | 0xabadfeed, 0xffadfeef ), "bitwise or test" )
return 0
if !assert( equals( 0b110011 ^ 0x2a, 0b011001 ), "bitwise xor test" )
return 0
if !assert( equals( 0xdeadbeef << 4, 0xdeadbeef0 ), "left shift test" )
return 0
if !assert( equals( 0xdeadbeef >> 4, 0xdeadbee ), "right shift test" )
return 0
return 1
|
Changes to tests/codegen/bitwiseops.em.
1 2 3 4 5 6 7 |
using module = CGModuleCreate( "bitwise ops test" )
$T lomarf( $T a, $T b )
{
return a ^ b;
}
| | > > > > > | 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 |
using module = CGModuleCreate( "bitwise ops test" )
$T lomarf( $T a, $T b )
{
return a ^ b;
}
using entryPoint = void( RTUInt( 8 ) a, RTUInt( 8 ) b , RTSInt( 16 ) c, RTSInt( 16 ) d ) {
lomarf( a, b )
lomarf( 219, d )
lomarf( d, 69 )
a ^ b
c ^ d
c ^ 1337
1337 ^ c
~d
a | b
a & b
a << 4
a >> 4
c << 4
c >> 4
}
CGGenerateFunction( module, entryPoint, "main" )
CGModuleEmitLLVMIr( module, "bitwiseops.ll" )
|
Changes to tests/codegen/bitwiseops.ll.
1 2 3 4 5 |
; ModuleID = 'bitwise ops test'
source_filename = "bitwise ops test"
define void @main(i8, i8, i16, i16) {
%5 = call i8 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri8i0_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i8i0_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3"(i8 %0, i8 %1)
| | | > > > > | | 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 |
; ModuleID = 'bitwise ops test'
source_filename = "bitwise ops test"
define void @main(i8, i8, i16, i16) {
%5 = call i8 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri8i0_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i8i0_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3"(i8 %0, i8 %1)
%6 = call i16 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri10i1_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i10i1_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3"(i16 219, i16 %3)
%7 = call i16 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri10i1_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i10i1_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3"(i16 %3, i16 69)
%8 = xor i8 %0, %1
%9 = xor i16 %2, %3
%10 = xor i16 %2, 1337
%11 = xor i16 1337, %2
%12 = xor i16 %3, -1
%13 = or i8 %0, %1
%14 = and i8 %0, %1
%15 = shl i8 %0, 4
%16 = lshr i8 %0, 4
%17 = shl i16 %2, 4
%18 = ashr i16 %2, 4
ret void
}
define private i8 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri8i0_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i8i0_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i8i0_2h$3"(i8, i8) {
%3 = xor i8 %0, %1
ret i8 %3
}
define private i16 @"_3R_4Rs2:e0s6:lomarf_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tpsa:rt_integeri10i1_2q_2_4V_2hs1:__4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4Vo_2ti0_5up_2qR_4Vo_2ti0_5Tp$2i10i1_2q_2_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3_4V_2h$3_4Vo_2ti0_5Tp$2i10i1_2h$3"(i16, i16) {
%3 = xor i16 %0, %1
ret i16 %3
}
|