Search for question
Question

Coursework Task 2: RISC-V code generator Summary In this task, you are going to implement a code generator targeting RISC-V assembly for the same simple programming language (i.e. you can reuse

the lexer and parser developed in Task 1). The principal idea here is to use stack-machine assembly, which can easily be embedded into RISC-V assembly, as an intermediate language to simplify this task. The main resource is a 'skeleton' project that you can download here. Please carefully read the details below and the submission guidelines. Input/output specification and some technical details As before, we assume that the input program is 'well-formed' and 'well-typed'. Your code generator will generate RISC-V assembly code (or in our case, stack-machine assembly code with RISC-V 'polyfills') that implements the input program and simulate the generated code with RARS. We suggest the following calling convention when calling a function: 1. Pushes a return value onto the stack (meant to be modified by the callee). 2. Pushes the arguments (in reverse order) onto the stack. 3. Pushes the return address the address right after the jump onto the stack. The organisation of the source files for Task 2 is very similar to Task 1; specifically, • Your code generator class Simple LangCode Generator implements SimpleLangVisitor, and it has an extra method visitProgram which also returns a String (generated code) and takes the same two arguments as in Task 1. • Your code generator simulates the generated code and prints out the results in exactly the same way as in Task 1 using RARS's API (see the skeleton project for details)./nSome suggestions and hints • Each new assembly instruction should be on a new line — don't put more than one instruction on a single line. • You'll need a function that generates fresh labels. Don't hardcode labels, ensure that the function generates a fresh label every time it is called. Don't use a random generator for this, use a global / static variable for this purpose. This is one of the few legitimate uses of global/ static variables. • Check that your submission is not miscompiling conditional and loop constructs like if / then/ else or repeat/until. • Consider giving a 'dummy' value to the unit expressions like skip (why?) https://users.sussex.ac.uk/~hh435/compilers/task2.html 12/6/23, 12:43 AM Coursework Task 2: RISC-V code generator • We strongly recommend testing your code before submission. Note that pasting code into RARS and simply press 'Assemble' is insufficient as test, because that doesn't test the termination of the code generated by your code generator. [Run] it! 1/2

Fig: 1

Fig: 2