Skip to content

Commit 06921b8

Browse files
committed
l8: setjmp
1 parent 2fc76a1 commit 06921b8

1 file changed

Lines changed: 80 additions & 14 deletions

File tree

x/l8.h

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ __attribute((naked))long sread( long fd,char*b,uint n){asm("xor %rax,%rax;syscal
1515
__attribute((naked))long swrite(long fd,char*b,uint n){asm("mov $1,%rax;syscall;ret");}
1616
__attribute((naked))void Exit(int x){asm("mov $60,%rax;syscall");}
1717

18-
//uint csr=0xbfc0; // ldmxcsr(&csr); (must be a memarg)
19-
//__attribute((naked))void ldmxcsr(uint*x){asm("ldmxcsr (%rdi);ret");}
2018
static double F64copysign(double x,double y){union{double f;ulong i;}ux={x},uy={y};ux.i&=-1ul/2;ux.i|=uy.i&1ul<<63;return ux.f;}
2119
static double F64floor(double x){long i=(long)(x<0?x-1.:x);return(long)i;}
2220
__attribute((naked))static double F64abs(double x){ asm("xor %eax,%eax;dec %rax;shr %rax;movq %rax,%xmm1;andpd %xmm1,%xmm0;ret;");}
@@ -31,23 +29,93 @@ static void wl(ulong x){ //debug-only
3129
swrite(1,s+i,32-i);}
3230

3331
static char*M_;
32+
static ulong brk0;
3433
#define I_ ((int*)M_)
3534
#define U_ ((ulong*)M_)
3635
static void*F_[]; //dispatch table
3736
#define int32_t int //some literals
38-
static ulong pages_=0;
37+
static ulong pages_=0,pages__=0; //a page is 64k
3938
static int Memorysize(void){return pages_;}
40-
static int Memorygrow(int d){pages_+=d;if(pages_>16384)return -1;brk((pages_<<16)+(ulong)M_);return pages_-d;}
41-
static void Memory(int x){M_=(char*)brk(0);Memorygrow(x);}
39+
static int Memorygrow(int d){pages_+=d;if(pages_>16384)return -1;brk(brk0+(pages_<<16));return pages_-d;}
40+
static void Memory(int x){brk0=brk(0);M_=(char*)brk0;Memorygrow(x);}
4241
static void Memory2(int x){}
4342

44-
//todo try/catch
45-
static int jb_,jb__;
46-
static int setjmp(int x){return 0;}
47-
static int Memorycopy2(int x,int y,int z){return 0;}
48-
static int Memorycopy3(int x,int y,int z){return 0;}
49-
static int Memorysize2(void){return 2;}
50-
static int Memorygrow2(int x){return 2;}
43+
//try/catch
44+
static void*jb_[5];static int jb__=0;
45+
#define setjmp __builtin_setjmp
46+
#define longjmp __builtin_setlongjmp
47+
static void panic(int x){if(!jb__)Exit(1);longjmp(jb_,1); }
48+
static int Memorysize2(void){return 0; }
49+
static void Memorygrow2(int x){}
50+
static int Memorycopy2(void/*int x,int y,int z*/){if(pages__){/*copy backwards*/ ulong o=(ulong)M_-brk0;for(int i=0;i<n;i++)U_[i-o]=U[i];M_=(char*)brk0; }
51+
pages__=pages_;brk(brk0+(pages_<<17));ulong n=pages_<<13;for(int i=0;i<n;i++)U_[i+n]=U_[i];M_=brk0+(pages_<<16);return 0;}
52+
static int Memorycopy3(int x,int y,int z){pages_=pages__;pages__=0;M_=(char*)brk0;brk(brk0+(pages_<<16));return 0;}
53+
/*
54+
void main_(void){
55+
ulong x2;
56+
kinit();
57+
doargs();
58+
write(Ku(0x000a6b2f6579746bull));
59+
store();
60+
for(;;){
61+
write(Ku(32ull));
62+
x2=readfile(mk(Ct,0));
63+
try(x2);
64+
}
65+
}
66+
static void store(void){ //we ignore everything, just use the call to Memorycopy2
67+
int g;
68+
g=((1<<(I32(128)-16))-Memorysize2());
69+
if(g>0){
70+
Memorygrow2(g);
71+
}
72+
Memorycopy2(0,0,((int)(1)<<I32(128)));
73+
}
74+
static void catch(void){ //catch is Memorysize2, the size calculation is ignored
75+
Memorycopy3(0,0,((int)(65536)*Memorysize2()));
76+
}
77+
static void try(ulong x){
78+
jb__=1;if(!setjmp(jb_)){;
79+
repl(x);
80+
store();
81+
}else{catch();}
82+
}
83+
*/
84+
85+
/*
86+
brk0<--pages_-->brk
87+
M_...
88+
+--------------+ without try/catch M_ is always brk0, brk increases occasionally
89+
90+
91+
brk0 M_ brk
92+
+--------------+--------------+ store() initial call: double memory, copy forward set new M_
93+
+----------------^ copy forward left-to-right is ok
94+
95+
brk0 pages__ pages_ brk
96+
+--------------+---------------------------+ during execution (try) active k increases memory (pages_>pages__)
97+
old k mem active k mem
98+
99+
100+
brk0 pages__
101+
M_ brk
102+
+------------------------+-----------------+ store() (no error) copy backwards, reset M_, shrink brk
103+
^---------------------+ left-to-right
104+
105+
brk0 pages__ M_ pages_ brk prepare for next try:
106+
+------------------------+---------------------------+ increase brk, copy forward
107+
108+
109+
catch():
110+
brk0 pages_ brk
111+
M_ in case of errors: reset
112+
+------------------------+ Memorycopy3 does not copy anything but resets pages_, M_ and brk, pages__=0
113+
114+
115+
in k.c, store() calls Memorycopy2 (calls to Memorysize2/Memorygrow2 are ignored)
116+
catch() calls Memorycopy3, args are ignored.
117+
*/
118+
51119

52120

53121
#define I8(x) (int)(M_[x])
@@ -77,5 +145,3 @@ static int Write(int f,int nf,int s,int n){if(!nf){swrite(1,M_+s,n);return 0;}
77145
char c=M_[f+nf];M_[f+nf]=0;int fd=open(M_+f,577,420);M_[f+nf]=c;if(fd<0)return -1;swrite(fd,M_+s,n);close(fd);return 0;}
78146
static int ReadIn(int d,int n){return(int)sread(1,M_+d,n);}
79147
static long Native(long x,long y){return(x+y)*0;}
80-
static void panic(int x){Exit(1);}
81-

0 commit comments

Comments
 (0)