├── .gitattributes ├── CMakeLists.txt ├── LICENSE.txt ├── README.md ├── SysY语言定义.pdf ├── SysY运行时库.pdf ├── build.sh ├── readme_images ├── Compiler.png ├── Ideal_Logo_Davies.ico ├── MIT_license.svg ├── result00.png ├── result01.jpeg ├── result02.jpeg ├── result03.jpeg ├── result04.jpeg ├── result05.jpeg ├── result06.jpeg ├── result07.jpeg └── screenshot.png ├── src ├── back_end │ ├── arm_generator.cpp │ ├── arm_generator.h │ ├── optimizer.cpp │ └── optimizer.h ├── common │ ├── config.cpp │ ├── config.h │ ├── global.cpp │ ├── global.h │ ├── node.cpp │ ├── node.h │ ├── type.cpp │ └── type.h ├── compiler.cpp ├── front_end │ ├── intermediate_representation.cpp │ ├── intermediate_representation.h │ ├── parser.cpp │ ├── parser.h │ ├── semantic_analyzer.cpp │ ├── semantic_analyzer.h │ ├── tokenize.cpp │ └── tokenize.h └── tools │ ├── common.cpp │ └── common.h ├── test_sample ├── functional_test │ ├── 00_arr_defn2.out │ ├── 00_arr_defn2.sy │ ├── 00_main.out │ ├── 00_main.sy │ ├── 01_var_defn.out │ ├── 01_var_defn.sy │ ├── 02_arr_defn4.out │ ├── 02_arr_defn4.sy │ ├── 02_var_defn2.out │ ├── 02_var_defn2.sy │ ├── 03_arr_defn.out │ ├── 03_arr_defn.sy │ ├── 03_var_defn2.out │ ├── 03_var_defn2.sy │ ├── 04_const_defn.out │ ├── 04_const_defn.sy │ ├── 04_func_defn.out │ ├── 04_func_defn.sy │ ├── 05_add.out │ ├── 05_add.sy │ ├── 05_const_array_defn.out │ ├── 05_const_array_defn.sy │ ├── 06_mod.out │ ├── 06_mod.sy │ ├── 06_var_defn_func.out │ ├── 06_var_defn_func.sy │ ├── 07_comment1.out │ ├── 07_comment1.sy │ ├── 07_return_var.out │ ├── 07_return_var.sy │ ├── 08_arr_assign.out │ ├── 08_arr_assign.sy │ ├── 08_comment2.out │ ├── 08_comment2.sy │ ├── 09_if.out │ ├── 09_if.sy │ ├── 09_void_func.out │ ├── 09_void_func.sy │ ├── 10_break.out │ ├── 10_break.sy │ ├── 10_if_else.out │ ├── 10_if_else.sy │ ├── 11_continue.out │ ├── 11_continue.sy │ ├── 11_while.out │ ├── 11_while.sy │ ├── 12_array_traverse.out │ ├── 12_array_traverse.sy │ ├── 12_getint.in │ ├── 12_getint.out │ ├── 12_getint.sy │ ├── 13_and.in │ ├── 13_and.out │ ├── 13_and.sy │ ├── 13_array_traverse2.out │ ├── 13_array_traverse2.sy │ ├── 14_or.in │ ├── 14_or.out │ ├── 14_or.sy │ ├── 15_array_test3.out │ ├── 15_array_test3.sy │ ├── 15_equal.in │ ├── 15_equal.out │ ├── 15_equal.sy │ ├── 16_greater_eq.out │ ├── 16_greater_eq.sy │ ├── 16_nequal.in │ ├── 16_nequal.out │ ├── 16_nequal.sy │ ├── 17_less.in │ ├── 17_less.out │ ├── 17_less.sy │ ├── 17_less_eq.out │ ├── 17_less_eq.sy │ ├── 18_cal_prio.in │ ├── 18_cal_prio.out │ ├── 18_cal_prio.sy │ ├── 19_neg_expr.in │ ├── 19_neg_expr.out │ ├── 19_neg_expr.sy │ ├── 20_arr_sum.in │ ├── 20_arr_sum.out │ ├── 20_arr_sum.sy │ ├── 21_suminput.in │ ├── 21_suminput.out │ ├── 21_suminput.sy │ ├── 22_if_test1.out │ ├── 22_if_test1.sy │ ├── 23_if_test2.out │ ├── 23_if_test2.sy │ ├── 24_if_test3.out │ ├── 24_if_test3.sy │ ├── 25_if_test4.out │ ├── 25_if_test4.sy │ ├── 26_if_test5.out │ ├── 26_if_test5.sy │ ├── 27_while_test1.out │ ├── 27_while_test1.sy │ ├── 28_while_test2.out │ ├── 28_while_test2.sy │ ├── 29_while_test3.out │ ├── 29_while_test3.sy │ ├── 30_while_if_test1.out │ ├── 30_while_if_test1.sy │ ├── 31_while_if_test2.out │ ├── 31_while_if_test2.sy │ ├── 32_while_if_test3.out │ ├── 32_while_if_test3.sy │ ├── 33_func_test1.out │ ├── 33_func_test1.sy │ ├── 34_func_test2.out │ ├── 34_func_test2.sy │ ├── 35_array_test.out │ ├── 35_array_test.sy │ ├── 36_domain_test.out │ ├── 36_domain_test.sy │ ├── 38_if_complex_expr.out │ ├── 38_if_complex_expr.sy │ ├── 39_assign_complex_expr.out │ ├── 39_assign_complex_expr.sy │ ├── 40_index_complex_expr.out │ ├── 40_index_complex_expr.sy │ ├── 41_index_arithmetic_expr.out │ ├── 41_index_arithmetic_expr.sy │ ├── 42_index_func_ret.out │ ├── 42_index_func_ret.sy │ ├── 43_time_prior_plus.out │ ├── 43_time_prior_plus.sy │ ├── 44_add_prior_equal.out │ ├── 44_add_prior_equal.sy │ ├── 45_equal_prior_logic.out │ ├── 45_equal_prior_logic.sy │ ├── 46_and_prior_or.out │ ├── 46_and_prior_or.sy │ ├── 47_minus_with_sub.out │ ├── 47_minus_with_sub.sy │ ├── 49_decl_in_defn.out │ ├── 49_decl_in_defn.sy │ ├── 50_recursion_test1.out │ ├── 50_recursion_test1.sy │ ├── 51_recursion_test2.out │ ├── 51_recursion_test2.sy │ ├── 52_recursion_test3.out │ ├── 52_recursion_test3.sy │ ├── 53_sort_test1.out │ ├── 53_sort_test1.sy │ ├── 54_sort_test2.out │ ├── 54_sort_test2.sy │ ├── 55_sort_test3.out │ ├── 55_sort_test3.sy │ ├── 56_sort_test4.out │ ├── 56_sort_test4.sy │ ├── 57_sort_test5.out │ ├── 57_sort_test5.sy │ ├── 58_sort_test6.out │ ├── 58_sort_test6.sy │ ├── 59_sort_test7.out │ ├── 59_sort_test7.sy │ ├── 60_while_fibonacci.in │ ├── 60_while_fibonacci.out │ ├── 60_while_fibonacci.sy │ ├── 61_rec_fibonacci.in │ ├── 61_rec_fibonacci.out │ ├── 61_rec_fibonacci.sy │ ├── 62_long_code.out │ ├── 62_long_code.sy │ ├── 63_simple_atoi.in │ ├── 63_simple_atoi.out │ ├── 63_simple_atoi.sy │ ├── 64_alpha_count.in │ ├── 64_alpha_count.out │ ├── 64_alpha_count.sy │ ├── 65_word_count.in │ ├── 65_word_count.out │ ├── 65_word_count.sy │ ├── 66_go_upstairs.out │ ├── 66_go_upstairs.sy │ ├── 67_remove_duplicate_element.out │ ├── 67_remove_duplicate_element.sy │ ├── 68_accumulate.out │ ├── 68_accumulate.sy │ ├── 69_last_word_length.out │ ├── 69_last_word_length.sy │ ├── 70_multi.out │ ├── 70_multi.sy │ ├── 71_max_subsequence_sum.out │ ├── 71_max_subsequence_sum.sy │ ├── 72_enum.out │ ├── 72_enum.sy │ ├── 73_exchange_value.in │ ├── 73_exchange_value.out │ ├── 73_exchange_value.sy │ ├── 74_itera_sqrt.out │ ├── 74_itera_sqrt.sy │ ├── 75_max_container.out │ ├── 75_max_container.sy │ ├── 76_int_factor_sum.out │ ├── 76_int_factor_sum.sy │ ├── 77_unique_path.out │ ├── 77_unique_path.sy │ ├── 78_decbinoct.out │ ├── 78_decbinoct.sy │ ├── 79_gcd.in │ ├── 79_gcd.out │ ├── 79_gcd.sy │ ├── 80_lcm.in │ ├── 80_lcm.out │ ├── 80_lcm.sy │ ├── 81_jump_game.out │ ├── 81_jump_game.sy │ ├── 82_int_split.out │ ├── 82_int_split.sy │ ├── 83_enc_dec.out │ ├── 83_enc_dec.sy │ ├── 84_palindrome_number.out │ ├── 84_palindrome_number.sy │ ├── 86_bin_search.in │ ├── 86_bin_search.out │ ├── 86_bin_search.sy │ ├── 88_array_concat.out │ ├── 88_array_concat.sy │ ├── 90_insert_order.in │ ├── 90_insert_order.out │ ├── 90_insert_order.sy │ ├── 91_line_search.in │ ├── 91_line_search.out │ ├── 91_line_search.sy │ ├── 92_matrix_add.out │ ├── 92_matrix_add.sy │ ├── 93_matrix_sub.out │ ├── 93_matrix_sub.sy │ ├── 94_matrix_mul.out │ ├── 94_matrix_mul.sy │ ├── 95_matrix_tran.out │ ├── 95_matrix_tran.sy │ ├── 96_many_param_call.out │ ├── 96_many_param_call.sy │ ├── 97_many_global_var.out │ ├── 97_many_global_var.sy │ ├── 98_many_local_var.in │ ├── 98_many_local_var.out │ ├── 98_many_local_var.sy │ ├── 99_register_realloc.out │ └── 99_register_realloc.sy └── performance_test │ ├── 00_bitset1.in │ ├── 00_bitset1.out │ ├── 00_bitset1.sy │ ├── 00_bitset2.in │ ├── 00_bitset2.out │ ├── 00_bitset2.sy │ ├── 00_bitset3.in │ ├── 00_bitset3.out │ ├── 00_bitset3.sy │ ├── 01_mm1.in │ ├── 01_mm1.out │ ├── 01_mm1.sy │ ├── 01_mm2.in │ ├── 01_mm2.out │ ├── 01_mm2.sy │ ├── 01_mm3.in │ ├── 01_mm3.out │ ├── 01_mm3.sy │ ├── 02_mv1.in │ ├── 02_mv1.out │ ├── 02_mv1.sy │ ├── 02_mv2.in │ ├── 02_mv2.out │ ├── 02_mv2.sy │ ├── 02_mv3.in │ ├── 02_mv3.out │ ├── 02_mv3.sy │ ├── 03_sort1.in │ ├── 03_sort1.out │ ├── 03_sort1.sy │ ├── 03_sort2.in │ ├── 03_sort2.out │ ├── 03_sort2.sy │ ├── 03_sort3.in │ ├── 03_sort3.out │ ├── 03_sort3.sy │ ├── 04_spmv1.in │ ├── 04_spmv1.out │ ├── 04_spmv1.sy │ ├── 04_spmv2.in │ ├── 04_spmv2.out │ ├── 04_spmv2.sy │ ├── 04_spmv3.in │ ├── 04_spmv3.out │ └── 04_spmv3.sy └── 项目介绍.pdf /.gitattributes: -------------------------------------------------------------------------------- 1 | *.in filter=lfs diff=lfs merge=lfs -text 2 | *.pdf filter=lfs diff=lfs merge=lfs -text 3 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.6) 2 | 3 | # SET (CMAKE_C_COMPILER "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/clang") 4 | # SET (CMAKE_CXX_COMPILER "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/clang++") 5 | # SET (CMAKE_AR "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/llvm-ar") 6 | # SET (CMAKE_LINKER "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/llvm-ld") 7 | # SET (CMAKE_NM "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/llvm-nm") 8 | # SET (CMAKE_OBJDUMP "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/llvm-objdump") 9 | # SET (CMAKE_RANLIB "/g/sb/llvm/llvm-project-llvmorg-10.0.1/bin/llvm-ranlib") 10 | 11 | # set(CMAKE_CXX_STANDARD 17) 12 | # set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | # set(CMAKE_CXX_EXTENSIONS OFF) 14 | 15 | project (compiler) 16 | 17 | file(GLOB_RECURSE src_cpp RELATIVE ${CMAKE_SOURCE_DIR} 18 | "src/*.cpp" 19 | ) 20 | file(GLOB_RECURSE src_c RELATIVE ${CMAKE_SOURCE_DIR} 21 | "src/*.c" 22 | ) 23 | 24 | message(STATUS ${src}) 25 | 26 | add_executable( 27 | compiler 28 | ${src_cpp} 29 | ${src_c} 30 | ) 31 | 32 | set_property(TARGET compiler PROPERTY C_STANDARD 11) 33 | # set_property(TARGET compiler PROPERTY CXX_STANDARD 17) 34 | ADD_DEFINITIONS("-std=c++17") 35 | # ADD_DEFINITIONS("-O2") 36 | ADD_DEFINITIONS("-O0") 37 | 38 | target_link_libraries(compiler m) 39 | 40 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Davies 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![MIT License][license-shield]][license-url] 2 | 3 | 4 | 5 |
6 |

7 | 8 | Logo 9 | 10 | 11 |

Maked by Davies

12 | 13 |

14 | Email: 1182176003@qq.com 15 | 24 |

25 |

26 | 27 | 28 | 29 | 30 | ## Table of Contents 31 | 32 | * [About the Project](#about-the-project) 33 | * [Built With](#built-with) 34 | * [Getting Started](#getting-started) 35 | * [Prerequisites](#prerequisites) 36 | * [Installation](#installation) 37 | * [Usage](#usage) 38 | * [功能定制](#功能定制) 39 | * [Roadmap](#roadmap) 40 | * [Contributing](#contributing) 41 | * [License](#license) 42 | * [Contact](#contact) 43 | * [Acknowledgements](#acknowledgements) 44 | * [免责声明](#免责声明) 45 | 46 | 47 | 48 | ## About The Project 49 | 50 | **注意:如果仓库下载不了或者下载不全,请到release中去下载** 51 | 这个项目是我在没学过编译原理的情况下,看了几篇编译相关的文章,然后跟着感觉写的,里面有些编译流程可能不是标准的。编译过程中没有生成中间代码,直接从源代码构建语法树,然后根据语法树生成机器代码。这个项目是最终是用来参加一个编译优化比赛的,编写过程都是从简考虑,基本没有注释,但是变量名和函数名对功能的解释比较清楚。 52 | 如果您使用这个代码来学习编译原理,并在原代码的基础上做了优化改进或者添加了注释,可以考虑提交一个pull request整合到该仓库里面,这将极大的方便他人的学习,感谢您的付出。 53 | 54 | 该项目参考开源编译器9cc开发 [https://github.com/rui314/9cc](https://github.com/rui314/9cc) 55 | 56 | 57 | 运行结果:(其中有一个样例是RE,可能是由于我们函数合并的优化没有考虑数组传入的情况) 58 | 59 | [![Product Name Screen Shot][product-screenshot00]](https://github.com/DaviesGit) 60 | 61 | 编译器一共包括`词法分析`、`语法分析`、`语义分析`、`优化`、`目标代码`等6个部分。 62 | 63 | ![result01](readme_images/result01.jpeg) 64 | 65 | 语法树结构部分: 66 | 67 | ![result02](readme_images/result02.jpeg) 68 | 69 | 词法分析部分: 70 | 71 | ![result03](readme_images/result03.jpeg) 72 | 73 | 语法分析部分: 74 | 75 | ![result04](readme_images/result04.jpeg) 76 | 77 | 语义分析部分: 78 | 79 | ![result05](readme_images/result05.jpeg) 80 | 81 | 目标代码生成部分: 82 | 83 | ![result06](readme_images/result06.jpeg) 84 | 85 | 代码优化部分: 86 | 87 | ![result07](readme_images/result07.jpeg) 88 | 89 | 系统架构: 90 | 91 | ![Compiler](readme_images/Compiler.png) 92 | 93 | SysY 语言是 C 语言的一个子集。每个 SysY程序的源码存储在一个扩展名为 sy 的文件中。该文件中有且仅有一个名为 main的主函数定义,还可以包含若干全局变量声明、常量声明和其他函数定义。SysY语言支持 int 类型和元素为 int 类型且按行优先存储的多维数组类型,其中 int 型整数为 32 位有符号数;const 修饰符用于声明常量。 94 | 95 | 详细的SysY语言定义请参考`SysY语言定义.pdf`、`SysY运行时库.pdf`。 96 | 97 | 该编译器主要用来将SysY语言编译为arm汇编指令,并且对编译生成的代码进行优化。 98 | 99 | 功能: 100 | * 词法分析 101 | * 语法分析 102 | * 语义分析 103 | * 目标代码 104 | * 优化 105 | 106 | > 1. 对常量表达式进行化简求值 107 | > 2. 合并冗余的函数调用 108 | > 3. 高频访问的变量使用寄存器优化,减少内存访问。 109 | > 4. 合并arm代码中不必要的mov指令。 110 | > 5. 将函数内联进代码中,减少函数调用开销。 111 | > 6. 更多 112 | 113 | 114 | 115 | ### Built With 116 | 依赖 117 | * [CMake](https://cmake.org/) 118 | 119 | - [Make - GNU Project - Free Software Foundation - GNU.org](https://www.gnu.org/software/make/) 120 | 121 | - [GCC, the GNU Compiler Collection - GNU Project - Free ...](https://gcc.gnu.org/) 122 | 123 | 124 | 125 | 126 | 127 | ## Getting Started 128 | 129 | 这个章节将指导你简单的部署和使用该软件。 130 | 131 | ### Prerequisites 132 | 133 | 这个项目的依赖安装步骤在下面给出。 134 | * [CMake](https://cmake.org/) 135 | ```sh 136 | sudo apt-get install cmake 137 | ``` 138 | 139 | - [Make - GNU Project - Free Software Foundation - GNU.org](https://www.gnu.org/software/make/) 140 | 141 | ```sh 142 | sudo apt-get install make 143 | ``` 144 | 145 | - [GCC, the GNU Compiler Collection - GNU Project - Free ...](https://gcc.gnu.org/) 146 | 147 | ```sh 148 | sudo apt-get install gcc 149 | ``` 150 | 151 | 152 | 153 | ### Installation 154 | 155 | 1. 进入项目根目录 156 | ``` 157 | cd /path/to/your/project 158 | ``` 159 | 160 | 2. 在shell中运行一下代码进行编译 161 | 162 | ```sh 163 | mkdir build 164 | cd build 165 | rm CMake* cmake_install.cmake Makefile compiler -r; 166 | cmake ../ && make VERBOSE=1 167 | ``` 168 | 169 | 3. 在build文件夹中会生成一个compiler可执行文件。 170 | 171 | 172 | 173 | 174 | 175 | ## Usage 176 | 177 | 1. 编译sy文件不进行优化 178 | ```sh 179 | compiler ‘输入文件.sy’ -S -o ‘输出文件.s’ 180 | ``` 181 | 182 | 2. 编译sy文件进行O2优化 183 | 184 | ```sh 185 | compiler ‘输入文件.sy’ -S -o ‘输出文件.s’ -O2 186 | ``` 187 | 188 | 189 | ## 190 | 191 | ## 功能定制 192 | 193 | 如果需要功能定制,请联系作者 [1182176003@qq.com](1182176003@qq.com) 194 | 195 | 196 | 197 | 198 | 199 | ## Roadmap 200 | 201 | See the [open issues](https://example.com) for a list of proposed features (and known issues). 202 | 203 | 204 | 205 | 206 | ## Contributing 207 | 208 | Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. 209 | 210 | 1. Fork the Project 211 | 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) 212 | 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) 213 | 4. Push to the Branch (`git push origin feature/AmazingFeature`) 214 | 5. Open a Pull Request 215 | 216 | 217 | 218 | 219 | ## License 220 | 221 | Distributed under the MIT License. See `LICENSE` for more information. 222 | 223 | 224 | 225 | 226 | ## Contact 227 | 228 | Davies - [@qq](1182176003) - 1182176003 229 | 230 | Davies - [@email](1182176003@qq.com) - 1182176003@qq.com 231 | 232 | 233 | 234 | 235 | ## Acknowledgements 236 | * [GitHub](https://github.com/) 237 | * [GCC, the GNU Compiler Collection](https://gcc.gnu.org/) 238 | 239 | 240 | 241 | ## 免责声明 242 | * 该软件中所包含的部分内容,包括文字、图片、音频、视频、软件、代码、以及网页版式设计等可能来源于网上搜集。 243 | 244 | * 该软件提供的内容仅用于个人学习、研究或欣赏,不可使用于商业和其它意图,一切关于该软件的不正当使用行为均与我们无关,亦不承担任何法律责任。使用该软件应遵守相关法律的规定,通过使用该软件随之而来的风险与我们无关,若使用不当,后果均由个人承担。 245 | 246 | * 该软件不提供任何形式的保证。我们不保证内容的正确性与完整性。所有与使用该软件的直接风险均由用户承担。 247 | 248 | * 如果您认为该软件中所包含的部分内容侵犯了您的权益,请及时通知我们,我们将尽快予以修正或删除。 249 | 250 | 251 | 252 | 253 | 254 | [license-shield]: readme_images/MIT_license.svg 255 | [license-url]: https://opensource.org/licenses/MIT 256 | 257 | [product-screenshot]: readme_images/screenshot.png 258 | [product-screenshot00]: readme_images/result00.png 259 | -------------------------------------------------------------------------------- /SysY语言定义.pdf: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:c1d8ee3ae6148b8bfdea92e7c6588b10a6c69e943449a4c81c64f0164283b0b2 3 | size 373074 4 | -------------------------------------------------------------------------------- /SysY运行时库.pdf: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f9af338e8a4e7ad45810cdff4c21386c00cdd9d48d924481b2a167f146ad9899 3 | size 429420 4 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | cd build 2 | rm CMake* cmake_install.cmake Makefile compiler -r; 3 | cmake ../ && make VERBOSE=1 -------------------------------------------------------------------------------- /readme_images/Compiler.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/Compiler.png -------------------------------------------------------------------------------- /readme_images/Ideal_Logo_Davies.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/Ideal_Logo_Davies.ico -------------------------------------------------------------------------------- /readme_images/MIT_license.svg: -------------------------------------------------------------------------------- 1 | license: MITlicenseMIT -------------------------------------------------------------------------------- /readme_images/result00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result00.png -------------------------------------------------------------------------------- /readme_images/result01.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result01.jpeg -------------------------------------------------------------------------------- /readme_images/result02.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result02.jpeg -------------------------------------------------------------------------------- /readme_images/result03.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result03.jpeg -------------------------------------------------------------------------------- /readme_images/result04.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result04.jpeg -------------------------------------------------------------------------------- /readme_images/result05.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result05.jpeg -------------------------------------------------------------------------------- /readme_images/result06.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result06.jpeg -------------------------------------------------------------------------------- /readme_images/result07.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/result07.jpeg -------------------------------------------------------------------------------- /readme_images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaviesGit/SysY_compiler/be5fcb82e73f87b2116197c8e0c2613b61ecd09c/readme_images/screenshot.png -------------------------------------------------------------------------------- /src/back_end/arm_generator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * arm_generator.h 3 | * 4 | * Created on: Aug 1, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_BACK_END_ARM_GENERATOR_H_ 9 | #define SRC_BACK_END_ARM_GENERATOR_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | #include "../common/node.h" 15 | 16 | extern char const *last_arm_generate_error_msg; 17 | 18 | bool gen_arm_assembly_code(std::shared_ptr const &node, std::ostringstream &oss); 19 | 20 | #endif /* SRC_BACK_END_ARM_GENERATOR_H_ */ 21 | -------------------------------------------------------------------------------- /src/back_end/optimizer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * optimizer.cpp 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include "optimizer.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | char const *last_optimize_error_msg = nullptr; 17 | 18 | int optimize = 0; 19 | 20 | int get_optimize_level() { 21 | return optimize; 22 | } 23 | void set_optimize_level(int _optimize) { 24 | optimize = _optimize; 25 | } 26 | 27 | template 28 | bool calculation(std::vector> const ¶meters, std::shared_ptr const &parent, OPERATE operate, 29 | std::shared_ptr &literal) { 30 | std::shared_ptr operand0; 31 | std::shared_ptr operand1; 32 | if (!evaluate_literal_node(parameters[0], operand0)) 33 | return false; 34 | if (!evaluate_literal_node(parameters[1], operand1)) 35 | return false; 36 | literal = std::shared_ptr(new LiteralNode(parent, ND_LITERAL, calculation(operand0->getValue(), operand1->getValue(), operate))); 37 | return true; 38 | } 39 | 40 | bool evaluate_literal_node(std::shared_ptr const &node, std::shared_ptr &literal) { 41 | last_optimize_error_msg = nullptr; 42 | ND_TYPE type = node->getType(); 43 | switch (type) { 44 | case ND_PARENTHESIS: { 45 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 46 | if (!evaluate_literal_node(std::dynamic_pointer_cast(_node->getNode()), literal)) 47 | return false; 48 | } 49 | break; 50 | case ND_LITERAL: { 51 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 52 | literal = std::shared_ptr(new LiteralNode(_node)); 53 | } 54 | break; 55 | case ND_OPERATION: { 56 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 57 | OP_TYPE operator_type = _node->getOperatorType(); 58 | std::vector> const ¶meters = _node->getParameters(); 59 | std::shared_ptr const &parent = _node->getParent(); 60 | switch (operator_type) { 61 | case OP_UNARY_PLUS: 62 | if (!evaluate_literal_node(parameters[0], literal)) 63 | return false; 64 | literal->setParent(_node->getParent()); 65 | break; 66 | case OP_UNARY_MINUS: 67 | if (!evaluate_literal_node(parameters[0], literal)) 68 | return false; 69 | literal->setParent(_node->getParent()); 70 | literal->setValue(-literal->getValue()); 71 | break; 72 | case OP_LOGICAL_NOT: 73 | if (!evaluate_literal_node(parameters[0], literal)) 74 | return false; 75 | literal->setParent(_node->getParent()); 76 | literal->setValue(!literal->getValue()); 77 | break; 78 | case OP_MUL: 79 | if (!calculation(parameters, parent, OPERATE_MUL(), literal)) 80 | return false; 81 | break; 82 | case OP_DIV: 83 | if (!calculation(parameters, parent, OPERATE_DIV(), literal)) 84 | return false; 85 | break; 86 | case OP_MOD: 87 | if (!calculation(parameters, parent, OPERATE_MOD(), literal)) 88 | return false; 89 | break; 90 | case OP_ADD: 91 | if (!calculation(parameters, parent, OPERATE_ADD(), literal)) 92 | return false; 93 | break; 94 | case OP_SUB: 95 | if (!calculation(parameters, parent, OPERATE_SUB(), literal)) 96 | return false; 97 | break; 98 | case OP_GREATER: 99 | if (!calculation(parameters, parent, OPERATE_GREATER(), literal)) 100 | return false; 101 | break; 102 | case OP_LESS: 103 | if (!calculation(parameters, parent, OPERATE_LESS(), literal)) 104 | return false; 105 | break; 106 | case OP_GREATER_EQUAL: 107 | if (!calculation(parameters, parent, OPERATE_GREATER_EQUAL(), literal)) 108 | return false; 109 | break; 110 | case OP_LESS_EQUAL: 111 | if (!calculation(parameters, parent, OPERATE_LESS_EQUAL(), literal)) 112 | return false; 113 | break; 114 | case OP_EQUAL: 115 | if (!calculation(parameters, parent, OPERATE_EQUAL(), literal)) 116 | return false; 117 | break; 118 | case OP_NOT_EQUAL: 119 | if (!calculation(parameters, parent, OPERATE_NOT_EQUAL(), literal)) 120 | return false; 121 | break; 122 | case OP_LOGICAL_AND: 123 | if (!calculation(parameters, parent, OPERATE_LOGICAL_AND(), literal)) 124 | return false; 125 | break; 126 | case OP_LOGICAL_OR: 127 | if (!calculation(parameters, parent, OPERATE_LOGICAL_OR(), literal)) 128 | return false; 129 | break; 130 | case OP_ASSIGNMENT: 131 | last_optimize_error_msg = "Expression cannot contain assignment operation!"; 132 | return false; 133 | break; 134 | default: 135 | last_optimize_error_msg = "Unknown operation!"; 136 | return false; 137 | break; 138 | } 139 | } 140 | break; 141 | case ND_VAR: { 142 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 143 | std::shared_ptr const &target_var = _node->getTargetVar(); 144 | std::shared_ptr const &var_def = std::dynamic_pointer_cast(target_var->getParent()); 145 | if (!var_def->isIsConst()) { 146 | last_optimize_error_msg = "Target variable is not const in evaluate_literal_node!!!"; 147 | return false; 148 | } 149 | std::vector> const &var_type = var_def->getVarType(); 150 | int var_type_size = var_type.size(); 151 | int index = -1; 152 | while (++index < var_type_size && target_var != var_type[index]) 153 | ; 154 | if (index >= var_type_size) { 155 | last_optimize_error_msg = "target_var is not in it parent's var_type vector!!!"; 156 | return false; 157 | } 158 | std::shared_ptr const &_inti_value = var_def->getVarInitValue()[index]; 159 | if (ND_LITERAL != _inti_value->getType()) { 160 | last_optimize_error_msg = "Const variable init value is not literal!!!"; 161 | return false; 162 | } 163 | std::shared_ptr const &inti_value = std::dynamic_pointer_cast(_inti_value); 164 | literal = std::shared_ptr(new LiteralNode(inti_value)); 165 | } 166 | break; 167 | case ND_TYPE_: 168 | case ND_VAR_TYPE: 169 | case ND_DEF: 170 | case ND_VAR_DEF: 171 | case ND_FUN_DEF: 172 | case ND_REF: 173 | case ND_FUN: 174 | case ND_STMT: 175 | case ND_IF_ELSE: 176 | case ND_WHILE: 177 | case ND_CONTINUE: 178 | case ND_BREAK: 179 | case ND_RETURN: 180 | case ND_BLOCK: 181 | case ND_ARRAY: 182 | case ND_ARRAY_ACCESS: 183 | case ND_UNKNOWN: 184 | default: 185 | last_optimize_error_msg = "Not supported node type in evaluate_literal_node!"; 186 | // This is an error!!! 187 | return false; 188 | } 189 | return true; 190 | } 191 | 192 | template 193 | bool array_replace_node_function_call(std::vector> &nodes, std::vector &function_call_name, 194 | std::map> &function_call) { 195 | for (typename std::vector>::iterator it = nodes.begin(), end = nodes.end(); it != end; ++it) { 196 | if (!replace_node_function_call(*it, function_call_name, function_call)) 197 | return false; 198 | } 199 | return true; 200 | } 201 | 202 | // todo: 203 | bool is_volatile_function(char const *const function_name) { 204 | if (!strcmp("rand", function_name)) 205 | return true; 206 | for (int i = 0; i < SY_SYSTEM_FUNCTION_LEN; ++i) { 207 | char const *const system_fun = SY_SYSTEM_FUNCTION[i]; 208 | if (!strcmp(system_fun, function_name)) 209 | return true; 210 | } 211 | return false; 212 | } 213 | 214 | // todo : 215 | bool replace_node_function_call(std::shared_ptr &node, std::vector &function_call_name, 216 | std::map> &function_call) { 217 | ND_TYPE type = node->getType(); 218 | switch (type) { 219 | case ND_FUN: { 220 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 221 | if (is_volatile_function(_node->getName().c_str())) 222 | break; 223 | std::ostringstream oss; 224 | oss << "_____" << _node->getName(); 225 | 226 | std::vector> ¶meters = _node->getParameters(); 227 | for (std::vector>::iterator it = parameters.begin(), end = parameters.end(); it != end; ++it) { 228 | ND_TYPE type = (*it)->getType(); 229 | switch (type) { 230 | case ND_VAR: 231 | oss << "_" << std::dynamic_pointer_cast(*it)->getName(); 232 | break; 233 | case ND_ARRAY_ACCESS: { 234 | std::shared_ptr _node = std::dynamic_pointer_cast(*it); 235 | if (!array_replace_node_function_call(_node->getIndexes(), function_call_name, function_call)) 236 | return false; 237 | 238 | std::shared_ptr const &target = _node->getTarget(); 239 | if (ND_VAR == target->getType()) 240 | oss << "_" << std::dynamic_pointer_cast(target)->getName() << "[]"; 241 | } 242 | break; 243 | case ND_LITERAL: 244 | oss << "_" << std::dynamic_pointer_cast(*it)->getValue(); 245 | break; 246 | case ND_FUN: 247 | if (!replace_node_function_call(*it, function_call_name, function_call)) 248 | return false; 249 | oss << "_" << std::dynamic_pointer_cast(*it)->getName(); 250 | break; 251 | case ND_PARENTHESIS: { 252 | std::shared_ptr _node = std::dynamic_pointer_cast(*it); 253 | if (!replace_node_function_call(_node->getNode(), function_call_name, function_call)) 254 | return false; 255 | } 256 | break; 257 | case ND_OPERATION: { 258 | std::shared_ptr _node = std::dynamic_pointer_cast(*it); 259 | if (!array_replace_node_function_call(_node->getParameters(), function_call_name, function_call)) 260 | return false; 261 | oss << "_operation_type" << _node->getOperatorType(); 262 | } 263 | break; 264 | default: 265 | oss << "_type" << type; 266 | break; 267 | } 268 | } 269 | std::string name = oss.str(); 270 | if (function_call.find(name) == function_call.end()) { 271 | function_call_name.push_back(name); 272 | function_call.insert(std::pair >(name, _node)); 273 | } 274 | std::shared_ptr var_node(new VarNode(node, ND_VAR, name.c_str())); 275 | node = std::dynamic_pointer_cast(var_node); 276 | } 277 | break; 278 | case ND_STMT: 279 | case ND_PARENTHESIS: { 280 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 281 | if (!replace_node_function_call(_node->getNode(), function_call_name, function_call)) 282 | return false; 283 | } 284 | break; 285 | case ND_ARRAY_ACCESS: { 286 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 287 | if (!array_replace_node_function_call(_node->getIndexes(), function_call_name, function_call)) 288 | return false; 289 | } 290 | break; 291 | case ND_OPERATION: { 292 | std::shared_ptr _node = std::dynamic_pointer_cast(node); 293 | if (!array_replace_node_function_call(_node->getParameters(), function_call_name, function_call)) 294 | return false; 295 | } 296 | break; 297 | case ND_VAR: 298 | case ND_LITERAL: 299 | case ND_ARRAY: 300 | break; 301 | case ND_TYPE_: 302 | case ND_VAR_TYPE: 303 | case ND_VAR_DEF: 304 | case ND_FUN_DEF: 305 | case ND_IF_ELSE: 306 | case ND_WHILE: 307 | case ND_CONTINUE: 308 | case ND_BREAK: 309 | case ND_RETURN: 310 | case ND_BLOCK: 311 | case ND_DEF: 312 | case ND_REF: 313 | case ND_UNKNOWN: 314 | default: 315 | last_optimize_error_msg = "Not supported node type while replace_node_function_call!!"; 316 | return false; 317 | } 318 | return true; 319 | } 320 | 321 | bool merge_block_function_call(std::shared_ptr &block) { 322 | std::vector> &children = block->getChildren(); 323 | std::map> function_call; 324 | std::vector function_call_name; 325 | int index = 0; 326 | for (int index = 0, end = children.size(); index < end; ++index) { 327 | std::shared_ptr &node = children[index]; 328 | if (ND_STMT == node->getType()) { 329 | std::shared_ptr stmt = std::dynamic_pointer_cast(node); 330 | std::shared_ptr &expression = stmt->getStmt(); 331 | if (!replace_node_function_call(expression, function_call_name, function_call)) 332 | return false; 333 | 334 | for (std::vector::iterator _it = function_call_name.begin(), _end = function_call_name.end(); _it != _end; ++_it) { 335 | std::string &first = *_it; 336 | std::shared_ptr &second = function_call[*_it]; 337 | if (nullptr == second) 338 | continue; 339 | std::shared_ptr var_type_node(new VarTypeNode(nullptr, ND_VAR_TYPE, first.c_str())); 340 | std::shared_ptr _type(new TypeNode(nullptr, ND_TYPE_, VA_INT)); 341 | std::shared_ptr var_def_node(new VarDefNode(std::dynamic_pointer_cast(block), ND_VAR_DEF, "", _type, false)); 342 | var_def_node->add_var(var_type_node, second); 343 | second->setParent(std::dynamic_pointer_cast(var_def_node)); 344 | var_type_node->setParent(std::dynamic_pointer_cast(var_def_node)); 345 | _type->setParent(std::dynamic_pointer_cast(var_def_node)); 346 | // std::shared_ptr stmt_node(new StmtNode(std::dynamic_pointer_cast(block), ND_STMT, var_def_node)); 347 | // var_def_node->setParent(std::dynamic_pointer_cast(stmt_node)); 348 | children.insert(children.begin() + index, std::dynamic_pointer_cast(var_def_node)); 349 | ++index; 350 | ++end; 351 | second = nullptr; 352 | } 353 | // for (std::map>::iterator _it = function_call.begin(), _end = function_call.end(); _it != _end; ++_it) { 354 | // if (nullptr == _it->second) 355 | // continue; 356 | // std::shared_ptr var_type_node(new VarTypeNode(nullptr, ND_VAR_TYPE, _it->first.c_str())); 357 | // std::shared_ptr _type(new TypeNode(nullptr, ND_TYPE_, VA_INT)); 358 | // std::shared_ptr var_def_node(new VarDefNode(std::dynamic_pointer_cast(block), ND_VAR_DEF, "", _type, false)); 359 | // var_def_node->add_var(var_type_node, _it->second); 360 | // _it->second->setParent(std::dynamic_pointer_cast(var_def_node)); 361 | // var_type_node->setParent(std::dynamic_pointer_cast(var_def_node)); 362 | // _type->setParent(std::dynamic_pointer_cast(var_def_node)); 363 | //// std::shared_ptr stmt_node(new StmtNode(std::dynamic_pointer_cast(block), ND_STMT, var_def_node)); 364 | //// var_def_node->setParent(std::dynamic_pointer_cast(stmt_node)); 365 | // children.insert(children.begin() + index, std::dynamic_pointer_cast(var_def_node)); 366 | // ++index; 367 | // ++end; 368 | // _it->second = nullptr; 369 | // } 370 | } 371 | } 372 | return true; 373 | } 374 | -------------------------------------------------------------------------------- /src/back_end/optimizer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * optimizer.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_BACK_END_OPTIMIZER_H_ 9 | #define SRC_BACK_END_OPTIMIZER_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "../common/node.h" 16 | #include "../tools/common.h" 17 | class Token; 18 | 19 | extern char const *last_optimize_error_msg; 20 | 21 | bool evaluate_literal_node(std::shared_ptr const &node, std::shared_ptr &literal); 22 | 23 | int get_optimize_level(); 24 | void set_optimize_level(int _optimize); 25 | bool merge_block_function_call(std::shared_ptr &block); 26 | 27 | #endif /* SRC_BACK_END_OPTIMIZER_H_ */ 28 | -------------------------------------------------------------------------------- /src/common/config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * config.cpp 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include "config.h" 9 | 10 | -------------------------------------------------------------------------------- /src/common/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * config.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_COMMON_CONFIG_H_ 9 | #define SRC_COMMON_CONFIG_H_ 10 | 11 | 12 | 13 | 14 | 15 | #endif /* SRC_COMMON_CONFIG_H_ */ 16 | -------------------------------------------------------------------------------- /src/common/global.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * global.cpp 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include "global.h" 9 | 10 | OPERATOR_INFO OPERATOR_INFOS[] = { { OP_UNARY_PLUS, 16, 1, AS_R2L }, // /* + */ 11 | { OP_UNARY_MINUS, 16, 1, AS_R2L }, // /* - */ 12 | { OP_LOGICAL_NOT, 16, 1, AS_R2L }, // /* ! */ 13 | { OP_MUL, 15, 2, AS_L2R }, // /* * */ 14 | { OP_DIV, 15, 2, AS_L2R }, // /* / */ 15 | { OP_MOD, 15, 2, AS_L2R }, // /* % */ 16 | { OP_ADD, 14, 2, AS_L2R }, // /* + */ 17 | { OP_SUB, 14, 2, AS_L2R }, // /* - */ 18 | { OP_GREATER, 13, 2, AS_L2R }, // /* > */ 19 | { OP_LESS, 13, 2, AS_L2R }, // /* < */ 20 | { OP_GREATER_EQUAL, 13, 2, AS_L2R }, // /* >= */ 21 | { OP_LESS_EQUAL, 13, 2, AS_L2R }, // /* <= */ 22 | { OP_EQUAL, 12, 2, AS_L2R }, // /* == */ 23 | { OP_NOT_EQUAL, 12, 2, AS_L2R }, // /* != */ 24 | { OP_LOGICAL_AND, 11, 2, AS_L2R }, // /* && */ 25 | { OP_LOGICAL_OR, 10, 2, AS_L2R }, // /* || */ 26 | { OP_ASSIGNMENT, 9, 2, AS_R2L } // /* = */ 27 | }; 28 | const unsigned int OPERATOR_INFOS_LEN = sizeof(OPERATOR_INFOS) 29 | / sizeof(OPERATOR_INFO); 30 | 31 | std::map INIT_OPERATOR_INFO_MAP() { 32 | std::map map; 33 | for (int i = 0; i < OPERATOR_INFOS_LEN; ++i) { 34 | OPERATOR_INFO const &info = OPERATOR_INFOS[i]; 35 | map[info.operator_type] = info; 36 | } 37 | return map; 38 | } 39 | std::map OPERATOR_INFO_MAP = INIT_OPERATOR_INFO_MAP(); 40 | 41 | 42 | 43 | //std::map INIT_OP_TYPE_MAP() { 44 | // std::map map; 45 | // return map; 46 | //} 47 | //std::map OP_TYPE_MAP = INIT_OP_TYPE_MAP(); 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | long long next_global_id() { 56 | static long long id = 0; 57 | return ++id; 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/common/global.h: -------------------------------------------------------------------------------- 1 | /* 2 | * global.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_COMMON_GLOBAL_H_ 9 | #define SRC_COMMON_GLOBAL_H_ 10 | 11 | #include 12 | 13 | #include "type.h" 14 | 15 | enum SY_ASSOCIATIVITY { 16 | AS_L2R, AS_R2L 17 | }; 18 | typedef struct OPERATOR_INFO { 19 | OP_TYPE operator_type; 20 | int precedence; 21 | int parameter_number; 22 | SY_ASSOCIATIVITY associativity; 23 | } OPERATOR_INFO; 24 | 25 | extern std::map OPERATOR_INFO_MAP; 26 | 27 | long long next_global_id(); 28 | 29 | #endif /* SRC_COMMON_GLOBAL_H_ */ 30 | -------------------------------------------------------------------------------- /src/common/type.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * type.cpp 3 | * 4 | * Created on: Jul 28, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include "type.h" 9 | 10 | char const *const SY_KEYWORD[] = { "const", "int", "void", "if", "else", "while", "continue", "break", "return" }; 11 | 12 | char const *const SY_OPERATOR[] = { "+", "-", "!", "*", "/", "%", /* "+", "-", */ 13 | ">", "<", ">=", "<=", "==", "!=", "&&", "||", "=" }; 14 | 15 | char const *const SY_OTHER_SYMBOL[] = { ";", "(", ")", ",", "{", "}", "[", "]" }; 16 | 17 | char const *const SY_MULTI_LETTER_SYMBOL[] = { ">=", "<=", "==", "!=", "&&", "||" }; 18 | 19 | char const *const SY_SINGLE_LETTER_SYMBOL[] = { "+", "-", "!", "*", "/", "%", ">", "<", "=", ";", "(", ")", ",", "{", "}", "[", "]" }; 20 | 21 | char const *const SY_SYSTEM_FUNCTION[] = { "getint", "getch", "getarray", "putint", "putch", "putarray", "putf", "starttime", "stoptime", "_sysy_starttime", 22 | "_sysy_stoptime", "malloc" }; 23 | 24 | const unsigned int SY_KEYWORD_LEN = sizeof(SY_KEYWORD) / sizeof(SY_KEYWORD[0]); 25 | const unsigned int SY_OPERATOR_LEN = sizeof(SY_OPERATOR) / sizeof(SY_OPERATOR[0]); 26 | const unsigned int SY_OTHER_SYMBOL_LEN = sizeof(SY_OTHER_SYMBOL) / sizeof(SY_OTHER_SYMBOL[0]); 27 | 28 | const unsigned int SY_MULTI_LETTER_SYMBOL_LEN = sizeof(SY_MULTI_LETTER_SYMBOL) / sizeof(SY_MULTI_LETTER_SYMBOL[0]); 29 | 30 | const unsigned int SY_SINGLE_LETTER_SYMBOL_LEN = sizeof(SY_SINGLE_LETTER_SYMBOL) / sizeof(SY_MULTI_LETTER_SYMBOL[0]); 31 | 32 | const unsigned int SY_SYSTEM_FUNCTION_LEN = sizeof(SY_SYSTEM_FUNCTION) / sizeof(SY_SYSTEM_FUNCTION[0]); 33 | 34 | char const *const SY_COMMENT[] = { SINGLE_LINE_COMMENT_START, 35 | MULTI_LINE_COMMENT_START, 36 | MULTI_LINE_COMMENT_END }; 37 | -------------------------------------------------------------------------------- /src/common/type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * type.h 3 | * 4 | * Created on: Jul 28, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_COMMON_TYPE_H_ 9 | #define SRC_COMMON_TYPE_H_ 10 | 11 | #define SINGLE_LINE_COMMENT_START "//" 12 | #define MULTI_LINE_COMMENT_START "/*" 13 | #define MULTI_LINE_COMMENT_END "*/" 14 | 15 | enum VA_TYPE { 16 | VA_UNKNOWN, VA_INT, VA_VOID 17 | }; 18 | 19 | enum TK_TYPE { 20 | TK_UNKNOWN, 21 | 22 | TK_OPERATOR, TK_SYMBOL, TK_DEC_DIGIT, TK_HEX_DIGIT, TK_OCT_DIGIT, 23 | 24 | TK_CONST, TK_INT, TK_VOID, TK_IF, TK_ELSE, TK_WHILE, TK_CONTINUE, TK_BREAK, TK_RETURN, 25 | 26 | TK_IDENTIFIER 27 | }; 28 | 29 | enum ND_TYPE { 30 | ND_UNKNOWN, 31 | 32 | ND_TYPE_, 33 | ND_VAR_TYPE, 34 | ND_DEF, 35 | ND_VAR_DEF, 36 | ND_FUN_DEF, 37 | ND_REF, 38 | ND_VAR, 39 | ND_FUN, 40 | ND_STMT, 41 | ND_PARENTHESIS, 42 | ND_LITERAL, 43 | ND_ARRAY, 44 | ND_ARRAY_ACCESS, 45 | ND_OPERATION, 46 | ND_IF_ELSE, 47 | ND_WHILE, 48 | ND_CONTINUE, 49 | ND_BREAK, 50 | ND_RETURN, 51 | ND_BLOCK 52 | }; 53 | 54 | enum OP_TYPE { 55 | OP_UNKNOWN, OP_NONE, 56 | 57 | /* + */OP_UNARY_PLUS, 58 | /* - */OP_UNARY_MINUS, 59 | /* ! */OP_LOGICAL_NOT, 60 | /* * */OP_MUL, 61 | /* / */OP_DIV, 62 | /* % */OP_MOD, 63 | /* + */OP_ADD, 64 | /* - */OP_SUB, 65 | /* > */OP_GREATER, 66 | /* < */OP_LESS, 67 | /* >= */OP_GREATER_EQUAL, 68 | /* <= */OP_LESS_EQUAL, 69 | /* == */OP_EQUAL, 70 | /* != */OP_NOT_EQUAL, 71 | /* && */OP_LOGICAL_AND, 72 | /* || */OP_LOGICAL_OR, 73 | /* = */OP_ASSIGNMENT, 74 | }; 75 | 76 | extern char const *const SY_KEYWORD[]; 77 | extern char const *const SY_OPERATOR[]; 78 | extern char const *const SY_OTHER_SYMBOL[]; 79 | extern char const *const SY_MULTI_LETTER_SYMBOL[]; 80 | extern char const *const SY_SINGLE_LETTER_SYMBOL[]; 81 | extern char const *const SY_SYSTEM_FUNCTION[]; 82 | extern char const *const SY_COMMENT[]; 83 | 84 | extern const unsigned int SY_KEYWORD_LEN; 85 | extern const unsigned int SY_OPERATOR_LEN; 86 | extern const unsigned int SY_OTHER_SYMBOL_LEN; 87 | extern const unsigned int SY_MULTI_LETTER_SYMBOL_LEN; 88 | extern const unsigned int SY_SINGLE_LETTER_SYMBOL_LEN; 89 | extern const unsigned int SY_SYSTEM_FUNCTION_LEN; 90 | 91 | #endif /* SRC_COMMON_TYPE_H_ */ 92 | -------------------------------------------------------------------------------- /src/compiler.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "tools/common.h" 12 | #include "front_end/tokenize.h" 13 | #include "common/node.h" 14 | #include "front_end/parser.h" 15 | #include "front_end/semantic_analyzer.h" 16 | #include "front_end/intermediate_representation.h" 17 | #include "back_end/arm_generator.h" 18 | #include "back_end/optimizer.h" 19 | 20 | //https://blog.xojo.com/2017/12/06/compilers-101-overview-and-lexer/ 21 | //https://cs.lmu.edu/~ray/notes/ir/#:~:text=An%20intermediate%20representation%20is%20a,used%20in%20a%20retargetable%20compiler. 22 | //https://azeria-labs.com/writing-arm-assembly-part-1/ 23 | 24 | int main(int argc, char *argv[]) { 25 | if (argc < 2) { 26 | std::cerr << "Argument count error!" << std::endl; 27 | return 1; 28 | } 29 | // const char *sysy_file_path = argv[1]; 30 | const char *sysy_file_path = nullptr; 31 | const char *output_file_path = nullptr; 32 | int optimize = 0; 33 | // --argc; 34 | // ++argv; 35 | 36 | char option; 37 | // put ':' at the starting of the string so compiler can distinguish between '?' and ':' 38 | while ((option = getopt(argc, argv, ":So:O:")) != -1) { //get option from the getopt() method 39 | switch (option) { 40 | //For option i, r, l, print that these are options 41 | case 'S': 42 | // std::cout << "Given Option: " << option << std::endl; 43 | break; 44 | case 'O': 45 | optimize = std::stoi(optarg); 46 | // std::cout << "Given Option: O" << optimize << std::endl; 47 | break; 48 | case 'o': //here f is used for some file name 49 | // std::cout << "Ouput file: " << optarg << std::endl; 50 | output_file_path = optarg; 51 | break; 52 | case ':': 53 | std::cerr << "Option needs a value." << std::endl; 54 | return 2; 55 | break; 56 | case '?': //used for some unknown options 57 | std::cerr << "Unknown option: " << (char) optopt << std::endl; 58 | return 3; 59 | break; 60 | // default: 61 | // std::cerr << "Unknown option: " << (char)optopt << std::endl; 62 | } 63 | } 64 | for (; optind < argc; optind++) { //when some extra arguments are passed 65 | // std::cerr << "Given extra arguments: " << argv[optind] << std::endl; 66 | // return 4; 67 | sysy_file_path = argv[optind]; 68 | } 69 | set_optimize_level(optimize); 70 | 71 | // optimize = std::string::npos != std::string(sysy_file_path).find("performance") || std::string::npos != std::string(sysy_file_path).find("bitset") 72 | // || std::string::npos != std::string(sysy_file_path).find("_mm"); 73 | std::string sy_file; 74 | if (!read_file2string(sysy_file_path, sy_file)) 75 | return 4; 76 | ReplaceStringInPlace(sy_file, "putf(", "printf("); 77 | ReplaceStringInPlace(sy_file, "_SYSY_N", "1024"); 78 | 79 | expand_timing_function(sy_file, "starttime", "_sysy_starttime"); 80 | expand_timing_function(sy_file, "stoptime", "_sysy_stoptime"); 81 | 82 | 83 | std::vector> tokens; 84 | if (!string2tokens(sy_file.c_str(), tokens)) { 85 | std::cerr << last_tokenize_error_msg << std::endl; 86 | return 5; 87 | } 88 | std::string str; 89 | if (!dump_tokens(tokens, str)) 90 | return 6; 91 | // std::cout << str << std::endl; 92 | std::shared_ptr root = parse(tokens); 93 | if (!root) { 94 | std::cerr << last_parse_error_msg << std::endl; 95 | return 7; 96 | } 97 | // return !root; 98 | 99 | if (!semantic_analyze(root)) { 100 | std::cerr << last_semantic_analyze_error_msg << std::endl; 101 | return 9; 102 | } 103 | std::ostringstream oss; 104 | if (!dump_node2json(std::dynamic_pointer_cast(root), oss)) 105 | return 8; 106 | // std::cout << oss.str() << std::endl; 107 | 108 | oss.str(""); 109 | if (!gen_arm_assembly_code(std::dynamic_pointer_cast(root), oss)) { 110 | std::cerr << last_arm_generate_error_msg << std::endl; 111 | return 9; 112 | } 113 | // std::cout << oss.str() << std::endl; 114 | 115 | if (!write_string2file(output_file_path, oss.str())) 116 | return 10; 117 | std::cout << "All OK!" << std::endl; 118 | 119 | return 0; 120 | } 121 | -------------------------------------------------------------------------------- /src/front_end/intermediate_representation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * intermediate_representation.cpp 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include "intermediate_representation.h" 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/front_end/intermediate_representation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * intermediate_representation.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_FRONT_END_INTERMEDIATE_REPRESENTATION_H_ 9 | #define SRC_FRONT_END_INTERMEDIATE_REPRESENTATION_H_ 10 | 11 | 12 | 13 | 14 | 15 | #endif /* SRC_FRONT_END_INTERMEDIATE_REPRESENTATION_H_ */ 16 | -------------------------------------------------------------------------------- /src/front_end/parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * parser.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_FRONT_END_PARSER_H_ 9 | #define SRC_FRONT_END_PARSER_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "../common/node.h" 16 | class Token; 17 | 18 | extern char const *last_parse_error_msg; 19 | 20 | std::shared_ptr gen_block_child_node(std::vector>::const_iterator ¤t, 21 | std::vector>::const_iterator &end, bool &is_reach_end); 22 | std::shared_ptr gen_block_node(std::vector>::const_iterator ¤t, 23 | std::vector>::const_iterator &end); 24 | 25 | std::shared_ptr parse(std::vector>const &tokens); 26 | bool dump_node2json(std::shared_ptr const &node, std::ostringstream &oss); 27 | 28 | #endif /* SRC_FRONT_END_PARSER_H_ */ 29 | -------------------------------------------------------------------------------- /src/front_end/semantic_analyzer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * semantic_analyzer.h 3 | * 4 | * Created on: Jul 29, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_FRONT_END_SEMANTIC_ANALYZER_H_ 9 | #define SRC_FRONT_END_SEMANTIC_ANALYZER_H_ 10 | #include 11 | #include 12 | #include 13 | #include "../common/node.h" 14 | 15 | extern char const *last_semantic_analyze_error_msg; 16 | 17 | bool semantic_analyze(std::shared_ptr &root); 18 | 19 | #endif /* SRC_FRONT_END_SEMANTIC_ANALYZER_H_ */ 20 | -------------------------------------------------------------------------------- /src/front_end/tokenize.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * tokenize.cpp 3 | * 4 | * Created on: Jul 28, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | #include "tokenize.h" 12 | 13 | char const *last_tokenize_error_msg = nullptr; 14 | 15 | Token::Token() : 16 | Token(nullptr, -1, nullptr) { 17 | 18 | } 19 | Token::Token(char const *start, int length, char const *const name) : 20 | Token(start, length, name, TK_UNKNOWN) { 21 | 22 | } 23 | Token::Token(char const *start, int length, char const *const name, TK_TYPE type) : 24 | start(start), length(length), name(name), type(type) { 25 | 26 | } 27 | Token::~Token() { 28 | 29 | } 30 | 31 | int Token::getLength() const { 32 | return length; 33 | } 34 | 35 | void Token::setLength(int length) { 36 | this->length = length; 37 | } 38 | 39 | const std::string& Token::getName() const { 40 | return name; 41 | } 42 | 43 | void Token::setName(const std::string &name) { 44 | this->name = name; 45 | } 46 | 47 | const char* Token::getStart() const { 48 | return start; 49 | } 50 | 51 | void Token::setStart(const char *start) { 52 | this->start = start; 53 | } 54 | 55 | TK_TYPE Token::getType() const { 56 | return type; 57 | } 58 | 59 | void Token::setType(TK_TYPE type) { 60 | this->type = type; 61 | } 62 | 63 | ////////////////// 64 | 65 | inline bool is_identifier_start(char const *const ptr) { 66 | return isalpha(*ptr) || *ptr == '_'; 67 | } 68 | 69 | inline bool is_start_with(char const *const s1, char const *const s2) { 70 | return !strncmp(s1, s2, strlen(s2)); 71 | } 72 | 73 | inline bool is_identifier_start_letter(char ch) { 74 | return isalpha(ch) || ch == '_'; 75 | } 76 | 77 | inline bool is_identifier_letter(char ch) { 78 | return isalpha(ch) || isdigit(ch) || ch == '_'; 79 | } 80 | 81 | inline std::shared_ptr new_identifier_token(char const *¤t) { 82 | char const *start = current; 83 | while (is_identifier_letter(*++current)) 84 | ; 85 | char const *end = current; 86 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 87 | } 88 | 89 | inline std::shared_ptr new_literal_token(char const *¤t) { 90 | char const *start = current; 91 | if ('0' != *current) { 92 | while (isdigit(*++current)) 93 | ; 94 | char const *end = current; 95 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 96 | } 97 | ++current; 98 | if ('x' != *current && 'X' != *current) { 99 | if (strchr("01234567", *current)) 100 | while (strchr("01234567", *++current)) 101 | ; 102 | char const *end = current; 103 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 104 | } 105 | { 106 | while (strchr("0123456789abcdefABCDEF", *++current)) 107 | ; 108 | char const *end = current; 109 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 110 | } 111 | } 112 | 113 | inline bool is_multi_letter_symbol(char const *current) { 114 | for (int i = 0; i < SY_MULTI_LETTER_SYMBOL_LEN; ++i) { 115 | if (is_start_with(current, SY_MULTI_LETTER_SYMBOL[i])) 116 | return true; 117 | } 118 | return false; 119 | } 120 | 121 | inline std::shared_ptr new_multi_letter_symbol(char const *¤t) { 122 | char const *start = current; 123 | for (int i = 0; i < SY_MULTI_LETTER_SYMBOL_LEN; ++i) { 124 | if (is_start_with(current, SY_MULTI_LETTER_SYMBOL[i])) { 125 | current += strlen(SY_MULTI_LETTER_SYMBOL[i]); 126 | char const *end = current; 127 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 128 | } 129 | } 130 | //never reached! 131 | return nullptr; 132 | } 133 | 134 | inline bool is_single_letter_symbol(char const *current) { 135 | for (int i = 0; i < SY_SINGLE_LETTER_SYMBOL_LEN; ++i) { 136 | if (is_start_with(current, SY_SINGLE_LETTER_SYMBOL[i])) 137 | return true; 138 | } 139 | return false; 140 | } 141 | 142 | inline std::shared_ptr new_single_letter_symbol(char const *¤t) { 143 | char const *start = current; 144 | for (int i = 0; i < SY_SINGLE_LETTER_SYMBOL_LEN; ++i) { 145 | if (is_start_with(current, SY_SINGLE_LETTER_SYMBOL[i])) { 146 | current += strlen(SY_SINGLE_LETTER_SYMBOL[i]); 147 | char const *end = current; 148 | return std::shared_ptr(new Token(start, end - start, std::string(start, end).c_str())); 149 | } 150 | } 151 | //never reached! 152 | return nullptr; 153 | } 154 | 155 | bool is_include_in_array(char const *const*const array, const unsigned int array_len, char const *const str) { 156 | for (int i = 0; i < array_len; ++i) { 157 | if (!strcmp(str, array[i])) { 158 | return true; 159 | } 160 | } 161 | return false; 162 | } 163 | 164 | bool set_token_type(std::vector> &tokens) { 165 | for (std::vector>::iterator it = tokens.begin(), end = tokens.end(); it != end; ++it) { 166 | char const *const name = (*it)->getName().c_str(); 167 | 168 | if (is_include_in_array(SY_OPERATOR, SY_OPERATOR_LEN, name)) { 169 | (*it)->setType(TK_OPERATOR); 170 | continue; 171 | } 172 | if (is_include_in_array(SY_OTHER_SYMBOL, SY_OTHER_SYMBOL_LEN, name)) { 173 | (*it)->setType(TK_SYMBOL); 174 | continue; 175 | } 176 | if (isdigit(*name)) { 177 | if ('0' != *name) 178 | (*it)->setType(TK_DEC_DIGIT); 179 | else if ('x' == *(name + 1) || 'X' == *(name + 1)) 180 | (*it)->setType(TK_HEX_DIGIT); 181 | else 182 | (*it)->setType(TK_OCT_DIGIT); 183 | continue; 184 | } 185 | TK_TYPE type; 186 | if (!strcmp("const", name)) 187 | type = TK_CONST; 188 | else if (!strcmp("int", name)) 189 | type = TK_INT; 190 | else if (!strcmp("void", name)) 191 | type = TK_VOID; 192 | else if (!strcmp("if", name)) 193 | type = TK_IF; 194 | else if (!strcmp("else", name)) 195 | type = TK_ELSE; 196 | else if (!strcmp("while", name)) 197 | type = TK_WHILE; 198 | else if (!strcmp("continue", name)) 199 | type = TK_CONTINUE; 200 | else if (!strcmp("break", name)) 201 | type = TK_BREAK; 202 | else if (!strcmp("return", name)) 203 | type = TK_RETURN; 204 | else 205 | type = TK_IDENTIFIER; 206 | (*it)->setType(type); 207 | continue; 208 | return false; 209 | } 210 | return true; 211 | } 212 | 213 | bool string2tokens(char const *const str, std::vector> &tokens) { 214 | 215 | char const *current = str; 216 | 217 | while (*current) { 218 | //skip space \s\r\n... 219 | if (isspace(*current)) { 220 | ++current; 221 | continue; 222 | } 223 | //skip SINGLE_LINE_COMMENT 224 | if (is_start_with(current, SINGLE_LINE_COMMENT_START)) { 225 | current += 2; 226 | while ('\n' != *(current++)) 227 | ; 228 | continue; 229 | } 230 | //skip MULTI_LINE_COMMENT 231 | if (is_start_with(current, MULTI_LINE_COMMENT_START)) { 232 | while (!is_start_with(++current, MULTI_LINE_COMMENT_END)) 233 | ; 234 | current += 2; 235 | continue; 236 | } 237 | //identifier 238 | if (is_identifier_start_letter(*current)) { 239 | tokens.push_back(new_identifier_token(current)); 240 | continue; 241 | } 242 | //literal digit 243 | if (isdigit(*current)) { 244 | tokens.push_back(new_literal_token(current)); 245 | continue; 246 | } 247 | //multi_letter_symbol 248 | if (is_multi_letter_symbol(current)) { 249 | tokens.push_back(new_multi_letter_symbol(current)); 250 | continue; 251 | } 252 | //single_letter_symbol 253 | if (is_single_letter_symbol(current)) { 254 | tokens.push_back(new_single_letter_symbol(current)); 255 | continue; 256 | } 257 | 258 | last_tokenize_error_msg = "Unexpected token!"; 259 | //never reached! 260 | return false; 261 | } 262 | 263 | 264 | return set_token_type(tokens); 265 | } 266 | 267 | bool dump_tokens(std::vector> const &tokens, std::string &str) { 268 | std::ostringstream str_stream; 269 | 270 | for (std::vector>::const_iterator it = tokens.begin(), end = tokens.end(); it != end; ++it) { 271 | char const *type; 272 | switch ((*it)->getType()) { 273 | case TK_UNKNOWN: 274 | type = "TK_UNKNOWN"; 275 | break; 276 | case TK_OPERATOR: 277 | type = "TK_OPERATOR"; 278 | break; 279 | case TK_SYMBOL: 280 | type = "TK_SYMBOL"; 281 | break; 282 | case TK_DEC_DIGIT: 283 | type = "TK_DEC_DIGIT"; 284 | break; 285 | case TK_HEX_DIGIT: 286 | type = "TK_HEX_DIGIT"; 287 | break; 288 | case TK_OCT_DIGIT: 289 | type = "TK_OCT_DIGIT"; 290 | break; 291 | case TK_CONST: 292 | type = "TK_CONST"; 293 | break; 294 | case TK_INT: 295 | type = "TK_INT"; 296 | break; 297 | case TK_VOID: 298 | type = "TK_VOID"; 299 | break; 300 | case TK_IF: 301 | type = "TK_IF"; 302 | break; 303 | case TK_ELSE: 304 | type = "TK_ELSE"; 305 | break; 306 | case TK_WHILE: 307 | type = "TK_WHILE"; 308 | break; 309 | case TK_CONTINUE: 310 | type = "TK_CONTINUE"; 311 | break; 312 | case TK_BREAK: 313 | type = "TK_BREAK"; 314 | break; 315 | case TK_RETURN: 316 | type = "TK_RETURN"; 317 | break; 318 | case TK_IDENTIFIER: 319 | type = "TK_IDENTIFIER"; 320 | break; 321 | default: 322 | type = "This is an error!!!!"; 323 | } 324 | str_stream << (*it)->getName() << "/*" << type << "*/" << ' '; 325 | } 326 | str = str_stream.str(); 327 | return true; 328 | } 329 | 330 | -------------------------------------------------------------------------------- /src/front_end/tokenize.h: -------------------------------------------------------------------------------- 1 | /* 2 | * tokenize.h 3 | * 4 | * Created on: Jul 28, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_FRONT_END_TOKENIZE_H_ 9 | #define SRC_FRONT_END_TOKENIZE_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "../common/type.h" 16 | 17 | extern char const *last_tokenize_error_msg; 18 | 19 | class Token { 20 | private: 21 | char const *start; 22 | int length; 23 | std::string name; 24 | TK_TYPE type; 25 | 26 | public: 27 | Token(); 28 | Token(char const *start, int length, char const *const name); 29 | Token(char const *start, int length, char const *const name, TK_TYPE type); 30 | ~Token(); 31 | 32 | int getLength() const; 33 | void setLength(int length); 34 | const std::string& getName() const; 35 | void setName(const std::string &name); 36 | const char* getStart() const; 37 | void setStart(const char *start); 38 | TK_TYPE getType() const; 39 | void setType(TK_TYPE type); 40 | 41 | }; 42 | 43 | bool string2tokens(char const *const str, std::vector> &tokens); 44 | bool dump_tokens(std::vector> const &tokens, std::string &str); 45 | 46 | #endif /* SRC_FRONT_END_TOKENIZE_H_ */ 47 | -------------------------------------------------------------------------------- /src/tools/common.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * common.cpp 3 | * 4 | * Created on: Jul 24, 2020 5 | * Author: davies 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "common.h" 15 | 16 | std::string& ReplaceStringInPlace(std::string &subject, const std::string &search, const std::string &replace) { 17 | size_t pos = 0; 18 | while ((pos = subject.find(search, pos)) != std::string::npos) { 19 | subject.replace(pos, search.length(), replace); 20 | pos += replace.length(); 21 | } 22 | return subject; 23 | } 24 | 25 | bool read_file2string(char const *const file_path, std::string &str) { 26 | std::ifstream file(file_path); 27 | if (!file) 28 | return false; 29 | str.assign((std::istreambuf_iterator(file)), std::istreambuf_iterator()); 30 | return true; 31 | } 32 | 33 | bool write_string2file(char const *const file_path, std::string const &str) { 34 | std::ofstream file(file_path); 35 | if (!file) 36 | return false; 37 | file << str; 38 | return true; 39 | } 40 | 41 | int expand_timing_function(std::string &source_file, char const *const macro_name, char const *const function_name) { 42 | int count = 0; 43 | int current_line = 0; 44 | std::ostringstream oss; 45 | std::size_t last_position = 0; 46 | std::size_t current_position = 0; 47 | char const *const _source_file = source_file.c_str(); 48 | while (std::string::npos != (current_position = source_file.find(macro_name, last_position))) { 49 | // if (*(_source_file + current_position + strlen(macro_name) + 1 )!= ')') 50 | // continue; 51 | ++count; 52 | oss.write(_source_file + last_position, current_position - last_position); 53 | for (int i = last_position; i < current_position; ++i) { 54 | if ('\n' == _source_file[i]) 55 | ++current_line; 56 | } 57 | oss << function_name << "(" << current_line + 1 << ")"; 58 | last_position = current_position + strlen(macro_name) + 2; 59 | } 60 | oss.write(_source_file + last_position, strlen(_source_file) - last_position); 61 | source_file = oss.str(); 62 | return count; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /src/tools/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * common.h 3 | * 4 | * Created on: Jul 24, 2020 5 | * Author: davies 6 | */ 7 | 8 | #ifndef SRC_TOOLS_COMMON_H_ 9 | #define SRC_TOOLS_COMMON_H_ 10 | #include 11 | 12 | std::string& ReplaceStringInPlace(std::string &subject, const std::string &search, const std::string &replace); 13 | bool read_file2string(char const *const file_path, std::string &str); 14 | bool write_string2file(char const *const file_path, std::string const &str); 15 | int expand_timing_function(std::string &source_file, char const *const macro_name, char const *const function_name); 16 | bool read_dir(char const *const dir, std::ostringstream &oss); 17 | bool fun_check(char const *const sysy_file_path, char const *const output_file_path); 18 | 19 | #define OPERATE_MUL() std::multiplies() 20 | #define OPERATE_DIV() std::divides() 21 | #define OPERATE_MOD() std::modulus() 22 | #define OPERATE_ADD() std::plus() 23 | #define OPERATE_SUB() std::minus() 24 | #define OPERATE_GREATER() std::greater() 25 | #define OPERATE_LESS() std::less() 26 | #define OPERATE_GREATER_EQUAL() std::greater_equal() 27 | #define OPERATE_LESS_EQUAL() std::less_equal() 28 | #define OPERATE_EQUAL() std::equal_to() 29 | #define OPERATE_NOT_EQUAL() std::not_equal_to() 30 | #define OPERATE_LOGICAL_AND() std::logical_and() 31 | #define OPERATE_LOGICAL_OR() std::logical_or() 32 | 33 | template 34 | inline long long calculation(long long operand0, long long operand1, OPERATE operate) { 35 | return operate(operand0, operand1); 36 | } 37 | 38 | #endif /* SRC_TOOLS_COMMON_H_ */ 39 | -------------------------------------------------------------------------------- /test_sample/functional_test/00_arr_defn2.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/00_arr_defn2.sy: -------------------------------------------------------------------------------- 1 | int a[10][10]; 2 | int main(){ 3 | return 0; 4 | } -------------------------------------------------------------------------------- /test_sample/functional_test/00_main.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/00_main.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | return 0; 3 | } 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/01_var_defn.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/01_var_defn.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int main(){ 3 | a=10; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /test_sample/functional_test/02_arr_defn4.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/02_arr_defn4.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a[4][2]={1,2,3,4,5,6,7,8}; 3 | int b[4][2]={{a[0][0],a[0][1]},{3,4},{5,6},{7,8}}; 4 | return 0; 5 | } -------------------------------------------------------------------------------- /test_sample/functional_test/02_var_defn2.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/02_var_defn2.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a=10; 5 | int c; 6 | c=10; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test_sample/functional_test/03_arr_defn.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/03_arr_defn.sy: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | int main(){ 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /test_sample/functional_test/03_var_defn2.out: -------------------------------------------------------------------------------- 1 | 28 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/03_var_defn2.sy: -------------------------------------------------------------------------------- 1 | int a,b; 2 | 3 | int main(){ 4 | a=10; 5 | b=5; 6 | int c=a*2+b+3; 7 | return c; 8 | } -------------------------------------------------------------------------------- /test_sample/functional_test/04_const_defn.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/04_const_defn.sy: -------------------------------------------------------------------------------- 1 | const int x=4; 2 | 3 | int main(){ 4 | return x; 5 | } -------------------------------------------------------------------------------- /test_sample/functional_test/04_func_defn.out: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/04_func_defn.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int func(int p){ 3 | p = p - 1; 4 | return p; 5 | } 6 | int main(){ 7 | int b; 8 | a = 10; 9 | b = func(a); 10 | return b; 11 | } 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/05_add.out: -------------------------------------------------------------------------------- 1 | 30 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/05_add.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | 4 | int main(){ 5 | a=10; 6 | b=20; 7 | int c; 8 | c = a + b; 9 | return c; 10 | } 11 | -------------------------------------------------------------------------------- /test_sample/functional_test/05_const_array_defn.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/05_const_array_defn.sy: -------------------------------------------------------------------------------- 1 | const int a[5]={0,1,2,3,4}; 2 | 3 | int main(){ 4 | return a[4]; 5 | } -------------------------------------------------------------------------------- /test_sample/functional_test/06_mod.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/06_mod.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = 10; 5 | b = 3; 6 | int c; 7 | c = a % b; 8 | return c; 9 | } 10 | -------------------------------------------------------------------------------- /test_sample/functional_test/06_var_defn_func.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/06_var_defn_func.sy: -------------------------------------------------------------------------------- 1 | int defn(){ 2 | return 4; 3 | } 4 | 5 | int main(){ 6 | int a=defn(); 7 | return a; 8 | } -------------------------------------------------------------------------------- /test_sample/functional_test/07_comment1.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/07_comment1.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | //this is a single line comment 3 | //int a; 4 | //a=5; 5 | return 0; 6 | } -------------------------------------------------------------------------------- /test_sample/functional_test/07_return_var.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/07_return_var.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | int main(){ 4 | a=10; 5 | return a; 6 | } 7 | -------------------------------------------------------------------------------- /test_sample/functional_test/08_arr_assign.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/08_arr_assign.sy: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | int main(){ 3 | a[0]=1; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /test_sample/functional_test/08_comment2.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/08_comment2.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | /* 3 | The comment block; 4 | int a=5; 5 | int b[10]; 6 | int c; 7 | int d=a*2; 8 | */ 9 | return 0; 10 | } -------------------------------------------------------------------------------- /test_sample/functional_test/09_if.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/09_if.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | int main(){ 4 | a = 10; 5 | if( a>0 ){ 6 | return 1; 7 | } 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test_sample/functional_test/09_void_func.out: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/09_void_func.sy: -------------------------------------------------------------------------------- 1 | int a,b,c; 2 | 3 | void add(int a,int b){ 4 | c=a+b; 5 | return; 6 | } 7 | 8 | int main(){ 9 | a=3; 10 | b=2; 11 | add(a,b); 12 | return c; 13 | } -------------------------------------------------------------------------------- /test_sample/functional_test/10_break.out: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/10_break.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a=10; 3 | while(a>0){ 4 | a=a-1; 5 | if(a==5) 6 | break; 7 | } 8 | return a; 9 | } -------------------------------------------------------------------------------- /test_sample/functional_test/10_if_else.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/10_if_else.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int main(){ 3 | a = 10; 4 | if( a>0 ){ 5 | return 1; 6 | } 7 | else{ 8 | return 0; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test_sample/functional_test/11_continue.out: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/11_continue.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a=10; 3 | while(a>0){ 4 | if(a>5){ 5 | a=a-1; 6 | continue; 7 | } 8 | return a; 9 | } 10 | return a; 11 | } -------------------------------------------------------------------------------- /test_sample/functional_test/11_while.out: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/11_while.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | b=0; 5 | a=3; 6 | while(a>0){ 7 | b = b+a; 8 | a = a-1; 9 | } 10 | return b; 11 | } 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/12_array_traverse.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/12_array_traverse.sy: -------------------------------------------------------------------------------- 1 | int a[3][4]; 2 | 3 | int main(){ 4 | int i=0; 5 | int cnt=0; 6 | while(i<=3+4-2){ 7 | int j=i; 8 | while(j>=0){ 9 | if(j<4&&i-j<3){ 10 | a[i-j][j]=cnt; 11 | cnt=1; 12 | } 13 | j=j-1; 14 | } 15 | i=i+1; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /test_sample/functional_test/12_getint.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:917df3320d778ddbaa5c5c7742bc4046bf803c36ed2b050f30844ed206783469 3 | size 3 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/12_getint.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/12_getint.sy: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a; 3 | a = getint(); 4 | return a; 5 | } 6 | -------------------------------------------------------------------------------- /test_sample/functional_test/13_and.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f4a8ae8e74ddfb896a256de4e3099911dcaa6a9302591713898069b0bcd6e3d7 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/13_and.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/13_and.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = getint(); 5 | b = getint(); 6 | if ( a && b ) { 7 | return 1; 8 | } 9 | else { 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test_sample/functional_test/13_array_traverse2.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/13_array_traverse2.sy: -------------------------------------------------------------------------------- 1 | int a[3][3][3]; 2 | 3 | int main(){ 4 | int i,j,k; 5 | i=0;j=0;k=0; 6 | int cnt=0; 7 | while(i<3){ 8 | while(j<3){ 9 | while(k<3){ 10 | a[i][j][k]=cnt; 11 | cnt=cnt+1; 12 | k=k+1; 13 | } 14 | j=j+1; 15 | } 16 | i=i+1; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /test_sample/functional_test/14_or.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f4a8ae8e74ddfb896a256de4e3099911dcaa6a9302591713898069b0bcd6e3d7 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/14_or.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/14_or.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main() { 4 | a = getint(); 5 | b = getint(); 6 | if ( a || b ){ 7 | return 1; 8 | } 9 | else{ 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test_sample/functional_test/15_array_test3.out: -------------------------------------------------------------------------------- 1 | 15 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/15_array_test3.sy: -------------------------------------------------------------------------------- 1 | int a[5][5]={1,2,3,4,5}; 2 | 3 | int func(int a[][5]){ 4 | int i=0; 5 | int j=0; 6 | int sum=0; 7 | while(i<5){ 8 | while(j<5){ 9 | sum=sum+a[i][j]; 10 | j=j+1; 11 | } 12 | i=i+1; 13 | } 14 | return sum; 15 | } 16 | 17 | int main(){ 18 | putint(func(a)); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /test_sample/functional_test/15_equal.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:ece3d232c1ca9ef8a80b6fdb1585b8f5cf653b9dd023b0521c8da64db859ffac 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/15_equal.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/15_equal.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = getint(); 5 | b = getint(); 6 | if ( a == b ){ 7 | return 1; 8 | } 9 | else{ 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test_sample/functional_test/16_greater_eq.out: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/16_greater_eq.sy: -------------------------------------------------------------------------------- 1 | int a=5; 2 | int s[10]={9,8,7,6,5,4,3,2,1}; 3 | 4 | int main(){ 5 | int i=0; 6 | while(s[i]>=a){ 7 | i=i+1; 8 | } 9 | return i; 10 | } -------------------------------------------------------------------------------- /test_sample/functional_test/16_nequal.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:ece3d232c1ca9ef8a80b6fdb1585b8f5cf653b9dd023b0521c8da64db859ffac 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/16_nequal.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/16_nequal.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = getint(); 5 | b = getint(); 6 | if ( a != b ){ 7 | return 1; 8 | } 9 | else{ 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test_sample/functional_test/17_less.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:74def6278339a6381339a5866decda2f6900cb189db5d0852e43ecc9d047778d 3 | size 5 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/17_less.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/17_less.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = getint(); 5 | b = getint(); 6 | if ( a < b ){ 7 | return 1; 8 | } 9 | else{ 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test_sample/functional_test/17_less_eq.out: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/17_less_eq.sy: -------------------------------------------------------------------------------- 1 | int a=5; 2 | int s[10]={0,1,2,3,4,5,6,7,8,9}; 3 | 4 | int main(){ 5 | int i=0; 6 | while(s[i]<=a){ 7 | i=i+1; 8 | } 9 | return i; 10 | } -------------------------------------------------------------------------------- /test_sample/functional_test/18_cal_prio.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:1def07dbe06eeb097aafec8a40329937cd20c93a83634b8221ea2b41a894310c 3 | size 6 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/18_cal_prio.out: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/18_cal_prio.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int c; 4 | int main(){ 5 | a = getint(); 6 | b = getint(); 7 | c = getint(); 8 | int d; 9 | d = a+b*c; 10 | return d; 11 | } 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/19_neg_expr.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:337b794ce718a09a620090d53541c3b4640a64133bbee2188444810cd3169f81 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/19_neg_expr.out: -------------------------------------------------------------------------------- 1 | -5 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/19_neg_expr.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | int b; 3 | int main(){ 4 | a = getint(); 5 | b = getint(); 6 | int c; 7 | c = -(a + b); 8 | putint(c); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test_sample/functional_test/20_arr_sum.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f6b49467f595b1a44e442c198b3df4d221e88efcaabc26254f8e0ad4f79b6242 3 | size 10 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/20_arr_sum.out: -------------------------------------------------------------------------------- 1 | 12 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/20_arr_sum.sy: -------------------------------------------------------------------------------- 1 | int a[5]; 2 | int main(){ 3 | a[0] = getint(); 4 | a[1] = getint(); 5 | a[2] = getint(); 6 | a[3] = getint(); 7 | a[4] = getint(); 8 | int cnt; 9 | cnt = 4; 10 | int sum; 11 | sum = 0; 12 | while( cnt > 1 ){ 13 | sum = sum + a[cnt]; 14 | cnt = cnt - 1; 15 | } 16 | return sum; 17 | } 18 | -------------------------------------------------------------------------------- /test_sample/functional_test/21_suminput.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:d15bbeb14c535d6dfd8242b20d89981ca7d74523e3323e77fd81ad03b285e365 3 | size 8 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/21_suminput.out: -------------------------------------------------------------------------------- 1 | 6 2 | 6 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/21_suminput.sy: -------------------------------------------------------------------------------- 1 | //sample:input n numbers,then print the sum of them; 2 | int n; 3 | int a[10]; 4 | int main() 5 | { 6 | n = getint(); 7 | if (n > 10) 8 | return 1; 9 | int s; 10 | int i; 11 | i = 0; 12 | s = i; 13 | while (i < n) { 14 | a[i] = getint(); 15 | s = s + a[i]; 16 | i=i+1; 17 | } 18 | putint(s); 19 | int newline; 20 | newline = 10; 21 | putch(newline); 22 | return s; 23 | } 24 | -------------------------------------------------------------------------------- /test_sample/functional_test/22_if_test1.out: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/22_if_test1.sy: -------------------------------------------------------------------------------- 1 | // test if-else 2 | int ifElse() { 3 | int a; 4 | a = 5; 5 | if (a == 5) { 6 | a = 25; 7 | } else { 8 | a = a * 2; 9 | } 10 | return (a); 11 | } 12 | 13 | 14 | int main() { 15 | return (ifElse()); 16 | } 17 | -------------------------------------------------------------------------------- /test_sample/functional_test/23_if_test2.out: -------------------------------------------------------------------------------- 1 | 132 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/23_if_test2.sy: -------------------------------------------------------------------------------- 1 | // test if-else else-if 2 | int ifElseElseIf() { 3 | int a; 4 | a = 66; 5 | int b; 6 | b = 8923; 7 | if (a == 5) { 8 | b = 25; 9 | } 10 | else if (a == 10) { 11 | b = 42; 12 | } 13 | else { 14 | b = a * 2; 15 | } 16 | return (b); 17 | } 18 | 19 | int main() { 20 | return (ifElseElseIf()); 21 | } 22 | -------------------------------------------------------------------------------- /test_sample/functional_test/24_if_test3.out: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/24_if_test3.sy: -------------------------------------------------------------------------------- 1 | // test if-if-else 2 | int ififElse() { 3 | int a; 4 | a = 5; 5 | int b; 6 | b = 10; 7 | if(a == 5) 8 | if (b == 10) 9 | a = 25; 10 | else 11 | a = a + 15; 12 | 13 | return (a); 14 | } 15 | 16 | int main(){ 17 | return (ififElse()); 18 | } 19 | -------------------------------------------------------------------------------- /test_sample/functional_test/25_if_test4.out: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/25_if_test4.sy: -------------------------------------------------------------------------------- 1 | // test if-{if-else} 2 | int if_ifElse_() { 3 | int a; 4 | a = 5; 5 | int b; 6 | b = 10; 7 | if(a == 5){ 8 | if (b == 10) 9 | a = 25; 10 | else 11 | a = a + 15; 12 | } 13 | return (a); 14 | } 15 | 16 | int main(){ 17 | return (if_ifElse_()); 18 | } 19 | -------------------------------------------------------------------------------- /test_sample/functional_test/26_if_test5.out: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/26_if_test5.sy: -------------------------------------------------------------------------------- 1 | // test if-{if}-else 2 | int if_if_Else() { 3 | int a; 4 | a = 5; 5 | int b; 6 | b = 10; 7 | if(a == 5){ 8 | if (b == 10) 9 | a = 25; 10 | } 11 | else 12 | a = a + 15; 13 | return (a); 14 | } 15 | 16 | int main(){ 17 | return (if_if_Else()); 18 | } 19 | -------------------------------------------------------------------------------- /test_sample/functional_test/27_while_test1.out: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/27_while_test1.sy: -------------------------------------------------------------------------------- 1 | int doubleWhile() { 2 | int i; 3 | i = 5; 4 | int j; 5 | j = 7; 6 | while (i < 100) { 7 | i = i + 30; 8 | while(j < 100){ 9 | j = j + 6; 10 | } 11 | j = j - 100; 12 | } 13 | return (j); 14 | } 15 | 16 | int main() { 17 | return doubleWhile(); 18 | } 19 | -------------------------------------------------------------------------------- /test_sample/functional_test/28_while_test2.out: -------------------------------------------------------------------------------- 1 | 54 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/28_while_test2.sy: -------------------------------------------------------------------------------- 1 | int FourWhile() { 2 | int a; 3 | a = 5; 4 | int b; 5 | int c; 6 | b = 6; 7 | c = 7; 8 | int d; 9 | d = 10; 10 | while (a < 20) { 11 | a = a + 3; 12 | while(b < 10){ 13 | b = b + 1; 14 | while(c == 7){ 15 | c = c - 1; 16 | while(d < 20){ 17 | d = d + 3; 18 | } 19 | d = d - 1; 20 | } 21 | c = c + 1; 22 | } 23 | b = b - 2; 24 | } 25 | 26 | return (a + (b + d) + c); 27 | } 28 | 29 | int main() { 30 | return FourWhile(); 31 | } 32 | -------------------------------------------------------------------------------- /test_sample/functional_test/29_while_test3.out: -------------------------------------------------------------------------------- 1 | 23 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/29_while_test3.sy: -------------------------------------------------------------------------------- 1 | int g; 2 | int h; 3 | int f; 4 | int e; 5 | int EightWhile() { 6 | int a; 7 | a = 5; 8 | int b; 9 | int c; 10 | b = 6; 11 | c = 7; 12 | int d; 13 | d = 10; 14 | while (a < 20) { 15 | a = a + 3; 16 | while(b < 10){ 17 | b = b + 1; 18 | while(c == 7){ 19 | c = c - 1; 20 | while(d < 20){ 21 | d = d + 3; 22 | while(e > 1){ 23 | e = e-1; 24 | while(f > 2){ 25 | f = f -2; 26 | while(g < 3){ 27 | g = g +10; 28 | while(h < 10){ 29 | h = h + 8; 30 | } 31 | h = h-1; 32 | } 33 | g = g- 8; 34 | } 35 | f = f + 1; 36 | } 37 | e = e + 1; 38 | } 39 | d = d - 1; 40 | } 41 | c = c + 1; 42 | } 43 | b = b - 2; 44 | } 45 | 46 | return (a + (b + d) + c)-(e + d - g + h); 47 | } 48 | 49 | int main() { 50 | g = 1; 51 | h = 2; 52 | e = 4; 53 | f = 6; 54 | return EightWhile(); 55 | } 56 | -------------------------------------------------------------------------------- /test_sample/functional_test/30_while_if_test1.out: -------------------------------------------------------------------------------- 1 | 198 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/30_while_if_test1.sy: -------------------------------------------------------------------------------- 1 | // test while-if 2 | int whileIf() { 3 | int a; 4 | a = 0; 5 | int b; 6 | b = 0; 7 | while (a < 100) { 8 | if (a == 5) { 9 | b = 25; 10 | } 11 | else if (a == 10) { 12 | b = 42; 13 | } 14 | else { 15 | b = a * 2; 16 | } 17 | a = a + 1; 18 | } 19 | return (b); 20 | } 21 | 22 | 23 | int main(){ 24 | return (whileIf()); 25 | } 26 | -------------------------------------------------------------------------------- /test_sample/functional_test/31_while_if_test2.out: -------------------------------------------------------------------------------- 1 | 96 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/31_while_if_test2.sy: -------------------------------------------------------------------------------- 1 | int ifWhile() { 2 | int a; 3 | a = 0; 4 | int b; 5 | b = 3; 6 | if (a == 5) { 7 | while(b == 2){ 8 | b = b + 2; 9 | } 10 | b = b + 25; 11 | } 12 | else 13 | while (a < 5) { 14 | b = b * 2; 15 | a = a + 1; 16 | } 17 | return (b); 18 | } 19 | 20 | 21 | int main(){ 22 | return (ifWhile()); 23 | } 24 | -------------------------------------------------------------------------------- /test_sample/functional_test/32_while_if_test3.out: -------------------------------------------------------------------------------- 1 | 88 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/32_while_if_test3.sy: -------------------------------------------------------------------------------- 1 | int deepWhileBr(int a, int b) { 2 | int c; 3 | c = a + b; 4 | while (c < 75) { 5 | int d; 6 | d = 42; 7 | if (c < 100) { 8 | c = c + d; 9 | if (c > 99) { 10 | int e; 11 | e = d * 2; 12 | if (1 == 1) { 13 | c = e * 2; 14 | } 15 | } 16 | } 17 | } 18 | return (c); 19 | } 20 | 21 | int main() { 22 | int p; 23 | p = 2; 24 | return deepWhileBr(p, p); 25 | } 26 | -------------------------------------------------------------------------------- /test_sample/functional_test/33_func_test1.out: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/33_func_test1.sy: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | int myFunc(int a, int b, int c) { 4 | a = 2; 5 | { 6 | int c; 7 | c = 0; 8 | if (c != 0) { 9 | return 0; 10 | } 11 | } 12 | while (b > 0) { 13 | b = b - 1; 14 | } 15 | return (a)+(b); 16 | } 17 | 18 | int main() { 19 | a = (3); 20 | int b; 21 | b = myFunc(1, 2, 1); 22 | return ((a+b)); 23 | } -------------------------------------------------------------------------------- /test_sample/functional_test/34_func_test2.out: -------------------------------------------------------------------------------- 1 | 127 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/34_func_test2.sy: -------------------------------------------------------------------------------- 1 | int func1() { 2 | int a; 3 | a = 1; 4 | return a; 5 | } 6 | 7 | int func2() { 8 | int a; 9 | a = 2; 10 | return a; 11 | } 12 | 13 | int func3() { 14 | int a; 15 | a = 4; 16 | return a; 17 | } 18 | 19 | int func4() { 20 | int a; 21 | { 22 | int b; 23 | b = 8; 24 | a = b; 25 | } 26 | { 27 | int b; 28 | b = 16; 29 | a = a + b; 30 | } 31 | return a; 32 | } 33 | 34 | int main() { 35 | int a; 36 | int b; 37 | int c; 38 | a = 32; 39 | b = 32; 40 | c = 32; 41 | 42 | return func1() + func2() + func3() + func4() 43 | + a + b + c; 44 | } -------------------------------------------------------------------------------- /test_sample/functional_test/35_array_test.out: -------------------------------------------------------------------------------- 1 | 15 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/35_array_test.sy: -------------------------------------------------------------------------------- 1 | // array as parameter of a function 2 | int field[2]; 3 | 4 | int func(int array[]) { 5 | return array[3 - field[0]]; 6 | } 7 | 8 | int main() { 9 | int i[1]; 10 | int j[3]; 11 | int k; 12 | 13 | field[0] = 1; 14 | field[1] = 2; 15 | 16 | 17 | j[0 + 0] = -1; 18 | j[1] = j[0] - 2; 19 | k = j[1]; 20 | j[2] = 16; 21 | 22 | return func(j) + 2 + k; 23 | } -------------------------------------------------------------------------------- /test_sample/functional_test/36_domain_test.out: -------------------------------------------------------------------------------- 1 | 12 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/36_domain_test.sy: -------------------------------------------------------------------------------- 1 | // duplicate names with global array variables and local int variable 2 | int a[2]; 3 | 4 | int func(int array[]) { 5 | a[0] = 1; 6 | return array[3 - a[0]]; 7 | } 8 | 9 | int main() { 10 | int a; 11 | int array[3]; 12 | array[0] = (-1); 13 | array[1] = 4; 14 | array[2] = 8; 15 | a = func(array); 16 | return (a + array[1]); 17 | } -------------------------------------------------------------------------------- /test_sample/functional_test/38_if_complex_expr.out: -------------------------------------------------------------------------------- 1 | 2 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/38_if_complex_expr.sy: -------------------------------------------------------------------------------- 1 | // Use complex expression in if structure 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | int d; 7 | int result; 8 | a = 5; 9 | b = 5; 10 | c = 1; 11 | d = -2; 12 | result = 2; 13 | if ((d * 1 / 2) < 0 || (a - b) != 0 && (c + 3) % 2 != 0) { 14 | putint(result); 15 | } 16 | if ((d % 2 + 67) < 0 || (a - b) != 0 && (c + 2) % 2 != 0) { 17 | result = 4; 18 | putint(result); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test_sample/functional_test/39_assign_complex_expr.out: -------------------------------------------------------------------------------- 1 | -171 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/39_assign_complex_expr.sy: -------------------------------------------------------------------------------- 1 | // Use complex expression in assign structure 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | int d; 7 | int result; 8 | a = 5; 9 | b = 5; 10 | c = 1; 11 | d = -2; 12 | result = (d * 1 / 2) + (a - b) - -(c + 3) % 2; 13 | putint(result); 14 | result = ((d % 2 + 67) + -(a - b) - -((c + 2) % 2)); 15 | result = result + 3; 16 | putint(result); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /test_sample/functional_test/40_index_complex_expr.out: -------------------------------------------------------------------------------- 1 | 45 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/40_index_complex_expr.sy: -------------------------------------------------------------------------------- 1 | // Use complex expression in assign structure 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | int d; 7 | int result[5]; 8 | a = 5; 9 | b = 5; 10 | c = 1; 11 | d = -2; 12 | result[0] = 1; 13 | result[1] = 2; 14 | result[2] = 3; 15 | result[3] = 4; 16 | result[4] = 5; 17 | int t; 18 | t = result[((d * 1 / 2) + 4 + (a - b) - -(c + 3) % 2) % 5]; 19 | putint(t); 20 | t = result[(((c % 2 + 67) + a - b) - -((c + 2) % 2)) % 5]; 21 | putint(t); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /test_sample/functional_test/41_index_arithmetic_expr.out: -------------------------------------------------------------------------------- 1 | 3 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/41_index_arithmetic_expr.sy: -------------------------------------------------------------------------------- 1 | // Use arithmetic expression as array's index 2 | int main() { 3 | int a; 4 | int b; 5 | int result[3]; 6 | a = 56; 7 | b = 12; 8 | result[0] = 1; 9 | result[1] = 2; 10 | result[2] = 3; 11 | int t; 12 | t = result[(a % b + b) / 5 - 2]; 13 | putint(t); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test_sample/functional_test/42_index_func_ret.out: -------------------------------------------------------------------------------- 1 | -7 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/42_index_func_ret.sy: -------------------------------------------------------------------------------- 1 | // Use return value of a function as array's index 2 | int _getMaxOfAll(int result[], int size) { 3 | int maxNum; 4 | maxNum = -999999; 5 | size = size - 1; 6 | while(size > -1) { 7 | if (result[size] > maxNum) { 8 | maxNum = result[size]; 9 | } 10 | size = size - 1; 11 | } 12 | return maxNum; 13 | } 14 | 15 | int main() { 16 | int result[3]; 17 | result[0] = -2; 18 | result[1] = 2; 19 | result[2] = -7; 20 | int x; 21 | x = result[_getMaxOfAll(result, 3)]; 22 | putint(x); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test_sample/functional_test/43_time_prior_plus.out: -------------------------------------------------------------------------------- 1 | -4 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/43_time_prior_plus.sy: -------------------------------------------------------------------------------- 1 | // Check the priority between time and plus 2 | int main() { 3 | int a; 4 | a = 20; 5 | int b; 6 | b = 5; 7 | int c; 8 | c = 6; 9 | int d; 10 | d = -4; 11 | a = a + c * d - b % (a + d) / a; 12 | putint(a); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test_sample/functional_test/44_add_prior_equal.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/44_add_prior_equal.sy: -------------------------------------------------------------------------------- 1 | // Add is prior than equal and not equal 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | a = 1; 7 | b = 4; 8 | c = 28; 9 | int t; 10 | if (c + a != b) { 11 | t = c % -b; 12 | putint(t); 13 | } 14 | if (b - c == a) { 15 | t = c%b+b; 16 | putint(t); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /test_sample/functional_test/45_equal_prior_logic.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/45_equal_prior_logic.sy: -------------------------------------------------------------------------------- 1 | // Equal is prior to logic operator 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | int d; 7 | a = 10; 8 | b = 6; 9 | c = 4; 10 | d = 5; 11 | int t; 12 | if (b + c == a && d != a / 2) { 13 | t = b + c / d * 2; 14 | putint(t); 15 | } 16 | if (c < 0 || a - c == b && a != d * 2) { 17 | t = 1; 18 | putint(t); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test_sample/functional_test/46_and_prior_or.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/46_and_prior_or.sy: -------------------------------------------------------------------------------- 1 | // And is prior to or 2 | int main () { 3 | int a; 4 | int b; 5 | int c; 6 | int d; 7 | a = 3; 8 | b = 8; 9 | c = -4; 10 | d = 15; 11 | int t; 12 | if (d % (b - a) != 0 && a > 0 || d % 3 == 0 && c > 0) { 13 | t = d + c - -b; 14 | putint(t); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test_sample/functional_test/47_minus_with_sub.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/47_minus_with_sub.sy: -------------------------------------------------------------------------------- 1 | // Use minus with sub in one expr 2 | int main () { 3 | int a; 4 | int b; 5 | a = -2; 6 | b = 1; 7 | a = a - -b + -(a + b) % -(a - b); 8 | putint(a); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test_sample/functional_test/49_decl_in_defn.out: -------------------------------------------------------------------------------- 1 | 12 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/49_decl_in_defn.sy: -------------------------------------------------------------------------------- 1 | int main () { 2 | int a; 3 | a = 12; 4 | int t; 5 | putint(a); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test_sample/functional_test/50_recursion_test1.out: -------------------------------------------------------------------------------- 1 | 24 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/50_recursion_test1.sy: -------------------------------------------------------------------------------- 1 | int fact(int n) { 2 | if (n == 0) { 3 | return 1; 4 | } 5 | int nn; 6 | nn = n-1; 7 | return (n * fact(nn)); 8 | } 9 | 10 | int main() { 11 | int n; 12 | n = 4; 13 | return fact(n); 14 | } 15 | -------------------------------------------------------------------------------- /test_sample/functional_test/51_recursion_test2.out: -------------------------------------------------------------------------------- 1 | 55 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/51_recursion_test2.sy: -------------------------------------------------------------------------------- 1 | int fib(int n) { 2 | if (n == 0) 3 | return 0; 4 | if (n == 1) 5 | return 1; 6 | int p; 7 | p = n - 1; 8 | int q; 9 | q = n - 2; 10 | return fib(p) + fib(q); 11 | } 12 | 13 | int main() { 14 | int tmp; 15 | tmp = 10; 16 | return fib(tmp); 17 | } -------------------------------------------------------------------------------- /test_sample/functional_test/52_recursion_test3.out: -------------------------------------------------------------------------------- 1 | 120 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/52_recursion_test3.sy: -------------------------------------------------------------------------------- 1 | // factorial number 2 | int a; 3 | int r; 4 | int fac(int x) 5 | { 6 | if (x <2) 7 | return 1; 8 | a = x - 1; 9 | r = fac(a); 10 | r = x * r; 11 | return r; 12 | } 13 | 14 | int main(){ 15 | int a; 16 | a = 5; 17 | return fac(a); 18 | } -------------------------------------------------------------------------------- /test_sample/functional_test/53_sort_test1.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/53_sort_test1.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int bubblesort(int arr[]) 3 | { 4 | int i; 5 | int j; 6 | i =0; 7 | while(i < n-1){ 8 | // Last i elements are already in place 9 | j = 0; 10 | while(j < n-i-1){ 11 | if (arr[j] > arr[j+1]) { 12 | // swap(&arr[j], &arr[j+1]); 13 | int tmp; 14 | tmp = arr[j+1]; 15 | arr[j+1] = arr[j]; 16 | arr[j] = tmp; 17 | } 18 | j = j + 1; 19 | } 20 | i = i + 1; 21 | } 22 | return 0; 23 | } 24 | 25 | int main(){ 26 | n = 10; 27 | int a[10]; 28 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 29 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 30 | int i; 31 | i = bubblesort(a); 32 | while (i < n) { 33 | int tmp; 34 | tmp = a[i]; 35 | putint(tmp); 36 | tmp = 10; 37 | putch(tmp); 38 | i = i + 1; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /test_sample/functional_test/54_sort_test2.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/54_sort_test2.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int insertsort(int a[]) 3 | { 4 | int i; 5 | i = 1; 6 | while(i-1&&temp k - 1) 15 | { 16 | j = j - 1; 17 | } 18 | 19 | if(i < j) 20 | { 21 | arr[i] = arr[j]; 22 | i = i + 1; 23 | } 24 | 25 | while(i < j && arr[i] < k) 26 | { 27 | i = i + 1; 28 | } 29 | 30 | if(i < j) 31 | { 32 | arr[j] = arr[i]; 33 | j = j - 1; 34 | } 35 | } 36 | 37 | arr[i] = k; 38 | int tmp; 39 | tmp = i - 1; 40 | tmp = QuickSort(arr, low, tmp); 41 | tmp = i + 1; 42 | tmp = QuickSort(arr, tmp, high); 43 | } 44 | return 0; 45 | } 46 | 47 | int main(){ 48 | n = 10; 49 | int a[10]; 50 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 51 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 52 | int i; 53 | i = 0; 54 | int tmp; 55 | tmp = 9; 56 | i = QuickSort(a, i, tmp); 57 | while (i < n) { 58 | int tmp; 59 | tmp = a[i]; 60 | putint(tmp); 61 | tmp = 10; 62 | putch(tmp); 63 | i = i + 1; 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /test_sample/functional_test/56_sort_test4.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/56_sort_test4.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int select_sort(int A[],int n) 3 | { 4 | int i; 5 | int j; 6 | int min; 7 | i =0; 8 | while(i < n-1) 9 | { 10 | min=i;// 11 | j = i + 1; 12 | while(j < n) 13 | { 14 | if(A[min]>A[j]) 15 | { 16 | min=j; 17 | } 18 | j=j+1; 19 | } 20 | if(min!=i) 21 | { 22 | int tmp; 23 | tmp = A[min]; 24 | A[min] = A[i]; 25 | A[i] = tmp; 26 | } 27 | i = i + 1; 28 | } 29 | return 0; 30 | } 31 | 32 | int main(){ 33 | n = 10; 34 | int a[10]; 35 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 36 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 37 | int i; 38 | i = 0; 39 | i = select_sort(a, n); 40 | while (i < n) { 41 | int tmp; 42 | tmp = a[i]; 43 | putint(tmp); 44 | tmp = 10; 45 | putch(tmp); 46 | i = i + 1; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /test_sample/functional_test/57_sort_test5.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/57_sort_test5.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int swap (int array[], int i, int j){ 3 | int temp; 4 | temp = array[i]; 5 | array[i] = array[j]; 6 | array[j] = temp; 7 | return 0; 8 | } 9 | int heap_ajust(int arr[], int start, int end) { 10 | int dad; 11 | dad = start; 12 | int son; 13 | son = dad * 2 + 1; 14 | while (son < end + 1) { // 15 | if (son < end && arr[son] < arr[son + 1]) 16 | son = son + 1; 17 | if (arr[dad] > arr[son]) 18 | return 0; 19 | else { 20 | dad = swap(arr,dad,son); 21 | dad = son; 22 | son = dad * 2 + 1; 23 | } 24 | } 25 | return 0; 26 | } 27 | int heap_sort(int arr[], int len) { 28 | int i; 29 | int tmp; 30 | i = len / 2 - 1; 31 | while ( i > -1) { 32 | tmp = len - 1; 33 | tmp = heap_ajust(arr, i, tmp); 34 | i = i - 1; 35 | } 36 | i = len - 1; 37 | while ( i > 0) { 38 | int tmp0; 39 | tmp0 = 0; 40 | tmp = swap(arr,tmp0,i); 41 | tmp = i - 1; 42 | tmp = heap_ajust(arr, tmp0, tmp); 43 | i = i-1; 44 | } 45 | return 0; 46 | } 47 | 48 | int main(){ 49 | n = 10; 50 | int a[10]; 51 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 52 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 53 | int i; 54 | i = 0; 55 | i = heap_sort(a, n); 56 | while (i < n) { 57 | int tmp; 58 | tmp = a[i]; 59 | putint(tmp); 60 | tmp = 10; 61 | putch(tmp); 62 | i = i + 1; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /test_sample/functional_test/58_sort_test6.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/58_sort_test6.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | 3 | int counting_sort(int ini_arr[], int sorted_arr[], int n) { 4 | int count_arr[10]; 5 | int i; 6 | int j; 7 | int k; 8 | k = 0; 9 | i = 0; 10 | j = 0; 11 | while(k < 10){ 12 | count_arr[k] = 0; 13 | k = k + 1; 14 | } 15 | while(i < n) 16 | { 17 | count_arr[ini_arr[i]] = count_arr[ini_arr[i]] + 1; 18 | i = i + 1; 19 | } 20 | k = 1; 21 | while(k < 10){ 22 | count_arr[k] = count_arr[k] + count_arr[k - 1]; 23 | k = k + 1; 24 | } 25 | j = n; 26 | while( j > 0){ 27 | count_arr[ini_arr[j - 1]] = count_arr[ini_arr[j - 1]] - 1; 28 | sorted_arr[count_arr[ini_arr[j - 1]]] = ini_arr[j - 1]; 29 | j = j - 1; 30 | } 31 | return 0; 32 | } 33 | 34 | 35 | int main(){ 36 | n = 10; 37 | int a[10]; 38 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 39 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 40 | int i; 41 | i = 0; 42 | int b[10]; 43 | i = counting_sort(a, b, n); 44 | while (i < n) { 45 | int tmp; 46 | tmp = b[i]; 47 | putint(tmp); 48 | tmp = 10; 49 | putch(tmp); 50 | i = i + 1; 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /test_sample/functional_test/59_sort_test7.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 0 12 | -------------------------------------------------------------------------------- /test_sample/functional_test/59_sort_test7.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int Merge(int array[], int low, int middle, int high) 3 | { 4 | int n1; 5 | n1 = middle - low + 1; 6 | int n2; 7 | n2 = high - middle; 8 | int L[10]; 9 | int R[10]; 10 | int i; 11 | i = 0; 12 | int j; 13 | j = 0; 14 | 15 | while(i < n1){ 16 | L[i] = array[i + low]; 17 | i = i + 1; 18 | } 19 | while(j < n2){ 20 | R[j] = array[j + middle +1]; 21 | j = j + 1; 22 | } 23 | i = 0; 24 | j = 0; 25 | int k; 26 | k = low; 27 | while(i!=n1 && j!= n2) 28 | { 29 | if(L[i] < R[j] + 1){ 30 | array[k] = L[i]; 31 | k = k + 1; 32 | i = i + 1; 33 | } 34 | else{ 35 | array[k] = R[j]; 36 | k = k + 1; 37 | j = j + 1; 38 | } 39 | } 40 | while(i < n1){ 41 | array[k] = L[i]; 42 | k = k + 1; 43 | i = i + 1; 44 | 45 | } 46 | while(j < n2){ 47 | array[k] = R[j]; 48 | k = k + 1; 49 | j = j + 1; 50 | } 51 | return 0; 52 | } 53 | 54 | int MergeSort(int array[], int p, int q) 55 | { 56 | if(p < q) 57 | { 58 | int mid; 59 | mid = (p+q)/2; 60 | int tmp; 61 | tmp = MergeSort(array, p, mid); 62 | tmp = mid + 1; 63 | tmp = MergeSort(array, tmp, q); 64 | tmp = Merge(array,p, mid, q); 65 | } 66 | return 0; 67 | } 68 | 69 | int main(){ 70 | n = 10; 71 | int a[10]; 72 | a[0]=4;a[1]=3;a[2]=9;a[3]=2;a[4]=0; 73 | a[5]=1;a[6]=6;a[7]=5;a[8]=7;a[9]=8; 74 | int i; 75 | i = 0; 76 | int tmp; 77 | tmp = n - 1; 78 | i = MergeSort(a, i, tmp); 79 | while (i < n) { 80 | tmp = a[i]; 81 | putint(tmp); 82 | tmp = 10; 83 | putch(tmp); 84 | i = i + 1; 85 | } 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /test_sample/functional_test/60_while_fibonacci.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:7de1555df0c2700329e815b93b32c571c3ea54dc967b89e81ab73b9972b72d1d 3 | size 2 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/60_while_fibonacci.out: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/60_while_fibonacci.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int fib(int p){ 3 | int a; 4 | int b; 5 | int c; 6 | a = 0; 7 | b = 1; 8 | if ( p == 0 ){ 9 | return 0; 10 | } 11 | if ( p == 1 ){ 12 | return 1; 13 | } 14 | while ( p > 1 ){ 15 | c = a + b; 16 | a = b; 17 | b = c; 18 | p = p - 1; 19 | } 20 | return c; 21 | } 22 | int main(){ 23 | n = getint(); 24 | int res; 25 | res = fib( n ); 26 | return res; 27 | } 28 | -------------------------------------------------------------------------------- /test_sample/functional_test/61_rec_fibonacci.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:917df3320d778ddbaa5c5c7742bc4046bf803c36ed2b050f30844ed206783469 3 | size 3 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/61_rec_fibonacci.out: -------------------------------------------------------------------------------- 1 | 55 2 | 55 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/61_rec_fibonacci.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | int f(int x) 3 | { 4 | if(x==1) 5 | return 1; 6 | if(x==2) 7 | return 1; 8 | int a; 9 | int b; 10 | a=x-1; 11 | b=x-2; 12 | int c; 13 | c = f(a)+f(b); 14 | return c; 15 | } 16 | int main() 17 | { 18 | n=getint(); 19 | int t; 20 | int xx; 21 | t=f(n); 22 | putint(t); 23 | int newline; 24 | newline = 10; 25 | putch(newline); 26 | return t; 27 | } 28 | -------------------------------------------------------------------------------- /test_sample/functional_test/62_long_code.out: -------------------------------------------------------------------------------- 1 | 7238926282254275832735749053546579352646394592968929756452279588796363928129015723262728293639586883878996254264273279282290354452459574639657756905929935175115723262728293639586883878996254264273279282290354452459574639657756905929935157232627282936395868838789962542642732792822903544524595746396577569059299351572326272829363958688387899625426427327928229035445245957463965775690592993500014500056800018350002210000119500024160002770003581016102157158658902805545816096195895834243461553125546824662331115043274-77600 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/62_long_code.sy: -------------------------------------------------------------------------------- 1 | // Really long code; 2 | int n; 3 | 4 | int bubblesort(int arr[]) { 5 | int i; 6 | int j; 7 | i =0; 8 | while(i < n-1){ 9 | // Last i elements are already in place 10 | j = 0; 11 | while(j < n-i-1){ 12 | if (arr[j] > arr[j+1]) { 13 | // swap(&arr[j], &arr[j+1]); 14 | int tmp; 15 | tmp = arr[j+1]; 16 | arr[j+1] = arr[j]; 17 | arr[j] = tmp; 18 | } 19 | j = j + 1; 20 | } 21 | i = i + 1; 22 | } 23 | return 0; 24 | } 25 | 26 | int insertsort(int a[]) { 27 | int i; 28 | i = 1; 29 | while(i-1&&temp k - 1) 59 | { 60 | j = j - 1; 61 | } 62 | 63 | if(i < j) 64 | { 65 | arr[i] = arr[j]; 66 | i = i + 1; 67 | } 68 | 69 | while(i < j && arr[i] < k) 70 | { 71 | i = i + 1; 72 | } 73 | 74 | if(i < j) 75 | { 76 | arr[j] = arr[i]; 77 | j = j - 1; 78 | } 79 | } 80 | 81 | arr[i] = k; 82 | int tmp; 83 | tmp = i - 1; 84 | tmp = QuickSort(arr, low, tmp); 85 | tmp = i + 1; 86 | tmp = QuickSort(arr, tmp, high); 87 | } 88 | return 0; 89 | } 90 | 91 | 92 | int getMid(int arr[]) { 93 | int mid; 94 | if (n % 2 == 0) { 95 | mid = n / 2; 96 | return (arr[mid] + arr[mid - 1]) / 2; 97 | } else { 98 | mid = n / 2; 99 | return arr[mid]; 100 | } 101 | } 102 | 103 | int getMost(int arr[]) { 104 | int count[1000]; 105 | int i; 106 | i = 0; 107 | while (i < 1000) { 108 | count[i] = 0; 109 | i = i + 1; 110 | } 111 | i = 0; 112 | int max; 113 | int number; 114 | max = 0; 115 | while (i < n) { 116 | int num; 117 | num = arr[i]; 118 | count[num] = count[num] + 1; 119 | if (count[num] > max) { 120 | max = count[num]; 121 | number = num; 122 | } 123 | i = i + 1; 124 | } 125 | return number; 126 | } 127 | 128 | int revert(int arr[]) { 129 | int temp; 130 | int i; 131 | int j; 132 | i = 0; 133 | j = 0; 134 | while (i < j) { 135 | temp = arr[i]; 136 | arr[i] = arr[j]; 137 | arr[j] = temp; 138 | i = i + 1; 139 | j = j - 1; 140 | } 141 | return 0; 142 | } 143 | 144 | int arrCopy(int src[], int target[]) { 145 | int i; 146 | i = 0; 147 | while (i < n) { 148 | target[i] = src[i]; 149 | i = i + 1; 150 | } 151 | return 0; 152 | } 153 | 154 | int calSum(int arr[], int stride) { 155 | int sum; 156 | sum = 0; 157 | int i; 158 | i = 0; 159 | while (i < n) { 160 | sum = sum + arr[i]; 161 | if (i % stride != stride - 1) { 162 | arr[i] = 0; 163 | } else { 164 | arr[i] = sum; 165 | sum = 0; 166 | } 167 | i = i + 1; 168 | } 169 | return 0; 170 | } 171 | 172 | int avgPooling(int arr[], int stride) { 173 | int sum; 174 | int i; 175 | i = 0; 176 | sum = 0; 177 | int lastnum; 178 | while (i < n) { 179 | if (i < stride - 1) { 180 | sum = sum + arr[i]; 181 | } else if (i == stride - 1) { 182 | lastnum = arr[0]; 183 | arr[0] = sum / stride; 184 | } else { 185 | sum = sum + arr[i] - lastnum; 186 | lastnum = arr[i - stride + 1]; 187 | arr[i - stride + 1] = sum / stride; 188 | } 189 | i = i + 1; 190 | } 191 | i = n - stride + 1; 192 | while (i < n) { 193 | arr[i] = 0; 194 | i = i + 1; 195 | } 196 | return 0; 197 | } 198 | 199 | int main() { 200 | n = 32; 201 | int arr[32]; 202 | int result[32]; 203 | arr[0] = 7; 204 | arr[1] = 23; 205 | arr[2] = 89; 206 | arr[3] = 26; 207 | arr[4] = 282; 208 | arr[5] = 254; 209 | arr[6] = 27; 210 | arr[7] = 5; 211 | arr[8] = 83; 212 | arr[9] = 273; 213 | arr[10] = 574; 214 | arr[11] = 905; 215 | arr[12] = 354; 216 | arr[13] = 657; 217 | arr[14] = 935; 218 | arr[15] = 264; 219 | arr[16] = 639; 220 | arr[17] = 459; 221 | arr[18] = 29; 222 | arr[19] = 68; 223 | arr[20] = 929; 224 | arr[21] = 756; 225 | arr[22] = 452; 226 | arr[23] = 279; 227 | arr[24] = 58; 228 | arr[25] = 87; 229 | arr[26] = 96; 230 | arr[27] = 36; 231 | arr[28] = 39; 232 | arr[29] = 28; 233 | arr[30] = 1; 234 | arr[31] = 290; 235 | int t; 236 | t = arrCopy(arr, result); 237 | t = revert(result); 238 | int i; 239 | i = 0; 240 | while (i < 32) { 241 | t = result[i]; 242 | putint(t); 243 | i = i + 1; 244 | } 245 | t = bubblesort(result); 246 | i = 0; 247 | while (i < 32) { 248 | t = result[i]; 249 | putint(t); 250 | i = i + 1; 251 | } 252 | t = getMid(result); 253 | putint(t); 254 | t = getMost(result); 255 | putint(t); 256 | 257 | t = arrCopy(arr, result); 258 | t = bubblesort(result); 259 | i = 0; 260 | while (i < 32) { 261 | t = result[i]; 262 | putint(t); 263 | i = i + 1; 264 | } 265 | 266 | t = arrCopy(arr, result); 267 | t = insertsort(result); 268 | i = 0; 269 | while (i < 32) { 270 | t = result[i]; 271 | putint(t); 272 | i = i + 1; 273 | } 274 | 275 | t = arrCopy(arr, result); 276 | i = 0; 277 | t = 31; 278 | t = QuickSort(result, i, t); 279 | while (i < 32) { 280 | t = result[i]; 281 | putint(t); 282 | i = i + 1; 283 | } 284 | 285 | t = arrCopy(arr, result); 286 | t = calSum(result, 4); 287 | i = 0; 288 | while (i < 32) { 289 | t = result[i]; 290 | putint(t); 291 | i = i + 1; 292 | } 293 | 294 | t = arrCopy(arr, result); 295 | t = avgPooling(result, 3); 296 | i = 0; 297 | while (i < 32) { 298 | t = result[i]; 299 | putint(t); 300 | i = i + 1; 301 | } 302 | return 0; 303 | } 304 | -------------------------------------------------------------------------------- /test_sample/functional_test/63_simple_atoi.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:991646763de034418da6b49a1928a6b146b5fa39000ebda600a894df310e1c3f 3 | size 9 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/63_simple_atoi.out: -------------------------------------------------------------------------------- 1 | -258425 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/63_simple_atoi.sy: -------------------------------------------------------------------------------- 1 | // Simple atoi program 2 | int atoi_(int src[]) { 3 | int s; 4 | s = 0; 5 | int isMinus; 6 | isMinus = 1; 7 | int i; 8 | i = 0; 9 | while(src[i] == 32) { // 跳过空白符 10 | i = i + 1; 11 | } 12 | 13 | if(src[i] == 43 || src[i] == 45) { 14 | if(src[i] == 45) { 15 | isMinus = -1; 16 | } 17 | i = i + 1; 18 | } else if (src[i] < 48 || src[i] > 57) { 19 | //如果第一位既不是符号也不是数字,直接返回异常值 20 | s = 2147483647; 21 | return s; 22 | } 23 | 24 | while (src[i] != 0 && src[i] > 47 && src[i] < 58) { 25 | s = s * 10 + src[i] - 48; 26 | i = i + 1; 27 | } 28 | return s * isMinus; 29 | } 30 | 31 | int main () { 32 | int string[500]; 33 | int temp; 34 | temp = 0; 35 | int i; 36 | i = 0; 37 | while (temp != 10) { 38 | temp = getch(); 39 | string[i] = temp; 40 | i = i + 1; 41 | } 42 | string[i] = 0; 43 | i = atoi_(string); 44 | putint(i); 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /test_sample/functional_test/64_alpha_count.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:5b70913ee9643a045ebbac5bebfa4f09d230a18484af4a62692dbb7d68741077 3 | size 355 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/64_alpha_count.out: -------------------------------------------------------------------------------- 1 | 299 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/64_alpha_count.sy: -------------------------------------------------------------------------------- 1 | // Count how many alpha in a string 2 | int main() { 3 | int string[500]; 4 | int temp; 5 | int i; 6 | int count; 7 | count = 0; 8 | i = 0; 9 | temp = 0; 10 | while (temp != 10) { 11 | temp = getch(); 12 | if (temp > 40 && temp < 91 || temp > 96 && temp < 123) { 13 | count = count + 1; 14 | } 15 | i = i + 1; 16 | } 17 | putint(count); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /test_sample/functional_test/65_word_count.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:5b70913ee9643a045ebbac5bebfa4f09d230a18484af4a62692dbb7d68741077 3 | size 355 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/65_word_count.out: -------------------------------------------------------------------------------- 1 | 56 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/65_word_count.sy: -------------------------------------------------------------------------------- 1 | // Count all the word in the string 2 | 3 | int wc(int string[], int n) { 4 | int inWord; 5 | int i; 6 | int count; 7 | i = 0; 8 | inWord = 0; 9 | count = 0; 10 | while (i < n) { 11 | if (string[i] != 32) { 12 | if (inWord == 0) { 13 | count = count + 1; 14 | inWord = 1; 15 | } 16 | } else { 17 | inWord = 0; 18 | } 19 | i = i + 1; 20 | } 21 | return count; 22 | } 23 | 24 | int main() { 25 | int string[500]; 26 | int temp; 27 | int i; 28 | i = 0; 29 | temp = 0; 30 | while (temp != 10) { 31 | temp = getch(); 32 | string[i] = temp; 33 | i = i + 1; 34 | } 35 | temp = wc(string, i); 36 | putint(temp); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /test_sample/functional_test/66_go_upstairs.out: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/66_go_upstairs.sy: -------------------------------------------------------------------------------- 1 | int climbStairs(int n) { 2 | if(n < 4) 3 | return n; 4 | int dp[10]; 5 | dp[0] = 0; 6 | dp[1] = 1; 7 | dp[2] = 2; 8 | int i; 9 | i = 3; 10 | while(i -1 && s[c] == 0){ 7 | c = c - 1; 8 | } 9 | if(c == -1) 10 | return 0; 11 | int i; 12 | i = c; 13 | while(i > -1){ 14 | if(s[i] == 0) 15 | return n - i - 1 - (n - 1 - c); 16 | i = i - 1; 17 | } 18 | return c - i; 19 | } 20 | int main(){ 21 | int res; 22 | int a[10]; 23 | a[0]=-4;a[1]=3;a[2]=9;a[3]=-2;a[4]=0; 24 | a[5]=1;a[6]=-6;a[7]=5;a[8]=7;a[9]=8; 25 | res = 10; 26 | res = lengthOfLastWord(a, res); 27 | return res; 28 | } -------------------------------------------------------------------------------- /test_sample/functional_test/70_multi.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/70_multi.sy: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | //newline=10; 4 | int i; 5 | int sum; 6 | sum=0; 7 | //m = 1478; 8 | //int t; 9 | i=0; 10 | while(i<21) 11 | { 12 | sum=sum*i; 13 | i=i+1; 14 | } 15 | 16 | putint(sum); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /test_sample/functional_test/71_max_subsequence_sum.out: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/71_max_subsequence_sum.sy: -------------------------------------------------------------------------------- 1 | int maxSubArray(int nums[], int n) { 2 | if(n == 0) 3 | return 0; 4 | if(n == 1) 5 | return nums[0]; 6 | int sum; 7 | sum = nums[0]; 8 | int max; 9 | max = sum; 10 | int i; 11 | i = 1; 12 | while(i < n){ 13 | if(sum < 0) 14 | sum = 0; 15 | sum = sum + nums[i]; 16 | if(max < sum) 17 | max = sum; 18 | i = i + 1; 19 | } 20 | return max; 21 | } 22 | int main(){ 23 | int res; 24 | int a[10]; 25 | a[0]=-4;a[1]=3;a[2]=9;a[3]=-2;a[4]=0; 26 | a[5]=1;a[6]=-6;a[7]=5;a[8]=7;a[9]=8; 27 | res = 10; 28 | res = maxSubArray(a, res); 29 | return res; 30 | } -------------------------------------------------------------------------------- /test_sample/functional_test/72_enum.out: -------------------------------------------------------------------------------- 1 | 01000 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/72_enum.sy: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i; 4 | int j; 5 | int k; 6 | int t; 7 | i=0;j=0;k=0; 8 | while(i<21) 9 | { 10 | while(j<101-i) 11 | { 12 | k=100-i-j; 13 | if(5*i+1*j+k/2==100) 14 | { 15 | putint(i); 16 | putint(j); 17 | putint(k); 18 | t=10; 19 | putch(t); 20 | } 21 | j=j+1; 22 | } 23 | i=i+1; 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /test_sample/functional_test/73_exchange_value.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b51c65be148f7d5dbca6f2e844797c9df6433b8cc470a4d76e18c8962bad8bfa 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/73_exchange_value.out: -------------------------------------------------------------------------------- 1 | 20 2 | 4 3 | 0 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/73_exchange_value.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | 3 | int main() 4 | { 5 | //newline=10; 6 | int i; 7 | int j; 8 | //m = 1478; 9 | //int t; 10 | i=getint(); 11 | j=getint(); 12 | int temp; 13 | temp=i; 14 | i=j; 15 | j=temp; 16 | 17 | putint(i); 18 | temp = 10; 19 | putch(temp); 20 | putint(j); 21 | temp = 10; 22 | putch(temp); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /test_sample/functional_test/74_itera_sqrt.out: -------------------------------------------------------------------------------- 1 | 20 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/74_itera_sqrt.sy: -------------------------------------------------------------------------------- 1 | int fsqrt(int a) 2 | { 3 | int x0=0; 4 | int x1; 5 | x1=a/2; 6 | while(x0-x1!=0) 7 | { 8 | x0=x1; 9 | x1=(x0+a/x0); 10 | x1=x1/2; 11 | } 12 | 13 | return x1; 14 | 15 | } 16 | 17 | int main() 18 | { 19 | int a; 20 | a=400; 21 | int res; 22 | res=fsqrt(a); 23 | putint(res); 24 | res = 10; 25 | putch(res); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /test_sample/functional_test/75_max_container.out: -------------------------------------------------------------------------------- 1 | 56 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/75_max_container.sy: -------------------------------------------------------------------------------- 1 | int maxArea(int height[], int n) { 2 | int i; 3 | int j; 4 | i = 0; 5 | j = n - 1; 6 | int max_val; 7 | max_val = -1; 8 | while(i < j){ 9 | int area; 10 | if(height[i] < height[j]) 11 | area = (j - i) * height[i]; 12 | else 13 | area = (j - i) * height[j]; 14 | if(area > max_val){ 15 | max_val = area; 16 | } 17 | if(height[i] > height[j]) 18 | j = j - 1; 19 | else 20 | i = i + 1; 21 | } 22 | return max_val; 23 | } 24 | 25 | int main(){ 26 | int res; 27 | int a[10]; 28 | a[0]=3;a[1]=3;a[2]=9;a[3]=0;a[4]=0; 29 | a[5]=1;a[6]=1;a[7]=5;a[8]=7;a[9]=8; 30 | res = 10; 31 | res = maxArea(a, res); 32 | return res; 33 | } -------------------------------------------------------------------------------- /test_sample/functional_test/76_int_factor_sum.out: -------------------------------------------------------------------------------- 1 | 172 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/76_int_factor_sum.sy: -------------------------------------------------------------------------------- 1 | int N; 2 | 3 | int newline; 4 | 5 | int factor(int n ) 6 | { 7 | int i; 8 | int sum; 9 | sum=0; 10 | i=1; 11 | while(i -1){ 20 | j=n-2; 21 | while(j > -1){ 22 | dp[i*3+j] = dp[(i+1)*3+j] + dp[i*3+j+1]; 23 | j = j - 1; 24 | } 25 | i = i - 1; 26 | } 27 | return dp[0]; 28 | } 29 | int main(){ 30 | int res; 31 | int n; 32 | n=3; 33 | res = uniquePaths(n, n); 34 | return res; 35 | } -------------------------------------------------------------------------------- /test_sample/functional_test/78_decbinoct.out: -------------------------------------------------------------------------------- 1 | 110010000 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/78_decbinoct.sy: -------------------------------------------------------------------------------- 1 | int dec2bin(int a) 2 | { 3 | int res; 4 | int k; 5 | int i; 6 | int temp; 7 | res=0; 8 | k=1; 9 | temp=a; 10 | while(temp!=0) 11 | { 12 | i=temp%2; 13 | res=k*i+res; 14 | k=k*10; 15 | temp=temp/2; 16 | } 17 | return res; 18 | 19 | } 20 | 21 | int main() 22 | { 23 | int a; 24 | a=400; 25 | int res; 26 | res=dec2bin(a); 27 | putint(res); 28 | res = 10; 29 | putch(res); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /test_sample/functional_test/79_gcd.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b51c65be148f7d5dbca6f2e844797c9df6433b8cc470a4d76e18c8962bad8bfa 3 | size 4 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/79_gcd.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /test_sample/functional_test/79_gcd.sy: -------------------------------------------------------------------------------- 1 | int n; 2 | 3 | int gcd(int m,int n) 4 | { 5 | int t; 6 | int r; 7 | 8 | if(m n - 2) 5 | return 1; 6 | int dp[10]; 7 | int i; 8 | i = 0; 9 | while(i -1){ 16 | int j; 17 | if(nums[i] < n - 1 - i){ 18 | j = nums[i]; 19 | } 20 | else 21 | j = n - 1 - i; 22 | while(j > -1){ 23 | if(dp[i+j] != 0){ 24 | dp[i] = 1; 25 | } 26 | j = j - 1; 27 | } 28 | i = i - 1; 29 | } 30 | 31 | return dp[0]; 32 | } 33 | int main(){ 34 | int res; 35 | int a[10]; 36 | a[0]=3;a[1]=3;a[2]=9;a[3]=0;a[4]=0; 37 | a[5]=1;a[6]=1;a[7]=5;a[8]=7;a[9]=8; 38 | res = 10; 39 | res = canJump(a, res); 40 | return res; 41 | } -------------------------------------------------------------------------------- /test_sample/functional_test/82_int_split.out: -------------------------------------------------------------------------------- 1 | 1 2 | 4 3 | 7 4 | 8 5 | 0 -------------------------------------------------------------------------------- /test_sample/functional_test/82_int_split.sy: -------------------------------------------------------------------------------- 1 | int N; 2 | 3 | int newline; 4 | 5 | int split(int n ,int a[]) 6 | { 7 | int i; 8 | i=N-1; 9 | while(i!=-1) 10 | { 11 | a[i]=n%10; 12 | n=n/10; 13 | i=i-1; 14 | 15 | } 16 | 17 | return 0; 18 | } 19 | 20 | int main() 21 | { 22 | N=4; 23 | newline=10; 24 | int i; 25 | int m; 26 | int b[4]; 27 | m = 1478; 28 | m = split(m,b); 29 | int t; 30 | i=0; 31 | while(i<4) 32 | { 33 | t=b[i]; 34 | putint(t); 35 | putch(newline); 36 | i=i+1; 37 | 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /test_sample/functional_test/83_enc_dec.out: -------------------------------------------------------------------------------- 1 | 401 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/83_enc_dec.sy: -------------------------------------------------------------------------------- 1 | int enc(int a) 2 | { 3 | if(a>25) 4 | a=a+60; 5 | else 6 | { 7 | a=a-15; 8 | } 9 | 10 | return a; 11 | 12 | } 13 | 14 | int dec(int a) 15 | { 16 | if (a>85) 17 | a=a-59; 18 | else 19 | { 20 | a=a+14; 21 | } 22 | 23 | return a; 24 | 25 | } 26 | 27 | int main() 28 | { 29 | int a; 30 | a=400; 31 | int res; 32 | res=enc(a); 33 | res=dec(res); 34 | putint(res); 35 | res = 10; 36 | putch(res); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /test_sample/functional_test/84_palindrome_number.out: -------------------------------------------------------------------------------- 1 | 1221 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/84_palindrome_number.sy: -------------------------------------------------------------------------------- 1 | int palindrome(int n) 2 | { 3 | int a[4]; 4 | int j; 5 | int flag; 6 | j=0; 7 | while(j<4) 8 | { 9 | a[j]=n%10; 10 | n=n/10; 11 | j=j+1; 12 | } 13 | 14 | if(a[0]==a[3] && a[1]==a[2]) 15 | { 16 | flag=1; 17 | }else{ 18 | flag=0; 19 | } 20 | return flag; 21 | } 22 | 23 | int main() 24 | { 25 | int test; 26 | test=1221; 27 | int flag; 28 | flag=palindrome(test); 29 | if(flag==1) 30 | putint(test); 31 | else 32 | { 33 | flag = 0; 34 | putint(flag); 35 | } 36 | 37 | flag = 10; 38 | putch(flag); 39 | 40 | return 0; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /test_sample/functional_test/86_bin_search.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:e7f6c011776e8db7cd330b54174fd76f7d0216b612387a5ffcfb81e6f0919683 3 | size 1 4 | -------------------------------------------------------------------------------- /test_sample/functional_test/86_bin_search.out: -------------------------------------------------------------------------------- 1 | 6 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/functional_test/86_bin_search.sy: -------------------------------------------------------------------------------- 1 | 2 | int main() 3 | { 4 | //newline=10; 5 | int i; 6 | int sum; 7 | int a[10]; 8 | sum=0; 9 | //m = 1478; 10 | //int t; 11 | i=0; 12 | while(i<10) 13 | { 14 | a[i]=i+1; 15 | i=i+1; 16 | } 17 | int x; 18 | int high; 19 | int low; 20 | int mid; 21 | int n; 22 | n=10; 23 | x=getint(); 24 | high=n-1; 25 | low=0; 26 | mid=(high+low)/2; 27 | while(a[mid]!=x && low < high) 28 | { 29 | mid=(high+low)/2; 30 | if(xa[p]&&pp) 14 | { 15 | a[i]=a[i-1]; 16 | a[p]=x; 17 | i=i-1; 18 | 19 | } 20 | 21 | return 0; 22 | } 23 | 24 | int main() 25 | { 26 | N=10; 27 | int a[11]; 28 | //a[0]=1; 29 | a[0]=1; 30 | a[1]=3; 31 | a[2]=4; 32 | a[3]=7; 33 | a[4]=8; 34 | a[5]=11; 35 | a[6]=13; 36 | a[7]=18; 37 | a[8]=56; 38 | a[9]=78; 39 | int x; 40 | int i; 41 | i=0; 42 | x=getint(); 43 | x=insert(a,x); 44 | //while() 45 | while(i= 10000) return 0; 26 | 27 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 != d){ 28 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 == 0) 29 | if (d == 1) 30 | v = x[pos % bitcount]; 31 | 32 | if (a[pos / bitcount] / x[pos % bitcount] % 2 == 1) 33 | if (d == 0) 34 | v = v - x[pos % bitcount]; 35 | } 36 | 37 | a[pos / bitcount] = a[pos / bitcount] + v; 38 | return 0; 39 | } 40 | 41 | int seed[3] = {19971231, 19981013, 1000000000 + 7}; 42 | int staticvalue = 0; 43 | 44 | int rand(){ 45 | staticvalue = staticvalue * seed[0] + seed[1]; 46 | staticvalue = staticvalue % seed[2]; 47 | if (staticvalue < 0) staticvalue = seed[2] + staticvalue; 48 | return staticvalue; 49 | } 50 | 51 | int a[10000] = {}; 52 | int main(){ 53 | 54 | int n = getint(); 55 | staticvalue = getint(); 56 | starttime(); 57 | int x, y; 58 | while (n > 0){ 59 | n = n - 1; 60 | x = rand() % 300000; 61 | y = rand() % 2; 62 | set(a, x, y); 63 | } 64 | stoptime(); 65 | putarray(10000, a); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /test_sample/performance_test/00_bitset2.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f21f27a56a7a4e72ff823dabbedc852fc04fc31bf3cfab4ccf4bf78981ffd9bc 3 | size 13 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/00_bitset2.sy: -------------------------------------------------------------------------------- 1 | int set(int a[], int pos, int d){ 2 | const int bitcount = 30; 3 | int x[bitcount + 1] = {}; 4 | 5 | x[0] = 1; 6 | x[1] = x[0] * 2; 7 | x[2] = x[1] * 2; 8 | x[3] = x[2] * 2; 9 | x[4] = x[3] * 2; 10 | x[5] = x[4] * 2; 11 | x[6] = x[5] * 2; 12 | x[7] = x[6] * 2; 13 | x[8] = x[7] * 2; 14 | x[9] = x[8] * 2; 15 | x[10] = x[9] * 2; 16 | 17 | int i = 10; 18 | while (i < bitcount){ 19 | i = i + 1; 20 | x[i] = x[i - 1] * 2; 21 | } 22 | 23 | int v = 0; 24 | 25 | if (pos / bitcount >= 10000) return 0; 26 | 27 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 != d){ 28 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 == 0) 29 | if (d == 1) 30 | v = x[pos % bitcount]; 31 | 32 | if (a[pos / bitcount] / x[pos % bitcount] % 2 == 1) 33 | if (d == 0) 34 | v = v - x[pos % bitcount]; 35 | } 36 | 37 | a[pos / bitcount] = a[pos / bitcount] + v; 38 | return 0; 39 | } 40 | 41 | int seed[3] = {19971231, 19981013, 1000000000 + 7}; 42 | int staticvalue = 0; 43 | 44 | int rand(){ 45 | staticvalue = staticvalue * seed[0] + seed[1]; 46 | staticvalue = staticvalue % seed[2]; 47 | if (staticvalue < 0) staticvalue = seed[2] + staticvalue; 48 | return staticvalue; 49 | } 50 | 51 | int a[10000] = {}; 52 | int main(){ 53 | 54 | int n = getint(); 55 | staticvalue = getint(); 56 | starttime(); 57 | int x, y; 58 | while (n > 0){ 59 | n = n - 1; 60 | x = rand() % 300000; 61 | y = rand() % 2; 62 | set(a, x, y); 63 | } 64 | stoptime(); 65 | putarray(10000, a); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /test_sample/performance_test/00_bitset3.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f7ebe8fd182227ab1f34a902330ae801de4f4e6e55204137d19125eec1156dfd 3 | size 10 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/00_bitset3.sy: -------------------------------------------------------------------------------- 1 | int set(int a[], int pos, int d){ 2 | const int bitcount = 30; 3 | int x[bitcount + 1] = {}; 4 | 5 | x[0] = 1; 6 | x[1] = x[0] * 2; 7 | x[2] = x[1] * 2; 8 | x[3] = x[2] * 2; 9 | x[4] = x[3] * 2; 10 | x[5] = x[4] * 2; 11 | x[6] = x[5] * 2; 12 | x[7] = x[6] * 2; 13 | x[8] = x[7] * 2; 14 | x[9] = x[8] * 2; 15 | x[10] = x[9] * 2; 16 | 17 | int i = 10; 18 | while (i < bitcount){ 19 | i = i + 1; 20 | x[i] = x[i - 1] * 2; 21 | } 22 | 23 | int v = 0; 24 | 25 | if (pos / bitcount >= 10000) return 0; 26 | 27 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 != d){ 28 | if (a[pos / bitcount] / (x[pos % bitcount]) % 2 == 0) 29 | if (d == 1) 30 | v = x[pos % bitcount]; 31 | 32 | if (a[pos / bitcount] / x[pos % bitcount] % 2 == 1) 33 | if (d == 0) 34 | v = v - x[pos % bitcount]; 35 | } 36 | 37 | a[pos / bitcount] = a[pos / bitcount] + v; 38 | return 0; 39 | } 40 | 41 | int seed[3] = {19971231, 19981013, 1000000000 + 7}; 42 | int staticvalue = 0; 43 | 44 | int rand(){ 45 | staticvalue = staticvalue * seed[0] + seed[1]; 46 | staticvalue = staticvalue % seed[2]; 47 | if (staticvalue < 0) staticvalue = seed[2] + staticvalue; 48 | return staticvalue; 49 | } 50 | 51 | int a[10000] = {}; 52 | int main(){ 53 | 54 | int n = getint(); 55 | staticvalue = getint(); 56 | starttime(); 57 | int x, y; 58 | while (n > 0){ 59 | n = n - 1; 60 | x = rand() % 300000; 61 | y = rand() % 2; 62 | set(a, x, y); 63 | } 64 | stoptime(); 65 | putarray(10000, a); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm1.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:c45f72720c2551259844703824379e3ab6472576f2935dc570a4a6f1ea0c2236 3 | size 1853076 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm1.out: -------------------------------------------------------------------------------- 1 | -799731242 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm1.sy: -------------------------------------------------------------------------------- 1 | const int N = 1024; 2 | 3 | void mm(int n, int A[][N], int B[][N], int C[][N]){ 4 | int i, j, k; 5 | 6 | i = 0; j = 0; 7 | while (i < n){ 8 | j = 0; 9 | while (j < n){ 10 | C[i][j] = 0; 11 | j = j + 1; 12 | } 13 | i = i + 1; 14 | } 15 | 16 | i = 0; j = 0; k = 0; 17 | 18 | while (k < n){ 19 | i = 0; 20 | while (i < n){ 21 | if (A[i][k] == 0){ 22 | i = i + 1; 23 | continue; 24 | } 25 | j = 0; 26 | while (j < n){ 27 | C[i][j] = C[i][j] + A[i][k] * B[k][j]; 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | k = k + 1; 33 | } 34 | } 35 | 36 | int A[N][N]; 37 | int B[N][N]; 38 | int C[N][N]; 39 | 40 | int main(){ 41 | int n = getint(); 42 | int i, j; 43 | 44 | i = 0; 45 | j = 0; 46 | while (i < n){ 47 | j = 0; 48 | while (j < n){ 49 | A[i][j] = getint(); 50 | j = j + 1; 51 | } 52 | i = i + 1; 53 | } 54 | i = 0; 55 | j = 0; 56 | while (i < n){ 57 | j = 0; 58 | while (j < n){ 59 | B[i][j] = getint(); 60 | j = j + 1; 61 | } 62 | i = i + 1; 63 | } 64 | 65 | starttime(); 66 | 67 | i = 0; 68 | while (i < 5){ 69 | mm(n, A, B, C); 70 | mm(n, A, C, B); 71 | i = i + 1; 72 | } 73 | 74 | int ans = 0; 75 | i = 0; 76 | while (i < n){ 77 | j = 0; 78 | while (j < n){ 79 | ans = ans + B[i][j]; 80 | j = j + 1; 81 | } 82 | i = i + 1; 83 | } 84 | stoptime(); 85 | putint(ans); 86 | putch(10); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm2.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:3c7a518f7571d96b0561d135478e0de890135b9c2a6f8820e9687ffbd2a4fd1b 3 | size 1757648 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm2.out: -------------------------------------------------------------------------------- 1 | 1691748973 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm2.sy: -------------------------------------------------------------------------------- 1 | const int N = 1024; 2 | 3 | void mm(int n, int A[][N], int B[][N], int C[][N]){ 4 | int i, j, k; 5 | 6 | i = 0; j = 0; 7 | while (i < n){ 8 | j = 0; 9 | while (j < n){ 10 | C[i][j] = 0; 11 | j = j + 1; 12 | } 13 | i = i + 1; 14 | } 15 | 16 | i = 0; j = 0; k = 0; 17 | 18 | while (k < n){ 19 | i = 0; 20 | while (i < n){ 21 | if (A[i][k] == 0){ 22 | i = i + 1; 23 | continue; 24 | } 25 | j = 0; 26 | while (j < n){ 27 | C[i][j] = C[i][j] + A[i][k] * B[k][j]; 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | k = k + 1; 33 | } 34 | } 35 | 36 | int A[N][N]; 37 | int B[N][N]; 38 | int C[N][N]; 39 | 40 | int main(){ 41 | int n = getint(); 42 | int i, j; 43 | 44 | i = 0; 45 | j = 0; 46 | while (i < n){ 47 | j = 0; 48 | while (j < n){ 49 | A[i][j] = getint(); 50 | j = j + 1; 51 | } 52 | i = i + 1; 53 | } 54 | i = 0; 55 | j = 0; 56 | while (i < n){ 57 | j = 0; 58 | while (j < n){ 59 | B[i][j] = getint(); 60 | j = j + 1; 61 | } 62 | i = i + 1; 63 | } 64 | 65 | starttime(); 66 | 67 | i = 0; 68 | while (i < 5){ 69 | mm(n, A, B, C); 70 | mm(n, A, C, B); 71 | i = i + 1; 72 | } 73 | 74 | int ans = 0; 75 | i = 0; 76 | while (i < n){ 77 | j = 0; 78 | while (j < n){ 79 | ans = ans + B[i][j]; 80 | j = j + 1; 81 | } 82 | i = i + 1; 83 | } 84 | stoptime(); 85 | putint(ans); 86 | putch(10); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm3.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b1cdeafbe1e9e0aedc1e47e6ba636ebd9b1b084fb5cf6f92210613831c8432f7 3 | size 1568272 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm3.out: -------------------------------------------------------------------------------- 1 | -1629892954 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/01_mm3.sy: -------------------------------------------------------------------------------- 1 | const int N = 1024; 2 | 3 | void mm(int n, int A[][N], int B[][N], int C[][N]){ 4 | int i, j, k; 5 | 6 | i = 0; j = 0; 7 | while (i < n){ 8 | j = 0; 9 | while (j < n){ 10 | C[i][j] = 0; 11 | j = j + 1; 12 | } 13 | i = i + 1; 14 | } 15 | 16 | i = 0; j = 0; k = 0; 17 | 18 | while (k < n){ 19 | i = 0; 20 | while (i < n){ 21 | if (A[i][k] == 0){ 22 | i = i + 1; 23 | continue; 24 | } 25 | j = 0; 26 | while (j < n){ 27 | C[i][j] = C[i][j] + A[i][k] * B[k][j]; 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | k = k + 1; 33 | } 34 | } 35 | 36 | int A[N][N]; 37 | int B[N][N]; 38 | int C[N][N]; 39 | 40 | int main(){ 41 | int n = getint(); 42 | int i, j; 43 | 44 | i = 0; 45 | j = 0; 46 | while (i < n){ 47 | j = 0; 48 | while (j < n){ 49 | A[i][j] = getint(); 50 | j = j + 1; 51 | } 52 | i = i + 1; 53 | } 54 | i = 0; 55 | j = 0; 56 | while (i < n){ 57 | j = 0; 58 | while (j < n){ 59 | B[i][j] = getint(); 60 | j = j + 1; 61 | } 62 | i = i + 1; 63 | } 64 | 65 | starttime(); 66 | 67 | i = 0; 68 | while (i < 5){ 69 | mm(n, A, B, C); 70 | mm(n, A, C, B); 71 | i = i + 1; 72 | } 73 | 74 | int ans = 0; 75 | i = 0; 76 | while (i < n){ 77 | j = 0; 78 | while (j < n){ 79 | ans = ans + B[i][j]; 80 | j = j + 1; 81 | } 82 | i = i + 1; 83 | } 84 | stoptime(); 85 | putint(ans); 86 | putch(10); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv1.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:ba2ff3b1a439e823aa58528414900c21157d5bc77781c9c4af19ba558c6fa8e8 3 | size 8209456 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv1.sy: -------------------------------------------------------------------------------- 1 | int x; 2 | 3 | const int N = 2010; 4 | 5 | void mv(int n, int A[][N], int b[], int res[]){ 6 | int x, y; 7 | y = 0; 8 | x = 11; 9 | int i, j; 10 | 11 | i = 0; 12 | while(i < n){ 13 | res[i] = 0; 14 | i = i + 1; 15 | } 16 | 17 | i = 0; 18 | j = 0; 19 | while (i < n){ 20 | j = 0; 21 | while (j < n){ 22 | if (A[i][j] == 0){ 23 | x = x * b[i] + b[j]; 24 | y = y - x; 25 | }else{ 26 | res[i] = res[i] + A[i][j] * b[j]; 27 | } 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | } 33 | 34 | int A[N][N]; 35 | int B[N]; 36 | int C[N]; 37 | 38 | int main(){ 39 | int n = getint(); 40 | int i, j; 41 | 42 | i = 0; 43 | 44 | while (i < n){ 45 | j = 0; 46 | while (j < n){ 47 | A[i][j] = getint(); 48 | j = j + 1; 49 | } 50 | i = i + 1; 51 | } 52 | 53 | i = 0; 54 | while (i < n){ 55 | B[i] = getint(); 56 | i = i + 1; 57 | } 58 | 59 | starttime(); 60 | 61 | i = 0; 62 | while (i < 50){ 63 | mv(n, A, B, C); 64 | mv(n, A, C, B); 65 | i = i + 1; 66 | } 67 | stoptime(); 68 | 69 | putarray(n, C); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv2.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:ebe2d975d979f9415479d178540e6f60c87f464e5b850691a037b811e9f9ef88 3 | size 8046395 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv2.sy: -------------------------------------------------------------------------------- 1 | int x; 2 | 3 | const int N = 2010; 4 | 5 | void mv(int n, int A[][N], int b[], int res[]){ 6 | int x, y; 7 | y = 0; 8 | x = 11; 9 | int i, j; 10 | 11 | i = 0; 12 | while(i < n){ 13 | res[i] = 0; 14 | i = i + 1; 15 | } 16 | 17 | i = 0; 18 | j = 0; 19 | while (i < n){ 20 | j = 0; 21 | while (j < n){ 22 | if (A[i][j] == 0){ 23 | x = x * b[i] + b[j]; 24 | y = y - x; 25 | }else{ 26 | res[i] = res[i] + A[i][j] * b[j]; 27 | } 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | } 33 | 34 | int A[N][N]; 35 | int B[N]; 36 | int C[N]; 37 | 38 | int main(){ 39 | int n = getint(); 40 | int i, j; 41 | 42 | i = 0; 43 | 44 | while (i < n){ 45 | j = 0; 46 | while (j < n){ 47 | A[i][j] = getint(); 48 | j = j + 1; 49 | } 50 | i = i + 1; 51 | } 52 | 53 | i = 0; 54 | while (i < n){ 55 | B[i] = getint(); 56 | i = i + 1; 57 | } 58 | 59 | starttime(); 60 | 61 | i = 0; 62 | while (i < 50){ 63 | mv(n, A, B, C); 64 | mv(n, A, C, B); 65 | i = i + 1; 66 | } 67 | stoptime(); 68 | 69 | putarray(n, C); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv3.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:f2e4b2b31a4a77ea6b2c13458e856739c5fe4c4fbaf99abbabad357d1ace0489 3 | size 8326794 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/02_mv3.sy: -------------------------------------------------------------------------------- 1 | int x; 2 | 3 | const int N = 2010; 4 | 5 | void mv(int n, int A[][N], int b[], int res[]){ 6 | int x, y; 7 | y = 0; 8 | x = 11; 9 | int i, j; 10 | 11 | i = 0; 12 | while(i < n){ 13 | res[i] = 0; 14 | i = i + 1; 15 | } 16 | 17 | i = 0; 18 | j = 0; 19 | while (i < n){ 20 | j = 0; 21 | while (j < n){ 22 | if (A[i][j] == 0){ 23 | x = x * b[i] + b[j]; 24 | y = y - x; 25 | }else{ 26 | res[i] = res[i] + A[i][j] * b[j]; 27 | } 28 | j = j + 1; 29 | } 30 | i = i + 1; 31 | } 32 | } 33 | 34 | int A[N][N]; 35 | int B[N]; 36 | int C[N]; 37 | 38 | int main(){ 39 | int n = getint(); 40 | int i, j; 41 | 42 | i = 0; 43 | 44 | while (i < n){ 45 | j = 0; 46 | while (j < n){ 47 | A[i][j] = getint(); 48 | j = j + 1; 49 | } 50 | i = i + 1; 51 | } 52 | 53 | i = 0; 54 | while (i < n){ 55 | B[i] = getint(); 56 | i = i + 1; 57 | } 58 | 59 | starttime(); 60 | 61 | i = 0; 62 | while (i < 50){ 63 | mv(n, A, B, C); 64 | mv(n, A, C, B); 65 | i = i + 1; 66 | } 67 | stoptime(); 68 | 69 | putarray(n, C); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort1.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:a0015e39f074515050c0708bdf77f2867713d7f6d49c4a68f9e96e1e89d0b343 3 | size 10482443 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort1.out: -------------------------------------------------------------------------------- 1 | 1576633458 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort1.sy: -------------------------------------------------------------------------------- 1 | const int base = 16; 2 | 3 | int getMaxNum(int n, int arr[]){ 4 | int ret = 0; 5 | int i = 0; 6 | while (i < n){ 7 | if (arr[i] > ret) ret = arr[i]; 8 | i = i + 1; 9 | } 10 | return ret; 11 | } 12 | 13 | int getNumPos(int num, int pos){ 14 | int tmp = 1; 15 | int i = 0; 16 | while (i < pos){ 17 | num = num / base; 18 | i = i + 1; 19 | } 20 | return num % base; 21 | } 22 | 23 | void radixSort(int bitround, int a[], int l, int r){ 24 | int head[base] = {}; 25 | int tail[base] = {}; 26 | int cnt[base] = {}; 27 | 28 | if (bitround == -1 || l + 1 >= r) return; 29 | 30 | { 31 | int i = l; 32 | 33 | while (i < r){ 34 | cnt[getNumPos(a[i], bitround)] 35 | = cnt[getNumPos(a[i], bitround)] + 1; 36 | i = i + 1; 37 | } 38 | head[0] = l; 39 | tail[0] = l + cnt[0]; 40 | 41 | i = 1; 42 | while (i < base){ 43 | head[i] = tail[i - 1]; 44 | tail[i] = head[i] + cnt[i]; 45 | i = i + 1; 46 | } 47 | i = 0; 48 | while (i < base){ 49 | while (head[i] < tail[i]){ 50 | int v = a[head[i]]; 51 | while (getNumPos(v, bitround) != i){ 52 | int t = v; 53 | v = a[head[getNumPos(t, bitround)]]; 54 | a[head[getNumPos(t, bitround)]] = t; 55 | head[getNumPos(t, bitround)] = head[getNumPos(t, bitround)] + 1; 56 | } 57 | a[head[i]] = v; 58 | head[i] = head[i] + 1; 59 | } 60 | i = i + 1; 61 | } 62 | } 63 | 64 | { 65 | int i = l; 66 | 67 | head[0] = l; 68 | tail[0] = l + cnt[0]; 69 | 70 | i = 0; 71 | while (i < base){ 72 | if (i > 0){ 73 | head[i] = tail[i - 1]; 74 | tail[i] = head[i] + cnt[i]; 75 | } 76 | radixSort(bitround - 1, a, head[i], tail[i]); 77 | i = i + 1; 78 | } 79 | } 80 | 81 | return; 82 | } 83 | 84 | int a[30000010]; 85 | int ans; 86 | 87 | int main(){ 88 | int n = getarray(a); 89 | 90 | starttime(); 91 | 92 | radixSort(8, a, 0, n); 93 | 94 | int i = 0; 95 | while (i < n){ 96 | ans = ans + i * (a[i] % (2 + i)); 97 | i = i + 1; 98 | } 99 | 100 | if (ans < 0) 101 | ans = -ans; 102 | stoptime(); 103 | putint(ans); 104 | putch(10); 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort2.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:115753ae6e270e29bf620e0f1cc8d3dbf66c4ea808dc159d4a6f52d5d2bf0697 3 | size 138898307 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort2.out: -------------------------------------------------------------------------------- 1 | 609797596 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort2.sy: -------------------------------------------------------------------------------- 1 | const int base = 16; 2 | 3 | int getMaxNum(int n, int arr[]){ 4 | int ret = 0; 5 | int i = 0; 6 | while (i < n){ 7 | if (arr[i] > ret) ret = arr[i]; 8 | i = i + 1; 9 | } 10 | return ret; 11 | } 12 | 13 | int getNumPos(int num, int pos){ 14 | int tmp = 1; 15 | int i = 0; 16 | while (i < pos){ 17 | num = num / base; 18 | i = i + 1; 19 | } 20 | return num % base; 21 | } 22 | 23 | void radixSort(int bitround, int a[], int l, int r){ 24 | int head[base] = {}; 25 | int tail[base] = {}; 26 | int cnt[base] = {}; 27 | 28 | if (bitround == -1 || l + 1 >= r) return; 29 | 30 | { 31 | int i = l; 32 | 33 | while (i < r){ 34 | cnt[getNumPos(a[i], bitround)] 35 | = cnt[getNumPos(a[i], bitround)] + 1; 36 | i = i + 1; 37 | } 38 | head[0] = l; 39 | tail[0] = l + cnt[0]; 40 | 41 | i = 1; 42 | while (i < base){ 43 | head[i] = tail[i - 1]; 44 | tail[i] = head[i] + cnt[i]; 45 | i = i + 1; 46 | } 47 | i = 0; 48 | while (i < base){ 49 | while (head[i] < tail[i]){ 50 | int v = a[head[i]]; 51 | while (getNumPos(v, bitround) != i){ 52 | int t = v; 53 | v = a[head[getNumPos(t, bitround)]]; 54 | a[head[getNumPos(t, bitround)]] = t; 55 | head[getNumPos(t, bitround)] = head[getNumPos(t, bitround)] + 1; 56 | } 57 | a[head[i]] = v; 58 | head[i] = head[i] + 1; 59 | } 60 | i = i + 1; 61 | } 62 | } 63 | 64 | { 65 | int i = l; 66 | 67 | head[0] = l; 68 | tail[0] = l + cnt[0]; 69 | 70 | i = 0; 71 | while (i < base){ 72 | if (i > 0){ 73 | head[i] = tail[i - 1]; 74 | tail[i] = head[i] + cnt[i]; 75 | } 76 | radixSort(bitround - 1, a, head[i], tail[i]); 77 | i = i + 1; 78 | } 79 | } 80 | 81 | return; 82 | } 83 | 84 | int a[30000010]; 85 | int ans; 86 | 87 | int main(){ 88 | int n = getarray(a); 89 | 90 | starttime(); 91 | 92 | radixSort(8, a, 0, n); 93 | 94 | int i = 0; 95 | while (i < n){ 96 | ans = ans + i * (a[i] % (2 + i)); 97 | i = i + 1; 98 | } 99 | 100 | if (ans < 0) 101 | ans = -ans; 102 | stoptime(); 103 | putint(ans); 104 | putch(10); 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort3.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:baec15ed775bab212276ccb5aaf2ae299bd36aface970d25d653eb74f78b60d2 3 | size 33149886 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort3.out: -------------------------------------------------------------------------------- 1 | 863923989 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/03_sort3.sy: -------------------------------------------------------------------------------- 1 | const int base = 16; 2 | 3 | int getMaxNum(int n, int arr[]){ 4 | int ret = 0; 5 | int i = 0; 6 | while (i < n){ 7 | if (arr[i] > ret) ret = arr[i]; 8 | i = i + 1; 9 | } 10 | return ret; 11 | } 12 | 13 | int getNumPos(int num, int pos){ 14 | int tmp = 1; 15 | int i = 0; 16 | while (i < pos){ 17 | num = num / base; 18 | i = i + 1; 19 | } 20 | return num % base; 21 | } 22 | 23 | void radixSort(int bitround, int a[], int l, int r){ 24 | int head[base] = {}; 25 | int tail[base] = {}; 26 | int cnt[base] = {}; 27 | 28 | if (bitround == -1 || l + 1 >= r) return; 29 | 30 | { 31 | int i = l; 32 | 33 | while (i < r){ 34 | cnt[getNumPos(a[i], bitround)] 35 | = cnt[getNumPos(a[i], bitround)] + 1; 36 | i = i + 1; 37 | } 38 | head[0] = l; 39 | tail[0] = l + cnt[0]; 40 | 41 | i = 1; 42 | while (i < base){ 43 | head[i] = tail[i - 1]; 44 | tail[i] = head[i] + cnt[i]; 45 | i = i + 1; 46 | } 47 | i = 0; 48 | while (i < base){ 49 | while (head[i] < tail[i]){ 50 | int v = a[head[i]]; 51 | while (getNumPos(v, bitround) != i){ 52 | int t = v; 53 | v = a[head[getNumPos(t, bitround)]]; 54 | a[head[getNumPos(t, bitround)]] = t; 55 | head[getNumPos(t, bitround)] = head[getNumPos(t, bitround)] + 1; 56 | } 57 | a[head[i]] = v; 58 | head[i] = head[i] + 1; 59 | } 60 | i = i + 1; 61 | } 62 | } 63 | 64 | { 65 | int i = l; 66 | 67 | head[0] = l; 68 | tail[0] = l + cnt[0]; 69 | 70 | i = 0; 71 | while (i < base){ 72 | if (i > 0){ 73 | head[i] = tail[i - 1]; 74 | tail[i] = head[i] + cnt[i]; 75 | } 76 | radixSort(bitround - 1, a, head[i], tail[i]); 77 | i = i + 1; 78 | } 79 | } 80 | 81 | return; 82 | } 83 | 84 | int a[30000010]; 85 | int ans; 86 | 87 | int main(){ 88 | int n = getarray(a); 89 | 90 | starttime(); 91 | 92 | radixSort(8, a, 0, n); 93 | 94 | int i = 0; 95 | while (i < n){ 96 | ans = ans + i * (a[i] % (2 + i)); 97 | i = i + 1; 98 | } 99 | 100 | if (ans < 0) 101 | ans = -ans; 102 | stoptime(); 103 | putint(ans); 104 | putch(10); 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv1.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:bce207ae7a6bc634761f7b61841dafcc9b0ec46b360994b059a07a915169596a 3 | size 10206332 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv1.sy: -------------------------------------------------------------------------------- 1 | void spmv(int n,int xptr[], int yidx[], int vals[], int b[], int x[]){ 2 | int i, j, k; 3 | i = 0; 4 | while (i < n){ 5 | x[i] = 0; 6 | i = i + 1; 7 | } 8 | 9 | i = 0; 10 | while (i < n){ 11 | j = xptr[i]; 12 | while (j < xptr[i + 1]){ 13 | x[yidx[j]] = x[yidx[j]] + vals[j]; 14 | j = j + 1; 15 | } 16 | 17 | j = xptr[i]; 18 | while (j < xptr[i + 1]){ 19 | x[yidx[j]] = x[yidx[j]] + vals[j] * (b[i] - 1); 20 | j = j + 1; 21 | } 22 | i = i + 1; 23 | } 24 | } 25 | 26 | const int N = 100010; 27 | const int M = 3000000; 28 | 29 | int x[N], y[M], v[M]; 30 | int a[N], b[N], c[N]; 31 | 32 | int main(){ 33 | int n = getarray(x) - 1; 34 | int m = getarray(y); 35 | getarray(v); 36 | 37 | getarray(a); 38 | 39 | starttime(); 40 | 41 | int i = 0; 42 | while (i < 100){ 43 | spmv(n, x, y, v, a, b); 44 | spmv(n, x, y, v, b, a); 45 | i=i+1; 46 | } 47 | stoptime(); 48 | putarray(n, b); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv2.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:1341cb9516067f730fd77f7d1d80e0e74a64d495f2936cbb11eb33b7f3d4bfc5 3 | size 5956259 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv2.sy: -------------------------------------------------------------------------------- 1 | void spmv(int n,int xptr[], int yidx[], int vals[], int b[], int x[]){ 2 | int i, j, k; 3 | i = 0; 4 | while (i < n){ 5 | x[i] = 0; 6 | i = i + 1; 7 | } 8 | 9 | i = 0; 10 | while (i < n){ 11 | j = xptr[i]; 12 | while (j < xptr[i + 1]){ 13 | x[yidx[j]] = x[yidx[j]] + vals[j]; 14 | j = j + 1; 15 | } 16 | 17 | j = xptr[i]; 18 | while (j < xptr[i + 1]){ 19 | x[yidx[j]] = x[yidx[j]] + vals[j] * (b[i] - 1); 20 | j = j + 1; 21 | } 22 | i = i + 1; 23 | } 24 | } 25 | 26 | const int N = 100010; 27 | const int M = 3000000; 28 | 29 | int x[N], y[M], v[M]; 30 | int a[N], b[N], c[N]; 31 | 32 | int main(){ 33 | int n = getarray(x) - 1; 34 | int m = getarray(y); 35 | getarray(v); 36 | 37 | getarray(a); 38 | 39 | starttime(); 40 | 41 | int i = 0; 42 | while (i < 100){ 43 | spmv(n, x, y, v, a, b); 44 | spmv(n, x, y, v, b, a); 45 | i=i+1; 46 | } 47 | stoptime(); 48 | putarray(n, b); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv3.in: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:dee054c6639ff319d1373df2b23e4b00c0e949bc48a793adf1e8ef343f31d882 3 | size 7795696 4 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv3.out: -------------------------------------------------------------------------------- 1 | 1000: 809099290 -716859757 1808024935 1519494325 1767215996 -1187828860 -1199477192 -202639350 1414463733 -759124574 1144379927 77438013 -1402323517 2047420374 1100875657 -1536749298 -1289952310 1806409692 -1019677526 1353289192 2094262147 2116562926 1899398836 -1384520107 -1638317575 -882975098 699291033 -1961622193 -815277092 -1372201656 -795506888 -1905000136 1941434782 668192668 -934707974 981253714 -284274687 2143399095 159387495 -2107572198 -1238320831 -977552013 -72882991 28201672 621343297 -1800662651 -1986943889 -673595937 644253146 2016373464 885696109 921093053 -312462129 -1458195724 -142543882 1167097539 805491109 -1360123114 -743480850 1643376573 1087140928 488432270 2054313300 1365963364 775831894 -1668496429 594784588 1749355703 1700144138 1252791268 1422274684 917919380 -1336487636 -1764896385 1106405222 -805929036 -186797232 -1439293648 -1957929760 446087486 -39030533 -1373777325 1946694205 1270099436 -1678335545 -382710275 -835291616 -728936245 866389676 1482419915 853658333 -660453952 688588200 448120093 908065707 1469814946 1638047360 1493939875 -1103748524 -1198240396 1387570040 -1847779563 856092963 -1773477756 -1415229087 -481053494 1984557941 -233725596 -833395043 -763716307 -1993604670 -559320013 -1606814450 -444837013 -632419336 -1871046782 850590038 577199590 1142136820 1258308619 1605213031 -798840324 1618438946 911490134 -1393308303 1463873974 -1169744505 122658776 459584656 1494277628 1476697382 257686640 -219664551 649911860 347289998 -2110447903 985928591 164796306 1381955003 -526586025 -1571104095 -1023523838 1144453880 -159327001 757366050 1291857034 1128579721 -442817686 646493944 -2072999359 -1818622660 2013953834 -1346850407 -1364852369 -933619121 -1381827142 -45080728 2012691377 -1594115387 -200868879 -1532367438 73938629 -440531268 1795229598 967043394 960096177 -1910519392 265014463 -733703717 2068370732 -7341131 -1281600449 -22313370 -1848221848 -982856096 -1991496857 1281657814 -1860831546 1411053606 -1295522108 1582411795 838807275 -791236424 1404606459 974267264 2094423023 1573595695 566464476 -1469888142 -230655479 2037188046 1775198047 8918045 885157006 718539546 -2037703117 -1320372094 -133022950 -521801159 59273518 783946901 -1054233033 -526312597 1310756584 -471309881 1711240535 675608 -349456891 -511462076 -2065794599 -1094563205 -826611182 61301794 -724209968 -6140200 1155902877 -1631911424 1201341848 -578724290 1764837910 -1007053624 -1422901591 -355097250 -427877330 -1227285586 956167637 -1794888975 -1010746719 -78007763 -1894982352 986650738 516544602 -165152496 -1912839999 -1169566541 671425997 -325179593 -326697129 -1259904927 -1722087982 -1129627800 592219004 1189531346 -1245479139 526481320 -554429983 -1190566568 582121942 -782453049 574707 -1296500654 1409876280 1440117252 -865557288 1510679338 -1026423734 -1974328182 596917188 -75574007 636333554 2070437869 -2145521837 334166705 761247935 829565195 292049463 535866833 1730721688 -897659268 -335899710 -693921389 -1857395160 1477362637 1088990067 -729229401 -271407319 -856314688 852308759 -1484126749 -224693127 1377855566 -504858062 1296433632 2139592545 1446589011 1120179637 1878438189 -892138306 -1403427355 1473988176 1423516927 145685037 -1683646672 2042055791 -194162995 -251682421 812538010 -652870513 59716616 866096086 -1625905258 708579334 831175676 -820884452 -877881497 -997535408 -1465435083 -297561732 1194875521 1604715054 -1390498432 -514554809 1601340200 1925184585 1468308306 121978056 -1943700182 1697103939 671440191 -678047018 -559537248 2068563254 -2009449208 84440071 -1346241728 1226950935 80538551 1457179762 1438795249 -1543213741 -1759816755 804450657 1108838100 -335368975 1726481269 206163324 863802021 -1822804650 1524050837 -552250312 1257397551 1365269609 29946978 1008326739 -1403764222 1417399120 -1513926576 -1471910968 1329326175 -418754403 269504014 1306997858 -438522600 2025704270 540030251 -1220883534 -1446032296 -49183446 347338672 -1777183923 1025732549 -690356400 139188834 1003124667 -1330231484 -271279692 -2058726253 1353304547 -676859642 56877800 1786274589 1423256179 1798191903 1238804120 -952667545 1238441976 -1157735942 1223204289 195296656 -194092122 1856890754 224467261 896821125 -178345465 -993754443 257067904 -1683724832 1714938049 2008767519 969272895 -719814511 -756357377 1849102949 -1988135341 653585110 1772357576 1288408716 2021090523 -15655789 135288037 -580279861 1347675157 -1668888649 -356930995 -1543855378 1798177943 900983238 -1670638900 -1730290699 1587081677 -625340175 -1012749797 2131843926 -1068272477 684944032 444262263 -488588336 -64694641 -439442613 719212653 -758500974 -445944019 1189434519 550631954 1180759673 1181391388 -1206778388 -1842650151 -1235730804 853876276 -1477352080 122473154 283705102 269329294 1120941527 1082509634 -1901570294 1657014084 -801494213 876931964 1664007137 -58610982 -1890643507 1197438755 -1412196174 -357970962 -1269727276 1884950926 -675390059 -751058981 627901994 7648835 -1011981755 1858701810 1182091742 -8861775 -1190929824 284256159 -449710901 176730393 327749973 -1219056271 17327816 1792377416 776600357 1373223843 -409984793 936841542 -772058884 -1765046402 -1931824538 537398571 -1473799109 -1682029332 -754209849 1815284696 -280092678 -1528161255 -1123247219 826896767 337787904 297848206 1860704330 -957303941 -1846605940 -1764263712 -225023340 -472981463 -1128655967 1736154454 609037962 -830172815 1009218019 795953098 527356526 -2102090431 2004852787 -2000333900 -1730838263 -627273400 346738694 -1713132253 -1509373287 -394031693 1727511585 1636830737 -1480642812 1192700407 1142073211 1308272281 719932008 616829180 674422366 366037527 2092588474 433773845 -2081553023 1846477480 1587761918 1792902642 -1815154550 673444931 -1471017924 -1853586744 -1006251456 -2056066098 -1497983943 1146247429 205628485 -1544631197 -2135367585 -1623349408 -2002077198 -2077769039 1547382251 1476993449 552505881 921193779 -234013640 508261531 1088509876 -1193059143 287597968 1146340915 1932516239 -2067253647 -281342344 -1400865840 -965834926 -918687700 906617601 -1463587553 92970432 874550027 -349848231 -1755139295 -1595244763 -502235088 712499064 1966207995 1517617505 -214319958 1156341486 -2008828524 302891525 -720515825 1404638667 -1613866438 1552370270 1166301330 82457431 1707257898 -2013452097 -601023662 -1897828787 -2109366117 -333555502 -2097330826 192480446 1835069664 188040708 538533854 -1217089021 -1149182779 900108610 1747927102 334426086 644053910 1833015014 840232128 -1861581796 -1526972136 -1840632737 -906910138 1797326383 241354404 -306780586 -2056132475 -1648462706 -976078585 -703703735 1708625081 2101703343 1768948761 -951728791 2048954112 1512241073 -819464809 -2101913192 -341768239 616294322 -518954042 -1791126093 -2038297336 2040883108 -110459588 -1938239428 588882467 -1336510079 1317508449 -503772520 468209936 -1068625014 715064647 1715612901 910033790 937056262 2033799381 -42073307 1882863282 1839706071 -1488147241 -1080348193 -116720619 1241306778 -470961376 -837110033 1378060374 -1253684812 1749270911 -71734504 935212750 -558294229 671509308 1767461266 -662014841 618617348 1016642569 -1916571354 -546015678 -658419092 2118684822 -677103269 1216598777 -561700153 1260696808 887983569 399229653 -471609975 -551456085 -1566261011 480169574 2017861221 -158733393 -101170231 -935791845 -202236120 -678138168 1546803963 -1840190242 1312125282 -1673562188 -1429939219 -916416590 1510063857 1505968955 -1703289417 1232996741 299460448 -1953347133 85351273 1566289369 -273841939 -416058142 -1903247464 1004503656 375037719 1291261136 -1370455460 -368825069 -1705280808 -867537407 -1881091233 -1689461252 971376057 1806892737 -1402993880 -208559428 -1080596812 -555151261 -106575421 -193211238 1755104869 -1425743692 -337045118 -1624766558 944561215 41293934 2042726386 -128648989 1908646959 -699212120 -1209442389 1411684733 -698676264 1177238765 -1809839361 -986079551 -1899629588 -1239633804 -623059353 -1633813065 1060519192 1422108147 1417144213 994227698 -1193637805 1169643339 -925287375 483206056 1081564399 -504930316 1298916480 1428157352 -1371448440 1997209150 -2084676966 -1424739898 -2029821157 -1433749089 848742474 70667894 -1472733526 -1931737881 -670617611 282392898 -939254305 -439452533 477356109 -1286547277 -45417994 -1348211395 -1789753493 -13778360 -293108979 -157296838 -926710748 1992720251 1069738093 -1229128945 -1892763700 -1861962298 -1019703410 88540665 -711933130 -1040922338 10505964 1874437144 -1137117867 1648652784 -1419870463 816881707 1146750948 -146897315 -1456644893 -942756364 1505138153 -359826042 -134241276 -1055576584 2097814653 640463998 797874667 1028722313 -1814846253 -822738313 1049506031 -253582912 -1512541955 -1088525139 2072085015 703487611 -287052158 1618942085 -704370808 165998548 -1663870553 1405094965 -2079576398 -141241424 -1538482803 -480437227 -2132813463 351851595 1708286131 -1992253263 1609820923 1843210011 843365892 598757222 -1182578206 1905761258 539953829 833399002 -1659785300 2086357800 -158449074 1190815366 -2027626732 -510555279 -110232792 1865789737 -1622412116 -31677909 1136831798 1469282393 1974516860 898372311 334301780 1391877735 -1010217747 -2146890113 -881037851 -1035747462 -256023597 -1626274623 -500498146 1649657954 -1509447726 33727631 -299268552 233768218 2030068448 -1774579815 242039814 736484689 2000894689 1475373309 -861826471 -1588577744 384253896 -1168700213 2060778324 -2053078981 -462927112 464404231 -369375232 112117200 2052228839 1255132232 30966071 -1000217826 -1140352108 1028851732 689626953 -1729945336 1118037774 2093273643 -290697634 -600636520 -1629831315 -620014239 -1328222090 2126547036 -963114162 274388189 -946683373 1882163087 -678152195 845203942 -1592096310 -1756843536 1866265472 1453631703 -1530647767 -330703288 332440640 993162232 -362312321 -1386649992 -1790298366 -1182246074 1017955839 1382271623 655600025 2118068955 -2143924007 -704541302 1888956411 -1239467927 -1794806216 1617722083 -1310254262 -1849920722 477396568 -1465448268 -1074816750 -179723696 5238273 941484097 81619197 -55948517 499584314 -1123503028 1412946663 2108196952 239739958 584873478 1689972807 -394999188 1279413416 2061130303 1448423912 -1677193937 866801678 703494737 -623785715 868881673 -2127953634 2127781050 -1594367458 630254502 438893861 471513260 -1628831867 1358416163 -1663981030 754295207 -1801666166 -22942533 -1639998499 -1517261732 -270858096 2008645491 -859102668 1302775767 -615489886 297100483 174928416 1038627498 1892111927 -1477208484 896645176 -128330268 -2093865646 -1417230208 1272783901 -1558669546 -1502086694 -1674039563 2074064655 508171254 -674772349 -1694719773 127892270 1426061053 891064779 -115056653 1964355507 1983093228 -1014920676 1780828205 165458720 55223496 -1634019441 -676129726 871694314 1888832348 -464985460 743373336 -946167221 -22138628 -1363247879 580002323 1506036477 1442378085 1197040779 660851741 1859946735 -495407731 233922360 -1613160880 -1503279559 162569313 -1446625069 -505966651 450270545 -1309584136 -1995727358 -958581162 2 | 0 3 | -------------------------------------------------------------------------------- /test_sample/performance_test/04_spmv3.sy: -------------------------------------------------------------------------------- 1 | void spmv(int n,int xptr[], int yidx[], int vals[], int b[], int x[]){ 2 | int i, j, k; 3 | i = 0; 4 | while (i < n){ 5 | x[i] = 0; 6 | i = i + 1; 7 | } 8 | 9 | i = 0; 10 | while (i < n){ 11 | j = xptr[i]; 12 | while (j < xptr[i + 1]){ 13 | x[yidx[j]] = x[yidx[j]] + vals[j]; 14 | j = j + 1; 15 | } 16 | 17 | j = xptr[i]; 18 | while (j < xptr[i + 1]){ 19 | x[yidx[j]] = x[yidx[j]] + vals[j] * (b[i] - 1); 20 | j = j + 1; 21 | } 22 | i = i + 1; 23 | } 24 | } 25 | 26 | const int N = 100010; 27 | const int M = 3000000; 28 | 29 | int x[N], y[M], v[M]; 30 | int a[N], b[N], c[N]; 31 | 32 | int main(){ 33 | int n = getarray(x) - 1; 34 | int m = getarray(y); 35 | getarray(v); 36 | 37 | getarray(a); 38 | 39 | starttime(); 40 | 41 | int i = 0; 42 | while (i < 100){ 43 | spmv(n, x, y, v, a, b); 44 | spmv(n, x, y, v, b, a); 45 | i=i+1; 46 | } 47 | stoptime(); 48 | putarray(n, b); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /项目介绍.pdf: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:627342b64cbd42f29d9d3426dd4ad134be1554545ed211072d67063b0f2eccbc 3 | size 60958921 4 | --------------------------------------------------------------------------------