diff --git a/AST/ast.h b/AST/ast.h index 748f1da8ad693b83914bfebd85262f542a29fb0e..fcb1e38ee104e1a60b1cc7cf7160f4a22708f051 100644 --- a/AST/ast.h +++ b/AST/ast.h @@ -371,7 +371,7 @@ public: virtual void visit(EQExpAST& ast) = 0; virtual void visit(LANDExpAST& ast) = 0; virtual void visit(LORExpAST& ast) = 0; - virtual void visit(ArraysAST& ast) = 0; + }; #endif diff --git a/IR/genIR.cpp b/IR/genIR.cpp index 781654d16079496dd6eb3c1331b656572caa01bb..1e3986b738279921b0ec616c7985351d95d6a42e 100644 --- a/IR/genIR.cpp +++ b/IR/genIR.cpp @@ -74,17 +74,6 @@ void GenIR::visit(DECLAST &ast) { } -void GenIR::visit(ArraysAST &ast) { - // é历数组的声明或åˆå§‹åŒ– - curType = ast.arrayType == TYPE_INT ? INT32_T : FLOAT_T; - isConst = ast.isConst; - - for (const auto &def : ast.list) { - def->ACcept(*this); - } -} - - void GenIR::visit(DEFAST &ast) { string varName = *ast.id; if (scope.in_global()) { @@ -119,12 +108,8 @@ void GenIR::visit(DEFAST &ast) { } else { // 1. è§£æžç»´åº¦ vector<int> dimensions; - if (dimensions.size()>19997) - return; useConst = true; for (auto &exp : ast.arrays) { - if (dimensions.size()>19997) - return; exp->ACcept(*this); int dimension = dynamic_cast<ConstantInt *>(recentVal)->value_; dimensions.push_back(dimension); @@ -134,8 +119,6 @@ void GenIR::visit(DEFAST &ast) { vector<ArrayType *> arrayTys( dimensions.size()); for (int i = dimensions.size() - 1; i >= 0; i--) { - if (dimensions.size()>19997) - return; if (i == dimensions.size() - 1) arrayTys[i] = module->get_array_type(curType, dimensions[i]); else @@ -191,33 +174,22 @@ void GenIR::visit(DEFAST &ast) { } else { // å¤„ç†æ•°ç»„类型的å˜é‡ vector<int> dimensions(ast.arrays.size()), dimensionsCnt((ast.arrays.size())); - if (dimensions.size()>19997) - return; + int totalByte = 1; useConst = true; for (int i = dimensions.size() - 1; i >= 0; i--) { - if (dimensions.size() >19997) { - return; - } ast.arrays[i]->ACcept(*this);// å¤„ç†æ¯ä¸ªç»´åº¦çš„å¤§å° int dimension = dynamic_cast<ConstantInt *>(recentVal)->value_; // 获å–ç»´åº¦çš„å¤§å° totalByte *= dimension; // 更新总å—节数 dimensions[i] = dimension; dimensionsCnt[i] = totalByte; - //1.判æ–第一个数组是å¦ä¸ºäºŒç»´æ•°ç»„,如果是就把该数组转æ¢ä¸ºå—ç¬¦ä¸²å‚¨å˜ - //2.è¯¥æ•°ç»„å¯¹åº”çš„å€¼ä¹Ÿå‚¨å˜ - //3.判æ–åŽé¢çš„æ•°ç»„,如果是二维数组,就也转æ¢ä¸ºå—符串储å˜å¹¶ä¸Žç¬¬ä¸€ä¸ªè¿›è¡Œå¯¹æ¯” - //4.若对比结果相ç‰ï¼Œç›´æŽ¥è¿”回第一个数组的值 + } - - totalByte *= 4; useConst = false; - // 创建数组类型 ArrayType *arrayTy = nullptr; for (int i = dimensions.size() - 1; i >= 0; i--) { - if (dimensions.size() >19997) - return; + if (i == dimensions.size() - 1) { @@ -234,7 +206,6 @@ void GenIR::visit(DEFAST &ast) { cout << "no initVal when define const!" << endl; return; } - // æ¸…é›¶æ•°ç»„å†…å˜ Value *i32P = builder->create_bitcast(arrayAlloc, INT32PTR_T); auto memclr = scope.find("memclr"); @@ -245,16 +216,14 @@ void GenIR::visit(DEFAST &ast) { // åˆå§‹åŒ–æ•°ç»„å…ƒç´ vector<Value *> idxs(dimensions.size() + 1); for (int i = 0; i < dimensions.size() + 1; i++) { - if (dimensions.size() >19997) { - return; - } + idxs[i] = CONST_INT(0); } Value *ptr = builder->create_gep(arrayAlloc, idxs); localInit(ptr, ast.initVal->initValList, dimensionsCnt, 1); } -} +} int GenIR::getNextDim(vector<int> &elementsCnts, int up) { @@ -443,8 +412,7 @@ void GenIR::visit(FUNCFParamAST &ast) { if (ast.isArray) { useConst = true; for (int i = ast.arrays.size() - 1; i >= 0; i--) { - if (ast.arrays.size()>19997) - return; + ast.arrays[i]->ACcept(*this); paramType = module->get_array_type(paramType, ((ConstantInt *)recentVal)->value_); diff --git a/IR/genIR.h b/IR/genIR.h index d7460ebdba5fd9b1517c68ef792b36b6826f9643..fbc362af10f098962cec0c749236e6ad963bf6af 100644 --- a/IR/genIR.h +++ b/IR/genIR.h @@ -33,7 +33,7 @@ public: void visit(EQExpAST &ast) override; void visit(LANDExpAST &ast) override; void visit(LORExpAST &ast) override; - void visit(ArraysAST& ast) override; + IRStmtBuilder *builder; Scope scope; diff --git a/README.md b/README.md index b0a1566080c0d6e4b8edc557e99f74b01a13a6f9..c0f7154eb1e14b19da1da493b70a9236a4afa29c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## 编译器Compiler +## 编译器Compiler å°†SysY2022è¯è¨€çš„代ç 翻译为Arm汇编代ç 。 @@ -13,25 +13,7 @@ + Visual Studio Code ### 构建 -`Clang`编译生æˆå¯æ‰§è¡Œæ–‡ä»¶ +`clang++`编译生æˆå¯æ‰§è¡Œæ–‡ä»¶ ### è¿è¡Œç¼–译器 -#### å‚考内容 -[1]Sysyè¯è¨€è§„范 https://syuanz.wiki/accipit/appendix/sysy-spec/ 浙江大å¦ç¼–译原ç†è¯¾ç¨‹å®žéªŒ - -[2]LLVM-CSE https://github.com/ivangogic/LLVM-CSE Ivan Gogic - -[3]北大编译实践在线文档 https://pku-minic.github.io/online-doc/#/preface/prerequisites - -[4]å…¬å…±è¡¨è¾¾å¼æ¶ˆé™¤ https://blog.csdn.net/qq_35985044/article/details/118761314 å°ä¼Ÿdb - -[5]循环优化 https://weedge.github.io/perf-book-cn/zh/chapters/9-Optimizing-Computations/9-3_Loop_Optimizations_cn.html weedge - -[6]详解三大编译器 https://blog.csdn.net/usstmiracle/article/details/134953871 aFakeProgramer - -[7]2022å¹´å‚赛作å“: -bddd - -[8]2023å¹´å‚赛作å“: -没有opå°±ä¸é…拿奖å—ã€åŒ—关大å¦ç¬¬83å·ä»£è¡¨é˜Ÿã€å¤§èåœç¼–译生产队 diff --git a/compiler b/compiler index 51415865c903d490ca4961b3303e040f8e64c04d..5a37d9e29be137303ac2f5353c120362548b43a4 100644 Binary files a/compiler and b/compiler differ