├── .gitignore ├── CMakeLists.txt ├── Documentations ├── common │ ├── LightIR.md │ ├── cminusf.md │ ├── figs │ │ └── image-20201109145323504.png │ └── logging.md ├── lab1 │ └── README.md ├── lab2 │ ├── README.md │ └── readings.md ├── lab3 │ └── README.md ├── lab4 │ └── README.md └── lab5 │ ├── CodeReading.md │ ├── README.md │ ├── 活跃变量.pdf │ └── 静态单赋值格式构造.pdf ├── README.md ├── Reports ├── lab1 │ └── report.md ├── lab2 │ └── report.md ├── lab3 │ ├── report.md │ └── team.txt ├── lab4 │ ├── contribution.md │ ├── eval_result │ └── report.md └── lab5 │ ├── contribution.md │ ├── report-phase1.md │ ├── report-phase2.md │ └── score ├── include ├── ast.hpp ├── cminusf_builder.hpp ├── lexical_analyzer.h ├── lightir │ ├── BasicBlock.h │ ├── Constant.h │ ├── Function.h │ ├── GlobalVariable.h │ ├── IRBuilder.h │ ├── IRprinter.h │ ├── Instruction.h │ ├── Module.h │ ├── Type.h │ ├── User.h │ └── Value.h ├── logging.hpp ├── optimization │ ├── ActiveVars.hpp │ ├── ConstPropagation.hpp │ ├── Dominators.h │ ├── LoopInvHoist.hpp │ ├── LoopSearch.hpp │ ├── Mem2Reg.hpp │ └── PassManager.hpp └── syntax_tree.h ├── src ├── CMakeLists.txt ├── cminusfc │ ├── CMakeLists.txt │ ├── cminusf_builder.cpp │ ├── cminusf_builder.myl.cpp │ └── cminusfc.cpp ├── common │ ├── CMakeLists.txt │ ├── ast.cpp │ ├── logging.cpp │ └── syntax_tree.c ├── io │ ├── CMakeLists.txt │ └── io.c ├── lexer │ ├── CMakeLists.txt │ └── lexical_analyzer.l ├── lightir │ ├── BasicBlock.cpp │ ├── CMakeLists.txt │ ├── Constant.cpp │ ├── Function.cpp │ ├── GlobalVariable.cpp │ ├── IRprinter.cpp │ ├── Instruction.cpp │ ├── Module.cpp │ ├── Type.cpp │ ├── User.cpp │ └── Value.cpp ├── optimization │ ├── ActiveVars.cpp │ ├── CMakeLists.txt │ ├── ConstPropagation.cpp │ ├── Dominators.cpp │ ├── LoopInvHoist.cpp │ ├── LoopSearch.cpp │ └── Mem2Reg.cpp └── parser │ ├── CMakeLists.txt │ ├── lexical_analyzer.l │ └── syntax_analyzer.y └── tests ├── CMakeLists.txt ├── lab1 ├── CMakeLists.txt ├── LAB1_SCORE ├── TA_token │ ├── 1.tokens │ ├── 10.tokens │ ├── 2.tokens │ ├── 3.tokens │ ├── 4.tokens │ ├── 5.tokens │ ├── 6.tokens │ ├── 7.tokens │ ├── 8.tokens │ └── 9.tokens ├── main.c ├── test_lexer.py └── testcase │ ├── 1.cminus │ ├── 10.cminus │ ├── 2.cminus │ ├── 3.cminus │ ├── 4.cminus │ ├── 5.cminus │ ├── 6.cminus │ ├── 7.cminus │ ├── 8.cminus │ └── 9.cminus ├── lab2 ├── .gitignore ├── CMakeLists.txt ├── easy │ ├── FAIL_array-expr.cminus │ ├── FAIL_decl.cminus │ ├── FAIL_empty-param.cminus │ ├── FAIL_func.cminus │ ├── FAIL_id.cminus │ ├── FAIL_local-decl.cminus │ ├── FAIL_nested-func.cminus │ ├── FAIL_var-init.cminus │ ├── array.cminus │ ├── call.cminus │ ├── div_by_0.cminus │ ├── expr-assign.cminus │ ├── expr.cminus │ ├── func.cminus │ ├── if.cminus │ ├── lex1.cminus │ ├── lex2.cminus │ ├── local-decl.cminus │ ├── math.cminus │ └── relop.cminus ├── hard │ ├── FAIL_array.cminus │ ├── FAIL_assign.cminus │ ├── FAIL_comment.cminus │ ├── FAIL_function.cminus │ ├── FAIL_op.cminus │ ├── FAIL_syntax1.cminus │ ├── assoc.cminus │ └── if.cminus ├── main.c ├── normal │ ├── You_Should_Pass.cminus │ ├── array1.cminus │ ├── array2.cminus │ ├── custom.cminus │ ├── func.cminus │ ├── gcd.cminus │ ├── if.cminus │ ├── selectionsort.cminus │ └── tap.cminus ├── syntree_easy_std │ ├── FAIL_array-expr.syntax_tree │ ├── FAIL_decl.syntax_tree │ ├── FAIL_empty-param.syntax_tree │ ├── FAIL_func.syntax_tree │ ├── FAIL_id.syntax_tree │ ├── FAIL_local-decl.syntax_tree │ ├── FAIL_nested-func.syntax_tree │ ├── FAIL_var-init.syntax_tree │ ├── array.syntax_tree │ ├── call.syntax_tree │ ├── div_by_0.syntax_tree │ ├── expr-assign.syntax_tree │ ├── expr.syntax_tree │ ├── func.syntax_tree │ ├── if.syntax_tree │ ├── lex1.syntax_tree │ ├── lex2.syntax_tree │ ├── local-decl.syntax_tree │ ├── math.syntax_tree │ └── relop.syntax_tree ├── syntree_normal_std │ ├── You_Should_Pass.syntax_tree │ ├── array1.syntax_tree │ ├── array2.syntax_tree │ ├── custom.syntax_tree │ ├── func.syntax_tree │ ├── gcd.syntax_tree │ ├── if.syntax_tree │ ├── selectionsort.syntax_tree │ └── tap.syntax_tree └── test_syntax.sh ├── lab3 ├── CMakeLists.txt ├── c_cases │ ├── assign.c │ ├── fun.c │ ├── if.c │ └── while.c ├── stu_cpp │ ├── assign_generator.cpp │ ├── fun_generator.cpp │ ├── if_generator.cpp │ └── while_generator.cpp ├── stu_ll │ ├── assign_hand.ll │ ├── fun_hand.ll │ ├── if_hand.ll │ └── while_hand.ll └── ta_gcd │ ├── gcd_array.c │ └── gcd_array_generator.cpp ├── lab4 ├── lab4_test.py ├── ta_answers │ ├── lv0_1 │ │ ├── decl_float.out │ │ ├── decl_float_array.out │ │ ├── decl_int.out │ │ ├── decl_int_array.out │ │ ├── input.in │ │ ├── input.out │ │ ├── output_float.out │ │ ├── output_int.out │ │ └── return.out │ ├── lv0_2 │ │ ├── num_add_float.out │ │ ├── num_add_int.out │ │ ├── num_add_mixed.out │ │ ├── num_comp1.out │ │ ├── num_comp2.out │ │ ├── num_div_float.out │ │ ├── num_div_int.out │ │ ├── num_div_mixed.out │ │ ├── num_eq_float.out │ │ ├── num_eq_int.out │ │ ├── num_eq_mixed.out │ │ ├── num_ge_float.out │ │ ├── num_ge_int.out │ │ ├── num_ge_mixed.out │ │ ├── num_gt_float.out │ │ ├── num_gt_int.out │ │ ├── num_gt_mixed.out │ │ ├── num_le_float.out │ │ ├── num_le_int.out │ │ ├── num_le_mixed.out │ │ ├── num_lt_float.out │ │ ├── num_lt_int.out │ │ ├── num_lt_mixed.out │ │ ├── num_mul_float.out │ │ ├── num_mul_int.out │ │ ├── num_mul_mixed.out │ │ ├── num_neq_float.out │ │ ├── num_neq_int.out │ │ ├── num_neq_mixed.out │ │ ├── num_sub_float.out │ │ ├── num_sub_int.out │ │ └── num_sub_mixed.out │ ├── lv1 │ │ ├── assign_cmp.out │ │ ├── assign_float_array_global.out │ │ ├── assign_float_array_local.out │ │ ├── assign_float_var_global.out │ │ ├── assign_float_var_local.out │ │ ├── assign_int_array_global.out │ │ ├── assign_int_array_local.out │ │ ├── assign_int_var_global.out │ │ ├── assign_int_var_local.out │ │ ├── idx_float.out │ │ ├── innout.in │ │ ├── innout.out │ │ ├── iteration1.out │ │ ├── iteration2.out │ │ ├── negidx_float.out │ │ ├── negidx_floatfuncall.out │ │ ├── negidx_int.out │ │ ├── negidx_intfuncall.out │ │ ├── negidx_voidfuncall.out │ │ ├── scope.out │ │ ├── selection1.out │ │ ├── selection2.out │ │ ├── selection3.out │ │ ├── transfer_float_to_int.out │ │ └── transfer_int_to_float.out │ ├── lv2 │ │ ├── assign_chain.out │ │ ├── funcall_array.out │ │ ├── funcall_array_array.out │ │ ├── funcall_chain.out │ │ ├── funcall_float_array.out │ │ ├── funcall_int_array.out │ │ ├── funcall_type_mismatch1.out │ │ ├── funcall_type_mismatch2.out │ │ ├── funcall_var.out │ │ ├── return_in_middle1.out │ │ ├── return_in_middle2.out │ │ ├── return_type_mismatch1.out │ │ └── return_type_mismatch2.out │ └── lv3 │ │ ├── complex1.out │ │ ├── complex2.in │ │ ├── complex2.out │ │ ├── complex3.in │ │ ├── complex3.out │ │ └── complex4.out ├── ta_testcases │ ├── lv0_1 │ │ ├── decl_float.cminus │ │ ├── decl_float_array.cminus │ │ ├── decl_int.cminus │ │ ├── decl_int_array.cminus │ │ ├── input.cminus │ │ ├── output_float.cminus │ │ ├── output_int.cminus │ │ └── return.cminus │ ├── lv0_2 │ │ ├── num_add_float.cminus │ │ ├── num_add_int.cminus │ │ ├── num_add_mixed.cminus │ │ ├── num_comp1.cminus │ │ ├── num_comp2.cminus │ │ ├── num_div_float.cminus │ │ ├── num_div_int.cminus │ │ ├── num_div_mixed.cminus │ │ ├── num_eq_float.cminus │ │ ├── num_eq_int.cminus │ │ ├── num_eq_mixed.cminus │ │ ├── num_ge_float.cminus │ │ ├── num_ge_int.cminus │ │ ├── num_ge_mixed.cminus │ │ ├── num_gt_float.cminus │ │ ├── num_gt_int.cminus │ │ ├── num_gt_mixed.cminus │ │ ├── num_le_float.cminus │ │ ├── num_le_int.cminus │ │ ├── num_le_mixed.cminus │ │ ├── num_lt_float.cminus │ │ ├── num_lt_int.cminus │ │ ├── num_lt_mixed.cminus │ │ ├── num_mul_float.cminus │ │ ├── num_mul_int.cminus │ │ ├── num_mul_mixed.cminus │ │ ├── num_neq_float.cminus │ │ ├── num_neq_int.cminus │ │ ├── num_neq_mixed.cminus │ │ ├── num_sub_float.cminus │ │ ├── num_sub_int.cminus │ │ └── num_sub_mixed.cminus │ ├── lv1 │ │ ├── assign_cmp.cminus │ │ ├── assign_float_array_global.cminus │ │ ├── assign_float_array_local.cminus │ │ ├── assign_float_var_global.cminus │ │ ├── assign_float_var_local.cminus │ │ ├── assign_int_array_global.cminus │ │ ├── assign_int_array_local.cminus │ │ ├── assign_int_var_global.cminus │ │ ├── assign_int_var_local.cminus │ │ ├── idx_float.cminus │ │ ├── innout.cminus │ │ ├── iteration1.cminus │ │ ├── iteration2.cminus │ │ ├── negidx_float.cminus │ │ ├── negidx_floatfuncall.cminus │ │ ├── negidx_int.cminus │ │ ├── negidx_intfuncall.cminus │ │ ├── negidx_voidfuncall.cminus │ │ ├── scope.cminus │ │ ├── selection1.cminus │ │ ├── selection2.cminus │ │ ├── selection3.cminus │ │ ├── transfer_float_to_int.cminus │ │ └── transfer_int_to_float.cminus │ ├── lv2 │ │ ├── assign_chain.cminus │ │ ├── funcall_array_array.cminus │ │ ├── funcall_chain.cminus │ │ ├── funcall_float_array.cminus │ │ ├── funcall_int_array.cminus │ │ ├── funcall_type_mismatch1.cminus │ │ ├── funcall_type_mismatch2.cminus │ │ ├── funcall_var.cminus │ │ ├── return_in_middle1.cminus │ │ ├── return_in_middle2.cminus │ │ ├── return_type_mismatch1.cminus │ │ └── return_type_mismatch2.cminus │ └── lv3 │ │ ├── complex1.cminus │ │ ├── complex2.cminus │ │ ├── complex3.cminus │ │ └── complex4.cminus └── testcases │ ├── 01.cminus │ ├── 01.in │ ├── 01.out │ ├── 02.cminus │ ├── 02.out │ ├── 03.cminus │ ├── 03.out │ ├── 04.cminus │ ├── 04.out │ ├── 05.cminus │ ├── 05.out │ ├── 06.cminus │ ├── 06.out │ ├── 07.cminus │ ├── 07.out │ ├── 08.cminus │ ├── 08.out │ ├── 09.cminus │ ├── 09.out │ ├── 10.cminus │ ├── 10.out │ ├── 11.cminus │ ├── 11.out │ ├── 12.cminus │ ├── 12.in │ ├── 12.out │ ├── 13.cminus │ ├── 13.out │ ├── 14.cminus │ ├── 14.out │ ├── 15.cminus │ ├── 15.out │ ├── 16.cminus │ ├── 16.out │ ├── 17.cminus │ ├── 17.out │ ├── 18.cminus │ ├── 18.out │ ├── 19.cminus │ ├── 19.out │ ├── 20.cminus │ ├── 20.out │ ├── 21.cminus │ ├── 21.out │ ├── 22.cminus │ └── 22.out ├── lab5 ├── lab5_tests.py └── testcases │ ├── ActiveVars │ ├── testcase-1.cminus │ ├── testcase-1.json │ ├── testcase-2.cminus │ ├── testcase-2.json │ ├── testcase-3.cminus │ ├── testcase-3.json │ ├── testcase-4.cminus │ ├── testcase-4.json │ ├── testcase-5.cminus │ ├── testcase-5.json │ ├── testcase-6.cminus │ ├── testcase-6.json │ ├── testcase-7.cminus │ └── testcase-7.json │ ├── ConstPropagation │ ├── baseline │ │ ├── testcase-1.ll │ │ ├── testcase-2.ll │ │ ├── testcase-3.ll │ │ ├── testcase-4.ll │ │ ├── testcase-5.ll │ │ ├── testcase-6.ll │ │ ├── testcase-7.ll │ │ └── testcase-8.ll │ ├── testcase-1.cminus │ ├── testcase-1.out │ ├── testcase-2.cminus │ ├── testcase-2.out │ ├── testcase-3.cminus │ ├── testcase-3.out │ ├── testcase-4.cminus │ ├── testcase-4.out │ ├── testcase-5.cminus │ ├── testcase-5.out │ ├── testcase-6.cminus │ ├── testcase-6.out │ ├── testcase-7.cminus │ ├── testcase-7.out │ ├── testcase-8.cminus │ └── testcase-8.out │ ├── LoopInvHoist │ ├── baseline │ │ ├── testcase-1.ll │ │ ├── testcase-2.ll │ │ ├── testcase-3.ll │ │ ├── testcase-4.ll │ │ ├── testcase-5.ll │ │ ├── testcase-6.ll │ │ ├── testcase-7.ll │ │ └── testcase-8.ll │ ├── testcase-1.cminus │ ├── testcase-1.out │ ├── testcase-2.cminus │ ├── testcase-2.out │ ├── testcase-3.cminus │ ├── testcase-3.out │ ├── testcase-4.cminus │ ├── testcase-4.out │ ├── testcase-5.cminus │ ├── testcase-5.out │ ├── testcase-6.cminus │ ├── testcase-6.out │ ├── testcase-7.cminus │ ├── testcase-7.out │ ├── testcase-8.cminus │ └── testcase-8.out │ └── myactvar │ ├── testcase-5.cminus │ ├── testcase-5.json │ ├── testcase-6.cminus │ └── testcase-6.json ├── test_ast.cpp └── test_logging.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | *.tokens 3 | .vscode 4 | lex.yy.c 5 | .DS_Store 6 | */.DS_Store 7 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(CMINUSF) 2 | cmake_minimum_required( VERSION 2.8 ) 3 | 4 | set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -std=c99") 5 | 6 | set(CMAKE_BUILD_TYPE "Debug") 7 | set(CMAKE_C_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb") 8 | set(CMAKE_C_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") 9 | set(CMAKE_CXX_STANDARD 17) 10 | 11 | find_package(FLEX REQUIRED) 12 | find_package(BISON REQUIRED) 13 | find_package(LLVM REQUIRED CONFIG) 14 | message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") 15 | message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") 16 | llvm_map_components_to_libnames( 17 | llvm_libs 18 | support 19 | core 20 | ) 21 | 22 | INCLUDE_DIRECTORIES( 23 | include 24 | ${LLVM_INCLUDE_DIRS} 25 | ) 26 | 27 | add_definitions(${LLVM_DEFINITIONS}) 28 | 29 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) 30 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) 31 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) 32 | 33 | include_directories(${PROJECT_SOURCE_DIR}) 34 | include_directories(${PROJECT_BINARY_DIR}) 35 | include_directories(include/lightir) 36 | include_directories(include/optimization) 37 | 38 | add_subdirectory(src) 39 | add_subdirectory(tests) 40 | -------------------------------------------------------------------------------- /Documentations/common/figs/image-20201109145323504.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/Documentations/common/figs/image-20201109145323504.png -------------------------------------------------------------------------------- /Documentations/common/logging.md: -------------------------------------------------------------------------------- 1 | # logging 工具使用 2 | 3 | ## 介绍 4 | 为了方便同学们在之后的实验中 debug,为大家设计了一个C++简单实用的分级日志工具。该工具将日志输出信息从低到高分成四种等级:`DEBUG`,`INFO`,`WARNING`,`ERROR`。通过设定环境变量`LOGV`的值,来选择输出哪些等级的日志。`LOGV`的取值是**0~3**,分别对应到上述的4种级别(`0:DEBUG`,`1:INFO`,`2:WARNING`,`3:ERROR`)。此外输出中还会包含打印该日志的代码所在位置。 5 | 6 | ## 使用 7 | 项目编译好之后,可以在`build`目录下运行`test_logging`,该文件的源代码在`tests/test_logging.cpp`。用法如下: 8 | ```cpp 9 | #include "logging.hpp" 10 | // 引入头文件 11 | int main(){ 12 | LOG(DEBUG) << "This is DEBUG log item."; 13 | // 使用关键字LOG,括号中填入要输出的日志等级 14 | // 紧接着就是<<以及日志的具体信息,就跟使用std::cout一样 15 | LOG(INFO) << "This is INFO log item"; 16 | LOG(WARNING) << "This is WARNING log item"; 17 | LOG(ERROR) << "This is ERROR log item"; 18 | return 0; 19 | } 20 | ``` 21 | 22 | 接着在运行该程序的时候,设定环境变量`LOGV=0`,那么程序就会输出级别**大于等于0**日志信息: 23 | ```bash 24 | user@user:${ProjectDir}/build$ LOGV=0 ./test_logging 25 | [DEBUG] (test_logging.cpp:5L main)This is DEBUG log item. 26 | [INFO] (test_logging.cpp:6L main)This is INFO log item 27 | [WARNING] (test_logging.cpp:7L main)This is WARNING log item 28 | [ERROR] (test_logging.cpp:8L main)This is ERROR log item 29 | ``` 30 | 输出中除了包含日志级别和用户想打印的信息,在圆括号中还包含了打印该信息代码的具体位置(包括文件名称、所在行、所在函数名称),可以很方便地定位到出问题的地方。 31 | 32 | 假如我们觉得程序已经没有问题了,不想看那么多的DEBUG信息,那么我们就可以设定环境变量`LOGV=1`,选择只看**级别大于等于1**的日志信息: 33 | ```bash 34 | user@user:${ProjectDir}/build$ LOGV=0 ./test_logging 35 | [INFO] (test_logging.cpp:6L main)This is INFO log item 36 | [WARNING] (test_logging.cpp:7L main)This is WARNING log item 37 | [ERROR] (test_logging.cpp:8L main)This is ERROR log item 38 | ``` 39 | 当然`LOGV`值越大,日志的信息将更加简略。如果没有设定`LOGV`的环境变量,将默认不输出任何信息。 40 | 41 | 这里再附带一个小技巧,如果日志内容多,在终端观看体验较差,可以输入以下命令将日志输出到文件中: 42 | ``` 43 | user@user:${ProjectDir}/build$ LOGV=0 ./test_logging > log 44 | ``` 45 | 然后就可以输出到文件名为log的文件中啦~ 46 | -------------------------------------------------------------------------------- /Documentations/lab5/活跃变量.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/Documentations/lab5/活跃变量.pdf -------------------------------------------------------------------------------- /Documentations/lab5/静态单赋值格式构造.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/Documentations/lab5/静态单赋值格式构造.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 实验说明 2 | 请fork此`repo`到自己的仓库下,随后在自己的仓库中完成实验,请确保自己的`repo`为`Private`。 3 | 4 | ### 目前已布置的实验: 5 | * [lab1](./Documentations/lab1/README.md) 6 | * DDL : 2020/10/13, 23:59:59 7 | * [lab2](./Documentations/lab2/README.md) 8 | * DDL : 2020/10/30, 23:59:59 9 | * [lab3](./Documentations/lab3/README.md) 10 | * DDL : 2020/11/16, 23:59:59 11 | * [lab4](./Documentations/lab4/README.md) 12 | * DDL : 2020/12/07, 23:59:59 13 | * [lab5](./Documentations/lab5/README.md) 14 | * DDL-Phase1 :2020/12/21 23:59:59 (北京标准时间,UTC+8) 15 | * DDL-Phase2 :2021/01/04 23:59:59 (北京标准时间,UTC+8) 16 | ### FAQ: How to merge upstream remote branches 17 | In brief, you need another alias for upstream repository (we assume you are now in your local copy of forked repository on Gitlab): 18 | ``` 19 | (shell) $ git remote add upstream http://222.195.68.197/staff/2020fall-compiler_cminus.git 20 | ``` 21 | Then try to merge remote commits to your local repository: 22 | ``` 23 | (shell) $ git pull upstream master 24 | ``` 25 | Then synchronize changes to your forked remote repository: 26 | ``` 27 | (shell) $ git push origin master 28 | ``` 29 | 30 | ## License 31 | 32 | Because the repo contains code from TAs, 33 | who have not licensed the code under an open source license, 34 | but only allowed us to public the code, 35 | the repo is only public for learning. 36 | 37 | ALL RIGHTS ARE RESERVED. 38 | -------------------------------------------------------------------------------- /Reports/lab1/report.md: -------------------------------------------------------------------------------- 1 | # lab1 实验报告 2 | 3 | ***REMOVED*** myl7 4 | 5 | ## 实验要求 6 | 7 | 基于 Flex 实现支持 cminus-f 的词法解析器。 8 | 9 | ## 实验难点 10 | 11 | 给出不同 token 的 regex,并合理安排这些 regex 的顺序。 12 | 13 | ## 实验设计 14 | 15 | 由于框架中已实现对于 `lines`, `pos_start`, `pos_end` 处理,这里只需维护这三个变量即可。 16 | 在完成 token 匹配后,更新这三个变量。 17 | 18 | 注意到要求的输出格式,这三个变量应从 1 开始计数。 19 | 20 | 额外的,对于注释,由于仅此 token 可以同时包含换行符和非换行符,需要特殊处理:历遍其内容,统计上述三个变量的变化。 21 | 22 | ## 实验结果验证 23 | 24 | 我自行提供的测试样例位于 `/tests/lab1/testcase/7.cminus`,期望结果位于 `/tests/lab1/TA_token/7.tokens`。 25 | 26 | 我尽可能地提供了所有的边缘情况,经测试可以通过。 27 | 28 | ## 实验反馈 29 | 30 | See [#62 Suggestion: C global variable sharing and trailing spaces at line end](staff/2020-fall-notice_board#62). 31 | -------------------------------------------------------------------------------- /Reports/lab2/report.md: -------------------------------------------------------------------------------- 1 | # lab2 实验报告 2 | 3 | ***REMOVED*** myl7 4 | 5 | ## 实验要求 6 | 7 | 基于 Flex 和 Bison 实现支持 cminus-f 的语法解析器。 8 | 9 | ## 实验难点 10 | 11 | Bison 和 Flex 的协作:Flex 作为 Bison 的词法解析部分,从 Bison 定义中获得 token 声明,再词法分析出相应 node,借助 yylval 传回 Bison,由 Bison 完成语法解析。 12 | 13 | ## 实验设计 14 | 15 | 此实现设计已在“实验难点”中进行了说明。 16 | 17 | ## 实验结果验证 18 | 19 | 本人提供了一份相对综合的测试源码,位于 `tests/lab2/normal/custom.cminus`,对应期望结果位于 `tests/lab2/syntree_normal_std/custom.syntax_tree`,借助提供的测试脚本进行统一的测试,没有问题出现。 20 | 21 | ## 实验反馈 22 | 23 | 此实验的拓展阅读相当有趣,希望能有更多这样的内容。 24 | -------------------------------------------------------------------------------- /Reports/lab3/team.txt: -------------------------------------------------------------------------------- 1 | 队长姓名: myl7 2 | 队长学号: ***REMOVED*** 3 | 4 | 队员 1 姓名: yuanyiwei 5 | 队员 1 学号: ***REMOVED*** 6 | -------------------------------------------------------------------------------- /Reports/lab4/contribution.md: -------------------------------------------------------------------------------- 1 | # 组员贡献 2 | 3 | ## 贡献详述 4 | 5 | 此次实验由于框架的耦合度较高,由我做主要设计,yuanyiwei负责对于框架中某一特定子过程给出实现原型以减小我的心智负担 6 | 7 | ### myl7 8 | 9 | 大部分 visit 方法的实现、调试 10 | 11 | ### yuanyiwei 12 | 13 | `is_returned` 系统,一部分的调试、错误复现 14 | 15 | ## 评定结果 16 | 17 | |名字|百分比| 18 | |:-:|:-:| 19 | |myl7|60| 20 | |yuanyiwei|40| 21 | -------------------------------------------------------------------------------- /Reports/lab4/report.md: -------------------------------------------------------------------------------- 1 | # Lab4 实验报告 2 | 3 | myl7 ***REMOVED*** 4 | 5 | yuanyiwei ***REMOVED*** 6 | 7 | ## 实验要求 8 | 9 | 整体上是实现 Cminus-f 从源码到二进制可执行程序的整个编译流程, 10 | 局部上是实现从 AST 到 LLVM IR 的翻译。 11 | 12 | ## 实验难点 13 | 14 | Cminus-f 的语法并不简单,有相当多的 corner case 需要处理。 15 | 16 | 同时由于实现的模版 visit 不带返回值、不带自定义参数,需要传入或者传出信息给下层或上层时,需借助全局变量。 17 | 这使得整个实现中各 visit 方法高度耦合,无法轻松地拆分成若干子任务以在成员间分配。 18 | 19 | ## 实验设计 20 | 21 | 1. 在“实验难点”部分提到了,为了完成方法间信息传递,使用了全局变量作为参数或返回值。 22 | (这不是一个好设计,但能在此框架下解决问题。) 23 | 24 | 另外值得一提的有两个: 25 | 26 | - 在 CompoundStmt 中,需检测 early return,我们使用了 `is_returned` 全局变量来监控,保证及时终止 27 | - 在 binary operator 的 type casting 处理中,为了避免重复代码,我们提供了一个宏函数 `BIN_CAST` 28 | 29 | 2. 隐式转换变量类型 30 | 31 | 实现了 `BIN_CAST` 宏,完成类型之间的快速转换 32 | 33 | ### 实验总结 34 | 35 | 时间安排尚不太合理 36 | 37 | 了解了 cpp 中子类和父类的转换方法 38 | 39 | 由于 cpp 缺少一些易于使用的部件,处理重复代码不太方便,使得此次实现中相当一部分代码被直接 inline 了 40 | 41 | ### 实验反馈 (可选 不会评分) 42 | 43 | 希望能加强测试样例 44 | 45 | 希望助教可以把自动测评脚本写得扩展性高一点 46 | 47 | ### 组间交流 (可选) 48 | 49 | 无 50 | -------------------------------------------------------------------------------- /Reports/lab5/contribution.md: -------------------------------------------------------------------------------- 1 | # 组员贡献 2 | 3 | ## 贡献详述 4 | 5 | ### myl7 6 | 7 | 在实验 5.1 写了 LoopSearch 部分, 8 | 在实验 5.2 实现了 ConstPropagation 和 LoopInvHoist 9 | 10 | ### yuanyiwei 11 | 12 | 在实验 5.1 写了 Mem2reg 部分, 13 | 在实验 5.2 实现了 ActiveVars 14 | 15 | ## 评定结果 16 | 17 | | 名字 | 百分比 | 18 | | :----: | :----: | 19 | | myl7 | 50% | 20 | | yuanyiwei | 50% | 21 | 22 | 百分比相加应当等于 100% 23 | 24 | 可以对特殊情况进行备注 25 | -------------------------------------------------------------------------------- /Reports/lab5/report-phase1.md: -------------------------------------------------------------------------------- 1 | # Lab5 实验报告-阶段一 2 | 3 | myl7 ***REMOVED*** 4 | 5 | yuanyiwei ***REMOVED*** 6 | 7 | ## 实验要求 8 | 9 | 理解 `Mem2Reg` 与 `LoopSearch` 两种 Pass 优化,回答思考题 10 | 11 | ## 思考题 12 | 13 | ### LoopSearch 14 | 15 | 1. 循环的入口如何确定?循环的入口的数量可能超过 1 嘛? 16 | 17 | 根据有向图强连通分量,可以找出强连通的 sccs 和每张图中的 base node。 18 | 19 | 循环的入口就是条件语句,不会超过 1。 20 | 21 | 2. 简述一下算法怎么解决循环嵌套的情况 22 | 23 | 每次找到一个 loop 后,我们删去 loop base,确保此 loop 的环被破开,再在剩余的 BB 中搜索 loop,就能找到内部的 loop。 24 | 25 | ### Mem2reg 26 | 27 | 1. 请简述支配边界的概念 28 | 29 | 支配边界的概念由一个变量拥有,表示这个变量能支配的赋值语句的集合,在此集合内的赋值语句中以此变量作为左值,从而改变了此变量。 30 | 这些赋值语句的效果将会在这个变量被使用时体现出来,因而其效果借由此变量体现,支配边界就是这些语句的范围。 31 | 32 | 2. 请简述 `phi` 节点的概念,与其存在的意义 33 | 34 | phi 节点用于辨认 SSA 模型中多个前驱所定的最终值。 35 | 36 | 由于 SSA 中,一个变量只能赋值一次,所以源码中的一个变量可能会对应 IR 中多个变量,需要使用 `Phi` 指令合并变量。 37 | 38 | 3. 请描述 `Mem2Reg Pass` 执行前后的 IR 的变化, 简述一下 39 | 40 | 在 `Mem2Reg Pass` 操作之后,IR 减少了「为了使用参数,创建新空间,把参数复制到新空间中」、「反复 `load` 某个指针」的操作, 41 | 多了 `Phi` 指令来辨认变量该使用来自哪个基本块的流。 42 | 43 | 4. 在放置 phi 节点的时候,算法是如何利用支配树的信息的 44 | 45 | 在 `Mem2Reg Pass` 中,算法先产生了一个支配树。 46 | 47 | `Mem2Reg::generate_phi()` 中取出支配树产生的支配边界(也就是 `bb_dominance_frontier_bb`),在其中每一块插入 `Phi` 指令; 48 | `Mem2Reg::re_name()` 中会用到支配树的 `dom_tree_succ_blocks_`,依次 `rename()`。 49 | 50 | 5. 算法是如何选择 `value`(变量最新的值)来替换 `load` 指令的(描述数据结构与维护方法) 51 | 52 | `var_val_stack[val]` 栈存放一些左值所可能使用的值。 53 | 54 | 遇到 load 指令,`instr->replace_all_use_with(var_val_stack[l_val].back()); wait_delete.push_back(instr);` 代码块把 load 指令的输出用 `var_val_stack[l_val]` 的栈顶值替换(在 Value.cpp 中看到所有的使用都被 new_val Value 替代),同时把该 load 指令加入要删除的队列中。 55 | 56 | 遇到 store 指令,`var_val_stack[l_val].push_back(r_val); wait_delete.push_back(instr);` 代码块把 store 指令的值作为一种新的可能加入到 `var_val_stack[l_val]` 栈中,同时把该 store 指令加入要删除的队列。 57 | 58 | 最后,根据产生的 phi 指令,从前面的 var_val_stack 栈中取出每一块 value 和基本块组成 phi 语句。 59 | 60 | ### 代码阅读总结 61 | 62 | 在此阶段下对于代码优化有了一些基本认识,希望能在后续的实现中运用起来 63 | 64 | ### 实验反馈 (可选 不会评分) 65 | 66 | 代码建议增加注释。 67 | 68 | ### 组间交流 (可选) 69 | 70 | 无 71 | -------------------------------------------------------------------------------- /Reports/lab5/score: -------------------------------------------------------------------------------- 1 | Report Phase1 2 | 4.357142857 3 | Report Phase2 4 | 10 5 | Constant Propagation 6 | 15 7 | Loop Invariants 8 | 15 9 | Active Variables 10 | 15 11 | Penalty 12 | 13 | Total 14 | 59.35714286 15 | -------------------------------------------------------------------------------- /include/lexical_analyzer.h: -------------------------------------------------------------------------------- 1 | #ifndef _LEXICAL_ANALYZER_H_ 2 | #define _LEXICAL_ANALYZER_H_ 3 | 4 | #include 5 | 6 | extern int fileno (FILE *__stream) __THROW __wur; 7 | 8 | #ifndef YYTOKENTYPE 9 | #define YYTOKENTYPE 10 | typedef enum cminus_token_type { 11 | //运算 12 | ADD = 259, 13 | SUB = 260, 14 | MUL = 261, 15 | DIV = 262, 16 | LT = 263, 17 | LTE = 264, 18 | GT = 265, 19 | GTE = 266, 20 | EQ = 267, 21 | NEQ = 268, 22 | ASSIN = 269, 23 | //符号 24 | SEMICOLON = 270, 25 | COMMA = 271, 26 | LPARENTHESE = 272, 27 | RPARENTHESE = 273, 28 | LBRACKET = 274, 29 | RBRACKET = 275, 30 | LBRACE = 276, 31 | RBRACE = 277, 32 | //关键字 33 | ELSE = 278, 34 | IF = 279, 35 | INT = 280, 36 | FLOAT = 281, 37 | RETURN = 282, 38 | VOID = 283, 39 | WHILE = 284, 40 | //ID和NUM 41 | IDENTIFIER = 285, 42 | INTEGER = 286, 43 | FLOATPOINT = 287, 44 | ARRAY = 288, 45 | LETTER = 289, 46 | //others 47 | EOL = 290, 48 | COMMENT = 291, 49 | BLANK = 292, 50 | ERROR = 258 51 | 52 | } Token; 53 | 54 | 55 | typedef struct{ 56 | char text[256]; 57 | int token; 58 | int lines; 59 | int pos_start; 60 | int pos_end; 61 | } Token_Node; 62 | 63 | #define MAX_NUM_TOKEN_NODE 1024 64 | 65 | void analyzer(char* input_file, Token_Node* token_stream); 66 | 67 | #endif /* YYTOKENTYPE */ 68 | #endif /* lexical_analyzer.h */ 69 | -------------------------------------------------------------------------------- /include/lightir/BasicBlock.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSYC_BASICBLOCK_H 2 | #define SYSYC_BASICBLOCK_H 3 | 4 | #include "Value.h" 5 | #include "Instruction.h" 6 | #include "Module.h" 7 | #include "Function.h" 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | class Function; 14 | class Instruction; 15 | class Module; 16 | 17 | class BasicBlock : public Value 18 | { 19 | public: 20 | static BasicBlock *create(Module *m, const std::string &name , 21 | Function *parent ) { 22 | auto prefix = name.empty() ? "" : "label_"; 23 | return new BasicBlock(m, prefix + name, parent); 24 | } 25 | 26 | // return parent, or null if none. 27 | Function *get_parent() { return parent_; } 28 | 29 | Module *get_module(); 30 | 31 | /****************api about cfg****************/ 32 | 33 | std::list &get_pre_basic_blocks() { return pre_bbs_; } 34 | std::list &get_succ_basic_blocks() { return succ_bbs_; } 35 | void add_pre_basic_block(BasicBlock *bb) { pre_bbs_.push_back(bb); } 36 | void add_succ_basic_block(BasicBlock *bb) { succ_bbs_.push_back(bb); } 37 | 38 | void remove_pre_basic_block(BasicBlock *bb) { pre_bbs_.remove(bb); } 39 | void remove_succ_basic_block(BasicBlock *bb) { succ_bbs_.remove(bb); } 40 | 41 | /****************api about cfg****************/ 42 | 43 | /// Returns the terminator instruction if the block is well formed or null 44 | /// if the block is not well formed. 45 | const Instruction *get_terminator() const; 46 | Instruction *get_terminator() { 47 | return const_cast( 48 | static_cast(this)->get_terminator()); 49 | } 50 | 51 | void add_instruction(Instruction *instr); 52 | void add_instr_begin(Instruction *instr); 53 | 54 | void delete_instr(Instruction *instr); 55 | 56 | bool empty() { return instr_list_.empty(); } 57 | 58 | int get_num_of_instr() { return instr_list_.size(); } 59 | std::list &get_instructions() { return instr_list_; } 60 | 61 | void erase_from_parent(); 62 | 63 | virtual std::string print() override; 64 | 65 | private: 66 | explicit BasicBlock(Module *m, const std::string &name , 67 | Function *parent ); 68 | std::list pre_bbs_; 69 | std::list succ_bbs_; 70 | std::list instr_list_; 71 | Function *parent_; 72 | 73 | }; 74 | 75 | #endif // SYSYC_BASICBLOCK_H -------------------------------------------------------------------------------- /include/lightir/Constant.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by cqy on 2020/6/29. 3 | // 4 | 5 | #ifndef SYSYC_CONSTANT_H 6 | #define SYSYC_CONSTANT_H 7 | #include "User.h" 8 | #include "Value.h" 9 | #include "Type.h" 10 | 11 | class Constant : public User 12 | { 13 | private: 14 | // int value; 15 | public: 16 | Constant(Type *ty, const std::string &name = "", unsigned num_ops = 0) 17 | : User(ty, name, num_ops) {} 18 | ~Constant() = default; 19 | }; 20 | 21 | class ConstantInt : public Constant 22 | { 23 | private: 24 | int value_; 25 | ConstantInt(Type* ty,int val) 26 | : Constant(ty,"",0),value_(val) {} 27 | public: 28 | 29 | static int get_value(ConstantInt *const_val) { return const_val->value_; } 30 | int get_value() { return value_; } 31 | static ConstantInt *get(int val, Module *m); 32 | static ConstantInt *get(bool val, Module *m); 33 | virtual std::string print() override; 34 | }; 35 | 36 | class ConstantArray : public Constant 37 | { 38 | private: 39 | std::vector const_array; 40 | 41 | ConstantArray(ArrayType *ty, const std::vector &val); 42 | public: 43 | 44 | ~ConstantArray()=default; 45 | 46 | Constant* get_element_value(int index); 47 | 48 | unsigned get_size_of_array() { return const_array.size(); } 49 | 50 | static ConstantArray *get(ArrayType *ty, const std::vector &val); 51 | 52 | virtual std::string print() override; 53 | }; 54 | 55 | class ConstantZero : public Constant 56 | { 57 | private: 58 | ConstantZero(Type *ty) 59 | : Constant(ty,"",0) {} 60 | public: 61 | static ConstantZero *get(Type *ty, Module *m); 62 | virtual std::string print() override; 63 | }; 64 | 65 | class ConstantFP : public Constant 66 | { 67 | private: 68 | float val_; 69 | ConstantFP(Type *ty, float val) 70 | : Constant(ty,"",0), val_(val) {} 71 | public: 72 | static ConstantFP *get(float val, Module *m); 73 | float get_value() { return val_; } 74 | virtual std::string print() override; 75 | }; 76 | 77 | #endif //SYSYC_CONSTANT_H 78 | -------------------------------------------------------------------------------- /include/lightir/GlobalVariable.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by cqy on 2020/6/29. 3 | // 4 | 5 | #ifndef SYSYC_GLOBALVARIABLE_H 6 | #define SYSYC_GLOBALVARIABLE_H 7 | 8 | #include "Module.h" 9 | #include "User.h" 10 | #include "Constant.h" 11 | 12 | class GlobalVariable : public User 13 | { 14 | private: 15 | bool is_const_ ; 16 | Constant* init_val_; 17 | GlobalVariable(std::string name, Module *m, Type* ty, bool is_const, Constant* init = nullptr); 18 | public: 19 | static GlobalVariable *create(std::string name, Module *m, Type* ty, bool is_const, 20 | Constant* init ); 21 | 22 | Constant *get_init() { return init_val_; } 23 | bool is_const() { return is_const_; } 24 | std::string print(); 25 | }; 26 | #endif //SYSYC_GLOBALVARIABLE_H 27 | -------------------------------------------------------------------------------- /include/lightir/IRprinter.h: -------------------------------------------------------------------------------- 1 | #include "Value.h" 2 | #include "Module.h" 3 | #include "Function.h" 4 | #include "GlobalVariable.h" 5 | #include "Constant.h" 6 | #include "BasicBlock.h" 7 | #include "Instruction.h" 8 | #include "User.h" 9 | #include "Type.h" 10 | 11 | std::string print_as_op( Value *v, bool print_ty ); 12 | std::string print_cmp_type( CmpInst::CmpOp op); 13 | std::string print_fcmp_type( FCmpInst::CmpOp op); -------------------------------------------------------------------------------- /include/lightir/Module.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSYC_MODULE_H 2 | #define SYSYC_MODULE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "Type.h" 9 | #include "GlobalVariable.h" 10 | #include "Value.h" 11 | #include "Function.h" 12 | 13 | class GlobalVariable; 14 | 15 | class Module 16 | { 17 | public: 18 | explicit Module(std::string name); 19 | ~Module(); 20 | 21 | Type *get_void_type(); 22 | Type *get_label_type(); 23 | IntegerType *get_int1_type(); 24 | IntegerType *get_int32_type(); 25 | PointerType *get_int32_ptr_type(); 26 | FloatType *get_float_type(); 27 | PointerType *get_float_ptr_type(); 28 | 29 | PointerType *get_pointer_type(Type *contained); 30 | ArrayType *get_array_type(Type *contained, unsigned num_elements); 31 | 32 | void add_function(Function *f); 33 | std::list get_functions(); 34 | void add_global_variable(GlobalVariable* g); 35 | std::list get_global_variable(); 36 | std::string get_instr_op_name( Instruction::OpID instr ) { return instr_id2string_[instr]; } 37 | void set_print_name(); 38 | virtual std::string print(); 39 | private: 40 | std::list global_list_; // The Global Variables in the module 41 | std::list function_list_; // The Functions in the module 42 | std::map value_sym_; // Symbol table for values 43 | std::map instr_id2string_; // Instruction from opid to string 44 | 45 | std::string module_name_; // Human readable identifier for the module 46 | std::string source_file_name_; // Original source file name for module, for test and debug 47 | 48 | private: 49 | IntegerType *int1_ty_; 50 | IntegerType *int32_ty_; 51 | Type *label_ty_; 52 | Type *void_ty_; 53 | FloatType *float32_ty_; 54 | 55 | std::map pointer_map_; 56 | std::map, ArrayType *> array_map_; 57 | }; 58 | 59 | #endif // SYSYC_MODULE_H -------------------------------------------------------------------------------- /include/lightir/User.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSYC_USER_H 2 | #define SYSYC_USER_H 3 | 4 | #include "Value.h" 5 | #include 6 | // #include 7 | 8 | class User : public Value 9 | { 10 | public: 11 | User(Type *ty, const std::string &name = "", unsigned num_ops = 0); 12 | ~User() = default; 13 | 14 | std::vector& get_operands(); 15 | 16 | // start from 0 17 | Value *get_operand(unsigned i) const; 18 | 19 | // start from 0 20 | void set_operand(unsigned i, Value *v); 21 | void add_operand( Value *v); 22 | 23 | unsigned get_num_operand() const; 24 | 25 | void remove_use_of_ops(); 26 | void remove_operands(int index1,int index2); 27 | 28 | private: 29 | // std::unique_ptr< std::list > operands_; // operands of this value 30 | std::vector operands_; // operands of this value 31 | unsigned num_ops_; 32 | }; 33 | 34 | #endif // SYSYC_USER_H 35 | -------------------------------------------------------------------------------- /include/lightir/Value.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSYC_VALUE_H 2 | #define SYSYC_VALUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Type; 9 | class Value; 10 | 11 | struct Use 12 | { 13 | Value *val_; 14 | unsigned arg_no_; // the no. of operand, e.g., func(a, b), a is 0, b is 1 15 | Use(Value *val, unsigned no) : val_(val), arg_no_(no) {} 16 | }; 17 | 18 | class Value 19 | { 20 | public: 21 | explicit Value(Type *ty, const std::string &name = ""); 22 | ~Value() = default; 23 | 24 | Type *get_type() const { return type_; } 25 | 26 | std::list &get_use_list() { return use_list_; } 27 | 28 | void add_use(Value *val, unsigned arg_no = 0); 29 | 30 | bool set_name(std::string name) { 31 | if (name_ == "") 32 | { 33 | name_=name; 34 | return true; 35 | } 36 | return false; 37 | } 38 | std::string get_name() const; 39 | 40 | void replace_all_use_with(Value *new_val); 41 | void remove_use(Value *val); 42 | 43 | virtual std::string print() = 0; 44 | private: 45 | Type *type_; 46 | std::list use_list_; // who use this value 47 | std::string name_; // should we put name field here ? 48 | }; 49 | 50 | #endif // SYSYC_VALUE_H 51 | -------------------------------------------------------------------------------- /include/logging.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOGGING_HPP 2 | #define LOGGING_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | enum LogLevel 9 | { 10 | DEBUG = 0, 11 | INFO, 12 | WARNING, 13 | ERROR 14 | }; 15 | struct LocationInfo 16 | { 17 | LocationInfo(std::string file, int line, const char *func) : file_(file), line_(line), func_(func) {} 18 | ~LocationInfo() = default; 19 | 20 | std::string file_; 21 | int line_; 22 | const char *func_; 23 | }; 24 | class LogStream; 25 | class LogWriter; 26 | 27 | class LogWriter 28 | { 29 | public: 30 | LogWriter(LocationInfo location, LogLevel loglevel) 31 | : location_(location), log_level_(loglevel) 32 | { 33 | char *logv = std::getenv("LOGV"); 34 | if (logv) 35 | { 36 | std::string string_logv = logv; 37 | env_log_level = std::stoi(logv); 38 | } 39 | else 40 | { 41 | env_log_level = 4; 42 | } 43 | }; 44 | 45 | void operator<(const LogStream &stream); 46 | 47 | private: 48 | void output_log(const std::ostringstream &g); 49 | LocationInfo location_; 50 | LogLevel log_level_; 51 | int env_log_level; 52 | }; 53 | 54 | class LogStream 55 | { 56 | public: 57 | LogStream() { sstream_ = new std::stringstream(); } 58 | ~LogStream() = default; 59 | 60 | template 61 | LogStream &operator<<(const T &val) noexcept 62 | { 63 | (*sstream_) << val; 64 | return *this; 65 | } 66 | 67 | friend class LogWriter; 68 | 69 | private: 70 | std::stringstream *sstream_; 71 | }; 72 | 73 | std::string level2string(LogLevel level); 74 | std::string get_short_name(const char *file_path); 75 | 76 | #define __FILESHORTNAME__ get_short_name(__FILE__) 77 | #define LOG_IF(level) \ 78 | LogWriter(LocationInfo(__FILESHORTNAME__, __LINE__, __FUNCTION__), level) < LogStream() 79 | #define LOG(level) LOG_##level 80 | #define LOG_DEBUG LOG_IF(DEBUG) 81 | #define LOG_INFO LOG_IF(INFO) 82 | #define LOG_WARNING LOG_IF(WARNING) 83 | #define LOG_ERROR LOG_IF(ERROR) 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /include/optimization/ActiveVars.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVEVARS_HPP 2 | #define ACTIVEVARS_HPP 3 | #include "PassManager.hpp" 4 | #include "Constant.h" 5 | #include "Instruction.h" 6 | #include "Module.h" 7 | 8 | #include "Value.h" 9 | #include "IRBuilder.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | class ActiveVars : public Pass 18 | { 19 | public: 20 | ActiveVars(Module *m) : Pass(m) {} 21 | void run(); 22 | std::string print(); 23 | private: 24 | Function *func_; 25 | std::map> live_in, live_out; 26 | }; 27 | 28 | #endif -------------------------------------------------------------------------------- /include/optimization/ConstPropagation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONSTPROPAGATION_HPP 2 | #define CONSTPROPAGATION_HPP 3 | #include "PassManager.hpp" 4 | #include "Constant.h" 5 | #include "Instruction.h" 6 | #include "Module.h" 7 | 8 | #include "Value.h" 9 | #include "IRBuilder.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // tips: 用来判断value是否为ConstantFP/ConstantInt 16 | ConstantFP *cast_constantfp(Value *value); 17 | ConstantInt *cast_constantint(Value *value); 18 | 19 | typedef std::map ConstMap; 20 | 21 | // tips: ConstFloder类 22 | 23 | class ConstFolder 24 | { 25 | public: 26 | ConstFolder(Module *m) : module_(m) {} 27 | Constant *compute2(Instruction::OpID op, Constant *value1, Constant *value2); 28 | Constant *compute2cmp(CmpInst::CmpOp op, Constant *a, Constant *b); 29 | Constant *compute2fcmp(FCmpInst::CmpOp op, Constant *a, Constant *b); 30 | Constant *compute1(Instruction::OpID op, Constant *a); 31 | Constant *compute(Instruction *ins, std::vector is_const_args); 32 | 33 | private: 34 | Module *module_; 35 | }; 36 | 37 | class ConstPropagation : public Pass 38 | { 39 | public: 40 | ConstPropagation(Module *m) : Pass(m) {} 41 | void run(); 42 | 43 | private: 44 | void pass_bb(BasicBlock *bb, ConstMap const_map); 45 | 46 | std::set bb_passed_set; 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/optimization/LoopInvHoist.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "PassManager.hpp" 6 | #include "Module.h" 7 | #include "Function.h" 8 | #include "BasicBlock.h" 9 | 10 | class LoopInvHoist : public Pass { 11 | public: 12 | LoopInvHoist(Module *m) : Pass(m) {} 13 | 14 | void run() override; 15 | }; 16 | 17 | BasicBlock *find_common_parent(BasicBlock *a, BasicBlock *b, BBset_t *bb_set); 18 | -------------------------------------------------------------------------------- /include/optimization/LoopSearch.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOOPSEARCH_HPP 2 | #define LOOPSEARCH_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include "PassManager.hpp" 8 | #include "Function.h" 9 | #include "BasicBlock.h" 10 | #include 11 | #include 12 | struct CFGNode; 13 | using CFGNodePtr = CFGNode*; 14 | using CFGNodePtrSet = std::unordered_set; 15 | using BBset_t = std::unordered_set; 16 | 17 | 18 | 19 | class LoopSearch : public Pass{ 20 | public: 21 | explicit LoopSearch(Module* m, bool dump=false) : Pass(m), dump(dump){} 22 | ~LoopSearch() = default; 23 | void build_cfg(Function *func,std::unordered_set &result); 24 | void run() override; 25 | bool strongly_connected_components( 26 | CFGNodePtrSet &nodes, 27 | std::unordered_set &result 28 | ); 29 | void dump_graph(CFGNodePtrSet& nodes,std::string title); 30 | void traverse( 31 | CFGNodePtr n, 32 | std::unordered_set &result); 33 | CFGNodePtr find_loop_base( 34 | CFGNodePtrSet *set, 35 | CFGNodePtrSet &reserved); 36 | 37 | // 利用iterator来遍历所有的loop 38 | auto begin(){ return loop_set.begin();} 39 | auto end(){ return loop_set.end(); } 40 | 41 | BasicBlock* get_loop_base(BBset_t *loop) { return loop2base[loop]; } 42 | 43 | // 得到bb所在最低层次的loop 44 | BBset_t *get_inner_loop(BasicBlock* bb){ 45 | if(bb2base.find(bb) == bb2base.end()) 46 | return nullptr; 47 | return base2loop[bb2base[bb]]; 48 | } 49 | 50 | // 得到输入loop的外一层的循环,如果没有则返回空 51 | BBset_t *get_parent_loop(BBset_t *loop); 52 | 53 | // 得到函数 f 里的所有循环 54 | std::unordered_set get_loops_in_func(Function *f); 55 | 56 | 57 | 58 | private: 59 | int index_count; 60 | bool dump; 61 | std::vector stack; 62 | // loops found 63 | std::unordered_set loop_set; 64 | // loops found in a function 65 | std::unordered_map> func2loop; 66 | // { entry bb of loop : loop } 67 | std::unordered_map base2loop; 68 | // { loop : entry bb of loop } 69 | std::unordered_map loop2base; 70 | // { bb : entry bb of loop} 默认最低层次的loop 71 | std::unordered_map bb2base; 72 | }; 73 | 74 | 75 | 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /include/optimization/Mem2Reg.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SYSYC_MEM2REG_HPP 2 | #define SYSYC_MEM2REG_HPP 3 | 4 | #include "Module.h" 5 | #include "Function.h" 6 | #include "IRBuilder.h" 7 | #include "BasicBlock.h" 8 | #include "Instruction.h" 9 | #include "PassManager.hpp" 10 | #include "Dominators.h" 11 | 12 | class Mem2Reg : public Pass 13 | { 14 | private: 15 | Function * func_; 16 | Dominators *dominators_; 17 | 18 | public: 19 | Mem2Reg(Module *m) : Pass(m){} 20 | ~Mem2Reg(){}; 21 | void run() override; 22 | void generate_phi(); 23 | void re_name(BasicBlock *bb); 24 | void remove_alloca(); 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /include/optimization/PassManager.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PASSMANAGER_HPP 2 | #define PASSMANAGER_HPP 3 | 4 | 5 | 6 | #include "Module.h" 7 | #include 8 | #include 9 | // using PassPtr = ; 10 | // using PassPtrList=; 11 | class Pass{ 12 | public: 13 | Pass(Module* m) : m_(m){ 14 | } 15 | 16 | virtual void run()=0; 17 | 18 | protected: 19 | 20 | Module* m_; 21 | }; 22 | 23 | class PassManager{ 24 | public: 25 | PassManager(Module* m) : m_(m){} 26 | template void add_pass(bool print_ir=false){ 27 | passes_.push_back(std::pair(new PassType(m_),print_ir)); 28 | } 29 | void run(){ 30 | for(auto pass : passes_){ 31 | pass.first->run(); 32 | if(pass.second){ 33 | std::cout<print(); 34 | } 35 | } 36 | } 37 | 38 | 39 | private: 40 | std::vector> passes_; 41 | Module* m_; 42 | 43 | }; 44 | 45 | #endif -------------------------------------------------------------------------------- /include/syntax_tree.h: -------------------------------------------------------------------------------- 1 | #ifndef __SYNTAXTREE_H__ 2 | #define __SYNTAXTREE_H__ 3 | 4 | #include 5 | 6 | #define SYNTAX_TREE_NODE_NAME_MAX 30 7 | 8 | struct _syntax_tree_node { 9 | struct _syntax_tree_node * parent; 10 | struct _syntax_tree_node * children[10]; 11 | int children_num; 12 | 13 | char name[SYNTAX_TREE_NODE_NAME_MAX]; 14 | }; 15 | typedef struct _syntax_tree_node syntax_tree_node; 16 | 17 | syntax_tree_node * new_anon_syntax_tree_node(); 18 | syntax_tree_node * new_syntax_tree_node(const char * name); 19 | int syntax_tree_add_child(syntax_tree_node * parent, syntax_tree_node * child); 20 | void del_syntax_tree_node(syntax_tree_node * node, int recursive); 21 | 22 | struct _syntax_tree { 23 | syntax_tree_node * root; 24 | }; 25 | typedef struct _syntax_tree syntax_tree; 26 | 27 | syntax_tree* new_syntax_tree(); 28 | void del_syntax_tree(syntax_tree * tree); 29 | void print_syntax_tree(FILE * fout, syntax_tree * tree); 30 | 31 | #endif /* SyntaxTree.h */ 32 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(lexer) 2 | add_subdirectory(parser) 3 | add_subdirectory(io) 4 | add_subdirectory(common) 5 | add_subdirectory(lightir) 6 | add_subdirectory(cminusfc) 7 | add_subdirectory(optimization) 8 | -------------------------------------------------------------------------------- /src/cminusfc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable( 2 | cminusfc 3 | cminusfc.cpp 4 | cminusf_builder.cpp 5 | ) 6 | 7 | # target_compile_options( 8 | # cminusc 9 | # PRIVATE "-fno-rtti" 10 | # ) 11 | 12 | target_link_libraries( 13 | cminusfc 14 | OP_lib 15 | IR_lib 16 | common 17 | syntax 18 | ) 19 | 20 | install( 21 | TARGETS cminusfc 22 | RUNTIME DESTINATION bin 23 | ) 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(common STATIC 2 | syntax_tree.c 3 | ast.cpp 4 | logging.cpp 5 | ) 6 | 7 | target_link_libraries(common) 8 | 9 | -------------------------------------------------------------------------------- /src/common/logging.cpp: -------------------------------------------------------------------------------- 1 | #include "logging.hpp" 2 | 3 | void LogWriter::operator<(const LogStream &stream) { 4 | std::ostringstream msg; 5 | msg << stream.sstream_->rdbuf(); 6 | output_log(msg); 7 | } 8 | 9 | void LogWriter::output_log(const std::ostringstream &msg) { 10 | if (log_level_ >= env_log_level) 11 | std::cout << "[" << level2string(log_level_) << "] " 12 | << "(" << location_.file_ 13 | << ":" << location_.line_ 14 | << "L "<< location_.func_<<")" 15 | << msg.str() << std::endl; 16 | 17 | } 18 | std::string level2string(LogLevel level) { 19 | switch (level) 20 | { 21 | case DEBUG: 22 | return "DEBUG"; 23 | 24 | case INFO: 25 | return "INFO"; 26 | 27 | case WARNING: 28 | return "WARNING"; 29 | 30 | case ERROR: 31 | return "ERROR"; 32 | 33 | default: 34 | return ""; 35 | } 36 | } 37 | std::string get_short_name(const char * file_path) { 38 | std::string short_file_path = file_path; 39 | int index = short_file_path.find_last_of('/'); 40 | 41 | return short_file_path.substr(index+1); 42 | } 43 | -------------------------------------------------------------------------------- /src/common/syntax_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "syntax_tree.h" 5 | 6 | syntax_tree_node * new_syntax_tree_node(const char * name) 7 | { 8 | syntax_tree_node * new_node = (syntax_tree_node *)malloc(sizeof(syntax_tree_node)); 9 | if (name) 10 | strncpy(new_node->name, name, SYNTAX_TREE_NODE_NAME_MAX); 11 | else 12 | new_node->name[0] = '\0'; 13 | new_node->children_num = 0; 14 | return new_node; 15 | } 16 | 17 | int syntax_tree_add_child(syntax_tree_node * parent, syntax_tree_node * child) 18 | { 19 | if (!parent || !child) return -1; 20 | parent->children[parent->children_num++] = child; 21 | return parent->children_num; 22 | } 23 | 24 | void del_syntax_tree_node(syntax_tree_node * node, int recursive) 25 | { 26 | if (!node) return; 27 | 28 | int i; 29 | if (recursive) { 30 | for (i = 0; i < node->children_num; i++) { 31 | del_syntax_tree_node(node->children[i], 1); 32 | } 33 | } 34 | free(node); 35 | } 36 | 37 | syntax_tree * new_syntax_tree() 38 | { 39 | return (syntax_tree *)malloc(sizeof(syntax_tree)); 40 | } 41 | 42 | void del_syntax_tree(syntax_tree * tree) 43 | { 44 | if (!tree) return; 45 | 46 | if (tree->root) { 47 | del_syntax_tree_node(tree->root, 1); 48 | } 49 | free(tree); 50 | } 51 | 52 | void print_syntax_tree_node(FILE * fout, syntax_tree_node * node, int level) 53 | { 54 | // assume fout valid now 55 | 56 | // check if "node" empty pointer 57 | if (!node) return; 58 | 59 | // print myself 60 | int i; 61 | for (i = 0; i < level; i++) { 62 | fprintf(fout, "| "); 63 | } 64 | fprintf(fout, ">--%s %s\n", (node->children_num ? "+" : "*"), node->name); 65 | 66 | for (i = 0; i < node->children_num; i++) { 67 | print_syntax_tree_node(fout, node->children[i], level + 1); 68 | } 69 | } 70 | 71 | void print_syntax_tree(FILE * fout, syntax_tree * tree) 72 | { 73 | if (!fout) return; 74 | 75 | print_syntax_tree_node(fout, tree->root, 0); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/io/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(cminus_io io.c) 2 | 3 | install( 4 | TARGETS cminus_io 5 | ARCHIVE DESTINATION lib 6 | ) 7 | -------------------------------------------------------------------------------- /src/io/io.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int input() { 4 | int a; 5 | scanf("%d", &a); 6 | return a; 7 | } 8 | 9 | void output(int a) { 10 | printf("%d\n", a); 11 | } 12 | 13 | void outputFloat(float a) { 14 | printf("%f\n", a); 15 | } 16 | 17 | void neg_idx_except() { 18 | printf("negative index exception\n"); 19 | exit(0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /src/lexer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | flex_target(lex lexical_analyzer.l ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c) 2 | add_library(flex STATIC 3 | lex.yy.c 4 | ) 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/lightir/BasicBlock.cpp: -------------------------------------------------------------------------------- 1 | #include "Module.h" 2 | #include "BasicBlock.h" 3 | #include "Function.h" 4 | #include "IRprinter.h" 5 | #include 6 | 7 | BasicBlock::BasicBlock(Module *m, const std::string &name = "", 8 | Function *parent = nullptr) 9 | : Value(Type::get_label_type(m), name), parent_(parent) 10 | { 11 | assert(parent && "currently parent should not be nullptr"); 12 | parent_->add_basic_block(this); 13 | } 14 | 15 | Module *BasicBlock::get_module() 16 | { 17 | return get_parent()->get_parent(); 18 | } 19 | 20 | void BasicBlock::add_instruction(Instruction *instr) 21 | { 22 | instr_list_.push_back(instr); 23 | } 24 | 25 | void BasicBlock::add_instr_begin(Instruction *instr) 26 | { 27 | instr_list_.push_front(instr); 28 | } 29 | 30 | void BasicBlock::delete_instr( Instruction *instr ) 31 | { 32 | instr_list_.remove(instr); 33 | instr->remove_use_of_ops(); 34 | } 35 | 36 | const Instruction *BasicBlock::get_terminator() const 37 | { 38 | if (instr_list_.empty()){ 39 | return nullptr; 40 | } 41 | switch (instr_list_.back()->get_instr_type()) 42 | { 43 | case Instruction::ret: 44 | return instr_list_.back(); 45 | break; 46 | 47 | case Instruction::br: 48 | return instr_list_.back(); 49 | break; 50 | 51 | default: 52 | return nullptr; 53 | break; 54 | } 55 | } 56 | 57 | void BasicBlock::erase_from_parent() 58 | { 59 | this->get_parent()->remove(this); 60 | } 61 | 62 | std::string BasicBlock::print() 63 | { 64 | std::string bb_ir; 65 | bb_ir += this->get_name(); 66 | bb_ir += ":"; 67 | // print prebb 68 | if(!this->get_pre_basic_blocks().empty()) 69 | { 70 | bb_ir += " ; preds = "; 71 | } 72 | for (auto bb : this->get_pre_basic_blocks() ) 73 | { 74 | if( bb != *this->get_pre_basic_blocks().begin() ) 75 | bb_ir += ", "; 76 | bb_ir += print_as_op(bb, false); 77 | } 78 | 79 | // print prebb 80 | if ( !this->get_parent() ) 81 | { 82 | bb_ir += "\n"; 83 | bb_ir += "; Error: Block without parent!"; 84 | } 85 | bb_ir += "\n"; 86 | for ( auto instr : this->get_instructions() ) 87 | { 88 | bb_ir += " "; 89 | bb_ir += instr->print(); 90 | bb_ir += "\n"; 91 | } 92 | 93 | return bb_ir; 94 | } -------------------------------------------------------------------------------- /src/lightir/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library( 2 | IR_lib STATIC 3 | Type.cpp 4 | User.cpp 5 | Value.cpp 6 | BasicBlock.cpp 7 | Constant.cpp 8 | Function.cpp 9 | GlobalVariable.cpp 10 | Instruction.cpp 11 | Module.cpp 12 | IRprinter.cpp 13 | ) -------------------------------------------------------------------------------- /src/lightir/Constant.cpp: -------------------------------------------------------------------------------- 1 | #include "Constant.h" 2 | #include "Module.h" 3 | #include 4 | #include 5 | 6 | ConstantInt *ConstantInt::get(int val, Module *m) 7 | { 8 | return new ConstantInt(Type::get_int32_type(m), val); 9 | } 10 | ConstantInt *ConstantInt::get(bool val, Module *m) 11 | { 12 | return new ConstantInt(Type::get_int1_type(m),val?1:0); 13 | } 14 | std::string ConstantInt::print() 15 | { 16 | std::string const_ir; 17 | Type *ty = this->get_type(); 18 | if ( ty->is_integer_type() && static_cast(ty)->get_num_bits() == 1 ) 19 | { 20 | //int1 21 | const_ir += (this->get_value() == 0) ? "false" : "true"; 22 | } 23 | else 24 | { 25 | //int32 26 | const_ir += std::to_string(this->get_value()); 27 | } 28 | return const_ir; 29 | } 30 | 31 | ConstantArray::ConstantArray(ArrayType *ty, const std::vector &val) 32 | : Constant(ty, "", val.size()) 33 | { 34 | for (int i = 0; i < val.size(); i++) 35 | set_operand(i, val[i]); 36 | this->const_array.assign(val.begin(),val.end()); 37 | } 38 | 39 | Constant *ConstantArray::get_element_value(int index) { 40 | return this->const_array[index]; 41 | } 42 | 43 | ConstantArray *ConstantArray::get(ArrayType *ty, const std::vector &val) 44 | { 45 | return new ConstantArray(ty, val); 46 | } 47 | 48 | std::string ConstantArray::print() 49 | { 50 | std::string const_ir; 51 | const_ir += this->get_type()->print(); 52 | const_ir += " "; 53 | const_ir += "["; 54 | for ( int i = 0 ; i < this->get_size_of_array() ; i++ ) 55 | { 56 | const_ir += get_element_value(i)->print(); 57 | const_ir += ", "; 58 | } 59 | return const_ir; 60 | } 61 | 62 | ConstantFP *ConstantFP::get(float val, Module *m) 63 | { 64 | return new ConstantFP(Type::get_float_type(m), val); 65 | } 66 | 67 | std::string ConstantFP::print() 68 | { 69 | std::stringstream fp_ir_ss; 70 | std::string fp_ir; 71 | double val = this->get_value(); 72 | fp_ir_ss << "0x"<< std::hex << *(uint64_t *)&val << std::endl; 73 | fp_ir_ss >> fp_ir; 74 | return fp_ir; 75 | } 76 | 77 | ConstantZero *ConstantZero::get(Type *ty, Module *m) 78 | { 79 | return new ConstantZero(ty); 80 | } 81 | 82 | std::string ConstantZero::print() 83 | { 84 | return "zeroinitializer"; 85 | } 86 | -------------------------------------------------------------------------------- /src/lightir/GlobalVariable.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by cqy on 2020/6/29. 3 | // 4 | #include "GlobalVariable.h" 5 | #include "IRprinter.h" 6 | 7 | GlobalVariable::GlobalVariable( std::string name, Module *m, Type* ty, bool is_const, Constant* init) 8 | : User(ty, name, init != nullptr), is_const_(is_const), init_val_(init) 9 | { 10 | m->add_global_variable(this); 11 | if (init) { 12 | this->set_operand(0, init); 13 | } 14 | }//global操作数为initval 15 | 16 | GlobalVariable *GlobalVariable::create(std::string name, Module *m, Type* ty, bool is_const, 17 | Constant* init = nullptr) 18 | { 19 | return new GlobalVariable(name, m, PointerType::get(ty), is_const, init); 20 | } 21 | 22 | std::string GlobalVariable::print() 23 | { 24 | std::string global_val_ir; 25 | global_val_ir += print_as_op(this, false); 26 | global_val_ir += " = "; 27 | global_val_ir += (this->is_const() ? "constant " : "global "); 28 | global_val_ir += this->get_type()->get_pointer_element_type()->print(); 29 | global_val_ir += " "; 30 | global_val_ir += this->get_init()->print(); 31 | return global_val_ir; 32 | } -------------------------------------------------------------------------------- /src/lightir/IRprinter.cpp: -------------------------------------------------------------------------------- 1 | #include "IRprinter.h" 2 | 3 | std::string print_as_op( Value *v, bool print_ty ) 4 | { 5 | std::string op_ir; 6 | if( print_ty ) 7 | { 8 | op_ir += v->get_type()->print(); 9 | op_ir += " "; 10 | } 11 | 12 | if (dynamic_cast(v)) 13 | { 14 | op_ir += "@"+v->get_name(); 15 | } 16 | else if ( dynamic_cast(v) ) 17 | { 18 | op_ir += "@"+v->get_name(); 19 | } 20 | else if ( dynamic_cast(v)) 21 | { 22 | op_ir += v->print(); 23 | } 24 | else 25 | { 26 | op_ir += "%"+v->get_name(); 27 | } 28 | 29 | return op_ir; 30 | } 31 | 32 | std::string print_cmp_type( CmpInst::CmpOp op ) 33 | { 34 | switch (op) 35 | { 36 | case CmpInst::GE: 37 | return "sge"; 38 | break; 39 | case CmpInst::GT: 40 | return "sgt"; 41 | break; 42 | case CmpInst::LE: 43 | return "sle"; 44 | break; 45 | case CmpInst::LT: 46 | return "slt"; 47 | break; 48 | case CmpInst::EQ: 49 | return "eq"; 50 | break; 51 | case CmpInst::NE: 52 | return "ne"; 53 | break; 54 | default: 55 | break; 56 | } 57 | return "wrong cmpop"; 58 | } 59 | 60 | std::string print_fcmp_type( FCmpInst::CmpOp op ) 61 | { 62 | switch (op) 63 | { 64 | case FCmpInst::GE: 65 | return "uge"; 66 | break; 67 | case FCmpInst::GT: 68 | return "ugt"; 69 | break; 70 | case FCmpInst::LE: 71 | return "ule"; 72 | break; 73 | case FCmpInst::LT: 74 | return "ult"; 75 | break; 76 | case FCmpInst::EQ: 77 | return "ueq"; 78 | break; 79 | case FCmpInst::NE: 80 | return "une"; 81 | break; 82 | default: 83 | break; 84 | } 85 | return "wrong fcmpop"; 86 | } -------------------------------------------------------------------------------- /src/lightir/User.cpp: -------------------------------------------------------------------------------- 1 | #include "User.h" 2 | #include 3 | 4 | User::User(Type *ty, const std::string &name , unsigned num_ops ) 5 | : Value(ty, name), num_ops_(num_ops) 6 | { 7 | // if (num_ops_ > 0) 8 | // operands_.reset(new std::list()); 9 | operands_.resize(num_ops_, nullptr); 10 | } 11 | 12 | std::vector& User::get_operands() 13 | { 14 | return operands_; 15 | } 16 | 17 | Value *User::get_operand(unsigned i) const 18 | { 19 | return operands_[i]; 20 | } 21 | 22 | void User::set_operand(unsigned i, Value *v) 23 | { 24 | assert(i < num_ops_ && "set_operand out of index"); 25 | // assert(operands_[i] == nullptr && "ith operand is not null"); 26 | operands_[i] = v; 27 | v->add_use(this, i); 28 | } 29 | 30 | void User::add_operand( Value *v) 31 | { 32 | operands_.push_back(v); 33 | v->add_use(this, num_ops_); 34 | num_ops_++; 35 | } 36 | 37 | unsigned User::get_num_operand() const 38 | { 39 | return num_ops_; 40 | } 41 | 42 | void User::remove_use_of_ops() 43 | { 44 | for (auto op : operands_) { 45 | op->remove_use(this); 46 | } 47 | } 48 | 49 | void User::remove_operands(int index1,int index2){ 50 | for(int i=index1;i<=index2;i++){ 51 | operands_[i]->remove_use(this); 52 | } 53 | operands_.erase(operands_.begin()+index1,operands_.begin()+index2+1); 54 | // std::cout< 5 | 6 | Value::Value(Type *ty, const std::string &name ) 7 | : type_(ty), name_(name) 8 | { 9 | 10 | } 11 | 12 | void Value::add_use(Value *val, unsigned arg_no ) 13 | { 14 | use_list_.push_back(Use(val, arg_no)); 15 | } 16 | 17 | std::string Value::get_name() const 18 | { 19 | return name_; 20 | } 21 | 22 | void Value::replace_all_use_with(Value *new_val) 23 | { 24 | for (auto use : use_list_) { 25 | auto val = dynamic_cast(use.val_); 26 | assert(val && "new_val is not a user"); 27 | val->set_operand(use.arg_no_, new_val); 28 | } 29 | } 30 | 31 | void Value::remove_use(Value *val) 32 | { 33 | auto is_val = [val] (const Use &use) { return use.val_ == val; }; 34 | use_list_.remove_if(is_val); 35 | } -------------------------------------------------------------------------------- /src/optimization/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library( 2 | OP_lib STATIC 3 | Dominators.cpp 4 | Mem2Reg.cpp 5 | LoopSearch.cpp 6 | ConstPropagation.cpp 7 | ActiveVars.cpp 8 | LoopInvHoist.cpp 9 | ) 10 | -------------------------------------------------------------------------------- /src/parser/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | flex_target(lex lexical_analyzer.l ${CMAKE_CURRENT_BINARY_DIR}/lexical_analyzer.c) 2 | bison_target(syntax syntax_analyzer.y ${CMAKE_CURRENT_BINARY_DIR}/syntax_analyzer.c) 3 | add_flex_bison_dependency(lex syntax) 4 | add_library(syntax STATIC 5 | ${BISON_syntax_OUTPUTS} 6 | ${FLEX_lex_OUTPUTS} 7 | ) 8 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(lab1) 2 | add_subdirectory(lab2) 3 | add_executable(test_ast test_ast.cpp) 4 | add_executable(test_logging test_logging.cpp) 5 | target_link_libraries(test_logging common) 6 | target_link_libraries(test_ast syntax common) 7 | add_subdirectory(lab3) 8 | -------------------------------------------------------------------------------- /tests/lab1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(lexer main.c) 2 | target_link_libraries(lexer flex) -------------------------------------------------------------------------------- /tests/lab1/LAB1_SCORE: -------------------------------------------------------------------------------- 1 | git : PASS 2 | 1.cminus : PASS 3 | 2.cminus : PASS 4 | 3.cminus : PASS 5 | 4.cminus : PASS 6 | 5.cminus : PASS 7 | 6.cminus : PASS 8 | 7.cminus : FAIL 9 | 8.cminus : FAIL 10 | 9.cminus : PASS 11 | 10.cminus : PASS -------------------------------------------------------------------------------- /tests/lab1/TA_token/1.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | gcd 285 1 5 8 3 | ( 272 1 9 10 4 | int 280 1 10 13 5 | u 285 1 14 15 6 | , 271 1 15 16 7 | int 280 1 17 20 8 | v 285 1 21 22 9 | , 271 1 22 23 10 | int 280 1 24 27 11 | e 285 1 28 29 12 | [] 288 1 29 31 13 | ) 273 1 31 32 14 | { 276 1 33 34 15 | if 279 2 3 5 16 | ( 272 2 6 7 17 | v 285 2 7 8 18 | == 267 2 9 11 19 | 0 286 2 12 13 20 | ) 273 2 13 14 21 | return 282 2 15 21 22 | u 285 2 22 23 23 | ; 270 2 23 24 24 | else 278 3 3 7 25 | return 282 3 8 14 26 | gcd 285 3 15 18 27 | ( 272 3 18 19 28 | v 285 3 19 20 29 | , 271 3 20 21 30 | u 285 3 22 23 31 | - 260 3 24 25 32 | u 285 3 26 27 33 | / 262 3 28 29 34 | v 285 3 30 31 35 | * 261 3 32 33 36 | v 285 3 34 35 37 | ) 273 3 35 36 38 | ; 270 3 36 37 39 | } 277 4 1 2 40 | int 280 5 1 4 41 | main 285 5 5 9 42 | ( 272 5 9 10 43 | void 283 5 10 14 44 | ) 273 5 14 15 45 | { 276 5 16 17 46 | int 280 6 6 9 47 | emptyArray 285 6 10 20 48 | [ 274 6 20 21 49 | 10 286 6 21 23 50 | ] 275 6 23 24 51 | ; 270 6 24 25 52 | int 280 7 3 6 53 | x 285 7 7 8 54 | ; 270 7 8 9 55 | int 280 7 10 13 56 | y 285 7 14 15 57 | ; 270 7 15 16 58 | int 280 7 17 20 59 | temp 285 7 21 25 60 | ; 270 7 25 26 61 | x 285 8 3 4 62 | = 269 8 5 6 63 | 72 286 8 7 9 64 | ; 270 8 9 10 65 | y 285 9 3 4 66 | = 269 9 5 6 67 | 18 286 9 7 9 68 | ; 270 9 9 10 69 | if 279 10 3 5 70 | ( 272 10 6 7 71 | x 285 10 7 8 72 | < 263 10 8 9 73 | y 285 10 9 10 74 | ) 273 10 10 11 75 | { 276 10 12 13 76 | temp 285 11 5 9 77 | = 269 11 10 11 78 | x 285 11 12 13 79 | ; 270 11 13 14 80 | x 285 12 5 6 81 | = 269 12 7 8 82 | y 285 12 9 10 83 | ; 270 12 10 11 84 | y 285 13 5 6 85 | = 269 13 7 8 86 | temp 285 13 9 13 87 | ; 270 13 13 14 88 | } 277 14 3 4 89 | gcd 285 15 6 9 90 | ( 272 15 9 10 91 | x 285 15 10 11 92 | , 271 15 11 12 93 | y 285 15 12 13 94 | , 271 15 13 14 95 | emptyArray 285 15 14 24 96 | ) 273 15 24 25 97 | ; 270 15 25 26 98 | return 282 16 3 9 99 | 0 286 16 10 11 100 | ; 270 16 11 12 101 | } 277 17 1 2 102 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/10.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | main 285 1 5 9 3 | ( 272 1 9 10 4 | void 283 1 10 14 5 | ) 273 1 14 15 6 | { 276 1 16 17 7 | int 280 2 5 8 8 | temp 285 2 9 13 9 | ; 270 2 13 14 10 | x 285 3 5 6 11 | = 269 3 7 8 12 | 72 286 3 9 11 13 | ; 270 3 11 12 14 | y 285 4 5 6 15 | = 269 4 7 8 16 | 18. 287 4 9 12 17 | ; 270 4 12 13 18 | x 285 5 5 6 19 | = 269 5 7 8 20 | x 285 5 9 10 21 | / 262 5 10 11 22 | .1 287 9 7 9 23 | * 261 10 7 8 24 | y 285 10 8 9 25 | ; 270 10 9 10 26 | return 282 11 5 11 27 | 0 286 11 12 13 28 | ; 270 11 13 14 29 | } 277 12 1 2 30 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/2.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | gcd 285 1 5 8 3 | ( 272 1 9 10 4 | float 281 1 10 15 5 | u 285 1 16 17 6 | , 271 1 17 18 7 | float 281 1 19 24 8 | v 285 1 25 26 9 | ) 273 1 26 27 10 | { 276 1 28 29 11 | if 279 2 5 7 12 | ( 272 2 8 9 13 | v 285 2 9 10 14 | < 263 2 11 12 15 | 0.001 287 2 13 18 16 | ) 273 2 18 19 17 | return 282 2 20 26 18 | u 285 2 27 28 19 | ; 270 2 28 29 20 | else 278 3 5 9 21 | return 282 3 10 16 22 | gcd 285 3 17 20 23 | ( 272 3 20 21 24 | v 285 3 21 22 25 | , 271 3 22 23 26 | u 285 3 24 25 27 | - 260 3 26 27 28 | u 285 3 28 29 29 | / 262 3 30 31 30 | v 285 3 32 33 31 | * 261 3 34 35 32 | v 285 3 36 37 33 | ) 273 3 37 38 34 | ; 270 3 38 39 35 | } 277 4 1 2 36 | int 280 5 1 4 37 | main 285 5 5 9 38 | ( 272 5 9 10 39 | void 283 5 10 14 40 | ) 273 5 14 15 41 | { 276 5 16 17 42 | float 281 6 5 10 43 | x 285 6 11 12 44 | ; 270 6 12 13 45 | float 281 6 14 19 46 | y 285 6 20 21 47 | ; 270 6 21 22 48 | float 281 6 23 28 49 | temp 285 6 29 33 50 | ; 270 6 33 34 51 | x 285 7 5 6 52 | = 269 7 7 8 53 | 72. 287 7 9 12 54 | ; 270 7 12 13 55 | y 285 8 5 6 56 | = 269 8 7 8 57 | 18. 287 8 9 12 58 | ; 270 8 12 13 59 | if 279 9 5 7 60 | ( 272 9 8 9 61 | x 285 9 9 10 62 | < 263 9 10 11 63 | y 285 9 11 12 64 | ) 273 9 12 13 65 | { 276 9 14 15 66 | temp 285 10 9 13 67 | = 269 10 14 15 68 | x 285 10 16 17 69 | ; 270 10 17 18 70 | x 285 11 9 10 71 | = 269 11 11 12 72 | y 285 11 13 14 73 | ; 270 11 14 15 74 | y 285 12 9 10 75 | = 269 12 11 12 76 | temp 285 12 13 17 77 | ; 270 12 17 18 78 | } 277 13 5 6 79 | gcd 285 14 5 8 80 | ( 272 14 8 9 81 | x 285 14 9 10 82 | , 271 14 10 11 83 | y 285 14 11 12 84 | ) 273 14 12 13 85 | ; 270 14 13 14 86 | return 282 15 5 11 87 | 0 286 15 12 13 88 | ; 270 15 13 14 89 | } 277 16 1 2 90 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/3.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | gcd 285 1 5 8 3 | ( 272 1 9 10 4 | int 280 1 10 13 5 | u 285 1 14 15 6 | , 271 1 15 16 7 | int 280 1 17 20 8 | v 285 1 21 22 9 | ) 273 1 22 23 10 | { 276 1 24 25 11 | if 279 2 5 7 12 | ( 272 2 8 9 13 | v 285 2 9 10 14 | == 267 2 11 13 15 | 0 286 2 14 15 16 | ) 273 2 15 16 17 | return 282 2 17 23 18 | u 285 2 24 25 19 | ; 270 2 25 26 20 | else 278 3 5 9 21 | return 282 3 10 16 22 | gcd 285 3 17 20 23 | ( 272 3 20 21 24 | v 285 3 21 22 25 | , 271 3 22 23 26 | u 285 3 24 25 27 | - 260 3 26 27 28 | u 285 3 28 29 29 | / 262 3 30 31 30 | v 285 3 32 33 31 | * 261 3 34 35 32 | v 285 3 36 37 33 | ) 273 3 37 38 34 | ; 270 3 38 39 35 | } 277 4 1 2 36 | int 280 8 1 4 37 | main 285 8 5 9 38 | ( 272 8 9 10 39 | void 283 8 10 14 40 | ) 273 8 14 15 41 | { 276 8 16 17 42 | int 280 9 5 8 43 | x 285 9 9 10 44 | ; 270 9 10 11 45 | int 280 9 12 15 46 | y 285 9 16 17 47 | ; 270 9 17 18 48 | int 280 9 19 22 49 | temp 285 9 23 27 50 | ; 270 9 27 28 51 | x 285 10 5 6 52 | = 269 10 7 8 53 | 72 286 10 9 11 54 | ; 270 10 11 12 55 | y 285 11 5 6 56 | = 269 11 7 8 57 | 18 286 11 9 11 58 | ; 270 11 11 12 59 | if 279 12 5 7 60 | ( 272 12 8 9 61 | x 285 12 9 10 62 | < 263 12 10 11 63 | y 285 12 11 12 64 | ) 273 12 12 13 65 | { 276 12 14 15 66 | temp 285 13 9 13 67 | = 269 13 14 15 68 | x 285 13 16 17 69 | ; 270 13 17 18 70 | x 285 14 9 10 71 | = 269 14 11 12 72 | y 285 14 13 14 73 | ; 270 14 14 15 74 | y 285 15 9 10 75 | = 269 15 11 12 76 | temp 285 15 13 17 77 | ; 270 15 17 18 78 | } 277 16 5 6 79 | gcd 285 17 5 8 80 | ( 272 17 8 9 81 | x 285 17 9 10 82 | , 271 17 10 11 83 | y 285 17 11 12 84 | ) 273 17 12 13 85 | ; 270 17 13 14 86 | return 282 18 5 11 87 | 0 286 18 12 13 88 | ; 270 18 13 14 89 | } 277 19 1 2 90 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/5.tokens: -------------------------------------------------------------------------------- 1 | int 280 2 1 4 2 | gcd 285 2 5 8 3 | ( 272 2 9 10 4 | int 280 2 10 13 5 | u 285 2 14 15 6 | , 271 2 15 16 7 | int 280 2 17 20 8 | v 285 2 21 22 9 | ) 273 2 22 23 10 | { 276 2 24 25 11 | if 279 3 5 7 12 | ( 272 3 8 9 13 | v 285 3 9 10 14 | == 267 3 11 13 15 | 0 286 3 14 15 16 | ) 273 3 15 16 17 | return 282 3 17 23 18 | u 285 3 24 25 19 | ; 270 3 25 26 20 | else 278 4 5 9 21 | return 282 4 10 16 22 | gcd 285 4 17 20 23 | ( 272 4 20 21 24 | v 285 4 21 22 25 | , 271 4 22 23 26 | u 285 4 24 25 27 | - 260 4 26 27 28 | u 285 4 28 29 29 | / 262 4 30 31 30 | v 285 4 32 33 31 | * 261 4 34 35 32 | v 285 4 36 37 33 | ) 273 4 37 38 34 | ; 270 4 38 39 35 | } 277 5 1 2 36 | int 280 6 1 4 37 | main 285 6 5 9 38 | ( 272 6 9 10 39 | void 283 6 10 14 40 | ) 273 6 14 15 41 | { 276 6 16 17 42 | int 280 7 5 8 43 | x 285 7 9 10 44 | ; 270 7 10 11 45 | int 280 7 12 15 46 | y 285 7 16 17 47 | ; 270 7 17 18 48 | int 280 7 19 22 49 | temp 285 7 23 27 50 | ; 270 7 27 28 51 | x 285 8 5 6 52 | = 269 8 7 8 53 | 72 286 8 9 11 54 | ; 270 8 11 12 55 | y 285 9 5 6 56 | = 269 9 7 8 57 | 18 286 9 9 11 58 | ; 270 9 11 12 59 | if 279 10 5 7 60 | ( 272 10 8 9 61 | x 285 10 9 10 62 | < 263 10 10 11 63 | y 285 10 11 12 64 | ) 273 10 12 13 65 | { 276 10 14 15 66 | temp 285 11 9 13 67 | = 269 11 14 15 68 | x 285 11 16 17 69 | ; 270 11 17 18 70 | x 285 12 9 10 71 | = 269 12 11 12 72 | y 285 12 13 14 73 | ; 270 12 14 15 74 | y 285 13 9 10 75 | = 269 13 11 12 76 | temp 285 13 13 17 77 | ; 270 13 17 18 78 | } 277 14 5 6 79 | gcd 285 15 5 8 80 | ( 272 15 8 9 81 | x 285 15 9 10 82 | , 271 15 10 11 83 | y 285 15 11 12 84 | ) 273 15 12 13 85 | ; 270 15 13 14 86 | return 282 16 5 11 87 | 0 286 16 12 13 88 | ; 270 16 13 14 89 | } 277 17 1 2 90 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/6.tokens: -------------------------------------------------------------------------------- 1 | void 283 5 1 5 2 | main 285 5 6 10 3 | ( 272 5 10 11 4 | void 283 5 11 15 5 | ) 273 5 15 16 6 | { 276 5 16 17 7 | int 280 7 5 8 8 | x 285 7 9 10 9 | ; 270 7 10 11 10 | int 280 7 12 15 11 | y 285 7 16 17 12 | ; 270 7 17 18 13 | int 280 7 19 22 14 | RESltado 285 7 23 31 15 | ; 270 7 31 32 16 | x 285 9 5 6 17 | = 269 9 7 8 18 | input 285 9 9 14 19 | ( 272 9 14 15 20 | ) 273 9 15 16 21 | ; 270 9 16 17 22 | y 285 11 18 19 23 | = 269 11 19 20 24 | input 285 11 20 25 25 | ( 272 11 25 26 26 | ) 273 11 26 27 27 | ; 270 11 27 28 28 | resultado 285 13 5 14 29 | = 269 13 15 16 30 | x 285 13 17 18 31 | + 259 13 18 19 32 | y 285 13 19 20 33 | ; 270 13 20 21 34 | output 285 15 5 11 35 | ( 272 15 11 12 36 | resultado 285 15 12 21 37 | ) 273 15 21 22 38 | ; 270 15 22 23 39 | } 277 17 1 2 40 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/7.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | main 285 1 5 9 3 | ( 272 1 9 10 4 | void 283 1 10 14 5 | ) 273 1 14 15 6 | { 276 1 16 17 7 | int 280 2 5 8 8 | temp 285 2 9 13 9 | ; 270 2 13 14 10 | x 285 3 5 6 11 | = 269 3 7 8 12 | 72 286 3 9 11 13 | ; 270 3 11 12 14 | y 285 4 5 6 15 | = 269 4 7 8 16 | 18 286 4 9 11 17 | ; 270 4 11 12 18 | x 285 5 5 6 19 | = 269 5 7 8 20 | x 285 5 9 10 21 | * 261 5 21 22 22 | 4 286 5 30 31 23 | ; 270 5 31 32 24 | return 282 6 5 11 25 | 0 286 6 12 13 26 | ; 270 6 13 14 27 | } 277 7 1 2 28 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/8.tokens: -------------------------------------------------------------------------------- 1 | int 280 1 1 4 2 | main 285 1 5 9 3 | ( 272 1 9 10 4 | void 283 1 10 14 5 | ) 273 1 14 15 6 | { 276 1 16 17 7 | int 280 2 5 8 8 | temp 285 2 9 13 9 | ; 270 2 13 14 10 | x 285 3 5 6 11 | = 269 3 7 8 12 | 72 286 3 9 11 13 | ; 270 3 11 12 14 | y 285 4 5 6 15 | = 269 4 7 8 16 | 18. 287 4 9 12 17 | ; 270 4 12 13 18 | x 285 5 5 6 19 | = 269 5 7 8 20 | x 285 5 9 10 21 | * 261 8 23 24 22 | 4. 287 8 29 31 23 | ; 270 8 31 32 24 | return 282 9 5 11 25 | 0 286 9 12 13 26 | ; 270 9 13 14 27 | } 277 10 1 2 28 | -------------------------------------------------------------------------------- /tests/lab1/TA_token/9.tokens: -------------------------------------------------------------------------------- 1 | float 281 1 1 6 2 | gcd 285 1 7 10 3 | ( 272 1 11 12 4 | float 281 1 12 17 5 | u 285 1 18 19 6 | , 271 1 19 20 7 | float 281 1 21 26 8 | v 285 1 27 28 9 | ) 273 1 28 29 10 | { 276 1 30 31 11 | if 279 2 5 7 12 | ( 272 2 8 9 13 | v 285 2 9 10 14 | < 263 2 11 12 15 | .001 287 2 13 17 16 | ) 273 2 17 18 17 | return 282 2 19 25 18 | u 285 2 26 27 19 | ; 270 2 27 28 20 | else 278 3 5 9 21 | return 282 3 10 16 22 | gcd 285 3 17 20 23 | ( 272 3 20 21 24 | v 285 3 21 22 25 | , 271 3 22 23 26 | u 285 3 24 25 27 | - 260 3 26 27 28 | u 285 3 28 29 29 | / 262 3 30 31 30 | v 285 3 32 33 31 | * 261 3 34 35 32 | v 285 3 36 37 33 | ) 273 3 37 38 34 | ; 270 3 38 39 35 | } 277 4 1 2 36 | int 280 5 1 4 37 | main 285 5 5 9 38 | ( 272 5 9 10 39 | void 283 5 10 14 40 | ) 273 5 14 15 41 | { 276 5 16 17 42 | float 281 6 5 10 43 | x 285 6 11 12 44 | ; 270 6 12 13 45 | float 281 6 14 19 46 | y 285 6 20 21 47 | ; 270 6 21 22 48 | float 281 6 23 28 49 | temp 285 6 29 33 50 | ; 270 6 33 34 51 | x 285 7 5 6 52 | = 269 7 7 8 53 | 72.0 287 7 9 13 54 | ; 270 7 13 14 55 | y 285 8 5 6 56 | = 269 8 7 8 57 | 18. 287 8 9 12 58 | ; 270 8 12 13 59 | if 279 9 5 7 60 | ( 272 9 8 9 61 | x 285 9 9 10 62 | < 263 9 10 11 63 | y 285 9 11 12 64 | ) 273 9 12 13 65 | { 276 9 14 15 66 | temp 285 10 9 13 67 | = 269 10 14 15 68 | x 285 10 16 17 69 | ; 270 10 17 18 70 | x 285 11 9 10 71 | = 269 11 11 12 72 | y 285 11 13 14 73 | ; 270 11 14 15 74 | y 285 12 9 10 75 | = 269 12 11 12 76 | temp 285 12 13 17 77 | ; 270 12 17 18 78 | } 277 13 5 6 79 | gcd 285 14 5 8 80 | ( 272 14 8 9 81 | x 285 14 9 10 82 | , 271 14 10 11 83 | y 285 14 11 12 84 | ) 273 14 12 13 85 | ; 270 14 13 14 86 | return 282 15 5 11 87 | 0 286 15 12 13 88 | ; 270 15 13 14 89 | } 277 16 1 2 90 | -------------------------------------------------------------------------------- /tests/lab1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | /// \brief process all *.cminus file 8 | /// 9 | /// note that: use relative path for all i/o operations 10 | int main(int argc, char **argv){ 11 | if (argc != 3){ 12 | printf("usage: lexer input_file output_file\n"); 13 | return 0; 14 | } 15 | char* input_file = argv[1]; 16 | char* output_file = argv[2]; 17 | 18 | 19 | //token_stream 至多存储 MAX_NUM_TOKEN_NODE 个Token_Node 20 | Token_Node token_stream[MAX_NUM_TOKEN_NODE]; 21 | memset(token_stream, 0, sizeof(Token_Node) * MAX_NUM_TOKEN_NODE); 22 | 23 | analyzer(input_file, token_stream); 24 | 25 | //wrtie file 26 | FILE* fp; 27 | if(!(fp = fopen(output_file,"w+"))){ 28 | printf("[ERR] Cannot create output file\n"); 29 | exit(1); 30 | } 31 | int index = 0; 32 | while(token_stream[index].token != 0){ 33 | fprintf(fp, "%s\t%d\t%d\t%d\t%d\n",token_stream[index].text, token_stream[index].token, 34 | token_stream[index].lines, token_stream[index].pos_start, token_stream[index].pos_end); 35 | index++; 36 | } 37 | 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /tests/lab1/test_lexer.py: -------------------------------------------------------------------------------- 1 | import os 2 | INPUT_FILE_DIR = './tests/lab1/testcase' 3 | OUTPUT_FILE_DIR = './tests/lab1/token' 4 | 5 | all_files = os.listdir(INPUT_FILE_DIR) 6 | target_files = [] 7 | for f in all_files: 8 | if f.endswith(".cminus"): 9 | target_files.append(f) 10 | print("Find %d files" % len(target_files)) 11 | 12 | if not os.path.exists(OUTPUT_FILE_DIR): 13 | os.makedirs(OUTPUT_FILE_DIR) 14 | 15 | for f in target_files: 16 | input_file_path = os.path.join(INPUT_FILE_DIR, f) 17 | output_file = f.split('.cminus')[0] + '.tokens' 18 | output_file_path = os.path.join(OUTPUT_FILE_DIR, output_file) 19 | os.system('./build/lexer %s %s' % (input_file_path, output_file_path)) 20 | 21 | 22 | -------------------------------------------------------------------------------- /tests/lab1/testcase/1.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v, int e[]) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | int main(void) { 6 | int emptyArray[10]; 7 | int x; int y; int temp; 8 | x = 72; 9 | y = 18; 10 | if (x 0) { 11 | j = 1; 12 | } 13 | else if (i == 0) { 14 | j = 2; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /tests/lab2/easy/lex1.cminus: -------------------------------------------------------------------------------- 1 | /* Este programa calcula a soma de dois números fornecidos 2 | 3 | pelo usuário */ 4 | 5 | void main(void){ 6 | 7 | int x; int y; int RESltado; 8 | 9 | x = input(); /* Este programa calcula a soma de dois números fornecidos 10 | 11 | pelo usuário */ y=input(); 12 | 13 | resultado = x+y; 14 | 15 | output(resultado); 16 | 17 | } -------------------------------------------------------------------------------- /tests/lab2/easy/lex2.cminus: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | int temp; 3 | x = 72; 4 | y = 18; 5 | x = x/*comment*/*/******/4; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /tests/lab2/easy/local-decl.cminus: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | int i; float j; 3 | void v; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab2/easy/math.cminus: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i; int j; 4 | i = 3 / (1 + (2 + 3) * (j + 1)); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab2/easy/relop.cminus: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i; 4 | i < 3; 5 | i > 3; 6 | i <= 3; 7 | i == 3; 8 | i >= 3; 9 | i != 3; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_array.cminus: -------------------------------------------------------------------------------- 1 | /* [] and [ ] are different */ 2 | float func(float array[ ]) 3 | {} -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_assign.cminus: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | a=1; 4 | 1=a; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_comment.cminus: -------------------------------------------------------------------------------- 1 | /* unclosed comment -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_function.cminus: -------------------------------------------------------------------------------- 1 | /* unclosed function */ 2 | int main(void) 3 | { -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_op.cminus: -------------------------------------------------------------------------------- 1 | /* unrecognizable operator */ 2 | int f(void){ 3 | 1^; 4 | ~~1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab2/hard/FAIL_syntax1.cminus: -------------------------------------------------------------------------------- 1 | /* various syntax error */ 2 | int main { 3 | if a = 1 { 4 | b = 3 5 | c = 4; 6 | } 7 | return {6}; 8 | -------------------------------------------------------------------------------- /tests/lab2/hard/assoc.cminus: -------------------------------------------------------------------------------- 1 | /* associativity and precedence */ 2 | int main(void) 3 | { 4 | a = b = c = 1 + 1 / 2 * 1 * (1 + 1 + 1 - 1 / 1) + 3 + 4 * 3; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab2/hard/if.cminus: -------------------------------------------------------------------------------- 1 | /* else should be bound to the closest if */ 2 | int main(void) 3 | { 4 | if (1) {} else if (2) {} else {} 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab2/main.c: -------------------------------------------------------------------------------- 1 | #include "syntax_tree.h" 2 | extern syntax_tree *parse(const char*); 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | syntax_tree *tree = NULL; 7 | const char *input = NULL; 8 | 9 | if (argc >= 3) { 10 | printf("usage: %s\n", argv[0]); 11 | printf("usage: %s \n", argv[0]); 12 | return 1; 13 | } 14 | 15 | if (argc == 2) { 16 | input = argv[1]; 17 | } 18 | 19 | // Call the syntax analyzer. 20 | tree = parse(input); 21 | print_syntax_tree(stdout, tree); 22 | del_syntax_tree(tree); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /tests/lab2/normal/You_Should_Pass.cminus: -------------------------------------------------------------------------------- 1 | /* 2 | I konw it's weird, even stupid, to code C like this. w(゚Д゚)w 3 | HOWEVER, we have to use some tricky cases to test your answer. 4 | */ 5 | 6 | float GVAR; 7 | void NeverEverDeclareLikeThis; 8 | int GARRAY[2333]; 9 | 10 | void MyFuncA(int floatNum, float intNum, void voidNums[]){ 11 | int IKnowYouAreVoid; 12 | return MyFuncB(IKnowYouAreVoid); 13 | } 14 | 15 | float MyFuncB(void){ 16 | int IAmVoid[0]; 17 | return MyFuncA(.0, 0, IAmVoid); 18 | } 19 | 20 | int main(void){ 21 | 22 | int a; int b; int c; 23 | 24 | a = b = c = (85 == 84 + 0.4); 25 | 26 | if(a = b){ 27 | GARRAY[ ( MyFuncB() ) ] = GARRAY[c = 1.*.1 == 1.1]; 28 | }else if (MyFuncC(NotDeclared)){ 29 | 30 | }else; 31 | 32 | return 0.; 33 | } -------------------------------------------------------------------------------- /tests/lab2/normal/array1.cminus: -------------------------------------------------------------------------------- 1 | /*Basic array part*/ 2 | 3 | void MyFuncA(int funcNums[]){ 4 | int i; int j; 5 | i = 0; 6 | while(i < 10){ 7 | j = 0; 8 | while(j < 10){ 9 | funcNums[i*10+j] = 1; 10 | j = j+1; 11 | } 12 | i = i+1; 13 | } 14 | } 15 | 16 | int main(void){ 17 | int nums[100]; 18 | 19 | MyFuncA(nums); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /tests/lab2/normal/array2.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | int main(void) { 6 | int emptyArray[10]; 7 | int x; int y; int temp; 8 | x = 72; 9 | y = 18; 10 | if (x= 0)){ 10 | if((a > b) == (a < b)); 11 | else{ 12 | a = b; 13 | } 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /tests/lab2/normal/selectionsort.cminus: -------------------------------------------------------------------------------- 1 | /* this is the sample program in C- in the book "Compiler Construction" */ 2 | /* A program to perform selection sort on a 10 element array. */ 3 | int x[10]; 4 | int minloc ( int a[], int low, int high ) 5 | { int i; int x; int k; 6 | k = low; 7 | x = a[low]; 8 | i = low + 1; 9 | while (i < high) 10 | { if (a[i] < x) 11 | { x = a[i]; 12 | k = i; } 13 | i = i + 1; 14 | } 15 | return k; 16 | } 17 | 18 | void sort( int a[], int low, int high) 19 | { int i; int k; 20 | i = low; 21 | while ( i < high-1) 22 | { int t; 23 | k = minloc(a, i, high); 24 | t = a[k]; 25 | a[k] = a[i]; 26 | a[i] = t; 27 | i = i + 1; 28 | } 29 | } 30 | 31 | void main(void) 32 | { int i; 33 | i = 0; 34 | while ( i < 10) 35 | { x[i] = input(); 36 | i = i + 1; } 37 | sort(x, 0, 10); 38 | i = 0; 39 | while (i < 10) 40 | { output(x[i]); 41 | i = i + 1; } 42 | } 43 | -------------------------------------------------------------------------------- /tests/lab2/normal/tap.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | int main(void) { 6 | int x; int y; int temp; 7 | x = 72; 8 | y = 18; 9 | if (x--+ program 2 | | >--+ declaration-list 3 | | | >--+ declaration 4 | | | | >--+ fun-declaration 5 | | | | | >--+ type-specifier 6 | | | | | | >--* int 7 | | | | | >--* main 8 | | | | | >--* ( 9 | | | | | >--+ params 10 | | | | | | >--* void 11 | | | | | >--* ) 12 | | | | | >--+ compound-stmt 13 | | | | | | >--* { 14 | | | | | | >--+ local-declarations 15 | | | | | | | >--+ local-declarations 16 | | | | | | | | >--* epsilon 17 | | | | | | | >--+ var-declaration 18 | | | | | | | | >--+ type-specifier 19 | | | | | | | | | >--* int 20 | | | | | | | | >--* a 21 | | | | | | | | >--* ; 22 | | | | | | >--+ statement-list 23 | | | | | | | >--+ statement-list 24 | | | | | | | | >--+ statement-list 25 | | | | | | | | | >--* epsilon 26 | | | | | | | | >--+ statement 27 | | | | | | | | | >--+ expression-stmt 28 | | | | | | | | | | >--+ expression 29 | | | | | | | | | | | >--+ var 30 | | | | | | | | | | | | >--* a 31 | | | | | | | | | | | >--* = 32 | | | | | | | | | | | >--+ expression 33 | | | | | | | | | | | | >--+ simple-expression 34 | | | | | | | | | | | | | >--+ additive-expression 35 | | | | | | | | | | | | | | >--+ term 36 | | | | | | | | | | | | | | | >--+ term 37 | | | | | | | | | | | | | | | | >--+ factor 38 | | | | | | | | | | | | | | | | | >--+ integer 39 | | | | | | | | | | | | | | | | | | >--* 1 40 | | | | | | | | | | | | | | | >--+ mulop 41 | | | | | | | | | | | | | | | | >--* / 42 | | | | | | | | | | | | | | | >--+ factor 43 | | | | | | | | | | | | | | | | >--+ integer 44 | | | | | | | | | | | | | | | | | >--* 0 45 | | | | | | | | | | >--* ; 46 | | | | | | | >--+ statement 47 | | | | | | | | >--+ return-stmt 48 | | | | | | | | | >--* return 49 | | | | | | | | | >--+ expression 50 | | | | | | | | | | >--+ simple-expression 51 | | | | | | | | | | | >--+ additive-expression 52 | | | | | | | | | | | | >--+ term 53 | | | | | | | | | | | | | >--+ factor 54 | | | | | | | | | | | | | | >--+ integer 55 | | | | | | | | | | | | | | | >--* 0 56 | | | | | | | | | >--* ; 57 | | | | | | >--* } 58 | -------------------------------------------------------------------------------- /tests/lab2/syntree_easy_std/expr-assign.syntax_tree: -------------------------------------------------------------------------------- 1 | >--+ program 2 | | >--+ declaration-list 3 | | | >--+ declaration 4 | | | | >--+ fun-declaration 5 | | | | | >--+ type-specifier 6 | | | | | | >--* int 7 | | | | | >--* main 8 | | | | | >--* ( 9 | | | | | >--+ params 10 | | | | | | >--* void 11 | | | | | >--* ) 12 | | | | | >--+ compound-stmt 13 | | | | | | >--* { 14 | | | | | | >--+ local-declarations 15 | | | | | | | >--+ local-declarations 16 | | | | | | | | >--+ local-declarations 17 | | | | | | | | | >--* epsilon 18 | | | | | | | | >--+ var-declaration 19 | | | | | | | | | >--+ type-specifier 20 | | | | | | | | | | >--* int 21 | | | | | | | | | >--* a 22 | | | | | | | | | >--* ; 23 | | | | | | | >--+ var-declaration 24 | | | | | | | | >--+ type-specifier 25 | | | | | | | | | >--* int 26 | | | | | | | | >--* b 27 | | | | | | | | >--* ; 28 | | | | | | >--+ statement-list 29 | | | | | | | >--+ statement-list 30 | | | | | | | | >--+ statement-list 31 | | | | | | | | | >--* epsilon 32 | | | | | | | | >--+ statement 33 | | | | | | | | | >--+ expression-stmt 34 | | | | | | | | | | >--+ expression 35 | | | | | | | | | | | >--+ var 36 | | | | | | | | | | | | >--* a 37 | | | | | | | | | | | >--* = 38 | | | | | | | | | | | >--+ expression 39 | | | | | | | | | | | | >--+ var 40 | | | | | | | | | | | | | >--* b 41 | | | | | | | | | | | | >--* = 42 | | | | | | | | | | | | >--+ expression 43 | | | | | | | | | | | | | >--+ simple-expression 44 | | | | | | | | | | | | | | >--+ additive-expression 45 | | | | | | | | | | | | | | | >--+ term 46 | | | | | | | | | | | | | | | | >--+ factor 47 | | | | | | | | | | | | | | | | | >--+ integer 48 | | | | | | | | | | | | | | | | | | >--* 1 49 | | | | | | | | | | >--* ; 50 | | | | | | | >--+ statement 51 | | | | | | | | >--+ return-stmt 52 | | | | | | | | | >--* return 53 | | | | | | | | | >--+ expression 54 | | | | | | | | | | >--+ simple-expression 55 | | | | | | | | | | | >--+ additive-expression 56 | | | | | | | | | | | | >--+ term 57 | | | | | | | | | | | | | >--+ factor 58 | | | | | | | | | | | | | | >--+ integer 59 | | | | | | | | | | | | | | | >--* 0 60 | | | | | | | | | >--* ; 61 | | | | | | >--* } 62 | -------------------------------------------------------------------------------- /tests/lab2/syntree_easy_std/local-decl.syntax_tree: -------------------------------------------------------------------------------- 1 | >--+ program 2 | | >--+ declaration-list 3 | | | >--+ declaration 4 | | | | >--+ fun-declaration 5 | | | | | >--+ type-specifier 6 | | | | | | >--* int 7 | | | | | >--* main 8 | | | | | >--* ( 9 | | | | | >--+ params 10 | | | | | | >--* void 11 | | | | | >--* ) 12 | | | | | >--+ compound-stmt 13 | | | | | | >--* { 14 | | | | | | >--+ local-declarations 15 | | | | | | | >--+ local-declarations 16 | | | | | | | | >--+ local-declarations 17 | | | | | | | | | >--+ local-declarations 18 | | | | | | | | | | >--* epsilon 19 | | | | | | | | | >--+ var-declaration 20 | | | | | | | | | | >--+ type-specifier 21 | | | | | | | | | | | >--* int 22 | | | | | | | | | | >--* i 23 | | | | | | | | | | >--* ; 24 | | | | | | | | >--+ var-declaration 25 | | | | | | | | | >--+ type-specifier 26 | | | | | | | | | | >--* float 27 | | | | | | | | | >--* j 28 | | | | | | | | | >--* ; 29 | | | | | | | >--+ var-declaration 30 | | | | | | | | >--+ type-specifier 31 | | | | | | | | | >--* void 32 | | | | | | | | >--* v 33 | | | | | | | | >--* ; 34 | | | | | | >--+ statement-list 35 | | | | | | | >--+ statement-list 36 | | | | | | | | >--* epsilon 37 | | | | | | | >--+ statement 38 | | | | | | | | >--+ return-stmt 39 | | | | | | | | | >--* return 40 | | | | | | | | | >--+ expression 41 | | | | | | | | | | >--+ simple-expression 42 | | | | | | | | | | | >--+ additive-expression 43 | | | | | | | | | | | | >--+ term 44 | | | | | | | | | | | | | >--+ factor 45 | | | | | | | | | | | | | | >--+ integer 46 | | | | | | | | | | | | | | | >--* 0 47 | | | | | | | | | >--* ; 48 | | | | | | >--* } 49 | -------------------------------------------------------------------------------- /tests/lab2/test_syntax.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # DO NOT MODIFY! 4 | # If you need customized behavior, please create your own script. 5 | 6 | CUR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 7 | TESTCASE="${1:-testcase}" 8 | TESTCASE_DIR="$CUR_DIR/$TESTCASE" 9 | OUTPUT_DIR="$CUR_DIR/syntree_$TESTCASE" 10 | BUILD_DIR="$CUR_DIR/../../build" 11 | 12 | # Make sure $OUTPUT_DIR exists. 13 | mkdir -p "$OUTPUT_DIR" 14 | 15 | # Generate a .syntax_tree file for all .cminus files in the testcase dir. 16 | SAVEIFS=$IFS 17 | IFS=$(echo -en "\n\b") 18 | for testcase in "$TESTCASE_DIR"/*.cminus 19 | do 20 | filename="$(basename $testcase)" 21 | echo "[info] Analyzing $filename" 22 | "$BUILD_DIR"/parser "$testcase" > "$OUTPUT_DIR/${filename%.cminus}.syntax_tree" 23 | done 24 | 25 | # Output a summary when requested. 26 | if [[ ${2:-no} != "no" ]]; then 27 | echo "[info] Comparing..." 28 | if [[ ${2:-no} == "verbose" ]]; then 29 | diff "$OUTPUT_DIR" "${OUTPUT_DIR}_std" 30 | else 31 | diff -qr "$OUTPUT_DIR" "${OUTPUT_DIR}_std" 32 | fi 33 | if [ $? -eq 0 ]; then 34 | echo "[info] No difference! Congratulations!" 35 | fi 36 | fi 37 | 38 | IFS=$SAVEIFS 39 | -------------------------------------------------------------------------------- /tests/lab3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable( 2 | gcd_array_generator 3 | ta_gcd/gcd_array_generator.cpp 4 | ) 5 | target_link_libraries( 6 | gcd_array_generator 7 | IR_lib 8 | ) 9 | 10 | add_executable( 11 | stu_assign_generator 12 | stu_cpp/assign_generator.cpp 13 | ) 14 | target_link_libraries( 15 | stu_assign_generator 16 | IR_lib 17 | ) 18 | add_executable( 19 | stu_fun_generator 20 | stu_cpp/fun_generator.cpp 21 | ) 22 | target_link_libraries( 23 | stu_fun_generator 24 | IR_lib 25 | ) 26 | add_executable( 27 | stu_if_generator 28 | stu_cpp/if_generator.cpp 29 | ) 30 | target_link_libraries( 31 | stu_if_generator 32 | IR_lib 33 | ) 34 | add_executable( 35 | stu_while_generator 36 | stu_cpp/while_generator.cpp 37 | ) 38 | target_link_libraries( 39 | stu_while_generator 40 | IR_lib 41 | ) 42 | -------------------------------------------------------------------------------- /tests/lab3/c_cases/assign.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a[10]; 3 | a[0] = 10; 4 | a[1] = a[0] * 2; 5 | return a[1]; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab3/c_cases/fun.c: -------------------------------------------------------------------------------- 1 | int callee(int a){ 2 | return 2 * a; 3 | } 4 | int main(){ 5 | return callee(110); 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab3/c_cases/if.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | float a = 5.555; 3 | if(a > 1) 4 | return 233; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab3/c_cases/while.c: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int a; 3 | int i; 4 | a = 10; 5 | i = 0; 6 | while(i < 10){ 7 | i = i + 1; 8 | a = a + i; 9 | } 10 | return a; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab3/stu_cpp/assign_generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "BasicBlock.h" 4 | #include "Constant.h" 5 | #include "Function.h" 6 | #include "IRBuilder.h" 7 | #include "Module.h" 8 | #include "Type.h" 9 | 10 | int main() { 11 | std::ios::sync_with_stdio(false); 12 | 13 | auto module = new Module("assign"); 14 | auto builder = new IRBuilder(nullptr, module); 15 | Type *Int32Type = Type::get_int32_type(module); 16 | 17 | // Constant helpers. 18 | auto const_int = [&module](auto num) { 19 | return ConstantInt::get(num, module); 20 | }; 21 | 22 | // int main() 23 | auto main_func = Function::create(FunctionType::get(Int32Type, {}), "main", module); 24 | auto main_bb = BasicBlock::create(module, "main_body", main_func); 25 | builder->set_insert_point(main_bb); 26 | 27 | // int a[10]; 28 | auto *Int10ArrayType = ArrayType::get(Int32Type, 10); 29 | auto a = builder->create_alloca(Int10ArrayType); 30 | 31 | // a[0] = 10; 32 | auto a0_gep_1 = builder->create_gep(a, {const_int(0), const_int(0)}); 33 | builder->create_store(const_int(10), a0_gep_1); 34 | 35 | // a[1] = a[0] * 2; 36 | auto a0_gep_2 = builder->create_gep(a, {const_int(0), const_int(0)}); 37 | auto a0 = builder->create_load(a0_gep_2); 38 | auto a1_1 = builder->create_imul(a0, const_int(2)); 39 | auto a1_gep_1 = builder->create_gep(a, {const_int(0), const_int(1)}); 40 | builder->create_store(a1_1, a1_gep_1); 41 | 42 | // return a[1]; 43 | auto a1_gep_2 = builder->create_gep(a, {const_int(0), const_int(1)}); 44 | auto a1_2 = builder->create_load(a1_gep_2); 45 | builder->create_ret(a1_2); 46 | 47 | std::cout << module->print(); 48 | delete module; 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /tests/lab3/stu_cpp/fun_generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "BasicBlock.h" 5 | #include "Constant.h" 6 | #include "Function.h" 7 | #include "IRBuilder.h" 8 | #include "Module.h" 9 | #include "Type.h" 10 | 11 | int main() { 12 | std::ios::sync_with_stdio(false); 13 | 14 | auto module = new Module("fun"); 15 | auto builder = new IRBuilder(nullptr, module); 16 | Type *Int32Type = Type::get_int32_type(module); 17 | 18 | // Constant helpers. 19 | auto const_int = [&module](auto num) { 20 | return ConstantInt::get(num, module); 21 | }; 22 | 23 | // int callee(int a) 24 | auto callee_func = Function::create(FunctionType::get(Int32Type, {Int32Type}), "callee", module); 25 | std::vector callee_args; 26 | for (auto arg = callee_func->arg_begin(); arg != callee_func->arg_end(); arg++) { 27 | callee_args.push_back(*arg); 28 | } 29 | auto callee_bb = BasicBlock::create(module, "callee_body", callee_func); 30 | builder->set_insert_point(callee_bb); 31 | 32 | // return 2 * a; 33 | auto callee_ret = builder->create_imul(const_int(2), callee_args[0]); 34 | builder->create_ret(callee_ret); 35 | 36 | // int main() 37 | auto main_func = Function::create(FunctionType::get(Int32Type, {}), "main", module); 38 | auto main_bb = BasicBlock::create(module, "main_body", main_func); 39 | builder->set_insert_point(main_bb); 40 | 41 | // return callee(110); 42 | auto ret = builder->create_call(callee_func, {const_int(110)}); 43 | builder->create_ret(ret); 44 | 45 | std::cout << module->print(); 46 | delete module; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /tests/lab3/stu_cpp/if_generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "BasicBlock.h" 4 | #include "Constant.h" 5 | #include "Function.h" 6 | #include "IRBuilder.h" 7 | #include "Module.h" 8 | #include "Type.h" 9 | 10 | int main() { 11 | std::ios::sync_with_stdio(false); 12 | 13 | auto module = new Module("if"); 14 | auto builder = new IRBuilder(nullptr, module); 15 | Type *Int32Type = Type::get_int32_type(module); 16 | Type *FloatType = Type::get_float_type(module); 17 | 18 | // Constant helpers. 19 | auto const_int = [&module](auto num) { 20 | return ConstantInt::get(num, module); 21 | }; 22 | auto const_fp = [&module](auto num) { 23 | return ConstantFP::get(num, module); 24 | }; 25 | 26 | // int main() 27 | auto main_func = Function::create(FunctionType::get(Int32Type, {}), "main", module); 28 | auto main_bb = BasicBlock::create(module, "main_body", main_func); 29 | builder->set_insert_point(main_bb); 30 | 31 | // float a = 5.555; 32 | auto a_alloca = builder->create_alloca(FloatType); 33 | builder->create_store(const_fp(5.555), a_alloca); 34 | 35 | // if (a > 1) 36 | auto a = builder->create_load(a_alloca); 37 | auto cond = builder->create_fcmp_gt(a, const_fp(1.0)); 38 | auto true_bb = BasicBlock::create(module, "main_true", main_func); 39 | auto false_bb = BasicBlock::create(module, "main_false", main_func); 40 | builder->create_cond_br(cond, true_bb, false_bb); 41 | 42 | // return 233; 43 | builder->set_insert_point(true_bb); 44 | builder->create_ret(const_int(233)); 45 | 46 | // return 0; 47 | builder->set_insert_point(false_bb); 48 | builder->create_ret(const_int(0)); 49 | 50 | std::cout << module->print(); 51 | delete module; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /tests/lab3/stu_cpp/while_generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "BasicBlock.h" 4 | #include "Constant.h" 5 | #include "Function.h" 6 | #include "IRBuilder.h" 7 | #include "Module.h" 8 | #include "Type.h" 9 | 10 | int main() { 11 | std::ios::sync_with_stdio(false); 12 | 13 | auto module = new Module("while"); 14 | auto builder = new IRBuilder(nullptr, module); 15 | Type *Int32Type = Type::get_int32_type(module); 16 | 17 | // Constant helpers. 18 | auto const_int = [&module](auto num) { 19 | return ConstantInt::get(num, module); 20 | }; 21 | 22 | // int main() 23 | auto main_func = Function::create(FunctionType::get(Int32Type, {}), "main", module); 24 | auto main_bb = BasicBlock::create(module, "main_body", main_func); 25 | builder->set_insert_point(main_bb); 26 | 27 | // int a; 28 | auto a_alloca = builder->create_alloca(Int32Type); 29 | 30 | // int i; 31 | auto i_alloca = builder->create_alloca(Int32Type); 32 | 33 | // a = 10; 34 | builder->create_store(const_int(10), a_alloca); 35 | 36 | // i = 0; 37 | builder->create_store(const_int(0), i_alloca); 38 | 39 | // while (i < 10) 40 | auto cond_bb = BasicBlock::create(module, "main_cond", main_func); 41 | builder->create_br(cond_bb); 42 | builder->set_insert_point(cond_bb); 43 | auto i_1 = builder->create_load(i_alloca); 44 | auto cond = builder->create_icmp_lt(i_1, const_int(10)); 45 | auto loop_bb = BasicBlock::create(module, "main_loop", main_func); 46 | auto exit_bb = BasicBlock::create(module, "main_exit", main_func); 47 | builder->create_cond_br(cond, loop_bb, exit_bb); 48 | 49 | // i = i + 1; 50 | builder->set_insert_point(loop_bb); 51 | auto i_2 = builder->create_load(i_alloca); 52 | auto i_new = builder->create_iadd(i_2, const_int(1)); 53 | builder->create_store(i_new, i_alloca); 54 | 55 | // a = a + i; 56 | auto a_1 = builder->create_load(a_alloca); 57 | auto i_3 = builder->create_load(i_alloca); 58 | auto a_new = builder->create_iadd(a_1, i_3); 59 | builder->create_store(a_new, a_alloca); 60 | builder->create_br(cond_bb); 61 | 62 | // return a; 63 | builder->set_insert_point(exit_bb); 64 | auto a_2 = builder->create_load(a_alloca); 65 | builder->create_ret(a_2); 66 | 67 | std::cout << module->print(); 68 | delete module; 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /tests/lab3/stu_ll/assign_hand.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'assign.c' 2 | source_filename = "assign.c" 3 | target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-pc-linux-gnu" 5 | 6 | ; Get pointer, then load/store value. 7 | 8 | ; int main() 9 | ; Function Attrs: noinline nounwind optnone sspstrong uwtable 10 | define dso_local i32 @main() #0 { 11 | ; int a[10]; 12 | %1 = alloca [10 x i32] 13 | ; a[0] = 10; 14 | %2 = getelementptr [10 x i32], [10 x i32]* %1, i32 0, i32 0 15 | store i32 10, i32* %2 16 | ; a[1] = a[0] * 2; 17 | %3 = getelementptr [10 x i32], [10 x i32]* %1, i32 0, i32 0 18 | %4 = load i32, i32* %3 19 | %5 = mul i32 %4, 2 20 | %6 = getelementptr [10 x i32], [10 x i32]* %1, i32 0, i32 1 21 | store i32 %5, i32* %6 22 | ; return a[1]; 23 | %7 = getelementptr [10 x i32], [10 x i32]* %1, i32 0, i32 1 24 | %8 = load i32, i32* %7 25 | ret i32 %8 26 | } 27 | 28 | attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 29 | 30 | !llvm.module.flags = !{!0, !1, !2} 31 | !llvm.ident = !{!3} 32 | 33 | !0 = !{i32 1, !"wchar_size", i32 4} 34 | !1 = !{i32 7, !"PIC Level", i32 2} 35 | !2 = !{i32 7, !"PIE Level", i32 2} 36 | !3 = !{!"clang version 11.0.0"} 37 | -------------------------------------------------------------------------------- /tests/lab3/stu_ll/fun_hand.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'fun.c' 2 | source_filename = "fun.c" 3 | target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-pc-linux-gnu" 5 | 6 | ; Function args and locals should gap one as stack pointer. 7 | 8 | ; int callee(int a) 9 | ; Function Attrs: noinline nounwind optnone sspstrong uwtable 10 | define dso_local i32 @callee(i32 %0) #0 { 11 | ; return 2 * a; 12 | %2 = mul i32 2, %0 13 | ret i32 %2 14 | } 15 | 16 | ; int main() 17 | ; Function Attrs: noinline nounwind optnone sspstrong uwtable 18 | define dso_local i32 @main() #0 { 19 | ; return callee(110); 20 | %1 = call i32 @callee(i32 110) 21 | ret i32 %1 22 | } 23 | 24 | attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 25 | 26 | !llvm.module.flags = !{!0, !1, !2} 27 | !llvm.ident = !{!3} 28 | 29 | !0 = !{i32 1, !"wchar_size", i32 4} 30 | !1 = !{i32 7, !"PIC Level", i32 2} 31 | !2 = !{i32 7, !"PIE Level", i32 2} 32 | !3 = !{!"clang version 11.0.0"} 33 | -------------------------------------------------------------------------------- /tests/lab3/stu_ll/if_hand.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'if.c' 2 | source_filename = "if.c" 3 | target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-pc-linux-gnu" 5 | 6 | ; Use label like asm to impl flow control. 7 | ; Float literal should be binary. 8 | 9 | ; int main() 10 | ; Function Attrs: noinline nounwind optnone sspstrong uwtable 11 | define dso_local i32 @main() #0 { 12 | ; float a = 5.555; 13 | %1 = alloca float 14 | store float 0x40163851e0000000, float* %1 15 | ; if (a > 1) 16 | %2 = load float, float* %1 17 | %3 = fcmp ugt float %2, 1.0 18 | br i1 %3, label %4, label %5 19 | 4: 20 | ; return 233; 21 | ret i32 233 22 | 5: 23 | ; return 0; 24 | ret i32 0 25 | } 26 | 27 | attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 28 | 29 | !llvm.module.flags = !{!0, !1, !2} 30 | !llvm.ident = !{!3} 31 | 32 | !0 = !{i32 1, !"wchar_size", i32 4} 33 | !1 = !{i32 7, !"PIC Level", i32 2} 34 | !2 = !{i32 7, !"PIE Level", i32 2} 35 | !3 = !{!"clang version 11.0.0"} 36 | -------------------------------------------------------------------------------- /tests/lab3/stu_ll/while_hand.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'while.c' 2 | source_filename = "while.c" 3 | target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-pc-linux-gnu" 5 | 6 | ; while should not be a problem. Impl it like asm. 7 | ; Be careful about numbers. As for the order, take the below as an example. 8 | 9 | ; int main() 10 | ; Function Attrs: noinline nounwind optnone sspstrong uwtable 11 | define dso_local i32 @main() #0 { 12 | ; int a; 13 | %1 = alloca i32 14 | ; int i; 15 | %2 = alloca i32 16 | ; a = 10; 17 | store i32 10, i32* %1 18 | ; i = 0; 19 | store i32 0, i32* %2 20 | ; while (i < 10) 21 | br label %3 22 | 3: 23 | %4 = load i32, i32* %2 24 | %5 = icmp slt i32 %4, 10 25 | br i1 %5, label %6, label %12 26 | 6: 27 | ; i = i + 1; 28 | %7 = load i32, i32* %2 29 | %8 = add i32 %7, 1 30 | store i32 %8, i32* %2 31 | ; a = a + i; 32 | %9 = load i32, i32* %1 33 | %10 = load i32, i32* %2 34 | %11 = add i32 %9, %10 35 | store i32 %11, i32* %1 36 | br label %3 37 | 12: 38 | ; return a; 39 | %13 = load i32, i32* %1 40 | ret i32 %13 41 | } 42 | 43 | attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 44 | 45 | !llvm.module.flags = !{!0, !1, !2} 46 | !llvm.ident = !{!3} 47 | 48 | !0 = !{i32 1, !"wchar_size", i32 4} 49 | !1 = !{i32 7, !"PIC Level", i32 2} 50 | !2 = !{i32 7, !"PIE Level", i32 2} 51 | !3 = !{!"clang version 11.0.0"} 52 | -------------------------------------------------------------------------------- /tests/lab3/ta_gcd/gcd_array.c: -------------------------------------------------------------------------------- 1 | int x[1]; 2 | int y[1]; 3 | 4 | int gcd (int u, int v) { 5 | if (v == 0) return u; 6 | else return gcd(v, u - u / v * v); 7 | } 8 | 9 | int funArray (int u[], int v[]) { 10 | int a; 11 | int b; 12 | int temp; 13 | a = u[0]; 14 | b = v[0]; 15 | if (a < b) { 16 | temp = a; 17 | a = b; 18 | b = temp; 19 | } 20 | return gcd(a, b); 21 | } 22 | 23 | int main(void) { 24 | x[0] = 90; 25 | y[0] = 18; 26 | return funArray(x, y); 27 | } -------------------------------------------------------------------------------- /tests/lab4/lab4_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import subprocess 3 | # { name: need_input } 4 | testcases = { 5 | "01": True, 6 | "02": False, 7 | "03": False, 8 | "04": False, 9 | "05": False, 10 | "06": False, 11 | "07": False, 12 | "08": False, 13 | "09": False, 14 | "10": False, 15 | "11": False, 16 | "12": True, 17 | "13": False, 18 | "14": False, 19 | "15": False, 20 | "16": False, 21 | "17": False, 22 | "18": False, 23 | "19": False, 24 | "20": False, 25 | "21": False, 26 | "22": False, 27 | } 28 | 29 | def eval(): 30 | EXE_PATH = "../../build/cminusfc" 31 | TEST_BASE_PATH = "./testcases/" 32 | print('===========TEST START===========') 33 | for case in testcases: 34 | print('Case %s:' % case, end='') 35 | TEST_PATH = TEST_BASE_PATH + case 36 | INPUT_PATH = TEST_BASE_PATH + case + '.in' 37 | OUTPUT_PATH = TEST_BASE_PATH + case + '.out' 38 | need_input = testcases[case] 39 | 40 | COMMAND = [TEST_PATH] 41 | 42 | result = subprocess.run([EXE_PATH, TEST_PATH + ".cminus"], stderr=subprocess.PIPE) 43 | if result.returncode == 0: 44 | input_option = None 45 | if need_input: 46 | with open(INPUT_PATH, "rb") as fin: 47 | input_option = fin.read() 48 | 49 | try: 50 | result = subprocess.run(COMMAND, input=input_option, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=1) 51 | with open(OUTPUT_PATH, "rb") as fout: 52 | if result.stdout == fout.read(): 53 | print('\t\033[32mSuccess\033[0m') 54 | else: 55 | print('\t\033[31mFail\033[0m') 56 | except Exception as _: 57 | print('\t\033[31mFail\033[0m') 58 | finally: 59 | subprocess.call(["rm", "-rf", TEST_PATH, TEST_PATH + ".o"]) 60 | 61 | else: 62 | print('\t\033[31mFail\033[0m') 63 | 64 | print('============TEST END============') 65 | 66 | 67 | if __name__ == "__main__": 68 | eval() 69 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/decl_float.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/decl_float.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/decl_float_array.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/decl_float_array.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/decl_int.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/decl_int.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/decl_int_array.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/decl_int_array.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/input.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/input.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/input.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/output_float.out: -------------------------------------------------------------------------------- 1 | 123.400002 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/output_int.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_1/return.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/ta_answers/lv0_1/return.out -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_add_float.out: -------------------------------------------------------------------------------- 1 | 123 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_add_int.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_add_mixed.out: -------------------------------------------------------------------------------- 1 | 1023 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_comp1.out: -------------------------------------------------------------------------------- 1 | -44.720001 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_comp2.out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | 0 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_div_float.out: -------------------------------------------------------------------------------- 1 | 12 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_div_int.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_div_mixed.out: -------------------------------------------------------------------------------- 1 | 12 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_eq_float.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_eq_int.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_eq_mixed.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_ge_float.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_ge_int.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_ge_mixed.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_gt_float.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_gt_int.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_gt_mixed.out: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_le_float.out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_le_int.out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_le_mixed.out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_lt_float.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_lt_int.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_lt_mixed.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_mul_float.out: -------------------------------------------------------------------------------- 1 | 123 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_mul_int.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_mul_mixed.out: -------------------------------------------------------------------------------- 1 | 123 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_neq_float.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_neq_int.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_neq_mixed.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 1 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_sub_float.out: -------------------------------------------------------------------------------- 1 | 192 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_sub_int.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv0_2/num_sub_mixed.out: -------------------------------------------------------------------------------- 1 | 1923 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_cmp.out: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | 0 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_float_array_global.out: -------------------------------------------------------------------------------- 1 | 1234.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_float_array_local.out: -------------------------------------------------------------------------------- 1 | 1234.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_float_var_global.out: -------------------------------------------------------------------------------- 1 | 1234.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_float_var_local.out: -------------------------------------------------------------------------------- 1 | 1234.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_int_array_global.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_int_array_local.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_int_var_global.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/assign_int_var_local.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/idx_float.out: -------------------------------------------------------------------------------- 1 | 1024 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/innout.in: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/innout.out: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/iteration1.out: -------------------------------------------------------------------------------- 1 | 10 2 | 9 3 | 8 4 | 7 5 | 6 6 | 5 7 | 4 8 | 3 9 | 2 10 | 1 11 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/iteration2.out: -------------------------------------------------------------------------------- 1 | 10 2 | 9 3 | 8 4 | 7 5 | 6 6 | 5 7 | 4 8 | 3 9 | 2 10 | 1 11 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/negidx_float.out: -------------------------------------------------------------------------------- 1 | negative index exception 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/negidx_floatfuncall.out: -------------------------------------------------------------------------------- 1 | negative index exception 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/negidx_int.out: -------------------------------------------------------------------------------- 1 | negative index exception 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/negidx_intfuncall.out: -------------------------------------------------------------------------------- 1 | negative index exception 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/negidx_voidfuncall.out: -------------------------------------------------------------------------------- 1 | negative index exception 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/scope.out: -------------------------------------------------------------------------------- 1 | 3 2 | 11 3 | 3 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/selection1.out: -------------------------------------------------------------------------------- 1 | 42 2 | 24 3 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/selection2.out: -------------------------------------------------------------------------------- 1 | 42 2 | 24 3 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/selection3.out: -------------------------------------------------------------------------------- 1 | 42 2 | 24 3 | 1234 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/transfer_float_to_int.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv1/transfer_int_to_float.out: -------------------------------------------------------------------------------- 1 | 1.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/assign_chain.out: -------------------------------------------------------------------------------- 1 | 3 2 | 3 3 | 3 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_array.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_array_array.out: -------------------------------------------------------------------------------- 1 | 1024 2 | 1024 3 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_chain.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_float_array.out: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_int_array.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_type_mismatch1.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_type_mismatch2.out: -------------------------------------------------------------------------------- 1 | 4.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/funcall_var.out: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/return_in_middle1.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/return_in_middle2.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/return_type_mismatch1.out: -------------------------------------------------------------------------------- 1 | 233 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv2/return_type_mismatch2.out: -------------------------------------------------------------------------------- 1 | 7.000000 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv3/complex2.in: -------------------------------------------------------------------------------- 1 | 9 2 | 8 3 | 7 4 | 6 5 | 5 6 | 4 7 | 3 8 | 2 9 | 1 10 | 0 11 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv3/complex2.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv3/complex3.in: -------------------------------------------------------------------------------- 1 | 78 2 | 117 3 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv3/complex3.out: -------------------------------------------------------------------------------- 1 | 39 2 | -------------------------------------------------------------------------------- /tests/lab4/ta_answers/lv3/complex4.out: -------------------------------------------------------------------------------- 1 | 1.000000 2 | -0.200000 3 | 0.400000 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/decl_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float a; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/decl_float_array.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float a[10]; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/decl_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/decl_int_array.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/input.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | input(); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/output_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | outputFloat(123.4); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/output_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1234); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_1/return.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | return; 3 | } 4 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_add_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(100.0 + 23.4); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_add_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1000 + 234); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_add_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1000 + 23.4); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_comp1.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | outputFloat(9 + 1.2 / (2 * 2 + 5 - 6) * 3.2 - 55); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_comp2.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output((1 > 2.) < 3); 3 | output((1 > 2) < ((3 == 4) >= 0)); 4 | output(((3 == 4.) >= 0) <= (4 != 4)); 5 | output(((1 > 2) < ((3 == 4) >= 0)) <= (4. != 4)); 6 | return; 7 | } 8 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_div_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(24.68 / 2.); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_div_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2468 / 2); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_div_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(24.68 / 2); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_eq_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. == 2.); 3 | output(2. == 2.); 4 | output(3. == 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_eq_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 == 2); 3 | output(2 == 2); 4 | output(3 == 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_eq_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. == 2); 3 | output(2. == 2); 4 | output(3 == 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_ge_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. >= 2.); 3 | output(2. >= 2.); 4 | output(3. >= 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_ge_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 >= 2); 3 | output(2 >= 2); 4 | output(3 >= 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_ge_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. >= 2); 3 | output(2. >= 2); 4 | output(3 >= 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_gt_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. > 2.); 3 | output(2. > 2.); 4 | output(3. > 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_gt_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 > 2); 3 | output(2 > 2); 4 | output(3 > 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_gt_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. > 2); 3 | output(2. > 2); 4 | output(3 > 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_le_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. <= 2.); 3 | output(2. <= 2.); 4 | output(3. <= 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_le_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 <= 2); 3 | output(2 <= 2); 4 | output(3 <= 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_le_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. <= 2); 3 | output(2. <= 2); 4 | output(3 <= 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_lt_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. < 2.); 3 | output(2. < 2.); 4 | output(3. < 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_lt_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 < 2); 3 | output(2 < 2); 4 | output(3 < 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_lt_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. < 2); 3 | output(2 < 2.); 4 | output(3. < 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_mul_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2. * 61.7); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_mul_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2 * 617); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_mul_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2 * 61.7); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_neq_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. != 2.); 3 | output(2. != 2.); 4 | output(3. != 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_neq_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1 != 2); 3 | output(2 != 2); 4 | output(3 != 2); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_neq_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1. != 2); 3 | output(2 != 2.); 4 | output(3 != 2.); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_sub_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(200.0 - 7.66); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_sub_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2000 - 766); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv0_2/num_sub_mixed.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(2000 - 76.6); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_cmp.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | int b; 4 | int c; 5 | a = 1 < 3; 6 | b = 2 == 4; 7 | c = 3 > 5; 8 | output(a); 9 | output(b); 10 | output(c); 11 | return; 12 | } 13 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_float_array_global.cminus: -------------------------------------------------------------------------------- 1 | float b[10]; 2 | void main(void) { 3 | b[3] = 1234.0; 4 | outputFloat(b[3]); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_float_array_local.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float b[10]; 3 | b[3] = 1234.0; 4 | outputFloat(b[3]); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_float_var_global.cminus: -------------------------------------------------------------------------------- 1 | float b; 2 | void main(void) { 3 | b = 1234.0; 4 | outputFloat(b); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_float_var_local.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float b; 3 | b = 1234.0; 4 | outputFloat(b); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_int_array_global.cminus: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | void main(void) { 3 | a[3] = 1234; 4 | output(a[3]); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_int_array_local.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | a[3] = 1234; 4 | output(a[3]); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_int_var_global.cminus: -------------------------------------------------------------------------------- 1 | int a; 2 | void main(void) { 3 | a = 1234; 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/assign_int_var_local.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 1234; 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/idx_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | a[0] = 1024; 4 | output(a[0.1]); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/innout.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = input(); 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/iteration1.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int i; 3 | i = 10; 4 | while (i) { 5 | output(i); 6 | i = i - 1; 7 | } 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/iteration2.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int i; 3 | i = 10; 4 | while (i > 0) { 5 | output(i); 6 | i = i - 1; 7 | } 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/negidx_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | a[2.-3]; 4 | return; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/negidx_floatfuncall.cminus: -------------------------------------------------------------------------------- 1 | float test(void) { 2 | int a[10]; 3 | a[2-3]; 4 | return 2.; 5 | } 6 | void main(void) { 7 | test(); 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/negidx_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | a[2-3]; 4 | return; 5 | } 6 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/negidx_intfuncall.cminus: -------------------------------------------------------------------------------- 1 | int test(void) { 2 | int a[10]; 3 | a[2-3]; 4 | return 2; 5 | } 6 | void main(void) { 7 | test(); 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/negidx_voidfuncall.cminus: -------------------------------------------------------------------------------- 1 | void test(void) { 2 | int a[10]; 3 | a[2-3]; 4 | return; 5 | } 6 | void main(void) { 7 | test(); 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/scope.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 3; 4 | output(a); 5 | { 6 | int a; 7 | a = 11; 8 | output(a); 9 | } 10 | output(a); 11 | return; 12 | } 13 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/selection1.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 2; 4 | if (a) 5 | output(42); 6 | output(24); 7 | return; 8 | } 9 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/selection2.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | if (2 > 1) 3 | output(42); 4 | output(24); 5 | if (1 > 2) { 6 | output (1234); 7 | } 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/selection3.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | if (2 > 1) { 3 | output(42); 4 | } else 5 | output(1234); 6 | 7 | output(24); 8 | 9 | if (2 < 1) 10 | output(42); 11 | else { 12 | output(1234); 13 | } 14 | return; 15 | } 16 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/transfer_float_to_int.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int a; 3 | a = 1.0; 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv1/transfer_int_to_float.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | float a; 3 | a = 1; 4 | outputFloat(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/assign_chain.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | int b; 4 | int c; 5 | a = b = c = 3; 6 | output(a); 7 | output(b); 8 | output(c); 9 | return; 10 | } 11 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_array_array.cminus: -------------------------------------------------------------------------------- 1 | void g(int b[]) { 2 | output(b[3]); 3 | return; 4 | } 5 | 6 | void f(int c[]) { 7 | output(c[3]); 8 | g(c); 9 | return; 10 | } 11 | 12 | void main(void) { 13 | int a[10]; 14 | a[3] = 1024; 15 | f(a); 16 | return; 17 | } 18 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_chain.cminus: -------------------------------------------------------------------------------- 1 | int addone(int a) { 2 | return a + 1; 3 | } 4 | void main(void) { 5 | int result; 6 | result = addone(addone(addone(addone(1230)))); 7 | output(result); 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_float_array.cminus: -------------------------------------------------------------------------------- 1 | void test(float a[]) { 2 | output(a[3]); 3 | return; 4 | } 5 | 6 | void main(void) { 7 | float a[10]; 8 | a[3] = 3.14; 9 | test(a); 10 | return; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_int_array.cminus: -------------------------------------------------------------------------------- 1 | void test(int a[]) { 2 | output(a[3]); 3 | return; 4 | } 5 | 6 | void main(void) { 7 | int a[10]; 8 | a[3] = 10; 9 | test(a); 10 | return; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_type_mismatch1.cminus: -------------------------------------------------------------------------------- 1 | void f(int a) { 2 | output(a); 3 | return; 4 | } 5 | 6 | void main(void) { 7 | float a; 8 | a = 10; 9 | f(a); 10 | return; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_type_mismatch2.cminus: -------------------------------------------------------------------------------- 1 | void f(float a) { 2 | outputFloat(a); 3 | return; 4 | } 5 | 6 | void main(void) { 7 | int a; 8 | a = 4.5; 9 | f(a); 10 | return; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/funcall_var.cminus: -------------------------------------------------------------------------------- 1 | void test(int a) { 2 | output(a); 3 | return; 4 | } 5 | 6 | void main(void) { 7 | int a; 8 | a = 10; 9 | test(a); 10 | return; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/return_in_middle1.cminus: -------------------------------------------------------------------------------- 1 | int result(void){ 2 | int i; 3 | if (1) { 4 | i = 1; 5 | return 0; 6 | } else { 7 | i = 2; 8 | } 9 | output(3); 10 | return 3; 11 | } 12 | 13 | void main(void) { 14 | output(result()); 15 | return; 16 | } 17 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/return_in_middle2.cminus: -------------------------------------------------------------------------------- 1 | int result(void){ 2 | int i; 3 | i = 10; 4 | while (i > 0) { 5 | return 0; 6 | } 7 | output(4); 8 | return 1; 9 | } 10 | 11 | void main(void) { 12 | output(result()); 13 | return; 14 | } 15 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/return_type_mismatch1.cminus: -------------------------------------------------------------------------------- 1 | int f(void) { 2 | return 233.3; 3 | } 4 | 5 | void main(void) { 6 | output(f()); 7 | return; 8 | } 9 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv2/return_type_mismatch2.cminus: -------------------------------------------------------------------------------- 1 | float f(void) { 2 | return 7; 3 | } 4 | 5 | void main(void) { 6 | outputFloat(f()); 7 | return; 8 | } 9 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv3/complex1.cminus: -------------------------------------------------------------------------------- 1 | /* 2 | This code is adopted from Dik T. Winter at CWI 3 | It computes pi to 800 decimal digits 4 | */ 5 | 6 | int mod(int a, int b) { 7 | return a - a / b * b; 8 | } 9 | 10 | void printfour(int input) { 11 | int a; 12 | int b; 13 | int c; 14 | int d; 15 | input = mod(input, 10000); 16 | d = mod(input, 10); 17 | input = input / 10; 18 | c = mod(input, 10); 19 | input = input / 10; 20 | b = mod(input, 10); 21 | input = input / 10; 22 | a = input; 23 | output(a); 24 | output(b); 25 | output(c); 26 | output(d); 27 | return; 28 | } 29 | 30 | void main(void) { 31 | int r[2801]; 32 | int i; 33 | int k; 34 | int b; 35 | int d; 36 | int c; 37 | c = 0; 38 | d = 1234; 39 | 40 | { 41 | int mod; 42 | mod = 0; 43 | while (mod < 2800) { 44 | r[mod] = 2000; 45 | mod = mod + 1; 46 | } 47 | } 48 | 49 | k = 2800; 50 | while (k) { 51 | int d; 52 | d = 0; 53 | i = k; 54 | 55 | while (i != 0) { 56 | d = d + r[i] * 10000; 57 | b = 2 * i - 1; 58 | r[i] = mod(d, b); 59 | d = d / b; 60 | i = i - 1; 61 | if (i != 0) { 62 | d = d * i; 63 | } 64 | } 65 | 66 | printfour(c + d / 10000); 67 | c = mod(d, 10000); 68 | 69 | k = k - 14; 70 | } 71 | 72 | return; 73 | } 74 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv3/complex2.cminus: -------------------------------------------------------------------------------- 1 | /* this is the sample program in C- in the book "Compiler Construction" */ 2 | /* A program to perform selection sort on a 10 element array. */ 3 | float x[10]; 4 | int minloc (float a[], float low, int high ) 5 | { 6 | int i; 7 | int x; 8 | int k; 9 | k = low; 10 | x = a[low]; 11 | i = low + 1; 12 | while (i < high) { 13 | if (a[i] < x) { 14 | x = a[i]; 15 | k = i; 16 | } 17 | i = i + 1; 18 | } 19 | return k; 20 | } 21 | 22 | void sort(float a[], int low, float high) 23 | { 24 | int i; 25 | int k; 26 | i = low; 27 | while (i < high - 1) 28 | { 29 | int t; 30 | k = minloc(a, i, high); 31 | t = a[k]; 32 | a[k] = a[i]; 33 | a[i] = t; 34 | i = i + 1; 35 | } 36 | return; 37 | } 38 | 39 | void main(void) 40 | { 41 | int i; 42 | i = 0; 43 | while ( i < 10) { 44 | x[i] = input(); 45 | i = i + 1; 46 | } 47 | sort(x, 0, 10); 48 | i = 0; 49 | while (i < 10) { 50 | output(x[i]); 51 | i = i + 1; 52 | } 53 | return; 54 | } 55 | -------------------------------------------------------------------------------- /tests/lab4/ta_testcases/lv3/complex3.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | 6 | void main(void) { 7 | int x; 8 | int y; 9 | int temp; 10 | x = input(); 11 | y = input(); 12 | if (x < y) { 13 | temp = x; 14 | x = y; 15 | y = temp; 16 | } 17 | temp = gcd(x, y); 18 | output(temp); 19 | return; 20 | } 21 | -------------------------------------------------------------------------------- /tests/lab4/testcases/01.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | input(); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/01.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/01.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/01.out -------------------------------------------------------------------------------- /tests/lab4/testcases/02.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1234); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/02.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/03.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | return; 3 | } 4 | -------------------------------------------------------------------------------- /tests/lab4/testcases/03.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/03.out -------------------------------------------------------------------------------- /tests/lab4/testcases/04.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/04.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/04.out -------------------------------------------------------------------------------- /tests/lab4/testcases/05.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/05.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/05.out -------------------------------------------------------------------------------- /tests/lab4/testcases/06.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float a; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/06.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/06.out -------------------------------------------------------------------------------- /tests/lab4/testcases/07.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float a[10]; 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/07.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myl7/compiler-2020-labs/885da3b402582b17f01af341f2fd08d97d638805/tests/lab4/testcases/07.out -------------------------------------------------------------------------------- /tests/lab4/testcases/08.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | outputFloat(123.4); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/08.out: -------------------------------------------------------------------------------- 1 | 123.400002 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/09.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | output(1000 + 234); 3 | return; 4 | } 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/09.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/10.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 1234; 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/testcases/10.out: -------------------------------------------------------------------------------- 1 | 1234 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/11.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int a; 3 | a = 1.0; 4 | output(a); 5 | return; 6 | } 7 | -------------------------------------------------------------------------------- /tests/lab4/testcases/11.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/12.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | 6 | void main(void) { 7 | int x; 8 | int y; 9 | int temp; 10 | x = input(); 11 | y = input(); 12 | if (x < y) { 13 | temp = x; 14 | x = y; 15 | y = temp; 16 | } 17 | temp = gcd(x, y); 18 | output(temp); 19 | return; 20 | } 21 | -------------------------------------------------------------------------------- /tests/lab4/testcases/12.in: -------------------------------------------------------------------------------- 1 | 78 2 | 117 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/12.out: -------------------------------------------------------------------------------- 1 | 39 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/13.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | float a; 3 | a = 1 + 2.3; 4 | outputFloat(a); 5 | a = 1 * 2.3; 6 | outputFloat(a); 7 | a = 1 + 2.3 * 4; 8 | outputFloat(a); 9 | a = (1 + 2.3) * 4; 10 | outputFloat(a); 11 | return; 12 | } 13 | -------------------------------------------------------------------------------- /tests/lab4/testcases/13.out: -------------------------------------------------------------------------------- 1 | 3.300000 2 | 2.300000 3 | 10.200000 4 | 13.200000 5 | -------------------------------------------------------------------------------- /tests/lab4/testcases/14.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | if (1 > 1.1) { 3 | output(1); 4 | } 5 | if (1.1 > 2) { 6 | output(2); 7 | } 8 | if (1 >= 3.3) { 9 | output(3); 10 | } 11 | output(4); 12 | return; 13 | } 14 | -------------------------------------------------------------------------------- /tests/lab4/testcases/14.out: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /tests/lab4/testcases/15.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 0; 4 | while (a < 10) { 5 | output(a); 6 | a = a + 1; 7 | } 8 | return; 9 | } 10 | -------------------------------------------------------------------------------- /tests/lab4/testcases/15.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | -------------------------------------------------------------------------------- /tests/lab4/testcases/16.cminus: -------------------------------------------------------------------------------- 1 | float a(int x) { 2 | return x; 3 | } 4 | 5 | int b(float x) { 6 | return x; 7 | } 8 | 9 | void main(void) { 10 | outputFloat(a(1)); 11 | output(b(1.1)); 12 | return; 13 | } 14 | -------------------------------------------------------------------------------- /tests/lab4/testcases/16.out: -------------------------------------------------------------------------------- 1 | 1.000000 2 | 1 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/17.cminus: -------------------------------------------------------------------------------- 1 | float a(int x) { 2 | return x; 3 | } 4 | 5 | int b(float x) { 6 | return x + 1.1; 7 | } 8 | 9 | void main(void) { 10 | outputFloat(a(1.2)); 11 | output(b(11)); 12 | return; 13 | } 14 | -------------------------------------------------------------------------------- /tests/lab4/testcases/17.out: -------------------------------------------------------------------------------- 1 | 1.000000 2 | 12 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/18.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a[10]; 3 | int b; 4 | int c; 5 | b = 1; 6 | c = 0; 7 | while (c < 10) { 8 | a[c] = 3; 9 | c = c + 1; 10 | } 11 | while (b <= 8) { 12 | a[b + 1.5] = 2; 13 | b = b + 1; 14 | } 15 | c = 0; 16 | while (c < 10) { 17 | output(a[c]); 18 | c = c + 1; 19 | } 20 | return; 21 | } 22 | -------------------------------------------------------------------------------- /tests/lab4/testcases/18.out: -------------------------------------------------------------------------------- 1 | 3 2 | 3 3 | 2 4 | 2 5 | 2 6 | 2 7 | 2 8 | 2 9 | 2 10 | 2 11 | -------------------------------------------------------------------------------- /tests/lab4/testcases/19.cminus: -------------------------------------------------------------------------------- 1 | int b(float x) { 2 | return x > 1; 3 | } 4 | 5 | void main(void) { 6 | int a[10]; 7 | a[1 < 2] = 3; 8 | output(a[1]); 9 | a[2] = b(2 > 3); 10 | output(a[2]); 11 | return; 12 | } 13 | -------------------------------------------------------------------------------- /tests/lab4/testcases/19.out: -------------------------------------------------------------------------------- 1 | 3 2 | 0 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/20.cminus: -------------------------------------------------------------------------------- 1 | void main(void) { 2 | int a; 3 | a = 1 > 2; 4 | output(a); 5 | a = (2 > 1.0) > (1 * 0 != 0); 6 | output(a); 7 | return; 8 | } 9 | -------------------------------------------------------------------------------- /tests/lab4/testcases/20.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/21.cminus: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | void main(void) { 4 | output(a); 5 | a = 1; 6 | output(a); 7 | return; 8 | } 9 | -------------------------------------------------------------------------------- /tests/lab4/testcases/21.out: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | -------------------------------------------------------------------------------- /tests/lab4/testcases/22.cminus: -------------------------------------------------------------------------------- 1 | int arrrr[100]; 2 | int hello; 3 | int xx; 4 | 5 | int assign(void) 6 | { 7 | int i; 8 | int j; 9 | int k; 10 | xx = 1; 11 | k = i = j; 12 | if (k = i = j) 13 | { 14 | int l; 15 | } 16 | k = i < j; 17 | return k; 18 | } 19 | 20 | void iff(void) 21 | { 22 | int i; 23 | int j; 24 | if (i < j) 25 | { 26 | int x; 27 | int y; 28 | x = 0; 29 | return; 30 | } 31 | else 32 | { 33 | int y; 34 | int z; 35 | y = 0; 36 | return; 37 | } 38 | i = 1; 39 | j = 0; 40 | if (i < j) 41 | { 42 | int x; 43 | j = 2; 44 | } 45 | return; 46 | } 47 | 48 | void loop(void) 49 | { 50 | int i; 51 | int j; 52 | i = 0; 53 | j = 1; 54 | while (i < 10) 55 | { 56 | int z; 57 | i = i + 1; 58 | while (j < 10) 59 | { 60 | int z; 61 | j = j + 1; 62 | return; 63 | } 64 | } 65 | while (i < 10) 66 | return; 67 | return; 68 | } 69 | 70 | void main(void) 71 | { 72 | int x; 73 | int y; 74 | int temp; 75 | x = 1; 76 | y = 100; 77 | if (x > y) 78 | { 79 | int z; 80 | if (x > y) 81 | { 82 | int zz; 83 | x = 1; 84 | } 85 | else 86 | { 87 | int yy; 88 | x = 2; 89 | } 90 | { 91 | int t; 92 | } 93 | } 94 | else 95 | { 96 | int z; 97 | while (x == 1) 98 | { 99 | x = 2; 100 | } 101 | { 102 | int t; 103 | } 104 | } 105 | output(x); 106 | output(y); 107 | return; 108 | } 109 | -------------------------------------------------------------------------------- /tests/lab4/testcases/22.out: -------------------------------------------------------------------------------- 1 | 2 2 | 100 3 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-1.cminus: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) { 2 | if (v == 0) return u; 3 | else return gcd(v, u - u / v * v); 4 | } 5 | 6 | void main(void) { 7 | int x; 8 | int y; 9 | int temp; 10 | x = 7; 11 | y = 8; 12 | if (x < y) { 13 | temp = x; 14 | x = y; 15 | y = temp; 16 | } 17 | temp = gcd(x, y); 18 | output(temp); 19 | return; 20 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-1.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "function": "gcd", 3 | "live_in": { 4 | "label_entry": ["%arg0","%arg1",], 5 | "label8": ["%arg0",], 6 | "label10": ["%arg0","%arg1",], 7 | 8 | }, 9 | "live_out": { 10 | "label_entry": ["%arg0","%arg1",], 11 | 12 | } 13 | } 14 | 15 | ,{ 16 | "function": "main", 17 | "live_in": { 18 | 19 | }, 20 | "live_out": { 21 | 22 | } 23 | } 24 | 25 | ,] -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-2.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | 3 | int i; 4 | int j; 5 | int k; 6 | int a; 7 | int b; 8 | int c; 9 | int d; 10 | 11 | i = 0 ; 12 | j = 2; 13 | k = 4; 14 | a = 6; 15 | b = 8; 16 | c = 10; 17 | d = 12; 18 | 19 | if( i > j ) 20 | { 21 | k = k + 1; 22 | } 23 | if( k > a) 24 | { 25 | b = b + 1; 26 | } 27 | if( b > c) 28 | { 29 | d = d + c; 30 | } 31 | 32 | return; 33 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-2.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "function": "main", 3 | "live_in": { 4 | "label15": ["%op14",], 5 | "label24": ["%op23",], 6 | "label34": ["%op33",], 7 | 8 | }, 9 | "live_out": { 10 | "label12": ["%op14",], 11 | "label21": ["%op23",], 12 | "label30": ["%op33",], 13 | 14 | } 15 | } 16 | 17 | ,] -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-3.cminus: -------------------------------------------------------------------------------- 1 | int global[25]; 2 | void main(void){ 3 | int i; 4 | int j; 5 | 6 | int a[25]; 7 | 8 | i=0; 9 | while (i < 25){ 10 | a[i] = i; 11 | i=i+1; 12 | } 13 | 14 | i = 0; 15 | while (i < 5){ 16 | j = 0; 17 | while (j < 5){ 18 | int x; 19 | int y; 20 | 21 | x = i+j; 22 | y = i*j; 23 | 24 | if(a[x] > a[y]) 25 | { 26 | global[i * 5 + j] = a[x]; 27 | } 28 | else 29 | { 30 | global[i * 5 + j] = a[y]; 31 | } 32 | j = j + 1; 33 | } 34 | i = 1 + i; 35 | } 36 | 37 | i=0; 38 | while (i < 5){ 39 | j=0; 40 | while (j < 5){ 41 | output(global[i * 5 + j]); 42 | j = j + 1; 43 | } 44 | i = 1 + i; 45 | } 46 | return; 47 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-4.cminus: -------------------------------------------------------------------------------- 1 | /* this is the sample program in C- in the book "Compiler Construction" */ 2 | /* A program to perform selection sort on a 10 element array. */ 3 | int x[10]; 4 | int minloc ( int a[], int low, int high ) 5 | { int i; int x; int k; 6 | k = low; 7 | x = a[low]; 8 | i = low + 1; 9 | while (i < high) 10 | { if (a[i] < x) 11 | { x = a[i]; 12 | k = i; } 13 | i = i + 1; 14 | } 15 | return k; 16 | } 17 | 18 | void sort( int a[], int low, int high) 19 | { int i; int k; 20 | i = low; 21 | while ( i < high-1) 22 | { int t; 23 | k = minloc(a, i, high); 24 | t = a[k]; 25 | a[k] = a[i]; 26 | a[i] = t; 27 | i = i + 1; 28 | } 29 | return; 30 | } 31 | 32 | void main(void) 33 | { int i; 34 | i = 0; 35 | 36 | x[0] = 4; 37 | x[1] = 7; 38 | x[2] = 9; 39 | x[3] = 2; 40 | x[4] = 0; 41 | x[5] = 6; 42 | x[6] = 1; 43 | x[7] = 3; 44 | x[8] = 5; 45 | x[9] = 8; 46 | 47 | sort(x, 0, 10); 48 | i = 0; 49 | while (i < 10) 50 | { output(x[i]); 51 | i = i + 1; } 52 | return; 53 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-5.cminus: -------------------------------------------------------------------------------- 1 | int x; 2 | int A[400]; 3 | int B[20]; 4 | int C[20]; 5 | 6 | void mv(int n, int A[], int b[], int res[]){ 7 | int i; 8 | int j; 9 | int x; 10 | int y; 11 | 12 | y = 0; 13 | x = 11; 14 | 15 | 16 | i = 0; 17 | while(i < n){ 18 | res[i] = 0; 19 | i = i + 1; 20 | } 21 | 22 | i = 0; 23 | j = 0; 24 | while (i < n){ 25 | j = 0; 26 | while (j < n){ 27 | if (A[i*20+j] == 0){ 28 | x = x * b[i] + b[j]; 29 | y = y - x; 30 | }else{ 31 | res[i] = res[i] + A[i*20+j] * b[j]; 32 | } 33 | j = j + 1; 34 | } 35 | i = i + 1; 36 | } 37 | } 38 | 39 | 40 | void main(void){ 41 | int n; 42 | int i; 43 | int j; 44 | 45 | n =14; 46 | i = 0; 47 | 48 | while (i < n){ 49 | j = 0; 50 | while (j < n){ 51 | A[i*20+j] = (n-i)*(n-j); 52 | j = j + 1; 53 | } 54 | i = i + 1; 55 | } 56 | 57 | i = 0; 58 | while (i < n){ 59 | B[i] = i*(n-i); 60 | i = i + 1; 61 | } 62 | 63 | i = 0; 64 | mv(n, A, B, C); 65 | mv(n, A, C, B); 66 | 67 | output(C[n-1]); 68 | return ; 69 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-6.cminus: -------------------------------------------------------------------------------- 1 | int x[10]; 2 | int y[10]; 3 | int v[10]; 4 | int a[10]; 5 | int b[10]; 6 | int c[10]; 7 | 8 | void spmv(int n,int xptr[], int yidx[], int vals[], int b[], int x[]){ 9 | int i; 10 | int j; 11 | int k; 12 | 13 | i = 0; 14 | while (i < n){ 15 | x[i] = 0; 16 | i = i + 1; 17 | } 18 | 19 | i = 0; 20 | while (i < n){ 21 | j = xptr[i]; 22 | while (j < xptr[i + 1]){ 23 | x[yidx[j]] = x[yidx[j]] + vals[j]; 24 | j = j + 1; 25 | } 26 | 27 | j = xptr[i]; 28 | while (j < xptr[i + 1]){ 29 | x[yidx[j]] = x[yidx[j]] + vals[j] * (b[i] - 1); 30 | j = j + 1; 31 | } 32 | i = i + 1; 33 | } 34 | } 35 | 36 | int getarray(int input[]) 37 | { 38 | int i; 39 | 40 | i = 0; 41 | while(i < 10) { 42 | input[i] = i; 43 | i = i + 1; 44 | } 45 | return 10; 46 | } 47 | 48 | void main(void){ 49 | int i; 50 | int n; 51 | int m; 52 | 53 | i = 0; 54 | n = getarray(x); 55 | m = getarray(y); 56 | 57 | getarray(v); 58 | getarray(a); 59 | 60 | spmv(n, x, y, v, a, b); 61 | spmv(n, x, y, v, b, a); 62 | 63 | output(b[0]); 64 | output(b[1]); 65 | output(b[2]); 66 | output(b[3]); 67 | output(b[4]); 68 | output(b[5]); 69 | output(b[6]); 70 | output(b[7]); 71 | output(b[8]); 72 | output(b[9]); 73 | return ; 74 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ActiveVars/testcase-7.cminus: -------------------------------------------------------------------------------- 1 | int matrix[20000000]; 2 | int a[100000]; 3 | 4 | int transpose(int n, int matrix[], int rowsize){ 5 | int colsize; 6 | int i; 7 | int j; 8 | 9 | i = 0; 10 | j = 0; 11 | colsize = n / rowsize; 12 | 13 | while (i < colsize){ 14 | j = 0; 15 | while (j < rowsize){ 16 | int curr; 17 | if (i < j){ 18 | j = j + 1; 19 | } 20 | else { 21 | curr = matrix[i * rowsize + j]; 22 | matrix[j * colsize + i] = matrix[i * rowsize + j]; 23 | matrix[i * rowsize + j] = curr; 24 | j = j + 1; 25 | } 26 | } 27 | i = i + 1; 28 | } 29 | return 1; 30 | } 31 | 32 | void main(void){ 33 | int n ; 34 | int len; 35 | int i; 36 | int ans; 37 | 38 | n = input(); 39 | len = 100000; 40 | 41 | i = 0; 42 | while (i < n){ 43 | matrix[i] = i; 44 | i = i + 1; 45 | } 46 | 47 | i = 0; 48 | while (i < len){ 49 | transpose(n, matrix, a[i]); 50 | i = i + 1; 51 | } 52 | 53 | ans = 0; 54 | 55 | i = 0; 56 | while (i < len){ 57 | ans = ans + i * i * matrix[i]; 58 | i = i + 1; 59 | } 60 | 61 | return ; 62 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-1.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-1.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | br label %label2 15 | label2: ; preds = %label_entry, %label7 16 | %op78 = phi i32 [ 0, %label_entry ], [ %op73, %label7 ] 17 | %op79 = phi i32 [ 0, %label_entry ], [ 34, %label7 ] 18 | %op4 = icmp slt i32 %op78, 100000000 19 | %op5 = zext i1 %op4 to i32 20 | %op6 = icmp ne i32 %op5, 0 21 | br i1 %op6, label %label7, label %label74 22 | label7: ; preds = %label2 23 | %op73 = add i32 %op78, 1 24 | br label %label2 25 | label74: ; preds = %label2 26 | %op77 = mul i32 %op79, %op79 27 | call void @output(i32 %op77) 28 | ret void 29 | } 30 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-2.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-2.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | br label %label6 15 | label6: ; preds = %label_entry, %label11 16 | %op65 = phi i32 [ 711082625, %label11 ], [ undef, %label_entry ] 17 | %op66 = phi i32 [ -599454271, %label11 ], [ undef, %label_entry ] 18 | %op67 = phi i32 [ 632274337, %label11 ], [ undef, %label_entry ] 19 | %op68 = phi i32 [ -1115555215, %label11 ], [ undef, %label_entry ] 20 | %op69 = phi i32 [ 0, %label_entry ], [ %op62, %label11 ] 21 | %op70 = phi i32 [ 0, %label_entry ], [ 457, %label11 ] 22 | %op8 = icmp slt i32 %op69, 100000000 23 | %op9 = zext i1 %op8 to i32 24 | %op10 = icmp ne i32 %op9, 0 25 | br i1 %op10, label %label11, label %label63 26 | label11: ; preds = %label6 27 | %op62 = add i32 %op69, 1 28 | br label %label6 29 | label63: ; preds = %label6 30 | call void @output(i32 %op65) 31 | ret void 32 | } 33 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-4.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-4.cminus" 3 | 4 | @a = global i32 zeroinitializer 5 | declare i32 @input() 6 | 7 | declare void @output(i32) 8 | 9 | declare void @outputFloat(float) 10 | 11 | declare void @neg_idx_except() 12 | 13 | define void @main() { 14 | label_entry: 15 | store i32 3, i32* @a 16 | br label %label4 17 | label4: ; preds = %label_entry, %label35 18 | %op38 = phi i32 [ 0, %label_entry ], [ %op37, %label35 ] 19 | %op6 = icmp slt i32 %op38, 100000000 20 | %op7 = zext i1 %op6 to i32 21 | %op8 = icmp ne i32 %op7, 0 22 | br i1 %op8, label %label9, label %label28 23 | label9: ; preds = %label4 24 | %op10 = load i32, i32* @a 25 | %op18 = sitofp i32 %op10 to float 26 | %op19 = fadd float %op18, 0x4026bc77a0000000 27 | %op26 = fsub float %op19, 0x408c9dd680000000 28 | %op27 = fcmp une float %op26,0x0 29 | br i1 %op27, label %label32, label %label35 30 | label28: ; preds = %label4 31 | %op29 = load i32, i32* @a 32 | %op30 = mul i32 %op29, 2 33 | %op31 = add i32 %op30, 5 34 | call void @output(i32 %op31) 35 | ret void 36 | label32: ; preds = %label9 37 | %op33 = load i32, i32* @a 38 | %op34 = add i32 %op33, 1 39 | store i32 %op34, i32* @a 40 | br label %label35 41 | label35: ; preds = %label9, %label32 42 | %op37 = add i32 %op38, 1 43 | br label %label4 44 | } 45 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-5.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-5.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | br label %label4 15 | label4: ; preds = %label_entry, %label50 16 | %op53 = phi i32 [ %op21, %label50 ], [ undef, %label_entry ] 17 | %op54 = phi i32 [ 0, %label_entry ], [ %op56, %label50 ] 18 | %op55 = phi i32 [ 0, %label_entry ], [ %op52, %label50 ] 19 | %op6 = icmp slt i32 %op55, 100000000 20 | %op7 = zext i1 %op6 to i32 21 | %op8 = icmp ne i32 %op7, 0 22 | br i1 %op8, label %label9, label %label29 23 | label9: ; preds = %label4 24 | %op12 = sdiv i32 %op55, 10000000 25 | %op19 = sitofp i32 %op12 to float 26 | %op20 = fadd float %op19, 0x0 27 | %op21 = fptosi float %op20 to i32 28 | br label %label31 29 | label29: ; preds = %label4 30 | call void @output(i32 %op54) 31 | ret void 32 | label31: ; preds = %label9 33 | %op38 = mul i32 2628, %op21 34 | %op40 = mul i32 %op38, %op21 35 | %op42 = mul i32 %op40, %op21 36 | %op44 = mul i32 %op42, %op21 37 | %op46 = mul i32 %op44, %op21 38 | %op48 = mul i32 %op46, %op21 39 | %op49 = add i32 %op54, %op48 40 | br label %label50 41 | label50: ; preds = %label31 42 | %op56 = phi i32 [ %op54, %label9 ], [ %op49, %label31 ] 43 | %op52 = add i32 %op55, 1 44 | br label %label4 45 | } 46 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-6.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-6.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define i32 @meanless(i32 %arg0) { 13 | label_entry: 14 | br label %label4 15 | label4: ; preds = %label_entry, %label10 16 | %op21 = phi i32 [ 178, %label10 ], [ undef, %label_entry ] 17 | %op22 = phi i32 [ 0, %label_entry ], [ %op18, %label10 ] 18 | %op7 = icmp slt i32 %op22, %arg0 19 | %op8 = zext i1 %op7 to i32 20 | %op9 = icmp ne i32 %op8, 0 21 | br i1 %op9, label %label10, label %label19 22 | label10: ; preds = %label4 23 | %op18 = add i32 %op22, 1 24 | br label %label4 25 | label19: ; preds = %label4 26 | ret i32 %op21 27 | } 28 | define void @main() { 29 | label_entry: 30 | br label %label5 31 | label5: ; preds = %label_entry, %label17 32 | %op20 = phi i32 [ 0, %label_entry ], [ %op19, %label17 ] 33 | %op7 = icmp slt i32 %op20, 10000000 34 | %op8 = zext i1 %op7 to i32 35 | %op9 = icmp ne i32 %op8, 0 36 | br i1 %op9, label %label10, label %label13 37 | label10: ; preds = %label5 38 | br label %label14 39 | label13: ; preds = %label5 40 | ret void 41 | label14: ; preds = %label10 42 | %op16 = call i32 @meanless(i32 21) 43 | br label %label17 44 | label17: ; preds = %label14 45 | %op19 = add i32 %op20, 1 46 | br label %label5 47 | } 48 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-7.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-7.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define i32 @score(i32 %arg0, i32 %arg1) { 13 | label_entry: 14 | br label %label14 15 | label14: ; preds = %label_entry 16 | br label %label22 17 | label20: ; preds = %label27 18 | ret i32 %op50 19 | label22: ; preds = %label14 20 | br label %label28 21 | label27: ; preds = %label33 22 | br label %label20 23 | label28: ; preds = %label22 24 | br label %label34 25 | label33: ; preds = %label39 26 | br label %label27 27 | label34: ; preds = %label28 28 | br label %label40 29 | label39: ; preds = %label51 30 | br label %label33 31 | label40: ; preds = %label34 32 | %op43 = sub i32 %arg1, %arg0 33 | %op45 = mul i32 %op43, 16 34 | %op47 = mul i32 %op45, 11 35 | %op49 = sub i32 10, %arg1 36 | %op50 = mul i32 %op47, %op49 37 | br label %label51 38 | label51: ; preds = %label40 39 | br label %label39 40 | } 41 | define void @main() { 42 | label_entry: 43 | br label %label4 44 | label4: ; preds = %label_entry, %label9 45 | %op24 = phi i32 [ 0, %label_entry ], [ %op19, %label9 ] 46 | %op25 = phi i32 [ %op14, %label9 ], [ undef, %label_entry ] 47 | %op26 = phi i32 [ %op11, %label9 ], [ undef, %label_entry ] 48 | %op27 = phi i32 [ 0, %label_entry ], [ %op21, %label9 ] 49 | %op6 = icmp slt i32 %op27, 100000000 50 | %op7 = zext i1 %op6 to i32 51 | %op8 = icmp ne i32 %op7, 0 52 | br i1 %op8, label %label9, label %label22 53 | label9: ; preds = %label4 54 | %op11 = sdiv i32 %op27, 10000000 55 | %op13 = sdiv i32 %op27, 10000000 56 | %op14 = sub i32 9, %op13 57 | %op18 = call i32 @score(i32 %op11, i32 %op14) 58 | %op19 = add i32 %op24, %op18 59 | %op21 = add i32 %op27, 1 60 | br label %label4 61 | label22: ; preds = %label4 62 | call void @output(i32 %op24) 63 | ret void 64 | } 65 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/baseline/testcase-8.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/ConstPropagation/testcase-8.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define i32 @main() { 13 | label_entry: 14 | br label %label2 15 | label2: ; preds = %label_entry, %label32 16 | %op47 = phi i32 [ 0, %label_entry ], [ %op49, %label32 ] 17 | %op48 = phi i32 [ 0, %label_entry ], [ %op34, %label32 ] 18 | %op4 = icmp slt i32 %op48, 100000000 19 | %op5 = zext i1 %op4 to i32 20 | %op6 = icmp ne i32 %op5, 0 21 | br i1 %op6, label %label7, label %label10 22 | label7: ; preds = %label2 23 | %op9 = icmp ne i32 %op47, 0 24 | br i1 %op9, label %label12, label %label22 25 | label10: ; preds = %label2 26 | call void @output(i32 %op47) 27 | ret i32 0 28 | label12: ; preds = %label7 29 | br label %label35 30 | label22: ; preds = %label7 31 | br label %label41 32 | label32: ; preds = %label40, %label46 33 | %op49 = phi i32 [ 1, %label40 ], [ 5, %label46 ] 34 | %op34 = add i32 %op48, 1 35 | br label %label2 36 | label35: ; preds = %label12 37 | br label %label40 38 | label40: ; preds = %label35 39 | br label %label32 40 | label41: ; preds = %label22 41 | br label %label46 42 | label46: ; preds = %label41 43 | br label %label32 44 | } 45 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-1.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int idx; 4 | 5 | i = 0; 6 | idx = 0; 7 | 8 | while(i < 100000000) 9 | { 10 | idx = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ; 11 | i=i+idx*idx*idx*idx*idx*idx*idx*idx/(idx*idx*idx*idx*idx*idx*idx*idx); 12 | } 13 | output(idx*idx); 14 | return ; 15 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-1.out: -------------------------------------------------------------------------------- 1 | 1156 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-2.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int c; 3 | int a; 4 | int b; 5 | int d; 6 | int f; 7 | int g; 8 | 9 | c=0; 10 | a=0; 11 | while(c<100000000) 12 | { 13 | a=1.23456*5.73478*2.3333*4.3673*6.34636; 14 | b=a*a*a*a*a*a; 15 | d=b*b*b*b*b*b; 16 | f=d*d*d*d*d*d; 17 | g=f*f*f*f*f*f; 18 | c=c+1; 19 | } 20 | output(g); 21 | return ; 22 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-2.out: -------------------------------------------------------------------------------- 1 | 711082625 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-3.cminus: -------------------------------------------------------------------------------- 1 | int opa; 2 | int opb; 3 | int opc; 4 | int opd; 5 | 6 | int max(void) 7 | { 8 | opa = 0*1*2*3*4*5*6*7; 9 | opb = 1*2*3*4*5*6*7*8; 10 | opc = 2*3*4*5*6*7*8*9; 11 | opd = 3*4*5*6*7*8*9*10; 12 | 13 | if( opa < opb){ 14 | if( opb < opc){ 15 | if( opc < opd){ 16 | return opd; 17 | } 18 | } 19 | } 20 | return 0; 21 | } 22 | 23 | void main(void){ 24 | int i; 25 | 26 | i = 0; 27 | 28 | while(i<200000000) 29 | { 30 | max(); 31 | i=i+1; 32 | } 33 | 34 | output(opa); 35 | output(opb); 36 | output(opc); 37 | output(opd); 38 | 39 | return ; 40 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-3.out: -------------------------------------------------------------------------------- 1 | 0 2 | 40320 3 | 362880 4 | 1814400 5 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-4.cminus: -------------------------------------------------------------------------------- 1 | int a; 2 | void main(void){ 3 | int i; 4 | 5 | a=(1<2)+2; 6 | i=0; 7 | while(i<100000000) 8 | { 9 | if(a+(3.0*1.23456*5.73478*2.3333*4.3673/6.34636)/3-2*1.23456*5.73478*2.3333*4.3673*6.34636) 10 | { 11 | a=a+1; 12 | } 13 | i=i+1; 14 | } 15 | output(a*2+5); 16 | return ; 17 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-4.out: -------------------------------------------------------------------------------- 1 | 200000011 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-5.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int a; 4 | int b; 5 | int ret; 6 | 7 | a=1; 8 | b=2; 9 | ret = 0; 10 | 11 | i=0; 12 | while(i<100000000) 13 | { 14 | int index; 15 | index = i/10000000 + 0*1.23456*5.73478*2.3333*4.3673*6.34636; 16 | if( (a < 3) < b ) 17 | { 18 | ret = ret + a*b*1314*index*index*index*index*index*index; 19 | } 20 | i = i+1; 21 | } 22 | 23 | output(ret); 24 | return ; 25 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-5.out: -------------------------------------------------------------------------------- 1 | 257532416 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-6.cminus: -------------------------------------------------------------------------------- 1 | int meanless(int num) 2 | { 3 | int i; 4 | int nothing; 5 | i=0; 6 | while(i 10) 15 | { 16 | if( hqw >5) 17 | { 18 | grades = (optTime - originTime)*cqy*hqw*(10-optTime); 19 | } 20 | } 21 | } 22 | } 23 | } 24 | return grades; 25 | } 26 | 27 | void main(void){ 28 | int i; 29 | int a; 30 | int b; 31 | int ret; 32 | 33 | ret = 0; 34 | 35 | i=0; 36 | while(i<100000000) 37 | { 38 | a = i/10000000; 39 | b = 9 - i/10000000; 40 | ret = ret + score(a, b); 41 | i = i+1; 42 | } 43 | 44 | output(ret); 45 | return ; 46 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-7.out: -------------------------------------------------------------------------------- 1 | 1657776128 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-8.cminus: -------------------------------------------------------------------------------- 1 | int main(void){ 2 | int a; 3 | int b; 4 | a=0; 5 | b=0; 6 | while(b<100000000){ 7 | if(a){ 8 | a=1; 9 | if(a+1.1111*2.33333+4.44444<10){ 10 | a=1.11111*1.111111*1.111111; 11 | } else { 12 | a=4; 13 | } 14 | } else { 15 | a=0; 16 | if(a+1.1111*2.33333+4.44444<10){ 17 | a=5; 18 | } else { 19 | a=1.11111*1.111111*1.111111; 20 | } 21 | } 22 | b=b+1; 23 | } 24 | 25 | output(a); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/ConstPropagation/testcase-8.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/baseline/testcase-1.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/LoopInvHoist/testcase-5.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | br label %label3 15 | label3: ; preds = %label_entry, %label58 16 | %op61 = phi i32 [ %op64, %label58 ], [ undef, %label_entry ] 17 | %op62 = phi i32 [ 1, %label_entry ], [ %op60, %label58 ] 18 | %op63 = phi i32 [ %op65, %label58 ], [ undef, %label_entry ] 19 | %op5 = icmp slt i32 %op62, 10000 20 | %op6 = zext i1 %op5 to i32 21 | %op7 = icmp ne i32 %op6, 0 22 | br i1 %op7, label %label8, label %label9 23 | label8: ; preds = %label3 24 | %op19 = mul i32 %op62, %op62 25 | %op21 = mul i32 %op19, %op62 26 | %op23 = mul i32 %op21, %op62 27 | %op25 = mul i32 %op23, %op62 28 | %op27 = mul i32 %op25, %op62 29 | %op29 = mul i32 %op27, %op62 30 | %op31 = mul i32 %op29, %op62 31 | %op33 = mul i32 %op31, %op62 32 | %op35 = mul i32 %op33, %op62 33 | %op37 = sdiv i32 %op35, %op62 34 | %op39 = sdiv i32 %op37, %op62 35 | %op41 = sdiv i32 %op39, %op62 36 | %op43 = sdiv i32 %op41, %op62 37 | %op45 = sdiv i32 %op43, %op62 38 | %op47 = sdiv i32 %op45, %op62 39 | %op49 = sdiv i32 %op47, %op62 40 | %op51 = sdiv i32 %op49, %op62 41 | %op53 = sdiv i32 %op51, %op62 42 | %op55 = sdiv i32 %op53, %op62 43 | br label %label11 44 | label9: ; preds = %label3 45 | call void @output(i32 %op61) 46 | ret void 47 | label11: ; preds = %label8, %label16 48 | %op64 = phi i32 [ %op61, %label8 ], [ %op55, %label16 ] 49 | %op65 = phi i32 [ 0, %label8 ], [ %op57, %label16 ] 50 | %op13 = icmp slt i32 %op65, 10000 51 | %op14 = zext i1 %op13 to i32 52 | %op15 = icmp ne i32 %op14, 0 53 | br i1 %op15, label %label16, label %label58 54 | label16: ; preds = %label11 55 | %op57 = add i32 %op65, 1 56 | br label %label11 57 | label58: ; preds = %label11 58 | %op60 = add i32 %op62, 1 59 | br label %label3 60 | } 61 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/baseline/testcase-2.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "/home/haiqwa/2020fall-compiler_cminus/tests/lab5/./testcases/LoopInvHoist/testcase-6.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | %op20 = mul i32 2, 2 15 | %op22 = mul i32 %op20, 2 16 | %op24 = mul i32 %op22, 2 17 | %op26 = mul i32 %op24, 2 18 | %op28 = mul i32 %op26, 2 19 | %op30 = mul i32 %op28, 2 20 | %op32 = mul i32 %op30, 2 21 | %op34 = mul i32 %op32, 2 22 | %op36 = mul i32 %op34, 2 23 | %op38 = sdiv i32 %op36, 2 24 | %op40 = sdiv i32 %op38, 2 25 | %op42 = sdiv i32 %op40, 2 26 | %op44 = sdiv i32 %op42, 2 27 | %op46 = sdiv i32 %op44, 2 28 | %op48 = sdiv i32 %op46, 2 29 | %op50 = sdiv i32 %op48, 2 30 | %op52 = sdiv i32 %op50, 2 31 | %op54 = sdiv i32 %op52, 2 32 | %op56 = sdiv i32 %op54, 2 33 | br label %label4 34 | label4: ; preds = %label_entry, %label59 35 | %op62 = phi i32 [ %op65, %label59 ], [ undef, %label_entry ] 36 | %op63 = phi i32 [ 0, %label_entry ], [ %op61, %label59 ] 37 | %op64 = phi i32 [ %op66, %label59 ], [ undef, %label_entry ] 38 | %op6 = icmp slt i32 %op63, 10000000 39 | %op7 = zext i1 %op6 to i32 40 | %op8 = icmp ne i32 %op7, 0 41 | br i1 %op8, label %label9, label %label10 42 | label9: ; preds = %label4 43 | br label %label12 44 | label10: ; preds = %label4 45 | call void @output(i32 %op62) 46 | ret void 47 | label12: ; preds = %label9, %label17 48 | %op65 = phi i32 [ %op62, %label9 ], [ %op56, %label17 ] 49 | %op66 = phi i32 [ 0, %label9 ], [ %op58, %label17 ] 50 | %op14 = icmp slt i32 %op66, 2 51 | %op15 = zext i1 %op14 to i32 52 | %op16 = icmp ne i32 %op15, 0 53 | br i1 %op16, label %label17, label %label59 54 | label17: ; preds = %label12 55 | %op58 = add i32 %op66, 1 56 | br label %label12 57 | label59: ; preds = %label12 58 | %op61 = add i32 %op63, 1 59 | br label %label4 60 | } 61 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/baseline/testcase-6.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'cminus' 2 | source_filename = "testcase-6.cminus" 3 | 4 | declare i32 @input() 5 | 6 | declare void @output(i32) 7 | 8 | declare void @outputFloat(float) 9 | 10 | declare void @neg_idx_except() 11 | 12 | define void @main() { 13 | label_entry: 14 | %op20 = mul i32 2, 2 15 | %op22 = mul i32 %op20, 2 16 | %op24 = mul i32 %op22, 2 17 | %op26 = mul i32 %op24, 2 18 | %op28 = mul i32 %op26, 2 19 | %op30 = mul i32 %op28, 2 20 | %op32 = mul i32 %op30, 2 21 | %op34 = mul i32 %op32, 2 22 | %op36 = mul i32 %op34, 2 23 | %op38 = sdiv i32 %op36, 2 24 | %op40 = sdiv i32 %op38, 2 25 | %op42 = sdiv i32 %op40, 2 26 | %op44 = sdiv i32 %op42, 2 27 | %op46 = sdiv i32 %op44, 2 28 | %op48 = sdiv i32 %op46, 2 29 | %op50 = sdiv i32 %op48, 2 30 | %op52 = sdiv i32 %op50, 2 31 | %op54 = sdiv i32 %op52, 2 32 | %op56 = sdiv i32 %op54, 2 33 | br label %label4 34 | label4: ; preds = %label_entry, %label59 35 | %op62 = phi i32 [ %op65, %label59 ], [ undef, %label_entry ] 36 | %op63 = phi i32 [ 0, %label_entry ], [ %op61, %label59 ] 37 | %op64 = phi i32 [ %op66, %label59 ], [ undef, %label_entry ] 38 | %op6 = icmp slt i32 %op63, 10000000 39 | %op7 = zext i1 %op6 to i32 40 | %op8 = icmp ne i32 %op7, 0 41 | br i1 %op8, label %label9, label %label10 42 | label9: ; preds = %label4 43 | br label %label12 44 | label10: ; preds = %label4 45 | call void @output(i32 %op62) 46 | ret void 47 | label12: ; preds = %label9, %label17 48 | %op65 = phi i32 [ %op62, %label9 ], [ %op56, %label17 ] 49 | %op66 = phi i32 [ 0, %label9 ], [ %op58, %label17 ] 50 | %op14 = icmp slt i32 %op66, 2 51 | %op15 = zext i1 %op14 to i32 52 | %op16 = icmp ne i32 %op15, 0 53 | br i1 %op16, label %label17, label %label59 54 | label17: ; preds = %label12 55 | %op58 = add i32 %op66, 1 56 | br label %label12 57 | label59: ; preds = %label12 58 | %op61 = add i32 %op63, 1 59 | br label %label4 60 | } 61 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-1.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int ret; 5 | 6 | i = 1; 7 | 8 | while(i<10000) 9 | { 10 | j = 0; 11 | while(j<10000) 12 | { 13 | ret = (i*i*i*i*i*i*i*i*i*i)/i/i/i/i/i/i/i/i/i/i; 14 | j=j+1; 15 | } 16 | i=i+1; 17 | } 18 | output(ret); 19 | return ; 20 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-1.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-2.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int a; 5 | int ret; 6 | 7 | i = 0; 8 | a = 2; 9 | 10 | while(i<10000000) 11 | { 12 | j = 0; 13 | 14 | while(j<2) 15 | { 16 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 17 | j=j+1; 18 | } 19 | i=i+1; 20 | } 21 | output(ret); 22 | return ; 23 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-2.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-3.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int k; 5 | int o; 6 | int p; 7 | int q; 8 | int a; 9 | int ret; 10 | 11 | a = 2; 12 | 13 | i = 0; 14 | while(i<1000000) 15 | { 16 | j = 0; 17 | while(j<2) 18 | { 19 | k = 0; 20 | while(k<2) 21 | { 22 | o = 0; 23 | while(o<2) 24 | { 25 | p = 0; 26 | while(p<2) 27 | { 28 | q = 0; 29 | while(q<2) 30 | { 31 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 32 | q=q+1; 33 | } 34 | p=p+1; 35 | } 36 | o=o+1; 37 | } 38 | k=k+1; 39 | } 40 | j=j+1; 41 | } 42 | i=i+1; 43 | } 44 | output(ret); 45 | return ; 46 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-3.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-4.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int k; 5 | int o; 6 | int p; 7 | int q; 8 | int a; 9 | int ret; 10 | 11 | a = 2; 12 | 13 | i = 0; 14 | while(i<1000000) 15 | { 16 | j = 0; 17 | while(j<2) 18 | { 19 | k = 0; 20 | while(k<2) 21 | { 22 | o = 0; 23 | while(o<2) 24 | { 25 | p = 0; 26 | while(p<2) 27 | { 28 | q = 0; 29 | while(q<2) 30 | { 31 | if( a > 1 ) 32 | { 33 | j = j+1; 34 | } 35 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 36 | q=q+1; 37 | } 38 | p=p+1; 39 | } 40 | o=o+1; 41 | } 42 | k=k+1; 43 | } 44 | j=j+1; 45 | } 46 | i=i+1; 47 | } 48 | output(ret); 49 | return ; 50 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-4.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-5.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int k; 5 | int ret; 6 | 7 | i = 1; 8 | 9 | while(i<100) 10 | { 11 | j = 0; 12 | while(j<100) 13 | { 14 | k=0; 15 | while(k<1000){ 16 | ret = (i*i*i*i*i*i*i*i*i*i)/i/i/i/i/i/i/i/i/i/i; 17 | k=k+1; 18 | } 19 | 20 | j=j+1; 21 | } 22 | i=i+1; 23 | } 24 | output(ret); 25 | return ; 26 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-5.out: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-6.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int i; 3 | int j; 4 | int a; 5 | int ret; 6 | 7 | i = 0; 8 | a = 2; 9 | 10 | while(i<10000000) 11 | { 12 | j = 0; 13 | 14 | while(j<2) 15 | { 16 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 17 | j=j+1; 18 | } 19 | i=i+1; 20 | } 21 | output(ret); 22 | return ; 23 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-6.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-7.cminus: -------------------------------------------------------------------------------- 1 | 2 | void main(void){ 3 | int i; 4 | int j; 5 | int k; 6 | int o; 7 | int p; 8 | int q; 9 | int ret; 10 | int a; 11 | a = 2; 12 | 13 | i = 0; 14 | while(i<1000000) 15 | { 16 | j = 0; 17 | while(j<2) 18 | { 19 | k = 0; 20 | while(k<2) 21 | { 22 | o = 0; 23 | while(o<2) 24 | { 25 | p = 0; 26 | while(p<2) 27 | { 28 | q = 0; 29 | while(q<2) 30 | { 31 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 32 | q=q+1; 33 | } 34 | p=p+1; 35 | } 36 | o=o+1; 37 | } 38 | k=k+1; 39 | } 40 | j=j+1; 41 | } 42 | i=i+1; 43 | } 44 | output(ret); 45 | return ; 46 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-7.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-8.cminus: -------------------------------------------------------------------------------- 1 | 2 | void main(void){ 3 | int i; 4 | int j; 5 | int k; 6 | int o; 7 | int p; 8 | int q; 9 | int ret; 10 | int a; 11 | int z; 12 | a = 2; 13 | 14 | i = 0; 15 | while(i<1000000) 16 | { 17 | j = 0; 18 | while(j<2) 19 | { 20 | k = 0; 21 | while(k<2) 22 | { 23 | o = 0; 24 | while(o<2) 25 | { 26 | p = 0; 27 | while(p<2) 28 | { 29 | q = 0; 30 | while(q<2) 31 | { 32 | if( a > 1 ) 33 | { 34 | z = 1; 35 | } 36 | ret = (a*a*a*a*a*a*a*a*a*a)/a/a/a/a/a/a/a/a/a/a; 37 | q=q+1; 38 | } 39 | p=p+1; 40 | } 41 | o=o+1; 42 | } 43 | k=k+1; 44 | } 45 | j=j+1; 46 | } 47 | i=i+1; 48 | } 49 | output(ret); 50 | return ; 51 | } -------------------------------------------------------------------------------- /tests/lab5/testcases/LoopInvHoist/testcase-8.out: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /tests/lab5/testcases/myactvar/testcase-5.cminus: -------------------------------------------------------------------------------- 1 | void main(void){ 2 | int a; 3 | int b; 4 | int c; 5 | int i; 6 | a = 1; 7 | b = 2; 8 | c = 3; 9 | i = 0; 10 | if(a 3 | int main(int argc, char **argv) 4 | { 5 | if (argc != 2) { 6 | std::cout << "usage: " << argv[0] << " " << std::endl; 7 | } else { 8 | auto s = parse(argv[1]); 9 | auto a = AST(s); 10 | auto printer = ASTPrinter(); 11 | a.run_visitor(printer); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tests/test_logging.cpp: -------------------------------------------------------------------------------- 1 | #include"logging.hpp" 2 | // 引入头文件 3 | int main(){ 4 | 5 | LOG(DEBUG) << "This is DEBUG log item."; 6 | // 使用关键字LOG,括号中填入要输出的日志等级 7 | // 紧接着就是<<以及日志的具体信息,这一步就跟使用std::cout一样 8 | LOG(INFO) << "This is INFO log item"; 9 | LOG(WARNING) << "This is WARNING log item"; 10 | LOG(ERROR) << "This is ERROR log item"; 11 | 12 | return 0; 13 | } --------------------------------------------------------------------------------