compiler: remove some unnecessary complexity

db4
Slava Pestov 2010-02-04 01:36:52 +13:00
parent c011380a19
commit 8d3c11c176
5 changed files with 18 additions and 72 deletions

View File

@ -1,4 +1,4 @@
! Copyright (C) 2007, 2009 Slava Pestov. ! Copyright (C) 2007, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors cpu.architecture vocabs.loader system USING: accessors cpu.architecture vocabs.loader system
sequences namespaces parser kernel kernel.private classes sequences namespaces parser kernel kernel.private classes
@ -33,6 +33,7 @@ enable-optimizer
gc gc
: compile-unoptimized ( words -- ) : compile-unoptimized ( words -- )
[ [ subwords ] map ] keep suffix concat
[ optimized? not ] filter compile ; [ optimized? not ] filter compile ;
"debug-compiler" get [ "debug-compiler" get [

View File

@ -24,24 +24,12 @@ H{ } clone insn-counts set-global
GENERIC: generate-insn ( insn -- ) GENERIC: generate-insn ( insn -- )
TUPLE: asm label code calls ;
SYMBOL: calls
: add-call ( word -- )
#! Compile this word later.
calls get push ;
! Mapping _label IDs to label instances ! Mapping _label IDs to label instances
SYMBOL: labels SYMBOL: labels
: init-generator ( -- ) : generate ( mr -- code )
H{ } clone labels set
V{ } clone calls set ;
: generate-insns ( asm -- code )
dup label>> [ dup label>> [
init-generator H{ } clone labels set
instructions>> [ instructions>> [
[ class insn-counts get inc-at ] [ class insn-counts get inc-at ]
[ generate-insn ] [ generate-insn ]
@ -49,22 +37,12 @@ SYMBOL: labels
] each ] each
] with-fixup ; ] with-fixup ;
: generate ( mr -- asm )
[
[ label>> ] [ generate-insns ] bi calls get
asm boa
] with-scope ;
: lookup-label ( id -- label ) : lookup-label ( id -- label )
labels get [ drop <label> ] cache ; labels get [ drop <label> ] cache ;
! Special cases ! Special cases
M: ##no-tco generate-insn drop ; M: ##no-tco generate-insn drop ;
M: ##call generate-insn word>> [ add-call ] [ %call ] bi ;
M: ##jump generate-insn word>> [ add-call ] [ %jump ] bi ;
M: _dispatch-label generate-insn M: _dispatch-label generate-insn
label>> lookup-label label>> lookup-label
cell 0 <repetition> % cell 0 <repetition> %
@ -104,6 +82,8 @@ CODEGEN: ##peek %peek
CODEGEN: ##replace %replace CODEGEN: ##replace %replace
CODEGEN: ##inc-d %inc-d CODEGEN: ##inc-d %inc-d
CODEGEN: ##inc-r %inc-r CODEGEN: ##inc-r %inc-r
CODEGEN: ##call %call
CODEGEN: ##jump %jump
CODEGEN: ##return %return CODEGEN: ##return %return
CODEGEN: ##slot %slot CODEGEN: ##slot %slot
CODEGEN: ##slot-imm %slot-imm CODEGEN: ##slot-imm %slot-imm

View File

@ -1,7 +1,7 @@
USING: assocs compiler.cfg.builder compiler.cfg.optimizer USING: assocs compiler.cfg.builder compiler.cfg.optimizer
compiler.errors compiler.tree.builder compiler.tree.optimizer compiler.errors compiler.tree.builder compiler.tree.optimizer
compiler.units help.markup help.syntax io parser quotations compiler.units compiler.codegen help.markup help.syntax io
sequences words ; parser quotations sequences words ;
IN: compiler IN: compiler
HELP: enable-optimizer HELP: enable-optimizer
@ -21,8 +21,6 @@ ARTICLE: "compiler-usage" "Calling the optimizing compiler"
ARTICLE: "compiler-impl" "Compiler implementation" ARTICLE: "compiler-impl" "Compiler implementation"
"The " { $vocab-link "compiler" } "vocabulary, in addition to providing the user-visible words of the compiler, implements the main compilation loop." "The " { $vocab-link "compiler" } "vocabulary, in addition to providing the user-visible words of the compiler, implements the main compilation loop."
$nl $nl
"Words are added to the " { $link compile-queue } " variable as needed and compiled."
{ $subsections compile-queue }
"Once compiled, a word is added to the assoc stored in the " { $link compiled } " variable. When compilation is complete, this assoc is passed to " { $link modify-code-heap } "." "Once compiled, a word is added to the assoc stored in the " { $link compiled } " variable. When compilation is complete, this assoc is passed to " { $link modify-code-heap } "."
$nl $nl
"The " { $link compile-word } " word performs the actual task of compiling an individual word. The process proceeds as follows:" "The " { $link compile-word } " word performs the actual task of compiling an individual word. The process proceeds as follows:"
@ -30,7 +28,7 @@ $nl
{ "The " { $link frontend } " word calls " { $link build-tree } ". If this fails, the error is passed to " { $link deoptimize } ". The logic for ignoring certain compile errors generated for inline words and macros is located here. If the error is not ignorable, it is added to the global " { $link compiler-errors } " assoc (see " { $link "compiler-errors" } ")." } { "The " { $link frontend } " word calls " { $link build-tree } ". If this fails, the error is passed to " { $link deoptimize } ". The logic for ignoring certain compile errors generated for inline words and macros is located here. If the error is not ignorable, it is added to the global " { $link compiler-errors } " assoc (see " { $link "compiler-errors" } ")." }
{ "If the word contains a breakpoint, compilation ends here. Otherwise, all remaining steps execute until machine code is generated. Any further errors thrown by the compiler are not reported as compile errors, but instead are ordinary exceptions. This is because they indicate bugs in the compiler, not errors in user code." } { "If the word contains a breakpoint, compilation ends here. Otherwise, all remaining steps execute until machine code is generated. Any further errors thrown by the compiler are not reported as compile errors, but instead are ordinary exceptions. This is because they indicate bugs in the compiler, not errors in user code." }
{ "The " { $link frontend } " word then calls " { $link optimize-tree } ". This produces the final optimized tree IR, and this stage of the compiler is complete." } { "The " { $link frontend } " word then calls " { $link optimize-tree } ". This produces the final optimized tree IR, and this stage of the compiler is complete." }
{ "The " { $link backend } " word calls " { $link build-cfg } " followed by " { $link optimize-cfg } " and a few other stages. Finally, it calls " { $link save-asm } ", and adds any uncompiled words called by this word to the compilation queue with " { $link compile-dependency } "." } { "The " { $link backend } " word calls " { $link build-cfg } " followed by " { $link optimize-cfg } " and a few other stages. Finally, it calls " { $link generate } "." }
} }
"If compilation fails, the word is stored in the " { $link compiled } " assoc with a value of " { $link f } ". This causes the VM to compile the word with the non-optimizing compiler." "If compilation fails, the word is stored in the " { $link compiled } " assoc with a value of " { $link f } ". This causes the VM to compile the word with the non-optimizing compiler."
$nl $nl

View File

@ -1,10 +1,9 @@
! Copyright (C) 2004, 2010 Slava Pestov. ! Copyright (C) 2004, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel namespaces arrays sequences io words fry USING: accessors kernel namespaces arrays sequences io words fry
continuations vocabs assocs dlists definitions math graphs continuations vocabs assocs definitions math graphs generic
generic generic.single combinators combinators.smart deques generic.single combinators combinators.smart macros
search-deques macros source-files.errors source-files.errors combinators.short-circuit classes.algebra
combinators.short-circuit classes.algebra
stack-checker stack-checker.dependencies stack-checker.inlining stack-checker stack-checker.dependencies stack-checker.inlining
stack-checker.errors stack-checker.errors
@ -22,20 +21,15 @@ compiler.cfg.mr
compiler.codegen ; compiler.codegen ;
IN: compiler IN: compiler
SYMBOL: compile-queue
SYMBOL: compiled SYMBOL: compiled
: compile? ( word -- ? ) : compile? ( word -- ? )
#! Don't attempt to compile certain words. #! Don't attempt to compile certain words.
{ {
[ "forgotten" word-prop ] [ "forgotten" word-prop ]
[ compiled get key? ]
[ inlined-block? ] [ inlined-block? ]
} 1|| not ; } 1|| not ;
: queue-compile ( word -- )
dup compile? [ compile-queue get push-front ] [ drop ] if ;
: compiler-message ( string -- ) : compiler-message ( string -- )
"trace-compilation" get [ global [ print flush ] bind ] [ drop ] if ; "trace-compilation" get [ global [ print flush ] bind ] [ drop ] if ;
@ -129,29 +123,10 @@ M: word combinator? inline? ;
contains-breakpoints? [ nip deoptimize* ] [ drop ] if contains-breakpoints? [ nip deoptimize* ] [ drop ] if
] [ deoptimize* ] if ; ] [ deoptimize* ] if ;
: compile-dependency ( word -- )
#! If a word calls an unoptimized word, try to compile the callee.
dup optimized? [ drop ] [ queue-compile ] if ;
! Only switch this off for debugging.
SYMBOL: compile-dependencies?
t compile-dependencies? set-global
: compile-dependencies ( asm -- )
compile-dependencies? get
[ calls>> [ compile-dependency ] each ] [ drop ] if ;
: save-asm ( asm -- )
[ [ code>> ] [ label>> ] bi compiled get set-at ]
[ compile-dependencies ]
bi ;
: backend ( tree word -- ) : backend ( tree word -- )
build-cfg [ build-cfg [
[ optimize-cfg build-mr ] with-cfg [ optimize-cfg build-mr ] with-cfg
generate [ generate ] [ label>> ] bi compiled get set-at
save-asm
] each ; ] each ;
: compile-word ( word -- ) : compile-word ( word -- )
@ -166,9 +141,6 @@ t compile-dependencies? set-global
} cleave } cleave
] with-return ; ] with-return ;
: compile-loop ( deque -- )
[ compile-word yield-hook get call( -- ) ] slurp-deque ;
SINGLETON: optimizing-compiler SINGLETON: optimizing-compiler
M: optimizing-compiler update-call-sites ( class generic -- words ) M: optimizing-compiler update-call-sites ( class generic -- words )
@ -180,16 +152,11 @@ M: optimizing-compiler update-call-sites ( class generic -- words )
] assoc-filter keys ; ] assoc-filter keys ;
M: optimizing-compiler recompile ( words -- alist ) M: optimizing-compiler recompile ( words -- alist )
[ H{ } clone compiled [
<hashed-dlist> compile-queue set [ compile? ] filter
H{ } clone compiled set [ compile-word yield-hook get call( -- ) ] each
[
[ queue-compile ]
[ subwords [ compile-dependency ] each ] bi
] each
compile-queue get compile-loop
compiled get >alist compiled get >alist
] with-scope ] with-variable
"--- compile done" compiler-message ; "--- compile done" compiler-message ;
M: optimizing-compiler to-recompile ( -- words ) M: optimizing-compiler to-recompile ( -- words )

View File

@ -8,7 +8,7 @@ IN: compiler.tests.low-level-ir
: compile-cfg ( cfg -- word ) : compile-cfg ( cfg -- word )
gensym gensym
[ build-mr generate code>> ] dip [ build-mr generate ] dip
[ associate >alist t t modify-code-heap ] keep ; [ associate >alist t t modify-code-heap ] keep ;
: compile-test-cfg ( -- word ) : compile-test-cfg ( -- word )