| 
									
										
										
										
											2009-01-13 18:12:43 -05:00
										 |  |  | ! Copyright (C) 2004, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | USING: accessors kernel namespaces arrays sequences io words fry | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:41 -04:00
										 |  |  | continuations vocabs assocs dlists definitions math graphs generic | 
					
						
							| 
									
										
										
										
											2009-04-10 04:52:12 -04:00
										 |  |  | combinators deques search-deques macros io source-files.errors stack-checker | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:41 -04:00
										 |  |  | stack-checker.state stack-checker.inlining combinators.short-circuit | 
					
						
							|  |  |  | compiler.errors compiler.units compiler.tree.builder | 
					
						
							|  |  |  | compiler.tree.optimizer compiler.cfg.builder compiler.cfg.optimizer | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | compiler.cfg.linearization compiler.cfg.two-operand | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:41 -04:00
										 |  |  | compiler.cfg.linear-scan compiler.cfg.stack-frame compiler.codegen | 
					
						
							|  |  |  | compiler.utilities ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: compiler | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | SYMBOL: compile-queue | 
					
						
							|  |  |  | SYMBOL: compiled | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : queue-compile? ( word -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  |         [ "forgotten" word-prop ] | 
					
						
							|  |  |  |         [ compiled get key? ] | 
					
						
							|  |  |  |         [ inlined-block? ] | 
					
						
							|  |  |  |         [ primitive? ] | 
					
						
							|  |  |  |     } 1|| not ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : queue-compile ( word -- )
 | 
					
						
							|  |  |  |     dup queue-compile? [ compile-queue get push-front ] [ drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : maybe-compile ( word -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-23 01:37:02 -05:00
										 |  |  |     dup optimized>> [ drop ] [ queue-compile ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | SYMBOLS: +optimized+ +unoptimized+ ;
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ripple-up ( words -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  |     dup "compiled-status" word-prop +unoptimized+ eq?
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     [ usage [ word? ] filter ] [ compiled-usage keys ] if
 | 
					
						
							|  |  |  |     [ queue-compile ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 21:16:31 -04:00
										 |  |  | : ripple-up? ( status word -- ? )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ nip changed-effects get key? ] | 
					
						
							|  |  |  |         [ "compiled-status" word-prop eq? not ] 2bi or
 | 
					
						
							|  |  |  |     ] keep "compiled-status" word-prop and ;
 | 
					
						
							| 
									
										
										
										
											2007-12-19 20:55:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : save-compiled-status ( word status -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-22 21:16:31 -04:00
										 |  |  |     [ over ripple-up? [ ripple-up ] [ drop ] if ] | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  |     [ "compiled-status" set-word-prop ] | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     2bi ;
 | 
					
						
							| 
									
										
										
										
											2007-12-19 20:55:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | : start ( word -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  |     "trace-compilation" get [ dup name>> print flush ] when
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     H{ } clone dependencies set
 | 
					
						
							|  |  |  |     H{ } clone generic-dependencies set
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |     f swap compiler-error ;
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:41 -04:00
										 |  |  | : ignore-error? ( word error -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-04-12 17:08:46 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             [ inline? ] | 
					
						
							|  |  |  |             [ macro? ] | 
					
						
							|  |  |  |             [ "transform-quot" word-prop ] | 
					
						
							|  |  |  |             [ "no-compile" word-prop ] | 
					
						
							|  |  |  |             [ "special" word-prop ] | 
					
						
							|  |  |  |         } 1|| | 
					
						
							|  |  |  |     ] [ error-type +compiler-warning+ eq? ] bi* and ;
 | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : fail ( word error -- * )
 | 
					
						
							| 
									
										
										
										
											2009-04-12 17:08:46 -04:00
										 |  |  |     [ 2dup ignore-error? [ drop f ] when swap compiler-error ] | 
					
						
							| 
									
										
										
										
											2007-12-24 21:41:46 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |         drop
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |         [ compiled-unxref ] | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |         [ f swap compiled get set-at ] | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  |         [ +unoptimized+ save-compiled-status ] | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     ] 2bi
 | 
					
						
							|  |  |  |     return ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : frontend ( word -- nodes )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     [ build-tree-from-word ] [ fail ] recover optimize-tree ;
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-12 18:37:42 -04:00
										 |  |  | ! Only switch this off for debugging. | 
					
						
							|  |  |  | SYMBOL: compile-dependencies? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | t compile-dependencies? set-global
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | : save-asm ( asm -- )
 | 
					
						
							|  |  |  |     [ [ code>> ] [ label>> ] bi compiled get set-at ] | 
					
						
							| 
									
										
										
										
											2008-10-12 18:37:42 -04:00
										 |  |  |     [ compile-dependencies? get [ calls>> [ maybe-compile ] each ] [ drop ] if ] | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : backend ( nodes word -- )
 | 
					
						
							|  |  |  |     build-cfg [ | 
					
						
							| 
									
										
										
										
											2008-10-22 19:39:41 -04:00
										 |  |  |         optimize-cfg | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |         build-mr | 
					
						
							| 
									
										
										
										
											2008-10-28 05:38:37 -04:00
										 |  |  |         convert-two-operand | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |         linear-scan | 
					
						
							|  |  |  |         build-stack-frame | 
					
						
							|  |  |  |         generate | 
					
						
							|  |  |  |         save-asm | 
					
						
							|  |  |  |     ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : finish ( word -- )
 | 
					
						
							|  |  |  |     [ +optimized+ save-compiled-status ] | 
					
						
							| 
									
										
										
										
											2008-10-19 02:10:21 -04:00
										 |  |  |     [ compiled-unxref ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup crossref? | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-11-22 23:46:57 -05:00
										 |  |  |             dependencies get
 | 
					
						
							|  |  |  |             generic-dependencies get
 | 
					
						
							| 
									
										
										
										
											2008-10-19 02:10:21 -04:00
										 |  |  |             compiled-xref | 
					
						
							|  |  |  |         ] [ drop ] if
 | 
					
						
							|  |  |  |     ] tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | : (compile) ( word -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  |     '[ | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |         _ { | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |             [ start ] | 
					
						
							|  |  |  |             [ frontend ] | 
					
						
							|  |  |  |             [ backend ] | 
					
						
							|  |  |  |             [ finish ] | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |         } cleave
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  |     ] with-return ;
 | 
					
						
							| 
									
										
										
										
											2007-12-24 21:41:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-19 15:06:20 -04:00
										 |  |  | : compile-loop ( deque -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-16 07:27:01 -04:00
										 |  |  |     [ (compile) yield-hook get call( -- ) ] slurp-deque ;
 | 
					
						
							| 
									
										
										
										
											2007-12-17 16:29:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  | : decompile ( word -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-24 21:17:11 -05:00
										 |  |  |     f 2array 1array modify-code-heap ;
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 00:55:16 -05:00
										 |  |  | : compile-call ( quot -- )
 | 
					
						
							|  |  |  |     [ dup infer define-temp ] with-compilation-unit execute ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-12 17:08:46 -04:00
										 |  |  | \ compile-call t "no-compile" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  | SINGLETON: optimizing-compiler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: optimizing-compiler recompile ( words -- alist )
 | 
					
						
							| 
									
										
										
										
											2007-12-17 16:29:54 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |         <hashed-dlist> compile-queue set
 | 
					
						
							| 
									
										
										
										
											2007-12-24 21:41:46 -05:00
										 |  |  |         H{ } clone compiled set
 | 
					
						
							| 
									
										
										
										
											2008-02-25 04:38:37 -05:00
										 |  |  |         [ queue-compile ] each
 | 
					
						
							| 
									
										
										
										
											2007-12-24 21:41:46 -05:00
										 |  |  |         compile-queue get compile-loop | 
					
						
							| 
									
										
										
										
											2008-11-12 01:10:50 -05:00
										 |  |  |         compiled get >alist
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  |     ] with-scope ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | : enable-compiler ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  |     optimizing-compiler compiler-impl set-global ;
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : disable-compiler ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  |     f compiler-impl set-global ;
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-27 17:26:39 -05:00
										 |  |  | : recompile-all ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  |     forget-errors all-words compile ;
 |