4 Intermediate Code Generation Code Generation And Optimizatio

Compiler Design and Construction · Unit 4 · 16 hrs

Intermediate Code Generation, Code Generation and Optimization

Exam-focused notes for Intermediate Code Generation, Code Generation and Optimization (Compiler Design and Construction, CSC376): what the TU syllabus asks and how it has actually been tested, with 19 solved past questions from this unit.

What this unit covers

  • Intermediate Code Generator: High-level and Low-level Intermediate representation
  • Syntax tree & DAG representations
  • Three-address code
  • Quadruples
  • Triples
  • SDT for intermediate code
  • Intermediate code generation for Declarations, Assignments, Control Flow, Boolean Expressions and Procedure Calls
  • Back patching
  • Code Generator: Factors affecting a code generator
  • Target Language
  • Basic blocks and flow graphs
  • Dynamic programming code-generation algorithm
  • Code Optimization: Need and criteria of Code Optimization
  • Basic optimization techniques
  • Case Studies of some compilers like C compiler, C++ complier

Intermediate Code Generator

208110 marks

What are the significances of intermediate code? Differentiate between DAG and Syntax tree. Represent the instruction A = B + C - D * E + G using quadruple and triple.[10]

- Task 1: Significances of intermediate code (conceptual) - Task 2: Differentiate DAG vs Syntax tree (conceptual) - Task 3: Expression to represent using quadruple and triple: $$A = B + C - D E + G$$ Operator precedence assumption: binds tighter than +/-; +...

Full solved answer →
20766 marks

Explain with example about different methods of intermediate code representation.[6]

Intermediate code is a machine-independent representation of the source program, generated by the front end of the compiler and used as input to the back end (code optimization and code generation phases). It acts as a bridge between the source language and...

Full solved answer →

Back patching

208110 marks

Illustrate the concept of backpatching with an example. Convert the regular expression a(a + b)a# to DFA.[10]

Backpatching is a technique used during single-pass code generation for handling forward jumps whose target addresses are not yet known when the jump instruction is generated. When we emit a conditional or unconditional jump, the destination label may still...

Full solved answer →

Dynamic programming code-generation algorithm

20815 marks

Write the code generation algorithm for the instruction a = b op c. [5]

Code generation is the final phase of compilation that maps intermediate code (three-address instructions) to target machine instructions. For a three-address instruction of the form a = b op c, the code generator must decide: - Which registers to use for o...

Full solved answer →

Basic optimization techniques

20815 marks

What are the techniques for compiler optimization? Explain. [5]

Code optimization is the process of improving intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and arranges statements to speed up execution without wasting resources. --- As per the ...

Full solved answer →

Basic blocks and flow graphs

20805 marks

How do you recognize basic block? Discuss about the factors that affect code generator. [5]

--- A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without any halt or possibility of branching except at the end. First, we identify leaders (the first statement of a basic block...

Full solved answer →
2081.110 marks

Explain the optimization techniques for code optimization. Convert the following program to basic block and control flow.

$M = A + B$

$N = C + D$

$\text{IF } (M > N) \rightarrow X = M - N;$

$\text{ELSE } E = M + N + X$

[10]

Program statements: (The repeated LaTeX text M=A+B, N=C+D etc. is just rendering duplication; the actual program has one statement each for M and N.) --- Code optimization transforms a program so it runs faster and/or uses less memory without changing its m...

Full solved answer →

Code Optimization

20805 marks

Why code optimization is needed? Describe any two techniques for loop optimization. [5]

Code optimization is the process of transforming a program to make it more efficient without changing its output or meaning. It is needed because: - Reduces space and increases speed: Optimization reduces the memory consumed by the program and increases the...

Full solved answer →
20785 marks

What is the advantages of code optimization? Explain about dead-code elimination. [5]

--- Code optimization is the process of improving intermediate code so that the output program runs faster and takes less memory space. Its main advantages are: 1. Improved Execution Speed: It arranges the sequence of statements in order to speed up program...

Full solved answer →
20766 marks

What is the purpose of code optimization? Explain different types of loop optimization techniques with example.[6]

Code optimization is the process of improving the intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and arranges the sequence of statements to speed up program execution without wastin...

Full solved answer →
20756 marks

Define code optimization. Discuss about any three code optimization techniques with example.[6]

Code optimization is the process of improving the intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and rearranges the sequence of statements to speed up program execution without wast...

Full solved answer →

Three-address code

20805 marks

What is three-address code? How high level code is converted to three address code? Illustrate with an example. [5]

Three-address code is an intermediate code representation where each instruction uses at most three addresses: two for operands and one for the result. Each instruction in three-address code is described as a 4-tuple: (operator, operand1, operand2, result) ...

Full solved answer →
20756 marks

Define three address codes. write three address codes for S = do m = n + p while a <= b [6]

Given data (from the question): - Statement to translate: S = do m = n + p while a <= b - Structure: a do ... while loop with body m = n + p and condition a <= b. No numeric matrices or values are involved; this is a compiler-design (intermediate code gener...

Full solved answer →

Quadruples

20785 marks

Define three address codes. Write down Quadruples for: $a = -b*(c+d)/e$. [5]

- Expression to translate: $a = -b (c + d) / e$ - Required output format: Quadruples - Also required: Definition of three address code No numeric data is involved; this is a code-generation problem. All required information is present. --- Three address cod...

Full solved answer →

Code Generator

20785 marks

Explain about the factors affecting target code generation. [5]

Target code generation is the final phase of compilation that translates optimized intermediate code into machine-level instructions for a specific target machine. Several factors influence how efficient and correct the generated target code will be. --- Th...

Full solved answer →
20766 marks

Discuss about the different factors affecting target code generation.[6]

Target code generation is the final phase of compilation that translates optimized intermediate code into target machine language. The quality and correctness of the generated code depends on several important factors. --- - The code generator takes optimiz...

Full solved answer →
20756 marks

Discuss about the different factors affecting target code generation.[6]

Target code generation is the final phase of compilation that translates optimized intermediate code into target machine code. Several important factors influence this process. --- The input to the code generator is the optimized intermediate code produced ...

Full solved answer →

Syntax tree & DAG representations

2081.110 marks

Discuss about Directed Acyclic Graph with an example. Represent the expression A = (B + C) - (D - E) using 3AC, Quadruple and Triple.[10]

--- A Directed Acyclic Graph (DAG) is a graph representation of an expression that, like a syntax tree, has: - Leaf nodes representing operands (identifiers/constants) - Interior nodes representing operators The key difference is that a DAG shares nodes for...

Full solved answer →

Intermediate code generation for Declarations, Assignments, Control Flow, Boolean Expressions and Procedure Calls

2081.15 marks

What are the advantages of intermediate code? How do you convert procedure call to 3AC? [5]

--- The key advantages of using intermediate code representation are: 1. Portability / Machine Independence - If a compiler translates source language directly to target machine language without intermediate code, then for each new machine a full native com...

Full solved answer →