Flow Control - Determining conditions for VM Code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Flow Control - Determining conditions for VM Code

peterxu422
On page 233 Fig. 11.5, I am wondering why for the VM Code portion why in general the ~(cond) is computed as opposed to computing cond for any types of conditional statements like:

if(cond)
   s1
else
   s2

Why is it that the VM translation is:
VM code for computing ~(cond) //Why not compute cond?
if-goto L1
VM code for executing s1
goto L2
label L1
   Vm Code for executing s2
label L2
....

I can see HOW it works but I'm wondering WHY this way? Does it have to do with performance optimization for the hardware?
Reply | Threaded
Open this post in threaded view
|

Re: Flow Control - Determining conditions for VM Code

cadet1620
Administrator
peterxu422 wrote
Why is it that the VM translation is:
VM code for computing ~(cond) //Why not compute cond?
if-goto L1
VM code for executing s1
goto L2
label L1
  &nvsp;Vm Code for executing s2
label L2
....
The else clause is optional. If it's not there, then the generated code only requires one jump:
  VM code for computing ~(cond)
  if-goto L1
  VM code for executing s1
  goto L2  
label L1
  Vm Code for executing s2  
label L2  
  ...
If the general if/then/else code generator tested for the true cond value you'd need an extra jump in the "no else" case.
  VM code for computing cond
  if-goto L1
  VM code for executing s2  
  goto L2
label L1
  VM code for executing s1
label L2
  ...

Also note that if the generated code for s1 comes after the code for s2, the code for the entire s1, which may be a multi-statement block, will need to be cached in memory until after the code for s2 is emitted.

--Mark