Quiz #5 Help Center

Learn more

Warning: The hard deadline has passed. You can attempt it, but you will not get credit for it. You are welcome to try it as a learning exercise.

Question 1

Using the simple language and code generation technique described in the lecture to evaluate the following 2 expressions:
E1: ( ( 3 - 2 ) * 6 ) + 5
E2: 5 + ( 6 * ( 3 - 2 ) )
Which of the following statements are true?
[Choose all that apply]

Question 2

Consider the following assembly language generated using the simple code generation strategy described in the lecture.
foo_entry:
    move $fp $sp
    sw $ra  0($sp)
    addiu sp $sp -4
    lw $a0 8($fp)
    sw $a0  0($sp)
    addiu $sp $sp -4
    lw $a0 4($fp)
    lw $t1  4($sp)
    addiu $sp $sp 4
    beq $a0 $t1 true1
false1:
    lw $a0 12($fp)
    b endif1
true1:
    li $a0 17
endif1:
   lw $ra 4($sp)
   addiu $sp $sp #1
   lw $fp 0($sp)
   jr $ra
One argument to an instruction (#1) is missing. And consider the following expression from which the code was generated, with missing subexpressions #2 and #3.

def foo(x,y,z) = if y = #2 then 17 else #3

What are the values for #1, #2 and #3?

Question 3

Consider the following Cool program:
class Main {
  main():Object {
    (new Bar).foo()
  };
};
class Foo inherits IO{
  a:Int;
  foo():Object{self};
};
class Bar inherits Foo {
  b:Int;
  foo():Object {
    let i:Int←0 in
      while i < 10 loop{
        (new Bar)@Foo.foo();
        i ← i+1;
      } pool
  };
};
Using the object layout described in lecture, in order to represent the objects that exist when this program terminates, how many pointers in memory will need to point to the code for some implementation of the method "foo", and how many pointers in memory will need to point to an Int that is some object's "a" attribute? Assume that all objects created still exist when the program terminates.

Question 4

Using the simple language and code generation technique from the lectures and putting temporaries on the stack, what will be the size (in bytes) of the activation record for the following function (assuming temporaries are stored in the activation record):
def f(x, y, z, w):
  if x = (y + z)
    then 5
  else if x = (if y = z then f(y, z, x, 1) else x)
    then x
  else x * (y + (z - x))

Question 5

Given the following object definitions:
class A {
  x ← Int;
  a() : Int { .. };
};
class B inherits A {
  y ← Int;
  z ← Int;
};
class C inherits B {
  c() : Int { .. };
};
What is the size of an object of class C in words using the COOL object layout strategy described in the video lectures?

Question 6

Consider the following program.
Class F {{
  factorial(x:Int) : Int { if x = 1 then 1 else x * factorial(x-1) fi };
}
Class Main {
  f:F ← new F;
  f.factorial(3);
}
Which of the following could be a valid step in the derivation of the dispatch to factorial in Main?
[Choose all that apply]

Question 7

Using the simple language, code generation technique described in the lecture to evaluate the following 2 expressions:
E1=e1+e2+e3+e4+e5
E2=e1+(e2+(e3+(e4+e5)))
Consider the conclusions about the number of temporaries(NT) needed in the evaluation of E1 and E2.
[Choose all that apply]

Question 8

Using the simple language, code generation technique described in the lecture to evaluate the following 2 expressions:
E1:
if e1=e2
  then if e3=e4 then e5 else e6
else
  if e3=e4 then e7 else e8
E2:
if e3=e4
  then if e1=e2 then e5 else e7
else
  if e1=e2 then e6 else e8
Consider the conclusions about the number of temporaries(NT) needed in the evaluation of E1 and E2.Choose the correct one.
    
You cannot submit your work until you agree to the Honor Code. Thanks!