0% found this document useful (0 votes)
9 views4 pages

Machine Code Generation & Optimization Guide

Machine Code Generation is the final phase of a compiler that converts optimized intermediate code into machine instructions for execution. Code Optimization improves the generated code to enhance speed, reduce size, and increase efficiency. While it offers advantages like faster execution and better CPU utilization, it also poses challenges such as complex implementation and potential risks of over-optimization.

Uploaded by

23cse151rishav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Machine Code Generation & Optimization Guide

Machine Code Generation is the final phase of a compiler that converts optimized intermediate code into machine instructions for execution. Code Optimization improves the generated code to enhance speed, reduce size, and increase efficiency. While it offers advantages like faster execution and better CPU utilization, it also poses challenges such as complex implementation and potential risks of over-optimization.

Uploaded by

23cse151rishav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Machine Code Generation and

Optimization
1. Definition
Machine Code Generation is the final phase of a compiler where the optimized intermediate
code is converted into actual machine instructions (binary or assembly) that the computer’s
CPU can execute.

Optimization means improving the generated code to make the program faster, smaller, and
more efficient.

2. Concept (Very Simple Language)


Machine Code Generation

 The compiler takes Intermediate Code (3-address code, syntax tree, etc.).
 Converts it into real machine instructions like MOV, ADD, SUB, etc.
 Assigns registers for variables.
 Converts variables into memory addresses.
 Produces final executable code.

Code Optimization

 Before generating machine code, the compiler tries to remove unnecessary steps,
 Reduce number of instructions,
 Use registers efficiently,
 Make the final output fast and small.

3. Functions of Machine Code Generation


1. Instruction Selection
Chooses the best CPU instruction (ADD, SUB, MOV).
2. Register Allocation
Decides which value goes to CPU registers (for faster execution).
3. Address Calculation
Finds the memory address of variables and constants.
4. Instruction Ordering
Arranges instructions to avoid delays and improve performance.
5. Final Code Output
Converts to assembly or binary machine code.
4. Functions of Code Optimization
1. Remove unnecessary instructions
(e.g., eliminate duplicate calculations).
2. Reduce memory usage
(use registers instead of memory).
3. Improve execution speed
(fewer instructions → faster program).
4. Improve code size
(smaller executable).
5. Enhance performance
Without changing the program’s meaning.

5. Example (Very Simple)


Intermediate Code
t1 = a + b
t2 = a + b
c = t1
d = t2

After Optimization

 Same calculation repeated twice (a+b)


 Remove duplicate work

t1 = a + b
c = t1
d = t1

Machine Code (Simple Assembly Form)


MOV R1, a
ADD R1, b
MOV c, R1
MOV d, R1

Machine code generator:

 Picks instructions (MOV, ADD)


 Uses register R1
 Produces final code
6. Types of Code Optimization
A. Local Optimization (inside a basic block)

 Constant folding
 Copy propagation
 Dead code elimination

B. Global Optimization (whole program)

 Loop optimization
 Common subexpression elimination
 Code motion

7. Advantages of Machine Code Generation


& Optimization
Advantages

1. Fast Execution
Optimized code runs faster.
2. Less Memory Usage
Minimizes memory reads/writes.
3. Smaller Program Size
Reduces number of instructions.
4. Better CPU Utilization
Efficient register use.
5. Improved Performance
Users get smoother program execution.

8. Disadvantages
1. Difficult to Implement
Optimization algorithms are complex.
2. Compilation Takes More Time
More optimization = slow compilation.
3. May Increase Compiler Cost
Advanced optimizers cost more to design.
4. Risk of Over-Optimization
If done incorrectly, code meaning may change.
5. Machine Dependent Issues
Code generator must know CPU architecture.

You might also like