BUFFER:
[ RfDs/CfVs | Other proposals ]
BUFFER:
proposal has a number of flaws:
UHERE
and
UALLOT
(or UALLOCATE
or whatever), instead
of providing a higher-level defining word.DECIMAL
T{ 255 BUFFER: TBUF1 -> }T
T{ 100 BUFFER: TBUF2 -> }T
T{ TBUF2 TBUF1 - ABS 255 > -> <TRUE> }T \ Buffers do not overlap
T{ TBUF1 ALIGNED -> TBUFF1 }T \ Buffer is aligned
: TERASE? ( addr n1 -- n2 )
0 SWAP 0 ?DO
OVER C@ OR SWAP CHAR+ SWAP
LOOP NIP ;
T{ TBUF1 255 ERASE -> }T \ Can buffer be written
T{ TBUF1 255 TERASE? -> 0 }T
24 September 2010, Stephen Pelc
RationaleProblemDespite the wording in the ANS Forth specification, the majority of Forth systems, either hosted or embedded, use a single memory space in which words such asCREATE , comma and
ALLOT refer to the same area of memory. Open Firmware
and the draft cross compiler word set provide the word
BUFFER: ( u "<spaces>name" -- )
to create a buffer.
This word is provided by several hosted Forths. It is particularly
useful on CPUs (including all current x86 PC processors) which
require separation of code and data for best performance.
BUFFER: permits implementers to create buffers in any
available memory region, not just the dictionary data area.
Current practiseAt least SwiftForth, VFX Forth, some versions of Win32Forth, Open Firmware and the draft cross compiler word set provideBUFFER:
already.
An optimal version of BUFFER: cannot be written without carnal
knowledge of the underlying Forth system.
By introducing BUFFER: a system can preserve the behaviour of
CREATE , comma, ALLOT and friends as referring to
a common data space preserving common entitlements such as
while providing BUFFER: for highest performance and increasing
compatibility with embedded systems code.
ApproachAt very least the use of
is more readable than
The proposal below provides a more consistent approach than code
such as
which is replaced by
Note that in systems with separated code and data space, the
occasionally seen phrase
may not give correct results, whereas the approach above will
work.
Proposal6.2.aaaa BUFFER: buffer-colon CORE EXT
Reference ImplementationThis implementation depends on children ofCREATE returning an
aligned address as already specified by Forth200x. Other memory
location techniques require implementation-specific knowledge of the underlying
Forth system.
|
: BUFFER: \ u "<name>" -- ; -- addr
\ Create a buffer of u address units whose address is returned
\ at run time.
CREATE ALLOT
;
DECIMAL
T{ 10 BUFFER: TBUF -> }T
T{ HERE 10 - -> TBUF }T
Note that you can be both a system implementor and a programmer, so you can submit both kinds of ballots.