kmm32(isa):add pr instruction

`pr x` (where x is a byte) puts ip+(int8)x onto the stack
eg. `pr #04 ju xx yy zz` skips bytes xx&yy and jumps to zz
         0  1  2  3  4
         ^
         +- relative to

this allows us to cut down by a full 3 bytes when referring
to labels that are closer than 128 bytes away, and allows
us to relocate less labels
(if we ever have relocatable binaries or something)
This commit is contained in:
Kitty-Cricket Piapiac 2023-03-31 14:03:34 -07:00
parent db8e763cb0
commit 7d8fc24c8f
4 changed files with 10 additions and 6 deletions

6
as.c
View File

@ -1,7 +1,7 @@
#include"u.h"
#define BE(y,...)Q(seql(Sl(y),x),__VA_ARGS__)OR
#define MEMSZ 0x40000
#define OPSZ 36
#define OPSZ 37
typedef struct l{W a;struct l*p;Ht c;}Label;
typedef struct{Vec ws;}Macro;
@ -19,10 +19,10 @@ _ 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,S n){*r=(Ref){n,mel,label};}
_ V lini(Label*l){l->a=mel,l->p=label,hini(&l->c,2,SZ(Label)),label=l;}
_ 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]="npexpbphpwfbfhfwmbmhmwioiissdrswduovadsumudianorxrslsrsaeqltgtnojujccacc";
_ 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;}

View File

@ -56,6 +56,7 @@ OPCODES
pb (--n) puts the next byte (8 bits) in memory onto the stack
ph (--n) ^ same but next half-word (16 bits)
pw (--n) ^ same but next word (32 bits)
pr (--n) puts ip+the next byte plus onto the stack
fb (a--n) fetches a byte from address `a and puts it onto the stack
fh (a--n) ^ same but with half-word
fw (a--n) ^ same but with word

View File

@ -27,6 +27,7 @@ _ V ex(V){ip+=0x40001;}
_ 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()));}
@ -60,9 +61,9 @@ _ V ca(V){pur(ip),ip=pop();}
_ V cc(V){AB;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*/
/*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*/
{np,ex,pb,ph,pw,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};
/*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};
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 lod(B*c,W l){for(ip=0;ip<l;++ip)mem[ip+0x100]=c[ip];ip=0x100;}

View File

@ -7,7 +7,9 @@
typedef void V;
typedef uint8_t B;
typedef int8_t Bs;
typedef uint16_t H;
typedef int16_t Hs;
typedef uint32_t W;
typedef int32_t Ws;
typedef struct{V(*io)(W);W(*oi)(V);} IoDevice;