Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
1. Input to the code generator
3.
2. Target program
4.
3. Memory management
5.
4. Instruction selection
6.
5. Register allocation
7.
6. Evaluation order
8.
Frequently Asked Questions
8.1.
What are the different issues in the design of a code generator?
8.2.
What is the role of code generator in compiler design?
8.3.
What properties a good code generator should have?
8.4.
What is the potential problem with automatic code generation?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Issues in Design of Code Generator

Author Muskan Gupta
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Issues in Design of Code Generator

The code generator transforms the intermediate representation of source code into a machine-readable format. The correct code should be generated using a code generator. Designing the code generator should be done to be easily implemented, tested, and maintained.

Also read About, Specifications of Tokens in Compiler Design, Lexical Analysis in Compiler Design

In this blog, we will learn about the concept of issues in design of code generator. So, let's begin!

The main issues in the design of a code generator are:

  • Input to the code generator
  • Target program
  • Memory management
  • Instruction selection
  • Register allocation
  • Evaluation order

1. Input to the code generator

In the input to the code generator, design issues in the code generator intermediate code created by the frontend and information from the symbol table that defines the run-time addresses of the data objects signified by the names in the intermediate representation are fed into the code generator. Intermediate codes may be represented mainly in quadruples, triples, indirect triples, postfix notation, syntax trees, DAGs(Directed Acyclic Graph), etc. The code generation step assumes that the input is free of all syntactic and state semantic mistakes, that all essential type checking has been performed, and that type-conversion operators have been introduced where needed.

Also See, Top Down Parsing

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

2. Target program

The code generator's output is the target program. The result could be:

  • Assembly language: It allows subprograms to be separately compiled.
  • Relocatable machine language: It simplifies the code generating process.
  • Absolute machine language: It can be stored in a specific position in memory and run immediately.

3. Memory management

In the memory management design, the source program's frontend and code generator map names address data items in run-time memory. It utilizes a symbol table. In a three-address statement, a name refers to the name's symbol-table entry. Labels in three-address statements must be transformed into instruction addresses.

For example,

j: goto i generates the following jump instruction:

if i < j, A backward jump instruction is generated with a target address equal to the quadruple i code location.

If i > j, It's a forward jump. The position of the first quadruple j machine instruction must be saved on a list for quadruple i. When i is processed, the machine locations for all instructions that forward hop to i are populated.

4. Instruction selection

In the Instruction selection, the design issues in the code generator program's efficiency will be improved by selecting the optimum instructions. It contains all of the instructions, which should be thorough and consistent. Regarding efficiency, instruction speeds and machine idioms have a big effect. Instruction selection is simple if we don't care about the target program's efficiency.

The relevant three-address statements, for example, would be translated into the following code sequence:

P:=Q+R

S:=P+T

MOV Q, R0

ADD R, R0

MOV R0, P

MOV P, R0

ADD T, R0

MOV R0, S

 

The fourth sentence is unnecessary since the P value is loaded again in that statement already stored. It results in an inefficient code sequence. A given intermediate representation can be translated into several distinct code sequences, each with considerable cost differences. Previous knowledge of instruction cost is required to build good sequences, yet reliable cost information is difficult to forecast.

5. Register allocation

In the Register allocation, design issues in the code generator can be accessed faster than memory. The instructions involving operands in the register are shorter and faster than those involved in memory operands.

The following sub-problems arise when we use registers:

  • Register allocation: In register allocation, we select the set of variables that will reside in the register.
  • Register assignment: In the Register assignment, we pick the register that contains a variable.

Certain machines require even-odd pairs of registers for some operands and results.

Example

Consider the following division instruction of the form:

x, y  

Where,

x is the dividend even register in even/odd register pair

y is the divisor

An old register is used to hold the quotient.

6. Evaluation order

The code generator determines the order in which the instructions are executed. The target code's efficiency is influenced by order of computations. Many computational orders will only require a few registers to store interim results. However, choosing the best order is a completely challenging task in the general case. 

Frequently Asked Questions

What are the different issues in the design of a code generator?

The code generator transforms the intermediate representation of source code into a machine-readable format. During the code generation step, a number of problems may occur: supplying data to the code generator, target program, memory management, Instruction choice, allocation of Register, and Evaluation sequence.

What is the role of code generator in compiler design?

A code generator is a compiler that converts the source program's intermediate representation into the destination program. In other words, a code generator converts an abstract syntax tree into executable code that is specific to a certain machine. It is expected that a code generator will produce accurate code.

What properties a good code generator should have?

While using a code generator successfully in your projects requires seven essential elements. Be sure you have a firm grasp of these principles. A good code generator should have the following properties: Code performance, Code quality, Open to custom code, Development tool integration, Generation speed, and Technology evolution support.

What is the potential problem with automatic code generation?

There are risks with automated code generators. It creates small alterations that could have big effects. As a result, a team will be required to manage, test, and document the metaprogramming tools, which include IDE and compile-time code generation tools and dynamic program tools that produce the actual code.

Conclusion

In this article, we have extensively discussed the concepts of design issues in designing a code generator and frequently asked questions.

After reading about the design issues of code generators, are you not feeling excited to read/explore more articles on the topic of compilers? Don't worry; Coding Ninjas has you covered. 

Recommended Reading:

Also check out some of the Guided Paths on topics such as Competitive ProgrammingComputer Networks, DBMSSystem Design, etc. as well as some Contests, Test Series and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Previous article
Code Generation
Next article
Target Machine
Live masterclass