compiler work

cvs
Slava Pestov 2004-12-05 04:45:41 +00:00
parent cd794415f0
commit 451bd02c0b
12 changed files with 98 additions and 33 deletions

View File

@ -12,14 +12,13 @@
- handle recursion with when, when* etc - handle recursion with when, when* etc
- optimizer rewrite stack ops - optimizer rewrite stack ops
- alien-call need special nodes - alien-call need special nodes
- fix dataflow unit tests - mutual recursion is borked with certain branch order
- fix inference of + = and others
+ linearizer/generator: + linearizer/generator:
- peephole optimizer - peephole optimizer
- linearize generic, 2generic - linearize generic, 2generic
- generate conditionals
- generator needs to be aware of labels
- getenv/setenv: if literal arg, compile as a load/store - getenv/setenv: if literal arg, compile as a load/store
- compiler: drop literal peephole optimization - compiler: drop literal peephole optimization

View File

@ -44,9 +44,15 @@ USE: unparser
USE: vectors USE: vectors
USE: words USE: words
! <LittleDan> peephole?
! <LittleDan> "whose peephole are we optimizing" "your mom's"
: begin-compiling ( word -- definition )
cell compile-aligned dup save-xt word-parameter ;
: (compile) ( word -- ) : (compile) ( word -- )
#! Should be called inside the with-compiler scope. #! Should be called inside the with-compiler scope.
dup save-xt word-parameter dataflow linearize generate ; begin-compiling dataflow optimize linearize generate ;
: compile-postponed ( -- ) : compile-postponed ( -- )
compile-words get [ compile-words get [
@ -56,7 +62,7 @@ USE: words
: compile ( word -- ) : compile ( word -- )
[ postpone-word compile-postponed ] with-compiler ; [ postpone-word compile-postponed ] with-compiler ;
: compiled : compiled ( -- )
#! Compile the most recently defined word. #! Compile the most recently defined word.
word compile ; parsing word compile ; parsing

View File

@ -93,6 +93,43 @@ USE: words
"arithmetic_type" SELF-CALL "arithmetic_type" SELF-CALL
8 ESP R+I ; 8 ESP R+I ;
\ #push [ compile-literal ] "generator" set-word-property #push [ compile-literal ] "generator" set-word-property
\ #call [ CALL compiled-offset defer-xt ] "generator" set-word-property
\ #return [ drop RET ] "generator" set-word-property #call [
dup postpone-word
CALL compiled-offset defer-xt
] "generator" set-word-property
#call-label [
CALL compiled-offset defer-xt
] "generator" set-word-property
#jump-label [
JUMP compiled-offset defer-xt
] "generator" set-word-property
#jump-label-t [
POP-DS
! condition is now in EAX
f address EAX CMP-I-R
! jump w/ address added later
JNE compiled-offset defer-xt
] "generator" set-word-property
#return [ drop RET ] "generator" set-word-property
#drop [ drop 4 ESI R-I ] "generator" set-word-property
#dup [
drop
ESI EAX [R]>R
4 ESI R+I
EAX ESI R>[R]
] "generator" set-word-property
#swap [ drop \ swap CALL compiled-offset defer-xt ] "generator" set-word-property
#over [ drop \ over 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

View File

@ -47,3 +47,6 @@ USE: words
: generate ( linear -- ) : generate ( linear -- )
#! Compile a word definition from linear IR. #! Compile a word definition from linear IR.
[ generate-node ] each ; [ generate-node ] each ;
#label [ save-xt ] "generator" set-word-property
#values [ nop ] "generator" set-word-property

View File

@ -114,8 +114,10 @@ USE: logic
#! is destructively modified. #! is destructively modified.
dup kill-set swap kill-nodes ; dup kill-set swap kill-nodes ;
: kill-branches ( literals branchlist -- branchlist ) : kill-branches ( literals node -- )
[ dupd kill-nodes ] map nip ; [
node-param [ [ dupd kill-nodes ] map nip ] change
] extend , ;
#push [ #push [
[ node-param get ] bind , [ node-param get ] bind ,
@ -135,24 +137,15 @@ USE: logic
#ifte [ scan-branches ] "scan-literal" set-word-property #ifte [ scan-branches ] "scan-literal" set-word-property
#ifte [ can-kill-branches? ] "can-kill" set-word-property #ifte [ can-kill-branches? ] "can-kill" set-word-property
#ifte [ kill-branches ] "kill-node" set-word-property
#ifte [ ( literals node -- )
[ node-param [ kill-branches ] change ] extend ,
] "kill-node" set-word-property
#generic [ scan-branches ] "scan-literal" set-word-property #generic [ scan-branches ] "scan-literal" set-word-property
#generic [ can-kill-branches? ] "can-kill" set-word-property #generic [ can-kill-branches? ] "can-kill" set-word-property
#generic [ kill-branches ] "kill-node" set-word-property
#generic [ ( literals node -- )
[ node-param [ kill-branches ] change ] extend ,
] "kill-node" set-word-property
#2generic [ scan-branches ] "scan-literal" set-word-property #2generic [ scan-branches ] "scan-literal" set-word-property
#2generic [ can-kill-branches? ] "can-kill" set-word-property #2generic [ can-kill-branches? ] "can-kill" set-word-property
#2generic [ kill-branches ] "kill-node" set-word-property
#2generic [ ( literals node -- )
[ node-param [ kill-branches ] change ] extend ,
] "kill-node" set-word-property
! Don't care about inputs to recursive combinator calls ! Don't care about inputs to recursive combinator calls
#call-label [ 2drop t ] "can-kill" set-word-property #call-label [ 2drop t ] "can-kill" set-word-property

View File

@ -56,7 +56,6 @@ USE: words
SYMBOL: compiled-xts SYMBOL: compiled-xts
: save-xt ( word -- ) : save-xt ( word -- )
cell compile-aligned
compiled-offset swap compiled-xts acons@ ; compiled-offset swap compiled-xts acons@ ;
: commit-xt ( xt word -- ) : commit-xt ( xt word -- )

View File

@ -50,6 +50,7 @@ USE: hashtables
copy-interpreter copy-interpreter
dataflow-graph off dataflow-graph off
infer-quot infer-quot
( #values values-node )
branch-effect branch-effect
] with-scope ; ] with-scope ;
@ -131,7 +132,7 @@ USE: hashtables
#! Infer effects for both branches, unify. #! Infer effects for both branches, unify.
3 ensure-d 3 ensure-d
dataflow-drop, pop-d dataflow-drop, pop-d
dataflow-drop, pop-d 2list dataflow-drop, pop-d swap 2list
>r 1 meta-d get vector-tail* #ifte r> >r 1 meta-d get vector-tail* #ifte r>
pop-d drop ( condition ) pop-d drop ( condition )
infer-branches ; infer-branches ;

View File

@ -48,6 +48,11 @@ SYMBOL: #ifte
SYMBOL: #generic SYMBOL: #generic
SYMBOL: #2generic SYMBOL: #2generic
! This is purely a marker for values we retain after a
! conditional. It does not generate code, but merely alerts the
! dataflow optimizer to the fact these values must be retained.
SYMBOL: #values
SYMBOL: #return SYMBOL: #return
SYMBOL: #drop SYMBOL: #drop

View File

@ -143,17 +143,22 @@ DEFER: apply-word
2drop 2drop
] ifte ; ] ifte ;
: return-node ( -- ) : check-return ( -- )
#! Add a #return node to the dataflow graph. #! Raise an error if word leaves values on return stack.
f #return dataflow, [ meta-r get vector-length 0 = [
"Word leaves elements on return stack" throw
] unless ;
: values-node ( op -- )
#! Add a #values or #return node to the graph.
f swap dataflow, [
meta-d get vector>list node-consume-d set meta-d get vector>list node-consume-d set
meta-r get vector-length 0 = [
"Word leaves elements on return stack" throw
] unless
] bind ; ] bind ;
: (infer) ( quot -- ) : (infer) ( quot -- )
f init-inference infer-quot return-node ; f init-inference
infer-quot
#return values-node check-return ;
: infer ( quot -- [ in | out ] ) : infer ( quot -- [ in | out ] )
#! Stack effect of a quotation. #! Stack effect of a quotation.

View File

@ -19,7 +19,15 @@ USE: words
: dataflow-contains-param? ( object list -- ? ) : dataflow-contains-param? ( object list -- ? )
#! Check if some dataflow node contains a given operation. #! Check if some dataflow node contains a given operation.
[ dupd node-param swap hash = ] some? nip ; [
dupd [
node-op get #label = [
node-param get dataflow-contains-param?
] [
node-param get =
] ifte
] bind
] some? nip ;
[ t ] [ [ t ] [
\ + [ 2 2 + ] dataflow dataflow-contains-param? >boolean \ + [ 2 2 + ] dataflow dataflow-contains-param? >boolean
@ -82,3 +90,10 @@ SYMBOL: #test
[ node-param | 5 ] [ node-param | 5 ]
}} "foobar" [ [ node-param get ] bind succ ] apply-dataflow }} "foobar" [ [ node-param get ] bind succ ] apply-dataflow
] unit-test ] unit-test
! Somebody (cough) got the order of ifte nodes wrong.
[ t ] [
#ifte [ [ 1 ] [ 2 ] ifte ] dataflow dataflow-contains-op? car
[ node-param get ] bind car car [ node-param get ] bind 1 =
] unit-test

View File

@ -11,3 +11,5 @@ USE: combinators
[ [ ] ] [ \ foo word-parameter dataflow kill-set ] unit-test [ [ ] ] [ \ foo word-parameter dataflow kill-set ] unit-test
[ [ [ + ] [ - ] ] ] [ [ 3 4 1 2 > [ + ] [ - ] ifte ] dataflow kill-set ] unit-test [ [ [ + ] [ - ] ] ] [ [ 3 4 1 2 > [ + ] [ - ] ifte ] dataflow kill-set ] unit-test
[ [ [ 1 ] [ 2 ] ] ] [ [ [ 1 ] [ 2 ] ifte ] dataflow kill-set ] unit-test

View File

@ -29,7 +29,7 @@ USE: words
[ 1 ] [ dummy-ifte-5 ] unit-test [ 1 ] [ dummy-ifte-5 ] unit-test
: dummy-ifte-6 : dummy-ifte-6
dup 1 <= [ dup 1 fixnum<= [
drop 1 drop 1
] [ ] [
1 fixnum- dup swap 1 fixnum- fixnum+ 1 fixnum- dup swap 1 fixnum- fixnum+