mirror of https://git.cro.wtf/kip.git
as:no need to write to file if no data specified
ie -org #0000 -org #2000 doesn't automatically set the rom filesize to #2000 anymore unless something is written after -org #2000
This commit is contained in:
parent
63e503662d
commit
4486db6b53
18
as.c
18
as.c
|
@ -8,7 +8,7 @@ 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;
|
||||
_ W org=0,mew,mel;
|
||||
_ Label*label;
|
||||
_ Ht macros;
|
||||
_ Vec refs;
|
||||
|
@ -39,16 +39,16 @@ _ 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,++*
|
|||
_ 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("emb",y=next(t),tasrt(y,'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",mew=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,mcpy(mel,x.p+1,x.l-=2),mew=mel+=x.l)OR
|
||||
Q('#'==c,B bs[tz(x)];tto4b(x,bs),mcpy(mel,bs,SZ(bs)),mew=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,++x.p,--x.l,rini(vput(&refs),0,x),mew=mel+=4)OR
|
||||
Q('^'==c,++x.p,--x.l,rini(vput(&refs),1,x),mew=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
|
||||
|
@ -56,7 +56,7 @@ _ V pass1(S*t){_ B o=0;S x=*t;B c;c=*x.p;
|
|||
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;}
|
||||
mb(mel++,o),mew=mel,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
|
||||
|
@ -64,12 +64,12 @@ _ V pass2(V){Label*l;Ws a;Bs b;Vfor(Ref,refs,r,i,Q(l=lget(r.p,r.n),
|
|||
_ 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);}
|
||||
_ V fout(F f){pass2();xw(f,meb,1,mew-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);
|
||||
printf("done: %d bytes wrote\n",mew-org);
|
||||
R 0;}
|
||||
|
|
Loading…
Reference in New Issue