Skip to content

youz/8cc-sqlite3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

8cc on SQLite3

This is a SQLite3 port of 8cc built on ELVM.

The compiler is implemented in a SELECT statement using recursive common table expression.

Requirements

SQLite3 shell (ver.3.9.0 or later) with JSON1 extension enabled

Warning
ver 3.39.0 or later is strongly recommended
ver 3.35.0 ~ 3.38.5 creates a huge temporary file (over 100GiB) by CTE-materialization feature.
ref. SQLite - The WITH Clause - Materialization Hints
Fixed in ver 3.39.0 to avoid unnecessary materialization.

Build

$ git clone https://github.com/youz/8cc-sqlite3.git
$ make

You'll get a database binary file elvm.db3. Or get the pre-built binary from here.

DB objects

Tables

  • src(b BLOB) - C source table
  • eir(b BLOB) - ELVM IR table
  • option(target TEXT) - Option table

Views

  • elvm_8cc(stdout TEXT) - C compiler (compiles C to ELVM IR)
  • elvm_elc(stdout TEXT) - ELVM IR assembler
  • elvm_8cc_elc(stdout TEXT) - pipelining 8cc & elc

Usage

Import C source code

$ sqlite3 elvm.db3
sqlite3> DELETE FROM src;
sqlite3> INSERT INTO src(b) VALUES('
int putchar(int);
int main() {
  char* p = "Hello, world!\n";
  for (; *p; p++) {
    putchar(*p);
  }
  return 0;
}');

or use readfile function.

sqlite3> DELETE FROM src;
sqlite3> INSERT INTO src(b) VALUES(readfile('samples/hello.c'));

Caution: Don't use #include directive in the C source

Compile C to ELVM IR

Select stdout from the view elvm_8cc and insert it into the table eir(b BLOB).

sqlite3> DELETE FROM eir;
sqlite3> INSERT INTO eir(b) SELECT stdout FROM elvm_8cc;

Caution: It takes a few minutes to compile a hello world program

Assemble ELVM IR to a target language

Set an alias-string for a target language to option.target. See Language List for details of alias strings.

Then select stdout from the view elvm_elc.

sqlite3> UPDATE option SET target = 'rb';
sqlite3> SELECT writefile('hello.rb', stdout) FROM elvm_elc;

Compile and assmeble at one time

Use the view elvm_8cc_elc

sqlite3> DELETE FROM src;
sqlite3> INSERT INTO src(b) VALUES(readfile('source.c'));
sqlite3> UPDATE option SET target = 'js';
sqlite3> SELECT writefile('output.js', stdout) FROM elvm_8cc_elc;

Supported target languages

alias target language
arm arm-linux
bef Befunge
bf Brainfuck
c C
cl CommonLisp
cpp C++14 (compile-time)
cr Crystal (compile-time)
el Emacs Lisp
forth Forth
go Go
i C-INTERCAL
java Java
js JavaScript
php PHP
piet Piet
pietasm Piet Assembler
pl Perl5
py Python
rb Ruby
sed sed
sh Bash
sqlite3 SQLite3
swift Swift
tex TeX
tf TensorFlow (WIP)
tm Turing machine
unl Unlambda
vim Vim script
ws Whitespace
x86 i386-linux

This list is based on ELVM revision e99b36e53bfd9c5721c579d143ac052cf94cdb17.

About

C Compiler written in SQL (for SQLite3)

Resources

License

Stars

Watchers

Forks

Packages

No packages published