Intermediate Code Generation: Case Statements | इंटरमीडिएट कोड जनरेशन में केस स्टेटमेंट्स


इंटरमीडिएट कोड जनरेशन में केस स्टेटमेंट्स (Case Statements in Intermediate Code Generation)

Case Statements या Switch Statements किसी प्रोग्राम के नियंत्रण प्रवाह (control flow) को व्यवस्थित करने के लिए उपयोग किए जाते हैं। ये कई स्थितियों में निर्णय लेने के लिए एक कुशल संरचना प्रदान करते हैं। Compiler Design में, इन Statements का Intermediate Code Generation बहुत सावधानी से किया जाता है ताकि Run-Time पर Decision-Making तेज़ और Memory-Efficient हो।

परिचय (Introduction)

Case या Switch Statements का उपयोग तब किया जाता है जब किसी Expression का मान कई संभावित विकल्पों में से एक के बराबर हो सकता है। उदाहरण के लिए:

switch (x) {
  case 1: a = 10; break;
  case 2: a = 20; break;
  default: a = 0;
}

Compiler को इसे Intermediate Representation (IR) में बदलना होता है ताकि वह Optimization और Machine Code Generation के लिए उपयुक्त हो।

Switch / Case Statement की संरचना (Structure of Switch/Case Statement)

  • एक Selector Expression (जैसे x)।
  • कई Case Labels जो अलग-अलग Values के लिए क्रियाएँ दर्शाते हैं।
  • एक वैकल्पिक Default Label

सामान्य रूप:

switch (expression) {
   case value1: statement1;
   case value2: statement2;
   ...
   default: statementN;
}

Case Statements के Intermediate Code Generation के चरण (Steps in Intermediate Code Generation)

  1. Expression Evaluation: Switch Expression का मूल्य निकाला जाता है।
  2. Label Creation: प्रत्येक Case के लिए Label Generate किया जाता है।
  3. Jump Table Construction: Compiler Jump Table बनाता है जिससे Control संबंधित Case पर स्थानांतरित हो सके।
  4. Intermediate Code Generation: Conditional Jumps या Direct Jumps के रूप में कोड बनाया जाता है।
  5. Optimization: Redundant Jump हटाकर Code Compact किया जाता है।

Intermediate Code Example

switch (x) {
   case 1: y = 10; break;
   case 2: y = 20; break;
   default: y = 0;
}

Intermediate Code Representation:

t1 = x
if t1 == 1 goto L1
if t1 == 2 goto L2
goto L3
L1: y = 10
goto L4
L2: y = 20
goto L4
L3: y = 0
L4:

Jump Table Representation

Jump Table एक ऐसी Data Structure होती है जिसमें प्रत्येक Case Value के लिए संबंधित Label Address स्टोर होता है। यह Approach Sequential Jumps से कहीं अधिक तेज़ होती है।

Jump Table Example:

t1 = x
if t1 < 1 or t1 > 3 goto L4
goto Table[t1]
Table:
1 -> L1
2 -> L2
3 -> L3

Case Statements के लिए Optimization Techniques

  • Jump Table Optimization: Case Labels को Continuous Range में Map किया जाता है।
  • Binary Search on Case Labels: Sparse Case Values के लिए Binary Search लागू किया जाता है।
  • Dead Code Removal: Unreachable Case Blocks हटाए जाते हैं।
  • Common Subexpression Elimination: Repeated Evaluations से बचा जाता है।

Complex Example

switch (x + y) {
  case 3: z = 1; break;
  case 5: z = 2; break;
  default: z = 0;
}

Intermediate Code:

t1 = x + y
if t1 == 3 goto L1
if t1 == 5 goto L2
goto L3
L1: z = 1
goto L4
L2: z = 2
goto L4
L3: z = 0
L4:

Backpatching in Case Statements

जब Compiler Intermediate Code बनाते समय Jump Targets को तुरंत निर्धारित नहीं कर पाता, तब वह Backpatching तकनीक का उपयोग करता है। यह तकनीक Jump Addresses को बाद में भरने के लिए Placeholder रखती है।

Backpatching Example:

switch (a) {
   case 1: b = 2; break;
   case 2: b = 4; break;
   default: b = 0;
}

Initially Compiler इस प्रकार Placeholder कोड बनाता है:

if a == 1 goto _
if a == 2 goto _
goto _

Backpatching के बाद Final Intermediate Code Labels भर दिए जाते हैं।

Case Statements में Control Flow Graph (CFG)

Compiler Case Statements को Control Flow Graph के रूप में प्रदर्शित करता है जहाँ प्रत्येक Case Block एक Node के रूप में कार्य करता है और Default Case को एक Exit Node के रूप में जोड़ा जाता है।

  • Start Node → Expression Evaluation
  • Decision Nodes → प्रत्येक Case के लिए Branch
  • Merge Node → Control वापस Main Flow में आता है

Advantages of Intermediate Representation in Case Statements

  • Machine Independent Representation
  • Optimization Friendly
  • Efficient Control Flow
  • Fast Jump Resolution

निष्कर्ष (Conclusion)

Compiler Design में Case Statements के Intermediate Code Generation का उद्देश्य है — विभिन्न विकल्पों (cases) के बीच कुशल Decision-Making सुनिश्चित करना। यह Jump Tables, Conditional Jumps, और Backpatching जैसी तकनीकों का उपयोग कर रनटाइम प्रदर्शन में सुधार करता है। Compiler इन Statements को Machine-Independent रूप में प्रस्तुत करके Code Optimization और Execution Speed दोनों को बढ़ाता है।

Related Post