Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Exception handling in LLVM


In this recipe, we will look into the exception handling infrastructure of LLVM. We will discuss how the exception handling information looks in the IR and the intrinsic functions provided by LLVM for exception handling.

Getting ready...

You must understand how exception handling works normally and the concepts of try, catch and throw and so on. You must also have Clang and LLVM installed in your path.

How to do it…

We will take an example to describe how exception handling works in LLVM:

  1. Open a file to write down the source code, and enter the source code to test exception handling:

    $ cat eh.cpp
    class Ex1 {};
    void throw_exception(int a, int b) {
      Ex1 ex1;
      if (a > b) {
        throw ex1;
      }
    }
    
    int test_try_catch() {
      try {
        throw_exception(2, 1);
      }
      catch(...) {
        return 1;
      }
      return 0;
    }
    
  2. Generate the bitcode file using the following command:

    $ clang -c eh.cpp -emit-llvm -o eh.bc
    
  3. To view the IR on the screen, run the following command, which will give...