• Stars
    star
    441
  • Rank 98,861 (Top 2 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 15 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

Low Level Java Virtual Machine

LLJVM

LLJVM provides a set of tools and libraries for running comparatively low level languages (such as C) on the JVM.

The C to JVM bytecode compilation provided by LLJVM involves several steps. Source code is first compiled to LLVM intermediate representation (IR) by a frontend such as llvm-gcc or clang. LLVM IR is then translated to Jasmin assembly code, linked against other Java classes, and then assembled to JVM bytecode.

The use of LLVM IR as the intermediate representation allows more information about the source program to be preserved, compared to other methods which use MIPS binary as the intermediate representation (such as NestedVM and Cibyl). For example, functions are mapped to individual JVM methods, and all function calls are made with native JVM invocation instructions. This allows compiled code to be linked against arbitrary Java classes, and Java programs to natively call individual functions in the compiled code. It also allows programs to be split across multiple classes (comparable to dynamic linking), rather than statically linking everything into a single class.

Also note that while C is currently the only supported input language, any language with a compiler targeting LLVM IR could potentially be supported.

This project is currently unmaintained. You may be interested in:

QUICKSTART

For a quick demonstration of some pre-compiled classes, download the runtime library and the demo package to a directory on your machine, and run java -jar lljvm-demo-0.2.jar.

To quickly start translating your own programs you might want to use Docker. Getting LLJVM to run on modern machines (and keeping it running) can be quite tricky as it depends heavily on an old version of LLVM. As an alternative, Martin Haye has set up a virtual machine image here that is pre-configured for and includes LLJVM. Install Docker (you may need Boot2Docker) and start it this way: docker run -i -v /your/project/dir:/project -t mhaye/lljvm /bin/bash

To compile LLJVM from source, follow the instructions below. If you have problems compiling from source and are running Linux on an i386-compatible platform, download the binary release, extract it, and download the runtime library into the resulting directory.

INSTALLATION

LLVM 2.7 must be installed to compile and run LLJVM. Jasmin is needed for assembling JVM bytecode.

To compile LLJVM, simply call make in the project root directory, and call make check to run the testsuite.

To generate the the documentation for the java packages and the backend, call make doc, which will generate HTML documentation in doc/java/ and doc/backend/ respectively. PDF documentation for the backend can be obtained by calling make doc-pdf (requires PDFLaTeX), which will generate doc/backend.pdf. A zip archive containing all of the documentation can be created by calling make doc-zip.

C COMPILER

The lljvm-cc frontend ties together several components to provide a C source code to JVM class file compiler.

In order to use the frontend, either llvm-gcc or clang must be installed. llvm-gcc is recommended, and is used instead of clang if both are available. It is also required that the classpath contain jasmin.jar.

To compile a small number of source files to a class file, the following style of command can be used:

lljvm-cc foo.c bar.c -o foo

This will generate foo.class, as well as a shell script foo, which sets the classpath appropriately and executes the classfile with the arguments passed to it. This allows the class to be called in the same way as any other executable. If calling the class without the shell script, note that the entire list of arguments must be passed to the class, including argument 0 (the name of the command).

For a larger number of source files, it is often more efficient to separately compile each file to an object file as needed:

lljvm-cc -c foo.c

then link the object files together into a class file:

lljvm-cc -link foo.o bar.o baz.o -o foo

Note that the -link flag must be used if object files are being supplied instead of source files.

To link the object files together as a library instead of an executable, -link-as-library can be passed instead of -link. In this case the shell script will not be generated.

To link against a library generated by lljvm-cc, the -l<name> flag can be used. This will search for a library in the classpath called lib<name>.class. Additional directories can be added to the classpath with the -L<path> flag. Such additions to the classpath will also be added to the shell script, so when linking an executable, the -L flag must be passed for each directory containing libraries needed by the executable (except the current directory and directories in the system classpath).

An exception to the above is the -lm flag. This will not search for libm.class, but will rather link against java.lang.Math and lljvm.runtime.Math. Also, -lc should not be used as libc is linked by default. If this is not desired, then the -nostdlib flag can be used.

By default all classes generated are placed in the default package. To assign the class to a specific package, the -classname flag can be used. For example, the following command:

lljvm-cc ... -classname=com.example.foo -o bar/baz

will create a class named foo, in the package com.example, placing it in the directory bar/com/example/. The shell script will be output to bar/baz.

If the -g3 flag is used, then Jasmin assembly with full debugging information will be output to <output>.j.

In addition to the above flags, any flag accepted by gcc or ld can also be used. However, sometimes these flags may not be passed to the correct component (this is a bug with lljvm-cc and should be reported).

The frontend can also be used to compile autoconf-based projects relatively easily (although sometimes some changes to the project's build system may be required):

./configure CC=lljvm-cc LD='lljvm-cc -link'
make CCLD='lljvm-cc -link'

DEMO

To demonstrate the capabilities of LLJVM, several common software packages can be compiled with lljvm-cc by entering the demo/ subdirectory and calling make. To verify that all of these compiled correctly, call make check.

A JAR archive containing all of the demo programs can be created by calling make demo in the project root directory. For usage information, execute the JAR in the project root directory with no arguments:

java -jar lljvm-demo.jar

BACKEND

The LLJVM Backend transforms LLVM IR into Jasmin assembly code. It can be invoked by:

lljvm-backend foo.bc > foo.j

There are two flags that are accepted by the backend: -classname and -g. Run lljvm-backend --help for more information.

The output file should then be linked by the LLJVM Linker (see below), and assembled into a class file by Jasmin:

java -jar jasmin.jar foo.j

RUNTIME

The LLVJM Runtime has three components, the Core Runtime (lljvm.runtime), the I/O Support Library (lljvm.io), and the C Standard Library (lljvm.lib.c). See the JavaDoc documentation for further details on the former two. The latter is Newlib compiled to JVM bytecode by lljvm-cc.

TOOLS

There are two command-line tools available: the linker, and the info utility. There are several ways to invoke these tools. The simplest way is to call them directly from the jar archive:

java -jar lljvm.jar <cmd> args...

Alternatively, if lljvm.jar is already in the classpath, one of the following can be used:

java lljvm.tools.Main <cmd> args...
java lljvm.tools.<cmd>.Main args...

LINKER

The LLJVM Linker qualifies references to external methods and fields in Jasmin assembly code. At the top of the code (before any .method directives), external references should be specified through the .extern pseudo-directive, such as:

.extern field foo I
.extern method bar(I)V

Then the linker can be invoked by:

java -jar lljvm.jar ld LIBRARY... < INPUT.j > OUTPUT.j

For example, the following assembly code:

.extern method cos(D)D
.extern field NULL I
...
invokestatic cos(D)D
getstatic NULL I
CLASSFORMETHOD cos(D)D

linked with the command:

java -jar lljvm.jar ld java.lang.Math lljvm.runtime.Memory

would produce:

...
invokestatic java/lang/Math/cos(D)D
getstatic lljvm/runtime/Memory/NULL I
ldc "java/lang/Math"

INFO

The info utility lists the type signatures of the public static fields and methods provided by a class. For example, to list those provided by libc:

java -jar lljvm.jar info lljvm.lib.c

By default, any identifiers beginning with an underscore are omitted. To disable this behaviour, pass the -v flag:

java -jar lljvm.jar info -v lljvm.lib.c

More Repositories

1

pandiff

Prose diffs for any document format supported by Pandoc
TypeScript
274
star
2

jasmin

Jasmin is an assembler for the Java Virtual Machine β›Ί
Java
158
star
3

tensor

Cross-platform Qt5/QML-based Matrix client
C++
156
star
4

subleq

CPU design and toolchain for a simple computer architecture
JavaScript
105
star
5

pixelstruct

a tool for visualising 3D scenes reconstructed from photographs
C++
45
star
6

TeX.js

TeXify: Typesetting for the Web
HTML
44
star
7

c-hashtable

Git mirror of the hash table data structure in C by Christopher Clark β›Ί
C
44
star
8

eigenGPT

Minimal C++ implementation of GPT2
C++
39
star
9

scholarpedia

An outdated Scholarpedia mirror
37
star
10

ipfs-maps

OSM vector tiles on IPFS
CSS
29
star
11

dblp.yaml

DBLP in citeproc-yaml format
TeX
27
star
12

hs-ipfs-api

A Haskell client library for the IPFS API
Haskell
26
star
13

hubot-matrix

Matrix adapter for hubot
JavaScript
26
star
14

sdbm

Git mirror of sdbm source code β›Ί
C
25
star
15

pyzui

PyZUI is an implementation of a Zooming User Interface (ZUI) for Python.
Python
19
star
16

stochaskell

Haskell
10
star
17

fpgatoy

Shadertoy for FPGAs
Verilog
9
star
18

gvwin

Shell
9
star
19

librtneat

A cleaned-up distribution of rtNEAT
C++
9
star
20

cspeak

C
8
star
21

mfc7400c

Implementation of the Brother MFC-7400C scanning protocol
C
7
star
22

aspi

Answer Set Programming, Interactively
Python
7
star
23

iccsh

Interactive C Compiler Shell
C
7
star
24

bib

Command-line bibliography manager
Python
6
star
25

polya

Hierarchical Bayesian Nonparametrics (Python/C++)
C++
6
star
26

latex-glassware

5
star
27

tp

SCSS
5
star
28

datum

Datum is a simple question answering system.
Java
4
star
29

texfonts

OpenType fonts included in TeX Live
CSS
4
star
30

librw-web

WebAssembly build of librw, with TypeScript bindings
TypeScript
4
star
31

dff2gltf

Converts DFF models to glTF, based on OpenRW
C++
3
star
32

deciban

3
star
33

latex-circuit

3
star
34

bootsh

Bootstrappable, self-hosting POSIX shell
C
3
star
35

blog

archive of my old blog
3
star
36

streetcar

C#
2
star
37

classfileanalyzer

Analyzer and Disassembler (Jasmin syntax 2) for Java class files
Java
2
star
38

davidar.github.com

My GitHub Page
HTML
2
star
39

cosmetic-filter

Basic cosmetic / element hiding filtering
JavaScript
2
star
40

toki-ante

CSS
2
star
41

3dglobe

PHP
2
star
42

jclassification

Java implementation of Support Vector Machine (SVM) and multi-layer neural-network, for supervised classification
Java
2
star
43

pytextcat

Python
2
star
44

rtneatbox

rtNEATbox is a simple neuro-evolution sandbox.
C++
2
star
45

fragx

GLSL Fragment Shader Executable Compiler
Python
2
star
46

pandoc-lang

Pandoc filter to detect the language of text
JavaScript
2
star
47

django-tabs

Git mirror of the django-tabs svn repository
Python
2
star
48

candc-docker

Docker image of C&C parser and Boxer
C
2
star
49

django-codehilite

Codehilite (source code syntax highlighting) template filter for Django
2
star
50

etc

Perl
1
star
51

glsl

https://www.shadertoy.com/user/davidar
GLSL
1
star
52

base-data

Python
1
star
53

fsimage

Shell
1
star
54

nodejs-sh

Call any program as if it were a Node.js function
TypeScript
1
star
55

bearssl

C
1
star
56

da.vidr.cc

Source code of da.vidr.cc
Java
1
star
57

blue-lacuna

Inform 7
1
star
58

obsidian-lacuna

Logseq graph of the island in Blue Lacuna https://blue-lacuna.textories.com/source.html
1
star
59

cutil

Various utility functions for C
C
1
star
60

django-helloworld

Django template tag that displays "Hello World!" in one of over 50 different languages
Python
1
star
61

docker-csx

Dockerised CiteSeerX
1
star
62

sdl-cube

C++
1
star
63

django-recurse

Git mirror of the recurse template tag by Lucas Murray
Python
1
star
64

django-quotes

Django template tag that displays a random quote
Python
1
star