Runtime compilation of runtime-constructed code is becoming standard practice in libraries, DSLs, and database management systems. Since compilation is expensive, systems that are sensitive to compile times such as relational database query compilers compile only hot code and interprets the rest with a much slower interpreter. We present a code generation technique that lowers an AST to binary code by stitching together code from a large library of binary AST node implementations. We call the implementations stencils because they have holes where values must be inserted during code generation. We show how to construct such a stencil library and describe the copy-and-patch technique that generates optimized binary code. The result is a code generator with negligible cost: it produces code from an AST in less time than it takes to construct the AST. Compared to LLVM, compilation is two orders of magnitude faster than -O0 and three orders of magnitude faster than higher optimization levels. The generated code runs an order of magnitude faster than interpretation and runs even faster than LLVM -O0. Thus, copy-and-patch can effectively replace both interpreters and LLVM -O0, making code generation more effective in compile-time sensitive applications.
翻译:运行时间构建代码的运行时间编译正在成为图书馆、 DSL 和数据库管理系统的标准做法。 由于编译费用昂贵, 系统对于编辑时间非常敏感, 如关系数据库查询汇编器等系统只编辑热代码, 并且用慢得多的翻译器来解释其余的代码。 我们展示了一种代码生成技术, 将二进制的 AST 编译为二进制代码, 将二进制 AST 节点执行的大型图书馆的代码缝合在一起。 我们调用执行系统 Stencils, 因为它们有在代码生成过程中必须插入值的洞洞。 我们展示了如何构建这样的 高级库, 并描述生成优化二进制代码的复制和批次技术。 结果是一个代码生成器, 其成本微不足道: 它生成一个 AST 的代码比构建 AST 所需的时间要少得多。 与 LLVM 执行程序相比, 编成两个数量级比 - O0 和三个数量级要快, 比最优化级别还要快。 生成的代码比 LLVM - O0 运行速度要快, 速度更快。 因此, 复制和 patch 能够有效地编辑两个翻译和LLLVM 的版本。