Hi all,

I wanted to discuss a little trick I implemented a few days ago.

In the new code generator I'm working on, there are various data types
representing virtual instructions. For example,

TUPLE: %call < insn word ;
TUPLE: %jump < insn word ;
TUPLE: %intrinsic < insn quot vregs ;
TUPLE: %return < insn ;

What I do is wrap a bunch of code in a 'make' form and build a
sequence of such instructions by traversing the high level IR.

For example, we might generate a call to '+' as follows,

\ + %call boa ,

However, this is a bit repetitive, so we might try factoring it out:

: %call ( word -- ) \ %call boa , ;

However, doing this for each instruction is tedious, and so is
specifying the superclass for each one. So I made a parsing word INSN:

INSN: %call word ;
INSN: %jump word ;
INSN: %intrinsic quot vregs ;
INSN: %return ;

Here is the implementation:

: INSN:
    parse-tuple-definition
    [ dup tuple eq? [ drop insn ] when ] dip
    [ define-tuple-class ]
    [ 2drop save-location ]
    [ 2drop dup [ boa , ] curry define-inline ]
    3tri ; parsing

It's a bit long, but it saves more code than it consumes.

Also 'make' is ideal here. I have instruction generation spread out
over many words. 'bake' doesn't apply here and passing a vector around
on the stack or in a local variable will get tedious very quickly. It
is also nice that the call to 'boa ,' can be hidden in a word with the
same name as the class.

Finally, for instructions with no parameters, eg %return, the
optimizer folds the 'boa' call at compile time (it actually does it
for tuples where all slots are read-only, of which empty tuples are a
special case of course). So, INSN: %return ; becomes something like

TUPLE: %return < insn ;
: %return ( -- ) T{ %return } , ; inline

Very nice.

I like programming in Factor. There's nothing else out there that
offers the unique mix of capabilities that Factor has.

Slava

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to