• Stars
    star
    664
  • Rank 67,903 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Virtual Machine for Intermediate Representation

VMIR - Virtual Machine for Intermediate Representation

Build status

VMIR is a standalone library written in C that can parse and execute:

  • WebAssembly .wasm files
  • LLVM Bitcode .bc files

Optionally it can generate machine code (JIT) to speed up execution significantly. JIT is currently only supported on 32 bit ARM.

VMIR is licensed under the MIT license. See LICENSE.

To build VMIR just type:

$ make

... and you will end up with a VMIR binary in very same directory. The library is compiled from a single file src/vmir.c which in turn include other files to keep the code somewhat separated.

Example

Let's create a small program and run it. Type the following well known snippet into a file called helloworld.c

int main(void)
{
  printf("Hello world\n");
  return 0;
}

Then compile it

clang -emit-llvm -target le32-unknown-nacl -c helloworld.c -o helloworld.bc

And finally, run it:

$ ./vmir helloworld.bc
Hello world

Compiling to WebAssembly requires a bit more work than a single line. However, there is a pre-built version of sha1sum included in the source repo.

$ echo hello | ./vmir examples/prebuilt/sha1sum.wasm
f572d396fae9206628714fb2ce00f72e94f2258f  -

If you're on Linux and want to go all crazy you can use VMIR to execute Bitcode and WebAssembly directly from the command line by installing a kernel binfmt pointing to the VMIR executable:

echo ":vmirwasm:M::\x00\x61\x73\x6d\x01::${PWD}/vmir:" | sudo tee /proc/sys/fs/binfmt_misc/register
echo ":vmirbc:M::\x42\x43\xc0\xde::${PWD}/vmir:" | sudo tee /proc/sys/fs/binfmt_misc/register

And then you just simply just do:

$ echo hello | examples/prebuilt/sha1sum.wasm
f572d396fae9206628714fb2ce00f72e94f2258f  -

Performance

Interpretation is about 10x slower (on x86) than the same binary compiled as native code. Still it's a lot faster than LLVM's own interpreter (which by all means is not intended to run code fast in any way)

Example run of test/misc/src/sha1test.c over 64MB of random data

Environment (Core i7 3.2GHz) ARMv7 BCM2709 (Rpi2)
Native 0.39s 3.54s
VMIR JIT n/a 17.5s
VMIR 4.8s 1m 42s
LLVM LLI 7m 39s n/a

Status

VMIR currently passes the gcc torture test suite on optimization level 0, 1 and 2. Those tests can be found in test/gcc-torture. Use make && ./runtest to run the tests.

Missing features, known bugs

  • The built-in libc is lacking a lot of functions and features. This is where most work needs to be done.
  • No support for vector types (Ie, code must be compiled with -fno-vectorize -fno-slp-vectorize).
  • Not all instructions classes / value types are JITed.
  • No C++ STL solution. Ideas welcome...

Compiling C/C++ to Bitcode

VMIR uses the same target as Google NativeClient. There are small examples in test/misc.

When building bigger projects consisting of multiple files you must llvm-link to combine the .bc files into a single file.

Compiling C/C++ to WebAssembly

Building for WebAssembly is a bit more involved atm. There is a document here: docs/wasm.md, that shows how to setup LLVM + Binaryen and the WebAssembly Binary Toolkit. Once you have that in place there are some small examples in examples/wasm that could get you started.

Embedding VMIR

Including VMIR in your own project is pretty straight forward. Just copy the files from src/ to your project but only compile vmir.c (it will include all other .c -files on its own). The API is defined in vmir.h. See src/main.c for example how to load and execute binaries.

VMIR's libc also offers an option to use TLSF for memory allocation. The default built-in allocator is a very simple linear search first-fit algorithm.

Wait? Wut? Why?

You might ask yourself what the purpose of VMIR actually is and why it even exists?

As with many of these kind of project I just wanted to scratch and itch but also be able to ship plugins written in C and C++ for another project of mine.

Now with the rise of WebAssembly I intend to focus more on that as the primary input to VMIR mostly because it's more stable than LLVM's Bitcode, which is not really meant to be used as a shippable object code.

Given enough time I also hope to improve the JIT engine to be able to emit code for more architectures (In particular ARMv8 and x86_64).

Follow me on https://twitter.com/andoma

More Repositories

1

movian

An advanced media center
C
272
star
2

libsvc

Tiny helper lib for building daemons in C
C
19
star
3

saga

C++ framework for deep learning
C++
14
star
4

ps3toolchain

Shell
12
star
5

libhap

Minimalistic C library for Apple's HomeKit Accessory Protocol
C
11
star
6

iptvdump

IPTV transport stream dumper
C
8
star
7

movian-plugin-youtube

Youtube plugin for Movian
JavaScript
8
star
8

showtime-plugin-youtube

The Best Online Video Sharing service now on Showtime.
JavaScript
3
star
9

showtime-plugin-viastream

Showtime plugin for Viastream online streaming video (Unofficial)
JavaScript
3
star
10

mios

C
3
star
11

showtime-plugin-tv4play

Showtime plugin for TV4 play online streaming video (Unofficial)
JavaScript
3
star
12

sterm

Very simple serial port console for Linux
C
3
star
13

movian-plugin-modarchive

JavaScript
3
star
14

showtime-plugin-headweb

Showtime plugin for Headweb online streaming video (Unofficial)
JavaScript
3
star
15

showtime-plugin-svtplay

Showtime plugin for SVT Play online streaming video (Unofficial)
JavaScript
2
star
16

htshttpd

A lousy HTTP server mostly used for testing various HTTP client features in Showtime
C
2
star
17

showtime-plugin-dropbox

Dropbox plugin for Showtime
JavaScript
2
star
18

showtime-plugin-omgmanga

Showtime plugin for reading manga
JavaScript
2
star
19

showtime-website

JavaScript
2
star
20

mkfatimg

Tiny tool for creating an SD image with just a FAT parition and also filling it with files from a directory on the host
C
2
star
21

vrec

A small video recording app for Linux
C
2
star
22

showtime-plugin-revision3

Showtime revision3 plugin
JavaScript
2
star
23

movian-plugin-gmeplayer

1
star
24

showtime-plugin-opensubtitles

Opensubtitles plugin for Showtime
JavaScript
1
star
25

ddserver

dyndns protocol server written C (uses nsupdate to update DDNS records)
C
1
star
26

xmp

C
1
star
27

spmc

Showtime Plugin Management Console
JavaScript
1
star
28

dotfiles

Just my dotfiles
Common Lisp
1
star
29

test

My test repo
1
star
30

hts

Various common stuff for Showtime and Tvheadend
1
star
31

showtime-plugin-numeric-osk

example repo for OSK plugins with showtime
1
star
32

stos-splash

C
1
star
33

stos

Movian OS top level build
C
1
star