recursive->iterative legalize