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 {
29 call uefi_print_string
36 ;; Read a character from the user into the given buffer.
39 ;; - RSI = Character buffer
42 ;; - BYTE [RSI] = Character
44 ;; Clobbers: RAX, RCX, R11, RDI, RSI, RDX
62 macro sys_terminate code {
69 ;; The code in this macro is placed at the end of each Forth word. When we are
70 ;; executing a definition, this code is what causes execution to resume at the
71 ;; next word in that definition.
73 ;; RSI points to the address of the definition of the next word to execute.
74 lodsq ; Load value at RSI into RAX and increment RSI
75 ;; Now RAX contains the location of the next word to execute. The first 8
76 ;; bytes of this word is the address of the codeword, which is what we want
78 jmp qword [rax] ; Jump to the codeword of the current word
81 ;; pushr and popr work on the return stack, whose location is stored in the
92 ;; The following macro generates the dictionary header. It updates the
93 ;; initial_latest_entry variable, which is used as the initial value of the
94 ;; latest_entry variable that is made available at runtime.
96 ;; The header contains a link to the previous entry, the length of the name of
97 ;; the word and the word itself as a string literal.
99 ;; This macro also defines a label LABEL_entry.
100 initial_latest_entry = 0
101 macro header label, name, immediate {
105 dq initial_latest_entry
111 db .string_end - ($ + 1)
116 initial_latest_entry = label#_entry
119 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
120 macro forth_asm label, name, immediate {
121 header label, name, immediate
126 section '.text' code readable executable
128 include "impl.asm" ; Misc. subroutines
129 include "bootstrap.asm" ; Forth words encoded in Assembly
132 cld ; Clear direction flag so LODSQ does the right thing.
133 mov rbp, return_stack_top ; Initialize return stack
142 ;; The codeword is the code that will be executed at the beginning of a forth
143 ;; word. It needs to save the old RSI and update it to point to the next word to
145 header DOCOL, 'DOCOL'
146 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
147 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
148 next ; Execute word pointed to by RSI
150 ;; This word is called at the end of a Forth definition. It just needs to
151 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
152 forth_asm EXIT, 'EXIT'
156 ;; LIT is a special word that reads the next "word pointer" and causes it to be
157 ;; placed on the stack rather than executed.
163 ;; When LITSTRING is encountered while executing a word, it instead reads a
164 ;; string from the definition of that word, and places that string on the stack
165 ;; as (buffer, length).
166 forth_asm LITSTRING, 'LITSTRING'
171 add rsi, rax ; Skip over string before resuming execution
174 ;; Given a string (a pointer following by a size), return the location of the
175 ;; dictionary entry for that word. If no such word exists, return 0.
176 forth_asm FIND, 'FIND'
179 pop [find.search_length]
180 pop [find.search_buffer]
181 mov rsi, [latest_entry] ; Start with the last added word
192 ;; Given an entry in the dictionary, return a pointer to the codeword of that
194 forth_asm TCFA, '>CFA'
196 add rax, 8 + 1 ; [rax] = length of name
197 movzx rbx, byte [rax]
199 add rax, rbx ; [rax] = codeword
203 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
204 ;; as a signed integer literal and jumps by that offset.
205 forth_asm BRANCH, 'BRANCH'
206 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
207 next ; Then, we can just continue execution as normal
209 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
210 forth_asm ZBRANCH, '0BRANCH'
211 ;; Compare top of stack to see if we should branch
218 add rsi, 8 ; We need to skip over the next word, which contains the offset.
221 ;; Duplicate the top of the stack.
222 forth_asm DUP_, 'DUP'
226 ;; Execute the codeword at the given address.
227 forth_asm EXEC, 'EXEC'
231 ;; Expects a character on the stack and prints it to standard output.
232 forth_asm EMIT, 'EMIT'
245 ;; Read a single character from the current input stream. Usually, this will wait
246 ;; for the user to press a key, and then return the corresponding character. When
247 ;; reading from a special buffer, it will instead return the next characater from
250 ;; The ASCII character code is placed on the stack.
258 ;; Are we reading from user input or from the input buffer?
259 cmp [input_buffer], 0
262 ;; Reading user input
268 movzx rax, byte [.buffer]
272 ;; Reading from buffer
273 mov rax, [input_buffer]
274 movzx rax, byte [rax]
277 dec [input_buffer_length]
280 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
281 ;; is valid until the next call to READ_WORD.
282 forth_asm READ_WORD, 'READ-WORD'
285 ;; Read characters until one of them is not whitespace.
287 ;; We consider newlines and spaces to be whitespace.
293 ;; We got a character that wasn't whitespace. Now read the actual word.
318 ;; Takes a string on the stack and replaces it with the decimal number that the
319 ;; string represents.
320 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
322 pop rdi ; String pointer
331 ;; Takes a string (in the form of a pointer and a length on the stack) and
332 ;; prints it to standard output.
333 forth_asm TELL, 'TELL'
345 ;; Exit the program cleanly.
346 forth_asm TERMINATE, 'TERMINATE'
349 ;; Duplicate a pair of elements.
350 forth_asm PAIRDUP, '2DUP'
359 ;; Swap the top two elements on the stack.
360 forth_asm SWAP, 'SWAP'
367 ;; Remove the top element from the stack.
368 forth_asm DROP, 'DROP'
372 forth_asm NOT_, 'NOT'
383 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
386 mov [.printed_length], 1
387 pop rax ; RAX = value to print
388 push rsi ; Save value of RSI
390 ;; We start by constructing the buffer to print in reverse
395 div rbx ; Put remainer in RDX and quotient in RAX
397 ;; Place the appropriate character in the buffer
406 ;; .printed_length is the number of characters that we ulitmately want to
407 ;; print. If we have printed a non-zero character, then we should update
410 je .skip_updating_real_length
412 mov [.printed_length], rbx
413 .skip_updating_real_length:
418 ;; Flip buffer around, since it is currently reversed
419 mov rcx, [.printed_length]
427 add rdi, [.printed_length]
435 mov rdx, [.printed_length]
438 ;; Restore RSI and continue execution
442 ;; Takes a value and an address, and stores the value at the given address.
449 ;; Takes an address and returns the value at the given address.
456 forth_asm PUT_BYTE, 'C!'
462 forth_asm GET_BYTE, 'C@'
464 movzx rax, byte [rax]
468 ;; Add two integers on the stack.
476 ;; Calculate difference between two integers on the stack. The second number is
477 ;; subtracted from the first.
485 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
486 ;; division of a by b.
487 forth_asm TIMESMOD, '/MOD'
496 ;; Read input until next " character is found. Push a string containing the
497 ;; input on the stack as (buffer length). Note that the buffer is only valid
498 ;; until the next call to S" and that no more than 255 characters can be read.
499 forth_asm READ_STRING, 'S"'
500 ;; If the input buffer is set, we should read from there instead.
501 cmp [input_buffer], 0
502 jne read_string_buffer
509 mov rsi, .char_buffer
512 mov al, [.char_buffer]
533 ;; We borrow READ_STRING's buffer. They won't mind.
534 mov [READ_STRING.length], 0
537 mov rbx, [input_buffer]
542 mov rdx, READ_STRING.buffer
543 add rdx, [READ_STRING.length]
545 inc [READ_STRING.length]
548 dec [input_buffer_length]
557 dec [input_buffer_length]
559 push READ_STRING.buffer
560 push [READ_STRING.length]
564 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
565 ;; points to the header. To compile a word, the user can then call ',' to
566 ;; continue to append data after the header.
568 ;; It takes the name of the word as a string (address length) on the stack.
569 forth_asm CREATE, 'CREATE'
570 pop rcx ; Word string length
571 pop rdx ; Word string pointer
573 mov rdi, [here] ; rdi = Address at which to insert this entry
574 mov rax, [latest_entry] ; rax = Address of the previous entry
575 mov [rdi], rax ; Insert link to previous entry
576 mov [latest_entry], rdi ; Update LATEST to point to this word
579 mov [rdi], byte 0 ; Insert immediate flag
582 mov [rdi], byte cl ; Insert length
584 ;; Insert word string
588 mov rsi, rdx ; rsi = Word string pointer
611 forth_asm PICK, 'PICK'
613 lea rax, [rsp + 8 * rax]
637 ;; Built-in variables:
643 forth LATEST, 'LATEST'
651 forth SYSCODE, 'SYSCODE'
656 forth INPUT_BUFFER, 'INPUT-BUFFER'
660 forth INPUT_LENGTH, 'INPUT-LENGTH'
661 dq LIT, input_buffer_length
664 section '.data' readable writable
666 ;; The LATEST variable holds a pointer to the word that was last added to the
667 ;; dictionary. This pointer is updated as new words are added, and its value is
668 ;; used by FIND to look up words.
669 latest_entry dq initial_latest_entry
671 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
675 ;; The interpreter can read either from standard input or from a buffer. When
676 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
677 ;; buffer instead of reading user input.
679 input_buffer_length dq 0
686 READ_STRING.char_buffer db ?
687 READ_STRING.buffer rb $FF
688 READ_STRING.length dq ?
690 DOTU.chars db '0123456789ABCDEF'
691 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
694 DOTU.printed_length dq ?
698 READ_WORD.buffer rb $FF
699 READ_WORD.length db ?
701 ;; Reserve space for compiled words, accessed through HERE.
709 ;; We store some Forth code in sys.f that defined common words that the user
710 ;; would expect to have available at startup. To execute these words, we just
711 ;; include the file directly in the binary, and then interpret it at startup.