In this paper, we present an LLM-based code translation method and an associated tool called CoTran, that translates whole-programs from one high-level programming language to another. Existing LLM-based code translation methods lack training to ensure that the translated code reliably compiles or bears substantial functional equivalence to the input code. In our work, we fine-tune an LLM using reinforcement learning, incorporating compiler feedback, and symbolic execution (symexec)-based testing feedback to assess functional equivalence between the input and output programs. The idea is to guide an LLM during fine-tuning, via compiler and symexec-based testing feedback, by letting it know how far it is from producing perfect translations. We conduct extensive experiments comparing CoTran with 14 other code translation tools, including human-written transpilers, LLM-based translation tools, and ChatGPT. Using a benchmark of over \num{57000} code pairs in Java and Python, we demonstrate that CoTran outperforms the other tools on relevant metrics such as compilation accuracy (CompAcc) and functional equivalence accuracy (FEqAcc). For example, in Python-to-Java translation, CoTran achieves 48.68% FEqAcc and 76.98% CompAcc, whereas the nearest competing tool (PLBART-base) gets 38.26% and 75.77% respectively. Additionally, CoTran, built on top of CodeT5, improves FEqAcc by +14.89% and CompAcc by +8.14% for Python-to-Java (resp., +12.94% and +4.30% for Java-to-Python).
翻译:本文提出了一种基于大语言模型(LLM)的代码翻译方法及其配套工具CoTran,该工具能够将完整程序从一种高级编程语言翻译至另一种语言。现有的基于LLM的代码翻译方法缺乏针对性训练,难以确保翻译后的代码可靠编译或与输入代码保持实质性的功能等价性。在本工作中,我们采用强化学习对LLM进行微调,结合编译器反馈以及基于符号执行(symexec)的测试反馈,以评估输入与输出程序之间的功能等价性。其核心思想是在微调过程中,通过编译器和基于符号执行的测试反馈来引导LLM,使其了解当前翻译结果与完美翻译之间的差距。我们进行了大量实验,将CoTran与另外14种代码翻译工具进行比较,包括人工编写的转译器、基于LLM的翻译工具以及ChatGPT。使用包含超过57,000对Java与Python代码的基准测试集,我们证明CoTran在相关指标(如编译准确率(CompAcc)和功能等价准确率(FEqAcc))上优于其他工具。例如,在Python到Java的翻译任务中,CoTran实现了48.68%的FEqAcc和76.98%的CompAcc,而最接近的竞争工具(PLBART-base)仅分别达到38.26%和75.77%。此外,基于CodeT5构建的CoTran在Python到Java翻译任务中将FEqAcc提升了+14.89%,CompAcc提升了+8.14%(在Java到Python任务中分别提升+12.94%和+4.30%)。