1 \
FLK primitive
optimizer
3 \
Copyright (C) 1998 Lars Krueger
5 \
This file is
part of FLK.
7 \
FLK is
free software
; you
can redistribute
it and/or
8 \ modify
it under the
terms of the
GNU General Public License
9 \
as published by the
Free Software Foundation; either version
2
10 \
of the
License, or (at
your option) any later version
.
12 \
This program is
distributed in the
hope that
it will
be useful
,
13 \ but
WITHOUT ANY WARRANTY; without
even the
implied warranty
of
14 \
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 \
GNU General Public License for more
details.
17 \
You should have
received a copy
of the GNU General Public License
18 \ along
with this
program; if not
, write
to the Free Software
19 \
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 \ $
Id: flkprim
.fs
,v
1.20 1998/08/30 10:50:59 root
Exp $
22 \ $
Log: flkprim
.fs
,v $
23 \
Revision 1.20 1998/08/30 10:50:59 root
24 \
new optimizing algorithm
26 \
Revision 1.19 1998/07/13 18:08:54 root
27 \ various
optimizations
29 \
Revision 1.18 1998/07/03 20:57:50 root
30 \ level
2 optimimizer added
32 \
Revision 1.17 1998/07/03 09:09:28 root
33 \ support
for level
2 optimizer
35 \
Revision 1.16 1998/06/08 22:14:51 root
36 \ literals
cache (preparation
to level
2 optimizer)
38 \
Revision 1.15 1998/06/01 17:51:42 root
39 \
SEE shows
the sourcefile
using VIEW
41 \
Revision 1.14 1998/05/27 18:52:12 root
42 \ \
: commants
added for SEE and HELP
44 \
Revision 1.13 1998/05/16 16:19:24 root
45 \ direct
terminfo access
47 \
Revision 1.12 1998/05/09 21:47:05 root
50 \
Revision 1.11 1998/05/03 12:06:37 root
53 \
Revision 1.10 1998/05/02 14:27:58 root
54 \ compile
only primitives
56 \
Revision 1.9 1998/05/01 18:11:25 root
57 \
GNU license
text added
60 \
Revision 1.8 1998/04/29 18:20:30 root
63 \
Revision 1.7 1998/04/27 18:41:42 root
64 \ exchange
primitive added
66 \
Revision 1.6 1998/04/25 11:02:07 root
67 \
* fixed
(crrect
now and faster
)
69 \
Revision 1.5 1998/04/24 16:47:39 root
72 \
Revision 1.4 1998/04/10 14:42:50 root
75 \
Revision 1.3 1998/04/09 11:35:03 root
76 \ primitives
added and checked, all
OK
78 \
Revision 1.2 1998/04/09 09:18:11 root
79 \ primives
checked, roll
corrected
81 \
Revision 1.1 1998/04/07 20:10:33 root
93 #tos-cache #USEREGS = IF
113 \
Store FALSE at
the given address
.
122 \
Store TRUE at
the given address
.
139 p
: CHAR+ ( addr
-- addr
+char
)
145 \
Decrease the given address
by the size of a character
(1 byte
).
146 p
: CHAR- ( addr
-- addr
-char
)
204 p
: * ( t1 t0
-- t1
*t0
)
279 tos2
1 CELLS [tos0
] mov,
290 1 CELLS [tos0
] free1
mov,
296 p
: 2OVER ( n1 n2 n3 n4
--- n1 n2 n3 n4 n1 n2
)
299 req
-any req
-any req
-any req
-any
307 p
: 2SWAP ( n1 n2 n3 n4
--- n3 n4 n1
2n )
310 req-any
req-any
req-any
req-any
315 p
: 2DUP ( n1 n2
-- n1 n2 n1 n2
)
325 \
Duplicate the top
3 values
on the stack.
326 p
: 3DUP ( t2 t1 t0
-- t2 t1 t0 f2 f1 f0
)
329 req-any
req-any
req-any
330 req-free req-free req-free
357 0 1 tos
-swap
1 reg
-free ;
360 p
: 2DROP ( n1 n2
-- )
376 p
: OVER ( n1 n2
-- n1 n2 n1
)
386 p
: ROT ( n1 n2 n3
--- n2 n3 n1
)
389 req-any
req-any
req-any
390 0 1 tos
-swap \ t2 t0 t1
391 0 2 tos
-swap
; \ t1 t0 t2
393 \
Put the the top of stack value
below the two next
values. Inverse operation
395 p
: -ROT ( n1 n2 n3
--- n3 n1 n2
)
400 0 1 tos
-swap \ t2 t0 t1
401 1 2 tos
-swap
; \ t0 t2 t1
403 \
Rotate the top four items
upwards.
404 p
: TURN ( t3 t2 t1 t0
-- t2 t1 t0 t3
)
407 req-any
req-any
req-any
req-any
408 0 1 tos
-swap \ t3 t2 t0 t1
409 0 3 tos
-swap \ t1 t2 t0 t3
410 2 3 tos
-swap
; \ t2 t1 t0 t3
412 \
Rotate the top four items
downwards.
413 p
: -TURN ( t3 t2 t1 t0
-- t0 t3 t2 t1
)
417 req-any
req-any \ t3 t2 t1 t0
418 0 1 tos
-swap \ t3 t2 t0 t1
419 1 2 tos
-swap \ t3 t0 t2 t1
420 2 3 tos
-swap \ t0 t3 t2 t1
423 \
Rotate the top five
items upwards.
424 p
: TWIST ( t4 t3 t2 t1 t0
-- t3 t2 t1 t0 t4
)
429 req-any \ t4 t3 t2 t1 t0
430 0 4 tos
-swap \ t0 t3 t2 t1 t4
431 4 3 tos
-swap \ t3 t0 t2 t1 t4
432 3 2 tos
-swap \ t3 t2 t0 t1 t4
433 2 1 tos
-swap \ t3 t2 t1 t0 t4
436 \
Rotate the top five
items downwards.
437 p
: -TWIST ( t4 t3 t2 t1 t0
-- t0 t4 t3 t2 t1
)
442 req-any \ t4 t3 t2 t1 t0
443 0 1 tos
-swap \ t4 t3 t2 t0 t1
444 1 2 tos
-swap \ t4 t3 t0 t2 t1
445 2 3 tos
-swap \ t4 t0 t3 t2 t1
446 3 4 tos
-swap \ t0 t4 t3 t2 t1
449 \
Rotate the top six
items upwards.
450 p
: ROTARE ( t5 t4 t3 t2 t1 t0
-- t4 t3 t2 t1 t0 t5
)
454 req-any
req-any \ t5 t4 t3 t2 t1 t0
455 5 4 tos
-swap \ t4 t5 t3 t2 t1 t0
456 4 3 tos
-swap \ t4 t3 t5 t2 t1 t0
457 3 2 tos
-swap \ t4 t3 t2 t5 t1 t0
458 2 1 tos
-swap \ t4 t3 t2 t1 t5 t0
459 1 0 tos
-swap \ t4 t3 t2 t1 t0 t5
462 \
Rotate the top six
items downwards.
463 p
: -ROTARE ( t5 t4 t3 t2 t1 t0
-- t0 t5 t4 t3 t2 t1
)
467 req-any
req-any \ t5 t4 t3 t2 t1 t0
468 0 1 tos
-swap \ t5 t4 t3 t2 t0 t1
469 1 2 tos
-swap \ t5 t4 t3 t0 t2 t1
470 2 3 tos
-swap \ t5 t4 t0 t3 t2 t1
471 3 4 tos
-swap \ t5 t0 t4 t3 t2 t1
472 4 5 tos
-swap \ t0 t5 t4 t3 t2 t1
476 p
: TUCK ( t1 t0
-- t0 t1 t0
)
483 0 free>tos \ t0 t1 f0
486 \
Copy the third stack item
on top.
487 p
: PLUCK ( t2 t1 t0
-- t2 t1 t0 t2
)
490 req-any
req-any
req-any
495 \
Copy the fourth stack item
on top.
496 p
: FLOCK ( t3 t2 t1 t0
-- t3 t2 t1 t0 t3
)
499 req-any
req-any
req-any
req-any
585 p
: LSHIFT ( x1 u
-- x2
)
594 p
: RSHIFT ( x1 u
-- x2
)
684 \
Perform a less
or equal
comparison. Equivalent to > INVERT
712 \
Perform a greater
or equal
comparison. Equivalent to < INVERT
751 p
: 2ROT ( x1 x2 x3 x4 x5 x6
-- x3 x4 x5 x6 x1 x2
)
754 req-any
req-any
req-any
755 req-any
req-any
req-any \ t5 t4 t3 t2 t1 t0
756 0 4 tos
-swap \ t5 t0 t3 t2 t1 t4
757 1 5 tos
-swap \ t1 t0 t3 t2 t5 t4
758 2 4 tos
-swap \ t1 t2 t3 t0 t5 t4
759 3 5 tos
-swap
; \ t3 t2 t1 t0 t5 t4
781 p
: FILL ( addr cnt
char -- )
791 p
: D+ ( d1 d2
-- d1
+d2
)
794 req-any
req-any
req-any
req-any
800 p
: D- ( d1 d2
-- d1
-d2
)
803 req-any
req-any
req-any
req-any
818 p
: D2/ ( d1l
d1h -- d2l
d2h )
821 req-any
req-any
req-free
828 p
: D0< ( dl
dh -- flag
)
837 p
: DNEGATE ( dl
dh -- d1l
d1h )
846 p
: CMOVE ( a1 a2 cnt
-- )
856 p
: CMOVE> ( a1 a2 cnt
-- )
880 p
: UM* ( u1 u2
-- ud
)
888 p
: UM/MOD ( ud un
-- ur uq
)
892 req-edx \ udh
=edx
=tos1
=rem
893 req-eax \ udl
=eax
=tos2
=quot
899 p
: SM/REM ( d1l
d1h n1
-- nrem nquot
)
914 offs
-ebp
## free0 add,
926 \
Retrieve the current
return stack pointer
.
934 \
Set the return stack pointer
. Attention: A wrong value
does not
lead to a
935 \ segmentation fault
immediate, but
at the next call
or return.
944 p
: DU< ( d1l
d1h d2l
d2h -- flag
)
957 p
: PICK ( n
-- tos
+n
)
965 offs
-ebp
[tos0
] tos0
mov, ;
967 \
Return the base address
of the data stack. This is the highest accessable
968 \ address
plus one
cell since
the stack grows
downwards.
969 p
: SP-BASE ( -- sp
-base)
973 HA-INIT-DATASTACK #[] free0 mov,
976 \
Return the base address
of the return stack. This is the highest accessable
977 \ address
plus one
cell since
the stack grows
downwards.
978 p
: RP-BASE ( -- rp
-base)
982 HA-INIT-CALLSTACK #[] free0 mov,
986 p
: ROLL ( xu
xu-1 ... x0 u
-- xu-1 ... x0
xu )
992 2 ## tos0 shl, \ tos0=4*u
1006 p
: FORTH-WORDLIST ( -- wid
)
1011 HA-DEF-WL ## free0 mov,
1014 \
Return the wordlist identifier
for the environment wordlist.
1015 p
: ENVIRONMENT-WORDLIST ( -- wid
)
1020 HA-ENV-WL ## free0 mov,
1023 \
Return the wordlist identifier
for the ASSEMBLER wordlist.
1024 p
: ASSEMBLER-WORDLIST ( -- wid
)
1029 HA-ASS-WL ## free0 mov,
1032 \
Return the wordlist identifier
for the EDITOR wordlist.
1033 p
: EDITOR-WORDLIST ( -- wid
)
1038 HA-EDT-WL ## free0 mov,
1041 \
Return the last possible
address in the data area
.
1042 p
: HERE-LIMIT ( -- here
-limit
)
1046 HA-HERE-LIMIT #[] free0 mov,
1049 \
Return the last possible
address in the code area
.
1050 p
: CHERE-LIMIT ( -- here
-limit
)
1054 HA-CHERE-LIMIT #[] free0 mov,
1057 \
Return inital
value of HERE.
1058 p
: HERE-INIT ( -- here
-limit
)
1062 HA-HERE-INIT #[] free0 mov,
1065 \
Return inital
value of CHERE.
1066 p
: CHERE-INIT ( -- here
-limit
)
1070 HA-CHERE-INIT #[] free0 mov,
1074 p
: COUNT ( c
-addr1
-- c
-addr2 u
)
1080 0 [tos0
] free0l
mov,
1084 \
Primitive for EXECUTE. Jumps to the given address.
1085 p
: (EXECUTE) ( addr
-- )
1094 p
: CELLS ( x
-- x
*4 )
1101 p
: CELL+ ( x
-- x
+4)
1107 \
Decrease the given address by one
cell (4 bytes
).
1108 p
: CELL- ( x
-- x
-4)
1114 \
Get the relocation table
.
1115 p
: RELOCATION-TABLE@ ( -- reltab
)
1119 HA-RELTABLE #[] free0 mov,
1122 \
Store the relocation table
.
1123 p
: RELOCATION-TABLE! ( reltab
-- )
1127 tos0
HA-RELTABLE #[] mov,
1131 p
: / ( n1 n2
-- n3
)
1134 req-edx \ n2
=tos0
=edx
1135 req-eax \ n1
=tos1
=eax
1140 free0 idiv
, \ eax
=quot
edx=rem
1144 p
: MOD ( n1 n2
-- n3
)
1147 req-edx \ n2
=tos0
=edx
1148 req-eax \ n1
=tos1
=eax
1153 free0 idiv
, \ eax
=quot
edx=rem
1158 p
: M+ ( dl
dh n
-- dl
dh )
1168 \
Return TRUE if the two numbers
differ in their sign
.
1169 p
: SignsDiffer? ( n1 n2
-- flag
)
1178 \
Exchange the number
and the content of the address.
1179 p
: exchange
( x1 addr
-- x2
)
1187 p
: ALIGNED ( addr
-- addr2
)
1192 3 INVERT ## tos0 and, ;
1195 p
: MAX ( n1 n2
-- n3
)
1206 p
: MIN ( n1 n2
-- n3
)
1226 \
Same as COUNT, but
for cell counted strings
.
1227 p
: $
COUNT ( c
-addr1
-- c
-addr2 u
)
1235 \
Put a 0 onto
the stack. Generates smaller code that the normal inline
1243 \
Advance to the cell containing
the address of the interpretation semantics
of
1245 p
: >CFA ( xt
-- cfa
)
1250 \
Advance to the cell containing
the address of the optimization semantics
of
1252 p
: >OCFA ( xt
-- ocfa
)
1257 \
Advance to the cell containing
the address of the data field
of the word.
1258 p
: >DFA ( xt
-- dfa
)
1263 \
Advance to the cell containing
the address of the filename of the word.
1264 p
: >FN ( xt
-- filename )
1269 \
Advance to the cell containing
the line number
of the word.
1270 p
: >DL ( xt
-- definition
-line )
1275 \
Advance to the byte containing
the flags of the word.
1276 p
: >FLAGS ( xt
-- ffa
)
1281 \
Advance to the byte containing
the byte counted string containing
the name
1283 p
: >NAME ( xt
-- nfa
)
1288 \
Convert name to a printable
string (same
as COUNT for now)
1289 p
: NAME>STRING ( nfa
-- c
-addr u
)
1295 0 [tos0
] free0l
mov,