-! Copyright (C) 2007, 2009 Slava Pestov.
+! Copyright (C) 2007, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors cpu.architecture vocabs.loader system
sequences namespaces parser kernel kernel.private classes
gc
: compile-unoptimized ( words -- )
+ [ [ subwords ] map ] keep suffix concat
[ optimized? not ] filter compile ;
"debug-compiler" get [
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
SYMBOL: labels
-: init-generator ( -- )
- H{ } clone labels set
- V{ } clone calls set ;
-
-: generate-insns ( asm -- code )
+: generate ( mr -- code )
dup label>> [
- init-generator
+ H{ } clone labels set
instructions>> [
[ class insn-counts get inc-at ]
[ generate-insn ]
] each
] with-fixup ;
-: generate ( mr -- asm )
- [
- [ label>> ] [ generate-insns ] bi calls get
- asm boa
- ] with-scope ;
-
: lookup-label ( id -- label )
labels get [ drop <label> ] cache ;
! Special cases
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
label>> lookup-label
cell 0 <repetition> %
CODEGEN: ##replace %replace
CODEGEN: ##inc-d %inc-d
CODEGEN: ##inc-r %inc-r
+CODEGEN: ##call %call
+CODEGEN: ##jump %jump
CODEGEN: ##return %return
CODEGEN: ##slot %slot
CODEGEN: ##slot-imm %slot-imm
USING: assocs compiler.cfg.builder compiler.cfg.optimizer
compiler.errors compiler.tree.builder compiler.tree.optimizer
-compiler.units help.markup help.syntax io parser quotations
-sequences words ;
+compiler.units compiler.codegen help.markup help.syntax io
+parser quotations sequences words ;
IN: compiler
HELP: enable-optimizer
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."
$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 } "."
$nl
"The " { $link compile-word } " word performs the actual task of compiling an individual word. The process proceeds as follows:"
{ "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." }
{ "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."
$nl
! Copyright (C) 2004, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel namespaces arrays sequences io words fry
-continuations vocabs assocs dlists definitions math graphs
-generic generic.single combinators combinators.smart deques
-search-deques macros source-files.errors
-combinators.short-circuit classes.algebra
+continuations vocabs assocs definitions math graphs generic
+generic.single combinators combinators.smart macros
+source-files.errors combinators.short-circuit classes.algebra
stack-checker stack-checker.dependencies stack-checker.inlining
stack-checker.errors
compiler.codegen ;
IN: compiler
-SYMBOL: compile-queue
SYMBOL: compiled
: compile? ( word -- ? )
#! Don't attempt to compile certain words.
{
[ "forgotten" word-prop ]
- [ compiled get key? ]
[ inlined-block? ]
} 1|| not ;
-: queue-compile ( word -- )
- dup compile? [ compile-queue get push-front ] [ drop ] if ;
-
: compiler-message ( string -- )
"trace-compilation" get [ global [ print flush ] bind ] [ drop ] if ;
contains-breakpoints? [ nip deoptimize* ] [ drop ] 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 -- )
build-cfg [
[ optimize-cfg build-mr ] with-cfg
- generate
- save-asm
+ [ generate ] [ label>> ] bi compiled get set-at
] each ;
: compile-word ( word -- )
} cleave
] with-return ;
-: compile-loop ( deque -- )
- [ compile-word yield-hook get call( -- ) ] slurp-deque ;
-
SINGLETON: optimizing-compiler
M: optimizing-compiler update-call-sites ( class generic -- words )
] assoc-filter keys ;
M: optimizing-compiler recompile ( words -- alist )
- [
- <hashed-dlist> compile-queue set
- H{ } clone compiled set
- [
- [ queue-compile ]
- [ subwords [ compile-dependency ] each ] bi
- ] each
- compile-queue get compile-loop
+ H{ } clone compiled [
+ [ compile? ] filter
+ [ compile-word yield-hook get call( -- ) ] each
compiled get >alist
- ] with-scope
+ ] with-variable
"--- compile done" compiler-message ;
M: optimizing-compiler to-recompile ( -- words )
: compile-cfg ( cfg -- word )
gensym
- [ build-mr generate code>> ] dip
+ [ build-mr generate ] dip
[ associate >alist t t modify-code-heap ] keep ;
: compile-test-cfg ( -- word )