Skip to content

Commit 7f1a295

Browse files
committed
l8
1 parent 06921b8 commit 7f1a295

4 files changed

Lines changed: 21 additions & 53 deletions

File tree

.github/workflows/k.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ jobs:
7272
run: cd i && wg -tags simd5 -c . > kv.c && clang -O3 -Wall -mavx2 -Wfatal-errors kv.c -okv -lm && ./kv k.t -e
7373

7474
- name: l8.c
75-
run: cd i/x && k k.k cc.k -e 'x:`<cc``l8' > l8.c
76-
gcc -Os -s -nostdlib -static -Wl,--nmagic -Wl,--build-id=none -fno-asynchronous-unwind-tables -ol8_ l8.c
77-
objcopy --remove-section .comment l8_ l8
75+
run: |
76+
cd i/x && k k.k cc.k -e 'x:`<cc``l8' > l8.c
77+
gcc -Os -s -nostdlib -static -Wl,--nmagic -Wl,--build-id=none -fno-asynchronous-unwind-tables -ol8_ l8.c
78+
objcopy --remove-section .comment l8_ l8
79+
./l8 ../k.t -e
7880

7981
- name: delete release
8082
uses: dev-drprasad/delete-older-releases@v0.2.0

k.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ abs -3 0 2 /3 0 2
4444
abs'-3 0 2 /3 0 2
4545
abs 3 4a30 /3. 4.
4646
abs/3 4a30 /5.
47+
_2.0 -3.0 /2 -3
4748
"efg"+_1 2 3 /"fhj"
4849
"efg"-_1 2 3 /"ddd"
4950
1 2 3.+4 5 6 /5. 7. 9.

x/cc.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ccprg:{
4747
(runt;fpre;cons;vars),(,fncs),tabl,(ccfmt init),main}
4848
4949
ccfun:{
50-
$[(`main~S x)&`lib?opts; :""; 0]
50+
$[(`main~S x)&`lib?opts; :"";(`l8?opts)&+/`store`catch=S x; :"";0]
5151
stat:$[I x;"";"static "]
5252
args:", "/y@&`arg=T c:&x=P
5353
ret:$[#r:", "/y@&`res=T c;r;"void"]

x/l8.h

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
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+
/*
2525
static 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

3132
static char*M_;
3233
static ulong brk0;
@@ -43,52 +44,21 @@ static void Memory2(int x){}
4344
//try/catch
4445
static void*jb_[5];static int jb__=0;
4546
#define setjmp __builtin_setjmp
46-
#define longjmp __builtin_setlongjmp
47+
#define longjmp __builtin_longjmp
4748
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-
*/
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
10979
catch():
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

Comments
 (0)