Intermediate Code :
Intermediate Code are machine independent code, but they are close to machine instruction. Syntax tree, Postfix notation, 3-address code, DAG can be used as intermediate language.
Need for Intermediate Code:
- Suppose we have x no of the source language and y no of the target language:
- Without ICG – we have to change each source language into target language directly, So, for each source-target pair we will need a compiler .Hence we need (x*y) compilers, which can be a very large number and which is literally impossible.
- With ICG – we will need only x number of compiler to convert each source language into intermediate code. We will also need y compiler to convert the intermediate code into y target languages.
so, we will need only (x+y) no of the compiler with ICG which is way lesser than x*y no of the compiler.
- Re-targeting is facilitated :
a compiler for a different machine can be created by attaching a back-end(which generate target code) for the new machine to an existing front-end(which generate intermediate code).
- Machine independent:
A Machine independent code-optimizer can be applied to the intermediate code. So this can be run on any machine.
Intermediate code is simple enough to be easily converted to any target code. So ICG reduces the overhead of target code generation.
- Complexity :
Intermediate code is Complex enough to represent all complex structure of high-level languages.
We can easily modify our code to get better performance and throughput by applying optimization technique to the Intermediate code.
Code Optimization :
Code Optimization applied on target code(assembly code) is a transformation technique which use to improve the code by consuming fewer resources. Code optimization is of 3 types :
- Machine-dependent optimization – Optimization applied after the target code(assembly code) generation is machine-dependent optimization.
- Machine-independent – In this, the compiler transforms a part of the intermediate code that does not involve any CPU registers and/or absolute memory locations. e.g, 3-address code.
- Local Optimization – The optimization which is performed within the block(sequence of consecutive statements) is local optimization e, g. If-Else, switch-case, conditional statements and loops such as Do-While, For, and repeat-until, etc.
Need for Code optimization:
- Loop frequency reduction:
Loop optimization is the process of optimization within the loop. This reduces the evaluation frequency of the expression and brings loop-invariant statements out of the loop.
- Dead code elimination:
It removes unnecessary instruction without changing the behavior of the code.
Code optimization increases the speed of the program.
After code optimization our program demands less no of resources thus it saves our resource(i.e, cpu, memory) for other programmer.
- neat and clean code:
After removing the common sub expression and redundant code our code becomes the clean code.
- Strength reduction:
Strength reduction means replacing costly operator by simple (cheap/low strength) operator.
- To execute less number of iteration we use code optimization.
- To combine the bodies of two loops whenever they are sharing same index variable, i.e – for loop jamming or loop fusion is a code optimization.
- Intermediate Code Generation in Compiler Design
- Frequency Reduction in Code Optimization
- Code Optimization in Compiler Design
- Code Converters - Binary to/from Gray Code
- What is Google Code-In?
- Three address code in Compiler
- DFA in LEX code which accepts strings ending with 11
- Register Allocations in Code Generation
- Hamming Code in Computer Network
- Code Converters - BCD(8421) to/from Excess-3
- Issues in the design of a code generator
- Introduction of Object Code in Compiler Design
- Lex code to count total number of tokens
- Compiler Design | Detection of a Loop in Three Address Code
- Computer Organization | Locality and Cache friendly code
- Lex code to replace a word with another word in a file
- DFA in LEX code which accepts Odd number of 0’s and even number of 1’s
- Loop Optimization Techniques | Set 2
- Introduction to Ant Colony Optimization
- Loop Optimization in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.