Skip to content

Advanced IR Generation and IT Certification Exams

  • Contact Us
Close Menu

Integrating the exception handling code into the application – Advanced IR Generation

November 8, 2022November 8, 2022| Donna martinIntegrating the exception handling code into the application – Advanced IR Generation| 0 Comment| 03:33

Categories :
  • Adding debug metadata
  • Adding line numbers
  • Catching an exception
  • ITCertification Exams
  • The LLVM pass manager

The IR for the division is generated inside the visit(BinaryOp &) method. Instead of just generating a sdiv instruction, we must generate an IR to compare the divisor with 0. If the divisor is 0, then the control flow continues in a basic block, raising the exception. Otherwise, the control flow continues in a basic block with the sdiv instruction. With the help of the createICmpEq() and addThrow() functions, we can code this very easily:


    case BinaryOp::Div:
      BasicBlock *TrueDest, *FalseDest;
      createICmpEq(Right, Int32Zero, TrueDest,
                   FalseDest, “divbyzero”, “notzero”);
      Builder.SetInsertPoint(TrueDest);
      addThrow(42); // Arbitrary payload value.
      Builder.SetInsertPoint(FalseDest);
      V = Builder.CreateSDiv(Left, Right);
      break;

The code generation part is now complete. To build the application, we must change into the build directory and run the ninja tool:


$ ninja

Once the build has finished, you can check the generated IR with the with a: 3/a expression:


$ src/calc “with a: 3/a”

You will see the additional IR needed to raise and catch the exception.

The generated IR now depends on the C++ runtime. The easiest way to link against the required libraries is to use the clang++ compiler. Rename the rtcalc.c file with the runtime functions for the expression calculator to rtcalc.cpp, and add extern “C” in front of each function inside the file. Then, use the llc tool to turn the generated IR into an object file, and the clang++ compiler to create an executable:


$ src/calc “with a: 3/a” | llc -filetype obj -o exp.o
$ clang++ -o exp exp.o ../rtcalc.cpp

Now, we can run the generated application with different values:


$ ./exp
Enter a value for a: 1
The result is: 3
$ ./exp
Enter a value for a: 0
Divide by zero!

In the second run, the input is 0, and this raises the exception. It works as expected!

In this section, we learned how to raise and catch exceptions. The code to generate the IR can be used as a blueprint for other compilers. Of course, the type information that’s used and the number of catch clauses depends on the input to the compiler, but the IR we need to generate still follows the pattern presented in this section.

Adding metadata is another way to provide further information to LLVM. In the next section, we’ll add type metadata to support the LLVM optimizer in certain situations.

Generating metadata for type-based alias analysis

Two pointers may point to the same memory cell, at which point they alias each other. Memory is not typed in the LLVM model, which makes it difficult for the optimizer to decide if two pointers alias each other or not. If the compiler can prove that two pointers do not alias each other, then more optimizations are possible. In the next section, we will have a closer look at the problem and investigate how adding additional metadata will help before we implement this approach.

Post navigation

Previous page Catching an exception – Advanced IR Generation
Next page Understanding the need for additional metadata – Advanced IR Generation-1

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Post

Extending single inheritance with interfaces – IR Generation for High-Level Language Constructs

November 8, 2021November 8, 2021 Donna martinExtending single inheritance with interfaces – IR Generation for High-Level Language Constructs

Languages such as Java support interfaces. An interface is a collection of abstract methods, comparable [...]

Read MoreRead More

Catching an exception – Advanced IR Generation

October 6, 2022October 6, 2022 Donna martinCatching an exception – Advanced IR Generation

To generate the IR code to catch an exception, we must add the addLandingPad() method. [...]

Read MoreRead More

Adding support for multiple inheritance – IR Generation for High-Level Language Constructs-2

February 10, 2022February 10, 2022 Donna martinAdding support for multiple inheritance – IR Generation for High-Level Language Constructs-2

This approach can also be used for implementing interfaces. As an interface only has methods, [...]

Read MoreRead More

Search

Dropdown Categories

Archives

  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • December 2021
  • November 2021

Meta

  • Log in

Tag Cloud

Back to Top

Cookie Policy | Terms | Privacy | About Us | © Sherrieland