A Java static method is defined in class C

A programming language has commands C and expressions E
March 22, 2023
Two alternatives to conservative garbage collection
March 22, 2023

A Java static method is defined in class C

2003 Paper 5 Question 6
Compiler Construction
(a) A Java static method is defined in class C by
class C {
public static int f(int x, int y) { int z = x; …; return x+y*z;
}
}
where ‘…’ represents commands the details of which are not important to
this question. It is called in an expression e of the form
f(f(1,2), f(3,4))
Give JVM (or other stack machine) code corresponding to the expression e
and explain how this is derived from the syntax tree for e. [6 marks]
(b) Explain how the body of f above is mapped into JVM (or other stack machine)
code, explaining the rˆole of the registers FP and SP (precise details are not
important, but their rˆole should be well explained). You may write ‘…’ for
the translation of the ‘…’ in f. [6 marks]
(c) Consider the Java class definitions:
class A {
public int a1, a2;
public void m() { println(“I am an A with ” + a1 + ” and ” + a2);
}
}
class B extends A {
public int b1, b2;
public void m() { println(“I am a B with ” + a1 + ” and ” + a2 +
” also with ” + b1 + ” and ” + b2);
}
}
Describe the run-time storage layout for objects of class A and for those of
class B, particularly noting the size and offsets of members and how a cast
of an object of type class B to one of class A can be achieved.
Explain how calls to m() work, particularly in code like:
public static void g(B x) { h(x); }
public static void h(A x) { x.m(); }
[8 marks]