Download High Level Language, Code Generation - Lecture Slides | CMSC 430 and more Study notes Computer Science in PDF only on Docsity! Code generation High level languages • Java – stack code – allocate registers to top of stack • object-oriented – method invocation – member layout • functional – higher order functions – function calls Code generator generators • tree pattern matching • tree parsing • peephole CMSC 430 Lecture 12, Page 1 Compiling Java Class files • structure for describing program • machine-independent stream of bytes • verified when loaded Issues • stack reduces reordering • virtual methods reduce inlining • multiple threads limit transformations • verify bytecodes to ensure safety Converting into real code • analyze stack to determine size • represent stack as temporary variables • try to avoid excessive copying • allocate variables to registers CMSC 430 Lecture 12, Page 2 Compiling stack code General algorithm • determine local storage max locals + max stack + max temps • form basic blocks • find stack height for instruction • translate instructions Naive approach • map each local/stack location to a frame location • translate each instruction • move locations between memory and registers Register allocation approach • map top of stack, first locals to registers • fixed approach maps registers for entire method • basic block approach maps registers for basic blocks CMSC 430 Lecture 12, Page 3 Object-oriented (OO) languages Objects • a collection of data • functions (methods) for operating on data Classes • collection of objects with same attributes • organizes space of objects • allows shared implementation of objects Implementation • class record – pointers to methods (method table) – storage for class data • object record – pointer to class type (tag) – storage for local data • location → offset in object record/method table CMSC 430 Lecture 12, Page 4 Class hierarchy Inheritance • class may inherit data/methods from another class • ancestor class bestows attributes (superclass) • descendent class inherits attributes (subclass) • subclass should work wherever superclass is expected • subclass may override methods from superclass (dynamic methods) • multiple ancestors → multiple inheritance Impact • class of object not completely known at compile-time (since object of type subclass is allowed wherever class is allowed) • need to test tags at runtime • could result in non-constant data/method pointer offset Can we eliminate overhead of data/method lookups? CMSC 430 Lecture 12, Page 5 Data layout optimization Single inheritance • ensure constant offset for fields through prefixing • when class B inherits from class A – lay out fields of A at beginning of B in same order – place new fields of B afterwards • field accessed as constant offset from object record Multiple inheritance • ensure constant offset for fields • assign slots for field via graph coloring (may leave gaps between slots) • descriptor table – eliminate gaps through indirection – assign unique descriptor slot via coloring – descriptor stores offsets for field • field accessed as constant offset plus indirection CMSC 430 Lecture 12, Page 6 Method lookup optimization Single inheritance • arrange method tables entries via prefixing • override methods by overwriting slot • ensure constant offset for methods • method are executed through 1. fetch pointer to class record from object 2. get function pointer at offset in method table 3. invoke method through function pointer Multiple inheritance • assign slots via graph coloring • overwrite slots as needed Additional optimizations • type propagation to prove class type - convert method lookup into function call • inlining - merge code into call site, eliminates call overhead CMSC 430 Lecture 12, Page 7 Inheritance example class A extends Obj { int a; f1(); } class B extends A { int b,c; f2(); } A x; x.a = x.f1(); Code for random 1. check x’s pointer to class record 2. if (x→class == A) (a) call x→method[0] (b) assign value to x.field[0] 3. else if (x→class == B) (a) call x→method[1] (b) assign value to x.field[2] Code for prefix 1. call x→method[0] 2. assign value to x.field[0] Class Method Tables Objects A B f1 f1 f2 a a b c Prefixed A B f1 f2 f1 a c b a Random CMSC 430 Lecture 12, Page 8