> What I've done is to run the first pass assuming that each SDI is> long, noting the location and target of each SDI in a table. Then> between the first and second passes, iterate over the table looking> for an instruction that is long but could be short, shorten it, and> adjust symbol table values appropriately. Repeat until you don't find> any more. This isn't perfectly optimal, but it's close and runs> reasonably fast. There was a paper by Szymanski on SDIs in the CACM> in the early 1970s that lays out all of the theory. -John]

I'd do it the other way round: start assuming that every SDI is short,
then look which of them have a too-large span and change them to use
longer instructions; repeat passes through the code until nothing
changes.

This will get you an optimal solution provided that all instructions
with a larger span are also longer (unlike the above algorithm that
may not find all possibilities for optimization; from a theory point
of view, my algorithm will give the least fixpoint of the solution
while John's algorithm with give the largest fixpoint).

Regards,
Joachim
[I'd better dig up Szymanski's paper and see what the problem with
iterating up is. -John]