项目作者: jdao55

项目描述 :
toy compiler implemented using c++ and llvm
高级语言: C++
项目地址: git://github.com/jdao55/toy-compiler.git
创建时间: 2019-10-01T20:49:35Z
项目社区:https://github.com/jdao55/toy-compiler

开源协议:

下载


toy-compiler

A toy (Kaleidoscope) compiler based on llvm’s guide https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html,
currently compiles to object files, which can be linked using clang/gcc

Building

Dependencies

  • cmake
  • C++ 20 compiler
  • conan
  • Flex
  • llvm

    Build using Cmake

  1. Clone dirrectory
    1. git clone https://github.com/jdao55/toy-compiler && cd toy-compiler
  2. Create build directory
    1. mkdir build && cd build
  3. Run cmake
    1. cmake ../
  4. Build using make
    1. make

    Usage

    ```
    Usage:
    toycomp [—out=filename] [—opt=level]
    toycomp (-h | —help)

Options:
-h —help Show this screen.
-o filname —out=filename Specify output object file name
-O level —opt=level Specify optimization level [1,2,3])”;

  1. # Example
  2. Kaleidoscope program test.toy
  3. ```python
  4. def add(x y)
  5. x + y
  6. def add3(x y z)
  7. x + y + z

Compile with toycompiler
toycompiler test.toy -out=add.o

Calling Kaleidoscope from c++

sample c++ program example.cpp

  1. #include <iostream>
  2. extern "C" {
  3. double add(double, double);
  4. double add3(double, double, double);
  5. }
  6. int main()
  7. {
  8. std::cout << add(1.2, 2.3) << std::endl;
  9. std::cout << add3(1.0, 2.0, 3.0) << std::endl;
  10. }

Compile and link using gcc/clang
g++ example.cpp add.o -o example

TODO

Language features

  • arrays
  • data types (WIP see types-ext branch)
    • integers
    • strings
  • main function

    Compiler features

  • add optimization passes (WIP see optimzer branch)
  • add compilation targets
  • refactor main?