Intermediate code: expressions, control flow और backpatching
लक्ष्य और conventions
Expressions और Boolean control flow को explicit intermediate instructions में बदलें। Three-address code (TAC) में सामान्यतः एक instruction पर अधिकतम एक elementary operator होता है, जैसे t=a+b, x=t, if x<y goto L, goto L। Labels instructions पहचानते हैं, source lines नहीं। Temporaries intermediate values के नाम हैं। उदाहरणों में pure integer arithmetic मानें, जब तक side effect अलग से न दें; Boolean उदाहरण में division-by-zero महत्वपूर्ण है।
1. Expressions और representations
x=(a+b)(c-d) के लिए t1=a+b; t2=c-d; t3=t1t2; x=t3 निकालें। Representation अनुमति दे तो अंतिम multiply सीधे x=t1*t2 भी हो सकता है। इसलिए count के प्रश्न में अलग final copy जरूरी है या नहीं, बताना चाहिए।
Quadruple (op,arg1,arg2,result) रखता है। पहली instructions (+,a,b,t1), (-,c,d,t2) हैं। Triple (op,arg1,arg2) रखता है और named result के बजाय instruction position refer करता है। Zero-based positions में 0:(+,a,b), 1:(-,c,d), 2:(*,(0),(1))। Indirect triples में अलग ordering list होती है, ताकि movement पर सभी references फिर number न करने पड़ें। (0) result reference है, literal integer zero नहीं।
2. Array address
One-dimensional A में A[L] का base B और element width w हो तो address(A[i])=B+(i−L)w। B=1000,L=1,w=4,i=6 पर offset=(6−1)4=20, address=1020। Base को width से multiply न करें।
Row-major 2D array में lower bounds Lr,Lc तथा N columns हों तो address(A[i,j])=B+((i−Lr)N+(j−Lc))w। 3×5 array, lower bounds 0,0, B=2000,w=8 में A[2,3] का element offset 2*5+3=13 और byte address 2104 है। Column-major में stride अलग होगा; layout स्पष्ट होना जरूरी है। भाषा में bounds checks हों तो वे address formula से अलग operations हैं।
3. Short-circuit Boolean translation
if (a!=0 && b/a>2) x=1; else x=0; का short-circuit TAC:
1: if a!=0 goto 3 2: goto 7 3: t=b/a 4: if t>2 goto 6 5: goto 7 6: x=1; goto 8 7: x=0 8: next statement
पढ़ने में सरलता के लिए label 6 पर assignment और branch साथ दिखाए हैं; instruction count में दो TAC instructions करें। a=0 पर path 1→2→7→8 है और b/a evaluate नहीं होता। a=2,b=8 पर 1→3→4→6→8 है, x=1 होता है और false assignment नहीं चलता। && से पहले दोनों operands eagerly निकालना short-circuit semantics तोड़ता है और zero से divide करा सकता है।
B1 || B2 में B1 true हो तो B2 नहीं चलता, false हो तो B2 चलता है। B1 && B2 में B1 true होने पर B2 चलता है, false पर छोड़ते हैं। Logical negation true/false branch lists बदल सकती है; arithmetic negation करना आवश्यक नहीं।
4. Backpatching
जिन jumps के targets अभी नहीं मिले, उनकी instruction indices सूची में रखें। makelist(i) singleton list, merge दो lists का जोड़ और backpatch(list,L) हर listed jump का target L भरता है। List में jump locations हैं, condition की tested values नहीं।
i,i+1 पर relational test “if a<b goto _” और “goto _” दे तो truelist={i}, falselist={i+1}। B=B1&&B2 के लिए B2 की start m याद रखें, B1.truelist को m पर patch करें; B.truelist=B2.truelist तथा B.falselist=merge(B1.falselist,B2.falselist)। OR में B1.falselist को B2 start पर patch करें, truelists merge करें और B2.falselist रखें। Marker nonterminal या saved next-instruction index से m मिल सकता है।
सावधानियाँ
TAC representation है, source का एक ही अनिवार्य output नहीं। Side effects, exceptions और भाषा का evaluation order बचना चाहिए। Short-circuit तथा bitwise operators अलग हैं। Target label abstract IR instruction address है, condition की value नहीं। Signed overflow, floating point, aliasing और bounds checks बाद की optimization सीमित करते हैं; intermediate code होने से इन्हें न हटाएँ।
मौलिक अभ्यास: छह single-correct MCQ
ये मौलिक शिक्षण प्रश्न हैं, पिछले वर्षों के प्रश्न नहीं। ठीक एक उत्तर चुनें.
P01. Named temporaries और अनिवार्य final copy के साथ x=(a+b)*(c-d) की दी sequence में कितनी TAC instructions हैं?
A. 2 B. 3 C. 4 D. 5
उत्तर: C
A: तीन arithmetic और copy चाहिए।
B: तीन में जरूरी copy छूटती है।
C: दो subexpressions, multiply, copy: चार।
D: पाँचवीं operation जरूरी नहीं।
P02. A[1] का address 1000 और width 4 bytes है। A[6] का address?
A. 1006 B. 1020 C. 1024 D. 4006
उत्तर: B
A: Index difference scale होगा।
B: 1000+(6−1)×4=1020।
C: यह lower bound zero मानता है।
D: Base को width से multiply नहीं करते।
P03. Row-major A[3][5], lower bounds zero, base 2000, width 8 में A[2][3]?
A. 2040 B. 2072 C. 2104 D. 2120
उत्तर: C
A: इसमें row stride के बदले i+j है।
B: पाँच-column stride सही नहीं लगा।
C: Offset (2×5+3)×8=104 bytes है।
D: 15 total elements हैं, इस element का offset नहीं।
P04. a!=0 && b/a>2 में a=0 हो तो सही short-circuit behavior?
A. पहले b/a निकाले B. b/a छोड़कर false C. b/a छोड़कर true D. हमेशा divide-by-zero error
उत्तर: B
A: पहले बायाँ operand जाँचना है।
B: False बायाँ operand AND तय कर देता है।
C: False AND कुछ भी false है।
D: खतरनाक expression चलता ही नहीं।
P05. B1&&B2 में कौन-सी list B2 की शुरुआत पर patch होगी?
A. B1.truelist B. B1.falselist C. B2.truelist D. B2 की दोनों lists
उत्तर: A
A: पहला true हो तो दूसरा जाँचना है।
B: पहला false हो तो B2 छोड़ते हैं।
C: यह B2 के result की list है।
D: ये B2 के बाद के outcomes हैं।
P06. Triple operand (0) में 0 क्या दर्शाता है?
A. हमेशा literal zero B. zero नाम का source variable C. Triple position 0 का result D. अनिवार्य null pointer
उत्तर: C
A: यहाँ parentheses result reference बताते हैं।
B: ऐसा variable नहीं दिया।
C: Triples पिछले results positions से refer करते हैं।
D: यह null pointer notation नहीं।
स्रोत और सीमा
Official 2026 course के अनुसार विषय का सीमित भाग। 2027 की पूर्णता का दावा नहीं। सभी उदाहरण तथा अभ्यास मौलिक हैं.
प्रगति सहेजने के लिए साइन इन करें। साइन इन