Code Monkey home page Code Monkey logo

node-llvmc's Introduction

node-llvmc

These are bindings to the LLVM C API for Node.js, written in TypeScript using node-ffi.

Setting Up

You will need an installation of LLVM that includes its shared library, which is called libLLVM.so or libLLVM.dylib. On macOS, for example, Xcode does not ship with the shared library, but the Homebrew package for LLVM does. If you build LLVM yourself, set LLVM_BUILD_LLVM_DYLIB=On to get the shared library.

Next, the dynamic linker will need to be able to find the shared library. If you installed LLVM with Homebrew, for example, try this:

$ export LD_LIBRARY_PATH=`brew --prefix llvm`/lib

to put the appropriate "keg-only" library directory on your linker path.

You should now be able to install and require the llvmc library. Installation works in the usual way:

$ yarn add llvmc
(or)
$ npm install --save llvmc

Examples

This repository contains two examples. You can build them by cloning this repository and then running:

$ cd node-llvmc
$ yarn  # Install the dependencies.
$ cd example
$ tsc  # Compile the examples.

The first example is really simple, but it shows all the pieces you need to generate LLVM IR. Type this to see it in action:

$ node build/example/basic.js

You'll see textual LLVM IR dumped to standard output, and the script will also write LLVM bitcode to a file out.bc. You can type clang out.bc to compile the program to native code and then ./a.out to execute it.

We have also included a more complex example based on LLVM's venerable Kaleidoscope tutorial. See the example's README for an introduction.

Using the Bindings

There are two ways to use the library: a low-level interface and a set of friendlier wrapper classes.

Low-Level Bindings

To use the direct bindings to the LLVM C API functions, import the LLVM object from the llvmc module. You can invoke the C API functions as methods on this object:

import { LLVM } from 'llvmc';
let mod = LLVM.LLVMModuleCreateWithName("some_module");
// ...
let ir = LLVM.LLVMPrintModuleToString(mod);
console.log(ir);
LLVM.LLVMDisposeModule(mod);

This low-level interface does not have useful TypeScript annotations (yet).

Higher-Level, Object-Oriented Interface

This library also provides ES2015 class wrappers for LLVM objects. These wrappers do come with TypeScript types, which enable completion and static checking. You can import these classes, such as Module, from the library:

import * as llvmc from 'llvmc';
let mod = llvmc.Module.create("some_module");
// ...
console.log(mod.toString());
mod.free();

Someday, we will generate documentation for this suite of classes.

Other Projects

Here are the other LLVM bindings I could find:

  • llvm2: Another FFI-based binding to the C API.
  • node-llvm: Bindings using a compiled extension.
  • petard: Another compiled extension, also focused on IR generation.

Credits

This is a project of Capra at Cornell. It was written by Adrian Sampson and Richie Henwood. The license is MIT.

node-llvmc's People

Contributors

alexandertrefz avatar rhenwood39 avatar sampsyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-llvmc's Issues

Fix crash on accessing function parameters

We're currently hitting a segfault when trying to iterate over the arguments of a newly-constructed function. Here's a piece of the traceback, from LLDB:

 * frame #0: 0x0000000106116f88 libLLVM.dylib`llvm::LLVMContext::shouldDiscardValueNames() const + 4
   frame #1: 0x0000000106145b77 libLLVM.dylib`llvm::Value::setNameImpl(llvm::Twine const&) + 39
   frame #2: 0x0000000106145e0c libLLVM.dylib`llvm::Value::setName(llvm::Twine const&) + 14
   frame #3: 0x00000001060f5f7a libLLVM.dylib`llvm::Argument::Argument(llvm::Type*, llvm::Twine const&, llvm::Function*) + 110
   frame #4: 0x00000001060f702b libLLVM.dylib`llvm::Function::BuildLazyArguments() const + 105
   frame #5: 0x00000001060d9dd1 libLLVM.dylib`LLVMGetParam + 27

How do you generate Object File Code with this library?

I've been working on a toy programming language written in Javascript and I need it to compile into an executable. All of the LLVM libraries that I've found written in Javascript only take you as far as IR or Bitcode, but never take you all the way to object code. I know that there are ways inside the LLVM library to generate object code, I'm just wondering if that is supported in this node-llvmc library.

Document how to use the libLLVM shared library

Highlights:

  • The package from your package manager may come with a libLLVM.so, in which case you're all set. Notably, Xcode does not, so you'll want to install from Homebrew, which provides the shared library by default.
  • If you build from source yourself, you'll want to set the LLVM_BUILD_LLVM_DYLIB configuration variable when running CMake, according to the LLVM guide.
  • Finally, to find the library, you may need to set an environment variable or otherwise configure the dynamic linking search path. The environment variable might be called LD_LIBRARY_PATH or DYLD_LIBRARY_PATH.

Add types for the low-level LLVM functions

We should add declare function TypeScript declarations for the low-level LLVM C API functions. That will make it easier to write lower-level code and will help avoid bugs in our own wrapper code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.