Table of contents
1.
Introduction
2.
Address Descriptor
3.
Register Descriptors
4.
Algorithm
5.
Generating Code
6.
Frequently Asked Questions
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

Author Prachi Singh
0 upvote
Compiler Design

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
ADD b, R0

Also see, Phases of Compiler, Lexical Analysis in Compiler Design

Address Descriptor

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.

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.

Also read About, Specifications of Tokens in Compiler Design

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:

Statement

Code Generation

Register Descriptor

Address Descriptor

a:= x-y 

 

MOV x, R0

SUB y, R0

R0 contains a

a in R0

  b:= x-z  

 

MOV x, R1

SUB z, R1

R0 contains a

R1 contains b

a in R0

b in R1

  c:= a+b  

 

ADD R1, R0

R0 contains c

R1 contains b

c in R1

b in R1

w:= b+c  

 

ADD R1, R0

MOV R0, w

R0 contains w

w in R0

w in R0 and memory

 

 

Frequently Asked Questions

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.

Recommended Reading:

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