Re: Euphoria to Java
- Posted by Jeffrey Fielding <JJProg at CYBERBURY.NET> Dec 02, 2000
- 497 views
I've been thinking of writing a C or Java translator/interpreter. I had written an Euphoria-like assembly language a while back with an interpreter in C, but it wasn't as fast as I would have liked and the C code was buggy. I just started trying to write a fast set of classes for Euphoria objects in C++. I'm even pretty impressed with the speed, though the I'm comparing interpreted Euphoria to compiled C++. I translated part of the sequence benchmark, and here's the results so far: Task exu my program s = repeat(999, 100) 811733 690667 z = x + y (both length 100) 210466 132933 appending to a sequence 5413333 6811670 slicing a sequence 2167500 3100000 So maybe with some more optimization, it would be worth it. What I've really been interested in, however, is writing a combined translator and optimizer that would automatically use primitive ints etc., like you suggest, and also allocate arrays where applicable instead of sequences. This would make the sieve benchmark, for example, almost as fast as a hand-coded C version. As for Java, I downloaded your Euphoria to Java translator, and it works quite nicely for most simple Euphoria programs. I tweaked a bit of the Java code and added some code to use built-in ints in for loops, which increased the speed significantly. The problem with Java for translated Euphoria programs, in my opinion, is that you can't do the kind of casting you can in C with primitive types. In C, you could do: struct sequence { int *data; int count; }; And you could make each element of data anything that can be stored in 4 bytes by doing something like (untested and my C is a little rusty): sequence *s = ((sequence **)seq->data)[0]; int x = seq->data[0]; float f = ((float *)seq->data)[0]; But in Java, you have to use a whole class such as Integer. And what's worse is that you can't re-assign Integers in Java, so every time you modify it you have to create a new object. This makes it really, really slow compared to using a primitive int. So maybe I'll start working on an Euphoria to C/Java translator over winter vacation... Jeff On Sat, 2 Dec 2000, David Cuny wrote: > For various and sundry reasons, I've been pondering a Euphoria to Java > translator. I've heard the arguments against Java - it's a slow, bloated > resource-hogging b&d style language. I used to believe some of them, but > with each new generation of PC, the requirements of Java seem more and more > modest. Additionally, the speed increases due to JIT compiler technology has > been nothing short of staggering. > > Some time back, I had written a preliminary Euphoria to Java translator. You > can find it at: > > http://www.lanset.com/dcuny/java.htm > > It's only capable of translating simple Euphoria programs. The project > stalled because I had trouble figuring out how to link it to Swing. As you > might guess, I've gotten back to the code more recently, and have been doing > some more experiments. > > I've been spurred on mostly by Robert's Euphoria to C translator. There's > obviously a good enough mapping between C and Euphoria for his translator to > work. But it seems to me that an equally viable mapping exists between > Euphoria and Java. For example, here's a mapping of the basic data types: > > integer -> int, Integer > atom -> long, Long > sequence -> Vector > > Robert's coding of sequences is: > > struct s1 { > object_ptr first; > long length; > long ref; > long postfill; > } > > Java already provides a mechanism for determining the length of a Vector, > and automatically maintains space for expansion. So the Java equivalent > could simply be: > > class EuObject { > Object data; > int refcount = 0; > } > > There's a close correlation in code generation as well. Here's an example: > > object o1, o2 > o1 = {1,2,3} > o2 = o1[2] > > Robert's translator generates: > > // o1 = {1,2,3} > _0 = _0o1; > _1 = NewS1(3); > _2 = (int)((s1_ptr)_1)->first; > *((int *)(_2+0)) = 1; > *((int *)(_2+4)) = 2; > *((int *)(_2+8)) = 3; > _0o1 = MAKE_SEQ(_1); > DeRef(_0); > > // o2 = o1[2] > _0 = _0o2; > _2 = (int)SEQ_PTR(_0o1); > _0o2 = (int)*(1 + ((s1_ptr)_2)->first); > Ref(_0o2); > DeRef(_0); > > Here's a guess at what the equivalent Java code might be: > > // o1 = {1,2,3} > _0 = _0o1; > _1 = new EuObject(); > _1.data = new Vector(); > _1.data.addElement( new Integer( 1 ) ); > _1.data.addElement( new Integer( 2 ) ); > _1.data.addElement( new Integer( 3 ) ); > _0o1 = _1; > _1.DeRef(); > > // o2 = o1[2] > _0 = _0o2; > _0o2 = _0o1.elementAt( 1 ); > _0o2.Ref(); > _0.DeRef(); > > Of course, it's sort of cheating not to show how Ref and DeRef might be > implemented: > > public void Ref() { > // is the object is a sequence? > if (this.data instanceof Vector) { > // increment the reference count > this.refcount += 1; > } > } > > public void DeRef() { > // is the object a sequence? > if (this.data instanceof Vector) { > // decrement the reference count > this.refcount -= 1; > } > > // release the object. if there are no more > // references to it, Java will garbage collect it. > this = _null; > } > > The Java code isn't that different from the C code, and I *suspect* that the > resulting Java code would be acceptably fast. > > If such a translator were written, Euphoria programs would be able to run on > a much wider base of machines - Macs, non-x86 based machines - anything that > Java has been ported to. > > In addition, we'd also have access to the AWT and Swing. Anyone subscribed > to this list for any period of time knows that I'd love for Euphoria to have > a portable GUI. > > So, assuming that the project is feasible, here comes the interesting > question: > > - Who wants to write it? > > Robert is obviously in the best position to write this. Whether he is > interested in doing this, or even if such a project is commercially viable, > I couldn't guess. > > Pete Eberlein is my number two pick, but he seems to have disappeared off > into the Land of Reality, that unhappy place of the 9 to 5 grind. > > I suspect that I could do it, but there are other things that I'd rather do. > > I've also got a sneaking feeling that *someone* is currently working on this > project, but it's nothing more than a hunch. > > Comments? > > -- David Cuny >