I am generating Java code on the fly which will then be compiled and loaded back in. My issues is that I essentially need jumps because when I return from the function I wish to continue execution from the point in the loop from where I have exited. Hopefully, the code here illustrates my desire. Although the actual code will have some ifs as well and be much deeper.
MyIter a,b,c; boolean func() { jump to correct state here state0: a = ...; while (a.next()) { state1: while (b.next()) { return true; // start again at state1 } b = ...; } c = ...; state2: while (c.next()) { return true; // start again at state2 } return false; } In C I would probably use a jump table and state variable. Performance is the key here and the code interacts with the Java application.
My best guesses so far have been:
- Flipping the nesting of loops and the logic. That became too cumbersome and problematic.
- Adding logic to allow the flow to get back to where it was but this may be messy.
- I assume the bytecode can jump so I could generate the bytecode myself but that doesn't seem ideal (is there any good libraries for this?)
- Continuation passing style to avoid returning but that would involve major restructuring, although currently the likely way to go.
I was wondering if anyone had any thoughts?