forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasmops.cpp
375 lines (341 loc) · 8.55 KB
/
asmops.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "parser/srcread.h"
#include "func.h"
#include <iostream>
namespace funC {
/*
*
* ASM-OP LIST FUNCTIONS
*
*/
int is_pos_pow2(td::RefInt256 x) {
if (sgn(x) > 0 && !sgn(x & (x - 1))) {
return x->bit_size(false) - 1;
} else {
return -1;
}
}
int is_neg_pow2(td::RefInt256 x) {
return sgn(x) < 0 ? is_pos_pow2(-x) : 0;
}
std::ostream& operator<<(std::ostream& os, AsmOp::SReg stack_reg) {
int i = stack_reg.idx;
if (i >= 0) {
if (i < 16) {
return os << 's' << i;
} else {
return os << i << " s()";
}
} else if (i >= -2) {
return os << "s(" << i << ')';
} else {
return os << i << " s()";
}
}
AsmOp AsmOp::Const(int arg, std::string push_op) {
std::ostringstream os;
os << arg << ' ' << push_op;
return AsmOp::Const(os.str());
}
AsmOp AsmOp::make_stk2(int a, int b, const char* str, int delta) {
std::ostringstream os;
os << SReg(a) << ' ' << SReg(b) << ' ' << str;
int c = std::max(a, b) + 1;
return AsmOp::Custom(os.str(), c, c + delta);
}
AsmOp AsmOp::make_stk3(int a, int b, int c, const char* str, int delta) {
std::ostringstream os;
os << SReg(a) << ' ' << SReg(b) << ' ' << SReg(c) << ' ' << str;
int m = std::max(a, std::max(b, c)) + 1;
return AsmOp::Custom(os.str(), m, m + delta);
}
AsmOp AsmOp::BlkSwap(int a, int b) {
std::ostringstream os;
if (a == 1 && b == 1) {
return AsmOp::Xchg(0, 1);
} else if (a == 1) {
if (b == 2) {
os << "ROT";
} else {
os << b << " ROLL";
}
} else if (b == 1) {
if (a == 2) {
os << "-ROT";
} else {
os << a << " -ROLL";
}
} else {
os << a << " " << b << " BLKSWAP";
}
return AsmOp::Custom(os.str(), a + b, a + b);
}
AsmOp AsmOp::BlkPush(int a, int b) {
std::ostringstream os;
if (a == 1) {
return AsmOp::Push(b);
} else if (a == 2 && b == 1) {
os << "2DUP";
} else {
os << a << " " << b << " BLKPUSH";
}
return AsmOp::Custom(os.str(), b + 1, a + b + 1);
}
AsmOp AsmOp::BlkDrop(int a) {
std::ostringstream os;
if (a == 1) {
return AsmOp::Pop();
} else if (a == 2) {
os << "2DROP";
} else {
os << a << " BLKDROP";
}
return AsmOp::Custom(os.str(), a, 0);
}
AsmOp AsmOp::BlkDrop2(int a, int b) {
if (!b) {
return BlkDrop(a);
}
std::ostringstream os;
os << a << " " << b << " BLKDROP2";
return AsmOp::Custom(os.str(), a + b, b);
}
AsmOp AsmOp::BlkReverse(int a, int b) {
std::ostringstream os;
os << a << " " << b << " REVERSE";
return AsmOp::Custom(os.str(), a + b, a + b);
}
AsmOp AsmOp::Tuple(int a) {
switch (a) {
case 1:
return AsmOp::Custom("SINGLE", 1, 1);
case 2:
return AsmOp::Custom("PAIR", 2, 1);
case 3:
return AsmOp::Custom("TRIPLE", 3, 1);
}
std::ostringstream os;
os << a << " TUPLE";
return AsmOp::Custom(os.str(), a, 1);
}
AsmOp AsmOp::UnTuple(int a) {
switch (a) {
case 1:
return AsmOp::Custom("UNSINGLE", 1, 1);
case 2:
return AsmOp::Custom("UNPAIR", 1, 2);
case 3:
return AsmOp::Custom("UNTRIPLE", 1, 3);
}
std::ostringstream os;
os << a << " UNTUPLE";
return AsmOp::Custom(os.str(), 1, a);
}
AsmOp AsmOp::IntConst(td::RefInt256 x) {
if (x->signed_fits_bits(8)) {
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT");
}
if (!x->is_valid()) {
return AsmOp::Const("PUSHNAN");
}
int k = is_pos_pow2(x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2");
}
k = is_pos_pow2(x + 1);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2DEC");
}
k = is_pos_pow2(-x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHNEGPOW2");
}
if (!x->mod_pow2_short(23)) {
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINTX");
}
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT");
}
AsmOp AsmOp::BoolConst(bool f) {
return AsmOp::Const(f ? "TRUE" : "FALSE");
}
AsmOp AsmOp::Parse(std::string custom_op) {
if (custom_op == "NOP") {
return AsmOp::Nop();
} else if (custom_op == "SWAP") {
return AsmOp::Xchg(1);
} else if (custom_op == "DROP") {
return AsmOp::Pop(0);
} else if (custom_op == "NIP") {
return AsmOp::Pop(1);
} else if (custom_op == "DUP") {
return AsmOp::Push(0);
} else if (custom_op == "OVER") {
return AsmOp::Push(1);
} else {
return AsmOp::Custom(custom_op);
}
}
AsmOp AsmOp::Parse(std::string custom_op, int args, int retv) {
auto res = Parse(custom_op);
if (res.is_custom()) {
res.a = args;
res.b = retv;
}
return res;
}
void AsmOp::out(std::ostream& os) const {
if (!op.empty()) {
os << op;
return;
}
switch (t) {
case a_none:
break;
case a_xchg:
if (!a && !(b & -2)) {
os << (b ? "SWAP" : "NOP");
break;
}
os << SReg(a) << ' ' << SReg(b) << " XCHG";
break;
case a_push:
if (!(a & -2)) {
os << (a ? "OVER" : "DUP");
break;
}
os << SReg(a) << " PUSH";
break;
case a_pop:
if (!(a & -2)) {
os << (a ? "NIP" : "DROP");
break;
}
os << SReg(a) << " POP";
break;
default:
throw src::Fatal{"unknown assembler operation"};
}
}
void AsmOp::out_indent_nl(std::ostream& os, bool no_eol) const {
for (int i = 0; i < indent; i++) {
os << " ";
}
out(os);
if (!no_eol) {
os << std::endl;
}
}
std::string AsmOp::to_string() const {
if (!op.empty()) {
return op;
} else {
std::ostringstream os;
out(os);
return os.str();
}
}
bool AsmOpList::append(const std::vector<AsmOp>& ops) {
for (const auto& op : ops) {
if (!append(op)) {
return false;
}
}
return true;
}
const_idx_t AsmOpList::register_const(Const new_const) {
if (new_const.is_null()) {
return not_const;
}
unsigned idx;
for (idx = 0; idx < constants_.size(); idx++) {
if (!td::cmp(new_const, constants_[idx])) {
return idx;
}
}
constants_.push_back(std::move(new_const));
return (const_idx_t)idx;
}
Const AsmOpList::get_const(const_idx_t idx) {
if ((unsigned)idx < constants_.size()) {
return constants_[idx];
} else {
return {};
}
}
void AsmOpList::show_var(std::ostream& os, var_idx_t idx) const {
if (!var_names_ || (unsigned)idx >= var_names_->size()) {
os << '_' << idx;
} else {
var_names_->at(idx).show(os, 2);
}
}
void AsmOpList::show_var_ext(std::ostream& os, std::pair<var_idx_t, const_idx_t> idx_pair) const {
auto i = idx_pair.first;
auto j = idx_pair.second;
if (!var_names_ || (unsigned)i >= var_names_->size()) {
os << '_' << i;
} else {
var_names_->at(i).show(os, 2);
}
if ((unsigned)j < constants_.size() && constants_[j].not_null()) {
os << '=' << constants_[j];
}
}
void AsmOpList::out(std::ostream& os, int mode) const {
if (!(mode & 2)) {
for (const auto& op : list_) {
op.out_indent_nl(os);
}
} else {
std::size_t n = list_.size();
for (std::size_t i = 0; i < n; i++) {
const auto& op = list_[i];
if (!op.is_comment() && i + 1 < n && list_[i + 1].is_comment()) {
op.out_indent_nl(os, true);
os << '\t';
do {
i++;
} while (i + 1 < n && list_[i + 1].is_comment());
list_[i].out(os);
os << std::endl;
} else {
op.out_indent_nl(os, false);
}
}
}
}
bool apply_op(StackTransform& trans, const AsmOp& op) {
if (!trans.is_valid()) {
return false;
}
switch (op.t) {
case AsmOp::a_none:
return true;
case AsmOp::a_xchg:
return trans.apply_xchg(op.a, op.b, true);
case AsmOp::a_push:
return trans.apply_push(op.a);
case AsmOp::a_pop:
return trans.apply_pop(op.a);
case AsmOp::a_const:
return !op.a && op.b == 1 && trans.apply_push_newconst();
case AsmOp::a_custom:
return op.is_gconst() && trans.apply_push_newconst();
default:
return false;
}
}
} // namespace funC