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 a C source code to LLVM assembly

Here we will convert a C code to intermediate representation in LLVM using the C frontend Clang.

Getting ready

Clang must be installed in the PATH.

How to do it...

  1. Lets create a C code in the multiply.c file, which will look something like the following:
    $ cat multiply.c
    int mult() {
    int a =5;
    int b = 3;
    int c = a * b;
    return c;
    }
    
  2. Use the following command to generate LLVM IR from the C code:
    $ clang -emit-llvm -S multiply.c -o multiply.ll
    
  3. Have a look at the generated IR:
    $ cat multiply.ll
    ; ModuleID = 'multiply.c'
    target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
    target triple = "x86_64-unknown-linux-gnu"
    
    ; Function Attrs: nounwind uwtable
    define i32 @mult() #0 {
      %a = alloca i32, align 4
      %b = alloca i32, align 4
      %c = alloca i32, align 4
      store i32 5, i32* %a, align 4
      store i32 3, i32* %b, align 4
      %1 = load i32* %a, align 4
      %2 = load i32* %b, align 4
      %3 = mul nsw i32 %1, %2
      store i32 %3, i32* %c, align 4
      %4 = load i32* %c, align 4
      ret i32 %4
    }
    

    We can also use the cc1 for generating IR:

    $ clang -cc1 -emit-llvm testfile.c -o testfile.ll
    

How it works...

The process of C code getting converted to IR starts with the process of lexing, wherein the C code is broken into a token stream, with each token representing an Identifier, Literal, Operator, and so on. This stream of tokens is fed to the parser, which builds up an abstract syntax tree with the help of Context free grammar (CFG) for the language. Semantic analysis is done afterwards to check whether the code is semantically correct, and then we generate code to IR.

Here we use the Clang frontend to generate the IR file from C code.

See also

  • In the next chapter, we will see how the lexer and parser work and how code generation is done. To understand the basics of LLVM IR, you can refer to http://llvm.org/docs/LangRef.html.
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