rewrite codegen