Code Monkey home page Code Monkey logo

kiwicc's Introduction

kiwicc

kiwicc is a hobby C compiler for RISC-V.

It can compile itself include preprocessor.

kiwicc is inspired by chibicc which is a C compiler for x86-64 and compilerbook.

Setup

$ docker build -t riscv_dev .

Run development environment

# run docker container for development
$ sh start_kiwicc_dev.sh

Build kiwicc

# in development environment
$ make

Test kiwicc

# in development environment

# test kiwicc compiled with gcc (stage 1)
$ make test

# test stage 2 kiwicc
$ make kiwicc-stage2

# test stage 3 kiwicc
$ make kiwicc-stage3

# test kiwicc from stage 1 to stage 3
# https://stackoverflow.com/questions/60567540/why-does-gcc-compile-itself-3-times
$ make test-all

Install kiwicc

$ make install

Compile a C program for RISC-V and run it on QEMU user-mode emulation

# in development environment

# compile with kiwicc
$ qemu-riscv64 kiwicc foo.c -o tmp.s
$ riscv64-unknown-linux-gnu-gcc tmp.s -o a.out

# compile with kiwicc installed by `sudo -E make install`
$ kiwicc foo.c -o tmp.o # binfmt_misc calls qemu-riscv64 implicitly
$ riscv64-unknown-linux-gnu-gcc tmp.s -o a.out

# compile with GCC
$ riscv64-unknown-linux-gnu-gcc -g -O0 foo.c

# run on QEMU user-mode emulation
$ qemu-riscv64 a.out

# debugging with gdb
$ riscv64-unknown-linux-gnu-gdb a.out
(gdb) shell qemu-riscv64 -g 1234 a.out &
(gdb) target remote :1234

# debugging kiwicc with gdb
$ riscv64-unknown-linux-gnu-gdb kiwicc
(gdb) shell qemu-riscv64 -g 1234 ./kiwicc tests/tests.c -I./tests/test_include -I./tests -fno-pic -o tmp.s &
(gdb) target remote :1234

Example

Compile and run 2048

# in development environment

$ sudo -E make install
$ cd /home/user
$ git clone https://github.com/mevdschee/2048.c
$ cd 2048.c
$ qemu-riscv64 /opt/riscv/bin/kiwicc -o 2048.s 2048.c # `kiwicc -o 2048.s 2048.c` is also OK.
$ riscv64-unknown-linux-gnu-gcc -o 2048 2048.s
$ qemu-riscv64 2048

Compile and run Tree command

# in development environment

$ sudo -E make install
$ cd /home/user
$ mkdir -p ./tree
$ curl -sL --retry 3 --insecure "http://mama.indstate.edu/users/ice/tree/src/tree-1.8.0.tgz" | tar xz --no-same-owner --strip-components=1 -C ./tree
$ cd tree

# configure, build and run
$ sed -i -e 's/^CC=gcc/CC=kiwicc/' -e 's/^CFLAGS/#CFLAGS/' -e 's/$(CC) $(LDFLAGS)/riscv64-unknown-linux-gnu-gcc $(LDFLAGS)/' Makefile
$ make
$ qemu-riscv64 tree

Reference

https://www.sigbus.info/compilerbook (Japanese)

https://github.com/rui314/chibicc

http://port70.net/~nsz/c/c11/n1570.html

https://riscv.org/technical/specifications/

https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md

https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md

https://msyksphinz-self.github.io/riscv-isadoc/html/index.html#

kiwicc's People

Contributors

utokusa avatar

Stargazers

Linus avatar

Watchers

James Cloos avatar  avatar

kiwicc's Issues

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.