Compiling simple loops
I program weird stuff to unwind. Lately, my pet project has been a custom programming language which I'm calling "avidscript". It is a somewhat C-style procedural language which compiles down to bytecode, and the intention is that it will execute in a stack-based Virtual Machine (VM) which is integrated into a game framework. Long term, I have designed some specs to develop it into a concurrent state/event-based language (one thread per FSM), as that will be particularly useful for programming game AIs. Anyway, I digress...
At the moment, I'm working on the low-level code generation. This is part of the compiler 'back-end'. The front-end parses the source code, turning it into an Intermediate Representation (IR) of the program. The back-end then turns the IR into final compiled code (in this case, bytecode for a VM-target... although it could equally be native machine code). I found the bytecode representation of simple loops quite interesting, particularly from an optimisation standpoint, so I thought I'd share my findings here.