VFX Forth 64 is built from the same source tree as its 32 bit predecessor. Because of the way operating systems are built, there are several details which require consideration before starting the conversion of a major application.
Migration is fairly straight-forward with a bit of preparation, most of which is for converting data structures.
Whereas the ARM32 and ARM64 instruction sets are different, the x64 instruction set is a binary extension of the x86 ISA. This causes as many problems as it solves while permitting 32 bit binaries to run unchanged. The AMD Opteron was the first processor to implement what is now called the x64 or x86_64 or AMD64 instruction set and was released in 2003.
Conversion of the 32 bit system to 64 bit was apparently simple, but it took a while for the inconsistencies and changes to work their way to the surface, especially when using R12 and R13 as index registers.
Regardless of the C definitions, VFX Forth treats 64 bit CPUs as having everything 64 bit unless otherwise defined.
Data size Memory ops name
64 @ ! X@ X! xword, e.g. .xword
32 L@ L! lword/dword
16 W@ W! word
8 C@ C! char/byte
In order to separate Forth data sizes from C data sizes (which can change according to O/S and even language) we define data sizes as names with a leading '_' character.
1 constant _BYTE
2 constant _WORD
4 constant _DWORD
8 constant _XWORD
8 constant _PTR
Many other sizes are defined for each operating system, but these may/will not be portable between versions of VFX Forth.
In a perfect world, since Windows only provides a single documented version of each structure, we ought to be able to use a single definition of a structure for both 32 bit and 64 bit use. This is almost feasible, but not quite. The main reason is the use of C pragmas to "adjust" layout.
We keep a C++ test file <Vfx>/Kernel/x64win/Tests/test.cpp that we use to check the Forth and C structure definitions. This file has revealed several surprises. Using the test file and its related header files is more reliable than counting bytes in the operating system documentation.
In order to cope with operating system default alignments, we
provide the word A-FIELD ( offset n -- ; addr -- 'addr)
which behaves like FIELD
, but aligns data items of size
2, 4, 8 or 16 bytes. The following is the Windows MSG
structure:
struct MSG \ -- len
_hwnd a-FIELD msg.hWnd
_uint a-FIELD msg.Message
_wparam a-FIELD msg.wParam
_lparam a-FIELD msg.lParam
_dword a-FIELD msg.time
point a-FIELD msg.point
\ _dword a-field msg.lPrivate
end-struct
Apart from unexpected pragma usage, a number of Windows structures are padded at the end, usually to an 8-byte boundary.
Increasingly, MPE starts Forth data structures names with a leading '/' to indicate a size. This practice will become more common over time.
The commonest source of error when using these structures is
to forget to change @
or !
as required by a
changed (or not) data size. This problem can be amost entirely
eliminated using an intelligent data structure package such
as ClassVFX below.
Intelligent data structures are a half-way house between Forth structures and a full OOP package. VFX Forth includes the ClassVFX system (see manual for details). The structures can be used as globals, locals or heap items.