diff options
| author | kitty <nepeta@canaglie.net> | 2026-03-05 02:00:18 +1100 |
|---|---|---|
| committer | kitty <nepeta@canaglie.net> | 2026-03-05 02:00:18 +1100 |
| commit | 12025926739a8b2b4c7a33d8eccd805cbce4d06a (patch) | |
| tree | 2a8853123980b89a0058263741acd944269761bb | |
| parent | 5b0a1bed59e817cad707d33018b8cb94b63088d7 (diff) | |
grow brk at runtime
i really need to figure out syscall error handling
| -rw-r--r-- | jefs.fs | 62 |
1 files changed, 42 insertions, 20 deletions
@@ -210,14 +210,51 @@ variable hld : .qword (.qword) say space ; \ }}} +\ SYSCALL ERRORS {{{ +: errno ( rax -- ?val err|0 ) dup 0< if negate else 0 then ; +: errno-flag ( rax -- err|0 ) dup 0< if negate else drop 0 then ; + +0 constant Enone +2 constant ENOENT +9 constant EBADF +13 constant EACCES +22 constant EINVAL + +: .errno ( err -- ) ?dup 0<> if case + Enone of endof + ENOENT of ." no such file or directory" endof + EBADF of ." bad file descriptor" endof + EACCES of ." permission denied" endof + EINVAL of ." invalid argument" endof + ." mystery error (spooky)" + endcase cr then ; +\ }}} + : ? @ . ; : .s sp cell+ ( skip sp itself ) begin dup s0 @ < while dup @ . cell+ repeat drop cr ; : .rs rp cell+ ( skip rsp itself ) begin dup rs0 @ < while dup @ . cell+ repeat drop cr ; +\ USER MEMORY {{{ +hex +1 constant PROT_READ +2 constant PROT_WRITE +4 constant PROT_EXEC +0 PROT_READ or PROT_WRITE or PROT_EXEC or constant PROT_rwx +decimal + +: mprotect 10 syscall3 ; +: sysbrk 12 syscall1 ; + : bytes-allocated heremax @ herestart @ - ; : bytes-used here @ herestart @ - ; : bytes-free bytes-allocated bytes-used - ; + +: brk@ 0 sysbrk ; +: mark-exec PROT_rwx bytes-allocated herestart @ mprotect .errno ; +: grow ( n -- ) brk@ + sysbrk heremax ! mark-exec ; \ todo check error + : .free bytes-free u. ." of " bytes-allocated u. ." bytes free (used " bytes-used (.) say ." )" cr ; +\ }}} : #bye ( code -- ) 60 syscall1 ; \ maybe this would be more elegant as a table? @@ -235,22 +272,6 @@ variable hld : (words) ( lfa -- ) >nfa dup w@ swap 2 + swap say 2 spaces ; : words latest @ begin ?dup 0<> while dup (words) @ repeat cr ; -\ SYSCALL ERRORS {{{ -: errno ( rax -- ?val err|0 ) dup 0< if negate else 0 then ; -: errno-flag ( rax -- err|0 ) dup 0< if negate else drop 0 then ; - -2 constant enoent -9 constant ebadf -13 constant eacces - -: .errno ( err -- ) ?dup 0<> if case - enoent of ." no such file or directory" endof - ebadf of ." bad file descriptor" endof - eacces of ." permission denied" endof - ." mystery error (spooky)" - endcase cr then ; -\ }}} - \ I/O {{{ 0 constant stdin 1 constant stdout @@ -290,7 +311,6 @@ make buffers-used #buffers cells allot make buffers->in #buffers cells allot make buffers-fd #buffers cells allot -\ todo separate handling of blank lines and EOF : cbuffer include-depth /buffer * buffers + ; : cbuffer-used include-depth cells buffers-used + ; : cbuffer->in include-depth cells buffers->in + ; @@ -305,10 +325,12 @@ stdin buffers-fd ! \ i just want something that Works right now 0 value (accept-n) 0 value (accept-a) -: accept ( a n -- n ) to (accept-n) to (accept-a) +\ todo this will not return a flag if the input buffer runs out +: accept ( a n -- n ? ) to (accept-n) to (accept-a) 0 begin dup (accept-n) < while buffer-key dup - 0>= if ( n c -- ) dup 10 = if drop dup to (accept-n) else over (accept-a) + c! 1+ then - else drop to (accept-n) then repeat ; + 0>= if ( n c -- ) dup 10 = if drop dup to (accept-n) true swap + else over (accept-a) + c! 1+ then + else drop to (accept-n) false swap then repeat swap ; \ i think i'll just take the wonkiness of \ 'if you use LOAD or something like that you lose the rest of that line' |
