Adding JIT support

A wide variety of tools can be applied to LLVM IR. For example, as demonstrated in Chapter 1, LLVM Design and Use, the IR can be dumped into bitcode or into an assembly. An optimization tool called opt can be run on IR. IR acts as the common platform—an abstract layer for all of these tools.

JIT support can also be added. It immediately evaluates the top-level expressions typed in. For example, 1 + 2;, as soon as it is typed in, evaluates the code and prints the value as 3.

How to do it...

Do the following steps:

  1. Define a static global variable for the execution engine in the toy.cpp file:
    static ExecutionEngine *TheExecutionEngine;
  2. In the toy.cpp file's main() function, write the code for JIT:
    int main() { … … init_precedence(); TheExecutionEngine ...

Get LLVM Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.