forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTree.cpp
3482 lines (3213 loc) · 137 KB
/
ASTree.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
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 <cstring>
#include <cstdint>
#include <stdexcept>
#include "ASTree.h"
#include "FastStack.h"
#include "pyc_numeric.h"
#include "bytecode.h"
// This must be a triple quote (''' or """), to handle interpolated string literals containing the opposite quote style.
// E.g. f'''{"interpolated "123' literal"}''' -> valid.
// E.g. f"""{"interpolated "123' literal"}""" -> valid.
// E.g. f'{"interpolated "123' literal"}' -> invalid, unescaped quotes in literal.
// E.g. f'{"interpolated \"123\' literal"}' -> invalid, f-string expression does not allow backslash.
// NOTE: Nested f-strings not supported.
#define F_STRING_QUOTE "'''"
static void append_to_chain_store(const PycRef<ASTNode>& chainStore,
PycRef<ASTNode> item, FastStack& stack, const PycRef<ASTBlock>& curblock);
/* Use this to determine if an error occurred (and therefore, if we should
* avoid cleaning the output tree) */
static bool cleanBuild;
/* Use this to prevent printing return keywords and newlines in lambdas. */
static bool inLambda = false;
/* Use this to keep track of whether we need to print out any docstring and
* the list of global variables that we are using (such as inside a function). */
static bool printDocstringAndGlobals = false;
/* Use this to keep track of whether we need to print a class or module docstring */
static bool printClassDocstring = true;
// shortcut for all top/pop calls
static PycRef<ASTNode> StackPopTop(FastStack& stack)
{
const auto node(stack.top());
stack.pop();
return node;
}
/* compiler generates very, VERY similar byte code for if/else statement block and if-expression
* statement
* if a: b = 1
* else: b = 2
* expression:
* b = 1 if a else 2
* (see for instance https://stackoverflow.com/a/52202007)
* here, try to guess if just finished else statement is part of if-expression (ternary operator)
* if it is, remove statements from the block and put a ternary node on top of stack
*/
static void CheckIfExpr(FastStack& stack, PycRef<ASTBlock> curblock)
{
if (stack.empty())
return;
if (curblock->nodes().size() < 2)
return;
auto rit = curblock->nodes().crbegin();
// the last is "else" block, the one before should be "if" (could be "for", ...)
if ((*rit)->type() != ASTNode::NODE_BLOCK ||
(*rit).cast<ASTBlock>()->blktype() != ASTBlock::BLK_ELSE)
return;
++rit;
if ((*rit)->type() != ASTNode::NODE_BLOCK ||
(*rit).cast<ASTBlock>()->blktype() != ASTBlock::BLK_IF)
return;
auto else_expr = StackPopTop(stack);
curblock->removeLast();
auto if_block = curblock->nodes().back();
auto if_expr = StackPopTop(stack);
curblock->removeLast();
stack.push(new ASTTernary(std::move(if_block), std::move(if_expr), std::move(else_expr)));
}
PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
{
PycBuffer source(code->code()->value(), code->code()->length());
FastStack stack((mod->majorVer() == 1) ? 20 : code->stackSize());
stackhist_t stack_hist;
std::stack<PycRef<ASTBlock> > blocks;
PycRef<ASTBlock> defblock = new ASTBlock(ASTBlock::BLK_MAIN);
defblock->init();
PycRef<ASTBlock> curblock = defblock;
blocks.push(defblock);
int opcode, operand;
int curpos = 0;
int pos = 0;
int unpack = 0;
bool else_pop = false;
bool need_try = false;
bool variable_annotations = false;
while (!source.atEof()) {
#if defined(BLOCK_DEBUG) || defined(STACK_DEBUG)
fprintf(stderr, "%-7d", pos);
#ifdef STACK_DEBUG
fprintf(stderr, "%-5d", (unsigned int)stack_hist.size() + 1);
#endif
#ifdef BLOCK_DEBUG
for (unsigned int i = 0; i < blocks.size(); i++)
fprintf(stderr, " ");
fprintf(stderr, "%s (%d)", curblock->type_str(), curblock->end());
#endif
fprintf(stderr, "\n");
#endif
curpos = pos;
bc_next(source, mod, opcode, operand, pos);
if (need_try && opcode != Pyc::SETUP_EXCEPT_A) {
need_try = false;
/* Store the current stack for the except/finally statement(s) */
stack_hist.push(stack);
PycRef<ASTBlock> tryblock = new ASTBlock(ASTBlock::BLK_TRY, curblock->end(), true);
blocks.push(tryblock);
curblock = blocks.top();
} else if (else_pop
&& opcode != Pyc::JUMP_FORWARD_A
&& opcode != Pyc::JUMP_IF_FALSE_A
&& opcode != Pyc::JUMP_IF_FALSE_OR_POP_A
&& opcode != Pyc::POP_JUMP_IF_FALSE_A
&& opcode != Pyc::POP_JUMP_FORWARD_IF_FALSE_A
&& opcode != Pyc::JUMP_IF_TRUE_A
&& opcode != Pyc::JUMP_IF_TRUE_OR_POP_A
&& opcode != Pyc::POP_JUMP_IF_TRUE_A
&& opcode != Pyc::POP_JUMP_FORWARD_IF_TRUE_A
&& opcode != Pyc::POP_BLOCK) {
else_pop = false;
PycRef<ASTBlock> prev = curblock;
while (prev->end() < pos
&& prev->blktype() != ASTBlock::BLK_MAIN) {
if (prev->blktype() != ASTBlock::BLK_CONTAINER) {
if (prev->end() == 0) {
break;
}
/* We want to keep the stack the same, but we need to pop
* a level off the history. */
//stack = stack_hist.top();
if (!stack_hist.empty())
stack_hist.pop();
}
blocks.pop();
if (blocks.empty())
break;
curblock = blocks.top();
curblock->append(prev.cast<ASTNode>());
prev = curblock;
CheckIfExpr(stack, curblock);
}
}
//fprintf(stderr, "Debug opcode: %s\n", Pyc::OpcodeName(opcode & 0xFF));
switch (opcode) {
case Pyc::BINARY_OP_A:
{
ASTBinary::BinOp op = ASTBinary::from_binary_op(operand);
if (op == ASTBinary::BIN_INVALID)
fprintf(stderr, "Unsupported `BINARY_OP` operand value: %d\n", operand);
PycRef<ASTNode> right = stack.top();
stack.pop();
PycRef<ASTNode> left = stack.top();
stack.pop();
stack.push(new ASTBinary(left, right, op));
}
break;
case Pyc::BINARY_ADD:
case Pyc::BINARY_AND:
case Pyc::BINARY_DIVIDE:
case Pyc::BINARY_FLOOR_DIVIDE:
case Pyc::BINARY_LSHIFT:
case Pyc::BINARY_MODULO:
case Pyc::BINARY_MULTIPLY:
case Pyc::BINARY_OR:
case Pyc::BINARY_POWER:
case Pyc::BINARY_RSHIFT:
case Pyc::BINARY_SUBTRACT:
case Pyc::BINARY_TRUE_DIVIDE:
case Pyc::BINARY_XOR:
case Pyc::BINARY_MATRIX_MULTIPLY:
case Pyc::INPLACE_ADD:
case Pyc::INPLACE_AND:
case Pyc::INPLACE_DIVIDE:
case Pyc::INPLACE_FLOOR_DIVIDE:
case Pyc::INPLACE_LSHIFT:
case Pyc::INPLACE_MODULO:
case Pyc::INPLACE_MULTIPLY:
case Pyc::INPLACE_OR:
case Pyc::INPLACE_POWER:
case Pyc::INPLACE_RSHIFT:
case Pyc::INPLACE_SUBTRACT:
case Pyc::INPLACE_TRUE_DIVIDE:
case Pyc::INPLACE_XOR:
case Pyc::INPLACE_MATRIX_MULTIPLY:
{
ASTBinary::BinOp op = ASTBinary::from_opcode(opcode);
if (op == ASTBinary::BIN_INVALID)
throw std::runtime_error("Unhandled opcode from ASTBinary::from_opcode");
PycRef<ASTNode> right = stack.top();
stack.pop();
PycRef<ASTNode> left = stack.top();
stack.pop();
stack.push(new ASTBinary(left, right, op));
}
break;
case Pyc::BINARY_SUBSCR:
{
PycRef<ASTNode> subscr = stack.top();
stack.pop();
PycRef<ASTNode> src = stack.top();
stack.pop();
stack.push(new ASTSubscr(src, subscr));
}
break;
case Pyc::BREAK_LOOP:
curblock->append(new ASTKeyword(ASTKeyword::KW_BREAK));
break;
case Pyc::BUILD_CLASS:
{
PycRef<ASTNode> class_code = stack.top();
stack.pop();
PycRef<ASTNode> bases = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
stack.push(new ASTClass(class_code, bases, name));
}
break;
case Pyc::BUILD_FUNCTION:
{
PycRef<ASTNode> fun_code = stack.top();
stack.pop();
stack.push(new ASTFunction(fun_code, {}, {}));
}
break;
case Pyc::BUILD_LIST_A:
{
ASTList::value_t values;
for (int i=0; i<operand; i++) {
values.push_front(stack.top());
stack.pop();
}
stack.push(new ASTList(values));
}
break;
case Pyc::BUILD_SET_A:
{
ASTSet::value_t values;
for (int i=0; i<operand; i++) {
values.push_front(stack.top());
stack.pop();
}
stack.push(new ASTSet(values));
}
break;
case Pyc::BUILD_MAP_A:
if (mod->verCompare(3, 5) >= 0) {
auto map = new ASTMap;
for (int i=0; i<operand; ++i) {
PycRef<ASTNode> value = stack.top();
stack.pop();
PycRef<ASTNode> key = stack.top();
stack.pop();
map->add(key, value);
}
stack.push(map);
} else {
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
stack.push(new ASTMap());
}
break;
case Pyc::BUILD_CONST_KEY_MAP_A:
// Top of stack will be a tuple of keys.
// Values will start at TOS - 1.
{
PycRef<ASTNode> keys = stack.top();
stack.pop();
ASTConstMap::values_t values;
values.reserve(operand);
for (int i = 0; i < operand; ++i) {
PycRef<ASTNode> value = stack.top();
stack.pop();
values.push_back(value);
}
stack.push(new ASTConstMap(keys, values));
}
break;
case Pyc::STORE_MAP:
{
PycRef<ASTNode> key = stack.top();
stack.pop();
PycRef<ASTNode> value = stack.top();
stack.pop();
PycRef<ASTMap> map = stack.top().cast<ASTMap>();
map->add(key, value);
}
break;
case Pyc::BUILD_SLICE_A:
{
if (operand == 2) {
PycRef<ASTNode> end = stack.top();
stack.pop();
PycRef<ASTNode> start = stack.top();
stack.pop();
if (start.type() == ASTNode::NODE_OBJECT
&& start.cast<ASTObject>()->object() == Pyc_None) {
start = NULL;
}
if (end.type() == ASTNode::NODE_OBJECT
&& end.cast<ASTObject>()->object() == Pyc_None) {
end = NULL;
}
if (start == NULL && end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE0));
} else if (start == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE2, start, end));
} else if (end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, start, end));
} else {
stack.push(new ASTSlice(ASTSlice::SLICE3, start, end));
}
} else if (operand == 3) {
PycRef<ASTNode> step = stack.top();
stack.pop();
PycRef<ASTNode> end = stack.top();
stack.pop();
PycRef<ASTNode> start = stack.top();
stack.pop();
if (start.type() == ASTNode::NODE_OBJECT
&& start.cast<ASTObject>()->object() == Pyc_None) {
start = NULL;
}
if (end.type() == ASTNode::NODE_OBJECT
&& end.cast<ASTObject>()->object() == Pyc_None) {
end = NULL;
}
if (step.type() == ASTNode::NODE_OBJECT
&& step.cast<ASTObject>()->object() == Pyc_None) {
step = NULL;
}
/* We have to do this as a slice where one side is another slice */
/* [[a:b]:c] */
if (start == NULL && end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE0));
} else if (start == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE2, start, end));
} else if (end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, start, end));
} else {
stack.push(new ASTSlice(ASTSlice::SLICE3, start, end));
}
PycRef<ASTNode> lhs = stack.top();
stack.pop();
if (step == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, lhs, step));
} else {
stack.push(new ASTSlice(ASTSlice::SLICE3, lhs, step));
}
}
}
break;
case Pyc::BUILD_STRING_A:
{
// Nearly identical logic to BUILD_LIST
ASTList::value_t values;
for (int i = 0; i < operand; i++) {
values.push_front(stack.top());
stack.pop();
}
stack.push(new ASTJoinedStr(values));
}
break;
case Pyc::BUILD_TUPLE_A:
{
// if class is a closure code, ignore this tuple
PycRef<ASTNode> tos = stack.top();
if (tos->type() == ASTNode::NODE_LOADBUILDCLASS) {
break;
}
ASTTuple::value_t values;
values.resize(operand);
for (int i=0; i<operand; i++) {
values[operand-i-1] = stack.top();
stack.pop();
}
stack.push(new ASTTuple(values));
}
break;
case Pyc::KW_NAMES_A:
{
int kwparams = code->getConst(operand).cast<PycTuple>()->size();
ASTKwNamesMap kwparamList;
std::vector<PycRef<PycObject>> keys = code->getConst(operand).cast<PycSimpleSequence>()->values();
for (int i = 0; i < kwparams; i++) {
kwparamList.add(new ASTObject(keys[kwparams - i - 1]), stack.top());
stack.pop();
}
stack.push(new ASTKwNamesMap(kwparamList));
}
break;
case Pyc::CALL_A:
case Pyc::CALL_FUNCTION_A:
case Pyc::INSTRUMENTED_CALL_A:
{
int kwparams = (operand & 0xFF00) >> 8;
int pparams = (operand & 0xFF);
ASTCall::kwparam_t kwparamList;
ASTCall::pparam_t pparamList;
/* Test for the load build class function */
stack_hist.push(stack);
int basecnt = 0;
ASTTuple::value_t bases;
bases.resize(basecnt);
PycRef<ASTNode> TOS = stack.top();
int TOS_type = TOS.type();
// bases are NODE_NAME and NODE_BINARY at TOS
while (TOS_type == ASTNode::NODE_NAME || TOS_type == ASTNode::NODE_BINARY) {
bases.resize(basecnt + 1);
bases[basecnt] = TOS;
basecnt++;
stack.pop();
TOS = stack.top();
TOS_type = TOS.type();
}
// qualified name is PycString at TOS
PycRef<ASTNode> name = stack.top();
stack.pop();
PycRef<ASTNode> function = stack.top();
stack.pop();
PycRef<ASTNode> loadbuild = stack.top();
stack.pop();
int loadbuild_type = loadbuild.type();
if (loadbuild_type == ASTNode::NODE_LOADBUILDCLASS) {
PycRef<ASTNode> call = new ASTCall(function, pparamList, kwparamList);
stack.push(new ASTClass(call, new ASTTuple(bases), name));
stack_hist.pop();
break;
}
else
{
stack = stack_hist.top();
stack_hist.pop();
}
/*
KW_NAMES(i)
Stores a reference to co_consts[consti] into an internal variable for use by CALL.
co_consts[consti] must be a tuple of strings.
New in version 3.11.
*/
if (mod->verCompare(3, 11) >= 0) {
PycRef<ASTNode> object_or_map = stack.top();
if (object_or_map.type() == ASTNode::NODE_KW_NAMES_MAP) {
stack.pop();
PycRef<ASTKwNamesMap> kwparams_map = object_or_map.cast<ASTKwNamesMap>();
for (ASTKwNamesMap::map_t::const_iterator it = kwparams_map->values().begin(); it != kwparams_map->values().end(); it++) {
kwparamList.push_front(std::make_pair(it->first, it->second));
pparams -= 1;
}
}
}
else {
for (int i = 0; i < kwparams; i++) {
PycRef<ASTNode> val = stack.top();
stack.pop();
PycRef<ASTNode> key = stack.top();
stack.pop();
kwparamList.push_front(std::make_pair(key, val));
}
}
for (int i=0; i<pparams; i++) {
PycRef<ASTNode> param = stack.top();
stack.pop();
if (param.type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> fun_code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = fun_code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name();
if (function_name->isEqual("<lambda>")) {
pparamList.push_front(param);
} else {
// Decorator used
PycRef<ASTNode> decor_name = new ASTName(function_name);
curblock->append(new ASTStore(param, decor_name));
pparamList.push_front(decor_name);
}
} else {
pparamList.push_front(param);
}
}
PycRef<ASTNode> func = stack.top();
stack.pop();
if ((opcode == Pyc::CALL_A || opcode == Pyc::INSTRUMENTED_CALL_A) &&
stack.top() == nullptr) {
stack.pop();
}
stack.push(new ASTCall(func, pparamList, kwparamList));
}
break;
case Pyc::CALL_FUNCTION_VAR_A:
{
PycRef<ASTNode> var = stack.top();
stack.pop();
int kwparams = (operand & 0xFF00) >> 8;
int pparams = (operand & 0xFF);
ASTCall::kwparam_t kwparamList;
ASTCall::pparam_t pparamList;
for (int i=0; i<kwparams; i++) {
PycRef<ASTNode> val = stack.top();
stack.pop();
PycRef<ASTNode> key = stack.top();
stack.pop();
kwparamList.push_front(std::make_pair(key, val));
}
for (int i=0; i<pparams; i++) {
pparamList.push_front(stack.top());
stack.pop();
}
PycRef<ASTNode> func = stack.top();
stack.pop();
PycRef<ASTNode> call = new ASTCall(func, pparamList, kwparamList);
call.cast<ASTCall>()->setVar(var);
stack.push(call);
}
break;
case Pyc::CALL_FUNCTION_KW_A:
{
PycRef<ASTNode> kw = stack.top();
stack.pop();
int kwparams = (operand & 0xFF00) >> 8;
int pparams = (operand & 0xFF);
ASTCall::kwparam_t kwparamList;
ASTCall::pparam_t pparamList;
for (int i=0; i<kwparams; i++) {
PycRef<ASTNode> val = stack.top();
stack.pop();
PycRef<ASTNode> key = stack.top();
stack.pop();
kwparamList.push_front(std::make_pair(key, val));
}
for (int i=0; i<pparams; i++) {
pparamList.push_front(stack.top());
stack.pop();
}
PycRef<ASTNode> func = stack.top();
stack.pop();
PycRef<ASTNode> call = new ASTCall(func, pparamList, kwparamList);
call.cast<ASTCall>()->setKW(kw);
stack.push(call);
}
break;
case Pyc::CALL_FUNCTION_VAR_KW_A:
{
PycRef<ASTNode> kw = stack.top();
stack.pop();
PycRef<ASTNode> var = stack.top();
stack.pop();
int kwparams = (operand & 0xFF00) >> 8;
int pparams = (operand & 0xFF);
ASTCall::kwparam_t kwparamList;
ASTCall::pparam_t pparamList;
for (int i=0; i<kwparams; i++) {
PycRef<ASTNode> val = stack.top();
stack.pop();
PycRef<ASTNode> key = stack.top();
stack.pop();
kwparamList.push_front(std::make_pair(key, val));
}
for (int i=0; i<pparams; i++) {
pparamList.push_front(stack.top());
stack.pop();
}
PycRef<ASTNode> func = stack.top();
stack.pop();
PycRef<ASTNode> call = new ASTCall(func, pparamList, kwparamList);
call.cast<ASTCall>()->setKW(kw);
call.cast<ASTCall>()->setVar(var);
stack.push(call);
}
break;
case Pyc::CALL_METHOD_A:
{
ASTCall::pparam_t pparamList;
for (int i = 0; i < operand; i++) {
PycRef<ASTNode> param = stack.top();
stack.pop();
if (param.type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> fun_code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = fun_code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name();
if (function_name->isEqual("<lambda>")) {
pparamList.push_front(param);
} else {
// Decorator used
PycRef<ASTNode> decor_name = new ASTName(function_name);
curblock->append(new ASTStore(param, decor_name));
pparamList.push_front(decor_name);
}
} else {
pparamList.push_front(param);
}
}
PycRef<ASTNode> func = stack.top();
stack.pop();
stack.push(new ASTCall(func, pparamList, ASTCall::kwparam_t()));
}
break;
case Pyc::CONTINUE_LOOP_A:
curblock->append(new ASTKeyword(ASTKeyword::KW_CONTINUE));
break;
case Pyc::COMPARE_OP_A:
{
PycRef<ASTNode> right = stack.top();
stack.pop();
PycRef<ASTNode> left = stack.top();
stack.pop();
auto arg = operand;
if (mod->verCompare(3, 12) >= 0)
arg >>= 4; // changed under GH-100923
stack.push(new ASTCompare(left, right, arg));
}
break;
case Pyc::CONTAINS_OP_A:
{
PycRef<ASTNode> right = stack.top();
stack.pop();
PycRef<ASTNode> left = stack.top();
stack.pop();
// The operand will be 0 for 'in' and 1 for 'not in'.
stack.push(new ASTCompare(left, right, operand ? ASTCompare::CMP_NOT_IN : ASTCompare::CMP_IN));
}
break;
case Pyc::DELETE_ATTR_A:
{
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR)));
}
break;
case Pyc::DELETE_GLOBAL_A:
code->markGlobal(code->getName(operand));
/* Fall through */
case Pyc::DELETE_NAME_A:
{
PycRef<PycString> varname = code->getName(operand);
if (varname->length() >= 2 && varname->value()[0] == '_'
&& varname->value()[1] == '[') {
/* Don't show deletes that are a result of list comps. */
break;
}
PycRef<ASTNode> name = new ASTName(varname);
curblock->append(new ASTDelete(name));
}
break;
case Pyc::DELETE_FAST_A:
{
PycRef<ASTNode> name;
if (mod->verCompare(1, 3) < 0)
name = new ASTName(code->getName(operand));
else
name = new ASTName(code->getLocal(operand));
if (name.cast<ASTName>()->name()->value()[0] == '_'
&& name.cast<ASTName>()->name()->value()[1] == '[') {
/* Don't show deletes that are a result of list comps. */
break;
}
curblock->append(new ASTDelete(name));
}
break;
case Pyc::DELETE_SLICE_0:
{
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE0))));
}
break;
case Pyc::DELETE_SLICE_1:
{
PycRef<ASTNode> upper = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE1, upper))));
}
break;
case Pyc::DELETE_SLICE_2:
{
PycRef<ASTNode> lower = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE2, NULL, lower))));
}
break;
case Pyc::DELETE_SLICE_3:
{
PycRef<ASTNode> lower = stack.top();
stack.pop();
PycRef<ASTNode> upper = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE3, upper, lower))));
}
break;
case Pyc::DELETE_SUBSCR:
{
PycRef<ASTNode> key = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, key)));
}
break;
case Pyc::DUP_TOP:
{
if (stack.top().type() == PycObject::TYPE_NULL) {
stack.push(stack.top());
} else if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
auto chainstore = stack.top();
stack.pop();
stack.push(stack.top());
stack.push(chainstore);
} else {
stack.push(stack.top());
ASTNodeList::list_t targets;
stack.push(new ASTChainStore(targets, stack.top()));
}
}
break;
case Pyc::DUP_TOP_TWO:
{
PycRef<ASTNode> first = stack.top();
stack.pop();
PycRef<ASTNode> second = stack.top();
stack.push(first);
stack.push(second);
stack.push(first);
}
break;
case Pyc::DUP_TOPX_A:
{
std::stack<PycRef<ASTNode> > first;
std::stack<PycRef<ASTNode> > second;
for (int i = 0; i < operand; i++) {
PycRef<ASTNode> node = stack.top();
stack.pop();
first.push(node);
second.push(node);
}
while (first.size()) {
stack.push(first.top());
first.pop();
}
while (second.size()) {
stack.push(second.top());
second.pop();
}
}
break;
case Pyc::END_FINALLY:
{
bool isFinally = false;
if (curblock->blktype() == ASTBlock::BLK_FINALLY) {
PycRef<ASTBlock> final = curblock;
blocks.pop();
stack = stack_hist.top();
stack_hist.pop();
curblock = blocks.top();
curblock->append(final.cast<ASTNode>());
isFinally = true;
} else if (curblock->blktype() == ASTBlock::BLK_EXCEPT) {
blocks.pop();
PycRef<ASTBlock> prev = curblock;
bool isUninitAsyncFor = false;
if (blocks.top()->blktype() == ASTBlock::BLK_CONTAINER) {
auto container = blocks.top();
blocks.pop();
auto asyncForBlock = blocks.top();
isUninitAsyncFor = asyncForBlock->blktype() == ASTBlock::BLK_ASYNCFOR && !asyncForBlock->inited();
if (isUninitAsyncFor) {
auto tryBlock = container->nodes().front().cast<ASTBlock>();
if (!tryBlock->nodes().empty() && tryBlock->blktype() == ASTBlock::BLK_TRY) {
auto store = tryBlock->nodes().front().try_cast<ASTStore>();
if (store) {
asyncForBlock.cast<ASTIterBlock>()->setIndex(store->dest());
}
}
curblock = blocks.top();
stack = stack_hist.top();
stack_hist.pop();
if (!curblock->inited())
fprintf(stderr, "Error when decompiling 'async for'.\n");
} else {
blocks.push(container);
}
}
if (!isUninitAsyncFor) {
if (curblock->size() != 0) {
blocks.top()->append(curblock.cast<ASTNode>());
}
curblock = blocks.top();
/* Turn it into an else statement. */
if (curblock->end() != pos || curblock.cast<ASTContainerBlock>()->hasFinally()) {
PycRef<ASTBlock> elseblk = new ASTBlock(ASTBlock::BLK_ELSE, prev->end());
elseblk->init();
blocks.push(elseblk);
curblock = blocks.top();
}
else {
stack = stack_hist.top();
stack_hist.pop();
}
}
}
if (curblock->blktype() == ASTBlock::BLK_CONTAINER) {
/* This marks the end of the except block(s). */
PycRef<ASTContainerBlock> cont = curblock.cast<ASTContainerBlock>();
if (!cont->hasFinally() || isFinally) {
/* If there's no finally block, pop the container. */
blocks.pop();
curblock = blocks.top();
curblock->append(cont.cast<ASTNode>());
}
}
}
break;
case Pyc::EXEC_STMT:
{
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
PycRef<ASTNode> loc = stack.top();
stack.pop();
PycRef<ASTNode> glob = stack.top();
stack.pop();
PycRef<ASTNode> stmt = stack.top();
stack.pop();
curblock->append(new ASTExec(stmt, glob, loc));
}
break;
case Pyc::FOR_ITER_A:
case Pyc::INSTRUMENTED_FOR_ITER_A:
{
PycRef<ASTNode> iter = stack.top(); // Iterable
stack.pop();
/* Pop it? Don't pop it? */
int end;
bool comprehension = false;
// before 3.8, there is a SETUP_LOOP instruction with block start and end position,
// the operand is usually a jump to a POP_BLOCK instruction
// after 3.8, block extent has to be inferred implicitly; the operand is a jump to a position after the for block
if (mod->majorVer() == 3 && mod->minorVer() >= 8) {
end = operand;
if (mod->verCompare(3, 10) >= 0)
end *= sizeof(uint16_t); // // BPO-27129
end += pos;
comprehension = strcmp(code->name()->value(), "<listcomp>") == 0;
} else {
PycRef<ASTBlock> top = blocks.top();
end = top->end(); // block end position from SETUP_LOOP
if (top->blktype() == ASTBlock::BLK_WHILE) {
blocks.pop();
} else {
comprehension = true;
}
}
PycRef<ASTIterBlock> forblk = new ASTIterBlock(ASTBlock::BLK_FOR, curpos, end, iter);
forblk->setComprehension(comprehension);
blocks.push(forblk.cast<ASTBlock>());
curblock = blocks.top();
stack.push(NULL);
}
break;
case Pyc::FOR_LOOP_A:
{
PycRef<ASTNode> curidx = stack.top(); // Current index
stack.pop();
PycRef<ASTNode> iter = stack.top(); // Iterable
stack.pop();
bool comprehension = false;
PycRef<ASTBlock> top = blocks.top();
if (top->blktype() == ASTBlock::BLK_WHILE) {
blocks.pop();
} else {
comprehension = true;
}
PycRef<ASTIterBlock> forblk = new ASTIterBlock(ASTBlock::BLK_FOR, curpos, top->end(), iter);
forblk->setComprehension(comprehension);
blocks.push(forblk.cast<ASTBlock>());
curblock = blocks.top();
/* Python Docs say:
"push the sequence, the incremented counter,
and the current item onto the stack." */
stack.push(iter);
stack.push(curidx);
stack.push(NULL); // We can totally hack this >_>
}
break;
case Pyc::GET_AITER:
{
// Logic similar to FOR_ITER_A
PycRef<ASTNode> iter = stack.top(); // Iterable
stack.pop();
PycRef<ASTBlock> top = blocks.top();
if (top->blktype() == ASTBlock::BLK_WHILE) {
blocks.pop();
PycRef<ASTIterBlock> forblk = new ASTIterBlock(ASTBlock::BLK_ASYNCFOR, curpos, top->end(), iter);
blocks.push(forblk.cast<ASTBlock>());
curblock = blocks.top();
stack.push(nullptr);
} else {
fprintf(stderr, "Unsupported use of GET_AITER outside of SETUP_LOOP\n");
}
}
break;
case Pyc::GET_ANEXT:
break;
case Pyc::FORMAT_VALUE_A:
{
auto conversion_flag = static_cast<ASTFormattedValue::ConversionFlag>(operand);
switch (conversion_flag) {
case ASTFormattedValue::ConversionFlag::NONE:
case ASTFormattedValue::ConversionFlag::STR:
case ASTFormattedValue::ConversionFlag::REPR:
case ASTFormattedValue::ConversionFlag::ASCII:
{
auto val = stack.top();
stack.pop();
stack.push(new ASTFormattedValue(val, conversion_flag, nullptr));
}
break;
case ASTFormattedValue::ConversionFlag::FMTSPEC:
{
auto format_spec = stack.top();
stack.pop();
auto val = stack.top();
stack.pop();
stack.push(new ASTFormattedValue(val, conversion_flag, format_spec));
}