Skip to content

Commit 565bc59

Browse files
committed
[Tolk] Refactor: get rid of split_vars, construct valid LET ops
In FunC (and in Tolk before), tensor vars (actually occupying several stack slots) were represented as a single var in terms or IR vars (Ops): > var a = (1, 2); > LET (_i) = (_1, _2) Now, every tensor of N stack slots is represented as N IR vars. > LET (_i, _j) = (_1, _2) This will give an ability to control access to parts of a tensor when implementing `tensorVar.0` syntax.
1 parent 989629a commit 565bc59

17 files changed

+101
-218
lines changed

tolk-tester/tests/a10.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ fun testStartBalanceCodegen2() {
104104
testDumpDontPolluteStack PROC:<{
105105
...
106106
DUMPSTK
107-
x{6d79} PUSHSLICE // f s _9
107+
x{6d79} PUSHSLICE // f s _5
108108
STRDUMP DROP
109-
SBITS // f _11
109+
SBITS // f _6
110110
}>
111111
"""
112112

tolk-tester/tests/cells-slices.tolk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ Note, that since 'compute-asm-ltr' became on be default, chaining methods codege
220220
1 PUSHINT // _0 _1=1
221221
SWAP // _1=1 _0
222222
32 STU // _0
223-
2 PUSHINT // _0 _5=2
224-
SWAP // _5=2 _0
223+
2 PUSHINT // _0 _4=2
224+
SWAP // _4=2 _0
225225
32 STU // _0
226-
3 PUSHINT // _0 _9=3
227-
SWAP // _9=3 _0
226+
3 PUSHINT // _0 _7=3
227+
SWAP // _7=3 _0
228228
32 STU // _0
229229
}>
230230
"""

tolk-tester/tests/logical-operators.tolk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ These are moments of future optimizations. For now, it's more than enough.
332332
DUP // x x
333333
IFNOTJMP:<{ // x
334334
DROP //
335-
1 PUSHINT // _7=1
335+
1 PUSHINT // _5=1
336336
}> // x
337337
DUP // x x
338338
IFNOTJMP:<{ // x
339339
DROP //
340-
1 PUSHINT // _8=1
340+
1 PUSHINT // _6=1
341341
}> // x
342342
100 THROWIFNOT
343-
-4 PUSHINT // _12=-4
343+
-4 PUSHINT // _9=-4
344344
}>
345345
"""
346346

tolk-tester/tests/mutate-methods.tolk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fun main(){}
307307
...
308308
incrementTwoInPlace CALLDICT // x y sum1
309309
-ROT
310-
10 PUSHINT // sum1 x y _8=10
310+
10 PUSHINT // sum1 x y _10=10
311311
incrementTwoInPlace CALLDICT // sum1 x y sum2
312312
s1 s3 s0 XCHG3 // x y sum1 sum2
313313
}>
@@ -317,8 +317,8 @@ fun main(){}
317317
"""
318318
load_next PROC:<{
319319
// cs
320-
32 LDI // _3 cs
321-
SWAP // cs _3
320+
32 LDI // _4 cs
321+
SWAP // cs _4
322322
}>
323323
"""
324324

tolk-tester/tests/null-keyword.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fun main() {
113113
CONS // numbers
114114
UNCONS // h numbers
115115
DUP // h numbers numbers
116-
CAR // h numbers _12
116+
CAR // h numbers _13
117117
"""
118118

119119
@fif_codegen
@@ -133,7 +133,7 @@ fun main() {
133133
"""
134134
test7 PROC:<{
135135
...
136-
LDOPTREF // b _18 _17
136+
LDOPTREF // b _8 _7
137137
DROP // b c
138138
ISNULL // b _11
139139
10 MULCONST // b _13

tolk/abscode.cpp

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ void TmpVar::dump(std::ostream& os) const {
3131
os << " : " << v_type << " (width ";
3232
os << v_type->calc_width_on_stack();
3333
os << ")";
34-
if (coord > 0) {
35-
os << " = _" << (coord >> 8) << '.' << (coord & 255);
36-
} else if (coord < 0) {
37-
int n = (~coord >> 8), k = (~coord & 0xff);
38-
if (k) {
39-
os << " = (_" << n << ".._" << (n + k - 1) << ")";
40-
} else {
41-
os << " = ()";
42-
}
43-
}
4434
os << std::endl;
4535
}
4636

@@ -51,7 +41,7 @@ void TmpVar::show(std::ostream& os, int omit_idx) const {
5141
return;
5242
}
5343
}
54-
os << '_' << idx;
44+
os << '_' << ir_idx;
5545
}
5646

5747
std::ostream& operator<<(std::ostream& os, const TmpVar& var) {
@@ -182,47 +172,6 @@ void VarDescrList::show(std::ostream& os) const {
182172
os << " ]\n";
183173
}
184174

185-
void Op::split_vars(const std::vector<TmpVar>& vars) {
186-
split_var_list(left, vars);
187-
split_var_list(right, vars);
188-
for (auto& op : block0) {
189-
op.split_vars(vars);
190-
}
191-
for (auto& op : block1) {
192-
op.split_vars(vars);
193-
}
194-
}
195-
196-
void Op::split_var_list(std::vector<var_idx_t>& var_list, const std::vector<TmpVar>& vars) {
197-
int new_size = 0, changes = 0;
198-
for (var_idx_t v : var_list) {
199-
int c = vars.at(v).coord;
200-
if (c < 0) {
201-
++changes;
202-
new_size += (~c & 0xff);
203-
} else {
204-
++new_size;
205-
}
206-
}
207-
if (!changes) {
208-
return;
209-
}
210-
std::vector<var_idx_t> new_var_list;
211-
new_var_list.reserve(new_size);
212-
for (var_idx_t v : var_list) {
213-
int c = vars.at(v).coord;
214-
if (c < 0) {
215-
int n = (~c >> 8), k = (~c & 0xff);
216-
while (k-- > 0) {
217-
new_var_list.push_back(n++);
218-
}
219-
} else {
220-
new_var_list.push_back(v);
221-
}
222-
}
223-
var_list = std::move(new_var_list);
224-
}
225-
226175
void Op::show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx, int mode) const {
227176
if (mode & 2) {
228177
os << pfx << " [";
@@ -444,26 +393,22 @@ void CodeBlob::print(std::ostream& os, int flags) const {
444393
os << "-------- END ---------\n\n";
445394
}
446395

447-
var_idx_t CodeBlob::create_var(TypePtr var_type, const LocalVarData* v_sym, SrcLocation location) {
448-
vars.emplace_back(var_cnt, var_type, v_sym, location);
449-
return var_cnt++;
450-
}
451-
452-
bool CodeBlob::import_params(FormalArgList&& arg_list) {
453-
if (var_cnt || in_var_cnt) {
454-
return false;
455-
}
456-
std::vector<var_idx_t> list;
457-
for (const auto& par : arg_list) {
458-
TypePtr arg_type;
459-
const LocalVarData* arg_sym;
460-
SrcLocation arg_loc;
461-
std::tie(arg_type, arg_sym, arg_loc) = par;
462-
list.push_back(create_var(arg_type, arg_sym, arg_loc));
463-
}
464-
emplace_back(loc, Op::_Import, list);
465-
in_var_cnt = var_cnt;
466-
return true;
396+
std::vector<var_idx_t> CodeBlob::create_var(TypePtr var_type, const LocalVarData* v_sym, SrcLocation loc) {
397+
std::vector<var_idx_t> ir_idx;
398+
ir_idx.reserve(var_type->calc_width_on_stack());
399+
if (const TypeDataTensor* t_tensor = var_type->try_as<TypeDataTensor>()) {
400+
for (TypePtr item : t_tensor->items) {
401+
std::vector<var_idx_t> nested = create_var(item, v_sym, loc);
402+
ir_idx.insert(ir_idx.end(), nested.begin(), nested.end());
403+
}
404+
} else if (var_type != TypeDataVoid::create()) {
405+
tolk_assert(var_type->calc_width_on_stack() == 1);
406+
vars.emplace_back(var_cnt, var_type, v_sym, loc);
407+
ir_idx.emplace_back(var_cnt);
408+
var_cnt++;
409+
}
410+
tolk_assert(static_cast<int>(ir_idx.size()) == var_type->calc_width_on_stack());
411+
return ir_idx;
467412
}
468413

469414
} // namespace tolk

tolk/analyzer.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,6 @@ namespace tolk {
2626
*
2727
*/
2828

29-
int CodeBlob::split_vars(bool strict) {
30-
int n = var_cnt, changes = 0;
31-
for (int j = 0; j < var_cnt; j++) {
32-
TmpVar& var = vars[j];
33-
int width_j = var.v_type->calc_width_on_stack();
34-
if (strict && width_j < 0) {
35-
throw ParseError{var.where, "variable does not have fixed width, cannot manipulate it"};
36-
}
37-
if (width_j == 1) {
38-
continue;
39-
}
40-
std::vector<TypePtr> comp_types;
41-
var.v_type->extract_components(comp_types);
42-
tolk_assert(width_j <= 254 && n <= 0x7fff00);
43-
tolk_assert((unsigned)width_j == comp_types.size());
44-
var.coord = ~((n << 8) + width_j);
45-
for (int i = 0; i < width_j; i++) {
46-
auto v = create_var(comp_types[i], vars[j].v_sym, vars[j].where);
47-
tolk_assert(v == n + i);
48-
tolk_assert(vars[v].idx == v);
49-
vars[v].coord = ((int)j << 8) + i + 1;
50-
}
51-
n += width_j;
52-
++changes;
53-
}
54-
if (!changes) {
55-
return 0;
56-
}
57-
for (auto& op : ops) {
58-
op.split_vars(vars);
59-
}
60-
return changes;
61-
}
62-
6329
bool CodeBlob::compute_used_code_vars() {
6430
VarDescrList empty_var_info;
6531
return compute_used_code_vars(ops, empty_var_info, true);

tolk/generics-helpers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ td::Result<std::vector<TypePtr>> deduce_substitutionTs_on_generic_func_call(cons
202202
try {
203203
GenericSubstitutionsDeduceForFunctionCall deducing(called_fun);
204204
for (const LocalVarData& param : called_fun->parameters) {
205-
if (param.declared_type->has_genericT_inside() && param.idx < static_cast<int>(arg_types.size())) {
206-
deducing.consider_next_condition(param.declared_type, arg_types[param.idx]);
205+
if (param.declared_type->has_genericT_inside() && param.param_idx < static_cast<int>(arg_types.size())) {
206+
deducing.consider_next_condition(param.declared_type, arg_types[param.param_idx]);
207207
}
208208
}
209209
int idx = deducing.get_first_not_deduced_idx();
@@ -233,7 +233,7 @@ const FunctionData* instantiate_generic_function(SrcLocation loc, const Function
233233
std::vector<LocalVarData> parameters;
234234
parameters.reserve(fun_ref->get_num_params());
235235
for (const LocalVarData& orig_p : fun_ref->parameters) {
236-
parameters.emplace_back(orig_p.name, orig_p.loc, replace_genericT_with_deduced(orig_p.declared_type, fun_ref->genericTs, substitutionTs), orig_p.flags, orig_p.idx);
236+
parameters.emplace_back(orig_p.name, orig_p.loc, replace_genericT_with_deduced(orig_p.declared_type, fun_ref->genericTs, substitutionTs), orig_p.flags, orig_p.param_idx);
237237
}
238238
TypePtr declared_return_type = replace_genericT_with_deduced(fun_ref->declared_return_type, fun_ref->genericTs, substitutionTs);
239239
const GenericsInstantiation* instantiationTs = new GenericsInstantiation(loc, std::move(substitutionTs));

0 commit comments

Comments
 (0)