CS39003 Assignment 6-Target Code Generator for tinyC Solved

30.00 $

Category:

Description

5/5 - (1 vote)

6: Target Code Generator for tinyC

1 Preamble – tinyC

The Lexical Grammar (Assignment 3) and the Phase Structure Grammar (As- signment 4) for tinyC have already been defined as subsets of the C language specification from the International Standard ISO/IEC 9899:1999 (E). Finally, three address code (TAC) structure and a further subset of tinyC has been spec- ified (Assignment 5) for translating the input tinyC program to TAC quad array, a supporting symbol table, and other auxiliary data structures.

In this assignment you will write a target code translator from the TAC quad array (with the supporting symbol table, and other auxiliary data structures) to the assembly language of x86-64. The translation is now machine-specific and your generated assembly code would be translated with the gcc assembler to produce the final executable codes for the tinyC program.

2

•

•

Scope of Target Translation

For simplicity restrict tinyC further:

  1. Skip shift and bit operators.
  2. Support only void, int, and char types. Skip double type.
  3. Support only one–dimensional arrays.
  4. Support only void, int, char, void*, int*, and char* types for returns types of functions.
  5. No type conversion to be supported.

For I/O, provide a library (as created in Assignment 2) using in-line as-

sembly language program of x86-64 along with syscall for gcc assembler.:

– int printStr(char *) – prints a string of characters. The parame- ter is terminated by ‘\0’. The return value is the number of characters printed.

– int printInt(int n) – prints the integer value of n (no newline). It returns the number of characters printed.

– int readInt(int *eP) – reads an integer (signed) and returns it. The parameter is for error (ERR = 1, OK = 0).

The header file myl.h of the library will be as follows:

#ifndef _MYL_H
#define _MYL_H
#define ERR 1
#define OK 0
int printStr(char *);
int printInt(int);
int readInt(int *eP);  // *eP is for error, if the input is not an integer
#endif

1

Memory Binding

This deals with the design of the allocation schema of variables (including parameters and constants) that associates each variable to the respective address expression or register. This needs to handle the following:

• Handle local variables, parameters, and return value for a function. These are automatic and reside in the Activation Record (AR) of the function. Various design schema for AR are possible based on the calling sequence protocol. A sample AR design could be as follows:

3 Design of the Translator

The steps for target code generation were outlined in Target Code Generation lecture presentations. In this assignment, however, you do not need to deal with any machine-independent or machine-specific optimization. Hence the transla- tion comprises the following major steps only:

Offset Stack Item

–ve Saved Registers –ve Callee Local Data

0 Base Pointer of Caller Return Address

+ve Return Value +ve Parameters

Responsibility

Callee Saves & Restores Callee defines and uses Callee Saves & Restores Saved by call, used by ret Callee writes, Caller reads Caller writes, Callee reads

Code Translation

Activation Record Structure with Management Protocol

  • –  Offset’s in the AR are with respect to the Base Pointer of Callee.
  • –  Return Value can alternatively be returned through a register

    (like accumulator).

  • –  The AR will be populated from the Symbol Table of the function.
  • –  Symbol Tables of nested blocks will be flattened and its variables

    allocated within the Symbol Table (and hence the AR) of the function where there occur in. Necessary name mangling will be performed to to take care of same lexical name for different variables in different nested scopes.

  • Handle global variables (note that local static variables are not al- lowed in tinyC) as static and generate allocations in static area. This will be populated from global symbol table (ST.gbl).
  • Generate Constants from Table of Constants – handle string con- stants as assembler symbols in DATA SEGMENT and integer con- stants as parts of target code (TEXT SEGMENT)
  • Register Allocations & Assignment: Create memory binding for vari- ables in registers:
    • –  After a load / store the variable on the activation record and the register have identical values
    • –  Registers can be used to store temporary computed values
    • –  Register allocations are often used to pass int or pointer param-

      eters

    • –  Register allocations are often used to return int or pointer values

      Note: Refer to Run-Time Environment lecture presentations for details and examples on memory binding.

      This deals with the translation of 3–Address quad’s to x86-64 assembly code. This needs to handle:

    • Generation of Function Prologue – few lines of code at the beginning of a function, which prepare the stack and registers for use within the function.
    • Generate Function Epilogue – appears at the end of the function, and restores the stack and registers to the state they were in before the function was called.

2

• Map 3–Address Code to Assembly – to translate the function body do:

  • –  Choose optimized assembly instructions for every expression, as- signment and control quad.
  • –  Use algebraic simplification & reduction of strength for choice of assembly instructions from a quad.
  • –  Use Machine Idioms (like inc for i++ or ++i in place of add reg, 1).

    Note: Refer to Target Code Generation lecture presentations for details. Target Code Integrate all the above code into an Assembly File for gcc assembler.

4

1.

2. 3.

4.

The Assignment

Write a target code (x86-64) translator from the 3-Address quad’s gen- erated from the flex and bison specifications of tinyC (with restrictions

as mentioned in Section 2). Assume that the input tinyC file is lexically, syntactically, and semantically correct. Hence no error handling and / or recovery is expected.

Prepare a Makefile to compile and test the project.
Prepare test input files ass6 roll test<number>.c to test the target code

translation and generate the translation output in ass6 roll <number>.asm.

Name your files as follows:

File

Flex Specification
Bison Specification
Data Structures (Class Definitions) and Global Function Prototypes
Data Structures, Function Implementa- tions and 3–Address Translator
Target Translator and x86-64 Transla- tor main()
Test Inputs
3–Address Test Outputs
Test Outputs

Naming

ass6 roll.l
ass6 roll.y
ass6 roll translator.h

ass6 roll translator.cxx
ass6 roll target translator.cxx

ass6 roll test<number>.c ass6 roll quads<number>.out ass6 roll <number>.asm

5.

Prepare a tar-archive with the name ass6 roll.tar containing all the files and upload to Moodle.

  • Assignment-6-5qa5uc.zip