-
Notifications
You must be signed in to change notification settings - Fork 0
/
r3-ant.cpp
1203 lines (1077 loc) · 32.2 KB
/
r3-ant.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
////////////////////////////////////////////////////////////
// r3 concatenative programing language - Pablo Hugo Reda
//
// Compiler to dword-code and virtual machine for r3 lang,
// with cell size of 64 bits,
//
//#define LINUX
//#define RPI // Tested on a Raspberry PI 4
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#if defined(LINUX) || defined(RPI)
#include <dlfcn.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/types.h>
#else // WINDOWS
#include <windows.h>
#endif
//----------------------
/*------COMPILER------*/
//----------------------
// 0-imm 1-code 2-data 3-reserve 4-bytes 5-qwords
int modo=0;
char *cerror=0;
char *werror;
int boot=-1;
int memcsize=0;
int memc=0;
int *memcode;
int memdsize=0xA00000; // 10MB data
int memd=0;
char *memdata;
char path[1024];
//---- includes
struct Include { char *nombre;char *str; };
int cntincludes;
Include includes[128];
int cntstacki;
int stacki[128];
//---- local dicctionary
struct Indice { char *nombre;int mem;int info; };
int cntdicc;
int dicclocal;
Indice dicc[8192];
//----- aux stack for compilation
int level;
int cntstacka;
int stacka[256];
int lastblock;
void iniA(void) { cntstacka=0; }
void pushA(int n) { stacka[cntstacka++]=n; }
int popA(void) { return stacka[--cntstacka]; }
//----- internal tokens, replace 8 first names
const char *r3asm[]={
";","LIT1","ADR","CALL","VAR"
};
//------ base dictionary, machine-forth or machine-r3
const char *r3bas[]={
";","(",")","[","]",
"EX",
"0?","1?","+?","-?",
"<?",">?","=?",">=?","<=?","<>?","AND?","NAND?","BT?",
"DUP","DROP","OVER","PICK2","PICK3","PICK4","SWAP","NIP",
"ROT","2DUP","2DROP","3DROP","4DROP","2OVER","2SWAP",
">R","R>","R@",
"AND","OR","XOR",
"+","-","*","/",
"<<",">>",">>>",
"MOD","/MOD","*/","*>>","<</",
"NOT","NEG","ABS","SQRT","CLZ",
"@","C@","W@","D@",
"@+","C@+","W@+","D@+",
"!","C!","W!","D!",
"!+","C!+","W!+","D!+",
"+!","C+!","W+!","D+!",
">A","A>","A+",
"A@","A!","A@+","A!+",
"CA@","CA!","CA@+","CA!+",
"DA@","DA!","DA@+","DA!+",
">B","B>","B+",
"B@","B!","B@+","B!+",
"CB@","CB!","CB@+","CB!+",
"DB@","DB!","DB@+","DB!+",
"MOVE","MOVE>","FILL",
"CMOVE","CMOVE>","CFILL",
"DMOVE","DMOVE>","DFILL",
"MEM",
"LOADLIB","GETPROC",
"SYS0","SYS1","SYS2","SYS3","SYS4","SYS5",
"SYS6","SYS7","SYS8","SYS9","SYS10",
//"SYS6F1",
//".",".S",
"",// !!cut the dicc!!!
"JMP","JMPR","LIT2","LIT3", // internal only
"AND_L","OR_L","XOR_L", // OPTIMIZATION WORDS
"+_L","-_L","*_L","/_L",
"<<_L",">>_L",">>>_L",
"MOD_L","/MOD_L","*/_L","*>>_L","<</_L",
"<?_L",">?_L","=?_L",">=?_L","<=?_L","<>?_L","AN?_L","NA?_L",
};
//------ enumaration for table jump
enum {
FIN,LIT,ADR,CALL,VAR,
EX,
ZIF,UIF,PIF,NIF,
IFL,IFG,IFE,IFGE,IFLE,IFNE,IFAND,IFNAND,IFBT,
DUP,DROP,OVER,PICK2,PICK3,PICK4,SWAP,NIP,
ROT,DUP2,DROP2,DROP3,DROP4,OVER2,SWAP2,
TOR,RFROM,ERRE,
AND,OR,XOR,
ADD,SUB,MUL,DIV,
SHL,SHR,SHR0,
MOD,DIVMOD,MULDIV,MULSHR,CDIVSH,
NOT,NEG,ABS,CSQRT,CLZ,
FECH,CFECH,WFECH,DFECH,
FECHPLUS,CFECHPLUS,WFECHPLUS,DFECHPLUS,
STOR,CSTOR,WSTOR,DSTOR,
STOREPLUS,CSTOREPLUS,WSTOREPLUS,DSTOREPLUS,
INCSTOR,CINCSTOR,WINCSTOR,DINCSTOR,
TOA,ATO,AA,
AF,AS,AFA,ASA,
CAF,CAS,CAFA,CASA,
DAF,DAS,DAFA,DASA,
TOB,BTO,BA,
BF,BS,BFA,BSA,
CBF,CBS,CBFA,CBSA,
DBF,DBS,DBFA,DBSA,
MOVED,MOVEA,FILL,
CMOVED,CMOVEA,CFILL,
DMOVED,DMOVEA,DFILL,
MEM,
LOADLIB,GETPROCA,
SYSCALL0,SYSCALL1,SYSCALL2,SYSCALL3,SYSCALL4,SYSCALL5,
SYSCALL6,SYSCALL7,SYSCALL8,SYSCALL9,SYSCALL10,
//SYSCALL6F1,
//DOT,DOTS,
ENDWORD, // !! cut the dicc !!!
JMP,JMPR,LIT2,LIT3, // internal
AND1,OR1,XOR1, // OPTIMIZATION WORDS
ADD1,SUB1,MUL1,DIV1,
SHL1,SHR1,SHR01,
MOD1,DIVMOD1,MULDIV1,MULSHR1,CDIVSH1,
IFL1,IFG1,IFE1,IFGE1,IFLE1,IFNE1,IFAND1,IFNAND1
};
//////////////////////////////////////
// DEBUG -- remove when all work ok
//////////////////////////////////////
void printword(char *s)
{
while (*s>32) putchar(*s++);
putchar(' ');
}
void printcode(int n)
{
if ((n&0xff)<5 && n!=0) {
printf(r3asm[n&0xff]);printf(" %d",n>>8);
} else if (((n&0xff)>=IFL && (n&0xff)<=IFNAND) || (n&0xff)==JMPR) {
printf(r3bas[n&0xff]);printf(" >> %d",n>>8);
} else if ((n&0xff)>=IFL1 && (n&0xff)<=IFNAND1) {
printf(r3bas[n&0xff]);printf(" %d",n>>16);printf(" >> %d",n<<16>>24);
} else if ((n&0xff)>ENDWORD ) {
printf(r3bas[n&0xff]);printf(" %d",n>>8);
} else
printf(r3bas[n&0xff]);
printf("\n");
}
void dumpcode()
{
printf("code\n");
printf("boot:%d\n",boot);
for(int i=1;i<memc;i++) {
printf("%x:",i);
printcode(memcode[i]);
}
printf("\n");
}
void dumpinc()
{
printf("includes\n");
for(int i=0;i<cntincludes;i++) {
printf("%d. ",i);
printword(includes[i].nombre);
printf("\n");
}
for(int i=0;i<cntstacki;i++) {
printf("%d. %d\n",i,stacki[i]);
}
}
void dumpdicc()
{
printf("diccionario\n");
for(int i=0;i<cntdicc;i++) {
printf("%d. ",i);
printword(dicc[i].nombre);
printf("%d ",dicc[i].mem);
printf("%d \n",dicc[i].info);
}
}
//////////////////////////////////////
// Compiler: from text to dwordcodes
//////////////////////////////////////
// scan for a valid number begin in *p char
// return number in global var "nro"
typedef __INT64_TYPE__ int64_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __INT32_TYPE__ int32_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __INT16_TYPE__ int16_t;
typedef __UINT16_TYPE__ uint16_t;
int64_t nro=0;
int isNro(char *p)
{
//if (*p=='&') { p++;nro=*p;return -1;} // codigo ascii
int dig=0,signo=0,base;
if (*p=='-') { p++;signo=1; } else if (*p=='+') p++;
if ((unsigned char)*p<33) return 0;// no es numero
switch(*p) {
case '$': base=16;p++;break;// hexa
case '%': base=2;p++;break;// binario
default: base=10;break;
};
nro=0;if ((unsigned char)*p<33) return 0;// no es numero
while ((unsigned char)*p>32) {
if (base!=10 && *p=='.') dig=0;
else if (*p<='9') dig=*p-'0';
else if (*p>='a') dig=*p-'a'+10;
else if (*p>='A') dig=*p-'A'+10;
else return 0;
if (dig<0 || dig>=base) return 0;
nro*=base;nro+=dig;
p++;
};
if (signo==1) nro=-nro;
return -1;
};
// scan for a valid fixed point number begin in *p char
// return number in global var "nro"
int isNrof(char *p) // decimal punto fijo 16.16
{
int64_t parte0;
int dig=0,signo=0;
if (*p=='-') { p++;signo=1; } else if (*p=='+') p++;
if ((unsigned char)*p<33) return 0;// no es numero
nro=0;
while ((unsigned char)*p>32) {
if (*p=='.') { parte0=nro;nro=1;if (*(p+1)<33) return 0; }
else {
if (*p<='9') dig=*p-'0'; else dig=-1;
if (dig<0 || dig>9) return 0;
nro=(nro*10)+dig;
}
p++;
};
int decim=1;
int64_t num=nro;
while (num>1) { decim*=10;num/=10; }
num=0x10000*nro/decim;
nro=(num&0xffff)|(parte0<<16);
if (signo==1) nro=-nro;
return -1;
};
// uppercase a char
char toupp(char c)
{
return c&0xdf;
}
// compare two string until len char
int strnicmp(const char *s1, const char *s2, size_t len)
{
int diff=0;
while (len--&&*s1&&*s2) {
if (*s1!=*s2) if (diff=(int)toupp(*s1)-(int)toupp(*s2)) break;
s1++;s2++;
}
return diff;
}
// compare two words, until space
int strequal(char *s1,char *s2)
{
while ((unsigned char)*s1>32) {
if (toupp(*s2++)!=toupp(*s1++)) return 0;
}
if (((unsigned char)*s2)>32) return 0;
return -1;
}
// advance pointer with space
char *trim(char *s)
{
while (((unsigned char)*s)<33&&*s!=0) s++;
return s;
}
// advance to next word
char *nextw(char *s)
{
while (((unsigned char)*s)>32) s++;
return s;
}
// advance to next line
char *nextcr(char *s)
{
while (((unsigned char)*s)>31||*s==9) s++;
return s;
}
// advance to next string ("), admit "" for insert " in a string, multiline is ok too
char *nextstr(char *s)
{
s++;
while (*s!=0) {
if (*s==34) {
s++;if (*s!=34) {
s++;break; } }
s++;
}
return s;
}
// ask for a word in the basic dicc
int isBas(char *p)
{
nro=0;
char **m=(char**)r3bas;
while (**m!=0) {
if (strequal(*m,p)) return -1;
*m++;nro++; }
return 0;
};
// ask for a word in the dicc, calculate local or exported too
int isWord(char *p)
{
int i=cntdicc;
while (--i>-1) {
if (strequal(dicc[i].nombre,p) && ((dicc[i].info&1)==1 || i>=dicclocal)) return i;
}
return -1;
};
// compile a token (int)
void codetok(int nro)
{
memcode[memc++]=nro;
}
// close variable definition with a place when no definition
void closevar()
{
if (cntdicc==0) return;
if (!dicc[cntdicc-1].info&0x10) return; // prev is var
if (dicc[cntdicc-1].mem<memd) return; // have val
memdata[memd]=0;memd+=8; // now 64 bits
}
// compile data definition, a VAR
void compilaDATA(char *str)
{
int ex=0;
closevar();
if (*(str+1)=='#') { ex=1;str++; } // exported
dicc[cntdicc].nombre=str+1;
memd+=memd&3; // align data!!! (FILL break error)
dicc[cntdicc].mem=memd;
dicc[cntdicc].info=ex+0x10; // 0x10 es dato
cntdicc++;
modo=2;
}
// compile a code definition, a WORD
void compilaCODE(char *str)
{
int ex=0;
closevar();
if (*(str+1)==':') { ex=1;str++; } // exported
dicc[cntdicc].nombre=str+1;
dicc[cntdicc].mem=memc;
dicc[cntdicc].info=ex; // 0x10 es dato
cntdicc++;
if (*(str+1)<33) {
ex=boot;boot=memc;
if (ex!=-1) { codetok((ex<<8)+CALL); } // call to prev boot code
}
modo=1;
}
// store in datamemory a string
int datasave(char *str)
{
int r=memd;
for(;*str!=0;str++) {
if (*str==34) { str++;if (*str!=34) break; }
memdata[memd++]=*str;
}
memdata[memd++]=0;
return r;
}
// compile a string, in code save the token to retrieve too.
void compilaSTR(char *str)
{
str++;
int ini=datasave(str);
if (modo<2) codetok((ini<<8)+ADR); // lit data
}
// Store in datamemory a number or reserve mem
void datanro(int64_t n) {
char *p=&memdata[memd];
switch(modo){
case 2:*(int64_t*)p=(int64_t)n;memd+=8;break;
case 3: for(int i=0;i<n;i++) { *p++=0; };memd+=n;break;
case 4:*p=(char)n;memd+=1;break;
case 5:*(int*)p=(int)n;memd+=4;break;
}
}
// Compile adress of var
void compilaADDR(int n)
{
if (modo>1) {
if ((dicc[n].info&0x10)==0)
datanro(dicc[n].mem);
else
datanro((int64_t)&memdata[dicc[n].mem]);
return;
}
codetok((dicc[n].mem<<8)+LIT+((dicc[n].info>>4)&1)); //1 code 2 data
}
// Compile literal
void compilaLIT(int64_t n)
{
if (modo>1) { datanro(n);return; }
int token=n;
codetok((token<<8)+LIT);
if ((token<<8>>8)==n) return;
token=n>>24;
codetok((token<<8)+LIT2);
if ((token<<8>>8)==(n>>24)) return;
token=n>>48;
codetok((token<<8)+LIT3);
}
// Start block code (
void blockIn(void)
{
pushA(memc);
level++;
}
// solve conditional void
int solvejmp(int from,int to)
{
int whi=false;
for (int i=from;i<to;i++) {
int op=memcode[i]&0xff;
if (op>=ZIF && op<=IFBT && (memcode[i]>>8)==0) { // patch while
memcode[i]|=(memc-i)<<8; // full dir
whi=true;
} else if (op>=IFL1 && op<=IFNAND1 && (memcode[i]&0xff00)==0) { // patch while
memcode[i]|=((memc-i)<<8)&0xff00; // byte dir
whi=true;
}
}
return whi;
}
// end block )
void blockOut(void)
{
int from=popA();
int dist=memc-from;
if (solvejmp(from,memc)) { // salta
codetok((-(dist+1)<<8)+JMPR); // jmpr
} else { // patch if
if ((memcode[from-1]&0xff)>=IFL1 && (memcode[from-1]&0xff)<=IFNAND1) {
memcode[from-1]|=(dist<<8)&0xff00; // byte dir
} else {
memcode[from-1]|=(dist<<8); // full dir
}
}
level--;
if (level<0) {
printf("ERROR bad )\n");
}
lastblock=memc;
}
// start anonymous definition (adress only word)
void anonIn(void)
{
pushA(memc);
codetok(JMP);
level++;
}
// end anonymous definition, save adress in stack
void anonOut(void)
{
int from=popA();
memcode[from]|=(memc<<8); // patch jmp
codetok((from+1)<<8|LIT);
level--;
if (level<0) {
printf("ERROR bad )\n");
}
}
// dicc base in data definition
void dataMAC(int n)
{
if (n==1) modo=4; // ( bytes
if (n==2) modo=2; // )
if (n==3) modo=5; // [ qwords
if (n==4) modo=2; // ]
if (n==MUL) modo=3; // * reserva bytes Qword Dword Kbytes
}
// compile word from base diccionary
void compilaMAC(int n)
{
if (modo>1) { dataMAC(n);return; }
if (n==0) { // ;
if (level==0) modo=0;
if ((memcode[memc-1]&0xff)==CALL && lastblock!=memc) { // avoid jmp to block
memcode[memc-1]=(memcode[memc-1]^CALL)|JMP; // call->jmp avoid ret
return;
}
}
if (n==1) { blockIn();return; } //( etiqueta
if (n==2) { blockOut();return; } //) salto
if (n==3) { anonIn();return; } //[ salto:etiqueta
if (n==4) { anonOut();return; } //] etiqueta;push
int token=memcode[memc-1];
// optimize conditional jump to short version
if (n>=IFL && n<=IFNAND && (token&0xff)==LIT && (token<<8>>16)==(token>>8)) {
memcode[memc-1]=((token<<8)&0xffff0000)|(n-IFL+IFL1);
return;
}
// optimize operation with constant
if (n>=AND && n<=CDIVSH && (token&0xff)==LIT && lastblock!=memc) {
memcode[memc-1]=(token^LIT)|(n-ADD+ADD1);
return;
}
codetok(n);
}
// compile word
void compilaWORD(int n)
{
if (modo>1) { datanro(n);return; }
codetok((dicc[n].mem<<8)+CALL+((dicc[n].info>>4)&1));
}
// --- error in code --
void seterror(char *f,char *s)
{
werror=s;
cerror=f;
}
// print error info
void printerror(char *name,char *src)
{
int line=1;
char *lc=src;
for (char *p=src;p<cerror;p++)
if (*p==10) { if (*(p+1)==13) p++;
line++;lc=p+1;
} else if (*p==13) { if (*(p+1)==10) p++;
line++;lc=p+1; }
*nextcr(lc)=0; // put 0 in the end of line
printf("in: ");printword(name);printf("\n\n");
printf("%s\n",lc);
for(char *p=lc;p<cerror;p++) if (*p==9) printf("\t"); else printf(" ");
printf("^-");
printf("ERROR %s, line %d\n\n",werror,line);
}
// |WEB| emscripten only
// |LIN| linux only
// |WIN| windows only
// |RPI| Raspberry PI only
char *nextcom(char *str)
{
#if defined(LINUX)
if (strnicmp(str,"|LIN|",5)==0) { // linux specific
return str+5;
}
#elif defined(EMSCRIPTEN)
if (strnicmp(str,"|WEB|",5)==0) { // web specific
return str+5;
}
#elif defined(RPI)
if (strnicmp(str,"|RPI|",5)==0) { // raspberry pi specific
return str+5;
}
#else
if (strnicmp(str,"|WIN|",5)==0) { // window specific
return str+5;
}
#endif
return nextcr(str);
}
// tokeniza string
int r3token(char *str)
{
level=0;
while(*str!=0) {
str=trim(str);if (*str==0) return -1;
switch (*str) {
case '^': // include
str=nextcr(str);break;
case '|': // comments
str=nextcom(str);break;
case '"': // strings
compilaSTR(str);str=nextstr(str);break;
case ':': // $3a : Definicion // :CODE
compilaCODE(str);str=nextw(str);break;
case '#': // $23 # Variable // #DATA
compilaDATA(str);str=nextw(str);break;
case 0x27: // $27 ' Direccion // 'ADR
nro=isWord(str+1);
if (nro<0) {
// if (isBas(str)) // 'ink allow compile replace
// { compilaMAC(nro);str=nextw(str);break; }
seterror(str,"adr not found");return 0;
}
compilaADDR(nro);str=nextw(str);break;
default:
if (isNro(str)||isNrof(str))
{ compilaLIT(nro);str=nextw(str);break; }
if (isBas(str))
{ compilaMAC(nro);str=nextw(str);break; }
nro=isWord(str);
if (nro<0) { seterror(str,"word not found");return 0; }
if (modo==1)
compilaWORD(nro);
else
compilaADDR(nro);
str=nextw(str);break;
}
}
return -1;
}
// open, alloc and load file to string in memory
char *openfile(char *filename)
{
long len;
char *buff;
FILE *f=fopen(filename,"rb");
if (!f) {
printf("FILE %s not found\n",filename);
cerror=(char*)1;
return 0;
}
fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET);
buff=(char*)malloc(len+1);
if (!buff) return 0;
fread(buff,1,len,f);
fclose(f);
buff[len]=0;
return buff;
/*
int fd=open(name,O_RDONLY);
int len=lseek(fd,0,SEEK_END);
void *data=mmap(0,len,PROT_READ,MAP_PRIVATE,fd,0);
*/
}
// include logic, not load many times
int isinclude(char *str)
{
char filename[1024];
char *fn=filename;
char *ns=str;
if (*str=='.') {
str++;
strcpy(filename,path);
while (*fn!=0) fn++;
}
while ((unsigned char)*str>31) { *fn++=*str++; }
*fn=0;
//printf("[%s]",filename);
for (int i=0;i<cntincludes;i++){
if (strequal(includes[i].nombre,ns)) return -1;
}
includes[cntincludes].nombre=ns; // ./coso y coso son distintos !!
includes[cntincludes].str=openfile(filename);
cntincludes++;
return cntincludes-1;
}
// free source code of includes
void freeinc()
{
for (int i=0;i<cntincludes;i++){
free(includes[cntincludes].str);
}
}
//----------- comments / configuration
// |MEM 640 set data memory size (in kb) min 1kb
char *syscom(char *str)
{
if (strnicmp(str,"|MEM ",5)==0) { // memory in Kb
if (isNro(trim(str+5))) {
memdsize=nro<<20;
if (memdsize<1<<20) memdsize=1<<20; // 1MB min
}
}
return nextcom(str);
}
// resolve includes, recursive definition
void r3includes(char *str)
{
if (str==0) return;
//if (*str=='.') { }
int ninc;
while(*str!=0) {
str=trim(str);
switch (*str) {
case '^': // include
ninc=isinclude(str+1);
if (ninc>=0) {
r3includes(includes[ninc].str);
stacki[cntstacki++]=ninc;
}
str=nextcr(str);
break;
case '|': // comments
str=syscom(str);break;
case ':': // code
modo=1;str=nextw(str);break;
case '#': // data
modo=0;str=nextw(str);break;
case '"': // strings
memcsize+=modo;str=nextstr(str);break;
default: // resto
memcsize+=modo;str=nextw(str);break;
}
}
return;
}
// Compile code in file
int r3compile(char *name)
{
printf("\nr3vm - PHREDA\n");
printf("compile:%s...",name);
char *sourcecode;
strcpy(path,name);// para ^. ahora pone el path del codigo origen
char *aa=path+strlen(path);
while (path<aa) { if (*aa=='/'||*aa=='\\') { *aa=0;break; } aa--; }
//printf("*%s*",path);
sourcecode=openfile(name);
if (sourcecode==0) return 0;
memcsize=0;
cntincludes=0;
cntstacki=0;
r3includes(sourcecode); // load includes
if (cerror!=0) return 0;
//dumpinc();
cntdicc=0;
dicclocal=0;
boot=-1;
memc=1; // direccion 0 para null
memd=0;
#if defined(LINUX)
memcode=(int*)mmap(NULL,sizeof(int)*memcsize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE|MAP_32BIT,-1,0);
memdata=(char*)mmap(NULL,memdsize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE|MAP_32BIT,-1,0);
#elif defined(RPI)
memcode=(int*)mmap(NULL,sizeof(int)*memcsize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE/*|MAP_32BIT*/,-1,0);
memdata=(char*)mmap(NULL,memdsize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE/*|MAP_32BIT*/,-1,0);
#else
memcode=(int*)malloc(sizeof(int)*memcsize);
memdata=(char*)malloc(memdsize);
#endif
// tokenize includes
for (int i=0;i<cntstacki;i++) {
if (!r3token(includes[stacki[i]].str)) {
printerror(includes[stacki[i]].nombre,includes[stacki[i]].str);
return 0;
}
dicclocal=cntdicc;
}
// last tokenizer
if (!r3token(sourcecode)) {
printerror(name,sourcecode);
return 0;
}
//memd+=memd&3; // align
//dumpdicc();
//dumpcode();
printf(" ok.\n");
printf("inc:%d - words:%d - code:%dKb - data:%dKb - free:%dKb\n\n",cntincludes,cntdicc,memc>>8,memd>>10,(memdsize-memd)>>10);
freeinc();
free(sourcecode);
return -1;
}
//----------------------
/*--------RUNER--------*/
//----------------------
#define iclz(x) __builtin_clz(x)
// http://www.devmaster.net/articles/fixed-point-optimizations/
static inline int64_t isqrt(int64_t value)
{
if (value==0) return 0;
int bshft = (63-iclz(value))>>1; // spot the difference!
int64_t g = 0;
int64_t b = 1<<bshft;
do {
int64_t temp = (g+g+b)<<bshft;
if (value >= temp) { g += b;value -= temp; }
b>>=1;
} while (bshft--);
return g;
}
//---------------------------//
// TOS..DSTACK--> <--RSTACK //
//---------------------------//
#define STACKSIZE 256
int64_t stack[STACKSIZE];
void printstack(int64_t *R){
int64_t *RTOS=&stack[STACKSIZE-1];
while (RTOS>=R) {
printf("%x ",*RTOS);
RTOS--;
}
}
void printstackd(int64_t *T){
int64_t *TOS=stack;
while (TOS<=T) {
printf("%d ",*TOS);
TOS++;
}
}
FILE *file;
void memset32(uint32_t *dest, uint32_t val, uint32_t count)
{ while (count--) *dest++ = val; }
void memset64(uint64_t *dest, uint64_t val, uint32_t count)
{ while (count--) *dest++ = val; }
// run code, from adress "boot"
void runr3(int boot)
{
stack[STACKSIZE-1]=0;
register int64_t TOS=0;
register int64_t *NOS=&stack[0];
register int64_t *RTOS=&stack[STACKSIZE-1];
register int64_t REGA=0;
register int64_t REGB=0;
register int64_t op=0;
register int ip=boot;
next:
op=memcode[ip++];
// printstack(RTOS);
printf("%x:%x:",ip,TOS);printcode(op);
switch(op&0xff){
case FIN:ip=*RTOS;RTOS++;if (ip==0) return;
// printf("r:%x ip:%x\n",*RTOS,ip);
goto next; // ;
case LIT:NOS++;*NOS=TOS;TOS=op>>8;goto next; // LIT1
case ADR:NOS++;*NOS=TOS;TOS=(int64_t)&memdata[op>>8];goto next; // LIT adr
case CALL:RTOS--;*RTOS=ip;ip=(unsigned int)op>>8;goto next; // CALL
case VAR:NOS++;*NOS=TOS;TOS=*(int64_t*)&memdata[op>>8];goto next;// VAR
case EX:RTOS--;*RTOS=ip;ip=TOS;TOS=*NOS;NOS--;goto next; //EX
case ZIF:if (TOS!=0) {ip+=(op>>8);}; goto next;//ZIF
case UIF:if (TOS==0) {ip+=(op>>8);}; goto next;//UIF
case PIF:if (TOS<0) {ip+=(op>>8);}; goto next;//PIF
case NIF:if (TOS>=0) {ip+=(op>>8);}; goto next;//NIF
case IFL:if (TOS<=*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFL
case IFG:if (TOS>=*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFG
case IFE:if (TOS!=*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFN
case IFGE:if (TOS>*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFGE
case IFLE:if (TOS<*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFLE
case IFNE:if (TOS==*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFNO
case IFAND:if (!(TOS&*NOS)) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFNA
case IFNAND:if (TOS&*NOS) {ip+=(op>>8);} TOS=*NOS;NOS--;goto next;//IFAN
case IFBT:if (*(NOS-1)>TOS||*(NOS-1)<*NOS) {ip+=(op>>8);}
TOS=*(NOS-1);NOS-=2;goto next;//BTW (need bit trick) //(TOS-*(NOS-1))|(*(NOS-1)-*NOS)>0
case DUP:NOS++;*NOS=TOS;goto next; //DUP
case DROP:TOS=*NOS;NOS--;goto next; //DROP
case OVER:NOS++;*NOS=TOS;TOS=*(NOS-1);goto next; //OVER
case PICK2:NOS++;*NOS=TOS;TOS=*(NOS-2);goto next;//PICK2
case PICK3:NOS++;*NOS=TOS;TOS=*(NOS-3);goto next;//PICK3
case PICK4:NOS++;*NOS=TOS;TOS=*(NOS-4);goto next;//PICK4
case SWAP:op=*NOS;*NOS=TOS;TOS=op;goto next; //SWAP
case NIP:NOS--;goto next; //NIP
case ROT:op=TOS;TOS=*(NOS-1);*(NOS-1)=*NOS;*NOS=op;goto next; //ROT
case DUP2:op=*NOS;NOS++;*NOS=TOS;NOS++;*NOS=op;goto next;//DUP2
case DROP2:NOS--;TOS=*NOS;NOS--;goto next; //DROP2
case DROP3:NOS-=2;TOS=*NOS;NOS--;goto next; //DROP3
case DROP4:NOS-=3;TOS=*NOS;NOS--;goto next; //DROP4
case OVER2:NOS++;*NOS=TOS;TOS=*(NOS-3);
NOS++;*NOS=TOS;TOS=*(NOS-3);goto next; //OVER2
case SWAP2:op=*NOS;*NOS=*(NOS-2);*(NOS-2)=op;
op=TOS;TOS=*(NOS-1);*(NOS-1)=op;goto next; //SWAP2
case TOR:RTOS--;*RTOS=TOS;TOS=*NOS;NOS--;goto next; //>r
case RFROM:NOS++;*NOS=TOS;TOS=*RTOS;RTOS++;goto next; //r>
case ERRE:NOS++;*NOS=TOS;TOS=*RTOS;goto next; //r@
case AND:TOS&=*NOS;NOS--;goto next; //AND
case OR:TOS|=*NOS;NOS--;goto next; //OR
case XOR:TOS^=*NOS;NOS--;goto next; //XOR
case ADD:TOS=*NOS+TOS;NOS--;goto next; //SUMA
case SUB:TOS=*NOS-TOS;NOS--;goto next; //RESTA
case MUL:TOS=*NOS*TOS;NOS--;goto next; //MUL
case DIV:TOS=(*NOS/TOS);NOS--;goto next; //DIV
case SHL:TOS=*NOS<<TOS;NOS--;goto next; //SAl
case SHR:TOS=*NOS>>TOS;NOS--;goto next; //SAR
case SHR0:TOS=((uint64_t)*NOS)>>TOS;NOS--;goto next; //SHR
case MOD:TOS=*NOS%TOS;NOS--;goto next; //MOD
case DIVMOD:op=*NOS;*NOS=op/TOS;TOS=op%TOS;goto next; //DIVMOD
case MULDIV:TOS=((long long)(*(NOS-1))*(*NOS)/TOS);NOS-=2;goto next; //MULDIV
case MULSHR:TOS=((long long)(*(NOS-1)*(*NOS))>>TOS);NOS-=2;goto next; //MULSHR
case CDIVSH:TOS=(long long)((*(NOS-1)<<TOS)/(*NOS));NOS-=2;goto next;//CDIVSH
case NOT:TOS=~TOS;goto next; //NOT