1 ;; The UEFI module defines the following functions. Each of these functions
2 ;; preserve the value of RSI and RSP. They may use other registers as they like.
5 ;; Called at initialization.
8 ;; Takes a string buffer in RCX and the length in RDX, and prints the string
12 ;; Wait for the user to type a key, and then put the corresponding ASCII byte
16 ;; Shut down the system, returning the error code given in RAX.
19 ;; The code in this macro is placed at the end of each Forth word. When we are
20 ;; executing a definition, this code is what causes execution to resume at the
21 ;; next word in that definition.
23 ;; RSI points to the address of the definition of the next word to execute.
24 lodsq ; Load value at RSI into RAX and increment RSI
25 ;; Now RAX contains the location of the next word to execute. The first 8
26 ;; bytes of this word is the address of the codeword, which is what we want
28 jmp qword [rax] ; Jump to the codeword of the current word
31 ;; pushr and popr work on the return stack, whose location is stored in the
42 ;; The following macro generates the dictionary header. It updates the
43 ;; initial_latest_entry variable, which is used as the initial value of the
44 ;; latest_entry variable that is made available at runtime.
46 ;; The header contains a link to the previous entry, the length of the name of
47 ;; the word and the word itself as a string literal.
49 ;; This macro also defines a label LABEL_entry.
50 initial_latest_entry = 0
51 macro header label, name, immediate {
55 dq initial_latest_entry
61 db .string_end - ($ + 1)
66 initial_latest_entry = label#_entry
69 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
70 macro forth_asm label, name, immediate {
71 header label, name, immediate
76 section '.text' code readable executable
78 include "impl.asm" ; Misc. subroutines
79 include "bootstrap.asm" ; Forth words encoded in Assembly
82 cld ; Clear direction flag so LODSQ does the right thing.
83 mov rbp, return_stack_top ; Initialize return stack
92 ;; The codeword is the code that will be executed at the beginning of a forth
93 ;; word. It needs to save the old RSI and update it to point to the next word to
96 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
97 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
98 next ; Execute word pointed to by RSI
100 ;; This word is called at the end of a Forth definition. It just needs to
101 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
102 forth_asm EXIT, 'EXIT'
106 ;; LIT is a special word that reads the next "word pointer" and causes it to be
107 ;; placed on the stack rather than executed.
113 ;; When LITSTRING is encountered while executing a word, it instead reads a
114 ;; string from the definition of that word, and places that string on the stack
115 ;; as (buffer, length).
116 forth_asm LITSTRING, 'LITSTRING'
121 add rsi, rax ; Skip over string before resuming execution
124 ;; Given a string (a pointer following by a size), return the location of the
125 ;; dictionary entry for that word. If no such word exists, return 0.
126 forth_asm FIND, 'FIND'
129 pop [find.search_length]
130 pop [find.search_buffer]
131 mov rsi, [latest_entry] ; Start with the last added word
142 ;; Given an entry in the dictionary, return a pointer to the codeword of that
144 forth_asm TCFA, '>CFA'
146 add rax, 8 + 1 ; [rax] = length of name
147 movzx rbx, byte [rax]
149 add rax, rbx ; [rax] = codeword
153 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
154 ;; as a signed integer literal and jumps by that offset.
155 forth_asm BRANCH, 'BRANCH'
156 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
157 next ; Then, we can just continue execution as normal
159 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
160 forth_asm ZBRANCH, '0BRANCH'
161 ;; Compare top of stack to see if we should branch
168 add rsi, 8 ; We need to skip over the next word, which contains the offset.
171 ;; Duplicate the top of the stack.
172 forth_asm DUP_, 'DUP'
176 ;; Execute the codeword at the given address.
177 forth_asm EXEC, 'EXEC'
181 ;; Expects a character on the stack and prints it to standard output.
182 forth_asm EMIT, 'EMIT'
195 ;; Read a single character from the current input stream. Usually, this will wait
196 ;; for the user to press a key, and then return the corresponding character. When
197 ;; reading from a special buffer, it will instead return the next characater from
200 ;; The ASCII character code is placed on the stack.
208 ;; Are we reading from user input or from the input buffer?
209 cmp [input_buffer], 0
212 ;; Reading user input
217 ;; Reading from buffer
218 mov rax, [input_buffer]
219 movzx rax, byte [rax]
222 dec [input_buffer_length]
225 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
226 ;; is valid until the next call to READ_WORD.
227 forth_asm READ_WORD, 'READ-WORD'
230 ;; Read characters until one of them is not whitespace.
232 ;; We consider newlines and spaces to be whitespace.
238 ;; We got a character that wasn't whitespace. Now read the actual word.
263 ;; Takes a string on the stack and replaces it with the decimal number that the
264 ;; string represents.
265 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
267 pop rdi ; String pointer
276 ;; Takes a string (in the form of a pointer and a length on the stack) and
277 ;; prints it to standard output.
278 forth_asm TELL, 'TELL'
290 ;; Exit the program cleanly.
291 forth_asm TERMINATE, 'TERMINATE'
295 ;; Duplicate a pair of elements.
296 forth_asm PAIRDUP, '2DUP'
305 ;; Swap the top two elements on the stack.
306 forth_asm SWAP, 'SWAP'
313 ;; Remove the top element from the stack.
314 forth_asm DROP, 'DROP'
318 forth_asm NOT_, 'NOT'
329 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
332 mov [.printed_length], 1
333 pop rax ; RAX = value to print
334 push rsi ; Save value of RSI
336 ;; We start by constructing the buffer to print in reverse
341 div rbx ; Put remainer in RDX and quotient in RAX
343 ;; Place the appropriate character in the buffer
352 ;; .printed_length is the number of characters that we ulitmately want to
353 ;; print. If we have printed a non-zero character, then we should update
356 je .skip_updating_real_length
358 mov [.printed_length], rbx
359 .skip_updating_real_length:
364 ;; Flip buffer around, since it is currently reversed
365 mov rcx, [.printed_length]
373 add rdi, [.printed_length]
381 mov rdx, [.printed_length]
384 ;; Restore RSI and continue execution
388 ;; Takes a value and an address, and stores the value at the given address.
395 ;; Takes an address and returns the value at the given address.
402 forth_asm PUT_BYTE, 'C!'
408 forth_asm GET_BYTE, 'C@'
410 movzx rax, byte [rax]
414 ;; Add two integers on the stack.
422 ;; Calculate difference between two integers on the stack. The second number is
423 ;; subtracted from the first.
431 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
432 ;; division of a by b.
433 forth_asm TIMESMOD, '/MOD'
442 ;; Read input until next " character is found. Push a string containing the
443 ;; input on the stack as (buffer length). Note that the buffer is only valid
444 ;; until the next call to S" and that no more than 255 characters can be read.
445 forth_asm READ_STRING, 'S"'
446 ;; If the input buffer is set, we should read from there instead.
447 cmp [input_buffer], 0
448 jne read_string_buffer
476 ;; We borrow READ_STRING's buffer. They won't mind.
477 mov [READ_STRING.length], 0
480 mov rbx, [input_buffer]
485 mov rdx, READ_STRING.buffer
486 add rdx, [READ_STRING.length]
488 inc [READ_STRING.length]
491 dec [input_buffer_length]
500 dec [input_buffer_length]
502 push READ_STRING.buffer
503 push [READ_STRING.length]
507 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
508 ;; points to the header. To compile a word, the user can then call ',' to
509 ;; continue to append data after the header.
511 ;; It takes the name of the word as a string (address length) on the stack.
512 forth_asm CREATE, 'CREATE'
513 pop rcx ; Word string length
514 pop rdx ; Word string pointer
516 mov rdi, [here] ; rdi = Address at which to insert this entry
517 mov rax, [latest_entry] ; rax = Address of the previous entry
518 mov [rdi], rax ; Insert link to previous entry
519 mov [latest_entry], rdi ; Update LATEST to point to this word
522 mov [rdi], byte 0 ; Insert immediate flag
525 mov [rdi], byte cl ; Insert length
527 ;; Insert word string
531 mov rsi, rdx ; rsi = Word string pointer
554 forth_asm PICK, 'PICK'
556 lea rax, [rsp + 8 * rax]
582 forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
583 dq LIT, system_table, GET
586 forth_asm EFICALL2, 'EFICALL2'
587 pop rax ; function pointer
588 pop rdx ; 2nd argument
589 pop rcx ; 1st argument
597 forth_asm EFICALL3, 'EFICALL3'
598 pop rax ; function pointer
599 pop r8 ; 3rd argument
600 pop rdx ; 2nd argument
601 pop rcx ; 1st argument
611 forth_asm EFICALL10, 'EFICALL10'
612 pop rax ; function pointer
614 mov rcx, [rsp + 8 * 9]
615 mov rdx, [rsp + 8 * 8]
616 mov r8, [rsp + 8 * 7]
617 mov r9, [rsp + 8 * 6]
619 ;; Reverse order of stack arguments
620 mov r10, [rsp + 8 * 5]
621 mov r11, [rsp + 8 * 0]
622 mov [rsp + 8 * 5], r11
623 mov [rsp + 8 * 0], r10
625 mov r10, [rsp + 8 * 4]
626 mov r11, [rsp + 8 * 1]
627 mov [rsp + 8 * 4], r11
628 mov [rsp + 8 * 1], r10
630 mov r10, [rsp + 8 * 3]
631 mov r11, [rsp + 8 * 2]
632 mov [rsp + 8 * 3], r11
633 mov [rsp + 8 * 2], r10
643 ;; Built-in variables:
649 forth LATEST, 'LATEST'
657 forth SYSCODE, 'SYSCODE'
662 forth INPUT_BUFFER, 'INPUT-BUFFER'
666 forth INPUT_LENGTH, 'INPUT-LENGTH'
667 dq LIT, input_buffer_length
670 section '.data' readable writable
672 ;; The LATEST variable holds a pointer to the word that was last added to the
673 ;; dictionary. This pointer is updated as new words are added, and its value is
674 ;; used by FIND to look up words.
675 latest_entry dq initial_latest_entry
677 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
681 ;; The interpreter can read either from standard input or from a buffer. When
682 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
683 ;; buffer instead of reading user input.
685 input_buffer_length dq 0
692 READ_STRING.char_buffer db ?
693 READ_STRING.buffer rb $FF
694 READ_STRING.length dq ?
696 DOTU.chars db '0123456789ABCDEF'
697 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
700 DOTU.printed_length dq ?
704 READ_WORD.buffer rb $FF
705 READ_WORD.length db ?
707 ;; Reserve space for compiled words, accessed through HERE.
715 ;; We store some Forth code in sys.f that defined common words that the user
716 ;; would expect to have available at startup. To execute these words, we just
717 ;; include the file directly in the binary, and then interpret it at startup.
720 file '../init/uefi.f'