One of features that makes QEMU so flexible is an ability to call to a function in the middle of executing translated target code phase. QEMU calls it a helper function. include/exec/def-helper.h [Note: changed to a set of helper headers on May 28. change log] is where QEMU defines macros for creating helper functions for us. Basically, they are just ordinary functions with a wrapper.
Do you remember that target code is translated into TCG intermediate code, then it is translated into host code? A helper function is blended during translation from target code to TCG code. Therefore, you can expected to see helper functions appearing in translate.c file. Helper functions are there to aid target code translation. Some target architecture instructions are hard to be described by pure TCG code. Helper functions provide an access to high-level functionalities and libraries in a running host.
QEMU uses Tiny Code Generator to translate code from target architecture to host architecture. One target instruction may be translated into many TCG instructions. It is the same concept as process virtual machine.
Target code is processed one instruction by one instruction to extract an instruction and operands out of each Program Counter address. It is the task for porters to craft an equivalent set of TCG instructions for a target instruction. You can see all available front-end TCG instructions in QEMU Wikipedia or in tcg/tcg-op.h.
In this post, I will give a part of lm32‘s branch equal instruction translation example. QEMU version is 2.0. I assume a host machine using i386 architecture.
If you happen to work on porting or adding new functionalities to QEMU. This post might be useful for you. Either you have a new architecture or a new feature in your brain. The first thing you have to do here is how to meld your new idea into it, so that the build system acknowledge your presence and work for you.
There are 3 files we have to work on in order to add new stuffs in QEMU.
QEMU knows which files to be built by the following diagram.