Object Oriented Language Implementation
Lectures 23-24
- For object oriented language compilation there are two stages
- Bootstrapping stage
- Retargeting stage
- Bootstrapping is the notion of implementing a language, by means of using (part of) that language’s own or some other language’s implementation.
- Many compilers undergo through a long chain of bootstrapping.
- There are three languages involved in writing a compiler
- Source Language (S)
- Target Language (T)
- Implementation Language (I)
- It converts OOPL to Non - OOPL
- It converts Non-OOPL to byte code
- There are two types of compilers
- Compiler: Generates code of machine on which it runs
- Cross-Compiler: Runs on one machine but generates ocde of another machine. e.g. Javac
- 2 steps for retargeting are
- Analysis: Non OOPL to intermediate representation
- Synthesis : intermediate representation to Pcode (byte code)
- What is difference between CISC and RISC ?
- We need some structures for book keeping
- per class structure - for all static data
- per object structure - contain all non static relationship
Class Abc {
int a;
A obja;
static int sa;
static double sd;
Abc();
Abc(int);
void Abc();
void Abc(double); }
- all static data - two fields (int, double)
- structure to hold a class object - (class name, data field names, method names, pointer to superclass)
- array of function pointers : static functions and non static functions
- Whenever first call to the class will be made the above structures will be filled.
- all non static data - (int, A)
- Handling different features
- Name mangling : Function is appended with types of function parameters
- e.g. Abc@I_v , Abc@D_v. When Abc(10) is called Abc@I_v will be invoked.
- In java, constructor and method have same semantics. Difference is that constructor has no return type and it is called with only “new” keyword.
- Constructor is prefixed with <init> in the class file
e.g. Abc() is written as <init>_Abc, Abc(int) -> <init>_Abc@I
- Constructors can also be overloaded.
- return type of a constructor changes to object structure
e.g. Abc() -> Abc@obj * <init>_Abc
Abc(int) -> Abc@obj * <init>_Abc@I
Abc(Abc) -> Abc@obj * <init>_Abc@Abc
- For static methods along with name mangling parameter list will have one addition. (Abc@class,<list of other arguments>)
- For non static method (Abc@obj *,<list of other arguments>)
e.g. Additions in constructor’s body
Abc() {
Abc@obj temp;
temp = (Abc@obj) malloc (sizeof(Abc@obj)) ;
// Based on super class call will be made to super //constructor
return temp;
}
- Whatever done till now is true for any ADT. For genericity we require template definition, template instantiation, template specialisation.
e.g. sort(<T> Array[], Int size) used as
sort(Int Array[], Int size), sort(Double Array[], Int size)
- Before compilation starts it will do template instantiation, T will be replaced by Int and Double. After name mangling they will work as overloaded functions.
- Template specialisation: Its job is to give a specific function for which template definition will not work e.g. for sorting array of strings it will gicve its own specific function.
- Everything in Object orientation is about dynamic casting which must be implicit for reusability.
Class Shape {
int dist;
Point center;
static int sa;
static double sd;
Shape();
Shape(int);
void Shape();
void Shape(double);
}
Shape s;
Circle c = new Circle();
s=c;
Circle c1 = (Circle) s;
- Now a per class structure <Shape@clas> is created (Shape, array of function pointer, pointer to superclass).Also a per object structure <Shape@obj> is created (int,Point, pointer to class structure) and c,s and c1 point to it.
- Polymorphism
- At the time of loading of class array of function pointers is filled with the address of methods that represent non static methods for polymorphism.
- If any pointer is null then it cannot instantiate object. Array of function pointers is per class and same for all objects of that class.
- For message dispatch, directly go to the function address. For RTTI we need to look at the class name present in the per class structure, if it does not match then go the super class and check.
- Space overhead: One address extra per object that points to class structure
- Time overhead: For every message dispatch one extra indirection to get address of function. Optimisations to reduce this overhead are available like java hotspots.