Skip to content

Commit b1d6bb4

Browse files
zma-barefootMihai Budiu
authored andcommitted
add ior/xor operators to IR::Constant (#1594)
* add ior/xor operators to IR::Constant
1 parent 248c73b commit b1d6bb4

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ build/
2626
.project
2727
.settings
2828

29+
#vim swaps
30+
.*.swp
31+
.*.swo
32+
.*.swa
33+
2934
# emacs backup
3035
*~
3136

ir/expression.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ IR::Constant::operator&(const IR::Constant &c) const {
125125
return IR::Constant(v);
126126
}
127127

128+
IR::Constant
129+
IR::Constant::operator|(const IR::Constant &c) const {
130+
mpz_class v;
131+
mpz_ior(v.get_mpz_t(), value.get_mpz_t(), c.value.get_mpz_t());
132+
return IR::Constant(v);
133+
}
134+
135+
IR::Constant
136+
IR::Constant::operator^(const IR::Constant &c) const {
137+
mpz_class v;
138+
mpz_xor(v.get_mpz_t(), value.get_mpz_t(), c.value.get_mpz_t());
139+
return IR::Constant(v);
140+
}
141+
128142
IR::Constant
129143
IR::Constant::operator-(const IR::Constant &c) const {
130144
mpz_class v;

ir/expression.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ class Constant : Literal {
213213
Constant operator<<(const unsigned &shift) const;
214214
Constant operator>>(const unsigned &shift) const;
215215
Constant operator&(const Constant &c) const;
216+
Constant operator|(const Constant &c) const;
217+
Constant operator^(const Constant &c) const;
216218
Constant operator-(const Constant &c) const;
217219
Constant operator-() const;
218220
#end

test/gtest/constant_expr_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,30 @@ TEST_F(ConstantExpr, TestUint64) {
115115
EXPECT_EQ(res, val);
116116
}
117117

118+
TEST_F(ConstantExpr, TestIntegerFuncs) {
119+
unsigned int val1 = 0x03030303U;
120+
unsigned int val2 = 0x06060606U;
121+
122+
IR::Constant c1(val1);
123+
IR::Constant c2(val2);
124+
125+
auto lsh_res = c1 << 1;
126+
auto rsh_res = c2 >> 1;
127+
auto and_res = c1 & c2;
128+
auto ior_res = c1 | c2;
129+
auto xor_res = c1 ^ c2;
130+
auto sub_res = c2 - c1;
131+
132+
EXPECT_EQ(lsh_res.asUnsigned(), 0x06060606U);
133+
EXPECT_EQ(rsh_res.asUnsigned(), 0x03030303U);
134+
EXPECT_EQ(and_res.asUnsigned(), 0x02020202U);
135+
EXPECT_EQ(ior_res.asUnsigned(), 0x07070707U);
136+
EXPECT_EQ(xor_res.asUnsigned(), 0x05050505U);
137+
EXPECT_EQ(sub_res.asUnsigned(), 0x03030303U);
138+
139+
IR::Constant c(int(123));
140+
auto neg_res = -c;
141+
EXPECT_EQ(neg_res.asInt(), -123);
142+
}
118143

119144
} // namespace Test

0 commit comments

Comments
 (0)