1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; UrForth level
1: self
-hosting
32-bit Forth compiler
3 ;; Copyright
(C
) 2020 Ketmar Dark
// Invisible Vector
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 UROPT_DEBUG_ALIAS equ
0
8 UROPT_DEBUG_BROPT equ
0
11 voc
-set
-active OPTIMISER
14 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 ;; replace alias
call with referred address
16 : optimise
-alias
-call ( cfa
-- true
// cfa false
)
19 dup c@
0xe9 = ifnot false exit
endif
20 \ endcr
." ALIAS?: " dup cfa
-wsize
. cr
21 ;; should be exactly
5/8 bytes
22 dup cfa
-wsize
(#cfa
) = ifnot false exit
endif
24 endcr
." ALIAS: " dup cfa
->nfa id
. ." -> " dup
1+ compiler
:(disp32@
) cfa
->nfa id
. cr
26 ;; get new cfa address
, and check
if it is an alias too
32 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; branch stack contands addresses of branch instructions
37 #jstack cells buffer
: jstack
42 else dup #jstack
< if dup
1+ to jsp jstack cells^
!
43 else 2drop
-1 to jsp
;; overflow
44 endcr
." WARNING: branch optimiser stack overflow in \`" latest
-nfa id
. ." \`\n"
50 else dup
to jsp jstack cells^ @
endif
53 : jpush
-branch
( -- ) here
[execute
-tail
] jpush
;
55 : jstack
-reset
( -- ) -1 to jsp
;
56 : jstack
-init
( -- ) forth
:(opt
-branches?
) if 0 else -1 endif to jsp
;
57 : jstack
-subinit
( -- ) forth
:(opt
-branches?
) if jsp
0 max
to jsp
0 jpush
endif ;
59 : jstack
-pop
-frame
( -- )
60 jsp
+if begin jpop not
-until
else jstack
-reset
endif
64 ;; type
: 0 is unconditional branch
; 1 is conditional branch
65 ;; cfa
: ( addr type
-- stopcode
)
66 : jstack
-foreach
( cfa
-- stopcode
)
67 jsp
1- dup
-if drop false
70 jstack i
+cells @ ?dup ifnot
break endif
71 dup @
['] branch <> rot dup >r execute
72 ?dup if rdrop unloop exit endif
73 r> -1 +loop drop false
78 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 false value (was-jump-fix)
82 false value (optimise-jumps-again)
84 : optimise-jumps ( -- )
87 false to (optimise-jumps-again)
88 [: ( addr type -- stopflag )
91 false to (was-jump-fix)
92 \ endcr ." ***[ " latest-nfa id. ." ]***\n"
94 [: ( addr braddr type -- addr stopflag )
95 >r 2dup cell+ @ = if ;; braddr jumps to addr, reroute it
97 (was-jump-fix) ifnot endcr ." === processing JUMP at 0x" over .hex8 ." <" latest-nfa id. ." > ===\n" true to (was-jump-fix) endif
98 endcr ." fixing " r@ if ." X" else space endif ." BRN at 0x" dup .hex8 cr
100 cell+ over cell+ @ 2dup swap @ <> if ( addr braddr+4 [addr+4] )
101 swap ! true to (optimise-jumps-again)
105 ;] jstack-foreach 2drop
108 ;] jstack-foreach drop
109 (optimise-jumps-again) not-until
111 else jstack-reset endif