Skip to content

PaperL/Mxx-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mxx Compiler

GitHub top language License

Latest tag Last commit GitHub repo size GitHub code size in bytes

For Chinese readme, see README_CN.md

Introduction

  • A compiler that compiles a C-and-Java-like language Mx* to LLVM Language Reference and RISC-V (RV32M) assembly (.s file), written in Java.
  • The output can be executed by interpreter Ravel.
  • This project is the assignment for the sophomore year of the ACM class of 2020. See assignment description at Compiler-2022.

Project Description

  • This project consists of four parts:

    • Semantic, Codegen, Optimization, and Bonus.
  • No third-party libraries are used except for Antlr4.

    • Basic parser and lexer are generated by Antlr4.
  • For detailed implementation descriptions for each part, see DETAIL.md (Chinese only).

Diagram

  • Additional features not required by the assignment description:

    • Support \* *\ comment;
    • Support optional comma at the end of list (e.g.: fun(1, 2, 3,););
    • Support ++ / -- expressions as left value;
    • Support constructor with parameters (not support semantic check);
    • Support escaped character \".
  • How to run

    • Run make <cmd> in a terminal in the root directory of this project. See Makefile for details.
      • build / clean: Build the project / clean the runtime content;
      • semantic: Run grammar and semantic analysis;
      • ir: Compile to LLVM IR;
      • run: Compile to RISC-V assembly;
      • test_ir: Evaluate the correctness of generated LLVM IR.
        • Run make test_ir TESTCASE=<name> to evaluate a single test case. See ir_local_judge.py for the evaluation script.

Source Code Structure

Italic denotes package names, and bold class names indicate that subsequent classes have implementation or inheritance relationships with that class.

  • Main

  • frontend

    • parser (generated by Antlr4)
      • MxxLexer
      • MxxParser
      • MxxParserListener, MxxParserBaseListener
      • MxxParserVisitor, MxxParserBaseVisitor
    • ast
      • node
        • AstNode, NodeRoot, NodeProgramSection, NodeClassDefine, NodeFunctionDefine, NodeVariableDefine, NodeType, NodeArgumentList, NodeSuite, NodeVariableTerm, NodeExpression, NodeBracket, NodeStatement, NodeExpressionList, NodeAtom
      • scope
        • VariableScope, BroadScope, ClassScope, FunctionScope
      • AstPosition, AstType
      • AstBuilder, ForwardCollector, SemanticChecker
    • ir
      • node
        • IrNode, IrTop, IrClass, IrFunction, IrBlock, IrInsturction
      • IrType, IrId
      • IrBuilder
  • backend

    • asm
      • node
        • AsmNode, AsmTop, AsmFunction, AsmBlock, AsmInstruction
      • AsmId, AsmStackFrame
      • AsmBuilder
    • optimization
      • IrOptimizer, AsmOptimiz
  • utility

    • error
      • Error, SyntaxError, SemanticError, InternalError
    • Constant
    • CmdArgument