Converting LLVM IR to JavaScript

In this recipe, we will briefly discuss how we can convert LLVM IR to JavaScript.

Getting ready

To convert IR to JavaScript, perform the following steps:

  1. We will make use of the emscripten LLVM to JavaScript compiler. You need to download the SDK provided at https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html . You can also build it from the source code, but just for experimenting, you can use the SDK that comes with the toolchain.
  2. After downloading the SDK, extract it to a location and go to the root folder of the download.
  3. Install the default-jre, nodejs, cmake, build-essential, and git dependencies.
  4. Execute the following commands to install the SDK:
    ./emsdk update
    ./emsdk install latest
    ./emsdk activate latest
    
  5. See the ~/emscripten script to check whether it has the correct values, and if not, update it accordingly.

How to do it…

Perform the following steps:

  1. Write the test code for the conversion:
    $ cat test.c
    #include<stdio.h>
    
    int main() {
      printf("hi, user!
    ");
      return 0;
    }
    
  2. Convert the code to the LLVM IR:
    $ clang –S –emit-llvm test.c
    
  3. Now use the emcc executable located in the emsdk_portable/emscripten/master directory to take this .ll file as the input and convert it into JavaScript:
    $ ./emcc test.ll
    
  4. The output file generated is the a.out.js file. We can execute this file using the following command:
    $ nodejs a.out.js
    hi, user!
    

See more

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178