Compiler scopes

Forth cross compilers for embedded systems have to address multiple memory types and compilation modes. Raptor pretty much follows the defacto standard cross-compiler wordset described by XCapp.PDF.

Cross compilers have multiple personalities. They can be compiling code to extend the compiler, compiling ROM code, etc. To switch personalities, you use HOST, INTERPRETER, COMPILER or TARGET. Each scope (or personality) has its own search order. You can list all of these by typing ORDERS. To list the current scope's search order, use ORDER. The default ORDERS are:

HOST
Context: FORTH ROOT
Current: FORTH
INTERPRETER
Context: FORTH *INTERPRETER ROOT
Current: *INTERPRETER
COMPILER
Context: *COMPILER FORTH ROOT
Current: *COMPILER
TARGET
Context: *INTERPRETER *TARGET ROOT
Current: *TARGET
---------------------------------
Current Order = HOST

Examples

The *COMPILER wordlist contains Forth primitives that operate on the optimizer's stack model. They are words that cause machine code to be generated. You can define your own compiler primitives like this:

COMPILER
: -ROT ROT ROT ;
TARGET

You can make new defining words in the INTERPRETER scope. For example:
INTERPRETER CDATA
: MyCons CREATE , DOES> @C ;
TARGET
1 MyCons One
2 MyCons Two

In this case, DOES> compiles the child code @C ; and remembers the address of this code. Each instance of MyCons produces a jump to this address.


Target memory usage

Raptor supports two kinds of memory: RAM and ROM. Within RAM, there can be initialized and uninitialized RAM. Initialized RAM depends on startup code in ROM to initialize it. Uninitialized RAM powers up to an unknown state. The Forth word HERE points to the next free byte in memory. In a cross compiler environment, HERE and related words use CCODE, CDATA, IDATA and UDATA to tell them which memory space to operate in.

They are often used when creating data structures. For example, consider "CREATE FOO 1 , 2 , 3 ,". The runtime semantics of FOO as well as the location of the data "1 2 3" can be determined by which memory space is chosen beforehand:

Space FOO returns an address that: Data is stored in: Comments
CDATA Points to the data section of ROM  Data section of ROM Use @C and C@C to fetch from code space.
CCODE  Points to the code part of FOO Code section of ROM  Bad idea. The address will point to the code, not the data.
UDATA Points to uninitialized RAM Uninitialized RAM  No good. At run time the table will be junk.
IDATA Points to initialized RAM Initialized RAM  Good. Just make sure there is startup code.

The first thing to do in a program is define memory spaces.
SECTION ( loaddr hiaddr <name> -- ) sets the address limits for the current memory space. It also creates a word that sets the address limits. Example:

interpreter hex
0000 0FFF idata section IRAM
1000 3FFF udata section URAM
1C00 1FFF cdata section ROM_DATA
0000 1BFF ccode section ROM_CODE

URAM etc. are often never used, so they end up being semantic sugar. If they are used, the appropriate memory must be selected first. Example: UDATA URAM

.ALLOCATION lists memory usage statistics.

On desktop Forths, code space and data space overlap. That is, data operators may access data in either space. Not so with embedded processors. The target CPU may be a Harvard architecture with separate instructions used to access program and data space. Raptor provides @C, C@C, !C and C!C to access code space.

This presents an interesting challenge for string compilation words such as .", S" , C" etc. For compibility with ANS Forth, strings can be compiled to IDATA space. This requires both RAM and ROM for storage of each string. In embedded systems, especially the single chip ones, RAM is expensive and ROM is cheap. It would be much better to compile the strings to ROM.

So, you can save RAM by using CDATA before a word that compiles strings to memory. Just use operators that know it's in code space. Since the CD16 uses 16-bit characters, the word P" is provided to store a packed counted ASCII string to code space. P>$ ( caddr arrr -- ) unpacks it and copies it to data space.