Implementing Higher-Level Languages Quick tour of programming language implementation techniques. From the Java level to the C level.
Ahead-of-time compiler compile time C source x86 assembly x86 machine x86 C compiler code code code assembler x86 machine run time code Output x86 computer Data Figures for compilers/runtime systems adapted from slides by Steve Freund. 2
Typical Compiler Source Lexical Analyzer Program Syntax Analyzer Analysis Semantic Analyzer Intermediate Code Synthesis Generator Code Optimizer Code Generator Target Program 3
Interpreter Source Program Interpreter = virtual machine Output Data 4
Compilers... that target interpreters Java source Java Java Compiler code bytecod e Java bytecode Java Output Virtual Machine Data 6
Interpreters... that use compilers. Source Compiler Program Target Program Virtual Output Machine Data 8
JIT Compilers and Optimization Java source just-in-time code compiler x86 machine bytecode javac code Output Performance Java Monitor bytecode bytecode • HotSpot JVM interpreter • Jikes RVM Data • SpiderMonkey JVM • v8 • Transmeta • ... 9
Data in Java Arrays Every element initialized to 0 or null Immutable length field Since it has this info, what can it do? int array[5]: C ?? ?? ?? ?? ?? 0 4 20 24 Java 5 00 00 00 00 00 Data Representation in Java
Data in Java Arrays Every element initialized to 0 or null Immutable length field Bounds-check every access. Bounds-checking sounds slow, but: int array[5]: 1. Length is likely in cache. 2. Compiler may store length in register for loops. C ?? ?? ?? ?? ?? 3. Compiler may prove that some checks 0 4 20 24 are redundant. Java 5 00 00 00 00 00 Data Representation in Java
Data in Java Characters and strings 16-bit Unicode Explicit length, no null terminator the string ‘CS 240’: C: ASCII 43 53 20 32 34 30 \0 16 7 0 1 4 Java: Unicode 6 00 43 00 53 00 20 00 32 00 34 00 30 Data Representation in Java
Data structures (objects) in Java C: programmer controls layout, inline vs. pointer. Java: objects always stored by reference, never stored inline. C Java struct rec { class Rec { int i; int i; int a[3]; int[] a = new int[3]; struct rec *p; Rec p; }; … } struct rec *r = malloc(...); r = new Rec(); struct rec r2; r2 = new Rec(); r->i = val; r.i = val; r->a[2] = val; r.a[2] = val; r->p = &r2; r.p = r2; i a p i a p 3 int[3] 16 24 16 0 4 0 4 8 0 4 16 Data Representation in Java
Pointers/References Pointers in C can point to any memory address References in Java can only point to [the starts of] objects And can only be dereferenced to access a field or element of that object Java class Rec { C struct rec { int i; int i; int[] a = new int[3]; int a[3]; Rec p; struct rec *p; } }; Rec r = new Rec(); struct rec* r = malloc(…); some_fn(r.a, 1) // ref, index some_fn(&(r.a[1])) //ptr r r x i a p i a p 3 int[3] 16 24 0 4 8 16 0 4 0 4 16 Data Representation in Java
Java objects fields class Point { int x; int y; constructor Point() { x = 0; y = 0; } methods boolean samePlace(Point p) { return (x == p.x) && (y == p.y); } String toString() { return "(" + x + "," + y + ")"; } } 21
Java objects code:Point() Point object code:Point.samePlace() Point class vtable p vtable pointer code:Point.toString() x y Point object vtable pointer q x y For each class, compiler maps: field signature à offset (index) vtable pointer : points to per-class virtual method table (vtable) For each class, compiler maps: method signature à index samePlace: 0 toString: 1 23
Implementing dynamic dispatch code:Point() Point object code:Point.samePlace() Point class vtable p vtable pointer code:Point.toString() x y Point object vtable pointer q x q y what happens (pseudo code): Java: Point* p = calloc(1,sizeof(Point)); p->header = ...; Point p = new Point(); p->vtable = Point_vtable; Point_constructor(p); return p->vtable[0](this=p, q); return p.samePlace(q);
Subclassing class ColorPoint extends 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 extensions? new field new method overriding method
dynamic (method) dispatch Java: what happens (pseudo code): Point p = ???; return p.vtable[1](p); return p.toString(); Point vtable Point object vtable code:Point.samePlace() x y code:Point.toString() ColorPoint object ColorPoint vtable vtable x code:ColorPoint.toString() y color code:ColorPoint.getColor()
Recommend
More recommend