Repository navigation
Expand file tree
/
Copy pathCifaBytecode.cpp
More file actions
3190 lines (3109 loc) · 167 KB
/
Copy pathCifaBytecode.cpp
File metadata and controls
3190 lines (3109 loc) · 167 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "CifaBytecode.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <memory>
#include <optional>
#include <unordered_map>
#include <unordered_set>
namespace cifa {
enum Op : unsigned {
MOVE, LOADI, LOADF, LOADK, LOADKX, LOADFALSE, LFALSESKIP, LOADTRUE, LOADNIL,
GETUPVAL, SETUPVAL, GETTABUP, GETTABLE, GETI, GETFIELD,
SETTABUP, SETTABLE, SETI, SETFIELD, NEWTABLE, SELF,
ADDI, ADDK, SUBK, MULK, MODK, POWK, DIVK, IDIVK,
BANDK, BORK, BXORK, SHRI, SHLI,
ADD, SUB, MUL, MOD, POW, DIV, IDIV, BAND, BOR, BXOR, SHL, SHR,
MMBIN, MMBINI, MMBINK, UNM, BNOT, NOT, LEN, CONCAT, CLOSE, TBC, JMP,
EQ, LT, LE, EQK, EQI, LTI, LEI, GTI, GEI, TEST, TESTSET,
CALL, TAILCALL, RETURN, RETURN0, RETURN1, FORLOOP, FORPREP,
TFORPREP, TFORCALL, TFORLOOP, SETLIST, CLOSURE, VARARG, VARARGPREP, EXTRAARG
};
constexpr unsigned POS_A = 7;
constexpr unsigned POS_k = 15;
constexpr unsigned POS_B = 16;
constexpr unsigned POS_C = 24;
constexpr unsigned POS_Bx = POS_k;
constexpr unsigned POS_Ax = POS_A;
constexpr int OFFSET_sJ = (1 << 24) - 1;
constexpr unsigned LUA_VNUMFLT = 3u | (1u << 4);
constexpr unsigned LUA_VNUMINT = 3u;
constexpr unsigned LUA_VSHRSTR = 4u;
constexpr unsigned LUA_VTRUE = 1u | (1u << 4);
constexpr unsigned LUA_VFALSE = 1u;
static std::uint32_t abc(unsigned op, unsigned a, unsigned b, unsigned c, unsigned k = 0)
{
return op | (a << POS_A) | (k << POS_k) | (b << POS_B) | (c << POS_C);
}
static std::uint32_t abx(unsigned op, unsigned a, unsigned bx)
{
return op | (a << POS_A) | (bx << POS_Bx);
}
static std::uint32_t asj(unsigned op, int jump)
{
return op | (static_cast<unsigned>(jump + OFFSET_sJ) << POS_Ax);
}
static std::uint32_t ax(unsigned op, unsigned value) { return op | (value << POS_Ax); }
constexpr std::size_t profile_instruction_limit = 2000000;
static std::uint64_t diagnostic_now_ns()
{
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
}
/* Lua TString stores its bytes directly after the object header. */
struct CifaLuaString {
unsigned char marked = 0;
unsigned char tt = LUA_VSHRSTR;
unsigned char extra = 0;
unsigned char shrlen = 0;
std::uint32_t hash = 0;
std::size_t length = 0;
CifaLuaString* hnext = nullptr;
char contents[1];
};
static const char* string_data(const CifaLuaString* value) { return value->contents; }
static std::size_t string_size(const CifaLuaString* value) { return value->length; }
static std::string string_copy(const CifaLuaString* value) { return {string_data(value), string_size(value)}; }
static CifaLuaString* new_lua_string(const std::string& text)
{
const std::size_t bytes = offsetof(CifaLuaString, contents) + text.size() + 1;
auto* value = static_cast<CifaLuaString*>(::operator new(bytes));
value->marked = 0;
value->tt = LUA_VSHRSTR;
value->extra = 0;
value->shrlen = static_cast<unsigned char>(std::min<std::size_t>(text.size(), 255));
value->length = text.size();
std::memcpy(value->contents, text.data(), text.size());
value->contents[text.size()] = '\0';
return value;
}
static void free_lua_string(CifaLuaString* value) { ::operator delete(value); }
struct Constant {
enum Kind { Integer, Number, String, Boolean } kind;
std::int64_t integer = 0;
double number = 0;
bool boolean = false;
std::string string;
mutable const CifaLuaString* runtime_string = nullptr;
};
struct UpvalueDesc {
bool instack = false;
unsigned index = 0;
};
static std::string script_function_key(const std::string& name, std::size_t arity)
{
return name + '\x1f' + std::to_string(arity);
}
struct Proto {
std::string source;
std::string debug_name;
unsigned linedefined = 0;
unsigned lastline = 0;
unsigned params = 0;
unsigned vararg = 0;
unsigned maxstack = 2;
std::vector<std::string> parameter_types;
std::string return_type;
std::vector<std::uint32_t> code;
std::vector<std::pair<std::size_t, std::string>> call_frames;
std::vector<std::pair<std::size_t, std::string>> call_errors;
std::vector<std::pair<std::size_t, std::string>> value_errors;
std::vector<std::size_t> create_table_reads;
std::vector<Constant> constants;
std::vector<Proto> children;
std::vector<UpvalueDesc> upvalues;
};
class CifaBytecode::FunctionCompiler {
public:
FunctionCompiler(const CifaBytecode& owner, Proto& proto, const std::unordered_map<std::string, FunctionOverloads>* functions,
bool root = false, unsigned first_register = 0,
const std::unordered_map<std::string, unsigned>* function_upvalues = nullptr)
: owner_(owner), proto_(proto), functions_(functions), root_(root),
function_upvalues_(function_upvalues), next_register_(first_register) {}
bool compile_body(const CalUnit& body, const std::vector<Function2::Argument>& args, std::string& error)
{
for (const auto& arg : args) {
local(arg.name);
numeric_kinds_.back()[arg.name] = type_kind(arg.type_name);
constexpr std::string_view vector_prefix = "vector<";
if (arg.type_name.starts_with(vector_prefix) && arg.type_name.ends_with('>'))
array_element_types_.back()[arg.name] = arg.type_name.substr(vector_prefix.size(), arg.type_name.size() - vector_prefix.size() - 1);
}
if (!emit_block(body, error)) return false;
for (const auto& [name, jump] : goto_jumps_) {
const auto label = labels_.find(name);
if (label == labels_.end()) {
error = "undefined goto label: " + name;
return false;
}
patch_jump(jump, label->second);
}
proto_.code.push_back(abc(RETURN0, 0, 0, 0));
proto_.maxstack = std::max<unsigned>(2, next_register_ + 1);
if (proto_.maxstack > 255)
{
error = "Lua register limit exceeded: " + std::to_string(proto_.maxstack);
return false;
}
return true;
}
private:
const CifaBytecode& owner_;
Proto& proto_;
const std::unordered_map<std::string, FunctionOverloads>* functions_;
bool root_ = false;
const std::unordered_map<std::string, unsigned>* function_upvalues_ = nullptr;
std::vector<std::unordered_map<std::string, unsigned>> locals_{1};
enum class NumericKind { Unknown, Integer, Float };
std::vector<std::unordered_map<std::string, NumericKind>> numeric_kinds_{1};
std::vector<std::unordered_set<std::string>> string_locals_{1};
std::vector<std::unordered_map<std::string, std::string>> declared_types_{1};
std::vector<std::unordered_map<std::string, std::string>> array_element_types_{1};
std::vector<std::unordered_map<std::string, std::unordered_map<std::string, std::string>>> struct_field_types_{1};
std::vector<std::unordered_set<std::string>> uninitialized_locals_{1};
std::vector<std::vector<unsigned>> loop_breaks_;
std::vector<std::vector<unsigned>> loop_continues_;
std::unordered_map<std::string, unsigned> labels_;
std::vector<std::pair<std::string, unsigned>> goto_jumps_;
std::vector<std::pair<std::string, unsigned>> one_based_indices_;
std::vector<std::pair<std::string, unsigned>> append_indices_;
std::unordered_set<std::string> root_globals_;
unsigned next_register_ = 0;
unsigned block_depth_ = 0;
std::optional<unsigned> one_based_index(const CalUnit& node) const
{
if (node.type != CalUnitType::Parameter || !node.v.empty()) return std::nullopt;
for (auto index = one_based_indices_.rbegin(); index != one_based_indices_.rend(); ++index)
if (index->first == node.str) return index->second;
return std::nullopt;
}
std::optional<unsigned> append_index(const CalUnit& node) const
{
if (node.type != CalUnitType::Parameter || !node.v.empty()) return std::nullopt;
for (auto index = append_indices_.rbegin(); index != append_indices_.rend(); ++index)
if (index->first == node.str) return index->second;
return std::nullopt;
}
unsigned local(const std::string& name)
{
if (const auto existing = find_local(name)) return *existing;
return declare_local(name);
}
unsigned declare_local(const std::string& name)
{
if (const auto it = locals_.back().find(name); it != locals_.back().end()) return it->second;
const unsigned result = next_register_++;
locals_.back().emplace(name, result);
return result;
}
std::optional<unsigned> find_local(const std::string& name) const
{
for (auto scope = locals_.rbegin(); scope != locals_.rend(); ++scope) {
if (const auto it = scope->find(name); it != scope->end()) return it->second;
}
return std::nullopt;
}
bool has_current_local(const std::string& name) const
{
return locals_.back().contains(name);
}
bool is_string_local(const std::string& name) const
{
for (auto scope = string_locals_.rbegin(); scope != string_locals_.rend(); ++scope)
if (scope->contains(name)) return true;
return false;
}
std::string declared_type(const std::string& name) const
{
for (auto scope = declared_types_.rbegin(); scope != declared_types_.rend(); ++scope)
if (const auto found = scope->find(name); found != scope->end()) return found->second;
return {};
}
std::string array_element_type(const std::string& name) const
{
for (auto scope = array_element_types_.rbegin(); scope != array_element_types_.rend(); ++scope)
if (const auto found = scope->find(name); found != scope->end()) return found->second;
return {};
}
std::string struct_field_type(const std::string& object, const std::string& field) const
{
for (auto scope = struct_field_types_.rbegin(); scope != struct_field_types_.rend(); ++scope) {
if (const auto object_types = scope->find(object); object_types != scope->end()) {
if (const auto type = object_types->second.find(field); type != object_types->second.end()) return type->second;
}
}
return {};
}
bool is_uninitialized_local(const std::string& name) const
{
for (auto scope = uninitialized_locals_.rbegin(); scope != uninitialized_locals_.rend(); ++scope)
if (scope->contains(name)) return true;
return false;
}
const std::vector<StructField>* struct_definition(const std::string& name) const
{
const auto& structs = owner_.compilation_struct_defs;
const auto found = structs.find(name);
return found == structs.end() ? nullptr : &found->second;
}
void emit_uninitialized_check(const CalUnit& node, unsigned value)
{
if (!is_uninitialized_local(node.str)) return;
const unsigned checked = next_register_++;
proto_.code.push_back(abc(NOT, checked, value, 0));
proto_.value_errors.emplace_back(proto_.code.size() - 1,
"variable '" + node.str + "' has not been initialized\n" + owner_.compiled_source_frame(node));
}
static NumericKind type_kind(const std::string& type_name)
{
if (type_name == "int") return NumericKind::Integer;
if (type_name == "double" || type_name == "float") return NumericKind::Float;
return NumericKind::Unknown;
}
NumericKind expression_kind(const CalUnit& node) const
{
if (node.type == CalUnitType::Constant)
return node.str.find_first_of(".eE") == std::string::npos ? NumericKind::Integer : NumericKind::Float;
if (node.type == CalUnitType::Parameter) {
std::optional<NumericKind> kind;
for (auto scope = numeric_kinds_.rbegin(); scope != numeric_kinds_.rend(); ++scope) {
if (const auto it = scope->find(node.str); it != scope->end()) { kind = it->second; break; }
}
if (node.v.empty()) return kind.value_or(NumericKind::Unknown);
if (node.v.size() == 1 && node.v[0].str == "[]") return type_kind(array_element_type(node.str));
return NumericKind::Unknown;
}
if (node.type == CalUnitType::Cast && node.v.size() == 1)
return type_kind(node.type_name);
if (node.type == CalUnitType::Operator && node.str == "()" && node.v.size() == 1)
return expression_kind(node.v[0]);
if (node.type == CalUnitType::Operator && node.v.size() == 1) {
if (node.str == "~") return NumericKind::Integer;
if (node.str == "+" || node.str == "-") return expression_kind(node.v[0]);
}
if (node.type == CalUnitType::Operator && node.v.size() == 2) {
const NumericKind left = expression_kind(node.v[0]);
const NumericKind right = expression_kind(node.v[1]);
if (left == NumericKind::Float || right == NumericKind::Float) return NumericKind::Float;
if (left == NumericKind::Integer && right == NumericKind::Integer
&& (node.str == "+" || node.str == "-" || node.str == "*" || node.str == "%" || node.str == "/" || node.str == "//"
|| node.str == "&" || node.str == "|" || node.str == "^" || node.str == "<<" || node.str == ">>"))
return NumericKind::Integer;
}
if (node.type == CalUnitType::Function && node.str == "size") return NumericKind::Integer;
return NumericKind::Unknown;
}
unsigned constant(const Constant& value)
{
for (unsigned i = 0; i < proto_.constants.size(); ++i) {
const auto& old = proto_.constants[i];
if (old.kind != value.kind) continue;
if (value.kind == Constant::Integer && old.integer == value.integer) return i;
if (value.kind == Constant::Number && old.number == value.number) return i;
if (value.kind == Constant::String && old.string == value.string) return i;
if (value.kind == Constant::Boolean && old.boolean == value.boolean) return i;
}
proto_.constants.push_back(value);
return static_cast<unsigned>(proto_.constants.size() - 1);
}
bool number(const std::string& text, Constant& result) const
{
try {
if (text.find_first_of(".eE") == std::string::npos) {
result.kind = Constant::Integer;
if (text.size() > 2 && text[0] == '0' && (text[1] == 'b' || text[1] == 'B')) {
result.integer = 0;
for (std::size_t index = 2; index < text.size(); ++index) {
if (text[index] != '0' && text[index] != '1') return false;
result.integer = (result.integer << 1) | (text[index] - '0');
}
} else result.integer = std::stoll(text, nullptr, 0);
} else {
result.kind = Constant::Number;
result.number = std::stod(text);
}
return true;
} catch (...) { return false; }
}
unsigned emit_constant(const Constant& value)
{
const auto index = constant(value);
if (value.kind == Constant::Integer && value.integer >= -(1 << 16) && value.integer <= (1 << 16)) {
const unsigned reg = next_register_++;
proto_.code.push_back(abx(LOADI, reg, static_cast<unsigned>(value.integer + ((1 << 17) - 1) / 2)));
return reg;
}
const unsigned reg = next_register_++;
proto_.code.push_back(abx(LOADK, reg, index));
return reg;
}
unsigned emit_global_get(const std::string& name, std::string& error)
{
(void)error;
const unsigned result = next_register_++;
const unsigned key = constant(Constant{Constant::String, 0, 0, false, name});
proto_.code.push_back(abc(GETTABUP, result, 0, key, 1));
return result;
}
void emit_global_set(const std::string& name, unsigned value)
{
const unsigned key = constant(Constant{Constant::String, 0, 0, false, name});
proto_.code.push_back(abc(SETTABUP, 0, key, value, 0));
}
unsigned emit_name(const std::string& name, std::string& error)
{
if (const auto slot = find_local(name)) return *slot;
return emit_global_get(name, error);
}
unsigned emit_index(const CalUnit& node, std::string& error)
{
if (node.v.empty() || node.v[0].str != "[]") {
error = "unsupported index shape";
return 0;
}
auto emit_step = [&](unsigned table, const CalUnit& subscript) -> unsigned {
if (subscript.type == CalUnitType::Constant) {
Constant constant_index{};
if (number(subscript.str, constant_index) && constant_index.kind == Constant::Integer
&& constant_index.integer >= 0 && constant_index.integer < 255) {
const unsigned result = next_register_++;
proto_.code.push_back(abc(GETI, result, table, static_cast<unsigned>(constant_index.integer + 1)));
return result;
}
}
if (const auto index = one_based_index(subscript)) {
const unsigned result = next_register_++;
proto_.code.push_back(abc(GETTABLE, result, table, *index));
return result;
}
const unsigned source_index = emit_expression(subscript, error);
if (subscript.type == CalUnitType::String
|| subscript.type == CalUnitType::Parameter && is_string_local(subscript.str)) {
const unsigned result = next_register_++;
proto_.code.push_back(abc(GETTABLE, result, table, source_index));
return result;
}
const unsigned index = next_register_++;
proto_.code.push_back(abc(ADDI, index, source_index, 128));
const unsigned result = next_register_++;
proto_.code.push_back(abc(GETTABLE, result, table, index));
return result;
};
unsigned result = emit_name(node.str, error);
for (const CalUnit& accessor : node.v) {
if (accessor.str != "[]" || accessor.v.size() != 1) {
error = "unsupported index shape";
return 0;
}
result = emit_step(result, accessor.v[0]);
}
return result;
}
unsigned emit_condition(const CalUnit& node, std::string& error)
{
if (node.type == CalUnitType::Operator && node.str == "&&" && node.v.size() == 2) {
const auto first = emit_condition(node.v[0], error);
const auto second = emit_condition(node.v[1], error);
const auto success_exit = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
const auto false_exit = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(first, false_exit);
patch_jump(second, false_exit);
patch_jump(success_exit, static_cast<unsigned>(proto_.code.size()));
return false_exit;
}
if (node.type == CalUnitType::Operator && node.str == "||" && node.v.size() == 2) {
const auto first = emit_condition(node.v[0], error);
const auto success_exit = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
const auto second_start = static_cast<unsigned>(proto_.code.size());
patch_jump(first, second_start);
const auto second = emit_condition(node.v[1], error);
patch_jump(success_exit, static_cast<unsigned>(proto_.code.size()));
return second;
}
if (node.type == CalUnitType::Operator && node.v.size() == 2 && node.str != "." && node.str != "::") {
auto left = emit_expression(node.v[0], error);
auto right = emit_expression(node.v[1], error);
unsigned op = 0;
bool invert = false;
if (node.str == "==") op = EQ;
else if (node.str == "!=") { op = EQ; invert = true; }
else if (node.str == "<") op = LT;
else if (node.str == "<=") op = LE;
else if (node.str == ">" || node.str == ">=") {
op = node.str == ">" ? LT : LE;
}
else { error = "unsupported condition: " + node.str; return 0; }
Constant right_constant{};
const bool immediate = (node.str == "==" || node.str == "!=" || node.str == "<" || node.str == "<=")
&& node.v[1].type == CalUnitType::Constant && number(node.v[1].str, right_constant)
&& right_constant.kind == Constant::Integer && right_constant.integer >= -127 && right_constant.integer <= 128;
if (node.str == ">" || node.str == ">=") std::swap(left, right);
const auto stable_left = next_register_++;
const auto stable_right = next_register_++;
proto_.code.push_back(abc(MOVE, stable_left, left, 0));
proto_.code.push_back(abc(MOVE, stable_right, right, 0));
if (immediate && (node.str == "==" || node.str == "!=" || node.str == "<" || node.str == "<=")) {
const unsigned immediate_op = op == EQ ? EQI : op == LT ? LTI : LEI;
proto_.code.push_back(abc(immediate_op, stable_left, static_cast<unsigned>(right_constant.integer + 127), 0, invert ? 1u : 0u));
}
else
proto_.code.push_back(abc(op, stable_left, stable_right, 0, invert ? 1u : 0u));
const auto jump = proto_.code.size();
proto_.code.push_back(asj(JMP, 0));
return static_cast<unsigned>(jump);
}
const auto value = emit_expression(node, error);
proto_.code.push_back(abc(TEST, value, 0, 0, 1));
const auto jump = proto_.code.size();
proto_.code.push_back(asj(JMP, 0));
return static_cast<unsigned>(jump);
}
void patch_jump(unsigned jump, unsigned target)
{
proto_.code[jump] = asj(JMP, static_cast<int>(target) - static_cast<int>(jump) - 1);
}
unsigned emit_expression(const CalUnit& node, std::string& error, bool prefer_general_arithmetic = false,
bool allow_uninitialized = false)
{
if (node.type == CalUnitType::Union && node.str == "{}") {
std::vector<const CalUnit*> elements;
const auto collect = [&](const CalUnit& item, const auto& self) -> void {
if (item.type == CalUnitType::Operator && item.str == "," && item.v.size() == 2) {
self(item.v[0], self);
self(item.v[1], self);
} else if (item.type != CalUnitType::None && item.type != CalUnitType::Split && item.str != ";") {
elements.push_back(&item);
}
};
for (const CalUnit& item : node.v) collect(item, collect);
const unsigned table = next_register_++;
proto_.code.push_back(abc(NEWTABLE, table, 0, static_cast<unsigned>(elements.size())));
proto_.code.push_back(ax(EXTRAARG, 0));
for (std::size_t index = 0; index < elements.size(); ++index) {
const unsigned value = emit_expression(*elements[index], error);
proto_.code.push_back(abc(SETI, table, static_cast<unsigned>(index + 1), value));
}
return table;
}
if (node.type == CalUnitType::Constant) {
Constant value{};
if (!number(node.str, value)) { error = "unsupported numeric literal: " + node.str; return 0; }
return emit_constant(value);
}
if (node.type == CalUnitType::String) {
Constant value{}; value.kind = Constant::String; value.string = node.str;
return emit_constant(value);
}
if (node.type == CalUnitType::Key && (node.str == "true" || node.str == "false")) {
Constant value{}; value.kind = Constant::Boolean; value.boolean = node.str == "true";
return emit_constant(value);
}
if (node.type == CalUnitType::Cast && node.v.size() == 1) {
unsigned source = emit_expression(node.v[0], error, true);
const NumericKind source_kind = expression_kind(node.v[0]);
if (node.type_name == "int" || node.type_name == "double" || node.type_name == "float") {
const unsigned checked = next_register_++;
proto_.code.push_back(abc(NOT, checked, source, 0));
proto_.value_errors.emplace_back(proto_.code.size() - 1,
"cannot convert value to '" + (node.type_name == "float" ? std::string("double") : node.type_name) + "'");
}
if (node.type_name == "int" && source_kind == NumericKind::Float) {
const unsigned result = next_register_++;
const unsigned one = constant(Constant{Constant::Integer, 1, 0, false, {}});
proto_.code.push_back(abc(IDIVK, result, source, one));
return result;
}
if ((node.type_name == "double" || node.type_name == "float") && source_kind == NumericKind::Integer) {
const unsigned result = next_register_++;
const unsigned one = constant(Constant{Constant::Number, 0, 1.0, false, {}});
proto_.code.push_back(abc(DIVK, result, source, one));
return result;
}
if (node.type_name == "bool") {
const unsigned negated = next_register_++;
const unsigned result = next_register_++;
proto_.code.push_back(abc(NOT, negated, source, 0));
proto_.code.push_back(abc(NOT, result, negated, 0));
return result;
}
return source;
}
if (node.type == CalUnitType::Parameter) {
if (!node.v.empty() && node.v[0].str == "[]") {
return emit_index(node, error);
}
const unsigned value = emit_name(node.str, error);
if (!allow_uninitialized) emit_uninitialized_check(node, value);
return value;
}
if (node.type == CalUnitType::Operator && node.str == "()" && node.v.size() == 1)
return emit_expression(node.v[0], error);
if (node.type == CalUnitType::Operator && node.str == "::" && node.v.size() == 2
&& node.v[1].type == CalUnitType::Parameter) {
const unsigned table = emit_expression(node.v[0], error);
const unsigned result = next_register_++;
const unsigned key = constant(Constant{Constant::String, 0, 0, false, node.v[1].str});
proto_.code.push_back(abc(GETFIELD, result, table, key));
return result;
}
if (node.type == CalUnitType::Operator && node.str == "." && node.v.size() == 2
&& node.v[1].type == CalUnitType::Parameter) {
const unsigned table = emit_expression(node.v[0], error);
const unsigned result = next_register_++;
const unsigned key = constant(Constant{Constant::String, 0, 0, false, node.v[1].str});
proto_.code.push_back(abc(GETFIELD, result, table, key));
return result;
}
if (node.type == CalUnitType::Operator && node.str == "," && node.v.size() == 2) {
emit_expression(node.v[0], error);
return emit_expression(node.v[1], error);
}
if (node.type == CalUnitType::Operator && node.str == "?" && node.v.size() == 2
&& node.v[1].v.size() == 2) {
const unsigned result = next_register_++;
const unsigned false_jump = emit_condition(node.v[0], error);
const unsigned true_value = emit_expression(node.v[1].v[0], error);
if (true_value != result) proto_.code.push_back(abc(MOVE, result, true_value, 0));
const unsigned end_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(false_jump, static_cast<unsigned>(proto_.code.size()));
const unsigned false_value = emit_expression(node.v[1].v[1], error);
if (false_value != result) proto_.code.push_back(abc(MOVE, result, false_value, 0));
patch_jump(end_jump, static_cast<unsigned>(proto_.code.size()));
return result;
}
if (node.type == CalUnitType::Operator && node.str == "&&" && node.v.size() == 2) {
const unsigned result = next_register_++;
const unsigned false_jump = emit_condition(node, error);
proto_.code.push_back(abx(LOADI, result, (1 << 16)));
const unsigned end_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(false_jump, static_cast<unsigned>(proto_.code.size()));
proto_.code.push_back(abx(LOADI, result, (1 << 16) - 1));
patch_jump(end_jump, static_cast<unsigned>(proto_.code.size()));
return result;
}
if (node.type == CalUnitType::Operator && node.str == "||" && node.v.size() == 2) {
const unsigned result = next_register_++;
const unsigned left = emit_expression(node.v[0], error);
proto_.code.push_back(abc(TEST, left, 0, 0, 1));
const unsigned rhs_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
proto_.code.push_back(abx(LOADI, result, (1 << 16)));
const unsigned left_end_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(rhs_jump, static_cast<unsigned>(proto_.code.size()));
const unsigned right = emit_expression(node.v[1], error);
proto_.code.push_back(abc(TEST, right, 0, 0, 1));
const unsigned false_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
proto_.code.push_back(abx(LOADI, result, (1 << 16)));
const unsigned right_end_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(false_jump, static_cast<unsigned>(proto_.code.size()));
proto_.code.push_back(abx(LOADI, result, (1 << 16) - 1));
const unsigned end = static_cast<unsigned>(proto_.code.size());
patch_jump(left_end_jump, end);
patch_jump(right_end_jump, end);
return result;
}
if (node.type == CalUnitType::Operator && node.v.size() == 1
&& (node.str == "+" || node.str == "-" || node.str == "~" || node.str == "!")) {
const unsigned value = emit_expression(node.v[0], error);
if (node.str == "+") return value;
const unsigned result = next_register_++;
const unsigned operation = node.str == "-" ? UNM : node.str == "~" ? BNOT : NOT;
proto_.code.push_back(abc(operation, result, value, 0));
return result;
}
if (node.type == CalUnitType::Operator && node.v.size() == 2
&& (node.str == "==" || node.str == "!=" || node.str == "<" || node.str == "<="
|| node.str == ">" || node.str == ">=")) {
const unsigned result = next_register_++;
const unsigned false_jump = emit_condition(node, error);
proto_.code.push_back(abx(LOADI, result, 1u << 16));
const unsigned end_jump = static_cast<unsigned>(proto_.code.size());
proto_.code.push_back(asj(JMP, 0));
patch_jump(false_jump, static_cast<unsigned>(proto_.code.size()));
proto_.code.push_back(abx(LOADI, result, (1u << 16) - 1));
patch_jump(end_jump, static_cast<unsigned>(proto_.code.size()));
return result;
}
if (node.type == CalUnitType::Operator && (node.str == "++" || node.str == "--" || node.str == "()++" || node.str == "()--") && node.v.size() == 1
&& node.v[0].type == CalUnitType::Parameter) {
const auto target = emit_name(node.v[0].str, error);
const bool postfix = node.str == "()++" || node.str == "()--";
const unsigned result = postfix ? next_register_++ : target;
if (postfix) proto_.code.push_back(abc(MOVE, result, target, 0));
const auto encoded_delta = (node.str == "++" || node.str == "()++") ? 128u : 126u;
proto_.code.push_back(abc(ADDI, target, target, encoded_delta));
if (root_ && !find_local(node.v[0].str)) emit_global_set(node.v[0].str, target);
return result;
}
if (node.type == CalUnitType::Operator && node.v.size() == 2 && node.str != "." && node.str != "::") {
static const std::unordered_map<std::string, unsigned> operations = {
{"+", ADD}, {"-", SUB}, {"*", MUL}, {"%", MOD}, {"//", IDIV},
{"&", BAND}, {"|", BOR}, {"^", BXOR}, {"<<", SHL}, {">>", SHR}
};
unsigned operation = 0;
if (node.str == "/") {
operation = expression_kind(node.v[0]) == NumericKind::Integer
&& expression_kind(node.v[1]) == NumericKind::Integer ? IDIV : DIV;
} else if (const auto it = operations.find(node.str); it != operations.end()) {
operation = it->second;
} else {
error = "unsupported operator: " + node.str;
if (node.str == "." && node.v.size() == 2) {
error += " left=" + std::to_string(static_cast<int>(node.v[0].type))
+ ":" + node.v[0].str + " right=" + std::to_string(static_cast<int>(node.v[1].type))
+ ":" + node.v[1].str;
}
return 0;
}
const auto left_value = emit_expression(node.v[0], error);
const auto left = next_register_++;
proto_.code.push_back(abc(MOVE, left, left_value, 0));
const auto contains_string = [&](const CalUnit& item, const auto& self) -> bool {
if (item.type == CalUnitType::String || item.type == CalUnitType::Parameter && is_string_local(item.str)) return true;
if (item.type == CalUnitType::Function) return item.str == "to_string";
if (item.type == CalUnitType::Operator && item.str == "." && item.v.size() == 2
&& item.v[1].type == CalUnitType::Function) return self(item.v[0], self);
for (const auto& child : item.v) if (self(child, self)) return true;
return false;
};
if (node.str == "+" && (contains_string(node.v[0], contains_string) || contains_string(node.v[1], contains_string))) {
const auto right = emit_expression(node.v[1], error);
const auto first = next_register_++;
const auto second = next_register_++;
proto_.code.push_back(abc(MOVE, first, left, 0));
proto_.code.push_back(abc(MOVE, second, right, 0));
proto_.code.push_back(abc(CONCAT, first, 2, 0));
return first;
}
Constant right_constant{};
const bool right_is_constant = node.v[1].type == CalUnitType::Constant
&& number(node.v[1].str, right_constant);
const NumericKind left_kind = expression_kind(node.v[0]);
const bool has_right_constant = !prefer_general_arithmetic && right_is_constant
&& (left_kind == NumericKind::Integer || operation == DIV)
&& (operation == ADD || operation == SUB || operation == MUL || operation == MOD
|| operation == DIV || operation == IDIV);
const unsigned right = has_right_constant ? 0 : emit_expression(node.v[1], error);
const auto result = next_register_++;
if (has_right_constant && operation == ADD && right_constant.kind == Constant::Integer
&& right_constant.integer >= -127 && right_constant.integer <= 127) {
proto_.code.push_back(abc(ADDI, result, left, static_cast<unsigned>(right_constant.integer + 127)));
} else if (has_right_constant) {
const unsigned constant_index = constant(right_constant);
const unsigned constant_operation = operation == ADD ? ADDK
: operation == SUB ? SUBK : operation == MUL ? MULK : operation == MOD ? MODK
: operation == DIV ? DIVK : operation == IDIV ? IDIVK : operation;
proto_.code.push_back(abc(constant_operation, result, left, constant_index));
} else {
proto_.code.push_back(abc(operation, result, left, right));
}
return result;
}
if (node.type == CalUnitType::Union && node.str == "{}") {
const unsigned table = next_register_++;
proto_.code.push_back(abc(NEWTABLE, table, 0, static_cast<unsigned>(node.v.size())));
proto_.code.push_back(ax(EXTRAARG, 0));
unsigned index = 1;
for (const auto& child : node.v) {
if (child.type == CalUnitType::None) continue;
const auto value = emit_expression(child, error);
proto_.code.push_back(abc(SETI, table, index++, value));
}
return table;
}
if (node.type == CalUnitType::Operator && node.str == "." && node.v.size() == 2
&& node.v[1].type == CalUnitType::Function) {
const auto receiver = emit_expression(node.v[0], error);
const auto method = node.v[1].str;
std::vector<const CalUnit*> arguments;
std::function<void(const CalUnit&)> flatten = [&](const CalUnit& item) {
if (item.type == CalUnitType::Operator && item.str == ",") for (const auto& child : item.v) flatten(child);
else if (item.type != CalUnitType::None) arguments.push_back(&item);
};
for (const auto& child : node.v[1].v) flatten(child);
if (method == "push_back") {
if (arguments.size() != 1) { error = "push_back expects one argument"; return 0; }
const auto value = emit_expression(*arguments[0], error);
if (const auto index = append_index(node.v[0])) {
proto_.code.push_back(abc(SETTABLE, receiver, *index, value));
proto_.code.push_back(abc(ADDI, *index, *index, 128));
return *index;
}
const auto index = next_register_++;
proto_.code.push_back(abc(LEN, index, receiver, 0));
proto_.code.push_back(abc(ADDI, index, index, 128));
proto_.code.push_back(abc(SETTABLE, receiver, index, value));
return index;
}
if (method == "pop_back") {
const unsigned function = next_register_;
next_register_ += 3;
const unsigned table_key = constant(Constant{Constant::String, 0, 0, false, "table"});
proto_.code.push_back(abc(GETTABUP, function, 0, table_key, 1));
const unsigned method_key = constant(Constant{Constant::String, 0, 0, false, "remove"});
proto_.code.push_back(abc(GETFIELD, function, function, method_key));
proto_.code.push_back(abc(MOVE, function + 1, receiver, 0));
const auto size = next_register_++;
proto_.code.push_back(abc(LEN, size, receiver, 0));
proto_.code.push_back(abc(MOVE, function + 2, size, 0));
proto_.code.push_back(abc(CALL, function, 3, 2));
return function;
}
const std::string function_name = method == "push_back" ? "insert" : method == "pop_back" ? "remove" : method;
std::vector<unsigned> argument_registers;
argument_registers.reserve(arguments.size());
for (const CalUnit* argument : arguments)
argument_registers.push_back(emit_expression(*argument, error));
const unsigned function = next_register_;
next_register_ += static_cast<unsigned>(argument_registers.size() + 2);
const unsigned table_key = constant(Constant{Constant::String, 0, 0, false, "table"});
proto_.code.push_back(abc(GETTABUP, function, 0, table_key, 1));
const unsigned method_key = constant(Constant{Constant::String, 0, 0, false, function_name});
proto_.code.push_back(abc(GETFIELD, function, function, method_key));
proto_.code.push_back(abc(MOVE, function + 1, receiver, 0));
for (std::size_t index = 0; index < argument_registers.size(); ++index)
proto_.code.push_back(abc(MOVE, function + 2 + static_cast<unsigned>(index), argument_registers[index], 0));
proto_.code.push_back(abc(CALL, function, static_cast<unsigned>(argument_registers.size() + 2), 2));
const unsigned result = next_register_++;
proto_.code.push_back(abc(MOVE, result, function, 0));
return result;
}
if (node.type == CalUnitType::Function) {
std::vector<const CalUnit*> arguments;
std::function<void(const CalUnit&)> flatten = [&](const CalUnit& item) {
if (item.type == CalUnitType::Operator && item.str == ",") for (const auto& child : item.v) flatten(child);
else if (item.type != CalUnitType::None) arguments.push_back(&item);
};
for (const auto& child : node.v) flatten(child);
if (node.str == "size") {
if (arguments.size() != 1) { error = "size expects one argument"; return 0; }
const auto value = emit_expression(*arguments[0], error);
const auto result = next_register_++;
proto_.code.push_back(abc(LEN, result, value, 0));
return result;
}
const unsigned function_reg = next_register_;
const std::string function_name = node.str == "to_string" ? "tostring"
: node.str == "println" ? "print" : node.str;
const bool is_script_function = functions_ != nullptr && functions_->contains(function_name);
const std::string closure_name = is_script_function
? script_function_key(function_name, arguments.size()) : function_name;
next_register_ += static_cast<unsigned>(arguments.size() + 1);
if (function_upvalues_) {
const auto function = function_upvalues_->find(closure_name);
if (function != function_upvalues_->end()) {
proto_.code.push_back(abc(GETUPVAL, function_reg, function->second, 0));
} else {
const unsigned name = constant(Constant{Constant::String, 0, 0, false, closure_name});
proto_.code.push_back(abc(GETTABUP, function_reg, 0, name, 1));
}
} else {
const unsigned name = constant(Constant{Constant::String, 0, 0, false, closure_name});
proto_.code.push_back(abc(GETTABUP, function_reg, 0, name, 1));
}
std::vector<unsigned> argument_registers;
argument_registers.reserve(arguments.size());
for (const auto* argument : arguments)
argument_registers.push_back(emit_expression(*argument, error, false, function_name == "type"));
for (std::size_t argument_index = 0; argument_index < arguments.size(); ++argument_index) {
const auto reg = argument_registers[argument_index];
const auto target = function_reg + 1 + static_cast<unsigned>(argument_index);
if (reg != target) proto_.code.push_back(abc(MOVE, target, reg, 0));
}
proto_.code.push_back(abc(CALL, function_reg, static_cast<unsigned>(arguments.size() + 1), 2));
proto_.call_frames.emplace_back(proto_.code.size() - 1,
owner_.compiled_source_frame(is_script_function || arguments.empty() ? node : *arguments.front()));
if (is_script_function) {
if (!functions_->at(function_name).contains(arguments.size())) {
std::vector<std::size_t> arities;
arities.reserve(functions_->at(function_name).size());
for (const auto& [arity, function] : functions_->at(function_name)) arities.push_back(arity);
std::sort(arities.begin(), arities.end());
std::string available;
for (std::size_t index = 0; index < arities.size(); ++index) {
if (index != 0) available += ", ";
available += std::to_string(arities[index]);
}
proto_.call_errors.emplace_back(proto_.code.size() - 1,
"function '" + function_name + "' has no overload for " + std::to_string(arguments.size())
+ " arguments; available: " + available);
}
}
if (function_name == "tostring") {
const auto result = next_register_++;
proto_.code.push_back(abc(MOVE, result, function_reg, 0));
return result;
}
return function_reg;
}
error = "unsupported expression node: " + node.str;
return 0;
}
bool emit_statement(const CalUnit& node, std::string& error)
{
if (node.type == CalUnitType::None || node.type == CalUnitType::Split || node.str == ";") return true;
if (node.type == CalUnitType::Label || (node.type == CalUnitType::Key && node.v.empty())) {
labels_[node.str] = static_cast<unsigned>(proto_.code.size());
return true;
}
if (node.type == CalUnitType::Union && (node.str.empty() || node.str == "{}")) return emit_block(node, error);
if (node.type == CalUnitType::Operator && node.str == "," && node.v.size() == 2)
return emit_statement(node.v[0], error) && emit_statement(node.v[1], error);
if (node.type == CalUnitType::Parameter && node.v.empty()) {
const NumericKind kind = node.with_type ? type_kind(node.type_name) : NumericKind::Unknown;
if (node.with_type) numeric_kinds_.back()[node.str] = kind;
if (node.with_type && node.type_name == "string") string_locals_.back().insert(node.str);
if (node.with_type && kind != NumericKind::Unknown) uninitialized_locals_.back().insert(node.str);
const auto target = node.with_type ? declare_local(node.str) : local(node.str);
if (kind == NumericKind::Integer) proto_.code.push_back(abx(LOADI, target, (1u << 16) - 1));
else if (kind == NumericKind::Float) {
const unsigned zero = constant(Constant{Constant::Number, 0, 0.0, false, {}});
proto_.code.push_back(abx(LOADK, target, zero));
} else {
proto_.code.push_back(abc(NEWTABLE, target, 0, 0));
proto_.code.push_back(ax(EXTRAARG, 0));
}
if (node.with_type && struct_definition(node.type_name)) {
const unsigned key = constant(Constant{Constant::String, 0, 0, false, "__cifa_struct_type"});
const unsigned type = emit_constant(Constant{Constant::String, 0, 0, false, node.type_name});
proto_.code.push_back(abc(SETFIELD, target, key, type));
auto& fields = struct_field_types_.back()[node.str];
for (const auto& field : *struct_definition(node.type_name))
fields[field.name] = field.type_name;
}
return true;
}
if (node.type == CalUnitType::Parameter && node.with_type && !node.v.empty() && node.v[0].str == "[]") {
array_element_types_.back()[node.str] = node.type_name;
const unsigned target = declare_local(node.str);
proto_.code.push_back(abc(NEWTABLE, target, 0, 0));
proto_.code.push_back(ax(EXTRAARG, 0));
unsigned table = target;
for (std::size_t depth = 1; depth < node.v.size(); ++depth) {
const unsigned child = next_register_++;
proto_.code.push_back(abc(NEWTABLE, child, 0, 0));
proto_.code.push_back(ax(EXTRAARG, 0));
proto_.code.push_back(abc(SETI, table, 1, child));
table = child;
}
return true;
}
if (node.type == CalUnitType::Operator && node.str == "=" && node.v.size() == 2) {
const bool field_assignment = node.v[0].type == CalUnitType::Operator && node.v[0].str == "."
&& node.v[0].v.size() == 2 && node.v[0].v[0].type == CalUnitType::Parameter
&& node.v[0].v[1].type == CalUnitType::Parameter;
if (node.v[0].type != CalUnitType::Parameter && !field_assignment) { error = "unsupported assignment target"; return false; }
if (field_assignment) {
unsigned source = emit_expression(node.v[1], error);
const std::string field_type = struct_field_type(node.v[0].v[0].str, node.v[0].v[1].str);
if (field_type == "int" && expression_kind(node.v[1]) == NumericKind::Float) {
const unsigned converted = next_register_++;
const unsigned one = constant(Constant{Constant::Integer, 1, 0, false, {}});
proto_.code.push_back(abc(IDIVK, converted, source, one));
source = converted;
} else if ((field_type == "double" || field_type == "float") && expression_kind(node.v[1]) == NumericKind::Integer) {
const unsigned converted = next_register_++;
const unsigned one = constant(Constant{Constant::Number, 0, 1.0, false, {}});
proto_.code.push_back(abc(DIVK, converted, source, one));
source = converted;
} else if (field_type == "bool") {
const unsigned negated = next_register_++;
const unsigned converted = next_register_++;
proto_.code.push_back(abc(NOT, negated, source, 0));
proto_.code.push_back(abc(NOT, converted, negated, 0));
source = converted;
}
const unsigned table = emit_name(node.v[0].v[0].str, error);
const unsigned key = constant(Constant{Constant::String, 0, 0, false, node.v[0].v[1].str});
proto_.code.push_back(abc(SETFIELD, table, key, source));
return error.empty();
}
if (node.v[0].with_type && !node.v[0].v.empty() && node.v[0].v[0].str == "[]")
array_element_types_.back()[node.v[0].str] = node.v[0].type_name;
if (node.v[0].with_type) numeric_kinds_.back()[node.v[0].str] = type_kind(node.v[0].type_name);
if (node.v[0].with_type && node.v[0].type_name == "string") string_locals_.back().insert(node.v[0].str);
std::string target_type = node.v[0].with_type ? node.v[0].type_name : declared_type(node.v[0].str);
if (target_type == "auto") {
target_type = expression_kind(node.v[1]) == NumericKind::Float ? "double"
: expression_kind(node.v[1]) == NumericKind::Integer ? "int"
: node.v[1].type == CalUnitType::String ? "string"
: node.v[1].str == "true" || node.v[1].str == "false" ? "bool" : "";
}
if (node.v[0].with_type && target_type != "") {
declared_types_.back()[node.v[0].str] = target_type;
numeric_kinds_.back()[node.v[0].str] = type_kind(target_type);
if (target_type == "string") string_locals_.back().insert(node.v[0].str);
if (const auto* definition = struct_definition(target_type)) {
auto& fields = struct_field_types_.back()[node.v[0].str];
for (const auto& field : *definition) fields[field.name] = field.type_name;
}
}
if (!node.v[0].v.empty() && node.v[0].v[0].str == "[]" && root_
&& !find_local(node.v[0].str) && !root_globals_.contains(node.v[0].str)
&& !owner_.registered_globals().contains(node.v[0].str)) {
const unsigned table = next_register_++;
proto_.code.push_back(abc(NEWTABLE, table, 0, 0));
proto_.code.push_back(ax(EXTRAARG, 0));
emit_global_set(node.v[0].str, table);