1 ;; The UEFI module defines the following functions. Each of these
2 ;; functions preserve the value of RSI and RSP. They may use other
3 ;; registers as they like.
6 ;; Called at initialization.
9 ;; Takes a string buffer in RCX and the length in RDX, and prints the string
13 ;; Wait for the user to type a key, and then put the corresponding ASCII byte
17 ;; Shut down the system, returning the error code given in RAX.
19 include 'src/uefi.asm'
21 ;; The code in this macro is placed at the end of each Forth word. When we are
22 ;; executing a definition, this code is what causes execution to resume at the
23 ;; next word in that definition.
25 ;; RSI points to the address of the definition of the next word to execute.
26 lodsq ; Load value at RSI into RAX and increment RSI
27 ;; Now RAX contains the location of the next word to execute. The first 8
28 ;; bytes of this word is the address of the codeword, which is what we want
30 jmp qword [rax] ; Jump to the codeword of the current word
33 ;; pushr and popr work on the return stack, whose location is stored in the
34 ;; register RBP. Always allocates an extra 8 bytes as "local frame"
44 ;; The following macro generates the dictionary header. It updates the
45 ;; initial_latest_entry variable, which is used as the initial value of the
46 ;; latest_entry variable that is made available at runtime.
48 ;; The header contains a link to the previous entry, the length of the name of
49 ;; the word and the word itself as a string literal.
51 ;; This macro also defines a label LABEL_entry.
52 initial_latest_entry = 0
53 macro header label, name, immediate {
57 dq initial_latest_entry
63 db .string_end - ($ + 1)
68 initial_latest_entry = label#_entry
71 ;; Define a Forth word that is implemented in assembly. See 'header'
73 macro forth_asm label, name, immediate {
74 header label, name, immediate
79 section '.text' code readable executable
81 include "impl.asm" ; Misc. subroutines
82 include "bootstrap.asm" ; Forth words encoded in Assembly
85 cld ; Clear direction flag so LODSQ does the right thing.
86 mov rbp, return_stack_top ; Initialize return stack
95 ;; The codeword is the code that will be executed at the beginning of
96 ;; a forth word. It needs to save the old RSI and update it to point
97 ;; to the next word to execute.
99 pushr rsi ; Save old value of RSI on return stack; we
100 ; will continue execution there after we are
101 ; done executing this word
102 lea rsi, [rax + 8] ; RAX currently points to the address of the
103 ; codeword, so we want to continue at RAX+8
104 next ; Execute word pointed to by RSI
106 ;; This word is called at the end of a Forth definition. It just needs to
107 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
108 forth_asm EXIT, 'EXIT'
112 ;; LIT is a special word that reads the next "word pointer" and causes
113 ;; it to be placed on the stack rather than executed.
119 ;; When LITSTRING is encountered while executing a word, it instead
120 ;; reads a string from the definition of that word, and places that
121 ;; string on the stack as (buffer, length).
122 forth_asm LITSTRING, 'LITSTRING'
127 add rsi, rax ; Skip over string before resuming execution
130 ;; Given a string (a pointer following by a size), return the location
131 ;; of the dictionary entry for that word. If no such word exists,
133 forth_asm FIND, 'FIND'
136 pop [find.search_length]
137 pop [find.search_buffer]
138 mov rsi, [latest_entry] ; Start with the last added word
149 ;; Given an entry in the dictionary, return a pointer to the codeword
151 forth_asm TCFA, '>CFA'
153 add rax, 8 + 1 ; [rax] = length of name
154 movzx rbx, byte [rax]
156 add rax, rbx ; [rax] = codeword
160 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the
161 ;; next word as a signed integer literal and jumps by that offset.
163 forth_asm BRANCH, 'BRANCH'
164 add rsi, [rsi] ; [RSI], which is the next word, contains the offset
165 ; 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
170 forth_asm ZBRANCH, '0BRANCH'
172 cmp rax, 0 ; Compare top of stack to see if we should branch
178 add rsi, 8 ; We need to skip over the next word, which contains
182 ;; Push the return stack pointer. "grows" negatively
183 forth_asm RSPGET, 'R='
187 ;; The return stack "grows" negatively, and rbp is the address of the top
188 ;; Move rbp by n (from stack) bytes
189 forth_asm RSPADD, 'R+'
194 ;; Push top of the stack.
195 forth_asm TOP_, 'TOP'
199 ;; Duplicate the top of the stack.
200 forth_asm DUP_, 'DUP'
204 ;; Execute the codeword at the given address.
205 forth_asm EXEC, 'EXEC'
209 ;; This word skips a word without exectuing, but pushes its address
210 forth_asm SKIP_, 'SKIP'
212 add rsi, 8 ; We need to skip over the next word, which contains
216 ;; Expects a character on the stack and prints it to standard output.
217 forth_asm EMIT, 'EMIT'
230 ;; Read a single character from the current input stream. Usually,
231 ;; this will wait for the user to press a key, and then return the
232 ;; corresponding character. When reading from a special buffer, it
233 ;; will instead return the next characater from that buffer.
235 ;; The ASCII character code is placed on the stack.
243 ;; Are we reading from user input or from the input buffer?
244 cmp [input_buffer], 0
247 ;; Reading user input
252 ;; Reading from buffer
253 mov rax, [input_buffer]
254 movzx rax, byte [rax]
257 dec [input_buffer_length]
260 ;; Read a word and push it onto the stack as a pointer and a size. The
261 ;; pointer is valid until the next call to READ_WORD.
262 forth_asm READ_WORD, 'READ-WORD'
265 ;; Read characters until one of them is not whitespace.
267 ;; We consider newlines, tabs and spaces to be whitespace.
275 ;; We got a character that wasn't whitespace. Now read the actual word.
302 ;; Takes a string on the stack and replaces it with the decimal number
303 ;; that the string represents.
304 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
306 pop rdi ; String pointer
315 ;; Takes a string (in the form of a pointer and a length on the stack) and
316 ;; prints it to standard output.
317 forth_asm TELL, 'TELL'
329 ;; Exit the program cleanly.
330 forth_asm TERMINATE, 'TERMINATE'
334 ;; Duplicate a pair of elements.
335 forth_asm PAIRDUP, '2DUP'
344 ;; Swap the top two elements on the stack.
345 forth_asm SWAP, 'SWAP'
352 ;; Remove the top element from the stack.
353 forth_asm DROP, 'DROP'
357 ;; Takes a value and an address, and stores the value at the given address.
365 forth_asm NOT_, 'NOT'
376 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
379 mov [.printed_length], 1
380 pop rax ; RAX = value to print
381 push rsi ; Save value of RSI
383 ;; We start by constructing the buffer to print in reverse
388 div rbx ; Put remainer in RDX and quotient in RAX
390 ;; Place the appropriate character in the buffer
399 ;; .printed_length is the number of characters that we ulitmately want to
400 ;; print. If we have printed a non-zero character, then we should update
403 je .skip_updating_real_length
405 mov [.printed_length], rbx
406 .skip_updating_real_length:
411 ;; Flip buffer around, since it is currently reversed
412 mov rcx, [.printed_length]
420 add rdi, [.printed_length]
428 mov rdx, [.printed_length]
431 ;; Restore RSI and continue execution
435 ;; Takes a value and an address, and stores the value at the given address.
442 ;; Takes an address and returns the value at the given address.
449 forth_asm PUT_BYTE, 'C!'
455 forth_asm GET_BYTE, 'C@'
457 movzx rax, byte [rax]
461 ;; Add two integers on the stack.
469 ;; Calculate difference between two integers on the stack. The second
470 ;; number is subtracted from the first.
478 ;; Multiply two integers on the stack ignoring overflow
486 ;; Given two integers a and b on the stack, pushes the quotient and
487 ;; remainder of division of a by b.
488 forth_asm TIMESMOD, '/MOD'
497 ;; Read input until next " character is found. Push a string
498 ;; containing the input on the stack as (buffer length). Note that the
499 ;; buffer is only valid until the next call to S" and that no more
500 ;; than 255 characters can be read.
501 forth_asm READ_STRING, 'S"'
502 ;; If the input buffer is set, we should read from there instead.
503 cmp [input_buffer], 0
504 jne read_string_buffer
532 ;; We borrow READ_STRING's buffer. They won't mind.
533 mov [READ_STRING.length], 0
536 mov rbx, [input_buffer]
541 mov rdx, READ_STRING.buffer
542 add rdx, [READ_STRING.length]
544 inc [READ_STRING.length]
547 dec [input_buffer_length]
556 dec [input_buffer_length]
558 push READ_STRING.buffer
559 push [READ_STRING.length]
563 ;; CREATE inserts a new header in the dictionary, and updates LATEST
564 ;; so that it points to the header. To compile a word, the user can
565 ;; then call ',' to continue to append data after the header.
567 ;; It takes the name of the word as a string (address length) on the
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]
618 forth_asm OVER, 'OVER'
667 forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
668 dq LIT, system_table, GET
671 forth_asm EFICALL1, 'EFICALL1'
672 pop rax ; function pointer
673 pop rcx ; 1st argument
679 forth_asm EFICALL2, 'EFICALL2'
680 pop rax ; function pointer
681 pop rdx ; 2nd argument
682 pop rcx ; 1st argument
688 forth_asm EFICALL3, 'EFICALL3'
689 pop rax ; function pointer
690 pop r8 ; 3rd argument
691 pop rdx ; 2nd argument
692 pop rcx ; 1st argument
699 forth_asm EFICALL4, 'EFICALL4'
700 pop rax ; function pointer
701 pop r9 ; 4th argument
702 pop r8 ; 3rd argument
703 pop rdx ; 2nd argument
704 pop rcx ; 1st argument
711 forth_asm EFICALL5, 'EFICALL5'
712 pop rax ; function pointer
713 pop r10 ; 5th argument
714 pop r9 ; 4th argument
715 pop r8 ; 3rd argument
716 pop rdx ; 2nd argument
717 pop rcx ; 1st argument
718 push r10 ; restore as stack argument
725 forth_asm EFICALL10, 'EFICALL10'
726 pop rax ; function pointer
727 mov rcx, [rsp + 8 * 9] ; 1st
728 mov rdx, [rsp + 8 * 8] ; 2nd
729 mov r8, [rsp + 8 * 7]
730 mov r9, [rsp + 8 * 6]
731 ;; Reverse order of stack arguments
732 mov r10, [rsp + 8 * 5]
733 mov r11, [rsp + 8 * 0]
734 mov [rsp + 8 * 5], r11
735 mov [rsp + 8 * 0], r10
736 mov r10, [rsp + 8 * 4]
737 mov r11, [rsp + 8 * 1]
738 mov [rsp + 8 * 4], r11
739 mov [rsp + 8 * 1], r10
740 mov r10, [rsp + 8 * 3]
741 mov r11, [rsp + 8 * 2]
742 mov [rsp + 8 * 3], r11
743 mov [rsp + 8 * 2], r10
750 ;; Built-in variables:
756 forth LATEST, 'LATEST'
764 forth SYSCODE, 'SYSCODE'
769 forth INPUT_BUFFER, 'INPUT-BUFFER'
773 forth INPUT_LENGTH, 'INPUT-LENGTH'
774 dq LIT, input_buffer_length
777 section '.data' readable writable
779 ;; The LATEST variable holds a pointer to the word that was last added
780 ;; to the dictionary. This pointer is updated as new words are added,
781 ;; and its value is used by FIND to look up words.
782 latest_entry dq initial_latest_entry
784 ;; The STATE variable is 0 when the interpreter is executing, and
785 ;; non-zero when it is compiling.
788 ;; The interpreter can read either from standard input or from a
789 ;; buffer. When input-buffer is set (non-null), words like READ-WORD
790 ;; and S" will use this buffer instead of reading user input.
792 input_buffer_length dq 0
799 READ_STRING.char_buffer db ?
800 READ_STRING.buffer rb $FF
801 READ_STRING.length dq ?
803 DOTU.chars db '0123456789abcdef'
804 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
807 DOTU.printed_length dq ?
811 READ_WORD.buffer rb $FF
812 READ_WORD.length db ?
814 ;; Reserve space for compiled words, accessed through HERE.
822 ;; We store some Forth code in sys.f that defined common words that
823 ;; the user would expect to have available at startup. To execute
824 ;; these words, we just include the file directly in the binary, and
825 ;; then interpret it at startup.
828 file '../init/uefi.f'
829 file '../init/blurb.f'