wm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdarg.h>
  4. #include<getopt.h>
  5. #include<unistd.h>
  6. #include<limits.h>
  7. #include<time.h>
  8. #include<sys/types.h>
  9. #include<sys/stat.h>
  10. #include<fcntl.h>
  11. #include<string.h>
  12. #include<math.h>
  13. #ifndef LLONG_MAX
  14. # define LLONG_MAX 9223372036854775807LL
  15. #endif
  16. typedef int I;typedef char C;typedef C*S;typedef size_t Z;typedef signed long long L;typedef void V;typedef V*P;typedef V**PP;typedef unsigned char U;typedef unsigned long long UL;typedef double D;
  17. #define R return
  18. #define K 1
  19. #define Rk return 1
  20. #define JA (EZ+SZ)
  21. #define SIZ(x)(sizeof(x))
  22. #define SW(x)switch(x){
  23. #define CS(x,a)case x:{a}break;
  24. #define DF(a)default:{a}}
  25. #define ELIF else if
  26. #define LP()while(1)
  27. I ST=0;I Du=0;I DD=0;Z SZ=5120,MZ=65535,EZ=65535,JZ=10;L*m;L sp=-K,jp=-K;U lo=0;L**mm=0;L mi=0;
  28. V d(){L p=m[0]-K;L o=p>=0&&p<MZ?m[p]:0;fprintf(stderr,"[wm] stopped at address: 0x%llx (value = 0x%llx); last instruction: 0x%x\n",p,o,lo);fprintf(stderr,"[wm] stack pointer: 0x%llx.\n",(UL)(EZ+sp));fputs("[wm] jump frame:\n",stderr);L i;for(i=0;i<JZ;i++){if(i>=10){fputs("...\n",stderr);break;}fprintf(stderr, " #%-5lld0x%llx%s\n",i,m[JA+i],i==jp?" (here)":"");}if(mi){fprintf(stderr,"[wm] memory banks (%lld total):\n",mi);for(i=0;i<mi;i++)fprintf(stderr," 0x%-5llx (%s)\n",i,mm[i]?"in use":"free");}}
  29. V e(S s,...){fflush(stderr);fprintf(stderr,"[wm] error: ");va_list a;va_start(a,s);vfprintf(stderr,s,a);va_end(a);putchar('\n');d();exit(K);}
  30. P a(Z z){P r=malloc(z);if(!r)e("out of memory.");R r;}
  31. P ra(P p,Z z){P r=realloc(p,z);if(!r)e("out of memory.");R r;}
  32. V l(S s,FILE*f){m[0]=K;if(!f)e("can't open file '%s'.",s);Z i=K;U b[8];LP(){Z z=fread(b,K,8,f);if(!z)break;if(z!=8){I pz=ftell(f);if(pz<0)e("decoding of the image '%s' failed.",s);else e("decoding of the image '%s' failed near byte at #%ld.",s,pz-z);}if(i>=LLONG_MAX)e("image '%s' is too large (max. = %lld).",s,LLONG_MAX);if(i>=EZ)e("image '%s' doesn't fit into the memory (size = %ld).",s,EZ);m[i++]=(((L)b[0])&0xff)|((((L)b[K])&0xff)<<8)|((((L)b[2])&0xff)<<16)|((((L)b[3])&0xff)<<24)|((((L)b[4])&0xff)<<32)|((((L)b[5])&0xff)<<40)|((((L)b[6])&0xff)<<48)|((((L)b[7])&0xff)<<56);if(Du){if(m[i-1]<1)fprintf(stderr,"%04lu:%08lld ",i-2,m[i-1]);else fprintf(stderr,"%04lu:%08llx ",i-2,m[i-1]);if((i-1)%4==0)fputs("\n",stderr);}}if(Du){fprintf(stderr,"(%lu)\n",i-1);if(!DD)fputs("\n",stderr);}}
  33. V pj(L a){if(jp>=0&&jp>=JZ)e("jump frame overflow.");m[++jp+JA]=a;}
  34. L tj(){if(jp<0)e("jump frame underflow.");R m[JA+jp--];}
  35. L lj(){if(jp<0)e("jump frame underflow.");R m[JA+jp];}
  36. Z g(){if(m[0]>=MZ)m[0]=K;R m[m[0]++];}
  37. L iL(){C b[64];if(!fgets(b,SIZ(b),stdin))R 0;R strtoll(b,0,10);}
  38. V ro(I);L r(L);V w(L,L);V j(I a){if(a==-K){ST=K;R;}ELIF(a==-5)a=tj();if(a<0||a>=MZ)e("attempt to jump to an illegal address: 0x%x.",a);m[0]=a;}
  39. S rS(L e){L i=e;S s=a(K);Z k=0,z=K;LP(){I c=r(i++);if(c<=0)break;if(k==z-K){z++;s=ra(s,z);}s[k++]=c;}s[i]=0;R s;}
  40. V fo(L a){L re=a++;I m=r(a++);S n=rS(r(a));I mo=m;m=m==0?O_WRONLY:m==K?(O_RDONLY|O_CREAT):m==2?O_RDWR:m==3?O_APPEND:-K;if(m<0)e("illegal mode flag: 0x%x.",mo);I dc=open(n,m);free(n);w(re,dc);}
  41. V fw(L a){I dc=r(a++);L re=a++;S s=rS(r(a));Z z=strlen(s);L c=write(dc,s,z);free(s);w(re,c);}
  42. Z wS(L a,S s){Z i=0;while(s[i++])w(a+i,s[i-1]);w(a+i,0);R i;}
  43. V fr(L e){I dc=r(e++);Z z=r(e++);S s=a(z+1);w(e++,read(dc,s,z));wS(r(e)-K,s);free(s);}
  44. V fs(L a){I dc=r(a++);I o=r(a++);I w=r(a);I wo=w;w=w==0?SEEK_SET:w==1?SEEK_CUR:w==2?SEEK_END:-K;if(w<0)e("illegal seek mode: 0x%x.",wo);lseek(dc,o,w);}
  45. L aM(Z z){I i;for(i=0;i<mi;i++){if(!mm[i])mm[i]=a(SIZ(L)*z);R i;}mi++;mm=ra(mm,SIZ(L*)*mi);mm[mi-1]=a(SIZ(L)*z);R mi-1;}
  46. V aF(L i){free(mm[i]);mm[i]=0;}
  47. L mR(L v){L i=r(v+K);L p=r(v+2);if(i<0||i>=mi||!mm[i])e("attempt to write to an illegal memory bank: 0x%x.",i);if(p<0||p>=64)e("attempt to write to an illegal memory bank: 0x%x (pointer: 0x%x).",i,p);R mm[i][p];}
  48. V mW(L v){L i=r(v+K);L p=r(v+2);L a=r(v+3);if(i<0||i>=mi||!mm[i])e("attempt to write to an illegal memory bank: 0x%x.",i);if(p<0||p>=64)e("attempt to write to an illegal memory bank: 0x%x (pointer: 0x%x).",i,p);mm[i][p]=a;}
  49. V mF(L v){L i=r(v+K);if(i<0||i>=mi||!mm[i])e("attempt to deallocate an illegal memory bank: 0x%x.",i);aF(i);}
  50. #define DR0(x,p)case x:{p}break
  51. #define DR1(x,p)case x:a=r(v+1);w(v+1,(p));break
  52. #define DR2(x,p)case x:a=r(v+1);b=r(v+2);w(v+2,(p));break
  53. #define DR3(x,p)case x:a=r(v+1);b=r(v+2);c=r(v+3);w(v+3,(p));break
  54. L mO(L a,L b){if(b==0)e("modulo by zero.");R a%b;}
  55. L dI(L a,L b){if(b==0)e("division by zero.");R a/b;}
  56. V pu(L v){if(sp>=0&&sp>=SZ)e("stack overflow (size = %d).",SZ);if(sp<0)sp=0;m[MZ+sp++]=v;}
  57. L po(){if(sp<0)e("stack underflow.");R m[MZ+--sp];}
  58. L riD(D d){L l;memcpy(&l,&d,SIZ(l));R l;}
  59. D riL(L l){D d;memcpy(&d,&l,SIZ(d));R d;}
  60. L eX(I k){if(DD)d();exit(k);R -1;}L ouD(L a){D d=riL(a);printf("%g",d);R a;}
  61. D dvD(D a,D b){if(b==0)e("division by zero.");R a/b;}
  62. D mdD(D a,D b){if(b==0)e("modulo by zero.");R fmod(a,b);}
  63. V cP(L v){L d=r(v);L a,b,c;
  64. SW(d)DR1(0,~a);DR2(1,b&a);DR2(2,b|a);
  65. DR2(3,b^a);DR2(4,b<<a);DR2(5,b>>a);
  66. DR2(6,a==0?0:a<0?-1:1);DR2(7,b+a);
  67. DR2(8,b-a);DR2(9,b*a);DR2(10,dI(b,a));
  68. DR2(11,mO(b,a));DR2(12,pow(b,a));
  69. DR1(13,riD(sqrt(riL(a))));DR1(20,-a);
  70. DR2(70,riD(riL(b)+riL(a)));DR2(71,riD(riL(b)-riL(a)));
  71. DR2(72,riD(riL(b)*riL(a)));DR2(73,riD(dvD(riL(b),riL(a))));
  72. DR2(74,riD(mdD(riL(b),riL(a))));DR2(75,riD(powf(riL(b),riL(a))));
  73. DR1(80,eX(a));DR1(81,ouD(a));
  74. DR0(40,a=po();pu(a);pu(a););DR0(41,a=po();b=po();pu(b);pu(a););DR0(42,po(););DF(e("illegal special directive: 0x%llx.",d);)}
  75. L r(L a){if(a<-1000){a=llabs(a);a-=1000;L i=a/64;L p=a%64;if(i<0||i>=mi||!mm[i])e("attempt to read an illegal memory bank: 0x%x.",i);if(p<0||p>=64)e("attempt to read an illegal memory bank: 0x%x (pointer: 0x%x).",i,p);R mm[i][p];}ELIF(a==-K)R getchar();ELIF(a==-2)R 0;ELIF(a==-3)Rk;ELIF(a==-4)R-K;ELIF(a==-5)R lj();ELIF(a==-6)R time(0);ELIF(a==-7)R MZ+sp;ELIF(a==-8)R EZ;ELIF(a==-9)R SZ;ELIF(a==-10)R MZ;ELIF(a==-11)R JZ;ELIF(a==-12)R iL();ELIF(a==-13)R aM(64);ELIF(a==-15)R 0;if(a<0||a>=MZ)e("attempt to read from an illegal address: 0x%x.",a);R m[a];}
  76. V w(L a,L v){if(a<-1000){a=llabs(a);a-=1000;L i=a/64;L p=a%64;if(i<0||i>=mi||!mm[i])e("attempt to write to an illegal memory bank: 0x%x.",i);if(p<0||p>=64)e("attempt to write to an illegal memory bank: 0x%x (pointer: 0x%x).",i,p);mm[i][p]=v;R;} else if(a==-K){putchar(v);R;}ELIF(a==-2){printf("%lld",v);R;}ELIF(a==-5){pj(v);R;}ELIF(a==-6){usleep(v);R;}ELIF(a==-7){sp=v-MZ;R;}ELIF(a==-8){fo(v);R;}ELIF(a==-9){if(v>=0)close(v);R;}ELIF(a==-10){fr(v);R;}ELIF(a==-11){fw(v);R;}ELIF(a==-12){fs(v);R;}ELIF(a==-13){SW(r(v))CS(0,w(v,aM(64));)CS(K,mF(v);)CS(2,mW(v);)CS(3,w(v,mR(v));)DF(w(v,-1);)R;}ELIF(a==-14){cP(v);R;}ELIF(a==-15){R;}if(a<0||a>=MZ)e("attempt to write (value = 0x%x) to an illegal address: 0x%x.",v,a);m[a]=v;}
  77. #define OP1(x,p)case x:a=g();if(s)break;{p}break
  78. #define OP2(x,p)case x:a=g();b=g();if(s)break;{p}break
  79. #define OP3(x,p)case x:a=g();b=g();c=g();if(s)break;{p}break
  80. enum{NOP,MVJ,SBLEZ,ABLEZ,SBLZ,BLES,NBNZ,DBNZ,SSLEZ,ASLEZ,IBNC,VBLZ,XBLZ,DSLZ,SSGT,MBNZ,MODBZ,AJA,LA,LD,IA,JMC,JW,PSH,PD,POP,SHLBNZ,SHRBNZ,NBZ,ANZ,ABGZ,SWP,STR};
  81. V ro(I s){L a,b,c,d,t;U o=g();lo=o;
  82. SW(o)CS(NOP,);OP3(MVJ,if(a!=b||(a==-15&&b==-15)||(a<0||b<0))w(b,r(a));j(c););
  83. OP3(SBLEZ,w(b,r(b)-r(a));if(r(b)<=0)j(c););
  84. OP3(ABLEZ,w(b,r(b)+r(a));if(r(b)<=0)j(c););
  85. OP3(SBLZ,w(b,r(b)-r(a));if(r(b)<0)j(c););
  86. OP3(BLES,if(r(b)<=r(a))j(c);else w(b,r(b)-r(a)););
  87. OP2(NBNZ,w(a,!r(a));if(r(a))j(b););
  88. OP2(DBNZ,w(a,r(a)-K);if(r(a))j(b););
  89. OP2(SSLEZ,w(b,r(b)-r(a));if(r(b)<0)ro(K););
  90. OP2(ASLEZ,w(b,r(b)+r(a));if(r(b)<0)ro(K););
  91. OP3(IBNC,t=r(b);w(b,r(a)+K);if(r(b)==t)j(c););
  92. OP3(VBLZ,w(b,dI(r(b),r(a)));if(r(b)<0)j(c););
  93. OP3(XBLZ,w(b,r(b)^r(a));if(r(b)>r(a))j(c););
  94. OP1(DSLZ,w(a,r(a)-K);if(r(a)<0)ro(K););
  95. OP2(SSGT,t=r(a)>r(b);w(b,r(b)-r(a));w(a,r(b));if(t)ro(K););
  96. OP3(MBNZ,if(a!=-3)w(b,r(b)*r(a));if(r(b))j(c););
  97. OP3(MODBZ,w(b,mO(r(b),r(a)));if(!r(b))j(c););
  98. OP2(AJA,w(b,r(b)+r(a));j(r(b)););
  99. OP2(LA,w(b,r(r(a))););OP2(LD,w(b,a););OP1(IA,t=r(r(a));w(t,r(t)+K););
  100. OP1(JMC,j(m[0]*a););OP1(JW,pj(m[0]);j(a););
  101. OP1(PSH,pu(r(a)););OP1(PD,pu(a););OP1(POP,w(a,po()););
  102. OP3(SHLBNZ,w(b,r(b)<<r(a));if(r(b))j(c););
  103. OP3(SHRBNZ,w(b,r(b)>>r(a));if(r(b))j(c););
  104. OP2(NBZ,w(a,~r(a));if(!r(a))j(c););
  105. OP3(ANZ,w(b,r(b)&r(a));if(r(b))j(c););
  106. OP3(ABGZ,w(b,r(b)+r(a));if(r(b)>0)j(c););
  107. OP2(SWP,t=r(a);w(a,r(b));w(b,t););OP2(STR,w(r(b),r(a)););DF(e("unrecognized operation: 0x%x.",o);)}
  108. V rn(){while(!ST)ro(0);}
  109. main(I c,S*as){I o;S f=0;
  110. while((o=getopt(c,as,"dDs:j:z:f:"))!=-K){
  111. SW(o)CS('d',Du=1;);CS('D',DD=1;);CS('f',f=optarg;);
  112. CS('s',MZ=strtol(optarg,0,10);if(MZ<=0){fputs("bad -s.\n",stderr);Rk;});
  113. CS('j',JZ=strtol(optarg,0,10);if(JZ<=0){fputs("bad -j.\n",stderr);Rk;});
  114. CS('z',SZ=strtol(optarg,0,10);if(SZ<=0){fputs("bad -z.\n",stderr);Rk;});
  115. CS('?',exit(K););}}
  116. I i;for(i=optind;i<c;i++)f=as[i];EZ=MZ;MZ+=SZ;MZ+=JZ;
  117. m=a(SIZ(L)*MZ);if(!f)l("<stdin>",stdin);else{FILE*fd=fopen(f,"rb");l(f,fd);fclose(fd);}rn();if(DD)d();free(m);for(i=0;i<mi;i++)if(mm[i])free(mm[i]);}