Path: utzoo!attcan!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: Forth Implementation Message-ID: <644.UUL1.3#5129@willett.UUCP> Date: 12 Mar 90 04:01:16 GMT Organization: Latest link in the ForthNet chain. (Pgh, PA) Lines: 69 Category 3, Topic 24 Message 53 Sun Mar 11, 1990 F.SERGEANT [Frank] at 10:40 CST Re: ?FOR , a version of FOR that executes zero times and other tinkerings To Robert Berkey Thanks for the tip on using the overflow flag in NEXT. I guess you lay down a 1-byte branch except when ?FOR & NEXT are far enough apart to require a 2-byte branch. I've got to consider that for all my branches. I hate the added complexity for the compiler (& for SEE). This week I changed from FOR to ?FOR. Here's my new code. I've built the branch in as part of 'for' so 'for' costs 4 bytes for each ?FOR NEXT loop. ( runtime FOR - keeps only count on Rstk) CODE for SWITCH, ( point to return stack) BX PUSH, ( save loop count on R stk) SWITCH, ( point back to data stack) BX POP, ( refill TOS ) 0 [SI] SI MOV, ( branch to next to skip loop 1st time) NXT, ( in-line threading next - as opposed to NEXT next) END-CODE This allowed me to eliminate '1-' when it appeared before the FOR. It also allowed me to eliminate the ?DUP IF 1- ..... THEN that often surrounded FOR ... NEXT. I went thru my applications making those changes, hoping for a net savings in bytes. I was surprised that there was a net increase in program size, but not much. EVERY FOR NEXT loop was 2 bytes longer because of the branch built into 'for' while the 2 byte savings for dropping '1-' and the 8 byte savings for dropping '?DUP IF 1-' only happened some of the time. In addition, a few times an extra '1+' was needed. Thus I consider this only a border-line success. I think I'm ahead due to the improved clarity of dropping the '?DUP IF 1-'. So, I've paid a small object code price for an improvement in source code clarity. It is probably worth it to me. There is something nice about 7 FOR ... NEXT looping 7 times rather than 8 and I've always liked the idea of 0 FOR ... NEXT doing it zero times! But, the above trade off is not my favorite kind. I much prefer the kind the source code gets simpler & clearer and the object code gets smaller and faster. Continuing to tinker, I added 'N!' ( u a - u) which stores u into a and keeps u on the stack. A quick search of my applications showed about 28 places it could be used in place of '!' and eliminate 'DUP'. I added '+UNDER' ( a b c - a+c b) which replaced 'SWAP n + SWAP' with 'n +UNDER'. I factored the 'IF' into 'ABORT"'. I added 'NIP'. These changes did reduce the object code size for my applications. But, not by enough for me to be sure it was worth having more words in the nucleus. I am about to go thru and look for places to use COUNT to walk forward thru a sequence of byte values (instead of DUP 1+ SWAP C@), a practice recently decried by Mitch Bradley (I think it was). I've thought of defining run-time begin if while else then repeat until again to improve the appearance of the SEE decompilation. For now I don't want to pay the price. They might go well in a special version designed for teaching Forth. -- Frank ----- This message came from GEnie via willett through a semi-automated process. Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'