Mohsen
Mohsen

Reputation: 11

Backpatch to flow-of-control of FOR LOOP

I am looking for Backpatching in FOR-LOOP.

I know this approach at IF-THEN-ELSE is in this way:

IF '(' expr M ')' stmt N ELSE L stmt L
{
   backpatch($4, $9 - $4);
   backpatch($7, $11 - $7);
}

You can use these markers in your answer:

FOR '(' expr ';' L expr M N ';' L expr N ')' L stmt N L

Please explan your answer.

Upvotes: 0

Views: 744

Answers (1)

Mohsen
Mohsen

Reputation: 11

At FOR '(' expr1 ';' L1 expr2 ';' L2 expr3 N1 ')' L3 stmt N2 we will have this scenario:

backpatch( $14 , $8 - $14 );
backpatch( $10 , $5 - $10 ); 
backpatchlist( $6.truelist , $12 );
backpatchlist( $6.falselist , pc );
  1. First line: When we are at end of LOOP (N2), we must jump to first of Increment Section of the loop (L2) to compute expr3
  2. Second line: After computing expr3, must jump to first of Test Expression section expr2
  3. Third line: if evaluation of expr2 is true, jump to first of LOOP's Body (L3) to compute stmt.
  4. Fourth line: if evaluation of expr2 is false, processing the loop has been finished and we must jump the first instruction after LOOP which is accessible by pc.

In this condition the markers must be defined as this:

L1,L2,L3 :  { 
              $$ = pc;
            }

N1       :  {
             emit(pop);
             $$ = pc;
             emit3(goto_, 0);
            }

N2       : { 
             $$ = pc;
             emit3(goto_, 0);
           }

Upvotes: 1

Related Questions