as:dont cd,add -inc

This commit is contained in:
Kitty-Cricket Piapiac 2023-03-21 14:06:31 -07:00
parent 9b85fb2c56
commit f4b062cfc6
1 changed files with 10 additions and 9 deletions

19
as.c
View File

@ -1,8 +1,8 @@
#include"u.h"
#define MEMSZ 0x40000
_ B*inb,meb[MEMSZ];
_ W inl,mel;
_ B meb[MEMSZ];
_ W mel;
V mb(W i,B b){Q(i>=MEMSZ||i<0x100,die("memory overflow, or tried to write to IVT {ip=%x}",i))meb[i]=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);}
@ -34,10 +34,12 @@ Ht macros;
Vec refs;
#define BE(y,...)Q(seql(Sl(y),x),__VA_ARGS__)OR
_ V frun(F);
_ V builtin(S*t){FILE*f;S y,x=*t;++x.p,--x.l;
BE("org",tasrt(next(t),'#'),mel=tton(*t))
BE("res",tasrt(next(t),'#'),mel+=tton(*t))
BE("emb",tasrt(y=next(t),'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",mel+=fread(meb+mel,1,(W)0-1,f)))
BE("inc",tasrt(y=next(t),'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",frun(f)))
tw(x),die(" unknown builtin");}
_ V pass1(S*t){S x=*t;B c,o;c=*x.p;
@ -55,15 +57,14 @@ _ V pass1(S*t){S x=*t;B c,o;c=*x.p;
_ V pass2(V){Label*l;
Vfor(Ref,refs,r,i,Q(l=Hget(Label,labels,r.n),mw(r.a,l->a))OR tw(r.n),die(" unknown ref"))}
_ V run(V){S t={0,inb};
hini(&labels,64,SZ(Label)),hini(&macros,64,SZ(Macro)),vini(&refs,64,SZ(Ref));
WH(0!=*next(&t).p,pass1(&t));
pass2();}
_ V frun(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),fread(in,1,l,f),in[l]=0,t.p=in;
WH(0!=*next(&t).p,pass1(&t));pass2();}
I main(I ac,C**av){FILE *f;
Q(3!=ac,die("usage: as INPUT.ASM OUTPUT.ROM"))
/*todo:this is undefined behaviour for binary streams (SEEK_END)*/
WIF(f,av[1],"rb",fseek(f,0,SEEK_END),inl=ftell(f),fseek(f,0,SEEK_SET),inb=mk(inl+1),fread(inb,1,inl,f))
WIF(f,av[2],"wb",xcd(dirname(av[1])),inb[inl]=0,run(),del(inb),fwrite(meb+0x100,1,mel-0x100,f))
hini(&labels,64,SZ(Label)),hini(&macros,64,SZ(Macro)),vini(&refs,64,SZ(Ref));
WIF(f,av[1],"rb",frun(f);)
WIF(f,av[2],"wb",fwrite(meb+0x100,1,mel-0x100,f))
printf("done: %d bytes wrote\n",mel-0x100);
R 0;}