What is the structure of source code of llvm-mc

Hi we have a project that need to put different machine code line by line into one object.

The project is under time pressure so we figure out a way using functions to do this, like the following example:

int run_arch1()
{
    int a = 23;
    run_on_arch2(
        "arch2 asm inst1\n"
        "arch2 asm inst2\n"
    );
    return 0;
}

We plan to compile the above C code into arch1 assembly, and using llvm-mc to compile the assembly into the target code.

In order to compile the string parameter of function run_on_arch2, we would like to put a hook in llvm-mc, So it will call the arch2 assembler to generate machine code and insert them into the position of calling run_on_arch2.

However, I’m new to llvm and do not familiar with llvm source code. Searching result from internet are also limited for llvm-mc.

Thus I would like to ask if anyone can give me a general idea of how it work, what is the basic code structure of llvm-mc and where I can find docs that can help me to finish my task.

I’m also debugging the source and hope to find it myself. But due to the time pressure I hope some one can give some directions.

Thanks a lot!!!

Although a bit dated, the original LLVM blog article on the MC layer is still a great overview of the design: Intro to the LLVM MC Project - The LLVM Project Blog.

You can also directly consult the MC layer APIs in include/llvm/MC and llvm-mc proper in tools/llvm-mc/llvm-mc.cpp.

Note that in order to use llvm-mc (which is just a driver tool built on the MC libraries/APIs), you’re going to need enough of an existing LLVM back end for arch2 to build those libraries. This would be a minimal TableGen description of the ISA (InstrInfo) and the required MC APIs (e.g., MCAsmBackend, MCCodeEmitter, MCTargetAsmParser, etc.). If that’s not available (or some other arch2 assembler), and depending on the complexity of arch2, that could be a lot of work-- particularly if you do not already have LLVM experience.

1 Like