cdjc / goto

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JumpTooFar Exception

cdjc opened this issue · comments

commented
          Thanks huge. Sadly, getting `JumpTooFar` exception on `http://github.com/BartMassey/hamurabi/hamurabi.py`. Apparently only 8-bit jump offsets are allowed in Python bytecode?

Originally posted by @BartMassey in #6 (comment)

commented

Greater than 8-bits Jump is definitely possible in Python bytecode. But it requires some extra effort. Short story is that to jump forward, say, 260 instructions (=1*256+4), the high bit needs to be an EXTENDED_ARG:

EXTENDED_ARG 1
JUMP_FORWARD 4

I fixed this for JUMP_BACKWARD (I needed it for some performance tests I was running). It's a little bit tricky because the calculation for the jump distance is from the instruction after the JUMP. Therefore on the boundary (jumping forward 256), the very fact of adding an EXTENDED_ARG can put the JUMP_FORWARD in a position where it no longer needs the EXTENDED_ARG! JUMP_BACKWARD is a bit easier on the boundary condition.

It's all very doable, but maybe not until the weekend. Will need to write some extra unit tests too.

Makes sense. Thanks much once again. I assume it's OK to have an EXTENDED_ARG even if you don't need it? Maybe just make all the jumps extended? It's not like optimal byte code is a goal here, I presume?

commented

Yes. That would be an easy solution. If it works. Optimal bytecode isn't a goal, but interestingly I did a test using a finite state machine to represent a sequence of floating point numbers. The FSM using labels and gotos was much faster than a using a loop and match statement. It was about as fast as a regular expression (which in Python is just a call into C code).

That's awesome and hilarious. Great work. I'm really enjoying watching this: hope you're enjoying working on it.

commented

Yes. I find a perverse sense of satisfaction in forcing a language like Python to have a construct like goto.

commented

Fixed. @BartMassey suggestion of using an EXTENDED_ARG (whether or not it is needed) seems to work. Thanks.

Thank you!

commented

I checked hamurabi.py and it works. Also, found a bug (once I figured out how not to be kill everyone) and sent a pull request.

Merged. Thanks much.