I would like to translate a plain Three Address Code file to Java Bytecode. There are some questions related to this topic already, but either they are not answered properly or the question goes way beyond what I'm looking for.
Take for instance this segment of code, generated with the front end of the compiler available in the "Dragon Book":
L1:L3: i = i + 1
L5: t1 = i * 8
t2 = a [ t1 ]
if t2 < v goto L3
L4: j = j - 1
How would it look like in bytecode? Do I need to reconstruct the symbol table to do the translation? It would be really helpful if someone could describe it like blackcompe did in this answer(I know JVM is a stack machine, not a register one).
Here's how I would write your code in bytecode. But this is just one way to do it, and the question is pretty open ended. I'm assuming that all the variables are ints except for a. If they are different types, the required code would obviously look different.
; assume i, j, a, and v are in slots 0-3 respectively
L3:
iinc 0 1
iload_0
bipush 8
imul
; store t1 in a variable for simplicity - you could simplify the code by eliminating the temporary
istore 4
aload_2
iload 4
iaload
istore 5
iload 5
iload_3
if_lt L3
iinc 1 -1
As mentioned, this is a pretty open ended question though. For example, the above code explicitly stores the temporary variables into local slots aka "registers" in order to match the code exactly. But you could simplify the code by rearranging things to avoid the temporaries as shown below
; assume i, j, a, and v are in slots 0-3 respectively
L3:
iinc 0 1
aload_2
iload_0
bipush 8
imul
iaload
iload_3
if_lt L3
iinc 1 -1