-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyStackVM.py
More file actions
2768 lines (2577 loc) · 101 KB
/
PyStackVM.py
File metadata and controls
2768 lines (2577 loc) · 101 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
import struct
import array
from typing import Union, Optional
float_t = struct.Struct("<f")
double_t = struct.Struct("<d")
BC_NOP = 0
BC_HLT = 1
BC_EQ0 = 2
BC_NE0 = 3
BC_LT0 = 4
BC_LE0 = 5
BC_GT0 = 6
BC_GE0 = 7
BC_CONV = 8
BC_SWAP = 9
BC_LOAD = 10
BC_STOR = 11
BC_CALL_E = 12
BC_RET_E = 13
BC_SYSRET = 14
# TODO: change BC_SYSRET
BC_INT = 15
BC_LSHIFT1 = 16
BC_LSHIFT2 = 17
BC_LSHIFT4 = 18
BC_LSHIFT8 = 19
BC_RSHIFT1 = 20
BC_RSHIFT2 = 21
BC_RSHIFT4 = 22
BC_RSHIFT8 = 23
BC_LROT1 = 24
BC_LROT2 = 25
BC_LROT4 = 26
BC_LROT8 = 27
BC_RROT1 = 28
BC_RROT2 = 29
BC_RROT4 = 30
BC_RROT8 = 31
BC_AND1 = 32
BC_AND2 = 33
BC_AND4 = 34
BC_AND8 = 35
BC_OR1 = 36
BC_OR2 = 37
BC_OR4 = 38
BC_OR8 = 39
BC_NOT1 = 40
BC_NOT2 = 41
BC_NOT4 = 42
BC_NOT8 = 43
BC_XOR1 = 44
BC_XOR2 = 45
BC_XOR4 = 46
BC_XOR8 = 47
BC_ADD1 = 48
BC_ADD2 = 49
BC_ADD4 = 50
BC_ADD8 = 51
BC_SUB1 = 52
BC_SUB2 = 53
BC_SUB4 = 54
BC_SUB8 = 55
BC_ADD_SP1 = 56
BC_ADD_SP2 = 57
BC_ADD_SP4 = 58
BC_ADD_SP8 = 59
BC_RST_SP1 = 60
BC_RST_SP2 = 61
BC_RST_SP4 = 62
BC_RST_SP8 = 63
BC_MUL1 = 64
BC_MUL1S = 65
BC_MUL2 = 66
BC_MUL2S = 67
BC_MUL4 = 68
BC_MUL4S = 69
BC_MUL8 = 70
BC_MUL8S = 71
BC_DIV1 = 72
BC_DIV1S = 73
BC_DIV2 = 74
BC_DIV2S = 75
BC_DIV4 = 76
BC_DIV4S = 77
BC_DIV8 = 78
BC_DIV8S = 79
BC_MOD1 = 80
BC_MOD1S = 81
BC_MOD2 = 82
BC_MOD2S = 83
BC_MOD4 = 84
BC_MOD4S = 85
BC_MOD8 = 86
BC_MOD8S = 87
BC_CMP1 = 88
BC_CMP1S = 89
BC_CMP2 = 90
BC_CMP2S = 91
BC_CMP4 = 92
BC_CMP4S = 93
BC_CMP8 = 94
BC_CMP8S = 95
BC_FADD_2 = 96
BC_FADD_4 = 97
BC_FADD_8 = 98
BC_FADD_16 = 99
BC_FSUB_2 = 100
BC_FSUB_4 = 101
BC_FSUB_8 = 102
BC_FSUB_16 = 103
BC_FMUL_2 = 104
BC_FMUL_4 = 105
BC_FMUL_8 = 106
BC_FMUL_16 = 107
BC_FDIV_2 = 108
BC_FDIV_4 = 109
BC_FDIV_8 = 110
BC_FDIV_16 = 111
BC_FMOD_2 = 112
BC_FMOD_4 = 113
BC_FMOD_8 = 114
BC_FMOD_16 = 115
BC_FCMP_2 = 116
BC_FCMP_4 = 117
BC_FCMP_8 = 118
BC_FCMP_16 = 119
BC_JMP = 120
BC_JMPIF = 121
BC_RJMP = 122
BC_RJMPIF = 123
BC_CALL = 124
BC_RCALL = 125
BC_RET = 126
BC_RET_N2 = 127
BCR_ABS_A4 = 0x00
BCR_ABS_A8 = 0x01
BCR_ABS_S4 = 0x02
BCR_ABS_S8 = 0x03
BCR_R_BP1 = 0x04
BCR_R_BP2 = 0x05
BCR_R_BP4 = 0x06
BCR_R_BP8 = 0x07
BCR_ABS_C = 0x08
BCR_REG_BP = 0x09
BCR_RES = 0x0A
BCR_EA_R_IP = 0x0B
BCR_TOS = 0x0C
BCR_SYSREG = 0x0D
BCR_SZ_1 = 0x0 << 5
BCR_SZ_2 = 0x1 << 5
BCR_SZ_4 = 0x2 << 5
BCR_SZ_8 = 0x3 << 5
BCR_TYP_MASK = 0x1F # low 5 bits
BCR_SZ_MASK = 0xE0 # high 3 bits 0:1, 1:2, 2:4, 3:8
BCR_R_BP_MASK = 0x1C
BCR_R_BP_VAL = 0x04
BCS_SZ1_A = 0x00
BCS_SZ2_A = 0x01
BCS_SZ4_A = 0x02
BCS_SZ8_A = 0x03
BCS_SZ16_A = 0x04
BCS_SZ32_A = 0x05
BCS_SZ64_A = 0x06
BCS_SZ128_A = 0x07
BCS_SZ1_B = 0x00
BCS_SZ2_B = 0x08
BCS_SZ4_B = 0x10
BCS_SZ8_B = 0x18
BCS_SZ16_B = 0x20
BCS_SZ32_B = 0x28
BCS_SZ64_B = 0x30
BCS_SZ128_B = 0x38
BCS_SZ_A_MASK = 0x07
BCS_SZ_B_MASK = 0x38
BCC_I_MASK = 0x0F
BCC_O_MASK = 0xF0
BCC_UI_1_I = 0x00
BCC_SI_1_I = 0x01
BCC_UI_2_I = 0x02
BCC_SI_2_I = 0x03
BCC_UI_4_I = 0x04
BCC_SI_4_I = 0x05
BCC_UI_8_I = 0x06
BCC_SI_8_I = 0x07
BCC_F_2_I = 0x08
BCC_F_4_I = 0x09
BCC_F_8_I = 0x0A
BCC_F_16_I = 0x0B
BCC_UI_1_O = 0x00
BCC_SI_1_O = 0x10
BCC_UI_2_O = 0x20
BCC_SI_2_O = 0x30
BCC_UI_4_O = 0x40
BCC_SI_4_O = 0x50
BCC_UI_8_O = 0x60
BCC_SI_8_O = 0x70
BCC_F_2_O = 0x80
BCC_F_4_O = 0x90
BCC_F_8_O = 0xA0
BCC_F_16_O = 0xB0
BCRE_SYS = 0x1 << 7
BCRE_RST_SP_SZ1 = 0x0 << 5
BCRE_RST_SP_SZ2 = 0x1 << 5
BCRE_RST_SP_SZ4 = 0x2 << 5
BCRE_RST_SP_SZ8 = 0x3 << 5
BCRE_RST_SP_SZ_MASK = 0x3 << 5
BCRE_RES_SZ1 = 0x0 << 3
BCRE_RES_SZ2 = 0x1 << 3
BCRE_RES_SZ4 = 0x2 << 3
BCRE_RES_SZ8 = 0x3 << 3
BCRE_RES_SZ_MASK = 0x3 << 3
BCCE_SYSCALL = 1 << 7
BCCE_N_REL = 1 << 6
BCCE_S_SYSN_SZ1 = 0 << 5
BCCE_S_SYSN_SZ2 = 1 << 5
BCCE_S_SYSN_SZ4 = 2 << 5
BCCE_S_SYSN_SZ8 = 3 << 5
BCCE_S_ARG_SZ1 = 0 << 3
BCCE_S_ARG_SZ2 = 1 << 3
BCCE_S_ARG_SZ4 = 2 << 3
BCCE_S_ARG_SZ8 = 3 << 3
# StackVm SysReg
SVSRB_PTE = 0x00
SVSRB_SP = 0x04
SVSRB_SYS_FN = 0x08
SVSR_HYPER_PTE = 0x00
SVSR_KERNEL_PTE = 0x01
SVSR_USER_PTE = 0x02
SVSR_WEB_PTE = 0x03
SVSR_HYPER_SP = 0x04
SVSR_KERNEL_SP = 0x05
SVSR_USER_SP = 0x06
SVSR_WEB_SP = 0x07
SVSR_HYPER_SYS_FN = 0x08
SVSR_KERNEL_SYS_FN = 0x09
SVSR_USER_SYS_FN = 0x0A
SVSR_WEB_SYS_FN = 0x0B
SVSR_FLAGS = 0x0C
# MISSING allocation for 0x0D
SVSR_HYPER_ISR = 0x0E
SVSR_KERNEL_ISR = 0x0F
StackVM_SVSR_Codes = {
"HYPER_PTE": 0x00, "KERNEL_PTE": 0x01, "USER_PTE": 0x02, "WEB_PTE": 0x03,
"HYPER_SP": 0x04, "KERNEL_SP": 0x05, "USER_SP": 0x06, "WEB_SP": 0x07,
"HYPER_SYS_FN": 0x08, "KERNEL_SYS_FN": 0x09, "USER_SYS_FN": 0x0A, "WEB_SYS_FN": 0x0B,
"FLAGS": 0x0C, "HYPER_ISR": 0x0E, "KERNEL_ISR": 0x0F
}
INT_INVAL_OPCODE = 6
INT_PROTECT_FAULT = 13
INT_PAGE_FAULT = 14
INT_INVAL_SYSCALL = 15
INT_LST = [
"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"UNKNOWN", "UNKNOWN", "INVAL_OPCODE", "UNKNOWN",
"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN",
"UNKNOWN", "PROTECT_FAULT", "PAGE_FAULT", "INVAL_SYSCALL"
] + ["UNKNOWN"] * 240
MRQ_DONT_CHECK = 0
MRQ_READ = 1
MRQ_WRITE = 2
MRQ_EXEC = 3
StackVM_BCRE_Codes = {
"RST_SP_SZ1": 0x00, "RST_SP_SZ2": 0x20, "RST_SP_SZ4": 0x40, "RST_SP_SZ8": 0x60,
"RES_SZ1": 0x0, "RES_SZ2": 0x8, "RES_SZ4": 0x10, "RES_SZ8": 0x18
}
LstStackVM_Codes = [
"NOP", "HLT", "EQ0", "NE0", "LT0", "LE0", "GT0", "GE0",
"CONV", "SWAP", "LOAD", "STOR", "CALL_E", "RET_E", "SYSRET", "INT",
"LSHIFT1", "LSHIFT2", "LSHIFT4", "LSHIFT8", "RSHIFT1", "RSHIFT2", "RSHIFT4", "RSHIFT8",
"LROT1", "LROT2", "LROT4", "LROT8", "RROT1", "RROT2", "RROT4", "RROT8",
"AND1", "AND2", "AND4", "AND8", "OR1", "OR2", "OR4", "OR8",
"NOT1", "NOT2", "NOT4", "NOT8", "XOR1", "XOR2", "XOR4", "XOR8",
"ADD1", "ADD2", "ADD4", "ADD8", "SUB1", "SUB2", "SUB4", "SUB8",
"ADD_SP1", "ADD_SP2", "ADD_SP4", "ADD_SP8", "RST_SP1", "RST_SP2", "RST_SP4", "RST_SP8",
"MUL1", "MUL1S", "MUL2", "MUL2S", "MUL4", "MUL4S", "MUL8", "MUL8S",
"DIV1", "DIV1S", "DIV2", "DIV2S", "DIV4", "DIV4S", "DIV8", "DIV8S",
"MOD1", "MOD1S", "MOD2", "MOD2S", "MOD4", "MOD4S", "MOD8", "MOD8S",
"CMP1", "CMP1S", "CMP2", "CMP2S", "CMP4", "CMP4S", "CMP8", "CMP8S",
"FADD_2", "FADD_4", "FADD_8", "FADD_16", "FSUB_2", "FSUB_4", "FSUB_8", "FSUB_16",
"FMUL_2", "FMUL_4", "FMUL_8", "FMUL_16", "FDIV_2", "FDIV_4", "FDIV_8", "FDIV_16",
"FMOD_2", "FMOD_4", "FMOD_8", "FMOD_16", "FCMP_2", "FCMP_4", "FCMP_8", "FCMP_16",
"JMP", "JMPIF", "RJMP", "RJMPIF", "CALL", "RCALL", "RET", "RET_N2",
]
StackVM_Codes = {
"NOP": 0, "HLT": 1, "EQ0": 2, "NE0": 3, "LT0": 4, "LE0": 5, "GT0": 6, "GE0": 7,
"CONV": 8, "SWAP": 9, "LOAD": 10, "STOR": 11, "CALL_E": 12, "RET_E": 13, "SYSRET": 14, "INT": 15,
"LSHIFT1": 16, "LSHIFT2": 17, "LSHIFT4": 18, "LSHIFT8": 19, "RSHIFT1": 20, "RSHIFT2": 21, "RSHIFT4": 22,
"RSHIFT8": 23,
"LROT1": 24, "LROT2": 25, "LROT4": 26, "LROT8": 27, "RROT1": 28, "RROT2": 29, "RROT4": 30, "RROT8": 31,
"AND1": 32, "AND2": 33, "AND4": 34, "AND8": 35, "OR1": 36, "OR2": 37, "OR4": 38, "OR8": 39,
"NOT1": 40, "NOT2": 41, "NOT4": 42, "NOT8": 43, "XOR1": 44, "XOR2": 45, "XOR4": 46, "XOR8": 47,
"ADD1": 48, "ADD2": 49, "ADD4": 50, "ADD8": 51, "SUB1": 52, "SUB2": 53, "SUB4": 54, "SUB8": 55,
"ADD_SP1": 56, "ADD_SP2": 57, "ADD_SP4": 58, "ADD_SP8": 59, "RST_SP1": 60, "RST_SP2": 61, "RST_SP4": 62,
"RST_SP8": 63,
"MUL1": 64, "MUL1S": 65, "MUL2": 66, "MUL2S": 67, "MUL4": 68, "MUL4S": 69, "MUL8": 70, "MUL8S": 71,
"DIV1": 72, "DIV1S": 73, "DIV2": 74, "DIV2S": 75, "DIV4": 76, "DIV4S": 77, "DIV8": 78, "DIV8S": 79,
"MOD1": 80, "MOD1S": 81, "MOD2": 82, "MOD2S": 83, "MOD4": 84, "MOD4S": 85, "MOD8": 86, "MOD8S": 87,
"CMP1": 88, "CMP1S": 89, "CMP2": 90, "CMP2S": 91, "CMP4": 92, "CMP4S": 93, "CMP8": 94, "CMP8S": 95,
"FADD_2": 96, "FADD_4": 97, "FADD_8": 98, "FADD_16": 99, "FSUB_2": 100, "FSUB_4": 101, "FSUB_8": 102,
"FSUB_16": 103,
"FMUL_2": 104, "FMUL_4": 105, "FMUL_8": 106, "FMUL_16": 107, "FDIV_2": 108, "FDIV_4": 109, "FDIV_8": 110,
"FDIV_16": 111,
"FMOD_2": 112, "FMOD_4": 113, "FMOD_8": 114, "FMOD_16": 115, "FCMP_2": 116, "FCMP_4": 117, "FCMP_8": 118,
"FCMP_16": 119,
"JMP": 120, "JMPIF": 121, "RJMP": 122, "RJMPIF": 123, "CALL": 124, "RCALL": 125, "RET": 126, "RET_N2": 127
}
def _test():
for c in range(len(LstStackVM_Codes)):
assert StackVM_Codes[LstStackVM_Codes[c]] == c, "error c = %u" % c
_test()
StackVM_BCR_Codes = {
"ABS_A4": 0x00, "ABS_A8": 0x01, "ABS_S4": 0x02, "ABS_S8": 0x03,
"R_BP1": 0x04, "R_BP2": 0x05, "R_BP4": 0x06, "R_BP8": 0x07,
"ABS_C": 0x08, "REG_BP": 0x09, "RES": 0x0A, "EA_R_IP": 0x0B, "TOS": 0x0C,
"SYSREG": 0x0D,
"SZ_1": 0x0 << 5, "SZ_2": 0x1 << 5, "SZ_4": 0x2 << 5, "SZ_8": 0x3 << 5
}
StackVM_BCS_Codes = {
"SZ1_A": 0x00, "SZ2_A": 0x01, "SZ4_A": 0x02, "SZ8_A": 0x03,
"SZ16_A": 0x04, "SZ32_A": 0x05, "SZ64_A": 0x06, "SZ128_A": 0x07,
"SZ1_B": 0x00, "SZ2_B": 0x08, "SZ4_B": 0x10, "SZ8_B": 0x18,
"SZ16_B": 0x20, "SZ32_B": 0x28, "SZ64_B": 0x30, "SZ128_B": 0x38
}
StackVM_BCC_Codes = {
"UI_1_I": 0x00, "SI_1_I": 0x01, "UI_2_I": 0x02, "SI_2_I": 0x03,
"UI_4_I": 0x04, "SI_4_I": 0x05, "UI_8_I": 0x06, "SI_8_I": 0x07,
"F_2_I": 0x08, "F_4_I": 0x09, "F_8_I": 0x0A, "F_16_I": 0x0B,
"UI_1_O": 0x00, "SI_1_O": 0x10, "UI_2_O": 0x20, "SI_2_O": 0x30,
"UI_4_O": 0x40, "SI_4_O": 0x50, "UI_8_O": 0x60, "SI_8_O": 0x70,
"F_2_O": 0x80, "F_4_O": 0x90, "F_8_O": 0xA0, "F_16_O": 0xB0
}
StackVM_BCCE_Codes = {
"SYSCALL": 0x80, "N_REL": 0x40,
"S_SYSN_SZ1": 0x00, "S_SYSN_SZ2": 0x20, "S_SYSN_SZ4": 0x40, "S_SYSN_SZ8": 0x60,
"S_ARG_SZ1": 0x00, "S_ARG_SZ2": 0x08, "S_ARG_SZ4": 0x10, "S_ARG_SZ8": 0x18
}
LstStackVM_BCR_Types = [
"ABS_A4", "ABS_A8", "ABS_S4", "ABS_S8",
"R_BP1", "R_BP2", "R_BP4", "R_BP8",
"ABS_C", "REG_BP", "RES", "EA_R_IP",
"TOS", "SYSREG"
]
LstStackVM_sysregs = [
"HYPER_PTE", "KERNEL_PTE", "USER_PTE", "WEB_PTE",
"HYPER_SP", "KERNEL_SP", "USER_SP", "WEB_SP",
"HYPER_SYS_FN", "KERNEL_SYS_FN", "USER_SYS_FN", "WEB_SYS_FN",
"FLAGS", "RESERVED", "HYPER_ISR", "KERNEL_ISR"
]
LstStackVM_BCS_Types = [
"SZ1_", "SZ2_", "SZ4_", "SZ8_",
"SZ16_", "SZ32_", "SZ64_", "SZ128_"
]
LstStackVM_BCC_Types = [
"UI_1_", "SI_1_", "UI_2_", "SI_2_",
"UI_4_", "SI_4_", "UI_8_", "SI_8_",
"F_2_", "F_4_", "F_8_", "F_16_",
]
"""
typedef unsigned long long SizeL;
struct Range {
SizeL Start, End;
};
class IdAllocator {
Range *LstRanges;
SizeL NumRanges, AllocRanges;
SizeL tMin, tMax;
// assume that an IdAllocator when fully constructed satisfies
// (NumRanges == 1 && AllocRanges >= NumRanges && LstRanges != 0 && LstRanges[0].End >= LstRanges[0].Start)
IdAllocator(Range *ArrRanges, SizeL LenArr, SizeL Min, SizeL Max) {
NumRanges = 1;
AllocRanges = LenArr;
LstRanges = ArrRanges;
LstRanges[0].Start = tMin = Min;
LstRanges[0].End = tMax = Max;
}
void ReduceRanges() {
SizeL i = 1;
for (SizeL c = 1; c < NumRanges; ++c) {
if (i != c) LstRanges[i] = LstRanges[c];
if (LstRanges[i - 1].End >= LstRanges[i].Start) {
LstRanges[i - 1].End = LstRanges[i].End;
LstRanges[i].Start = LstRanges[i].End;
} else if (LstRanges[i - 1].Start != LstRanges[i - 1].End) ++i;
}
NumRanges = i;
}
Range GetRange(SizeL Length) {
if (Length == 0) return {LstRanges[0].Start, LstRanges[0].Start};
Range Rtn = {0, 0};
for (SizeL c = 0; c < NumRanges; ++c) {
if (LstRanges[c].Start - LstRanges[c].End >= Length) {
Rtn.Start = LstRanges[c].Start;
Rtn.End = Rtn.Start + Length;
break;
}
}
return Rtn;
}
bool FreeRange(SizeL Start, SizeL Length) {
if (Start < tMin) return false; // outside of range
SizeL End = Start + Length;
if (End > tMax) return false; // outside of range
SizeL c;
for (c = 0; c < NumRanges; ++c) {
if (LstRanges[c].Start <= End) {
LstRanges[c].Start = Start;
ReduceRanges();
return true;
} else if (LstRanges[c].End >= Start) {
if (LstRanges[c].End <= End) LstRanges[c].End = End;
ReduceRanges();
return true;
} else if (LstRanges[c].Start > End) {
break;
}
}
if (c >= NumRanges) return false; // outsize range?
{
SizeL iPos = c;
c = NumRanges;
if (c + 1 > AllocRanges) return false; // not enough static memory
while (c-- > iPos) {
LstRanges[c + 1] = LstRanges[c];
}
++NumRanges;
c = iPos;
}
LstRanges[c] = {Start, End};
ReduceRanges();
return true;
}
}
Range MallocRanges[1024];
IdAllocator MemAlloc(MallocRanges, 1024, __malloc_heap_begin, __malloc_heap_end);
void *malloc(SizeL nBytes) {
Range Res = MemAlloc.GetRange(nBytes + sizeof(nBytes));
if (Res.Start == 0 && Res.End == 0) {
return 0;
}
void *Rtn = (void *)(Res.Start);
*(SizeL *)Rtn = nBytes;
return (void *) ((SizeL) Rtn + sizeof(nBytes));
}
bool free(void *ptr) {
ptr = (void *)((SizeL)ptr - sizeof(SizeL));
return MemAlloc.FreeRange((SizeL)ptr, *(SizeL *)ptr);
}
"""
def vm_load(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
a = vm_inst.get_instr_dat(1)
if a is None:
vm_inst.ip -= 1
return
typ = a & BCR_TYP_MASK
sz = 1 << ((a & BCR_SZ_MASK) >> 5)
if typ == BCR_ABS_A4:
addr = vm_inst.get_instr_dat(4)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.get(sz, addr)
if data is None:
vm_inst.ip -= 6
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 6
elif typ == BCR_ABS_A8:
addr = vm_inst.get_instr_dat(8)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.get(sz, addr)
if data is None:
vm_inst.ip -= 10
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 10
elif typ == BCR_ABS_S4:
addr = vm_inst.pop(4)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.get(sz, addr)
if data is None:
vm_inst.ip -= 2
vm_inst.sp -= 4
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 2
vm_inst.sp -= 4
elif typ == BCR_ABS_S8:
addr = vm_inst.pop(8)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.get(sz, addr)
if data is None:
vm_inst.ip -= 2
vm_inst.sp -= 8
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 2
vm_inst.sp -= 8
elif typ & BCR_R_BP_MASK == BCR_R_BP_VAL: # BCR_R_BP1, BCR_R_BP2, BCR_R_BP4, BCR_R_BP8
n_bytes = 1 << (typ & 0x03)
addr = vm_inst.get_instr_dat(n_bytes, 1)
if addr is None:
vm_inst.ip -= 2
return
addr += vm_inst.bp
data = vm_inst.get(sz, addr)
if data is None:
vm_inst.ip -= 2 + n_bytes
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 2 + n_bytes
elif typ == BCR_ABS_C:
data = vm_inst.get_instr_dat(sz)
if data is None:
vm_inst.ip -= 2
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 2 + sz
elif typ == BCR_REG_BP:
if not vm_inst.push(8, vm_inst.bp):
vm_inst.ip -= 2
elif typ == BCR_EA_R_IP:
off = vm_inst.get_instr_dat(sz, 1)
if off is None:
vm_inst.ip -= 2
return
if not vm_inst.push(8, off + vm_inst.ip):
vm_inst.ip -= 2 + sz
elif typ == BCR_TOS:
data = vm_inst.get(sz, vm_inst.sp)
if data is None:
vm_inst.ip -= 2
return
if not vm_inst.push(sz, data):
vm_inst.ip -= 2
elif typ == BCR_SYSREG:
which = vm_inst.get_instr_dat(1)
if which is None:
vm_inst.ip -= 2
return
if not vm_inst.push(8, vm_inst.sys_regs[which]):
vm_inst.ip -= 3
else:
raise ValueError(
"Unsupported BCR code for BC_LOAD instruction: %u at 0x%X" % (typ, vm_inst.ip))
def vm_store(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
a = vm_inst.get_instr_dat(1)
if a is None:
vm_inst.ip -= 1
return
typ = a & BCR_TYP_MASK
sz = 1 << ((a & BCR_SZ_MASK) >> 5)
if typ == BCR_ABS_A4:
addr = vm_inst.get_instr_dat(4)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.pop(sz)
if data is None:
vm_inst.ip -= 6
return
if not vm_inst.set(sz, addr, data):
vm_inst.ip -= 6
vm_inst.sp -= sz
elif typ == BCR_ABS_A8:
addr = vm_inst.get_instr_dat(8)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.pop(sz)
if data is None:
vm_inst.ip -= 10
return
if not vm_inst.set(sz, addr, data):
vm_inst.ip -= 10
vm_inst.sp -= sz
elif typ == BCR_ABS_S4:
addr = vm_inst.pop(4)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.pop(sz)
if data is None:
vm_inst.ip -= 2
vm_inst.sp -= 4
return
if not vm_inst.set(sz, addr, data):
vm_inst.ip -= 2
vm_inst.sp -= 4 + sz
elif typ == BCR_ABS_S8:
addr = vm_inst.pop(8)
if addr is None:
vm_inst.ip -= 2
return
data = vm_inst.pop(sz)
if data is None:
vm_inst.ip -= 2
vm_inst.sp -= 8
return
if not vm_inst.set(sz, addr, data):
vm_inst.ip -= 2
vm_inst.sp -= 8 + sz
elif typ & BCR_R_BP_MASK == BCR_R_BP_VAL: # BCR_R_BP1, BCR_R_BP2, BCR_R_BP4, BCR_R_BP8
n_bytes = 1 << (typ & 0x03)
addr = vm_inst.get_instr_dat(n_bytes, 1)
if addr is None:
vm_inst.ip -= 2
return
addr += vm_inst.bp
vm_inst.set(sz, addr, vm_inst.pop(sz))
elif typ == BCR_SYSREG:
which = vm_inst.get_instr_dat(1)
reg_v = vm_inst.pop(8)
vm_inst.sys_regs[which] = reg_v
if which == 0xC:
vm_inst.priv_lvl = (reg_v >> 8) & 3
vm_inst.priority = reg_v & 0xFF
elif typ == BCR_ABS_C:
raise ValueError("BCR_ABS_C is unsupported on store instruction at 0x%X" % vm_inst.ip)
elif typ == BCR_REG_BP:
vm_inst.bp = vm_inst.pop(8)
elif typ == BCR_EA_R_IP:
raise ValueError("BCR_EA_R_IP is unsupported on store instruction at 0x%X" % vm_inst.ip)
else:
raise ValueError(
"Unsupported BCR code for BC_STOR instruction: %u at 0x%X" % (typ, vm_inst.ip))
def vm_exit(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
vm_inst.running = 0
def get_vm_sz_type(conv_typ):
if conv_typ >= 0xC:
raise NotImplementedError("Not Implemented")
is_flt = bool(conv_typ & 0x8)
typ = 2 if is_flt else (conv_typ & 0x1)
sz = conv_typ & 0x7
if is_flt:
sz = 2 << sz
else:
sz = 1 << (sz >> 1)
return sz, typ
def vm_conv(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
typ = vm_inst.get_instr_dat(1)
cnv_typ_i = typ & BCC_I_MASK
cnv_typ_o = (typ & BCC_O_MASK) >> 4
sz_i, typ_i = get_vm_sz_type(cnv_typ_i)
sz_o, typ_o = get_vm_sz_type(cnv_typ_o)
a = vm_inst.pop(sz_i, typ_i)
if typ_o == 2:
a = float(a)
else:
a = int(a)
if a < 0:
typ_o = 1
vm_inst.push(sz_o, a, typ_o)
def vm_swap(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
typ = vm_inst.get_instr_dat(1)
sz_cls_a = typ & BCS_SZ_A_MASK
sz_cls_b = (typ & BCS_SZ_B_MASK) >> 3
sz_a = 1 << sz_cls_a
sz_b = 1 << sz_cls_b
b = vm_inst.pop(sz_b)
a = vm_inst.pop(sz_a)
vm_inst.push(sz_b, b)
vm_inst.push(sz_a, a)
def vm_call_ext(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
a = vm_inst.get_instr_dat(1)
if a & 0x80:
sys_num_sz_cls = (a >> 5) & 3
sys_num_sz = (1, 2, 4, 8)[sys_num_sz_cls]
sys_num = vm_inst.pop(sys_num_sz)
vm_inst.syscall(sys_num)
'''sys_num = vm_inst.pop()
if vm_inst.cr4 & 0x3 == vm_inst.virtual_syscalls_lvl:
vm_inst.virt_syscall(sys_num)
else:
num = vm_inst.sys_fn[vm_inst.cr4 & 0x3]
old_regs = [vm_inst.cr4, vm_inst.ip, vm_inst.sp, vm_inst.bp] # TODO
vm_inst.call(num)
raise NotImplementedError("SysCall (CALL_E with IS_SYS=1) is unsupported")
'''
else:
addr = vm_inst.pop(8)
if a & 0x40:
vm_inst.call(addr + vm_inst.ip)
else:
vm_inst.call(addr)
def vm_ret_ext(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
a = vm_inst.get_instr_dat(1)
if a & 0x80:
vm_inst.sysret()
# raise NotImplementedError("SysCall (CALL_E with IS_SYS=1) is unsupported")
else:
sz_cls_rst_sp = (a & 0x60) >> 5
rst_sp = vm_inst.pop(1 << sz_cls_rst_sp)
sz_cls_res_sz = (a & 0x18) >> 3
res_sz = vm_inst.pop(1 << sz_cls_res_sz)
vm_inst.ret(rst_sp, res_sz)
def vm_sys_ret(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
vm_inst.trap(6, vm_inst.ip)
# del vm_inst
# raise NotImplementedError("SysCall (CALL_E with IS_SYS=1) is unsupported")
class InterruptApi(object):
def __init__(self):
import sys
self.sys = sys
self.stdout = sys.stdout
self.stdin = sys.stdin
self.read_buf = ""
def interrupt(self, vm_inst, n):
"""
:param VirtualMachine vm_inst:
:param int n:
"""
if n == 0x29: # StackVM Pow function
cmd = vm_inst.get(1, vm_inst.sp)
if cmd != 0:
raise NotImplementedError("Not Implemented")
assert cmd == 0 # double pow(double base, int exponent);
exponent = vm_inst.get(4, vm_inst.sp + 1)
if exponent & 0x80000000:
exponent -= 0x100000000
base = vm_inst.get_float(8, vm_inst.sp + 5)
vm_inst.set_float(8, vm_inst.sp + 13, base ** exponent)
return
if n != 0x21: # MSDOS "INT 21h"
raise NotImplementedError("Not Implemented")
cmd = vm_inst.get(1, vm_inst.sp)
if cmd == 0x01: # getchar [int getchar()]
vm_inst.set(4, vm_inst.sp + 1, ord(self.getchar()))
elif cmd == 0x02: # putchar [void putchar(int)]
ch = vm_inst.get(4, vm_inst.sp + 1)
ch = chr(ch)
self.putchar(ch)
# skip 0x05, 0x06
elif cmd == 0x07: # TODO: getch (direct) [int getch()]
vm_inst.set(4, vm_inst.sp + 1, ord(self.getchar()))
elif cmd == 0x08: # TODO: getch [int getch()]
vm_inst.set(4, vm_inst.sp + 1, ord(self.getchar()))
elif cmd == 0x09: # puts [void puts(char *str)]
addr = vm_inst.get(8, vm_inst.sp + 1)
byts = bytearray()
ch = vm_inst.get(1, addr)
while ch:
byts.append(ch)
addr += 1
ch = vm_inst.get(1, addr)
byts = bytes(byts)
self.puts(byts)
elif cmd == 0x0A: # gets [UInt64 gets(char *str, UInt64 length)]
addr = vm_inst.get(8, vm_inst.sp + 1)
size = vm_inst.get(8, vm_inst.sp + 9)
length = self.gets_len()
real_len = min(size, length, len(vm_inst.memory) - addr)
vm_inst.memory[addr: addr + real_len] = self.read_buf[:real_len]
self.read_buf = self.read_buf[real_len:]
vm_inst.set(8, vm_inst.sp + 17, real_len)
elif cmd == 0x0B: # get status [UInt64 getStatus()]
vm_inst.set(8, vm_inst.sp + 1, self.gets_len())
def putchar(self, ch):
self.stdout.write(str(ch))
def puts(self, s):
self.stdout.write(s.decode('utf-8'))
def getchar(self):
if len(self.read_buf) == 0:
self.read_buf = self.stdin.readline()
rtn = self.read_buf[0]
self.read_buf = self.read_buf[1:]
return rtn
def gets(self):
if len(self.read_buf):
rtn = self.read_buf
self.read_buf = ""
return rtn
else:
return self.stdin.readline()
def gets_len(self):
length = len(self.read_buf)
if length:
return length
self.read_buf = self.stdin.readline()
return len(self.read_buf)
class AdvProgIntCtl(object):
__slots__ = ["int_ready", "which_int", "arg0", "arg1", "arg2", "arg3"]
def __init__(self):
self.int_ready = False
self.which_int = 0
self.arg0 = 0
self.arg1 = 0
self.arg2 = 0
self.arg3 = 0
def trigger(self, which_int: int, arg0: int=0, arg1: int=0, arg2: int=0, arg3: int=0):
self.int_ready = True
self.which_int = which_int
self.arg0 = arg0
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
def vm_interrupt(vm_inst):
"""
:param VirtualMachine vm_inst:
"""
a = vm_inst.get_instr_dat(1)
# if a > 0x3F: raise NotImplementedError("Not Implemented")
vm_inst.switch_to_interrupt(a)
# vm_inst.api.interrupt(vm_inst, a)
def sign_of(a):
if a > 0:
return 1
elif a < 0:
return -1
else:
return 0
def lshift1(b, a):
return a << b
def rshift1(b, a):
return a >> b
def lrot1(b, a, n):
mask = (1 << n) - 1
b %= n
return ((a << b) | (a >> (n - b))) & mask
def rrot1(b, a, n):
mask = (1 << n) - 1
b %= n
return ((a >> b) | (a << (n - b))) & mask
def and1(b, a):
return a & b
def or1(b, a):
return a | b
def not1(a, n):
return a ^ ((1 << n) - 1)
def xor1(b, a):
return a ^ b
def add1(b, a):
return a + b
def sub1(b, a):
return a - b
def mul1(b, a):
return a * b
def div1(b, a):
return a // b
def fdiv(b, a):
return a / b
def mod1(b, a):
return a % b
def cmp1(b, a):
return sign_of(a - b)
VM_DISABLED = 0
VM_4_LVL_9_BIT = 1
VM_4_LVL_10_BIT = 2
VME_NONE = 0
VME_PAGE_NOT_PRESENT = 1
VME_PAGE_BAD_PERMS = 2
class ObjectIdAllocator(object):
__slots__ = ["free_list", "objects"]
def __init__(self, start: int, end: int):
self.free_list = [(start, end)]
self.objects = {}
def acquire_id(self) -> int:
start, end = self.free_list[0]
if end - start > 1:
self.free_list[0] = (start + 1, end)
else:
self.free_list.pop(0)
return start
def release_id(self, num: int):
last_index = -1
free_list = self.free_list
for c, (start, end) in enumerate(free_list):
if end > num:
if start == num + 1:
free_list[c] = (num, end)
last_index = c
break
elif start <= num:
break
else:
free_list.insert(c, (num, num + 1))
last_index = c
break
elif end == num:
if c + 1 < len(free_list):
last_index = c + 1
free_list[c] = (start, end + 1)
if last_index > 0: