Inlined several words for size.

This commit is contained in:
Brad Nelson
2022-02-04 18:26:36 -08:00
parent 02c459297b
commit 14642db347
13 changed files with 130 additions and 42 deletions

View File

@ -284,6 +284,7 @@ $(POSIX):
$(POSIX)/ueforth: \ $(POSIX)/ueforth: \
posix/main.c \ posix/main.c \
common/opcodes.h \ common/opcodes.h \
common/extra_opcodes.h \
common/calls.h \ common/calls.h \
common/calling.h \ common/calling.h \
common/floats.h \ common/floats.h \
@ -306,6 +307,7 @@ $(WINDOWS):
$(WINDOWS)/uEf32.obj: \ $(WINDOWS)/uEf32.obj: \
windows/main.c \ windows/main.c \
common/opcodes.h \ common/opcodes.h \
common/extra_opcodes.h \
common/calls.h \ common/calls.h \
common/calling.h \ common/calling.h \
common/floats.h \ common/floats.h \
@ -322,6 +324,7 @@ $(WINDOWS)/uEf32.exe: \
$(WINDOWS)/uEf64.obj: \ $(WINDOWS)/uEf64.obj: \
windows/main.c \ windows/main.c \
common/opcodes.h \ common/opcodes.h \
common/extra_opcodes.h \
common/calls.h \ common/calls.h \
common/calling.h \ common/calling.h \
common/floats.h \ common/floats.h \
@ -354,6 +357,7 @@ $(ESP32_SIM)/Esp32forth-sim: \
esp32/sim_main.cpp \ esp32/sim_main.cpp \
esp32/main.cpp \ esp32/main.cpp \
common/opcodes.h \ common/opcodes.h \
common/extra_opcodes.h \
common/floats.h \ common/floats.h \
common/calling.h \ common/calling.h \
common/floats.h \ common/floats.h \
@ -378,6 +382,7 @@ $(ESP32)/ESP32forth:
ESP32_PARTS = common/replace.js \ ESP32_PARTS = common/replace.js \
esp32/template.ino \ esp32/template.ino \
common/opcodes.h \ common/opcodes.h \
common/extra_opcodes.h \
common/floats.h \ common/floats.h \
common/calling.h \ common/calling.h \
common/core.h \ common/core.h \
@ -395,6 +400,7 @@ $(ESP32)/ESP32forth/ESP32forth.ino: $(ESP32_PARTS) | $(ESP32)/ESP32forth
REVISION=$(REVISION) \ REVISION=$(REVISION) \
config=@esp32/config.h \ config=@esp32/config.h \
opcodes=@common/opcodes.h \ opcodes=@common/opcodes.h \
extra_opcodes=@common/extra_opcodes.h \
calling=@common/calling.h \ calling=@common/calling.h \
floats=@common/floats.h \ floats=@common/floats.h \
core=@common/core.h \ core=@common/core.h \

View File

@ -1,6 +1,3 @@
: ( 41 parse drop drop ; immediate
: \ 10 parse drop drop ; immediate
\ Copyright 2021 Bradley D. Nelson \ Copyright 2021 Bradley D. Nelson
\ \
\ Licensed under the Apache License, Version 2.0 (the "License"); \ Licensed under the Apache License, Version 2.0 (the "License");
@ -15,42 +12,9 @@
\ See the License for the specific language governing permissions and \ See the License for the specific language governing permissions and
\ limitations under the License. \ limitations under the License.
( Useful Basic Compound Words ) : ( 41 parse drop drop ; immediate
: nip ( a b -- b ) swap drop ; : \ 10 parse drop drop ; immediate
: rdrop ( r: n n -- ) r> r> drop >r ; ( Now can do comments! )
: */ ( n n n -- n ) */mod nip ;
: * ( n n -- n ) 1 */ ;
: /mod ( n n -- n n ) 1 swap */mod ;
: / ( n n -- n ) /mod nip ;
: mod ( n n -- n ) /mod drop ;
: invert ( n -- ~n ) -1 xor ;
: negate ( n -- -n ) invert 1 + ;
: - ( n n -- n ) negate + ;
: rot ( a b c -- c a b ) >r swap r> swap ;
: -rot ( a b c -- b c a ) swap >r swap r> ;
: < ( a b -- a<b ) - 0< ;
: > ( a b -- a>b ) swap - 0< ;
: <= ( a b -- a>b ) swap - 0< 0= ;
: >= ( a b -- a<b ) - 0< 0= ;
: = ( a b -- a!=b ) - 0= ;
: <> ( a b -- a!=b ) = 0= ;
: 0<> ( n -- n) 0= 0= ;
: bl 32 ; : nl 10 ;
: 1+ 1 + ; : 1- 1 - ;
: 2* 2 * ; : 2/ 2 / ;
: 4* 4 * ; : 4/ 4 / ;
: +! ( n a -- ) swap over @ + swap ! ;
( Cells )
: cell+ ( n -- n ) cell + ;
: cells ( n -- n ) cell * ;
: cell/ ( n -- n ) cell / ;
( Double Words )
: 2drop ( n n -- ) drop drop ;
: 2dup ( a b -- a b a b ) over over ;
: 2@ ( a -- lo hi ) dup @ swap cell+ @ ;
: 2! ( lo hi a -- ) dup >r cell+ ! r> ! ;
( Dictionary ) ( Dictionary )
: here ( -- a ) 'sys @ ; : here ( -- a ) 'sys @ ;

50
ueforth/common/extra.fs Normal file
View File

@ -0,0 +1,50 @@
\ Copyright 2022 Bradley D. Nelson
\
\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at
\
\ http://www.apache.org/licenses/LICENSE-2.0
\
\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.
( Useful Basic Compound Words )
: nip ( a b -- b ) swap drop ;
: rdrop ( r: n n -- ) r> r> drop >r ;
: */ ( n n n -- n ) */mod nip ;
: * ( n n -- n ) 1 */ ;
: /mod ( n n -- n n ) 1 swap */mod ;
: / ( n n -- n ) /mod nip ;
: mod ( n n -- n ) /mod drop ;
: invert ( n -- ~n ) -1 xor ;
: negate ( n -- -n ) invert 1 + ;
: - ( n n -- n ) negate + ;
: rot ( a b c -- c a b ) >r swap r> swap ;
: -rot ( a b c -- b c a ) swap >r swap r> ;
: < ( a b -- a<b ) - 0< ;
: > ( a b -- a>b ) swap - 0< ;
: <= ( a b -- a>b ) swap - 0< 0= ;
: >= ( a b -- a<b ) - 0< 0= ;
: = ( a b -- a!=b ) - 0= ;
: <> ( a b -- a!=b ) = 0= ;
: 0<> ( n -- n) 0= 0= ;
: bl 32 ; : nl 10 ;
: 1+ 1 + ; : 1- 1 - ;
: 2* 2 * ; : 2/ 2 / ;
: 4* 4 * ; : 4/ 4 / ;
: +! ( n a -- ) swap over @ + swap ! ;
( Cells )
: cell+ ( n -- n ) cell + ;
: cells ( n -- n ) cell * ;
: cell/ ( n -- n ) cell / ;
( Double Words )
: 2drop ( n n -- ) drop drop ;
: 2dup ( a b -- a b a b ) over over ;
: 2@ ( a -- lo hi ) dup @ swap cell+ @ ;
: 2! ( lo hi a -- ) dup >r cell+ ! r> ! ;

View File

@ -0,0 +1,51 @@
// Copyright 2021 Bradley D. Nelson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define EXTRA_OPCODE_LIST \
Y(nip, NIP) \
Y(rdrop, --rp) \
X("*/", STARSLASH, SSMOD_FUNC; NIP) \
X("*", STAR, tos *= *sp--) \
X("/mod", SLASHMOD, DUP; *sp = 1; SSMOD_FUNC) \
X("/", SLASH, DUP; *sp = 1; SSMOD_FUNC; NIP) \
Y(mod, DUP; *sp = 1; SSMOD_FUNC; DROP) \
Y(invert, tos = ~tos) \
Y(negate, tos = -tos) \
X("-", MINUS, tos = (*sp--) - tos) \
Y(rot, w = sp[-1]; sp[-1] = *sp; *sp = tos; tos = w) \
X("-rot", MROT, w = tos; tos = *sp; *sp = sp[-1]; sp[-1] = w) \
X("<", LESS, tos = (*sp--) < tos ? -1 : 0) \
X(">", GREATER, tos = (*sp--) > tos ? -1 : 0) \
X("<=", LESSEQ, tos = (*sp--) <= tos ? -1 : 0) \
X(">=", GREATEREQ, tos = (*sp--) >= tos ? -1 : 0) \
X("=", EQUAL, tos = (*sp--) == tos ? -1 : 0) \
X("<>", NOTEQUAL, tos = (*sp--) != tos ? -1 : 0) \
X("0<>", ZNOTEQUAL, tos = tos ? -1 : 0) \
Y(bl, DUP; tos = ' ') \
Y(nl, DUP; tos = '\n') \
X("1+", ONEPLUS, ++tos) \
X("1-", ONEMINUS, --tos) \
X("2*", TWOSTAR, tos <<= 1) \
X("2/", TWOSLASH, tos >>= 1) \
X("4*", FOURSTAR, tos <<= 2) \
X("4/", FOURSLASH, tos >>= 2) \
X("+!", PLUSSTORE, *(cell_t *) tos += *sp--; DROP) \
X("cell+", CELLPLUS, tos += sizeof(cell_t)) \
X("cells", CELLSTAR, tos *= sizeof(cell_t)) \
X("cell/", CELLSLASH, DUP; tos = sizeof(cell_t); DUP; *sp = 1; SSMOD_FUNC; NIP) \
X("2drop", TWODROP, DROP; DROP) \
X("2dup", TWODUP, DUP; tos = sp[-1]; DUP; tos = sp[-1]) \
X("2@", TWOAT, DUP; *sp = ((cell_t *) tos)[1]; tos = *(cell_t *) tos) \
X("2!", TWOSTORE, DUP; ((cell_t *) tos)[0] = sp[-1]; \
((cell_t *) tos)[1] = *sp; DROP; DROP; DROP)

View File

@ -170,6 +170,11 @@ e: check-boot
out: aligned out: aligned
out: allot out: allot
out: here out: here
out: \
out: (
;e
e: check-extra-opcodes
out: 2! out: 2!
out: 2@ out: 2@
out: 2dup out: 2dup
@ -205,8 +210,6 @@ e: check-boot
out: */ out: */
out: rdrop out: rdrop
out: nip out: nip
out: \
out: (
;e ;e
e: check-core-opcodes e: check-core-opcodes
@ -397,6 +400,7 @@ e: check-phase1
check-[]conds check-[]conds
check-boot check-boot
check-core-opcodes check-core-opcodes
check-extra-opcodes
check-float-opcodes check-float-opcodes
;e ;e

View File

@ -22,6 +22,7 @@ static cell_t *forth_run(cell_t *init_rp) {
if (!init_rp) { if (!init_rp) {
#define X(name, op, code) create(name, sizeof(name) - 1, name[0] == ';', && OP_ ## op); #define X(name, op, code) create(name, sizeof(name) - 1, name[0] == ';', && OP_ ## op);
PLATFORM_OPCODE_LIST PLATFORM_OPCODE_LIST
EXTRA_OPCODE_LIST
OPCODE_LIST OPCODE_LIST
#undef X #undef X
return 0; return 0;
@ -31,6 +32,7 @@ static cell_t *forth_run(cell_t *init_rp) {
rp = init_rp; UNPARK; NEXT; rp = init_rp; UNPARK; NEXT;
#define X(name, op, code) OP_ ## op: { code; } NEXT; #define X(name, op, code) OP_ ## op: { code; } NEXT;
PLATFORM_OPCODE_LIST PLATFORM_OPCODE_LIST
EXTRA_OPCODE_LIST
OPCODE_LIST OPCODE_LIST
#undef X #undef X
OP_DOCOLON: ++rp; *rp = (cell_t) ip; ip = (cell_t *) (w + sizeof(cell_t)); NEXT; OP_DOCOLON: ++rp; *rp = (cell_t) ip; ip = (cell_t *) (w + sizeof(cell_t)); NEXT;

View File

@ -31,7 +31,7 @@ internals definitions
for aft 2dup c@ swap c@ <> if 2drop rdrop 0 exit then 1+ swap 1+ then next 2drop -1 ; for aft 2dup c@ swap c@ <> if 2drop rdrop 0 exit then 1+ swap 1+ then next 2drop -1 ;
forth definitions also internals forth definitions also internals
: :noname ( -- xt ) 0 , current @ @ , NONAMED SMUDGE or , : :noname ( -- xt ) 0 , current @ @ , NONAMED SMUDGE or ,
here dup current @ ! ['] = @ , postpone ] ; here dup current @ ! ['] mem= @ , postpone ] ;
: str= ( a n a n -- f) >r swap r@ <> if rdrop 2drop 0 exit then r> mem= ; : str= ( a n a n -- f) >r swap r@ <> if rdrop 2drop 0 exit then r> mem= ;
: startswith? ( a n a n -- f ) >r swap r@ < if rdrop 2drop 0 exit then r> mem= ; : startswith? ( a n a n -- f ) >r swap r@ < if rdrop 2drop 0 exit then r> mem= ;
: .s ." <" depth n. ." > " raw.s cr ; : .s ." <" depth n. ." > " raw.s cr ;

View File

@ -17,6 +17,7 @@
#include "esp32/options.h" #include "esp32/options.h"
#include "common/opcodes.h" #include "common/opcodes.h"
#include "common/extra_opcodes.h"
#include "common/floats.h" #include "common/floats.h"
#include "common/calling.h" #include "common/calling.h"

View File

@ -22,6 +22,7 @@
{{config}} {{config}}
{{options}} {{options}}
{{opcodes}} {{opcodes}}
{{extra_opcodes}}
{{floats}} {{floats}}
{{calling}} {{calling}}
{{builtins.h}} {{builtins.h}}

View File

@ -16,6 +16,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "common/opcodes.h" #include "common/opcodes.h"
#include "common/extra_opcodes.h"
#include "common/floats.h" #include "common/floats.h"
#include "common/calling.h" #include "common/calling.h"
#include "common/calls.h" #include "common/calls.h"

View File

@ -93,6 +93,10 @@ EXECUTE BRANCH 0BRANCH DONEXT DOLIT
ALITERAL CELL DOES&gt; IMMEDIATE 'SYS ALITERAL CELL DOES&gt; IMMEDIATE 'SYS
</pre> </pre>
<p><b>NOTE: Later to reduce the use of the RAM heap and improve performance,
additional non-essential extra opcodes were added in place of high-level
words.</b></p>
<p> <p>
See <a href="https://github.com/flagxor/eforth/blob/main/ueforth/common/opcodes.h">opcodes.h</a>. See <a href="https://github.com/flagxor/eforth/blob/main/ueforth/common/opcodes.h">opcodes.h</a>.
</p> </p>

View File

@ -24,6 +24,7 @@ enum {
OP_DODOES, OP_DODOES,
#define X(name, op, code) OP_ ## op, #define X(name, op, code) OP_ ## op,
PLATFORM_OPCODE_LIST PLATFORM_OPCODE_LIST
EXTRA_OPCODE_LIST
OPCODE_LIST OPCODE_LIST
#undef X #undef X
}; };
@ -33,6 +34,7 @@ static cell_t *forth_run(cell_t *init_rp) {
#define X(name, op, code) \ #define X(name, op, code) \
create(name, sizeof(name) - 1, name[0] == ';', (void *) OP_ ## op); create(name, sizeof(name) - 1, name[0] == ';', (void *) OP_ ## op);
PLATFORM_OPCODE_LIST PLATFORM_OPCODE_LIST
EXTRA_OPCODE_LIST
OPCODE_LIST OPCODE_LIST
#undef X #undef X
return 0; return 0;
@ -47,6 +49,7 @@ work:
switch (*(cell_t *) w & 0xff) { switch (*(cell_t *) w & 0xff) {
#define X(name, op, code) case OP_ ## op: { code; } NEXT; #define X(name, op, code) case OP_ ## op: { code; } NEXT;
PLATFORM_OPCODE_LIST PLATFORM_OPCODE_LIST
EXTRA_OPCODE_LIST
OPCODE_LIST OPCODE_LIST
#undef X #undef X
case OP_DOCOLON: ++rp; *rp = (cell_t) ip; ip = (cell_t *) (w + sizeof(cell_t)); NEXT; case OP_DOCOLON: ++rp; *rp = (cell_t) ip; ip = (cell_t *) (w + sizeof(cell_t)); NEXT;

View File

@ -28,6 +28,7 @@
#endif #endif
#include "common/opcodes.h" #include "common/opcodes.h"
#include "common/extra_opcodes.h"
#include "common/floats.h" #include "common/floats.h"
#include "common/calling.h" #include "common/calling.h"
#include "common/calls.h" #include "common/calls.h"