Table of contents
1.
Introduction
2.
Address and Register Descriptors
3.
Features of a Code Generator
4.
Limitations Incurred in a Code Generator
5.
Frequently Asked Questions
5.1.
Define Code Generator in compiler design.
5.2.
What is the final output received after code generation?
6.
Conclusion
Last Updated: Mar 27, 2024

Code Generator

Author Dhruv Sharma
0 upvote
Compiler Design

Introduction

Code Generator is one of the core components involved during the process of Code generation while compilation is taking place. Since Code Generation is the last phase of generating assembly code/machine-readable code during compilation. 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 


The code generator ensures that the code generation is efficiently happening using fast memory accesses through memory registers instead of cache. Since registers are an expensive memory resource, they are limited and must be used effectively. Performing different operations on registers is much more efficient as registers are faster than cache memory. Therefore, to try and use a minimum number of registers to incur a minimal overall cost, a 'Code generator is used to generate the target code for three-address statements to store the intermediate operands in which registers play a crucial role.

Also See, Top Down Parsing

Address and Register Descriptors

Consider the following three address statements -

a:= b + c


It can have the following sequence of codes:

register descriptor contains the track of what is currently in each register. It shows that all the registers are initially empty.

And an address descriptor that is used to store the location where one can find the current value of the name at run time.

The code generator then uses these descriptors to keep track of the executed set of instructions efficiently in the limited set of registers.

Features of a Code Generator

A Code-Generator takes care of the following aspects of the code generation process:

  1. It takes a basic instruction block and transforms it into simple three-address instructions if it already is not in that format.

Example:

For an instruction P1: x = y + z * w

A code generator performs the following optimizations to change it to a three-address instruction format:

q0 = z * w

x  = y + q0

2. It uses memory registers to move, store and perform operations on data so that one can use fast and accessible storage.

3. Due to the use of registers, the overall computation time gets reduced.
 

Read about Instruction Format in Computer Architecture

Limitations Incurred in a Code Generator

The following are some of the disadvantages or limitations of optimizations through a code generator:

  1. The size of the memory of the registers is generally available in small amounts ( up to a few hundred Kbs only )
  2. During intermediate operations such as context-switch and procedure calls, one needs to save and restore changes that affect the overall performance.

Frequently Asked Questions

Define Code Generator in compiler design.

Code Generator determines the values that are to be stored in the registers through address and register descriptors on three-address formatted instructions.

What is the final output received after code generation?

It could yield both Assembly/Machine code based on the requirements and configurations as the final output after the code generation.

Conclusion

Congratulations on making it to the end of the blog!! 

After going through this article, one would be able to understand the functionality and the role of a Code Generator used during the process of code generation by a compiler. To better understand the whole code generation process, you can take a deep dive here.

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.

Live masterclass