ilo-vm/source/ilo-min.c

71 lines
2 KiB
C

/* ilo-min.c (c) charles childers */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define V void
#define I int
#define C char
#define T s[sp]
#define N s[sp-1]
#define R r[rp]
#define G s[(--sp)+1]
#define D sp--
#define P(x) s[++sp]=x
#define F(x,y) f=open(rf,x,0666);y;close(f)
#define B(x,y) f=open(bf,x,0666);b_s();y;close(f)
#define W(x) while(c){x;c--;a++;b++;}
#define X(x) inst[(o>>x)&0xFF]()
#define RUN while(ip<65536)
#define DR "ilo.rom"
#define DB "ilo.blocks"
I ip,sp,rp,s[33],r[257],m[65536],a,b,c,f,o; C *bf,*rf;
V rst() {ip=sp=rp=0;}
V i_l() {F(O_RDONLY,read(f,&m,262144)); rst();}
V i_w() {F(O_WRONLY,write(f,&m,262144));}
V b_s() {a=G; b=G; lseek(f,4096*b,SEEK_SET);}
V b_l() {B(O_RDONLY,read(f,m+a,4096));}
V b_w() {B(O_WRONLY,write(f,m+a,4096));}
V p() {c=G; b=G; a=G;}
V no() {}
V li() {ip++; P(m[ip]);}
V du() {a=T; P(a);}
V dr() {s[sp]=0; D;}
V sw() {a=T; T=N; N=a;}
V pu() {rp++; R=G;}
V po() {P(R); rp--;}
V ju() {ip=G-1;}
V ca() {rp++; R=ip; ip=G-1;}
V cc() {a=G; if (G != 0) {rp++; R=ip; ip=a-1;}}
V cj() {a=G; if (G != 0) ip=a-1;}
V re() {ip=R; rp--;}
V eq() {N=(N == T) ? -1 : 0; D;}
V ne() {N=(N != T) ? -1 : 0; D;}
V lt() {N=(N < T) ? -1 : 0; D;}
V gt() {N=(N > T) ? -1 : 0; D;}
V fe() {T = m[T];}
V st() {m[T]=N; D; D;}
V ad() {N += T; D;}
V su() {N -= T; D;}
V mu() {N *= T; D;}
V di() {a=T; b=N; T=b/a; N=b%a;}
V an() {N=T & N; D;}
V or() {N=T | N; D;}
V xo() {N=T ^ N; D;}
V sl() {N=T << N; D;}
V sr() {N=T >> N; D;}
V cp() {p(); P(-1); W(if(m[b]!=m[a])T=0);}
V cy() {p(); W(m[b]=m[a])}
V i0() {putc(G,stdout);}
V i1() {P(getc(stdin));}
V i5() {i_l(); ip=-1;}
V i6() {ip=65536;}
V i7() {a=sp; P(a); P(rp);}
V (*ix[8])() = {i0, i1, b_l, b_w, i_w, i5, i6, i7};
V io() {ix[G]();}
V (*inst[30])()={
no, li, du, dr, sw, pu, po, ju, ca, cc, cj, re, eq, ne, lt,
gt, fe, st, ad, su, mu, di, an, or, xo, sl, sr, cp, cy, io};
V execute() {RUN{o=m[ip]; X(0); X(8); X(16); X(24); ip++;}}
V ds() {for (;sp>0;D) printf("#%d: %d\n", sp, s[sp]);}
V run() {i_l(); execute(); ds();}
I main(I n, C **v) {bf=(n>1)?v[1]:DB; rf=(n>2)?v[2]:DR; run();}