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 segment readable executable
94 include "impl.asm" ; Misc. subroutines
95 include "bootstrap.asm" ; Forth words encoded in Assembly
98 cld ; Clear direction flag so LODSQ does the right thing.
99 mov rbp, return_stack_top ; Initialize return stack
106 ;; The codeword is the code that will be executed at the beginning of a forth
107 ;; word. It needs to save the old RSI and update it to point to the next word to
109 header DOCOL, 'DOCOL'
110 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
111 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
112 next ; Execute word pointed to by RSI
114 ;; This word is called at the end of a Forth definition. It just needs to
115 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
116 forth_asm EXIT, 'EXIT'
120 ;; LIT is a special word that reads the next "word pointer" and causes it to be
121 ;; placed on the stack rather than executed.
127 ;; Given a string (a pointer following by a size), return the location of the
128 ;; dictionary entry for that word. If no such word exists, return 0.
129 forth_asm FIND, 'FIND'
132 pop [find.search_length]
133 pop [find.search_buffer]
134 mov rsi, [latest_entry] ; Start with the last added word
145 ;; Given an entry in the dictionary, return a pointer to the codeword of that
147 forth_asm TCFA, '>CFA'
149 add rax, 8 + 1 ; [rax] = length of name
150 movzx rbx, byte [rax]
152 add rax, rbx ; [rax] = codeword
156 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
157 ;; as a signed integer literal and jumps by that offset.
158 forth_asm BRANCH, 'BRANCH'
159 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
160 next ; Then, we can just continue execution as normal
162 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
163 forth_asm ZBRANCH, '0BRANCH'
164 ;; Compare top of stack to see if we should branch
171 add rsi, 8 ; We need to skip over the next word, which contains the offset.
174 ;; Duplicate the top of the stack.
175 forth_asm DUP_, 'DUP'
179 ;; Execute the codeword at the given address.
180 forth_asm EXEC, 'EXEC'
184 ;; Expects a character on the stack and prints it to standard output.
185 forth_asm EMIT, 'EMIT'
198 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
199 ;; is valid until the next call to READ_WORD.
200 forth_asm READ_WORD, 'READ-WORD'
201 ;; Are we reading from user input or from the input buffer?
202 cmp [input_buffer], 0
205 ;; Reading user input
216 ;; Reading from buffer
219 mov rsi, [input_buffer]
220 mov rcx, [input_buffer_length]
224 mov [input_buffer], rsi ; Updated buffer
225 mov [input_buffer_length], rcx ; Length of updated buffer
226 push rdi ; Word buffer
227 push rdx ; Length of word buffer
232 ;; Takes a string on the stack and replaces it with the decimal number that the
233 ;; string represents.
234 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
236 pop rdi ; String pointer
245 ;; Takes a string (in the form of a pointer and a length on the stack) and
246 ;; prints it to standard output.
247 forth_asm TELL, 'TELL'
259 ;; Exit the program cleanly.
260 forth_asm TERMINATE, 'TERMINATE'
265 ;; Duplicate a pair of elements.
266 forth_asm PAIRDUP, '2DUP'
275 ;; Swap the top two elements on the stack.
276 forth_asm SWAP, 'SWAP'
283 ;; Remove the top element from the stack.
284 forth_asm DROP, 'DROP'
288 forth_asm NOT_, 'NOT'
299 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
302 mov [.printed_length], 1
303 pop rax ; RAX = value to print
304 push rsi ; Save value of RSI
306 ;; We start by constructing the buffer to print in reverse
311 div rbx ; Put remainer in RDX and quotient in RAX
313 ;; Place the appropriate character in the buffer
322 ;; .printed_length is the number of characters that we ulitmately want to
323 ;; print. If we have printed a non-zero character, then we should update
326 je .skip_updating_real_length
328 mov [.printed_length], rbx
329 .skip_updating_real_length:
334 ;; Flip buffer around, since it is currently reversed
335 mov rcx, [.printed_length]
343 add rdi, [.printed_length]
351 mov rdx, [.printed_length]
354 ;; Restore RSI and continue execution
358 ;; Takes a value and an address, and stores the value at the given address.
365 ;; Takes an address and returns the value at the given address.
372 forth_asm PUT_BYTE, 'C!'
378 forth_asm GET_BYTE, 'C@'
380 movzx rax, byte [rax]
384 ;; Add two integers on the stack.
392 ;; Calculate difference between two integers on the stack. The second number is
393 ;; subtracted from the first.
401 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
402 ;; division of a by b.
403 forth_asm TIMESMOD, '/MOD'
412 ;; Read input until next " character is found. Push a string containing the
413 ;; input on the stack as (buffer length). Note that the buffer is only valid
414 ;; until the next call to S" and that no more than 255 characters can be read.
415 forth_asm READ_STRING, 'S"'
416 ;; If the input buffer is set, we should read from there instead.
417 cmp [input_buffer], 0
418 jne read_string_buffer
427 mov rsi, .char_buffer
431 mov al, [.char_buffer]
452 ;; We borrow READ_STRING's buffer. They won't mind.
453 mov [READ_STRING.length], 0
456 mov rbx, [input_buffer]
461 mov rdx, READ_STRING.buffer
462 add rdx, [READ_STRING.length]
464 inc [READ_STRING.length]
467 dec [input_buffer_length]
476 dec [input_buffer_length]
478 push READ_STRING.buffer
479 push [READ_STRING.length]
483 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
484 ;; points to the header. To compile a word, the user can then call ',' to
485 ;; continue to append data after the header.
487 ;; It takes the name of the word as a string (address length) on the stack.
488 forth_asm CREATE, 'CREATE'
489 pop rcx ; Word string length
490 pop rdx ; Word string pointer
492 mov rdi, [here] ; rdi = Address at which to insert this entry
493 mov rax, [latest_entry] ; rax = Address of the previous entry
494 mov [rdi], rax ; Insert link to previous entry
495 mov [latest_entry], rdi ; Update LATEST to point to this word
498 mov [rdi], byte 0 ; Insert immediate flag
501 mov [rdi], byte cl ; Insert length
503 ;; Insert word string
507 mov rsi, rdx ; rsi = Word string pointer
530 forth_asm PICK, 'PICK'
532 lea rax, [rsp + 8 * rax]
556 ;; Built-in variables:
562 forth LATEST, 'LATEST'
570 forth SYSCODE, 'SYSCODE'
575 forth INPUT_BUFFER, 'INPUT-BUFFER'
579 forth INPUT_LENGTH, 'INPUT-LENGTH'
580 dq LIT, input_buffer_length
583 segment readable writable
585 ;; The LATEST variable holds a pointer to the word that was last added to the
586 ;; dictionary. This pointer is updated as new words are added, and its value is
587 ;; used by FIND to look up words.
588 latest_entry dq initial_latest_entry
590 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
594 ;; The interpreter can read either from standard input or from a buffer. When
595 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
596 ;; buffer instead of reading user input.
598 input_buffer_length dq 0
605 READ_STRING.char_buffer db ?
606 READ_STRING.buffer rb $FF
607 READ_STRING.length dq ?
609 DOTU.chars db '0123456789ABCDEF'
610 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
613 DOTU.printed_length dq ?
615 ;; Reserve space for compiled words, accessed through HERE.
625 ;; We store some Forth code in sys.f that defined common words that the user
626 ;; would expect to have available at startup. To execute these words, we just
627 ;; include the file directly in the binary, and then interpret it at startup.