summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--readme.md46
-rw-r--r--sanctuary.s69
2 files changed, 105 insertions, 10 deletions
diff --git a/readme.md b/readme.md
index 56a31fe..46112f3 100644
--- a/readme.md
+++ b/readme.md
@@ -13,14 +13,52 @@ for amd64 linux systems.
## Glossary
-the following is a list of words available in this forth. (neither of these have been implemented yet i'm just putting them here in the meantime lol)
+the following is a list of words available in this forth.
+
+### `#tib ( -- a )`
+variable containing the amount of characters in the input buffer.
+
+### `>in ( -- a )`
+variable containing the index of the first unparsed character
+in the input buffer.
+
+### `brk@ ( -- a )`
+yields current program break.
+
+### `bye ( -- )`
+exits the forth system.
### `dp ( -- a )`
-`dp` is a variable that contains the lowest free byte of memory in user memory.
+a variable that contains the lowest free byte of memory in user memory.
+
+### `dp0 ( -- )`
+a variable that contains the first byte of user memory.
+
+### `dp$ ( -- )`
+a variable that contains the last available byte of user memory.
+
+### `executable ( u a -- )`
+marks the u bytes starting at address a as executable.
+this is used primarily to mark the program break,
+which is used as the user memory space.
+
+### `grow ( u -- )`
+grows the user memory space by u bytes.
### `here ( -- a )`
-`here` yields the address of the first available byte
-in user memory.
+yields the address of the first available byte in user memory.
+
+### `latest ( -- a )`
+a variable containing the execution token of
+the most recently created word.
+
+### `state ( -- a )`
+a variable containing a boolean value.
+if 0 (false), the system is in interpreting mode,
+if -1 (true), the system is in compiling mode.
+
+### `tib ( -- a )`
+a variable containing the address of the current input buffer.
## dictionary format
diff --git a/sanctuary.s b/sanctuary.s
index 5859ac1..b511d68 100644
--- a/sanctuary.s
+++ b/sanctuary.s
@@ -1,8 +1,6 @@
; sanctuary
; macros {{{
-; in this forth pspush and pspop are only necessary if you need to
-; go deeper into the stack.
; TODO: error handling (once i add that)
%macro pspush 1
lea r14, [r14-8]
@@ -16,6 +14,10 @@
lea r14, [r14+8]
%endmacro
+%macro psdrop 0
+ lea r14, [r14+8]
+%endmacro
+
%define s_latest 0
%macro defdict 3 ; name label flags
@@ -37,14 +39,20 @@
; to how user variables are planned to work in sanctuary
; so todo make better later? i don't know if it really matters
; because it will only apply to builtin variables.
-%macro defvar 3
+%macro defvar 4
%2: dq %4
defdict %1, %2, %3
- ; pspush qword %2 ; todo
- %2:
+ pspush qword %2
%endmacro
; }}}
+%assign INTERPRET 0x0
+%assign COMPILING (~0x1)
+
+%assign __NR_mprotect 10
+%assign __NR_brk 12
+%assign __NR_exit 60
+
section .bss
resq 4091
wstk: resq 1
@@ -52,6 +60,55 @@ wstk: resq 1
section .text
global _start
_start:
+ lea r14, [wstk + 8]
+
+ call brk@
+ mov qword [dp], r15
+ mov qword [dp0], r15
+ mov r15, 0x9c400
+ call grow
+ call bye
+
+defcode "brk@", brk@, 0
+ xor rdi, rdi
+ mov rax, __NR_brk
+ syscall
+ pspush rax
+ ret
+
+defcode "grow", grow, 0
+ call brk@
+ pspop rdi
+ pspop r13
+ add rdi, r13
+ mov rax, __NR_brk
+ syscall
+ mov qword [dp$], rax
+ ret
+
+defcode "executable", executable, 0
+ mov rdx, 0x7 ; PROT_{READ,WRITE,EXEC}
+ pspop rdi ; addr
+ pspop rsi
+ mov rax, __NR_mprotect
+ syscall
+ ret
+
+defcode "here", here, 0
+ pspush qword [dp]
+ ret
+
+defcode "bye", bye, 0
mov rdi, 0
- mov rax, 60
+ mov rax, __NR_exit
syscall
+ ret
+
+defvar "state", state, 0, INTERPRET
+defvar "dp", dp, 0, 0
+defvar "dp0", dp0, 0, 0
+defvar "dp$", dp$, 0, 0
+defvar "tib", tib, 0, 0 ; todo set correct initial value
+defvar "#tib", n_tib, 0, 0 ; todo set correct initial value
+defvar ">in", to_in, 0, 0
+defvar "latest", latest, 0, lfa_latest