Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LLVM Cookbook

You're reading from   LLVM Cookbook Over 80 engaging recipes that will help you build a compiler frontend, optimizer, and code generator using LLVM

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781785285981
Length 296 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. LLVM Design and Use FREE CHAPTER 2. Steps in Writing a Frontend 3. Extending the Frontend and Adding JIT Support 4. Preparing Optimizations 5. Implementing Optimizations 6. Target-independent Code Generator 7. Optimizing the Machine Code 8. Writing an LLVM Backend 9. Using LLVM for Various Useful Projects Index

Converting IR to LLVM bitcode

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.

Getting Ready

The llvm-as tool must be installed in the PATH.

How to do it...

Do the following steps:

  1. First create an IR code that will be used as input to llvm-as:
    $ cat test.ll
    define i32 @mult(i32 %a, i32 %b) #0 {
      %1 = mul nsw i32 %a, %b
      ret i32 %1
    }
    
  2. To convert LLVM IR in test.ll to bitcode format, you need to use the following command:
    llvm-as test.ll –o test.bc
    
  3. The output is generated in the 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:
    How to do it...

    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:

    How to do it...

How it works...

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.

There's more...

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.

See also

You have been reading a chapter from
LLVM Cookbook
Published in: May 2015
Publisher:
ISBN-13: 9781785285981
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime