Ahead- of- time Tpical Compiler compile time C source C 86 assembl 86 assembler 86 machine Source Leical Analzer Snta Analzer Semantic Analzer Analsis Intermediate Code Generator Snthesis run time 86 machine 86 computer Code Optimizer Code Generator Figures for s/runtime sstems adapted from slides b Steve Freund. 1 Target 2 Interpreter Compilers... that target interpreters Source source Compiler btecod e Interpreter = virtual machine bte Virtual Machine 3 5 1
Interpreters... that use s. JIT Compilers and Optimization Source Compiler Target Virtual Machine 7 source javac bte just-in-time bte Performance Monitor bte interpreter JVM 86 machine HotSpot JVM Jikes RVM SpiderMonke v8 Trans meta 8... Virtual Machine Model On translation, laout, and implementation We show natural, common, or conventional translations. Bte compile time run time High- Level Language Virtual Machine Language Ahead- of- time : No guarantee of this implementation/laout. Language is (mostl clean) abstraction. C: Much of implementation/laout guaranteed. Language eposes man machine details. Virtual machine (interpreter) JIT Native Machine Language 9 2
in Integers, floats, doubles, pointers same as C Null is tpicall represented as 0 Characters and strings Arras Objects pointers? called references much more constrained in Arras Ever element initialized to 0 or null Length specified in immutable field at start of arra (int 4 btes) arra.length returns value of this field Since it has this info, what can it do? int arra[5]: C?????????? 0 4 20 24 5 00 00 00 00 00 Representation in Representation in in Arras Ever element initialized to 0 or null Length specified in immutable field at start of arra (int 4 btes) arra.length returns value of this field Ever access triggers a bounds- check Code is added to ensure the inde is within bounds Eception if out- of- bounds int arra[5]: C?????????? 0 4 20 24 5 00 00 00 00 00 Bounds- checking sounds slow, but: 1. Length is likel in cache. 2. Compiler ma store length in register for loops. 3. Compiler ma prove that some checks are redundant. in Characters and strings Two- bte Uni instead of ASCII Represents most of the world s alphabets String not bounded b a \0 (null character) Bounded b hidden length field at beginning of string the string CS 240 : C: ASCII : Uni 43 53 20 32 34 30 \0 0 1 4 7 16 6 00 43 00 53 00 20 00 32 00 34 00 30 Representation in Representation in 3
structures (objects) in Objects are alwas stored b reference, never stored inline. Include comple data tpes (arras, other objects, etc.) using references Pointer/reference fields and variables In C, we have - > and. for field selection depending on whether we have a pointer to a struct or a struct (*r).a is so common it becomes r- >a C struct rec { int a[ 3]; struct rec *p; ; class Re c { int[] a = n ew int [3]; Rec p; In, all non- primitive variables are references to objects We alwas use r.a notation But reall follow reference to r with offset to a, just like C s r- >a struct rec *r = malloc(...); struct rec r2 ; r->i = val; r->a[2] = val ; r->p = & r2; r = new Rec() ; r2 = new Rec( ); r.i = va l; r.a[2] = val; r.p = r2 ; 0 4 16 20 Representation in 0 4 8 12 3 int[3] 0 4 16 Implementation Pointers/References Pointers in C can point to an memor address References in can onl point to [the starts of] objects And can onl be dereferenced to access a field or element of that object C struct rec { int a[ 3]; struct rec *p; ; struct rec* r = mal loc( ); some_fn(&(r.a[1])) //pt r r 0 4 16 20 class Re c { int[] a = n ew int [3]; Rec p; Rec r = new R ec(); some_fn( r.a, 1) // ref, inde r 0 4 8 12 Representation in 0 3 int[3] 4 16 Casting in C We can cast an pointer into an other pointer; just look at the same bits differentl struct BlockInfo { int size AndTa gs; struct BlockInfo* n et; struct BlockInfo* prev; ; tpedef struc t Bloc kinfo Block Info; int ; BlockInf o *b; BlockInf o *ne wblock ; newblock = (BlockInfo *) ( (ch ar *) b + ); s n p 0 4 8 12 s n Implementation p Cast b into char pointer so that ou can add bte offset without scaling Cast back into BlockInfo pointer so ou can use it as BlockInfo struct 4
Tpe- safe casting in objects Can onl cast compatible object references class Object { class Vehicle { int passengers; class Boat etends Vehicle { int propellers; class Car etends Vehicle { int wheels; // Vehicle is a super class of Boat and Car, which are siblings Vehicle v = new Vehicle(); Car c1 = new Car(); Boat b1 = new Boat(); Vehicle v1 = new Car(); // ok, everthing needed for Vehicle // is also in Car Vehicle v2 = v1; // ok, v1 is alread a Vehicle Car c2 = new Boat(); // incompatible tpe Boat and // Car are siblings Car c3 = new Vehicle(); // wrong direction; elements in Car // not in Vehicle (wheels) Boat b2 = (Boat) v; // run-time error; Vehicle does not contain // all elements in Boat (propellers) Car c4 = (Car) v2; // ok, v2 started out as Car Car c5 = (Car) b1; // incompatible tpes, b1 is Boat class Point { int ; int ; Point() { = 0; = 0; boolean sameplace(point p) { return ( == p.) && ( == p.); String tostring() { return "(" + + "," + + ")"; fields constructor methods How is this implemented / enforced? 20 objects : Poin t() Implementing dnamic dispatch : Poin t() p vtable pointer Point class vtable : Poin t.sa mepl ace() p vtable pointer Point class vtable : Poin t.sa mepl ace() : Poin t.to Stri ng() : Poin t.to Stri ng() vtable pointer q For each class, maps: field signature à offset (inde) vtable pointer : points to per- class virtual method table (vtable) For each class, maps: method signature à inde sameplace: 0 tostring: 1 22 vtable pointer q q what happens (pseudo ): : Point* p = calloc(1,sizeof(point)); Point p = new Point(); p->header =...; p->vtable = &Point_vtable; Point_constructor(p); return p.sameplace(q); return p.vtable[0](this=p, q); 5
Subclassing class ColorPoint etends Point{ String color; boolean getcolor() { return color; String tostring() { return super.tostring() + "[" + color + "]"; How do we access superclass pieces? fields inherited methods Where do we put etensions? new field new method overriding method dnamic (method) dispatch : Point p =???; return p.tostring(); vtable Color ColorPoint vtable vtable color what happens (pseudo ): return p.vtable[1](p); Point vtable : Poin t.sa mepl ace() : Poin t.to Stri ng() : Colo rpoi nt.t ostr ing() : Colo rpoi nt.g etco lor() 6