| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  | USING: prettyprint compiler.cfg.debugger compiler.cfg.linearization | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | compiler.cfg.predecessors compiler.cfg.stack-analysis | 
					
						
							|  |  |  | compiler.cfg.instructions sequences kernel tools.test accessors | 
					
						
							|  |  |  | sequences.private alien math combinators.private compiler.cfg | 
					
						
							| 
									
										
										
										
											2009-06-26 18:44:33 -04:00
										 |  |  | compiler.cfg.checker compiler.cfg.rpo | 
					
						
							| 
									
										
										
										
											2009-05-28 03:50:07 -04:00
										 |  |  | compiler.cfg.dce compiler.cfg.registers compiler.cfg.useless-blocks | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  | sets namespaces arrays cpu.architecture ;
 | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | IN: compiler.cfg.stack-analysis.tests | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  | ! Fundamental invariant: a basic block should not load or store a value more than once | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | : test-stack-analysis ( quot -- cfg )
 | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  |     dup cfg? [ test-cfg first ] unless
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  |     compute-predecessors | 
					
						
							|  |  |  |     stack-analysis | 
					
						
							| 
									
										
										
										
											2009-06-27 18:32:37 -04:00
										 |  |  |     dup check-cfg ;
 | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | : linearize ( cfg -- mr )
 | 
					
						
							| 
									
										
										
										
											2009-05-31 13:20:46 -04:00
										 |  |  |     flatten-cfg instructions>> ;
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | [ ] [ [ ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Only peek once | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ 1 ] [ [ dup drop dup ] test-stack-analysis linearize [ ##peek? ] count ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Redundant replace is redundant | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ f ] [ [ dup drop ] test-stack-analysis linearize [ ##replace? ] any? ] unit-test | 
					
						
							|  |  |  | [ f ] [ [ swap swap ] test-stack-analysis linearize [ ##replace? ] any? ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Replace required here | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ t ] [ [ dup ] test-stack-analysis linearize [ ##replace? ] any? ] unit-test | 
					
						
							|  |  |  | [ t ] [ [ [ drop 1 ] when ] test-stack-analysis linearize [ ##replace? ] any? ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Only one replace, at the end | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ 1 ] [ [ [ 1 ] [ 2 ] if ] test-stack-analysis linearize [ ##replace? ] count ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Do we support the full language? | 
					
						
							|  |  |  | [ ] [ [ { [ ] [ ] } dispatch ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ { [ ] [ ] } dispatch dup ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ | 
					
						
							|  |  |  |     [ "int" { "int" "int" } "cdecl" [ + ] alien-callback ] | 
					
						
							|  |  |  |     test-cfg second test-stack-analysis drop
 | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Test loops | 
					
						
							|  |  |  | [ ] [ [ [ t ] loop ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ [ dup ] loop ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Make sure that peeks are inserted in the right place | 
					
						
							|  |  |  | [ ] [ [ [ drop 1 ] when ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! This should be a total no-op | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ f ] [ [ [ ] dip ] test-stack-analysis linearize [ ##replace? ] any? ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Don't insert inc-d/inc-r; that's wrong! | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | [ 1 ] [ [ dup ] test-stack-analysis linearize [ ##inc-d? ] count ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-25 20:18:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Bug in height tracking | 
					
						
							|  |  |  | [ ] [ [ dup [ ] [ reverse ] if ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ dup [ ] [ dup reverse drop ] if ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ [ drop dup 4.0 > ] find-last-integer ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Bugs with code that throws | 
					
						
							|  |  |  | [ ] [ [ [ "Oops" throw ] unless ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ [ ] (( -- * )) call-effect-unsafe ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | [ ] [ [ dup [ "Oops" throw ] when dup ] test-stack-analysis drop ] unit-test | 
					
						
							| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  | [ ] [ [ B{ 1 2 3 4 } over [ "Oops" throw ] when swap ] test-stack-analysis drop ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Make sure the replace stores a value with the right height | 
					
						
							|  |  |  | [ ] [ | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  |     [ [ . ] [ 2drop 1 ] if ] test-stack-analysis eliminate-dead-code linearize | 
					
						
							| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  |     [ ##replace? ] filter [ length 1 assert= ] [ first loc>> D 0 assert= ] bi
 | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! translate-loc was the wrong way round | 
					
						
							|  |  |  | [ ] [ | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  |     [ 1 2 rot ] test-stack-analysis eliminate-dead-code linearize | 
					
						
							| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  |     [ [ ##load-immediate? ] count 2 assert= ] | 
					
						
							|  |  |  |     [ [ ##peek? ] count 1 assert= ] | 
					
						
							|  |  |  |     [ [ ##replace? ] count 3 assert= ] | 
					
						
							|  |  |  |     tri
 | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ ] [ | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  |     [ 1 2 ? ] test-stack-analysis eliminate-dead-code linearize | 
					
						
							| 
									
										
										
										
											2009-05-26 03:59:14 -04:00
										 |  |  |     [ [ ##load-immediate? ] count 2 assert= ] | 
					
						
							|  |  |  |     [ [ ##peek? ] count 1 assert= ] | 
					
						
							|  |  |  |     [ [ ##replace? ] count 1 assert= ] | 
					
						
							|  |  |  |     tri
 | 
					
						
							| 
									
										
										
										
											2009-05-26 04:42:39 -04:00
										 |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Sync before a back-edge, not after | 
					
						
							| 
									
										
										
										
											2009-05-27 19:58:01 -04:00
										 |  |  | ! ##peeks should be inserted before a ##loop-entry | 
					
						
							| 
									
										
										
										
											2009-05-31 20:04:26 -04:00
										 |  |  | ! Don't optimize out the constants | 
					
						
							|  |  |  | [ 1 t ] [ | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  |     [ 1000 [ ] times ] test-stack-analysis eliminate-dead-code linearize | 
					
						
							| 
									
										
										
										
											2009-05-31 20:04:26 -04:00
										 |  |  |     [ [ ##add-imm? ] count ] [ [ ##load-immediate? ] any? ] bi
 | 
					
						
							| 
									
										
										
										
											2009-05-27 19:58:01 -04:00
										 |  |  | ] unit-test | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Correct height tracking | 
					
						
							| 
									
										
										
										
											2009-06-26 18:44:33 -04:00
										 |  |  | [ t ] [ | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  |     [ pick [ <array> ] [ drop ] if swap ] test-stack-analysis eliminate-dead-code | 
					
						
							| 
									
										
										
										
											2009-06-27 18:32:37 -04:00
										 |  |  |     reverse-post-order 3 swap nth
 | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  |     instructions>> [ ##peek? ] filter first2 [ loc>> ] [ loc>> ] bi*
 | 
					
						
							| 
									
										
										
										
											2009-06-26 18:44:33 -04:00
										 |  |  |     2array { D 1 D 0 } set= | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ D 1 ] [ | 
					
						
							|  |  |  |     V{ T{ ##branch } } 0 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##peek f V int-regs 0 D 2 } T{ ##branch } } 1 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ | 
					
						
							|  |  |  |         T{ ##peek f V int-regs 1 D 2 } | 
					
						
							|  |  |  |         T{ ##inc-d f -1 } | 
					
						
							|  |  |  |         T{ ##branch } | 
					
						
							|  |  |  |     } 2 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##call f \ + -1 } T{ ##branch } } 3 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##return } } 4 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test-diamond | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cfg new 0 get >>entry | 
					
						
							|  |  |  |     compute-predecessors | 
					
						
							|  |  |  |     stack-analysis | 
					
						
							|  |  |  |     drop
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     3 get instructions>> second loc>> | 
					
						
							| 
									
										
										
										
											2009-06-27 18:32:37 -04:00
										 |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Do inserted ##peeks reference the correct stack location if | 
					
						
							|  |  |  | ! an ##inc-d/r was also inserted? | 
					
						
							|  |  |  | [ D 0 ] [ | 
					
						
							|  |  |  |     V{ T{ ##branch } } 0 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##branch } } 1 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ | 
					
						
							|  |  |  |         T{ ##peek f V int-regs 1 D 0 } | 
					
						
							|  |  |  |         T{ ##branch } | 
					
						
							|  |  |  |     } 2 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ | 
					
						
							|  |  |  |         T{ ##call f \ + -1 } | 
					
						
							|  |  |  |         T{ ##inc-d f 1 } | 
					
						
							|  |  |  |         T{ ##branch } | 
					
						
							|  |  |  |     } 3 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##return } } 4 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test-diamond | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cfg new 0 get >>entry | 
					
						
							|  |  |  |     compute-predecessors | 
					
						
							|  |  |  |     stack-analysis | 
					
						
							|  |  |  |     drop
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 23:52:28 -04:00
										 |  |  |     3 get instructions>> [ ##peek? ] find nip loc>> | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Missing ##replace | 
					
						
							|  |  |  | [ t ] [ | 
					
						
							| 
									
										
										
										
											2009-06-29 17:37:40 -04:00
										 |  |  |     [ [ "B" ] 2dip dup [ [ /mod ] dip ] when ] test-stack-analysis | 
					
						
							| 
									
										
										
										
											2009-06-28 23:52:28 -04:00
										 |  |  |     reverse-post-order last
 | 
					
						
							|  |  |  |     instructions>> [ ##replace? ] filter [ loc>> ] map
 | 
					
						
							|  |  |  |     { D 0 D 1 D 2 } set= | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Inserted ##peeks reference the wrong stack location | 
					
						
							|  |  |  | [ t ] [ | 
					
						
							|  |  |  |     [ [ "B" ] 2dip dup [ [ /mod ] dip ] when ] test-stack-analysis | 
					
						
							|  |  |  |     eliminate-dead-code reverse-post-order 3 swap nth
 | 
					
						
							|  |  |  |     instructions>> [ ##peek? ] filter [ loc>> ] map
 | 
					
						
							| 
									
										
										
										
											2009-06-29 17:37:40 -04:00
										 |  |  |     { R 0 D 0 D 1 } set= | 
					
						
							| 
									
										
										
										
											2009-06-28 23:52:28 -04:00
										 |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ D 0 ] [ | 
					
						
							|  |  |  |     V{ T{ ##branch } } 0 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##branch } } 1 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ | 
					
						
							|  |  |  |         T{ ##peek f V int-regs 1 D 0 } | 
					
						
							|  |  |  |         T{ ##inc-d f 1 } | 
					
						
							|  |  |  |         T{ ##branch } | 
					
						
							|  |  |  |     } 2 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ | 
					
						
							|  |  |  |         T{ ##inc-d f 1 } | 
					
						
							|  |  |  |         T{ ##branch } | 
					
						
							|  |  |  |     } 3 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     V{ T{ ##return } } 4 test-bb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test-diamond | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cfg new 0 get >>entry | 
					
						
							|  |  |  |     compute-predecessors | 
					
						
							|  |  |  |     stack-analysis | 
					
						
							|  |  |  |     drop
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-27 18:32:37 -04:00
										 |  |  |     3 get instructions>> [ ##peek? ] find nip loc>> | 
					
						
							| 
									
										
										
										
											2009-06-26 18:29:55 -04:00
										 |  |  | ] unit-test |