Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java as a Metalinguistic Framework Vijay Saraswat June 2002.

Similar presentations

Presentation on theme: "Java as a Metalinguistic Framework Vijay Saraswat June 2002."— Presentation transcript:

1 Java as a Metalinguistic Framework Vijay Saraswat June 2002

2 Outline (20 min) (30 min) (10 min) The Java language, and the JVM New language concepts, and their implementation Questions

3 Java Class-based Single inheritance Members: Fields, Constructors, Methods public/private/protected/abstract/fin al/native/static 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 If…then…else, switch for.., while.., do.., continue,break Typed exceptions try…catch, try…finally 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

4 Java compilation – the JVM 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 ClassFile { u4 magic u2 minor_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1] u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count] u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[method_count]; u2 attributes_count; attribute_info attributes[attributes_count] }

5 Java 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

6 Java compilation Compiler Parses Type checks Code generates Dynamic profiling, JIT optimization Dynamic code rewriting Class libraries Garbage collection, object finalization

7 Java 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.

8 Some 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) Making the future safe for the past: Adding Genericity to the Java Programming Language Bracha, Odersky, Stoutamire, Wadler class LinkedList implements Collection { protected class Node { A elt; Node next = null; Node (A elt) { this.elt = elt;} public LinkedList() {} public void add(A elt) { if (head == null) { head=new Node(elt); tail = head;} else { Node(elt); tail =;}} }… }

9 Some 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 unit class Table { Hashtable table; public void add(Session s) { table.put(s.getKey()); }… } public class Forwarder { S target; public Forwarder(S target) { = target;} public void revoke() { = null;} delegate target; }

10 Information 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. L={o1:r1,r2;o2:r2,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) =< l(e). L1 =< 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

11 Integrating 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 -> 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), X=Y Concurrent Constraint Programming, Saraswat, 1993

12 Programming in cc(Herbrand) append(X,Y, Z) :: X=_A._X1 -> Z1^(Z=A.Z1, append(X1, Y, Z1)) X = [] -> Z=Y. nrev(X, Y) :: X = _A._X1 -> Z^(nrev(X1, Z), append(Z, [A], Y)) point(In, X, Y, Color) :: In=move(_dX, _dY).In1 -> point(In, X+dX, Y+dY), In=get(_X1, _Y1).In1 -> X=X1, Y=Y1, point(In1, X, Y, Color).

13 Jcc – 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 -> Bound Unrealized -> Realized Bound -> 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.

14 Append 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, second )); }

15 Code in jcc Allow realized annotations on methods, parameters. Call suspends until the corresponding object is realized. Add when … catch and when…finally constructs. Implement by compiling into scheme on previous slide. public realized List reverse( final List remainder) { return this.isNull() ? remainder : List( this.head)); } public realized List append( List second ) { return this.isNull() ? second : new List( this.head, second )); }

16 Extends 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

17 Programming in jhcc public class Furnace implements Plant { const Real heatR, coolR, initTemp; public readOnly Fluent temp; public inputOnly Fluent 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.plant=p;} … } public class ControlledFurnace { Controller c; Furnace f; public ControlledFurnace(Furnace f, Controller c) { this.c = c; this.f = f;} public void run() {; c.setPlant(f);; }

18 M – 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) (Mark Miller et al)

19 Mass Computing People 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

20 Java as a metalinguistic framework Powerful framework Supports quick experimentation with different language features

Download ppt "Java as a Metalinguistic Framework Vijay Saraswat June 2002."

Similar presentations

Ads by Google