Java Reference
In-Depth Information
11.3.9 Conditional Execution
/
Visitor code for Marker 12
/
procedure
visit
(CondTesting n )
f alseLabel ← gen
L
abel
()
45
endLabel ← gen
L
abel
()
call n . getPredicate() . accept( this )
predicateResult n . getPredicate() . getResultLocal()
46
call
emit
B
ranch
I
f
F
alse
( predicateResult , f alseLabel )
47
call n . getTrueBranch() . accept( this )
call
48
emit
B
ranch
( endLabel )
call
emit
L
abel
D
ef
( f alseLabel )
call n . getFalseBranch() . accept( this )
call
49
emit
L
abel
D
ef
( endLabel )
end
A CondTesting node represents conditional execution based on the outcome
of some predicate. Languages like Java and C allow conditional execution
among statements using the if construct. Exercise 5 explores other syntax
with similar semantics.
The VM code that is generated for an AST is a linear form, in the sense
that the conclusion of one instruction typically causes the next instruction in
sequence to begin execution, unless the flow of control is interrupted. Code
involving conditional execution therefore requires the use of jumps :totransfer
control between instructions that should execute, and to skip over instructions
that should not execute.
Marker 45 reserves two labels for use in generating the code for an
CondTesting node. One label ( f alselabel ) is used to receive control follow-
ing the predicate's test, should the predicate evaluate to false. The other label
marks the end of code generated for the CondTestingnode.
AST
JVM Instructions
; Predicate code (Marker 46 )
iload 5
; Is the predicate false?
ifeq f alseLabel
; True branch (Marker 48 )
sipush 431
goto endLabel
falseLabel:
; False branch (Marker 49 )
sipush 250
endLabel:
IfIsh
if
n
ConstantIsh
ConstantIsh
ComputeIsh
a
431
250
 
 
Search WWH ::




Custom Search