Question: Part 1 : Writing a Makefile ( 1 0 % ) Your submission must contain a file Makefile that builds your code. This will also
Part : Writing a Makefile 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 itTo build your code, we should be able to type:$ makeThis must create an executable in the asn 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 test$ make test$ make testEach of these targets should run your mjava executable on testfilestestNmclassFor example, if we type:$ make testYour Makefile should run:.mjava testfilestestFinally, we should be able to type:$ make testThis should run all tests in order test through testFor each target in your Makefile, be sure that you are using the appropriatedependencies.Part : Building MiniJVM Complete minijvm.c to implement MiniJVM. The minimum functions to implement areas follows:char jvmreadconst char filenamePurpose Reads the bytes from the specified file into an arrayParameters filename: Name of the mclass file from which to read withoutthe mclass extensionWhat to Do Compute the target filename filename mclass" If it doesn't exist, print an error and exit with EXITFAILURE Dynamically allocate an array of bytes to store the bytecode. Afile may contain up to MAXCLASSSIZE bytes Open the specified file Read it bytebybyte 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 jvminitconst char filenamePurpose Initializes a new minijvm structParameters filename: Name of the mclass file from which to read withoutthe mclass extensionWhat 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 useReturns A pointer to the initialized minijvmvoid jvmfreeminijvm jvmPurpose 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 jvmrunminijvm jvmPurpose 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 thereturnvalue member of jvm to and stopexecution If the bytecode is return, set the returnvaluemember of jvm to 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 mainint argc, char argvPurpose The main entry point to MiniJVMParameters argc: Number of parameters passed to the program argv: Array of parameters passed to the program. argv isalways the name of the program. argv 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 upon successful termination of an mclass file if any error occurs in the programSample OutputTrying to run a file that does not exist:$ mjava nonexistentFile 'nonexistent.mclass' not found$ echo $$ 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 upon an error.Successfully running a program:$ mjava testfilestest$ echo $Observe that the program returns upon successful termination.Running a program that has a division by zero error:mjava testfilestestDivision by zero$ echo $Observe that the program returns since an error occurred.Running the program with invalid arguments:$ mjavaUsage: mjvm FILENAME$ echo $IMPORTANT: 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 testtest Memcheck, a memory error detector... LEAK SUMMARY: definitely lost: bytes in blocks indirectly lost: bytes in blocks possibly lost: bytes in blocks still reachable: bytes in blocks suppressed: bytes in blocksAnd here's what it reports when you don't have a memory leak:$ valgrind mjava testtest Memcheck, a memory error detector... All heap blocks were freed no leaks are possibleTest FilesThe testfiles directory contains eight mclass files with which you can test yourprogram. Humanreadable 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
