3 ;; At compile-time we load the module given by the environment variable
4 ;; OS_INCLUDE. All of the following these procedures should preserve the value
5 ;; of RSI and RSP. They may use other registers as they like.
7 ;; The module should provide the following:
10 ;; Macro to start the text segment.
13 ;; Macro to start the data segment.
16 ;; Called at initialization.
19 ;; Takes a string buffer in RCX and the length in RDX, and prints the string
23 ;; Wait for the user to type a key, and then put the corresponding ASCII byte
27 ;; Shut down the system, returning the error code given in RAX.
28 include '%OS_INCLUDE%'
30 ;; The code in this macro is placed at the end of each Forth word. When we are
31 ;; executing a definition, this code is what causes execution to resume at the
32 ;; next word in that definition.
34 ;; RSI points to the address of the definition of the next word to execute.
35 lodsq ; Load value at RSI into RAX and increment RSI
36 ;; Now RAX contains the location of the next word to execute. The first 8
37 ;; bytes of this word is the address of the codeword, which is what we want
39 jmp qword [rax] ; Jump to the codeword of the current word
42 ;; pushr and popr work on the return stack, whose location is stored in the
53 ;; The following macro generates the dictionary header. It updates the
54 ;; initial_latest_entry variable, which is used as the initial value of the
55 ;; latest_entry variable that is made available at runtime.
57 ;; The header contains a link to the previous entry, the length of the name of
58 ;; the word and the word itself as a string literal.
60 ;; This macro also defines a label LABEL_entry.
61 initial_latest_entry = 0
62 macro header label, name, immediate {
66 dq initial_latest_entry
72 db .string_end - ($ + 1)
77 initial_latest_entry = label#_entry
80 ;; Define a Forth word that is implemented in assembly. See 'header' for details.
81 macro forth_asm label, name, immediate {
82 header label, name, immediate
87 include "impl.asm" ; Misc. subroutines
88 include "bootstrap.asm" ; Forth words encoded in Assembly
93 cld ; Clear direction flag so LODSQ does the right thing.
94 mov rbp, return_stack_top ; Initialize return stack
103 ;; The codeword is the code that will be executed at the beginning of a forth
104 ;; word. It needs to save the old RSI and update it to point to the next word to
106 header DOCOL, 'DOCOL'
107 pushr rsi ; Save old value of RSI on return stack; we will continue execution there after we are done executing this word
108 lea rsi, [rax + 8] ; RAX currently points to the address of the codeword, so we want to continue at RAX+8
109 next ; Execute word pointed to by RSI
111 ;; This word is called at the end of a Forth definition. It just needs to
112 ;; restore the old value of RSI (saved by 'DOCOL') and resume execution.
113 forth_asm EXIT, 'EXIT'
117 ;; LIT is a special word that reads the next "word pointer" and causes it to be
118 ;; placed on the stack rather than executed.
124 ;; When LITSTRING is encountered while executing a word, it instead reads a
125 ;; string from the definition of that word, and places that string on the stack
126 ;; as (buffer, length).
127 forth_asm LITSTRING, 'LITSTRING'
132 add rsi, rax ; Skip over string before resuming execution
135 ;; Given a string (a pointer following by a size), return the location of the
136 ;; dictionary entry for that word. If no such word exists, return 0.
137 forth_asm FIND, 'FIND'
140 pop [find.search_length]
141 pop [find.search_buffer]
142 mov rsi, [latest_entry] ; Start with the last added word
153 ;; Given an entry in the dictionary, return a pointer to the codeword of that
155 forth_asm TCFA, '>CFA'
157 add rax, 8 + 1 ; [rax] = length of name
158 movzx rbx, byte [rax]
160 add rax, rbx ; [rax] = codeword
164 ;; BRANCH is the fundamental mechanism for branching. BRANCH reads the next word
165 ;; as a signed integer literal and jumps by that offset.
166 forth_asm BRANCH, 'BRANCH'
167 add rsi, [rsi] ; [RSI], which is the next word, contains the offset; we add this to the instruction pointer.
168 next ; Then, we can just continue execution as normal
170 ;; 0BRANCH is like BRANCH, but it jumps only if the top of the stack is zero.
171 forth_asm ZBRANCH, '0BRANCH'
172 ;; Compare top of stack to see if we should branch
179 add rsi, 8 ; We need to skip over the next word, which contains the offset.
182 ;; Duplicate the top of the stack.
183 forth_asm DUP_, 'DUP'
187 ;; Execute the codeword at the given address.
188 forth_asm EXEC, 'EXEC'
192 ;; Expects a character on the stack and prints it to standard output.
193 forth_asm EMIT, 'EMIT'
206 ;; Read a single character from the current input stream. Usually, this will wait
207 ;; for the user to press a key, and then return the corresponding character. When
208 ;; reading from a special buffer, it will instead return the next characater from
211 ;; The ASCII character code is placed on the stack.
219 ;; Are we reading from user input or from the input buffer?
220 cmp [input_buffer], 0
223 ;; Reading user input
228 ;; Reading from buffer
229 mov rax, [input_buffer]
230 movzx rax, byte [rax]
233 dec [input_buffer_length]
236 ;; Read a word and push it onto the stack as a pointer and a size. The pointer
237 ;; is valid until the next call to READ_WORD.
238 forth_asm READ_WORD, 'READ-WORD'
241 ;; Read characters until one of them is not whitespace.
243 ;; We consider newlines and spaces to be whitespace.
249 ;; We got a character that wasn't whitespace. Now read the actual word.
274 ;; Takes a string on the stack and replaces it with the decimal number that the
275 ;; string represents.
276 forth_asm PARSE_NUMBER, 'PARSE-NUMBER'
278 pop rdi ; String pointer
287 ;; Takes a string (in the form of a pointer and a length on the stack) and
288 ;; prints it to standard output.
289 forth_asm TELL, 'TELL'
301 ;; Exit the program cleanly.
302 forth_asm TERMINATE, 'TERMINATE'
306 ;; Duplicate a pair of elements.
307 forth_asm PAIRDUP, '2DUP'
316 ;; Swap the top two elements on the stack.
317 forth_asm SWAP, 'SWAP'
324 ;; Remove the top element from the stack.
325 forth_asm DROP, 'DROP'
329 forth_asm NOT_, 'NOT'
340 ;; .U prints the value on the stack as an unsigned integer in hexadecimal.
343 mov [.printed_length], 1
344 pop rax ; RAX = value to print
345 push rsi ; Save value of RSI
347 ;; We start by constructing the buffer to print in reverse
352 div rbx ; Put remainer in RDX and quotient in RAX
354 ;; Place the appropriate character in the buffer
363 ;; .printed_length is the number of characters that we ulitmately want to
364 ;; print. If we have printed a non-zero character, then we should update
367 je .skip_updating_real_length
369 mov [.printed_length], rbx
370 .skip_updating_real_length:
375 ;; Flip buffer around, since it is currently reversed
376 mov rcx, [.printed_length]
384 add rdi, [.printed_length]
392 mov rdx, [.printed_length]
395 ;; Restore RSI and continue execution
399 ;; Takes a value and an address, and stores the value at the given address.
406 ;; Takes an address and returns the value at the given address.
413 forth_asm PUT_BYTE, 'C!'
419 forth_asm GET_BYTE, 'C@'
421 movzx rax, byte [rax]
425 ;; Add two integers on the stack.
433 ;; Calculate difference between two integers on the stack. The second number is
434 ;; subtracted from the first.
442 ;; Given two integers a and b on the stack, pushes the quotient and remainder of
443 ;; division of a by b.
444 forth_asm TIMESMOD, '/MOD'
453 ;; Read input until next " character is found. Push a string containing the
454 ;; input on the stack as (buffer length). Note that the buffer is only valid
455 ;; until the next call to S" and that no more than 255 characters can be read.
456 forth_asm READ_STRING, 'S"'
457 ;; If the input buffer is set, we should read from there instead.
458 cmp [input_buffer], 0
459 jne read_string_buffer
487 ;; We borrow READ_STRING's buffer. They won't mind.
488 mov [READ_STRING.length], 0
491 mov rbx, [input_buffer]
496 mov rdx, READ_STRING.buffer
497 add rdx, [READ_STRING.length]
499 inc [READ_STRING.length]
502 dec [input_buffer_length]
511 dec [input_buffer_length]
513 push READ_STRING.buffer
514 push [READ_STRING.length]
518 ;; CREATE inserts a new header in the dictionary, and updates LATEST so that it
519 ;; points to the header. To compile a word, the user can then call ',' to
520 ;; continue to append data after the header.
522 ;; It takes the name of the word as a string (address length) on the stack.
523 forth_asm CREATE, 'CREATE'
524 pop rcx ; Word string length
525 pop rdx ; Word string pointer
527 mov rdi, [here] ; rdi = Address at which to insert this entry
528 mov rax, [latest_entry] ; rax = Address of the previous entry
529 mov [rdi], rax ; Insert link to previous entry
530 mov [latest_entry], rdi ; Update LATEST to point to this word
533 mov [rdi], byte 0 ; Insert immediate flag
536 mov [rdi], byte cl ; Insert length
538 ;; Insert word string
542 mov rsi, rdx ; rsi = Word string pointer
565 forth_asm PICK, 'PICK'
567 lea rax, [rsp + 8 * rax]
593 forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
594 dq LIT, system_table, GET
597 forth_asm EFICALL2, 'EFICALL2'
598 pop rax ; function pointer
599 pop rdx ; 2nd argument
600 pop rcx ; 1st argument
608 forth_asm EFICALL3, 'EFICALL3'
609 pop rax ; function pointer
610 pop r8 ; 3rd argument
611 pop rdx ; 2nd argument
612 pop rcx ; 1st argument
622 forth_asm EFICALL10, 'EFICALL10'
623 pop rax ; function pointer
625 mov rcx, [rsp + 8 * 9]
626 mov rdx, [rsp + 8 * 8]
627 mov r8, [rsp + 8 * 7]
628 mov r9, [rsp + 8 * 6]
630 ;; Reverse order of stack arguments
631 mov r10, [rsp + 8 * 5]
632 mov r11, [rsp + 8 * 0]
633 mov [rsp + 8 * 5], r11
634 mov [rsp + 8 * 0], r10
636 mov r10, [rsp + 8 * 4]
637 mov r11, [rsp + 8 * 1]
638 mov [rsp + 8 * 4], r11
639 mov [rsp + 8 * 1], r10
641 mov r10, [rsp + 8 * 3]
642 mov r11, [rsp + 8 * 2]
643 mov [rsp + 8 * 3], r11
644 mov [rsp + 8 * 2], r10
654 ;; Built-in variables:
660 forth LATEST, 'LATEST'
668 forth SYSCODE, 'SYSCODE'
673 forth INPUT_BUFFER, 'INPUT-BUFFER'
677 forth INPUT_LENGTH, 'INPUT-LENGTH'
678 dq LIT, input_buffer_length
683 ;; The LATEST variable holds a pointer to the word that was last added to the
684 ;; dictionary. This pointer is updated as new words are added, and its value is
685 ;; used by FIND to look up words.
686 latest_entry dq initial_latest_entry
688 ;; The STATE variable is 0 when the interpreter is executing, and non-zero when
692 ;; The interpreter can read either from standard input or from a buffer. When
693 ;; input-buffer is set (non-null), words like READ-WORD and S" will use this
694 ;; buffer instead of reading user input.
696 input_buffer_length dq 0
703 READ_STRING.char_buffer db ?
704 READ_STRING.buffer rb $FF
705 READ_STRING.length dq ?
707 DOTU.chars db '0123456789ABCDEF'
708 DOTU.buffer rq 16 ; 64-bit number has no more than 16 digits in hex
711 DOTU.printed_length dq ?
715 READ_WORD.buffer rb $FF
716 READ_WORD.length db ?
718 ;; Reserve space for compiled words, accessed through HERE.
726 ;; We store some Forth code in sys.f that defined common words that the user
727 ;; would expect to have available at startup. To execute these words, we just
728 ;; include the file directly in the binary, and then interpret it at startup.