Ka w a Other languages on JVM Compiling Dynamic Languages - - PowerPoint PPT Presentation

ka w a other languages on jvm compiling dynamic languages
SMART_READER_LITE
LIVE PREVIEW

Ka w a Other languages on JVM Compiling Dynamic Languages - - PowerPoint PPT Presentation

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 dieren t things Ja v a as a programming language P er Bothner ob jectorien ted


slide-1
SLIDE 1 Ka w a Compiling Dynamic Languages to the Ja v a VM P er Bothner bothnercygnusc
  • m
  • Cygn
us Solutions UsenixF r e enix
  • Dr
aft version June
  • Other
languages
  • n
JVM Ja v a can b e though t
  • f
as t w
  • dieren
t things Ja v a as a programming language
  • b
jectorien ted syn tax similar to CC Ja v a as a mac hineen vironmen t libraries
  • p
  • rtable
b yteco des run virtual mac hine Need for
  • ther
languages to coexist in the Ja v a en vironmen t Highlev el scripting languages esp ecially useful Can use extensiv e Ja v a libraries and p
  • rtable
b yteco des Benet from Ja v a
  • ptimization
eorts Man y examples NetRexx Tcl Ada
  • June
  • Implemen
tation Strategies In terpreter written in Ja v a Go
  • d
resp
  • nsiv
eness Slo w execution time T ranslate to Ja v a source P
  • r
resp
  • nsiv
eness F ast execution time T ranslate to Ja v a b yteco de Go
  • d
resp
  • nsiv
eness F ast execution time Ka w a do es the last T ranslating Sc heme directly to b yteco des is m uc h faster Required for in teractiv e resp
  • nse
in readev alprin t lo
  • p
Byteco des are more general b yteco des ha v e goto whic h is not in Ja v a language June
  • Ev
al
  • Immediate
execution Scripting languages ha v e eval T ak e a command t yp ed in teractiv ely
  • r
constructed
  • n
the y
  • and
immediately execute it A batc h translator do es not supp
  • rt
eval w ell Ka w a compiles directly to inmemory b yteco des so is highly resp
  • nsiv
e Uses a simple in terpreter for simple expressions for ev en faster resp
  • nse
June
slide-2
SLIDE 2 Compilation F ron tend Sc heme source
  • read
  • list
form
  • analysis
  • R
ECMAScript
  • lexer
  • tok
en stream
  • parser
  • Expression
June
  • Compilation
Bac kend Expression
  • R
  • ptimization
  • co
de generation
  • ClassType
  • R
  • writeToArray
  • in
ternal arra y
  • writeToFile
  • class
le
  • R
  • R
zip arc hiv e
  • ClassLoader
  • Running
co de June
  • Loading
a Sc heme source le Read expressions un til EOF
  • normally
yields a sequence
  • f
PairWithPosition lists W rap expressions in a dumm y noargumen t lambda The analysis phase do es macro expansion resolv es lexical bindings Could do
  • ptimizations
Result is a LambdaExp expression
  • b
ject Compile to in ternal b yte arra ys con taining b yteco des and class denitions Uses the same format as Ja v a class les Use a ClassLoader to con v ert b yte arra ys to loaded classes Apply the result whic h ev aluates toplev el expressions June
  • Expressions
Abstract Expression class represen ts languageindep enden t partially pro cessed expressions IfExp
  • conditional
expression ApplyExp
  • application
call LambdaExp
  • anon
ymous pro cedures LetExp
  • blo
c k with lexical bindings QuoteExp
  • literal
constan t ReferenceExp
  • v
ariable reference ErrorExp
  • syn
tax error seen
  • compile
metho d compiles the expression in to b yteco de instructions eval is
  • nly
used to ev aluate simple expressions in teractiv ely
  • June
slide-3
SLIDE 3 Co de Generation public abstract class Expression
  • public
abstract void compileCompilatio n state Target target
  • A
Compilation manages the state for a compilation unit and manages
  • ne
  • r
more gnubytecodeClass Ty pe
  • b
jects
  • ne
for eac h generated class le T
  • compile
an Expression in v
  • k
e its compile metho d This generates b yteco des to ev aluate the Expression Calls compile recursiv ely for subexpressions A Target sp ecies where to lea v e the result June
  • The
bytecode P ac k age The gnubytecode pac k age is an ecien t in termediatelev el library for w
  • rking
with Ja v a class les Co de generation reading writing prin ting disassem bling ClassType
  • Information
ab
  • ut
a class as a whole CpoolEntry abstract
  • A
slot in the constan t p
  • l
Variable
  • Lo
cal v ariable Field Attribute abstract
  • Used
for miscellaneous info Method
  • Handles
metho ds and b yteco de instructions CodeAttr
  • A
Methods b yteco de instructions
  • CodeAttr
has man y mediumlev el metho ds for generating b yteco de instructions F
  • r
example c
  • de
attremitPushInti Generates co de to push int literal i
  • n
JVM stac k June
  • Calling
Ja v a metho ds Ka w a mak es it con v enien t to call Ja v a metho ds form Sc heme define charupcase char primitivestati cm et hod javalangCharac te r toUpperCase char char char Con v erts Sc heme c haracter v alue to Ja v a primitiv e char calls toUpperCase metho d in javalangCharac ter
  • and
con v erts result bac k Compiler can inline call to kno wn primitiv e metho d Otherwise Ja v a reection feature is used Similar access to arra y elemen ts and
  • b
ject elds June
  • Sc
heme vs Ja v a Sc heme and Ja v a are v ery dieren t kinds
  • f
languages Sc heme is dynamically t yp ed while Ja v a is statically t yp ed Ja v a is an
  • b
jectorien ted language Sc heme is a nonpure functional language Pro cedures are rstclass
  • b
jects lexical scoping requires closures Sc heme denes arithmetic
  • n
a tree
  • f
n um b er t yp es Ja v a normally uses ra w mac hinelev el n um b ers Ho w should w e map Sc heme constructs in to Ja v a constructs June
slide-4
SLIDE 4 Dynamic T yp es Ja v a lik e Smalltalk and unlik e C has a ro
  • ted
inheritance graph All classes are deriv ed from the class Object whic h is an alias for javalangObject Sc heme and ECMAScript ha v e v ariable declarations but without t yp e sp ecications hence all Sc heme v alues b elong to some subclass
  • f
Object Some latitude when to use standard builtin Ja v a classes for Sc heme v alues
  • r
write
  • ur
  • wn
June
  • Ob
jects and V alues Sc heme b
  • leans
are represen ted using Ja v a Booleans Sym b
  • ls
are represen ted using in terned Ja v a Strings Sc heme lists v ectors and strings use new classes in a Sequence colections hierarc h y
  • June
  • Sequences
abstract class Sequence class Vector extends Sequence Used for Sc heme v ectors class List extends Sequence ListEmpty
  • new
List
  • Empty
list class Pair extends List Has car and cdr elds class PairWithPosition extends Pair A Pair with linen um b er information June
  • Num
b ers gnumath pac k age implemen ts Sc heme n um b ers and more F
  • rms
a coheren t class hierarc h y
  • Pro
vides inniteprecision rationals Complex n um b ers pro vided Has quan tities with units and dimensions June
slide-5
SLIDE 5 Pro cedures public abstract class Procedure
  • public
abstract Object applyNObject args
  • A
primitiv e pro cedure is written in Ja v a as a subclass
  • f
Procedure Eg
  • denes
applyN to add argumen ts A Sc heme function is compiled in to a sub class
  • f
Procedure with the Sc heme b
  • dy
compiled in to b
  • dy
  • f
applyN Allo cating instance
  • f
a Procedure subclass creates Sc heme pro cedure v alue Nested pro cedure has a eld for the static con text inherited lexical en vironmen t A closure is an instance
  • f
suc h a class June
  • T
ailcalls Sc heme language requires tailcalleliminati
  • n
whic h is a generalization
  • f
tailrecursionelimination CALL F follo w ed b y RETURN should b e same as GOTO F Ie curren t frame m ust b e p
  • pp
ed b efore CALL to a v
  • id
stac k gro wth Allo ws iteration and state mac hines to b e expressed using recursion Best handled with suitable calling con v en tion whic h is not p
  • rtable
in Ja v a Ka w a implemen ts tailrecursionelimination
  • nly
If compiler sees a call to the curren t function it replaces it b y a goto General tailcalleliminatio n will b e implemen ted using a new heapbased calling con v en tion can coexist with the curren t faster calling con v en tion June
  • Con
tin uations Continuations pro vide a w a y to capture the curren t stac k en vironmen t They can b e used to implemen t new con trol structures coroutines bac ktrac king exception handling and more Can b e implemen ted b y cop ying the stac k
  • but
this requires assem blylev el co de Ka w a implemen ts limited con tin uations sucien t to implemen t catc hthro w and exception handling It uses the Ja v a exception handling mec hanism F ull supp
  • rt
will b e added based
  • n
the new tailcall con v en tion June
  • Other
languages Most
  • f
the Ka w a is indep enden t
  • f
Sc heme The same tec hniques and most
  • f
the co de could b e reused to implemen t
  • ther
languages esp ecially dynamically t yp ed
  • nes
suc h as Tcl Rexx Smalltalk APL
  • New
languages ma y require new data t yp es
  • just
write the appropriate Ja v a classes Most languages require their
  • wn
parser This w
  • uld
translate text in to Expressions Eac h language has a dieren t standard en vironmen t con taining predened v alues t yp es and functions June
slide-6
SLIDE 6 ECMAScript ECMA standard no
  • for
the core
  • f
Ja v aScript V ery dynamic
  • b
jectbased language with protot yp e inheritance but no classes Lexer and parser written Most predened functions and
  • b
jects missing Generates Expression and compiles to Ja v a b yteco des June
  • DSSSL
and SGML Industry migrating to SGML for do cumen t storage and manipulation HTML is an instance
  • f
SGML XML is simplied SGML DSSSL is the ISO standard language for sp ecifying st yle and pro cessing
  • f
SGML DSSSL is basically standard Sc heme with no sideeects plus some extensions plus lots
  • f
standard classes for manipulating text
  • w
etc The
  • riginal
preCygn us Ka w a w as written to pro cess DSSSL Ka w a implemen ts some
  • f
the DSSSL extensions DSSSL seems b e the cause
  • f
renew ed in terest in Sc heme and Ka w a June
  • Status
and F uture w
  • rk
Ka w a is used for a n um b er
  • f
commercial and academic pro jects and has a mem b er mailing list Ka w a is still b eing activ ely dev elop ed and enhanced Plans include Implemen t missing features
  • f
standard Sc heme R
  • RS
Finish ECMAScript implemen tation Implemen t Emacs Lisp core Pla y with an arra y language Design a graphics in terface Add a mo dule system Supp
  • rt
  • ptional
t yp e declarations and lo cal t yp e inference Supp
  • rt
using ra w Ja v a data t yp es in Sc heme
  • June
  • Fine
prin t Not a Cygn us pro duct No connection with T ekT
  • ls
Ja v a IDE
  • f
the same name Home page httpwwwcygnu s com
  • bot
hn er ka wa ht ml June