









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 IR Code Generation II 1 / 31
Observations:
An object record contains space for variables belong to this class, as well as for variables belong to ancestor classes.
How should the variables be laid out so that their offsets can be computed statically by the compiler?
For single-inheritance languages, we have a solution.
The Prefixing Method —
When a class B extends a class A, those variables of B that are inherited from A are laid out in a B record at the beginning, in the same order they appear in an A record.
Since a subclass always extends the set of variables defined in its base class, compiler can consistently assign each variable a fixed (static) offset in the object record; this offset will be the same in every object record for that class and any of its subclasses. Compiled methods can then reference variables by offset rather than by name.
What if the declarations are not in the right order?
class D extends C { int l=6; } class C extends A { int k=5; } class B extends A { int m=3, n=4; } class A { int i=1, j=2; }
Solution — Perform a topological sort on class decls based on inheritance relationship, then collect the size info.
Jingke Li (Portland State University) CS322 IR Code Generation II 7 / 31
Once objects’ sizes are known, class variables offsets can be computed easily — The offset of a subclass’s first instance variable equals the parent class’s object size.
class A { int i=1, 0 j=2; wdSize } class B extends A { int m=3, 2wdSize n=4; 3wdSize } class C extends A { int k=5; 2wdSize } class D extends C { int l=6; 3wdSize }
According to Java’s semantics, an instance variable will always be initialized, either by the user or by the compiler.
The difficulty is that the initialization happens when an new class object is created (could be anywhere in a program) while the user-provided initialization information is the class declaration section.
Solution — Collect class variable initialization info while processing class decls and store the info in class variable’s symbol table entry. (If there no user-provided init value, a default value is stored.)
Jingke Li (Portland State University) CS322 IR Code Generation II 9 / 31
Example:
class A { int i=1, 0 1 j=2; wdSize 2 } class B extends A { int m=3, 2wdSize 3 n=4; 3wdSize 4 } class C extends A { int k=5; 2wdSize 5 } class D extends C { int l=6; 3wdSize 6 }
E -> E1 ’.’ Id Obtain object’s address from E1 and variable offset from Id. class t4 { public static void main(String[] a) { A a = new A(); B b = new B();System.out.println(a.i+a.j+b.x+b.y); } }class B { int x = 1; int y = 2; } class A { int i = 3; int j = 4; } main (locals=2,[MOVE (TEMP 100) (CALL (NAME malloc) ((BINOP * (CONST 2) (NAME wSZ))))] max_args=0) { [MOVE (MEM (TEMP 100)) (CONST 3)] [MOVE (MEM (BINOP + (TEMP 100) (NAME wSZ))) (CONST 4)][MOVE (VAR 1) (TEMP 100)] [MOVE (TEMP 101) (CALL (NAME malloc) ((BINOP * (CONST 2) (NAME wSZ))))] [MOVE (MEM (TEMP 101)) (CONST 1)][MOVE (MEM (BINOP + (TEMP 101) (NAME wSZ))) (CONST 2)] [MOVE (VAR 2) (TEMP 101)] [CALLST (NAME prInt) ((BINOP + (BINOP + (BINOP +(MEMBER (VAR 1) 0) // 1st var of a (i) (MEMBER (VAR 1) 1)) // 2nd var of a (j) (MEMBER (VAR 2) 0))(MEMBER (VAR 2) 1)))] // 2nd var of b (y) // 1st var of b (x) }
Jingke Li (Portland State University) CS322 IR Code Generation II 13 / 31
Static methods can be invoked only through class names. However, inheritance needs to be taken into consideration.
Example: class c1 { static void print(int i) { System.out.println(i); } public static void main(String[] a) { c2.print(123); } } class c2 extends c1 { ... }
Given C .f (), the compiler can:
Method binding approach is a choice of language design — some languages use static binding (e.g. C++), some use dynamic binding (e.g. Java).
Static-binding methods are invoked through variables that represent objects.
Given v .f (), the compiler can:
Jingke Li (Portland State University) CS322 IR Code Generation II 15 / 31
With dynamic-binding, given v .f (), the method to be invoked may not be the one defined in the declared class of variable v. E.g.
class A { public void foo() {...} ... } class B extends A { public void foo() {...} ... } A a = new B(); a.foo();
The declared type of a is A, yet the version of foo to be invoked by a.foo() should be the one defined in B.
So the previous approach won’t work.
E -> E1 ’.’ Id ExpList
Jingke Li (Portland State University) CS322 IR Code Generation II 19 / 31
How to map method names in a source program downto unique labels in the IR code?
A Simple Solution: Consider the fact that class definitions are not nested, hence class names are unique (within the scope of a package, to be pricise). We can simply add class name as a prefix to a method’s name.
Example: class test { public static void main(...) ... } class A { public int foo(...) ... public void bar(...) ... }
main (locals=2, max_args=3) { ... ... (CALL (NAME A_foo) ... } A_foo (...) ... A_bar (...) ...
Both local variables and parameters are eventually stored in a method’s activation record — space allocated on stack at runtime, and are accessed through offsets from a stack pointer.
At IR level, they are stored in two separate abstract arrays, VAR and PARAM, and are indexed by their positions in their corresponding declarations. class t1 { public static void main(String[] a) { A a = new A(); a.sum(3,4); } }class A { public void sum(int m, int n) } { int i = 1; int j = 2; System.out.println(i+j+m+n);^ } main (locals=1,[MOVE (TEMP 100) (CALL (NAME malloc) ( (NAME wSZ)))] max_args=3) { [MOVE (VAR 1) (TEMP 100)][CALLST (NAME A_sum) ( (VAR 1) (CONST 3) (CONST 4))] } A_sum (locals=2,[MOVE (VAR 1) (CONST 1)] max_args=0) { [MOVE (VAR 2) (CONST 2)] [CALLST (NAME prInt) ( (BINOP + (BINOP + (BINOP + (VAR 1)(VAR 2)) // 1st var (i)// 2nd var (j) (PARAM 1)) // 1st param (m) } (PARAM 2)))] // 2nd param (n)
Jingke Li (Portland State University) CS322 IR Code Generation II 21 / 31
Class variables defined in a method’s enclosing scopes can be accessed within the method’s body. But how does a method know about its class object’s address? class B { int x = 1; int y = 2; } class A extends B { int i = 3; int j = 4; public void sum() { System.out.println(i+j+x+y); } }
Solution:
class t1 { public static void main(String[] a) { A a = new A(); a.sum(3,4); } } class A { public void sum(int m, int n) { int i = 1; int j = 2; System.out.println(i+j+m+n); } }
IR_PROGRAM main (locals=1, max_args=3) { ... } A_sum (locals=2, max_args=0) { ... }
Jingke Li (Portland State University) CS322 IR Code Generation II 25 / 31
When a class can extend several parent classes, the prefixing layout method for class variables doesn’t work any more — it is not possible to put two parents’ variables both at the beginning.
Some form of ordering among the parents’ variables is needed. In fact, some form of global ordering among all classes’ variables is needed.
The Global Graph Coloring Approach: The problem of ordering all classes’ variables can be modeled as a graph-coloring problem:
class A { int a = 0; } class B { int b = 0; int c = 0; } class C extends A { int d = 0; } class D extends B,C { int e = 0; }
a
b
c d
e
0
1
2 3
5
A a
B
b c
C a
d
D a b c d e
Jingke Li (Portland State University) CS322 IR Code Generation II 27 / 31
Problem: Empty slot in the middle of objects. Solution: Pack variables in
objects records; store graph coloring result and packing info in class descriptors.
a b c
a d
a b c d e
1 (^12)
1
2
1 (^23) 4 5
A B C D a bc
a
d
a bc d e Accessing Variables:
Methods can be looked up using the same technique.
class A { int a = 0; } class B extends A { int b = 0; int c = 0; } class C extends A { int d = 0; } class D extends C { int e = 0; }
A: C: ptr to Object ptr to Object ptr to A ptr to A ptr to C B: ptr to Object D: ptr to A ptr to Object ptr to B ptr to A ptr to C ptr to D