-;; vim: syntax=fasm
-
-;; At compile-time we load the module given by the environment variable
-;; OS_INCLUDE. This module should define the following macros:
-;;
-;; Each of these functions should preserve the value of RSI and RSP. They may
-;; use other registers as they like.
+;; The UEFI module defines the following functions. Each of these functions
+;; preserve the value of RSI and RSP. They may use other registers as they like.
;;
;; os_initialize
;; Called at initialization.
;;
;; os_terminate
;; Shut down the system, returning the error code given in RAX.
-include '%OS_INCLUDE%'
+include 'os/uefi.asm'
;; The code in this macro is placed at the end of each Forth word. When we are
;; executing a definition, this code is what causes execution to resume at the
dq BRANCH, -8 * 2
dq TERMINATE
+;; EFI:
+
+forth EFI_SYSTEM_TABLE_CONSTANT, 'SystemTable'
+ dq LIT, system_table, GET
+ dq EXIT
+
+forth_asm EFICALL2, 'EFICALL2'
+ pop rax ; function pointer
+ pop rdx ; 2nd argument
+ pop rcx ; 1st argument
+
+ sub rsp, 32
+ call rax
+ add rsp, 32
+
+ next
+
+forth_asm EFICALL3, 'EFICALL3'
+ pop rax ; function pointer
+ pop r8 ; 3rd argument
+ pop rdx ; 2nd argument
+ pop rcx ; 1st argument
+
+ sub rsp, 32
+ call rax
+ add rsp, 32
+
+ push rax
+
+ next
+
+forth_asm EFICALL10, 'EFICALL10'
+ pop rax ; function pointer
+
+ mov rcx, [rsp + 8 * 9]
+ mov rdx, [rsp + 8 * 8]
+ mov r8, [rsp + 8 * 7]
+ mov r9, [rsp + 8 * 6]
+
+ ;; Reverse order of stack arguments
+ mov r10, [rsp + 8 * 5]
+ mov r11, [rsp + 8 * 0]
+ mov [rsp + 8 * 5], r11
+ mov [rsp + 8 * 0], r10
+
+ mov r10, [rsp + 8 * 4]
+ mov r11, [rsp + 8 * 1]
+ mov [rsp + 8 * 4], r11
+ mov [rsp + 8 * 1], r10
+
+ mov r10, [rsp + 8 * 3]
+ mov r11, [rsp + 8 * 2]
+ mov [rsp + 8 * 3], r11
+ mov [rsp + 8 * 2], r10
+
+ sub rsp, 32
+ call rax
+ add rsp, 32 + 8 * 10
+
+ push rax
+
+ next
+
;; Built-in variables:
forth STATE, 'STATE'
;; We store some Forth code in sys.f that defined common words that the user
;; would expect to have available at startup. To execute these words, we just
;; include the file directly in the binary, and then interpret it at startup.
-sysf file 'sys.f'
+sysf:
+file 'sys.f'
+file 'uefi.f'
sysf.len = $ - sysf