This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.cpp
2174 lines (2108 loc) · 88.8 KB
/
vm.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
// c4.c - C in four functions
// char, int, and pointer types
// if, while, return, for and expression statements
// Written by Robert Swierczek
// + x86 JIT compiler by Dmytro Sirenko
// + win32 port by Joe Bogner
// + port to paq Kaido Orav
#ifdef WINDOWS
#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
#define MAP_FILE 0
#define MAP_SHARED 1
#define MAP_PRIVATE 2
#define MAP_TYPE 0xf
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
#define MAP_FAILED ((void *)-1)
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
#else
#include <sys/mman.h>
#endif
void jitReadonly(void *p, size_t size){
#ifdef WINDOWS
DWORD oldProtect;
VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect);
#else
mprotect(p, size, PROT_READ | PROT_EXEC);
#endif
}
// tokens and classes (operators last and in precedence order)
enum { Num = 128, Fun, Sys, Glo, Loc, Id, Load, Enter,
Char, Else, Enum, If, Int, Short, Return, For, Sizeof, While,
Comma, Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
};
// opcodes
enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LS ,LC ,SI ,SS ,SC ,PSH ,
OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,BOUND,VTHIS,
PRTF,VMS,VMI,VMX,H2,READ,WRTE,EXIT};
// types
enum { rCHAR, sSHORT, iINT, PTR };
// identifier offsets (since we can't create an ident struct)
enum { Tk, Hash, Name, Class, Type, Val, HClass, HType, HVal,IDLen,UBound, Idsz };
enum { VMCOMPRESS=0,VMDETECT,VMENCODE,VMDECODE};
#ifdef VMMSG
#define kprintf printf // prints error messages
//#define dprintf printf // prints x86 asm to console
#define dprintf(...) // prints x86 asm to console
#else
#define kprintf(...)
#define dprintf(...)
#endif
class VM {
private:
Array<char> data1;
char *p, *lp, // current position in source code
*data,*data0, // data/bss pointer,
*jitmem, // executable memory for JIT-compiled native code
*je; // current position in emitted native code
int *e, *le, *text,*codestart, // current position in emitted code
*id, // currently parsed indentifier
*n, // current node in abstract syntax tree
*sym, // symbol table (simple list of identifiers)
*ast,
tk, // current token
ival, // current token value
ty, // current expression type
loc, // local variable offset
line; // current line number
int fd, bt, poolsz, *idmain,*idp,*idupdate;
int *iddetect, *iddecode, *idencode;//functions detect,decode and encode
int *pc, *sp,*sp0, *bp, cycle; // vm registers
int i, *t,*pc0,tmp; // temps
int a;
int initvm( ) ;
char *mod;
public:
int debug; // print executed instructions
BlockData& x;
int vmMode;
Array<char*> mem; //array of allocated memory
Array<int> memSize; // size in bytes of memory in mem[mindex]
Array<int*> membound; //array of allocated memory
Array<int> prSize; // size in bytes of memory in mem[mindex]
int totalPR;
Array<int> mcomp; //component list set in vmi
Array<ContextMap *>cmC;
int smc, apm1, rcm, scm, cm, mx,st,av,ds,mm,dhs,sm,sk,apm2,em,/*tapm,*/uas,lmx,statec,byt;
MixMap1 mmA[256];
Mixer1 mxA[256];
StaticMap stA[256];
DynamicSMap dsA[256];
DynamicHSMap dhsA[256];
APM1 apm1A[256];
StateMapContext smA[256];
AvgMap avA[256];
SmallStationaryContextMap scmA[256];
StationaryMap smcA[256];
RunContextMap rcmA[256];
SkMap skA[256];
APM2 apm2A[256];
ErrMap emA[256];
//TAPM tapmA[256];
UAS uasA[256];
LmxMap lmxA[256];
ByteMap bmA[256];
int totalc; //total number of components
int currentc; //current component, used in vmi
int initdone; //set to 1 after main exits
int mindex; // count fo memory allocations
FILE *inFile, *outFile; // files for decoding and encoding
int inpos;
int plpos;
VMParam *parm;
StateTable vmstate;
U8 vm_nn[256][512*4];
U8 nn01[256][256];
VM(char* m,BlockData& bd,int mode, VMParam *p=0);
~VM() ;
void next();
void expr(int lev);
void stmt();
int dovm(int *ttt);
void gen(int *n);
int dojit();
int detect(int c4,int pos);
int decode(int info,int len);
int encode(int info,int len);
int block(int info1,int info2);
int doupdate1(int y, int c0, int bpos,U32 c4,int pos);
int doupdate2(int y, int c0, int bpos,U32 c4,int pos);
void killvm( );
void decompile();
int getPrediction( );
void updateComponents(int p);
};
// alloc function in interpreted code
// keep track of pointers and sizes in bytes
// no bounds test
char* vmmalloc(VM* v,size_t i,int w){
char*ptr= (char*)calloc(i*w,1);
if (ptr==0) perror("mem error "),printf("%d ",i),quit("VM mem alloc fail");
v->mem.resize(v->mem.size()+1);
v->mindex =v->mem.size();
v->mem[v->mindex-1]=ptr;
v->memSize.resize(v->mindex);
v->memSize[v->mindex-1]=i*w;
return ptr;
}
void VM::killvm( ){
if ( smc>0 ) {
for (int i=0;i<smc;i++) smA[i].Free();
}
if ( apm1>0 ) {
for (int i=0;i<apm1;i++) apm1A[i].Free();
}
if ( apm2>0 ) {
for (int i=0;i<apm2;i++) apm2A[i].Free();
}
/* if ( tapm>0 ) {
for (int i=0;i<tapm;i++) tapmA[i].Free();
}*/
if ( scm>0 ) {
for (int i=0;i<scm;i++) scmA[i].Free();
}
if ( cm>0 ) {
for (int i=0;i<cm;i++) delete cmC[i];
}
if ( mx>0 ) {
for (int i=0;i<mx;i++) mxA[i].Free();
}
if ( ds>0 ) {
for (int i=0;i<ds;i++) dsA[i].Free();
}
if ( dhs>0 ) {
for (int i=0;i<dhs;i++) dhsA[i].Free();
}
if ( sm>0 ) {
for (int i=0;i<sm;i++) smcA[i].Free();
}
if ( rcm>0 ) {
for (int i=0;i<rcm;i++) rcmA[i].Free();
}
if ( uas>0 ) {
for (int i=0;i<uas;i++) uasA[i].Free();
}
// free memory allocated by vmmalloc
if (mindex){
for (int i=0;i<mindex;i++){
free(mem[i]);
//programChecker.free((U64)memSize[i]); // meaningless if MT enabled and thread count 1+
}
}
smc=apm1=apm2=/*tapm=*/rcm=scm=cm=mx=mm=st=av=ds=dhs=sm=sk=currentc=totalc=initdone=mindex=0;
if (sym) free(sym),sym=0;
if (ast) free(ast),ast=0;
if (text) free(text),text=0;
//if (data) free(data),data=0;
if (sp0) free(sp0),sp0=sp=0;
UnmapViewOfFile(jitmem);
}
//vms - set number of components
void components(VM* v,int a,int b,int c,int d,int e,int f,int g,int h,int i,int j,int k,int l,int m,int o,int p,int q,int r,int s,int sg,int bt){
if (v->initdone==1) {kprintf("VM vms error: vms allowed only in main\n ");quit();}
if (v->totalc>0) {kprintf("VM vms error: vms allready called\n ");quit();}
v->smc=a, v->apm1=b,v->ds=c,v->av=d,v->scm=e, v->rcm=f, v->cm=g, v->mx=h,v->st=i,v->mm=j,v->dhs=k,v->sm=l,v->sk=m,v->apm2=o,v->em=p,/*v->tapm=q,*/v->uas=r,v->lmx=s,v->statec=sg,v->byt=bt;
v->totalc= a+b+c+d+e+f+g+h+i+j+k+h+l+m+o+p+q+r+s+sg+bt;
v->mcomp.resize(v->totalc);
if (v->totalc==0 && h>0) quit("No inputs for mixer defined VM\n");
}
//vmi - init components
enum {vmSMC=1,vmAPM1,vmDS,vmAVG,vmSCM,vmRCM,vmCM,vmMX,vmST,vmMM,vmDHS,vmSM,vmSK,vmAPM2,vmERR,vmTAPM1,vmUAS,vmLMX,vmSTA,vmBYT};
const char* cNames[]={
"SMC","APM","DS ","AVG","SCM","RCM","CM ","MX ","ST ","MM ","DHS","SM ","SK ","AP2","ERR","TAP","UAS","LMX","STA","BYT"
};
void printcomponent(int component){
printf("%s",cNames[component-1]);
}
void initcomponent(VM* v,int component,int componentIndex, int f,int d, int indexOfInputs){
//assert(componentIndex>=0); //component index
//assert(d>=0); //component context
int cms3=0,cms4=0,rd=0,cmbias=2;
if (component==vmCM) cms3=(indexOfInputs>>8)&255,cms4=(indexOfInputs>>16)&255,cmbias=(indexOfInputs>>24)&15,rd=(indexOfInputs>>28)&1,indexOfInputs=indexOfInputs&255;
if ( component==vmDS) cms3=(indexOfInputs>>8)&0xffff,indexOfInputs=indexOfInputs&255;
//printcomponent(component); printf(" component %d, componentIndex %d, f %d, d %d, indexOfInputs %d\n",component, componentIndex, f, d, indexOfInputs);
if (v->initdone==1) {kprintf("VM vmi error: vmi allowed only in main\n ");quit();}
if (v->currentc> v->totalc) {kprintf("VM vmi error: component %d not set %d - %d\n ",component,v->currentc, v->totalc);quit();}
if (componentIndex> 255) {kprintf("VM vmi error: componentIndex\n ");quit();}
if (component==vmTAPM1 ) {kprintf("VM vmi error: TAPM is disabled.\n ");quit();}
const int ii=componentIndex+1;
bool isInputs= (component==vmAPM1 || component==vmAPM2 /*|| component==vmTAPM*/ || component==vmUAS||component==vmDS||component==vmERR||component==vmBYT||component==vmSTA|| component==vmDHS || component==vmAVG || (component==vmST && indexOfInputs==-1)||(component==vmSK && indexOfInputs==-1)||(component==vmSMC && indexOfInputs==-1));
if (indexOfInputs>=0 && v->x.cInputs <indexOfInputs && isInputs==false){// input sets for mixers
v->x.cInputs++;
v->x.mxInputs.resize(v->x.mxInputs.size()+1);
if(v->x.mxInputs.size()<=indexOfInputs){
v->x.mxInputs.resize(indexOfInputs+1);
v->x.cInputs=indexOfInputs;
}
}
switch (component) {
case vmSMC: {
if ( indexOfInputs==-1)v->totalPR++;
if (ii>v->smc ) {
kprintf("VM vmi error: smc(%d) defined %d, max %d\n",component,ii, v->smc);
quit();
}
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount++;
break;
}
case vmAPM1:{
v->totalPR++;
if (ii>v->apm1) {
kprintf("VM vmi error: apm1(%d) defined %d, max %d\n",component,ii, v->apm1);
quit();
}
break;
}
case vmAPM2:{v->totalPR++; if (ii>v->apm2) {kprintf("VM vmi error: apm2(%d) defined %d, max %d\n",component,ii, v->apm2);quit();}
break; }
// case vmTAPM:{v->totalPR++; if (ii>v->tapm) {kprintf("VM vmi error: tapm(%d) defined %d, max %d\n",component,ii, v->tapm);quit();}
// break; }
case vmUAS:{v->totalPR++; if (ii>v->uas) {kprintf("VM vmi error: uas(%d) defined %d, max %d\n",component,ii, v->uas);quit();}
break; }
case vmDS:{v->totalc=v->totalc+indexOfInputs-1;v->mcomp.resize(v->mcomp.size()+indexOfInputs); v->totalPR+=indexOfInputs; if (ii>v->ds) {kprintf("VM vmi error: ds(%d) defined %d, max %d\n",component,ii, v->ds);quit();}
if (f<1) {kprintf("VM vmi error:ds(%d) memory bits must be larger then 0.",ii);quit();}
break; }
case vmDHS:{v->totalc=v->totalc+indexOfInputs-1;v->mcomp.resize(v->mcomp.size()+indexOfInputs); v->totalPR+=indexOfInputs; if (ii>v->dhs) {kprintf("VM vmi error: dhs(%d) defined %d, max %d\n",component,ii, v->dhs);quit();}
if (f<1) {kprintf("VM vmi error:dhs(%d) memory bits must be larger then 0.",ii);quit();}
break; }
case vmRCM: { if (ii>v->rcm ) {kprintf("VM vmi error: rcm(%d) defined %d, max %d\n",component,ii, v->rcm);quit(); }
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount++;
break; }
case vmSCM: { if (ii>v->scm ) {kprintf("VM vmi error: scm(%d) defined %d, max %d\n",component,ii, v->scm);quit(); }
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount+=2;
break; }
case vmAVG:{ v->totalPR++; if (ii>v->av ) {kprintf("VM vmi error: AVG(%d) defined %d, max %d\n",component,ii, v->av);quit();}
break; }
case vmLMX:{ v->totalPR++; if (ii>v->lmx ) {kprintf("VM vmi error: LMX(%d) defined %d, max %d\n",component,ii, v->lmx);quit();}
break; }
case vmCM: {if (ii>v->cm ) {kprintf("VM vmi error: cm(%d) defined %d, max %d\n",component,ii, v->cm);quit();}
v->cmC.resize(v->cmC.size()+1);
break; }
case vmMX: {v->totalPR++;
break; }
case vmST: {if ( indexOfInputs==-1)v->totalPR++; if (ii>v->st ) {kprintf("VM vmi error: st(%d) defined %d, max %d\n",component,ii, v->st);quit();}
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount++;
break; }
case vmMM: { v->x.mxInputs[indexOfInputs].ncount++;
break; }
case vmSM: { if (ii>v->sm ) {kprintf("VM vmi error: sm(%d) defined %d, max %d\n",component,ii, v->sm);quit(); }
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount+=2;
break; }
case vmSK: {if ( indexOfInputs==-1)v->totalPR++; if (ii>v->sk ) {kprintf("VM vmi error: sk(%d) defined %d, max %d\n",component,ii, v->sk);quit(); }
if ( indexOfInputs>=0) v->x.mxInputs[indexOfInputs].ncount+=1;
break; }
case vmERR: { break; }
case vmSTA: { break; }
case vmBYT: { break; }
default: quit("VM vmi error\n");
}
int prindex=0;
if (component==vmAPM1 || component==vmAPM2|| /*component==vmTAPM ||*/ component==vmUAS || component==vmDS|| component==vmDHS || component==vmAVG || component==vmLMX|| (component==vmST && indexOfInputs==-1)||(component==vmSK && indexOfInputs==-1)||(component==vmSMC && indexOfInputs==-1) || component==vmMX)prindex=v->totalPR;
// If Autotune then ignore model parameters, first run is allways with model parameters.
switch (component) {
case vmSMC:{
int smc_l=d; // limit
if (v->parm){
if (v->parm->vm_smc[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_smc_limit[componentIndex]=smc_l;
smc_l=v->parm->vm_smc_limit[componentIndex];
}
}
U8 *n=&v->vm_nn[0][0];
v->smA[componentIndex].Init(f, smc_l,n);
break;
}
case vmAPM1: {
int apm_l=d; // limit
if (v->parm){
if (v->parm->vm_apm[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_apm_limit[componentIndex]=apm_l;
apm_l=v->parm->vm_apm_limit[componentIndex];
}
}
v->apm1A[componentIndex].Init(f,apm_l,indexOfInputs);
break;
}
case vmAPM2: {
int apm_l=d>>8; // limit
int apm_s=d&255;
if (apm_l==0) apm_l=0;
if (apm_s==0) apm_s=24;
if (v->parm){
if (v->parm->vm_apm2[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_apm2_limit[componentIndex]=apm_l;
apm_l=v->parm->vm_apm2_limit[componentIndex];
if (v->parm->isactive==true) v->parm->vm_apm2_step[componentIndex]=apm_s;
apm_s=v->parm->vm_apm2_step[componentIndex];
}
}
v->apm2A[componentIndex].Init(f,(apm_l<<8)+apm_s,indexOfInputs);
break;
}
/*case vmTAPM: {
int apm_l0=f&0xffff; // limit
int apm_l1=f>>16;
int apm_l2=d&0xfff;
int apm_l3=(d>>12)&0xfff;
int apm_l4=indexOfInputs&0xffff;
int apm_w1=(indexOfInputs>>16)&0xff;//8
int apm_w2=(indexOfInputs>>24)&0xff;//21
int apm_wb1=(U32(d)>>24);
// If Autotune then ignore model parameters, first run is allways with model parameters.
if (v->parm){
if (v->parm->vm_tapm[componentIndex]){
if (v->parm->isactive==true) {
v->parm->vm_tapm_limit0[componentIndex]=apm_l0;
v->parm->vm_tapm_limit1[componentIndex]=apm_l1;
v->parm->vm_tapm_limit2[componentIndex]=apm_l2;
v->parm->vm_tapm_limit3[componentIndex]=apm_l3;
v->parm->vm_tapm_limit4[componentIndex]=apm_l4;
v->parm->vm_tapm_limitw1[componentIndex]=apm_w1;
v->parm->vm_tapm_limitw2[componentIndex]=apm_w2;
v->parm->vm_tapm_limitwb1[componentIndex]=apm_wb1;
}
apm_l0=v->parm->vm_tapm_limit0[componentIndex];
apm_l1=v->parm->vm_tapm_limit1[componentIndex];
apm_l2=v->parm->vm_tapm_limit2[componentIndex];
apm_l3=v->parm->vm_tapm_limit3[componentIndex];
apm_l4=v->parm->vm_tapm_limit4[componentIndex];
apm_w1=v->parm->vm_tapm_limitw1[componentIndex];
apm_w2=v->parm->vm_tapm_limitw2[componentIndex];
apm_wb1=v->parm->vm_tapm_limitwb1[componentIndex];
}
}
v->tapmA[componentIndex].Init(apm_l0,apm_l1,apm_l2,apm_l3,apm_l4,apm_w1,apm_w2,apm_wb1);
break;
}*/
case vmSTA: {
int sta_l0=f&0xffff; // limit
int sta_l1=f>>16;
int sta_l2=d&0xffff;
int sta_l3=(d>>16);
int sta_l4=indexOfInputs&0xffff;
int sta_l5=(indexOfInputs>>16)&255;
int sta_l6=(indexOfInputs>>24)&255;
int isSta=sta_l0|sta_l1|sta_l2|sta_l3|sta_l4|sta_l5|sta_l6;
if (isSta==0)sta_l0=42,sta_l1=41,sta_l2=13,sta_l3=6,sta_l4=5,sta_l5=41,sta_l6=64; // These parameters are bad
if (v->parm){
if (v->parm->vm_nnst[componentIndex]){
if (v->parm->isactive==true) {
v->parm->vm_nnst_limit0[componentIndex]=sta_l0;
v->parm->vm_nnst_limit1[componentIndex]=sta_l1;
v->parm->vm_nnst_limit2[componentIndex]=sta_l2;
v->parm->vm_nnst_limit3[componentIndex]=sta_l3;
v->parm->vm_nnst_limit4[componentIndex]=sta_l4;
v->parm->vm_nnst_limit5[componentIndex]=sta_l5;
v->parm->vm_nnst_limit6[componentIndex]=sta_l6;
}
sta_l0=v->parm->vm_nnst_limit0[componentIndex];
sta_l1=v->parm->vm_nnst_limit1[componentIndex];
sta_l2=v->parm->vm_nnst_limit2[componentIndex];
sta_l3=v->parm->vm_nnst_limit3[componentIndex];
sta_l4=v->parm->vm_nnst_limit4[componentIndex];
sta_l5=v->parm->vm_nnst_limit5[componentIndex];
sta_l6=v->parm->vm_nnst_limit6[componentIndex];
}
}
//if (componentIndex>0)
v->vmstate.Init(sta_l0,sta_l1,sta_l2,sta_l3,sta_l4,sta_l5,sta_l6);
// states
for (int i=0;i<256;i++) v->vm_nn[componentIndex+1][i]=v->vmstate.next(i,0);
for (int i=256;i<512;i++) v->vm_nn[componentIndex+1][i]=v->vmstate.next(i-256,1);
// counts
for (int i=512;i<512+256;i++) v->vm_nn[componentIndex+1][i]=v->vmstate.next(i-512,2);
for (int i=512+256;i<512+512;i++) v->vm_nn[componentIndex+1][i]=v->vmstate.next(i-256-512,3);
//only n0 or n1
for (int i=0;i<256;i++) {
int n0=-!v->vmstate.next(i,2);
int n1=-!v->vmstate.next(i,3);
int r=0;
if ((n1-n0)==1) r=2;
if ((n1-n0)==-1) r=1;
v->nn01[componentIndex+1][i]=r;
}
break;
}
case vmUAS: {
int bits=f;
int mask=d;
bool domask=false;
int rate=indexOfInputs;
if (rate==0) rate=5;
if (v->parm){
if (v->parm->vm_uas[componentIndex]){
if (v->parm->isactive==true) {
v->parm->vm_uas_bits[componentIndex]=bits;
}
bits=v->parm->vm_uas_bits[componentIndex];
}
if (v->parm->vm_uasm[componentIndex]){
if (v->parm->isactive==true) {
v->parm->vm_uas_mask[componentIndex]=(1<<bits)-1;
}
mask=v->parm->vm_uas_mask[componentIndex];
domask=true;
}
if (v->parm->vm_uasr[componentIndex]){
if (v->parm->isactive==true) {
v->parm->vm_uas_rate[componentIndex]=rate;
}
rate=v->parm->vm_uas_rate[componentIndex];
}
}
vm_uas_mask_max[componentIndex]=(1<<bits)-1;//set max mask
v->uasA[componentIndex].Init(bits,mask,domask,rate);
break;
}
case vmDS: {
int ds_l=U32(d)&0xffff;
int ds_l1=U32(d)>>16;
int stable=f>>16;
if (v->parm){
if (v->parm->vm_ds[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_ds_limit[componentIndex]=ds_l;
ds_l=v->parm->vm_ds_limit[componentIndex];
//printf("%d\n",ds_p0);
}
}
U8 *n=&v->vm_nn[stable][0];
v->dsA[componentIndex].Init(f&255,ds_l,indexOfInputs,n);
break; }
case vmDHS: {
int stable=f>>16;
U8 *n=&v->vm_nn[stable][0];
v->dhsA[componentIndex].Init(f&255,d,indexOfInputs,n);
break; }
case vmRCM: {
int rcm_ml=d&255; // limit
if (rcm_ml==0) rcm_ml=8;
if (v->parm){
if (v->parm->vm_rcm[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_rcm_limit[componentIndex]=rcm_ml;
rcm_ml=v->parm->vm_rcm_limit[componentIndex];
}
}
v->rcmA[componentIndex].Init(f<=0?4096:f*4096,rcm_ml);
break;}
case vmSCM: v->scmA[componentIndex].Init(f);
break;
case vmAVG:{
int avg_l0=f&255; // limit
int avg_l1=(f>>8)&255;
if (avg_l0==0) avg_l0=1;
if (avg_l1==0) avg_l1=1;
if (v->parm){
if (v->parm->vm_avg[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_avg_limit0[componentIndex]=avg_l0;
if (v->parm->isactive==true) v->parm->vm_avg_limit1[componentIndex]=avg_l1;
avg_l0=v->parm->vm_avg_limit0[componentIndex];
avg_l1=v->parm->vm_avg_limit1[componentIndex];
if (avg_l0==avg_l1 && avg_l0>1) avg_l0++,v->parm->vm_avg_limit0[componentIndex]=avg_l0;
U32 f0;
if (ispowerof2((avg_l0+avg_l1))) {
}
else {
f0= nextPOTwo(avg_l0+avg_l1) ;
avg_l1= f0 -avg_l0;
}
}
}
v->avA[componentIndex].Init(indexOfInputs&0xff,(indexOfInputs>>8)&255,f,d);
break;
}
case vmLMX:{
int lmx_l0=f&255; // limit
int lmx_l1=(f>>8)&255;
int w=d;
if (w==0) w=2048;
if (v->parm){
if (v->parm->vm_lmx[componentIndex] ){
if (v->parm->isactive==true) v->parm->vm_lmx_w[componentIndex]=w;
w=v->parm->vm_lmx_w[componentIndex];
}
}
v->lmxA[componentIndex].Init(lmx_l0,lmx_l1,w);
break;
}
case vmERR:{
int e_h=(U32(f)>>16);
if (e_h==0) e_h=4095;
int e_l=U32(f)&0xffff;
if (e_l==0) e_l=2047;
if (v->parm){
if (v->parm->vm_err[componentIndex]){
if (v->parm->isactive==true ) v->parm->vm_err_limit[componentIndex]=e_l;
e_l=v->parm->vm_err_limit[componentIndex];
}
if (v->parm->vm_err1[componentIndex]){
if (v->parm->isactive==true ) v->parm->vm_err1_limit[componentIndex]=e_h;
e_h=v->parm->vm_err1_limit[componentIndex];
}
}
v->emA[componentIndex].Init(e_l,e_h);
break;
}
case vmBYT:{
int b_v=U32(f)&0xff;
int b_l=U32(d)&0xff;
if (v->parm ){
if (v->parm->isactive==true ){
v->parm->vm_byt_limit[componentIndex]=b_v;
v->parm->vm_byt_limit_max[componentIndex]=b_l;
}
b_v=v->parm->vm_byt_limit[componentIndex];
//printf("%d, ",b_v);
}
v->bmA[componentIndex].Init(b_v);
break;
}
case vmCM:{
if (componentIndex>=v->cmC.size()) printf("CM index %d to large. Max %d\n",componentIndex,v->cmC.size32()),quit();
int cm_l=(U32(d)>>8)&255; // limit
if (cm_l==0) cm_l=4;
int cms_l=(U32(d)>>16)&255; // sm rate
if (cms_l==0) cms_l=32;
int cms2_l=(U32(d)>>24)&255; // sm2 rate
if (cms2_l==0) cms2_l=12;
if (cms3==0) cms3=32;
if (cms4==0) cms4=12;
if (cmbias<2) cmbias=2;
int mem=U32(f)&0xffffff;
int stindex=U32(f)>>24;
if (v->parm){
if (v->parm->vm_cm[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_cm_limit[componentIndex]=cm_l;
cm_l=v->parm->vm_cm_limit[componentIndex];
}
if (v->parm->vm_cms[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_cms_limit[componentIndex]=cms_l;
cms_l=v->parm->vm_cms_limit[componentIndex];
}
if (v->parm->vm_cms2[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_cms2_limit[componentIndex]=cms2_l;
cms2_l=v->parm->vm_cms2_limit[componentIndex];
}
if (v->parm->vm_cms3[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_cms3_limit[componentIndex]=cms3;
cms3=v->parm->vm_cms3_limit[componentIndex];
}if (v->parm->vm_cms4[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_cms4_limit[componentIndex]=cms4;
cms4=v->parm->vm_cms4_limit[componentIndex];
}
//printf("%d,%d,%d,%d\n",cm_l,cms_l,cms2_l,cms3,cms4);
}
U8 *n=&v->vm_nn[stindex][0];U8 *n1=&v->nn01[stindex][0];
v->cmC[componentIndex] = (ContextMap*)new ContextMap(mem<=0?4096:mem*4096,(d&255)|(cm_l<<8)|(cms_l<<16)|(cms2_l<<24),v->x,cms3,n,n1,cms4,rd);
if ( (indexOfInputs)>=0) v->x.mxInputs[(indexOfInputs)].ncount+=v->cmC[componentIndex]->inputs *(d&255);
break;
}
case vmMX: {
// read model info
int mx_err=(f>>8)&0xffff; // err
int mx_sh=f&255; // shift
if (mx_sh==0)mx_sh=64;
int mx_ue=f>>24;
if (mx_ue==0)mx_ue=28;
if (v->parm ){
if (v->parm->vm_mixer[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_mixer_limit[componentIndex]=mx_err;
mx_err=v->parm->vm_mixer_limit[componentIndex];
}
if (v->parm->vm_mixer_ml[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_mixer_limit_ml[componentIndex]=mx_sh;
mx_sh=v->parm->vm_mixer_limit_ml[componentIndex];
}
if (v->parm->vm_mixer_ue[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_mixer_limit_ue[componentIndex]=mx_ue;
mx_ue=v->parm->vm_mixer_limit_ue[componentIndex];
}
}
v->mxA[componentIndex].Init(d,mx_sh,mx_err,mx_ue); //context,shift,err
break;
}
case vmST: v->stA[componentIndex].Init(f,indexOfInputs);
break;
case vmMM: v->mmA[componentIndex].Init(d,f);
break;
case vmSM:{
int sm_l=(d>>8)&255; // limit
int sm_b=d&255;
if (sm_l==0)sm_l=8<<1;
if (v->parm){
if (v->parm->vm_sm[componentIndex]){
if (v->parm->isactive==true) v->parm->vm_sm_limit[componentIndex]=sm_l;
sm_l=v->parm->vm_sm_limit[componentIndex];
}
}
v->smcA[componentIndex].Init(f,sm_b,sm_l);
break;}
case vmSK:{
v->skA[componentIndex].Init();
break;}
default:
quit("VM vmi error\n");
break;
}
int m=indexOfInputs;
if (component==vmAVG || component==vmLMX ||component==vmAPM1|| component==vmAPM2|| /*component==vmTAPM||*/ component==vmERR|| component==vmBYT|| component==vmSTA|| component==vmUAS) m=0;
if (indexOfInputs==-1) m=0;
if (component==vmDS || component==vmDHS ) {m=0;
for (int j=0;j< indexOfInputs;j++) v->mcomp[v->currentc++] =m+((prindex-indexOfInputs+j+1)<<24)+(componentIndex<<16)+(component<<8);
}else{
v->mcomp[v->currentc++] =m+(prindex<<24)+(componentIndex<<16)+(component<<8); // 0x00iiccmm index,component, inputs
}
if (doDebugInfo==true){
int pri=v->mcomp[v->currentc-1]>>24&0xff;
printf("0x%08x ", v->mcomp[v->currentc-1]);
if (pri==0) printf(" (");else printf(" pr[%d](",pri-1);
printcomponent(v->mcomp[v->currentc-1]>>8&0xff);
printf("[%d]) input[%d] ",v->mcomp[v->currentc-1]>>16&0xff,v->mcomp[v->currentc-1]&0xff);
//if (v->mcomp[v->currentc-1]&0xff=vmMM) printf("pr[%d] ",v->mcomp[v->currentc-1]>>16&0xff);
printf("\n" );
}
//when no mixer is present and some component uses mixer, crash
}
void initcomponent1(const int indexOfInputs, const int d, const int f,const int componentIndex, const int component, VM* v){
initcomponent( v, component, componentIndex, f, d, indexOfInputs);
}
//set context to component
int setcomponent(VM* v, int c, int i, U32 f){
int a=0;
switch (c) {
case vmSMC: {
v->smA[i].set(f,v->x.y);
break;}
case vmAPM1:{
v->apm1A[i].cxt=(f);
break;}
case vmAPM2:{
v->apm2A[i].cx=(f);
break;}
/*case vmTAPM:{
v->tapmA[i].set(f, v->x.y);
break;}*/
case vmDS:{
v->dsA[i].set(f,v->x.y);
break;}
case vmDHS:{
a=v->dhsA[i].set(f,v->x.y);
break;}
case vmRCM:{
v->rcmA[i].set(f,v->x.c4);
break;}
case vmSCM:{
v->scmA[i].set(f);
break;}
case vmCM:{
v->cmC[i]->set(f);
break;}
case vmUAS:{
v->uasA[i].set(f);
break;}
case vmMX:{
v->mxA[i].cxt=f;
a=v->mxA[i].err>>3;
break;}
case vmST:{
v->stA[i].set(f);
break;}
case vmMM: {
break;}
case vmSM:{
v->smcA[i].set(f);
break;}
case vmSK:{
v->skA[i].set(f);
break;}
case vmERR:{
v->emA[i].cx=f;
a=v->emA[i].q();
break;}
case vmBYT:{
a=v->bmA[i].q();
break;}
case vmAVG:{
v->avA[i].set(f);
break;}
case vmLMX:{
break;}
case vmSTA:{
break;}
default:{
quit("VM vmx error\n");
break;}
}
return a;
}
int setcomponent1( U32 f, int i, int c, VM* v ) {return setcomponent( v, c, i, f);
}
// i size
// pos -2 - Seek to pos
// 0 -2 - Seek to end
// 0 -1 - Seek to start
int readfile(VM* v,U8 *i,int size){
assert(size>-3);
assert(v->inFile!=NULL);
if (size>0)return fread (i,1,size,v->inFile);
if (size==-2)fseek (v->inFile , 0 , SEEK_END);
else if (size==-1) fseek(v->inFile, v->inpos, SEEK_SET); // set to block start pos not file start pos
else return -1;
return 0;
}
int readfile1(int size, U8 *i,VM* v){return readfile(v,i, size);}
int writefile(VM* v,U8 *i,int size){
assert(size>-3);
assert(v->outFile!=NULL);
if (size==-2)fseek (v->outFile , 0 , SEEK_END);
else if (size==-1) fseek(v->outFile, 0, SEEK_SET);
else return fwrite (i , 1, size, v->outFile);
return -1;
}
int writefile1(int size, U8 *i,VM* v){return writefile(v,i, size);}
VM::VM(char* m,BlockData& bd,int mode, VMParam *p):data1(2024*1024),x(bd),vmMode(mode),mem(0),memSize(0),membound(0),prSize(0),mcomp(0),cmC(0),parm(p),
vmstate(42,41,13,6,5,41,63) //statable 42,41,13,6,5,16,14
{
data=&data1[0];
mod=m;
smc=apm1=apm2=/*tapm=*/uas=rcm=scm=cm=mx=st=av=mm=ds=dhs=sm=sk=statec=byt=currentc=totalc=initdone=mindex=totalPR=em=plpos=0;
debug=0;
if (mode==VMCOMPRESS){
// init statetable
/*for (int i=0;i<256;i++) vm_nn[0][i]=vmstate.next(i,0);
for (int i=256;i<512;i++) vm_nn[0][i]=vmstate.next(i-256,1);
for (int i=512;i<512+256;i++) vm_nn[0][i]=vmstate.next(i-512,2);
for (int i=512+256;i<512+512;i++) vm_nn[0][i]=vmstate.next(i-256-512,3);
for (int i=0;i<256;i++) {
int n0=-!vmstate.next(i,2);
int n1=-!vmstate.next(i,3);
int r=0;
if ((n1-n0)==1 ) r=2;
if ((n1-n0)==-1 ) r=1;
nn01[0][i]=r;
}*/
// Use original state table if none defined
for (int i=0;i<256;i++) vm_nn[0][i]=nex(i,0);
for (int i=256;i<512;i++) vm_nn[0][i]=nex(i-256,1);
for (int i=512;i<512+256;i++) vm_nn[0][i]=nex(i-512,2);
for (int i=512+256;i<512+512;i++) vm_nn[0][i]=nex(i-256-512,3);
for (int i=0;i<256;i++) {
int n0=-!nex(i,2);
int n1=-!nex(i,3);
int r=0;
if ((n1-n0)==1 ) r=2;
if ((n1-n0)==-1 ) r=1;
nn01[0][i]=r;
}
}
//load cfg file, if error then exit
if (initvm()==-1) {
exit(1);
}
initdone=1;
totalc=currentc; //update total count to current count
prSize.resize(totalPR--);
int maxMX=-1;
int maxAPM=-1;
int maxSMC=-1;
int maxDS=-1;
int maxCM=-1;
int maxSM=-1;
int maxCMS=-1;
int maxRCM1=-1;
int maxAPM2=-1;
int maxAVG=-1;
int maxERR=-1;
//int maxTAPM=-1;
int maxUAS=-1;
int maxLMX=-1;
int maxSTA=-1;
int maxBYT=-1;
// if mixer is used parse all input arrays
if(x.cInputs>=0 && x.cInputs<256&& idupdate[Val]){
// init input arrays
for (int j=0;j<x.cInputs+1;j++) {
x.mxInputs[j].ncount=(x.mxInputs[j].ncount+15)&-16;
x.mxInputs[j].n.resize(x.mxInputs[j].ncount );
// resize inputs[x] to correct size
}
// provide inputs array info to mixers
for (int i=0;i<totalc;i++){
int prindex=mcomp[i]>>24;
int compnr=(mcomp[i]>>8)&0xff;
// individual components
if (prindex>0 && compnr==vmMX){
int index=(mcomp[i]>>16)&0xff;
int input=mcomp[i]&0xff;
// set input
mxA[index].setTxWx(x.mxInputs[input].n.size(),&x.mxInputs[input].n[0]);
maxMX=index+1;
}
}
}
for (int i=0;i<totalc;i++){
int compnr=(mcomp[i]>>8)&0xff;
int index=((mcomp[i]>>16)&0xff)+1;
if (compnr==vmAPM1){ maxAPM=index; }
else if (compnr==vmAPM2){ maxAPM2=index; }
// else if (compnr==vmTAPM){ maxTAPM=index; }
else if (compnr==vmSMC){ maxSMC=index; }
else if (compnr==vmDS){ maxDS=index; }
else if (compnr==vmCM){ maxCM=maxCMS=index; }
else if (compnr==vmSM){ maxSM=index; }
//else if (compnr==vmCM){ maxCMS=index; }
else if (compnr==vmRCM){ maxRCM1=index; }
else if (compnr==vmAVG){ maxAVG=index; }
else if (compnr==vmLMX){ maxLMX=index; }
else if (compnr==vmERR){ maxERR=index; }
else if (compnr==vmUAS){ maxUAS=index; }
else if (compnr==vmSTA){ maxSTA=index; }
else if (compnr==vmBYT){ maxBYT=index; }
}
// Disable component parameters not in use
if (parm){
for (int i=maxMX;i<256;i++){
parm->vm_mixer[i]=false;
parm->vm_mixer_ml[i]=false;
parm->vm_mixer_ue[i]=false;
}
for (int i=maxAPM;i<256;i++){
parm->vm_apm[i]=false;
}
for (int i=maxAPM2;i<256;i++){
parm->vm_apm2[i]=false;
}
/*for (int i=maxTAPM;i<256;i++){
parm->vm_tapm[i]=false;
}*/
for (int i=maxSMC;i<256;i++){
parm->vm_smc[i]=false;
}
for (int i=maxDS;i<256;i++){
parm->vm_ds[i]=false;
}
for (int i=maxCM;i<256;i++){
parm->vm_cm[i]=false;
}
for (int i=maxSM;i<256;i++){
parm->vm_sm[i]=false;
}
for (int i=maxCMS;i<256;i++){
parm->vm_cms[i]=false;
parm->vm_cms2[i]=false;
parm->vm_cms3[i]=false;
parm->vm_cms4[i]=false;
}
for (int i=maxRCM1;i<256;i++){
parm->vm_rcm[i]=false;
}
for (int i=maxAVG;i<256;i++){
parm->vm_avg[i]=false;
}
for (int i=maxLMX;i<256;i++){
parm->vm_lmx[i]=false;
}
for (int i=maxERR;i<256;i++){
parm->vm_err[i]=false;
parm->vm_err1[i]=false;
}
for (int i=maxUAS;i<256;i++){
parm->vm_uas[i]=false;
parm->vm_uasm[i]=false;
parm->vm_uasr[i]=false;
}
for (int i=maxSTA;i<256;i++){
parm->vm_nnst[i]=false;
}
for (int i=maxBYT;i<256;i++){
parm->vm_byt[i]=false;
}
}
//kprintf("\n");
}
VM::~VM() {killvm();
}
void VM::next(){
char *pp;
int nu;
while (tk = *p) {
++p;
if (tk == '\n') {
++line;
}
else if (tk == '#') {
while (*p != 0 && *p != '\n') ++p;
}
else if ((tk >= 'a' && tk <= 'z') || (tk >= 'A' && tk <= 'Z') || tk == '_') {
pp = p - 1;
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_')
tk = tk * 147 + *p++;
tk = (tk << 6) + (p - pp);
id = sym;
while (id[Tk]) {
if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
id = id + Idsz;
}
id[Name] = (int)pp;
id[Hash] = tk;
id[IDLen] = p - pp;
tk = id[Tk] = Id;
return;
}
else if (tk == '0' && *(p)== 'x') { //Hexadecimal numbers
p++;
for (ival = 0; '\0' != (nu = *p); p++) {
if ( nu >= 'a' && nu <= 'f') {
nu = nu - 'a' + 10;
} else if (nu >= 'A' && nu <= 'F') {
nu = nu - 'A' + 10;
} else if (nu >= '0' && nu <= '9') {
nu = nu - '0';
} else {
tk = Num;
return;
}
ival = ival<<4;
ival =ival + nu;
}
}
else if (tk >= '0' && tk <= '9') { //numbers
ival = tk - '0';
while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0';
tk = Num;
return;
}
else if (tk == '/') { //comment