| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2011-11-30 19:02:37 -05:00
										 |  |  | USING: accessors arrays assocs classes classes.private | 
					
						
							|  |  |  | classes.tuple classes.tuple.private continuations definitions | 
					
						
							|  |  |  | generic init kernel kernel.private math namespaces sequences | 
					
						
							| 
									
										
										
										
											2012-05-04 12:05:15 -04:00
										 |  |  | sets source-files.errors vocabs words ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:01:01 -05:00
										 |  |  | FROM: namespaces => set ;
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | FROM: sets => members ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | IN: compiler.units | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: old-definitions | 
					
						
							|  |  |  | SYMBOL: new-definitions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: redefine-error def ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 20:14:53 -04:00
										 |  |  | : throw-redefine-error ( definition -- )
 | 
					
						
							| 
									
										
										
										
											2010-02-19 19:41:33 -05:00
										 |  |  |     \ redefine-error boa throw-continue ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  | : add-once ( key set -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:13:14 -04:00
										 |  |  |     dupd ?adjoin [ drop ] [ throw-redefine-error ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  | : (remember-definition) ( definition loc set -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-02 04:10:27 -05:00
										 |  |  |     [ over set-where ] dip add-once ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | : remember-definition ( definition loc -- )
 | 
					
						
							|  |  |  |     new-definitions get first (remember-definition) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | : fake-definition ( definition -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  |     old-definitions get [ delete ] with each ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | : remember-class ( class loc -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-23 20:14:53 -04:00
										 |  |  |     [ dup new-definitions get first in? [ dup throw-redefine-error ] when ] dip
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  |     new-definitions get second (remember-definition) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : forward-reference? ( word -- ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  |     dup old-definitions get [ in? ] with any? [ | 
					
						
							|  |  |  |         new-definitions get [ in? ] with any? not
 | 
					
						
							|  |  |  |     ] [ drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  | SYMBOL: compiler-impl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | HOOK: update-call-sites compiler-impl ( class generic -- words )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 16:25:53 -05:00
										 |  |  | : changed-call-sites ( class generic -- )
 | 
					
						
							|  |  |  |     update-call-sites [ changed-definition ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | M: generic update-generic ( class generic -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-20 16:25:53 -05:00
										 |  |  |     [ changed-call-sites ] | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  |     [ remake-generic drop ] | 
					
						
							| 
									
										
										
										
											2010-01-29 11:10:10 -05:00
										 |  |  |     [ changed-conditionally drop ] | 
					
						
							|  |  |  |     2tri ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: sequence update-methods ( class seq -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-29 03:53:14 -05:00
										 |  |  |     implementors [ update-generic ] with each ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  | HOOK: recompile compiler-impl ( words -- alist )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 07:08:04 -05:00
										 |  |  | HOOK: to-recompile compiler-impl ( -- words )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: process-forgotten-words compiler-impl ( words -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  | : compile ( words -- )
 | 
					
						
							|  |  |  |     recompile t f modify-code-heap ;
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-15 20:15:28 -04:00
										 |  |  | ! Non-optimizing compiler | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | M: f update-call-sites | 
					
						
							|  |  |  |     2drop { } ;
 | 
					
						
							| 
									
										
										
										
											2009-11-08 07:08:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f to-recompile | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     changed-definitions get members [ word? ] filter ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f recompile | 
					
						
							|  |  |  |     [ dup def>> ] { } map>assoc ;
 | 
					
						
							| 
									
										
										
										
											2009-11-08 07:08:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f process-forgotten-words drop ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 06:52:05 -04:00
										 |  |  | : without-optimizer ( quot -- )
 | 
					
						
							|  |  |  |     [ f compiler-impl ] dip with-variable ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  | : <definitions> ( -- pair ) { HS{ } HS{ } } [ clone ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: definition-observers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | GENERIC: definitions-changed ( set obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-25 20:37:43 -05:00
										 |  |  | [ V{ } clone definition-observers set-global ] | 
					
						
							| 
									
										
										
										
											2009-10-19 22:17:02 -04:00
										 |  |  | "compiler.units" add-startup-hook | 
					
						
							| 
									
										
										
										
											2008-02-25 20:37:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 09:21:31 -04:00
										 |  |  | ! This goes here because vocabs cannot depend on init | 
					
						
							|  |  |  | [ V{ } clone vocab-observers set-global ] | 
					
						
							| 
									
										
										
										
											2009-10-19 22:17:02 -04:00
										 |  |  | "vocabs" add-startup-hook | 
					
						
							| 
									
										
										
										
											2009-05-01 09:21:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | : add-definition-observer ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-25 20:37:43 -05:00
										 |  |  |     definition-observers get push ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : remove-definition-observer ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 01:23:08 -04:00
										 |  |  |     definition-observers get remove-eq! drop ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | : notify-definition-observers ( set -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  |     definition-observers get
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |     [ definitions-changed ] with each ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | ! Incremented each time stack effects potentially changed, used | 
					
						
							|  |  |  | ! by compiler.tree.propagation.call-effect for call( and execute( | 
					
						
							|  |  |  | ! inline caching | 
					
						
							| 
									
										
										
										
											2011-11-02 15:54:31 -04:00
										 |  |  | : effect-counter ( -- n ) REDEFINITION-COUNTER special-object ; inline
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 03:53:14 -05:00
										 |  |  | GENERIC: always-bump-effect-counter? ( defspec -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 03:53:14 -05:00
										 |  |  | M: object always-bump-effect-counter? drop f ;
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | : changed-vocabs ( set -- vocabs )
 | 
					
						
							|  |  |  |     members [ word? ] filter
 | 
					
						
							|  |  |  |     [ vocabulary>> dup [ lookup-vocab ] when ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | : updated-definitions ( -- set )
 | 
					
						
							|  |  |  |     HS{ } clone
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     forgotten-definitions get union! | 
					
						
							| 
									
										
										
										
											2013-03-10 21:04:37 -04:00
										 |  |  |     new-definitions get first union! | 
					
						
							|  |  |  |     new-definitions get second union! | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     changed-definitions get union! | 
					
						
							|  |  |  |     maybe-changed get union! | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  |     dup changed-vocabs over adjoin-all ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-15 01:27:02 -04:00
										 |  |  | : process-forgotten-definitions ( -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     forgotten-definitions get members | 
					
						
							| 
									
										
										
										
											2009-11-08 07:08:04 -05:00
										 |  |  |     [ [ word? ] filter process-forgotten-words ] | 
					
						
							| 
									
										
										
										
											2009-04-15 01:27:02 -04:00
										 |  |  |     [ [ delete-definition-errors ] each ] | 
					
						
							|  |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | : bump-effect-counter? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     changed-effects get members | 
					
						
							|  |  |  |     maybe-changed get members | 
					
						
							|  |  |  |     changed-definitions get members [ always-bump-effect-counter? ] filter
 | 
					
						
							|  |  |  |     3array combine new-words get [ in? not ] curry any? ;
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : bump-effect-counter ( -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-13 00:08:18 -05:00
										 |  |  |     bump-effect-counter? [ | 
					
						
							| 
									
										
										
										
											2011-11-02 15:54:31 -04:00
										 |  |  |         REDEFINITION-COUNTER special-object 0 or
 | 
					
						
							|  |  |  |         1 + REDEFINITION-COUNTER set-special-object | 
					
						
							| 
									
										
										
										
											2010-01-13 00:08:18 -05:00
										 |  |  |     ] when ;
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : notify-observers ( -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  |     updated-definitions dup null? | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  |     [ drop ] [ notify-definition-observers notify-error-observers ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  | : update-existing? ( defs -- ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     new-words get [ in? not ] curry any? ;
 | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : reset-pics? ( -- ? )
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     outdated-generics get null? not ;
 | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | : finish-compilation-unit ( -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-27 09:02:54 -05:00
										 |  |  |     [ ] [ | 
					
						
							|  |  |  |         remake-generics | 
					
						
							| 
									
										
										
										
											2010-02-01 08:49:05 -05:00
										 |  |  |         to-recompile [ | 
					
						
							|  |  |  |             recompile | 
					
						
							|  |  |  |             update-tuples | 
					
						
							|  |  |  |             process-forgotten-definitions | 
					
						
							|  |  |  |         ] keep update-existing? reset-pics? modify-code-heap | 
					
						
							| 
									
										
										
										
											2010-01-27 09:02:54 -05:00
										 |  |  |         bump-effect-counter | 
					
						
							|  |  |  |         notify-observers | 
					
						
							|  |  |  |     ] if-bootstrapping ;
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-03 09:20:48 -05:00
										 |  |  | TUPLE: nesting-observer new-words ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 12:11:18 -04:00
										 |  |  | M: nesting-observer definitions-changed | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |     [ members ] dip new-words>> [ delete ] curry each ;
 | 
					
						
							| 
									
										
										
										
											2010-02-03 09:20:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-nesting-observer ( -- )
 | 
					
						
							|  |  |  |     new-words get nesting-observer boa
 | 
					
						
							|  |  |  |     [ nesting-observer set ] [ add-definition-observer ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : remove-nesting-observer ( -- )
 | 
					
						
							|  |  |  |     nesting-observer get remove-definition-observer ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 04:52:14 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2008-05-28 20:34:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-nested-compilation-unit ( quot -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |         HS{ } clone changed-definitions set
 | 
					
						
							|  |  |  |         HS{ } clone maybe-changed set
 | 
					
						
							|  |  |  |         HS{ } clone changed-effects set
 | 
					
						
							|  |  |  |         HS{ } clone outdated-generics set
 | 
					
						
							| 
									
										
										
										
											2008-05-28 20:34:18 -04:00
										 |  |  |         H{ } clone outdated-tuples set
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |         HS{ } clone new-words set
 | 
					
						
							| 
									
										
										
										
											2010-02-03 09:20:48 -05:00
										 |  |  |         add-nesting-observer | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             remove-nesting-observer | 
					
						
							|  |  |  |             finish-compilation-unit | 
					
						
							|  |  |  |         ] [ ] cleanup
 | 
					
						
							| 
									
										
										
										
											2008-05-28 20:34:18 -04:00
										 |  |  |     ] with-scope ; inline
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-compilation-unit ( quot -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         <definitions> new-definitions set
 | 
					
						
							|  |  |  |         <definitions> old-definitions set
 | 
					
						
							| 
									
										
										
										
											2013-03-10 19:12:40 -04:00
										 |  |  |         HS{ } clone forgotten-definitions set
 | 
					
						
							| 
									
										
										
										
											2010-02-03 09:20:48 -05:00
										 |  |  |         with-nested-compilation-unit | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  |     ] with-scope ; inline
 |