From 732d64c8322db3d924e4417d01a91a57d67c268b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 6 Dec 2004 02:17:09 +0000 Subject: [PATCH] linerization of generic, 2generic --- TODO.FACTOR.txt | 2 - library/bootstrap/boot-stage2.factor | 6 +- library/compiler/compiler-macros.factor | 70 -------------- library/compiler/compiler.factor | 8 +- library/compiler/dummy-compiler.factor | 5 - library/compiler/generator-x86.factor | 76 ++++++++++++--- library/compiler/generic.factor | 93 ------------------- library/compiler/ifte.factor | 87 ----------------- library/compiler/linearizer.factor | 31 +++++++ library/namespaces.factor | 2 +- library/primitives.factor | 2 +- .../asm-test.factor | 0 .../bail-out.factor | 0 .../{x86-compiler => compiler}/generic.factor | 0 .../{x86-compiler => compiler}/ifte.factor | 0 library/test/compiler/optimizer.factor | 15 +++ .../{x86-compiler => compiler}/simple.factor | 0 .../{x86-compiler => compiler}/stack.factor | 0 library/test/test.factor | 14 +-- 19 files changed, 125 insertions(+), 286 deletions(-) delete mode 100644 library/compiler/compiler-macros.factor delete mode 100644 library/compiler/dummy-compiler.factor delete mode 100644 library/compiler/generic.factor delete mode 100644 library/compiler/ifte.factor rename library/test/{x86-compiler => compiler}/asm-test.factor (100%) rename library/test/{x86-compiler => compiler}/bail-out.factor (100%) rename library/test/{x86-compiler => compiler}/generic.factor (100%) rename library/test/{x86-compiler => compiler}/ifte.factor (100%) create mode 100644 library/test/compiler/optimizer.factor rename library/test/{x86-compiler => compiler}/simple.factor (100%) rename library/test/{x86-compiler => compiler}/stack.factor (100%) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index d4ccbe9b1f..c652f42b69 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -13,12 +13,10 @@ - optimizer rewrite stack ops - alien-call need special nodes - mutual recursion is borked with certain branch order -- fix inference of + = and others + linearizer/generator: - peephole optimizer -- linearize generic, 2generic - getenv/setenv: if literal arg, compile as a load/store - compiler: drop literal peephole optimization diff --git a/library/bootstrap/boot-stage2.factor b/library/bootstrap/boot-stage2.factor index b4744d2595..f6204ce759 100644 --- a/library/bootstrap/boot-stage2.factor +++ b/library/bootstrap/boot-stage2.factor @@ -139,6 +139,7 @@ USE: stdio "/library/tools/jedit.factor" "/library/cli.factor" + "/library/sdl/hsv.factor" ] [ dup print run-resource @@ -163,13 +164,10 @@ cpu "x86" = [ ! "/library/sdl/sdl-gfx.factor" ! "/library/sdl/sdl-keysym.factor" ! "/library/sdl/sdl-utils.factor" -! "/library/sdl/hsv.factor" ] [ dup print run-resource ] each -] [ - "/library/compiler/dummy-compiler.factor" dup print run-resource -] ifte +] when "/library/bootstrap/init-stage2.factor" dup print run-resource diff --git a/library/compiler/compiler-macros.factor b/library/compiler/compiler-macros.factor deleted file mode 100644 index 5b5f0aff19..0000000000 --- a/library/compiler/compiler-macros.factor +++ /dev/null @@ -1,70 +0,0 @@ -! :folding=indent:collapseFolds=1: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. Redistributions in binary form must reproduce the above copyright notice, -! this list of conditions and the following disclaimer in the documentation -! and/or other materials provided with the distribution. -! -! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -IN: compiler -USE: alien - -: LITERAL ( cell -- ) - #! Push literal on data stack. - 4 ESI R+I - ESI I>[R] ; - -: [LITERAL] ( cell -- ) - #! Push complex literal on data stack by following an - #! indirect pointer. - 4 ESI R+I - EAX [I]>R - EAX ESI R>[R] ; - -: PUSH-DS ( -- ) - #! Push contents of EAX onto datastack. - 4 ESI R+I - EAX ESI R>[R] ; - -: POP-DS ( -- ) - #! Pop datastack, store pointer to datastack top in EAX. - ESI EAX [R]>R - 4 ESI R-I ; - -: SELF-CALL ( name -- ) - #! Call named C function in Factor interpreter executable. - dlsym-self CALL JUMP-FIXUP ; - -: TYPE ( -- ) - #! Peek datastack, store type # in EAX. - ESI PUSH-[R] - "type_of" SELF-CALL - 4 ESP R+I ; - -: ARITHMETIC-TYPE ( -- ) - #! Peek top two on datastack, store arithmetic type # in EAX. - ESI EAX R>R - EAX PUSH-[R] - 4 EAX R-I - EAX PUSH-[R] - "arithmetic_type" SELF-CALL - 8 ESP R+I ; diff --git a/library/compiler/compiler.factor b/library/compiler/compiler.factor index fd384114f3..6191d3c55f 100644 --- a/library/compiler/compiler.factor +++ b/library/compiler/compiler.factor @@ -52,7 +52,13 @@ USE: words : (compile) ( word -- ) #! Should be called inside the with-compiler scope. - begin-compiling dataflow optimize linearize generate ; + begin-compiling dataflow ( optimize ) linearize generate ; + +: precompile ( word -- ) + #! Print linear IR of word. + [ + word-parameter dataflow optimize linearize [.] + ] with-scope ; : compile-postponed ( -- ) compile-words get [ diff --git a/library/compiler/dummy-compiler.factor b/library/compiler/dummy-compiler.factor deleted file mode 100644 index caacd6be7c..0000000000 --- a/library/compiler/dummy-compiler.factor +++ /dev/null @@ -1,5 +0,0 @@ -! Loaded on non-x86 platforms. -IN: compiler - -: init-assembler ; -: compile-all ; diff --git a/library/compiler/generator-x86.factor b/library/compiler/generator-x86.factor index 92d1504739..7d367db548 100644 --- a/library/compiler/generator-x86.factor +++ b/library/compiler/generator-x86.factor @@ -38,6 +38,7 @@ USE: namespaces USE: stack USE: strings USE: words +USE: vectors : LITERAL ( cell -- ) #! Push literal on data stack. @@ -78,21 +79,6 @@ USE: words #! Call named C function in Factor interpreter executable. dlsym-self CALL JUMP-FIXUP ; -: TYPE ( -- ) - #! Peek datastack, store type # in EAX. - ESI PUSH-[R] - "type_of" SELF-CALL - 4 ESP R+I ; - -: ARITHMETIC-TYPE ( -- ) - #! Peek top two on datastack, store arithmetic type # in EAX. - ESI EAX R>R - EAX PUSH-[R] - 4 EAX R-I - EAX PUSH-[R] - "arithmetic_type" SELF-CALL - 8 ESP R+I ; - #push [ compile-literal ] "generator" set-word-property #call [ @@ -126,10 +112,70 @@ USE: words EAX ESI R>[R] ] "generator" set-word-property +! This is crap #swap [ drop \ swap CALL compiled-offset defer-xt ] "generator" set-word-property #over [ drop \ over CALL compiled-offset defer-xt ] "generator" set-word-property +#pick [ drop \ pick CALL compiled-offset defer-xt ] "generator" set-word-property #nip [ drop \ nip CALL compiled-offset defer-xt ] "generator" set-word-property #tuck [ drop \ tuck CALL compiled-offset defer-xt ] "generator" set-word-property #rot [ drop \ rot CALL compiled-offset defer-xt ] "generator" set-word-property #>r [ drop \ >r CALL compiled-offset defer-xt ] "generator" set-word-property #r> [ drop \ r> CALL compiled-offset defer-xt ] "generator" set-word-property + +: begin-jump-table ( -- ) + #! Compile a piece of code that jumps to an offset in a + #! jump table indexed by the type of the Factor object in + #! EAX. + #! The jump table must immediately follow this macro. + 2 EAX R<r dup type r> vector-nth execute ; + TYPE compile-jump-table ; + +#generic [ compile-generic ] "generator" set-word-property + +: ARITHMETIC-TYPE ( -- ) + #! Peek top two on datastack, store arithmetic type # in EAX. + ESI EAX R>R + EAX PUSH-[R] + 4 EAX R-I + EAX PUSH-[R] + "arithmetic_type" SELF-CALL + 8 ESP R+I ; + +: compile-2generic ( vtable -- ) + #! Compile a faster alternative to + #! : 2generic ( obj vtable -- ) + #! >r 2dup arithmetic-type r> vector-nth execute ; + ARITHMETIC-TYPE compile-jump-table ; + +#2generic [ compile-2generic ] "generator" set-word-property diff --git a/library/compiler/generic.factor b/library/compiler/generic.factor deleted file mode 100644 index 2e8a7e6c20..0000000000 --- a/library/compiler/generic.factor +++ /dev/null @@ -1,93 +0,0 @@ -! :folding=indent:collapseFolds=1: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. Redistributions in binary form must reproduce the above copyright notice, -! this list of conditions and the following disclaimer in the documentation -! and/or other materials provided with the distribution. -! -! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -IN: compiler -USE: combinators -USE: words -USE: stack -USE: kernel -USE: math -USE: lists -USE: vectors - -: begin-jump-table ( -- start-fixup end-fixup ) - 2 EAX R<r - compile-table-jump - (compile-jump-table) - r> end-jump-table ; - -: compile-generic ( compile-time: vtable -- ) - #! Compile a faster alternative to - #! : generic ( obj vtable -- ) - #! >r dup type r> vector-nth execute ; - pop-literal commit-literals - TYPE compile-jump-table ; - -: compile-2generic ( compile-time: vtable -- ) - #! Compile a faster alternative to - #! : 2generic ( obj vtable -- ) - #! >r 2dup arithmetic-type r> vector-nth execute ; - pop-literal commit-literals - ARITHMETIC-TYPE compile-jump-table ; - -\ generic [ compile-generic ] "compiling" set-word-property -\ 2generic [ compile-2generic ] "compiling" set-word-property diff --git a/library/compiler/ifte.factor b/library/compiler/ifte.factor deleted file mode 100644 index c66f7e390b..0000000000 --- a/library/compiler/ifte.factor +++ /dev/null @@ -1,87 +0,0 @@ -! :folding=indent:collapseFolds=1: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. Redistributions in binary form must reproduce the above copyright notice, -! this list of conditions and the following disclaimer in the documentation -! and/or other materials provided with the distribution. -! -! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, -! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -IN: compiler -USE: combinators -USE: words -USE: stack -USE: kernel -USE: math -USE: lists - -: compile-test ( -- ) - POP-DS - ! condition is now in EAX - f address EAX CMP-I-R ; - -: compile-f-test ( -- fixup ) - #! Push addr where we write the branch target address. - compile-test - ! jump w/ address added later - JE ; - -: compile-t-test ( -- fixup ) - #! Push addr where we write the branch target address. - compile-test - ! jump w/ address added later - JNE ; - -: branch-target ( fixup -- ) - compiled-offset swap JUMP-FIXUP ; - -: compile-else ( fixup -- fixup ) - #! Push addr where we write the branch target address, - #! and fixup branch target address from compile-f-test. - #! Push f for the fixup if we're tail position. - tail? [ RET f ] [ JUMP ] ifte swap branch-target ; - -: end-if ( fixup -- ) - tail? [ RET ] when [ branch-target ] when* ; - -: compile-ifte ( compile-time: true false -- ) - pop-literal pop-literal commit-literals - compile-f-test >r - ( t -- ) compile-quot - r> compile-else >r - ( f -- ) compile-quot - r> end-if ; - -: compile-when ( compile-time: true -- ) - pop-literal commit-literals - compile-f-test >r - ( t -- ) compile-quot - r> end-if ; - -: compile-unless ( compile-time: false -- ) - pop-literal commit-literals - compile-t-test >r - ( f -- ) compile-quot - r> end-if ; - -\ ifte [ compile-ifte ] "compiling" set-word-property -\ when [ compile-when ] "compiling" set-word-property -\ unless [ compile-unless ] "compiling" set-word-property diff --git a/library/compiler/linearizer.factor b/library/compiler/linearizer.factor index dab206d6ee..b1076c5cae 100644 --- a/library/compiler/linearizer.factor +++ b/library/compiler/linearizer.factor @@ -33,6 +33,10 @@ USE: namespaces USE: inference USE: combinators +! The linear IR is close to assembly language. It also resembles +! Forth code in some sense. It exists so that pattern matching +! optimization can be performed against it. + ! Linear IR nodes. This is in addition to the symbols already ! defined in dataflow vocab. @@ -79,6 +83,25 @@ SYMBOL: #jump ( tail-call ) swap (linearize) ( true branch ) label, ( branch target of false branch end ) ; +: generic-head ( param op -- end label/param ) + #! Output the jump table insn and return a list of + #! label/branch pairs. + >r +