Fix allocation of local variables (and global references)

We were just pushing values on the stack in random order, now we sort them by their BP-relative offset and push the top ones first, as we should.