forked from Forth-Standard/forth200x
/
buffer.txt
92 lines (74 loc) · 2.74 KB
/
buffer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
RfD: BUFFER:
4 February 2009, Stephen Pelc
Rationale
=========
Problem
-------
Despite 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 as CREATE, comma and ALLOT refer to the
same area of memory. Open Firmware and the draft cross compiler
word set provide the word BUFFER: ( n "<spaces>name" -- ) to create
a buffer. In the cross compiler word set, BUFFER: creates the buffer
in an area of uninitialised memory.
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. On some
existing hosted systems, I have noted a variation in performance
(on the same Forth system) of 7:1 for a benchmark dependent on the
location of a single variable.
Current practice
----------------
At least SwiftForth, VFX Forth, and some versions of Win32Forth
provide BUFFER: 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
CREATE FOOP 55 , 66 , 100 ALLOT
while providing BUFFER: for highest performance and increasing
compatibility with embedded systems code.
Approach
--------
At very least the use of
256 BUFFER: Xbuff
is more readable than
CREATE Xbuff 256 ALLOT
The proposal below provides a more consistent approach than code
such as
CREATE Xbuff 0 C, 255 ALLOT
which is replced by
256 BUFFER: Xbuff 0 Xbuff C!
Note that in systems with separated code and data space, the
occasionally seen phrase
VARIABLE Xbuff 256 CELL - ALLOT
may not give correct results.
Similarly, it is well worth defining the contiguity rules for the
children of VARIABLE, VALUE and friends, but this is outside the
scope of the BUFFER: proposal.
Proposal
========
x.y.z.nnnn BUFFER: CORE EXT
( n “<spaces>name” -- )
Skip leading space delimiters. Parse name delimited by a space.
Create a definition for name, with the execution semantics defined
below. Reserve n characters at an aligned address. Contiguity of
this region with any other region is undefined.
name Execution: ( -- a-addr )
A-addr is the address of the space reserved by BUFFER: when it
defined name. A program is responsible for initializing the
contents.
Reference Implementation
========================
This implementation depends on children of CREATE returning an
aligned address as already secified by Forth200x.
: BUFFER: \ n "<name>" -- ; -- addr
\ Create a buffer of n address units whose address is returned
\ at run time.
CREATE ALLOT
;
Tests
=====
TBD