# Factor interpreter in C

## Slava Pestov — 2004-06-26 06:01:28

    Hi everybody,

    For the last week or so I've been working on a new Factor interpreter

    written in C.

    It is similar in design to the Java interpreter. The major difference is

    how automatic persistence is handled.

    In the C interpreter, a workspace image is simply a dump of the Factor

    heap. Images do not contain native code -- only Factor data structures

    such as literals, cons cells, arrays etc.

    The C interpreter cannot directly read Factor source files -- it needs a

    binary image.

    I have developed a "cross-compiler" that runs in the Java interpreter

    and compiles Factor word definitions to an image file suitable for the C VM.

    It is already possible to cross-compile a few of the simpler Factor

    library functions such as "append", "length", "fib" into a new image,

    and load the image in the C VM. It even gives the correct result some of

    the time :-)

    Eventually the goal is to write a Factor parser in Factor, and

    cross-compile this to create the first 'bootstrap' image.

    Once this is done, the C VM will be usable interactively, and will also

    be able to load its own sources and generate new images.

    The C core is only 800 lines of code or so and already provides a good

    base of functionality. The remaining tasks at the VM level are:

    - Garbage collection (!)

    - Exception system

    - Remaining data types: vector, string, more number types

    - Growable stacks

    - Exposing primitives for file and network streams

    Right now a compiled and stripped VM binary is 8k and When functionally

    complete I expect it to maybe be double that. This means the entire

    thing will fit in the L1 cache of most CPUs.

    A deliberate design goal of the C VM is that it will not use more than

    one native OS thread. A lightweight co-operative multitasking and

    non-blocking I/O API using continuations will be added later.

    Slava

## John Cowan — 2004-06-28 13:50:38

    Slava Pestov scripsit:

     > - Garbage collection (!)

     Consider using the Boehm-Demers-Weiser conservative garbage collector

    that Joy uses.  GC is a hard problem and there's no reason for you

    to solve it yourself.  See http://www.hpl.hp.com/personal/Hans_Boehm/gc/ .

    Note that the space and time overhead is only slightly worse than

    that of typical malloc/free implementations.

    --

    What is the sound of Perl?  Is it not the       John Cowan

    sound of a [Ww]all that people have stopped     cowan@...

    banging their head against?  --Larry            http://www.ccil.org/~cowan

## Chris Double — 2004-06-28 22:14:13

    On Mon, 28 Jun 2004 09:50:38 -0400, "John Cowan" <cowan@...> said:

     > Slava Pestov scripsit:

    >

    > > - Garbage collection (!)

    >

    > Consider using the Boehm-Demers-Weiser conservative garbage collector

    > that Joy uses.  GC is a hard problem and there's no reason for you

    > to solve it yourself.  See http://www.hpl.hp.com/personal/Hans_Boehm/gc/

     Another 'off the shelf' solution is Ravenbrooks Memory Pool System:

    http://www.ravenbrook.com/project/mps/

    This is open source and is the garbage collector used in Functional

    Developer (http://www.functionalobjects.com), a commercial (and recently

    open sourced) Dylan development system.

    Chris.

    --

      Chris Double

      chris.double@...