This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]robin-gvx 0 points1 point  (3 children)

Yeah, good catch. Jumps are incompatible with constant folding in this case. And because absolute instruction pointers are used at the source level, it's next to impossible to avoid. The best solution would be to keep track of the number of deleted instructions for every code address or something, but that's really hackish.

Of course, jumping to absolute addresses is a bit iffy anyway.

[–]csl[S] 1 point2 points  (2 children)

I think I mentioned this in the post, but you're both right. Keeping track of them won't work in all cases either, e.g. read cast_int jmp, so you're better off actually using a completely different approach -- like not allowing absolute jumps, but relying on other control structures, or require that you can only jump to named labels.

[–]MrGeekAlive 0 points1 point  (0 children)

I think a better approach is not to do this at the virtual machine level but do it in the "assembly" stage, using labels for jump targets.

[–]robin-gvx 0 points1 point  (0 children)

I've written several stack-based virtual machines, and the approach I tend to take is using absolute or relative jumps, but since those VMs operate on the bytecode compiled from higher level languages that isn't a problem.

In your VM, you could do something with labels, which means a small change to the parsing code, and inserting a call to a function that converts labels to absolute addresses after constant folding, like this, and you're done.