3 format ELF64 executable
7 ;; [NOTE] Volatile registers Linux (syscalls) vs UEFI
9 ;; Linux syscalls: RAX, RCX, R11
10 ;; UEFI: RAX, RCX, R11, RDX, R8, R9, R10
12 ;; We are in the process of replacing our dependency on Linux with a dependency
13 ;; on UEFI. The following macros attempt to isolate what would be syscalls in
14 ;; Linux; thus, we will be able to replace these with UEFI-based implementations,
15 ;; and in theory we should expect the program to work.
17 ;; Print a string of a given length.
20 ;; - RCX = Pointer to buffer
21 ;; - RDX = Buffer length
23 ;; Clobbers: RAX, RCX, R11, RDI, RSI
24 macro sys_print_string {
33 ;; The code in this macro is placed at the end of each Forth word. When we are
34 ;; executing a definition, this code is what causes execution to resume at the
35 ;; next word in that definition.
37 ;; RSI points to the address of the definition of the next word to execute.
38 lodsq ; Load value at RSI into RAX and increment RSI
39 ;; Now RAX contains the location of the next word to execute. The first 8
40 ;; bytes of this word is the address of the codeword, which is what we want
42 jmp qword [rax] ; Jump to the codeword of the current word
45 ;; pushr and popr work on the return stack, whose location is stored in the
56 ;; The following macro generates the dictionary header. It updates the
57 ;; initial_latest_entry variable, which is used as the initial value of the
58 ;; latest_entry variable that is made available at runtime.
60 ;; The header contains a link to the previous entry, the length of the name of
61 ;; the word and the word itself as a string literal.
63 ;; This macro also defines a label LABEL_entry.
64 initial_latest_entry = 0
65 macro header label, name, immediate {
69 dq initial_latest_entry
75 db .string_end - ($ + 1)
80 initial_latest_entry = label#_entry
83 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
84 macro forth_asm label, name, immediate {
85 header label, name, immediate
90 ;; Define a Forth word that is implemented in Forth. (The body will be a list of
92 macro forth label, name, immediate {
93 header label, name, immediate
97 segment readable executable
104 cld ; Clear direction flag so LODSQ does the right thing.
105 mov rbp, return_stack_top ; Initialize return stack
112 ;; The codeword is the code that will be executed at the beginning of a forth
113 ;; word. It needs to save the old RSI and update it to point to the next word to
115 header DOCOL, 'DOCOL'
116 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
117 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
118 next ; Execute word pointed to by RSI
120 ;; This word is called at the end of a Forth definition. It just needs to
121 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
122 forth_asm EXIT, 'EXIT'
126 ;; LIT is a special word that reads the next "word pointer" and causes it to be
127 ;; placed on the stack rather than executed.
133 ;; Given a string (a pointer following by a size), return the location of the
134 ;; dictionary entry for that word. If no such word exists, return 0.
135 forth_asm FIND, 'FIND'
138 pop [find.search_length]
139 pop [find.search_buffer]
140 mov rsi, [latest_entry] ; Start with the last added word
151 ;; Given an entry in the dictionary, return a pointer to the codeword of that
153 forth_asm TCFA, '>CFA'
155 add rax, 8 + 1 ; [rax] = length of name
156 movzx rbx, byte [rax]
158 add rax, rbx ; [rax] = codeword
162 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
163 ;; as a signed integer literal and jumps by that offset.
164 forth_asm BRANCH, 'BRANCH'
165 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
166 next ; Then, we can just continue execution as normal
168 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
169 forth_asm ZBRANCH, '0BRANCH'
170 ;; Compare top of stack to see if we should branch
177 add rsi, 8 ; We need to skip over the next word, which contains the offset.
180 ;; Duplicate the top of the stack.
181 forth_asm DUP_, 'DUP'
185 ;; Execute the codeword at the given address.
186 forth_asm EXEC, 'EXEC'
190 ;; Expects a character on the stack and prints it to standard output.
191 forth_asm EMIT, 'EMIT'
204 ;; Prints a newline to standard output.
205 forth NEWLINE, 'NEWLINE'
210 ;; Prints a space to standard output.
216 ;; Read a word from standard input and push it onto the stack as a pointer and a
217 ;; size. The pointer is valid until the next call to READ_WORD.
218 forth_asm READ_WORD, 'READ-WORD'
228 ;; Takes a string on the stack and replaces it with the decimal number that the
229 ;; string represents.
230 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
232 pop rdi ; String pointer
241 forth READ_NUMBER, 'READ-NUMBER'
246 ;; Takes a string (in the form of a pointer and a length on the stack) and
247 ;; prints it to standard output.
248 forth_asm TELL, 'TELL'
260 ;; Exit the program cleanly.
261 forth_asm TERMINATE, 'TERMINATE'
266 ;; Duplicate a pair of elements.
267 forth_asm PAIRDUP, '2DUP'
276 ;; Swap the top two elements on the stack.
277 forth_asm SWAP, 'SWAP'
284 ;; Remove the top element from the stack.
285 forth_asm DROP, 'DROP'
289 forth_asm NOT_, 'NOT'
300 ;; The INTERPRET word reads and interprets user input. It's behavior depends on
301 ;; the current STATE. It provides special handling for integers.
302 forth INTERPRET, 'INTERPRET'
306 ;; Stack is (word length word length).
307 dq FIND ; Try to find word
309 dq ZBRANCH, 8 * 22 ; Check if word is found
313 dq STATE, GET, ZBRANCH, 8 * 11 ; Check whether we are in compilation or immediate mode
315 ;; (Word found, compilation mode)
316 dq DUP_, IS_IMMEDIATE, NOT_, ZBRANCH, 8 * 6 ; If the word is immediate, continue as we would in immediate mode
318 ;; Otherwise, we want to compile this word
324 ;; (Word found, immediate mode)
327 ;; Stack is (word length addr)
334 ;; - No word is found, assume it is an integer literal -
335 ;; Stack is (word length addr)
339 dq STATE, GET, ZBRANCH, 8 * 5 ; Check whether we are in compilation or immediate mode
341 ;; (Number, compilation mode)
346 ;; (Number, immediate mode)
349 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
352 mov [.printed_length], 1
353 pop rax ; RAX = value to print
354 push rsi ; Save value of RSI
356 ;; We start by constructing the buffer to print in reverse
361 div rbx ; Put remainer in RDX and quotient in RAX
363 ;; Place the appropriate character in the buffer
372 ;; .printed_length is the number of characters that we ulitmately want to
373 ;; print. If we have printed a non-zero character, then we should update
376 je .skip_updating_real_length
378 mov [.printed_length], rbx
379 .skip_updating_real_length:
384 ;; Flip buffer around, since it is currently reversed
385 mov rcx, [.printed_length]
393 add rdi, [.printed_length]
401 mov rdx, [.printed_length]
404 ;; Restore RSI and continue execution
408 ;; Takes a value and an address, and stores the value at the given address.
415 ;; Takes an address and returns the value at the given address.
422 forth_asm PUT_BYTE, 'C!'
428 forth_asm GET_BYTE, 'C@'
430 movzx rax, byte [rax]
434 ;; Add two integers on the stack.
442 ;; Calculate difference between two integers on the stack. The second number is
443 ;; subtracted from the first.
451 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
452 ;; division of a by b.
453 forth_asm TIMESMOD, '/MOD'
462 ;; Get the location of the STATE variable. It can be set with '!' and read with
468 ;; Get the location of the LATEST variable. It can be set with '!' and read with
470 forth LATEST, 'LATEST'
474 ;; Get the location at which compiled words are expected to be added. This
475 ;; pointer is usually modified automatically when calling ',', but we can also
476 ;; read it manually with 'HERE'.
482 dq HERE, GET, PUT ; Set the memory at the address pointed to by HERE
483 dq HERE, GET, LIT, 8, PLUS ; Calculate new address for HERE to point to
484 dq HERE, PUT ; Update HERE to point to the new address
487 ;; Read user input until next " character is found. Push a string containing the
488 ;; input on the stack as (buffer length). Note that the buffer is only valid
489 ;; until the next call to S" and that no more than 255 character can be read.
490 forth_asm READ_STRING, 'S"'
498 mov rsi, .char_buffer
502 mov al, [.char_buffer]
520 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
521 ;; points to the header. To compile a word, the user can then call ',' to
522 ;; continue to append data after the header.
524 ;; It takes the name of the word as a string (address length) on the stack.
525 forth_asm CREATE, 'CREATE'
526 pop rcx ; Word string length
527 pop rdx ; Word string pointer
529 mov rdi, [here] ; rdi = Address at which to insert this entry
530 mov rax, [latest_entry] ; rax = Address of the previous entry
531 mov [rdi], rax ; Insert link to previous entry
532 mov [latest_entry], rdi ; Update LATEST to point to this word
535 mov [rdi], byte 0 ; Insert immediate flag
538 mov [rdi], byte cl ; Insert length
540 ;; Insert word string
544 mov rsi, rdx ; rsi = Word string pointer
553 ;; Mark the last added word as immediate.
554 forth IMMEDIATE, 'IMMEDIATE', 1
561 ;; Given the address of a word, return 0 if the given word is not immediate.
562 forth IS_IMMEDIATE, 'IMMEDIATE?'
567 ;; Enter immediate mode, immediately
568 forth INTO_IMMEDIATE, '[', 1
569 dq LIT, 0, STATE, PUT_BYTE
572 ;; Enter compilation mode
573 forth OUTOF_IMMEDIATE, ']'
574 dq LIT, 1, STATE, PUT_BYTE
591 forth_asm PICK, 'PICK'
593 lea rax, [rsp + 8 * rax]
615 segment readable writable
617 ;; The LATEST variable holds a pointer to the word that was last added to the
618 ;; dictionary. This pointer is updated as new words are added, and its value is
619 ;; used by FIND to look up words.
620 latest_entry dq initial_latest_entry
622 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
631 READ_STRING.char_buffer db ?
632 READ_STRING.buffer rb $FF
633 READ_STRING.length dq ?
635 DOTU.chars db '0123456789ABCDEF'
636 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
639 DOTU.printed_length dq ?
641 ;; Reserve space for compiled words, accessed through HERE.