Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  A Simple Code Generator - Code Generation, Computer Science and IT Engineering

A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

A Simple Code Generator

NEXT-USE INFORMATION

  • If the name in a register is no longer needed, then we remove the name from the register and the register can be used to store some other names.

Input: Basic block B of three-address statements

Output: At each statement i: x = y op z, we attach to i the liveliness and next-uses of x, y and z.

Method: We start at the last statement of B and scan backwards.

1.  Attach to statement i the information currently found in the symbol table regarding the next-use and liveliness of x, y and z.

2.   In the symbol table, set x to “not live” and “no next use”.

A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE)

A SIMPLE CODE GENERATOR

  • A code generator generates target code for a sequence of three- address statements and

effectively uses registers to store operands of the statements.

  • For example: consider the three-address statement a := b+c It can have the following sequence of codes:

ADD Rj, Ri Cost = 1

(or)

ADD c, Ri Cost = 2

(or)

MOV c, Rj Cost = 3

ADD Rj, Ri

 

Register and Address Descriptors:

  • A register descriptor is used to keep track of what is currently in each registers. The register descriptors show that initially all the registers are empty.
  • An address descriptor stores the location where the current value of the name can be found at run time.

 

A code-generation algorithm:

The algorithm takes as input a sequence of three-address statements constituting a basic block. For each three-address statement of the form x : = y op z, perform the following actions:

1.   Invoke a function getreg to determine the location L where the result of the computation y op z should be stored.

2.     Consult the address descriptor for y to determine y’, the current location of y. Prefer the register for y’ if the value of y is currently both in memory and a register. If the value of y is not already in L, generate the instruction MOV y’ , L to place a copy of y in L.

3.    Generate the instruction OP z’ , L where z’ is a current location of z. Prefer a register to a memory location if z is in both. Update the address descriptor of x to indicate that x is in location L. If x is in L, update its descriptor and remove x from all other descriptors.

4. If the current values of y or z have no next uses, are not live on exit from the block, and are in registers, alter the register descriptor to indicate that, after execution of x : = y op z , those registers will no longer contain y or z

 

Generating Code for Assignment Statements:

  • The assignment d : = (a-b) + (a-c) + (a-c) might be translated into the following three-address code sequence:

Code sequence for the example is:

A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE)
A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) 

Generating Code for Indexed Assignments

The table shows the code sequences generated for the indexed assignmen a:= b[ i ] and a[ i ]:= b

A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) 

Generating Code for Pointer Assignments

The table shows the code sequences generated for the pointer assignments a : = *p and *p : = a

A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE)

if x < 0 goto z ADD z, R0

MOV R0,x

CJ< z

The document A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on A Simple Code Generator - Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is a code generator?
Ans. A code generator is a tool or software that automates the process of generating source code or program code. It helps developers in saving time and effort by automatically creating code based on predefined templates or specifications.
2. What is code generation in computer science and IT engineering?
Ans. Code generation is a process in computer science and IT engineering where source code or program code is automatically created using code generators. It involves transforming high-level representations of a program, such as models or specifications, into executable code in a specific programming language.
3. How does a code generator work?
Ans. A code generator works by taking input from the developer in the form of specifications, models, or templates. It analyzes the input and applies predefined rules and transformations to generate code in a specific programming language. The generated code can then be further customized or modified by the developer as per the requirements.
4. What are the benefits of using a code generator?
Ans. Using a code generator offers several benefits, including: - Increased productivity: Code generation automates the repetitive task of writing boilerplate code, saving time and effort. - Consistency: Code generators ensure consistency in coding standards and practices, as they generate code based on predefined templates or specifications. - Reduced errors: By automating code generation, the chances of human errors in manual coding are minimized. - Customizability: Code generators provide flexibility to customize the generated code as per specific requirements. - Faster development: With code generation, developers can quickly generate code for common functionalities, allowing them to focus on more complex aspects of the software development process.
5. What are some popular code generation tools?
Ans. Some popular code generation tools used in computer science and IT engineering include: - Eclipse Modeling Framework (EMF): It is a powerful framework for building domain-specific modeling tools that can generate code based on models. - Apache Velocity: It is a template engine that can be used for code generation by defining templates with placeholders to be replaced with actual code. - Yeoman: It is a code generator tool that provides a wide range of generators for different programming languages and frameworks. - ANTLR: It is a powerful parser generator that can be used for generating code based on grammar specifications. - JHipster: It is a popular code generator specifically designed for generating modern web applications using Java, Spring Boot, and Angular/React/Vue.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Sample Paper

,

ppt

,

Extra Questions

,

A Simple Code Generator - Code Generation

,

Summary

,

study material

,

video lectures

,

A Simple Code Generator - Code Generation

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

Viva Questions

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

A Simple Code Generator - Code Generation

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

past year papers

,

mock tests for examination

,

pdf

,

Free

,

Semester Notes

,

MCQs

,

Previous Year Questions with Solutions

,

Important questions

,

Objective type Questions

,

Exam

;