Search code examples
qemuemulationx86-emulationeflags

Can't find definition of compute_all_xxx functions in QEMU repository for i386 target?


I was reading the source code of Qemu and wonder how Qemu emulate eflags for x86 processor, in the target-i386/cc-helper.c file I found that functions like compute_all_addw do the job, I want to know how Qemu implements those functions but I can not find the definition of all these functions. Did I miss something? Help please.


Solution

  • QEMU is using a trick here that it does fairly often -- using the C preprocessor to create multiple very similar functions without having to write them all out by hand.

    In this specific case, the code is in target-i386/cc_helper_template.h:

    static int glue(compute_all_add, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
    {
        int cf, pf, af, zf, sf, of;
        [...]
    }
    

    where glue() is a macro which just sticks its arguments together. SUFFIX and DATA_TYPE are defined earlier in the .h file based on the value of SHIFT. target-i386/cc_helper.c then #includes the template .h file multiple times, defining SHIFT differently each time. This results in function definitions for compute_all_addb, compute_all_addw, compute_all_addl and compute_all_addq being generated from the single template.

    In general if you can't find a function definition in QEMU's source code with 'grep' the chances are good that it's being autogenerated like this -- searching for a substring of the function name may help.