Semantics-Directed Compilation of Nonlinear Patterns