Enhancing LLMs in Long Code Translation through Instrumentation and Program State Alignment

Xin-Ye Li, Ya-Li Du, Ming LiarXiv 2025 · DOI

Large Language Models (LLMs) have demonstrated remarkable proficiency in translating code between programming languages. However, a significant performance gap exists between translating short snippets and complex, real-world modules. While models excel at functional guesses for small functions, they often struggle with long-context programs where syntactic appearance frequently masks functional incorrectness.

Researchers from Nanjing University have introduced PAST (Program State Alignment augmented Translation), a framework that treats code translation as a dynamic alignment problem rather than a purely linguistic task. By utilizing LLM-driven instrumentation to synchronize internal program states, PAST significantly improves the reliability of long-context code migration.

The Challenge of Long-Context Translation

Current benchmarks like HumanEval or MBPP primarily focus on short code segments. In these limited contexts, LLMs can often infer logic based on syntax. Real-world software migration, however, involves "long code" spanning hundreds or thousands of tokens. The researchers identify two primary failure modes in these scenarios:

  1. The Appearance Trap: LLMs produce code that looks syntactically valid but contains subtle semantic errors, such as incorrect signed vs. unsigned integer comparisons.
  2. The Needle in a Haystack: In a 500-line file, a single-character error can break the entire program. Without execution feedback, localizing and fixing such bugs is nearly impossible for an LLM.

As illustrated in the performance data, even state-of-the-art models experience a precipitous drop in both compilation success and functional pass rates as the input token count increases.

Performance decay of various LLMs as code length increases, showing a sharp decline in both compilation and pass rates beyond 512 tokens.

The PAST Framework: Program State Alignment

The core philosophy of PAST is that while the syntax of two programs differs, their internal states—the values of variables at specific execution points—should remain identical from entry to exit. The framework operates in three distinct stages:

1. Instrumentation Stage

Instead of translating raw code, the LLM is first tasked with inserting "breakpoints" (print statements) into the source program. These probes are strategically placed to reveal execution flow and critical variable values. This process forces the model to create a "cognitive map" of the program's logic before translation begins.

The instrumentation workflow where the LLM identifies key execution areas and inserts print statements to track variable values.

2. Translation Stage

The LLM translates the instrumented source code into the target language, following specific instructions to preserve the logic and placement of the breakpoints.

3. Program State Alignment Stage

Both the source and translated programs are executed against test cases. If the resulting execution traces differ, the LLM uses the discrepancy to localize the error. The system then employs repair strategies, such as "Direct Repair" or "Localize-and-re-translate," to bring the target program's state back into alignment with the source.

Evaluation on the LongTrans Benchmark

To evaluate the framework, the authors introduced LongTrans, an execution-based benchmark derived from CodeContests (Codeforces). Featuring over 31,000 samples in C++, Java, and Python, LongTrans samples are 2-3x longer than those in previous benchmarks, providing a rigorous test of long-context reasoning.

The results demonstrate that PAST provides substantial improvements across various models:

  • GPT-4o: Computational Accuracy (CA) increased from 57.51% to 84.70%.
  • DeepSeek-Coder-V2: Accuracy rose from 52.45% to 76.43%.
  • Length Resilience: For code exceeding 2,048 tokens, PAST achieved a 97.8% relative improvement for DeepSeek-Coder-V2, effectively mitigating the performance decay typically associated with long contexts.

Conclusion

By shifting the paradigm from "translation as text generation" to "translation as functional alignment," the PAST framework addresses the fundamental weaknesses of LLMs in handling complex software migration. While the method introduces some computational overhead due to execution loops, the dramatic gains in accuracy suggest a promising path forward for automated, high-stakes code translation.

Summary updated

BibTeX
@misc{li2025enhancing,
  author = {Xin-Ye Li and Ya-Li Du and Ming Li},
  title = {{Enhancing LLMs in Long Code Translation through Instrumentation and Program State Alignment}},
  year = {2025},
  eprint = {2504.02017},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.02017},
}
Figure