Docsity

Storage Allocation and Offset Decision in Object-Oriented Languages - Prof. Jingke Li, Exams of Computer Science

Toggle sidebar
bg1
IR Code Generation (Part II)
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 IR Code Generation II 1 / 31
Object-Oriented Language Issues
•Classes and Objects
•Storage allocation
•Static class variables
•Dynamic class variables
•Method Invocations
•Static methods
•Static-binding methods
•Dynamic-binding methods
•MINI’s method invocation
•Others
•Method names
•Local variables and parameters
•Non-local (class) variables
•Activation record size
•This pointer
•Advanced Topics
•Multiple inheritance
•Membership testing
Jingke Li (Portland State University) CS322 IR Code Generation II 2 / 31
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Storage Allocation and Offset Decision in Object-Oriented Languages - Prof. Jingke Li and more Exams Computer Science in PDF only on Docsity!

IR Code Generation (Part II)

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 IR Code Generation II 1 / 31

Object-Oriented Language Issues

  • (^) Classes and Objects
    • Storage allocation
    • Static class variables
    • Dynamic class variables
  • Method Invocations
    • (^) Static methods
    • (^) Static-binding methods
    • (^) Dynamic-binding methods
    • (^) MINI’s method invocation
      • (^) Others
        • Method names
        • Local variables and parameters
        • Non-local (class) variables
        • Activation record size
        • This pointer
      • Advanced Topics
        • (^) Multiple inheritance
        • (^) Membership testing

Storage Allocation for Class Objects

Observations:

  • Static class variables are established per class; they should be allocated to a single static place
  • (^) Dynamic class variables are cloned every time a class object is created; they should be stored in the allocated space for the object. General Strategies:
  • (^) A class descriptor for each class —
  • pointer(s) to parent class descriptor(s)
  • pointers to (local) methods
  • storage for static variables
  • (^) An object record for each class object —
  • pointer to class descriptor
  • storage for (local) class variables
  • storage for inherited variables Alternative strategies exist. For example, the items in a class descriptor can be allocated individually with proper IDs attached to indicate their association to the class, e.g. using class name as a prefix. Jingke Li (Portland State University) CS322 IR Code Generation II 3 / 31

Object Record Layout

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.

Deciding Object’s Size (cont.)

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

Deciding Class Variables’ Offsets

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 decls variable offsets

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 }


Class Variables’ Initialization

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

Class Variables’ Initialization (cont.)

Example:

class decls offsets init exps

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 }


Accessing Object’s Instance Variables

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

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:

  1. Search C ’s definition to see if the method f is defined there. If not, search the parent’s class definition. Repeat this step until the method is found.
  2. Generate code for accessing the method.

Static-Binding Methods

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:

  1. From v ’s declaration, figure out the v ’s class type (say C ).
  2. Search C ’s definition to see if the method f is defined there. If not, search the parent’s class definition. Repeat this step until the method is found.
  3. Generate code for accessing the method.

Jingke Li (Portland State University) CS322 IR Code Generation II 15 / 31

Dynamic-Binding Methods

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.

MINI’s Method Invocation

E -> E1 ’.’ Id ExpList

  • Extract the class name C out from the class object E1.
  • (^) Concatenate C and method name (Id) to form a unique label.
  • (^) Construct a CALL node with the label and an argument list. class test { public static void main(String[]{ A a = new A(); int x = a.foo(1,2); a) a.bar(3,4); } } class A {public int foo(int i, int j) { return i+j; } public void bar(int i, int j) { System.out.println(i+j); } } main (locals=2,[MOVE (TEMP 100) (CALL (NAME malloc) ( (NAME wSZ)))] max_args=3) { [MOVE (VAR 1) (TEMP 100)] [MOVE (TEMP 101) (CALL (NAME A_foo) ( (VAR 1) (CONST 1) (CONST[MOVE (VAR 2) (TEMP 101)] 2)))] [CALLST (NAME A_bar) ( (VAR 1) (CONST 3) (CONST 4))] }A_foo (locals=0, max_args=0) { [RETURN (BINOP + (PARAM 1) (PARAM 2))] } A_bar (locals=0, max_args=0) { }[CALLST (NAME prInt) ( (BINOP + (PARAM 1) (PARAM 2)))]

Jingke Li (Portland State University) CS322 IR Code Generation II 19 / 31

Method Names

  • Methods are defined within the scope of classes. The same name can be used by two different methods in two different classes.
  • At the IR level, however, there is only a single name space.

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 (...) ...

Local Variables and Parameters

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

Accessing Class Variables

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:

  • Always pass the current object’s address as the zero-th parameter ((PARAM 0)) to a method invocation; this special parameter is called access link.
  • Class variables can then be accessed through offsets from the access link. We use an abstract reference form (MEMBER <var’s idx>) to represent them.

Space for Method Invocation — An Example

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

Advanced Topic: Multiple Inheritance

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:

  • There is a node for each distinct variable name.
  • There is an edge for any two variables which coexist (perhaps by inheritance) in the same class.

The Global Graph Coloring Approach Example

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

Multiple Inheritance (cont.)

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:

  1. Fetch the descriptor pointer from the object.
  2. Fetch the variable-offset value from the descriptor.
  3. Access the data the appropriate offset in the object.

Methods can be looked up using the same technique.

Class Membership Example

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