From 8281c2fb55c3a7652051f8f0361a8dc4fa375203 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 12:45:27 +1200 Subject: [PATCH 01/14] alien.inline.compile: write library files to resource:alien-inline-libs --- basis/alien/inline/compiler/compiler.factor | 15 ++++++++++----- basis/alien/inline/inline.factor | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/basis/alien/inline/compiler/compiler.factor b/basis/alien/inline/compiler/compiler.factor index b1ccc2baab..d049668eec 100644 --- a/basis/alien/inline/compiler/compiler.factor +++ b/basis/alien/inline/compiler/compiler.factor @@ -2,12 +2,19 @@ ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays combinators fry generalizations io.encodings.ascii io.files io.files.temp io.launcher kernel -locals make sequences system vocabs.parser words ; +locals make sequences system vocabs.parser words io.directories +io.pathnames ; IN: alien.inline.compiler SYMBOL: C SYMBOL: C++ +: inline-libs-directory ( -- path ) + "alien-inline-libs" resource-path dup make-directories ; + +: inline-library-file ( name -- path ) + inline-libs-directory prepend-path ; + : library-suffix ( -- str ) os { { [ dup macosx? ] [ drop ".dylib" ] } @@ -16,10 +23,8 @@ SYMBOL: C++ } cond ; : library-path ( str -- str' ) - '[ - "lib-" % current-vocab name>> % - "-" % _ % library-suffix % - ] "" make temp-file ; + '[ "lib" % "-" % _ % library-suffix % ] + "" make inline-library-file ; : src-suffix ( lang -- str ) { diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 88cc5e3519..8ec0952c5a 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -58,7 +58,7 @@ SYMBOL: c-strings PRIVATE> : define-c-library ( name -- ) - c-library set + [ current-vocab name>> % "_" % % ] "" make c-library set V{ } clone c-strings set V{ } clone compiler-args set ; From 59f0dbb5167a66aa95bbb164438ef4ed6ff690f6 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 13:14:43 +1200 Subject: [PATCH 02/14] alien.inline: fix library name and us remove-library --- basis/alien/inline/compiler/compiler.factor | 3 +-- basis/alien/inline/inline.factor | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/basis/alien/inline/compiler/compiler.factor b/basis/alien/inline/compiler/compiler.factor index d049668eec..d7d2d6fc43 100644 --- a/basis/alien/inline/compiler/compiler.factor +++ b/basis/alien/inline/compiler/compiler.factor @@ -23,8 +23,7 @@ SYMBOL: C++ } cond ; : library-path ( str -- str' ) - '[ "lib" % "-" % _ % library-suffix % ] - "" make inline-library-file ; + '[ "lib" % _ % library-suffix % ] "" make temp-file ; : src-suffix ( lang -- str ) { diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 8ec0952c5a..20ccd43e5c 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -55,10 +55,13 @@ SYMBOL: c-strings compiler-args get c-strings get "\n" join c-library get compile-to-library ; + +: c-library-name ( name -- name' ) + [ current-vocab name>> % "_" % % ] "" make ; PRIVATE> : define-c-library ( name -- ) - [ current-vocab name>> % "_" % % ] "" make c-library set + c-library-name c-library set V{ } clone c-strings set V{ } clone compiler-args set ; @@ -104,7 +107,8 @@ PRIVATE> ] 3bi ; : delete-inline-library ( str -- ) - library-path dup exists? [ delete-file ] [ drop ] if ; + c-library-name [ remove-library ] + [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ; SYNTAX: C-LIBRARY: scan define-c-library ; From dea872c7e3526b9df0bf9995d6084cdc9461de14 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 00:25:46 -0500 Subject: [PATCH 03/14] compiler.cfg.linear-scan.allocation: fix broken spill slot reuse logic --- .../allocation/coalescing/coalescing.factor | 15 +++++++++++---- .../allocation/spilling/spilling.factor | 4 ++-- .../cfg/linear-scan/allocation/state/state.factor | 14 ++------------ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/basis/compiler/cfg/linear-scan/allocation/coalescing/coalescing.factor b/basis/compiler/cfg/linear-scan/allocation/coalescing/coalescing.factor index e99c2ba710..ef8a9c56f8 100644 --- a/basis/compiler/cfg/linear-scan/allocation/coalescing/coalescing.factor +++ b/basis/compiler/cfg/linear-scan/allocation/coalescing/coalescing.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel sequences +USING: accessors kernel sequences namespaces assocs fry combinators.short-circuit compiler.cfg.linear-scan.live-intervals compiler.cfg.linear-scan.allocation.state ; @@ -20,9 +20,16 @@ IN: compiler.cfg.linear-scan.allocation.coalescing [ avoids-inactive-intervals? ] } 1&& ; +: reuse-spill-slot ( old new -- ) + [ vreg>> spill-slots get at ] dip '[ _ vreg>> spill-slots get set-at ] when* ; + +: reuse-register ( old new -- ) + reg>> >>reg drop ; + +: (coalesce) ( old new -- ) + [ add-active ] [ [ delete-active ] [ add-handled ] bi ] bi* ; + : coalesce ( live-interval -- ) dup copy-from>> active-interval - [ [ add-active ] [ [ delete-active ] [ add-handled ] bi ] bi* ] - [ reg>> >>reg drop ] - 2bi ; + [ reuse-spill-slot ] [ reuse-register ] [ (coalesce) ] 2tri ; \ No newline at end of file diff --git a/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor b/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor index 8c91ca7f60..b89c1f4de2 100644 --- a/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor +++ b/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor @@ -38,10 +38,10 @@ ERROR: bad-live-ranges interval ; } 2cleave ; : assign-spill ( live-interval -- ) - dup assign-spill-slot >>spill-to f >>split-next drop ; + dup vreg>> assign-spill-slot >>spill-to f >>split-next drop ; : assign-reload ( live-interval -- ) - dup assign-spill-slot >>reload-from drop ; + dup vreg>> assign-spill-slot >>reload-from drop ; : split-and-spill ( live-interval n -- before after ) split-for-spill 2dup [ assign-spill ] [ assign-reload ] bi* ; diff --git a/basis/compiler/cfg/linear-scan/allocation/state/state.factor b/basis/compiler/cfg/linear-scan/allocation/state/state.factor index 1e670ad6a6..3e646b40f0 100644 --- a/basis/compiler/cfg/linear-scan/allocation/state/state.factor +++ b/basis/compiler/cfg/linear-scan/allocation/state/state.factor @@ -126,18 +126,8 @@ SYMBOL: spill-counts ! Mapping from vregs to spill slots SYMBOL: spill-slots -DEFER: assign-spill-slot - -: compute-spill-slot ( live-interval -- n ) - dup copy-from>> - [ assign-spill-slot ] - [ vreg>> reg-class>> next-spill-slot ] ?if ; - -: assign-spill-slot ( live-interval -- n ) - dup vreg>> spill-slots get at [ ] [ - [ compute-spill-slot dup ] keep - vreg>> spill-slots get set-at - ] ?if ; +: assign-spill-slot ( vreg -- n ) + spill-slots get [ reg-class>> next-spill-slot ] cache ; : init-allocator ( registers -- ) registers set From 11347e784c1d2dc4c6c4a34de593ef3520683bad Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 03:05:45 -0500 Subject: [PATCH 04/14] insn. doesn't print numbers --- basis/compiler/cfg/debugger/debugger.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/compiler/cfg/debugger/debugger.factor b/basis/compiler/cfg/debugger/debugger.factor index 60805124cd..e355ee2ac1 100644 --- a/basis/compiler/cfg/debugger/debugger.factor +++ b/basis/compiler/cfg/debugger/debugger.factor @@ -26,7 +26,7 @@ M: word test-cfg ] map ; : insn. ( insn -- ) - tuple>array [ pprint bl ] each nl ; + tuple>array but-last [ pprint bl ] each nl ; : mr. ( mrs -- ) [ From ae67de6f905ab393af3542b9821047683fe12063 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 03:58:51 -0500 Subject: [PATCH 05/14] compiler.cfg.linear-scan: fix fencepost error in spill insertion --- .../allocation/spilling/spilling.factor | 2 +- .../linear-scan/assignment/assignment.factor | 4 +- .../cfg/linear-scan/linear-scan-tests.factor | 203 ++++++++++++++---- 3 files changed, 167 insertions(+), 42 deletions(-) diff --git a/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor b/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor index b89c1f4de2..14046a91f1 100644 --- a/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor +++ b/basis/compiler/cfg/linear-scan/allocation/spilling/spilling.factor @@ -17,7 +17,7 @@ ERROR: bad-live-ranges interval ; ] [ drop ] if ; : trim-before-ranges ( live-interval -- ) - [ ranges>> ] [ uses>> last ] bi + [ ranges>> ] [ uses>> last 1 + ] bi [ '[ from>> _ <= ] filter-here ] [ swap last (>>to) ] 2bi ; diff --git a/basis/compiler/cfg/linear-scan/assignment/assignment.factor b/basis/compiler/cfg/linear-scan/assignment/assignment.factor index 9275c6d687..c0f90e5932 100644 --- a/basis/compiler/cfg/linear-scan/assignment/assignment.factor +++ b/basis/compiler/cfg/linear-scan/assignment/assignment.factor @@ -107,7 +107,7 @@ SYMBOL: check-assignment? ERROR: overlapping-registers intervals ; : check-assignment ( intervals -- ) - dup [ copy-from>> ] map sift [ vreg>> ] map '[ vreg>> _ member? not ] filter + dup [ copy-from>> ] map sift '[ vreg>> _ member? not ] filter dup [ reg>> ] map all-unique? [ drop ] [ overlapping-registers ] if ; : active-intervals ( n -- intervals ) @@ -150,7 +150,7 @@ ERROR: bad-live-values live-values ; : begin-block ( bb -- ) dup basic-block set - dup block-from prepare-insn + dup block-from activate-new-intervals [ [ live-in ] [ block-from ] bi compute-live-values ] keep register-live-ins get set-at ; diff --git a/basis/compiler/cfg/linear-scan/linear-scan-tests.factor b/basis/compiler/cfg/linear-scan/linear-scan-tests.factor index 06817071d4..bc3061128c 100644 --- a/basis/compiler/cfg/linear-scan/linear-scan-tests.factor +++ b/basis/compiler/cfg/linear-scan/linear-scan-tests.factor @@ -82,9 +82,9 @@ check-numbering? on T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } { start 0 } - { end 1 } + { end 2 } { uses V{ 0 1 } } - { ranges V{ T{ live-range f 0 1 } } } + { ranges V{ T{ live-range f 0 2 } } } } T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } @@ -107,9 +107,9 @@ check-numbering? on T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } { start 0 } - { end 0 } + { end 1 } { uses V{ 0 } } - { ranges V{ T{ live-range f 0 0 } } } + { ranges V{ T{ live-range f 0 1 } } } } T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } @@ -132,9 +132,9 @@ check-numbering? on T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } { start 0 } - { end 0 } + { end 1 } { uses V{ 0 } } - { ranges V{ T{ live-range f 0 0 } } } + { ranges V{ T{ live-range f 0 1 } } } } T{ live-interval { vreg T{ vreg { reg-class int-regs } { n 1 } } } @@ -1317,38 +1317,6 @@ USING: math.private ; allocate-registers drop ] unit-test -! Spill slot liveness was computed incorrectly, leading to a FEP -! early in bootstrap on x86-32 -[ t ] [ - [ - H{ } clone live-ins set - H{ } clone live-outs set - H{ } clone phi-live-ins set - T{ basic-block - { id 12345 } - { instructions - V{ - T{ ##gc f V int-regs 6 V int-regs 7 } - T{ ##peek f V int-regs 0 D 0 } - T{ ##peek f V int-regs 1 D 1 } - T{ ##peek f V int-regs 2 D 2 } - T{ ##peek f V int-regs 3 D 3 } - T{ ##peek f V int-regs 4 D 4 } - T{ ##peek f V int-regs 5 D 5 } - T{ ##replace f V int-regs 0 D 1 } - T{ ##replace f V int-regs 1 D 2 } - T{ ##replace f V int-regs 2 D 3 } - T{ ##replace f V int-regs 3 D 4 } - T{ ##replace f V int-regs 4 D 5 } - T{ ##replace f V int-regs 5 D 0 } - } - } - } dup 1array { { int-regs V{ 0 1 2 3 } } } (linear-scan) - instructions>> first - live-values>> assoc-empty? - ] with-scope -] unit-test - [ f ] [ T{ live-range f 0 10 } T{ live-range f 20 30 } @@ -2482,4 +2450,161 @@ V{ 7 get 9 get 1vector >>successors drop 8 get 9 get 1vector >>successors drop -[ ] [ { 1 2 3 4 5 } test-linear-scan-on-cfg ] unit-test \ No newline at end of file +[ ] [ { 1 2 3 4 5 } test-linear-scan-on-cfg ] unit-test + +! Fencepost error in assignment pass +V{ T{ ##branch } } 0 test-bb + +V{ + T{ ##peek f V int-regs 0 D 0 } + T{ ##compare-imm-branch f V int-regs 0 5 cc= } +} 1 test-bb + +V{ T{ ##branch } } 2 test-bb + +V{ + T{ ##peek f V int-regs 1 D 0 } + T{ ##peek f V int-regs 2 D 0 } + T{ ##replace f V int-regs 1 D 0 } + T{ ##replace f V int-regs 2 D 0 } + T{ ##branch } +} 3 test-bb + +V{ + T{ ##replace f V int-regs 0 D 0 } + T{ ##return } +} 4 test-bb + +test-diamond + +[ ] [ { 1 2 } test-linear-scan-on-cfg ] unit-test + +[ 0 ] [ 1 get instructions>> [ _spill? ] count ] unit-test + +[ 1 ] [ 2 get instructions>> [ _spill? ] count ] unit-test + +[ 1 ] [ 3 get instructions>> [ _spill? ] count ] unit-test + +[ 1 ] [ 4 get instructions>> [ _reload? ] count ] unit-test + +! Another test case for fencepost error in assignment pass +V{ T{ ##branch } } 0 test-bb + +V{ + T{ ##peek f V int-regs 0 D 0 } + T{ ##compare-imm-branch f V int-regs 0 5 cc= } +} 1 test-bb + +V{ + T{ ##peek f V int-regs 1 D 0 } + T{ ##peek f V int-regs 2 D 0 } + T{ ##replace f V int-regs 1 D 0 } + T{ ##replace f V int-regs 2 D 0 } + T{ ##replace f V int-regs 0 D 0 } + T{ ##branch } +} 2 test-bb + +V{ + T{ ##branch } +} 3 test-bb + +V{ + T{ ##replace f V int-regs 0 D 0 } + T{ ##return } +} 4 test-bb + +test-diamond + +[ ] [ { 1 2 } test-linear-scan-on-cfg ] unit-test + +[ 0 ] [ 1 get instructions>> [ _spill? ] count ] unit-test + +[ 1 ] [ 2 get instructions>> [ _spill? ] count ] unit-test + +[ 1 ] [ 2 get instructions>> [ _reload? ] count ] unit-test + +[ 0 ] [ 3 get instructions>> [ _spill? ] count ] unit-test + +[ 0 ] [ 4 get instructions>> [ _reload? ] count ] unit-test + +! GC check tests + +! Spill slot liveness was computed incorrectly, leading to a FEP +! early in bootstrap on x86-32 +[ t ] [ + [ + H{ } clone live-ins set + H{ } clone live-outs set + H{ } clone phi-live-ins set + T{ basic-block + { id 12345 } + { instructions + V{ + T{ ##gc f V int-regs 6 V int-regs 7 } + T{ ##peek f V int-regs 0 D 0 } + T{ ##peek f V int-regs 1 D 1 } + T{ ##peek f V int-regs 2 D 2 } + T{ ##peek f V int-regs 3 D 3 } + T{ ##peek f V int-regs 4 D 4 } + T{ ##peek f V int-regs 5 D 5 } + T{ ##replace f V int-regs 0 D 1 } + T{ ##replace f V int-regs 1 D 2 } + T{ ##replace f V int-regs 2 D 3 } + T{ ##replace f V int-regs 3 D 4 } + T{ ##replace f V int-regs 4 D 5 } + T{ ##replace f V int-regs 5 D 0 } + } + } + } dup 1array { { int-regs V{ 0 1 2 3 } } } (linear-scan) + instructions>> first + live-values>> assoc-empty? + ] with-scope +] unit-test + +V{ + T{ ##peek f V int-regs 0 D 0 } + T{ ##peek f V int-regs 1 D 1 } + T{ ##replace f V int-regs 1 D 1 } + T{ ##branch } +} 0 test-bb + +V{ + T{ ##gc f V int-regs 2 V int-regs 3 } + T{ ##branch } +} 1 test-bb + +V{ + T{ ##replace f V int-regs 0 D 0 } + T{ ##return } +} 2 test-bb + +0 get 1 get 1vector >>successors drop +1 get 2 get 1vector >>successors drop + +[ ] [ { 1 2 3 } test-linear-scan-on-cfg ] unit-test + +[ H{ { V int-regs 0 3 } } ] [ 1 get instructions>> first live-values>> ] unit-test + + + +V{ + T{ ##peek f V int-regs 0 D 0 } + T{ ##peek f V int-regs 1 D 1 } + T{ ##compare-imm-branch f V int-regs 1 5 cc= } +} 0 test-bb + +V{ + T{ ##gc f V int-regs 2 V int-regs 3 } + T{ ##replace f V int-regs 0 D 0 } + T{ ##return } +} 1 test-bb + +V{ + T{ ##return } +} 2 test-bb + +0 get 1 get 2 get V{ } 2sequence >>successors drop + +[ ] [ { 1 2 3 } test-linear-scan-on-cfg ] unit-test + +[ H{ { V int-regs 0 3 } } ] [ 1 get instructions>> first live-values>> ] unit-test From 2e7f337b3dd6d750f139b40d1dec871aa0220031 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 23:53:50 +1200 Subject: [PATCH 06/14] alien.inline: made define-c-function and define-c-function' standalone --- basis/alien/inline/inline.factor | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 20ccd43e5c..2c0825f8b4 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -39,8 +39,8 @@ SYMBOL: c-strings : prototype-string' ( function types return -- str ) [ dup arg-list ] prototype-string ; -: append-function-body ( prototype-str -- str ) - " {\n" append parse-here append "\n}\n" append ; +: append-function-body ( prototype-str body -- str ) + [ swap % " {\n" % % "\n}\n" % ] "" make ; : compile-library? ( -- ? ) c-library get library-path dup exists? [ @@ -69,14 +69,18 @@ PRIVATE> compile-library? [ compile-library ] when c-library get dup library-path "cdecl" add-library ; -: define-c-function ( function types effect -- ) - [ factor-function define-declared ] 3keep prototype-string - append-function-body c-strings get push ; +: define-c-function ( function types effect body -- ) + [ + [ factor-function define-declared ] + [ prototype-string ] 3bi + ] dip append-function-body c-strings get push ; -: define-c-function' ( function effect -- ) - [ in>> ] keep [ factor-function define-declared ] 3keep - out>> prototype-string' - append-function-body c-strings get push ; +: define-c-function' ( function effect body -- ) + [ + [ in>> ] keep + [ factor-function define-declared ] + [ out>> prototype-string' ] 3bi + ] dip append-function-body c-strings get push ; : define-c-link ( str -- ) "-l" prepend compiler-args get push ; @@ -123,7 +127,7 @@ SYNTAX: C-LINK/FRAMEWORK: scan define-c-link/framework ; SYNTAX: C-INCLUDE: scan define-c-include ; SYNTAX: C-FUNCTION: - function-types-effect define-c-function ; + function-types-effect parse-here define-c-function ; SYNTAX: C-TYPEDEF: scan scan define-c-typedef ; From 0851823ba92428e7ac57c8ecc6c12ba20d87aac1 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 23:54:40 +1200 Subject: [PATCH 07/14] alien.inline: remove vocab argument from define-c-struct --- basis/alien/inline/inline.factor | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 2c0825f8b4..7f530bc64b 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -100,15 +100,15 @@ PRIVATE> "" make c-strings get push ] 2bi ; -: define-c-struct ( name vocab fields -- ) - [ define-struct ] [ - nip over +: define-c-struct ( name fields -- ) + [ current-vocab swap define-struct ] [ + over [ "typedef struct " % "_" % % " {\n" % [ first2 swap % " " % % ";\n" % ] each "} " % % ";\n" % ] "" make c-strings get push - ] 3bi ; + ] 2bi ; : delete-inline-library ( str -- ) c-library-name [ remove-library ] @@ -132,7 +132,7 @@ SYNTAX: C-FUNCTION: SYNTAX: C-TYPEDEF: scan scan define-c-typedef ; SYNTAX: C-STRUCTURE: - scan current-vocab parse-definition define-c-struct ; + scan parse-definition define-c-struct ; SYNTAX: ;C-LIBRARY compile-c-library ; From 864a6e75080785aca83be7bd0fb93e72d6795a67 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 23:55:05 +1200 Subject: [PATCH 08/14] alien.inline: better names --- basis/alien/inline/inline.factor | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 7f530bc64b..37e01b5209 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -82,16 +82,16 @@ PRIVATE> [ out>> prototype-string' ] 3bi ] dip append-function-body c-strings get push ; -: define-c-link ( str -- ) +: c-link-to ( str -- ) "-l" prepend compiler-args get push ; -: define-c-framework ( str -- ) +: c-use-framework ( str -- ) "-framework" swap compiler-args get '[ _ push ] bi@ ; -: define-c-link/framework ( str -- ) - os macosx? [ define-c-framework ] [ define-c-link ] if ; +: c-link-to/use-framework ( str -- ) + os macosx? [ c-use-framework ] [ c-link-to ] if ; -: define-c-include ( str -- ) +: c-include ( str -- ) "#include " prepend c-strings get push ; : define-c-typedef ( old new -- ) @@ -110,7 +110,7 @@ PRIVATE> ] "" make c-strings get push ] 2bi ; -: delete-inline-library ( str -- ) +: delete-inline-library ( name -- ) c-library-name [ remove-library ] [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ; @@ -118,13 +118,13 @@ SYNTAX: C-LIBRARY: scan define-c-library ; SYNTAX: COMPILE-AS-C++ t library-is-c++ set ; -SYNTAX: C-LINK: scan define-c-link ; +SYNTAX: C-LINK: scan c-link-to ; -SYNTAX: C-FRAMEWORK: scan define-c-framework ; +SYNTAX: C-FRAMEWORK: scan c-use-framework ; -SYNTAX: C-LINK/FRAMEWORK: scan define-c-link/framework ; +SYNTAX: C-LINK/FRAMEWORK: scan c-link-to/use-framework ; -SYNTAX: C-INCLUDE: scan define-c-include ; +SYNTAX: C-INCLUDE: scan c-include ; SYNTAX: C-FUNCTION: function-types-effect parse-here define-c-function ; From dc80d8575f0f3ac124876c870afd59e31d5d4a42 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Fri, 10 Jul 2009 23:55:32 +1200 Subject: [PATCH 09/14] alien.inline: added documentation --- basis/alien/inline/inline-docs.factor | 205 ++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 basis/alien/inline/inline-docs.factor diff --git a/basis/alien/inline/inline-docs.factor b/basis/alien/inline/inline-docs.factor new file mode 100644 index 0000000000..bce3f2530c --- /dev/null +++ b/basis/alien/inline/inline-docs.factor @@ -0,0 +1,205 @@ +! Copyright (C) 2009 Jeremy Hughes. +! See http://factorcode.org/license.txt for BSD license. +USING: help.markup help.syntax kernel strings effects ; +IN: alien.inline + +: $binding-note ( x -- ) + drop + { "This word requires that certain variables are correctly bound. " + "Call " { $link POSTPONE: define-c-library } " to set them up." } print-element ; + +HELP: ;C-LIBRARY +{ $syntax ";C-LIBRARY" } +{ $description "Writes, compiles, and links code generated since previous invocation of " { $link POSTPONE: C-LIBRARY: } "." } +{ $see-also POSTPONE: compile-c-library } ; + +HELP: C-FRAMEWORK: +{ $syntax "C-FRAMEWORK: name" } +{ $description "OS X only. Link to named framework. Takes effect when " { $link POSTPONE: ;C-LIBRARY } " is called." } +{ $see-also POSTPONE: c-use-framework } ; + +HELP: C-FUNCTION: +{ $syntax "C-FUNCTION: return name ( args ... )\nbody\n;" } +{ $description "Appends a function to the C library in scope and defines an FFI word that calls it." } +{ $examples + { $example + "USING: alien.inline prettyprint ;" + "IN: cmath.ffi" + "" + "C-LIBRARY: cmathlib" + "" + "C-FUNCTION: int add ( int a, int b )" + " return a + b;" + ";" + "" + ";C-LIBRARY" + "" + "1 2 add ." + "3" } +} +{ $see-also POSTPONE: define-c-function } ; + +HELP: C-INCLUDE: +{ $syntax "C-INCLUDE: name" } +{ $description "Appends an include line to the C library in scope." } +{ $see-also POSTPONE: c-include } ; + +HELP: C-LIBRARY: +{ $syntax "C-LIBRARY: name" } +{ $description "Starts a new C library scope. Other " { $snippet "alien.inline" } " syntax can be used after this word." } +{ $examples + { $example + "USING: alien.inline ;" + "IN: rectangle.ffi" + "" + "C-LIBRARY: rectlib" + "" + "C-STRUCTURE: rectangle { \"int\" \"width\" } { \"int\" \"height\" } ;" + "" + "C-FUNCTION: int area ( rectangle c )" + " return c.width * c.height;" + ";" + "" + ";C-LIBRARY" + "" } +} +{ $see-also POSTPONE: define-c-library } ; + +HELP: C-LINK/FRAMEWORK: +{ $syntax "C-LINK/FRAMEWORK: name" } +{ $description "Equivalent to " { $link POSTPONE: C-FRAMEWORK: } " on OS X and " { $link POSTPONE: C-LINK: } " everywhere else." } +{ $see-also POSTPONE: c-link-to/use-framework } ; + +HELP: C-LINK: +{ $syntax "C-LINK: name" } +{ $description "Link to named library. Takes effect when " { $link POSTPONE: ;C-LIBRARY } " is called." } +{ $see-also POSTPONE: c-link-to } ; + +HELP: C-STRUCTURE: +{ $syntax "C-STRUCTURE: name pairs ... ;" } +{ $description "Like " { $snippet "C-STRUCT:" } " but also generates equivalent C code."} +{ $see-also POSTPONE: define-c-struct } ; + +HELP: C-TYPEDEF: +{ $syntax "C-TYPEDEF: old new" } +{ $description "Like " { $snippet "TYPEDEF:" } " but generates a C typedef statement too." } +{ $see-also POSTPONE: define-c-typedef } ; + +HELP: COMPILE-AS-C++ +{ $syntax "COMPILE-AS-C++" } +{ $description "Insert this word anywhere between " { $link POSTPONE: C-LIBRARY: } " and " { $link POSTPONE: ;C-LIBRARY } " and the generated code will be treated as C++ with " { $snippet "extern \"C\"" } " prepended to each function prototype." } ; + +HELP: DELETE-C-LIBRARY: +{ $syntax "DELETE-C-LIBRARY: name" } +{ $description "Deletes the shared library file corresponding to " { $snippet "name" } " . " } +{ $notes + { $list + { "Must be executed in the vocabulary where " { $snippet "name" } " is defined. " } + "This word is mainly useful for unit tests." + } +} +{ $see-also POSTPONE: delete-inline-library } ; + +HELP: RAW-C: +{ $syntax "RAW-C:" "body" ";" } +{ $description "Insert a string into the generated source file. Useful for macros and other details not implemented in " { $snippet "alien.inline" } "." } ; + +CONSTANT: foo "abc" + +HELP: compile-c-library +{ $description "Writes, compiles, and links code generated since last invocation of " { $link POSTPONE: define-c-library } ". " + "Also calls " { $snippet "add-library" } ". " + "This word does nothing if the shared library is younger than the factor source file." } +{ $notes $binding-note } ; + +HELP: c-use-framework +{ $values + { "str" string } +} +{ $description "OS X only. Adds " { $snippet "-framework name" } " to linker command." } +{ $notes $binding-note } +{ $see-also c-link-to c-link-to/use-framework } ; + +HELP: define-c-function +{ $values + { "function" "function name" } { "types" "a sequence of C types" } { "effect" effect } { "body" string } +} +{ $description "Defines a C function and a factor word which calls it." } +{ $notes + { $list + { "The number of " { $snippet "types" } " must match the " { $snippet "in" } " count of the " { $snippet "effect" } "." } + { "There must be only one " { $snippet "out" } " element. It must be a legal C return type with dashes (-) instead of spaces." } + $binding-note + } +} +{ $see-also POSTPONE: define-c-function' } ; + +HELP: define-c-function' +{ $values + { "function" "function name" } { "effect" effect } { "body" string } +} +{ $description "Defines a C function and a factor word which calls it. See " { $link define-c-function } " for more information." } +{ $notes + { $list + { "Each effect element must be a legal C type with dashes (-) instead of spaces. " + "C argument names will be generated alphabetically, starting with " { $snippet "a" } "." } + $binding-note + } +} +{ $see-also define-c-function } ; + +HELP: c-include +{ $values + { "str" string } +} +{ $description "Appends an include line to the C library in scope." } +{ $notes $binding-note } ; + +HELP: define-c-library +{ $values + { "name" string } +} +{ $description "Starts a new C library scope. Other " { $snippet "alien.inline" } " words can be used after this one." } ; + +HELP: c-link-to +{ $values + { "str" string } +} +{ $description "Adds " { $snippet "-lname" } " to linker command." } +{ $notes $binding-note } +{ $see-also c-use-framework c-link-to/use-framework } ; + +HELP: c-link-to/use-framework +{ $values + { "str" string } +} +{ $description "Equivalent to " { $link c-use-framework } " on OS X and " { $link c-link-to } " everywhere else." } +{ $notes $binding-note } +{ $see-also c-link-to c-use-framework } ; + +HELP: define-c-struct +{ $values + { "name" string } { "fields" "type/name pairs" } +} +{ $description "Defines a C struct and factor words which operate on it." } +{ $notes $binding-note } ; + +HELP: define-c-typedef +{ $values + { "old" "C type" } { "new" "C type" } +} +{ $description "Define C and factor typedefs." } +{ $notes $binding-note } ; + +HELP: delete-inline-library +{ $values + { "name" string } +} +{ $description "Delete the shared library file corresponding to " { $snippet "name" } "." } +{ $notes "Must be executed in the vocabulary where " { $snippet "name" } " is defined. " } ; + +ARTICLE: "alien.inline" "Inline C" +{ $vocab-link "alien.inline" } +; + +ABOUT: "alien.inline" From e0fa51512f6e1ac8342155f788a02a9ee4947a6c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 07:07:54 -0500 Subject: [PATCH 10/14] llvm: new add-llvm-library word to make things a bit more portable --- extra/llvm/core/core.factor | 19 +++++++++++------- extra/llvm/engine/engine.factor | 35 +++++++++++---------------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/extra/llvm/core/core.factor b/extra/llvm/core/core.factor index 00a395d3b2..cb62821390 100644 --- a/extra/llvm/core/core.factor +++ b/extra/llvm/core/core.factor @@ -1,18 +1,23 @@ ! Copyright (C) 2009 Matthew Willis. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.libraries alien.syntax ; +USING: alien.libraries alien.syntax system sequences combinators ; IN: llvm.core << -"LLVMSystem" "/usr/local/lib/libLLVMSystem.dylib" "cdecl" add-library +: add-llvm-library ( name -- ) + dup + { + { [ os macosx? ] [ "/usr/local/lib/lib" ".dylib" surround ] } + { [ os windows? ] [ ".dll" append ] } + { [ os unix? ] [ ".so" append ] } + } cond add-library ; -"LLVMSupport" "/usr/local/lib/libLLVMSupport.dylib" "cdecl" add-library - -"LLVMCore" "/usr/local/lib/libLLVMCore.dylib" "cdecl" add-library - -"LLVMBitReader" "/usr/local/lib/libLLVMBitReader.dylib" "cdecl" add-library +"LLVMSystem" add-llvm-library +"LLVMSupport" add-llvm-library +"LLVMCore" add-llvm-library +"LLVMBitReader" add-llvm-library >> diff --git a/extra/llvm/engine/engine.factor b/extra/llvm/engine/engine.factor index 1fa7ef01d6..d259c740e6 100644 --- a/extra/llvm/engine/engine.factor +++ b/extra/llvm/engine/engine.factor @@ -5,29 +5,18 @@ IN: llvm.engine << -"LLVMExecutionEngine" "/usr/local/lib/libLLVMExecutionEngine.dylib" "cdecl" add-library - -"LLVMTarget" "/usr/local/lib/libLLVMTarget.dylib" "cdecl" add-library - -"LLVMAnalysis" "/usr/local/lib/libLLVMAnalysis.dylib" "cdecl" add-library - -"LLVMipa" "/usr/local/lib/libLLVMipa.dylib" "cdecl" add-library - -"LLVMTransformUtils" "/usr/local/lib/libLLVMTransformUtils.dylib" "cdecl" add-library - -"LLVMScalarOpts" "/usr/local/lib/libLLVMScalarOpts.dylib" "cdecl" add-library - -"LLVMCodeGen" "/usr/local/lib/libLLVMCodeGen.dylib" "cdecl" add-library - -"LLVMAsmPrinter" "/usr/local/lib/libLLVMAsmPrinter.dylib" "cdecl" add-library - -"LLVMSelectionDAG" "/usr/local/lib/libLLVMSelectionDAG.dylib" "cdecl" add-library - -"LLVMX86CodeGen" "/usr/local/lib/libLLVMX86CodeGen.dylib" "cdecl" add-library - -"LLVMJIT" "/usr/local/lib/libLLVMJIT.dylib" "cdecl" add-library - -"LLVMInterpreter.dylib" "/usr/local/lib/libLLVMInterpreter.dylib" "cdecl" add-library +"LLVMExecutionEngine" add-llvm-library +"LLVMTarget" add-llvm-library +"LLVMAnalysis" add-llvm-library +"LLVMipa" add-llvm-library +"LLVMTransformUtils" add-llvm-library +"LLVMScalarOpts" add-llvm-library +"LLVMCodeGen" add-llvm-library +"LLVMAsmPrinter" add-llvm-library +"LLVMSelectionDAG" add-llvm-library +"LLVMX86CodeGen" add-llvm-library +"LLVMJIT" add-llvm-library +"LLVMInterpreter" add-llvm-library >> From 4a5cb3aac3f2b85818a74fbc3bf18acd6f2ba4a4 Mon Sep 17 00:00:00 2001 From: Jeremy Hughes Date: Sat, 11 Jul 2009 00:08:40 +1200 Subject: [PATCH 11/14] alien.inline: added with-c-library word --- basis/alien/inline/inline-docs.factor | 8 +++++++- basis/alien/inline/inline.factor | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/basis/alien/inline/inline-docs.factor b/basis/alien/inline/inline-docs.factor index bce3f2530c..58eca558ea 100644 --- a/basis/alien/inline/inline-docs.factor +++ b/basis/alien/inline/inline-docs.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Jeremy Hughes. ! See http://factorcode.org/license.txt for BSD license. -USING: help.markup help.syntax kernel strings effects ; +USING: help.markup help.syntax kernel strings effects quotations ; IN: alien.inline : $binding-note ( x -- ) @@ -198,6 +198,12 @@ HELP: delete-inline-library { $description "Delete the shared library file corresponding to " { $snippet "name" } "." } { $notes "Must be executed in the vocabulary where " { $snippet "name" } " is defined. " } ; +HELP: with-c-library +{ $values + { "name" string } { "quot" quotation } +} +{ $description "Calls " { $link define-c-library } ", then the quotation, then " { $link compile-c-library } ", then sets all variables bound by " { $snippet "define-c-library" } " to " { $snippet "f" } "." } ; + ARTICLE: "alien.inline" "Inline C" { $vocab-link "alien.inline" } ; diff --git a/basis/alien/inline/inline.factor b/basis/alien/inline/inline.factor index 37e01b5209..1df77d6600 100644 --- a/basis/alien/inline/inline.factor +++ b/basis/alien/inline/inline.factor @@ -6,7 +6,7 @@ generalizations grouping io.directories io.files io.files.info io.files.temp kernel lexer math math.order math.ranges multiline namespaces sequences source-files splitting strings system vocabs.loader vocabs.parser words -alien.c-types alien.structs make parser ; +alien.c-types alien.structs make parser continuations ; IN: alien.inline c-library-name [ remove-library ] [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ; +: with-c-library ( name quot -- ) + [ [ define-c-library ] dip call compile-c-library ] + [ cleanup-variables ] [ ] cleanup ; inline + SYNTAX: C-LIBRARY: scan define-c-library ; SYNTAX: COMPILE-AS-C++ t library-is-c++ set ; From b7aac8c13a68418e7ef2d544126ddfb28f4a0150 Mon Sep 17 00:00:00 2001 From: sheeple Date: Fri, 10 Jul 2009 07:38:19 -0500 Subject: [PATCH 12/14] llvm.core: fix add-llvm-library --- extra/llvm/core/core.factor | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extra/llvm/core/core.factor b/extra/llvm/core/core.factor index cb62821390..0d1b22e288 100644 --- a/extra/llvm/core/core.factor +++ b/extra/llvm/core/core.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2009 Matthew Willis. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.libraries alien.syntax system sequences combinators ; +USING: alien.libraries alien.syntax system sequences combinators kernel ; IN: llvm.core @@ -11,8 +11,8 @@ IN: llvm.core { { [ os macosx? ] [ "/usr/local/lib/lib" ".dylib" surround ] } { [ os windows? ] [ ".dll" append ] } - { [ os unix? ] [ ".so" append ] } - } cond add-library ; + { [ os unix? ] [ "lib" ".so" surround ] } + } cond "cdecl" add-library ; "LLVMSystem" add-llvm-library "LLVMSupport" add-llvm-library From c25ac2a066fa682a63c63378e6bcc5781610202a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 07:46:47 -0500 Subject: [PATCH 13/14] llvm: add unportable tag --- extra/llvm/core/tags.txt | 1 + extra/llvm/engine/tags.txt | 1 + extra/llvm/invoker/tags.txt | 1 + extra/llvm/jit/tags.txt | 1 + extra/llvm/reader/tags.txt | 1 + extra/llvm/tags.txt | 1 + extra/llvm/types/tags.txt | 1 + extra/llvm/wrappers/tags.txt | 1 + 8 files changed, 8 insertions(+) create mode 100644 extra/llvm/core/tags.txt create mode 100644 extra/llvm/engine/tags.txt create mode 100644 extra/llvm/invoker/tags.txt create mode 100644 extra/llvm/jit/tags.txt create mode 100644 extra/llvm/reader/tags.txt create mode 100644 extra/llvm/types/tags.txt create mode 100644 extra/llvm/wrappers/tags.txt diff --git a/extra/llvm/core/tags.txt b/extra/llvm/core/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/core/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/engine/tags.txt b/extra/llvm/engine/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/engine/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/invoker/tags.txt b/extra/llvm/invoker/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/invoker/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/jit/tags.txt b/extra/llvm/jit/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/jit/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/reader/tags.txt b/extra/llvm/reader/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/reader/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/tags.txt b/extra/llvm/tags.txt index bb863cf9a0..bf2a35f15b 100644 --- a/extra/llvm/tags.txt +++ b/extra/llvm/tags.txt @@ -1 +1,2 @@ bindings +unportable diff --git a/extra/llvm/types/tags.txt b/extra/llvm/types/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/types/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/llvm/wrappers/tags.txt b/extra/llvm/wrappers/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/llvm/wrappers/tags.txt @@ -0,0 +1 @@ +unportable From 1cf6bb7f99b0369a413670d8306eea0b2d4935ff Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jul 2009 07:48:49 -0500 Subject: [PATCH 14/14] compiler.cfg.linear-scan: disable unit test for unimplemented feature --- basis/compiler/cfg/linear-scan/linear-scan-tests.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/basis/compiler/cfg/linear-scan/linear-scan-tests.factor b/basis/compiler/cfg/linear-scan/linear-scan-tests.factor index bc3061128c..e8b4b67cf0 100644 --- a/basis/compiler/cfg/linear-scan/linear-scan-tests.factor +++ b/basis/compiler/cfg/linear-scan/linear-scan-tests.factor @@ -1770,7 +1770,8 @@ test-diamond 2 get instructions>> first regs>> V int-regs 1 swap at assert= ] unit-test -[ _copy ] [ 3 get instructions>> second class ] unit-test +! Not until splitting is finished +! [ _copy ] [ 3 get instructions>> second class ] unit-test ! Resolve pass; make sure the spilling is done correctly V{ T{ ##peek f V int-regs 3 R 1 } T{ ##branch } } 0 test-bb