Question: Part 1 : Writing a Makefile ( 1 0 % ) Your submission must contain a file Makefile that builds your code. This will also

Part 1: Writing a Makefile (10%)Your submission must contain a file Makefile that builds your code. This will also helpyou build and test your code, which is why we are starting with it.To build your code, we should be able to type:$ makeThis must create an executable in the asn5 directory called mjava.We should also be able to type:$ make cleanThis should remove all .o files along with the mjava executable.We should be able to type:$ make test1$ make test2..$ make test8Each of these targets should run your mjava executable on testfiles/testN.mclassFor example, if we type:$ make test1Your Makefile should run:./mjava testfiles/test1Finally, we should be able to type:$ make testThis should run all tests in order (test1 through test8).For each target in your Makefile, be sure that you are using the appropriatedependencies.Part 2: Building MiniJVM (90%)Complete minijvm.c to implement MiniJVM. The minimum functions to implement areas follows:char* jvm_read(const char* filename)Purpose Reads the bytes from the specified file into an arrayParameters filename: Name of the .mclass file from which to read (withoutthe .mclass extension)What to Do Compute the target filename (filename +".mclass") If it doesn't exist, print an error and exit with EXIT_FAILURE Dynamically allocate an array of bytes to store the bytecode. Afile may contain up to MAX_CLASS_SIZE bytes Open the specified file Read it byte-by-byte into the array until EOF is reached Close the fileReturns A pointer to the array of bytecodeNote Look up the functions fopen, fread, fclose, and stat stat could be used to determine if the file doesn't existminijvm* jvm_init(const char* filename)Purpose Initializes a new minijvm structParameters filename: Name of the .mclass file from which to read (withoutthe .mclass extension)What to Do Dynamically allocate a new minijvm struct Read the file and store the bytecode array in the struct Set the program counter to point at the first bytecode in the array Create the operand stack (see stack.h for the function to use)Returns A pointer to the initialized minijvmvoid jvm_free(minijvm* jvm)Purpose Frees all memory allocated for the MiniJVMParameters jvm: The minijvm struct to deallocateWhat to Do Deallocate all memory dynamically allocated for the MiniJVMNote Don't forget to free memory even if you exit early due to an errorvoid jvm_run(minijvm* jvm)Purpose Runs the program loaded into the MiniJVMParameters jvm: The minijvm initialized with the bytecode to runWhat to Do In an infinite loopo Get the next bytecode pointed to by the program counter Interpret each bytecode as an unsigned charo Call a helper function to execute it If an error occurs in your helper function, set thereturn_value member of jvm to 1 and stopexecution If the bytecode is return, set the return_valuemember of jvm to 0 and stop executiono Update the program counter to point to the next bytecode Note that some bytecodes take arguments in thebytecode array Your helper functions may also modify the programcounter as neededNote This will likely be a big switch statement, but the code to execute thebytecodes must be implemented in helper functions. There are otherways to implement this rather than switch. For example, you couldimplement a lookup table with pointers to functions for each bytecode.However, keep things simple unless you're looking for a challenge.int main(int argc, char** argv)Purpose The main entry point to MiniJVMParameters argc: Number of parameters passed to the program argv: Array of parameters passed to the program. argv[0] isalways the name of the program. argv[1] is the first parameterWhat to Do Call functions to initialize MiniJVM, run the program, and freedynamically allocated memory If an incorrect number of arguments is passed, call usageReturns 0, upon successful termination of an .mclass file 1, if any error occurs in the programSample OutputTrying to run a file that does not exist:$ ./mjava nonexistentFile 'nonexistent.mclass' not found$ echo $?1$? Is a shell variable that stores the status code returned by the last command. You canecho this to check that your program is returning the correct values. Observe that theprogram returns 1 upon an error.Successfully running a program:$ ./mjava testfiles/test12$ echo $?0Observe that the program returns 0 upon successful termination.Running a program that has a division by zero error:%./mjava testfiles/test32Division by zero$ echo $?1Observe that the program returns 1 since an error occurred.Running the program with invalid arguments:$ ./mjavaUsage: mjvm FILENAME$ echo $?1IMPORTANT: We will be using automated tests to grade your assignments and get themreturned to you as soon as possible. If you do not wish to have marks deducted, it isimportant that your output is exactly as shown.A Word on Memory LeaksDon't forget to free the memory allocated for your MiniJVM before the program exits. Ifyour program has a memory leak, marks will be deducted. Fortunately, there's a helpfulprogram called valgrind that you can use to check for memory leaks. Here's an exampleof what valgrind reports when you have a memory leak:$ valgrind ./mjava test/test1==9006== Memcheck, a memory error detector...==9006== LEAK SUMMARY:==9006== definitely lost: 72 bytes in 1 blocks <---==9006== indirectly lost: 1,056 bytes in 4 blocks==9006== possibly lost: 0 bytes in 0 blocks==9006== still reachable: 0 bytes in 0 blocks==9006== suppressed: 0 bytes in 0 blocksAnd here's what it reports when you don't have a memory leak:$ valgrind ./mjava test/test1==9090== Memcheck, a memory error detector...==9090== All heap blocks were freed -- no leaks are possibleTest FilesThe testfiles directory contains eight .mclass files with which you can test yourprogram. Human-readable versions are available in eight corresponding .mjava files.You should trace the code in these files to ensure you understand what the code is doingand what your program should output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!