From: | Bob Sheff <bsheff2@yahoo.com> |
Newsgroups: | comp.compilers |
Date: | 15 Jul 2003 15:34:11 -0400 |
Organization: | AT&T Worldnet |
References: | 03-07-042 |
Keywords: | translator, analysis |
Posted-Date: | 15 Jul 2003 15:34:11 EDT |
Hi,
Removal of GOTO is done all the time (by humans and) source code
translators. The simple case of GOTO is to find the <target> label,
bracket the group of statements and then logically invert the <if>
expression. An even simpler case is in assembly when the GOTO is used to
allow definition of constant/variable local storage which may just be
relocated after the GOTO is removed.
A more difficult case ( 2 pass or backward looking) is when the <target> is
ahead of the GOTO, but the procedure is similar: bracket the group, and
convert the <if> to a <DO>..<While> (or a <repeat>...<until> logically
inverted <expression>).
A further extension of the forward looking GOTO is to recognize the program
logic of an <if> exp1 then <do1> <elseif> exp2 then <do2> <else> <do3>,
where the GOTOs <target> the next <if> and the blocks <do1>and <do2> end in
a GOTO <target> past the <do3>.
Sometimes a "rats nest" of tests and GOTOs can actually be de-compiled to a
more complicated but human understandable logic expression:
if ( a>b ) goto 1;
if ( c<5 ) goto 2 => if ( a>b || c>=5 ) foo();
1 call foo
2 ...
Sometimes you can even recognize a sequence of GOTOs and code that can be
de-compiled to a switch( )..case..default construct! FORTRAN computed
GOTO also comes to mind, but that generally requires massive movement of
code into the label structure or tweaking the code to make it fit the
switch()-case, but sometimes this is what the programmer had in mind when
s/he wrote it originally.
All the time you are doing this, you must be aware of labels which allow
entry into the block you are bracketing.
GOSUBs (as I remember them) are just subroutines which must be extracted to
a procedure format and called from the code where they are invoked by
fall-into means. Of course, you must know which local variables are used,
automatically and provide them as by reference arguments when needed. This
is less of a problem with an imbedded function target language like Pascal.
What is your problem with "RETURN"?
hth
Regards, Bob
Bob Sheff, Independent consultant available for work on source
translation projects and other interesting coding.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.