]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/cpu/x86/64/bootstrap.factor
use radix literals
[factor.git] / basis / cpu / x86 / 64 / bootstrap.factor
index 47d6a59895420c25cf7a450c55c2429663f8e77b..8ed80af2c694c530c681f019333adc5143b899c5 100755 (executable)
@@ -4,7 +4,7 @@ USING: bootstrap.image.private kernel kernel.private namespaces
 system layouts vocabs parser compiler.constants
 compiler.codegen.relocation math math.private cpu.x86.assembler
 cpu.x86.assembler.operands sequences generic.single.private
-threads.private ;
+threads.private locals ;
 IN: bootstrap.x86
 
 8 \ cell set
@@ -39,9 +39,9 @@ IN: bootstrap.x86
     ! alignment
     RSP stack-frame-size bootstrap-cell - SUB
     ! store entry point
-    RSP stack-frame-size bootstrap-cell 3 * - [+] RAX MOV
+    RSP stack-frame-size 3 bootstrap-cells - [+] RAX MOV
     ! store stack frame size
-    RSP stack-frame-size bootstrap-cell 2 * - [+] stack-frame-size MOV
+    RSP stack-frame-size 2 bootstrap-cells - [+] stack-frame-size MOV
 ] jit-prolog jit-define
 
 [
@@ -91,49 +91,28 @@ IN: bootstrap.x86
     "end_callback" jit-call
 ] \ c-to-factor define-sub-primitive
 
-USE: locals
-
-:: jit-save-volatile-regs ( -- save-size )
-    ! do we also need to save XMM?
-    volatile-regs length bootstrap-cell * 16 align :> save-size
-    RSP 2 bootstrap-cells [+] save-size ADD ! bump up stack frame size
-    RSP save-size SUB
-    volatile-regs
-    [| r i | RSP i bootstrap-cell * [+] r MOV ] each-index
-    save-size ;
-
-:: jit-restore-volatile-regs ( save-size -- )
-    volatile-regs
-    [| r i | r RSP i bootstrap-cell * [+] MOV ] each-index
-    RSP save-size ADD ;
-
-[| |
-    jit-save-volatile-regs :> save-size
-    jit-save-context
-    RAX vm-reg vm-signal-handler-addr-offset [+] MOV
-    RAX CALL
-    save-size jit-restore-volatile-regs
-] \ signal-handler define-sub-primitive
+: signal-handler-save-regs ( -- regs )
+    { RAX RCX RDX RBX RBP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15 } ;
+
+:: jit-signal-handler-prolog ( -- frame-size )
+    signal-handler-save-regs :> save-regs
+    save-regs length 1 + bootstrap-cells 16 align stack-frame-size + :> frame-size
+    ! minus a cell each for flags, return address
+    ! use LEA so we don't dirty flags
+    RSP RSP frame-size 2 bootstrap-cells - neg [+] LEA
+    save-regs [| r i | RSP i bootstrap-cells [+] r MOV ] each-index
+    PUSHF
+    ! Now that the registers are saved, we can make the stack frame
+    RAX 0 MOV rc-absolute-cell rel-this
+    RSP frame-size 3 bootstrap-cells - [+] RAX MOV
+    RSP frame-size 2 bootstrap-cells - [+] frame-size MOV
+    frame-size ;
 
-[| |
-    jit-save-volatile-regs :> save-size
-    jit-save-context
-    RAX vm-reg vm-signal-handler-addr-offset [+] MOV
-    RAX CALL
-    ! Stack at this point has a fake stack frame set up to represent the
-    ! leaf procedure we interrupted. We must tear down that frame in
-    ! addition to our own before resuming.
-    ! Grab our resume address and place it just underneath the leaf proc's
-    ! return address, since we can't touch any registers once they've been
-    ! restored. If we got this far there should be no faults here and we
-    ! can get away with corrupting the stack frame.
-    RAX RSP save-size 3 bootstrap-cells + [+] MOV
-    RSP save-size 6 bootstrap-cells + [+] RAX MOV
-
-    ! Popping 3 extra cells here plus the 3 cells the epilogue pops leaves
-    ! the resume address at the top of the stack for when the epilogue RETs.
-    save-size 3 bootstrap-cells + jit-restore-volatile-regs
-] \ leaf-signal-handler define-sub-primitive
+:: jit-signal-handler-epilog ( frame-size -- )
+    POPF
+    signal-handler-save-regs
+    [| r i | r RSP i bootstrap-cells [+] MOV ] each-index
+    RSP RSP frame-size 2 bootstrap-cells - [+] LEA ;
 
 [
     arg1 ds-reg [] MOV
@@ -155,6 +134,9 @@ USE: locals
     jit-load-context
     jit-restore-context
 
+    ! Clear the fault flag
+    vm-reg vm-fault-flag-offset [+] 0 MOV
+
     ! Call quotation
     jit-jump-quot
 ] \ unwind-native-frames define-sub-primitive
@@ -211,7 +193,7 @@ USE: locals
 \ lazy-jit-compile define-combinator-primitive
 
 [
-    temp2 HEX: ffffffff MOV f rc-absolute-cell rel-literal
+    temp2 0xffffffff MOV f rc-absolute-cell rel-literal
     temp1 temp2 CMP
 ] pic-check-tuple jit-define
 
@@ -357,8 +339,9 @@ USE: locals
     jit-push-param
     jit-jump-quot ;
 
-: jit-safepoint ( -- )
-    0 [RIP+] EAX MOV rc-relative rel-safepoint ;
+[
+    0 [RIP+] EAX MOV rc-relative rel-safepoint
+] \ jit-safepoint jit-define
 
 [
     jit-start-context-and-delete