Project 6: Ugly Assembly Code

Description

Now that you have a working compiler front-end for Good Lang, you will build a simple back-end that will convert the Bad Code into Ugly Code. Ugly Lang is similar to Bad Lang, but with the following differences:

  • There are no scalar or array variables.
  • There are eight registers called regA, regB, regC, regD, regE, regF, regG, and regH. These are identical to scalar variables, but you have a limited number of them.
  • There are no array-based instructions so you must find replacements for ar_get_idx, ar_set_idx, ar_get_siz, ar_set_siz and ar_copy.
  • You may use three new instructions: load, store, and mem_copy. Each takes two arguments, the first indicating the source and the second indicating the destination. For "load", the first argument evaluates to the memory position to load from, and the second argument must be a register to put that value into. For "store", the first argument evaluates to the value to be stored and the second to the memory position to place that value. Finally, for "mem_copy", both arguments evaluate to memory positions where the contents of the first one is copied into the second.

Fundamentally, these changes mean that you need to handle your own memory management, especially for arrays.

You will need to add the function "generate_ugly_code_from_string" to project.py, so that the test cases can check for ugly code compilation functionality. This function should take a multiline good lang string and output a multiline string of valid ugly code. Note: the function "generate_bad_code_from_string" must still work as in previous projects.

Manual Testing

Similar to the previous projects, software to run the Bad and Ugly code generated is available in the starter code. We have provided a bad_and_ugly_interpreter.py script that can run either language.

The following command will invoke the bad interpreter on the input from the file (a file with bad code in it) test.bad and save the executed output to test.out.

$ ./bad_and_ugly_interpreter.py test.bad test.out

The following command will invoke the ugly interpreter on the input from the file (a file with ugly code in it) test.ugly and save the executed output to test.out.

$ ./bad_and_ugly_interpreter.py --ugly test.ugly test.out

You can also enable the debug mode to get more output from the interpreter regarding the state of memory and various other data structures.

$ ./bad_and_ugly_interpreter.py --ugly test.ugly test.out --debug

Submission

When you submit your program, you must include:

  • A project.py with the "generate_bad_code_from_string" function and the "generate_ugly_code_from_string" function.
  • The bad_and_ugly_interpreter.py file unmodified.
  • A README.txt file that includes the your name, a brief summary of what does and does not work in your code. Your README may also include any notes that you want us to read before grading your program, such as any external resources you used in its development.
  • You may divide your project code into multiple files to do so you will need to use relative imports (if that doesn't mean anything to you, just keep everything in the project.py file).