mirror of https://git.cro.wtf/kip.git
76 lines
3.6 KiB
C
76 lines
3.6 KiB
C
#include"u.h"
|
|
#define BE(y,...)Q(seql(Sl(y),x),__VA_ARGS__)OR
|
|
#define MEMSZ 0x40000
|
|
#define OPSZ 37
|
|
|
|
typedef struct l{W a;struct l*p;Ht c;}Label;
|
|
typedef struct{Vec ws;}Macro;
|
|
typedef struct{B r;S n;W a;Label*p;}Ref;
|
|
|
|
_ B orq=0,meb[MEMSZ];
|
|
_ W org=0,mel;
|
|
_ Label*label;
|
|
_ Ht macros;
|
|
_ Vec refs;
|
|
_ V finp(F);
|
|
|
|
_ V mb(W i,B b){Q(i>=MEMSZ||i<org,die("memory overflow or underflow {ip=%x}",i))meb[i-org]=b;}
|
|
_ V mh(W i,H h){mb(i,h>>8),mb(i+1,h);}
|
|
_ V mw(W i,W w){mh(i,w>>16),mh(i+2,w);}
|
|
_ V mcpy(W dst,B*src,W l){for(W i=0;i<l;++i)mb(dst+i,src[i]);}
|
|
_ V rini(Ref*r,B b,S n){*r=(Ref){b,n,mel,label};}
|
|
_ V lini(Label*l){l->a=mel,l->p=label,hini(&l->c,4,SZ(Label)),label=l;}
|
|
_ Label*lget(Label*l,S n){S x;Label*p=l;x=snxt(n,'/');
|
|
WH(p,Q(l=hget(p->c,x),break)p=p->p)N(l,R l)WH((x=snxt(S0,'/')).l,Q(l,l=hget(l->c,x))OR R 0)R l;}
|
|
_ B ops[OPSZ*2]="npexpbphpwprfbfhfwmbmhmwioiissdrswduovadsumudianorxrslsrsaeqltgtnojujccacc";
|
|
_ B op(B x[2]){W i=0;WH(i++<OPSZ,Q(ops[i*2]==x[0]&&ops[1+i*2]==x[1],R i));R OPSZ;}
|
|
_ B htob(B h){R h>='a'&&h<='f'?h-'a'+10:h>='A'&&h<='F'?h-'F'+10:h>='0'&&h<='9'?h-'0':16;}
|
|
_ B ws(B x){R' '==x||'\t'==x||'\r'==x||'\n'==x;}
|
|
_ V tw(S t){fprintf(stderr,"`%.*s`",t.l,t.p);}
|
|
_ V tasrt(S t,C c){Q(c!=*t.p,tw(t),die(" expected %c",c))}
|
|
_ B tz(S t){R t.l<4?1:t.l<6?2:t.l<10?4:(tw(t),die(" >=32 bits"),0);}
|
|
_ V tto4b(S w,B*bs){W b;++w.p,--w.l;Sfor(w,a,i,Q(16>(b=htob(a)),bs[i/2]=(i%2?bs[i/2]:0)<<4|b)OR tw(w),die(" unknown char `%c`\n",a))}
|
|
_ W tton(S w){W b;W r;r=0,++w.p,--w.l;Sfor(w,a,i,Q(16>(b=htob(a)),r<<=4,r|=b)OR tw(w),die(" unknown char `%c`\n",a));R r;}
|
|
_ S next(S*t){B c,**p;W*l;l=&t->l,p=&t->p;*p+=*l,*l=0;WH(ws(**p),++*p);c=**p,++*l;
|
|
Q(0==c,R*t)OR
|
|
Q('"'==c,WH('"'!=(*p)[(*l)++],))OR
|
|
Q('{'==c,WH('}'!=(*p)[(*l)++],))OR WH(!ws((*p)[*l]),++*l);
|
|
R*t;}
|
|
_ V builtin(S*t){FILE*f;S y,x=*t;++x.p,--x.l;
|
|
BE("org",y=next(t),tasrt(y,'#'),mel=tton(*t);N(orq,orq=1,org=mel))
|
|
BE("res",y=next(t),tasrt(y,'#'),mel+=tton(*t))
|
|
BE("emb",y=next(t),tasrt(y,'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",mel+=fread(meb+mel,1,(W)0-1,f)))
|
|
BE("inc",y=next(t),tasrt(y,'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",finp(f)))
|
|
tw(x),die(" unknown builtin");}
|
|
_ V pass1(S*t){_ B o=0;S x=*t;B c;c=*x.p;
|
|
Q('{'==c,/*discard*/)OR
|
|
Q('"'==c,mcpy(mel,x.p+1,x.l-=2),mel+=x.l)OR
|
|
Q('#'==c,B bs[tz(x)];tto4b(x,bs),mcpy(mel,bs,SZ(bs)),mel+=SZ(bs))OR
|
|
Q(':'==c,++x.p,--x.l,lini(hput(&label->c,x)))OR
|
|
Q('@'==c,++x.p,--x.l,rini(vput(&refs),0,x),mel+=4)OR
|
|
Q('^'==c,++x.p,--x.l,rini(vput(&refs),1,x),mel+=1)OR
|
|
Q('!'==c,Macro*m;++x.p,--x.l,m=hput(¯os,x);vini(&m->ws,8,SZ(S));WH(';'!=*next(t).p,*(S*)vput(&m->ws)=*t))OR
|
|
Q('`'==c,Macro*m;++x.p,--x.l;Q(m=hget(macros,x),Vfor(S,m->ws,y,i,pass1(&y)))OR tw(x),die(" unknown macro"))OR
|
|
Q('-'==c,builtin(t))OR
|
|
Q(';'==c,N(label->p,die("; at top-level"))label=label->p)OR
|
|
Q('~'==c,++x.p,--x.l,o|=0x40,pass1(&x))OR
|
|
Q('$'==c,++x.p,--x.l,o|=0x80,pass1(&x))OR
|
|
Q(2!=x.l||OPSZ==(o|=op(x.p)),tw(x),die(" unknown inst"))OR
|
|
mb(mel++,o),o=0;}
|
|
_ V pass2(V){Label*l;Ws a;Bs b;Vfor(Ref,refs,r,i,Q(l=lget(r.p,r.n),
|
|
Q(0==r.r,mw(r.a,l->a))OR
|
|
Q(1==r.r,a=l->a-r.a;Q(a==(b=a),mb(r.a,b))OR tw(r.n),die(" too far (%d bytes) for ^ref",a)))OR
|
|
tw(r.n),die(" unknown ref"))}
|
|
_ V finp(F f){B*in;W l;S t={0};
|
|
fseek(f,0,SEEK_END),l=ftell(f),fseek(f,0,SEEK_SET),in=mk(l+1),xr(f,in,1,l),in[l]=0,t.p=in;
|
|
WH(0!=*next(&t).p,pass1(&t))}
|
|
_ V fout(F f){pass2();xw(f,meb,1,mel-org);}
|
|
I main(I ac,C**av){FILE *f;
|
|
Q(3!=ac,die("usage: as INPUT.ASM OUTPUT.ROM"))
|
|
label=&(Label){0,0,{0}};
|
|
hini(&label->c,64,SZ(Label)),hini(¯os,64,SZ(Macro)),vini(&refs,64,SZ(Ref));
|
|
WIF(f,av[1],"rb",finp(f))
|
|
WIF(f,av[2],"wb",fout(f))
|
|
printf("done: %d bytes wrote\n",mel-org);
|
|
R 0;}
|