Ka w a Other languages on JVM Compiling Dynamic Languages to the Ja v a VM Ja v a can b e though t of as t w o di�eren t things� Ja v a as a programming language P er Bothner �ob ject�orien ted� syn tax similar to C�C��� �bothner�cygnus�c om � Ja v a as a mac hine�en vironmen t �libraries � p ortable b yte�co des run virtual mac hine� Cygn us Solutions Need for other languages to co�exist in the Ja v a en vironmen t� High�lev el �scripting� languages esp ecially useful� Can use extensiv e Ja v a libraries� and p ortable b yteco des� Bene�t from Ja v a optimization e�orts� Usenix�F r e enix ���� Man y examples� NetRexx� Tcl� Ada� ��� �Dr aft version� June� ���� � June� ���� � Implemen tation Strategies In terpreter written in Ja v a� Ev al � Immediate execution Go o d resp onsiv eness� Slo w execution time� Scripting languages ha v e eval � T ranslate to Ja v a source� T ak e a command t yp ed in teractiv ely or constructed on the �y � and P o or resp onsiv eness� immediately execute it� F ast execution time� A �batc h� translator do es not supp ort w ell� eval T ranslate to Ja v a b yteco de� Go o d resp onsiv eness� Ka w a compiles directly to in�memory b yteco des� so is highly F ast execution time� resp onsiv e� Ka w a do es the last� Uses a simple in terpreter for �simple� expressions for ev en T ranslating Sc heme directly to b yteco des is m uc h faster� faster resp onse� Required for in teractiv e resp onse �in read�ev al�prin t lo op�� Byteco des are more general �b yteco des ha v e goto � whic h is not in Ja v a language�� June� ���� � June� ���� �
Compilation F ron t�end Compilation Bac k�end � � � � ECMAScript Expression ClassType Sc heme source co de generation � � � � � � � � R � � � R � � � � � � � � � � � optimization �writeToArray read lexer �writeToFile � � � � � � � � � � � � � � � R � � �zip arc hiv e in ternal arra y list form tok en stream �class �le � � � � � R � � � � � � � � parser analysis ClassLoader � � � � � � � � � R � � Expression Running co de June� ���� � June� ���� � Expressions Loading a Sc heme source �le Abstract Expression class represen ts �language�indep enden t� Read expressions un til EOF � normally yields a sequence of partially pro cessed� expressions� lists� PairWithPosition � conditional expression IfExp W rap expressions in a dumm y no�argumen t lambda � � application �call� ApplyExp The analysis phase do es macro expansion� resolv es lexical bindings� LambdaExp � anon ymous pro cedures �Could do optimizations�� � blo c k with lexical bindings LetExp Result is a LambdaExp expression ob ject� � literal �constan t� QuoteExp ReferenceExp � v ariable reference Compile to in ternal b yte arra ys con taining b yteco des and class � syn tax error seen ErrorExp de�nitions� ��� �Uses the same format as Ja v a �les�� �class compile metho d compiles the expression in to b yteco de Use a to con v ert b yte arra ys to loaded classes� ClassLoader instructions� Apply the result� whic h ev aluates top�lev el expressions� eval is only used to ev aluate simple expressions in teractiv ely � June� ���� � June� ���� �
The P ac k age bytecode Co de Generation The pac k age is an e�cien t in termediate�lev el library gnu�bytecode public abstract class Expression for w orking with Ja v a �class �les� � Co de generation� reading� writing� prin ting� disassem bling� public abstract void � Information ab out a class as a whole� ClassType compile�Compilatio n state� Target target�� CpoolEntry �abstract� � A slot in the constan t p o ol� ���� � Lo cal v ariable� Variable � Field A manages the state for a compilation unit� Compilation Attribute �abstract� � Used for miscellaneous info� and manages one or more gnu�bytecode�Class Ty pe ob jects� � Handles metho ds and b yte�co de instructions� Method one for eac h generated �le� �class CodeAttr � A Method �s b yteco de instructions� ��� T o compile an Expression � in v ok e its metho d� compile This generates b yteco des to ev aluate the Expression � has man y medium�lev el metho ds for generating b yteco de CodeAttr Calls compile recursiv ely for sub�expressions� instructions� F or example� c o de attr �emitPushInt� i �� A Target sp eci�es where to lea v e the result� Generates co de to push int literal i on JVM stac k� June� ���� � June� ���� �� Calling Ja v a metho ds Sc heme vs Ja v a Ka w a mak es it con v enien t to call Ja v a metho ds form Sc heme� �define �char�upcase char� Sc heme and Ja v a are v ery di�eren t kinds of languages� ��primitive�stati c�m et hod �java�lang�Charac te r� �toUpperCase� Sc heme is dynamically t yp ed� while Ja v a is statically t yp ed� �char� ��char��� Ja v a is an ob ject�orien ted language� char�� Sc heme is a �non�pure� functional language� Pro cedures are Con v erts Sc heme c haracter v alue to Ja v a primitiv e char � calls �rst�class ob jects� lexical scoping requires closures� toUpperCase metho d in java�lang�Charac ter � and con v erts Sc heme de�nes arithmetic on a tree of n um b er t yp es� Ja v a result bac k� normally uses ra w mac hine�lev el n um b ers� Compiler can inline call to kno wn primitiv e metho d� Ho w should w e map Sc heme constructs in to Ja v a constructs� Otherwise� Ja v a re�ection feature is used� Similar access to arra y elemen ts and ob ject �elds� June� ���� �� June� ���� ��
Dynamic T yp es Ja v a �lik e Smalltalk and unlik e C��� has a �ro oted� inheritance Ob jects and V alues graph� All classes are deriv ed from the class Object �whic h is an alias for java�lang�Object �� Sc heme b o oleans are represen ted using Ja v a Boolean s� Sc heme �and ECMAScript� ha v e v ariable declarations� but Sym b ols are represen ted using in terned Ja v a String s� without t yp e sp eci�cations� Sc heme lists� v ectors� and strings use new classes in a hence all Sc heme v alues b elong to some sub�class of Object � �colections� hierarc h y � Sequence Some latitude when to use standard �builtin� Ja v a classes for Sc heme v alues� or write our o wn� June� ���� �� June� ���� �� Sequences Num b ers abstract class Sequence class Vector extends Sequence gnu�math pac k age implemen ts Sc heme n um b ers and more� Used for Sc heme v ectors� F orms a coheren t class hierarc h y � class List extends Sequence Pro vides in�nite�precision rationals� List�Empty � new List ��� �� Empty list Complex n um b ers pro vided� class Pair extends List Has car and cdr �elds� Has quan tities� with units and dimensions� class PairWithPosition extends Pair A with line�n um b er information� Pair June� ���� �� June� ���� ��
Recommend
More recommend