]> gitweb.factorcode.org Git - factor.git/commitdiff
cpu.x86.*: improved varargs support
authorBjörn Lindqvist <bjourne@gmail.com>
Tue, 2 Aug 2016 03:03:02 +0000 (05:03 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Tue, 2 Aug 2016 21:14:37 +0000 (23:14 +0200)
Now on the x86.64 platform, we set the AL register to contain the nr of
floating point values passed to the function as required by the abi.

basis/cpu/x86/32/32.factor
basis/cpu/x86/64/64.factor
basis/cpu/x86/64/unix/unix.factor
basis/cpu/x86/64/windows/windows.factor
basis/cpu/x86/x86-docs.factor
basis/cpu/x86/x86-tests.factor
basis/cpu/x86/x86.factor

index ce384bb0d016e151a367d98920f76545b1f03ac0..312b6cc2f93ef8dfaebe3608f9e162f08616139e 100755 (executable)
@@ -192,7 +192,7 @@ M: x86.32 %end-callback ( -- )
     ! MINGW ABI incompatibility disaster
     [ large-struct? ] [ mingw eq? os windows? not or ] bi* and ;
 
-M: x86.32 %prepare-var-args ( -- ) ;
+M: x86.32 %prepare-var-args ( reg-inputs -- ) drop ;
 
 M:: x86.32 stack-cleanup ( stack-size return abi -- n )
     ! a) Functions which are stdcall/fastcall/thiscall have to
index 9e2ee5e11d6ac0f05eafc45c2d731e562abfcd3b..916768f0585353da2cdc2ffa857c770e289d915f 100644 (file)
@@ -1,11 +1,10 @@
 ! Copyright (C) 2005, 2011 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types assocs combinators
-compiler.cfg.intrinsics compiler.codegen.gc-maps
-compiler.codegen.labels compiler.codegen.relocation
-compiler.constants cpu.architecture cpu.x86 cpu.x86.assembler
-cpu.x86.assembler.operands cpu.x86.features kernel layouts locals
-math sequences specialized-arrays system vocabs ;
+USING: alien alien.c-types assocs combinators compiler.cfg.intrinsics
+compiler.codegen.gc-maps compiler.codegen.labels
+compiler.codegen.relocation compiler.constants cpu.architecture
+cpu.x86 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86.features
+kernel locals math sequences specialized-arrays system vocabs ;
 SPECIALIZED-ARRAY: uint
 IN: cpu.x86.64
 
@@ -112,8 +111,6 @@ M: x86.64 %end-callback ( -- )
     param-reg-0 vm-reg MOV
     "end_callback" f f %c-invoke ;
 
-M: x86.64 %prepare-var-args ( -- ) EAX EAX XOR ;
-
 M: x86.64 stack-cleanup 3drop 0 ;
 
 M: x86.64 %cleanup 0 assert= ;
index 84a34988e0d68ab0f36de77610cd4351c8c91da3..6a605e9d0b741c1946b401d7485008300d490acc 100644 (file)
@@ -1,10 +1,9 @@
 ! Copyright (C) 2008, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays sequences math splitting make assocs
-kernel layouts system alien.c-types classes.struct
-cpu.architecture cpu.x86.assembler cpu.x86.assembler.operands
-cpu.x86 cpu.x86.64 compiler.cfg.builder.alien
-compiler.cfg.builder.alien.boxing compiler.cfg.registers ;
+USING: accessors alien.c-types arrays assocs
+compiler.cfg.builder.alien.boxing cpu.architecture cpu.x86
+cpu.x86.assembler cpu.x86.assembler.operands kernel layouts make math
+math.order sequences splitting system ;
 IN: cpu.x86.64.unix
 
 M: x86.64 param-regs
@@ -44,3 +43,7 @@ M: x86.64 dummy-stack-params? f ;
 M: x86.64 dummy-int-params? f ;
 
 M: x86.64 dummy-fp-params? f ;
+
+M: x86.64 %prepare-var-args ( reg-inputs -- )
+    [ second reg-class-of float-regs? ] count 8 min
+    [ EAX EAX XOR ] [ <byte> AL swap MOV ] if-zero ;
index 0d48531de521556f367e6e2d92eda4406e27ca4b..14ca118f7c267f07ab91daabff5cdf1d66dc8d1c 100644 (file)
@@ -23,3 +23,6 @@ M: x86.64 dummy-stack-params? f ;
 M: x86.64 dummy-int-params? t ;
 
 M: x86.64 dummy-fp-params? t ;
+
+M: x86.64 %prepare-var-args ( reg-inputs -- )
+    drop ;
index eca47be0c4095f6e4d64ba4410ab24e5ee600fdf..6a2d6f1bee882b98b7b620b9034b8da587f2b89d 100644 (file)
@@ -1,6 +1,6 @@
 USING: compiler.cfg.registers cpu.x86.assembler
-cpu.x86.assembler.operands.private help.markup help.syntax layouts
-math ;
+cpu.x86.assembler.operands cpu.x86.assembler.operands.private
+help.markup help.syntax layouts math sequences system ;
 IN: cpu.x86
 
 HELP: %boolean
@@ -12,6 +12,10 @@ HELP: %boolean
 { $description "Helper word for emitting conditional move instructions." }
 { $see-also CMOVL CMOVLE CMOVG CMOVGE CMOVE CMOVNE } ;
 
+HELP: %prepare-var-args
+{ $values { "reg-inputs" sequence } }
+{ $description "Emits code needed for calling variadic functions. On " { $link unix } " " { $link x86.64 } ", the " { $link AL } " register must contain the number of float registers used. " } ;
+
 HELP: JLE
 { $values { "dst" "destination offset (relative to the instruction pointer register)" } }
 { $description "Emits a 'jle' instruction." } ;
index 56fc27372b5ca17e181f76f76c96c27625a074b8..be9cecd2a723131fbc716bd6a449e35918f03ea8 100644 (file)
@@ -82,9 +82,19 @@ cpu x86.64? [
 
 ! %prepare-varargs
 ${
-    cpu x86.64? B{ 49 192 } B{ } ?
+    ! xor eax, eax
+    cpu x86.64? os unix? and B{ 49 192 } B{ } ?
+    ! mov al, 2
+    cpu x86.64? os unix? and B{ 176 2 } B{ } ?
 } [
-    [ %prepare-var-args ] B{ } make
+    [ { } %prepare-var-args ] B{ } make
+    [
+        {
+            { T{ spill-slot } int-rep RDI }
+            { T{ spill-slot { n 0 } } float-rep XMM0 }
+            { T{ spill-slot { n 8 } } double-rep XMM1 }
+        } %prepare-var-args
+    ] B{ } make
 ] unit-test
 
 ! %prologue
index 6c5e5d610a94a29598ba006e0ac932799a8ebbcb..a3fa51404956bc3ae67f292fe6e2714002804dac 100644 (file)
@@ -645,20 +645,19 @@ HOOK: %discard-reg-param cpu ( rep reg -- )
 : %store-return ( dst rep -- )
     dup return-reg %store-reg-param ;
 
-HOOK: %prepare-var-args cpu ( -- )
+HOOK: %prepare-var-args cpu ( reg-inputs -- )
 
 HOOK: %cleanup cpu ( n -- )
 
 M:: x86 %alien-assembly ( reg-inputs
-                         stack-inputs
-                         reg-outputs
-                         dead-outputs
-                         cleanup
-                         stack-size
-                         quot -- )
+                          stack-inputs
+                          reg-outputs
+                          dead-outputs
+                          cleanup
+                          stack-size
+                          quot -- )
     stack-inputs [ first3 %store-stack-param ] each
-    reg-inputs [ first3 %store-reg-param ] each
-    %prepare-var-args
+    reg-inputs [ [ first3 %store-reg-param ] each ] [ %prepare-var-args ] bi
     quot call( -- )
     cleanup %cleanup
     reg-outputs [ first3 %load-reg-param ] each
@@ -671,7 +670,10 @@ M: x86 %alien-invoke ( reg-inputs stack-inputs
                        symbols dll gc-map -- )
                        '[ _ _ _ %c-invoke ] %alien-assembly ;
 
-M:: x86 %alien-indirect ( src reg-inputs stack-inputs reg-outputs dead-outputs cleanup stack-size gc-map -- )
+M:: x86 %alien-indirect ( src
+                          reg-inputs stack-inputs
+                          reg-outputs dead-outputs
+                          cleanup stack-size gc-map -- )
     reg-inputs stack-inputs reg-outputs dead-outputs cleanup stack-size [
         src ?spill-slot CALL
         gc-map gc-map-here