Skip to content

北京大学《编译原理》课程实践

Notifications You must be signed in to change notification settings

uednd/PKU-Compiler-Course

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PKU Compiler Course

Project Structure

src/
├── frontend/             # Frontend: lexical analysis, syntax analysis, AST
│   ├── ast.c/h          # Abstract syntax tree definition and operations
│   ├── sysy.l           # Flex lexical analyzer
│   └── sysy.y           # Bison syntax analyzer
├── midend/               # Middle-end: intermediate code generation
│   ├── codegen.c/h      # Koopa IR code generator
│   └── koopa_ir.c/h     # Koopa IR processing utilities
├── backend/              # Backend: target code generation
│   └── riscv_gen.c/h    # RISC-V assembly code generator
└── main.c                # Main program entry point

Quick start

# pull image
docker pull maxxing/compiler-dev
# mount
docker run -it --rm -v "/home/uednd/Code/PKU-Compiler-Course:/workspace" maxxing/compiler-dev bash
cd /workspace
# config and build
cmake -DCMAKE_BUILD_TYPE=Debug -B build && cmake --build build

Usage

Generate Koopa IR

./build/compiler -koopa test/hello.c -o hello.koopa
cat hello.koopa

Generate RISC-V Assembly

./build/compiler -riscv test/hello.c -o hello.s
cat hello.s

Show AST Structure (Debug)

./build/compiler -ast test/hello.c -o hello.ast

Example

Given input file test/hello.c:

int main() {
  return 0;
}

Koopa IR Output (-koopa mode):

fun @main(): i32 {
%entry:
  ret 0
}

RISC-V Assembly Output (-riscv mode):

  .text
  .globl main
main:
  li a0, 0
  ret

Note:
If you are using Linux and the repository is on a mounted external hard drive, Docker may not have the necessary permissions when running the mount command above, and however, it won't tell you about it. This shit wasted 2hs of my life.

About

北京大学《编译原理》课程实践

Resources

Stars

Watchers

Forks