forked from t-wy/web-python-bytecode-disassembler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdis.js
More file actions
1662 lines (1643 loc) · 78.6 KB
/
dis.js
File metadata and controls
1662 lines (1643 loc) · 78.6 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
// this file is created by t-wy
// this repr is for generating code
function real_repr(object) {
return object.real_repr ?? object.repr;
}
function subscr_repr(object) {
// handle the [x] syntax
// e.g [1], ["key"], [1:2:3], [1:,:2,(3,4)]
var to_process = [];
if (object.type === "tuple") {
to_process = object.children;
} else {
to_process.push(object);
}
var reprs = [];
for (var i = 0; i < to_process.length; ++i) {
var entry = to_process[i];
if (entry.type === "slice") {
let start_repr = entry.start === None ? "" : real_repr(entry.start);
let stop_repr = entry.stop === None ? "" : real_repr(entry.stop);
let step_repr = entry.step === None ? "" : (":" + real_repr(entry.step));
reprs.push(`${start_repr}:${stop_repr}${step_repr}`);
} else {
reprs.push(real_repr(entry));
}
}
return `${reprs.join(", ")}`;
}
function get_priority(object) {
return object.priority ?? 0;
}
// Lib/dis.py
var show_caches = false;
function dis(code_dict) {
var {version, argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, freevars, cellvars, localsplusnames, localspluskinds, filename, name, qualname, firstlineno, lnotab, linetable, exceptiontable} = code_dict;
let lines = [];
let compiler_flag_names = {1: "OPTIMIZED", 2: "NEWLOCALS", 4: "VARARGS", 8: "VARKEYWORDS", 16: "NESTED", 32: "GENERATOR", 64: "NOFREE", 128: "COROUTINE", 256: "ITERABLE_COROUTINE", 512: "ASYNC_GENERATOR"};
let flag_names = [];
lines.push(" Show Code");
lines.push("------------------------------");
let remaining_flag = flags;
for (let i = 0; i < 32; ++i) {
let flag = 1 << i;
if (remaining_flag & flag) {
let flag_name = compiler_flag_names[flag];
flag_names.push(flag_name === undefined ? flag.toString(16) : flag_name);
remaining_flag ^= flag
if (remaining_flag === 0) break;
}
}
if (remaining_flag) {
flag_names.push(remaining_flag.toString(16));
}
if (flag_names.length === 0) {
flag_names.push("0x0");
}
lines.push("Name: " + name.str);
if (array_compare(version, [3]) >= 0) { // str
lines.push("Filename: " + filename.str);
} else { // bytes
lines.push("Filename: " + filename.str);
}
lines.push("Argument count: " + argcount);
if (array_compare(version, [3, 8]) >= 0) {
lines.push("Positional-only arguments: " + posonlyargcount);
};
if (array_compare(version, [3]) >= 0) {
lines.push("Kw-only arguments: " + kwonlyargcount);
};
lines.push("Number of locals: " + nlocals);
lines.push("Stack size: " + stacksize);
lines.push("Flags: " + flag_names.join(", "));
if (consts.children.length) {
lines.push("Constants:");
consts.children.forEach((item, index) => {
lines.push(`${index.toString().padStart(4)}: ${item.repr}`);
})
}
if (names.children.length) {
lines.push("Names:");
names.children.forEach((item, index) => {
lines.push(`${index.toString().padStart(4)}: ${item.str}`);
})
}
if (varnames.children.length) {
lines.push("Variable names:");
varnames.children.forEach((item, index) => {
lines.push(`${index.toString().padStart(4)}: ${item.str}`);
})
}
if (freevars.children.length) {
lines.push("Free variables:");
freevars.children.forEach((item, index) => {
lines.push(`${index.toString().padStart(4)}: ${item.str}`);
})
}
if (cellvars.children.length) {
lines.push("Cell variables:");
cellvars.children.forEach((item, index) => {
lines.push(`${index.toString().padStart(4)}: ${item.str}`);
})
}
lines.push("==============================");
lines.push(" Disassembly");
lines.push("------------------------------");
var unsupported_opcodes = new Set();
var source_code = [];
var scope_entries = [
/*
Format: {"type":, "start":, "end": , "istart": , "iend": , }
start / end: facilitates jumps
istart / iend: facilitates indentation
*/
];
if (array_compare(version, [2, 6]) >= 0 && array_compare(version, [3, 13]) <= 0) { // opargs
let opargs = [];
let version_str = version.join(".");
let opcode_list = opcodes[version_str] ?? null;
let inline_cache_entries = _inline_cache_entries[version_str] ?? null;
if (opcode_list === null) {
lines.push("Currently Not Supported");
}
// _unpack_opargs
if (opcode_list !== null) {
let extended_arg = 0;
let arg;
if (array_compare(version, [3, 6]) >= 0) {
for (let i = 0; i < code.value.length; i += 2) {
let op = code.value[i];
if (op >= opcode_list["HAVE_ARGUMENT"]) {
arg = code.value[i + 1] | extended_arg;
extended_arg = op === opcode_list["EXTENDED_ARG"] ? arg << 8 : 0;
} else {
arg = null;
if (array_compare(version, [3, 10]) >= 0) {
extended_arg = 0;
}
}
opargs.push({"offset": i, "opcode": op, "arg": arg});
}
} else {
for (let i = 0; i < code.value.length;) {
let offset = i;
let op = code.value[i];
++i;
if (op >= opcode_list["HAVE_ARGUMENT"]) {
arg = code.value[i] + code.value[i + 1] * 256 + extended_arg;
extended_arg = 0;
i += 2;
if (op === opcode_list["EXTENDED_ARG"]) {
extended_arg = arg << 16;
}
}
opargs.push({"offset": offset, "opcode": op, "arg": arg});
}
}
}
// lines.push(JSON.stringify(opargs));
// findlinestarts
let linestarts = {};
if (array_compare(version, [3, 11]) >= 0) {
// co_lines from codeobject.c
let lastlineno = null;
let lineno = firstlineno;
let addr = 0;
let byte_incr = null;
let line_incr = null;
let shift = -1;
let varint = 0;
// lineiter_next
var cursor = 0;
while (cursor < linetable.value.length) {
var incr = linetable.value[cursor];
line_incr = {
15: 0, // PY_CODE_LOCATION_INFO_NONE
13: 3, // PY_CODE_LOCATION_INFO_NO_COLUMNS,
14: 3, // PY_CODE_LOCATION_INFO_LONG
10: 0, // PY_CODE_LOCATION_INFO_ONE_LINE0
11: 1, // PY_CODE_LOCATION_INFO_ONE_LINE1
12: 2, // PY_CODE_LOCATION_INFO_ONE_LINE2
}[(incr >> 3) & 15] ?? 0; // Same line
if (line_incr === 3) {
var cursor2 = cursor + 1;
shift = 0; // scan_signed_varint
var read = linetable.value[cursor2++];
line_incr = read & 63;
while (read & 64) {
read = linetable.value[cursor2++];
shift += 6;
line_incr |= (read & 63) << shift;
};
if (line_incr & 1) {
line_incr = -(line_incr >> 1);
} else {
line_incr = line_incr >> 1;
}
}
lineno += line_incr;
// is_no_line_marker: (incr >> 3) === 0x1f
if (!((incr >> 3) === 0x1f)) {
if (lineno !== lastlineno) {
linestarts[addr] = lineno;
lastlineno = lineno;
}
}
// next_code_delta
addr += ((incr & 7) + 1) * 2; // sizeof(CODEUNIT);
++cursor;
while (cursor < linetable.value.length && (linetable.value[cursor] & 128) === 0) {
++cursor;
}
}
// console.log(linestarts);
} else if (array_compare(version, [3, 10]) >= 0) {
// co_lines from codeobject.c
let lastlineno = null;
let lineno = firstlineno;
let addr = 0;
let loc = false; // delta, ldelta
let byte_incr = null;
// lineiter_next
linetable.value.forEach(line_incr => {
loc = !loc;
if (loc) {
byte_incr = line_incr;
} else {
// line_incr
if (line_incr !== 0x80) {
lineno += line_incr - (line_incr >= 0x80 ? 0x100 : 0);
if (byte_incr) {
if (lastlineno !== lineno) {
linestarts[addr] = lineno;
lastlineno = lineno;
}
}
}
addr += byte_incr;
}
})
// console.log(linestarts);
} else {
let lastlineno = null;
let lineno = firstlineno;
let addr = 0;
let loc = false; // byte_incr, line_incr
let byte_incr = null;
lnotab.value.forEach(line_incr => {
loc = !loc;
if (loc) {
byte_incr = line_incr;
} else {
// byte_incr
if (byte_incr) {
if (lineno !== lastlineno) {
linestarts[addr] = lineno;
lastlineno = lineno;
}
}
addr += byte_incr;
// line_incr
if (array_compare(version, [3, 6]) >= 0) {
lineno += line_incr - (line_incr >= 0x80 ? 0x100 : 0);
} else {
lineno += line_incr;
}
}
})
if (lineno !== lastlineno) {
linestarts[addr] = lineno;
}
// console.log(linestarts);
}
// lines.push(JSON.stringify(linestarts));
// findlabels
var directed_graph = [];
var graph_wait = [0];
let labels = new Set();
let label;
var last_offset = null;
var connected = true;
opargs.forEach(item => {
if (connected && last_offset !== null) {
directed_graph.push([last_offset, item.offset]);
}
connected = true;
last_offset = item.offset;
let opcode = opcode_list["opmap"][item.opcode] ?? `<${item.opcode}>`;
if (item.arg !== null) {
if (opcode_list["hasjrel"].includes(item.opcode)) {
// relative jump
var signed_arg = item.arg;
if (array_compare(version, [3, 10]) >= 0) {
if (array_compare(version, [3, 11]) >= 0) {
if (opcode.indexOf("JUMP_BACKWARD") === 0) {
signed_arg = -signed_arg;
}
}
label = item.offset + 2 + signed_arg * 2;
if (array_compare(version, [3, 12]) >= 0) {
label += 2 * (inline_cache_entries[opcode] ?? 0);
}
} else if (array_compare(version, [3, 6]) >= 0) {
label = item.offset + 2 + signed_arg;
} else {
label = item.offset + 3 + signed_arg;
}
} else if (opcode_list["hasjabs"].includes(item.opcode)) {
// absolute jump
if (array_compare(version, [3, 10]) >= 0) {
label = item.arg * 2;
} else {
label = item.arg;
}
} else {
return;
}
// set can handle duplicates
labels.add(label);
directed_graph.push([item.offset, label]);
if (opcode.indexOf("IF") === -1) { // unconditional jumps
connected = false;
}
}
})
directed_graph.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
console.log("digraph{\n" + directed_graph.map(x => `"${x[0]}"->"${x[1]}"`).join("\n") + "\n}");
// lines.push(JSON.stringify(labels));
// _parse_exception_table
var exception_entries = [];
if (exceptiontable !== null) {
var cursor = 0;
function _parse_varint() {
var b = exceptiontable.value[cursor++];
var val = b & 63;
while (b & 64) {
val <<= 6;
b = exceptiontable.value[cursor++];
val |= b & 63;
}
return val;
}
while (cursor < exceptiontable.value.length) {
var start = _parse_varint() * 2;
var length = _parse_varint() * 2;
var end = start + length;
var target = _parse_varint() * 2;
var dl = _parse_varint();
var depth = dl >> 1;
var lasti = (dl & 1) === 1;
exception_entries.push({
"start": start,
"end": end,
"target": target,
"depth": depth,
"lasti": lasti
});
}
exception_entries.forEach(entry => {
for (let i = entry.start; i < entry.end; i++) {
labels.add(entry.target);
}
})
}
// _disassemble_bytes
let max_offset = code.value.length - 2;
let offset_width = max_offset >= 10000 ? max_offset.toString().length : 4;
// line lookup
let final_object = opargs.slice(-1)[0].offset;
let line_map = {};
{
let last_offset = 0;
let last_line = firstlineno;
Object.entries(linestarts).forEach(linestart => {
for (let offset = last_offset; offset < linestart[0]; offset++) {
line_map[offset] = last_line;
};
last_offset = linestart[0];
last_line = linestart[1];
})
for (let offset = last_offset; offset <= final_object; offset++) {
line_map[offset] = last_line;
};
}
// print dis
let source_code_stack = [];
let module_counter = 0;
// opcode process
function op1(symbol, TOS, priority) {
let TOS_repr = real_repr(TOS);
if (get_priority(TOS) > priority){
TOS_repr = `(${TOS_repr})`;
}
let expr = `${symbol}${TOS_repr}`;
return {"type": "expression", "repr": expr, "priority": priority};
}
function op2(TOS1, symbol, TOS, priority, right_asso=false) {
let cond;
let TOS1_repr = real_repr(TOS1);
let TOS_repr = real_repr(TOS);
if (right_asso) {
if (get_priority(TOS1) >= priority) {
TOS1_repr = `(${TOS1_repr})`;
}
if (get_priority(TOS) > priority) {
TOS_repr = `(${TOS_repr})`;
}
} else {
if (get_priority(TOS1) > priority) {
TOS1_repr = `(${TOS1_repr})`;
}
if (get_priority(TOS) >= priority) {
TOS_repr = `(${TOS_repr})`;
}
}
if (cond){
expr = `(${expr})`;
}
let expr = `${TOS1_repr} ${symbol} ${TOS_repr}`;
return {"type": "expression", "repr": expr, "priority": priority};
}
function add_scope(type, start, end, istart, iend) {
scope_entries.push({
"type": type,
"start": start,
"end": end,
"istart": istart,
"iend": iend,
})
}
function process_name(offset, item, lvalue) {
// console.debug("process_name", offset, item, varname);
// processing varname = item
if (item.type === "module") {
if (item.import_name === null) {
add_expression(offset, {
"type": "statement",
"repr": `import ${item.module_name}${item.module_name !== lvalue ? ` as ${lvalue}` : ""}`
});
} else {
let module = source_code_stack.pop();
// peek
let peek = null;
if (source_code.length) {
peek = source_code.pop();
}
if (peek !== null && peek.entry.type === "import_statement" && peek.entry.module_id === item.module_id) {
// merge
add_expression(offset, {
"type": "import_statement",
"repr": `${real_repr(peek.entry)}, ${item.import_name}${item.import_name !== lvalue ? ` as ${lvalue}` : ""}`, "module_id": item.module_id
});
} else {
if (peek !== null) {
// return
add_expression(peek.offset, peek.entry);
}
add_expression(offset, {
"type": "import_statement",
"repr": `from ${item.module_name} import ${item.import_name}${item.import_name !== lvalue ? ` as ${lvalue}` : ""}`,
"module_id": item.module_id
});
}
source_code_stack.push(module);
}
} else if (item.type === "function") {
add_expression(offset, {
"type": "statement",
"repr": `def ${lvalue}(???):`
});
} else if (item.type === "unpacked_expression") {
item.children.push(lvalue);
if (item.children.length === item.num) {
if (item.ex) {
item.children.pop();
item.children.push("*" + lvalue);
} else {
add_expression(offset, {
"type": "statement",
"repr": `${item.children.join(", ")} = ${real_repr(item)}`
});
}
} else {
// not fully fed
source_code_stack.push(item);
}
} else if (item.type === "inplace_expression") {
add_expression(offset, {
"type": "statement",
"repr": real_repr(item)
});
} else if (item.type === "for_expression") {
add_expression(offset, {
"type": "statement",
"repr": `for ${lvalue} in ${real_repr(item)}:`
});
add_scope("for", item.start, item.end, offset + 1, item.end);
} else {
add_expression(offset, {
"type": "statement",
"repr": `${lvalue} = ${real_repr(item)}`
});
}
}
function add_expression(offset, expression) {
let line_number = (Object.entries(linestarts).filter(x => offset >= x[0]).slice(-1)[0] ?? [null, null])[1];
source_code.push({
"lineno": line_number,
"offset": offset,
"entry": expression
});
}
opargs.forEach(item => {
// _get_instructions_bytes
let starts_line = linestarts[item.offset] ?? null;
let line_number = (Object.entries(linestarts).filter(x => item.offset >= x[0]).slice(-1)[0] ?? [null, null])[1];
if (starts_line !== null && item.offset > 0) {
// extra line break in opcode
lines.push("");
}
let opcode = opcode_list["opmap"][item.opcode] ?? `<${item.opcode}>`;
console.debug(line_number, opcode, item);
let arg = item.arg;
let argrepr = "";
if (item.arg !== null) {
if (opcode_list["hasconst"].includes(item.opcode)) {
if (arg >= 0 && arg < consts.children.length) {
argrepr = consts.children[arg].repr;
}
} else if (opcode_list["hasname"].includes(item.opcode)) {
if (array_compare(version, [3, 11]) >= 0 && opcode === "LOAD_GLOBAL") {
if (arg >= 0 && (arg >> 1) < names.children.length) {
argrepr = names.children[arg >> 1].str;
} else {
argrepr = (arg >> 1).toString();
}
if (arg & 1) {
if (array_compare(version, [3, 13]) >= 0) {
argrepr = argrepr + " + NULL";
} else {
argrepr = "NULL + " + argrepr;
}
}
} else if (array_compare(version, [3, 12]) >= 0 && opcode === "LOAD_ATTR") {
if (arg >= 0 && (arg >> 1) < names.children.length) {
argrepr = names.children[arg >> 1].str;
} else {
argrepr = (arg >> 1).toString();
}
if (arg & 1) {
if (array_compare(version, [3, 13]) >= 0) {
argrepr = argrepr + " + NULL|self";
} else {
argrepr = "NULL|self + " + argrepr;
}
}
} else if (array_compare(version, [3, 12]) >= 0 && opcode === "LOAD_SUPER_ATTR") {
if (arg >= 0 && (arg >> 2) < names.children.length) {
argrepr = names.children[arg >> 2].str;
} else {
argrepr = (arg >> 2).toString();
}
if (arg & 1) {
if (array_compare(version, [3, 13]) >= 0) {
argrepr = argrepr + " + NULL|self";
} else {
argrepr = "NULL|self + " + argrepr;
}
}
} else {
if (arg >= 0 && arg < names.children.length) {
argrepr = names.children[arg].str;
} else {
argrepr = arg.toString();
}
}
} else if (array_compare(version, [3, 10]) >= 0 && opcode_list["hasjabs"].includes(item.opcode)) {
argrepr = `to ${arg * 2}`;
} else if (opcode_list["hasjrel"].includes(item.opcode)) {
var signed_arg = arg;
if (array_compare(version, [3, 11]) >= 0) {
if (opcode.indexOf('JUMP_BACKWARD') >= 0) {
signed_arg = -signed_arg;
}
}
if (array_compare(version, [3, 10]) >= 0) {
var argval = item.offset + 2 + signed_arg * 2;
if (array_compare(version, [3, 12]) >= 0) {
argval += 2 * (inline_cache_entries[opcode] ?? 0);
}
argrepr = `to ${argval}`;
} else if (array_compare(version, [3, 6]) >= 0) {
argrepr = `to ${item.offset + 2 + signed_arg}`;
} else {
argrepr = `to ${item.offset + 3 + signed_arg}`;
}
} else if (array_compare(version, [3, 13]) >= 0 && ["LOAD_FAST_LOAD_FAST", "STORE_FAST_LOAD_FAST", "STORE_FAST_STORE_FAST"].includes(opcode)) {
var arg1 = arg >> 4;
var arg2 = arg & 15;
var argrepr1, argrepr2;
if (arg1 >= 0 && arg1 < localsplusnames.children.length) {
argrepr1 = localsplusnames.children[arg1].str;
} else {
argrepr1 = arg1.toString();
}
if (arg2 >= 0 && arg2 < localsplusnames.children.length) {
argrepr2 = localsplusnames.children[arg2].str;
} else {
argrepr2 = arg2.toString();
}
argrepr = argrepr1 + ", " + argrepr2;
} else if (opcode_list["haslocal"].includes(item.opcode)) {
if (array_compare(version, [3, 11]) >= 0) {
if (arg >= 0 && arg < localsplusnames.children.length) {
argrepr = localsplusnames.children[arg].str;
} else {
argrepr = arg.toString();
}
} else {
if (arg >= 0 && arg < varnames.children.length) {
argrepr = varnames.children[arg].str;
} else {
argrepr = arg.toString();
}
}
} else if (opcode_list["hascompare"].includes(item.opcode)) {
let actual_arg;
let to_bool = false;
if (array_compare(version, [3, 13]) >= 0) {
actual_arg = arg >> 5;
to_bool = (arg & 16) === 16;
} else if (array_compare(version, [3, 12]) >= 0) {
actual_arg = arg >> 4;
} else {
actual_arg = arg;
}
if (array_compare(version, [3, 12]) >= 0) {
argrepr = ['<', '<=', '==', '!=', '>', '>='][actual_arg] ?? `<${arg}>`;
} else {
argrepr = ['<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD'][actual_arg] ?? `<${arg}>`; // only up to >= for 3.9 thereafter
}
if (to_bool) {
argrepr = `bool(${argrepr})`;
}
} else if (opcode_list["hasfree"].includes(item.opcode)) {
if (array_compare(version, [3, 11] >= 0)) {
if (arg >= 0 && arg < localsplusnames.children.length) {
argrepr = localsplusnames.children[arg].str;
} else {
argrepr = arg.toString();
}
} else {
if (arg >= 0 && arg < cellvars.children.length) {
argrepr = cellvars.children[arg].str;
} else {
argrepr = arg.toString();
}
}
} else if (array_compare(version, [3, 3]) >= 0 && array_compare(version, [3, 11]) <= 0 && opcode_list["hasnargs"].includes(item.opcode)) { // removed in 3.6
argrepr = `${arg % 256} positional, ${arg >> 8} keyword pair`;
} else if (opcode === "FORMAT_VALUE" || opcode === "CONVERT_VALUE") {
argrepr = ['', 'str', 'repr', 'ascii'][arg & 0x3];
if (arg & 0x4) {
if (argrepr !== "") {
argrepr += ", ";
}
argrepr += "with format";
}
} else if (opcode === "MAKE_FUNCTION" || opcode === "SET_FUNCTION_ATTRIBUTE") { // added in 3.8, 3.13
argrepr = ['defaults', 'kwdefaults', 'annotations', 'closure'].filter((_, index) => arg & (1 << index)).join(", ");
} else if (opcode === "BINARY_OP") { // added in 3.11
argrepr = arg < 26 ? (
['+', '&', '//', '<<', '@', '*', '%', '|', '**', '>>', '-', '/', '^'][arg % 13] +
(arg >= 13 ? "=" : "")
) : `<${arg}>`;
} else if (opcode === "CALL_INTRINSIC_1") { // added in 3.12
argrepr = [
"INTRINSIC_1_INVALID",
"INTRINSIC_PRINT",
"INTRINSIC_IMPORT_STAR",
"INTRINSIC_STOPITERATION_ERROR",
"INTRINSIC_ASYNC_GEN_WRAP",
"INTRINSIC_UNARY_POSITIVE",
"INTRINSIC_LIST_TO_TUPLE",
"INTRINSIC_TYPEVAR",
"INTRINSIC_PARAMSPEC",
"INTRINSIC_TYPEVARTUPLE",
"INTRINSIC_SUBSCRIPT_GENERIC",
"INTRINSIC_TYPEALIAS",
][arg] ?? `<${arg}>`;
} else if (opcode === "CALL_INTRINSIC_2") { // added in 3.12
argrepr = [
"INTRINSIC_2_INVALID",
"INTRINSIC_PREP_RERAISE_STAR",
"INTRINSIC_TYPEVAR_WITH_BOUND",
"INTRINSIC_TYPEVAR_WITH_CONSTRAINTS",
"INTRINSIC_SET_FUNCTION_TYPE_PARAMS",
][arg] ?? `<${arg}>`;
}
}
let fields = [
starts_line === null ? " " : starts_line.toString().padStart(3),
" ", // mark_as_current: "-->"
labels.has(item.offset) ? ">>" : " ", // is_jump_target
item.offset.toString().padStart(offset_width),
opcode.padEnd(20),
arg === null ? "" : arg.toString().padStart(5),
(arg === null || argrepr === "") ? "" : `(${argrepr})`
]
if (opcode !== "CACHE" || show_caches) {
lines.push(fields.join(" ").trimEnd());
}
try {
// process opcode (check Python/bytecodes.c)
switch (opcode) {
case "EXTENDED_ARG": break;
case "PUSH_NULL": { // 3.11+
source_code_stack.push({"type": "NULL", "repr": "NULL"});
break;
}
case "POP_TOP": {
let temp_item = source_code_stack.pop();
if (temp_item.type !== "module") {
add_expression(item.offset, temp_item);
}
break;
}
case "ROT_TWO": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
source_code_stack.push(TOS);
source_code_stack.push(TOS1);
break;
}
case "ROT_THREE": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
let TOS2 = source_code_stack.pop();
source_code_stack.push(TOS);
source_code_stack.push(TOS2);
source_code_stack.push(TOS1);
break;
}
case "ROT_FOUR": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
let TOS2 = source_code_stack.pop();
let TOS3 = source_code_stack.pop();
source_code_stack.push(TOS);
source_code_stack.push(TOS3);
source_code_stack.push(TOS2);
source_code_stack.push(TOS1);
break;
}
case "COPY": {
console.assert(arg > 0);
source_code_stack.push(source_code_stack[source_code_stack.length - arg]);
break;
}
case "SWAP": {
let temp = source_code_stack[source_code_stack.length - arg];
source_code_stack[source_code_stack.length - arg] = source_code_stack[source_code_stack.length - 1];
source_code_stack[source_code_stack.length - 1] = temp;
break;
}
case "DUP_TOP": {
let temp_item = source_code_stack.pop();
source_code_stack.push(temp_item);
source_code_stack.push(temp_item);
break;
}
case "DUP_TOP_TWO": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
source_code_stack.push(TOS1);
source_code_stack.push(TOS);
source_code_stack.push(TOS1);
source_code_stack.push(TOS);
break;
}
case "IMPORT_NAME": {
let fromlist = source_code_stack.pop();
let level = source_code_stack.pop();
let varname = names.children[arg].str;
source_code_stack.push({"type": "module", "repr": `<module '${varname}'>`, "module_name": varname, "import_name": null, "module_id": module_counter++});
break;
}
case "IMPORT_FROM": {
let module = source_code_stack.pop();
let varname = names.children[arg].str;
source_code_stack.push(module);
source_code_stack.push({"type": "module", "repr": `<module '${module.module_name}.${varname}'>`, "module_name": module.module_name, "import_name": varname, "module_id": module.module_id});
break;
}
case "IMPORT_STAR": {
let module = source_code_stack.pop();
add_expression(item.offset, {
"type": "import_statement",
"repr": `from ${module.module_name} import *`,
"module_id": module.module_id
});
break;
}
case "LOAD_CONST": {
source_code_stack.push(consts.children[arg]);
break;
}
case "LOAD_NAME": {
let varname = names.children[arg].str;
source_code_stack.push({"type": "expression", "repr": varname, "priority": 0});
break;
}
case "LOAD_FAST": {
let varname;
if (array_compare(version, [3, 11]) >= 0) {
varname = localsplusnames.children[arg].str;
} else {
varname = varnames.children[arg].str;
}
source_code_stack.push({"type": "expression", "repr": varname, "priority": 0});
break;
}
case "LOAD_FAST_LOAD_FAST": { // 3.13
let varname1 = localsplusnames.children[arg >> 4].str;
let varname2 = localsplusnames.children[arg & 15].str;
source_code_stack.push({"type": "expression", "repr": varname1, "priority": 0});
source_code_stack.push({"type": "expression", "repr": varname2, "priority": 0});
break;
}
case "LOAD_CLOSURE": {
let varname;
if (array_compare(version, [3, 11]) >= 0) { // same as LOAD_FAST
varname = localsplusnames.children[arg].str;
} else {
varname = cellvars.children[arg].str;
}
source_code_stack.push({"type": "expression", "repr": varname, "priority": 0});
break;
}
case "LOAD_DEREF": {
let varname;
if (array_compare(version, [3, 11]) >= 0) {
varname = localsplusnames.children[arg].str;
} else {
varname = varnames.children[arg].str;
}
source_code_stack.push({"type": "expression", "repr": varname, "priority": 0});
break;
}
case "LOAD_GLOBAL": {
let varname;
let push_null = false;
if (array_compare(version, [3, 11]) >= 0) {
if (arg & 1) {
push_null = true;
}
varname = names.children[arg >> 1].str;
} else {
varname = names.children[arg].str;
}
if (push_null & array_compare(version, [3, 13]) < 0) {
source_code_stack.push({"type": "NULL", "repr": "NULL"});
}
source_code_stack.push({"type": "expression", "repr": varname, "priority": 0});
if (push_null & array_compare(version, [3, 13]) >= 0) {
source_code_stack.push({"type": "NULL", "repr": "NULL"});
}
break;
}
case "LOAD_ATTR": {
let TOS = source_code_stack.pop();
let name;
let push_null = false;
if (array_compare(version, [3, 12]) >= 0) {
if (arg & 1) {
push_null = true;
}
name = names.children[arg >> 1].str;
} else {
name = names.children[arg].str;
}
let TOS_repr = real_repr(TOS);
if (get_priority(TOS) > 2) {
TOS_repr = `(${TOS_repr})`;
}
if (push_null & array_compare(version, [3, 13]) < 0) {
// NULL just as an unbound method
source_code_stack.push({"type": "NULL", "repr": "NULL"});
}
source_code_stack.push({"type": "expression", "repr": `${TOS_repr}.${name}`, "priority": 2});
if (push_null & array_compare(version, [3, 13]) >= 0) {
source_code_stack.push({"type": "NULL", "repr": "NULL"});
}
break;
}
case "LOAD_METHOD": { // since 3.7
let TOS = source_code_stack.pop();
let TOS_repr = real_repr(TOS);
if (get_priority(TOS) > 2) {
TOS_repr = `(${TOS_repr})`;
}
let name = names.children[arg].str;
if (array_compare(version, [3, 11]) >= 0) {
// NULL just as an unbound method
source_code_stack.push({"type": "NULL", "repr": "NULL"});
}
source_code_stack.push({"type": "expression", "repr": `${TOS_repr}.${name}`, "priority": 2});
break;
}
case "LOAD_BUILD_CLASS": {
source_code_stack.push({"type": "build_class_expression", "repr": "__build_class__"});
break;
}
case "STORE_NAME": {
let temp_item = source_code_stack.pop();
let varname = names.children[arg].str;
process_name(item.offset, temp_item, varname);
break;
}
case "STORE_FAST": {
let temp_item = source_code_stack.pop();
let varname;
if (array_compare(version, [3, 11]) >= 0) {
varname = localsplusnames.children[arg].str;
} else {
varname = varnames.children[arg].str;
}
process_name(item.offset, temp_item, varname);
break;
}
case "STORE_FAST_STORE_FAST": { // 3.13
let temp_item1 = source_code_stack.pop();
let temp_item2 = source_code_stack.pop();
let varname1 = localsplusnames.children[arg >> 4].str;
let varname2 = localsplusnames.children[arg & 15].str;
process_name(item.offset, temp_item1, varname1);
process_name(item.offset, temp_item2, varname2);
break;
}
case "STORE_DEREF": {
let temp_item = source_code_stack.pop();
let varname;
if (array_compare(version, [3, 11]) >= 0) {
varname = localsplusnames.children[arg].str;
} else {
varname = varnames.children[arg].str;
}
process_name(item.offset, temp_item, varname);
break;
}
case "STORE_GLOBAL": {
let temp_item = source_code_stack.pop();
let varname = names.children[arg].str;
process_name(item.offset, temp_item, varname);
break;
}
case "STORE_ATTR": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
let TOS_repr = real_repr(TOS);
if (get_priority(TOS) > 2) {
TOS_repr = `(${TOS_repr})`;
}
let name = names.children[arg].str;
process_name(item.offset, TOS1, `${TOS_repr}.${name}`);
break;
}
case "STORE_SUBSCR": {
let TOS = source_code_stack.pop();
let TOS1 = source_code_stack.pop();
let TOS1_repr = real_repr(TOS1);
if (get_priority(TOS1) > 2) {
TOS1_repr = `(${TOS1_repr})`;
}
let TOS2 = source_code_stack.pop();
process_name(item.offset, TOS2, `${TOS1_repr}[${subscr_repr(TOS)}]`);
break;
}
case "UNPACK_SEQUENCE": {
let TOS = source_code_stack.pop();
source_code_stack.push({"type": "unpacked_expression", "repr": real_repr(TOS), "num": arg, "ex": false, "children": []});
break;
}
case "UNPACK_EX": {