From 3a9cb54976c0d5091bee0c9eb8a7cdc034defe3c Mon Sep 17 00:00:00 2001
From: PurePasserby <1045060941@qq.com>
Date: Sun, 9 Jun 2024 21:14:22 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=86unsigned=20short=E6=8D=A2=E6=88=90size?=
 =?UTF-8?q?=5Ft=E9=81=BF=E5=85=8D=E6=95=B0=E7=BB=84=E7=B4=A2=E5=BC=95?=
 =?UTF-8?q?=E6=BA=A2=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 frontend/gen_ir.cpp                   |  2 +-
 frontend/include/MyVisitor.h          |  4 +--
 frontend/include/SymbolTable.h        | 12 +++----
 frontend/src/ExpVisitor.cpp           |  2 ++
 frontend/src/IRStatement.cpp          |  8 ++---
 frontend/src/MyVisitor.cpp            | 25 +++++++------
 frontend/src/PrivateVisitor.cpp       |  4 +--
 frontend/src/SymbolTable.cpp          |  4 +--
 test_cases/passed_cases/73_int_io.in  | 14 ++++++++
 test_cases/passed_cases/73_int_io.out |  6 ++++
 test_cases/passed_cases/73_int_io.sy  | 52 +++++++++++++++++++++++++++
 11 files changed, 106 insertions(+), 27 deletions(-)
 create mode 100755 test_cases/passed_cases/73_int_io.in
 create mode 100755 test_cases/passed_cases/73_int_io.out
 create mode 100755 test_cases/passed_cases/73_int_io.sy

diff --git a/frontend/gen_ir.cpp b/frontend/gen_ir.cpp
index 0b2a244..efef68b 100644
--- a/frontend/gen_ir.cpp
+++ b/frontend/gen_ir.cpp
@@ -31,7 +31,7 @@ int main(int argc, char* argv[]) {
     SCNUCC::SysYParser parser(&tokens);
     auto tree = parser.compUnit();
 
-    freopen("debug.txt","w",stderr);
+    // freopen("debug.txt","w",stderr);
 
     SCNUCC::MyVisitor visitor;
     visitor.visitCompUnit(tree);
diff --git a/frontend/include/MyVisitor.h b/frontend/include/MyVisitor.h
index bb60e2c..3f64828 100644
--- a/frontend/include/MyVisitor.h
+++ b/frontend/include/MyVisitor.h
@@ -243,7 +243,7 @@ private:
   BaseType string2CType(const std::string &type);
 
   Type buildType(BaseType type, SymKind kind = SymKind::VAR, bool is_const = false, bool is_global = false,
-                 std::vector<unsigned short> array_shape = {});
+                 std::vector<size_t> array_shape = {});
 
   std::shared_ptr<FuncSymbol> buildFuncSymbol(SysYParser::FuncDefContext *ctx);
 
@@ -320,7 +320,7 @@ private:
       IRPtr ir = std::any_cast<IRPtr>(values);
       int pos = 0;
       int stride = 1;
-      std::vector<unsigned short> shape = symbol->GetType().array_shape_;
+      std::vector<size_t> shape = symbol->GetType().array_shape_;
       for (int i = (int)indices.size() - 1; i >= 0; i--) {
         pos += indices[i] * stride;
         stride *= shape[i];
diff --git a/frontend/include/SymbolTable.h b/frontend/include/SymbolTable.h
index 3aca538..7a541a2 100644
--- a/frontend/include/SymbolTable.h
+++ b/frontend/include/SymbolTable.h
@@ -35,7 +35,7 @@ struct Type {
 
     bool is_global_ = false;
 
-    std::vector<unsigned short> array_shape_;
+    std::vector<size_t> array_shape_;
 
     Type() = default;
 
@@ -49,7 +49,7 @@ struct Type {
       SymKind k, 
       bool is_const, 
       bool is_global,
-      const std::vector<unsigned short>& shape
+      const std::vector<size_t>& shape
     ) 
     : base_type_(t), 
       kind_(k), 
@@ -170,7 +170,7 @@ public:
 
   auto IsFunc() const -> bool;
 
-  auto Dim() const -> unsigned short;
+  auto Dim() const -> size_t;
 
   auto IsGlobal() const -> const bool; //是否为全局变量
 
@@ -181,10 +181,10 @@ public:
    * 通过indices数组来找到这个位置的常量, 转换太费时间.
   */
   template<typename T>
-  auto GetArrayConst(const std::vector<unsigned short> &indices) -> T {
+  auto GetArrayConst(const std::vector<size_t> &indices) -> T {
     int pos = 0;
     int stride = 1;
-    std::vector<unsigned short> shape = type_.array_shape_;
+    std::vector<size_t> shape = type_.array_shape_;
     for (int i = (int)indices.size() - 1; i >= 0; i--) {
       pos += indices[i] * stride;
       stride *= shape[i];
@@ -297,7 +297,7 @@ public:
 
   void PrintAllParams();
 
-  auto ParamSize() const -> unsigned short;
+  auto ParamSize() const -> size_t;
 
   auto GetAllParams() const -> const std::vector<Type> &;
 
diff --git a/frontend/src/ExpVisitor.cpp b/frontend/src/ExpVisitor.cpp
index ab5b160..277ee81 100644
--- a/frontend/src/ExpVisitor.cpp
+++ b/frontend/src/ExpVisitor.cpp
@@ -171,6 +171,8 @@ std::any MyVisitor::visitLVal(SysYParser::LValContext *ctx) {
     for (size_t i = 0; i < ctx->exp().size(); i++) {  // 拿出索引
       auto exp_ir = std::any_cast<IRPtr>(visit(ctx->exp(i)));
       LoadVar(exp_ir);
+      ZeroExtend(exp_ir);
+      Float2Int(exp_ir);
       indices.push_back(exp_ir);
       IRPtr new_gep = std::make_shared<GEPIR>(type == BaseType::INT ? IRTYPE::IR_I32 : IRTYPE::IR_FLOAT, var_sym,
                                               indices);
diff --git a/frontend/src/IRStatement.cpp b/frontend/src/IRStatement.cpp
index 8780bce..2cdcfcb 100644
--- a/frontend/src/IRStatement.cpp
+++ b/frontend/src/IRStatement.cpp
@@ -396,7 +396,7 @@ auto GblVarDeclIR::ToSSA() const -> std::string {
   Type type = symbol_->GetType();
   if (symbol_->IsPtr()) {
     // 数组
-    std::vector<unsigned short> vt = type.array_shape_;
+    std::vector<size_t> vt = type.array_shape_;
     ssa << "@" << symbol_->GetString() << " = dso_local global ";
     // for (size_t i = 0; i < vt.size(); i++) {
     //   ssa << "[" << vt[i] << " x";
@@ -434,7 +434,7 @@ ArrayDeclIR::ArrayDeclIR(IRTYPE::IRTYPE type, std::shared_ptr<VarSymbol> symbol)
 auto ArrayDeclIR::ToSSA() const -> std::string {
   std::stringstream ssa;
   Type type = symbol_->GetType();
-  std::vector<unsigned short> vt = type.array_shape_;
+  std::vector<size_t> vt = type.array_shape_;
   ssa << GetName() << " = alloca ";
   for (size_t i = 0; i < vt.size(); i++) {
     ssa << "[" << vt[i] << " x ";
@@ -528,7 +528,7 @@ auto BitCastIR::ToSSA() const -> std::string {
   std::stringstream ssa;
   ssa << GetName() << " = bitcast ";
   Type type = symbol_->GetType();
-  std::vector<unsigned short> vt = type.array_shape_;
+  std::vector<size_t> vt = type.array_shape_;
   long long bytes = 4;
   for (size_t i = 0; i < vt.size(); i++) {
     ssa << "[" << vt[i] << " x ";
@@ -592,7 +592,7 @@ auto GEPIR::ToSSA() const -> std::string {
   std::stringstream ssa;
   ssa << GetName() << " = " << IR_OP_STR[op_] << " ";
   Type type = symbol_->GetType();
-  std::vector<unsigned short> shape = type.array_shape_;
+  std::vector<size_t> shape = type.array_shape_;
   // 这一位不是0,那么就是本地数组或者直接调用全局数组
   if (shape[0] != 0) {
     for (size_t i = n - 1; i < shape.size(); i++) {
diff --git a/frontend/src/MyVisitor.cpp b/frontend/src/MyVisitor.cpp
index 8d3100d..281d8e2 100644
--- a/frontend/src/MyVisitor.cpp
+++ b/frontend/src/MyVisitor.cpp
@@ -143,7 +143,7 @@ std::any MyVisitor::visitConstDef(SysYParser::ConstDefContext *ctx) {
       size_t num = ctx->constExp().size();
       for (size_t i = 0; i < num; i++) {
         auto shape_ir = std::any_cast<IRPtr>(visit(ctx->constExp(i)));
-        unsigned short shape = (unsigned short)std::any_cast<int>(shape_ir->GetConstantVal());
+        size_t shape = (size_t)std::any_cast<int>(shape_ir->GetConstantVal());
         type.array_shape_.emplace_back(shape);
       }
       auto var_symbol = std::make_shared<VarSymbol>(type, var_name);
@@ -196,7 +196,7 @@ std::any MyVisitor::visitConstDef(SysYParser::ConstDefContext *ctx) {
       size_t num = ctx->constExp().size();
       for (size_t i = 0; i < num; i++) {
         auto shape_ir = std::any_cast<IRPtr>(visit(ctx->constExp(i)));
-        unsigned short shape = (unsigned short)std::any_cast<int>(shape_ir->GetConstantVal());
+        size_t shape = (size_t)std::any_cast<int>(shape_ir->GetConstantVal());
         type.array_shape_.emplace_back(shape);
       }
       auto var_symbol = std::make_shared<VarSymbol>(type, var_name);
@@ -292,7 +292,7 @@ std::any MyVisitor::visitVarDef(SysYParser::VarDefContext *ctx) {
       size_t num = ctx->constExp().size();
       for (size_t i = 0; i < num; i++) {
         auto shape_ir = std::any_cast<IRPtr>(visit(ctx->constExp(i)));
-        unsigned short shape = (unsigned short)std::any_cast<int>(shape_ir->GetConstantVal());
+        size_t shape = (size_t)std::any_cast<int>(shape_ir->GetConstantVal());
         type.array_shape_.emplace_back(shape);
       }
       auto var_symbol = std::make_shared<VarSymbol>(type, var_name);
@@ -322,7 +322,7 @@ std::any MyVisitor::visitVarDef(SysYParser::VarDefContext *ctx) {
       } else {
         std::ostringstream suffix;
         // std::string suffix = "zeroinitializer";
-        std::vector<unsigned short> vt = type.array_shape_;
+        std::vector<size_t> vt = type.array_shape_;
         for (size_t i = 0; i < vt.size(); i++) {
           suffix << "[" << vt[i] << " x";
         }
@@ -365,7 +365,7 @@ std::any MyVisitor::visitVarDef(SysYParser::VarDefContext *ctx) {
       size_t num = ctx->constExp().size();
       for (size_t i = 0; i < num; i++) {
         auto shape_ir = std::any_cast<IRPtr>(visit(ctx->constExp(i)));
-        unsigned short shape = (unsigned short)std::any_cast<int>(shape_ir->GetConstantVal());
+        size_t shape = (size_t)std::any_cast<int>(shape_ir->GetConstantVal());
         type.array_shape_.emplace_back(shape);
       }
       auto var_symbol = std::make_shared<VarSymbol>(type, var_name);
@@ -526,7 +526,7 @@ std::any MyVisitor::visitFuncFParam(SysYParser::FuncFParamContext *ctx) {
     type.array_shape_.push_back(0);
     for (auto exp: ctx->exp()) {
       auto ir = std::any_cast<IRPtr>(visit(exp));
-      unsigned short num = std::any_cast<int>(ir->GetConstantVal());
+      size_t num = std::any_cast<int>(ir->GetConstantVal());
       type.array_shape_.push_back(num);
     }
   }
@@ -544,6 +544,11 @@ std::any MyVisitor::visitBlock(SysYParser::BlockContext* ctx) {
     auto cfg = cfgs_.back();  // 这个cfg是在进入block之前创建的
 
     for (auto item : ctx->blockItem()) {
+        // 如果已经return 那么就不递归visit了
+        if (return_flag_ != BLOCK::NO_RETURN){
+          break;
+        }
+
         // child可能是decl或者是stmt
         if (auto decl = item->decl()) {
             visit(decl);
@@ -905,7 +910,7 @@ void MyVisitor::SyLibInit() {
             "putarray",
             std::vector<Type>{
                 Type(BaseType::INT, SymKind::VAR, false, false),
-                Type(BaseType::INT, SymKind::VAR, false, false,std::vector<unsigned short>{0})
+                Type(BaseType::INT, SymKind::VAR, false, false,std::vector<size_t>{0})
             }
         )
     );
@@ -915,7 +920,7 @@ void MyVisitor::SyLibInit() {
         std::make_shared<FuncSymbol>(
             Type(BaseType::INT, SymKind::FUNC, true, true),
             "getarray",
-            std::vector<Type>{Type(BaseType::INT, SymKind::VAR, false, false,std::vector<unsigned short>{0})}
+            std::vector<Type>{Type(BaseType::INT, SymKind::VAR, false, false,std::vector<size_t>{0})}
         )
     );
 
@@ -926,7 +931,7 @@ void MyVisitor::SyLibInit() {
             "putfarray",
             std::vector<Type>{
                 Type(BaseType::INT, SymKind::VAR, false, false),
-                Type(BaseType::FLOAT, SymKind::VAR, false, false,std::vector<unsigned short>{0})
+                Type(BaseType::FLOAT, SymKind::VAR, false, false,std::vector<size_t>{0})
             }
         )
     );
@@ -936,7 +941,7 @@ void MyVisitor::SyLibInit() {
         std::make_shared<FuncSymbol>(
             Type(BaseType::INT, SymKind::FUNC, true, true),
             "getfarray",
-            std::vector<Type>{Type(BaseType::FLOAT, SymKind::VAR, false, false,std::vector<unsigned short>{0})}
+            std::vector<Type>{Type(BaseType::FLOAT, SymKind::VAR, false, false,std::vector<size_t>{0})}
         )
     );
 
diff --git a/frontend/src/PrivateVisitor.cpp b/frontend/src/PrivateVisitor.cpp
index a5a41a1..159d4f5 100644
--- a/frontend/src/PrivateVisitor.cpp
+++ b/frontend/src/PrivateVisitor.cpp
@@ -263,7 +263,7 @@ auto MyVisitor::string2CType(const std::string &type) -> BaseType {
 }
 
 auto MyVisitor::buildType(BaseType type, SymKind kind, bool is_const, bool is_global,
-                          std::vector<unsigned short> array_shape) -> Type {
+                          std::vector<size_t> array_shape) -> Type {
   Type t;
   t.kind_ = kind;
   t.base_type_ = type;
@@ -642,7 +642,7 @@ auto MyVisitor::GlobalInitArray(const std::shared_ptr<VarSymbol> &symbol,
     
     
     auto vt = std::any_cast<std::vector<std::any>>(values);
-    std::vector<unsigned short> vt_ = type.array_shape_;
+    std::vector<size_t> vt_ = type.array_shape_;
     size_t num = vt_[cur];
 
     for (size_t i = cur; i < vt_.size(); i++) {
diff --git a/frontend/src/SymbolTable.cpp b/frontend/src/SymbolTable.cpp
index da6afed..2857ab6 100644
--- a/frontend/src/SymbolTable.cpp
+++ b/frontend/src/SymbolTable.cpp
@@ -179,7 +179,7 @@ auto Symbol::GetString() const -> const std::string {
 }
 
 /* VarSymbol 类实现 */
-auto VarSymbol::Dim() const -> unsigned short {
+auto VarSymbol::Dim() const -> size_t {
   // 返回符号的维度信息
   return type_.array_shape_.size();
 }
@@ -201,7 +201,7 @@ auto VarSymbol::IsFunc() const -> bool {
 }
 
 /* FuncSymbol 类实现 */
-auto FuncSymbol::ParamSize() const -> unsigned short {
+auto FuncSymbol::ParamSize() const -> size_t {
   return func_params_.size();
 }
 
diff --git a/test_cases/passed_cases/73_int_io.in b/test_cases/passed_cases/73_int_io.in
new file mode 100755
index 0000000..98c32c2
--- /dev/null
+++ b/test_cases/passed_cases/73_int_io.in
@@ -0,0 +1,14 @@
+	
+	 
+5
+  4006571
+
+ 9900	
+	 	1504379
+
+
+758219   	 	  
+ 99336677
+
+
+	
\ No newline at end of file
diff --git a/test_cases/passed_cases/73_int_io.out b/test_cases/passed_cases/73_int_io.out
new file mode 100755
index 0000000..651a1bc
--- /dev/null
+++ b/test_cases/passed_cases/73_int_io.out
@@ -0,0 +1,6 @@
+4006571
+9900
+1504379
+758219
+99336677
+0
diff --git a/test_cases/passed_cases/73_int_io.sy b/test_cases/passed_cases/73_int_io.sy
new file mode 100755
index 0000000..4f1b992
--- /dev/null
+++ b/test_cases/passed_cases/73_int_io.sy
@@ -0,0 +1,52 @@
+const int ascii_0 = 48;
+
+int my_getint()
+{
+    int sum = 0, c;
+
+    while (1) {
+        c = getch() - ascii_0;
+        if (c < 0 || c > 9) {
+            continue;
+        } else {
+            break;
+        }
+    }
+    sum = c;
+
+    while (1) {
+        c = getch() - ascii_0;
+        if (c >= 0 && c <= 9) {
+            sum = sum * 10 + c;
+        } else {
+            break;
+        }
+    }
+
+    return sum;
+}
+
+void my_putint(int a)
+{
+    int b[16], i = 0;
+    while (a > 0) {
+        b[i] = a % 10 + ascii_0;
+        a = a / 10;
+        i = i + 1;
+    }
+    while (i > 0) {
+        i = i - 1;
+        putch(b[i]);
+    }
+}
+
+int main()
+{
+    int n = my_getint();
+    while (n > 0) {
+        int m = my_getint();
+        my_putint(m); putch(10);
+        n = n - 1;
+    }
+    return 0;
+}
-- 
GitLab