June 11, 2003

JavaOne - compiling programming languages to the JVM

Java | Open Source
Per Bothner explaining how Kawa works.

Per Bothner's talk on Kawa is about to start. Per worked on various things over the year. He used to work for Cygnus, before being acquired by RedHat; he worked on GCC and many other tools.

17:16 It started. Apparently Per is the only presenter not affiliated with any company, which an almost unheard of at JavaOne. The attendance is pretty light, it's either too late or people don't care about languages other than Java.

What do you do when you need a higher language than Java? Well, you can write an interpreter. However if you do repetitive computations, it can get pretty slow. Another approach is to compile the program in your language to Java source code.

The best approach is to compile directly to in-memory Java bytecodes. Per makes the interesting assertion that bytecodes are more general than Java source: you actually have goto statements.

Kawa was written while at Cygnus in 1996 and is a GNU project, with a more liberal license than GPL. Kawa can be run interactively from the command line, it can be compiled to a program file. Languages implemented in Kawa: Scheme, XQuery, XSLT, Emacs Lisp etc.

Short introduction to Scheme, an "impure" functional language because of assignment. You run Kawa by doing java kawa.repl and you get the interactive prompt. It supports big integers.You can write applets in Scheme's Kawa.

Another language supported is Common Lisp. Guy Steele was instrumental in the Scheme, Common Lisp and Java languages.

Emacs Lisp: "Emacs is still the most powerful text editor". Kawa compiles Elisp to Java bytecodes. Goal is modern Emacs re-implementation that can efficiently use existing Emacs packages. Uses Swing to represent and display text. A nice JEmacs screenshot, unfortunately not many people actually contribute to it.

XQuery is a very high-level language used for querying, combining and generating XML-like data sets. It is a superset of XPath. Kawa supports XQuery with Qexo, which is missing some features, but still very useful. Some example of XQuery to generate an HTML fragment: it uses HTML markup and XQuery syntax to generate the output page. The example can generate XHTML 1.0, HTML, Scheme from the same file. The example XQuery program can be compiled to a Java class with a main, or to a servlet which can be deployed on a servlet container.

XQuery can be considered as an alternative to JSP. An XQuery program can also be compiled to a CGI program, not very useful these days however. You can embed the XQuery engine in a Java program and take advantage of its power.

Next language shown is XSLT. The Kawa implementation compiles an XSLT stylesheet into a Java class. The project is incomplete, but it's a useful example.

BRL is Beautiful Report Language, a template language much like JSP. Instead of embedding Java, you embed Scheme. KRL - Kawa Report Language - is Per's implementation. The language uses square brackets to embed Scheme code. You can embed such code within HTML tags.

Nice is a strongly typed language with multi-methods, parametric types, anonymous functions, tuples and multiple implementation. KRL and Nice were both written by other people than Per.

Implementation. Each language is a subclass of Interpreter. Each Interpreter uses a Lexer to parse an expression or a program file. The result is an Excpression instance. There are many subclasses of Expression. Once you have an Expression object, you call the compile() method to compile the script. This method takes two arguments, a Compilation object for managing the state, and a Target object for specifying where to leave the result, usually the JVM's stack.

The implementation uses the gnu.bytecode package written by Per for handling bytecodes and .class files: code generation, reading, writing, printing and disassembling. This is a library for dealing with the very low level bytecodes. Apache has the equivalent BCEL, but Per claims gnu.bytecode is more efficient because it doesn't generate a Java object for each bytecode being outputted.

In summary, Kawa includes a good compiler and useful libraries. The Scheme and XQuery languages on Kawa are the most popular languages. The license is either GPL or a more liberal license that allows you to include Kawa in a commercial application if you don't do any modifications to the original code. If you do such changes, you are required to submit them back to Kawa.

Kawa is available at http://www.gnu.org/software/kawa, Qexo could be found at http://www.gnu.org/software/qexo/.

Questions. XQuery was started in summer 2001, and is still in works. Per works on it part-time, he's day-time job is working for Apple (as a contractor at Apple - working on GCC?). Kawa's Scheme is not tail-call elimination. Writing parsers with Yacc sucks, Per prefers writing them by hand. GCC is replacing the Yacc parser with a hand-written descendent recursive parser. Kawa is an optimizing compiler, sort of. It doesn't do common subexpression elimination, it uses a simple minded register allocation. Errors generated at runtime will have an exception stacktrace that refers to the original source file.

Posted by ovidiu at June 11, 2003 06:15 PM |
 
Copyright © 2002-2016 Ovidiu Predescu.