From c2fe23829c58cc5caa1d7981c5a58e5e77ec444b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Dec 2004 22:27:07 +0000 Subject: [PATCH] stack op rewriting cleaned up, #push-immediate/#push-indirect distinction simplifies generator, optimizer fixes --- TODO.FACTOR.txt | 1 - library/compiler/generator-x86.factor | 33 +++++----------------- library/compiler/linearizer.factor | 17 ++++++++++++ library/compiler/optimizer.factor | 40 +++++++++++++++------------ library/lists.factor | 4 +-- library/test/compiler/stack.factor | 15 ++++++++++ 6 files changed, 63 insertions(+), 47 deletions(-) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 77d448571e..d15bbf0162 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -10,7 +10,6 @@ - type inference - handle odd base cases, with code after ifte - handle recursion with when, when* etc -- optimizer rewrite stack ops - alien-call need special nodes + linearizer/generator: diff --git a/library/compiler/generator-x86.factor b/library/compiler/generator-x86.factor index 79b7e2fb21..4f72e3f158 100644 --- a/library/compiler/generator-x86.factor +++ b/library/compiler/generator-x86.factor @@ -40,31 +40,6 @@ USE: strings USE: words USE: vectors -: 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] ; - -: immediate? ( obj -- ? ) - #! fixnums and f have a pointerless representation, and - #! are compiled immediately. Everything else can be moved - #! by GC, and is indexed through a table. - dup fixnum? swap f eq? or ; - -: compile-literal ( obj -- ) - dup immediate? [ - address LITERAL - ] [ - intern-literal [LITERAL] - ] ifte ; - : PUSH-DS ( -- ) #! Push contents of EAX onto datastack. 4 ESI R+I @@ -79,7 +54,13 @@ USE: vectors #! Call named C function in Factor interpreter executable. dlsym-self CALL JUMP-FIXUP ; -#push [ compile-literal ] "generator" set-word-property +#push-immediate [ + address 4 ESI R+I ESI I>[R] +] "generator" set-word-property + +#push-indirect [ + intern-literal 4 ESI R+I EAX [I]>R EAX ESI R>[R] +] "generator" set-word-property #call [ dup postpone-word diff --git a/library/compiler/linearizer.factor b/library/compiler/linearizer.factor index e51696d8f1..7590fc97cc 100644 --- a/library/compiler/linearizer.factor +++ b/library/compiler/linearizer.factor @@ -32,6 +32,9 @@ USE: stack USE: namespaces USE: inference USE: combinators +USE: math +USE: logic +USE: kernel ! The linear IR is close to assembly language. It also resembles ! Forth code in some sense. It exists so that pattern matching @@ -40,6 +43,8 @@ USE: combinators ! Linear IR nodes. This is in addition to the symbols already ! defined in inference vocab. +SYMBOL: #push-immediate +SYMBOL: #push-indirect SYMBOL: #jump-label-t ( branch if top of stack is true ) SYMBOL: #jump-label ( unconditional branch ) SYMBOL: #jump ( tail-call ) @@ -66,6 +71,18 @@ SYMBOL: #return-to ( push addr on C stack ) #! rest is arguments. [ (linearize) ] make-list ; +: immediate? ( obj -- ? ) + #! fixnums and f have a pointerless representation, and + #! are compiled immediately. Everything else can be moved + #! by GC, and is indexed through a table. + dup fixnum? swap f eq? or ; + +#push [ + [ node-param get ] bind + dup immediate? #push-immediate #push-indirect ? + swons , +] "linearizer" set-word-property + :