| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | ! Copyright (C) 2004, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  | USING: accessors kernel namespaces arrays sequences io | 
					
						
							|  |  |  | words fry continuations vocabs assocs dlists definitions math | 
					
						
							|  |  |  | threads graphs generic combinators deques search-deques io | 
					
						
							|  |  |  | stack-checker stack-checker.state stack-checker.inlining | 
					
						
							|  |  |  | compiler.errors compiler.units compiler.tree.builder | 
					
						
							|  |  |  | compiler.tree.optimizer compiler.cfg.builder | 
					
						
							|  |  |  | compiler.cfg.optimizer compiler.cfg.linearization | 
					
						
							|  |  |  | compiler.cfg.two-operand compiler.cfg.linear-scan | 
					
						
							|  |  |  | compiler.cfg.stack-frame compiler.codegen ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: compiler | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | SYMBOL: compile-queue | 
					
						
							|  |  |  | SYMBOL: compiled | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : queue-compile ( word -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ dup "forgotten" word-prop ] [ ] } | 
					
						
							|  |  |  |         { [ dup compiled get key? ] [ ] } | 
					
						
							|  |  |  |         { [ dup inlined-block? ] [ ] } | 
					
						
							|  |  |  |         { [ dup primitive? ] [ ] } | 
					
						
							|  |  |  |         [ dup compile-queue get push-front ] | 
					
						
							|  |  |  |     } cond drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : maybe-compile ( word -- )
 | 
					
						
							|  |  |  |     dup compiled>> [ drop ] [ queue-compile ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  | SYMBOL: +failed+ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ripple-up ( words -- )
 | 
					
						
							|  |  |  |     dup "compiled-effect" word-prop +failed+ eq?
 | 
					
						
							|  |  |  |     [ usage [ word? ] filter ] [ compiled-usage keys ] if
 | 
					
						
							|  |  |  |     [ queue-compile ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ripple-up? ( word effect -- ? )
 | 
					
						
							|  |  |  |     #! If the word has previously been compiled and had a | 
					
						
							|  |  |  |     #! different stack effect, we have to recompile any callers. | 
					
						
							|  |  |  |     swap "compiled-effect" word-prop [ = not ] keep and ;
 | 
					
						
							| 
									
										
										
										
											2007-12-19 20:55:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : save-effect ( word effect -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     [ dupd ripple-up? [ ripple-up ] [ drop ] if ] | 
					
						
							|  |  |  |     [ "compiled-effect" set-word-prop ] | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  | : fail ( word error -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |     [ 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 ] | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |         [ +failed+ save-effect ] | 
					
						
							|  |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:17:55 -04:00
										 |  |  |     ] 2bi
 | 
					
						
							|  |  |  |     return ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : frontend ( word -- effect nodes )
 | 
					
						
							|  |  |  |     [ 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-19 02:10:21 -04:00
										 |  |  | : finish ( effect word -- )
 | 
					
						
							|  |  |  |     [ swap save-effect ] | 
					
						
							|  |  |  |     [ 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 -- )
 | 
					
						
							|  |  |  |     [ (compile) yield ] slurp-deque ;
 | 
					
						
							| 
									
										
										
										
											2007-12-17 16:29:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  | : decompile ( word -- )
 | 
					
						
							|  |  |  |     f 2array 1array t modify-code-heap ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : optimized-recompile-hook ( 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 ( -- )
 | 
					
						
							|  |  |  |     [ optimized-recompile-hook ] recompile-hook set-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : disable-compiler ( -- )
 | 
					
						
							|  |  |  |     [ default-recompile-hook ] recompile-hook set-global ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-27 17:26:39 -05:00
										 |  |  | : recompile-all ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-24 03:19:38 -05:00
										 |  |  |     forget-errors all-words compile ;
 |