| 
									
										
										
										
											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-08-15 00:35:19 -04:00
										 |  |  | USING: kernel namespaces arrays sequences io debugger words fry | 
					
						
							| 
									
										
										
										
											2008-08-12 04:31:48 -04:00
										 |  |  | compiler.units continuations vocabs assocs dlists definitions | 
					
						
							| 
									
										
										
										
											2008-08-27 06:52:38 -04:00
										 |  |  | math threads graphs generic combinators deques search-deques | 
					
						
							| 
									
										
										
										
											2008-08-12 04:38:56 -04:00
										 |  |  | stack-checker stack-checker.state compiler.generator | 
					
						
							|  |  |  | compiler.errors compiler.tree.builder compiler.tree.optimizer ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: compiler | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-07 08:49:36 -04:00
										 |  |  | : compile-begins ( word -- )
 | 
					
						
							|  |  |  |     f swap compiler-error ;
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  | : compile-failed ( word error -- )
 | 
					
						
							|  |  |  |     [ 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-05-07 08:49:36 -04:00
										 |  |  |     ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  | : compile-succeeded ( effect word -- )
 | 
					
						
							|  |  |  |     [ swap save-effect ] | 
					
						
							|  |  |  |     [ compiled-unxref ] | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-06-06 21:47:09 -04:00
										 |  |  |         dup crossref? | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-09-03 19:23:48 -04:00
										 |  |  |             dependencies get >alist
 | 
					
						
							|  |  |  |             generic-dependencies get >alist
 | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |             compiled-xref | 
					
						
							|  |  |  |         ] [ drop ] if
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |     ] tri ;
 | 
					
						
							| 
									
										
										
										
											2008-01-01 14:54:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (compile) ( word -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  |     '[ | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |         H{ } clone dependencies set
 | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |         H{ } clone generic-dependencies set
 | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |         _ { | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |             [ compile-begins ] | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2008-08-12 04:38:56 -04:00
										 |  |  |                 [ build-tree-from-word ] [ compile-failed return ] recover
 | 
					
						
							|  |  |  |                 optimize-tree | 
					
						
							| 
									
										
										
										
											2008-05-07 08:49:36 -04:00
										 |  |  |             ] | 
					
						
							|  |  |  |             [ dup generate ] | 
					
						
							|  |  |  |             [ compile-succeeded ] | 
					
						
							|  |  |  |         } 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-02-17 19:38:29 -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 ;
 |