CIT 595 Spring 2011

Homework #6

Due Mon Apr 25 1:30pm

Late homeworks will be accepted until Weds Apr 27 1:30pm with no penalty


In this assignment, you will convert Java programs from bytecode (in human-readable format) to their original Java source code. You will also fix bugs in the programs, and make optimizations.

Most of the bytecode instructions have been covered in lecture, but for some you will need to look at the Java Virtual Machine instruction set to get information about particular opcodes.

Part 1. (20 points)
The file Student.out contains the bytecode instructions for a class called "Student" that has three fields (a String called "name", an array of ints representing "scores", and a "count" of how many scores have been put in the array) and three methods, as well as a constructor.

Convert each of the four methods from bytecode to Java code. The names of the methods have been changed (otherwise it's too easy to figure out what they do!), but the comments in the .out file are used to indicate which fields/methods are being accessed in the class' Runtime Constant Pool.

For this part of the assignment, submit with the four methods implemented. Then, in a separate document (your "writeup"), describe in plain English what each of the three methods does (you don't have to explain what the constructor does!).


Part 2. (10 points)
The Student class as it is written is not threadsafe: if multiple threads are accessing the same object simultaneously, race conditions can occur. Assuming that synchronization is done on the object itself (using the monitor from "this"), where should you put "monitorenter" and "monitorexit" instructions in Student.out? Note that you do not have to modify the code in Student.out, just indicate in your writeup where you would put the instructions.


Part 3. (5 points)
Instead of using the "mark and sweep" approach to garbage collection, another technique is to keep track of the number of references to each object (i.e., variables that point to that object), and then free the object when the reference count drops to zero.

However, there are certain situations in which this can lead to a memory leak: an object no longer in use may not be garbage collected because there are still references to it. How can this happen? Give a specific example.


Part 4. (15 points)
Listed below are a method and its unoptimized bytecode:

public int optimizeMe(int x, int y, boolean k) {
   final int z = 3;
   int m;
   if (x > y) m = x;
   else m = y;

   if (false && k) {

   int s = z + 5;
   return m + s + 5 - 1;
public int optimizeMe(int, int, boolean);
   0:   iconst_3                             
   1:   istore  4     // z = 3                   
   3:   iload_1                          
   4:   iload_2                          
   5:   if_icmple 14  // if (x <= y) goto 14
   8:   iload_1                          
   9:   istore  5     // m = x            
   11:  goto    17     
   14:  iload_2                          
   15:  istore  5     // m = y                
   17:  iconst_0      // push "false" 
   18:  ifeq    28    // if false, goto 28
   21:  iload_3       // push k
   22:  ifeq    28    // if false, goto 28          
   25:  iinc    5, 1  // m++                   
   28:  iload   4                        
   30:  iconst_5                   
   31:  iadd          // add z and 5                   
   32:  istore  6     // s = z + 5     
   34:  iload   5            
   36:  iload   6                        
   38:  iadd          // add m and s                   
   39:  iconst_5                         
   40:  iadd          // add 5 to m + s
   41:  iconst_1
   42:  isub          // subtract 1 from (m + s + 5)
   43:  ireturn

Use constant folding and dead code elimination to optimize the bytecode shown above. In your writeup, include the optimized bytecode, along with the line numbers. How many instructions were saved? How many bytes were saved?


Academic Honesty

You must work on this assignment individually. Collaboration is explicitly not allowed. That includes (but is not limited to) working together on the assignment, providing solutions to or receiving solutions from another student, or receiving assistance from an outside source. Whereas it is okay to discuss the intent of the assignment or address other clarification issues with other students, you must not discuss approaches, algorithms, or code.

If you need help with this assignment, please visit a member of the teaching staff during office hours, or contact one of us to set up an appointment.

For this assignment:
  1. Put your source code files ( in a directory called "homework6_[your-SEAS-id]". For instance, mine would be homework6_cdmurphy.
  2. Last, from the directory ABOVE your "homework6_[your-SEAS-id]" directory (ie, its parent), tar and/or zip the directory with all your files in it. For instance, on eniac you could do "tar -cvf homework6_[your-SEAS-id].tar ./homework6_[your-SEAS-id]".
  3. Submit the tar/zip file in Blackboard.

Failure to properly follow the submission instructions could result in a delay of grading your assignment and/or a lateness penalty, so please be sure to do it correctly!

Homeworks are to be submitted via Blackboard, as described on the course overview page. Please be sure to tar and/or zip your files into a single submission file!

Please note: You may submit this assignment up to two days late with no penalty!