Question: Design a compiler for a processor that supports the following Assembly instructions: - ADD R 0 , R 1 : R 0 = R 0
Design a compiler for a processor that supports the following Assembly instructions:
ADD R R: RRR
MOV R imm: R imm
MOV rbpoffset R: Store R into memory location of rbpoffset
MOV rbpoffset R: Store R into memory location of rbpoffset
MOV Rrbpoffset: Restore R from memory location of rbpoffset
MOV Rrbpoffset: Restore R from memory location of rbpoffset
The compiler allows users to write code in C instead of Assembly and generates the
corresponding Assembly code.
Create a compiler in C that reads a C file at cmscprojinput.c Each line in the C file
will be of the following format:
;
;
For example, the following instructions are possible:
x ;
y ;
z x y ;
tempVar z y ;
The compiler should output in Assembly to the screen, separating each Assembly instruction in
its own line.
You can break down the compiler into the following operations:
Read a line from input file
std::ifstream filecmscprojinput.c;
while std::getlinefile line
Identify the C instruction. For each C instruction, have a sequence of Assembly instructions
that can be used to implement it For example, C instruction is
implemented by MOV R imm Assembly instruction.
Check if line contains symbol to identify the instruction:
if linefind std::string::npos
Identify the variablesimmediate values used in the instruction
If words is a vector of string variables that splits each line, then words is the destination
register, wordswords are the input registers for the addition instruction. words is
the immediate value for the immediate load instruction.
Keep a unordered map keyvalue pair that stores the C variable name as key and stack
offset location as the value.
Keep a variable that points to the number of stack locations used ie number of C
variables Assume that variable needs to be saved for the entirety of the program.
For each C instruction, break it into the following sections:
Retrieve the input variables from stack memory into registers. Load immediate value into
registers. Write out separate Assembly instructions to perform each of these operations.
Perform the sequence of Assembly instructions that is equivalent to the C instruction.
Write out the sequence.
Store the output into stack memory. If the variable already exists ie key exists in
unordered map then write back to that offset. If variable doesnt exist, create a new
location based on the variable that keeps track of number of stack locations used
Some additional notes:
Submit cmscprojcpp file. Compile your code using the following command and
confirm that it works as expected:
go cmscproj cmscprojcpp
There are only registers in this processor. If you have more than two variables, you have to
offload some of the variables into stack memory rbpoffset After the register has been
stored, the register is free to use for a different purpose. When the variable is needed again,
read back from stack memory to retrieve its contents.
For simplicity, assume that any stack offset is available for use by the compiler and each
variable can be stored in one memory location ie memory stack, variables, registers are all
bit wide
The compiler can either try to reduce the number of assembly instructions needed or it can
be a fast compiler ie reduce compiler complexity and save all variables to stack whenever it
changes so that registers are always available You can choose the fast option to simplify the
project.
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
