Write a program to read and analyze a set of class files whose names are given as command-line arguments.
The first goal of the analysis is to count the number of times each JVM opcode appears in the collection of class files. The second goal is to identify and count the number of references to different int, long, float and double constants by iconst, lconst, fconst, dconst, bipush, sipush, ldc, ldc_w, and ldc2_w instructions. The third goal is to identify and count the number of references to different local slots by iload, lload, fload, dload, aload, istore, lstore, fstore, dstore and astore instructions.
These counts are global counts: keep them across all class files that you analyze. This means you will need to keep a list of all constants referenced in the class files analyzed so far. In addition you will need a data structure to track all local slots referenced in the class files analyzed so far. Because local slots are assigned sequentially starting with 0, use an array that can be indexed by the slot number. Each element of the array will store the number of times its index has been referenced as a local slot. Dynamically allocate this array so that it can be resized if necessary.
Also use an array to count how many times each opcode is seen. This array can be indexed by the opcode itself. Each element of the array will store the number of times its index has appeared as an opcode.
When all class files have been processed, first print to stdout the counts for the opcodes. For partial credit print the numeric opcodes with their counts, one per line. For full credit print the opcode mnemonics (e.g. iadd) instead of the numeric opcodes. Whether printing numeric opcodes or opcode mnemonics, print the opcodes in numeric opcode order.
After the opcode counts are printed, then print each constant that was referenced, with its reference count, one per line. Print the int constants first, then the long constants, then the float constants, and finally the double constants. For full credit, print the constants in each type category in sorted order, from smallest (furthest left on the number line) to biggest. Start each line with the type of constant found on that line, then the value of the constant, then its reference count. Remember that the constants are stored in big-endian format in the class file and will need to be put into little-endian format to be printed on agate.cs.unh.edu. Use the printf formats %d, %ld, %e and %e when printing int, long, float and double constants, respectively.
Finally, print each local slot that was referenced, with its reference count, one per line. Start each line with the word local, then the local slot number, then its reference count. Print the local slots in increasing local slot number order, starting with zero and continuing to the largest local slot referenced in any class file analyzed. (It is possible local slots will have no references. Why? Simply print zero for the reference count in this case.)
You should check that there is at least one class file given on the command line. And you should check that each argument is a class file (i.e. starts with the correct magic number).
You are not responsible for doing a full validation of the structure of the class files you are reading. However, if there is a problem in the class file that causes you to be unable to process the class file, then print an error message to stderr that gives the name of the class file. Then continue processing with the next class file.
You can create test files by writing Java code and compiling it. You could also test using class files from the standard class libraries, which are available in this jar file:
You may want to include this header file:
As always, remember, you may lose points if your program is not properly structured or adequately documented. See the mandatory guidelines given in the course overview webpage. In particular, be sure to decompose your program into functions. Do not submit one monolithic main program!
There are two labs associated with this program: Lab 5 on February 24 and Lab 6 on March 2.
Your programs will be graded using agate.cs.unh.edu so be sure to test in that environment.
You should submit all the source code for this assignment in a single file called prog3.c.
Please turn-off any debugging code before you submit your program.
Your programs should be submitted for grading from agate.cs.unh.edu.
To turn in this assignment, type:
~cs520/bin/submit prog3 prog3.c
Submissions can be checked by typing:
~cs520/bin/scheck prog3
This assignment is due Tuesday March 6. The standard late policy concerning late submissions will be in effect. See the course overview webpage.
Remember: as always you are expected to do your own work on this assignment. There is code available on the Internet for reading class files, but I want you to write this code yourself. Remember: if you can find the code on the Internet, so can I.
Comments and questions should be directed to hatcher@unh.edu