Skip to content

Bariona/Compiler

Repository files navigation

Mx-Compiler


  • Current Progress: Optimization done...

💻 Course Link

  • A language-processing system:
graph TB
S(source program)-->P[Preprocessor]
P--modified source program-->C[Compiler]
C--target assembly program-->A[Assembler]
A--relocatable machine code-->L[Linker/Loader]
li[library file <br> relocatable object files]-->L
L-->T(target machine code)
Loading

Configuration

IDEA Java environment

Set up Antlr4 on Win10

Relevant commands

   antlr4 <filename>.g4 -visitor
   javac *.java
   grun <filename> <rulename> -tokens
   
   diff <file1> <file2> -qZB # regardless of space endl

compile java file:

javac <filename>.java 
javac -encoding UTF-8 <filename>.java # support Chinese comment 
java <filename>

enable assert mode: in IDEA's run configuration, add -ea in "VM option".

.c to .ll

clang <filename>.c -S -emit-llvm -O0 -fno-discard-value-names

.ll to .s

llc builtin.ll -o builtin.s -march=riscv32 -mattr=+m

Semantic Design

  • File structure:
graph LR
g4[Mx.g4]-->V[MxBaseVisitor]
g4-->P[MxParser, MxLexer...]

V==ASTNode==>A[ASTBuilder]

A--Scope-->S[SemanticChecker]
A--for debug-->D[ASTPrinter]
Loading
  • Procedure description:
graph LR
I(Code)--Character Stream-->L[Lexer]
L--Token Stream-->P[Parser]
P--frontend.parser tree-->AB[AST build]
P--visitor mode-->AB[AST build]
AB==AST node==>S[Semantic check]
AB==Scope manager==>S
Loading

IR

based on LLVM IR

Code generation

RISCV32i

Register Allocation

Reference

  1. Yx written by Antlr4
  2. Tiger Book (Modern Compiler Implementation)
  3. Dragon Book (Compilers: Principles, Techniques, and Tools 2e)
  4. NJU Static Analysis by Prof. Yue Li and Tian Tan
  5. SSA Book

About

🗿 toy Compiler for Mx* language

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published