Compiling Standard ML to Java Bytecodes

Compiling Standard ML to Java Bytecodes

Compiling Standard ML to Java Byteco des NickBenton Andrew Kennedy George Russell In Proceedings of the rd ACM SIGPLAN Conference on Functional Programming Septemb er Baltimore The following copyright notice is required by the ACM see httpwwwacmorgpubscopy righ tpol icy html c Copyright by the Asso ciation for Computing Machinery Inc Permission to make digital or hard copies of part or all of this work for p ersonal or classro om use is granted without fee provided that copies are not made or distributed for prot or commercial advantage and that copies b ear this notice and the full citation on the rst page Copyrights for comp onents of this work owned by others than ACM must b e honored Abstracting with credit is p ermitted To copy otherwise to republish to p ost on servers or to redistribute to lists requires prior sp ecic p ermission andor a fee Request p ermissions from Publications Dept ACM Inc fax or permissionsacmorg Compiling Standard ML to Java Byteco des Nick Benton Andrew Kennedy George Russell Persimmon IT Inc Cambridge UK fnickandrewgeorgegpersimmoncouk Abstract adoption of functional languages in real world applications and the situation is getting worse as more software has to MLJ compiles SML into veriercompliant Java byte op erate in a complex environment interacting with com co des Its features include typ echecked interlanguage work p onents written in a variety of languages p ossibly wrapp ed ing extensions which allow ML and Javacodetocall each up in a distributed comp onentarchitecture such as CORBA other automatic recompilation management compact com DCOM or JavaBeans piled co de and runtime p erformance which using a just in Because the semantic gap between Java and ML is time compiling Java virtual machine usually exceeds that smaller than that b etween C and ML and b ecause Java uses of existing sp ecialised byteco de interpreters for ML Notable a simple scheme for dynamic linking MLJ is able to make features of the compiler itself include wholeprogram optimi interlanguage working safe and straightforward MLJ co de sation based on rewriting compilation of p olymorphism by can not only call external Java metho ds but can also ma sp ecialisation a novel monadic intermediate language which nipulate Java ob jects and declare Java classes with metho ds expresses eect information in the typ e system and some in implemented in ML which can b e called from Java Thus teresting data representation choices the MLJ programmer can not only write applets in ML but also has instant access to standard libraries for and D orking graphics GUIs database access so ckets and netw Intro duction concurrency CORBA connectivity security servlets sound and so on as well as to a large and rapidlygrowing collec The success of Sun Microsystems Java language means tion of thirdpartycode that virtual machines executing Javas secure multi The interesting question is whether ML can b e compiled threaded garbagecollected byteco de and supp orted by a into Javabyteco des which are ecient enough to b e useful capable collection of standard library classes are now not Java itself is often criticised for being to o slow esp ecially just available for a wide range of architectures and op erat running co de which do es signicant amounts of allo cation ing systems but are actually installed on most mo dern ma and its byteco des were certainly not designed with compila chines The idea of compiling a functional language suchas tion of other languages in mind There is little opp ortunity ML into Javabyteco des is thus very app ealing as well as the for lowlevel backend trickery since wehave no control over obvious attraction of b eing able to run the same compiled the garbage collector heap layout etc and the requirement co de on any machine with a JVM the p otential b enets of that compiled classes pass the Javaverier places strict typ e interlanguage working b etween Java and ML are consider constraints on the co de we generate Furthermore current able Java virtual machines not only store activation records on Many existing compilers for functional languages have a xedsize stack but also fail to optimise tail calls Thus the ability to call external functions written in another lan the initial prosp ects for generating acceptably ecientJava guage usually C Unfortunately dierences in memory byteco des from a functional language did not lo ok go o d our mo dels and typ e systems make most of these foreign func rst very simpleminded lamb dacalculus to Java translator tion interfaces awkward to use limited in functionality and plus an early JVM ran the nb b enchmark times slower even typ eunsafe Consequently although there are for ex than Moscow ML and it was clear that a practical ML to ample go o d functional graphics libraries which call X Jav a byteco de compiler would have to do fairly extensive the typical functional programmer probably do esnt b other optimisations MLJ is still a workinprogress and there is to use a C language interface to call everyday library func scop e for signicant improvement in b oth compilation sp eed tions to say calculate an MD checksum manipulate a GIF and the generated co de in particular the currentversion le or access a database She thus either do es more w ork still only optimises simple tail calls but it is already quite than should b e necessaryorgives up and uses another lan usable on source programs of several thousand lines and pro guage This is surely a ma jor factor holding back the wider duces co de which with a go o d mo dern JVM usually out p erforms the p opular MoscowMLbyteco de interpreter To app ear in the rd ACM SIGPLAN Conference on Functional Programming Septemb er Baltimore Java A large subset of the new standard Basis Library has Overview b een implemented Compiler phases The interlanguage features bring Java typ es and val ues into ML whilst enforcing a separation b etween the two MLJ is intended for writing compact selfcontained appli though a Javatyp e and an ML typ e maywell end up the cations applets and software comp onents and do es not have same in the compiled co de External Java typ es maybe the usual SML interactive readevalprint top level Instead referred to in ML simply by using their Java names in quo it op erates much more like a traditional batch compiler The tation marks Thus structures which make up a pro ject are separately parsed typ echecked translated into our typ ed Monadic Interme javaawtGraphics int diate Language MIL see section and simplied These is the typ e of pairs whose rst comp onentisaJava ob ject separately compiled MIL terms are then linked together into representing a graphics context and whose second comp o one large MIL term which is extensively transformed b efore nentisanMLinteger An imp ortant subtlety is that whilst b eing translated into our lowlevel Basic Blo ckCodeBBC in Java all pointer reference typ es implicitly include the see section Finally the backend turns the BBC into a echose to make their quoted Javatyp e sp ecial value nullw collection of compiled Java class les whichby default are names in ML refer only to the nonnull values Where a placed in a single zip archive value of Java reference typ e t may b e passed from Javato This wholeprogram approach to compilation is unusual ML co de as the result of an external eld access or metho d though not unique It increases recompilation times call or as a parameter to a metho d implemented in ML considerably but do es allow us easily to pro duce much faster then that value is given the ML typ e t option with the and just as imp ortantly for applets smaller co de We value NONE corresp onding to Java null and values of the monomorphise the whole program and can p erform trans form SOMEv corresp onding to nonnull values Similarly formations suc h as inlining deadco de elimination arity we guarantee that anyMLvalue of typ e t option will b e raising and knownfunction call optimisation with no regard represented by the an element of the underlying Java class for mo dule b oundaries so there is no runtime cost asso ciated This complication allows the typ e system to catch statically with use of the mo dule system what would otherwise b e dynamic NullPointerExceptions App endix A contains an example of JVM co de generated and also gives the compiler more freedom in cho osing and byMLJ sharing data representations see Section The builtin structure Java includes ML synonyms for Compilation environment common Jav atyp es and co ercions b etween many equivalent MLJ can b e run entirely in batchmodeorasaninteractive ML and Javatyp es for example JavatoString which con recompilation environment Toplevel structures and signa verts a Java String into an ML string With one exception tures are stored onep erle as in Moscow ML MLJ fromWord these all generate no actual byteco des and are do esnt implement functors All compilation is driven by included just to separate the twotyp e systems securely in the need to pro duce one or more named Java class les for the source an application this is usually just the class containing the MLJ co de can p erform basic Java op erations such as eld main metho d whilst for an applet it is usually a sub class access metho d invo cation and ob ject creation by using a of javaawtApplet Once these ro ot classes and their ex collection of new keywords all of which start with an under p orted names have b een sp ecied the make command com score which ts well with the existing lexical structure piles links and optimises the required structures using

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us