Title: Java as a Metalinguistic Framework
1Java as a Metalinguistic Framework
- Vijay Saraswat
- vijay_at_saraswat.org
- June 2002
2Outline
- (20 min)
- (30 min)
- (10 min)
- The Java language, and the JVM
- New language concepts, and their implementation
- Questions
3Java
- Class-based
- Single inheritance
- Members Fields, Constructors, Methods
- public/private/protected/abstract/final/native/sta
tic - Methods may be overloaded
- Interfaces provide signatures
- No delegation
- Java provides capabilities
- Type-security
- State-encapsulation
- Final methods
- Unforgeable references
- Built-in garbage collection
- Arrays
- Control structures
- assignment
- Ifthenelse, switch
- for.., while.., do.., continue,break
- Typed exceptions
- trycatch, tryfinally
- throw
- Method calls, return
- Type hierarchy
- Singly rooted
- Separate built-in types
- Extensive libraries, with native state
- No generics
- Threads, synchronization
- Dynamic Linking Contexts
- User-defined class loaders
- Allows container-based computing
The Java Language Specification, 2d ed Gosling,
Joy, Steele, Bracha 2000
4Java compilation the JVM
ClassFile u4 magic u2 minor_version u2
constant_pool_count cp_info constant_poolconstan
t_pool_count-1 u2 access_flags u2
this_class u2 super_class u2 interfaces_count u
2 interfacesinterfaces_count u2
fields_count field_info fieldsfields_count u2
methods_count method_info methodsmethod_count
u2 attributes_count attribute_info
attributesattributes_count
- Stack-oriented machine, with fixed stack-frame
size. - Typed byte codes
- Objects are heap-allocated.
- Classes
- Verification
- Preparation
- Resolution
- Ported extensively
- Windows, Sparc, IBM mainframes, cell phones/PDAs
(KVM)
The Java Virtual Machine Specification, 2d ed,
Lindholm and Yellin, 1999
5Java Virtual Machine Byte codes (enumeration)
- Load and store instructions
- Move data between local variables and op stack.
- Arithmetic instructions
- Add, subtract, mult, divide, remainder, negate,
shift, bitwise ops, local variable inc,
comparisons - Type conversion instructions
- Object creation and manipulation
- Create, access fields of classes and instances,
load/store arrays to operand stack, casting - Operand stack management
- Push/pop etc
- Control transfer
- (un)conditional branches
- Method invocation and return
- Virtual, interface, special, static
- Synchronization
- Monitorexit, monitorenter
6Java compilation
- Compiler
- Parses
- Type checks
- Code generates
- Dynamic profiling, JIT optimization
- Dynamic code rewriting
- Class libraries
- Garbage collection, object finalization
7Java as a metalinguistic framework
- Basic idea
- Reuse the language framework
- Syntax, Compiler, runtime system
- JL Tools (Myers)
- Jikes
- Pizza/GJ compiler
- Implement the new features through
- static reasoning (source code, byte code)
- source-to-source transformation
- Modifications to the root object.
- disallowing use of (certain) features/libraries.
- Advantages
- Time to market -- keeps the cost of developing a
new language low! - Incrementally replace components (e.g. compiler,
run-time system, virtual machine) - Disadvantage
- Suitable only for Java-like languages
- But see Microsoft C and the Common Runtime
System.
8Some simple examples Generic types
- Generic types
- Introduce parameters into class and method
definitions - Better than programming at the top-level type
- Implementation
- Compiles to unchanged JVM.
- Uses type-erasures, inserts typecasts (which are
guaranteed to succed)
class LinkedListltAgt implements CollectionltAgt
protected class Node A elt Node next
null Node (A elt) this.elt elt public
LinkedList() public void add(A elt) if
(head null) headnew Node(elt)
tail head else
tail.nextnew Node(elt) tail tail.next
Making the future safe for the past Adding
Genericity to the Java Programming Language
Bracha, Odersky, Stoutamire, Wadler
9Some object-oriented techniques
- Get rid of statics replace by unit classes
- Implement by compiling into a normal class with
a single static field.
- Delegates
- Implemented by source-to-source compilation
public class Forwarder ltSgt S target public
Forwarder(S target) this.target
target public void revoke() this.target
null delegate target
public unit class Table ltSession implements Keygt
Hashtable ltSessiongt table public void
add(Session s) table.put(s.getKey())
10Information Flows extended type-checking
- Key idea Static checking of flow annotations
- Data values are labeled with security policies.
Each policy has an owner (security principal) and
a set of readers (other principals). E.g.
Lo1r1,r2o2r2,r3 - Programmer annotates variables x with labels l(x)
statically.
- Compiler flows labels, computing a label l(e)
for each RHS e in an assignment x e - An assignment x is label-safe iff l(x) lt l(e).
- L1 lt L2 def for every policy in L1 there is at
least one policy in L2 that is at least as
restrictive. - Operations cause joins of labels to be taken.
- Compiler label-checks program, and outputs Java
(stripped of labels).
JFlow Practical mostly-static information flow
control, Myers, POPL 99
11Integrating Concurrent Constraint Programming
- Motivation
- Threads, synchronization, locking are poor
programming concepts - Witness the complexity of the memory model for
Java! - Alternative
- event-loops
- promises
- Introduce (logic) variables
- Computation progresses by imposing and checking
constraints
- Agent combinators (A)
- c
- c -gt A
- A, A
- X A
- p(t1, , tn)
- Program is a collection of definitions
- p(X1,,Xn)A
- Constraint system Herbrand
- X f(t1, , tn), XY
Concurrent Constraint Programming, Saraswat,
1993
12Programming in cc(Herbrand)
append(X,Y, Z) X_A._X1 -gt Z1(ZA.Z1,
append(X1, Y, Z1)) X -gt ZY. nrev(X, Y)
X _A._X1 -gt Z(nrev(X1, Z), append(Z, A,
Y)) point(In, X, Y, Color) Inmove(_dX,
_dY).In1 -gt point(In, XdX, YdY),
Inget(_X1, _Y1).In1 -gt XX1, YY1, point(In1, X,
Y, Color).
13Jcc CCP in Java
- Introduce promises ( typed logic variables)
just an instance of the given type. - Interface Promise basic implementation
BasicPromise - A BasicPromise is in one of 4 states
- Unrealized and unwatched
- Unrealized and watched
- Bound
- Realized
- Promises may be equated to each other
- Objects are created as (top-level) constants
(realized), or as (unrealized and unwatched)
promises - As a result of equatings, object change state
- Unrealized -gt Bound
- Unrealized -gt Realized
- Bound -gt Realized
- Any suspended computations are run when a promise
is realized. - Method invoked on object o is always delegated to
the object obtained by dereferencing o.
14Append in Java, using promises
public List append( final List second )
List me (List) this.dereference()
if ( me.isUnrealized())
final List promise new List()
me.runWhenRealizedDerefed(new Runnable()
public void run()
promise.equate(
List.this.append( second ) )
)
return promise return
me.appendRealized( second)
private List appendRealized( List
second ) return this.isNull() ?
second new List( this.head,
this.rest.append( second ))
15Code in jcc
public realized List reverse( final List
remainder) return this.isNull() ?
remainder this.rest.reverse()
.append(new List( this.head)) public
realized List append( List second )
return this.isNull() ? second new List(
this.head,
this.rest.append( second ))
- Allow realized annotations on methods,
parameters. - Call suspends until the corresponding object is
realized. - Add when catch and whenfinally constructs.
- Implement by compiling into scheme on previous
slide.
16Extends to Hybrid CC
- Timed CCP obtained from CCP by extending
relations through time - Default CCP obtained by allowing detection of
absence of information. - Hybrid CCP obtained by moving to continuous time.
- These languages allow a rich plethora of
combinators to be defined within the language - do watching
- do until
- Jhcc introduces these control constructs into
jcc.. - Implementation via compilation to JVMs
Timed Default Concurrent Constraint
Programming, Saraswat et al1996 Computing with
continuous change, Gupta et al 1998
17Programming in jhcc
public class Furnace implements Plant const
Real heatR, coolR, initTemp public readOnly
FluentltRealgt temp public inputOnly FluentltBoolgt
switchOn public Furnace(Real heatR, Real coolR,
Real initT ) this.heatR heatR this.coolR
coolR this.initTemp initT public
void run() temp initTemp, time always
dot(temp)heatR on switchOn time always
dot(temp)-coolR on switchOn
public class Controller Plant plant public
void setPlant(Plant p) this.plantp public
class ControlledFurnace Controller c Furnace
f public ControlledFurnace(Furnace f,
Controller c)
this.c c this.f f public void run()
c.run() c.setPlant(f) f.run()
18M the basis for Matrix
- E
- Pure capability-based
- Distributed language
- Vats and Event loops
- Promise-based architecture
- Current approach
- Interpreter in Java
- M
- Jcc inter-JVM communication (SMI crypto)
http//www.erights.org (Mark Miller et al)
19Mass ComputingPeople interacting in virtual
worlds
- Networked
- Users can connect from anywhere, anytime
(wireless, wire-line) using appropriate clients
(graphical, voice, web-based). - Persistent
- World state persists through connections,
incarnations. - Objects are meta-mutable.
- Secure
- Users are credentialed, authenticated.
- No user can bring down a world.
- Interactive
- World is populated with stateful objects with
which you interact room, table, chairs,
cabinets, assignments, labyrinths, trolls,
games, puzzles, assignments, museums, simulations
- Extensible
- You can create your own objects
- Resource-sensitive
- Users have quotas, own objs
- Federated
- Multiple worlds in a federation
- Objects may migrate
20Java as a metalinguistic framework
- Powerful framework
- Supports quick experimentation with different
language features