1.
Introduction
2.
3.
Register Descriptors
4.
Algorithm
5.
Generating Code
6.
6.1.
Define Code Generator in compiler design.
6.2.
What is the final output received after code generation?
6.3.
Name the technique used for allocating registers efficiently.
6.4.
In how many ways can a graph be colored in compiler design?
6.5.
What is the need for multiple passes?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Code Generation

Prachi Singh
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

## Introduction

Code Generation is the last scenario of Compiler Design. Code Generation is the activity of generating assembly code/machine-readable code. Some significant properties of this activity are given as

1. Absolute quality code
2. Efficient Use of Resources
3. Quick Code

``````Example:
Let us take the three address statements a:= b+c.
It consists of the following sequence of codes.
MOV a, R0

An address descriptor is a container used for storing the location of the name of the current value so that it can be accessed during run time.

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

## Register Descriptors

A register descriptor is a container depicting that all registers are empty in their initial states.

It contains complete information about each track of the register.

Also See, Top Down Parsing

## Algorithm

The algorithm follows a queue of three address statements. The address statements are of form x: y op z simulates specific tasks.

The task description is given with the help of the following algorithm:

1. The initial step is to invoke a function getreg to capture the location L where the optimization of y op z is stored.
2. Manage the descriptive address of b to determine b'. If the value of b is present in memory and registers both, then the value of b' will be considered. If there is the absence of a value of b in L, then there is a need to generate the instruction MOV b', to copy a value of b in L.
3. A new instruction OP c' is to be generated. Updation of address descriptor of a takes place to depict that a is stored in L. If a is already present in L, then update the descriptor and remove a from all other descriptors.
4. After reaching the stage where b or c have no further uses, alter the register description. Now, the register will not store the values of b, and c.

## Generating Code

The given assignment statement w:= (x-y) + (x-z) + (x-z) can be depicted into the queue of three address codes:

``a:= x-y @  b:= x-z  @  c:= a+b   @  w:= b+c ``

Code Sequence for the above problem is given as:

### Define Code Generator in compiler design.

Code Generator determines the values that are to be stored in the registers.

### What is the final output received after code generation?

Machine code is the final output received after code generation.

### Name the technique used for allocating registers efficiently.

Linear Scan Algorithm

### In how many ways can a graph be colored in compiler design?

Every planar graph can be colored in four different ways.

### What is the need for multiple passes?

Multiple passes provide better quality code generation.

## Conclusion

Congratulations on finishing the blog!! After reading this blog, you will grasp the concept of Code Generation.

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass