LARA

Translation of Relations

Consider translation of <,=, < = ,>,>=,=,!= on integers

Operations of type

\begin{equation*}
  \textsf{Int} \times \textsf{Int} \to \{ 0, 1 \}
\end{equation*}

Note: there are no instructions that take two integers from stack and leave the result of comparison on stack

There are conditional branches for comparison of two stack operands:

  • in general called if_icmpCOND for different 'COND'

Compilation scheme

  • assuming translations compute results on top of the stack

What should be the compiled code?

[[ e1 COND e2 ]] =
        [[ e1 ]]
        [[ e2 ]]
        if_icmpCOND nTrue
        bipush 0
        goto nAfter
nTrue:  
        bipush 1
nAfter:

Example

static boolean test(int x, int y) {
  return (x < y);
}

Bytecode:

static boolean test(int, int);
  Code:
   0:   iload_0
   1:   iload_1
   2:   if_icmpge       9
   5:   iconst_1
   6:   goto    10
   9:   iconst_0
   10:  ireturn

Note:

  • iconst is a shorter form for bipush
  • the code above uses the dual branch and swaps constants true and false