In this recipe, you will learn to generate LLVM bit code from IR. The LLVM bit code file format (also known as bytecode) is actually two things: a bitstream container format and an encoding of LLVM IR into the container format.
Do the following steps:
llvm-as
:$ cat test.ll define i32 @mult(i32 %a, i32 %b) #0 { %1 = mul nsw i32 %a, %b ret i32 %1 }
test.ll
to bitcode format, you need to use the following command:llvm-as test.ll –o test.bc
test.bc
file, which is in bit stream format; so, when we want to have a look at output in text format, we get it as shown in the following screenshot:Since this is a bitcode file, the best way to view its content would be by using the hexdump
tool. The following screenshot shows the output of hexdump
:
The llvm-as
is the LLVM assembler. It converts the LLVM assembly file that is the LLVM IR into LLVM bitcode. In the preceding command, it takes the test.ll
file as the input and outputs, and test.bc
as the bitcode file.
To encode LLVM IR into bitcode, the concept of blocks and records is used. Blocks represent regions of bitstream, for example—a function body, symbol table, and so on. Each block has an ID specific to its content (for example, function bodies in LLVM IR are represented by ID 12). Records consist of a record code and an integer value, and they describe the entities within the file such as instructions, global variable descriptors, type descriptions, and so on.
Bitcode files for LLVM IR might be wrapped in a simple wrapper structure. This structure contains a simple header that indicates the offset and size of the embedded BC file.
18.189.178.237