Coder Social home page Coder Social logo

drtcc's Introduction

DrTcc (Dr. Tiny C Compiler)

DrTcc(玩具项目)使用 C++ 14 实现了一个C语言子集,基于栈虚拟机 , 既像解释器又像编译器。这个项目旨在满足我的好奇心和好玩心。

介绍

在开始启动这个项目之前,是想实现一个算法可视化平台,因此学习了write-a-C-interpreter ,给了我实现的方向。代码量不多但我依然觉得结构臃肿,写的不够优雅,没怎么发挥 C++的实力。等学过 Lua 的VM 和 Compiler Optimization再来review code。

文件目录结构

DrTcc
├─bin
│      happy.txt
│      xc.txt
├─include							
│      AST.cpp
│      AST.h
│      GenCode.cpp
│      GenCode.h
│      ImportSTL.h
│      Lexer.cpp
│      Lexer.h
│      MemoryPool.h
│      Parser.cpp
│      Parser.h
│      Token.cpp
│      Token.h
│      Type.h
│      VM.cpp
│      VM.h
│
├─src
│      main.cpp

特性

  1. 手写递归下降Parser
  2. 输入源码生成AST(abstract syntax trees),实现简易内存池管理AST结点,采用POD类型
  3. 在生成AST结点时和IR时语义分析和简易的类型检查
  4. 指令集参考write-a-C-interpreter,根据AST生成IR
  5. 栈虚拟机实现虚拟内存(采用二级页表结构),执行IR,实现物理内存隔离

调试信息

// located in include/Parser.cpp
#define Tokenize 0		// 输出词法分析后的Token流
#define GEN_AST 0		// 输出AST

// located in include/GenCode.cpp
#define GEN_DEBUG 0		// 输出AST-->IR

// located in include/VM.cpp	
#define VM_DEBUG 0				// 输出VM内存信息
#define INSTRUCTION_DEBUG  0	// 输出VM字节码

改进方向

  1. stack based -> register based
  2. 编译器后端优化.. (还没开始学..枯了)
  3. 争取下一次用更好的OOP在解耦合,争取去掉宏定义(但有时候宏定义真的很好用),争取下一次更好的 use C++

功能/支持

  • 全局/局部变量及初始化
  • 函数调用以及递归调用,函数形参
  • 枚举
  • if, while语句
  • 一元,二元,三元运算
  • sizeof运算
  • 取址和解引用
  • 类型转换
  • 一些内建函数(printf,malloc....)

Test & 截图

以此代码为例:

int fibonacci(int i)
{
    if (i <= 1)
        return 1;
    return fibonacci(i - 1) + fibonacci(i - 2);
}

int main()
{
    int i;
    i = 0;
    printf("----- fibonacci -----\n");
    while (i <= 10)
    {
        printf("fibonacci(%2d) = %d\n", i, fibonacci(i));
        i++;
    }
    return 0;
}
运行结果:

产生的指令:

213123

Build & Run

只支持32位,使用 Cmake MinGw build

make 

直接编译你的源文件

.\happy.exe SourceCodeFile

bin/xc.txtwrite-a-C-interpreter 源码,递归下去实现write-a-C-interpreter的自举

.\happy xc.txt xc.txt ... 

或者先编译xc.txt 再编译你的源文件

.\happy xc.txt YourSourceCodeFile

Licence

The original code is licenced with MIT

drtcc's People

Contributors

organ1sm avatar

Watchers

 avatar

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.