Skip to content

Commit ebde55e

Browse files
committed
[Tolk] Simplify VarDescr, drop _Const enum value
Now, only VarDescr::int_const represents whether it's a constant, without being combined (and potentially become unsync) val bit.
1 parent cee4c67 commit ebde55e

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

tolk-tester/tests/logical-operators.tolk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ fun testConvertIfToIfnot(x: bool) {
148148
return -4;
149149
}
150150

151+
@pure
152+
fun get123(): int
153+
asm "123 PUSHINT";
154+
155+
@method_id(114)
156+
fun test114() {
157+
val fals = (get123() < 0) as int;
158+
return (fals ? -1 : fals) != 0;
159+
}
160+
161+
@method_id(115)
162+
fun test115() {
163+
val tru = get123() >= 0;
164+
val fals = get123() < 0;
165+
166+
if ((true || false) && (false || false)) {
167+
throw 123;
168+
}
169+
if ((tru || fals) && (fals || fals)) {
170+
throw 456;
171+
}
172+
return (tru, fals);
173+
}
174+
175+
151176
fun main() {
152177

153178
}
@@ -187,6 +212,8 @@ fun main() {
187212
@testcase | 112 | 5 0 | 0 -1 0 -1 -1
188213
@testcase | 112 | 0 -1 | -1 -1 -1 -1 -1
189214
@testcase | 113 | 0 | 1
215+
@testcase | 114 | | 0
216+
@testcase | 115 | | -1 0
190217

191218
@fif_codegen
192219
"""

tolk-tester/tests/smart-cast-tests.tolk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ fun test10(): int {
148148
return x + y;
149149
}
150150

151+
@method_id(111)
151152
fun test11() {
152153
var [x, y] = [getNullableInt(), getNullableInt()];
153-
if (random()) { return x == null || y == null ? -1 : x + y; }
154+
if (eq(10) < 0) { return x == null || y == null ? -1 : x + y; }
154155
if (true && (x == null || y == null) && !!true) { return 0; }
155156
return x + y;
156157
}
@@ -665,6 +666,7 @@ fun main(x: int?): int {
665666

666667
/**
667668
@testcase | 0 | 1 | 1
669+
@testcase | 111 | | 10
668670
@testcase | 123 | | 7
669671
@testcase | 124 | 4 | 6
670672
@testcase | 124 | null | 15

tolk/abscode.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ void VarDescr::show_value(std::ostream& os) const {
5959
if (val & _Int) {
6060
os << 'i';
6161
}
62-
if (val & _Const) {
63-
os << 'c';
64-
}
6562
if (val & _Zero) {
6663
os << '0';
6764
}
@@ -114,7 +111,7 @@ void VarDescr::set_const(td::RefInt256 value) {
114111
if (!int_const->signed_fits_bits(257)) {
115112
int_const.write().invalidate();
116113
}
117-
val = _Const | _Int;
114+
val = _Int;
118115
int s = sgn(int_const);
119116
if (s < -1) {
120117
val |= _Nan | _NonZero;
@@ -130,41 +127,41 @@ void VarDescr::set_const(td::RefInt256 value) {
130127
}
131128
}
132129

133-
void VarDescr::set_const(std::string value) {
134-
str_const = value;
135-
val = _Const;
130+
void VarDescr::set_const(const std::string&) {
131+
int_const.clear();
132+
val = 0;
136133
}
137134

138135
void VarDescr::operator|=(const VarDescr& y) {
139-
val &= y.val;
140-
if (is_int_const() && y.is_int_const() && cmp(int_const, y.int_const) != 0) {
141-
val &= ~_Const;
142-
}
143-
if (!(val & _Const)) {
144-
int_const.clear();
136+
if (is_int_const()) {
137+
bool y_same = y.is_int_const() && *int_const == *y.int_const;
138+
if (!y_same) {
139+
int_const.clear();
140+
}
145141
}
142+
val &= y.val;
146143
}
147144

148145
void VarDescr::operator&=(const VarDescr& y) {
149-
val |= y.val;
150-
if (y.int_const.not_null() && int_const.is_null()) {
146+
if (y.is_int_const()) {
151147
int_const = y.int_const;
152148
}
149+
val |= y.val;
153150
}
154151

155152
void VarDescr::set_value(const VarDescr& y) {
156-
val = y.val;
157153
int_const = y.int_const;
154+
val = y.val;
158155
}
159156

160157
void VarDescr::set_value(VarDescr&& y) {
161-
val = y.val;
162158
int_const = std::move(y.int_const);
159+
val = y.val;
163160
}
164161

165162
void VarDescr::clear_value() {
166-
val = 0;
167163
int_const.clear();
164+
val = 0;
168165
}
169166

170167
void VarDescrList::show(std::ostream& os) const {

tolk/analyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ bool operator==(const VarDescrList& x, const VarDescrList& y) {
6262
}
6363

6464
bool same_values(const VarDescr& x, const VarDescr& y) {
65-
if (x.val != y.val || x.int_const.is_null() != y.int_const.is_null()) {
65+
if (x.val != y.val || x.is_int_const() != y.is_int_const()) {
6666
return false;
6767
}
68-
if (x.int_const.not_null() && cmp(x.int_const, y.int_const) != 0) {
68+
if (x.is_int_const() && *x.int_const != *y.int_const) {
6969
return false;
7070
}
7171
return true;

tolk/tolk.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ struct VarDescr {
6868
enum { _Last = 1, _Unused = 2 };
6969
int flags;
7070
enum {
71-
_Const = 16,
7271
_Int = 32,
7372
_Zero = 64,
7473
_NonZero = 128,
@@ -79,16 +78,15 @@ struct VarDescr {
7978
_Even = 16384,
8079
_Odd = 32768,
8180
};
82-
static constexpr int ConstZero = _Const | _Int | _Zero | _Pos | _Neg | _Finite | _Even;
83-
static constexpr int ConstOne = _Const | _Int | _NonZero | _Pos | _Finite | _Odd;
84-
static constexpr int ConstTrue = _Const | _Int | _NonZero | _Neg | _Finite | _Odd;
81+
static constexpr int ConstZero = _Int | _Zero | _Pos | _Neg | _Finite | _Even;
82+
static constexpr int ConstOne = _Int | _NonZero | _Pos | _Finite | _Odd;
83+
static constexpr int ConstTrue = _Int | _NonZero | _Neg | _Finite | _Odd;
8584
static constexpr int ValBit = _Int | _Pos | _Finite;
8685
static constexpr int ValBool = _Int | _Neg | _Finite;
8786
static constexpr int FiniteInt = _Int | _Finite;
8887
static constexpr int FiniteUInt = _Int | _Finite | _Pos;
8988
int val;
9089
td::RefInt256 int_const;
91-
std::string str_const;
9290

9391
explicit VarDescr(var_idx_t _idx = -1, int _flags = 0, int _val = 0) : idx(_idx), flags(_flags), val(_val) {
9492
}
@@ -120,7 +118,12 @@ struct VarDescr {
120118
return val & _Odd;
121119
}
122120
bool is_int_const() const {
123-
return (val & (_Int | _Const)) == (_Int | _Const) && int_const.not_null();
121+
#ifdef TOLK_DEBUG
122+
if (int_const.not_null()) {
123+
tolk_assert(val & _Int);
124+
}
125+
#endif
126+
return int_const.not_null();
124127
}
125128
bool always_nonpos() const {
126129
return val & _Neg;
@@ -151,7 +154,7 @@ struct VarDescr {
151154
}
152155
void set_const(long long value);
153156
void set_const(td::RefInt256 value);
154-
void set_const(std::string value);
157+
void set_const(const std::string& value);
155158
void operator+=(const VarDescr& y) {
156159
flags &= y.flags;
157160
}

0 commit comments

Comments
 (0)