Giter Club home page Giter Club logo

c-compiler-rs's Introduction

KECC: KAIST Educational C Compiler

Install

Install rustup.

Build

cargo build            # debug build
cargo build --release  # release build

Run

cargo run -- -h                                     # print options
cargo run -- -p            examples/c/fibonacci.c   # parse
cargo run -- -i            examples/c/fibonacci.c   # irgen
cargo run -- -O --iroutput examples/c/fibonacci.c   # optimize
cargo run --               examples/c/fibonacci.c   # compile

cargo run -- --irrun examples/c/fibonacci.c   # interprets the IR

cargo run --release -- examples/c/fibonacci.c  # compile with release build

Test

RUST_MIN_STACK=33554432 cargo test             # debug build test
RUST_MIN_STACK=33554432 cargo test --release   # release build test

RUST_MIN_STACK=33554432 cargo test test_examples_write_c       # run write_c test

RUST_MIN_STACK=33554432 cargo test test_examples_irgen_small   # run irgen test using a small subset of examples
RUST_MIN_STACK=33554432 cargo test test_examples_irgen         # run irgen test

RUST_MIN_STACK=33554432 cargo test test_examples_simplify_cfg  # run simplify_cfg test
RUST_MIN_STACK=33554432 cargo test test_examples_mem2reg       # run mem2reg test
RUST_MIN_STACK=33554432 cargo test test_examples_deadcode      # run deadcode test
RUST_MIN_STACK=33554432 cargo test test_examples_gvn           # run gvn test

RUST_MIN_STACK=33554432 cargo test test_examples_asmgen_small  # run asmgen test using a small subset of examples
RUST_MIN_STACK=33554432 cargo test test_examples_asmgen        # run asmgen test

RUST_MIN_STACK=33554432 cargo test test_examples_end_to_end    # run irgen, optimize and asmgen pipeline test

RUST_MIN_STACK=33554432 is necessary for deep call stack for irgen tests.

Fuzzing

We encourage you to do homework using the test-driven development approach (TDD). You randomly generate test input, and if it fails, then reduce it as much as possible and manually inspect the reduced test input. For example, for homework 1, do:

# randomly generates test inputs and tests them
python3 tests/fuzz.py --print

# reduces the failing test input as much as possible
python3 tests/fuzz.py --print --reduce

# fix your code for the reduced test input
cat tests/test_reduced.c

Install

# Ubuntu 18.04 or higher
apt install -y make cmake python3
apt install -y csmith libcsmith-dev creduce

Run

The following script generates 10 random test cases and tests your C AST printer:

python3 tests/fuzz.py --help        # print options
python3 tests/fuzz.py --print -n10  # test C AST printer for 10 times

We use csmith to randomly generate C source codes. csmith will be automatically downloaded and built by the test script. For more information, we refer to the Csmith homepage.

Reduce

When the fuzzer finds a buggy input program for your compiler, it is highly likely that the input program is too big to manually inspect. We use creduce that reduces the buggy input program as much as possible.

Suppose tests/test_polished.c is the buggy input program. Then the following script reduces the program to tests/test_reduced.c:

python3 tests/fuzz.py --reduce <fuzz-option>

<fuzz-option> can be --print or --irgen. It shall be the one used in Run.

How it reduces test case?

The script performs unguided test-case reduction using creduce: given a buggy program, it randomly reduces the program; check if the reduced program still fails on the test, and if so, replaces the given program with the reduced one; repeat until you get a small enough buggy program. For more information, we refer to the Creduce homepage.

[NOTICE] The fuzzer supports Ubuntu 18.04 or 20.04 only. It may work for other platforms, but if it doesn't, please run the fuzzer in Ubuntu 18.04 or 20.04.

Running RISC-V Binaries

Install

# Ubuntu 20.04 or higher
apt install gcc-10-riscv64-linux-gnu g++-10-riscv64-linux-gnu qemu-user-static

Cross-Compilation and Architecture-Emulation

# Compile C source code into RISC-V assembly
riscv64-linux-gnu-gcc-10 hello.c -S -o hello.S

# Link to an RISC-V executable
riscv64-linux-gnu-gcc-10 -static hello.S -o hello

# Emulate the executable
qemu-riscv64-static ./hello

# Check the return value
echo $?

Run Benchmark for Performance Competition

cd bench
make run

Submission

  • Submit the corresponding files to gg.kaist.ac.kr.
  • Run ./scripts/make-submissions.sh to generate hw3.zip to hw6.zip, which you should submit for homework 3 to 6.

Debug

  • start qemu gdbserver with qemu-riscv64-static -g 33669 ./a.out
  • connect qemu gdbserver with following commands:
        $ gdb-multiarch ./a.out
            > target remote 127.0.0.1:33669
            > layout regs
            > layout asm
  • double to hex
import binascii
import struct
struct.unpack('d', binascii.unhexlify("3cfd4f970e804f1"))

def double_to_hex(f):
    return hex(struct.unpack('<Q', struct.pack('<d', f))[0])

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.