Optimization and Bytecodes

During optimization, bytecodes can be separated into four basic types that need optimization, all in slightly different ways:

  • Span-dependent jumps
  • Span-dependent immediates or effective addresses
  • Offset setters (org and align)
  • Multiples (times)

The first two can be handled in a relatively straightforward manner: start assuming everything is the smallest possible size, then go through a loop upsizing any that are out of range, and roll those size increases into the dependent spans. If the bytecodes dependent on those spans go out of range, go and upsize those. Repeat until nothing is out of range.

Unfortunately, the last two (and particularly the last one), break this paradigm. Suddenly the bytecode can get smaller instead of larger! I've tackled multiples by simply recalculating them on any change, after checking for cycles at the beginning. Note that as a span may be over a bytecode and a multiple following it (e.g. an old-style align macro), one may cancel out the other, so it's important to handle multiples before handling anything else.

Offset setters are handled via a separate mechanism (which I'm currently working on). More on this in my next entry.

Add comment