Intermediate Code Generation: Declarations | इंटरमीडिएट कोड जनरेशन में घोषणाएँ


इंटरमीडिएट कोड जनरेशन में घोषणाएँ (Declarations in Intermediate Code Generation)

Intermediate Code Generation Compiler Design का एक प्रमुख चरण है जिसमें प्रोग्राम का High-Level Source Code एक ऐसी रूपरेखा में परिवर्तित किया जाता है जो मशीन कोड के क़रीब होती है लेकिन अभी भी मशीन स्वतंत्र (machine independent) होती है। यह रूपरेखा Intermediate Code कहलाती है, और इसे Compiler Optimization तथा Code Generation दोनों के लिए आधार माना जाता है।

परिचय (Introduction)

Intermediate Code का निर्माण Compiler के Front-End और Back-End के बीच की एक कड़ी (bridge) है। इसका उद्देश्य है – भाषा की संरचना (syntax) से स्वतंत्र लेकिन सटीक कोड का निर्माण करना ताकि उसे किसी भी मशीन आर्किटेक्चर पर आसानी से ट्रांसलेट किया जा सके।

जब Compiler Intermediate Code बनाता है, तो उसे यह तय करना होता है कि प्रोग्राम में घोषित किए गए सभी Variables, Constants, Arrays, Structures, और Functions के लिए मेमोरी और डेटा प्रकार कैसे प्रबंधित किए जाएँ। इस प्रक्रिया को Declaration Handling कहते हैं।

घोषणाएँ (Declarations) क्या होती हैं?

Declaration Compiler को यह बताती है कि कौन से Identifiers (variables, arrays, structures, etc.) प्रोग्राम में उपयोग होंगे, और उनका डेटा प्रकार, आकार, तथा Scope क्या होगा। Compiler इन घोषणाओं के आधार पर Symbol Table में एंट्री बनाता है।

उदाहरण:

int a, b;
float c;
char d;

यहाँ Compiler इन सभी variables के लिए Symbol Table में Type, Size, और Address Store करता है।

Intermediate Code Representation (मध्यवर्ती कोड रूप)

Compiler Declarations को Intermediate Representation (IR) में परिवर्तित करता है ताकि आगे Code Generation में उसका उपयोग किया जा सके। IR आमतौर पर तीन रूपों में होता है:

  • Three-Address Code (TAC)
  • Quadruples / Triples
  • Abstract Syntax Trees (AST)

उदाहरण (Three-Address Code):

int a, b;
a = 10;
b = a + 5;

Intermediate Representation:

t1 = 10
a = t1
t2 = a + 5
b = t2

Declaration Handling की प्रक्रिया (Process of Handling Declarations)

Compiler Declaration Handling के दौरान निम्नलिखित चरणों का पालन करता है:

  1. Parsing: Syntax Tree के माध्यम से Declaration को पहचाना जाता है।
  2. Symbol Table Insertion: प्रत्येक Identifier की एंट्री Symbol Table में जोड़ी जाती है।
  3. Type Checking: Type Compatibility का परीक्षण किया जाता है।
  4. Intermediate Code Generation: Memory Initialization और Storage Allocation के लिए कोड बनाया जाता है।

Symbol Table में Declaration की भूमिका

Symbol Table Declaration Phase का सबसे महत्वपूर्ण हिस्सा है। Compiler प्रत्येक Declared Variable के बारे में निम्नलिखित जानकारी रखता है:

  • Variable Name
  • Data Type
  • Memory Location / Address
  • Scope (Local / Global)
  • Storage Class (static, auto, extern)

उदाहरण:

int a;
float b;

Symbol Table Entries:

NameTypeAddressScope
aint1000global
bfloat1004global

Declarations के Intermediate Code Patterns

Compiler Declarations के लिए Intermediate Instructions उत्पन्न करता है जो Memory Allocation और Initialization को दर्शाते हैं।

उदाहरण:

int a = 5;
float b;

Intermediate Code (TAC):

a = 5
declare b, float

Declarations के प्रकार (Types of Declarations)

  • Variable Declarations: डेटा टाइप और नाम घोषित करना।
  • Array Declarations: निश्चित आकार की मेमोरी ब्लॉक बनाना।
  • Structure Declarations: User-defined डेटा प्रकारों की परिभाषा।
  • Function Declarations: Function Signature की परिभाषा।

Array Declaration Example:

int arr[5];

Intermediate Representation:

declare arr, array(5), int

Memory Allocation in Declarations

Compiler Static Allocation (for global variables) और Dynamic Allocation (for local or heap variables) के बीच निर्णय लेता है।

Static Allocation Example:

int x = 10;

Dynamic Allocation Example:

int *p = malloc(10 * sizeof(int));

Code Generation Issues with Declarations

  • Data Alignment and Padding
  • Storage Class and Scope Management
  • Array Index Calculation
  • Type Conversion and Compatibility
  • Cross-Referencing in Nested Scopes

Declarations में Optimization

Compiler Redundant Declarations को हटाकर और Constant Folding लागू करके Optimization करता है।

  • Dead Variable Elimination
  • Constant Propagation
  • Memory Reuse in Basic Blocks

Intermediate Code for Declarations (Extended Example)

int a = 5, b = 10;
float c;
c = a + b;

Intermediate Code:

declare a, int
declare b, int
declare c, float
a = 5
b = 10
t1 = a + b
c = t1

निष्कर्ष (Conclusion)

Intermediate Code Generation के Declaration Handling चरण में Compiler यह सुनिश्चित करता है कि प्रत्येक Variable और Data Structure के लिए उचित Memory, Type और Scope निर्धारित हों। यह Compiler के अगले चरणों — जैसे Type Checking, Code Generation, और Optimization — के लिए एक ठोस आधार तैयार करता है। Declarations को सही ढंग से संभालना Compiler की दक्षता और Accuracy दोनों के लिए अत्यंत महत्वपूर्ण है।

Related Post