• Stars
    star
    144
  • Rank 255,590 (Top 6 %)
  • Language
    C
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

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

Repository Details

An experimental C to Javascript/Lua/Perl5/Lisp/Java compiler
                                  CLUE v0.6
                                  =========

                          (C) 2008-2013 David Given
                                 2013-03-14


INTRODUCTION
============

Clue is an experimental C compiler for Lua, Javascript, Perl, Common Lisp,
Java, and C. It compiles, eventually, ANSI C programs into a form which may
be run on a standard runtime for any of these languages.

Clue is EXPERIMENTAL and UNFINISHED. It is not suitable for any kind of
production use. There are a number of C language features that are not
implemented yet, and it produces rather bad code. However, it will compile
a large number of non-trivial programs.



INSTALLATION
============

Installation is unfortunately rather involved and will only work on Unix-like
systems. I develop on Linux. You *may* be able to make it all work on Cygwin
but you're on your own. Sorry.

TO RUN PROGRAMS BUILT WITH THE LUA51 BACK END:
   ...you will need a Lua 5.1.3 interpreter (such as the stock Lua or Mike
   Pall's LuaJIT) with the LuaSocket and BitLib modules installed.

TO RUN PROGRAMS BUILT WITH THE LUA52 BACK END:
   ...you will need a Lua 5.2 interpreter (such as the stock Lua or Mike
   Pall's LuaJIT) with the LuaSocket module installed.

TO RUN PROGRAMS BUILT WITH THE JAVASCRIPT BACK END:
   ...you will need Node.

TO RUN PROGRAMS BUILT WITH THE PERL5 BACK END:
   ...you will need a Perl 5 interpreter with the Time::Hires module (which
   is normally installed automatically).

TO RUN PROGRAMS BUILT WITH THE COMMON LISP BACK END:
   ...you will need a copy of the SBCL Common Lisp environment. It may or
   may not work with other Common Lisp systems.
   ...thanks to Peter Maydell (http://sourceforge.net/users/pm215) for
   contributing this.
   
TO RUN PROGRAMS BUILT WITH THE C BACK END:
   ...you will need a C toolchain --- but then if you can build Clue, you
   will already have one.

TO RUN PROGRAMS BUILT WITH THE JAVA BACK END:
   ...you will need a JDK. Clue generates class files by producing Java source
   code and compiling it, so you'll need a javac as well as a java interpreter.
   I develop with the official Sun Java 6 distribution.
         
TO BUILD THE COMPILER:
   
1. Fetch sparse 0.4.1 from here:

     http://www.kernel.org/pub/software/devel/sparse/

   Apply the supplied sparse.patch file, compile it, and install it.    

2. Edit the supplied pmfile and change the section at the top to point
   at where you installed sparse's headers and libraries.
   
3. Do:

     ./pm

   Clue will then build the compiler and the modified Lua interpreter that's
   used as an assembler.
   
Clue is currently not designed to be installed anywhere; it should be run from
the place where you built it. I did warn you it was experimental...



USAGE
=====

You may compile a C program as follows:

	bin/clue -m<backend> test/helloworld.c > output.<extension>
	
<backend> may be lua51, lua52, js, perl5, java or c.
   
This will read in the source file, compile it and write out the result.

Once you have a runnable result, you may run it with the cluerun tool. This
sets up the run-time environment needed to run Clue code.

    ./cluerun -f output.lua
or  ./cluerun -f output.js
or  ./cluerun -f output.pl
or  ./cluerun -f output.lisp
or  ./cluerun -f output.j
or  ./cluerun -f output.c

By default, cluerun will try to use LuaJIT for .lua files, SpiderMonkey
for .js files and Perl 5 for .pl files. You can change this with the -e
option, which may have one of the following values:

    lua51        standard Lua interpreter (version 5.1)
    lua52        standard Lua interpreter (version 5.2)
    luajit2-jon  Mike Pall's LuaJIT in JIT mode
    js           Node V8 Javascript interpreter
    perl5        Perl 5 interpreter
    lisp         SBCL Common Lisp interpreter
    j            standard Java compiler and interpreter
    c            C compiler via gcc

You can pass arguments to the Clue program by suffixing cluerun with a --.

    ./cluerun -e js -f output.js -- arg1 arg2 arg3

You may use multiple files --- all of the same type, of course --- and they
will get linked together and main() run.

    ./cluerun -e js -f file1.js -f file2.js -f file3.js -- arg1 arg2 arg3   

This works to a certain extent (it's used by the benchmarks), but is still a
bit problematic. (sparse has some bugs that cause prototyping symbols to
sometimes not work correctly.)



BENCHMARKING
============

Clue is complete enough to run a number of benchmarks. These are located
in the test directory.

Most of the benchmarks are taken from the Computer Language Benchmark
Game (neรฉ the Great Computer Language Shootout), located here:

    http://shootout.alioth.debian.org/
    
The CLBG benchmarks can be run en mass using the supplied run-benchmarks
script. It will run each benchmark using gcc, clue via a Lua interpreter,
and clue via LuaJIT (if you have it). (Note that the script does *not*
use the same number of iterations as the CLBG uses, as I'm not getting
any younger.) However, the CLBG benchmarks are very inaccurate and they
should be considered unit tests only.

Also supplied is a version of the very elderly Whetstone benchmark. This
can be run as a normal program.

See README.benchmarks for the results I get on my machine.



WRITING PROGRAMS WITH CLUE
==========================

Clue supports standard ANSI C (C89, some C99, some bugs). Unfortunately,
most programmers don't! There are a number of places where Clue behaves
very differently to normal C compilers, 

- you may not cast any kind of pointer to a number, or vice
  versa (except for the special case of constant 0, which may be
  cast to any kind of pointer).
  
- you may not cast function pointers to object pointers or vice
  versa.
 
- accessing memory via the wrong kind of pointer is not supported. This
  means you can't do this:
  
  {
    void* p = &fnord;
    return *(int*)p;
  }
  
  You *can* use unions to store two objects of different types at the same
  memory location --- but accessing the wrong one will probably produce a
  runtime error.
  
- sizeof(char) == sizeof(int) == sizeof(long) == sizeof(void(*)()) == 1.
  sizeof(void*) == 2.

- trying to operate on uninitialised memory will most likely produce a
  runtime error (as it will probably be initialised to something that you
  can't do things like arithmetic operations on). 

- the libc is *very* terse. I implemented only those functions necessary
  to make the benchmarks work.

Violating any of these will sometimes produce a compile-time error but
mostly you'll get a really, really strange run-time error.



BUGS
====

Hahahaha! Hahahahahaha! Haha!

Okay, there are some bugs. Did I mention this was experimental?

The ones I've actually noticed include, but are not limited to:

Compiler bugs:

- inline doesn't work.

- Multiple declarations of items doesn't work. (This is a sparse bug.)

- sparse can't always distinguish between different scalar types of the same
  size, which means that backends can't distinguish between reals and integers;
  this usually manifests itself in parameters being passed to functions in the
  wrong register. This means that backends must use the same type to represent
  both reals and integers, which prevents certain optimisations in c and java.
  (This is a sparse bug.)
  
- Various compiler features aren't implemented yet, like switch or varargs.

Libc bugs:

- stdio can only read or write text on Lua; this causes clbg-mandelbrot to
  produce invalid data when run using clue.
  
- printf is very basic and doesn't properly implement the printf spec
  (different on each platform).
  
- the Common Lisp libc is only partially implemented, which means that the
  benchmarks aren't run for it. 

There are others. Oh, yes, there are others.
  


TODO
====

There are a number of ways to take Clue further.

- Add more targets. I'd like the Java target to emit bytecode; that way we
  get a real goto implementation, which should boost performance. There's
  also an experimental Common Lisp target that was contributed that needs a
  libc to be useful. 
   
- Finish the language coverage. There are a number of C language features
  that I haven't gotten around to doing (like switch).  


LICENSING
=========

Different parts of the supplied source are covered by different licenses.

test/clbg-*.c are very slightly tweaked (mostly changing %ld to %d in
format strings) versions of the Computer Language Benchmark Game
benchmarks. See http://shootout.alioth.debian.org. These are covered by the
Revised BSD license. See
http://shootout.alioth.debian.org/gp4/miscfile.php?file=license&title=revised%20BSD%20license
for the text.

test/whetstone.c is a hacked copy of one of the million or so Whetstone
variations dating from the 1980s. It is, I believe, public domain. If anyone
has more information, please get in touch.

src/clue/cg-lisp.c and src/lisp was contributed by Peter Maydell. It is
covered by the Revised BSD license (text below).

Practically everything else was written by me, David Given, and is covered by
the Revised BSD license. The text follows:

Copyright (c) 2008, David Given
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

# $Id$
# $HeadURL$
# $LastChangedDate: 2008-07-19 23:13:17 +0100 (Sat, 19 Jul 2008) $

More Repositories

1

wordgrinder

A word processor which gets the hell out of your way and lets you get some work done.
Lua
943
star
2

ack

The Amsterdam Compiler Kit
C
420
star
3

fluxengine

PSOC5 floppy disk imaging interface
C++
352
star
4

cpmish

An open source sort-of CP/M 2.2 distribution.
Assembly
346
star
5

cpm65

CP/M for the 6502
Assembly
266
star
6

cowgol

A self-hosted Ada-inspired programming language for very small systems.
C
238
star
7

minix2

Minix 1 and 2, Quick and Dirty editions
C
117
star
8

typetalk

A SmallTalk like live coding system running in the browser, based on TypeScript.
TypeScript
65
star
9

luje

a Java virtual machine written in pure Lua
Java
61
star
10

fforth

A small, portable Forth written in Posix C
C
44
star
11

LBW

Experimental tool for running Linux binaries on Windows
C++
39
star
12

telinkdebugger

A Raspberry Pi Pico-based debugger bridge for the Telink SWS debug protocol.
C++
22
star
13

pcemu

A software 8086 PC emulator.
PostScript
19
star
14

polf

A toy
Assembly
17
star
15

piface

Bare metal boot loader for the Raspberry Pi's VideoCore processor (no ARM!)
C
12
star
16

narcissus

A chording keyboard tool for X
C
11
star
17

cowjac

An experimental Java bytecode to C++ transpiler
Java
10
star
18

jpegfinder

A simple tool for scraping jpeg frames from files. Suitable for recovering corrupted MJPEG files. I'm looking at you, Hubsan.
C
10
star
19

glueesp

A port of the Geoworks Glue DOS linker.
C
10
star
20

ibm6770keyboard

A USB interface for the IBM 6770/6780 keyboard.
C++
9
star
21

cowbel

An experimental programming language
xBase
8
star
22

btracker

A chiptracker editor and player for the BBC Micro.
Assembly
8
star
23

calculon

A very small, fast shader language using LLVM.
C++
8
star
24

bogomandel

A silly toy Mandelbrot program for the BBC Micro.
Assembly
7
star
25

dso152-breakout

Breakout for the Fnirsi DSO152
C++
7
star
26

plmc

A compiler for PL/M targeting LLVM bitcode.
Yacc
6
star
27

dbztool

A command line tool for accessing the bootstrap protocol on Dragonball CPUs.
C++
5
star
28

gcc-vc4

An experimental port of gcc 4.8.1 to the VideoCore IV.
C
5
star
29

gruntle

A text-based CYO MMORPG engine
HTML
4
star
30

cshell

A very simple command line shell for the Commodore 64.
Assembly
4
star
31

qemu-z80

Z80 support for qemu
C
4
star
32

bterm

Firmware/hardware to use a Brother WP-1 word processor as a serial terminal
Assembly
4
star
33

vtech6502

A collection of data about the internals of various VTech 6502-based toys.
4
star
34

cparser

C
4
star
35

maxii-keyboard

PSoC5 firmware to for an ancient MAX-II industrial keyboard
C
4
star
36

libfirm

C
3
star
37

spey

A spam-filtering SMTP proxy using greylisting
Shell
3
star
38

comal65

A Comal interpreter for the 6502
Assembly
3
star
39

stellation

A web based real time space warfare RTS.
Shell
2
star
40

vimutti

Tool for decrypting Buddha Machine flash images.
C
2
star
41

pblq

Amstrad eMailer PBL boot loader client
Shell
2
star
42

objective-lua

A dialect of Lua extended with syntax borrowed from Objective C to add object orientation support.
Lua
1
star
43

ebooksyncer

A tiny tool for syncing (and decrypting) eink Kindle books from the device to a directory for backup.
1
star
44

flooded-moon

The moon, like you've never seen it before.
POV-Ray SDL
1
star
45

uboot-pw1

Customised uboot for Kindle Paperwhite gen 1 devices.
C
1
star
46

bmdkey

Python
1
star
47

sdcc

Patched sdcc for Fuzix
C
1
star
48

fluxengine-testdata

Test data for the FluxEngine project.
1
star
49

ab

Yet Another Build Generator
Python
1
star
50

fgit

Tools for using Fossil as a git client.
Shell
1
star