]> gitweb.factorcode.org Git - factor.git/commitdiff
FFI rewrite part 1: split up ##alien-invoke and friends into smaller instructions
authorSlava Pestov <slava@factorcode.org>
Mon, 10 May 2010 01:36:52 +0000 (21:36 -0400)
committerSlava Pestov <slava@factorcode.org>
Mon, 10 May 2010 01:36:52 +0000 (21:36 -0400)
33 files changed:
basis/alien/arrays/arrays.factor
basis/alien/c-types/c-types-docs.factor
basis/alien/c-types/c-types.factor
basis/bootstrap/compiler/compiler.factor
basis/bootstrap/help/help.factor
basis/calendar/calendar-docs.factor
basis/classes/struct/struct.factor
basis/compiler/cfg/block-joining/block-joining.factor
basis/compiler/cfg/branch-splitting/branch-splitting.factor
basis/compiler/cfg/build-stack-frame/build-stack-frame.factor
basis/compiler/cfg/builder/alien/alien.factor [new file with mode: 0644]
basis/compiler/cfg/builder/blocks/blocks.factor
basis/compiler/cfg/builder/builder.factor
basis/compiler/cfg/cfg.factor
basis/compiler/cfg/checker/checker.factor
basis/compiler/cfg/dataflow-analysis/dataflow-analysis.factor
basis/compiler/cfg/instructions/instructions.factor
basis/compiler/cfg/intrinsics/fixnum/fixnum.factor
basis/compiler/cfg/representations/rewrite/rewrite.factor
basis/compiler/cfg/rpo/rpo.factor
basis/compiler/cfg/scheduling/scheduling.factor
basis/compiler/cfg/stacks/finalize/finalize.factor
basis/compiler/cfg/utilities/utilities.factor
basis/compiler/codegen/alien/alien.factor [deleted file]
basis/compiler/codegen/alien/authors.txt [deleted file]
basis/compiler/codegen/codegen.factor
basis/compiler/compiler.factor
basis/compiler/tests/alien.factor
basis/compiler/tests/linkage-errors.factor [new file with mode: 0644]
basis/cpu/architecture/architecture.factor
basis/cpu/ppc/ppc.factor
basis/cpu/x86/32/32.factor
basis/cpu/x86/64/64.factor

index dc9d3e0d05a69ba836a35c19a26f0afceb732fec..bf87cfd9f193ca535b54d89ba7efc867cb51ae3d 100644 (file)
@@ -22,17 +22,11 @@ M: array c-type-align first c-type-align ;
 
 M: array c-type-align-first first c-type-align-first ;
 
-M: array unbox-parameter drop void* unbox-parameter ;
-
-M: array unbox-return drop void* unbox-return ;
-
-M: array box-parameter drop void* box-parameter ;
-
-M: array box-return drop void* box-return ;
+M: array base-type drop void* base-type ;
 
 M: array stack-size drop void* stack-size ;
 
-M: array flatten-c-type drop { int-rep } ;
+M: array flatten-c-type drop void* flatten-c-type ;
 
 PREDICATE: string-type < pair
     first2 [ c-string = ] [ word? ] bi* and ;
@@ -43,35 +37,19 @@ M: string-type c-type-class drop object ;
 
 M: string-type c-type-boxed-class drop object ;
 
-M: string-type heap-size
-    drop void* heap-size ;
-
-M: string-type c-type-align
-    drop void* c-type-align ;
-
-M: string-type c-type-align-first
-    drop void* c-type-align-first ;
-
-M: string-type unbox-parameter
-    drop void* unbox-parameter ;
+M: string-type heap-size drop void* heap-size ;
 
-M: string-type unbox-return
-    drop void* unbox-return ;
+M: string-type c-type-align drop void* c-type-align ;
 
-M: string-type box-parameter
-    drop void* box-parameter ;
+M: string-type c-type-align-first drop void* c-type-align-first ;
 
-M: string-type box-return
-    drop void* box-return ;
+M: string-type base-type drop void* base-type ;
 
-M: string-type stack-size
-    drop void* stack-size ;
+M: string-type stack-size drop void* stack-size ;
 
-M: string-type c-type-rep
-    drop int-rep ;
+M: string-type c-type-rep drop int-rep ;
 
-M: string-type flatten-c-type
-    drop { int-rep } ;
+M: string-type flatten-c-type drop void* flatten-c-type ;
 
 M: string-type c-type-boxer-quot
     second dup binary =
index 9592fb1812715d38f11e4ae412d8df18f7e93d4c..bf26dd5f88687adba8de6c1ea4a50d72c7a5c9d5 100644 (file)
@@ -43,21 +43,6 @@ HELP: c-setter
 { $description "Outputs a quotation which writes values of this C type to a C structure." }
 { $errors "Throws an error if the type does not exist." } ;
 
-HELP: box-parameter
-{ $values { "n" math:integer } { "c-type" "a C type" } }
-{ $description "Generates code for converting a C value stored at  offset " { $snippet "n" } " from the top of the stack into a Factor object to be pushed on the data stack." }
-{ $notes "This is an internal word used by the compiler when compiling callbacks." } ;
-
-HELP: box-return
-{ $values { "c-type" "a C type" } }
-{ $description "Generates code for converting a C value stored in return registers into a Factor object to be pushed on the data stack." }
-{ $notes "This is an internal word used by the compiler when compiling alien calls." } ;
-
-HELP: unbox-return
-{ $values { "c-type" "a C type" } }
-{ $description "Generates code for converting a Factor value on the data stack into a C value to be stored in the return registers." }
-{ $notes "This is an internal word used by the compiler when compiling callbacks." } ;
-
 HELP: define-deref
 { $values { "c-type" "a C type" } }
 { $description "Defines a word " { $snippet "*name" } " with stack effect " { $snippet "( c-ptr -- value )" } " for reading a value with C type " { $snippet "name" } " stored at an alien pointer." }
index 98b15b7af8460e42cd226004c14afb35c8ec00f8..d916ce9dec00d27dc7313b596247afaf2f32acc4 100644 (file)
@@ -111,27 +111,11 @@ GENERIC: c-type-align-first ( name -- n )
 
 M: abstract-c-type c-type-align-first align-first>> ;
 
-: c-type-box ( n c-type -- )
-    [ rep>> ] [ boxer>> ] bi %box ;
+GENERIC: base-type ( c-type -- c-type )
 
-: c-type-unbox ( n c-type -- )
-    [ rep>> ] [ unboxer>> ] bi %unbox ;
+M: c-type-name base-type c-type ;
 
-GENERIC: box-parameter ( n c-type -- )
-
-M: c-type box-parameter c-type-box ;
-
-GENERIC: box-return ( c-type -- )
-
-M: c-type box-return f swap c-type-box ;
-
-GENERIC: unbox-parameter ( n c-type -- )
-
-M: c-type unbox-parameter c-type-unbox ;
-
-GENERIC: unbox-return ( c-type -- )
-
-M: c-type unbox-return f swap c-type-unbox ;
+M: c-type base-type ;
 
 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
 
@@ -179,10 +163,7 @@ PROTOCOL: c-type-protocol
     c-type-setter
     c-type-align
     c-type-align-first
-    box-parameter
-    box-return
-    unbox-parameter
-    unbox-return
+    base-type
     heap-size
     stack-size
     flatten-c-type ;
@@ -204,18 +185,6 @@ TUPLE: long-long-type < c-type ;
 : <long-long-type> ( -- c-type )
     long-long-type new ;
 
-M: long-long-type unbox-parameter ( n c-type -- )
-    unboxer>> %unbox-long-long ;
-
-M: long-long-type unbox-return ( c-type -- )
-    f swap unbox-parameter ;
-
-M: long-long-type box-parameter ( n c-type -- )
-    boxer>> %box-long-long ;
-
-M: long-long-type box-return ( c-type -- )
-    f swap box-parameter ;
-
 M: long-long-type flatten-c-type
     int-rep (flatten-c-type) ;
 
index 56109e2de6f6591b315d8306d71822eb39640e4b..9c753ce08f96da6f2b46c989ca8a1823fd5fe477 100644 (file)
@@ -117,6 +117,8 @@ gc
 
     " done" print flush
 
+    "alien.syntax" require
+    "alien.complex" require
     "io.streams.byte-array.fast" require
 
 ] unless
index 553b91a6aee084ce85489bf540bcf75646a693eb..f77829ae860ec5cdcdf2a965d43118e8288761af 100644 (file)
@@ -6,12 +6,10 @@ IN: bootstrap.help
 : load-help ( -- )
     "help.lint" require
     "help.vocabs" require
-    "alien.syntax" require
-    "compiler" require
 
     t load-help? set-global
 
-    [ vocab ] load-vocab-hook [
+    [ dup vocab [ ] [ no-vocab ] ?if ] load-vocab-hook [
         dictionary get values
         [ docs-loaded?>> not ] filter
         [ load-docs ] each
index a5a31ebd659808537b2dd22de3e08bbec46e724a..e76aace4647a74d5b18fa9f44ade55449c059f6f 100644 (file)
@@ -8,7 +8,7 @@ HELP: duration
 { $description "A duration is a period of time years, months, days, hours, minutes, and seconds.  All duration slots can store " { $link real } " numbers. Compare two durations with the " { $link <=> } " word." } ;
 
 HELP: timestamp
-{ $description "A timestamp is a date and a time with a timezone offset.  Timestamp slots must store integers except for " { $snippet "seconds" } ", which stores reals, and " { $snippet "gmt-offset" } ", which stores a " { $link duration } ". Compare two duarionts with the " { $link <=> } " word." } ;
+{ $description "A timestamp is a date and a time with a timezone offset.  Timestamp slots must store integers except for " { $snippet "seconds" } ", which stores reals, and " { $snippet "gmt-offset" } ", which stores a " { $link duration } ". Compare two durations with the " { $link <=> } " word." } ;
 
 { timestamp duration } related-words
 
index 74b4882ffb87a7b2d59f525a008a9844aecabf65..d33f6fa35d85746e299c0f4e2406956b88243ab5 100644 (file)
@@ -169,20 +169,10 @@ M: struct-c-type c-type ;
 : if-value-struct ( ctype true false -- )
     [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
 
-M: struct-c-type unbox-parameter
-    [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
-
-M: struct-c-type box-parameter
-    [ %box-large-struct ] [ box-parameter ] if-value-struct ;
-
 : if-small-struct ( c-type true false -- ? )
     [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
 
-M: struct-c-type unbox-return
-    [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
-
-M: struct-c-type box-return
-    [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
+M: struct-c-type base-type ;
 
 M: struct-c-type stack-size
     [ heap-size cell align ] [ stack-size ] if-value-struct ;
index 3f98c3711f4efb545efdef280f069e2a1946c535..54cff306ed3c12f4a1ef996c87f71e44b0d42262 100644 (file)
@@ -10,9 +10,9 @@ IN: compiler.cfg.block-joining
 ! before stack analysis.
 : join-block? ( bb -- ? )
     {
-        [ kill-block? not ]
+        [ kill-block?>> not ]
         [ predecessors>> length 1 = ]
-        [ predecessor kill-block? not ]
+        [ predecessor kill-block?>> not ]
         [ predecessor successors>> length 1 = ]
         [ [ predecessor ] keep back-edge? not ]
     } 1&& ;
index 1daabf6f0efaee6fdb49b0124121ab1f3a2901da..b6cde4d43560783ee6d896c092a59634f2056981 100644 (file)
@@ -1,9 +1,10 @@
-! Copyright (C) 2009 Doug Coleman, Slava Pestov.
+! Copyright (C) 2009, 2010 Doug Coleman, Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors combinators.short-circuit kernel math math.order
-sequences assocs namespaces vectors fry arrays splitting
-compiler.cfg.def-use compiler.cfg compiler.cfg.rpo compiler.cfg.predecessors
-compiler.cfg.renaming compiler.cfg.instructions compiler.cfg.utilities ;
+USING: accessors combinators combinators.short-circuit kernel
+math math.order sequences assocs namespaces vectors fry arrays
+splitting compiler.cfg.def-use compiler.cfg compiler.cfg.rpo
+compiler.cfg.predecessors compiler.cfg.renaming
+compiler.cfg.instructions compiler.cfg.utilities ;
 IN: compiler.cfg.branch-splitting
 
 : clone-instructions ( insns -- insns' )
@@ -15,10 +16,12 @@ IN: compiler.cfg.branch-splitting
     ! 'back-edge?' work.
     <basic-block>
         swap
-        [ instructions>> clone-instructions >>instructions ]
-        [ successors>> clone >>successors ]
-        [ number>> >>number ]
-        tri ;
+        {
+            [ instructions>> clone-instructions >>instructions ]
+            [ successors>> clone >>successors ]
+            [ kill-block?>> >>kill-block? ]
+            [ number>> >>number ]
+        } cleave ;
 
 : new-blocks ( bb -- copies )
     dup predecessors>> [
index 8f98ab7adde64162a9765a24b61b143eb9609e5b..747e0f54cfe0c51a4ba00776727a60e35da04a3b 100644 (file)
@@ -14,13 +14,7 @@ GENERIC: compute-stack-frame* ( insn -- )
     frame-required? on
     stack-frame [ max-stack-frame ] change ;
 
-UNION: stack-frame-insn
-    ##alien-invoke
-    ##alien-indirect
-    ##alien-assembly
-    ##alien-callback ;
-
-M: stack-frame-insn compute-stack-frame*
+M: ##stack-frame compute-stack-frame*
     stack-frame>> request-stack-frame ;
 
 M: ##call compute-stack-frame* drop frame-required? on ;
diff --git a/basis/compiler/cfg/builder/alien/alien.factor b/basis/compiler/cfg/builder/alien/alien.factor
new file mode 100644 (file)
index 0000000..8bdf4cc
--- /dev/null
@@ -0,0 +1,293 @@
+! Copyright (C) 2008, 2010 Slava Pestov.\r
+! See http://factorcode.org/license.txt for BSD license.\r
+USING: accessors arrays layouts math math.order math.parser\r
+combinators fry sequences locals alien alien.private\r
+alien.strings alien.c-types alien.libraries classes.struct\r
+namespaces kernel strings libc quotations cpu.architecture\r
+compiler.alien compiler.utilities compiler.tree compiler.cfg\r
+compiler.cfg.builder compiler.cfg.builder.blocks\r
+compiler.cfg.instructions compiler.cfg.stack-frame\r
+compiler.cfg.stacks ;\r
+FROM: compiler.errors => no-such-symbol no-such-library ;\r
+IN: compiler.cfg.builder.alien\r
+\r
+GENERIC: next-fastcall-param ( rep -- )\r
+\r
+: ?dummy-stack-params ( rep -- )\r
+    dummy-stack-params? [ rep-size cell align stack-params +@ ] [ drop ] if ;\r
+\r
+: ?dummy-int-params ( rep -- )\r
+    dummy-int-params? [ rep-size cell /i 1 max int-regs +@ ] [ drop ] if ;\r
+\r
+: ?dummy-fp-params ( rep -- )\r
+    drop dummy-fp-params? [ float-regs inc ] when ;\r
+\r
+M: int-rep next-fastcall-param\r
+    int-regs inc [ ?dummy-stack-params ] [ ?dummy-fp-params ] bi ;\r
+\r
+M: float-rep next-fastcall-param\r
+    float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;\r
+\r
+M: double-rep next-fastcall-param\r
+    float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;\r
+\r
+GENERIC# reg-class-full? 1 ( reg-class abi -- ? )\r
+\r
+M: stack-params reg-class-full? 2drop t ;\r
+\r
+M: reg-class reg-class-full?\r
+    [ get ] swap '[ _ param-regs length ] bi >= ;\r
+\r
+: alloc-stack-param ( rep -- n reg-class rep )\r
+    stack-params get\r
+    [ rep-size cell align stack-params +@ ] dip\r
+    stack-params dup ;\r
+\r
+: alloc-fastcall-param ( rep -- n reg-class rep )\r
+    [ [ reg-class-of get ] [ reg-class-of ] [ next-fastcall-param ] tri ] keep ;\r
+\r
+:: alloc-parameter ( rep abi -- reg rep )\r
+    rep dup reg-class-of abi reg-class-full?\r
+    [ alloc-stack-param ] [ alloc-fastcall-param ] if\r
+    [ abi param-reg ] dip ;\r
+\r
+: reset-fastcall-counts ( -- )\r
+    { int-regs float-regs stack-params } [ 0 swap set ] each ;\r
+\r
+: with-param-regs ( quot -- )\r
+    #! In quot you can call alloc-parameter\r
+    [ reset-fastcall-counts call ] with-scope ; inline\r
+\r
+:: move-parameters ( params word -- )\r
+    #! Moves values from C stack to registers (if word is\r
+    #! ##load-param-reg) and registers to C stack (if word is\r
+    #! ##save-param-reg).\r
+    0 params alien-parameters flatten-c-types [\r
+        [ params abi>> alloc-parameter word execute( offset reg rep -- ) ]\r
+        [ rep-size cell align + ]\r
+        2bi\r
+    ] each drop ; inline\r
+\r
+: parameter-offsets ( types -- offsets )\r
+    0 [ stack-size + ] accumulate nip ;\r
+\r
+: each-parameter ( parameters quot -- )\r
+    [ [ parameter-offsets ] keep ] dip 2each ; inline\r
+\r
+: reverse-each-parameter ( parameters quot -- )\r
+    [ [ parameter-offsets ] keep ] dip 2reverse-each ; inline\r
+\r
+: prepare-unbox-parameters ( parameters -- offsets types indices )\r
+    [ parameter-offsets ] [ ] [ length iota <reversed> ] tri ;\r
+\r
+GENERIC: unbox-parameter ( n c-type -- )\r
+\r
+M: c-type unbox-parameter\r
+    [ rep>> ] [ unboxer>> ] bi ##unbox ;\r
+\r
+M: long-long-type unbox-parameter\r
+    unboxer>> ##unbox-long-long ;\r
+\r
+M: struct-c-type unbox-parameter\r
+    [ ##unbox-large-struct ] [ base-type unbox-parameter ] if-value-struct ;\r
+\r
+: unbox-parameters ( offset node -- )\r
+    parameters>> swap\r
+    '[\r
+        prepare-unbox-parameters\r
+        [ ##pop-stack [ _ + ] dip base-type unbox-parameter ] 3each\r
+    ]\r
+    [ length neg ##inc-d ]\r
+    bi ;\r
+\r
+: prepare-box-struct ( node -- offset )\r
+    #! Return offset on C stack where to store unboxed\r
+    #! parameters. If the C function is returning a structure,\r
+    #! the first parameter is an implicit target area pointer,\r
+    #! so we need to use a different offset.\r
+    return>> large-struct?\r
+    [ ##prepare-box-struct cell ] [ 0 ] if ;\r
+\r
+: objects>registers ( params -- )\r
+    #! Generate code for unboxing a list of C types, then\r
+    #! generate code for moving these parameters to registers on\r
+    #! architectures where parameters are passed in registers.\r
+    [\r
+        [ prepare-box-struct ] keep\r
+        [ unbox-parameters ] keep\r
+        \ ##load-param-reg move-parameters\r
+    ] with-param-regs ;\r
+\r
+GENERIC: box-return ( c-type -- )\r
+\r
+M: c-type box-return\r
+    [ f ] dip [ rep>> ] [ boxer>> ] bi ##box ;\r
+\r
+M: long-long-type box-return\r
+    [ f ] dip boxer>> ##box-long-long ;\r
+\r
+M: struct-c-type box-return\r
+    [ ##box-small-struct ] [ ##box-large-struct ] if-small-struct ;\r
+\r
+: box-return* ( node -- )\r
+    return>> [ ] [ base-type box-return ##push-stack ] if-void ;\r
+\r
+GENERIC# dlsym-valid? 1 ( symbols dll -- ? )\r
+\r
+M: string dlsym-valid? dlsym ;\r
+\r
+M: array dlsym-valid? '[ _ dlsym ] any? ;\r
+\r
+: check-dlsym ( symbols dll -- )\r
+    dup dll-valid? [\r
+        dupd dlsym-valid?\r
+        [ drop ] [ cfg get word>> no-such-symbol ] if\r
+    ] [ dll-path cfg get word>> no-such-library drop ] if ;\r
+\r
+: decorated-symbol ( params -- symbols )\r
+    [ function>> ] [ parameters>> [ stack-size ] map-sum number>string ] bi\r
+    {\r
+        [ drop ]\r
+        [ "@" glue ]\r
+        [ "@" glue "_" prepend ]\r
+        [ "@" glue "@" prepend ]\r
+    } 2cleave\r
+    4array ;\r
+\r
+: alien-invoke-dlsym ( params -- symbols dll )\r
+    [ dup abi>> callee-cleanup? [ decorated-symbol ] [ function>> ] if ]\r
+    [ library>> load-library ]\r
+    bi 2dup check-dlsym ;\r
+\r
+: return-size ( ctype -- n )\r
+    #! Amount of space we reserve for a return value.\r
+    {\r
+        { [ dup c-struct? not ] [ drop 0 ] }\r
+        { [ dup large-struct? not ] [ drop 2 cells ] }\r
+        [ heap-size ]\r
+    } cond ;\r
+\r
+: <alien-stack-frame> ( params -- stack-frame )\r
+    stack-frame new\r
+        swap\r
+        [ return>> return-size >>return ]\r
+        [ alien-parameters [ stack-size ] map-sum >>params ] bi\r
+        t >>calls-vm? ;\r
+\r
+: alien-node-height ( params -- )\r
+    [ out-d>> length ] [ in-d>> length ] bi - adjust-d ;\r
+\r
+: emit-alien-node ( node quot -- )\r
+    '[\r
+        make-kill-block\r
+        params>>\r
+        [ <alien-stack-frame> ##stack-frame ]\r
+        _\r
+        [ alien-node-height ]\r
+        tri\r
+    ] emit-trivial-block ; inline\r
+\r
+M: #alien-invoke emit-node\r
+    [\r
+        ! Unbox parameters\r
+        dup objects>registers\r
+        ! Call function\r
+        dup alien-invoke-dlsym ##alien-invoke\r
+        ! Box return value\r
+        dup ##cleanup\r
+        box-return*\r
+    ] emit-alien-node ;\r
+\r
+M: #alien-indirect emit-node\r
+    [\r
+        ! Save alien at top of stack to temporary storage\r
+        ##prepare-alien-indirect\r
+        ! Unbox parameters\r
+        dup objects>registers\r
+        ! Call alien in temporary storage\r
+        ##alien-indirect\r
+        ! Box return value\r
+        dup ##cleanup\r
+        box-return*\r
+    ] emit-alien-node ;\r
+\r
+M: #alien-assembly emit-node\r
+    [\r
+        ! Unbox parameters\r
+        dup objects>registers\r
+        ! Generate assembly\r
+        dup quot>> ##alien-assembly\r
+        ! Box return value\r
+        box-return*\r
+    ] emit-alien-node ;\r
+\r
+GENERIC: box-parameter ( n c-type -- )\r
+\r
+M: c-type box-parameter\r
+    [ rep>> ] [ boxer>> ] bi ##box ;\r
+\r
+M: long-long-type box-parameter\r
+    boxer>> ##box-long-long ;\r
+\r
+M: struct-c-type box-parameter\r
+    [ ##box-large-struct ] [ base-type box-parameter ] if-value-struct ;\r
+\r
+: box-parameters ( params -- )\r
+    alien-parameters\r
+    [ base-type box-parameter ##push-context-stack ] each-parameter ;\r
+\r
+: registers>objects ( node -- )\r
+    ! Generate code for boxing input parameters in a callback.\r
+    [\r
+        dup \ ##save-param-reg move-parameters\r
+        ##begin-callback\r
+        box-parameters\r
+    ] with-param-regs ;\r
+\r
+: callback-return-quot ( ctype -- quot )\r
+    return>> {\r
+        { [ dup void? ] [ drop [ ] ] }\r
+        { [ dup large-struct? ] [ heap-size '[ _ memcpy ] ] }\r
+        [ c-type c-type-unboxer-quot ]\r
+    } cond ;\r
+\r
+: callback-prep-quot ( params -- quot )\r
+    parameters>> [ c-type c-type-boxer-quot ] map spread>quot ;\r
+\r
+: wrap-callback-quot ( params -- quot )\r
+    [ callback-prep-quot ] [ quot>> ] [ callback-return-quot ] tri 3append\r
+     yield-hook get\r
+     '[ _ _ do-callback ]\r
+     >quotation ;\r
+\r
+GENERIC: unbox-return ( c-type -- )\r
+\r
+M: c-type unbox-return\r
+    [ f ] dip [ rep>> ] [ unboxer>> ] bi ##unbox ;\r
+\r
+M: long-long-type unbox-return\r
+    [ f ] dip unboxer>> ##unbox-long-long ;\r
+\r
+M: struct-c-type unbox-return\r
+    [ ##unbox-small-struct ] [ ##unbox-large-struct ] if-small-struct ;\r
+\r
+M: #alien-callback emit-node\r
+    dup params>> xt>> dup\r
+    [\r
+        ##prologue\r
+        [\r
+            [ registers>objects ]\r
+            [ wrap-callback-quot ##alien-callback ]\r
+            [\r
+                alien-return [ ##end-callback ] [\r
+                    ##pop-context-stack\r
+                    ##to-nv\r
+                    ##end-callback\r
+                    ##from-nv\r
+                    base-type unbox-return\r
+                ] if-void\r
+            ] tri\r
+        ] emit-alien-node\r
+        ##epilogue\r
+        ##return\r
+    ] with-cfg-builder ;\r
index 8e96255bdd05c70014576eeb0bd0e344ffe087e6..293c3fe09b21fc63f8cc4a3477ae32a13c2c82e5 100644 (file)
@@ -1,4 +1,4 @@
-! Copyright (C) 2009 Slava Pestov.
+! Copyright (C) 2009, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors arrays fry kernel make math namespaces sequences
 compiler.cfg compiler.cfg.instructions compiler.cfg.stacks
@@ -31,6 +31,9 @@ IN: compiler.cfg.builder.blocks
     call
     ##branch begin-basic-block ; inline
 
+: make-kill-block ( -- )
+    basic-block get t >>kill-block? drop ;
+
 : call-height ( #call -- n )
     [ out-d>> length ] [ in-d>> length ] bi - ;
 
@@ -38,6 +41,7 @@ IN: compiler.cfg.builder.blocks
     [
         [ word>> ##call ]
         [ call-height adjust-d ] bi
+        make-kill-block
     ] emit-trivial-block ;
 
 : begin-branch ( -- ) clone-current-height (begin-basic-block) ;
@@ -66,7 +70,7 @@ IN: compiler.cfg.builder.blocks
     [ ] find nip [ second current-height set ] [ end-basic-block ] if* ;
 
 : emit-conditional ( branches -- )
-    ! branchies is a sequence of pairs as above
+    ! branches is a sequence of pairs as above
     end-basic-block
     [ merge-heights begin-basic-block ]
     [ set-successors ]
index c0ba1144a54102b9ef082a028b2ed043f5fef611..059a7f2215c2adaaae13f5cc7e50c8c5c81fd1b9 100644 (file)
@@ -57,6 +57,7 @@ GENERIC: emit-node ( node -- )
     [ basic-block get [ emit-node ] [ drop ] if ] each ;
 
 : begin-word ( -- )
+    make-kill-block
     ##prologue
     ##branch
     begin-basic-block ;
@@ -82,8 +83,12 @@ GENERIC: emit-node ( node -- )
 : emit-call ( word height -- )
     over loops get key?
     [ drop loops get at emit-loop-call ]
-    [ [ [ ##call ] [ adjust-d ] bi* ] emit-trivial-block ]
-    if ;
+    [
+        [
+            [ ##call ] [ adjust-d ] bi*
+            make-kill-block
+        ] emit-trivial-block
+    ] if ;
 
 ! #recursive
 : recursive-height ( #recursive -- n )
@@ -195,7 +200,11 @@ M: #shuffle emit-node
 
 ! #return
 : emit-return ( -- )
-    ##branch begin-basic-block ##epilogue ##return ;
+    ##branch
+    begin-basic-block
+    make-kill-block
+    ##epilogue
+    ##return ;
 
 M: #return emit-node drop emit-return ;
 
@@ -205,49 +214,6 @@ M: #return-recursive emit-node
 ! #terminate
 M: #terminate emit-node drop ##no-tco end-basic-block ;
 
-! FFI
-: return-size ( ctype -- n )
-    #! Amount of space we reserve for a return value.
-    {
-        { [ dup c-struct? not ] [ drop 0 ] }
-        { [ dup large-struct? not ] [ drop 2 cells ] }
-        [ heap-size ]
-    } cond ;
-
-: <alien-stack-frame> ( params -- stack-frame )
-    stack-frame new
-        swap
-        [ return>> return-size >>return ]
-        [ alien-parameters [ stack-size ] map-sum >>params ] bi
-        t >>calls-vm? ;
-
-: alien-node-height ( params -- )
-    [ out-d>> length ] [ in-d>> length ] bi - adjust-d ;
-
-: emit-alien-node ( node quot -- )
-    [
-        [ params>> dup dup <alien-stack-frame> ] dip call
-        alien-node-height
-    ] emit-trivial-block ; inline
-
-M: #alien-invoke emit-node
-    [ ##alien-invoke ] emit-alien-node ;
-
-M: #alien-indirect emit-node
-    [ ##alien-indirect ] emit-alien-node ;
-
-M: #alien-assembly emit-node
-    [ ##alien-assembly ] emit-alien-node ;
-
-M: #alien-callback emit-node
-    dup params>> xt>> dup
-    [
-        ##prologue
-        [ ##alien-callback ] emit-alien-node
-        ##epilogue
-        ##return
-    ] with-cfg-builder ;
-
 ! No-op nodes
 M: #introduce emit-node drop ;
 
index c49d63850962ca9e5462bae022de2ba51c39ec21..5f5283bcd51de173509b8bc16973c078a1727686 100644 (file)
@@ -9,6 +9,7 @@ number
 { instructions vector }
 { successors vector }
 { predecessors vector }
+{ kill-block? boolean }
 { unlikely? boolean } ;
 
 : <basic-block> ( -- bb )
index d7a48a1511a6b0ff84e4f4828090839bc710b6d2..f4fee8b7b229172a4254b2973a77762fce4c27e0 100644 (file)
@@ -7,50 +7,11 @@ compiler.cfg.utilities compiler.cfg.finalization
 compiler.utilities ;
 IN: compiler.cfg.checker
 
-! Check invariants
-
-ERROR: bad-kill-block bb ;
-
-: check-kill-block ( bb -- )
-    dup instructions>> dup penultimate ##epilogue? [
-        {
-            [ length 2 = ]
-            [ last { [ ##return? ] [ ##jump? ] } 1|| ]
-        } 1&&
-    ] [ last ##branch? ] if
-    [ drop ] [ bad-kill-block ] if ;
-
-ERROR: last-insn-not-a-jump bb ;
-
-: check-last-instruction ( bb -- )
-    dup instructions>> last {
-        [ ##branch? ]
-        [ ##dispatch? ]
-        [ conditional-branch-insn? ]
-        [ ##no-tco? ]
-    } 1|| [ drop ] [ last-insn-not-a-jump ] if ;
-
-ERROR: bad-kill-insn bb ;
-
-: check-kill-instructions ( bb -- )
-    dup instructions>> [ kill-vreg-insn? ] any?
-    [ bad-kill-insn ] [ drop ] if ;
-
-: check-normal-block ( bb -- )
-    [ check-last-instruction ]
-    [ check-kill-instructions ]
-    bi ;
-
 ERROR: bad-successors ;
 
 : check-successors ( bb -- )
     dup successors>> [ predecessors>> member-eq? ] with all?
     [ bad-successors ] unless ;
 
-: check-basic-block ( bb -- )
-    [ dup kill-block? [ check-kill-block ] [ check-normal-block ] if ]
-    [ check-successors ]
-    bi ;
-
 : check-cfg ( cfg -- )
-    [ check-basic-block ] each-basic-block ;
+    [ check-successors ] each-basic-block ;
index dde44fd15ddcfe8306242491e040274f2fa06c0e..553b84383334cbd60bca6567c2caf8895d03a503 100644 (file)
@@ -18,27 +18,21 @@ MIXIN: dataflow-analysis
 : <dfa-worklist> ( cfg dfa -- queue )
     block-order <hashed-dlist> [ push-all-front ] keep ;
 
-GENERIC# compute-in-set 2 ( bb out-sets dfa -- set )
-
-M: kill-block compute-in-set 3drop f ;
-
-M:: basic-block compute-in-set ( bb out-sets dfa -- set )
+:: compute-in-set ( bb out-sets dfa -- set )
     ! Only consider initialized sets.
-    bb dfa predecessors
-    [ out-sets key? ] filter
-    [ out-sets at ] map
-    bb dfa join-sets ;
+    bb kill-block?>> [ f ] [
+        bb dfa predecessors
+        [ out-sets key? ] filter
+        [ out-sets at ] map
+        bb dfa join-sets
+    ] if ;
 
 :: update-in-set ( bb in-sets out-sets dfa -- ? )
     bb out-sets dfa compute-in-set
     bb in-sets maybe-set-at ; inline
 
-GENERIC# compute-out-set 2 ( bb out-sets dfa -- set )
-
-M: kill-block compute-out-set 3drop f ;
-
-M:: basic-block compute-out-set ( bb in-sets dfa -- set )
-    bb in-sets at bb dfa transfer-set ;
+:: compute-out-set ( bb in-sets dfa -- set )
+    bb kill-block?>> [ f ] [ bb in-sets at bb dfa transfer-set ] if ;
 
 :: update-out-set ( bb in-sets out-sets dfa -- ? )
     bb in-sets dfa compute-out-set
index 91d01adb839038b219dc2f5d3513319675a0ef74..87055eb55026dbb3ed8ce665bc488fb9e2a76d90 100644 (file)
@@ -609,17 +609,73 @@ use: src/tagged-rep
 literal: offset ;
 
 ! FFI
+INSN: ##stack-frame
+literal: stack-frame ;
+
+INSN: ##box
+literal: n rep boxer ;
+
+INSN: ##box-long-long
+literal: n boxer ;
+
+INSN: ##box-small-struct
+literal: c-type ;
+
+INSN: ##box-large-struct
+literal: n c-type ;
+
+INSN: ##unbox
+literal: n rep unboxer ;
+
+INSN: ##unbox-long-long
+literal: n unboxer ;
+
+INSN: ##unbox-large-struct
+literal: n c-type ;
+
+INSN: ##unbox-small-struct
+literal: c-type ;
+
+INSN: ##pop-stack
+literal: n ;
+
+INSN: ##pop-context-stack ;
+
+INSN: ##prepare-box-struct ;
+
+INSN: ##load-param-reg
+literal: offset reg rep ;
+
+INSN: ##push-stack ;
+
 INSN: ##alien-invoke
-literal: params stack-frame ;
+literal: symbols dll ;
 
-INSN: ##alien-indirect
-literal: params stack-frame ;
+INSN: ##cleanup
+literal: params ;
+
+INSN: ##prepare-alien-indirect ;
+
+INSN: ##alien-indirect ;
 
 INSN: ##alien-assembly
-literal: params stack-frame ;
+literal: quot ;
+
+INSN: ##push-context-stack ;
+
+INSN: ##save-param-reg
+literal: offset reg rep ;
+
+INSN: ##begin-callback ;
 
 INSN: ##alien-callback
-literal: params stack-frame ;
+literal: quot ;
+
+INSN: ##end-callback ;
+
+INSN: ##to-nv ;
+
+INSN: ##from-nv ;
 
 ! Control flow
 INSN: ##phi
@@ -758,15 +814,6 @@ UNION: clobber-insn
 ##unary-float-function
 ##binary-float-function ;
 
-! Instructions that kill all live vregs
-UNION: kill-vreg-insn
-##call
-##prologue
-##epilogue
-##alien-invoke
-##alien-indirect
-##alien-callback ;
-
 ! Instructions that have complex expansions and require that the
 ! output registers are not equal to any of the input registers
 UNION: def-is-use-insn
index b9cfac3b92f382daf0199c397df3dae98473712c..6b87ca8fd6f727414233e7abf26a6fef7dbc9cb9 100644 (file)
@@ -51,7 +51,11 @@ IN: compiler.cfg.intrinsics.fixnum
     [ ds-drop ds-drop ds-push ] with-branch ;
 
 : emit-overflow-case ( word -- final-bb )
-    [ ##call -1 adjust-d ] with-branch ;
+    [
+        ##call
+        -1 adjust-d
+        make-kill-block
+    ] with-branch ;
 
 : emit-fixnum-overflow-op ( quot word -- )
     ! Inputs to the final instruction need to be copied because
index b0da0d190ac2951d40864d6242fafffffa284a72..06444c66f84134d3754e4bddbecbce83f9f7cc0d 100644 (file)
@@ -90,15 +90,14 @@ M: ##copy conversions-for-insn , ;
 M: insn conversions-for-insn , ;
 
 : conversions-for-block ( bb -- )
-    dup kill-block? [ drop ] [
+    [
         [
-            [
-                H{ } clone alternatives set
-                [ conversions-for-insn ] each
-            ] V{ } make
-        ] change-instructions drop
-    ] if ;
+            alternatives get clear-assoc
+            [ conversions-for-insn ] each
+        ] V{ } make
+    ] change-instructions drop ;
 
 : insert-conversions ( cfg -- )
+    H{ } clone alternatives set
     V{ } clone renaming-set set
     [ conversions-for-block ] each-basic-block ;
index a76beca1811d045d331b2c877dd5e8c5a9dbaa13..6d449540f2a082b53a9dba9841be600e4f8e9a31 100644 (file)
@@ -36,8 +36,10 @@ SYMBOL: visited
     [ reverse-post-order ] dip each ; inline
 
 : optimize-basic-block ( bb quot -- )
-    [ drop basic-block set ]
-    [ change-instructions drop ] 2bi ; inline
+    over kill-block?>> [ 2drop ] [
+        over basic-block set
+        change-instructions drop
+    ] if ; inline
 
 : simple-optimization ( ... cfg quot: ( ... insns -- ... insns' ) -- ... )
     '[ _ optimize-basic-block ] each-basic-block ; inline
index 1c6c6987f7885d843ea04ac86ae2cc866e7e2990..04e4142a35e4fd13290e02bb816955c8c830c1fe 100644 (file)
@@ -1,9 +1,10 @@
 ! Copyright (C) 2009, 2010 Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays assocs compiler.cfg.def-use
-compiler.cfg.dependence compiler.cfg.instructions
-compiler.cfg.liveness compiler.cfg.rpo cpu.architecture fry
-kernel locals make math namespaces sequences sets ;
+USING: accessors arrays assocs fry kernel locals make math
+namespaces sequences sets combinators.short-circuit
+compiler.cfg.def-use compiler.cfg.dependence
+compiler.cfg.instructions compiler.cfg.liveness compiler.cfg.rpo
+cpu.architecture ;
 IN: compiler.cfg.scheduling
 
 ! Instruction scheduling to reduce register pressure, from:
@@ -128,7 +129,6 @@ ERROR: definition-after-usage vreg old-bb new-bb ;
 
 : schedule-instructions ( cfg -- cfg' )
     dup [
-        dup might-spill?
-        [ schedule-block ]
-        [ drop ] if
+        dup { [ kill-block?>> not ] [ might-spill? ] } 1&&
+        [ schedule-block ] [ drop ] if
     ] each-basic-block ;
index 41512f206febd08865a3af7ebab00166782615f6..a35d82bbb58ea3a5a115362a8cba6e8f638b3af0 100644 (file)
@@ -43,7 +43,7 @@ ERROR: bad-peek dst loc ;
 : visit-edge ( from to -- )
     ! If both blocks are subroutine calls, don't bother
     ! computing anything.
-    2dup [ kill-block? ] both? [ 2drop ] [
+    2dup [ kill-block?>> ] both? [ 2drop ] [
         2dup [ [ insert-replaces ] [ insert-peeks ] 2bi ##branch ] V{ } make
         [ 2drop ] [ insert-basic-block ] if-empty
     ] if ;
index 0158c0546caccde1648c50e870293c1d241f936e..38ca9a950f497125469e44dc8bcf28fb6fb08f75 100644 (file)
@@ -6,12 +6,6 @@ sets vectors fry arrays compiler.cfg compiler.cfg.instructions
 compiler.cfg.rpo compiler.utilities ;
 IN: compiler.cfg.utilities
 
-PREDICATE: kill-block < basic-block
-    instructions>> {
-        [ length 2 >= ]
-        [ penultimate kill-vreg-insn? ]
-    } 1&& ;
-
 : back-edge? ( from to -- ? )
     [ number>> ] bi@ >= ;
 
diff --git a/basis/compiler/codegen/alien/alien.factor b/basis/compiler/codegen/alien/alien.factor
deleted file mode 100644 (file)
index 3af2203..0000000
+++ /dev/null
@@ -1,207 +0,0 @@
-! Copyright (C) 2008, 2010 Slava Pestov.
-! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien alien.complex alien.c-types
-alien.libraries alien.private alien.strings arrays
-classes.struct combinators compiler.alien
-compiler.cfg.instructions compiler.codegen
-compiler.codegen.fixup compiler.errors compiler.utilities
-cpu.architecture fry kernel layouts libc locals make math
-math.order math.parser namespaces quotations sequences strings
-system ;
-FROM: compiler.errors => no-such-symbol ;
-IN: compiler.codegen.alien
-
-! ##alien-invoke
-GENERIC: next-fastcall-param ( rep -- )
-
-: ?dummy-stack-params ( rep -- )
-    dummy-stack-params? [ rep-size cell align stack-params +@ ] [ drop ] if ;
-
-: ?dummy-int-params ( rep -- )
-    dummy-int-params? [ rep-size cell /i 1 max int-regs +@ ] [ drop ] if ;
-
-: ?dummy-fp-params ( rep -- )
-    drop dummy-fp-params? [ float-regs inc ] when ;
-
-M: int-rep next-fastcall-param
-    int-regs inc [ ?dummy-stack-params ] [ ?dummy-fp-params ] bi ;
-
-M: float-rep next-fastcall-param
-    float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;
-
-M: double-rep next-fastcall-param
-    float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;
-
-GENERIC# reg-class-full? 1 ( reg-class abi -- ? )
-
-M: stack-params reg-class-full? 2drop t ;
-
-M: reg-class reg-class-full?
-    [ get ] swap '[ _ param-regs length ] bi >= ;
-
-: alloc-stack-param ( rep -- n reg-class rep )
-    stack-params get
-    [ rep-size cell align stack-params +@ ] dip
-    stack-params dup ;
-
-: alloc-fastcall-param ( rep -- n reg-class rep )
-    [ [ reg-class-of get ] [ reg-class-of ] [ next-fastcall-param ] tri ] keep ;
-
-:: alloc-parameter ( rep abi -- reg rep )
-    rep dup reg-class-of abi reg-class-full?
-    [ alloc-stack-param ] [ alloc-fastcall-param ] if
-    [ abi param-reg ] dip ;
-
-: reset-fastcall-counts ( -- )
-    { int-regs float-regs stack-params } [ 0 swap set ] each ;
-
-: with-param-regs ( quot -- )
-    #! In quot you can call alloc-parameter
-    [ reset-fastcall-counts call ] with-scope ; inline
-
-:: move-parameters ( params word -- )
-    #! Moves values from C stack to registers (if word is
-    #! %load-param-reg) and registers to C stack (if word is
-    #! %save-param-reg).
-    0 params alien-parameters flatten-c-types [
-        [ params abi>> alloc-parameter word execute( offset reg rep -- ) ]
-        [ rep-size cell align + ]
-        2bi
-    ] each drop ; inline
-
-: parameter-offsets ( types -- offsets )
-    0 [ stack-size + ] accumulate nip ;
-
-: each-parameter ( parameters quot -- )
-    [ [ parameter-offsets ] keep ] dip 2each ; inline
-
-: reverse-each-parameter ( parameters quot -- )
-    [ [ parameter-offsets ] keep ] dip 2reverse-each ; inline
-
-: prepare-unbox-parameters ( parameters -- offsets types indices )
-    [ parameter-offsets ] [ ] [ length iota <reversed> ] tri ;
-
-: unbox-parameters ( offset node -- )
-    parameters>> swap
-    '[ prepare-unbox-parameters [ %pop-stack [ _ + ] dip unbox-parameter ] 3each ]
-    [ length neg %inc-d ]
-    bi ;
-
-: prepare-box-struct ( node -- offset )
-    #! Return offset on C stack where to store unboxed
-    #! parameters. If the C function is returning a structure,
-    #! the first parameter is an implicit target area pointer,
-    #! so we need to use a different offset.
-    return>> large-struct?
-    [ %prepare-box-struct cell ] [ 0 ] if ;
-
-: objects>registers ( params -- )
-    #! Generate code for unboxing a list of C types, then
-    #! generate code for moving these parameters to registers on
-    #! architectures where parameters are passed in registers.
-    [
-        [ prepare-box-struct ] keep
-        [ unbox-parameters ] keep
-        \ %load-param-reg move-parameters
-    ] with-param-regs ;
-
-: box-return* ( node -- )
-    return>> [ ] [ box-return %push-stack ] if-void ;
-
-GENERIC# dlsym-valid? 1 ( symbols dll -- ? )
-
-M: string dlsym-valid? dlsym ;
-
-M: array dlsym-valid? '[ _ dlsym ] any? ;
-
-: check-dlsym ( symbols dll -- )
-    dup dll-valid? [
-        dupd dlsym-valid?
-        [ drop ] [ compiling-word get no-such-symbol ] if
-    ] [
-        dll-path compiling-word get no-such-library drop
-    ] if ;
-
-: decorated-symbol ( params -- symbols )
-    [ function>> ] [ parameters>> [ stack-size ] map-sum number>string ] bi
-    {
-        [ drop ]
-        [ "@" glue ]
-        [ "@" glue "_" prepend ]
-        [ "@" glue "@" prepend ]
-    } 2cleave
-    4array ;
-
-: alien-invoke-dlsym ( params -- symbols dll )
-    [ dup abi>> callee-cleanup? [ decorated-symbol ] [ function>> ] if ]
-    [ library>> load-library ]
-    bi 2dup check-dlsym ;
-
-M: ##alien-invoke generate-insn
-    params>>
-    ! Unbox parameters
-    dup objects>registers
-    %prepare-var-args
-    ! Call function
-    dup alien-invoke-dlsym %alien-invoke
-    ! Box return value
-    dup %cleanup
-    box-return* ;
-
-M: ##alien-assembly generate-insn
-    params>>
-    ! Unbox parameters
-    dup objects>registers
-    %prepare-var-args
-    ! Generate assembly
-    dup quot>> call( -- )
-    ! Box return value
-    box-return* ;
-
-! ##alien-indirect
-M: ##alien-indirect generate-insn
-    params>>
-    ! Save alien at top of stack to temporary storage
-    %prepare-alien-indirect
-    ! Unbox parameters
-    dup objects>registers
-    %prepare-var-args
-    ! Call alien in temporary storage
-    %alien-indirect
-    ! Box return value
-    dup %cleanup
-    box-return* ;
-
-! ##alien-callback
-: box-parameters ( params -- )
-    alien-parameters [ box-parameter %push-context-stack ] each-parameter ;
-
-: registers>objects ( node -- )
-    ! Generate code for boxing input parameters in a callback.
-    [
-        dup \ %save-param-reg move-parameters
-        %begin-callback
-        box-parameters
-    ] with-param-regs ;
-
-: callback-return-quot ( ctype -- quot )
-    return>> {
-        { [ dup void? ] [ drop [ ] ] }
-        { [ dup large-struct? ] [ heap-size '[ _ memcpy ] ] }
-        [ c-type c-type-unboxer-quot ]
-    } cond ;
-
-: callback-prep-quot ( params -- quot )
-    parameters>> [ c-type c-type-boxer-quot ] map spread>quot ;
-
-: wrap-callback-quot ( params -- quot )
-    [ callback-prep-quot ] [ quot>> ] [ callback-return-quot ] tri 3append
-     yield-hook get
-     '[ _ _ do-callback ]
-     >quotation ;
-
-M: ##alien-callback generate-insn
-    params>>
-    [ registers>objects ]
-    [ wrap-callback-quot %alien-callback ]
-    [ alien-return [ %end-callback ] [ %end-callback-value ] if-void ] tri ;
diff --git a/basis/compiler/codegen/alien/authors.txt b/basis/compiler/codegen/alien/authors.txt
deleted file mode 100644 (file)
index 1901f27..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Slava Pestov
index d5e4987ee09453b82af0ef92838b9fd7e99ae641..5b2c52ce28ef8d346f0a3a755f8f27d142de385e 100755 (executable)
@@ -91,6 +91,8 @@ M: ##dispatch generate-insn
 ! Special cases
 M: ##no-tco generate-insn drop ;
 
+M: ##stack-frame generate-insn drop ;
+
 M: ##prologue generate-insn
     drop
     cfg get stack-frame>>
@@ -251,6 +253,7 @@ CODEGEN: ##call-gc %call-gc
 CODEGEN: ##spill %spill
 CODEGEN: ##reload %reload
 
+! Conditional branches
 <<
 
 SYNTAX: CONDITIONAL:
@@ -270,3 +273,31 @@ CONDITIONAL: ##check-nursery-branch %check-nursery-branch
 CONDITIONAL: ##fixnum-add %fixnum-add
 CONDITIONAL: ##fixnum-sub %fixnum-sub
 CONDITIONAL: ##fixnum-mul %fixnum-mul
+
+! FFI
+CODEGEN: ##box %box
+CODEGEN: ##box-long-long %box-long-long
+CODEGEN: ##box-large-struct %box-large-struct
+CODEGEN: ##box-small-struct %box-small-struct
+CODEGEN: ##unbox %unbox
+CODEGEN: ##unbox-long-long %unbox-long-long
+CODEGEN: ##unbox-large-struct %unbox-large-struct
+CODEGEN: ##unbox-small-struct %unbox-small-struct
+CODEGEN: ##pop-stack %pop-stack
+CODEGEN: ##pop-context-stack %pop-context-stack
+CODEGEN: ##prepare-box-struct %prepare-box-struct
+CODEGEN: ##load-param-reg %load-param-reg
+CODEGEN: ##push-stack %push-stack
+CODEGEN: ##alien-invoke %alien-invoke
+CODEGEN: ##cleanup %cleanup
+CODEGEN: ##prepare-alien-indirect %prepare-alien-indirect
+CODEGEN: ##alien-indirect %alien-indirect
+CODEGEN: ##push-context-stack %push-context-stack
+CODEGEN: ##save-param-reg %save-param-reg
+CODEGEN: ##begin-callback %begin-callback
+CODEGEN: ##alien-callback %alien-callback
+CODEGEN: ##end-callback %end-callback
+CODEGEN: ##to-nv %to-nv
+CODEGEN: ##from-nv %from-nv
+
+M: ##alien-assembly generate-insn quot>> call( -- ) ;
index 4c8a9ca61d0e652390e4724d03ba17204a4b4004..e4fd64505e36cee763218e7e170f58ac17c92797 100644 (file)
@@ -15,11 +15,11 @@ compiler.tree.optimizer
 
 compiler.cfg
 compiler.cfg.builder
+compiler.cfg.builder.alien
 compiler.cfg.optimizer
 compiler.cfg.finalization
 
-compiler.codegen
-compiler.codegen.alien ;
+compiler.codegen ;
 IN: compiler
 
 SYMBOL: compiled
index 7bbc0a904ff6a495bd2bb0be37b108df8e415016..bd770eb8def8a861849979862da7553cbfaf45f5 100755 (executable)
@@ -610,11 +610,6 @@ FUNCTION: short ffi_test_48 ( bool-field-test x ) ;
 [ 200 ] [ thread-callback-2 thread-callback-invoker ] unit-test
 [ 100 ] [ "p" get ?promise ] unit-test
 
-! Regression: calling an undefined function would raise a protection fault
-FUNCTION: void this_does_not_exist ( ) ;
-
-[ this_does_not_exist ] [ { "kernel-error" 9 f f } = ] must-fail-with
-
 ! More alien-assembly tests are in cpu.* vocabs
 : assembly-test-1 ( -- ) void { } cdecl [ ] alien-assembly ;
 
diff --git a/basis/compiler/tests/linkage-errors.factor b/basis/compiler/tests/linkage-errors.factor
new file mode 100644 (file)
index 0000000..fc59f65
--- /dev/null
@@ -0,0 +1,21 @@
+USING: tools.test namespaces assocs alien.syntax kernel\r
+compiler.errors accessors alien ;\r
+FROM: alien.libraries => add-library ;\r
+IN: compiler.tests.linkage-errors\r
+\r
+! Regression: calling an undefined function would raise a protection fault\r
+FUNCTION: void this_does_not_exist ( ) ;\r
+\r
+[ this_does_not_exist ] [ { "kernel-error" 9 f f } = ] must-fail-with\r
+\r
+[ T{ no-such-symbol { name "this_does_not_exist" } } ]\r
+[ \ this_does_not_exist linkage-errors get at error>> ] unit-test\r
+\r
+<< "no_such_library" "no_such_library" cdecl add-library >>\r
+\r
+LIBRARY: no_such_library\r
+\r
+FUNCTION: void no_such_function ( ) ;\r
+\r
+[ T{ no-such-library { name "no_such_library" } } ]\r
+[ \ no_such_function linkage-errors get at error>> ] unit-test\r
index 19a9b027859980b83bdceccfe46422e807420b79..6657fd8c8501dd2ed8231d2852f7b80b0b191fd4 100644 (file)
@@ -624,7 +624,9 @@ HOOK: %alien-callback cpu ( quot -- )
 
 HOOK: %end-callback cpu ( -- )
 
-HOOK: %end-callback-value cpu ( c-type -- )
+HOOK: %to-nv cpu ( -- )
+
+HOOK: %from-nv cpu ( -- )
 
 HOOK: stack-cleanup cpu ( params -- n )
 
index 7e23a0b9c1c9d0ce047c888eb18f4095be6a62d4..ce7a4e13eb01dc0b6f36be5dd9442826199571c9 100644 (file)
@@ -836,14 +836,9 @@ M: ppc %end-callback ( -- )
     3 %load-vm-addr
     "end_callback" f %alien-invoke ;
 
-M: ppc %end-callback-value ( ctype -- )
-    ! Save top of data stack
-    16 ds-reg 0 LWZ
-    %end-callback
-    ! Restore top of data stack
-    3 16 MR
-    ! Unbox former top of data stack to return registers
-    unbox-return ;
+M: ppc %to-nv ( -- ) 16 3 MR ;
+
+M: ppc %from-nv ( -- ) 3 16 MR ;
 
 M: ppc %unbox-small-struct ( size -- )
     heap-size cell align cell /i {
index 8618affaedcd8f5a47d9eda109b7c1cb226498f4..619f4fcad4796c454884b75a4f8b3ebef6d0a906 100755 (executable)
@@ -283,14 +283,9 @@ M: x86.32 %end-callback ( -- )
     0 save-vm-ptr
     "end_callback" f %alien-invoke ;
 
-M: x86.32 %end-callback-value ( ctype -- )
-    %pop-context-stack
-    4 stack@ EAX MOV
-    %end-callback
-    ! Place former top of data stack back in EAX
-    EAX 4 stack@ MOV
-    ! Unbox EAX
-    unbox-return ;
+M: x86.32 %to-nv ( -- ) 4 stack@ EAX MOV ;
+
+M: x86.32 %from-nv ( -- ) EAX 4 stack@ MOV ;
 
 GENERIC: float-function-param ( stack-slot dst src -- )
 
index 5baeed81b85968355727637fd17e432e0a4b90bd..73a4df5b45f712c3c5fa739f2248481e9d9a45d1 100644 (file)
@@ -249,13 +249,9 @@ M: x86.64 %end-callback ( -- )
     param-reg-0 %mov-vm-ptr
     "end_callback" f %alien-invoke ;
 
-M: x86.64 %end-callback-value ( ctype -- )
-    %pop-context-stack
-    nv-reg param-reg-0 MOV
-    %end-callback
-    param-reg-0 nv-reg MOV
-    ! Unbox former top of data stack to return registers
-    unbox-return ;
+M: x86.64 %to-nv ( -- ) nv-reg param-reg-0 MOV ;
+
+M: x86.64 %from-nv ( -- ) param-reg-0 nv-reg MOV ;
 
 : float-function-param ( i src -- )
     [ float-regs cdecl param-regs nth ] dip double-rep %copy ;