$30
1 Introduction
In the previous assignment, we have performed syntax and semantic analysis of a source code written in a subset of C language. In this assignment you have to generate intermediate code for a source program having no error. That means if your source code does not contain any error, which was to be detected in the previous offline, you will have to generate intermediate code for the source code. We have picked 8086 assembly language as our intermediate representation.
2 Tasks
You have to complete the following tasks in this assignment.
2.1 Intermediate Code Generation
You have to generate an 8086-assembly language program from the input file after the input file successfully pass all the previous steps (lexical, syntax, semantics). You have to do write codes in the same file of your previous assignment. You need to follow the below guidelines to generate assembly code.
To generate assembly code for conditional statements and loops, define two functions named newLabel() and newTemp() where newLabel will generate a new label on each call and newTemp will generate a new temporary variable.
Write a procedure for println(ID) function and call this procedure in your assembly code whenever you reduce a rule containing println.
You have to declare the variables declared in the source code in the data segment of the assembly code. As the variable name can be same in different scope, you can concatenate the scope id with the variable name to make it unique.
You have to handle arguments passing and return values during a function call using stack.
Generate the code on the fly. Do not build the code by concatenating through a synthesized attribute.
Annotate the generated assembly code by putting the statement as a comment at the beginning of each block of assembly code that corresponds to the statement.
Bonus tasks: The following tasks will be considered bonus. (i) Handling recursive function, (ii) efficient use of temporary variables, (iii) use of short-circuit or jumping code.
You may also find the given sample code helpful in this case. Note that the sample file consists of only some portion of the grammar. You have to generate intermediate code for all the productions given in previous grammar.txt file.
2.2 Optimization
You have to do some peephole optimization works after intermediate code is generated. See Section 8.7 of your textbook for examples of peephole optimizations. One such example is removing redundant mov instruction from consecutive instructions as shown below.
mov ax, a mov a, ax ß redundant
In this case, the second mov instruction can be omitted.
3 Input
The input will be a C source program in .c extension. File name will be given from command line.
4 Output
The output of this assignment are primarily two files, namely asm and optimized_code.asm. The file code.asm will contain the generated assembly code before performing optimization. The other file optimized_code.asm will contain the assembly code after optimization. The generated assembly files must run successfully on the emulator EMU8086.
Additionally, like the last assignment (assignment 3), there will be an txt file that will contain error count and any lexical, syntax or semantic errors.
Note that, if there are errors in the input file then no codes will be generated, i.e., when txt is not empty, the files code.asm and optimized_code.asm will be blank.