kmm32(isa):keep flag

as:$ keep flag

keep flag, whether to decrement the stack pointers for
the arguments used in the instruction
e.g. (a--n) --> (a--a n)

this is useful in situations where we want to keep the
arguments on the stack to do something else with, e.g.
a lot of the times we'll do `du fb` or something. now
we can save bytes and just do `$fb`. yippee!
This commit is contained in:
Kitty-Cricket Piapiac 2023-04-04 00:43:28 -07:00
parent 654eed58d5
commit 62453d6377
4 changed files with 46 additions and 34 deletions

1
as.c
View File

@ -54,6 +54,7 @@ _ V pass1(S*t){_ B o=0;S x=*t;B c;c=*x.p;
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),

View File

@ -47,7 +47,8 @@ INSTRUCTIONS
||O: Opcode
|F: Flip return and data stacks
| (eg 01000010 will put the next byte in memory onto the return stack)
U: Unused
K: Keep left hand operands
(eg (a--n) below becomes (a--a n))
OPCODES

View File

@ -42,6 +42,7 @@ SYNTAX
resulting opcode:
~ - F flag (flip stack manipulations from data <-> return stacks)
$ - K flag (keep lhs on rhs)
BUILTINS
Colour of arguments denoted with first character.

75
kmm32.c
View File

@ -4,11 +4,12 @@
#define DS ((W*)(mem+(MEMSZ-0x800)))
#define RS ((W*)(mem+(MEMSZ-0x400)))
#define SD (RS-DS)
#include<stdio.h>
B mem[MEMSZ];
W*dp=DS,*rp=RS,ip;
_ W pop(V){R*--dp;}
//_ W pop(V){R*--dp;}
_ V put(W i){*dp++=i;}
_ V pur(W i){*rp++=i;}
_ V swp(V){W*t;t=dp,dp=rp,rp=t;}
@ -28,42 +29,50 @@ _ V pb(V){put(lob(ip)),ip+=1;}
_ V ph(V){put(loh(ip)),ip+=2;}
_ V pw(V){put(low(ip)),ip+=4;}
_ V pr(V){put(ip+(Bs)lob(ip)),ip+=1;}
_ V fb(V){put(lob(pop()));}
_ V fh(V){put(loh(pop()));}
_ V fw(V){put(low(pop()));}
_ V mb(V){AB,stb(b,a);}
_ V mh(V){AB,sth(b,a);}
_ V mw(V){AB,stw(b,a);}
_ V io(V){AB,ios[b].io(a);}
_ V ii(V){put(ios[pop()].oi());}
_ V ss(V){pur(pop());}
_ V dr(V){--dp;}
_ V sw(V){AB,put(b),put(a);}
_ V du(V){put(*(dp-1));}
_ V ov(V){put(*(dp-2));}
_ V ad(V){AB,put(a+b);}
_ V su(V){AB,put(a-b);}
_ V mu(V){AB,put(a*b);}
_ V di(V){AB,put(a/b),put(a%b);}
_ V an(V){AB,put(a&b);}
_ V or(V){AB,put(a|b);}
_ V xr(V){AB,put(a^b);}
_ V sl(V){AB,put(a<<b);}
_ V sr(V){AB,put(a>>b);}
_ V sa(V){AB,put((Ws)a>>b);}
_ V eq(V){AB,put(a==b);}
_ V lt(V){AB,put(a<b);}
_ V gt(V){AB,put(a>b);}
_ V no(V){put(!pop());}
_ V ju(V){ip=pop();}
_ V jc(V){AB;Q(a,ip=b)}
_ V ca(V){pur(ip),ip=pop();}
_ V cc(V){AB;Q(a,pur(ip),ip=b)}
_ V fb(V){put(lob(a));}
_ V fh(V){put(loh(a));}
_ V fw(V){put(low(a));}
_ V mb(V){stb(b,a);}
_ V mh(V){sth(b,a);}
_ V mw(V){stw(b,a);}
_ V io(V){ios[b].io(a);}
_ V ii(V){put(ios[a].oi());}
_ V ss(V){pur(a);}
_ V dr(V){(V)a;}
_ V sw(V){put(b),put(a);}
_ V du(V){put(a),put(a);}
_ V ov(V){put(a),put(b),put(a);}
_ V ad(V){put(a+b);}
_ V su(V){put(a-b);}
_ V mu(V){put(a*b);}
_ V di(V){put(a/b),put(a%b);}
_ V an(V){put(a&b);}
_ V or(V){put(a|b);}
_ V xr(V){put(a^b);}
_ V sl(V){put(a<<b);}
_ V sr(V){put(a>>b);}
_ V sa(V){put((Ws)a>>b);}
_ V eq(V){put(a==b);}
_ V lt(V){put(a<b);}
_ V gt(V){put(a>b);}
_ V no(V){put(!a);}
_ V ju(V){ip=a;}
_ V jc(V){Q(a,ip=b)}
_ V ca(V){pur(ip),ip=a;}
_ V cc(V){Q(a,pur(ip),ip=b)}
_ V(*ops[])(V)=
/*0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24*/
/*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36*/
{np,ex,pb,ph,pw,pr,fb,fh,fw,mb,mh,mw,io,ii,ss,dr,sw,du,ov,ad,su,mu,di,an,or,xr,sl,sr,sa,eq,lt,gt,no,ju,jc,ca,cc};
_ B arity[]=
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2};
V emu(V){B m,f;while(ip<0x40000){Q((f=(m=mem[ip++])>>6)&1,swp());ops[0x3f&m]();Q(f&1,swp());}}
_ V op(B n){B k,f,c,o;o=n&0x3f,f=n>>6&1,k=n>>7,c=arity[o];
Q(f,swp())
Q(0==c, ops[o]())OR
Q(1==c,a=dp[-1], dp-= !k,ops[o]())OR
Q(2==c,a=dp[-2],b=dp[-1],dp-=2*!k,ops[o]())
Q(f,swp())}
V emu(V){while(ip<0x40000){op(mem[ip++]);}}
V lod(B*c,W l){for(ip=0;ip<l;++ip)mem[ip+0x100]=c[ip];ip=0x100;}