Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# set (CMAKE_CXX_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -lm")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og -lm")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdebug-prefix-map=${CMAKE_BINARY_DIR}=${CMAKE_SOURCE_DIR}") #fix gdb can't find source path
message(${CMAKE_CXX_FLAGS})
set(CMAKE_BUILD_TYPE "Debug")
Expand All @@ -21,6 +21,7 @@ set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_EXPORT_COMPILECOMMANDS ON)
set(CMAKE_CXX_FLAGS "-DCMAKE_EXPORT_COMPILE_COMMANDS=1")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)

include_directories(${CMAKE_SOURCE_DIR}/util)
include_directories(${CMAKE_SOURCE_DIR}/include/backend)
Expand All @@ -35,7 +36,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/yacc)
add_subdirectory(${PROJECT_SOURCE_DIR}/ir)
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
add_subdirectory(${PROJECT_SOURCE_DIR}/backend)
add_subdirectory(${PROJECT_SOURCE_DIR}/test_infrastructure)
# add_subdirectory(${PROJECT_SOURCE_DIR}/test_infrastructure)
# add_subdirectory(${PROJECT_SOURCE_DIR}/Target)

target_link_libraries(SYSY-compiler Yacc Lib Backend opt Analysis) #Target
Expand Down
13 changes: 13 additions & 0 deletions backend/BackendDCE.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* @file BackendDCE.cpp
* @brief 死代码消除 (Dead Code Elimination, DCE) 实现文件
*
* 本文件实现了 `BackendDCE` 类中的方法,用于在 RISC-V 后端编译器中进行死代码消除。
* 主要功能包括:
* - 计算基本块的后继
* - 遍历函数中的所有基本块并尝试删除无用指令
* - 更新每个基本块的活跃变量集合
* - 判断是否可以处理某个指令
*/
//? 对寄存器分配前的MIR优化

#include "../include/backend/BackendDCE.hpp"
using DCE = BackendDCE;

Expand Down
12 changes: 12 additions & 0 deletions backend/BuildInFunctionTransform.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* @file BuildInFunctionTransform.cpp
* @brief 内置函数转换实现文件
*
* 本文件实现了 `BuildInFunctionTransform` 类中的 `run` 方法,用于在编译过程中对内置函数调用进行转换。
* 主要功能包括:
* - 遍历函数中的所有基本块
* - 检查每个基本块中的指令,如果是 `CallInst` 类型的指令,则调用 `Trival::BuildInTransform` 进行转换
* - 更新迭代器以确保遍历的正确性
*/
//? 在指令选择之前的优化阶段,属于高层 IR 的转换过程。

#include "../include/backend/BuildInFunctionTransform.hpp"
#include "../include/lib/Trival.hpp"

Expand Down
11 changes: 11 additions & 0 deletions backend/CodeLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @file CodeLayout.cpp
* @brief 代码布局优化实现文件
*
* 本文件实现了 `CodeLayout` 类中的 `run` 和 `getFreq` 方法,用于优化 RISC-V 函数的基本块布局。
* 主要功能包括:
* - 计算每个基本块的频率
* - 根据频率重新排列基本块以优化代码布局
* - 确保入口块始终位于最前面
* - 调整分支指令以适应新的基本块顺序
*/
#include "../include/backend/CodeLayout.hpp"
#include <cmath>

Expand Down
20 changes: 20 additions & 0 deletions backend/DeleteDeadBlock.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/**
* @file DeleteDeadBlock.cpp
* @brief 删除无用的基本块实现文件
*
* 本文件实现了 `DeleteDeadBlock` 类中的 `run` 方法,用于在编译过程中删除无用的基本块。
* 主要功能包括:
* - 遍历函数中的所有基本块
* - 查找并合并只包含一条无条件跳转指令的基本块
* - 更新所有指令中的基本块引用,确保引用指向合并后的基本块
* - 删除所有未作为代表的基本块,即所有被合并的基本块
*/

/**
* @brief 删除无用的基本块
*
* 该函数通过合并冗余的基本块来简化控制流图。它主要通过查找和合并那些只包含一条无条件跳转指令的基本块来实现。
*
* @param func RISC-V函数,包含一系列基本块
* @return bool 总是返回false,表示未修改控制流图的结构
*/
#include "../include/backend/DeleteDeadBlock.hpp"
bool DeleteDeadBlock::run(RISCVFunction *func){
std::unordered_map<RISCVBasicBlock*,RISCVBasicBlock*> alias;
Expand Down
9 changes: 9 additions & 0 deletions backend/FloatToDex.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @file FloatToDex.cpp
* @brief 浮点数转换为十进制表示
*
* 本文件实现了 `FloatToDex` 模块中的两个函数:
* - `getFloatIEEE`:获取浮点数的 IEEE 754 表示
* - `binaryToDecimal`:将二进制字符串转换为十进制整数
*/
//? 辅助工具,用于将浮点数转换为十进制表示
#include "../include/backend/FloatToDex.hpp"

void getFloatIEEE(float value) {
Expand Down
12 changes: 12 additions & 0 deletions backend/LegalizePass.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* @file LegalizePass.cpp
* @brief RISC-V 指令合法化处理实现
*
* 本文件实现了 RISC-V 指令集架构下的指令合法化过程,主要包括:
* - 将伪指令转换为实际指令
* - 处理立即数超出范围的情况
* - 调整寄存器和内存操作数
* - 处理分支指令中的立即数
* - 处理常量 0 的特殊情况
*/
//? 质量合法化优化
#include "../include/backend/LegalizePass.hpp"
#include "../include/backend/RISCVAsmPrinter.hpp"
#include <cstring>
Expand Down
12 changes: 12 additions & 0 deletions backend/Liveness.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* @file RISCVMIR_Liveness.cpp
* @brief 实现 RISC-V 指令集架构的活跃性分析和寄存器分配
*
* 本文件实现了 RISC-V 架构下的活跃性分析(Liveness Analysis)和寄存器分配(Register Allocation)。
* 主要功能包括:
* - 计算每个基本块的活跃输入(Live-in)和活跃输出(Live-out)
* - 计算每个指令的活跃区间(Live Intervals)
* - 构建干扰图(Interference Graph)
* - 打印活跃信息和分析结果
*/

#include "../include/backend/RISCVMIR.hpp"
#include "../include/backend/RISCVMOperand.hpp"
#include "../include/backend/RISCVRegister.hpp"
Expand Down
8 changes: 8 additions & 0 deletions backend/LowerFomralArguments.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @file RISCVISel.cpp
* @brief RISC-V 后端参数传递和并行参数处理实现
* @details 本文件实现了 RISC-V 后端的形式参数传递以及并行函数参数的特殊处理。主要功能包括:
* - 处理并行函数的参数传递,通过全局地址加载和内存操作将参数从堆栈加载到寄存器。
* - 普通函数的参数传递,根据参数类型和数量,将参数从堆栈或物理寄存器加载到虚拟寄存器。
*/

#include "../include/backend/RISCVISel.hpp"
/// @todo 参数带有浮点数的情况

Expand Down
13 changes: 13 additions & 0 deletions backend/PhiElimination.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/**
* @file PhiElimination.cpp
* @brief 实现了 Phi 消除优化的相关功能。
*
* 本文件包含了 `PhiElimination` 类的实现,该类负责处理 RISC-V 架构下的 Phi 指令消除问题。
* 主要功能包括:
* - 查找并创建关键基本块
* - 处理 Phi 指令的依赖关系图
* - 生成并插入复制指令
* - 处理基本块中的 Phi 指令
* - 对整个函数进行 Phi 消除优化
*/

#include "../include/backend/PhiElimination.hpp"
#include "../include/backend/RISCVMIR.hpp"
#include "../include/backend/RISCVTrival.hpp"
Expand Down
10 changes: 10 additions & 0 deletions backend/PostRACalleeSavedLegalizer.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* @file PostRACalleeSavedLegalizer.cpp
* @brief 实现了调用者保存寄存器的合法性检查和处理。
*
* 本文件包含了 `PostRACalleeSavedLegalizer` 类的实现,该类负责在寄存器分配后处理调用者保存的寄存器。
* 主要功能包括:
* - 筛选出需要保存和恢复的调用者保存寄存器
* - 在函数入口处保存这些寄存器
* - 在函数出口处恢复这些寄存器
*/
#include "../include/backend/PostRACalleeSavedLegalizer.hpp"
#include "../include/backend/RISCVTrival.hpp"

Expand Down
6 changes: 6 additions & 0 deletions backend/RISCVAsmPrinter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @file RISCVAsmPrinter.cpp
* @brief RISC-V 汇编代码生成器实现文件
* @details 本文件实现了 RISC-V 架构的汇编代码生成器,包括全局变量、临时变量、函数和段的处理。
* 提供了将中间表示(IR)转换为 RISC-V 汇编代码的功能。
*/
#include "../include/backend/RISCVAsmPrinter.hpp"

SegmentType __oldtype=TEXT;
Expand Down
6 changes: 6 additions & 0 deletions backend/RISCVContext.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @file RISCVContext.cpp
* @brief RISC-V 代码生成上下文实现文件
* @details 本文件实现了 RISC-V 代码生成上下文类 `RISCVLoweringContext`,负责将中间表示(IR)转换为 RISC-V 机器指令。
* 包括创建操作数、插入映射关系、生成虚拟寄存器等功能。
*/
#include "../include/backend/RISCVContext.hpp"
#include "../include/backend/RISCVAsmPrinter.hpp"
RISCVMOperand* RISCVLoweringContext::Create(Value* val){
Expand Down
7 changes: 7 additions & 0 deletions backend/RISCVFrameContext.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @file RISCVFrameContext.cpp
* @brief RISC-V 帧上下文实现文件
* @details 本文件实现了 RISC-V 帧上下文相关的类,包括 `NamedMOperand`、`OuterTag`、`RISCVObject`、`RISCVGlobalObject`、
* `RISCVTempFloatObject`、`RISCVFrameObject` 和 `StackRegister`。这些类用于管理 RISC-V 汇编代码中的命名操作数、
* 全局对象、栈帧对象和堆栈寄存器。
*/
#include "../include/backend/RISCVFrameContext.hpp"
std::string& NamedMOperand::GetName() {return name;}
NamedMOperand::NamedMOperand(std::string _name,RISCVType _tp):RISCVMOperand(_tp),name(_name){}
Expand Down
7 changes: 7 additions & 0 deletions backend/RISCVLowering.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @file RISCVLowering.cpp
* @brief RISC-V 代码生成和优化实现文件
* @details 本文件实现了 RISC-V 模块和函数的代码生成与优化过程。主要包括全局参数的降低、内置函数转换、
* φ节点消除、指令选择、寄存器分配、死代码消除、调度、帧生成和代码布局等步骤。
*/

#include "../include/backend/RISCVLowering.hpp"
#include "../include/backend/BuildInFunctionTransform.hpp"
#include "../include/backend/PhiElimination.hpp"
Expand Down
34 changes: 34 additions & 0 deletions backend/RISCVMIR.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
/**
* @file RISCVMIR.cpp
* @brief RISC-V 中间表示 (MIR) 的实现文件
* @details 本文件实现了 RISCVMIR 类及其相关方法,用于构建和操作 RISC-V 架构的中间表示。
* 包括指令的操作数管理、指令生成、打印等功能。
*
* 该类的主要作用是实现指令的功能,包括指令的操作符、左右操作数,同时指令集的定义也在该类中。
* 该类仅提供了简单的创建、修改左右操作数以及获取该指令不同信息的函数。
*
* @section MIR内容
* - 指令操作符:定义了 RISC-V 指令集的操作符。
* - 操作数管理:管理指令的操作数,包括定义操作数和获取操作数。
* - 指令生成:提供生成 RISC-V 指令的方法。
* - 打印功能:提供打印指令信息的方法。
*
* @section 作用
* RISCVMIR 类在后端编译过程中起到了关键作用,主要用于:
* - 构建和操作 RISC-V 架构的中间表示。
* - 管理指令的操作数和生成指令。
* - 为后续的优化和代码生成阶段提供基础设施。
*
* @section 使用该文件的其他文件
* - BuildInFunctionTransform.cpp:用于内置函数转换。
* - RISCVISel.cpp:用于指令选择。
* - PhiElimination.cpp:用于 Phi 指令消除。
* - LegalizePass.cpp:用于指令合法化。
* - DeleteDeadBlock.cpp:用于删除死基本块。
* - BackendDCE.cpp:用于死代码消除。
* - RegAlloc.cpp:用于寄存器分配。
* - PostRACalleeSavedLegalizer.cpp:用于调用者保存寄存器合法化。
* - CodeLayout.cpp:用于代码布局。
* - RISCVAsmPrinter.cpp:用于汇编生成。
*/

#include "../include/backend/RISCVMIR.hpp"


Expand Down
6 changes: 6 additions & 0 deletions backend/RISCVMOperand.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @file RISCVMOperand.cpp
* @brief RISC-V 操作数类的实现文件
* @details 本文件实现了 RISCVMOperand 类及其子类 Imm 的方法,用于管理和操作 RISC-V 架构中的各种操作数。
* 包括常量操作数的创建、获取和打印功能。
*/
#include "../include/backend/RISCVMOperand.hpp"
#include "../include/backend/RISCVFrameContext.hpp"
#include "../include/backend/RISCVMIR.hpp"
Expand Down
6 changes: 6 additions & 0 deletions backend/RISCVRegister.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @file RISCVRegister.cpp
* @brief RISC-V 寄存器类的实现文件
* @details 本文件实现了 RISC-V 架构中的物理寄存器、虚拟寄存器、加载地址寄存器以及寄存器列表的相关方法。
* 包括寄存器的创建、获取、设置和打印功能。
*/
#include "../include/backend/RISCVRegister.hpp"
#include "../include/lib/MagicEnum.hpp"
#include <sstream>
Expand Down
5 changes: 5 additions & 0 deletions backend/RISCVTrival.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @file RISCVTrival.cpp
* @brief RISC-V 简单指令生成类的实现文件
* @details 本文件实现了 RISC-V 架构中简单指令的生成方法,包括从源操作数复制到目标虚拟寄存器的指令生成。
*/
#include "../include/backend/RISCVTrival.hpp"

RISCVMIR* RISCVTrival::CopyFrom(VirRegister* dst,RISCVMOperand* src){
Expand Down
10 changes: 10 additions & 0 deletions backend/RegAlloc.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* @file RegAllocImpl.cpp
* @brief 实现了寄存器分配的图着色算法。
*
* 本文件包含了 `RegAllocImpl` 类的实现,该类负责执行寄存器分配的图着色算法。
* 主要功能包括:
* - 初始化图着色对象
* - 运行图着色算法
* - 释放图着色对象
*/
#include "../include/backend/RegAlloc.hpp"
#include "../include/backend/LegalizePass.hpp"
void spill_reg(Operand vreg);
Expand Down
Loading