1 Star 1 Fork 0

cong葱 / simplecc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

A simple and modular C-like compiler in C++

1. Source language

This project is a small compiler written in modern C++ for education and practice. The source language it compiles is called C0, which is a modified subset of C for educational purposes. The BNF grammar rules of C0 can be found in the file Grammar. In brief, this tiny language consists of the following components:

  1. Constant declarations, like const int a = 1, f = 2;
  2. Variable declarations, like char b; int c;
  3. Function definitions, like void func() {}
  4. Some C-like statements and expressions.

The test/ folder contains some vivid examples of the C0 language (files with .c0 suffix).

2. Supported backends

2.1 Mips Assembly

We have a hand-rolled stack-based IR generator that emits unoptimized Mips assembly, which can be executed by the Mars simulator. To use this backend, please run:

simplecc --asm input.c0

For more examples of the emitted Mips assembly

2.2 LLVM IR

We also integrate with the LLVM backend to enjoy its capacities of optimization and native codegen. To obtain an LLVM IR file, please run:

simplecc --emit-llvm input.c0

For more examples of emitted IR, please see the *.ll files in test/LLVM/ir/.

To obtain a native executable, please run:

simplecc --emit-llvm | clang -x ir

3. Visualization & debug support

We provide some support to inspect the intermediate results of the compiler.

3.1 Tokens

To see a stream of tokens, please run:

simplecc --print-tokens input.c0

This will print details about the tokens extracted from the source (one token in a line).

3.2 AST

To pretty-print the AST of the program, please run:

simplecc --print-ast input.c0

To obtain a flowchart of the AST, a graphviz-based diagram of AST can be generated with:

simplecc --ast-graph input.c0

This command will generate a dot file, which can be passed to the command dot as follows:

simplecc --ast-graph input.c0 | dot -Tpng -o input.png

3.3 Syntax checking

To check the syntax of a source file (but not emitting anything), please run:

simplecc --check-only input.c0

This will print any detected errors to the console.

4. Build & Install

This project requires a modern compiler that supports C++11. Please run the following commands to build and install our executable:

cd simplecc/ && mkdir -p build/
cd build/ && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME ../src
make && make install

The command simplecc will then be available.

5. Optimization

For simplicity, we do not implement too many optimization passes. Only simple constant folding and dead code elimination are implemented. Please see src/lib/Transform for more details.

6. Citation

If you use our code in your research, we highly recommend your proper citation as follows:

@misc{cong_simplecc_2018,
	title = {Simplecc: {A} simple and modular {C}-like compiler in {C}++},
	shorttitle = {Simplecc},
	url = {https://github.com/cgsdfc/simplecc.git},
	abstract = {Simplecc is a small compiler written in modern C++ that compiles an educational C-like language C0. It has a primary backend that emits stack-based unoptimized Mips assembly. It also integrates with an external LLVM backend that enables optimization and native codegen. Internally it uses a generated parser and generated abstract syntax tree with other important components written in an object-oriented style.},
	author = {Cong Feng},
	year = {2018},
}
MIT License Copyright (c) 2018 Cong Feng. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A simple and modular C-like compiler in C++ for educational purposes. 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/cgsdfc/simplecc.git
git@gitee.com:cgsdfc/simplecc.git
cgsdfc
simplecc
simplecc
master

搜索帮助