1-
21// gcc -Os -s -nostdlib -static -Wl,--nmagic -Wl,--build-id=none -fno-asynchronous-unwind-tables k.c
32// (objcopy --remove-section .comment a.out b.out)
43
@@ -16,17 +15,19 @@ __attribute((naked))long swrite(long fd,char*b,uint n){asm("mov $1,%rax;syscall;
1615__attribute ((naked))void Exit(int x){asm (" mov $60,%rax;syscall" );}
1716
1817 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 ;}
19- static double F64floor (double x){long i=( long )(x <0 ?x -1 .:x); return ( long )i ;}
18+ static double F64floor (double x){double f=( double )( long )x; return x==f?f:x <0 ?f -1.0 :f ;}
2019__attribute ((naked))static double F64abs(double x){ asm (" xor %eax,%eax;dec %rax;shr %rax;movq %rax,%xmm1;andpd %xmm1,%xmm0;ret;" );}
2120__attribute ((naked))static double F64sqrt(double x){ asm (" sqrtsd %xmm0,%xmm0;ret" );}
2221__attribute ((naked))static double F64min(double x,double y){asm (" minsd %xmm1,%xmm0;ret;\n " );}
2322__attribute ((naked))static double F64max(double x,double y){asm (" maxsd %xmm1,%xmm0;ret;\n " );}
2423
24+ /*
2525static void wl(ulong x){ //debug-only
2626 char s[32];s[31]=10;s[30]='0';
2727 if(!x){swrite(1,s+30,2);return;}
2828 int i=31;while(x){s[--i]='0'+(x%10);x/=10;}
2929 swrite(1,s+i,32-i);}
30+ */
3031
3132static char *M_ ;
3233static ulong brk0;
@@ -43,52 +44,21 @@ static void Memory2(int x){}
4344// try/catch
4445static void *jb_[5 ];static int jb__=0 ;
4546#define setjmp __builtin_setjmp
46- #define longjmp __builtin_setlongjmp
47+ #define longjmp __builtin_longjmp
4748static 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- */
49+ static void store (void ){ulong n=pages_<<13 ;if (pages__){/* copy backwards*/ ulong o=((ulong)M_ -brk0)>>3 ;for (int i=0 ;i<n;i++)U_ [i-o]=U_ [i];M_ =(char *)brk0; }
50+ pages__=pages_;brk (brk0+(pages_<<17 ));for (int i=0 ;i<n;i++)U_ [i+n]=U_ [i];M_ =(char *)(brk0+(pages_<<16 ));}
51+ static void catch (void ){pages_=pages__;pages__=0 ;M_ =(char *)brk0;brk (brk0+(pages_<<16 ));}
52+ /* k repl does:
53+ main(){ store(); for(;;){ write(Ku(32)); ulong x=readfile(mk(Ct,0)); try(x); }}
54+ try(ulong x){ jb__=1;if(!setjmp(jb_)){ repl(x); store(); }else{catch(); }}
8455
85- /*
8656 brk0<--pages_-->brk
8757 M_...
8858 +--------------+ without try/catch M_ is always brk0, brk increases occasionally
8959
9060
91- brk0 M_ brk
61+ brk0 M_ brk
9262 +--------------+--------------+ store() initial call: double memory, copy forward set new M_
9363 +----------------^ copy forward left-to-right is ok
9464
@@ -102,22 +72,17 @@ static void try(ulong x){
10272 +------------------------+-----------------+ store() (no error) copy backwards, reset M_, shrink brk
10373 ^---------------------+ left-to-right
10474
105- brk0 pages__ M_ pages_ brk prepare for next try:
75+ brk0 pages__ M_ pages_ brk also in store() prepare for next try:
10676 +------------------------+---------------------------+ increase brk, copy forward
10777
10878
10979catch():
11080 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.
81+ M_ in case of errors:
82+ +------------------------+ reset pages_, M_ and brk, pages__=0
11783 */
11884
11985
120-
12186#define I8 (x ) (int )(M_ [x])
12287#define I32 (x ) (I_ [(x)>>2 ])
12388#define I64 (x ) (long )(U_ [(x)>>3 ])
0 commit comments