compiler: Sort models topologically

Sort models topologically using Base->Model as edges. This way
the base models are defined before the models extending them.