├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── Project 验收细则.md ├── README.md ├── documents ├── report.assets │ ├── NodeClassHierachy.png │ ├── course.png │ ├── if.png │ ├── if.png:Zone.Identifier │ ├── ifvisual.png │ ├── ifvisual.png:Zone.Identifier │ ├── multi.png │ ├── pipeline.png │ ├── quicksort.png │ ├── token1.png │ ├── token2.png │ ├── visual.png:Zone.Identifier │ ├── visualwhile.png │ ├── while.png │ └── while.png:Zone.Identifier ├── report.md ├── report.pdf ├── ~$设计展示.pptx └── 设计展示.pptx ├── ource_filename = main ├── src ├── Emit.cpp ├── Emit.h ├── main.cpp ├── makefile ├── parsing.y ├── token.l ├── treenode.cpp └── treenode.h ├── test ├── course │ ├── course │ ├── course.bc │ ├── course.c │ ├── course.ll │ ├── course.o │ ├── course.s │ └── linux-amd64 ├── multiplication │ ├── linux-amd64 │ ├── multi │ ├── multi.bc │ ├── multi.ll │ ├── multi.o │ ├── multi.s │ └── multiply.c └── quicksort │ ├── linux-amd64 │ ├── quicksort │ └── quicksort.c ├── visualization ├── AST.json ├── ast.html ├── ast.js └── sample.json └── wez_room ├── array.ll ├── char.ll ├── course.ll ├── easy ├── easy.bc ├── easy.ll ├── easy.o ├── easy.s ├── fun.c ├── function.ll ├── gets.ll ├── if.ll ├── ifelse.ll ├── multi ├── multi.bc ├── multi.ll ├── multi.o ├── multi.s ├── print ├── print.bc ├── print.ll ├── print.o ├── print.s ├── quicksort ├── quicksort.bc ├── quicksort.ll ├── quicksort.o ├── quicksort.s ├── quicksortnb.ll ├── scanf.ll ├── string.ll └── while.ll /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | .vscode/* 4 | tester/auto-advisor/darwin-amd64 5 | tester/auto-advisor/darwin-arm64 6 | tester/auto-advisor/linux-amd64 7 | tester/auto-advisor/windows-amd64.exe 8 | tester/matrix-multiplication/darwin-amd64 9 | tester/matrix-multiplication/darwin-arm64 10 | tester/matrix-multiplication/linux-amd64 11 | tester/matrix-multiplication/windows-amd64.exe 12 | tester/quicksort/darwin-amd64 13 | tester/quicksort/darwin-arm64 14 | tester/quicksort/linux-amd64 15 | tester/quicksort/windows-amd64.exe 16 | 17 | build/* 18 | .*/* 19 | 20 | *.exe 21 | 22 | src/token.cpp 23 | src/lex.yy.cc 24 | src/parsing.cpp 25 | src/parsing.hpp 26 | CMakeLists.txt 27 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(Compiler) 3 | 4 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 5 | 6 | set(CMAKE_CXX_STANDARD 14) 7 | 8 | find_package(LLVM REQUIRED CONFIG) 9 | 10 | if (APPLE) 11 | find_package(PkgConfig REQUIRED) 12 | pkg_check_modules(JSONCPP jsoncpp) 13 | else() 14 | find_package(jsoncpp REQUIRED) 15 | endif() 16 | 17 | include_directories(${LLVM_INCLUDE_DIRS}) 18 | add_definitions(${LLVM_DEFINITIONS}) 19 | add_executable(Compiler src/Emit.cpp src/Emit.h src/treenode.cpp src/treenode.h src/main.cpp src/parsing.cpp src/parsing.hpp src/lex.yy.cc) 20 | set(LLVM_LINK_COMPONENTS 21 | Core 22 | ExecutionEngine 23 | Interpreter 24 | MC 25 | Support 26 | nativecodegen 27 | mcjit 28 | asmparser 29 | x86asmparser 30 | ) 31 | llvm_map_components_to_libnames(llvm_libs ${LLVM_LINK_COMPONENTS}) 32 | target_link_libraries(Compiler ${llvm_libs} jsoncpp_lib) 33 | #explicit_llvm_config(Compiler core support engine interpreter executionengine mcjit orcjit) 34 | -------------------------------------------------------------------------------- /Project 验收细则.md: -------------------------------------------------------------------------------- 1 | # Project 验收细则 2 | 3 | Project 的具体要求分为代码风格、功能测试、课堂展示、实验报告四个部分,本文档主要说明代码风格和功能测试。 4 | 5 | 代码风格,指源代码的书写风格和组织管理,体现在命名,注释,缩进,布局,版本控制等。功能测试指是否能够完成指定任务,以及运行结果是否符合预期。 6 | 7 | 验收的大致流程是: 8 | 9 | 1. 检视代码风格; 10 | 2. 进行功能测试。 11 | 12 | 下面介绍具体规则。 13 | 14 | ## 1 代码风格 15 | 16 | 必须使用 `git` 进行版本控制。版本控制应当贯穿实验的整个开发过程,完成实验后再 `git init` 的、只有零星 `commit` 的不视为使用了版本控制。注意,任何 `commit` 中出现了抄袭的代码,均判定为抄袭。 17 | 18 | 代码风格的考察重点在于可读性。可读性是指其他人是否能够通过代码快速理解编码者的意图。有意义的命名、合理的模块设计、适当的注释、较强的一致性等可以增强可读性。一致性是指编码时采用的格式细节是否始终保持不变,强一致性会降低阅读的难度,增加可读性。建议自行搜索你所使用的语言的编码规范作为参考。 19 | 20 | ### 1.1 评价标准 21 | 22 | | 得分 | 标准 | 23 | | :---: | :--- | 24 | | D | 没有上交代码或者被判抄袭。 | 25 | | C | 没有参加验收,但上交了代码。 | 26 | | B | 没有使用 `git` 或者代码难以理解。 | 27 | | B+ | 使用了 `git` 且代码较为混乱。 | 28 | | A | 使用了 `git` 且代码基本可读。 | 29 | | A+ | 使用了 `git` 且代码风格良好。 | 30 | 31 | ## 2 功能测试 32 | 33 | ### 2.1 基本要求 34 | 35 | ``` mermaid 36 | graph LR 37 | A[源代码] -->|词法分析器| B[token 序列] -->|语法分析器| C[AST] 38 | A -->|语法分析器| C 39 | ``` 40 | 41 | 实现一个编译器,一般是从词法分析器和语法分析器开始。上图展示了可选的技术路径: 42 | 43 | 1. 使用词法分析器(手写的或工具生成的)将源代码转换成 token 序列,然后使用语法分析器(手写的或工具生成的)将 token 序列转换成 AST 。 44 | 2. 使用语法分析器(手写的或工具生成的)直接将源代码转换为 AST 。 45 | 46 | 手写一个词法分析器非常简单,但是手写语法分析器可能就困难一些。常用的分析器生成工具有 lex 、 yacc 等。某些工具有非常强大的功能,生成出来的分析器能够提供更多的错误信息或者具有更强的健壮性,你也可以选择它们。 47 | 48 | ``` mermaid 49 | graph LR 50 | A[AST] -->|IR 生成工具| B[IR] -->|IR 编译工具| C[机器码] 51 | B[IR] -->|IR 解释工具| D[直接执行 IR] 52 | ``` 53 | 54 | 从 AST 出发,选择就非常多样了。以下提供几种可能的思路: 55 | 56 | 1. 完全使用 LLVM 等框架,使用 LLVM 将 AST 转换为 IR ,然后使用 LLVM 将 IR 转换为机器码来运行。这种方法的缺点在于需要学习新的知识,查看一些文档(大部分是英文的);优点是掌握 LLVM 之后工作量大幅降低了。 57 | 2. 自己将 AST 转换为 LLVM 标准的 IR ,然后使用 LLVM 将 IR 转化为机器码或者使用 LLVM 执行 IR 。这种方法的优点是不需要看 LLVM 的文档了,有关 IR 的文档短得多;缺点在于自己写的生成器可能附带大量 bug 。 58 | 3. 自己将 AST 转换为 IR ,然后另外实现一个 IR 解释器来运行这个 IR 。这种方法的优点是不需要阅读任何文档,缺点是调试时间大幅增加。 59 | 60 | 以下是严禁使用的技术路线: 61 | 62 | ``` mermaid 63 | graph LR 64 | A[源代码] -->|解释器| B[直接执行源代码] 65 | A -->|编译器| C[C 等其他现存语言的代码] -->|现有编译器| D[可执行文件] 66 | ``` 67 | 68 | 本课程要求实现的必须是一个编译器,而不是解释器;另外,不允许将源代码编译成一种高级语言,然后调用已有的编译器来编译它。本课程要求生成中间代码,至于中间代码之后的部分不做要求。你可以将其转化为机器码,也可以写一个解释器来运行它(在不考虑性能的情况下,使用 Python 写一个 IR 解释器是很简单的)。 69 | 70 | 总而言之,你的编译器必须生成一个能以某种方式执行的“结果”,这个结果必须能在现代操作系统的常见架构上运行。 71 | 72 | ### 2.2 语法定义 73 | 74 | 可以自己设计一种新语言,也可以实现现有语言的子集,也可以混合你喜欢的多种语言。但是,设计出的语言必须是现代高级语言,不能体现出任何与底层硬件或者体系架构相关的设计,比如这个[反例](https://trove42.com/introducing-pikachu-programming-language/)以及相似的更多[晦涩编程语言](https://en.wikipedia.org/wiki/Esoteric_programming_language)。 75 | 76 | ### 2.3 测试点 77 | 78 | 需要使用设计出的语言解决以下问题。所有问题的输入中只会使用 ASCII 码表中的可打印字符,并且本文中的整数是指匹配正则表达式 `-?(0|[1-9][0-9]*)` 的字符串。 79 | 80 | #### 2.3.1 快速排序 81 | 82 | 以下是排序问题的定义: 83 | 84 | 输入:第一行是一个整数 $N$ ,满足 $0 \leqslant N \leqslant 10^4$ 。接下来是 $N$ 个整数,每个整数独占一行。整数 $x_i$ 满足 $-10^4 < x_i < 10^4$ 但不保证 $i \ne j$ 时 $x_i \ne x_j$ 。 85 | 86 | 输出:要求将输入的 $N$ 个整数按非降序排序。计算结束后,需要将 $N$ 个整数依次输出,每个数字独占一行。注意最后一个数字输出后必须换行。 87 | 88 | 要求必须使用[快速排序](https://en.wikipedia.org/wiki/Quicksort)算法解决该问题,并且必须使用递归形式的实现。 89 | 90 | ##### 例一 91 | 92 | 输入: 93 | 94 | ``` 95 | 0 96 | ``` 97 | 98 | 输出: 99 | 100 | ``` 101 | ``` 102 | 103 | ##### 例二 104 | 105 | 输入: 106 | 107 | ``` 108 | 4 109 | 3 110 | 2 111 | 1 112 | 1 113 | ``` 114 | 115 | 输出: 116 | 117 | ``` 118 | 1 119 | 1 120 | 2 121 | 3 122 | ``` 123 | 124 | ##### 例三 125 | 126 | 输入: 127 | 128 | ``` 129 | 3 130 | 1 131 | 0 132 | -1 133 | ``` 134 | 135 | 输出: 136 | 137 | ``` 138 | -1 139 | 0 140 | 1 141 | ``` 142 | 143 | #### 2.3.2 矩阵乘法 144 | 145 | 以下是矩阵乘法问题的定义: 146 | 147 | 输入:两个矩阵 $A$ 和 $B$ 。每个矩阵输入的第一行是单个空格隔开的两个整数 $M$ 和 $N$ ,满足 $1 \leqslant M, N \leqslant 25$ ,分别代表了矩阵的行数和列数。接下来 $M$ 行,每行是 $N$ 个整数,整数之间使用一个以上空格隔开,并满足整数的位数(负号算一位)加上空格的数量等于 $6$ 。 整数全部在区间 $(-1000, 1000)$ 中。不保证 $A$ 和 $B$ 之间可以进行乘法操作。 148 | 149 | 输出:如果 $A$ 和 $B$ 的维度不满足乘法的要求(即 $N_A \ne M_B$ ),则输出 `Incompatible Dimensions` ,该信息独占一行(输出后需要换行);否则,需要计算: 150 | 151 | $$ 152 | \begin{aligned} 153 | C & = 154 | \begin{bmatrix} 155 | c_{0, 0} & \dots & c_{0, N_C - 1} \\ 156 | c_{1, 0} & \dots & c_{1, N_C - 1} \\ 157 | \dots & \dots & \dots \\ 158 | c_{M_C - 1, 0} & \dots & c_{M_C - 1, N_C - 1} \\ 159 | \end{bmatrix} 160 | = A B \\ 161 | & = 162 | \begin{bmatrix} 163 | a_{0, 0} & \dots & a_{0, N_A - 1} \\ 164 | a_{1, 0} & \dots & a_{1, N_A - 1} \\ 165 | \dots & \dots & \dots \\ 166 | a_{M_A - 1, 0} & \dots & a_{M_A - 1, N_A - 1} \\ 167 | \end{bmatrix} 168 | \begin{bmatrix} 169 | b_{0, 0} & \dots & b_{0, N_B - 1} \\ 170 | b_{1, 0} & \dots & b_{1, N_B - 1} \\ 171 | \dots & \dots & \dots \\ 172 | b_{M_B - 1, 0} & \dots & b_{M_B - 1, N_B - 1} \\ 173 | \end{bmatrix} 174 | \end{aligned} 175 | $$ 176 | 177 | 其中: 178 | 179 | $$ 180 | c_{i, j} = \sum_{k = 0}^{N_A - 1} a_{i, k} b_{k, j} 181 | $$ 182 | 183 | 计算完成后需要输出结果。结果分为 $M_C$ 行输出,每一行有 $N_C$ 个整数,每个整数前有数个空格,并满足整数的位数(负号算一位)加上空格的数量等于 $10$ 。 184 | 185 | ##### 例一 186 | 187 | 输入: 188 | 189 | ``` 190 | 2 2 191 | 1 2 192 | 3 4 193 | 3 1 194 | 1 195 | 2 196 | 3 197 | ``` 198 | 199 | 输出: 200 | 201 | ``` 202 | Incompatible Dimensions 203 | ``` 204 | 205 | ##### 例二 206 | 207 | 输入: 208 | 209 | ``` 210 | 2 2 211 | 1 2 212 | 3 4 213 | 2 3 214 | 1 2 0 215 | 0 3 4 216 | ``` 217 | 218 | 输出: 219 | 220 | ``` 221 | 1 8 8 222 | 3 18 16 223 | ``` 224 | 225 | ##### 例三 226 | 227 | 输入: 228 | 229 | ``` 230 | 4 1 231 | 123 232 | 345 233 | 567 234 | 789 235 | 1 4 236 | -123 345 -567 789 237 | ``` 238 | 239 | 输出: 240 | 241 | ``` 242 | -15129 42435 -69741 97047 243 | -42435 119025 -195615 272205 244 | -69741 195615 -321489 447363 245 | -97047 272205 -447363 622521 246 | ``` 247 | 248 | #### 2.3.3 选课助手 249 | 250 | 选课助手问题的定义如下: 251 | 252 | 输入:输入是一个培养方案,每行一门课程,空行代表输入结束。每门课程是一个四元组 $\mathcal{C} = (\text{课程名称}, \text{学分}, \text{前置课程}, \text{成绩})$ 。每个字段可能包含任意长度的字符串,但不会有前置和后置的空白符。各个字段之间使用 `|` 隔开。 253 | 254 | - $\text{课程名称}$ 是不包含 `,` 、 `;` 和 `|` 的长度大于 0 小于 5 的字符串。 255 | - $\text{学分}$ 是非负整数,大于 0 小于 5 。 256 | - $\text{前置课程}$ 是修读本门课程时必须已经获得学分的课程。 `,` 相当于逻辑操作 `and` , `;` 相当于逻辑操作 `or` 。 `,` 的优先级高于 `;` 。 `A,B;C,D` 的含义是 `A` 并且 `B` , 或者 `C` 并且 `D` 。使用类似 `C` 语言的方式描述就是 `A && B || C && D` 。注意前置课程不一定存在于培养方案中。 257 | - $\text{成绩}$ 是能够匹配正则表达式 `[ABCDF]?` 的字符串。当本字段为空时,代表还未修读本课程;当成绩为 `F` 时,代表曾经修读但是挂科,没有获得学分;其他情况代表成功获得学分。 258 | 259 | 输出:需要计算以下内容。 260 | 261 | - GPA ,使用四分制,其中 `A` 等于 4 , `B` 等于 3 , `C` 等于 2 , `D` 等于 1 , `F` 等于 0 ,结果保留一位小数,格式匹配正则表达式 `[0-3]\.[0-9]|4\.0` 。计算公式为:$GPA = (\sum \text{成绩} \times \text{学分}) / \text{尝试学分}$ 。 262 | - 尝试学分,已经获得成绩的课程的总学分。包括获得 `F` 成绩的课程。 263 | - 已修学分,已经获得的总学分。成绩为 `F` 的课程没有获得学分。 264 | - 剩余学分,培养方案中还有多少学分没有修读,包括成绩为 `F` 的课程。 265 | - 推荐课程,满足前置课程条件,可以修读的课程但还没有获得学分的课程。必须按照课程出现在输入中的先后顺序进行输出。 266 | 267 | 提示点: 268 | 269 | 1. 如果前置课程不存在于该培养方案中,应该当作是未修读的课程。假如培养方案只有一条 `x|2|y|`,`y` 不在培养方案中,当作没有修读的课程,所以 `x` 不满足前置课程条件,不进行推荐。由于 `y` 的前置课程是未知的,所以也不需要推荐 `y` 。 270 | 2. 注意有空的培养方案,此时输入是一个换行。 271 | 3. 随机测试案例的生成过程大致如下:在 100 门课(课的名称是类似 `c2`、`c98` 的编号式命名)之间随机添加依赖关系,保证没有环路,保证每门课的前置课程不会超过 7 组(由 `;` 分隔的部分是一组),每一组中不会超过 7 门。 272 | 4. 不保证某课程所依赖的课程一定出现在它的前面。举个例子,可能先有 `x|2|y|` ,再有 `y|3||` 。 273 | 274 | 输出的具体细节必须严格按照以下示例中的格式。 275 | 276 | ##### 例一 277 | 278 | 输入: 279 | 280 | ``` 281 | c0|3|| 282 | c1|4|c0| 283 | c2|3|c0| 284 | c3|4|c1| 285 | c4|4|c2| 286 | c5|3|c3| 287 | c6|3|c2| 288 | ``` 289 | 290 | 输出: 291 | 292 | ``` 293 | GPA: 0.0 294 | Hours Attempted: 0 295 | Hours Completed: 0 296 | Credits Remaining: 24 297 | 298 | Possible Courses to Take Next 299 | c0 300 | ``` 301 | 302 | ##### 例二 303 | 304 | 假如某学生已经修读了培养方案中的所有课程,那么可能的输出是: 305 | 306 | ``` 307 | GPA: 3.5 308 | Hours Attempted: 129 309 | Hours Completed: 129 310 | Credits Remaining: 0 311 | 312 | Possible Courses to Take Next 313 | None - Congratulations! 314 | ``` 315 | 316 | 注意!只有在未修学分等于 0 时,才输出 `None - Congratulations!` 。如果未修学分不为 0 ,但是没有可以推荐的课程,那么应该输出: 317 | 318 | ``` 319 | GPA: 3.5 320 | Hours Attempted: 129 321 | Hours Completed: 129 322 | Credits Remaining: 8 323 | 324 | Possible Courses to Take Next 325 | ``` 326 | 327 | ##### 例三 328 | 329 | 输入: 330 | 331 | ``` 332 | c53|2||B 333 | c40|5|c28,c26,c9,c25,c23,c38,c17;c30,c6,c9,c10,c28,c18;c2,c8,c3;c38,c18,c27;c18,c23,c38,c20,c39,c0,c32|B 334 | c34|4|c17;c20,c16,c21,c9,c25;c3,c1,c23,c31,c2,c20|C 335 | c18|3|c7,c3,c2,c13,c0,c15,c1;c6,c10,c14;c13;c17,c8,c0,c5,c13;c11,c2,c12;c1,c8,c7,c6|F 336 | c33|4|c31,c4,c12,c15,c20,c28;c24,c28,c21,c12;c24;c25,c31,c22,c14;c13,c25,c23,c31,c15;c3,c2,c28,c5,c30,c16|C 337 | c47|5|c11,c0;c20,c11,c32;c22,c39,c34;c5,c42,c3|C 338 | c15|2||C 339 | c81|1|c33,c6,c34,c16,c10,c72;c1,c59,c18,c58,c11;c30,c26,c45,c40;c11,c67,c60,c74,c59|A 340 | c12|5|c10,c2,c3,c5,c1,c0,c7| 341 | c67|1|c50,c39,c51,c63,c40|C 342 | c80|4|c24,c43|B 343 | c87|1|c9,c42,c39,c57;c72,c27,c2,c42,c13,c63;c21,c57;c41| 344 | c0|5||B 345 | c49|2||A 346 | c85|5|c51,c70,c59,c69,c45,c29;c61,c53,c32,c13,c24,c18,c58;c8,c20,c22,c36;c8;c40,c42,c62,c43,c19| 347 | c98|4|c65,c3,c96,c33,c46|F 348 | c64|5|c7,c13,c51,c12;c44,c10;c44,c5,c57,c42,c53;c43,c44,c46,c29,c5,c21,c49;c56,c29,c44,c10,c55,c22,c9;c38,c39,c8,c16,c62,c28,c9|D 349 | c55|1||A 350 | c82|2|c32,c76,c26,c49,c44,c80|B 351 | c23|4|c9,c1;c18,c5,c12,c7,c16,c1,c3;c0,c9;c22,c17,c1;c22,c12,c10| 352 | c63|3|c40,c29;c36,c29,c47,c23,c48,c13,c32;c55,c39;c16,c45|D 353 | c89|1||A 354 | c39|1|c17,c26,c6,c9,c29,c3;c2,c28,c15,c38,c13,c29;c18,c1,c38,c33,c24;c31,c17,c22,c21|D 355 | c90|2|| 356 | c59|2|c1,c41,c5;c55,c44,c36,c39,c46,c4;c29,c52,c7,c40,c6,c26|D 357 | c25|4|c4,c2;c18,c7,c2,c6,c20,c9,c22;c3,c21,c8,c13,c9|D 358 | c75|3|c41,c45;c6,c4,c22,c3|D 359 | c66|2|c21,c56,c30,c62,c33,c38,c9;c57,c28,c16,c53,c46;c12,c11,c59,c0,c34;c0;c13,c6,c20,c64,c63;c18,c50,c59,c9,c53,c24|F 360 | c38|5|c5,c33,c29,c35,c31;c20,c9,c16,c30;c11,c29,c24,c2|C 361 | c2|1||A 362 | c74|2|c48,c4,c60,c42,c29,c1;c21;c54,c24,c45,c73,c49;c65,c4|F 363 | c48|3||C 364 | c65|2|c9,c47,c57,c6,c19| 365 | c21|3|c17,c7,c18,c19|C 366 | c27|4|c7,c8,c25,c2;c20,c3,c19,c8,c7;c22,c23,c0,c18,c9;c24,c13,c2;c7;c26,c22,c0,c13,c2,c15,c5|D 367 | c61|3|c6,c55,c25;c49,c29,c45,c15,c14;c3,c19,c0,c37,c26,c27,c53| 368 | c72|3||C 369 | c45|3|| 370 | c91|3|c37;c54,c34;c45,c57,c38,c83,c49,c71,c54;c3,c0,c77,c71,c1;c4,c56,c22,c40,c13,c57;c16|C 371 | c73|5|c42,c62,c22,c14,c9;c17,c35,c2|D 372 | c3|1||C 373 | c42|2|c29,c28,c34;c37,c26,c27,c8,c9;c24,c19,c31,c21,c8,c0,c32;c34,c25,c38|C 374 | c36|4|c28,c16,c15,c25,c33|A 375 | c96|2|c73,c38,c50;c26,c84,c5,c61,c12,c20;c44;c65,c36|B 376 | c88|5|c0,c35,c40,c9,c39;c80,c51,c14,c72,c77,c8,c57;c67,c38,c63,c31,c0|F 377 | c22|2|c16,c9,c4,c5;c6,c15,c19| 378 | c41|4|c21,c14,c32,c8,c1;c31,c5,c17,c36,c26;c38,c13,c31;c32,c13,c27,c0;c6,c36|D 379 | c35|5|c6,c29,c9,c32,c22;c23,c9,c22,c30,c17,c20,c19;c24,c19,c27,c10,c32,c26;c12,c30,c5;c26,c34,c27,c0;c21,c13,c26,c4,c29,c8| 380 | c46|2|c21,c4,c7,c34;c5;c35;c18,c17,c11;c18,c14,c35,c1,c25,c19| 381 | c92|1|c41,c52,c81,c66,c9,c0,c68|D 382 | c9|5|c1,c4,c6,c3;c0,c6,c1,c8;c7,c4,c1;c6,c3,c5;c8,c1,c6,c5,c0;c2,c4,c0,c5,c7,c1| 383 | c11|5||C 384 | c94|4||D 385 | c29|4|c16| 386 | c93|3|c11,c5,c16,c43,c49,c48;c36,c48,c45,c83,c27,c84;c60,c83,c17,c0,c48,c27,c59;c12,c56|B 387 | c57|2|c42,c44,c34;c41,c26,c3,c17;c43,c15,c33,c2,c39|A 388 | c70|3|c58,c59,c0,c49,c54;c43,c3,c39,c2,c11,c13;c53,c18,c50,c47|D 389 | c78|2|| 390 | c56|3|c31,c24,c43,c0,c9,c21,c12;c23,c16,c41,c26,c19,c45;c32,c11,c13,c28,c2,c44,c24;c15,c43,c27,c45,c38,c2;c22,c23,c13,c14;c48|C 391 | c58|2|c1,c13;c44,c16,c40,c51,c28|F 392 | c8|3|c6,c0,c3,c7,c1,c2;c3,c7,c1;c6,c1;c5,c2|A 393 | c7|4|c5,c6;c4,c3,c0,c5,c1,c2;c1,c5;c0,c3,c2,c5,c6;c5;c1| 394 | c5|2||D 395 | c28|3|c23,c4|A 396 | c68|1|c23,c36,c51;c13,c44,c60|F 397 | c13|5|c3,c7,c1,c12,c9,c6,c4;c7,c2;c10,c5,c0,c8|C 398 | c1|1||B 399 | c37|5|c30|B 400 | c71|2|c5,c40,c61,c60,c9,c37,c35;c32,c30,c4,c49,c0,c12;c18,c43,c47;c58,c17,c59,c56,c66,c69,c29| 401 | c6|5|| 402 | c77|1|c47,c3,c50;c49,c37,c34| 403 | c19|2|c2,c15,c4;c13,c10,c15,c14|A 404 | c32|2||F 405 | c26|1|c0,c25,c13,c15,c12,c9,c17|F 406 | c43|2|c11,c22,c34,c25;c41,c25,c0|F 407 | c44|1|c0,c31,c30,c26,c11,c7,c17;c7,c16|D 408 | c95|3|c73,c75,c51,c69,c21,c22;c68,c75,c55,c6;c52;c48,c32,c80,c57,c68,c5,c62;c68,c50,c93,c77,c40|A 409 | c20|2||D 410 | c97|5|c61,c67|F 411 | c16|3|c0,c13,c9,c8|D 412 | c62|4|c44,c14,c15,c27,c18,c47,c53;c13,c51,c33;c56,c54,c46,c16,c32;c17,c23,c27,c7,c1,c32;c61,c51,c56,c44,c40,c9;c20|B 413 | c86|2|c32,c10,c63,c26;c32,c11,c67,c85,c42,c58,c34;c27,c46;c31,c9,c24,c76,c79,c49,c39|F 414 | c52|1|c10;c43,c34,c26,c50,c12;c27,c28,c36,c38,c14;c6,c8,c49,c39| 415 | c31|1|c26,c23|C 416 | c17|4|c8,c4,c3,c10,c15;c0,c5,c10,c6;c5,c16,c2,c10,c7,c9|A 417 | c30|1||D 418 | c4|1||B 419 | c99|4|c96,c86,c41,c7,c70;c67,c9,c37,c40,c3,c65,c92;c34,c22,c57,c61,c37;c9,c6,c22,c1,c82,c81;c71| 420 | c84|5|c33,c1,c30,c74;c34;c69,c65,c15;c7;c43,c69,c0,c44;c55|B 421 | c79|4|c48,c65,c59;c16,c25,c66|F 422 | c51|3|c21,c17,c48,c39,c11,c9;c26,c21,c8,c36,c39;c28;c41,c50,c23,c42,c21,c8,c3;c9|D 423 | c76|1|c46,c66,c41,c26,c70,c69;c74,c69,c49,c59,c17,c68,c30;c17,c29,c3,c33,c25,c21,c65;c72,c75,c50,c0;c49,c20,c23,c13,c45,c63,c30|D 424 | c50|2|c34,c40,c6;c6,c23,c26,c34,c21,c36,c19;c27,c1,c49|B 425 | c83|2|c22,c36,c72;c15,c79,c36;c45,c6,c46,c72,c23,c31;c60,c31,c48,c56;c78,c38,c57,c54;c61,c15,c29,c46,c17|C 426 | c69|1|c14,c54;c1,c61,c0;c51,c22,c25,c49,c46,c13;c44,c20,c29,c67,c11;c32,c61,c42,c68,c67,c2,c62;c42,c31,c51,c24|D 427 | c10|1|c6,c9,c8;c1,c0,c6,c8,c3;c8,c7,c1,c3,c4,c6|A 428 | c14|5|c2,c6,c8,c4,c3,c9;c4,c6,c12,c5,c1;c11|D 429 | c24|3|c18,c9,c11,c7,c10,c8;c21;c18,c21,c2,c12;c21,c17,c12| 430 | c54|4|c8;c34;c28,c53,c24;c13,c30|C 431 | c60|2|c48,c52;c47,c34,c53;c0,c40,c55,c22;c42;c5,c19;c42,c23,c2,c6|B 432 | ``` 433 | 434 | 输出: 435 | 436 | ``` 437 | GPA: 1.9 438 | Hours Attempted: 220 439 | Hours Completed: 185 440 | Credits Remaining: 100 441 | 442 | Possible Courses to Take Next 443 | c18 444 | c87 445 | c85 446 | c90 447 | c66 448 | c74 449 | c45 450 | c88 451 | c46 452 | c29 453 | c78 454 | c58 455 | c7 456 | c68 457 | c6 458 | c77 459 | c32 460 | c43 461 | c52 462 | c24 463 | ``` 464 | 465 | #### 2.3.4 测试工具 466 | 467 | 以上三个问题通过位于 `tester` 文件夹中的测试工具进行测试,如果没有你的平台能够使用的测试工具,请及时联系助教。目前提供以下版本: 468 | 469 | | 操作系统 | 体系架构 | 470 | | :---: | :---: | 471 | | Linux | amd64 | 472 | | MacOS | amd64/arm64 | 473 | | Windows | amd64 | 474 | 475 | 假设你使用 Windows 操作系统和 amd64 架构的计算机,并且使用设计好的编译器将快速排序的源代码编译成了 `qs.exe` ,那么可以在命令行中通过如下命令使用测试工具: 476 | 477 | ``` 478 | ./windows-amd64.exe ./qs.exe 479 | ``` 480 | 481 | 前一个路径是测试工具的路径,后一个路径是编译好的可执行文件的路径。如果得到如下输出: 482 | 483 | ``` 484 | fixed case 0 (size 0)...pass! 485 | fixed case 1 (size 1)...pass! 486 | fixed case 2 (size 2)...pass! 487 | fixed case 3 (size 2)...pass! 488 | fixed case 4 (size 3)...pass! 489 | fixed case 5 (size 3)...pass! 490 | fixed case 6 (size 3)...pass! 491 | fixed case 7 (size 3)...pass! 492 | fixed case 8 (size 3)...pass! 493 | fixed case 9 (size 4)...pass! 494 | fixed case 10 (size 9)...pass! 495 | fixed case 11 (size 9)...pass! 496 | fixed case 12 (size 10000)...pass! 497 | fixed case 13 (size 10000)...pass! 498 | fixed case 14 (size 4096)...pass! 499 | randomly generated case 0 (size 10000)...pass! 500 | randomly generated case 1 (size 10000)...pass! 501 | randomly generated case 2 (size 10000)...pass! 502 | randomly generated case 3 (size 10000)...pass! 503 | randomly generated case 4 (size 10000)...pass! 504 | randomly generated case 5 (size 10000)...pass! 505 | randomly generated case 6 (size 10000)...pass! 506 | randomly generated case 7 (size 10000)...pass! 507 | randomly generated case 8 (size 10000)...pass! 508 | randomly generated case 9 (size 10000)...pass! 509 | ---------------------------------------- 510 | 2021-06-05 13:29:47.868 511 | ``` 512 | 513 | 那么说明通过了测试。注意测试工具中使用了随机生成的测试数据,因此多次测试的结果可能有所不同。建议多次测试保证能够成功通过测试,避免验收时抽奖。 514 | 515 | 如果你不能直接生成一个可执行文件,而是需要多个命令才能最终执行编译结果,那么你需要把所有命令写在一个脚本中,例如 `qs.sh` 或者 `qs.bat` (根据操作系统而不同),然后测试该脚本即可。 516 | 517 | ``` 518 | ./darwin-arm64 ./qs.sh 519 | ``` 520 | 521 | 使用 LLVM 时主函数必须返回 0 ,否则可能会出现如下错误: 522 | 523 | ``` 524 | panic: error happened while waiting for given program 525 | error message: 526 | exit status 1 527 | ... 528 | ``` 529 | 530 | ### 2.4 进阶主题 531 | 532 | 除了上述基本功能外,你还可以探索一些进阶主题。根据实现难度不同,可以获得一些额外奖励。 533 | 534 | - 复杂的数据类型,比如结构体、变长数组、字典等; 535 | - 错误恢复; 536 | - 宏展开; 537 | - 函数式; 538 | - 循环优化; 539 | - 面向对象; 540 | - …… 541 | 542 | ### 2.5 评价标准 543 | 544 | | 得分 | 标准 | 545 | | :---: | :--- | 546 | | D | 没有上交代码或者被判抄袭。 | 547 | | C | 没有参加验收,但上交了代码。 | 548 | | B | 参加了验收,但是没有通过任何测试点。 | 549 | | B+ | 通过某个测试点。 | 550 | | A | 通过两个测试点。 | 551 | | A+ | 通过全部测试点。 | 552 | 553 | 进阶主题可以使你在基础得分上获得一些额外奖励。换句话说,通过的测试点数量决定了得分档位(一个确定的区间),然后进阶主题可以使你更靠近这个区间的最大值。所以通过完成进阶主题获得分数的性价比是受到严格限制的。建议先完成测试点,有余力的同学可以试试进阶主题。 554 | 555 | #### 2.5.1 人数校正 556 | 557 | 1 人队伍:通过全部测试程序后,成功实现的进阶主题数量加一。 558 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MiniC-Compiler 2 | ZJU编译原理大作业 3 | 4 | 参考文献: 5 | https://gnuu.org/2009/09/18/writing-your-own-toy-compiler/ 6 | 7 | 编译流程 8 | ``` 9 | llvm-as-10 easy.ll 10 | llc-10 easy.bc 11 | clang-10 -c easy.s 12 | clang-10 easy.o -o easy 13 | ./easy 14 | ``` 15 | 16 | ``` c 17 | float fuck (int wez, float sb) { 18 | int wezsb = 3; 19 | while (wez < wezsb) { 20 | wez = wez + 1; 21 | } 22 | return 1.0; 23 | } 24 | int main(){ 25 | fuck(1, 2.0); 26 | return 0; 27 | } 28 | ``` 29 | 30 | ``` c 31 | int j; 32 | int main() { 33 | j = 10; 34 | while(j < 10) { 35 | int j; 36 | while(j < 20) { 37 | int j; 38 | j = 10; 39 | } 40 | j = 20; 41 | } 42 | return 0; 43 | } 44 | ``` 45 | 46 | -------------------------------------------------------------------------------- /documents/report.assets/NodeClassHierachy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/NodeClassHierachy.png -------------------------------------------------------------------------------- /documents/report.assets/course.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/course.png -------------------------------------------------------------------------------- /documents/report.assets/if.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/if.png -------------------------------------------------------------------------------- /documents/report.assets/if.png:Zone.Identifier: -------------------------------------------------------------------------------- 1 | [ZoneTransfer] 2 | LastWriterPackageFamilyName=Microsoft.ScreenSketch_8wekyb3d8bbwe 3 | ZoneId=3 4 | -------------------------------------------------------------------------------- /documents/report.assets/ifvisual.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/ifvisual.png -------------------------------------------------------------------------------- /documents/report.assets/ifvisual.png:Zone.Identifier: -------------------------------------------------------------------------------- 1 | [ZoneTransfer] 2 | LastWriterPackageFamilyName=Microsoft.ScreenSketch_8wekyb3d8bbwe 3 | ZoneId=3 4 | -------------------------------------------------------------------------------- /documents/report.assets/multi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/multi.png -------------------------------------------------------------------------------- /documents/report.assets/pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/pipeline.png -------------------------------------------------------------------------------- /documents/report.assets/quicksort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/quicksort.png -------------------------------------------------------------------------------- /documents/report.assets/token1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/token1.png -------------------------------------------------------------------------------- /documents/report.assets/token2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/token2.png -------------------------------------------------------------------------------- /documents/report.assets/visual.png:Zone.Identifier: -------------------------------------------------------------------------------- 1 | [ZoneTransfer] 2 | LastWriterPackageFamilyName=Microsoft.ScreenSketch_8wekyb3d8bbwe 3 | ZoneId=3 4 | -------------------------------------------------------------------------------- /documents/report.assets/visualwhile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/visualwhile.png -------------------------------------------------------------------------------- /documents/report.assets/while.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.assets/while.png -------------------------------------------------------------------------------- /documents/report.assets/while.png:Zone.Identifier: -------------------------------------------------------------------------------- 1 | [ZoneTransfer] 2 | LastWriterPackageFamilyName=Microsoft.ScreenSketch_8wekyb3d8bbwe 3 | ZoneId=3 4 | -------------------------------------------------------------------------------- /documents/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/report.pdf -------------------------------------------------------------------------------- /documents/~$设计展示.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/~$设计展示.pptx -------------------------------------------------------------------------------- /documents/设计展示.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/documents/设计展示.pptx -------------------------------------------------------------------------------- /src/Emit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "Emit.h" 12 | 13 | llvm::LLVMContext myContext; //定义全局context 14 | llvm::IRBuilder<> myBuilder(myContext); //定义全局IRbuilder 15 | 16 | map& EmitContext::getTop() { // 得到栈顶对应的当前域的符号表 17 | return symbolTable_stack.back()-> local_var; 18 | } 19 | map& EmitContext::getTopType(){ 20 | return symbolTable_stack.back()->local_var_type; 21 | } 22 | 23 | void EmitContext::pushBlock(){ 24 | symbolTable_stack.push_back(new symbolTable()); 25 | } 26 | 27 | void EmitContext::popBlock(){ 28 | symbolTable *tmp = symbolTable_stack.back(); 29 | symbolTable_stack.pop_back(); 30 | delete tmp; 31 | } 32 | 33 | llvm::Value* EmitContext::findVariable(string variableName) { 34 | 35 | // 首先按照作用域由近到远查找局部变量 36 | vector::reverse_iterator riter = symbolTable_stack.rbegin(); 37 | for (; riter != symbolTable_stack.rend(); ++riter) 38 | { 39 | symbolTable* curTable = *riter; 40 | if(curTable->local_var.find(variableName) != curTable->local_var.end()) 41 | return curTable->local_var[variableName]; 42 | } 43 | 44 | // 若没有局部变量, 则查找全局变量 45 | // 若没有返回 nullptr 46 | return this->myModule->getGlobalVariable(variableName, true); 47 | } 48 | 49 | llvm::Function* EmitContext::getPrintf(){ 50 | vector printf_arg_types; //printf内参数的类型 51 | printf_arg_types.push_back(myBuilder.getInt8PtrTy()); //8位代表void* 52 | 53 | llvm::FunctionType* printf_type = 54 | llvm::FunctionType::get(myBuilder.getInt32Ty(),printf_arg_types,true); 55 | llvm::Function* printf_func = 56 | llvm::Function::Create(printf_type,llvm::Function::ExternalLinkage,llvm::Twine("printf"),this->myModule); 57 | 58 | printf_func->setCallingConv(llvm::CallingConv::C); 59 | return printf_func; 60 | } 61 | 62 | llvm::Function* EmitContext::getScanf(){ 63 | llvm::FunctionType* scanf_type = 64 | llvm::FunctionType::get(myBuilder.getInt32Ty(),true); 65 | llvm::Function* scanf_func = 66 | llvm::Function::Create(scanf_type,llvm::Function::ExternalLinkage,llvm::Twine("scanf"),this->myModule); 67 | 68 | scanf_func->setCallingConv(llvm::CallingConv::C); 69 | return scanf_func; 70 | } 71 | 72 | llvm::Function* EmitContext::getGets(){ 73 | llvm::FunctionType* gets_type = 74 | llvm::FunctionType::get(myBuilder.getInt32Ty(),true); 75 | llvm::Function* gets_func = 76 | llvm::Function::Create(gets_type,llvm::Function::ExternalLinkage,llvm::Twine("gets"),this->myModule); 77 | 78 | gets_func->setCallingConv(llvm::CallingConv::C); 79 | return gets_func; 80 | } 81 | 82 | 83 | void EmitContext::Run(BlockNode* Root){ 84 | Root->emitter(*this); 85 | llvm::verifyModule(*this->myModule, &llvm::outs()); 86 | this->myModule->print(llvm::outs(), nullptr); 87 | } 88 | 89 | EmitContext::EmitContext(){ 90 | this->myModule = new llvm::Module("main",myContext); 91 | this->printf = getPrintf(); 92 | this->scanf = getScanf(); 93 | this->gets = getGets(); 94 | this->hasReturn = false; 95 | this->isArgs = false; 96 | } 97 | 98 | -------------------------------------------------------------------------------- /src/Emit.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "treenode.h" 27 | 28 | using namespace std; 29 | 30 | extern llvm::LLVMContext myContext; //定义全局context 31 | extern llvm::IRBuilder<> myBuilder; //定义全局IRbuilder 32 | 33 | class symbolTable{ 34 | public: 35 | map local_var; //局部变量map 36 | map local_var_type;//局部变量string-llvm::type的map 37 | }; 38 | 39 | 40 | class EmitContext{ 41 | public: 42 | vector symbolTable_stack; //符号栈 43 | 44 | public: 45 | llvm::Module *myModule; 46 | llvm::Function *printf,*scanf, *gets; 47 | llvm::Function* currentFunc; 48 | llvm::BasicBlock* returnBB; 49 | llvm::Value* returnVal; 50 | bool isArgs; 51 | bool hasReturn; 52 | 53 | EmitContext(); 54 | //~EmitContext(); 55 | 56 | map& getTop(); 57 | map& getTopType(); 58 | 59 | void pushBlock(); 60 | void popBlock(); 61 | 62 | llvm::Value* findVariable(string variableName); 63 | llvm::Type* findVariableType(string variableName); 64 | 65 | llvm::Function* getCurFunction(); 66 | void pushFunction(llvm::Function* func); 67 | void popFunction(); 68 | 69 | llvm::Function* getPrintf(); //得到llvm形式的printf函数 70 | llvm::Function* getScanf(); //得到llvm形式的scanf函数 71 | llvm::Function* getGets(); //得到llvm形式的gets函数 72 | void Run(BlockNode* Root); //对根节点进行run--------------------此处假定parser最开始的根节点为TreeNode 73 | 74 | }; 75 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "fstream" 2 | #include "Emit.h" 3 | #include "treenode.h" 4 | #include 5 | 6 | extern int yyparse(void); 7 | extern BlockNode* programBlock; 8 | 9 | int main(){ 10 | yyparse(); 11 | //BlockNode* Root; 12 | EmitContext* generator = new EmitContext(); 13 | 14 | llvm::InitializeNativeTarget(); 15 | llvm::InitializeNativeTargetAsmParser(); 16 | llvm::InitializeNativeTargetAsmPrinter(); 17 | 18 | cout<<"program begin"<generateJson(jsonString); 23 | ofstream fout; 24 | fout.open("../visualization/AST.json"); 25 | fout << jsonString; 26 | fout.close(); 27 | 28 | generator->Run(programBlock); 29 | 30 | return 0; 31 | 32 | } -------------------------------------------------------------------------------- /src/makefile: -------------------------------------------------------------------------------- 1 | love: 2 | flex -o lex.yy.cc token.l 3 | bison -d -o parsing.cpp parsing.y 4 | 5 | test: 6 | flex -o test.cpp test.l 7 | -------------------------------------------------------------------------------- /src/parsing.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "treenode.h" 3 | #include 4 | #include 5 | #define YYERROR_VERBOSE 1 6 | BlockNode *programBlock; // the root 7 | extern int yylex(); 8 | extern int yylineno; 9 | void yyerror(const char *s) { std::printf("Error: %s", s); } 10 | void printError(int lineno, string s) { std::printf(" at: line: %d\n", lineno); } 11 | %} 12 | 13 | %error-verbose 14 | 15 | %union { 16 | TreeNode *node; 17 | BlockNode *block; 18 | ExpressionNode *expression; 19 | StatementNode *statement; 20 | IdentifierNode *identifier; 21 | VariableDeclarationNode *variableDeclaration; 22 | std::vector *varibleDeclarationList; 23 | std::vector *expressionList; 24 | std::string *string; 25 | int token; 26 | } 27 | 28 | %token IDENTIFIER 29 | %token CONSTANT_INT CONSTANT_FLOAT CONSTANT_CHAR CONSTANT_STRING// 1, 1.0, 'a' "abc" 30 | %token PLUS MINUS MUL DIV // +, -, *, / 31 | %token AND OR // &&, || 32 | %token NOT GAD // !, & 33 | %token EQU NEQ LESST GREATERT LEQ GEQ // ==, !=, <, >, <=, >= 34 | %token RETURN 35 | %token IF ELSE WHILE BREAK 36 | 37 | 38 | %left OR 39 | %left AND 40 | %left EQU NEQ LESST GREATERT LEQ GEQ 41 | %left PLUS MINUS 42 | %left MUL DIV 43 | %right '!' 44 | %left '(' ')' '[' ']' 45 | 46 | %type identifier 47 | %type const_value expression 48 | %type statement var_decl func_decl 49 | %type func_decl_args 50 | %type call_args 51 | %type program statements block 52 | /* %type comparison */ 53 | 54 | %start program 55 | 56 | %% 57 | 58 | program: 59 | statements { 60 | programBlock = $1; 61 | }; 62 | 63 | statements: 64 | statement { 65 | $$ = new BlockNode(yylineno); 66 | $$->statementList.push_back($1); 67 | } 68 | | statements statement { 69 | $1->statementList.push_back($2); 70 | }; 71 | 72 | statement: 73 | var_decl ';' 74 | | func_decl 75 | | expression ';' { 76 | $$ = new ExpressionStatementNode(*$1, yylineno); 77 | } 78 | | RETURN ';' { 79 | $$ = new ReturnVoidNode(yylineno); 80 | } 81 | | RETURN expression ';' { 82 | $$ = new ReturnStatementNode(*$2, yylineno); 83 | } 84 | | BREAK ';' { 85 | $$ = new BreakStatementNode(yylineno); 86 | } 87 | | IF '(' expression ')' block { 88 | $$ = new IfStatementNode(*$3, *$5, yylineno); 89 | } 90 | | IF '(' expression ')' block ELSE block { 91 | $$ = new IfElseStatementNode(*$3, *$5, *$7, yylineno); 92 | } 93 | | IF error ELSE block { 94 | printError(yylineno, ""); 95 | } 96 | | WHILE '(' expression ')' block { 97 | $$ = new WhileStatementNode(*$3, *$5, yylineno); 98 | } 99 | | WHILE error ')' { 100 | printError(yylineno, ""); 101 | } 102 | | WHILE error ']' { 103 | printError(yylineno, ""); 104 | } 105 | | error ';' { 106 | printError(yylineno, ""); 107 | } 108 | ; 109 | 110 | block: 111 | '{' statements '}' { 112 | $$ = $2; 113 | } 114 | | '{' '}' { 115 | $$ = new BlockNode(yylineno); 116 | }; 117 | 118 | var_decl: 119 | identifier identifier { 120 | $$ = new VariableDeclarationNode(*$1, *$2, yylineno); 121 | } 122 | | identifier identifier '=' expression { 123 | $$ = new VariableDeclarationNode(*$1, *$2, $4, yylineno); 124 | } 125 | | identifier identifier '[' CONSTANT_INT ']' { // array 126 | $$ = new VariableDeclarationNode(*$1, *$2, atoi($4->c_str()), yylineno); 127 | } 128 | | identifier identifier '[' error ']' { 129 | printError(yylineno, ""); 130 | } 131 | | identifier error { 132 | printError(yylineno, ""); 133 | } 134 | | error identifier { 135 | printError(yylineno, ""); 136 | } 137 | ; 138 | 139 | func_decl: 140 | identifier identifier '(' func_decl_args ')' block { 141 | $$ = new FunctionDeclarationNode(*$1, *$2, *$4, *$6, yylineno); // TODO: delete $4 ? 142 | } 143 | | identifier identifier error ')' { 144 | printError(yylineno, ""); 145 | } 146 | ; 147 | 148 | func_decl_args: 149 | { 150 | $$ = new std::vector(); 151 | } 152 | | var_decl { 153 | $$ = new std::vector(); 154 | $$->push_back($1); 155 | } 156 | | func_decl_args ',' var_decl { 157 | $1->push_back($3); 158 | }; 159 | 160 | identifier: 161 | IDENTIFIER { 162 | $$ = new IdentifierNode(*$1, yylineno); 163 | }; 164 | 165 | const_value: 166 | CONSTANT_INT { 167 | $$ = new IntNode(atoi($1->c_str()), yylineno); 168 | } 169 | | CONSTANT_FLOAT { 170 | $$ = new FloatNode(atof($1->c_str()), yylineno); 171 | } 172 | | CONSTANT_CHAR { 173 | $$ = new CharNode(*$1, yylineno); 174 | } 175 | | CONSTANT_STRING { 176 | $$ = new StringNode(*$1, yylineno); 177 | }; 178 | 179 | call_args: 180 | { 181 | $$ = new std::vector(); 182 | } 183 | | expression { 184 | $$ = new std::vector(); 185 | $$->push_back($1); 186 | } 187 | | call_args ',' expression { 188 | $1->push_back($3); 189 | }; 190 | 191 | expression: 192 | identifier '=' expression { 193 | $$ = new AssignmentNode(*$1, *$3, yylineno); 194 | } 195 | | identifier '(' call_args ')' { 196 | $$ = new FunctionCallNode(*$1, *$3, yylineno); 197 | } 198 | | identifier { 199 | $$ = $1; 200 | } 201 | | GAD identifier { 202 | $$ = new getAddrNode(*$2, yylineno); 203 | } 204 | | GAD identifier '[' expression ']' { 205 | $$ = new getArrayAddrNode(*$2, *$4, yylineno); 206 | } 207 | | expression MUL expression { 208 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 209 | } 210 | | expression DIV expression { 211 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 212 | } 213 | | expression PLUS expression { 214 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 215 | } 216 | | expression MINUS expression { 217 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 218 | } 219 | | expression AND expression { 220 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 221 | } 222 | | expression OR expression { 223 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 224 | } 225 | | expression LESST expression { 226 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 227 | } 228 | | expression GREATERT expression { 229 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 230 | } 231 | | expression EQU expression { 232 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 233 | } 234 | | expression NEQ expression { 235 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 236 | } 237 | | expression LEQ expression { 238 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 239 | } 240 | | expression GEQ expression { 241 | $$ = new BinaryOpNode($2, *$1, *$3, yylineno); 242 | } 243 | | '(' expression ')' { 244 | $$ = $2; 245 | } 246 | | identifier '[' expression ']' { // array element access 247 | $$ = new ArrayElementNode(*$1, *$3, yylineno); 248 | } 249 | | identifier '[' expression ']' '=' expression { // array element access 250 | $$ = new ArrayElementAssignNode(*$1, *$3, *$6, yylineno); 251 | } 252 | | const_value 253 | | expression '=' error { 254 | printError(yylineno, "wrong assign expression"); 255 | } 256 | | expression AND error { 257 | printError(yylineno, ""); 258 | } 259 | | expression OR error { 260 | printError(yylineno, ""); 261 | } 262 | | expression PLUS error { 263 | printError(yylineno, ""); 264 | } 265 | | expression MINUS error { 266 | printError(yylineno, ""); 267 | } 268 | | expression MUL error { 269 | printError(yylineno, ""); 270 | } 271 | | expression DIV error { 272 | printError(yylineno, ""); 273 | } 274 | | expression LESST error { 275 | printError(yylineno, ""); 276 | } 277 | | expression GREATERT error { 278 | printError(yylineno, ""); 279 | } 280 | | expression EQU error { 281 | printError(yylineno, ""); 282 | } 283 | | expression NEQ error { 284 | printError(yylineno, ""); 285 | } 286 | | expression LEQ error { 287 | printError(yylineno, ""); 288 | } 289 | | expression GEQ error { 290 | printError(yylineno, ""); 291 | } 292 | expression '[' error ']' { 293 | printError(yylineno, ""); 294 | } 295 | ; 296 | %% -------------------------------------------------------------------------------- /src/token.l: -------------------------------------------------------------------------------- 1 | D [0-9] 2 | L [a-zA-Z_] 3 | H [a-fA-F0-9] 4 | E ([Ee][+-]?{D}+) 5 | 6 | %{ 7 | #include 8 | #include 9 | #include 10 | #include "treenode.h" 11 | #include "parsing.hpp" 12 | 13 | #define SAVE_TOKEN yylval.string = new std::string(yytext, yyleng) 14 | #define TOKEN(t) (yylval.token = t) 15 | 16 | int column = 0; 17 | 18 | void count(void); 19 | void comment(void); 20 | %} 21 | 22 | %option yylineno 23 | 24 | %% 25 | /* match two types of comments */ 26 | "/*" { comment(); } 27 | "//"[^\n]* { /* consume //-comment */ } 28 | 29 | /* match all the necessary keywords */ 30 | "break" { count(); return TOKEN(BREAK); } 31 | "else" { count(); return TOKEN(ELSE); } 32 | "if" { count(); return TOKEN(IF); } 33 | "return" { count(); return TOKEN(RETURN); } 34 | "while" { count(); return TOKEN(WHILE); } 35 | 36 | /* identifiers */ 37 | {L}({L}|{D})* { count(); SAVE_TOKEN; return IDENTIFIER;} 38 | 39 | /* constants */ 40 | 0[xX]{H}+ { count(); SAVE_TOKEN; return CONSTANT_INT; /* hexadecimal */ } 41 | 0[0-7]* { count(); SAVE_TOKEN; return CONSTANT_INT; /* octal */ } 42 | [1-9]{D}* { count(); SAVE_TOKEN; return CONSTANT_INT; /* decimal */ } 43 | 44 | \'.\'|\'\\.\' { count(); SAVE_TOKEN; return CONSTANT_CHAR; /* character */ } 45 | \"(\\.|[^"\\])*\" { count(); SAVE_TOKEN; return CONSTANT_STRING; /* string */ } 46 | 47 | 48 | {D}+{E} { count(); SAVE_TOKEN; return CONSTANT_FLOAT; } 49 | {D}*"."{D}+{E}? { count(); SAVE_TOKEN; return CONSTANT_FLOAT; } 50 | {D}+"."{D}*{E}? { count(); SAVE_TOKEN; return CONSTANT_FLOAT; } 51 | 52 | 53 | /* operators */ 54 | "&&" { count(); return TOKEN(AND); } 55 | "||" { count(); return TOKEN(OR); } 56 | "<=" { count(); return TOKEN(LEQ); } 57 | ">=" { count(); return TOKEN(GEQ); } 58 | "==" { count(); return TOKEN(EQU); } 59 | "!=" { count(); return TOKEN(NEQ); } 60 | ";" { count(); return TOKEN(';'); } 61 | "{" { count(); return TOKEN('{'); } 62 | "}" { count(); return TOKEN('}'); } 63 | "," { count(); return TOKEN(','); } 64 | ":" { count(); return TOKEN(':'); } 65 | "=" { count(); return TOKEN('='); } 66 | "(" { count(); return TOKEN('('); } 67 | ")" { count(); return TOKEN(')'); } 68 | "[" { count(); return TOKEN('['); } 69 | "]" { count(); return TOKEN(']'); } 70 | "." { count(); return TOKEN('.'); } 71 | "&" { count(); return TOKEN(GAD); } 72 | "!" { count(); return TOKEN(NOT); } 73 | "~" { count(); return TOKEN('~'); } 74 | "-" { count(); return TOKEN(MINUS); } 75 | "+" { count(); return TOKEN(PLUS); } 76 | "*" { count(); return TOKEN(MUL); } 77 | "/" { count(); return TOKEN(DIV); } 78 | "%" { count(); return TOKEN('%'); } 79 | "<" { count(); return TOKEN(LESST); } 80 | ">" { count(); return TOKEN(GREATERT); } 81 | "^" { count(); return TOKEN('^'); } 82 | "|" { count(); return TOKEN('|'); } 83 | "?" { count(); return TOKEN('?'); } 84 | 85 | [ \t\v\n\f] { count(); } 86 | . { printf("unknown token : %s in line: %d\n", yytext, yylineno); } 87 | 88 | %% 89 | 90 | int yywrap(void) 91 | { 92 | return 1; 93 | } 94 | 95 | 96 | void comment(void) 97 | { 98 | column = 0; 99 | char c, prev = 0; 100 | 101 | while (std::cin >> c) /* (EOF maps to 0) */ 102 | { 103 | if (c == '/' && prev == '*') 104 | return; 105 | prev = c; 106 | } 107 | printf("unterminated comment"); 108 | } 109 | 110 | void count(void) 111 | { 112 | int i; 113 | 114 | for (i = 0; yytext[i] != '\0'; i++) 115 | if (yytext[i] == '\n') 116 | column = 0; 117 | else if (yytext[i] == '\t') 118 | column += 4 - (column % 4); 119 | else 120 | column++; 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/treenode.h: -------------------------------------------------------------------------------- 1 | #ifndef _TREENODE_H_ 2 | 3 | #define _TREENODE_H_ 4 | 5 | #include "llvm/ADT/STLExtras.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | class EmitContext; 17 | 18 | class TreeNode { 19 | public: 20 | TreeNode(int lineNo) : lineNo(lineNo) {} 21 | virtual ~TreeNode() {} 22 | virtual llvm::Value *emitter(EmitContext &emitContext) { 23 | return nullptr; 24 | } 25 | virtual void generateJson(string &s) {} 26 | public: 27 | int lineNo; 28 | }; 29 | 30 | class ExpressionNode : public TreeNode { 31 | public: 32 | ExpressionNode(int lineNo) : TreeNode(lineNo) {} 33 | virtual llvm::Value *getAddr(EmitContext &emitContext) { 34 | return nullptr; 35 | } 36 | //virtual llvm::Value *emitter(EmitContext &emitContext); 37 | }; 38 | 39 | class StatementNode : public TreeNode { 40 | public: 41 | StatementNode(int lineNo) : TreeNode(lineNo) {} 42 | //virtual llvm::Value *emitter(EmitContext &emitContext); 43 | }; 44 | 45 | class IntNode : public ExpressionNode { 46 | public: 47 | IntNode(int value, int lineNo) : ExpressionNode(lineNo), value(value) {} 48 | llvm::Value *emitter(EmitContext &emitContext); 49 | void generateJson(string &s); 50 | public: 51 | int value; 52 | }; 53 | 54 | class FloatNode : public ExpressionNode { 55 | public: 56 | FloatNode(float value, int lineNo) : ExpressionNode(lineNo), value(value) {} 57 | llvm::Value *emitter(EmitContext &emitContext); 58 | void generateJson(string &s); 59 | public: 60 | float value; 61 | }; 62 | 63 | class CharNode : public ExpressionNode { 64 | public: 65 | CharNode(string &value, int lineNo) : ExpressionNode(lineNo), value(value) {} 66 | llvm::Value *emitter(EmitContext &emitContext); 67 | void generateJson(string &s); 68 | public: 69 | string &value; 70 | }; 71 | 72 | class StringNode : public ExpressionNode { 73 | public: 74 | StringNode(string &value, int lineNo) : ExpressionNode(lineNo), value(value) {} 75 | llvm::Value *emitter(EmitContext &emitContext); 76 | llvm::Value *getAddr(EmitContext &emitContext); 77 | void generateJson(string &s); 78 | public: 79 | string &value; 80 | }; 81 | 82 | class IdentifierNode : public ExpressionNode { 83 | public: 84 | IdentifierNode(string &name, int lineNo) : ExpressionNode(lineNo), name(name) {} 85 | llvm::Value *emitter(EmitContext &emitContext); 86 | llvm::Value *getAddr(EmitContext &emitContext); 87 | void generateJson(string &s); 88 | public: 89 | string name; 90 | }; 91 | 92 | class ArrayElementNode : public ExpressionNode { //identifier[expression] 表示数组中某个元素 93 | public: 94 | ArrayElementNode(IdentifierNode& identifier, ExpressionNode &index, int lineNo) : ExpressionNode(lineNo), identifier(identifier), index(index) {} 95 | llvm::Value *emitter(EmitContext &emitContext); 96 | llvm::Value *getAddr(EmitContext &emitContext); 97 | void generateJson(string &s); 98 | public: 99 | IdentifierNode& identifier; 100 | ExpressionNode &index; 101 | }; 102 | 103 | class ArrayElementAssignNode : public ExpressionNode { //identifier[expression] 表示数组中某个元素 104 | public: 105 | ArrayElementAssignNode(IdentifierNode &identifier, ExpressionNode &index, ExpressionNode &rhs, int lineNo) : ExpressionNode(lineNo), identifier(identifier), index(index), rhs(rhs) {} 106 | llvm::Value *emitter(EmitContext &emitContext); 107 | void generateJson(string &s); 108 | public: 109 | IdentifierNode& identifier; 110 | ExpressionNode &index; 111 | ExpressionNode &rhs; 112 | }; 113 | 114 | class FunctionCallNode : public ExpressionNode { 115 | public: 116 | FunctionCallNode(IdentifierNode &identifier, vector &args, int lineNo) : ExpressionNode(lineNo), identifier(identifier), args(args) {} 117 | FunctionCallNode(IdentifierNode &identifier, int lineNo) : ExpressionNode(lineNo), identifier(identifier) {} 118 | llvm::Value *emitter(EmitContext &emitContext); 119 | void generateJson(string &s); 120 | public: 121 | IdentifierNode& identifier; 122 | vector args; 123 | }; 124 | 125 | class BinaryOpNode : public ExpressionNode { //增加关系型二元运算 126 | public: 127 | BinaryOpNode(int op, ExpressionNode &lhs, ExpressionNode &rhs, int lineNo) : ExpressionNode(lineNo), op(op), lhs(lhs), rhs(rhs) {} 128 | llvm::Value *emitter(EmitContext &emitContext); 129 | void generateJson(string &s); 130 | public: 131 | int op; 132 | ExpressionNode& lhs; 133 | ExpressionNode& rhs; 134 | }; 135 | 136 | class getAddrNode : public ExpressionNode { //取地址运算符 137 | public: 138 | getAddrNode(IdentifierNode &rhs, int lineNo) : ExpressionNode(lineNo), rhs(rhs) {} 139 | llvm::Value *emitter(EmitContext &emitContext); 140 | void generateJson(string &s); 141 | public: 142 | IdentifierNode& rhs; 143 | }; 144 | 145 | class getArrayAddrNode : public ExpressionNode { //取地址运算符 146 | public: 147 | getArrayAddrNode(IdentifierNode &rhs, ExpressionNode &index, int lineNo) : ExpressionNode(lineNo), index(index), rhs(rhs) {} 148 | llvm::Value *emitter(EmitContext &emitContext); 149 | void generateJson(string &s); 150 | public: 151 | ExpressionNode& index; 152 | IdentifierNode& rhs; 153 | }; 154 | 155 | class AssignmentNode : public ExpressionNode { 156 | public: 157 | AssignmentNode(IdentifierNode &lhs, ExpressionNode &rhs, int lineNo) : ExpressionNode(lineNo), lhs(lhs), rhs(rhs) {} 158 | llvm::Value *emitter(EmitContext &emitContext); 159 | void generateJson(string &s); 160 | public: 161 | IdentifierNode& lhs; 162 | ExpressionNode& rhs; 163 | }; 164 | 165 | class BlockNode : public ExpressionNode { 166 | public: 167 | BlockNode(int lineNo) : ExpressionNode(lineNo) {} 168 | BlockNode(vector statementList, int lineNo) : ExpressionNode(lineNo), statementList(statementList) {} 169 | llvm::Value *emitter(EmitContext &emitContext); 170 | void generateJson(string &s); 171 | public: 172 | vector statementList; 173 | }; 174 | 175 | class ExpressionStatementNode : public StatementNode { //增加语句 176 | public: 177 | ExpressionNode &expression; 178 | ExpressionStatementNode(ExpressionNode& expression, int lineNo) : StatementNode(lineNo), expression(expression) {} 179 | virtual llvm::Value* emitter(EmitContext &emitContext); 180 | void generateJson(string &s); 181 | }; 182 | 183 | class BreakStatementNode : public StatementNode { 184 | public: 185 | BreakStatementNode(int lineNo) : StatementNode(lineNo) {} 186 | virtual llvm::Value* emitter(EmitContext &emitContext); 187 | void generateJson(string &s); 188 | }; 189 | 190 | class IfElseStatementNode : public StatementNode { 191 | public: 192 | IfElseStatementNode(ExpressionNode &expression, BlockNode &ifBlock, BlockNode &elseBlock, int lineNo) 193 | : StatementNode(lineNo), expression(expression), ifBlock(ifBlock), elseBlock(elseBlock) {} 194 | virtual llvm::Value* emitter(EmitContext &emitContext); 195 | void generateJson(string &s); 196 | public: 197 | ExpressionNode &expression; 198 | BlockNode &ifBlock; 199 | BlockNode &elseBlock; 200 | }; 201 | 202 | class IfStatementNode : public StatementNode { 203 | public: 204 | IfStatementNode(ExpressionNode &expression, BlockNode &ifBlock, int lineNo) 205 | : StatementNode(lineNo), expression(expression), ifBlock(ifBlock) {} 206 | virtual llvm::Value* emitter(EmitContext &emitContext); 207 | void generateJson(string &s); 208 | public: 209 | ExpressionNode &expression; 210 | BlockNode &ifBlock; 211 | }; 212 | 213 | class WhileStatementNode : public StatementNode { 214 | public: 215 | WhileStatementNode(ExpressionNode &expression, BlockNode &block, int lineNo) 216 | : StatementNode(lineNo), expression(expression), block(block) {} 217 | virtual llvm::Value* emitter(EmitContext &emitContext); 218 | void generateJson(string &s); 219 | public: 220 | ExpressionNode &expression; 221 | BlockNode █ 222 | }; 223 | 224 | class ReturnStatementNode : public StatementNode { 225 | public: 226 | ReturnStatementNode(ExpressionNode &expression, int lineNo) : StatementNode(lineNo), expression(expression) {} 227 | virtual llvm::Value* emitter(EmitContext &emitContext); 228 | void generateJson(string &s); 229 | public: 230 | ExpressionNode &expression; 231 | }; 232 | 233 | class ReturnVoidNode : public StatementNode { 234 | public: 235 | ReturnVoidNode(int lineNo) : StatementNode(lineNo) {} 236 | virtual llvm::Value* emitter(EmitContext &emitContext); 237 | void generateJson(string &s); 238 | public: 239 | }; 240 | 241 | class VariableDeclarationNode : public StatementNode { //增加数组的声明 242 | public: 243 | VariableDeclarationNode(IdentifierNode &type, IdentifierNode &identifier, int lineNo) : StatementNode(lineNo), type(type), identifier(identifier), size(0), assignmentExpression(nullptr) {} 244 | 245 | VariableDeclarationNode(IdentifierNode &type, IdentifierNode &identifier, 246 | int size, int lineNo) : 247 | StatementNode(lineNo), type(type), identifier(identifier), size(size), assignmentExpression(nullptr) {} 248 | 249 | VariableDeclarationNode(IdentifierNode& type, IdentifierNode& identifier, 250 | ExpressionNode *assignmentExpression, int lineNo) : 251 | StatementNode(lineNo), type(type), identifier(identifier), assignmentExpression(assignmentExpression) { 252 | } 253 | 254 | virtual llvm::Value* emitter(EmitContext &emitContext); 255 | void generateJson(string &s); 256 | public: 257 | int size; // size != 0 means this is an array 258 | IdentifierNode &type; 259 | IdentifierNode &identifier; 260 | ExpressionNode *assignmentExpression; 261 | }; 262 | 263 | class FunctionDeclarationNode : public StatementNode { 264 | public: 265 | FunctionDeclarationNode(IdentifierNode &type, IdentifierNode &identifier, 266 | vector args, BlockNode& block, int lineNo) : StatementNode(lineNo), type(type), identifier(identifier), args(args), block(block) {} 267 | virtual llvm::Value* emitter(EmitContext &emitContext); 268 | void generateJson(string &s); 269 | public: 270 | IdentifierNode &type; 271 | IdentifierNode &identifier; 272 | BlockNode █ 273 | vector args; 274 | }; 275 | 276 | 277 | #endif // _TREE_H_ -------------------------------------------------------------------------------- /test/course/course: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/course/course -------------------------------------------------------------------------------- /test/course/course.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/course/course.bc -------------------------------------------------------------------------------- /test/course/course.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char course_info[1000000]; 4 | char pre_course[1000000]; 5 | int pass_course[11100]; 6 | int course_id[11100]; 7 | 8 | int main() { 9 | int total_credit = 0; 10 | float total_gpa = 0; 11 | int pass_credit = 0; 12 | int try_credit = 0; 13 | int course_num = 0; 14 | while(1) { 15 | //scanf("%s", &course_info[course_num * 1000]); 16 | gets(&course_info[course_num * 1000]); 17 | if(course_info[course_num * 1000] == '\0') { 18 | break; 19 | } 20 | course_num = course_num + 1; 21 | } 22 | 23 | int course = 0; 24 | while(course < course_num) { 25 | int idx = course * 1000; 26 | int preidx = course * 1000; 27 | while(course_info[idx] != '|') { 28 | if(course_info[idx] != 'c') { 29 | course_id[course] = course_id[course] * 10 + course_info[idx] - '0'; 30 | } 31 | idx = idx + 1; 32 | } 33 | idx = idx + 1; 34 | int credit = course_info[idx] - '0'; 35 | total_credit = total_credit + credit; 36 | idx = idx + 1; 37 | idx = idx + 1; 38 | while(course_info[idx] != '|') { 39 | pre_course[preidx] = course_info[idx]; 40 | preidx = preidx + 1; 41 | idx = idx + 1; 42 | } 43 | pre_course[preidx] = '\0'; 44 | char grade = course_info[idx + 1]; 45 | if(grade == 'A') { 46 | total_gpa = total_gpa + credit * 4; 47 | try_credit = try_credit + credit; 48 | pass_credit = pass_credit + credit; 49 | pass_course[course_id[course]] = 1; 50 | } 51 | if(grade == 'B') { 52 | total_gpa = total_gpa + credit * 3; 53 | try_credit = try_credit + credit; 54 | pass_credit = pass_credit + credit; 55 | pass_course[course_id[course]] = 1; 56 | } 57 | if(grade == 'C') { 58 | total_gpa = total_gpa + credit * 2; 59 | try_credit = try_credit + credit; 60 | pass_credit = pass_credit + credit; 61 | pass_course[course_id[course]] = 1; 62 | } 63 | if(grade == 'D') { 64 | total_gpa = total_gpa + credit; 65 | try_credit = try_credit + credit; 66 | pass_credit = pass_credit + credit; 67 | pass_course[course_id[course]] = 1; 68 | } 69 | if(grade == 'F') { 70 | try_credit = try_credit + credit; 71 | } 72 | course = course + 1; 73 | } 74 | 75 | if(try_credit != 0) { 76 | total_gpa = total_gpa / try_credit; 77 | } 78 | 79 | printf("GPA: %.1f\n", total_gpa); 80 | printf("Hours Attempted: %d\n", try_credit); 81 | printf("Hours Completed: %d\n", pass_credit); 82 | 83 | int course_remain = total_credit - pass_credit; 84 | printf("Credits Remaining: %d\n", course_remain); 85 | 86 | printf("\n"); 87 | printf("Possible Courses to Take Next\n"); 88 | if(course_remain == 0) { 89 | printf(" None - Congratulations!\n"); 90 | return 0; 91 | } 92 | course = 0; 93 | while(course < course_num) { 94 | if(pass_course[course_id[course]] == 0) { 95 | int can = 0; 96 | if(pre_course[course * 1000] == '\0') { 97 | can = 1; 98 | } else { 99 | int cid = 0; 100 | int flag = 1; 101 | int preidx = course * 1000; 102 | while(1) { 103 | if(pre_course[preidx] == '\0') { 104 | if(pass_course[cid] == 0) { 105 | flag = 0; 106 | } 107 | if(flag == 1) { 108 | can = 1; 109 | } 110 | break; 111 | } 112 | if(pre_course[preidx] == ';') { 113 | if(pass_course[cid] == 0) { 114 | flag = 0; 115 | } 116 | if(flag == 1) { 117 | can = 1; 118 | } 119 | cid = 0; 120 | flag = 1; 121 | preidx = preidx + 1; 122 | } 123 | if(pre_course[preidx] == ',') { 124 | if(pass_course[cid] == 0) { 125 | flag = 0; 126 | } 127 | cid = 0; 128 | preidx = preidx + 1; 129 | } 130 | if(pre_course[preidx] != 'c') { 131 | cid = cid * 10 + pre_course[preidx] - '0'; 132 | } 133 | preidx = preidx + 1; 134 | } 135 | } 136 | if(can == 1) { 137 | printf(" "); 138 | int course_idx = course * 1000; 139 | while(course_info[course_idx] != '|') { 140 | printf("%c", course_info[course_idx]); 141 | course_idx = course_idx + 1; 142 | } 143 | printf("\n"); 144 | } 145 | } 146 | course = course + 1; 147 | } 148 | return 0; 149 | } -------------------------------------------------------------------------------- /test/course/course.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/course/course.o -------------------------------------------------------------------------------- /test/course/course.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl main # -- Begin function main 4 | .p2align 4, 0x90 5 | .type main,@function 6 | main: # @main 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | pushq %rbp 10 | .cfi_def_cfa_offset 16 11 | .cfi_offset %rbp, -16 12 | movq %rsp, %rbp 13 | .cfi_def_cfa_register %rbp 14 | pushq %r14 15 | pushq %rbx 16 | subq $32, %rsp 17 | .cfi_offset %rbx, -32 18 | .cfi_offset %r14, -24 19 | movl $0, -36(%rbp) 20 | movl $0, -20(%rbp) 21 | movl $0, -28(%rbp) 22 | movl $0, -24(%rbp) 23 | movl $0, -32(%rbp) 24 | .p2align 4, 0x90 25 | .LBB0_1: # %cond 26 | # =>This Inner Loop Header: Depth=1 27 | imull $1000, -32(%rbp), %eax # imm = 0x3E8 28 | cltq 29 | leaq .Lcourse_info(%rax), %rdi 30 | xorl %eax, %eax 31 | callq gets 32 | imull $1000, -32(%rbp), %eax # imm = 0x3E8 33 | cltq 34 | cmpb $0, .Lcourse_info(%rax) 35 | je .LBB0_2 36 | # %bb.57: # %afterifonly 37 | # in Loop: Header=BB0_1 Depth=1 38 | incl -32(%rbp) 39 | jmp .LBB0_1 40 | .LBB0_2: # %afterLoop 41 | movq %rsp, %rax 42 | leaq -16(%rax), %r14 43 | movq %r14, %rsp 44 | movl $0, -16(%rax) 45 | jmp .LBB0_3 46 | .p2align 4, 0x90 47 | .LBB0_22: # %afterifonly129 48 | # in Loop: Header=BB0_3 Depth=1 49 | incl (%r14) 50 | .LBB0_3: # %cond3 51 | # =>This Loop Header: Depth=1 52 | # Child Loop BB0_5 Depth 2 53 | # Child Loop BB0_10 Depth 2 54 | movl (%r14), %eax 55 | cmpl -32(%rbp), %eax 56 | jge .LBB0_23 57 | # %bb.4: # %loop4 58 | # in Loop: Header=BB0_3 Depth=1 59 | movq %rsp, %rax 60 | leaq -16(%rax), %rdx 61 | movq %rdx, %rsp 62 | imull $1000, (%r14), %ecx # imm = 0x3E8 63 | movl %ecx, -16(%rax) 64 | movq %rsp, %rax 65 | leaq -16(%rax), %rcx 66 | movq %rcx, %rsp 67 | imull $1000, (%r14), %esi # imm = 0x3E8 68 | movl %esi, -16(%rax) 69 | jmp .LBB0_5 70 | .p2align 4, 0x90 71 | .LBB0_8: # %afterifonly20 72 | # in Loop: Header=BB0_5 Depth=2 73 | incl (%rdx) 74 | .LBB0_5: # %cond11 75 | # Parent Loop BB0_3 Depth=1 76 | # => This Inner Loop Header: Depth=2 77 | movslq (%rdx), %rax 78 | cmpb $124, .Lcourse_info(%rax) 79 | je .LBB0_9 80 | # %bb.6: # %loop12 81 | # in Loop: Header=BB0_5 Depth=2 82 | movslq (%rdx), %rax 83 | cmpb $99, .Lcourse_info(%rax) 84 | je .LBB0_8 85 | # %bb.7: # %if19 86 | # in Loop: Header=BB0_5 Depth=2 87 | movslq (%r14), %rax 88 | movl .Lcourse_id(,%rax,4), %esi 89 | leal (%rsi,%rsi,4), %esi 90 | movslq (%rdx), %rdi 91 | movzbl .Lcourse_info(%rdi), %edi 92 | leal -48(%rdi,%rsi,2), %esi 93 | movl %esi, .Lcourse_id(,%rax,4) 94 | jmp .LBB0_8 95 | .p2align 4, 0x90 96 | .LBB0_9: # %afterLoop13 97 | # in Loop: Header=BB0_3 Depth=1 98 | incl (%rdx) 99 | movq %rsp, %rsi 100 | leaq -16(%rsi), %rax 101 | movq %rax, %rsp 102 | movslq (%rdx), %rdi 103 | movb .Lcourse_info(%rdi), %bl 104 | addb $-48, %bl 105 | movzbl %bl, %edi 106 | movl %edi, -16(%rsi) 107 | addl %edi, -36(%rbp) 108 | addl $2, (%rdx) 109 | .p2align 4, 0x90 110 | .LBB0_10: # %cond44 111 | # Parent Loop BB0_3 Depth=1 112 | # => This Inner Loop Header: Depth=2 113 | movslq (%rdx), %rsi 114 | cmpb $124, .Lcourse_info(%rsi) 115 | je .LBB0_12 116 | # %bb.11: # %loop45 117 | # in Loop: Header=BB0_10 Depth=2 118 | movslq (%rcx), %rsi 119 | movslq (%rdx), %rdi 120 | movzbl .Lcourse_info(%rdi), %ebx 121 | movb %bl, .Lpre_course(%rsi) 122 | incl (%rcx) 123 | incl (%rdx) 124 | jmp .LBB0_10 125 | .p2align 4, 0x90 126 | .LBB0_12: # %afterLoop46 127 | # in Loop: Header=BB0_3 Depth=1 128 | movslq (%rcx), %rcx 129 | movb $0, .Lpre_course(%rcx) 130 | movq %rsp, %rsi 131 | leaq -16(%rsi), %rcx 132 | movq %rcx, %rsp 133 | movl (%rdx), %edx 134 | incl %edx 135 | movslq %edx, %rdx 136 | movb .Lcourse_info(%rdx), %dl 137 | movb %dl, -16(%rsi) 138 | cmpb $65, %dl 139 | jne .LBB0_14 140 | # %bb.13: # %if64 141 | # in Loop: Header=BB0_3 Depth=1 142 | movl (%rax), %edx 143 | shll $2, %edx 144 | xorps %xmm0, %xmm0 145 | cvtsi2ss %edx, %xmm0 146 | addss -20(%rbp), %xmm0 147 | movss %xmm0, -20(%rbp) 148 | movl (%rax), %edx 149 | addl %edx, -24(%rbp) 150 | movl (%rax), %edx 151 | addl %edx, -28(%rbp) 152 | movslq (%r14), %rdx 153 | movslq .Lcourse_id(,%rdx,4), %rdx 154 | movl $1, .Lpass_course(,%rdx,4) 155 | .LBB0_14: # %afterifonly65 156 | # in Loop: Header=BB0_3 Depth=1 157 | cmpb $66, (%rcx) 158 | jne .LBB0_16 159 | # %bb.15: # %if80 160 | # in Loop: Header=BB0_3 Depth=1 161 | movl (%rax), %edx 162 | leal (%rdx,%rdx,2), %edx 163 | xorps %xmm0, %xmm0 164 | cvtsi2ss %edx, %xmm0 165 | addss -20(%rbp), %xmm0 166 | movss %xmm0, -20(%rbp) 167 | movl (%rax), %edx 168 | addl %edx, -24(%rbp) 169 | movl (%rax), %edx 170 | addl %edx, -28(%rbp) 171 | movslq (%r14), %rdx 172 | movslq .Lcourse_id(,%rdx,4), %rdx 173 | movl $1, .Lpass_course(,%rdx,4) 174 | .LBB0_16: # %afterifonly81 175 | # in Loop: Header=BB0_3 Depth=1 176 | cmpb $67, (%rcx) 177 | jne .LBB0_18 178 | # %bb.17: # %if96 179 | # in Loop: Header=BB0_3 Depth=1 180 | movl (%rax), %edx 181 | addl %edx, %edx 182 | xorps %xmm0, %xmm0 183 | cvtsi2ss %edx, %xmm0 184 | addss -20(%rbp), %xmm0 185 | movss %xmm0, -20(%rbp) 186 | movl (%rax), %edx 187 | addl %edx, -24(%rbp) 188 | movl (%rax), %edx 189 | addl %edx, -28(%rbp) 190 | movslq (%r14), %rdx 191 | movslq .Lcourse_id(,%rdx,4), %rdx 192 | movl $1, .Lpass_course(,%rdx,4) 193 | .LBB0_18: # %afterifonly97 194 | # in Loop: Header=BB0_3 Depth=1 195 | cmpb $68, (%rcx) 196 | jne .LBB0_20 197 | # %bb.19: # %if112 198 | # in Loop: Header=BB0_3 Depth=1 199 | xorps %xmm0, %xmm0 200 | cvtsi2ssl (%rax), %xmm0 201 | addss -20(%rbp), %xmm0 202 | movss %xmm0, -20(%rbp) 203 | movl (%rax), %edx 204 | addl %edx, -24(%rbp) 205 | movl (%rax), %edx 206 | addl %edx, -28(%rbp) 207 | movslq (%r14), %rdx 208 | movslq .Lcourse_id(,%rdx,4), %rdx 209 | movl $1, .Lpass_course(,%rdx,4) 210 | .LBB0_20: # %afterifonly113 211 | # in Loop: Header=BB0_3 Depth=1 212 | cmpb $70, (%rcx) 213 | jne .LBB0_22 214 | # %bb.21: # %if128 215 | # in Loop: Header=BB0_3 Depth=1 216 | movl (%rax), %eax 217 | addl %eax, -24(%rbp) 218 | jmp .LBB0_22 219 | .LBB0_23: # %afterLoop5 220 | cmpl $0, -24(%rbp) 221 | je .LBB0_25 222 | # %bb.24: # %if136 223 | movss -20(%rbp), %xmm0 # xmm0 = mem[0],zero,zero,zero 224 | cvtsi2ssl -24(%rbp), %xmm1 225 | divss %xmm1, %xmm0 226 | movss %xmm0, -20(%rbp) 227 | .LBB0_25: # %afterifonly137 228 | movss -20(%rbp), %xmm0 # xmm0 = mem[0],zero,zero,zero 229 | cvtss2sd %xmm0, %xmm0 230 | movl $.L_Const_String_, %edi 231 | movb $1, %al 232 | callq printf 233 | movl -24(%rbp), %esi 234 | movl $.L_Const_String_.1, %edi 235 | xorl %eax, %eax 236 | callq printf 237 | movl -28(%rbp), %esi 238 | movl $.L_Const_String_.2, %edi 239 | xorl %eax, %eax 240 | callq printf 241 | movq %rsp, %rbx 242 | leaq -16(%rbx), %rsp 243 | movl -36(%rbp), %esi 244 | subl -28(%rbp), %esi 245 | movl %esi, -16(%rbx) 246 | movl $.L_Const_String_.3, %edi 247 | xorl %eax, %eax 248 | callq printf 249 | movl $.L_Const_String_.4, %edi 250 | xorl %eax, %eax 251 | callq printf 252 | movl $.L_Const_String_.5, %edi 253 | xorl %eax, %eax 254 | callq printf 255 | cmpl $0, -16(%rbx) 256 | jne .LBB0_28 257 | # %bb.26: # %if155 258 | movl $.L_Const_String_.6, %edi 259 | xorl %eax, %eax 260 | callq printf 261 | .LBB0_27: # %afterLoop163 262 | movl $0, -40(%rbp) 263 | movl -40(%rbp), %eax 264 | leaq -16(%rbp), %rsp 265 | popq %rbx 266 | popq %r14 267 | popq %rbp 268 | .cfi_def_cfa %rsp, 8 269 | retq 270 | .LBB0_28: # %afterifonly156 271 | .cfi_def_cfa %rbp, 16 272 | movl $0, (%r14) 273 | jmp .LBB0_29 274 | .p2align 4, 0x90 275 | .LBB0_34: # %afterifonly169 276 | # in Loop: Header=BB0_29 Depth=1 277 | incl (%r14) 278 | .LBB0_29: # %cond161 279 | # =>This Loop Header: Depth=1 280 | # Child Loop BB0_36 Depth 2 281 | # Child Loop BB0_54 Depth 2 282 | movl (%r14), %eax 283 | cmpl -32(%rbp), %eax 284 | jge .LBB0_27 285 | # %bb.30: # %loop162 286 | # in Loop: Header=BB0_29 Depth=1 287 | movslq (%r14), %rax 288 | movslq .Lcourse_id(,%rax,4), %rax 289 | cmpl $0, .Lpass_course(,%rax,4) 290 | jne .LBB0_34 291 | # %bb.31: # %if168 292 | # in Loop: Header=BB0_29 Depth=1 293 | movq %rsp, %rcx 294 | leaq -16(%rcx), %rax 295 | movq %rax, %rsp 296 | movl $0, -16(%rcx) 297 | imull $1000, (%r14), %ecx # imm = 0x3E8 298 | movslq %ecx, %rcx 299 | cmpb $0, .Lpre_course(%rcx) 300 | je .LBB0_32 301 | # %bb.35: # %else 302 | # in Loop: Header=BB0_29 Depth=1 303 | movq %rsp, %rcx 304 | leaq -16(%rcx), %rdx 305 | movq %rdx, %rsp 306 | movl $0, -16(%rcx) 307 | movq %rsp, %rsi 308 | leaq -16(%rsi), %rcx 309 | movq %rcx, %rsp 310 | movl $1, -16(%rsi) 311 | movq %rsp, %rdi 312 | leaq -16(%rdi), %rsi 313 | movq %rsi, %rsp 314 | imull $1000, (%r14), %ebx # imm = 0x3E8 315 | movl %ebx, -16(%rdi) 316 | jmp .LBB0_36 317 | .p2align 4, 0x90 318 | .LBB0_52: # %afterifonly243 319 | # in Loop: Header=BB0_36 Depth=2 320 | incl (%rsi) 321 | .LBB0_36: # %cond185 322 | # Parent Loop BB0_29 Depth=1 323 | # => This Inner Loop Header: Depth=2 324 | movslq (%rsi), %rdi 325 | cmpb $0, .Lpre_course(%rdi) 326 | je .LBB0_37 327 | # %bb.40: # %afterifonly189 328 | # in Loop: Header=BB0_36 Depth=2 329 | movslq (%rsi), %rdi 330 | cmpb $59, .Lpre_course(%rdi) 331 | jne .LBB0_46 332 | # %bb.41: # %if207 333 | # in Loop: Header=BB0_36 Depth=2 334 | movslq (%rdx), %rdi 335 | cmpl $0, .Lpass_course(,%rdi,4) 336 | jne .LBB0_43 337 | # %bb.42: # %if214 338 | # in Loop: Header=BB0_36 Depth=2 339 | movl $0, (%rcx) 340 | .LBB0_43: # %afterifonly215 341 | # in Loop: Header=BB0_36 Depth=2 342 | cmpl $1, (%rcx) 343 | jne .LBB0_45 344 | # %bb.44: # %if221 345 | # in Loop: Header=BB0_36 Depth=2 346 | movl $1, (%rax) 347 | .LBB0_45: # %afterifonly222 348 | # in Loop: Header=BB0_36 Depth=2 349 | movl $0, (%rdx) 350 | movl $1, (%rcx) 351 | incl (%rsi) 352 | .LBB0_46: # %afterifonly208 353 | # in Loop: Header=BB0_36 Depth=2 354 | movslq (%rsi), %rdi 355 | cmpb $44, .Lpre_course(%rdi) 356 | jne .LBB0_50 357 | # %bb.47: # %if227 358 | # in Loop: Header=BB0_36 Depth=2 359 | movslq (%rdx), %rdi 360 | cmpl $0, .Lpass_course(,%rdi,4) 361 | jne .LBB0_49 362 | # %bb.48: # %if234 363 | # in Loop: Header=BB0_36 Depth=2 364 | movl $0, (%rcx) 365 | .LBB0_49: # %afterifonly235 366 | # in Loop: Header=BB0_36 Depth=2 367 | movl $0, (%rdx) 368 | incl (%rsi) 369 | .LBB0_50: # %afterifonly228 370 | # in Loop: Header=BB0_36 Depth=2 371 | movslq (%rsi), %rdi 372 | cmpb $99, .Lpre_course(%rdi) 373 | je .LBB0_52 374 | # %bb.51: # %if242 375 | # in Loop: Header=BB0_36 Depth=2 376 | movl (%rdx), %edi 377 | leal (%rdi,%rdi,4), %edi 378 | movslq (%rsi), %rbx 379 | movzbl .Lpre_course(%rbx), %ebx 380 | leal -48(%rbx,%rdi,2), %edi 381 | movl %edi, (%rdx) 382 | jmp .LBB0_52 383 | .p2align 4, 0x90 384 | .LBB0_37: # %if188 385 | # in Loop: Header=BB0_29 Depth=1 386 | movslq (%rdx), %rdx 387 | cmpl $0, .Lpass_course(,%rdx,4) 388 | jne .LBB0_39 389 | # %bb.38: # %if195 390 | # in Loop: Header=BB0_29 Depth=1 391 | movl $0, (%rcx) 392 | .LBB0_39: # %afterifonly196 393 | # in Loop: Header=BB0_29 Depth=1 394 | cmpl $1, (%rcx) 395 | jne .LBB0_33 396 | .LBB0_32: # %if177 397 | # in Loop: Header=BB0_29 Depth=1 398 | movl $1, (%rax) 399 | .LBB0_33: # %afterifelse 400 | # in Loop: Header=BB0_29 Depth=1 401 | cmpl $1, (%rax) 402 | jne .LBB0_34 403 | # %bb.53: # %if255 404 | # in Loop: Header=BB0_29 Depth=1 405 | movl $.L_Const_String_.7, %edi 406 | xorl %eax, %eax 407 | callq printf 408 | movq %rsp, %rax 409 | leaq -16(%rax), %rbx 410 | movq %rbx, %rsp 411 | imull $1000, (%r14), %ecx # imm = 0x3E8 412 | movl %ecx, -16(%rax) 413 | .p2align 4, 0x90 414 | .LBB0_54: # %cond262 415 | # Parent Loop BB0_29 Depth=1 416 | # => This Inner Loop Header: Depth=2 417 | movslq (%rbx), %rax 418 | cmpb $124, .Lcourse_info(%rax) 419 | je .LBB0_56 420 | # %bb.55: # %loop263 421 | # in Loop: Header=BB0_54 Depth=2 422 | movslq (%rbx), %rax 423 | movzbl .Lcourse_info(%rax), %esi 424 | movl $.L_Const_String_.8, %edi 425 | xorl %eax, %eax 426 | callq printf 427 | incl (%rbx) 428 | jmp .LBB0_54 429 | .p2align 4, 0x90 430 | .LBB0_56: # %afterLoop264 431 | # in Loop: Header=BB0_29 Depth=1 432 | movl $.L_Const_String_.9, %edi 433 | xorl %eax, %eax 434 | callq printf 435 | incl (%r14) 436 | jmp .LBB0_29 437 | .Lfunc_end0: 438 | .size main, .Lfunc_end0-main 439 | .cfi_endproc 440 | # -- End function 441 | .type .Lcourse_info,@object # @course_info 442 | .local .Lcourse_info 443 | .comm .Lcourse_info,1000000,16 444 | .type .Lpre_course,@object # @pre_course 445 | .local .Lpre_course 446 | .comm .Lpre_course,1000000,16 447 | .type .Lpass_course,@object # @pass_course 448 | .local .Lpass_course 449 | .comm .Lpass_course,44400,16 450 | .type .Lcourse_id,@object # @course_id 451 | .local .Lcourse_id 452 | .comm .Lcourse_id,44400,16 453 | .type .L_Const_String_,@object # @_Const_String_ 454 | .section .rodata,"a",@progbits 455 | .L_Const_String_: 456 | .asciz "GPA: %.1f\n" 457 | .size .L_Const_String_, 11 458 | 459 | .type .L_Const_String_.1,@object # @_Const_String_.1 460 | .p2align 4 461 | .L_Const_String_.1: 462 | .asciz "Hours Attempted: %d\n" 463 | .size .L_Const_String_.1, 21 464 | 465 | .type .L_Const_String_.2,@object # @_Const_String_.2 466 | .p2align 4 467 | .L_Const_String_.2: 468 | .asciz "Hours Completed: %d\n" 469 | .size .L_Const_String_.2, 21 470 | 471 | .type .L_Const_String_.3,@object # @_Const_String_.3 472 | .p2align 4 473 | .L_Const_String_.3: 474 | .asciz "Credits Remaining: %d\n" 475 | .size .L_Const_String_.3, 23 476 | 477 | .type .L_Const_String_.4,@object # @_Const_String_.4 478 | .L_Const_String_.4: 479 | .asciz "\n" 480 | .size .L_Const_String_.4, 2 481 | 482 | .type .L_Const_String_.5,@object # @_Const_String_.5 483 | .p2align 4 484 | .L_Const_String_.5: 485 | .asciz "Possible Courses to Take Next\n" 486 | .size .L_Const_String_.5, 31 487 | 488 | .type .L_Const_String_.6,@object # @_Const_String_.6 489 | .p2align 4 490 | .L_Const_String_.6: 491 | .asciz " None - Congratulations!\n" 492 | .size .L_Const_String_.6, 27 493 | 494 | .type .L_Const_String_.7,@object # @_Const_String_.7 495 | .L_Const_String_.7: 496 | .asciz " " 497 | .size .L_Const_String_.7, 3 498 | 499 | .type .L_Const_String_.8,@object # @_Const_String_.8 500 | .L_Const_String_.8: 501 | .asciz "%c" 502 | .size .L_Const_String_.8, 3 503 | 504 | .type .L_Const_String_.9,@object # @_Const_String_.9 505 | .L_Const_String_.9: 506 | .asciz "\n" 507 | .size .L_Const_String_.9, 2 508 | 509 | .section ".note.GNU-stack","",@progbits 510 | -------------------------------------------------------------------------------- /test/course/linux-amd64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/course/linux-amd64 -------------------------------------------------------------------------------- /test/multiplication/linux-amd64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/multiplication/linux-amd64 -------------------------------------------------------------------------------- /test/multiplication/multi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/multiplication/multi -------------------------------------------------------------------------------- /test/multiplication/multi.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/multiplication/multi.bc -------------------------------------------------------------------------------- /test/multiplication/multi.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @A = private global [200000 x i32] zeroinitializer 5 | @B = private global [200000 x i32] zeroinitializer 6 | @C = private global [200000 x i32] zeroinitializer 7 | @input = private global [100000 x i8] zeroinitializer 8 | @_Const_String_ = private constant [7 x i8] c"%d %d\0A\00" 9 | @_Const_String_.1 = private constant [3 x i8] c"%d\00" 10 | @_Const_String_.2 = private constant [6 x i8] c"%d %d\00" 11 | @_Const_String_.3 = private constant [3 x i8] c"%d\00" 12 | @_Const_String_.4 = private constant [25 x i8] c"Incompatible Dimensions\0A\00" 13 | @_Const_String_.5 = private constant [5 x i8] c"%10d\00" 14 | @_Const_String_.6 = private constant [2 x i8] c"\0A\00" 15 | 16 | declare i32 @printf(i8*, ...) 17 | 18 | declare i32 @scanf(...) 19 | 20 | declare i32 @gets(...) 21 | 22 | define i32 @main() { 23 | entry: 24 | %0 = alloca i32 25 | %A_M = alloca i32 26 | %A_N = alloca i32 27 | %B_M = alloca i32 28 | %B_N = alloca i32 29 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_, i32 0, i32 0), i32* %A_M, i32* %A_N) 30 | %i = alloca i32 31 | store i32 0, i32* %i 32 | %j = alloca i32 33 | store i32 0, i32* %j 34 | br label %cond 35 | 36 | return: ; preds = %afterLoop87, %if 37 | %1 = load i32, i32* %0 38 | ret i32 %1 39 | 40 | cond: ; preds = %afterLoop4, %entry 41 | %LoadInst = load i32, i32* %i 42 | %LoadInst1 = load i32, i32* %A_M 43 | %icmptmp = icmp slt i32 %LoadInst, %LoadInst1 44 | %whileCond = icmp ne i1 %icmptmp, false 45 | br i1 %whileCond, label %loop, label %afterLoop 46 | 47 | loop: ; preds = %cond 48 | store i32 0, i32* %j 49 | br label %cond2 50 | 51 | afterLoop: ; preds = %cond 52 | %scanf15 = call i32 (...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_Const_String_.2, i32 0, i32 0), i32* %B_M, i32* %B_N) 53 | store i32 0, i32* %i 54 | store i32 0, i32* %j 55 | br label %cond16 56 | 57 | cond2: ; preds = %loop3, %loop 58 | %LoadInst5 = load i32, i32* %j 59 | %LoadInst6 = load i32, i32* %A_N 60 | %icmptmp7 = icmp slt i32 %LoadInst5, %LoadInst6 61 | %whileCond8 = icmp ne i1 %icmptmp7, false 62 | br i1 %whileCond8, label %loop3, label %afterLoop4 63 | 64 | loop3: ; preds = %cond2 65 | %LoadInst9 = load i32, i32* %i 66 | %LoadInst10 = load i32, i32* %A_N 67 | %2 = mul i32 %LoadInst9, %LoadInst10 68 | %LoadInst11 = load i32, i32* %j 69 | %3 = add i32 %2, %LoadInst11 70 | %elePtr = getelementptr inbounds [200000 x i32], [200000 x i32]* @A, i32 0, i32 %3 71 | %scanf12 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.1, i32 0, i32 0), i32* %elePtr) 72 | %LoadInst13 = load i32, i32* %j 73 | %4 = add i32 %LoadInst13, 1 74 | store i32 %4, i32* %j 75 | br label %cond2 76 | 77 | afterLoop4: ; preds = %cond2 78 | %LoadInst14 = load i32, i32* %i 79 | %5 = add i32 %LoadInst14, 1 80 | store i32 %5, i32* %i 81 | br label %cond 82 | 83 | cond16: ; preds = %afterLoop25, %afterLoop 84 | %LoadInst19 = load i32, i32* %i 85 | %LoadInst20 = load i32, i32* %B_M 86 | %icmptmp21 = icmp slt i32 %LoadInst19, %LoadInst20 87 | %whileCond22 = icmp ne i1 %icmptmp21, false 88 | br i1 %whileCond22, label %loop17, label %afterLoop18 89 | 90 | loop17: ; preds = %cond16 91 | store i32 0, i32* %j 92 | br label %cond23 93 | 94 | afterLoop18: ; preds = %cond16 95 | %LoadInst37 = load i32, i32* %A_N 96 | %LoadInst38 = load i32, i32* %B_M 97 | %icmptmp39 = icmp ne i32 %LoadInst37, %LoadInst38 98 | %ifCond = icmp ne i1 %icmptmp39, false 99 | br i1 %ifCond, label %if, label %afterifonly 100 | 101 | cond23: ; preds = %loop24, %loop17 102 | %LoadInst26 = load i32, i32* %j 103 | %LoadInst27 = load i32, i32* %B_N 104 | %icmptmp28 = icmp slt i32 %LoadInst26, %LoadInst27 105 | %whileCond29 = icmp ne i1 %icmptmp28, false 106 | br i1 %whileCond29, label %loop24, label %afterLoop25 107 | 108 | loop24: ; preds = %cond23 109 | %LoadInst30 = load i32, i32* %i 110 | %LoadInst31 = load i32, i32* %B_N 111 | %6 = mul i32 %LoadInst30, %LoadInst31 112 | %LoadInst32 = load i32, i32* %j 113 | %7 = add i32 %6, %LoadInst32 114 | %elePtr33 = getelementptr inbounds [200000 x i32], [200000 x i32]* @B, i32 0, i32 %7 115 | %scanf34 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.3, i32 0, i32 0), i32* %elePtr33) 116 | %LoadInst35 = load i32, i32* %j 117 | %8 = add i32 %LoadInst35, 1 118 | store i32 %8, i32* %j 119 | br label %cond23 120 | 121 | afterLoop25: ; preds = %cond23 122 | %LoadInst36 = load i32, i32* %i 123 | %9 = add i32 %LoadInst36, 1 124 | store i32 %9, i32* %i 125 | br label %cond16 126 | 127 | if: ; preds = %afterLoop18 128 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @_Const_String_.4, i32 0, i32 0)) 129 | store i32 0, i32* %0 130 | br label %return 131 | 132 | afterifonly: ; preds = %afterLoop18 133 | store i32 0, i32* %i 134 | store i32 0, i32* %j 135 | br label %cond40 136 | 137 | cond40: ; preds = %afterLoop49, %afterifonly 138 | %LoadInst43 = load i32, i32* %i 139 | %LoadInst44 = load i32, i32* %A_M 140 | %icmptmp45 = icmp slt i32 %LoadInst43, %LoadInst44 141 | %whileCond46 = icmp ne i1 %icmptmp45, false 142 | br i1 %whileCond46, label %loop41, label %afterLoop42 143 | 144 | loop41: ; preds = %cond40 145 | store i32 0, i32* %j 146 | br label %cond47 147 | 148 | afterLoop42: ; preds = %cond40 149 | store i32 0, i32* %i 150 | br label %cond85 151 | 152 | cond47: ; preds = %afterLoop59, %loop41 153 | %LoadInst50 = load i32, i32* %j 154 | %LoadInst51 = load i32, i32* %B_N 155 | %icmptmp52 = icmp slt i32 %LoadInst50, %LoadInst51 156 | %whileCond53 = icmp ne i1 %icmptmp52, false 157 | br i1 %whileCond53, label %loop48, label %afterLoop49 158 | 159 | loop48: ; preds = %cond47 160 | %LoadInst54 = load i32, i32* %i 161 | %LoadInst55 = load i32, i32* %B_N 162 | %10 = mul i32 %LoadInst54, %LoadInst55 163 | %LoadInst56 = load i32, i32* %j 164 | %11 = add i32 %10, %LoadInst56 165 | %tmpvar = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %11 166 | store i32 0, i32* %tmpvar 167 | %k = alloca i32 168 | store i32 0, i32* %k 169 | br label %cond57 170 | 171 | afterLoop49: ; preds = %cond47 172 | %LoadInst84 = load i32, i32* %i 173 | %12 = add i32 %LoadInst84, 1 174 | store i32 %12, i32* %i 175 | br label %cond40 176 | 177 | cond57: ; preds = %loop58, %loop48 178 | %LoadInst60 = load i32, i32* %k 179 | %LoadInst61 = load i32, i32* %A_N 180 | %icmptmp62 = icmp slt i32 %LoadInst60, %LoadInst61 181 | %whileCond63 = icmp ne i1 %icmptmp62, false 182 | br i1 %whileCond63, label %loop58, label %afterLoop59 183 | 184 | loop58: ; preds = %cond57 185 | %LoadInst64 = load i32, i32* %i 186 | %LoadInst65 = load i32, i32* %B_N 187 | %13 = mul i32 %LoadInst64, %LoadInst65 188 | %LoadInst66 = load i32, i32* %j 189 | %14 = add i32 %13, %LoadInst66 190 | %tmpvar67 = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %14 191 | %LoadInst68 = load i32, i32* %i 192 | %LoadInst69 = load i32, i32* %B_N 193 | %15 = mul i32 %LoadInst68, %LoadInst69 194 | %LoadInst70 = load i32, i32* %j 195 | %16 = add i32 %15, %LoadInst70 196 | %tmparray = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %16 197 | %tmpvar71 = load i32, i32* %tmparray 198 | %LoadInst72 = load i32, i32* %i 199 | %LoadInst73 = load i32, i32* %A_N 200 | %17 = mul i32 %LoadInst72, %LoadInst73 201 | %LoadInst74 = load i32, i32* %k 202 | %18 = add i32 %17, %LoadInst74 203 | %tmparray75 = getelementptr inbounds [200000 x i32], [200000 x i32]* @A, i32 0, i32 %18 204 | %tmpvar76 = load i32, i32* %tmparray75 205 | %LoadInst77 = load i32, i32* %k 206 | %LoadInst78 = load i32, i32* %B_N 207 | %19 = mul i32 %LoadInst77, %LoadInst78 208 | %LoadInst79 = load i32, i32* %j 209 | %20 = add i32 %19, %LoadInst79 210 | %tmparray80 = getelementptr inbounds [200000 x i32], [200000 x i32]* @B, i32 0, i32 %20 211 | %tmpvar81 = load i32, i32* %tmparray80 212 | %21 = mul i32 %tmpvar76, %tmpvar81 213 | %22 = add i32 %tmpvar71, %21 214 | store i32 %22, i32* %tmpvar67 215 | %LoadInst82 = load i32, i32* %k 216 | %23 = add i32 %LoadInst82, 1 217 | store i32 %23, i32* %k 218 | br label %cond57 219 | 220 | afterLoop59: ; preds = %cond57 221 | %LoadInst83 = load i32, i32* %j 222 | %24 = add i32 %LoadInst83, 1 223 | store i32 %24, i32* %j 224 | br label %cond47 225 | 226 | cond85: ; preds = %afterLoop94, %afterLoop42 227 | %LoadInst88 = load i32, i32* %i 228 | %LoadInst89 = load i32, i32* %A_M 229 | %icmptmp90 = icmp slt i32 %LoadInst88, %LoadInst89 230 | %whileCond91 = icmp ne i1 %icmptmp90, false 231 | br i1 %whileCond91, label %loop86, label %afterLoop87 232 | 233 | loop86: ; preds = %cond85 234 | store i32 0, i32* %j 235 | br label %cond92 236 | 237 | afterLoop87: ; preds = %cond85 238 | store i32 0, i32* %0 239 | br label %return 240 | 241 | cond92: ; preds = %loop93, %loop86 242 | %LoadInst95 = load i32, i32* %j 243 | %LoadInst96 = load i32, i32* %B_N 244 | %icmptmp97 = icmp slt i32 %LoadInst95, %LoadInst96 245 | %whileCond98 = icmp ne i1 %icmptmp97, false 246 | br i1 %whileCond98, label %loop93, label %afterLoop94 247 | 248 | loop93: ; preds = %cond92 249 | %LoadInst99 = load i32, i32* %i 250 | %LoadInst100 = load i32, i32* %B_N 251 | %25 = mul i32 %LoadInst99, %LoadInst100 252 | %LoadInst101 = load i32, i32* %j 253 | %26 = add i32 %25, %LoadInst101 254 | %tmparray102 = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %26 255 | %tmpvar103 = load i32, i32* %tmparray102 256 | %printf104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @_Const_String_.5, i32 0, i32 0), i32 %tmpvar103) 257 | %LoadInst105 = load i32, i32* %j 258 | %27 = add i32 %LoadInst105, 1 259 | store i32 %27, i32* %j 260 | br label %cond92 261 | 262 | afterLoop94: ; preds = %cond92 263 | %printf106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @_Const_String_.6, i32 0, i32 0)) 264 | %LoadInst107 = load i32, i32* %i 265 | %28 = add i32 %LoadInst107, 1 266 | store i32 %28, i32* %i 267 | br label %cond85 268 | } -------------------------------------------------------------------------------- /test/multiplication/multi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/multiplication/multi.o -------------------------------------------------------------------------------- /test/multiplication/multi.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl main # -- Begin function main 4 | .p2align 4, 0x90 5 | .type main,@function 6 | main: # @main 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | pushq %rbp 10 | .cfi_def_cfa_offset 16 11 | .cfi_offset %rbp, -16 12 | movq %rsp, %rbp 13 | .cfi_def_cfa_register %rbp 14 | subq $32, %rsp 15 | leaq -20(%rbp), %rsi 16 | leaq -16(%rbp), %rdx 17 | movl $.L_Const_String_, %edi 18 | xorl %eax, %eax 19 | callq scanf 20 | movl $0, -8(%rbp) 21 | movl $0, -4(%rbp) 22 | jmp .LBB0_1 23 | .p2align 4, 0x90 24 | .LBB0_5: # %afterLoop4 25 | # in Loop: Header=BB0_1 Depth=1 26 | incl -8(%rbp) 27 | .LBB0_1: # %cond 28 | # =>This Loop Header: Depth=1 29 | # Child Loop BB0_3 Depth 2 30 | movl -8(%rbp), %eax 31 | cmpl -20(%rbp), %eax 32 | jge .LBB0_6 33 | # %bb.2: # %loop 34 | # in Loop: Header=BB0_1 Depth=1 35 | movl $0, -4(%rbp) 36 | .p2align 4, 0x90 37 | .LBB0_3: # %cond2 38 | # Parent Loop BB0_1 Depth=1 39 | # => This Inner Loop Header: Depth=2 40 | movl -4(%rbp), %eax 41 | cmpl -16(%rbp), %eax 42 | jge .LBB0_5 43 | # %bb.4: # %loop3 44 | # in Loop: Header=BB0_3 Depth=2 45 | movl -8(%rbp), %eax 46 | imull -16(%rbp), %eax 47 | addl -4(%rbp), %eax 48 | cltq 49 | leaq .LA(,%rax,4), %rsi 50 | movl $.L_Const_String_.1, %edi 51 | xorl %eax, %eax 52 | callq scanf 53 | incl -4(%rbp) 54 | jmp .LBB0_3 55 | .LBB0_6: # %afterLoop 56 | leaq -24(%rbp), %rsi 57 | leaq -12(%rbp), %rdx 58 | movl $.L_Const_String_.2, %edi 59 | xorl %eax, %eax 60 | callq scanf 61 | movl $0, -8(%rbp) 62 | movl $0, -4(%rbp) 63 | jmp .LBB0_7 64 | .p2align 4, 0x90 65 | .LBB0_11: # %afterLoop25 66 | # in Loop: Header=BB0_7 Depth=1 67 | incl -8(%rbp) 68 | .LBB0_7: # %cond16 69 | # =>This Loop Header: Depth=1 70 | # Child Loop BB0_9 Depth 2 71 | movl -8(%rbp), %eax 72 | cmpl -24(%rbp), %eax 73 | jge .LBB0_12 74 | # %bb.8: # %loop17 75 | # in Loop: Header=BB0_7 Depth=1 76 | movl $0, -4(%rbp) 77 | .p2align 4, 0x90 78 | .LBB0_9: # %cond23 79 | # Parent Loop BB0_7 Depth=1 80 | # => This Inner Loop Header: Depth=2 81 | movl -4(%rbp), %eax 82 | cmpl -12(%rbp), %eax 83 | jge .LBB0_11 84 | # %bb.10: # %loop24 85 | # in Loop: Header=BB0_9 Depth=2 86 | movl -8(%rbp), %eax 87 | imull -12(%rbp), %eax 88 | addl -4(%rbp), %eax 89 | cltq 90 | leaq .LB(,%rax,4), %rsi 91 | movl $.L_Const_String_.3, %edi 92 | xorl %eax, %eax 93 | callq scanf 94 | incl -4(%rbp) 95 | jmp .LBB0_9 96 | .LBB0_12: # %afterLoop18 97 | movl -16(%rbp), %eax 98 | cmpl -24(%rbp), %eax 99 | je .LBB0_15 100 | # %bb.13: # %if 101 | movl $.L_Const_String_.4, %edi 102 | xorl %eax, %eax 103 | callq printf 104 | .LBB0_14: # %afterLoop87 105 | movl $0, -28(%rbp) 106 | movl -28(%rbp), %eax 107 | movq %rbp, %rsp 108 | popq %rbp 109 | .cfi_def_cfa %rsp, 8 110 | retq 111 | .LBB0_15: # %afterifonly 112 | .cfi_def_cfa %rbp, 16 113 | movl $0, -8(%rbp) 114 | movl $0, -4(%rbp) 115 | jmp .LBB0_16 116 | .p2align 4, 0x90 117 | .LBB0_29: # %afterLoop49 118 | # in Loop: Header=BB0_16 Depth=1 119 | incl -8(%rbp) 120 | .LBB0_16: # %cond40 121 | # =>This Loop Header: Depth=1 122 | # Child Loop BB0_18 Depth 2 123 | # Child Loop BB0_20 Depth 3 124 | movl -8(%rbp), %eax 125 | cmpl -20(%rbp), %eax 126 | jge .LBB0_23 127 | # %bb.17: # %loop41 128 | # in Loop: Header=BB0_16 Depth=1 129 | movl $0, -4(%rbp) 130 | jmp .LBB0_18 131 | .p2align 4, 0x90 132 | .LBB0_22: # %afterLoop59 133 | # in Loop: Header=BB0_18 Depth=2 134 | incl -4(%rbp) 135 | .LBB0_18: # %cond47 136 | # Parent Loop BB0_16 Depth=1 137 | # => This Loop Header: Depth=2 138 | # Child Loop BB0_20 Depth 3 139 | movl -4(%rbp), %eax 140 | cmpl -12(%rbp), %eax 141 | jge .LBB0_29 142 | # %bb.19: # %loop48 143 | # in Loop: Header=BB0_18 Depth=2 144 | movl -8(%rbp), %eax 145 | imull -12(%rbp), %eax 146 | addl -4(%rbp), %eax 147 | cltq 148 | movl $0, .LC(,%rax,4) 149 | movq %rsp, %rcx 150 | leaq -16(%rcx), %rax 151 | movq %rax, %rsp 152 | movl $0, -16(%rcx) 153 | .p2align 4, 0x90 154 | .LBB0_20: # %cond57 155 | # Parent Loop BB0_16 Depth=1 156 | # Parent Loop BB0_18 Depth=2 157 | # => This Inner Loop Header: Depth=3 158 | movl (%rax), %ecx 159 | cmpl -16(%rbp), %ecx 160 | jge .LBB0_22 161 | # %bb.21: # %loop58 162 | # in Loop: Header=BB0_20 Depth=3 163 | movl -8(%rbp), %ecx 164 | movl -12(%rbp), %edx 165 | movl %ecx, %esi 166 | imull %edx, %esi 167 | movl -4(%rbp), %edi 168 | addl %edi, %esi 169 | movslq %esi, %r8 170 | imull -16(%rbp), %ecx 171 | movl (%rax), %esi 172 | addl %esi, %ecx 173 | movslq %ecx, %rcx 174 | movl .LA(,%rcx,4), %ecx 175 | imull %edx, %esi 176 | addl %edi, %esi 177 | movslq %esi, %rdx 178 | imull .LB(,%rdx,4), %ecx 179 | addl %ecx, .LC(,%r8,4) 180 | incl (%rax) 181 | jmp .LBB0_20 182 | .LBB0_23: # %afterLoop42 183 | movl $0, -8(%rbp) 184 | jmp .LBB0_24 185 | .p2align 4, 0x90 186 | .LBB0_28: # %afterLoop94 187 | # in Loop: Header=BB0_24 Depth=1 188 | movl $.L_Const_String_.6, %edi 189 | xorl %eax, %eax 190 | callq printf 191 | incl -8(%rbp) 192 | .LBB0_24: # %cond85 193 | # =>This Loop Header: Depth=1 194 | # Child Loop BB0_26 Depth 2 195 | movl -8(%rbp), %eax 196 | cmpl -20(%rbp), %eax 197 | jge .LBB0_14 198 | # %bb.25: # %loop86 199 | # in Loop: Header=BB0_24 Depth=1 200 | movl $0, -4(%rbp) 201 | .p2align 4, 0x90 202 | .LBB0_26: # %cond92 203 | # Parent Loop BB0_24 Depth=1 204 | # => This Inner Loop Header: Depth=2 205 | movl -4(%rbp), %eax 206 | cmpl -12(%rbp), %eax 207 | jge .LBB0_28 208 | # %bb.27: # %loop93 209 | # in Loop: Header=BB0_26 Depth=2 210 | movl -8(%rbp), %eax 211 | imull -12(%rbp), %eax 212 | addl -4(%rbp), %eax 213 | cltq 214 | movl .LC(,%rax,4), %esi 215 | movl $.L_Const_String_.5, %edi 216 | xorl %eax, %eax 217 | callq printf 218 | incl -4(%rbp) 219 | jmp .LBB0_26 220 | .Lfunc_end0: 221 | .size main, .Lfunc_end0-main 222 | .cfi_endproc 223 | # -- End function 224 | .type .LA,@object # @A 225 | .local .LA 226 | .comm .LA,800000,16 227 | .type .LB,@object # @B 228 | .local .LB 229 | .comm .LB,800000,16 230 | .type .LC,@object # @C 231 | .local .LC 232 | .comm .LC,800000,16 233 | .type .Linput,@object # @input 234 | .local .Linput 235 | .comm .Linput,100000,16 236 | .type .L_Const_String_,@object # @_Const_String_ 237 | .section .rodata,"a",@progbits 238 | .L_Const_String_: 239 | .asciz "%d %d\n" 240 | .size .L_Const_String_, 7 241 | 242 | .type .L_Const_String_.1,@object # @_Const_String_.1 243 | .L_Const_String_.1: 244 | .asciz "%d" 245 | .size .L_Const_String_.1, 3 246 | 247 | .type .L_Const_String_.2,@object # @_Const_String_.2 248 | .L_Const_String_.2: 249 | .asciz "%d %d" 250 | .size .L_Const_String_.2, 6 251 | 252 | .type .L_Const_String_.3,@object # @_Const_String_.3 253 | .L_Const_String_.3: 254 | .asciz "%d" 255 | .size .L_Const_String_.3, 3 256 | 257 | .type .L_Const_String_.4,@object # @_Const_String_.4 258 | .p2align 4 259 | .L_Const_String_.4: 260 | .asciz "Incompatible Dimensions\n" 261 | .size .L_Const_String_.4, 25 262 | 263 | .type .L_Const_String_.5,@object # @_Const_String_.5 264 | .L_Const_String_.5: 265 | .asciz "%10d" 266 | .size .L_Const_String_.5, 5 267 | 268 | .type .L_Const_String_.6,@object # @_Const_String_.6 269 | .L_Const_String_.6: 270 | .asciz "\n" 271 | .size .L_Const_String_.6, 2 272 | 273 | .section ".note.GNU-stack","",@progbits 274 | -------------------------------------------------------------------------------- /test/multiplication/multiply.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int A[200000]; 4 | int B[200000]; 5 | int C[200000]; 6 | 7 | char input[100000]; 8 | 9 | int main() { 10 | int A_M; 11 | int A_N; 12 | int B_M; 13 | int B_N; 14 | //scanf("%s", input); 15 | //scanf("%s", input); 16 | scanf("%d %d\n", &A_M, &A_N); 17 | int i = 0; 18 | int j = 0; 19 | while(i < A_M) { 20 | j = 0; 21 | while(j < A_N) { 22 | scanf("%d", &A[i * A_N + j]); 23 | j = j + 1; 24 | } 25 | i = i + 1; 26 | } 27 | 28 | //scanf("%s", input); 29 | //scanf("%s", input); 30 | scanf("%d %d", &B_M, &B_N); 31 | i = 0; 32 | j = 0; 33 | while(i < B_M) { 34 | j = 0; 35 | while(j < B_N) { 36 | scanf("%d", &B[i * B_N + j]); 37 | j = j + 1; 38 | } 39 | i = i + 1; 40 | } 41 | 42 | if(A_N != B_M) { 43 | printf("Incompatible Dimensions\n"); 44 | return 0; 45 | } 46 | 47 | i = 0; 48 | j = 0; 49 | while(i < A_M) { 50 | j = 0; 51 | while(j < B_N) { 52 | C[i * B_N + j] = 0; 53 | int k = 0; 54 | while(k < A_N) { 55 | C[i * B_N + j] = C[i * B_N + j] + A[i * A_N + k] * B[k * B_N + j]; 56 | k = k + 1; 57 | } 58 | j = j + 1; 59 | } 60 | i = i + 1; 61 | } 62 | 63 | i = 0; 64 | while(i < A_M) { 65 | j = 0; 66 | while(j < B_N) { 67 | printf("%10d", C[i * B_N + j]); 68 | j = j + 1; 69 | } 70 | printf("\n"); 71 | i = i + 1; 72 | } 73 | return 0; 74 | } -------------------------------------------------------------------------------- /test/quicksort/linux-amd64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/quicksort/linux-amd64 -------------------------------------------------------------------------------- /test/quicksort/quicksort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/test/quicksort/quicksort -------------------------------------------------------------------------------- /test/quicksort/quicksort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void quicksort(int A[10], int left, int right) { 4 | int i; 5 | int j; 6 | int x; 7 | int y; 8 | i = left; 9 | j = right; 10 | x = A[(left + right) / 2]; 11 | while(i <= j) { 12 | while (A[i] < x) { 13 | i = i + 1; 14 | } 15 | while (x < A[j]) { 16 | j = j - 1; 17 | } 18 | if (i <= j) { 19 | y = A[i]; 20 | A[i] = A[j]; 21 | A[j] = y; 22 | i = i + 1; 23 | j = j - 1; 24 | } 25 | } 26 | if (left < j) { 27 | quicksort(A, left, j); 28 | } 29 | if (i < right) { 30 | quicksort(A, i, right); 31 | } 32 | return; 33 | } 34 | 35 | int main() 36 | { 37 | int B[1000000]; 38 | int N; 39 | scanf("%d", &N); 40 | int i = 0; 41 | while(i < N) { 42 | scanf("%d", &B[i]); 43 | i = i + 1; 44 | } 45 | int left = 0; 46 | int right = N - 1; 47 | quicksort(B, left, right); 48 | i = 0; 49 | while(i < N) { 50 | printf("%d\n", B[i]); 51 | i = i + 1; 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /visualization/AST.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name" : "Block", 4 | "children" : 5 | [ 6 | { 7 | "name" : "FunctionDeclaration", 8 | "children" : 9 | [ 10 | { 11 | "name" : "Identifier: int" 12 | }, 13 | { 14 | "name" : "Identifier: f" 15 | }, 16 | { 17 | "name" : "VariableDeclaration", 18 | "children" : 19 | [ 20 | { 21 | "name" : "Identifier: int" 22 | }, 23 | { 24 | "name" : "Identifier: a" 25 | } 26 | ] 27 | }, 28 | { 29 | "name" : "Block", 30 | "children" : 31 | [ 32 | { 33 | "name" : "ExpressionStatement", 34 | "children" : 35 | [ 36 | { 37 | "name" : "ArrayElementAssign", 38 | "children" : 39 | [ 40 | { 41 | "name" : "Identifier: a" 42 | }, 43 | { 44 | "name" : "IntValue: 2" 45 | }, 46 | { 47 | "name" : "IntValue: 3" 48 | } 49 | ] 50 | } 51 | ] 52 | }, 53 | { 54 | "name" : "VariableDeclaration", 55 | "children" : 56 | [ 57 | { 58 | "name" : "Identifier: int" 59 | }, 60 | { 61 | "name" : "Identifier: b" 62 | } 63 | ] 64 | }, 65 | { 66 | "name" : "ExpressionStatement", 67 | "children" : 68 | [ 69 | { 70 | "name" : "ArrayElementAssign", 71 | "children" : 72 | [ 73 | { 74 | "name" : "Identifier: b" 75 | }, 76 | { 77 | "name" : "IntValue: 3" 78 | }, 79 | { 80 | "name" : "IntValue: 4" 81 | } 82 | ] 83 | } 84 | ] 85 | }, 86 | { 87 | "name" : "ExpressionStatement", 88 | "children" : 89 | [ 90 | { 91 | "name" : "FunctionCall", 92 | "children" : 93 | [ 94 | { 95 | "name" : "Identifier: printf" 96 | }, 97 | { 98 | "name" : "StringValue: a[]: %d b[]: %d\n" 99 | }, 100 | { 101 | "name" : "ArrayElement", 102 | "children" : 103 | [ 104 | { 105 | "name" : "Identifier: a" 106 | }, 107 | { 108 | "name" : "IntValue: 2" 109 | } 110 | ] 111 | }, 112 | { 113 | "name" : "ArrayElement", 114 | "children" : 115 | [ 116 | { 117 | "name" : "Identifier: b" 118 | }, 119 | { 120 | "name" : "IntValue: 3" 121 | } 122 | ] 123 | } 124 | ] 125 | } 126 | ] 127 | }, 128 | { 129 | "name" : "ReturnStatement", 130 | "children" : 131 | [ 132 | { 133 | "name" : "IntValue: 2" 134 | } 135 | ] 136 | } 137 | ] 138 | } 139 | ] 140 | }, 141 | { 142 | "name" : "FunctionDeclaration", 143 | "children" : 144 | [ 145 | { 146 | "name" : "Identifier: int" 147 | }, 148 | { 149 | "name" : "Identifier: main" 150 | }, 151 | { 152 | "name" : "Block", 153 | "children" : 154 | [ 155 | { 156 | "name" : "VariableDeclaration", 157 | "children" : 158 | [ 159 | { 160 | "name" : "Identifier: int" 161 | }, 162 | { 163 | "name" : "Identifier: A" 164 | } 165 | ] 166 | }, 167 | { 168 | "name" : "ExpressionStatement", 169 | "children" : 170 | [ 171 | { 172 | "name" : "FunctionCall", 173 | "children" : 174 | [ 175 | { 176 | "name" : "Identifier: f" 177 | }, 178 | { 179 | "name" : "Identifier: A" 180 | } 181 | ] 182 | } 183 | ] 184 | }, 185 | { 186 | "name" : "ExpressionStatement", 187 | "children" : 188 | [ 189 | { 190 | "name" : "FunctionCall", 191 | "children" : 192 | [ 193 | { 194 | "name" : "Identifier: printf" 195 | }, 196 | { 197 | "name" : "StringValue: A[]: %d\n" 198 | }, 199 | { 200 | "name" : "ArrayElement", 201 | "children" : 202 | [ 203 | { 204 | "name" : "Identifier: A" 205 | }, 206 | { 207 | "name" : "IntValue: 2" 208 | } 209 | ] 210 | } 211 | ] 212 | } 213 | ] 214 | }, 215 | { 216 | "name" : "ReturnStatement", 217 | "children" : 218 | [ 219 | { 220 | "name" : "IntValue: 0" 221 | } 222 | ] 223 | } 224 | ] 225 | } 226 | ] 227 | } 228 | ] 229 | } -------------------------------------------------------------------------------- /visualization/ast.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 |
5 | 6 | -------------------------------------------------------------------------------- /visualization/ast.js: -------------------------------------------------------------------------------- 1 | var margin = { 2 | top: 20, 3 | right: 120, 4 | bottom: 20, 5 | left: 120 6 | }, 7 | width = 2960 - margin.right - margin.left, 8 | height = 2800 - margin.top - margin.bottom; 9 | 10 | var root = { 11 | "name": "Block", 12 | "children": [] 13 | }; 14 | 15 | d3.json("AST.json", function(error, data) { 16 | root = data; 17 | }); 18 | 19 | var i = 0, 20 | duration = 750, 21 | rectW = 160, 22 | rectH = 30; 23 | 24 | var tree = d3.layout.tree().nodeSize([200, 40]); 25 | var diagonal = d3.svg.diagonal() 26 | .projection(function (d) { 27 | return [d.x + rectW / 2, d.y + rectH / 2]; 28 | }); 29 | 30 | var svg = d3.select("#body").append("svg").attr("width", 10000).attr("height", 10000) 31 | .call(zm = d3.behavior.zoom().scaleExtent([0.1,20]).on("zoom", redraw)).append("g") 32 | .attr("transform", "translate(" + 350 + "," + 20 + ")"); 33 | 34 | //necessary so that zoom knows where to zoom and unzoom from 35 | zm.translate([350, 30]); 36 | 37 | root.x0 = 0; 38 | root.y0 = height / 2; 39 | 40 | function collapse(d) { 41 | if (d.children) { 42 | d._children = d.children; 43 | d._children.forEach(collapse); 44 | d.children = null; 45 | } 46 | } 47 | 48 | root.children.forEach(collapse); 49 | update(root); 50 | 51 | d3.select("#body").style("height", "800px"); 52 | 53 | function update(source) { 54 | 55 | // Compute the new tree layout. 56 | var nodes = tree.nodes(root).reverse(), 57 | links = tree.links(nodes); 58 | 59 | // Normalize for fixed-depth. 60 | nodes.forEach(function (d) { 61 | d.y = d.depth * 180; 62 | }); 63 | 64 | // Update the nodes… 65 | var node = svg.selectAll("g.node") 66 | .data(nodes, function (d) { 67 | return d.id || (d.id = ++i); 68 | }); 69 | 70 | // Enter any new nodes at the parent's previous position. 71 | var nodeEnter = node.enter().append("g") 72 | .attr("class", "node") 73 | .attr("transform", function (d) { 74 | return "translate(" + source.x0 + "," + source.y0 + ")"; 75 | }) 76 | .on("click", click); 77 | 78 | nodeEnter.append("rect") 79 | .attr("width", rectW) 80 | .attr("height", rectH) 81 | .attr("stroke", "black") 82 | .attr("stroke-width", 1) 83 | .style("fill", function (d) { 84 | return d._children ? "lightsteelblue" : "#fff"; 85 | }); 86 | 87 | nodeEnter.append("text") 88 | .attr("x", rectW / 2) 89 | .attr("y", rectH / 2) 90 | .attr("dy", ".35em") 91 | .attr("text-anchor", "middle") 92 | .text(function (d) { 93 | return d.name; 94 | }); 95 | 96 | // Transition nodes to their new position. 97 | var nodeUpdate = node.transition() 98 | .duration(duration) 99 | .attr("transform", function (d) { 100 | return "translate(" + d.x + "," + d.y + ")"; 101 | }); 102 | 103 | nodeUpdate.select("rect") 104 | .attr("width", rectW) 105 | .attr("height", rectH) 106 | .attr("stroke", "black") 107 | .attr("stroke-width", 1) 108 | .style("fill", function (d) { 109 | return d._children ? "lightsteelblue" : "#fff"; 110 | }); 111 | 112 | nodeUpdate.select("text") 113 | .style("fill-opacity", 1); 114 | 115 | // Transition exiting nodes to the parent's new position. 116 | var nodeExit = node.exit().transition() 117 | .duration(duration) 118 | .attr("transform", function (d) { 119 | return "translate(" + source.x + "," + source.y + ")"; 120 | }) 121 | .remove(); 122 | 123 | nodeExit.select("rect") 124 | .attr("width", rectW) 125 | .attr("height", rectH) 126 | //.attr("width", bbox.getBBox().width)"" 127 | //.attr("height", bbox.getBBox().height) 128 | .attr("stroke", "black") 129 | .attr("stroke-width", 1); 130 | 131 | nodeExit.select("text"); 132 | 133 | // Update the links… 134 | var link = svg.selectAll("path.link") 135 | .data(links, function (d) { 136 | return d.target.id; 137 | }); 138 | 139 | // Enter any new links at the parent's previous position. 140 | link.enter().insert("path", "g") 141 | .attr("class", "link") 142 | .attr("x", rectW / 2) 143 | .attr("y", rectH / 2) 144 | .attr("d", function (d) { 145 | var o = { 146 | x: source.x0, 147 | y: source.y0 148 | }; 149 | return diagonal({ 150 | source: o, 151 | target: o 152 | }); 153 | }); 154 | 155 | // Transition links to their new position. 156 | link.transition() 157 | .duration(duration) 158 | .attr("d", diagonal); 159 | 160 | // Transition exiting nodes to the parent's new position. 161 | link.exit().transition() 162 | .duration(duration) 163 | .attr("d", function (d) { 164 | var o = { 165 | x: source.x, 166 | y: source.y 167 | }; 168 | return diagonal({ 169 | source: o, 170 | target: o 171 | }); 172 | }) 173 | .remove(); 174 | 175 | // Stash the old positions for transition. 176 | nodes.forEach(function (d) { 177 | d.x0 = d.x; 178 | d.y0 = d.y; 179 | }); 180 | } 181 | 182 | // Toggle children on click. 183 | function click(d) { 184 | if (d.children) { 185 | d._children = d.children; 186 | d.children = null; 187 | } else { 188 | d.children = d._children; 189 | d._children = null; 190 | } 191 | update(d); 192 | } 193 | 194 | //Redraw for zoom 195 | function redraw() { 196 | //console.log("here", d3.event.translate, d3.event.scale); 197 | svg.attr("transform", 198 | "translate(" + d3.event.translate + ")" 199 | + " scale(" + d3.event.scale + ")"); 200 | } -------------------------------------------------------------------------------- /visualization/sample.json: -------------------------------------------------------------------------------- 1 | { 2 | "children" : 3 | [ 4 | { 5 | "children" : 6 | [ 7 | { 8 | "name" : "ID" 9 | }, 10 | { 11 | "name" : "LB" 12 | }, 13 | { 14 | "name" : "INT" 15 | }, 16 | { 17 | "name" : "RB" 18 | } 19 | ], 20 | "name" : "VarDec" 21 | } 22 | ], 23 | "name" : "ExtDecList" 24 | } -------------------------------------------------------------------------------- /wez_room/array.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [10 x i8] c"%d %d %d\0A\00" 5 | 6 | declare i32 @printf(i8*, ...) 7 | 8 | declare i32 @scanf(...) 9 | 10 | define i32 @main() { 11 | entry: 12 | %a = alloca [10 x i32] 13 | %tmpvar = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 0 14 | store i32 1, i32* %tmpvar 15 | %tmpvar1 = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 4 16 | store i32 5, i32* %tmpvar1 17 | %t = alloca i32 18 | store i32 10, i32* %t 19 | %i = alloca i32 20 | %LoadInst = load i32, i32* %t 21 | store i32 %LoadInst, i32* %i 22 | %tmparray = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 4 23 | %tmpvar2 = load i32, i32* %tmparray 24 | store i32 %tmpvar2, i32* %t 25 | %tmparray3 = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 0 26 | %tmpvar4 = load i32, i32* %tmparray3 27 | %tmparray5 = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 4 28 | %tmpvar6 = load i32, i32* %tmparray5 29 | %LoadInst7 = load i32, i32* %t 30 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @_Const_String_, i32 0, i32 0), i32 %tmpvar4, i32 %tmpvar6, i32 %LoadInst7) 31 | ret i32 0 32 | } -------------------------------------------------------------------------------- /wez_room/char.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [7 x i8] c"%c %c\0A\00" 5 | 6 | declare i32 @printf(i8*, ...) 7 | 8 | declare i32 @scanf(...) 9 | 10 | define i32 @main() { 11 | entry: 12 | %0 = alloca i32 13 | %c = alloca i8 14 | store i8 99, i8* %c 15 | %d = alloca i8 16 | store i8 68, i8* %d 17 | %LoadInst = load i8, i8* %c 18 | %LoadInst1 = load i8, i8* %d 19 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_, i32 0, i32 0), i8 %LoadInst, i8 %LoadInst1) 20 | store i32 0, i32* %0 21 | br label %return 22 | 23 | return: ; preds = %entry 24 | %1 = load i32, i32* %0 25 | ret i32 %1 26 | } -------------------------------------------------------------------------------- /wez_room/course.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @course_info = private global [1000000 x i8] zeroinitializer 5 | @pre_course = private global [1000000 x i8] zeroinitializer 6 | @pass_course = private global [11100 x i32] zeroinitializer 7 | @course_id = private global [11100 x i32] zeroinitializer 8 | @_Const_String_ = private constant [3 x i8] c"%s\00" 9 | @_Const_String_.1 = private constant [11 x i8] c"GPA: %.1f\0A\00" 10 | @_Const_String_.2 = private constant [21 x i8] c"Hours Attempted: %d\0A\00" 11 | @_Const_String_.3 = private constant [21 x i8] c"Hours Completed: %d\0A\00" 12 | @_Const_String_.4 = private constant [23 x i8] c"Credits Remaining: %d\0A\00" 13 | @_Const_String_.5 = private constant [31 x i8] c"Possible Courses to Take Next\0A\00" 14 | @_Const_String_.6 = private constant [27 x i8] c" None - Congratulations!\0A\00" 15 | @_Const_String_.7 = private constant [3 x i8] c" \00" 16 | @_Const_String_.8 = private constant [3 x i8] c"%c\00" 17 | @_Const_String_.9 = private constant [2 x i8] c"\0A\00" 18 | 19 | declare i32 @printf(i8*, ...) 20 | 21 | declare i32 @scanf(...) 22 | 23 | define i32 @main() { 24 | entry: 25 | %0 = alloca i32 26 | %total_credit = alloca i32 27 | store i32 0, i32* %total_credit 28 | %total_gpa = alloca float 29 | store float 0.000000e+00, float* %total_gpa 30 | %pass_credit = alloca i32 31 | store i32 0, i32* %pass_credit 32 | %try_credit = alloca i32 33 | store i32 0, i32* %try_credit 34 | %course_num = alloca i32 35 | store i32 0, i32* %course_num 36 | br label %cond 37 | 38 | return: ; preds = %afterLoop162, %if154 39 | %1 = load i32, i32* %0 40 | ret i32 %1 41 | 42 | cond: ; preds = %afterifonly, %entry 43 | br i1 true, label %loop, label %afterLoop 44 | 45 | loop: ; preds = %cond 46 | %LoadInst = load i32, i32* %course_num 47 | %2 = mul i32 %LoadInst, 1000 48 | %elePtr = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %2 49 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_, i32 0, i32 0), i8* %elePtr) 50 | %LoadInst1 = load i32, i32* %course_num 51 | %3 = mul i32 %LoadInst1, 1000 52 | %tmparray = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %3 53 | %tmpvar = load i8, i8* %tmparray 54 | %icmptmp = icmp eq i8 %tmpvar, 101 55 | %ifCond = icmp ne i1 %icmptmp, false 56 | br i1 %ifCond, label %if, label %afterifonly 57 | 58 | afterLoop: ; preds = %if, %cond 59 | %course = alloca i32 60 | store i32 0, i32* %course 61 | br label %cond3 62 | 63 | if: ; preds = %loop 64 | br label %afterLoop 65 | 66 | afterifonly: ; preds = %loop 67 | %LoadInst2 = load i32, i32* %course_num 68 | %4 = add i32 %LoadInst2, 1 69 | store i32 %4, i32* %course_num 70 | br label %cond 71 | 72 | cond3: ; preds = %afterifonly129, %afterLoop 73 | %LoadInst6 = load i32, i32* %course 74 | %LoadInst7 = load i32, i32* %course_num 75 | %icmptmp8 = icmp slt i32 %LoadInst6, %LoadInst7 76 | %whileCond = icmp ne i1 %icmptmp8, false 77 | br i1 %whileCond, label %loop4, label %afterLoop5 78 | 79 | loop4: ; preds = %cond3 80 | %idx = alloca i32 81 | %LoadInst9 = load i32, i32* %course 82 | %5 = mul i32 %LoadInst9, 1000 83 | store i32 %5, i32* %idx 84 | %preidx = alloca i32 85 | %LoadInst10 = load i32, i32* %course 86 | %6 = mul i32 %LoadInst10, 1000 87 | store i32 %6, i32* %preidx 88 | br label %cond11 89 | 90 | afterLoop5: ; preds = %cond3 91 | %LoadInst138 = load i32, i32* %try_credit 92 | %icmptmp139 = icmp ne i32 %LoadInst138, 0 93 | %ifCond140 = icmp ne i1 %icmptmp139, false 94 | br i1 %ifCond140, label %if136, label %afterifonly137 95 | 96 | cond11: ; preds = %afterifonly20, %loop4 97 | %LoadInst14 = load i32, i32* %idx 98 | %tmparray15 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst14 99 | %tmpvar16 = load i8, i8* %tmparray15 100 | %icmptmp17 = icmp ne i8 %tmpvar16, 124 101 | %whileCond18 = icmp ne i1 %icmptmp17, false 102 | br i1 %whileCond18, label %loop12, label %afterLoop13 103 | 104 | loop12: ; preds = %cond11 105 | %LoadInst21 = load i32, i32* %idx 106 | %tmparray22 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst21 107 | %tmpvar23 = load i8, i8* %tmparray22 108 | %icmptmp24 = icmp ne i8 %tmpvar23, 99 109 | %ifCond25 = icmp ne i1 %icmptmp24, false 110 | br i1 %ifCond25, label %if19, label %afterifonly20 111 | 112 | afterLoop13: ; preds = %cond11 113 | %LoadInst35 = load i32, i32* %idx 114 | %7 = add i32 %LoadInst35, 1 115 | store i32 %7, i32* %idx 116 | %credit = alloca i32 117 | %LoadInst36 = load i32, i32* %idx 118 | %tmparray37 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst36 119 | %tmpvar38 = load i8, i8* %tmparray37 120 | %8 = sub i8 %tmpvar38, 48 121 | %tmptypecast39 = zext i8 %8 to i32 122 | store i32 %tmptypecast39, i32* %credit 123 | %LoadInst40 = load i32, i32* %total_credit 124 | %LoadInst41 = load i32, i32* %credit 125 | %9 = add i32 %LoadInst40, %LoadInst41 126 | store i32 %9, i32* %total_credit 127 | %LoadInst42 = load i32, i32* %idx 128 | %10 = add i32 %LoadInst42, 1 129 | store i32 %10, i32* %idx 130 | %LoadInst43 = load i32, i32* %idx 131 | %11 = add i32 %LoadInst43, 1 132 | store i32 %11, i32* %idx 133 | br label %cond44 134 | 135 | if19: ; preds = %loop12 136 | %LoadInst26 = load i32, i32* %course 137 | %tmpvar27 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst26 138 | %LoadInst28 = load i32, i32* %course 139 | %tmparray29 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst28 140 | %tmpvar30 = load i32, i32* %tmparray29 141 | %12 = mul i32 %tmpvar30, 10 142 | %LoadInst31 = load i32, i32* %idx 143 | %tmparray32 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst31 144 | %tmpvar33 = load i8, i8* %tmparray32 145 | %tmptypecast = zext i8 %tmpvar33 to i32 146 | %13 = add i32 %12, %tmptypecast 147 | %14 = sub i32 %13, 48 148 | store i32 %14, i32* %tmpvar27 149 | br label %afterifonly20 150 | 151 | afterifonly20: ; preds = %if19, %loop12 152 | %LoadInst34 = load i32, i32* %idx 153 | %15 = add i32 %LoadInst34, 1 154 | store i32 %15, i32* %idx 155 | br label %cond11 156 | 157 | cond44: ; preds = %loop45, %afterLoop13 158 | %LoadInst47 = load i32, i32* %idx 159 | %tmparray48 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst47 160 | %tmpvar49 = load i8, i8* %tmparray48 161 | %icmptmp50 = icmp ne i8 %tmpvar49, 124 162 | %whileCond51 = icmp ne i1 %icmptmp50, false 163 | br i1 %whileCond51, label %loop45, label %afterLoop46 164 | 165 | loop45: ; preds = %cond44 166 | %LoadInst52 = load i32, i32* %preidx 167 | %tmpvar53 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst52 168 | %LoadInst54 = load i32, i32* %idx 169 | %tmparray55 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst54 170 | %tmpvar56 = load i8, i8* %tmparray55 171 | store i8 %tmpvar56, i8* %tmpvar53 172 | %LoadInst57 = load i32, i32* %preidx 173 | %16 = add i32 %LoadInst57, 1 174 | store i32 %16, i32* %preidx 175 | %LoadInst58 = load i32, i32* %idx 176 | %17 = add i32 %LoadInst58, 1 177 | store i32 %17, i32* %idx 178 | br label %cond44 179 | 180 | afterLoop46: ; preds = %cond44 181 | %LoadInst59 = load i32, i32* %preidx 182 | %tmpvar60 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst59 183 | store i8 0, i8* %tmpvar60 184 | %grade = alloca i8 185 | %LoadInst61 = load i32, i32* %idx 186 | %18 = add i32 %LoadInst61, 1 187 | %tmparray62 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %18 188 | %tmpvar63 = load i8, i8* %tmparray62 189 | store i8 %tmpvar63, i8* %grade 190 | %LoadInst66 = load i8, i8* %grade 191 | %icmptmp67 = icmp eq i8 %LoadInst66, 65 192 | %ifCond68 = icmp ne i1 %icmptmp67, false 193 | br i1 %ifCond68, label %if64, label %afterifonly65 194 | 195 | if64: ; preds = %afterLoop46 196 | %LoadInst69 = load float, float* %total_gpa 197 | %LoadInst70 = load i32, i32* %credit 198 | %19 = mul i32 %LoadInst70, 4 199 | %tmptypecast71 = sitofp i32 %19 to float 200 | %20 = fadd float %LoadInst69, %tmptypecast71 201 | store float %20, float* %total_gpa 202 | %LoadInst72 = load i32, i32* %try_credit 203 | %LoadInst73 = load i32, i32* %credit 204 | %21 = add i32 %LoadInst72, %LoadInst73 205 | store i32 %21, i32* %try_credit 206 | %LoadInst74 = load i32, i32* %pass_credit 207 | %LoadInst75 = load i32, i32* %credit 208 | %22 = add i32 %LoadInst74, %LoadInst75 209 | store i32 %22, i32* %pass_credit 210 | %LoadInst76 = load i32, i32* %course 211 | %tmparray77 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst76 212 | %tmpvar78 = load i32, i32* %tmparray77 213 | %tmpvar79 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %tmpvar78 214 | store i32 1, i32* %tmpvar79 215 | br label %afterifonly65 216 | 217 | afterifonly65: ; preds = %if64, %afterLoop46 218 | %LoadInst82 = load i8, i8* %grade 219 | %icmptmp83 = icmp eq i8 %LoadInst82, 66 220 | %ifCond84 = icmp ne i1 %icmptmp83, false 221 | br i1 %ifCond84, label %if80, label %afterifonly81 222 | 223 | if80: ; preds = %afterifonly65 224 | %LoadInst85 = load float, float* %total_gpa 225 | %LoadInst86 = load i32, i32* %credit 226 | %23 = mul i32 %LoadInst86, 3 227 | %tmptypecast87 = sitofp i32 %23 to float 228 | %24 = fadd float %LoadInst85, %tmptypecast87 229 | store float %24, float* %total_gpa 230 | %LoadInst88 = load i32, i32* %try_credit 231 | %LoadInst89 = load i32, i32* %credit 232 | %25 = add i32 %LoadInst88, %LoadInst89 233 | store i32 %25, i32* %try_credit 234 | %LoadInst90 = load i32, i32* %pass_credit 235 | %LoadInst91 = load i32, i32* %credit 236 | %26 = add i32 %LoadInst90, %LoadInst91 237 | store i32 %26, i32* %pass_credit 238 | %LoadInst92 = load i32, i32* %course 239 | %tmparray93 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst92 240 | %tmpvar94 = load i32, i32* %tmparray93 241 | %tmpvar95 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %tmpvar94 242 | store i32 1, i32* %tmpvar95 243 | br label %afterifonly81 244 | 245 | afterifonly81: ; preds = %if80, %afterifonly65 246 | %LoadInst98 = load i8, i8* %grade 247 | %icmptmp99 = icmp eq i8 %LoadInst98, 67 248 | %ifCond100 = icmp ne i1 %icmptmp99, false 249 | br i1 %ifCond100, label %if96, label %afterifonly97 250 | 251 | if96: ; preds = %afterifonly81 252 | %LoadInst101 = load float, float* %total_gpa 253 | %LoadInst102 = load i32, i32* %credit 254 | %27 = mul i32 %LoadInst102, 2 255 | %tmptypecast103 = sitofp i32 %27 to float 256 | %28 = fadd float %LoadInst101, %tmptypecast103 257 | store float %28, float* %total_gpa 258 | %LoadInst104 = load i32, i32* %try_credit 259 | %LoadInst105 = load i32, i32* %credit 260 | %29 = add i32 %LoadInst104, %LoadInst105 261 | store i32 %29, i32* %try_credit 262 | %LoadInst106 = load i32, i32* %pass_credit 263 | %LoadInst107 = load i32, i32* %credit 264 | %30 = add i32 %LoadInst106, %LoadInst107 265 | store i32 %30, i32* %pass_credit 266 | %LoadInst108 = load i32, i32* %course 267 | %tmparray109 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst108 268 | %tmpvar110 = load i32, i32* %tmparray109 269 | %tmpvar111 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %tmpvar110 270 | store i32 1, i32* %tmpvar111 271 | br label %afterifonly97 272 | 273 | afterifonly97: ; preds = %if96, %afterifonly81 274 | %LoadInst114 = load i8, i8* %grade 275 | %icmptmp115 = icmp eq i8 %LoadInst114, 68 276 | %ifCond116 = icmp ne i1 %icmptmp115, false 277 | br i1 %ifCond116, label %if112, label %afterifonly113 278 | 279 | if112: ; preds = %afterifonly97 280 | %LoadInst117 = load float, float* %total_gpa 281 | %LoadInst118 = load i32, i32* %credit 282 | %tmptypecast119 = sitofp i32 %LoadInst118 to float 283 | %31 = fadd float %LoadInst117, %tmptypecast119 284 | store float %31, float* %total_gpa 285 | %LoadInst120 = load i32, i32* %try_credit 286 | %LoadInst121 = load i32, i32* %credit 287 | %32 = add i32 %LoadInst120, %LoadInst121 288 | store i32 %32, i32* %try_credit 289 | %LoadInst122 = load i32, i32* %pass_credit 290 | %LoadInst123 = load i32, i32* %credit 291 | %33 = add i32 %LoadInst122, %LoadInst123 292 | store i32 %33, i32* %pass_credit 293 | %LoadInst124 = load i32, i32* %course 294 | %tmparray125 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst124 295 | %tmpvar126 = load i32, i32* %tmparray125 296 | %tmpvar127 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %tmpvar126 297 | store i32 1, i32* %tmpvar127 298 | br label %afterifonly113 299 | 300 | afterifonly113: ; preds = %if112, %afterifonly97 301 | %LoadInst130 = load i8, i8* %grade 302 | %icmptmp131 = icmp eq i8 %LoadInst130, 70 303 | %ifCond132 = icmp ne i1 %icmptmp131, false 304 | br i1 %ifCond132, label %if128, label %afterifonly129 305 | 306 | if128: ; preds = %afterifonly113 307 | %LoadInst133 = load i32, i32* %try_credit 308 | %LoadInst134 = load i32, i32* %credit 309 | %34 = add i32 %LoadInst133, %LoadInst134 310 | store i32 %34, i32* %try_credit 311 | br label %afterifonly129 312 | 313 | afterifonly129: ; preds = %if128, %afterifonly113 314 | %LoadInst135 = load i32, i32* %course 315 | %35 = add i32 %LoadInst135, 1 316 | store i32 %35, i32* %course 317 | br label %cond3 318 | 319 | if136: ; preds = %afterLoop5 320 | %LoadInst141 = load float, float* %total_gpa 321 | %LoadInst142 = load i32, i32* %try_credit 322 | %tmptypecast143 = sitofp i32 %LoadInst142 to float 323 | %36 = fdiv float %LoadInst141, %tmptypecast143 324 | store float %36, float* %total_gpa 325 | br label %afterifonly137 326 | 327 | afterifonly137: ; preds = %if136, %afterLoop5 328 | %LoadInst144 = load float, float* %total_gpa 329 | %tmpdouble = fpext float %LoadInst144 to double 330 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @_Const_String_.1, i32 0, i32 0), double %tmpdouble) 331 | %LoadInst145 = load i32, i32* %try_credit 332 | %printf146 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @_Const_String_.2, i32 0, i32 0), i32 %LoadInst145) 333 | %LoadInst147 = load i32, i32* %pass_credit 334 | %printf148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @_Const_String_.3, i32 0, i32 0), i32 %LoadInst147) 335 | %course_remain = alloca i32 336 | %LoadInst149 = load i32, i32* %total_credit 337 | %LoadInst150 = load i32, i32* %pass_credit 338 | %37 = sub i32 %LoadInst149, %LoadInst150 339 | store i32 %37, i32* %course_remain 340 | %LoadInst151 = load i32, i32* %course_remain 341 | %printf152 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @_Const_String_.4, i32 0, i32 0), i32 %LoadInst151) 342 | %printf153 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @_Const_String_.5, i32 0, i32 0)) 343 | %LoadInst156 = load i32, i32* %course_remain 344 | %icmptmp157 = icmp eq i32 %LoadInst156, 0 345 | %ifCond158 = icmp ne i1 %icmptmp157, false 346 | br i1 %ifCond158, label %if154, label %afterifonly155 347 | 348 | if154: ; preds = %afterifonly137 349 | %printf159 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @_Const_String_.6, i32 0, i32 0)) 350 | store i32 0, i32* %0 351 | br label %return 352 | 353 | afterifonly155: ; preds = %afterifonly137 354 | store i32 0, i32* %course 355 | br label %cond160 356 | 357 | cond160: ; preds = %afterifonly168, %afterifonly155 358 | %LoadInst163 = load i32, i32* %course 359 | %LoadInst164 = load i32, i32* %course_num 360 | %icmptmp165 = icmp slt i32 %LoadInst163, %LoadInst164 361 | %whileCond166 = icmp ne i1 %icmptmp165, false 362 | br i1 %whileCond166, label %loop161, label %afterLoop162 363 | 364 | loop161: ; preds = %cond160 365 | %LoadInst169 = load i32, i32* %course 366 | %tmparray170 = getelementptr inbounds [11100 x i32], [11100 x i32]* @course_id, i32 0, i32 %LoadInst169 367 | %tmpvar171 = load i32, i32* %tmparray170 368 | %tmparray172 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %tmpvar171 369 | %tmpvar173 = load i32, i32* %tmparray172 370 | %icmptmp174 = icmp eq i32 %tmpvar173, 0 371 | %ifCond175 = icmp ne i1 %icmptmp174, false 372 | br i1 %ifCond175, label %if167, label %afterifonly168 373 | 374 | afterLoop162: ; preds = %cond160 375 | store i32 0, i32* %0 376 | br label %return 377 | 378 | if167: ; preds = %loop161 379 | %can = alloca i32 380 | store i32 0, i32* %can 381 | %LoadInst177 = load i32, i32* %course 382 | %38 = mul i32 %LoadInst177, 1000 383 | %tmparray178 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %38 384 | %tmpvar179 = load i8, i8* %tmparray178 385 | %icmptmp180 = icmp eq i8 %tmpvar179, 0 386 | %ifCond181 = icmp ne i1 %icmptmp180, false 387 | br i1 %ifCond181, label %if176, label %else 388 | 389 | afterifonly168: ; preds = %afterifonly255, %loop161 390 | %LoadInst275 = load i32, i32* %course 391 | %39 = add i32 %LoadInst275, 1 392 | store i32 %39, i32* %course 393 | br label %cond160 394 | 395 | if176: ; preds = %if167 396 | store i32 1, i32* %can 397 | br label %afterifelse 398 | 399 | else: ; preds = %if167 400 | %cid = alloca i32 401 | store i32 0, i32* %cid 402 | %flag = alloca i32 403 | store i32 1, i32* %flag 404 | %preidx182 = alloca i32 405 | %LoadInst183 = load i32, i32* %course 406 | %40 = mul i32 %LoadInst183, 1000 407 | store i32 %40, i32* %preidx182 408 | br label %cond184 409 | 410 | afterifelse: ; preds = %afterLoop186, %if176 411 | %LoadInst256 = load i32, i32* %can 412 | %icmptmp257 = icmp eq i32 %LoadInst256, 1 413 | %ifCond258 = icmp ne i1 %icmptmp257, false 414 | br i1 %ifCond258, label %if254, label %afterifonly255 415 | 416 | cond184: ; preds = %afterifonly242, %else 417 | br i1 true, label %loop185, label %afterLoop186 418 | 419 | loop185: ; preds = %cond184 420 | %LoadInst189 = load i32, i32* %preidx182 421 | %tmparray190 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst189 422 | %tmpvar191 = load i8, i8* %tmparray190 423 | %icmptmp192 = icmp eq i8 %tmpvar191, 0 424 | %ifCond193 = icmp ne i1 %icmptmp192, false 425 | br i1 %ifCond193, label %if187, label %afterifonly188 426 | 427 | afterLoop186: ; preds = %afterifonly202, %cond184 428 | br label %afterifelse 429 | 430 | if187: ; preds = %loop185 431 | %LoadInst196 = load i32, i32* %cid 432 | %tmparray197 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %LoadInst196 433 | %tmpvar198 = load i32, i32* %tmparray197 434 | %icmptmp199 = icmp eq i32 %tmpvar198, 0 435 | %ifCond200 = icmp ne i1 %icmptmp199, false 436 | br i1 %ifCond200, label %if194, label %afterifonly195 437 | 438 | afterifonly188: ; preds = %loop185 439 | %LoadInst208 = load i32, i32* %preidx182 440 | %tmparray209 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst208 441 | %tmpvar210 = load i8, i8* %tmparray209 442 | %icmptmp211 = icmp eq i8 %tmpvar210, 59 443 | %ifCond212 = icmp ne i1 %icmptmp211, false 444 | br i1 %ifCond212, label %if206, label %afterifonly207 445 | 446 | if194: ; preds = %if187 447 | store i32 0, i32* %flag 448 | br label %afterifonly195 449 | 450 | afterifonly195: ; preds = %if194, %if187 451 | %LoadInst203 = load i32, i32* %flag 452 | %icmptmp204 = icmp eq i32 %LoadInst203, 1 453 | %ifCond205 = icmp ne i1 %icmptmp204, false 454 | br i1 %ifCond205, label %if201, label %afterifonly202 455 | 456 | if201: ; preds = %afterifonly195 457 | store i32 1, i32* %can 458 | br label %afterifonly202 459 | 460 | afterifonly202: ; preds = %if201, %afterifonly195 461 | br label %afterLoop186 462 | 463 | if206: ; preds = %afterifonly188 464 | %LoadInst215 = load i32, i32* %cid 465 | %tmparray216 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %LoadInst215 466 | %tmpvar217 = load i32, i32* %tmparray216 467 | %icmptmp218 = icmp eq i32 %tmpvar217, 0 468 | %ifCond219 = icmp ne i1 %icmptmp218, false 469 | br i1 %ifCond219, label %if213, label %afterifonly214 470 | 471 | afterifonly207: ; preds = %afterifonly221, %afterifonly188 472 | %LoadInst228 = load i32, i32* %preidx182 473 | %tmparray229 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst228 474 | %tmpvar230 = load i8, i8* %tmparray229 475 | %icmptmp231 = icmp eq i8 %tmpvar230, 44 476 | %ifCond232 = icmp ne i1 %icmptmp231, false 477 | br i1 %ifCond232, label %if226, label %afterifonly227 478 | 479 | if213: ; preds = %if206 480 | store i32 0, i32* %flag 481 | br label %afterifonly214 482 | 483 | afterifonly214: ; preds = %if213, %if206 484 | %LoadInst222 = load i32, i32* %flag 485 | %icmptmp223 = icmp eq i32 %LoadInst222, 1 486 | %ifCond224 = icmp ne i1 %icmptmp223, false 487 | br i1 %ifCond224, label %if220, label %afterifonly221 488 | 489 | if220: ; preds = %afterifonly214 490 | store i32 1, i32* %can 491 | br label %afterifonly221 492 | 493 | afterifonly221: ; preds = %if220, %afterifonly214 494 | store i32 0, i32* %cid 495 | store i32 1, i32* %flag 496 | %LoadInst225 = load i32, i32* %preidx182 497 | %41 = add i32 %LoadInst225, 1 498 | store i32 %41, i32* %preidx182 499 | br label %afterifonly207 500 | 501 | if226: ; preds = %afterifonly207 502 | %LoadInst235 = load i32, i32* %cid 503 | %tmparray236 = getelementptr inbounds [11100 x i32], [11100 x i32]* @pass_course, i32 0, i32 %LoadInst235 504 | %tmpvar237 = load i32, i32* %tmparray236 505 | %icmptmp238 = icmp eq i32 %tmpvar237, 0 506 | %ifCond239 = icmp ne i1 %icmptmp238, false 507 | br i1 %ifCond239, label %if233, label %afterifonly234 508 | 509 | afterifonly227: ; preds = %afterifonly234, %afterifonly207 510 | %LoadInst243 = load i32, i32* %preidx182 511 | %tmparray244 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst243 512 | %tmpvar245 = load i8, i8* %tmparray244 513 | %icmptmp246 = icmp ne i8 %tmpvar245, 99 514 | %ifCond247 = icmp ne i1 %icmptmp246, false 515 | br i1 %ifCond247, label %if241, label %afterifonly242 516 | 517 | if233: ; preds = %if226 518 | store i32 0, i32* %flag 519 | br label %afterifonly234 520 | 521 | afterifonly234: ; preds = %if233, %if226 522 | store i32 0, i32* %cid 523 | %LoadInst240 = load i32, i32* %preidx182 524 | %42 = add i32 %LoadInst240, 1 525 | store i32 %42, i32* %preidx182 526 | br label %afterifonly227 527 | 528 | if241: ; preds = %afterifonly227 529 | %LoadInst248 = load i32, i32* %cid 530 | %43 = mul i32 %LoadInst248, 10 531 | %LoadInst249 = load i32, i32* %preidx182 532 | %tmparray250 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @pre_course, i32 0, i32 %LoadInst249 533 | %tmpvar251 = load i8, i8* %tmparray250 534 | %tmptypecast252 = zext i8 %tmpvar251 to i32 535 | %44 = add i32 %43, %tmptypecast252 536 | %45 = sub i32 %44, 48 537 | store i32 %45, i32* %cid 538 | br label %afterifonly242 539 | 540 | afterifonly242: ; preds = %if241, %afterifonly227 541 | %LoadInst253 = load i32, i32* %preidx182 542 | %46 = add i32 %LoadInst253, 1 543 | store i32 %46, i32* %preidx182 544 | br label %cond184 545 | 546 | if254: ; preds = %afterifelse 547 | %printf259 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.7, i32 0, i32 0)) 548 | %course_idx = alloca i32 549 | %LoadInst260 = load i32, i32* %course 550 | %47 = mul i32 %LoadInst260, 1000 551 | store i32 %47, i32* %course_idx 552 | br label %cond261 553 | 554 | afterifonly255: ; preds = %afterLoop263, %afterifelse 555 | br label %afterifonly168 556 | 557 | cond261: ; preds = %loop262, %if254 558 | %LoadInst264 = load i32, i32* %course_idx 559 | %tmparray265 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst264 560 | %tmpvar266 = load i8, i8* %tmparray265 561 | %icmptmp267 = icmp ne i8 %tmpvar266, 124 562 | %whileCond268 = icmp ne i1 %icmptmp267, false 563 | br i1 %whileCond268, label %loop262, label %afterLoop263 564 | 565 | loop262: ; preds = %cond261 566 | %LoadInst269 = load i32, i32* %course_idx 567 | %tmparray270 = getelementptr inbounds [1000000 x i8], [1000000 x i8]* @course_info, i32 0, i32 %LoadInst269 568 | %tmpvar271 = load i8, i8* %tmparray270 569 | %printf272 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.8, i32 0, i32 0), i8 %tmpvar271) 570 | %LoadInst273 = load i32, i32* %course_idx 571 | %48 = add i32 %LoadInst273, 1 572 | store i32 %48, i32* %course_idx 573 | br label %cond261 574 | 575 | afterLoop263: ; preds = %cond261 576 | %printf274 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @_Const_String_.9, i32 0, i32 0)) 577 | br label %afterifonly255 578 | } -------------------------------------------------------------------------------- /wez_room/easy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/easy -------------------------------------------------------------------------------- /wez_room/easy.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/easy.bc -------------------------------------------------------------------------------- /wez_room/easy.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | declare i32 @printf(i8*, ...) 5 | 6 | declare i32 @scanf(...) 7 | 8 | define i32 @main() { 9 | entry: 10 | %i = alloca i32 11 | store i32 0, i32* %i 12 | %LoadInst = load i32, i32* %i 13 | ret i32 %LoadInst 14 | } -------------------------------------------------------------------------------- /wez_room/easy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/easy.o -------------------------------------------------------------------------------- /wez_room/easy.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl main # -- Begin function main 4 | .p2align 4, 0x90 5 | .type main,@function 6 | main: # @main 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | movl $0, -4(%rsp) 10 | xorl %eax, %eax 11 | retq 12 | .Lfunc_end0: 13 | .size main, .Lfunc_end0-main 14 | .cfi_endproc 15 | # -- End function 16 | .section ".note.GNU-stack","",@progbits 17 | -------------------------------------------------------------------------------- /wez_room/fun.c: -------------------------------------------------------------------------------- 1 | void f(int a[10]) { 2 | printf("%d\n", a[2]); 3 | a[4] = 6; 4 | return; 5 | } 6 | 7 | int main() { 8 | int b[10]; 9 | b[2] = 3; 10 | f(b); 11 | printf("b: %d\n", b[4]); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /wez_room/function.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [4 x i8] c"%d\0A\00" 5 | @_Const_String_.1 = private constant [4 x i8] c"%d\0A\00" 6 | 7 | declare i32 @printf(i8*, ...) 8 | 9 | declare i32 @scanf(...) 10 | 11 | define void @f(i32* %a1, i32 %b2) { 12 | entry: 13 | %a = alloca i32* 14 | store i32* %a1, i32** %a 15 | %b = alloca i32 16 | store i32 %b2, i32* %b 17 | %0 = load i32*, i32** %a 18 | %tmparray = getelementptr inbounds i32, i32* %0, i32 4 19 | %tmpvar = load i32, i32* %tmparray 20 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_, i32 0, i32 0), i32 %tmpvar) 21 | %1 = load i32*, i32** %a 22 | %tmpvar3 = getelementptr inbounds i32, i32* %1, i32 2 23 | store i32 2, i32* %tmpvar3 24 | br label %return 25 | 26 | return: ; preds = %entry 27 | ret void 28 | } 29 | 30 | define i32 @main() { 31 | entry: 32 | %0 = alloca i32 33 | %b = alloca [10 x i32] 34 | %tmpvar = getelementptr inbounds [10 x i32], [10 x i32]* %b, i32 0, i32 4 35 | store i32 4, i32* %tmpvar 36 | %c = alloca i32 37 | store i32 2, i32* %c 38 | %arrayPtr = getelementptr inbounds [10 x i32], [10 x i32]* %b, i32 0, i32 0 39 | %LoadInst = load i32, i32* %c 40 | call void @f(i32* %arrayPtr, i32 %LoadInst) 41 | %tmparray = getelementptr inbounds [10 x i32], [10 x i32]* %b, i32 0, i32 2 42 | %tmpvar1 = load i32, i32* %tmparray 43 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.1, i32 0, i32 0), i32 %tmpvar1) 44 | store i32 0, i32* %0 45 | br label %return 46 | 47 | return: ; preds = %entry 48 | %1 = load i32, i32* %0 49 | ret i32 %1 50 | } -------------------------------------------------------------------------------- /wez_room/gets.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @str = private global [10 x i8] zeroinitializer 5 | @_Const_String_ = private constant [4 x i8] c"%s\0A\00" 6 | 7 | declare i32 @printf(i8*, ...) 8 | 9 | declare i32 @scanf(...) 10 | 11 | declare i32 @gets(...) 12 | 13 | define i32 @main() { 14 | entry: 15 | %0 = alloca i32 16 | %gets = call i32 (...) @gets(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i32 0, i32 0)) 17 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i32 0, i32 0)) 18 | store i32 0, i32* %0 19 | br label %return 20 | 21 | return: ; preds = %entry 22 | %1 = load i32, i32* %0 23 | ret i32 %1 24 | } -------------------------------------------------------------------------------- /wez_room/if.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @j = private global i32 0 5 | @_Const_String_ = private constant [7 x i8] c"j: %d\0A\00" 6 | 7 | declare i32 @printf(i8*, ...) 8 | 9 | declare i32 @scanf(...) 10 | 11 | define i32 @main() { 12 | entry: 13 | %j = alloca i32 14 | store i32 3, i32* %j 15 | %i = alloca i32 16 | store i32 2, i32* %i 17 | %LoadInst = load i32, i32* %i 18 | %icmptmp = icmp slt i32 %LoadInst, 4 19 | %ifCond = icmp ne i1 %icmptmp, false 20 | br i1 %ifCond, label %if, label %else 21 | 22 | if: ; preds = %entry 23 | %j1 = alloca i32 24 | store i32 4, i32* %j1 25 | br label %afterifelse 26 | 27 | else: ; preds = %entry 28 | store i32 5, i32* %j 29 | br label %afterifelse 30 | 31 | afterifelse: ; preds = %else, %if 32 | %LoadInst2 = load i32, i32* %j 33 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_, i32 0, i32 0), i32 %LoadInst2) 34 | ret i32 0 35 | } -------------------------------------------------------------------------------- /wez_room/ifelse.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [7 x i8] c"i: %d\0A\00" 5 | @_Const_String_.1 = private constant [12 x i8] c"if: j : %d\0A\00" 6 | @_Const_String_.2 = private constant [14 x i8] c"else: j : %d\0A\00" 7 | @_Const_String_.3 = private constant [7 x i8] c"i: %d\0A\00" 8 | 9 | declare i32 @printf(i8*, ...) 10 | 11 | declare i32 @scanf(...) 12 | 13 | define i32 @main() { 14 | entry: 15 | %i = alloca i32 16 | store i32 10, i32* %i 17 | %LoadInst = load i32, i32* %i 18 | %icmptmp = icmp sgt i32 %LoadInst, 9 19 | %ifCond = icmp ne i1 %icmptmp, false 20 | br i1 %ifCond, label %if, label %else 21 | 22 | if: ; preds = %entry 23 | %LoadInst1 = load i32, i32* %i 24 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_, i32 0, i32 0), i32 %LoadInst1) 25 | br label %afterifelse 26 | 27 | else: ; preds = %entry 28 | br label %afterifelse 29 | 30 | afterifelse: ; preds = %else, %if 31 | %LoadInst5 = load i32, i32* %i 32 | %icmptmp6 = icmp slt i32 %LoadInst5, 8 33 | %ifCond7 = icmp ne i1 %icmptmp6, false 34 | br i1 %ifCond7, label %if2, label %else3 35 | 36 | if2: ; preds = %afterifelse 37 | %j = alloca i32 38 | store i32 10, i32* %j 39 | %LoadInst8 = load i32, i32* %j 40 | %printf9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @_Const_String_.1, i32 0, i32 0), i32 %LoadInst8) 41 | br label %afterifelse4 42 | 43 | else3: ; preds = %afterifelse 44 | %j10 = alloca i32 45 | store i32 12, i32* %j10 46 | %LoadInst11 = load i32, i32* %j10 47 | %printf12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @_Const_String_.2, i32 0, i32 0), i32 %LoadInst11) 48 | br label %afterifelse4 49 | 50 | afterifelse4: ; preds = %else3, %if2 51 | %LoadInst16 = load i32, i32* %i 52 | %icmptmp17 = icmp slt i32 %LoadInst16, 20 53 | %ifCond18 = icmp ne i1 %icmptmp17, false 54 | br i1 %ifCond18, label %if13, label %else14 55 | 56 | if13: ; preds = %afterifelse4 57 | %LoadInst19 = load i32, i32* %i 58 | %printf20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_.3, i32 0, i32 0), i32 %LoadInst19) 59 | br label %afterifelse15 60 | 61 | else14: ; preds = %afterifelse4 62 | br label %afterifelse15 63 | 64 | afterifelse15: ; preds = %else14, %if13 65 | ret i32 0 66 | } -------------------------------------------------------------------------------- /wez_room/multi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/multi -------------------------------------------------------------------------------- /wez_room/multi.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/multi.bc -------------------------------------------------------------------------------- /wez_room/multi.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @A = private global [200000 x i32] zeroinitializer 5 | @B = private global [200000 x i32] zeroinitializer 6 | @C = private global [200000 x i32] zeroinitializer 7 | @_Const_String_ = private constant [6 x i8] c"%d %d\00" 8 | @_Const_String_.1 = private constant [3 x i8] c"%d\00" 9 | @_Const_String_.2 = private constant [6 x i8] c"%d %d\00" 10 | @_Const_String_.3 = private constant [3 x i8] c"%d\00" 11 | @_Const_String_.4 = private constant [25 x i8] c"Incompatible Dimensions\0A\00" 12 | @_Const_String_.5 = private constant [4 x i8] c"%d \00" 13 | @_Const_String_.6 = private constant [2 x i8] c"\0A\00" 14 | 15 | declare i32 @printf(i8*, ...) 16 | 17 | declare i32 @scanf(...) 18 | 19 | define i32 @main() { 20 | entry: 21 | %0 = alloca i32 22 | %A_M = alloca i32 23 | %A_N = alloca i32 24 | %B_M = alloca i32 25 | %B_N = alloca i32 26 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_Const_String_, i32 0, i32 0), i32* %A_M, i32* %A_N) 27 | %i = alloca i32 28 | store i32 0, i32* %i 29 | %j = alloca i32 30 | store i32 0, i32* %j 31 | br label %cond 32 | 33 | return: ; preds = %afterLoop87, %if 34 | %1 = load i32, i32* %0 35 | ret i32 %1 36 | 37 | cond: ; preds = %afterLoop4, %entry 38 | %LoadInst = load i32, i32* %i 39 | %LoadInst1 = load i32, i32* %A_M 40 | %icmptmp = icmp slt i32 %LoadInst, %LoadInst1 41 | %whileCond = icmp ne i1 %icmptmp, false 42 | br i1 %whileCond, label %loop, label %afterLoop 43 | 44 | loop: ; preds = %cond 45 | store i32 0, i32* %j 46 | br label %cond2 47 | 48 | afterLoop: ; preds = %cond 49 | %scanf15 = call i32 (...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_Const_String_.2, i32 0, i32 0), i32* %B_M, i32* %B_N) 50 | store i32 0, i32* %i 51 | store i32 0, i32* %j 52 | br label %cond16 53 | 54 | cond2: ; preds = %loop3, %loop 55 | %LoadInst5 = load i32, i32* %j 56 | %LoadInst6 = load i32, i32* %A_N 57 | %icmptmp7 = icmp slt i32 %LoadInst5, %LoadInst6 58 | %whileCond8 = icmp ne i1 %icmptmp7, false 59 | br i1 %whileCond8, label %loop3, label %afterLoop4 60 | 61 | loop3: ; preds = %cond2 62 | %LoadInst9 = load i32, i32* %i 63 | %LoadInst10 = load i32, i32* %A_N 64 | %2 = mul i32 %LoadInst9, %LoadInst10 65 | %LoadInst11 = load i32, i32* %j 66 | %3 = add i32 %2, %LoadInst11 67 | %elePtr = getelementptr inbounds [200000 x i32], [200000 x i32]* @A, i32 0, i32 %3 68 | %scanf12 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.1, i32 0, i32 0), i32* %elePtr) 69 | %LoadInst13 = load i32, i32* %j 70 | %4 = add i32 %LoadInst13, 1 71 | store i32 %4, i32* %j 72 | br label %cond2 73 | 74 | afterLoop4: ; preds = %cond2 75 | %LoadInst14 = load i32, i32* %i 76 | %5 = add i32 %LoadInst14, 1 77 | store i32 %5, i32* %i 78 | br label %cond 79 | 80 | cond16: ; preds = %afterLoop25, %afterLoop 81 | %LoadInst19 = load i32, i32* %i 82 | %LoadInst20 = load i32, i32* %B_M 83 | %icmptmp21 = icmp slt i32 %LoadInst19, %LoadInst20 84 | %whileCond22 = icmp ne i1 %icmptmp21, false 85 | br i1 %whileCond22, label %loop17, label %afterLoop18 86 | 87 | loop17: ; preds = %cond16 88 | store i32 0, i32* %j 89 | br label %cond23 90 | 91 | afterLoop18: ; preds = %cond16 92 | %LoadInst37 = load i32, i32* %A_N 93 | %LoadInst38 = load i32, i32* %B_M 94 | %icmptmp39 = icmp ne i32 %LoadInst37, %LoadInst38 95 | %ifCond = icmp ne i1 %icmptmp39, false 96 | br i1 %ifCond, label %if, label %afterifonly 97 | 98 | cond23: ; preds = %loop24, %loop17 99 | %LoadInst26 = load i32, i32* %j 100 | %LoadInst27 = load i32, i32* %B_N 101 | %icmptmp28 = icmp slt i32 %LoadInst26, %LoadInst27 102 | %whileCond29 = icmp ne i1 %icmptmp28, false 103 | br i1 %whileCond29, label %loop24, label %afterLoop25 104 | 105 | loop24: ; preds = %cond23 106 | %LoadInst30 = load i32, i32* %i 107 | %LoadInst31 = load i32, i32* %B_N 108 | %6 = mul i32 %LoadInst30, %LoadInst31 109 | %LoadInst32 = load i32, i32* %j 110 | %7 = add i32 %6, %LoadInst32 111 | %elePtr33 = getelementptr inbounds [200000 x i32], [200000 x i32]* @B, i32 0, i32 %7 112 | %scanf34 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.3, i32 0, i32 0), i32* %elePtr33) 113 | %LoadInst35 = load i32, i32* %j 114 | %8 = add i32 %LoadInst35, 1 115 | store i32 %8, i32* %j 116 | br label %cond23 117 | 118 | afterLoop25: ; preds = %cond23 119 | %LoadInst36 = load i32, i32* %i 120 | %9 = add i32 %LoadInst36, 1 121 | store i32 %9, i32* %i 122 | br label %cond16 123 | 124 | if: ; preds = %afterLoop18 125 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @_Const_String_.4, i32 0, i32 0)) 126 | store i32 0, i32* %0 127 | br label %return 128 | 129 | afterifonly: ; preds = %afterLoop18 130 | store i32 0, i32* %i 131 | store i32 0, i32* %j 132 | br label %cond40 133 | 134 | cond40: ; preds = %afterLoop49, %afterifonly 135 | %LoadInst43 = load i32, i32* %i 136 | %LoadInst44 = load i32, i32* %A_M 137 | %icmptmp45 = icmp slt i32 %LoadInst43, %LoadInst44 138 | %whileCond46 = icmp ne i1 %icmptmp45, false 139 | br i1 %whileCond46, label %loop41, label %afterLoop42 140 | 141 | loop41: ; preds = %cond40 142 | store i32 0, i32* %j 143 | br label %cond47 144 | 145 | afterLoop42: ; preds = %cond40 146 | store i32 0, i32* %i 147 | br label %cond85 148 | 149 | cond47: ; preds = %afterLoop59, %loop41 150 | %LoadInst50 = load i32, i32* %j 151 | %LoadInst51 = load i32, i32* %B_N 152 | %icmptmp52 = icmp slt i32 %LoadInst50, %LoadInst51 153 | %whileCond53 = icmp ne i1 %icmptmp52, false 154 | br i1 %whileCond53, label %loop48, label %afterLoop49 155 | 156 | loop48: ; preds = %cond47 157 | %LoadInst54 = load i32, i32* %i 158 | %LoadInst55 = load i32, i32* %B_N 159 | %10 = mul i32 %LoadInst54, %LoadInst55 160 | %LoadInst56 = load i32, i32* %j 161 | %11 = add i32 %10, %LoadInst56 162 | %tmpvar = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %11 163 | store i32 0, i32* %tmpvar 164 | %k = alloca i32 165 | store i32 0, i32* %k 166 | br label %cond57 167 | 168 | afterLoop49: ; preds = %cond47 169 | %LoadInst84 = load i32, i32* %i 170 | %12 = add i32 %LoadInst84, 1 171 | store i32 %12, i32* %i 172 | br label %cond40 173 | 174 | cond57: ; preds = %loop58, %loop48 175 | %LoadInst60 = load i32, i32* %k 176 | %LoadInst61 = load i32, i32* %A_N 177 | %icmptmp62 = icmp slt i32 %LoadInst60, %LoadInst61 178 | %whileCond63 = icmp ne i1 %icmptmp62, false 179 | br i1 %whileCond63, label %loop58, label %afterLoop59 180 | 181 | loop58: ; preds = %cond57 182 | %LoadInst64 = load i32, i32* %i 183 | %LoadInst65 = load i32, i32* %B_N 184 | %13 = mul i32 %LoadInst64, %LoadInst65 185 | %LoadInst66 = load i32, i32* %j 186 | %14 = add i32 %13, %LoadInst66 187 | %tmpvar67 = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %14 188 | %LoadInst68 = load i32, i32* %i 189 | %LoadInst69 = load i32, i32* %B_N 190 | %15 = mul i32 %LoadInst68, %LoadInst69 191 | %LoadInst70 = load i32, i32* %j 192 | %16 = add i32 %15, %LoadInst70 193 | %tmparray = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %16 194 | %tmpvar71 = load i32, i32* %tmparray 195 | %LoadInst72 = load i32, i32* %i 196 | %LoadInst73 = load i32, i32* %A_N 197 | %17 = mul i32 %LoadInst72, %LoadInst73 198 | %LoadInst74 = load i32, i32* %k 199 | %18 = add i32 %17, %LoadInst74 200 | %tmparray75 = getelementptr inbounds [200000 x i32], [200000 x i32]* @A, i32 0, i32 %18 201 | %tmpvar76 = load i32, i32* %tmparray75 202 | %LoadInst77 = load i32, i32* %k 203 | %LoadInst78 = load i32, i32* %B_N 204 | %19 = mul i32 %LoadInst77, %LoadInst78 205 | %LoadInst79 = load i32, i32* %j 206 | %20 = add i32 %19, %LoadInst79 207 | %tmparray80 = getelementptr inbounds [200000 x i32], [200000 x i32]* @B, i32 0, i32 %20 208 | %tmpvar81 = load i32, i32* %tmparray80 209 | %21 = mul i32 %tmpvar76, %tmpvar81 210 | %22 = add i32 %tmpvar71, %21 211 | store i32 %22, i32* %tmpvar67 212 | %LoadInst82 = load i32, i32* %k 213 | %23 = add i32 %LoadInst82, 1 214 | store i32 %23, i32* %k 215 | br label %cond57 216 | 217 | afterLoop59: ; preds = %cond57 218 | %LoadInst83 = load i32, i32* %j 219 | %24 = add i32 %LoadInst83, 1 220 | store i32 %24, i32* %j 221 | br label %cond47 222 | 223 | cond85: ; preds = %afterLoop94, %afterLoop42 224 | %LoadInst88 = load i32, i32* %i 225 | %LoadInst89 = load i32, i32* %A_M 226 | %icmptmp90 = icmp slt i32 %LoadInst88, %LoadInst89 227 | %whileCond91 = icmp ne i1 %icmptmp90, false 228 | br i1 %whileCond91, label %loop86, label %afterLoop87 229 | 230 | loop86: ; preds = %cond85 231 | store i32 0, i32* %j 232 | br label %cond92 233 | 234 | afterLoop87: ; preds = %cond85 235 | store i32 0, i32* %0 236 | br label %return 237 | 238 | cond92: ; preds = %loop93, %loop86 239 | %LoadInst95 = load i32, i32* %j 240 | %LoadInst96 = load i32, i32* %B_N 241 | %icmptmp97 = icmp slt i32 %LoadInst95, %LoadInst96 242 | %whileCond98 = icmp ne i1 %icmptmp97, false 243 | br i1 %whileCond98, label %loop93, label %afterLoop94 244 | 245 | loop93: ; preds = %cond92 246 | %LoadInst99 = load i32, i32* %i 247 | %LoadInst100 = load i32, i32* %B_N 248 | %25 = mul i32 %LoadInst99, %LoadInst100 249 | %LoadInst101 = load i32, i32* %j 250 | %26 = add i32 %25, %LoadInst101 251 | %tmparray102 = getelementptr inbounds [200000 x i32], [200000 x i32]* @C, i32 0, i32 %26 252 | %tmpvar103 = load i32, i32* %tmparray102 253 | %printf104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.5, i32 0, i32 0), i32 %tmpvar103) 254 | %LoadInst105 = load i32, i32* %j 255 | %27 = add i32 %LoadInst105, 1 256 | store i32 %27, i32* %j 257 | br label %cond92 258 | 259 | afterLoop94: ; preds = %cond92 260 | %printf106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @_Const_String_.6, i32 0, i32 0)) 261 | %LoadInst107 = load i32, i32* %i 262 | %28 = add i32 %LoadInst107, 1 263 | store i32 %28, i32* %i 264 | br label %cond85 265 | } -------------------------------------------------------------------------------- /wez_room/multi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/multi.o -------------------------------------------------------------------------------- /wez_room/multi.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl main # -- Begin function main 4 | .p2align 4, 0x90 5 | .type main,@function 6 | main: # @main 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | pushq %rbp 10 | .cfi_def_cfa_offset 16 11 | .cfi_offset %rbp, -16 12 | movq %rsp, %rbp 13 | .cfi_def_cfa_register %rbp 14 | subq $32, %rsp 15 | leaq -20(%rbp), %rsi 16 | leaq -16(%rbp), %rdx 17 | movl $.L_Const_String_, %edi 18 | xorl %eax, %eax 19 | callq scanf 20 | movl $0, -8(%rbp) 21 | movl $0, -4(%rbp) 22 | jmp .LBB0_1 23 | .p2align 4, 0x90 24 | .LBB0_5: # %afterLoop4 25 | # in Loop: Header=BB0_1 Depth=1 26 | incl -8(%rbp) 27 | .LBB0_1: # %cond 28 | # =>This Loop Header: Depth=1 29 | # Child Loop BB0_3 Depth 2 30 | movl -8(%rbp), %eax 31 | cmpl -20(%rbp), %eax 32 | jge .LBB0_6 33 | # %bb.2: # %loop 34 | # in Loop: Header=BB0_1 Depth=1 35 | movl $0, -4(%rbp) 36 | .p2align 4, 0x90 37 | .LBB0_3: # %cond2 38 | # Parent Loop BB0_1 Depth=1 39 | # => This Inner Loop Header: Depth=2 40 | movl -4(%rbp), %eax 41 | cmpl -16(%rbp), %eax 42 | jge .LBB0_5 43 | # %bb.4: # %loop3 44 | # in Loop: Header=BB0_3 Depth=2 45 | movl -8(%rbp), %eax 46 | imull -16(%rbp), %eax 47 | addl -4(%rbp), %eax 48 | cltq 49 | leaq .LA(,%rax,4), %rsi 50 | movl $.L_Const_String_.1, %edi 51 | xorl %eax, %eax 52 | callq scanf 53 | incl -4(%rbp) 54 | jmp .LBB0_3 55 | .LBB0_6: # %afterLoop 56 | leaq -24(%rbp), %rsi 57 | leaq -12(%rbp), %rdx 58 | movl $.L_Const_String_.2, %edi 59 | xorl %eax, %eax 60 | callq scanf 61 | movl $0, -8(%rbp) 62 | movl $0, -4(%rbp) 63 | jmp .LBB0_7 64 | .p2align 4, 0x90 65 | .LBB0_11: # %afterLoop25 66 | # in Loop: Header=BB0_7 Depth=1 67 | incl -8(%rbp) 68 | .LBB0_7: # %cond16 69 | # =>This Loop Header: Depth=1 70 | # Child Loop BB0_9 Depth 2 71 | movl -8(%rbp), %eax 72 | cmpl -24(%rbp), %eax 73 | jge .LBB0_12 74 | # %bb.8: # %loop17 75 | # in Loop: Header=BB0_7 Depth=1 76 | movl $0, -4(%rbp) 77 | .p2align 4, 0x90 78 | .LBB0_9: # %cond23 79 | # Parent Loop BB0_7 Depth=1 80 | # => This Inner Loop Header: Depth=2 81 | movl -4(%rbp), %eax 82 | cmpl -12(%rbp), %eax 83 | jge .LBB0_11 84 | # %bb.10: # %loop24 85 | # in Loop: Header=BB0_9 Depth=2 86 | movl -8(%rbp), %eax 87 | imull -12(%rbp), %eax 88 | addl -4(%rbp), %eax 89 | cltq 90 | leaq .LB(,%rax,4), %rsi 91 | movl $.L_Const_String_.3, %edi 92 | xorl %eax, %eax 93 | callq scanf 94 | incl -4(%rbp) 95 | jmp .LBB0_9 96 | .LBB0_12: # %afterLoop18 97 | movl -16(%rbp), %eax 98 | cmpl -24(%rbp), %eax 99 | je .LBB0_15 100 | # %bb.13: # %if 101 | movl $.L_Const_String_.4, %edi 102 | xorl %eax, %eax 103 | callq printf 104 | .LBB0_14: # %afterLoop87 105 | movl $0, -28(%rbp) 106 | movl -28(%rbp), %eax 107 | movq %rbp, %rsp 108 | popq %rbp 109 | .cfi_def_cfa %rsp, 8 110 | retq 111 | .LBB0_15: # %afterifonly 112 | .cfi_def_cfa %rbp, 16 113 | movl $0, -8(%rbp) 114 | movl $0, -4(%rbp) 115 | jmp .LBB0_16 116 | .p2align 4, 0x90 117 | .LBB0_29: # %afterLoop49 118 | # in Loop: Header=BB0_16 Depth=1 119 | incl -8(%rbp) 120 | .LBB0_16: # %cond40 121 | # =>This Loop Header: Depth=1 122 | # Child Loop BB0_18 Depth 2 123 | # Child Loop BB0_20 Depth 3 124 | movl -8(%rbp), %eax 125 | cmpl -20(%rbp), %eax 126 | jge .LBB0_23 127 | # %bb.17: # %loop41 128 | # in Loop: Header=BB0_16 Depth=1 129 | movl $0, -4(%rbp) 130 | jmp .LBB0_18 131 | .p2align 4, 0x90 132 | .LBB0_22: # %afterLoop59 133 | # in Loop: Header=BB0_18 Depth=2 134 | incl -4(%rbp) 135 | .LBB0_18: # %cond47 136 | # Parent Loop BB0_16 Depth=1 137 | # => This Loop Header: Depth=2 138 | # Child Loop BB0_20 Depth 3 139 | movl -4(%rbp), %eax 140 | cmpl -12(%rbp), %eax 141 | jge .LBB0_29 142 | # %bb.19: # %loop48 143 | # in Loop: Header=BB0_18 Depth=2 144 | movl -8(%rbp), %eax 145 | imull -12(%rbp), %eax 146 | addl -4(%rbp), %eax 147 | cltq 148 | movl $0, .LC(,%rax,4) 149 | movq %rsp, %rcx 150 | leaq -16(%rcx), %rax 151 | movq %rax, %rsp 152 | movl $0, -16(%rcx) 153 | .p2align 4, 0x90 154 | .LBB0_20: # %cond57 155 | # Parent Loop BB0_16 Depth=1 156 | # Parent Loop BB0_18 Depth=2 157 | # => This Inner Loop Header: Depth=3 158 | movl (%rax), %ecx 159 | cmpl -16(%rbp), %ecx 160 | jge .LBB0_22 161 | # %bb.21: # %loop58 162 | # in Loop: Header=BB0_20 Depth=3 163 | movl -8(%rbp), %ecx 164 | movl -12(%rbp), %edx 165 | movl %ecx, %esi 166 | imull %edx, %esi 167 | movl -4(%rbp), %edi 168 | addl %edi, %esi 169 | movslq %esi, %r8 170 | imull -16(%rbp), %ecx 171 | movl (%rax), %esi 172 | addl %esi, %ecx 173 | movslq %ecx, %rcx 174 | movl .LA(,%rcx,4), %ecx 175 | imull %edx, %esi 176 | addl %edi, %esi 177 | movslq %esi, %rdx 178 | imull .LB(,%rdx,4), %ecx 179 | addl %ecx, .LC(,%r8,4) 180 | incl (%rax) 181 | jmp .LBB0_20 182 | .LBB0_23: # %afterLoop42 183 | movl $0, -8(%rbp) 184 | jmp .LBB0_24 185 | .p2align 4, 0x90 186 | .LBB0_28: # %afterLoop94 187 | # in Loop: Header=BB0_24 Depth=1 188 | movl $.L_Const_String_.6, %edi 189 | xorl %eax, %eax 190 | callq printf 191 | incl -8(%rbp) 192 | .LBB0_24: # %cond85 193 | # =>This Loop Header: Depth=1 194 | # Child Loop BB0_26 Depth 2 195 | movl -8(%rbp), %eax 196 | cmpl -20(%rbp), %eax 197 | jge .LBB0_14 198 | # %bb.25: # %loop86 199 | # in Loop: Header=BB0_24 Depth=1 200 | movl $0, -4(%rbp) 201 | .p2align 4, 0x90 202 | .LBB0_26: # %cond92 203 | # Parent Loop BB0_24 Depth=1 204 | # => This Inner Loop Header: Depth=2 205 | movl -4(%rbp), %eax 206 | cmpl -12(%rbp), %eax 207 | jge .LBB0_28 208 | # %bb.27: # %loop93 209 | # in Loop: Header=BB0_26 Depth=2 210 | movl -8(%rbp), %eax 211 | imull -12(%rbp), %eax 212 | addl -4(%rbp), %eax 213 | cltq 214 | movl .LC(,%rax,4), %esi 215 | movl $.L_Const_String_.5, %edi 216 | xorl %eax, %eax 217 | callq printf 218 | incl -4(%rbp) 219 | jmp .LBB0_26 220 | .Lfunc_end0: 221 | .size main, .Lfunc_end0-main 222 | .cfi_endproc 223 | # -- End function 224 | .type .LA,@object # @A 225 | .local .LA 226 | .comm .LA,800000,16 227 | .type .LB,@object # @B 228 | .local .LB 229 | .comm .LB,800000,16 230 | .type .LC,@object # @C 231 | .local .LC 232 | .comm .LC,800000,16 233 | .type .L_Const_String_,@object # @_Const_String_ 234 | .section .rodata,"a",@progbits 235 | .L_Const_String_: 236 | .asciz "%d %d" 237 | .size .L_Const_String_, 6 238 | 239 | .type .L_Const_String_.1,@object # @_Const_String_.1 240 | .L_Const_String_.1: 241 | .asciz "%d" 242 | .size .L_Const_String_.1, 3 243 | 244 | .type .L_Const_String_.2,@object # @_Const_String_.2 245 | .L_Const_String_.2: 246 | .asciz "%d %d" 247 | .size .L_Const_String_.2, 6 248 | 249 | .type .L_Const_String_.3,@object # @_Const_String_.3 250 | .L_Const_String_.3: 251 | .asciz "%d" 252 | .size .L_Const_String_.3, 3 253 | 254 | .type .L_Const_String_.4,@object # @_Const_String_.4 255 | .p2align 4 256 | .L_Const_String_.4: 257 | .asciz "Incompatible Dimensions\n" 258 | .size .L_Const_String_.4, 25 259 | 260 | .type .L_Const_String_.5,@object # @_Const_String_.5 261 | .L_Const_String_.5: 262 | .asciz "%d " 263 | .size .L_Const_String_.5, 4 264 | 265 | .type .L_Const_String_.6,@object # @_Const_String_.6 266 | .L_Const_String_.6: 267 | .asciz "\n" 268 | .size .L_Const_String_.6, 2 269 | 270 | .section ".note.GNU-stack","",@progbits 271 | -------------------------------------------------------------------------------- /wez_room/print: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/print -------------------------------------------------------------------------------- /wez_room/print.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/print.bc -------------------------------------------------------------------------------- /wez_room/print.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [8 x i8] c"i : %d\0A\00" 5 | 6 | declare i32 @printf(i8*, ...) 7 | 8 | declare i32 @scanf(...) 9 | 10 | define i32 @main() { 11 | entry: 12 | %i = alloca i32 13 | store i32 0, i32* %i 14 | %LoadInst = load i32, i32* %i 15 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @_Const_String_, i32 0, i32 0), i32 %LoadInst) 16 | %LoadInst1 = load i32, i32* %i 17 | ret i32 %LoadInst1 18 | } -------------------------------------------------------------------------------- /wez_room/print.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/print.o -------------------------------------------------------------------------------- /wez_room/print.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl main # -- Begin function main 4 | .p2align 4, 0x90 5 | .type main,@function 6 | main: # @main 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | pushq %rax 10 | .cfi_def_cfa_offset 16 11 | movl $10, 4(%rsp) 12 | movl $.L_Const_String_, %edi 13 | movl $10, %esi 14 | xorl %eax, %eax 15 | callq printf 16 | movl 4(%rsp), %eax 17 | popq %rcx 18 | .cfi_def_cfa_offset 8 19 | retq 20 | .Lfunc_end0: 21 | .size main, .Lfunc_end0-main 22 | .cfi_endproc 23 | # -- End function 24 | .type .L_Const_String_,@object # @_Const_String_ 25 | .section .rodata,"a",@progbits 26 | .L_Const_String_: 27 | .asciz "%d\\n" 28 | .size .L_Const_String_, 5 29 | 30 | .section ".note.GNU-stack","",@progbits 31 | -------------------------------------------------------------------------------- /wez_room/quicksort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/quicksort -------------------------------------------------------------------------------- /wez_room/quicksort.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/quicksort.bc -------------------------------------------------------------------------------- /wez_room/quicksort.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [3 x i8] c"%d\00" 5 | @_Const_String_.1 = private constant [3 x i8] c"%d\00" 6 | @_Const_String_.2 = private constant [4 x i8] c"%d\0A\00" 7 | 8 | declare i32 @printf(i8*, ...) 9 | 10 | declare i32 @scanf(...) 11 | 12 | define void @quicksort(i32* %A1, i32 %left2, i32 %right3) { 13 | entry: 14 | %A = alloca i32* 15 | store i32* %A1, i32** %A 16 | %left = alloca i32 17 | store i32 %left2, i32* %left 18 | %right = alloca i32 19 | store i32 %right3, i32* %right 20 | %i = alloca i32 21 | %j = alloca i32 22 | %x = alloca i32 23 | %y = alloca i32 24 | %LoadInst = load i32, i32* %left 25 | store i32 %LoadInst, i32* %i 26 | %LoadInst4 = load i32, i32* %right 27 | store i32 %LoadInst4, i32* %j 28 | %LoadInst5 = load i32, i32* %left 29 | %LoadInst6 = load i32, i32* %right 30 | %0 = add i32 %LoadInst5, %LoadInst6 31 | %1 = sdiv i32 %0, 2 32 | %2 = load i32*, i32** %A 33 | %tmparray = getelementptr inbounds i32, i32* %2, i32 %1 34 | %tmpvar = load i32, i32* %tmparray 35 | store i32 %tmpvar, i32* %x 36 | br label %cond 37 | 38 | return: ; preds = %afterifonly55 39 | ret void 40 | 41 | cond: ; preds = %afterifonly, %entry 42 | %LoadInst7 = load i32, i32* %i 43 | %LoadInst8 = load i32, i32* %j 44 | %icmptmp = icmp sle i32 %LoadInst7, %LoadInst8 45 | %whileCond = icmp ne i1 %icmptmp, false 46 | br i1 %whileCond, label %loop, label %afterLoop 47 | 48 | loop: ; preds = %cond 49 | br label %cond9 50 | 51 | afterLoop: ; preds = %cond 52 | %LoadInst47 = load i32, i32* %left 53 | %LoadInst48 = load i32, i32* %j 54 | %icmptmp49 = icmp slt i32 %LoadInst47, %LoadInst48 55 | %ifCond50 = icmp ne i1 %icmptmp49, false 56 | br i1 %ifCond50, label %if45, label %afterifonly46 57 | 58 | cond9: ; preds = %loop10, %loop 59 | %LoadInst12 = load i32, i32* %i 60 | %3 = load i32*, i32** %A 61 | %tmparray13 = getelementptr inbounds i32, i32* %3, i32 %LoadInst12 62 | %tmpvar14 = load i32, i32* %tmparray13 63 | %LoadInst15 = load i32, i32* %x 64 | %icmptmp16 = icmp slt i32 %tmpvar14, %LoadInst15 65 | %whileCond17 = icmp ne i1 %icmptmp16, false 66 | br i1 %whileCond17, label %loop10, label %afterLoop11 67 | 68 | loop10: ; preds = %cond9 69 | %LoadInst18 = load i32, i32* %i 70 | %4 = add i32 %LoadInst18, 1 71 | store i32 %4, i32* %i 72 | br label %cond9 73 | 74 | afterLoop11: ; preds = %cond9 75 | br label %cond19 76 | 77 | cond19: ; preds = %loop20, %afterLoop11 78 | %LoadInst22 = load i32, i32* %x 79 | %LoadInst23 = load i32, i32* %j 80 | %5 = load i32*, i32** %A 81 | %tmparray24 = getelementptr inbounds i32, i32* %5, i32 %LoadInst23 82 | %tmpvar25 = load i32, i32* %tmparray24 83 | %icmptmp26 = icmp slt i32 %LoadInst22, %tmpvar25 84 | %whileCond27 = icmp ne i1 %icmptmp26, false 85 | br i1 %whileCond27, label %loop20, label %afterLoop21 86 | 87 | loop20: ; preds = %cond19 88 | %LoadInst28 = load i32, i32* %j 89 | %6 = sub i32 %LoadInst28, 1 90 | store i32 %6, i32* %j 91 | br label %cond19 92 | 93 | afterLoop21: ; preds = %cond19 94 | %LoadInst29 = load i32, i32* %i 95 | %LoadInst30 = load i32, i32* %j 96 | %icmptmp31 = icmp sle i32 %LoadInst29, %LoadInst30 97 | %ifCond = icmp ne i1 %icmptmp31, false 98 | br i1 %ifCond, label %if, label %afterifonly 99 | 100 | if: ; preds = %afterLoop21 101 | %LoadInst32 = load i32, i32* %i 102 | %7 = load i32*, i32** %A 103 | %tmparray33 = getelementptr inbounds i32, i32* %7, i32 %LoadInst32 104 | %tmpvar34 = load i32, i32* %tmparray33 105 | store i32 %tmpvar34, i32* %y 106 | %LoadInst35 = load i32, i32* %i 107 | %8 = load i32*, i32** %A 108 | %tmpvar36 = getelementptr inbounds i32, i32* %8, i32 %LoadInst35 109 | %LoadInst37 = load i32, i32* %j 110 | %9 = load i32*, i32** %A 111 | %tmparray38 = getelementptr inbounds i32, i32* %9, i32 %LoadInst37 112 | %tmpvar39 = load i32, i32* %tmparray38 113 | store i32 %tmpvar39, i32* %tmpvar36 114 | %LoadInst40 = load i32, i32* %j 115 | %10 = load i32*, i32** %A 116 | %tmpvar41 = getelementptr inbounds i32, i32* %10, i32 %LoadInst40 117 | %LoadInst42 = load i32, i32* %y 118 | store i32 %LoadInst42, i32* %tmpvar41 119 | %LoadInst43 = load i32, i32* %i 120 | %11 = add i32 %LoadInst43, 1 121 | store i32 %11, i32* %i 122 | %LoadInst44 = load i32, i32* %j 123 | %12 = sub i32 %LoadInst44, 1 124 | store i32 %12, i32* %j 125 | br label %afterifonly 126 | 127 | afterifonly: ; preds = %if, %afterLoop21 128 | br label %cond 129 | 130 | if45: ; preds = %afterLoop 131 | %LoadInst51 = load i32*, i32** %A 132 | %LoadInst52 = load i32, i32* %left 133 | %LoadInst53 = load i32, i32* %j 134 | call void @quicksort(i32* %LoadInst51, i32 %LoadInst52, i32 %LoadInst53) 135 | br label %afterifonly46 136 | 137 | afterifonly46: ; preds = %if45, %afterLoop 138 | %LoadInst56 = load i32, i32* %i 139 | %LoadInst57 = load i32, i32* %right 140 | %icmptmp58 = icmp slt i32 %LoadInst56, %LoadInst57 141 | %ifCond59 = icmp ne i1 %icmptmp58, false 142 | br i1 %ifCond59, label %if54, label %afterifonly55 143 | 144 | if54: ; preds = %afterifonly46 145 | %LoadInst60 = load i32*, i32** %A 146 | %LoadInst61 = load i32, i32* %i 147 | %LoadInst62 = load i32, i32* %right 148 | call void @quicksort(i32* %LoadInst60, i32 %LoadInst61, i32 %LoadInst62) 149 | br label %afterifonly55 150 | 151 | afterifonly55: ; preds = %if54, %afterifonly46 152 | br label %return 153 | } 154 | 155 | define i32 @main() { 156 | entry: 157 | %0 = alloca i32 158 | %B = alloca [1000000 x i32] 159 | %N = alloca i32 160 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_, i32 0, i32 0), i32* %N) 161 | %i = alloca i32 162 | store i32 0, i32* %i 163 | br label %cond 164 | 165 | return: ; preds = %afterLoop10 166 | %1 = load i32, i32* %0 167 | ret i32 %1 168 | 169 | cond: ; preds = %loop, %entry 170 | %LoadInst = load i32, i32* %i 171 | %LoadInst1 = load i32, i32* %N 172 | %icmptmp = icmp slt i32 %LoadInst, %LoadInst1 173 | %whileCond = icmp ne i1 %icmptmp, false 174 | br i1 %whileCond, label %loop, label %afterLoop 175 | 176 | loop: ; preds = %cond 177 | %LoadInst2 = load i32, i32* %i 178 | %elePtr = getelementptr inbounds [1000000 x i32], [1000000 x i32]* %B, i32 0, i32 %LoadInst2 179 | %scanf3 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.1, i32 0, i32 0), i32* %elePtr) 180 | %LoadInst4 = load i32, i32* %i 181 | %2 = add i32 %LoadInst4, 1 182 | store i32 %2, i32* %i 183 | br label %cond 184 | 185 | afterLoop: ; preds = %cond 186 | %left = alloca i32 187 | store i32 0, i32* %left 188 | %right = alloca i32 189 | %LoadInst5 = load i32, i32* %N 190 | %3 = sub i32 %LoadInst5, 1 191 | store i32 %3, i32* %right 192 | %arrayPtr = getelementptr inbounds [1000000 x i32], [1000000 x i32]* %B, i32 0, i32 0 193 | %LoadInst6 = load i32, i32* %left 194 | %LoadInst7 = load i32, i32* %right 195 | call void @quicksort(i32* %arrayPtr, i32 %LoadInst6, i32 %LoadInst7) 196 | store i32 0, i32* %i 197 | br label %cond8 198 | 199 | cond8: ; preds = %loop9, %afterLoop 200 | %LoadInst11 = load i32, i32* %i 201 | %LoadInst12 = load i32, i32* %N 202 | %icmptmp13 = icmp slt i32 %LoadInst11, %LoadInst12 203 | %whileCond14 = icmp ne i1 %icmptmp13, false 204 | br i1 %whileCond14, label %loop9, label %afterLoop10 205 | 206 | loop9: ; preds = %cond8 207 | %LoadInst15 = load i32, i32* %i 208 | %tmparray = getelementptr inbounds [1000000 x i32], [1000000 x i32]* %B, i32 0, i32 %LoadInst15 209 | %tmpvar = load i32, i32* %tmparray 210 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.2, i32 0, i32 0), i32 %tmpvar) 211 | %LoadInst16 = load i32, i32* %i 212 | %4 = add i32 %LoadInst16, 1 213 | store i32 %4, i32* %i 214 | br label %cond8 215 | 216 | afterLoop10: ; preds = %cond8 217 | store i32 0, i32* %0 218 | br label %return 219 | } -------------------------------------------------------------------------------- /wez_room/quicksort.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dontnet-wuenze/MiniC-Compiler/0eb5253b95a470ddb2c2d0da63b45da32aa3c126/wez_room/quicksort.o -------------------------------------------------------------------------------- /wez_room/quicksort.s: -------------------------------------------------------------------------------- 1 | .text 2 | .file "main" 3 | .globl quicksort # -- Begin function quicksort 4 | .p2align 4, 0x90 5 | .type quicksort,@function 6 | quicksort: # @quicksort 7 | .cfi_startproc 8 | # %bb.0: # %entry 9 | subq $40, %rsp 10 | .cfi_def_cfa_offset 48 11 | movq %rdi, 16(%rsp) 12 | movl %esi, 32(%rsp) 13 | movl %edx, 28(%rsp) 14 | movl %esi, 12(%rsp) 15 | movl %edx, 8(%rsp) 16 | addl %edx, %esi 17 | movl %esi, %eax 18 | shrl $31, %eax 19 | addl %esi, %eax 20 | sarl %eax 21 | cltq 22 | movl (%rdi,%rax,4), %eax 23 | movl %eax, 24(%rsp) 24 | .p2align 4, 0x90 25 | .LBB0_1: # %cond 26 | # =>This Loop Header: Depth=1 27 | # Child Loop BB0_7 Depth 2 28 | # Child Loop BB0_9 Depth 2 29 | movl 12(%rsp), %eax 30 | cmpl 8(%rsp), %eax 31 | jg .LBB0_2 32 | .p2align 4, 0x90 33 | .LBB0_7: # %cond9 34 | # Parent Loop BB0_1 Depth=1 35 | # => This Inner Loop Header: Depth=2 36 | movslq 12(%rsp), %rax 37 | movq 16(%rsp), %rcx 38 | movl (%rcx,%rax,4), %eax 39 | cmpl 24(%rsp), %eax 40 | jge .LBB0_9 41 | # %bb.8: # %loop10 42 | # in Loop: Header=BB0_7 Depth=2 43 | incl 12(%rsp) 44 | jmp .LBB0_7 45 | .p2align 4, 0x90 46 | .LBB0_10: # %loop20 47 | # in Loop: Header=BB0_9 Depth=2 48 | decl 8(%rsp) 49 | .LBB0_9: # %cond19 50 | # Parent Loop BB0_1 Depth=1 51 | # => This Inner Loop Header: Depth=2 52 | movl 24(%rsp), %eax 53 | movslq 8(%rsp), %rcx 54 | movq 16(%rsp), %rdx 55 | cmpl (%rdx,%rcx,4), %eax 56 | jl .LBB0_10 57 | # %bb.11: # %afterLoop21 58 | # in Loop: Header=BB0_1 Depth=1 59 | movl 12(%rsp), %eax 60 | cmpl 8(%rsp), %eax 61 | jg .LBB0_1 62 | # %bb.12: # %if 63 | # in Loop: Header=BB0_1 Depth=1 64 | movq 16(%rsp), %rax 65 | movslq 12(%rsp), %rcx 66 | movl (%rax,%rcx,4), %edx 67 | movl %edx, 36(%rsp) 68 | movslq 8(%rsp), %rdx 69 | movl (%rax,%rdx,4), %edx 70 | movl %edx, (%rax,%rcx,4) 71 | movslq 8(%rsp), %rax 72 | movq 16(%rsp), %rcx 73 | movl 36(%rsp), %edx 74 | movl %edx, (%rcx,%rax,4) 75 | incl 12(%rsp) 76 | decl 8(%rsp) 77 | jmp .LBB0_1 78 | .LBB0_2: # %afterLoop 79 | movl 32(%rsp), %eax 80 | cmpl 8(%rsp), %eax 81 | jge .LBB0_4 82 | # %bb.3: # %if45 83 | movq 16(%rsp), %rdi 84 | movl 32(%rsp), %esi 85 | movl 8(%rsp), %edx 86 | callq quicksort 87 | .LBB0_4: # %afterifonly46 88 | movl 12(%rsp), %eax 89 | cmpl 28(%rsp), %eax 90 | jge .LBB0_6 91 | # %bb.5: # %if54 92 | movq 16(%rsp), %rdi 93 | movl 12(%rsp), %esi 94 | movl 28(%rsp), %edx 95 | callq quicksort 96 | .LBB0_6: # %afterifonly55 97 | addq $40, %rsp 98 | .cfi_def_cfa_offset 8 99 | retq 100 | .Lfunc_end0: 101 | .size quicksort, .Lfunc_end0-quicksort 102 | .cfi_endproc 103 | # -- End function 104 | .globl main # -- Begin function main 105 | .p2align 4, 0x90 106 | .type main,@function 107 | main: # @main 108 | .cfi_startproc 109 | # %bb.0: # %entry 110 | pushq %rbp 111 | .cfi_def_cfa_offset 16 112 | .cfi_offset %rbp, -16 113 | movq %rsp, %rbp 114 | .cfi_def_cfa_register %rbp 115 | subq $4000016, %rsp # imm = 0x3D0910 116 | leaq -8(%rbp), %rsi 117 | movl $.L_Const_String_, %edi 118 | xorl %eax, %eax 119 | callq scanf 120 | movl $0, -4(%rbp) 121 | .p2align 4, 0x90 122 | .LBB1_1: # %cond 123 | # =>This Inner Loop Header: Depth=1 124 | movl -4(%rbp), %eax 125 | cmpl -8(%rbp), %eax 126 | jge .LBB1_3 127 | # %bb.2: # %loop 128 | # in Loop: Header=BB1_1 Depth=1 129 | movslq -4(%rbp), %rax 130 | leaq -4000012(%rbp,%rax,4), %rsi 131 | movl $.L_Const_String_.1, %edi 132 | xorl %eax, %eax 133 | callq scanf 134 | incl -4(%rbp) 135 | jmp .LBB1_1 136 | .LBB1_3: # %afterLoop 137 | movq %rsp, %rax 138 | leaq -16(%rax), %rsp 139 | movl $0, -16(%rax) 140 | movq %rsp, %rcx 141 | leaq -16(%rcx), %rsp 142 | movl -8(%rbp), %edx 143 | decl %edx 144 | movl %edx, -16(%rcx) 145 | movl -16(%rax), %esi 146 | leaq -4000012(%rbp), %rdi 147 | callq quicksort 148 | movl $0, -4(%rbp) 149 | .p2align 4, 0x90 150 | .LBB1_4: # %cond8 151 | # =>This Inner Loop Header: Depth=1 152 | movl -4(%rbp), %eax 153 | cmpl -8(%rbp), %eax 154 | jge .LBB1_6 155 | # %bb.5: # %loop9 156 | # in Loop: Header=BB1_4 Depth=1 157 | movslq -4(%rbp), %rax 158 | movl -4000012(%rbp,%rax,4), %esi 159 | movl $.L_Const_String_.2, %edi 160 | xorl %eax, %eax 161 | callq printf 162 | incl -4(%rbp) 163 | jmp .LBB1_4 164 | .LBB1_6: # %afterLoop10 165 | movl $0, -12(%rbp) 166 | xorl %eax, %eax 167 | movq %rbp, %rsp 168 | popq %rbp 169 | .cfi_def_cfa %rsp, 8 170 | retq 171 | .Lfunc_end1: 172 | .size main, .Lfunc_end1-main 173 | .cfi_endproc 174 | # -- End function 175 | .type .L_Const_String_,@object # @_Const_String_ 176 | .section .rodata,"a",@progbits 177 | .L_Const_String_: 178 | .asciz "%d" 179 | .size .L_Const_String_, 3 180 | 181 | .type .L_Const_String_.1,@object # @_Const_String_.1 182 | .L_Const_String_.1: 183 | .asciz "%d" 184 | .size .L_Const_String_.1, 3 185 | 186 | .type .L_Const_String_.2,@object # @_Const_String_.2 187 | .L_Const_String_.2: 188 | .asciz "%d\n" 189 | .size .L_Const_String_.2, 4 190 | 191 | .section ".note.GNU-stack","",@progbits 192 | -------------------------------------------------------------------------------- /wez_room/quicksortnb.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [3 x i8] c"%d\00" 5 | @_Const_String_.1 = private constant [3 x i8] c"%d\00" 6 | @_Const_String_.2 = private constant [4 x i8] c"%d \00" 7 | 8 | declare i32 @printf(i8*, ...) 9 | 10 | declare i32 @scanf(...) 11 | 12 | define void @quicksort(i32* %A1, i32 %left2, i32 %right3) { 13 | entry: 14 | %A = alloca i32* 15 | store i32* %A1, i32** %A 16 | %left = alloca i32 17 | store i32 %left2, i32* %left 18 | %right = alloca i32 19 | store i32 %right3, i32* %right 20 | %i = alloca i32 21 | %j = alloca i32 22 | %x = alloca i32 23 | %y = alloca i32 24 | %LoadInst = load i32, i32* %left 25 | store i32 %LoadInst, i32* %i 26 | %LoadInst4 = load i32, i32* %right 27 | store i32 %LoadInst4, i32* %j 28 | %LoadInst5 = load i32, i32* %left 29 | %LoadInst6 = load i32, i32* %right 30 | %0 = add i32 %LoadInst5, %LoadInst6 31 | %1 = sdiv i32 %0, 2 32 | %2 = load i32*, i32** %A 33 | %tmparray = getelementptr inbounds i32, i32* %2, i32 %1 34 | %tmpvar = load i32, i32* %tmparray 35 | store i32 %tmpvar, i32* %x 36 | br label %cond 37 | 38 | return: ; preds = %afterifonly55 39 | ret void 40 | 41 | cond: ; preds = %afterifonly, %entry 42 | %LoadInst7 = load i32, i32* %i 43 | %LoadInst8 = load i32, i32* %j 44 | %icmptmp = icmp sle i32 %LoadInst7, %LoadInst8 45 | %whileCond = icmp ne i1 %icmptmp, false 46 | br i1 %whileCond, label %loop, label %afterLoop 47 | 48 | loop: ; preds = %cond 49 | br label %cond9 50 | 51 | afterLoop: ; preds = %cond 52 | %LoadInst47 = load i32, i32* %left 53 | %LoadInst48 = load i32, i32* %j 54 | %icmptmp49 = icmp slt i32 %LoadInst47, %LoadInst48 55 | %ifCond50 = icmp ne i1 %icmptmp49, false 56 | br i1 %ifCond50, label %if45, label %afterifonly46 57 | 58 | cond9: ; preds = %loop10, %loop 59 | %LoadInst12 = load i32, i32* %i 60 | %3 = load i32*, i32** %A 61 | %tmparray13 = getelementptr inbounds i32, i32* %3, i32 %LoadInst12 62 | %tmpvar14 = load i32, i32* %tmparray13 63 | %LoadInst15 = load i32, i32* %x 64 | %icmptmp16 = icmp slt i32 %tmpvar14, %LoadInst15 65 | %whileCond17 = icmp ne i1 %icmptmp16, false 66 | br i1 %whileCond17, label %loop10, label %afterLoop11 67 | 68 | loop10: ; preds = %cond9 69 | %LoadInst18 = load i32, i32* %i 70 | %4 = add i32 %LoadInst18, 1 71 | store i32 %4, i32* %i 72 | br label %cond9 73 | 74 | afterLoop11: ; preds = %cond9 75 | br label %cond19 76 | 77 | cond19: ; preds = %loop20, %afterLoop11 78 | %LoadInst22 = load i32, i32* %x 79 | %LoadInst23 = load i32, i32* %j 80 | %5 = load i32*, i32** %A 81 | %tmparray24 = getelementptr inbounds i32, i32* %5, i32 %LoadInst23 82 | %tmpvar25 = load i32, i32* %tmparray24 83 | %icmptmp26 = icmp slt i32 %LoadInst22, %tmpvar25 84 | %whileCond27 = icmp ne i1 %icmptmp26, false 85 | br i1 %whileCond27, label %loop20, label %afterLoop21 86 | 87 | loop20: ; preds = %cond19 88 | %LoadInst28 = load i32, i32* %j 89 | %6 = sub i32 %LoadInst28, 1 90 | store i32 %6, i32* %j 91 | br label %cond19 92 | 93 | afterLoop21: ; preds = %cond19 94 | %LoadInst29 = load i32, i32* %i 95 | %LoadInst30 = load i32, i32* %j 96 | %icmptmp31 = icmp sle i32 %LoadInst29, %LoadInst30 97 | %ifCond = icmp ne i1 %icmptmp31, false 98 | br i1 %ifCond, label %if, label %afterifonly 99 | 100 | if: ; preds = %afterLoop21 101 | %LoadInst32 = load i32, i32* %i 102 | %7 = load i32*, i32** %A 103 | %tmparray33 = getelementptr inbounds i32, i32* %7, i32 %LoadInst32 104 | %tmpvar34 = load i32, i32* %tmparray33 105 | store i32 %tmpvar34, i32* %y 106 | %LoadInst35 = load i32, i32* %i 107 | %8 = load i32*, i32** %A 108 | %tmpvar36 = getelementptr inbounds i32, i32* %8, i32 %LoadInst35 109 | %LoadInst37 = load i32, i32* %j 110 | %9 = load i32*, i32** %A 111 | %tmparray38 = getelementptr inbounds i32, i32* %9, i32 %LoadInst37 112 | %tmpvar39 = load i32, i32* %tmparray38 113 | store i32 %tmpvar39, i32* %tmpvar36 114 | %LoadInst40 = load i32, i32* %j 115 | %10 = load i32*, i32** %A 116 | %tmpvar41 = getelementptr inbounds i32, i32* %10, i32 %LoadInst40 117 | %LoadInst42 = load i32, i32* %y 118 | store i32 %LoadInst42, i32* %tmpvar41 119 | %LoadInst43 = load i32, i32* %i 120 | %11 = add i32 %LoadInst43, 1 121 | store i32 %11, i32* %i 122 | %LoadInst44 = load i32, i32* %j 123 | %12 = sub i32 %LoadInst44, 1 124 | store i32 %12, i32* %j 125 | br label %afterifonly 126 | 127 | afterifonly: ; preds = %if, %afterLoop21 128 | br label %cond 129 | 130 | if45: ; preds = %afterLoop 131 | %LoadInst51 = load i32*, i32** %A 132 | %LoadInst52 = load i32, i32* %left 133 | %LoadInst53 = load i32, i32* %j 134 | call void @quicksort(i32* %LoadInst51, i32 %LoadInst52, i32 %LoadInst53) 135 | br label %afterifonly46 136 | 137 | afterifonly46: ; preds = %if45, %afterLoop 138 | %LoadInst56 = load i32, i32* %i 139 | %LoadInst57 = load i32, i32* %right 140 | %icmptmp58 = icmp slt i32 %LoadInst56, %LoadInst57 141 | %ifCond59 = icmp ne i1 %icmptmp58, false 142 | br i1 %ifCond59, label %if54, label %afterifonly55 143 | 144 | if54: ; preds = %afterifonly46 145 | %LoadInst60 = load i32*, i32** %A 146 | %LoadInst61 = load i32, i32* %i 147 | %LoadInst62 = load i32, i32* %right 148 | call void @quicksort(i32* %LoadInst60, i32 %LoadInst61, i32 %LoadInst62) 149 | br label %afterifonly55 150 | 151 | afterifonly55: ; preds = %if54, %afterifonly46 152 | br label %return 153 | } 154 | 155 | define i32 @main() { 156 | entry: 157 | %0 = alloca i32 158 | %B = alloca [10 x i32] 159 | %N = alloca i32 160 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_, i32 0, i32 0), i32* %N) 161 | %i = alloca i32 162 | store i32 0, i32* %i 163 | br label %cond 164 | 165 | return: ; preds = %afterLoop10 166 | %1 = load i32, i32* %0 167 | ret i32 %1 168 | 169 | cond: ; preds = %loop, %entry 170 | %LoadInst = load i32, i32* %i 171 | %LoadInst1 = load i32, i32* %N 172 | %icmptmp = icmp slt i32 %LoadInst, %LoadInst1 173 | %whileCond = icmp ne i1 %icmptmp, false 174 | br i1 %whileCond, label %loop, label %afterLoop 175 | 176 | loop: ; preds = %cond 177 | %LoadInst2 = load i32, i32* %i 178 | %elePtr = getelementptr inbounds [10 x i32], [10 x i32]* %B, i32 0, i32 %LoadInst2 179 | %scanf3 = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_.1, i32 0, i32 0), i32* %elePtr) 180 | %LoadInst4 = load i32, i32* %i 181 | %2 = add i32 %LoadInst4, 1 182 | store i32 %2, i32* %i 183 | br label %cond 184 | 185 | afterLoop: ; preds = %cond 186 | %left = alloca i32 187 | store i32 0, i32* %left 188 | %right = alloca i32 189 | %LoadInst5 = load i32, i32* %N 190 | %3 = sub i32 %LoadInst5, 1 191 | store i32 %3, i32* %right 192 | %arrayPtr = getelementptr inbounds [10 x i32], [10 x i32]* %B, i32 0, i32 0 193 | %LoadInst6 = load i32, i32* %left 194 | %LoadInst7 = load i32, i32* %right 195 | call void @quicksort(i32* %arrayPtr, i32 %LoadInst6, i32 %LoadInst7) 196 | store i32 0, i32* %i 197 | br label %cond8 198 | 199 | cond8: ; preds = %loop9, %afterLoop 200 | %LoadInst11 = load i32, i32* %i 201 | %LoadInst12 = load i32, i32* %N 202 | %icmptmp13 = icmp slt i32 %LoadInst11, %LoadInst12 203 | %whileCond14 = icmp ne i1 %icmptmp13, false 204 | br i1 %whileCond14, label %loop9, label %afterLoop10 205 | 206 | loop9: ; preds = %cond8 207 | %LoadInst15 = load i32, i32* %i 208 | %tmparray = getelementptr inbounds [10 x i32], [10 x i32]* %B, i32 0, i32 %LoadInst15 209 | %tmpvar = load i32, i32* %tmparray 210 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.2, i32 0, i32 0), i32 %tmpvar) 211 | %LoadInst16 = load i32, i32* %i 212 | %4 = add i32 %LoadInst16, 1 213 | store i32 %4, i32* %i 214 | br label %cond8 215 | 216 | afterLoop10: ; preds = %cond8 217 | store i32 0, i32* %0 218 | br label %return 219 | } -------------------------------------------------------------------------------- /wez_room/scanf.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [6 x i8] c"%d %d\00" 5 | @_Const_String_.1 = private constant [7 x i8] c"%d %d\0A\00" 6 | 7 | declare i32 @printf(i8*, ...) 8 | 9 | declare i32 @scanf(...) 10 | 11 | define i32 @main() { 12 | entry: 13 | %b = alloca i32 14 | %a = alloca [10 x i32] 15 | %LoadInst = load [10 x i32], [10 x i32]* %a 16 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @_Const_String_, i32 0, i32 0), [10 x i32] %LoadInst, i32* %b) 17 | %tmparray = getelementptr inbounds [10 x i32], [10 x i32]* %a, i32 0, i32 0 18 | %tmpvar = load i32, i32* %tmparray 19 | %LoadInst1 = load i32, i32* %b 20 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @_Const_String_.1, i32 0, i32 0), i32 %tmpvar, i32 %LoadInst1) 21 | ret i32 0 22 | } -------------------------------------------------------------------------------- /wez_room/string.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [3 x i8] c"%s\00" 5 | @_Const_String_.1 = private constant [4 x i8] c"%s\0A\00" 6 | @_Const_String_.2 = private constant [4 x i8] c"%d\0A\00" 7 | @_Const_String_.3 = private constant [9 x i8] c"success\0A\00" 8 | 9 | declare i32 @printf(i8*, ...) 10 | 11 | declare i32 @scanf(...) 12 | 13 | define i32 @main() { 14 | entry: 15 | %0 = alloca i32 16 | %c = alloca i8 17 | store i8 99, i8* %c 18 | %d = alloca i8 19 | store i8 68, i8* %d 20 | %b = alloca [10 x i8] 21 | br label %cond 22 | 23 | return: ; preds = %afterLoop 24 | %1 = load i32, i32* %0 25 | ret i32 %1 26 | 27 | cond: ; preds = %afterifonly, %entry 28 | br i1 true, label %loop, label %afterLoop 29 | 30 | loop: ; preds = %cond 31 | %arrayPtr = getelementptr inbounds [10 x i8], [10 x i8]* %b, i32 0, i32 0 32 | %scanf = call i32 (...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_Const_String_, i32 0, i32 0), i8* %arrayPtr) 33 | %arrayPtr1 = getelementptr inbounds [10 x i8], [10 x i8]* %b, i32 0, i32 0 34 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.1, i32 0, i32 0), i8* %arrayPtr1) 35 | %tmparray = getelementptr inbounds [10 x i8], [10 x i8]* %b, i32 0, i32 0 36 | %tmpvar = load i8, i8* %tmparray 37 | %printf2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @_Const_String_.2, i32 0, i32 0), i8 %tmpvar) 38 | %tmparray3 = getelementptr inbounds [10 x i8], [10 x i8]* %b, i32 0, i32 0 39 | %tmpvar4 = load i8, i8* %tmparray3 40 | %icmptmp = icmp eq i8 %tmpvar4, 101 41 | %ifCond = icmp ne i1 %icmptmp, false 42 | br i1 %ifCond, label %if, label %afterifonly 43 | 44 | afterLoop: ; preds = %if, %cond 45 | %printf5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @_Const_String_.3, i32 0, i32 0)) 46 | store i32 0, i32* %0 47 | br label %return 48 | 49 | if: ; preds = %loop 50 | br label %afterLoop 51 | 52 | afterifonly: ; preds = %loop 53 | br label %cond 54 | } -------------------------------------------------------------------------------- /wez_room/while.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'main' 2 | source_filename = "main" 3 | 4 | @_Const_String_ = private constant [33 x i8] c"localsum : %d, i : %d, cnt : %d\0A\00" 5 | 6 | declare i32 @printf(i8*, ...) 7 | 8 | declare i32 @scanf(...) 9 | 10 | define i32 @main() { 11 | entry: 12 | %localsum = alloca i32 13 | store i32 5, i32* %localsum 14 | %i = alloca i32 15 | store i32 2, i32* %i 16 | %cnt = alloca i32 17 | store i32 0, i32* %cnt 18 | br label %cond 19 | 20 | cond: ; preds = %afterLoop4, %entry 21 | %LoadInst = load i32, i32* %cnt 22 | %icmptmp = icmp slt i32 %LoadInst, 3 23 | %whileCond = icmp ne i1 %icmptmp, false 24 | br i1 %whileCond, label %loop, label %afterLoop 25 | 26 | loop: ; preds = %cond 27 | %LoadInst1 = load i32, i32* %cnt 28 | %0 = add i32 %LoadInst1, 1 29 | store i32 %0, i32* %cnt 30 | %j = alloca i32 31 | store i32 0, i32* %j 32 | br label %cond2 33 | 34 | afterLoop: ; preds = %cond 35 | %LoadInst10 = load i32, i32* %i 36 | %LoadInst11 = load i32, i32* %localsum 37 | %1 = add i32 %LoadInst10, %LoadInst11 38 | %2 = add i32 %1, 10 39 | store i32 %2, i32* %i 40 | %LoadInst12 = load i32, i32* %localsum 41 | %LoadInst13 = load i32, i32* %i 42 | %LoadInst14 = load i32, i32* %cnt 43 | %printf = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @_Const_String_, i32 0, i32 0), i32 %LoadInst12, i32 %LoadInst13, i32 %LoadInst14) 44 | ret i32 0 45 | 46 | cond2: ; preds = %loop3, %loop 47 | %LoadInst5 = load i32, i32* %j 48 | %icmptmp6 = icmp slt i32 %LoadInst5, 5 49 | %whileCond7 = icmp ne i1 %icmptmp6, false 50 | br i1 %whileCond7, label %loop3, label %afterLoop4 51 | 52 | loop3: ; preds = %cond2 53 | %LoadInst8 = load i32, i32* %localsum 54 | %3 = add i32 %LoadInst8, 1 55 | store i32 %3, i32* %localsum 56 | %LoadInst9 = load i32, i32* %j 57 | %4 = add i32 %LoadInst9, 1 58 | store i32 %4, i32* %j 59 | br label %cond2 60 | 61 | afterLoop4: ; preds = %cond2 62 | br label %cond 63 | } --------------------------------------------------------------------------------