C->FORTH FORTH->C Translators 
Author Message
 C->FORTH FORTH->C Translators

Would anyone care to comment on favorites?

-drl



Fri, 01 Jul 2005 06:48:52 GMT  
 C->FORTH FORTH->C Translators
On Sun, 12 Jan 2003 22:48:52 GMT, "Danny Ross Lunsford"

Quote:

>Would anyone care to comment on favorites?

>-drl

I know some of these, they are called programmers ;-)

Keep up the good handworking!

Coos



Fri, 01 Jul 2005 09:25:50 GMT  
 C->FORTH FORTH->C Translators

Quote:

> Would anyone care to comment on favorites?

Generally translators will not produce good code.  For
one thing each language needs its own style of factoring.

I have seen one C->ANS Forth compiler but was not
impressed.  MPE also has a product that compiles C
to Forth.

The easiest way to do Forth->C is to just use a Forth
that is in fact a C program.  Then one can compile it
and Forth code that follows from C.  So I would recommend
something halfway in between C and Forth, an ANS Forth
written in C.  There are a number to choose from.

Best wishes,
Jeff Fox



Sun, 03 Jul 2005 12:47:21 GMT  
 C->FORTH FORTH->C Translators
Dear Lunsford,

Jeff Fox wrote

Quote:
> Generally translators will not produce good code.  For
> one thing each language needs its own style of factoring.

I'm impressed with
        "Compilation of Forth to C"
        by Anton Ertl, Martin Maierhofer
        "Forth code size and programming effort"
        by Anton Ertl
both at
        http://www.complang.tuwien.ac.at/projects/forth.html

It appears that all the source code for this "Forth to C" translator
is available for download, although I haven't tried it.
Yes, it produces really ugly C code, but that's just an intermediate
step --
the C code is then run through a standard C compiler to get an
executable, then the C code can be deleted, perhaps never to be seen
by human eyes.

The net effect is: code written in Forth is converted to executable
code. The little detour through C results in code that is faster and
smaller than executable code generated by other popular Forth
compilers. Is that not "good" ?

That web page also points to a couple of other "Forth to C"
translators.

I haven't seen any "C to Forth" translators,
but I imagine that C compilers for any of the "Forth CPUs" must come
very close.

--
DAV



Tue, 05 Jul 2005 18:15:41 GMT  
 C->FORTH FORTH->C Translators
...

Quote:
>>Generally translators will not produce good code.  For
>>one thing each language needs its own style of factoring.

> ...
> It appears that all the source code for this "Forth to C" translator
> is available for download, although I haven't tried it.
> Yes, it produces really ugly C code, but that's just an intermediate
> step --
> the C code is then run through a standard C compiler to get an
> executable, then the C code can be deleted, perhaps never to be seen
> by human eyes.

> The net effect is: code written in Forth is converted to executable
> code. The little detour through C results in code that is faster and
> smaller than executable code generated by other popular Forth
> compilers. Is that not "good" ?

What do you base this assertion on, given that you haven't tried it?
It's hard to imagine that translating Forth to "really ugly C" and
then putting that through a C compiler produces better code than
a good optimizing Forth-to-code compiler can.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================



Wed, 06 Jul 2005 01:50:11 GMT  
 C->FORTH FORTH->C Translators


Quote:

> ...
> >>Generally translators will not produce good code.  For
> >>one thing each language needs its own style of factoring.

> > ...
> > It appears that all the source code for this "Forth to C" translator
> > is available for download, although I haven't tried it.
> > Yes, it produces really ugly C code, but that's just an intermediate
> > step --
> > the C code is then run through a standard C compiler to get an
> > executable, then the C code can be deleted, perhaps never to be seen
> > by human eyes.

> > The net effect is: code written in Forth is converted to executable
> > code. The little detour through C results in code that is faster and
> > smaller than executable code generated by other popular Forth
> > compilers. Is that not "good" ?

> What do you base this assertion on, given that you haven't tried it?
> It's hard to imagine that translating Forth to "really ugly C" and
> then putting that through a C compiler produces better code than
> a good optimizing Forth-to-code compiler can.

> Cheers,
> Elizabeth

Though I haven't seen it, this must be true - because the only sensible
strategy for F->C would be a dispatch table of function pointers, and that
ends up being the same thing as FORTH with the overhead of C calling
conventions.

In any case, I'm more interested in C->FORTH because the native environment
will be FORTH.

-drl



Wed, 06 Jul 2005 11:55:54 GMT  
 C->FORTH FORTH->C Translators

Quote:




>>...

>>>>Generally translators will not produce good code.  For
>>>>one thing each language needs its own style of factoring.

>>>...
>>>  [...]
>>>The net effect is: code written in Forth is converted to executable
>>>code. The little detour through C results in code that is faster and
>>>smaller than executable code generated by other popular Forth
>>>compilers. Is that not "good" ?

>>What do you base this assertion on, given that you haven't tried it?
>>It's hard to imagine that translating Forth to "really ugly C" and
>>then putting that through a C compiler produces better code than
>>a good optimizing Forth-to-code compiler can.
>> [...]

> Though I haven't seen it, this must be true - because the only sensible
> strategy for F->C would be a dispatch table of function pointers, and that
> ends up being the same thing as FORTH with the overhead of C calling
> conventions.

I don't think this discussion does justice to Ertl and
Maierhofer's forth2c.  It's idea is fairly sophisticated, and
the benchmarks show it does very well indeed.

There's nothing like a dispatch table of function pointers that
I'm aware of, and the overhead of C calling is eliminated for
sequences of "primitive" words.  The code can look messy,
because it puts stack elements into C local varibles, and does
the stack juggling by transfering among them.  Reasonable C
compilers then optimize most of the juggling away, producing
quite efficient runtime code.

I hope Anton will correct me if that description isn't correct...

Less sophisticated schemes like the ^Forth translator (and
possibly Timbre*) don't use local variables very aggressively,
and so don't eliminate very much stack juggling.  ^Forth
translation does eliminate the overhead of C function calls for
primitives, which are inlined C code fragments.  It also has the
option of keeping TOS in a register.  There is no dispatch
table.  It sort of treats C as a macro assembler, and allows the
explicit insertion of C fragments into Forth, in the same spirit
as Forth assemblers.

-- David

* I don't intend to say that Timbre is unsophisticated (it *is*
sophisticated), just that AFAIK it's not aggressive about local
variables.



Wed, 06 Jul 2005 21:36:02 GMT  
 C->FORTH FORTH->C Translators

Quote:

>> The net effect is: code written in Forth is converted to executable
>> code. The little detour through C results in code that is faster and
>> smaller than executable code generated by other popular Forth
>> compilers. Is that not "good" ?

>What do you base this assertion on, given that you haven't tried it?

He/she/it cited his/her/its sources; here are the direct URLs:

http://www.complang.tuwien.ac.at/papers/ertl%26maierhofer95.ps.gz
http://www.complang.tuwien.ac.at/forth/performance.html

Of course, the data is from 1995, before SwiftForth and VFX, but the
benchmarks are available in both Forth and f2c-generated form (as well
as manually written C) at

http://www.complang.tuwien.ac.at/forth/bench.zip

So you can test it with your favourite Forth and C compilers yourself
and report the results here.

Just to give you an idea of the quality of the resulting code, here's
a Forth definition:

: max ( n1 n2 -- n )
 2dup < if
   swap drop
 else
   drop
 endif ;

And here's the code resulting from combining f2c with gcc-2.6.3 -O3
-fomit-frame-pointer:

  movl 4(%esp),%edx
  movl 8(%esp),%eax
  cmpl %edx,%eax
  jge L5
  movl %edx,%eax
L5:
  ret

However, f2c is not a full Forth compiler, and using its approach in a
full ANS Forth would be hard, possibly result in slower code, and it
would not be competetive in compile-time and in Forth code employing
run-time code generation (such as forth/mm-rtcg.fs in bench.zip).

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html



Thu, 07 Jul 2005 19:50:54 GMT  
 C->FORTH FORTH->C Translators
On Sat, 18 Jan 2003 03:55:54 GMT, "Danny Ross Lunsford"

Quote:

>In any case, I'm more interested in C->FORTH because the native environment
>will be FORTH.

As Jeff noted, MPE has a C to Forth compiler. In order to generate
reasonably efficient code, the host Forth needs a local variable
system and some extensions need to be added. With a VFX compiler
on the back end, the result is quite reasonable performance.

Stephen
--

MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, fax: +44 (0)23 8033 9691
web: http://www.mpeltd.demon.co.uk - free VFX Forth downloads



Fri, 08 Jul 2005 01:36:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. CS people in Forth?

2. CS People in Forth?

3. Forth in CS

4. Believe or not, Forth in CS!

5. Forth and CS

6. Where does FORTH belong in CS and Academia?

7. to CS: or not to CS: in F-PC assembler

8. CORRECTING -->NOW SHOWING - wiki.cs.uiuc.edu/VisualWorks/Recent+Changes

9. HELP>>>>>>>Fortran-Pascal Linking

10. >>>>>>>FROM SMTK TO C++

11. ><><><><>Heeeeeeeeeeeeeeelp on INT 14!><><><><><>

12. <<<<<YOU MUST CHECK THIS OUT >>>>>>>>>> 2103

 

 
Powered by phpBB® Forum Software