├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── misc.xml └── modules.xml ├── compiler2016.iml ├── design ├── Mill.g4 ├── ast.png ├── performance.numbers ├── performance.pdf └── presentation.pdf ├── finalvars.sh ├── lib ├── antlr-4.5.2-complete.jar └── builtin_functions.s ├── makefile ├── midtermvars.sh ├── readme.md ├── src └── com │ └── abcdabcd987 │ └── compiler2016 │ ├── AST │ ├── ASTNode.java │ ├── ArrayAccess.java │ ├── ArrayTypeNode.java │ ├── BinaryExpr.java │ ├── BoolConst.java │ ├── BreakStmt.java │ ├── CompoundStmt.java │ ├── ContinueStmt.java │ ├── Decl.java │ ├── EmptyExpr.java │ ├── Expr.java │ ├── ForLoop.java │ ├── FunctionCall.java │ ├── FunctionDecl.java │ ├── FunctionTypeNode.java │ ├── IASTVisitor.java │ ├── Identifier.java │ ├── IfStmt.java │ ├── IntConst.java │ ├── MemberAccess.java │ ├── NewExpr.java │ ├── NullLiteral.java │ ├── PrimitiveTypeNode.java │ ├── Program.java │ ├── ReturnStmt.java │ ├── SelfDecrement.java │ ├── SelfIncrement.java │ ├── SourcePosition.java │ ├── Stmt.java │ ├── StringConst.java │ ├── StructDecl.java │ ├── StructTypeNode.java │ ├── TypeNode.java │ ├── UnaryExpr.java │ ├── VariableDecl.java │ ├── VariableDeclStmt.java │ └── WhileLoop.java │ ├── BackEnd │ ├── FunctionInliner.java │ ├── GlobalVariableResolver.java │ ├── GraphColoringAllocator.java │ ├── IRPrinter.java │ ├── InstructionLivenessAnalysis.java │ ├── LLIRInterpreter.java │ ├── LocalBottomUpAllocator.java │ ├── RegisterAllocator.java │ ├── SSATransformer.java │ └── StupidAllocator.java │ ├── CompilerOptions.java │ ├── FrontEnd │ ├── ASTBuilder.java │ ├── ASTPrinter.java │ ├── CompilationError.java │ ├── GlobalVariableInitializationHacker.java │ ├── IRBuilder.java │ ├── SemanticChecker.java │ ├── StructFunctionDeclarator.java │ └── StructSymbolScanner.java │ ├── IR │ ├── BasicBlock.java │ ├── BinaryOperation.java │ ├── Branch.java │ ├── BranchInstruction.java │ ├── Call.java │ ├── Function.java │ ├── HeapAllocate.java │ ├── IIRVisitor.java │ ├── IRInstruction.java │ ├── IRRoot.java │ ├── IntComparison.java │ ├── IntImmediate.java │ ├── IntValue.java │ ├── Jump.java │ ├── Load.java │ ├── Move.java │ ├── PhiInstruction.java │ ├── PhysicalRegister.java │ ├── Register.java │ ├── Return.java │ ├── StackSlot.java │ ├── StaticData.java │ ├── StaticSpace.java │ ├── StaticString.java │ ├── Store.java │ ├── SystemCall.java │ ├── UnaryOperation.java │ └── VirtualRegister.java │ ├── MIPS │ ├── IRBuiltinFunctionInserter.java │ ├── MIPSPrinter.java │ ├── MIPSRegister.java │ ├── MIPSRegisterSet.java │ ├── RegisterInformationInjector.java │ └── TargetIRTransformer.java │ ├── MidTerm.java │ ├── Mill.java │ ├── Parser │ ├── MillBaseListener.java │ ├── MillLexer.java │ ├── MillListener.java │ └── MillParser.java │ ├── Symbol │ ├── ArrayType.java │ ├── FunctionType.java │ ├── GlobalSymbolTable.java │ ├── PrimitiveType.java │ ├── StructType.java │ ├── SymbolInfo.java │ ├── SymbolTable.java │ ├── Type.java │ └── VariableType.java │ └── Utility │ ├── TeeOutputStream.java │ └── Utility.java ├── test └── com │ └── abcdabcd987 │ └── compiler2016 │ ├── BackEnd │ ├── CodeGenTest.java │ ├── FinalTest.java │ ├── IRBuilderTest.java │ ├── SSAConstructorTest.java │ └── SSADestructorTest.java │ ├── FrontEnd │ ├── ASTBuilderTest.java │ ├── SemanticCheckerTest.java │ └── StructFunctionSymbolTest.java │ └── Parser │ └── ParserTest.java └── testcase ├── error └── selfref-5090379042-jiaxiao.mill ├── final ├── a-function-call.mx ├── a-function-call.out ├── array_test1-mahaojun.in ├── array_test1-mahaojun.mx ├── array_test1-mahaojun.out ├── array_test2-mahaojun.in ├── array_test2-mahaojun.mx ├── array_test2-mahaojun.out ├── basicopt1-5100309127-hetianxing.limit ├── basicopt1-5100309127-hetianxing.mx ├── basicopt1-5100309127-hetianxing.out ├── builtin-5140519064-youyurong.in ├── builtin-5140519064-youyurong.mx ├── builtin-5140519064-youyurong.out ├── bulgarian-5110379024-wuhang.limit ├── bulgarian-5110379024-wuhang.mx ├── bulgarian-5110379024-wuhang.out ├── class_test-mahaojun.mx ├── class_test-mahaojun.out ├── expr-5110309085-jintianxing.limit ├── expr-5110309085-jintianxing.mx ├── expr-5110309085-jintianxing.out ├── function_test-huyuncong.in ├── function_test-huyuncong.mx ├── function_test-huyuncong.out ├── gcd-5090379042-jiaxiao.mx ├── gcd-5090379042-jiaxiao.out ├── hanoi-5100379110-daibo.in ├── hanoi-5100379110-daibo.limit ├── hanoi-5100379110-daibo.mx ├── hanoi-5100379110-daibo.out ├── hashmap-5100309127-hetianxing.limit ├── hashmap-5100309127-hetianxing.mx ├── hashmap-5100309127-hetianxing.out ├── heapsort-5100379110-daibo.in ├── heapsort-5100379110-daibo.limit ├── heapsort-5100379110-daibo.mx ├── heapsort-5100379110-daibo.out ├── horse-5100309153-yanghuan.in ├── horse-5100309153-yanghuan.limit ├── horse-5100309153-yanghuan.mx ├── horse-5100309153-yanghuan.out ├── horse2-5100309153-yanghuan.in ├── horse2-5100309153-yanghuan.limit ├── horse2-5100309153-yanghuan.mx ├── horse2-5100309153-yanghuan.out ├── horse3-5100309153-yanghuan.in ├── horse3-5100309153-yanghuan.limit ├── horse3-5100309153-yanghuan.mx ├── horse3-5100309153-yanghuan.out ├── lvalue2-5110379024-wuhang.mx ├── lvalue2-5110379024-wuhang.out ├── magic-5100309153-yanghuan.limit ├── magic-5100309153-yanghuan.mx ├── magic-5100309153-yanghuan.out ├── manyarguments-5100379110-daibo.mx ├── manyarguments-5100379110-daibo.out ├── maxflow-5100379110-daibo.limit ├── maxflow-5100379110-daibo.mx ├── maxflow-5100379110-daibo.out ├── multiarray-5100309153-yanghuan.mx ├── multiarray-5100309153-yanghuan.out ├── pi-5090379042-jiaxiao.mx ├── pi-5090379042-jiaxiao.out ├── prime-5100309153-yanghuan.in ├── prime-5100309153-yanghuan.limit ├── prime-5100309153-yanghuan.mx ├── prime-5100309153-yanghuan.out ├── qsort-5100379110-daibo.limit ├── qsort-5100379110-daibo.mx ├── qsort-5100379110-daibo.out ├── queens-5100379110-daibo.limit ├── queens-5100379110-daibo.mx ├── queens-5100379110-daibo.out ├── spill2-5100379110-daibo.limit ├── spill2-5100379110-daibo.mx ├── spill2-5100379110-daibo.out ├── statement_test-huyuncong.in ├── statement_test-huyuncong.mx ├── statement_test-huyuncong.out ├── string_test-huyuncong.in ├── string_test-huyuncong.mx ├── string_test-huyuncong.out ├── superloop-5090379042-jiaxiao.in ├── superloop-5090379042-jiaxiao.limit ├── superloop-5090379042-jiaxiao.mx ├── superloop-5090379042-jiaxiao.out ├── tak-5090379042-jiaxiao.in ├── tak-5090379042-jiaxiao.limit ├── tak-5090379042-jiaxiao.mx ├── tak-5090379042-jiaxiao.out ├── twinprime-5090379042-jiaxiao.limit ├── twinprime-5090379042-jiaxiao.mx └── twinprime-5090379042-jiaxiao.out ├── ir ├── array1.mx ├── array2.mx ├── array3.mx ├── array4.mx ├── builtin1.mx ├── builtin2.mx ├── builtin3.mx ├── function1.mx ├── function2.mx ├── global1.mx ├── loop1.mx ├── loop2.mx ├── loop3.mx ├── loop4.mx ├── simple1.mx ├── simple2.mx ├── simple3.mx ├── simple4.mx ├── simple5.mx ├── simple6.mx ├── simple7.mx ├── ssa1.mx ├── ssa2.mx ├── string1.mx ├── string2.mx ├── struct1.mx ├── struct2.mx ├── struct3.mx ├── symtable1.mx └── symtable2.mx └── semantic ├── basic_test-yurongyou.mill ├── compile_error ├── arrop-1-5100379071-puyouer.mx ├── asiop-1-5100379071-puyouer.mx ├── asiop-2-5100379071-puyouer.mx ├── asiop-3-5120309049-liaochao.mx ├── asiop-4-5120309049-liaochao.mx ├── asiop-5-5100379071-puyouer.mx ├── asiop-6-5100379071-puyouer.mx ├── asiop-7-5120309049-liaochao.mx ├── biop-1-5100379071-puyouer.mx ├── bitop-1-5120309049-liaochao.mx ├── boop-1-5120309049-liaochao.mx ├── class-1-5100379071-puyouer.mx ├── class-2-5120309049-liaochao.mx ├── ctrflow-1-5100379071-puyouer.mx ├── ctrflow-2-5120309049-liaochao.mx ├── ctrflow-3-5100379071-puyouer.mx ├── ctrflow-4-5120309049-liaochao.mx ├── ctrflow-5-5120309049-liaochao.mx ├── fldop-1-5120309049-liaochao.mx ├── func-1-5100379071-puyouer.mx ├── func-2-5120309049-liaochao.mx ├── func-3-5100379071-puyouer.mx ├── func-4-5100379071-puyouer.mx ├── func-5-5100309127-hetianxing.mx ├── func-6-5130309059-lijiajun.mx ├── func-7-5140309552-wancheng.mx ├── incop-1-5120309049-liaochao.mx ├── relop-1-5120309049-liaochao.mx ├── relop-2-5120309049-liaochao.mx ├── scope-1-5140519064-youyurong.mx ├── scope-2-5140519064-youyurong.mx ├── var-1-5110379024-wuhang.mx ├── var-2-5100309127-hetianxing.mx ├── var-3-5120309049-liaochao.mx └── var-4-5140309552-wancheng.mx ├── grammar_patch_1.mill ├── grammar_patch_2.mill ├── hello-5090379042-jiaxiao.mill ├── language_manual.mill └── passed ├── basicopt1-5100309127-hetianxing.mx ├── builtin-5140519064-youyurong.mx ├── bulgarian-5110379024-wuhang.mx ├── expr-5110309085-jintianxing.mx ├── hanoi-5100379110-daibo.mx ├── hashmap-5100309127-hetianxing.mx ├── heapsort-5100379110-daibo.mx ├── horse-5100309153-yanghuan.mx ├── horse2-5100309153-yanghuan.mx ├── horse3-5100309153-yanghuan.mx ├── lvalue2-5110379024-wuhang.mx ├── magic-5100309153-yanghuan.mx ├── maxflow-5100379110-daibo.mx ├── prime-5100309153-yanghuan.mx ├── qsort-5100379110-daibo.mx ├── queens-5100379110-daibo.mx ├── spill2-5100379110-daibo.mx ├── superloop-5090379042-jiaxiao.mx ├── tak-5090379042-jiaxiao.mx └── twinprime-5090379042-jiaxiao.mx /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/java,intellij,osx 2 | 3 | ### Java ### 4 | *.class 5 | bin/ 6 | 7 | # Mobile Tools for Java (J2ME) 8 | .mtj.tmp/ 9 | 10 | # Package Files # 11 | *.jar 12 | *.war 13 | *.ear 14 | 15 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 16 | hs_err_pid* 17 | 18 | 19 | ### Intellij ### 20 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 21 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 22 | 23 | # User-specific stuff: 24 | .idea/workspace.xml 25 | .idea/tasks.xml 26 | .idea/dictionaries 27 | .idea/vcs.xml 28 | .idea/jsLibraryMappings.xml 29 | 30 | # Sensitive or high-churn files: 31 | .idea/dataSources.ids 32 | .idea/dataSources.xml 33 | .idea/dataSources.local.xml 34 | .idea/sqlDataSources.xml 35 | .idea/dynamic.xml 36 | .idea/uiDesigner.xml 37 | 38 | # Gradle: 39 | .idea/gradle.xml 40 | .idea/libraries 41 | 42 | # Mongo Explorer plugin: 43 | .idea/mongoSettings.xml 44 | 45 | ## File-based project format: 46 | *.iws 47 | 48 | ## Plugin-specific files: 49 | 50 | # IntelliJ 51 | /out/ 52 | 53 | # mpeltonen/sbt-idea plugin 54 | .idea_modules/ 55 | 56 | # JIRA plugin 57 | atlassian-ide-plugin.xml 58 | 59 | # Crashlytics plugin (for Android Studio and IntelliJ) 60 | com_crashlytics_export_strings.xml 61 | crashlytics.properties 62 | crashlytics-build.properties 63 | fabric.properties 64 | 65 | ### Intellij Patch ### 66 | *.iml 67 | 68 | 69 | ### OSX ### 70 | .DS_Store 71 | .AppleDouble 72 | .LSOverride 73 | 74 | # Icon must end with two \r 75 | Icon 76 | 77 | 78 | # Thumbnails 79 | ._* 80 | 81 | # Files that might appear in the root of a volume 82 | .DocumentRevisions-V100 83 | .fseventsd 84 | .Spotlight-V100 85 | .TemporaryItems 86 | .Trashes 87 | .VolumeIcon.icns 88 | 89 | # Directories potentially created on remote AFP share 90 | .AppleDB 91 | .AppleDesktop 92 | Network Trash Folder 93 | Temporary Items 94 | .apdisk 95 | 96 | 97 | 98 | 99 | !lib/*.jar 100 | lib/statspim/usr/ 101 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /compiler2016.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /design/ast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abcdabcd987/compiler2016/a07184b704e80265d5701aa5b7f34d2de2bdffce/design/ast.png -------------------------------------------------------------------------------- /design/performance.numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abcdabcd987/compiler2016/a07184b704e80265d5701aa5b7f34d2de2bdffce/design/performance.numbers -------------------------------------------------------------------------------- /design/performance.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abcdabcd987/compiler2016/a07184b704e80265d5701aa5b7f34d2de2bdffce/design/performance.pdf -------------------------------------------------------------------------------- /design/presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abcdabcd987/compiler2016/a07184b704e80265d5701aa5b7f34d2de2bdffce/design/presentation.pdf -------------------------------------------------------------------------------- /finalvars.sh: -------------------------------------------------------------------------------- 1 | #!bin/bash 2 | export CCHK="java -classpath ../lib/antlr-4.5.2-complete.jar:../bin com.abcdabcd987.compiler2016.Mill" -------------------------------------------------------------------------------- /lib/antlr-4.5.2-complete.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abcdabcd987/compiler2016/a07184b704e80265d5701aa5b7f34d2de2bdffce/lib/antlr-4.5.2-complete.jar -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | all: 2 | mkdir -p bin 3 | find ./src -name *.java | javac -d bin -classpath "lib/antlr-4.5.2-complete.jar" @/dev/stdin 4 | mkdir bin/lib 5 | cp lib/builtin_functions.s bin/lib 6 | 7 | clean: 8 | rm -rf bin 9 | -------------------------------------------------------------------------------- /midtermvars.sh: -------------------------------------------------------------------------------- 1 | #!bin/bash 2 | export CCHK="java -classpath ../lib/antlr-4.5.2-complete.jar:../bin com.abcdabcd987.compiler2016.MidTerm" -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Compiler 2016 2 | 3 | This is a toy compiler for the course [Compiler 2016](http://acm.sjtu.edu.cn/wiki/Compiler_2016) at ACM Class, SJTU. **The source language is [Mx\*](http://acm.sjtu.edu.cn/w/images/9/93/Mx_language_manual.pdf). The target is MIPS assembly (in SPIM format).** 4 | 5 | You can refer to [my presentation slides](https://github.com/abcdabcd987/compiler2016/raw/master/design/presentation.pdf) (or on [SlideShare](http://www.slideshare.net/abcdabcd987/compiler2016-by-abcdabcd987)) to know something about this compiler and also what I've learnt during the course. 6 | 7 | When debugging this compiler, I wrote another project [LLIRInterpreter](https://github.com/abcdabcd987/LLIRInterpreter) which reads text IR and does interpretation. 8 | 9 | ## Feature (what you can find in the source code) 10 | 11 | - Copy elimination 12 | - Function inlining 13 | - SSA Transform 14 | - Construction 15 | - Destruction 16 | - Naive dead code elimination 17 | - Simple constant propagation 18 | - Register Allocation 19 | - Local bottom-up allocator 20 | - Interference graph coloring allocator 21 | 22 | ## Usage 23 | 24 | ``` 25 | $ java com.abcdabcd987.compiler2016.Mill --help 26 | Mill - Mx* language implementation made with love by abcdabcd987 27 | Usage: mill [options] [input] 28 | Options: 29 | -help Print this help message 30 | -o Write output to 31 | -reg-alloc Set register allocator to 32 | Available register allocators: 33 | no: Don't allocate at all. (CISC-like) 34 | local: Local bottom-up allocator 35 | color: Global allocation by interference graph coloring 36 | -print-ast Print the abstract semantic tree 37 | -print-ir Print the intermediate representation 38 | -print-ssa-ir Print the intermediate representation after SSA transforms 39 | -no-inline Disable function inlining 40 | -no-ssa Disable single static assignment analysis and transforms 41 | -no-naive-dce Disable naive dead code elimination 42 | -no-scp Disable simple constant propagate 43 | ``` 44 | 45 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ASTNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.SymbolTable; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public abstract class ASTNode { 9 | public abstract void accept(IASTVisitor visitor); 10 | public SymbolTable scope; 11 | } 12 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ArrayAccess.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class ArrayAccess extends Expr { 7 | public Expr array; 8 | public Expr subscript; 9 | public SourcePosition posArray; 10 | public SourcePosition posSubscript; 11 | 12 | public ArrayAccess(Expr array, Expr subscript, SourcePosition posArray, SourcePosition posSubscript) { 13 | this.array = array; 14 | this.subscript = subscript; 15 | this.posArray = posArray; 16 | this.posSubscript = posSubscript; 17 | } 18 | 19 | @Override 20 | public void accept(IASTVisitor visitor) { 21 | visitor.visit(this); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ArrayTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public class ArrayTypeNode extends TypeNode { 9 | public final TypeNode baseType; 10 | 11 | public ArrayTypeNode(TypeNode baseType) { 12 | this.baseType = baseType; 13 | } 14 | 15 | @Override 16 | public void accept(IASTVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | 20 | @Override 21 | public Type.Types getType() { 22 | return Type.Types.ARRAY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/BinaryExpr.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class BinaryExpr extends Expr { 7 | public enum BinaryOp { 8 | ASSIGN, 9 | LOGICAL_OR, LOGICAL_AND, 10 | BITWISE_OR, BITWISE_AND, XOR, 11 | EQ, NE, LT, GT, LE, GE, 12 | SHL, SHR, 13 | ADD, SUB, 14 | MUL, DIV, MOD 15 | } 16 | 17 | public final BinaryOp op; 18 | public Expr lhs; 19 | public Expr rhs; 20 | public final SourcePosition posOp; 21 | public final SourcePosition posLhs; 22 | public final SourcePosition posRhs; 23 | 24 | public BinaryExpr(BinaryOp op, Expr lhs, Expr rhs, SourcePosition posOp, SourcePosition posLhs, SourcePosition posRhs) { 25 | this.op = op; 26 | this.lhs = lhs; 27 | this.rhs = rhs; 28 | this.posOp = posOp; 29 | this.posLhs = posLhs; 30 | this.posRhs = posRhs; 31 | } 32 | 33 | @Override 34 | public void accept(IASTVisitor visitor) { 35 | visitor.visit(this); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/BoolConst.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class BoolConst extends Expr { 7 | public final boolean value; 8 | 9 | public BoolConst(boolean value) { 10 | this.value = value; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/BreakStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class BreakStmt extends Stmt { 7 | public SourcePosition pos; 8 | 9 | public BreakStmt(SourcePosition pos) { 10 | this.pos = pos; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/CompoundStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-03-26. 9 | */ 10 | public class CompoundStmt extends Stmt { 11 | public final List stmts; 12 | 13 | public static class Builder { 14 | private List stmts = new LinkedList<>(); 15 | 16 | public void add(Object node) { 17 | if (node instanceof Stmt) stmts.add((Stmt) node); 18 | else if (node instanceof List) ((List) node).stream().forEachOrdered(this::add); 19 | else throw new RuntimeException("CompoundStmt accepts Stmt only."); 20 | } 21 | 22 | public CompoundStmt build() { return new CompoundStmt(stmts); } 23 | } 24 | 25 | public CompoundStmt(List stmts) { 26 | this.stmts = stmts; 27 | } 28 | 29 | @Override 30 | public void accept(IASTVisitor visitor) { 31 | visitor.visit(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ContinueStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class ContinueStmt extends Stmt { 7 | public SourcePosition pos; 8 | 9 | public ContinueStmt(SourcePosition pos) { 10 | this.pos = pos; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/Decl.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public abstract class Decl extends ASTNode { 7 | } 8 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/EmptyExpr.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class EmptyExpr extends Expr { 7 | @Override 8 | public void accept(IASTVisitor visitor) { 9 | visitor.visit(this); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/Expr.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.IR.BasicBlock; 4 | import com.abcdabcd987.compiler2016.IR.IntValue; 5 | import com.abcdabcd987.compiler2016.Symbol.Type; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-03-26. 9 | */ 10 | public abstract class Expr extends Stmt { 11 | // for semantic check 12 | public Type exprType; 13 | public boolean isLvalue = true; 14 | 15 | // for IR: condition check 16 | public BasicBlock ifTrue; 17 | public BasicBlock ifFalse; 18 | 19 | // for IR: expr value 20 | public IntValue intValue; 21 | public IntValue addressValue; 22 | public int addressOffset; 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ForLoop.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public class ForLoop extends Stmt { 9 | public List initWithDecl; 10 | public Expr init; 11 | public Expr cond; 12 | public Expr step; 13 | public Stmt body; 14 | public SourcePosition posCond; 15 | 16 | public ForLoop(Expr init, Expr cond, Expr step, Stmt body, SourcePosition posCond) { 17 | this.initWithDecl = null; 18 | this.init = init; 19 | this.cond = cond; 20 | this.step = step; 21 | this.body = body; 22 | this.posCond = posCond; 23 | } 24 | 25 | public ForLoop(List initWithDecl, Expr cond, Expr step, Stmt body, SourcePosition posCond) { 26 | this.initWithDecl = initWithDecl; 27 | this.init = null; 28 | this.body = body; 29 | this.cond = cond; 30 | this.step = step; 31 | this.posCond = posCond; 32 | } 33 | 34 | @Override 35 | public void accept(IASTVisitor visitor) { 36 | visitor.visit(this); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/FunctionCall.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by abcdabcd987 on 2016-03-26. 10 | */ 11 | public class FunctionCall extends Expr { 12 | public final Expr name; 13 | public final List parameters; 14 | public final SourcePosition posName; 15 | public final List posArgs; 16 | public Expr argThis = null; // for builtin string & array function 17 | 18 | public static class Builder { 19 | private Expr name; 20 | private List parameters = new ArrayList<>(); 21 | private SourcePosition posName; 22 | private List posArgs = new ArrayList<>(); 23 | 24 | public void setName(Expr name) { 25 | this.name = name; 26 | } 27 | 28 | public void setPosName(SourcePosition posName) { 29 | this.posName = posName; 30 | } 31 | 32 | public void addArg(Object arg) { 33 | if (arg instanceof Expr) parameters.add((Expr)arg); 34 | else throw new RuntimeException("Invalid type"); 35 | } 36 | 37 | public void addPosArg(SourcePosition pos) { 38 | posArgs.add(pos); 39 | } 40 | 41 | public FunctionCall build() { 42 | return new FunctionCall(name, parameters, posName, posArgs); 43 | } 44 | } 45 | 46 | public FunctionCall(Expr name, List parameters, SourcePosition posName, List posArgs) { 47 | this.name = name; 48 | this.parameters = parameters; 49 | this.posName = posName; 50 | this.posArgs = posArgs; 51 | } 52 | 53 | @Override 54 | public void accept(IASTVisitor visitor) { 55 | visitor.visit(this); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/FunctionDecl.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.FunctionType; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by abcdabcd987 on 2016-03-26. 10 | */ 11 | public class FunctionDecl extends Decl { 12 | public final TypeNode returnType; 13 | public final String name; 14 | public final List argTypes; 15 | public final CompoundStmt body; 16 | public final SourcePosition posReturnType; 17 | public final SourcePosition posName; 18 | public FunctionType functionType; 19 | 20 | public static class Builder { 21 | private TypeNode returnType; 22 | private String name; 23 | private List argTypes = new ArrayList<>(); 24 | private CompoundStmt body; 25 | private SourcePosition posReturnType; 26 | private SourcePosition posName; 27 | 28 | public void setReturnType(TypeNode returnType) { 29 | this.returnType = returnType; 30 | } 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | public void setBody(CompoundStmt body) { 35 | this.body = body; 36 | } 37 | public void setPosReturnType(SourcePosition posReturnType) { 38 | this.posReturnType = posReturnType; 39 | } 40 | public void setPosName(SourcePosition posName) { 41 | this.posName = posName; 42 | } 43 | 44 | public void addArgType(Object node) { 45 | if (node instanceof VariableDecl) argTypes.add((VariableDecl)node); 46 | else throw new RuntimeException("Invalid type"); 47 | } 48 | 49 | public FunctionDecl build() { 50 | return new FunctionDecl(returnType, name, argTypes, body, posReturnType, posName); 51 | } 52 | } 53 | 54 | public FunctionDecl(TypeNode returnType, String name, List argTypes, CompoundStmt body, SourcePosition posReturnType, SourcePosition posName) { 55 | this.returnType = returnType; 56 | this.name = name; 57 | this.argTypes = argTypes; 58 | this.body = body; 59 | this.posReturnType = posReturnType; 60 | this.posName = posName; 61 | } 62 | 63 | @Override 64 | public void accept(IASTVisitor visitor) { 65 | visitor.visit(this); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/FunctionTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-30. 7 | */ 8 | public class FunctionTypeNode extends TypeNode { 9 | @Override 10 | public Type.Types getType() { 11 | return Type.Types.FUNCTION; 12 | } 13 | 14 | @Override 15 | public void accept(IASTVisitor visitor) { 16 | visitor.visit(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/IASTVisitor.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-28. 5 | */ 6 | public interface IASTVisitor { 7 | void visit(Program node); 8 | 9 | void visit(StructDecl node); 10 | void visit(VariableDecl node); 11 | void visit(FunctionDecl node); 12 | 13 | void visit(ArrayTypeNode node); 14 | void visit(PrimitiveTypeNode node); 15 | 16 | void visit(BreakStmt node); 17 | void visit(ContinueStmt node); 18 | void visit(ReturnStmt node); 19 | void visit(FunctionTypeNode node); 20 | void visit(StructTypeNode node); 21 | 22 | void visit(CompoundStmt node); 23 | void visit(IfStmt node); 24 | void visit(ForLoop node); 25 | void visit(WhileLoop node); 26 | void visit(VariableDeclStmt node); 27 | 28 | void visit(ArrayAccess node); 29 | void visit(UnaryExpr node); 30 | void visit(BinaryExpr node); 31 | void visit(EmptyExpr node); 32 | void visit(FunctionCall node); 33 | void visit(NewExpr node); 34 | void visit(MemberAccess node); 35 | void visit(SelfDecrement node); 36 | void visit(SelfIncrement node); 37 | 38 | void visit(Identifier node); 39 | void visit(BoolConst node); 40 | void visit(IntConst node); 41 | void visit(StringConst node); 42 | void visit(NullLiteral node); 43 | 44 | void visit(Expr node); 45 | void visit(Stmt node); 46 | void visit(Decl node); 47 | void visit(ASTNode node); 48 | void visit(BinaryExpr.BinaryOp node); 49 | void visit(UnaryExpr.UnaryOp node); 50 | } 51 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/Identifier.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.SymbolInfo; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public class Identifier extends Expr { 9 | public final String name; 10 | public final SourcePosition pos; 11 | public SymbolInfo symbolInfo; 12 | 13 | public Identifier(String name, SourcePosition pos) { 14 | this.name = name; 15 | this.pos = pos; 16 | } 17 | 18 | @Override 19 | public void accept(IASTVisitor visitor) { 20 | visitor.visit(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/IfStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class IfStmt extends Stmt { 7 | public Expr cond; 8 | public Stmt then; 9 | public Stmt otherwise; 10 | public SourcePosition posCond; 11 | 12 | public IfStmt(Expr cond, Stmt then, Stmt otherwise, SourcePosition posCond) { 13 | this.cond = cond; 14 | this.then = then; 15 | this.otherwise = otherwise; 16 | this.posCond = posCond; 17 | } 18 | 19 | @Override 20 | public void accept(IASTVisitor visitor) { 21 | visitor.visit(this); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/IntConst.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class IntConst extends Expr { 7 | public final int value; 8 | 9 | public IntConst(int value) { 10 | this.value = value; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/MemberAccess.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class MemberAccess extends Expr { 7 | public final Expr record; 8 | public final String member; 9 | public final SourcePosition posRecord; 10 | public final SourcePosition posMember; 11 | 12 | public MemberAccess(Expr record, String member, SourcePosition posRecord, SourcePosition posMember) { 13 | this.record = record; 14 | this.member = member; 15 | this.posRecord = posRecord; 16 | this.posMember = posMember; 17 | } 18 | 19 | @Override 20 | public void accept(IASTVisitor visitor) { 21 | visitor.visit(this); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/NewExpr.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-03-27. 8 | */ 9 | public class NewExpr extends Expr { 10 | public final TypeNode type; 11 | public final List dim; 12 | public final List posDim; 13 | 14 | public static class Builder { 15 | private TypeNode type; 16 | private List dim = new ArrayList<>(); 17 | private List posDim = new ArrayList<>(); 18 | 19 | public void setType(TypeNode type) { 20 | this.type = type; 21 | } 22 | 23 | public void addDimension(Object node) { 24 | if (node instanceof Expr) dim.add((Expr)node); 25 | else if (node == null) dim.add(null); 26 | else throw new RuntimeException("Invalid type"); 27 | } 28 | 29 | public void addPosDimension(SourcePosition pos) { 30 | posDim.add(pos); 31 | } 32 | 33 | public NewExpr build() { 34 | return new NewExpr(type, dim, posDim); 35 | } 36 | } 37 | 38 | public NewExpr(TypeNode type, List dim, List posDim) { 39 | this.type = type; 40 | this.dim = dim; 41 | this.posDim = posDim; 42 | } 43 | 44 | @Override 45 | public void accept(IASTVisitor visitor) { 46 | visitor.visit(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/NullLiteral.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-28. 5 | */ 6 | public class NullLiteral extends Expr { 7 | 8 | @Override 9 | public void accept(IASTVisitor visitor) { 10 | visitor.visit(this); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/PrimitiveTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-30. 7 | */ 8 | public class PrimitiveTypeNode extends TypeNode { 9 | private Type.Types type; 10 | 11 | public PrimitiveTypeNode(Type.Types type) { 12 | this.type = type; 13 | } 14 | 15 | @Override 16 | public void accept(IASTVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | 20 | @Override 21 | public Type.Types getType() { 22 | return type; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/Program.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-03-26. 8 | */ 9 | public class Program extends ASTNode { 10 | public final List decls; 11 | 12 | public static class Builder { 13 | private List decls = new ArrayList<>(); 14 | 15 | public void add(Object node) { 16 | if (node instanceof Decl) decls.add((Decl)node); 17 | else if (node instanceof List) ((List) node).stream().forEachOrdered(this::add); 18 | else throw new RuntimeException("Invalid type."); 19 | } 20 | 21 | public Program build() { 22 | return new Program(decls); 23 | } 24 | } 25 | 26 | public Program(List decls) { 27 | this.decls = decls; 28 | } 29 | 30 | @Override 31 | public void accept(IASTVisitor visitor) { 32 | visitor.visit(this); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/ReturnStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class ReturnStmt extends Stmt { 7 | public Expr value; 8 | public SourcePosition pos; 9 | public SourcePosition posValue; 10 | 11 | public ReturnStmt(Expr value, SourcePosition pos, SourcePosition posValue) { 12 | this.value = value; 13 | this.pos = pos; 14 | this.posValue = posValue; 15 | } 16 | 17 | @Override 18 | public void accept(IASTVisitor visitor) { 19 | visitor.visit(this); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/SelfDecrement.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class SelfDecrement extends Expr { 7 | public final Expr self; 8 | public final SourcePosition posSelf; 9 | 10 | public SelfDecrement(Expr self, SourcePosition posSelf) { 11 | this.self = self; 12 | this.posSelf = posSelf; 13 | } 14 | 15 | @Override 16 | public void accept(IASTVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/SelfIncrement.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class SelfIncrement extends Expr { 7 | public final Expr self; 8 | public final SourcePosition posSelf; 9 | 10 | public SelfIncrement(Expr self, SourcePosition posSelf) { 11 | this.self = self; 12 | this.posSelf = posSelf; 13 | } 14 | 15 | @Override 16 | public void accept(IASTVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/SourcePosition.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import org.antlr.v4.runtime.ParserRuleContext; 4 | import org.antlr.v4.runtime.Token; 5 | import org.antlr.v4.runtime.tree.TerminalNode; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-04-04. 9 | */ 10 | public class SourcePosition { 11 | public final int line; 12 | public final int column; 13 | 14 | public SourcePosition(int line, int column) { 15 | this.line = line; 16 | this.column = column; 17 | } 18 | 19 | public SourcePosition(Token token) { 20 | this.line = token.getLine(); 21 | this.column = token.getCharPositionInLine(); 22 | } 23 | 24 | public SourcePosition(ParserRuleContext ctx) { 25 | this(ctx.start); 26 | } 27 | 28 | public SourcePosition(TerminalNode terminal) { 29 | this(terminal.getSymbol()); 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Line " + line + " Column " + column; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/Stmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public abstract class Stmt extends ASTNode { 7 | } 8 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/StringConst.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class StringConst extends Expr { 7 | public final String value; 8 | 9 | public StringConst(String value) { 10 | this.value = value; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/StructDecl.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-03-26. 8 | */ 9 | public class StructDecl extends Decl { 10 | public final List members; 11 | public final String name; 12 | public final SourcePosition posName; 13 | 14 | public static class Builder { 15 | private String name; 16 | private List members = new ArrayList<>(); 17 | private SourcePosition posName; 18 | 19 | public void setPosName(SourcePosition posName) { 20 | this.posName = posName; 21 | } 22 | 23 | public void add(Object node) { 24 | if (node instanceof VariableDecl) members.add((VariableDecl)node); 25 | else throw new RuntimeException("Invalid type"); 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public StructDecl build() { 33 | return new StructDecl(name, members, posName); 34 | } 35 | } 36 | 37 | public StructDecl(String name, List members, SourcePosition posName) { 38 | this.members = members; 39 | this.name = name; 40 | this.posName = posName; 41 | } 42 | 43 | @Override 44 | public void accept(IASTVisitor visitor) { 45 | visitor.visit(this); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/StructTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public class StructTypeNode extends TypeNode { 9 | public final String name; 10 | 11 | public StructTypeNode(String name) { 12 | this.name = name; 13 | } 14 | 15 | @Override 16 | public void accept(IASTVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | 20 | @Override 21 | public Type.Types getType() { 22 | return Type.Types.STRUCT; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/TypeNode.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | import com.abcdabcd987.compiler2016.Symbol.Type; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-26. 7 | */ 8 | public abstract class TypeNode extends ASTNode { 9 | public abstract Type.Types getType(); 10 | } 11 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/UnaryExpr.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class UnaryExpr extends Expr { 7 | public enum UnaryOp { 8 | INC, DEC, POS, NEG, LOGICAL_NOT, BITWISE_NOT 9 | } 10 | 11 | public UnaryOp op; 12 | public Expr body; 13 | public SourcePosition posOp; 14 | public SourcePosition posBody; 15 | 16 | public UnaryExpr(UnaryOp op, Expr body, SourcePosition posOp, SourcePosition posBody) { 17 | this.op = op; 18 | this.body = body; 19 | this.posOp = posOp; 20 | this.posBody = posBody; 21 | } 22 | 23 | @Override 24 | public void accept(IASTVisitor visitor) { 25 | visitor.visit(this); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/VariableDecl.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class VariableDecl extends Decl { 7 | public TypeNode type; 8 | public String name; 9 | public Expr init; 10 | public SourcePosition posType; 11 | public SourcePosition posName; 12 | public SourcePosition posInit; 13 | 14 | public VariableDecl(TypeNode type, String name, Expr init, SourcePosition posType, SourcePosition posName, SourcePosition posInit) { 15 | this.type = type; 16 | this.name = name; 17 | this.init = init; 18 | this.posType = posType; 19 | this.posName = posName; 20 | this.posInit = posInit; 21 | } 22 | 23 | @Override 24 | public void accept(IASTVisitor visitor) { 25 | visitor.visit(this); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/VariableDeclStmt.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-30. 5 | */ 6 | public class VariableDeclStmt extends Stmt { 7 | public final VariableDecl decl; 8 | 9 | public VariableDeclStmt(VariableDecl decl) { 10 | this.decl = decl; 11 | } 12 | 13 | @Override 14 | public void accept(IASTVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/AST/WhileLoop.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.AST; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-26. 5 | */ 6 | public class WhileLoop extends Stmt { 7 | public Expr cond; 8 | public Stmt body; 9 | public SourcePosition posCond; 10 | 11 | public WhileLoop(Expr cond, Stmt body, SourcePosition posCond) { 12 | this.cond = cond; 13 | this.body = body; 14 | this.posCond = posCond; 15 | } 16 | 17 | @Override 18 | public void accept(IASTVisitor visitor) { 19 | visitor.visit(this); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/BackEnd/InstructionLivenessAnalysis.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.BackEnd; 2 | 3 | import com.abcdabcd987.compiler2016.IR.*; 4 | 5 | import java.util.Collection; 6 | import java.util.HashSet; 7 | import java.util.List; 8 | import java.util.Set; 9 | 10 | /** 11 | * Instruction level liveness analysis for virtual register and stack slot. 12 | * Used by graph coloring register allocation. 13 | * See: http://www.cs.colostate.edu/~mstrout/CS553/slides/lecture03.pdf 14 | * Use reverse pre order (although the best one is the reverse post order on the reverse CFG) 15 | * Created by abcdabcd987 on 2016-05-05. 16 | */ 17 | public class InstructionLivenessAnalysis { 18 | private IRRoot ir; 19 | 20 | public InstructionLivenessAnalysis(IRRoot ir) { 21 | this.ir = ir; 22 | } 23 | 24 | private void initBlock(BasicBlock BB) { 25 | for (IRInstruction inst = BB.getHead(); inst != null; inst = inst.getNext()) 26 | if (inst.liveOut == null) { 27 | inst.liveOut = new HashSet<>(); 28 | inst.liveIn = new HashSet<>(); 29 | } else { 30 | inst.liveOut.clear(); 31 | inst.liveIn.clear(); 32 | } 33 | } 34 | 35 | private void processFunction(Function func) { 36 | func.calcReversePreOrder(); 37 | List RPO = func.getReversePreOrder(); 38 | RPO.forEach(this::initBlock); 39 | 40 | Set out = new HashSet<>(); 41 | Set in = new HashSet<>(); 42 | boolean changed = true; 43 | while (changed) { 44 | changed = false; 45 | for (BasicBlock BB : RPO) { 46 | for (IRInstruction inst = BB.getLast(); inst != null; inst = inst.getPrev()) { 47 | in.clear(); 48 | out.clear(); 49 | if (inst instanceof Branch) { 50 | out.addAll(((Branch) inst).getThen().getHead().liveIn); 51 | out.addAll(((Branch) inst).getElse().getHead().liveIn); 52 | } else if (inst instanceof Jump) { 53 | out.addAll(((Jump) inst).getTarget().getHead().liveIn); 54 | } else if (!(inst instanceof Return)) { 55 | assert inst.getNext() != null; // inst is not a branch, thus inst.getNext() not null 56 | out.addAll(inst.getNext().liveIn); 57 | } 58 | in.addAll(out); 59 | Register defined = inst.getDefinedRegister(); 60 | if (defined instanceof VirtualRegister) 61 | in.remove(defined); 62 | inst.getUsedRegister().stream() 63 | .filter(x -> x instanceof VirtualRegister) 64 | .forEach(x -> in.add((VirtualRegister) x)); 65 | if (!inst.liveOut.equals(out) || !inst.liveIn.equals(in)) { 66 | changed = true; 67 | inst.liveOut.clear(); 68 | inst.liveIn.clear(); 69 | inst.liveOut.addAll(out); 70 | inst.liveIn.addAll(in); 71 | } 72 | } 73 | } 74 | } 75 | } 76 | 77 | public void run() { 78 | ir.functions.values().forEach(this::processFunction); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/BackEnd/RegisterAllocator.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.BackEnd; 2 | 3 | import com.abcdabcd987.compiler2016.IR.*; 4 | 5 | /** 6 | * Register Allocator Base Class 7 | * Target dependent register information should be injected in advance. 8 | * Before allocation, registers can be {@link VirtualRegister} and {@link IntImmediate}. 9 | * After allocation, registers in (almost) all {@link IRInstruction} are {@link PhysicalRegister} and {@link IntImmediate}, 10 | * except that address field in {@link Load#address}/{@link Store#address} can be {@link StackSlot}. 11 | * Created by abcdabcd987 on 2016-05-02. 12 | */ 13 | public abstract class RegisterAllocator { 14 | public abstract void run(); 15 | } 16 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/CompilerOptions.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-11. 5 | */ 6 | public class CompilerOptions { 7 | private static final int SIZE_INT = 4; 8 | private static final int SIZE_BOOL = 4; // FIXME! 9 | private static final int SIZE_POINTER = 4; 10 | 11 | public static boolean ifPrintAST = false; 12 | public static boolean ifPrintRawIR = false; 13 | public static boolean ifPrintSSAIR = false; 14 | public static boolean ifPrintIRAfterInline = false; 15 | public static boolean enableInline = true; 16 | public static boolean enableSSA = true; 17 | public static boolean eliminateDeadCode = true; 18 | public static boolean simpleConstantPropagate = true; 19 | public static String registerAllocator = "color"; 20 | 21 | public static int getSizeInt() { 22 | return SIZE_INT; 23 | } 24 | 25 | public static int getSizeBool() { 26 | return SIZE_BOOL; 27 | } 28 | 29 | public static int getSizePointer() { 30 | return SIZE_POINTER; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/FrontEnd/CompilationError.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.FrontEnd; 2 | 3 | import com.abcdabcd987.compiler2016.AST.SourcePosition; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by abcdabcd987 on 2016-03-31. 10 | */ 11 | public class CompilationError { 12 | private List errors = new ArrayList<>(); 13 | 14 | public void add(SourcePosition pos, String reason) { 15 | reason = pos + ": " + reason; 16 | errors.add(reason); 17 | throw new RuntimeException(reason); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/BinaryOperation.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-07. 8 | */ 9 | public class BinaryOperation extends IRInstruction { 10 | public enum BinaryOp { 11 | ADD, SUB, MUL, DIV, MOD, 12 | SHL, SHR, AND, OR, XOR 13 | } 14 | 15 | private Register dest; 16 | private BinaryOp op; 17 | private IntValue lhs; 18 | private IntValue rhs; 19 | 20 | public BinaryOperation(BasicBlock BB, Register dest, BinaryOp op, IntValue lhs, IntValue rhs) { 21 | super(BB); 22 | this.dest = dest; 23 | this.op = op; 24 | this.lhs = lhs; 25 | this.rhs = rhs; 26 | reloadUsedRegisterCollection(); 27 | } 28 | 29 | public Register getDest() { 30 | return dest; 31 | } 32 | 33 | public BinaryOp getOp() { 34 | return op; 35 | } 36 | 37 | public IntValue getLhs() { 38 | return lhs; 39 | } 40 | 41 | public IntValue getRhs() { 42 | return rhs; 43 | } 44 | 45 | public void setLhs(IntValue value) { 46 | lhs = value; 47 | reloadUsedRegisterCollection(); 48 | } 49 | 50 | @Override 51 | public void accept(IIRVisitor visitor) { 52 | visitor.visit(this); 53 | } 54 | 55 | @Override 56 | public Register getDefinedRegister() { 57 | return dest; 58 | } 59 | 60 | @Override 61 | protected void reloadUsedRegisterCollection() { 62 | usedRegister.clear(); 63 | if (lhs instanceof Register) usedRegister.add((Register) lhs); 64 | if (rhs instanceof Register) usedRegister.add((Register) rhs); 65 | usedIntValue.clear(); 66 | usedIntValue.add(lhs); 67 | usedIntValue.add(rhs); 68 | } 69 | 70 | @Override 71 | public void setDefinedRegister(Register newReg) { 72 | dest = newReg; 73 | } 74 | 75 | @Override 76 | public void setUsedRegister(Map regMap) { 77 | if (lhs instanceof Register) lhs = regMap.get(lhs); 78 | if (rhs instanceof Register) rhs = regMap.get(rhs); 79 | reloadUsedRegisterCollection(); 80 | } 81 | 82 | @Override 83 | public void renameDefinedRegister(Function idSupplier) { 84 | dest = ((VirtualRegister) dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 85 | } 86 | 87 | @Override 88 | public void renameUsedRegister(Function idSupplier) { 89 | if (lhs instanceof VirtualRegister) 90 | lhs = ((VirtualRegister) lhs).getSSARenamedRegister(idSupplier.apply((VirtualRegister) lhs)); 91 | if (rhs instanceof VirtualRegister) 92 | rhs = ((VirtualRegister) rhs).getSSARenamedRegister(idSupplier.apply((VirtualRegister) rhs)); 93 | reloadUsedRegisterCollection(); 94 | } 95 | 96 | @Override 97 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 98 | if (lhs == oldValue) lhs = newValue; 99 | if (rhs == oldValue) rhs = newValue; 100 | reloadUsedRegisterCollection(); 101 | } 102 | 103 | @Override 104 | public BinaryOperation copyAndRename(Map renameMap) { 105 | return new BinaryOperation( 106 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 107 | (Register) renameMap.getOrDefault(dest, dest), 108 | op, 109 | (IntValue) renameMap.getOrDefault(lhs, lhs), 110 | (IntValue) renameMap.getOrDefault(rhs, rhs) 111 | ); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/BranchInstruction.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import com.abcdabcd987.compiler2016.BackEnd.SSATransformer; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-04-10. 9 | */ 10 | public abstract class BranchInstruction extends IRInstruction { 11 | public BranchInstruction(BasicBlock BB, IRInstruction prev, IRInstruction next) { 12 | super(BB, prev, next); 13 | } 14 | 15 | protected BranchInstruction(BasicBlock BB) { 16 | super(BB); 17 | } 18 | 19 | public abstract Collection getUsedBasicBlock(); 20 | 21 | @Override 22 | public abstract BranchInstruction copyAndRename(Map renameMap); 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/Call.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-04-14. 7 | */ 8 | public class Call extends IRInstruction { 9 | private Register dest; 10 | private Function func; 11 | private List args = new ArrayList<>(); 12 | 13 | public Call(BasicBlock BB, VirtualRegister dest, Function func) { 14 | super(BB); 15 | this.dest = dest; 16 | this.func = func; 17 | } 18 | 19 | public Register getDest() { 20 | return dest; 21 | } 22 | 23 | public Function getFunc() { 24 | return func; 25 | } 26 | 27 | public Call appendArg(IntValue arg) { 28 | args.add(arg); 29 | if (arg instanceof Register) 30 | usedRegister.add((Register) arg); 31 | return this; 32 | } 33 | 34 | public List getArgs() { 35 | return args; 36 | } 37 | 38 | @Override 39 | public void accept(IIRVisitor visitor) { 40 | visitor.visit(this); 41 | } 42 | 43 | @Override 44 | public Register getDefinedRegister() { 45 | return dest; 46 | } 47 | 48 | @Override 49 | protected void reloadUsedRegisterCollection() { 50 | usedRegister.clear(); 51 | usedIntValue.clear(); 52 | for (IntValue arg : args) { 53 | if (arg instanceof Register) 54 | usedRegister.add((Register) arg); 55 | usedIntValue.add(arg); 56 | } 57 | } 58 | 59 | @Override 60 | public void setDefinedRegister(Register newReg) { 61 | dest = newReg; 62 | } 63 | 64 | @Override 65 | public void setUsedRegister(Map regMap) { 66 | for (int i = 0; i < args.size(); ++i) 67 | if (args.get(i) instanceof Register) { 68 | args.set(i, regMap.get(args.get(i))); 69 | } 70 | reloadUsedRegisterCollection(); 71 | } 72 | 73 | @Override 74 | public void renameDefinedRegister(java.util.function.Function idSupplier) { 75 | if (dest instanceof VirtualRegister) 76 | dest = ((VirtualRegister) dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 77 | } 78 | 79 | @Override 80 | public void renameUsedRegister(java.util.function.Function idSupplier) { 81 | for (int i = 0; i < args.size(); ++i) 82 | if (args.get(i) instanceof VirtualRegister) { 83 | VirtualRegister reg = (VirtualRegister) args.get(i); 84 | args.set(i, reg.getSSARenamedRegister(idSupplier.apply(reg))); 85 | } 86 | reloadUsedRegisterCollection(); 87 | } 88 | 89 | @Override 90 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 91 | for (int i = 0; i < args.size(); ++i) 92 | if (args.get(i) == oldValue) 93 | args.set(i, newValue); 94 | reloadUsedRegisterCollection(); 95 | } 96 | 97 | @Override 98 | public Call copyAndRename(Map renameMap) { 99 | Call call = new Call( 100 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 101 | (VirtualRegister) renameMap.getOrDefault(dest, dest), 102 | func 103 | ); 104 | for (IntValue arg : args) call.appendArg((IntValue) renameMap.getOrDefault(arg, arg)); 105 | return call; 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/HeapAllocate.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-14. 8 | */ 9 | public class HeapAllocate extends IRInstruction { 10 | private Register dest; 11 | private IntValue allocSize; 12 | 13 | public HeapAllocate(BasicBlock BB, VirtualRegister dest, IntValue allocSize) { 14 | super(BB); 15 | this.dest = dest; 16 | this.allocSize = allocSize; 17 | reloadUsedRegisterCollection(); 18 | } 19 | 20 | public IntValue getAllocSize() { 21 | return allocSize; 22 | } 23 | 24 | public Register getDest() { 25 | return dest; 26 | } 27 | 28 | @Override 29 | public void accept(IIRVisitor visitor) { 30 | visitor.visit(this); 31 | } 32 | 33 | @Override 34 | public Register getDefinedRegister() { 35 | return dest; 36 | } 37 | 38 | @Override 39 | protected void reloadUsedRegisterCollection() { 40 | usedRegister.clear(); 41 | if (allocSize instanceof Register) usedRegister.add((Register) allocSize); 42 | usedIntValue.clear(); 43 | usedIntValue.add(allocSize); 44 | } 45 | 46 | @Override 47 | public void setDefinedRegister(Register newReg) { 48 | dest = newReg; 49 | } 50 | 51 | @Override 52 | public void setUsedRegister(Map regMap) { 53 | if (allocSize instanceof Register) allocSize = regMap.get(allocSize); 54 | reloadUsedRegisterCollection(); 55 | } 56 | 57 | @Override 58 | public void renameDefinedRegister(Function idSupplier) { 59 | if (dest instanceof VirtualRegister) 60 | dest = ((VirtualRegister)dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 61 | } 62 | 63 | @Override 64 | public void renameUsedRegister(Function idSupplier) { 65 | if (allocSize instanceof VirtualRegister) 66 | allocSize = ((VirtualRegister) allocSize).getSSARenamedRegister(idSupplier.apply((VirtualRegister) allocSize)); 67 | reloadUsedRegisterCollection(); 68 | } 69 | 70 | @Override 71 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 72 | if (allocSize == oldValue) allocSize = newValue; 73 | reloadUsedRegisterCollection(); 74 | } 75 | 76 | @Override 77 | public IRInstruction copyAndRename(Map renameMap) { 78 | return new HeapAllocate( 79 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 80 | (VirtualRegister) renameMap.getOrDefault(dest, dest), 81 | (IntValue) renameMap.getOrDefault(allocSize, allocSize) 82 | ); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IIRVisitor.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-11. 5 | */ 6 | public interface IIRVisitor { 7 | void visit(IRRoot node); 8 | void visit(BasicBlock node); 9 | void visit(Function node); 10 | 11 | void visit(BinaryOperation node); 12 | void visit(UnaryOperation node); 13 | void visit(IntComparison node); 14 | void visit(IntImmediate node); 15 | void visit(Call node); 16 | void visit(SystemCall node); 17 | void visit(PhiInstruction node); 18 | 19 | void visit(Branch node); 20 | void visit(Return node); 21 | void visit(Jump node); 22 | 23 | void visit(VirtualRegister node); 24 | void visit(PhysicalRegister node); 25 | void visit(StackSlot node); 26 | void visit(HeapAllocate node); 27 | void visit(Load node); 28 | void visit(Store node); 29 | void visit(Move node); 30 | 31 | void visit(StaticSpace node); 32 | void visit(StaticString node); 33 | } 34 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IRInstruction.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import com.abcdabcd987.compiler2016.BackEnd.FunctionInliner; 4 | import com.abcdabcd987.compiler2016.BackEnd.SSATransformer; 5 | 6 | import java.util.*; 7 | 8 | /** 9 | * Created by abcdabcd987 on 2016-04-07. 10 | */ 11 | public abstract class IRInstruction { 12 | protected BasicBlock curBB; 13 | private IRInstruction prev = null; 14 | private IRInstruction next = null; 15 | boolean removed = false; 16 | protected List usedRegister = new ArrayList<>(); 17 | protected List usedIntValue = new ArrayList<>(); 18 | 19 | // liveness analysis 20 | public Set liveIn = null; 21 | public Set liveOut = null; 22 | 23 | public IRInstruction(BasicBlock curBB, IRInstruction prev, IRInstruction next) { 24 | this.curBB = curBB; 25 | this.prev = prev; 26 | this.next = next; 27 | } 28 | 29 | public IRInstruction(BasicBlock curBB) { 30 | this.curBB = curBB; 31 | } 32 | 33 | public void linkNext(IRInstruction node) { 34 | next = node; 35 | node.prev = this; 36 | } 37 | public void linkPrev(IRInstruction node) { 38 | prev = node; 39 | node.next = this; 40 | } 41 | 42 | public void prepend(IRInstruction node) { 43 | if (prev != null) prev.linkNext(node); 44 | else curBB.setHead(node); 45 | node.linkNext(this); 46 | } 47 | 48 | public void append(IRInstruction node) { 49 | if (next != null) next.linkPrev(node); 50 | else curBB.setLast(node); 51 | node.linkPrev(this); 52 | } 53 | 54 | public void remove() { 55 | assert !removed; 56 | if (this instanceof BranchInstruction) curBB.cleanEnd(); 57 | if (prev != null) prev.next = next; 58 | if (next != null) next.prev = prev; 59 | if (curBB.getHead() == this) curBB.setHead(next); 60 | if (curBB.getLast() == this) curBB.setLast(prev); 61 | removed = true; 62 | } 63 | 64 | public abstract void accept(IIRVisitor visitor); 65 | 66 | public final Collection getUsedRegister() { 67 | return Collections.unmodifiableCollection(usedRegister); 68 | } 69 | 70 | public final Collection getUsedIntValue() { 71 | return Collections.unmodifiableCollection(usedIntValue); 72 | } 73 | public abstract Register getDefinedRegister(); 74 | protected abstract void reloadUsedRegisterCollection(); 75 | public abstract void setDefinedRegister(Register newReg); 76 | public abstract void setUsedRegister(Map regMap); 77 | public abstract void renameDefinedRegister(java.util.function.Function idSupplier); 78 | public abstract void renameUsedRegister(java.util.function.Function idSupplier); 79 | public abstract void replaceIntValueUse(IntValue oldValue, IntValue newValue); 80 | 81 | /** 82 | * used by {@link FunctionInliner} 83 | * make a copy of current instruction and rename used value 84 | * @param renameMap the map use to rename used value 85 | * @return return a renamed copy of current instruction 86 | */ 87 | public abstract IRInstruction copyAndRename(Map renameMap); 88 | 89 | public IRInstruction getPrev() { 90 | return prev; 91 | } 92 | 93 | public IRInstruction getNext() { 94 | return next; 95 | } 96 | 97 | public BasicBlock getBasicBlock() { 98 | return curBB; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IRRoot.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-04-13. 7 | */ 8 | public class IRRoot { 9 | public Map functions = new LinkedHashMap<>(); 10 | public Map stringPool = new LinkedHashMap<>(); 11 | public List dataList = new ArrayList<>(); 12 | 13 | public IRRoot() { 14 | stringPool.put("\\n", new StaticString("\\n")); 15 | } 16 | 17 | public void calcRecursiveCalleeSet() { 18 | functions.values().forEach(x -> x.recursiveCalleeSet.clear()); 19 | Set set = new HashSet<>(); 20 | boolean changed = true; 21 | while (changed) { 22 | changed = false; 23 | for (Function func : functions.values()) { 24 | set.clear(); 25 | set.addAll(func.calleeSet); 26 | func.calleeSet.forEach(x -> set.addAll(x.recursiveCalleeSet)); 27 | if (!set.equals(func.recursiveCalleeSet)) { 28 | func.recursiveCalleeSet.clear(); 29 | func.recursiveCalleeSet.addAll(set); 30 | changed = true; 31 | } 32 | } 33 | } 34 | } 35 | 36 | public void accept(IIRVisitor visitor) { 37 | visitor.visit(this); 38 | } 39 | 40 | public Function builtinPrintString; 41 | public Function builtinPrintlnString; 42 | public Function builtinPrintInt; 43 | public Function builtinPrintlnInt; 44 | public Function builtinToString; 45 | public Function builtinGetString; 46 | public Function builtinGetInt; 47 | public Function builtinStringConcat; 48 | public Function builtinStringEqual; 49 | public Function builtinStringLess; 50 | public Function builtinStringParseInt; 51 | public Function builtinStringSubString; 52 | public List builtinFunctions = new ArrayList<>(); 53 | } 54 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IntComparison.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-07. 8 | */ 9 | public class IntComparison extends IRInstruction { 10 | public enum Condition { 11 | EQ, NE, GT, GE, LT, LE 12 | } 13 | 14 | private Register dest; 15 | private Condition cond; 16 | private IntValue lhs; 17 | private IntValue rhs; 18 | 19 | public IntComparison(BasicBlock BB, VirtualRegister dest, Condition cond, IntValue lhs, IntValue rhs) { 20 | super(BB); 21 | this.dest = dest; 22 | this.cond = cond; 23 | this.lhs = lhs; 24 | this.rhs = rhs; 25 | reloadUsedRegisterCollection(); 26 | } 27 | 28 | public Register getDest() { 29 | return dest; 30 | } 31 | 32 | public Condition getCond() { 33 | return cond; 34 | } 35 | 36 | public IntValue getLhs() { 37 | return lhs; 38 | } 39 | 40 | public void setLhs(IntValue value) { 41 | lhs = value; 42 | reloadUsedRegisterCollection(); 43 | } 44 | 45 | public IntValue getRhs() { 46 | return rhs; 47 | } 48 | 49 | @Override 50 | public void accept(IIRVisitor visitor) { 51 | visitor.visit(this); 52 | } 53 | 54 | @Override 55 | public Register getDefinedRegister() { 56 | return dest; 57 | } 58 | 59 | @Override 60 | protected void reloadUsedRegisterCollection() { 61 | usedRegister.clear(); 62 | if (lhs instanceof Register) usedRegister.add((Register) lhs); 63 | if (rhs instanceof Register) usedRegister.add((Register) rhs); 64 | usedIntValue.clear(); 65 | usedIntValue.add(lhs); 66 | usedIntValue.add(rhs); 67 | } 68 | 69 | @Override 70 | public void setDefinedRegister(Register newReg) { 71 | dest = newReg; 72 | } 73 | 74 | @Override 75 | public void setUsedRegister(Map regMap) { 76 | if (lhs instanceof Register) lhs = regMap.get(lhs); 77 | if (rhs instanceof Register) rhs = regMap.get(rhs); 78 | reloadUsedRegisterCollection(); 79 | } 80 | 81 | @Override 82 | public void renameDefinedRegister(Function idSupplier) { 83 | if (dest instanceof VirtualRegister) 84 | dest = ((VirtualRegister) dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 85 | } 86 | 87 | @Override 88 | public void renameUsedRegister(Function idSupplier) { 89 | if (lhs instanceof VirtualRegister) 90 | lhs = ((VirtualRegister) lhs).getSSARenamedRegister(idSupplier.apply((VirtualRegister) lhs)); 91 | if (rhs instanceof VirtualRegister) 92 | rhs = ((VirtualRegister) rhs).getSSARenamedRegister(idSupplier.apply((VirtualRegister) rhs)); 93 | reloadUsedRegisterCollection(); 94 | } 95 | 96 | @Override 97 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 98 | if (lhs == oldValue) lhs = newValue; 99 | if (rhs == oldValue) rhs = newValue; 100 | reloadUsedRegisterCollection(); 101 | } 102 | 103 | @Override 104 | public IRInstruction copyAndRename(Map renameMap) { 105 | return new IntComparison( 106 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 107 | (VirtualRegister) renameMap.getOrDefault(dest, dest), 108 | cond, 109 | (IntValue) renameMap.getOrDefault(lhs, lhs), 110 | (IntValue) renameMap.getOrDefault(rhs, rhs) 111 | ); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IntImmediate.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-07. 5 | */ 6 | public class IntImmediate extends IntValue { 7 | private int value; 8 | 9 | public IntImmediate(int value) { 10 | this.value = value; 11 | } 12 | 13 | @Override 14 | public void accept(IIRVisitor visitor) { 15 | visitor.visit(this); 16 | } 17 | 18 | @Override 19 | public IntImmediate copy() { 20 | return new IntImmediate(value); 21 | } 22 | 23 | public int getValue() { 24 | return value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/IntValue.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import com.abcdabcd987.compiler2016.BackEnd.FunctionInliner; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-04-23. 7 | */ 8 | public abstract class IntValue { 9 | public abstract void accept(IIRVisitor visitor); 10 | 11 | /** 12 | * used in {@link FunctionInliner} 13 | */ 14 | public abstract IntValue copy(); 15 | } 16 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/Jump.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | import java.util.Map; 7 | import java.util.function.Function; 8 | 9 | /** 10 | * Created by abcdabcd987 on 2016-04-08. 11 | */ 12 | public class Jump extends BranchInstruction { 13 | private BasicBlock target; 14 | 15 | public Jump(BasicBlock BB, BasicBlock target) { 16 | super(BB); 17 | this.target = target; 18 | } 19 | 20 | @Override 21 | public void accept(IIRVisitor visitor) { 22 | visitor.visit(this); 23 | } 24 | 25 | @Override 26 | public VirtualRegister getDefinedRegister() { 27 | return null; 28 | } 29 | 30 | @Override 31 | protected void reloadUsedRegisterCollection() { 32 | 33 | } 34 | 35 | @Override 36 | public void setDefinedRegister(Register newReg) { 37 | assert false; 38 | } 39 | 40 | @Override 41 | public void setUsedRegister(Map regMap) { 42 | 43 | } 44 | 45 | @Override 46 | public void renameDefinedRegister(Function idSupplier) { 47 | 48 | } 49 | 50 | @Override 51 | public void renameUsedRegister(Function idSupplier) { 52 | 53 | } 54 | 55 | @Override 56 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 57 | 58 | } 59 | 60 | @Override 61 | public Collection getUsedBasicBlock() { 62 | return Collections.singletonList(target); 63 | } 64 | 65 | @Override 66 | public Jump copyAndRename(Map renameMap) { 67 | return new Jump( 68 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 69 | (BasicBlock) renameMap.getOrDefault(target, target) 70 | ); 71 | } 72 | 73 | public BasicBlock getTarget() { 74 | return target; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/Move.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-23. 8 | */ 9 | public class Move extends IRInstruction { 10 | private Register dest; 11 | private IntValue source; 12 | 13 | public Move(BasicBlock BB, Register dest, IntValue source) { 14 | super(BB); 15 | this.dest = dest; 16 | this.source = source; 17 | reloadUsedRegisterCollection(); 18 | } 19 | 20 | public Register getDest() { 21 | return dest; 22 | } 23 | 24 | public IntValue getSource() { 25 | return source; 26 | } 27 | 28 | @Override 29 | public void accept(IIRVisitor visitor) { 30 | visitor.visit(this); 31 | } 32 | 33 | @Override 34 | public Register getDefinedRegister() { 35 | return dest; 36 | } 37 | 38 | @Override 39 | protected void reloadUsedRegisterCollection() { 40 | usedRegister.clear(); 41 | if (source instanceof Register) usedRegister.add((Register) source); 42 | usedIntValue.clear(); 43 | usedIntValue.add(source); 44 | } 45 | 46 | @Override 47 | public void setDefinedRegister(Register newReg) { 48 | dest = newReg; 49 | } 50 | 51 | @Override 52 | public void setUsedRegister(Map regMap) { 53 | if (source instanceof Register) source = regMap.get(source); 54 | reloadUsedRegisterCollection(); 55 | } 56 | 57 | @Override 58 | public void renameDefinedRegister(Function idSupplier) { 59 | if (dest instanceof VirtualRegister) 60 | dest = ((VirtualRegister) dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 61 | } 62 | 63 | @Override 64 | public void renameUsedRegister(Function idSupplier) { 65 | if (source instanceof VirtualRegister) 66 | source = ((VirtualRegister) source).getSSARenamedRegister(idSupplier.apply((VirtualRegister) source)); 67 | reloadUsedRegisterCollection(); 68 | } 69 | 70 | @Override 71 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 72 | if (source == oldValue) source = newValue; 73 | reloadUsedRegisterCollection(); 74 | } 75 | 76 | @Override 77 | public Move copyAndRename(Map renameMap) { 78 | return new Move( 79 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 80 | (Register) renameMap.getOrDefault(dest, dest), 81 | (IntValue) renameMap.getOrDefault(source, source) 82 | ); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/PhiInstruction.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.function.*; 7 | import java.util.function.Function; 8 | 9 | /** 10 | * Created by abcdabcd987 on 2016-04-07. 11 | */ 12 | public class PhiInstruction extends IRInstruction { 13 | private BasicBlock BB; 14 | public VirtualRegister dest; 15 | public Map paths = new HashMap<>(); 16 | 17 | public PhiInstruction(BasicBlock BB, VirtualRegister dest) { 18 | super(BB); 19 | this.BB = BB; 20 | this.dest = dest; 21 | } 22 | 23 | public void add(VirtualRegister value, BasicBlock from) { 24 | paths.put(from, value); 25 | if (value != null) usedRegister.add(value); 26 | } 27 | 28 | public IntValue getValue(BasicBlock from) { 29 | return paths.get(from); 30 | } 31 | 32 | @Override 33 | public void accept(IIRVisitor visitor) { 34 | visitor.visit(this); 35 | } 36 | 37 | @Override 38 | public Register getDefinedRegister() { 39 | return dest; 40 | } 41 | 42 | @Override 43 | public void remove() { 44 | BB.phi.remove(dest); 45 | } 46 | 47 | @Override 48 | protected void reloadUsedRegisterCollection() { 49 | assert false; 50 | } 51 | 52 | @Override 53 | public void setDefinedRegister(Register newReg) { 54 | assert false; 55 | } 56 | 57 | @Override 58 | public void setUsedRegister(Map regMap) { 59 | assert false; 60 | } 61 | 62 | @Override 63 | public void renameDefinedRegister(Function idSupplier) { 64 | assert false; 65 | } 66 | 67 | @Override 68 | public void renameUsedRegister(Function idSupplier) { 69 | assert false; 70 | } 71 | 72 | @Override 73 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 74 | for (Map.Entry e : paths.entrySet()) 75 | if (e.getValue() == oldValue) 76 | paths.put(e.getKey(), newValue); 77 | } 78 | 79 | @Override 80 | public IRInstruction copyAndRename(Map renameMap) { 81 | // do nothing 82 | assert false; 83 | return null; 84 | } 85 | 86 | public BasicBlock getBasicBlock() { 87 | return BB; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/PhysicalRegister.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-30. 5 | */ 6 | public abstract class PhysicalRegister extends Register { 7 | public abstract boolean isGeneral(); 8 | public abstract boolean isCallerSave(); 9 | public abstract boolean isCalleeSave(); 10 | public abstract String getName(); 11 | 12 | @Override 13 | public IntValue copy() { 14 | // do nothing 15 | return null; 16 | } 17 | 18 | @Override 19 | public void accept(IIRVisitor visitor) { 20 | visitor.visit(this); 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return getName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/Register.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-30. 5 | */ 6 | public abstract class Register extends IntValue { 7 | } 8 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/Return.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-11. 8 | */ 9 | public class Return extends BranchInstruction { 10 | private IntValue ret; 11 | 12 | public Return(BasicBlock BB, IntValue ret) { 13 | super(BB); 14 | this.ret = ret; 15 | reloadUsedRegisterCollection(); 16 | } 17 | 18 | @Override 19 | public void accept(IIRVisitor visitor) { 20 | visitor.visit(this); 21 | } 22 | 23 | @Override 24 | public VirtualRegister getDefinedRegister() { 25 | return null; 26 | } 27 | 28 | @Override 29 | protected void reloadUsedRegisterCollection() { 30 | usedRegister.clear(); 31 | if (ret instanceof Register) usedRegister.add((Register) ret); 32 | usedIntValue.clear(); 33 | if (ret != null) usedIntValue.add(ret); 34 | } 35 | 36 | @Override 37 | public void setDefinedRegister(Register newReg) { 38 | assert false; 39 | } 40 | 41 | @Override 42 | public void setUsedRegister(Map regMap) { 43 | if (ret instanceof Register) ret = regMap.get(ret); 44 | reloadUsedRegisterCollection(); 45 | } 46 | 47 | @Override 48 | public void renameDefinedRegister(Function idSupplier) { 49 | // do nothing 50 | } 51 | 52 | @Override 53 | public void renameUsedRegister(Function idSupplier) { 54 | if (ret instanceof VirtualRegister) 55 | ret = ((VirtualRegister) ret).getSSARenamedRegister(idSupplier.apply((VirtualRegister) ret)); 56 | reloadUsedRegisterCollection(); 57 | } 58 | 59 | @Override 60 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 61 | if (ret == oldValue) ret = newValue; 62 | reloadUsedRegisterCollection(); 63 | } 64 | 65 | @Override 66 | public Collection getUsedBasicBlock() { 67 | return Collections.emptyList(); 68 | } 69 | 70 | @Override 71 | public Return copyAndRename(Map renameMap) { 72 | return new Return( 73 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 74 | (IntValue) renameMap.getOrDefault(ret, ret) 75 | ); 76 | } 77 | 78 | public IntValue getRet() { 79 | return ret; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/StackSlot.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-30. 5 | */ 6 | public class StackSlot extends Register { 7 | private Function parent; 8 | private String hintName; 9 | 10 | public StackSlot(Function parent, String hintName) { 11 | this.parent = parent; 12 | this.hintName = hintName; 13 | parent.stackSlots.add(this); 14 | } 15 | 16 | public Function getParent() { 17 | return parent; 18 | } 19 | 20 | public String getHintName() { 21 | return hintName; 22 | } 23 | 24 | @Override 25 | public void accept(IIRVisitor visitor) { 26 | visitor.visit(this); 27 | } 28 | 29 | @Override 30 | public IntValue copy() { 31 | // do nothing 32 | assert false; 33 | return null; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/StaticData.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-30. 5 | */ 6 | public abstract class StaticData extends Register { 7 | private String hintName; 8 | 9 | public StaticData(String hintName) { 10 | this.hintName = hintName; 11 | } 12 | 13 | @Override 14 | public IntValue copy() { 15 | // do nothing 16 | return this; 17 | } 18 | 19 | public String getHintName() { 20 | return hintName; 21 | } 22 | 23 | public abstract int getRegisterSize(); 24 | } 25 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/StaticSpace.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-04-30. 5 | */ 6 | public class StaticSpace extends StaticData { 7 | public int length; 8 | 9 | public StaticSpace(int length, String hintName) { 10 | super(hintName); 11 | this.length = length; 12 | } 13 | 14 | @Override 15 | public void accept(IIRVisitor visitor) { 16 | visitor.visit(this); 17 | } 18 | 19 | @Override 20 | public int getRegisterSize() { 21 | return length; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/StaticString.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-04-30. 7 | */ 8 | public class StaticString extends StaticData { 9 | public String value; 10 | 11 | public StaticString(String value) { 12 | super("str"); 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public int getRegisterSize() { 18 | return CompilerOptions.getSizePointer(); 19 | } 20 | 21 | @Override 22 | public void accept(IIRVisitor visitor) { 23 | visitor.visit(this); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/SystemCall.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.Map; 4 | import java.util.function.*; 5 | import java.util.function.Function; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-05-03. 9 | */ 10 | public class SystemCall extends IRInstruction { 11 | public SystemCall(BasicBlock curBB) { 12 | super(curBB); 13 | } 14 | 15 | @Override 16 | public void accept(IIRVisitor visitor) { 17 | visitor.visit(this); 18 | } 19 | 20 | @Override 21 | public Register getDefinedRegister() { 22 | return null; 23 | } 24 | 25 | @Override 26 | protected void reloadUsedRegisterCollection() { 27 | 28 | } 29 | 30 | @Override 31 | public void setDefinedRegister(Register newReg) { 32 | assert false; 33 | } 34 | 35 | @Override 36 | public void setUsedRegister(Map regMap) { 37 | // do nothing 38 | } 39 | 40 | @Override 41 | public void renameDefinedRegister(Function idSupplier) { 42 | // do nothing 43 | } 44 | 45 | @Override 46 | public void renameUsedRegister(Function idSupplier) { 47 | // do nothing 48 | } 49 | 50 | @Override 51 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 52 | 53 | } 54 | 55 | @Override 56 | public IRInstruction copyAndRename(Map renameMap) { 57 | // do nothing 58 | assert false; 59 | return null; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/UnaryOperation.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.*; 4 | import java.util.function.Function; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-07. 8 | */ 9 | public class UnaryOperation extends IRInstruction { 10 | public enum UnaryOp { 11 | NEG, NOT 12 | } 13 | 14 | private Register dest; 15 | private UnaryOp op; 16 | private IntValue operand; 17 | 18 | public UnaryOperation(BasicBlock BB, VirtualRegister dest, UnaryOp op, IntValue operand) { 19 | super(BB); 20 | this.dest = dest; 21 | this.op = op; 22 | this.operand = operand; 23 | reloadUsedRegisterCollection(); 24 | } 25 | 26 | @Override 27 | public void accept(IIRVisitor visitor) { 28 | visitor.visit(this); 29 | } 30 | 31 | @Override 32 | public Register getDefinedRegister() { 33 | return dest; 34 | } 35 | 36 | @Override 37 | protected void reloadUsedRegisterCollection() { 38 | usedRegister.clear(); 39 | if (operand instanceof Register) usedRegister.add((Register) operand); 40 | usedIntValue.clear(); 41 | usedIntValue.add(operand); 42 | } 43 | 44 | @Override 45 | public void setDefinedRegister(Register newReg) { 46 | dest = newReg; 47 | } 48 | 49 | @Override 50 | public void setUsedRegister(Map regMap) { 51 | if (operand instanceof Register) operand = regMap.get(operand); 52 | reloadUsedRegisterCollection(); 53 | } 54 | 55 | @Override 56 | public void renameDefinedRegister(Function idSupplier) { 57 | if (dest instanceof VirtualRegister) 58 | dest = ((VirtualRegister) dest).getSSARenamedRegister(idSupplier.apply((VirtualRegister) dest)); 59 | } 60 | 61 | @Override 62 | public void renameUsedRegister(Function idSupplier) { 63 | if (operand instanceof VirtualRegister) 64 | operand = ((VirtualRegister) operand).getSSARenamedRegister(idSupplier.apply((VirtualRegister) operand)); 65 | reloadUsedRegisterCollection(); 66 | } 67 | 68 | @Override 69 | public void replaceIntValueUse(IntValue oldValue, IntValue newValue) { 70 | if (operand == oldValue) operand = newValue; 71 | reloadUsedRegisterCollection(); 72 | } 73 | 74 | @Override 75 | public IRInstruction copyAndRename(Map renameMap) { 76 | return new UnaryOperation( 77 | (BasicBlock) renameMap.getOrDefault(curBB, curBB), 78 | (VirtualRegister) renameMap.getOrDefault(dest, dest), 79 | op, 80 | (IntValue) renameMap.getOrDefault(operand, operand) 81 | ); 82 | } 83 | 84 | public Register getDest() { 85 | return dest; 86 | } 87 | 88 | public UnaryOp getOp() { 89 | return op; 90 | } 91 | 92 | public IntValue getOperand() { 93 | return operand; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/IR/VirtualRegister.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.IR; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-04-23. 8 | */ 9 | public class VirtualRegister extends Register { 10 | private String hintName; 11 | private int ssaId = -1; 12 | private VirtualRegister oldName = null; 13 | private Map newNames = null; 14 | public PhysicalRegister forcedPhysicalRegister = null; 15 | 16 | public VirtualRegister(String hintName) { 17 | this.hintName = hintName; 18 | } 19 | 20 | private VirtualRegister(String hintName, int ssaId, VirtualRegister oldName) { 21 | this.hintName = hintName; 22 | this.ssaId = ssaId; 23 | this.oldName = oldName; 24 | } 25 | 26 | public VirtualRegister getSSARenamedRegister(int id) { 27 | assert ssaId == -1; 28 | if (newNames == null) newNames = new HashMap<>(); 29 | VirtualRegister newName = newNames.get(id); 30 | if (newName == null) { 31 | newName = new VirtualRegister(hintName, id, this); 32 | newNames.put(id, newName); 33 | } 34 | return newName; 35 | } 36 | 37 | public int getSSAId() { 38 | return ssaId; 39 | } 40 | 41 | public VirtualRegister getOldName() { 42 | return oldName; 43 | } 44 | 45 | @Override 46 | public void accept(IIRVisitor visitor) { 47 | visitor.visit(this); 48 | } 49 | 50 | @Override 51 | public VirtualRegister copy() { 52 | return new VirtualRegister(hintName); 53 | } 54 | 55 | public String getHintName() { 56 | return hintName; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return ssaId == -1 ? hintName : hintName + "." + ssaId; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/MIPS/MIPSRegister.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.MIPS; 2 | 3 | import com.abcdabcd987.compiler2016.IR.PhysicalRegister; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-04-30. 7 | */ 8 | public class MIPSRegister extends PhysicalRegister { 9 | private final int id; 10 | private final String name; 11 | private final boolean isGeneral; 12 | private final boolean isCallerSave; 13 | private final boolean isCalleeSave; 14 | 15 | public MIPSRegister(int id, String name, boolean isGeneral, boolean isCallerSave, boolean isCalleeSave) { 16 | this.id = id; 17 | this.name = name; 18 | this.isGeneral = isGeneral; 19 | this.isCallerSave = isCallerSave; 20 | this.isCalleeSave = isCalleeSave; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return name; 26 | } 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | @Override 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | @Override 38 | public boolean isGeneral() { 39 | return isGeneral; 40 | } 41 | 42 | @Override 43 | public boolean isCallerSave() { 44 | return isCallerSave; 45 | } 46 | 47 | @Override 48 | public boolean isCalleeSave() { 49 | return isCalleeSave; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/MidTerm.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016; 2 | 3 | import com.abcdabcd987.compiler2016.AST.Program; 4 | import com.abcdabcd987.compiler2016.FrontEnd.*; 5 | import com.abcdabcd987.compiler2016.Parser.MillLexer; 6 | import com.abcdabcd987.compiler2016.Parser.MillParser; 7 | import com.abcdabcd987.compiler2016.Symbol.GlobalSymbolTable; 8 | import org.antlr.v4.runtime.ANTLRInputStream; 9 | import org.antlr.v4.runtime.CommonTokenStream; 10 | import org.antlr.v4.runtime.tree.ParseTree; 11 | import org.antlr.v4.runtime.tree.ParseTreeWalker; 12 | 13 | import java.io.InputStream; 14 | 15 | /** 16 | * Created by abcdabcd987 on 2016-04-06. 17 | */ 18 | public class MidTerm { 19 | public static void main(String[] argv) { 20 | try { 21 | InputStream is = System.in; 22 | ANTLRInputStream input = new ANTLRInputStream(is); 23 | MillLexer lexer = new MillLexer(input); 24 | CommonTokenStream tokens = new CommonTokenStream(lexer); 25 | MillParser parser = new MillParser(tokens); 26 | 27 | ParseTree tree = parser.program(); 28 | ParseTreeWalker walker = new ParseTreeWalker(); 29 | ASTBuilder astBuilder = new ASTBuilder(); 30 | walker.walk(astBuilder, tree); 31 | Program program = astBuilder.getProgram(); 32 | 33 | CompilationError ce = new CompilationError(); 34 | GlobalSymbolTable sym = new GlobalSymbolTable(); 35 | StructSymbolScanner structSymbolScanner = new StructSymbolScanner(sym, ce); 36 | StructFunctionDeclarator structFunctionDeclarator = new StructFunctionDeclarator(sym, ce); 37 | SemanticChecker semanticChecker = new SemanticChecker(sym, ce); 38 | 39 | program.accept(structSymbolScanner); 40 | program.accept(structFunctionDeclarator); 41 | program.accept(semanticChecker); 42 | //program.accept(printer); 43 | } catch (Exception e) { 44 | //e.printStackTrace(System.err); 45 | System.exit(1); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/ArrayType.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-31. 7 | */ 8 | public class ArrayType extends VariableType { 9 | public VariableType bodyType; 10 | public ArrayType(VariableType bodyType) { 11 | this.type = Types.ARRAY; 12 | this.bodyType = bodyType; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "" + bodyType; 18 | } 19 | 20 | @Override 21 | public String toStructureString(String indent) { 22 | return toString() + "\n"; 23 | } 24 | 25 | @Override 26 | public boolean isSameType(Type rhs) { 27 | if (rhs.type == Types.NULL) return true; 28 | if (rhs.type != Types.ARRAY) return false; 29 | ArrayType t = (ArrayType) rhs; 30 | return bodyType.isSameType(t.bodyType); 31 | } 32 | 33 | @Override 34 | public int getRegisterSize() { 35 | return CompilerOptions.getSizePointer(); 36 | } 37 | 38 | @Override 39 | public int getMemorySize() { 40 | return CompilerOptions.getSizePointer(); 41 | } 42 | 43 | @Override 44 | public boolean isPointerType() { 45 | return true; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/FunctionType.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by abcdabcd987 on 2016-03-31. 10 | */ 11 | public class FunctionType extends Type { 12 | public VariableType returnType; 13 | public String name; 14 | public List argTypes = new ArrayList<>(); 15 | public List argNames = new ArrayList<>(); 16 | 17 | public FunctionType(VariableType returnType, String name) { 18 | this.returnType = returnType; 19 | this.name = name; 20 | this.type = Types.FUNCTION; 21 | } 22 | 23 | public void addArg(String name, VariableType type) { 24 | argNames.add(name); 25 | argTypes.add(type); 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | StringBuilder sb = new StringBuilder(); 31 | sb.append(""); 32 | sb.append("name: ").append(name); 33 | sb.append(", retType: ").append(returnType); 34 | sb.append(", argTypes: ["); 35 | argTypes.stream().forEachOrdered(x -> sb.append(x).append(", ")); 36 | sb.append("]"); 37 | return sb.toString(); 38 | } 39 | 40 | @Override 41 | public String toStructureString(String indent) { 42 | return indent + toString() + "\n"; 43 | } 44 | 45 | @Override 46 | public boolean isSameType(Type rhs) { 47 | return this == rhs; 48 | } 49 | 50 | @Override 51 | public int getRegisterSize() { 52 | return CompilerOptions.getSizePointer(); 53 | } 54 | 55 | @Override 56 | public int getMemorySize() { 57 | return CompilerOptions.getSizePointer(); 58 | } 59 | 60 | @Override 61 | public boolean isPointerType() { 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/PrimitiveType.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-31. 7 | */ 8 | public class PrimitiveType extends VariableType { 9 | public PrimitiveType(Types type) { 10 | this.type = type; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return "" + this.type.name(); 16 | } 17 | 18 | 19 | @Override 20 | public String toStructureString(String indent) { 21 | return indent + toString() + "\n"; 22 | } 23 | 24 | @Override 25 | public boolean isSameType(Type rhs) { 26 | return type == rhs.type; 27 | } 28 | 29 | @Override 30 | public int getRegisterSize() { 31 | switch (type) { 32 | case INT: 33 | return CompilerOptions.getSizeInt(); 34 | case BOOL: 35 | return CompilerOptions.getSizeBool(); 36 | default: 37 | return CompilerOptions.getSizePointer(); 38 | } 39 | } 40 | 41 | @Override 42 | public int getMemorySize() { 43 | return getRegisterSize(); 44 | } 45 | 46 | @Override 47 | public boolean isPointerType() { 48 | switch (type) { 49 | case INT: 50 | case BOOL: 51 | return false; 52 | default: 53 | return true; 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/StructType.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | 5 | /** 6 | * Created by abcdabcd987 on 2016-03-31. 7 | */ 8 | public class StructType extends VariableType { 9 | public String name; 10 | public SymbolTable members; 11 | 12 | public StructType(String name) { 13 | this.type = Types.STRUCT; 14 | this.name = name; 15 | this.members = new SymbolTable(null); 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "" + name; 21 | } 22 | 23 | @Override 24 | public String toStructureString(String indent) { 25 | StringBuilder sb = new StringBuilder(); 26 | sb.append(indent).append(toString()).append(" {\n"); 27 | sb.append(members.toStructureString(indent + " ")); 28 | sb.append(indent).append("}\n"); 29 | return sb.toString(); 30 | } 31 | 32 | @Override 33 | public boolean isSameType(Type rhs) { 34 | return rhs.type == Types.NULL || this == rhs; 35 | } 36 | 37 | @Override 38 | public int getRegisterSize() { 39 | return CompilerOptions.getSizePointer(); 40 | } 41 | 42 | @Override 43 | public int getMemorySize() { 44 | return members.getSize(); 45 | } 46 | 47 | @Override 48 | public boolean isPointerType() { 49 | return true; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/SymbolInfo.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import com.abcdabcd987.compiler2016.IR.Register; 4 | import com.abcdabcd987.compiler2016.IR.StaticData; 5 | import com.abcdabcd987.compiler2016.IR.VirtualRegister; 6 | 7 | /** 8 | * Created by abcdabcd987 on 2016-04-11. 9 | */ 10 | public class SymbolInfo { 11 | private Type type; 12 | private int offset; 13 | public Register register = null; 14 | 15 | public SymbolInfo(Type type, int offset) { 16 | this.type = type; 17 | this.offset = offset; 18 | } 19 | 20 | public Type getType() { 21 | return type; 22 | } 23 | 24 | public int getOffset() { 25 | return offset; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/SymbolTable.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by abcdabcd987 on 2016-03-31. 8 | */ 9 | public class SymbolTable { 10 | private Map map = new LinkedHashMap<>(); 11 | private SymbolTable enclosingScope; 12 | private int offset = 0; 13 | private boolean isGlobal = false; 14 | 15 | public SymbolTable(SymbolTable enclosingScope) { 16 | this.enclosingScope = enclosingScope; 17 | } 18 | 19 | public static SymbolTable createGlobalSymbolTable() { 20 | SymbolTable sym = new SymbolTable(null); 21 | sym.isGlobal = true; 22 | return sym; 23 | } 24 | 25 | public void define(String name, Type type) { 26 | map.put(name, new SymbolInfo(type, offset)); 27 | offset += type.getRegisterSize(); 28 | } 29 | 30 | public SymbolInfo getInfoCurrent(String name) { return map.get(name); } 31 | 32 | public SymbolInfo getInfo(String name) { 33 | SymbolInfo t = map.get(name); 34 | if (t != null) return t; 35 | if (enclosingScope != null) return enclosingScope.getInfo(name); 36 | return null; 37 | } 38 | 39 | public int getSize() { 40 | return offset; 41 | } 42 | 43 | public Type getTypeCurrent(String name) { 44 | SymbolInfo info = getInfoCurrent(name); 45 | return info != null ? info.getType() : null; 46 | } 47 | 48 | public Type getType(String name) { 49 | SymbolInfo info = getInfo(name); 50 | return info != null ? info.getType() : null; 51 | } 52 | 53 | public SymbolTable getEnclosingScope() { 54 | return enclosingScope; 55 | } 56 | 57 | public String toStructureString(String indent) { 58 | StringBuilder sb = new StringBuilder(); 59 | map.entrySet().stream().forEachOrdered(x -> 60 | sb.append(indent).append(x.getKey()).append(": ").append(x.getValue()).append("\n") 61 | ); 62 | return sb.toString(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/Type.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-31. 5 | */ 6 | public abstract class Type { 7 | public enum Types { 8 | INT, BOOL, STRING, VOID, ARRAY, STRUCT, FUNCTION, NULL 9 | } 10 | 11 | public Types type; 12 | 13 | public abstract String toStructureString(String indent); 14 | 15 | public abstract boolean isSameType(Type rhs); 16 | 17 | public abstract int getRegisterSize(); 18 | 19 | public abstract int getMemorySize(); 20 | 21 | public abstract boolean isPointerType(); 22 | } 23 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Symbol/VariableType.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Symbol; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-03-31. 5 | */ 6 | public abstract class VariableType extends Type { 7 | } 8 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Utility/TeeOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Utility; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | import java.util.List; 9 | 10 | /** 11 | * Created by abcdabcd987 on 2016-04-18. 12 | */ 13 | public class TeeOutputStream extends OutputStream { 14 | private List streams = new ArrayList<>(); 15 | 16 | public TeeOutputStream(OutputStream... streams) { 17 | super(); 18 | Collections.addAll(this.streams, streams); 19 | } 20 | 21 | public void add(OutputStream outputStream) { 22 | streams.add(outputStream); 23 | } 24 | 25 | @Override 26 | public void write(int b) throws IOException { 27 | for (OutputStream stream : streams) stream.write(b); 28 | } 29 | 30 | @Override 31 | public void write(byte[] b) throws IOException { 32 | for (OutputStream stream : streams) stream.write(b); 33 | } 34 | 35 | @Override 36 | public void write(byte[] b, int off, int len) throws IOException { 37 | for (OutputStream stream : streams) stream.write(b, off, len); 38 | } 39 | 40 | @Override 41 | public void flush() throws IOException { 42 | for (OutputStream stream : streams) stream.flush(); 43 | } 44 | 45 | @Override 46 | public void close() throws IOException { 47 | super.close(); 48 | // do not close streams 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/abcdabcd987/compiler2016/Utility/Utility.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Utility; 2 | 3 | /** 4 | * Created by abcdabcd987 on 2016-05-03. 5 | */ 6 | public class Utility { 7 | public static String unescape(String s) { 8 | StringBuilder sb = new StringBuilder(); 9 | for (int i = 0; i < s.length(); ++i) { 10 | if (s.charAt(i) != '\\') { 11 | sb.append(s.charAt(i)); 12 | } else { 13 | ++i; 14 | switch (s.charAt(i)) { 15 | case 't': sb.append('\t'); break; 16 | case 'n': sb.append('\n'); break; 17 | case 'r': sb.append('\r'); break; 18 | case '\'':sb.append('\''); break; 19 | case '"': sb.append('"'); break; 20 | case '\\':sb.append('\\'); break; 21 | default: sb.append(s.charAt(i)); 22 | } 23 | } 24 | } 25 | return sb.toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/com/abcdabcd987/compiler2016/BackEnd/CodeGenTest.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.BackEnd; 2 | 3 | import com.abcdabcd987.compiler2016.CompilerOptions; 4 | import com.abcdabcd987.compiler2016.Mill; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.junit.runners.Parameterized; 8 | 9 | import java.io.File; 10 | import java.io.FileInputStream; 11 | import java.io.InputStream; 12 | import java.util.ArrayList; 13 | import java.util.Collection; 14 | 15 | /** 16 | * Created by abcdabcd987 on 2016-04-13. 17 | */ 18 | @RunWith(Parameterized.class) 19 | public class CodeGenTest { 20 | @Parameterized.Parameters 21 | public static Collection data() { 22 | Collection params = new ArrayList<>(); 23 | for (File f : new File("testcase/ir/").listFiles()) { 24 | if (f.isFile() && f.getName().endsWith(".mx")) { 25 | params.add(new Object[] { "testcase/ir/" + f.getName() }); 26 | } 27 | } 28 | return params; 29 | } 30 | 31 | private String filename; 32 | 33 | public CodeGenTest(String filename) { 34 | this.filename = filename; 35 | } 36 | 37 | @Test 38 | public void testPass() throws Exception { 39 | CompilerOptions.ifPrintAST = false; 40 | CompilerOptions.ifPrintRawIR = true; 41 | CompilerOptions.ifPrintSSAIR = false; 42 | CompilerOptions.enableSSA = false; 43 | CompilerOptions.registerAllocator = "no"; 44 | 45 | System.out.println(filename); 46 | InputStream is = new FileInputStream(filename); 47 | new Mill(is, System.out).run(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test/com/abcdabcd987/compiler2016/FrontEnd/ASTBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.FrontEnd; 2 | 3 | import com.abcdabcd987.compiler2016.AST.Program; 4 | import com.abcdabcd987.compiler2016.Parser.MillLexer; 5 | import com.abcdabcd987.compiler2016.Parser.MillParser; 6 | import org.antlr.v4.runtime.ANTLRInputStream; 7 | import org.antlr.v4.runtime.BailErrorStrategy; 8 | import org.antlr.v4.runtime.CommonTokenStream; 9 | import org.antlr.v4.runtime.tree.ParseTree; 10 | import org.antlr.v4.runtime.tree.ParseTreeWalker; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.junit.runners.Parameterized; 14 | 15 | import java.io.File; 16 | import java.io.FileInputStream; 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | 22 | /** 23 | * Created by abcdabcd987 on 2016-03-28. 24 | */ 25 | @RunWith(Parameterized.class) 26 | public class ASTBuilderTest { 27 | @Parameterized.Parameters 28 | public static Collection data() { 29 | Collection params = new ArrayList<>(); 30 | for (File f : new File("testcase/").listFiles()) { 31 | if (f.isFile() && f.getName().endsWith(".mill")) { 32 | params.add(new Object[] { "testcase/" + f.getName() }); 33 | } 34 | } 35 | return params; 36 | } 37 | 38 | private String filename; 39 | 40 | public ASTBuilderTest(String filename) { 41 | this.filename = filename; 42 | } 43 | 44 | @Test 45 | public void testAST() throws IOException { 46 | System.out.println(filename); 47 | 48 | InputStream is = new FileInputStream(filename); 49 | ANTLRInputStream input = new ANTLRInputStream(is); 50 | MillLexer lexer = new MillLexer(input); 51 | CommonTokenStream tokens = new CommonTokenStream(lexer); 52 | MillParser parser = new MillParser(tokens); 53 | parser.setErrorHandler(new BailErrorStrategy()); 54 | 55 | ParseTree tree = parser.program(); 56 | ParseTreeWalker walker = new ParseTreeWalker(); 57 | ASTBuilder astBuilder = new ASTBuilder(); 58 | walker.walk(astBuilder, tree); 59 | ASTPrinter astPrinter = new ASTPrinter(System.out); 60 | Program program = astBuilder.getProgram(); 61 | program.accept(astPrinter); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /test/com/abcdabcd987/compiler2016/FrontEnd/SemanticCheckerTest.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.FrontEnd; 2 | 3 | import com.abcdabcd987.compiler2016.AST.Program; 4 | import com.abcdabcd987.compiler2016.Parser.MillLexer; 5 | import com.abcdabcd987.compiler2016.Parser.MillParser; 6 | import com.abcdabcd987.compiler2016.Symbol.GlobalSymbolTable; 7 | import org.antlr.v4.runtime.ANTLRInputStream; 8 | import org.antlr.v4.runtime.CommonTokenStream; 9 | import org.antlr.v4.runtime.tree.ParseTree; 10 | import org.antlr.v4.runtime.tree.ParseTreeWalker; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.junit.runners.Parameterized; 14 | import static org.junit.Assert.*; 15 | 16 | import java.io.File; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.Collection; 22 | 23 | /** 24 | * Created by abcdabcd987 on 2016-04-02. 25 | */ 26 | @RunWith(Parameterized.class) 27 | public class SemanticCheckerTest { 28 | @Parameterized.Parameters 29 | public static Collection data() { 30 | Collection params = new ArrayList<>(); 31 | for (File f : new File("testcase/semantic/passed/").listFiles()) { 32 | if (f.isFile() && f.getName().endsWith(".mx")) { 33 | params.add(new Object[] { "testcase/semantic/passed/" + f.getName(), true }); 34 | } 35 | } 36 | for (File f : new File("testcase/semantic/compile_error/").listFiles()) { 37 | if (f.isFile() && f.getName().endsWith(".mx")) { 38 | params.add(new Object[] { "testcase/semantic/compile_error/" + f.getName(), false }); 39 | } 40 | } 41 | return params; 42 | } 43 | 44 | private String filename; 45 | private boolean shouldPass; 46 | 47 | public SemanticCheckerTest(String filename, boolean shouldPass) { 48 | this.filename = filename; 49 | this.shouldPass = shouldPass; 50 | } 51 | 52 | @Test 53 | public void testPass() throws IOException { 54 | System.out.println(filename); 55 | 56 | try { 57 | InputStream is = new FileInputStream(filename); 58 | ANTLRInputStream input = new ANTLRInputStream(is); 59 | MillLexer lexer = new MillLexer(input); 60 | CommonTokenStream tokens = new CommonTokenStream(lexer); 61 | MillParser parser = new MillParser(tokens); 62 | 63 | ParseTree tree = parser.program(); 64 | ParseTreeWalker walker = new ParseTreeWalker(); 65 | ASTBuilder astBuilder = new ASTBuilder(); 66 | walker.walk(astBuilder, tree); 67 | Program program = astBuilder.getProgram(); 68 | ASTPrinter printer = new ASTPrinter(System.out); 69 | 70 | CompilationError ce = new CompilationError(); 71 | GlobalSymbolTable sym = new GlobalSymbolTable(); 72 | StructSymbolScanner structSymbolScanner = new StructSymbolScanner(sym, ce); 73 | program.accept(structSymbolScanner); 74 | StructFunctionDeclarator structFunctionDeclarator = new StructFunctionDeclarator(sym, ce); 75 | program.accept(structFunctionDeclarator); 76 | SemanticChecker semanticChecker = new SemanticChecker(sym, ce); 77 | program.accept(semanticChecker); 78 | program.accept(printer); 79 | if (!shouldPass) fail("Should not pass."); 80 | } catch (Exception e) { 81 | if (shouldPass) throw e; 82 | else e.printStackTrace(System.err); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /test/com/abcdabcd987/compiler2016/FrontEnd/StructFunctionSymbolTest.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.FrontEnd; 2 | 3 | import com.abcdabcd987.compiler2016.AST.Program; 4 | import com.abcdabcd987.compiler2016.Parser.MillLexer; 5 | import com.abcdabcd987.compiler2016.Parser.MillParser; 6 | import com.abcdabcd987.compiler2016.Symbol.GlobalSymbolTable; 7 | import org.antlr.v4.runtime.ANTLRInputStream; 8 | import org.antlr.v4.runtime.BailErrorStrategy; 9 | import org.antlr.v4.runtime.CommonTokenStream; 10 | import org.antlr.v4.runtime.tree.ParseTree; 11 | import org.antlr.v4.runtime.tree.ParseTreeWalker; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.junit.runners.Parameterized; 15 | 16 | import java.io.File; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.Collection; 22 | 23 | /** 24 | * Created by abcdabcd987 on 2016-03-31. 25 | */ 26 | @RunWith(Parameterized.class) 27 | public class StructFunctionSymbolTest { 28 | @Parameterized.Parameters 29 | public static Collection data() { 30 | Collection params = new ArrayList<>(); 31 | for (File f : new File("testcase/").listFiles()) { 32 | if (f.isFile() && f.getName().endsWith(".mill")) { 33 | params.add(new Object[] { "testcase/" + f.getName() }); 34 | } 35 | } 36 | return params; 37 | } 38 | 39 | private String filename; 40 | 41 | public StructFunctionSymbolTest(String filename) { 42 | this.filename = filename; 43 | } 44 | 45 | @Test 46 | public void testAST() throws IOException { 47 | System.out.println(filename); 48 | 49 | InputStream is = new FileInputStream(filename); 50 | ANTLRInputStream input = new ANTLRInputStream(is); 51 | MillLexer lexer = new MillLexer(input); 52 | CommonTokenStream tokens = new CommonTokenStream(lexer); 53 | MillParser parser = new MillParser(tokens); 54 | parser.setErrorHandler(new BailErrorStrategy()); 55 | 56 | ParseTree tree = parser.program(); 57 | ParseTreeWalker walker = new ParseTreeWalker(); 58 | ASTBuilder astBuilder = new ASTBuilder(); 59 | walker.walk(astBuilder, tree); 60 | Program program = astBuilder.getProgram(); 61 | 62 | CompilationError ce = new CompilationError(); 63 | GlobalSymbolTable sym = new GlobalSymbolTable(); 64 | StructSymbolScanner structSymbolScanner = new StructSymbolScanner(sym, ce); 65 | program.accept(structSymbolScanner); 66 | StructFunctionDeclarator structFunctionDeclarator = new StructFunctionDeclarator(sym, ce); 67 | program.accept(structFunctionDeclarator); 68 | 69 | System.out.println(sym.toStructureString()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /test/com/abcdabcd987/compiler2016/Parser/ParserTest.java: -------------------------------------------------------------------------------- 1 | package com.abcdabcd987.compiler2016.Parser; 2 | 3 | import org.antlr.v4.runtime.*; 4 | import org.antlr.v4.runtime.tree.*; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.junit.runners.Parameterized; 9 | import org.junit.runners.Parameterized.Parameters; 10 | 11 | import java.io.*; 12 | import java.util.ArrayList; 13 | import java.util.Collection; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | /** 18 | * Created by abcdabcd987 on 2016-03-24. 19 | */ 20 | @RunWith(Parameterized.class) 21 | public class ParserTest { 22 | 23 | @Parameters 24 | public static Collection data() { 25 | Collection params = new ArrayList<>(); 26 | for (File f : new File("testcase/").listFiles()) { 27 | if (f.isFile() && f.getName().endsWith(".mill")) { 28 | params.add(new Object[] { "testcase/" + f.getName() }); 29 | } 30 | } 31 | return params; 32 | } 33 | 34 | private String filename; 35 | 36 | public ParserTest(String filename) { 37 | this.filename = filename; 38 | } 39 | 40 | @Test 41 | public void testNoException() throws IOException { 42 | System.out.println(filename); 43 | 44 | InputStream is = new FileInputStream(filename); 45 | ANTLRInputStream input = new ANTLRInputStream(is); 46 | MillLexer lexer = new MillLexer(input); 47 | CommonTokenStream tokens = new CommonTokenStream(lexer); 48 | MillParser parser = new MillParser(tokens); 49 | parser.setErrorHandler(new BailErrorStrategy()); 50 | 51 | parser.program(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /testcase/error/selfref-5090379042-jiaxiao.mill: -------------------------------------------------------------------------------- 1 | int f(int anything) { 2 | return 0; 3 | } 4 | 5 | int main() { 6 | int x = f(x); 7 | printf("%d\n", x); 8 | return x; 9 | } 10 | -------------------------------------------------------------------------------- /testcase/final/a-function-call.mx: -------------------------------------------------------------------------------- 1 | void foo(int d, int x, int y) { 2 | println(toString(d*1000 + x*10 + y)); 3 | if (d == 1) return; 4 | int t = x; x = y; y = t; 5 | foo(1, x, y); 6 | println(toString(d*1000 + x*10 + y)); 7 | } 8 | int main() { 9 | foo(7, 5, 3); 10 | } -------------------------------------------------------------------------------- /testcase/final/a-function-call.out: -------------------------------------------------------------------------------- 1 | 7053 2 | 1035 3 | 7035 -------------------------------------------------------------------------------- /testcase/final/array_test1-mahaojun.in: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | -------------------------------------------------------------------------------- /testcase/final/array_test1-mahaojun.mx: -------------------------------------------------------------------------------- 1 | //input: 1 2 3 4 2 | 3 | int[] a = new int[4]; 4 | int main() 5 | { 6 | int[] b = new int[a.size()]; 7 | int i; 8 | for (i = 0; i < a.size(); i++) 9 | { 10 | a[i] = 0; 11 | b[i] = getInt(); 12 | } 13 | for (i = 0; i < a.size(); i++) 14 | { 15 | print(toString(a[i])); 16 | } 17 | println(""); 18 | a=b; 19 | for (i = 0; i < a.size(); i++) 20 | { 21 | print(toString(a[i])); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /testcase/final/array_test1-mahaojun.out: -------------------------------------------------------------------------------- 1 | 0000 2 | 1234 -------------------------------------------------------------------------------- /testcase/final/array_test2-mahaojun.in: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | -------------------------------------------------------------------------------- /testcase/final/array_test2-mahaojun.mx: -------------------------------------------------------------------------------- 1 | // input: 1 2 3 4 2 | 3 | int[] a = new int[4]; 4 | int main() 5 | { 6 | int[][] b = new int[4][]; 7 | int i; 8 | b[0] = a; 9 | b[1] = a; 10 | b[2] = a; 11 | b[3] = a; 12 | println(toString(b.size())); 13 | for (i = 0; i < b[0].size(); i++) 14 | b[0][i] = getInt(); 15 | for (i = 0; i < b[1].size(); i++) 16 | print(toString(b[1][i])); 17 | println(""); 18 | for (i = 0; i < b[2].size(); i++) 19 | b[2][i] = 0; 20 | for (i = 0; i < b[3].size(); i++) 21 | print(toString(b[3][i])); 22 | } 23 | -------------------------------------------------------------------------------- /testcase/final/array_test2-mahaojun.out: -------------------------------------------------------------------------------- 1 | 4 2 | 1234 3 | 0000 -------------------------------------------------------------------------------- /testcase/final/basicopt1-5100309127-hetianxing.limit: -------------------------------------------------------------------------------- 1 | 700000 2 | -------------------------------------------------------------------------------- /testcase/final/basicopt1-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int[][] a = new int[100][]; 4 | int i; 5 | int j; 6 | for (i = 0; i < 100; i++) 7 | a[i] = new int[100]; 8 | int sum = 0; 9 | 10 | for (i = 0;i < 100;i++) 11 | for (j = 0;j < 100;j++) 12 | a[i][j] = 0; 13 | int quotient; 14 | int remainder; 15 | for (i = 0;i < 100;i++) 16 | if (i > 20 && i < 80) { 17 | for (j = 0;j < 100;j++) 18 | if (j > 5 || i < 90) { 19 | quotient = j * 4 / 100; 20 | remainder = j * 4 % 100; 21 | a[i + quotient][remainder] = j + (100 - 1 + 1 - 1 + 1) / 2; 22 | } 23 | } 24 | 25 | for (i = 0;i < 100;i++) 26 | for (j = 0;j < 100;j++) 27 | sum = sum + a[i][j]; 28 | println(toString(sum)); 29 | } 30 | -------------------------------------------------------------------------------- /testcase/final/basicopt1-5100309127-hetianxing.out: -------------------------------------------------------------------------------- 1 | 99850 2 | -------------------------------------------------------------------------------- /testcase/final/builtin-5140519064-youyurong.in: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /testcase/final/builtin-5140519064-youyurong.mx: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int[] a = new int[3]; 3 | print("I have done " + toString(a.size()) + " little things"); 4 | println("too young!"); 5 | print(toString("\"hahaha\"".length())); 6 | println("four scores and seven years".substring(0, getInt())); 7 | println(toString("45 best songs in the world".parseInt())); 8 | return "45 best songs".ord(5); 9 | } 10 | -------------------------------------------------------------------------------- /testcase/final/builtin-5140519064-youyurong.out: -------------------------------------------------------------------------------- 1 | I have done 3 little thingstoo young! 2 | 8four scores 3 | 45 4 | -------------------------------------------------------------------------------- /testcase/final/bulgarian-5110379024-wuhang.limit: -------------------------------------------------------------------------------- 1 | 1500000 2 | -------------------------------------------------------------------------------- /testcase/final/bulgarian-5110379024-wuhang.mx: -------------------------------------------------------------------------------- 1 | // Target: Simulate a Bulgarian-solitaire game. 2 | // Possible opitimization: Dead code elimination, common expression, inline function, loop unrolling, etc. 3 | // REMARKS: A funny game. If you like, you can try to prove that when n=1+2+..+i(i>0), the game will always stop 4 | // and converge to the only solution: {1,2,...i}. :) 5 | 6 | int n; 7 | int h; 8 | int now; 9 | int[] a; 10 | int A = 48271; 11 | int M = 2147483647; 12 | int Q; 13 | int R; 14 | int seed=1; 15 | int random() { 16 | int tempseed = A * (seed % Q) - R * (seed / Q); 17 | if (tempseed >= 0) 18 | seed = tempseed; 19 | else 20 | seed = tempseed + M; 21 | return seed; 22 | } 23 | void initialize(int val) { 24 | seed = val; 25 | } 26 | void swap(int x,int y) { 27 | int temp = a[x]; 28 | a[x] = a[y]; 29 | a[y] = temp; 30 | } 31 | bool pd(int x) { 32 | for (;h <= x; ++h) 33 | if (x == h * (h + 1) / 2) 34 | return true; 35 | return false; 36 | } 37 | void show() { 38 | int i; 39 | for (i = 0; i < now; ++i) 40 | print(toString(a[i]) + " "); 41 | println(""); 42 | } 43 | bool win() 44 | { 45 | int i; 46 | int j; 47 | int[] b = new int[100]; 48 | int temp; 49 | if (now != h) 50 | return false; 51 | for (j = 0; j < now; ++j) 52 | b[j] = a[j]; 53 | for (i = 0;i < now - 1; ++i) 54 | for (j = i + 1;j < now; ++j) 55 | if (b[i] > b[j]) { 56 | temp = b[i]; 57 | b[i] = b[j]; 58 | b[j] = temp; 59 | } 60 | for (i = 0; i < now; ++i) 61 | if (b[i] != i + 1) 62 | return false; 63 | return true; 64 | } 65 | void merge() 66 | { 67 | int i; 68 | for (i = 0;i < now; ++i) 69 | if (a[i] == 0) { 70 | int j; 71 | for (j = i+1; j < now; ++j) 72 | if (a[j] != 0) { 73 | swap(i,j); 74 | break; 75 | } 76 | } 77 | for (i=0;i n) 113 | a[i] = random() % 10 + 1; 114 | temp = temp + a[i]; 115 | } 116 | a[now - 1] = n - temp; 117 | show(); 118 | merge(); 119 | while (!win()) { 120 | println("step " + toString(++count) + ":"); 121 | move(); 122 | merge(); 123 | show(); 124 | } 125 | println("Total: " + toString(count) + " step(s)"); 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /testcase/final/class_test-mahaojun.mx: -------------------------------------------------------------------------------- 1 | 2 | class TA{ 3 | string state; 4 | int anger; 5 | } 6 | int init_anger = 100; 7 | int work_anger = 10; 8 | void work(string st, TA ta) 9 | { 10 | if (ta.anger <= 100) println(st + ", " + ta.state + " enjoys this work. XD"); 11 | else println(st + ", " + ta.state + " wants to give up!!!!!"); 12 | ta.anger = ta.anger + work_anger; 13 | } 14 | int main() 15 | { 16 | TA mr; 17 | TA mars; 18 | mr = new TA; 19 | mr.state = "the leading TA"; 20 | mr.anger = 0; 21 | mars = new TA; 22 | mars.state = "the striking TA"; 23 | mars.anger = init_anger; 24 | work("MR", mr); 25 | work("Mars", mars); 26 | work("Mars", mars); 27 | } -------------------------------------------------------------------------------- /testcase/final/class_test-mahaojun.out: -------------------------------------------------------------------------------- 1 | MR, the leading TA enjoys this work. XD 2 | Mars, the striking TA enjoys this work. XD 3 | Mars, the striking TA wants to give up!!!!! 4 | -------------------------------------------------------------------------------- /testcase/final/expr-5110309085-jintianxing.limit: -------------------------------------------------------------------------------- 1 | 40000 2 | -------------------------------------------------------------------------------- /testcase/final/expr-5110309085-jintianxing.out: -------------------------------------------------------------------------------- 1 | -66060719 -323398799 -743275679 2 | -------------------------------------------------------------------------------- /testcase/final/function_test-huyuncong.in: -------------------------------------------------------------------------------- 1 | 5 2 | 5 3 | ABCDE 4 | 1 5 | 2 6 | 3 7 | 4 8 | 5 9 | -------------------------------------------------------------------------------- /testcase/final/function_test-huyuncong.mx: -------------------------------------------------------------------------------- 1 | //考察点:section 9 函数,包括函数定义,内建函数 2 | //算法:斜堆 3 | //样例输入: 4 | //5 5 5 | //ABCDE 6 | //1 2 3 4 5 7 | //样例输出: 8 | //5 E 9 | //10 10 | int N; 11 | int M; 12 | string ch; 13 | 14 | int[] l; 15 | int[] r; 16 | int[] w; 17 | 18 | int merge(int x,int y) 19 | { 20 | if (0 == x) return y; 21 | if (0 == y) return x; 22 | if (w[x] < w[y]) { 23 | int e = x; 24 | x = y; 25 | y = e; 26 | } 27 | r[x] = merge(r[x],y); 28 | int e = l[x]; 29 | l[x] = r[x]; 30 | r[x] = e; 31 | return x; 32 | } 33 | 34 | int main() 35 | { 36 | N = getInt(); 37 | M = getInt(); 38 | ch = getString(); 39 | l = new int[N+M+5]; 40 | r = new int[N+M+5]; 41 | w = new int[N+M+5]; 42 | int i; 43 | for (i=1;i <= N;i++) { 44 | w[i] = getInt(); 45 | l[i] = 0; 46 | r[i] = 0; 47 | } 48 | for (i=1;i <= M;i++) { 49 | w[i + N] = ch.ord(i-1); 50 | l[i + N] = 0; 51 | r[i + N] = 0; 52 | } 53 | int rt0 = 1; 54 | int rt1 = N + 1; 55 | for (i = 2;i <= N;i++) 56 | rt0 = merge(rt0,i); 57 | for (i = N+2;i<= N+M;i++) 58 | rt1 = merge(rt1,i); 59 | print(toString(w[rt0])); 60 | print(" "); 61 | print(ch.substring(rt1-N-1,rt1-N-1)); 62 | print("\n"); 63 | println(toString(merge(rt0,rt1))); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /testcase/final/function_test-huyuncong.out: -------------------------------------------------------------------------------- 1 | 5 E 2 | 10 3 | -------------------------------------------------------------------------------- /testcase/final/gcd-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | int gcd(int x, int y) { 2 | if (x%y == 0) return y; 3 | else return gcd(y, x%y); 4 | } 5 | 6 | int main() { 7 | println(toString(gcd(10,1))); 8 | println(toString(gcd(34986,3087))); 9 | println(toString(gcd(2907,1539))); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /testcase/final/gcd-5090379042-jiaxiao.out: -------------------------------------------------------------------------------- 1 | 1 2 | 1029 3 | 171 4 | -------------------------------------------------------------------------------- /testcase/final/hanoi-5100379110-daibo.in: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /testcase/final/hanoi-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 450000 2 | -------------------------------------------------------------------------------- /testcase/final/hanoi-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int cd(int d, string a, string b, string c, int sum) { 2 | if (d == 1) { 3 | println("move " + a + " --> " + c); 4 | sum++; 5 | } else { 6 | sum = cd(d - 1, a, c, b, sum); 7 | println("move " + a + " --> " + c); 8 | sum = cd(d - 1, b, a, c, sum); 9 | sum++; 10 | } 11 | return sum; 12 | } 13 | 14 | int main() { 15 | string a = "A"; 16 | string b = "B"; 17 | string c = "C"; 18 | int d = getInt(); 19 | int sum = cd(d, a, b, c, 0); 20 | println(toString(sum)); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /testcase/final/hashmap-5100309127-hetianxing.limit: -------------------------------------------------------------------------------- 1 | 550000 2 | -------------------------------------------------------------------------------- /testcase/final/hashmap-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | int hashsize = 100; 2 | class node { 3 | int key; 4 | int data; 5 | node next; 6 | } 7 | node[] table; 8 | int getHash(int n) { 9 | return (n * 237) % hashsize; 10 | } 11 | void put(int key, int data) { 12 | int p; 13 | node ptr = null; 14 | p = getHash(key); 15 | if (table[p] == null) { 16 | table[p] = new node; 17 | table[p].key = key; 18 | table[p].data = data; 19 | table[p].next = null; 20 | return; 21 | } 22 | ptr = table[p]; 23 | while (ptr.key != key) { 24 | if (ptr.next == null) { 25 | ptr.next = new node; 26 | ptr.next.key = key; 27 | ptr.next.next = null; 28 | } 29 | ptr = ptr.next; 30 | } 31 | ptr.data = data; 32 | } 33 | int get(int key) { 34 | node ptr = null; 35 | ptr = table[getHash(key)]; 36 | while (ptr.key != key) { 37 | ptr = ptr.next; 38 | } 39 | return ptr.data; 40 | } 41 | int main() { 42 | int i; 43 | table = new node[100]; 44 | for (i = 0;i < hashsize;i++) 45 | table[i] = null; 46 | for (i = 0;i < 1000;i++) 47 | put(i, i); 48 | for (i = 0;i < 1000;i++) 49 | println(toString(i) + " " + toString(get(i))); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /testcase/final/heapsort-5100379110-daibo.in: -------------------------------------------------------------------------------- 1 | 9987 2 | 3 | -------------------------------------------------------------------------------- /testcase/final/heapsort-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 20000000 2 | -------------------------------------------------------------------------------- /testcase/final/heapsort-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int n; 2 | int[] a; 3 | 4 | void exchange(int x,int y) 5 | { 6 | int t=a[x]; 7 | a[x]=a[y]; 8 | a[y]=t; 9 | } 10 | 11 | int makeHeap() { 12 | int i; 13 | int t; 14 | int j; 15 | i = (n - 1) / 2; 16 | t = j = 0; 17 | while (i >= 0) { 18 | j = i * 2; 19 | if (i*2+1 a[j]) { 21 | exchange(i,j); 22 | } 23 | i = i - 1; 24 | } 25 | return 0; 26 | } 27 | 28 | int adjustHeap(int n) { 29 | int i; 30 | int j; 31 | int t; 32 | i = j = t = 0; 33 | while (i * 2 < n) { 34 | j = i*2; 35 | if (i*2+1 a[j]) { 37 | int t = a[i]; 38 | a[i] = a[j]; 39 | a[j] = t; 40 | i = j; 41 | } 42 | else break; 43 | } 44 | return 0; 45 | } 46 | 47 | int heapSort() { 48 | int t; 49 | int k; 50 | t = 0; 51 | for (k = 0; k < n; k = k + 1) { 52 | t = a[0]; 53 | a[0] = a[n-k-1]; 54 | a[n-k-1] = t; 55 | adjustHeap(n-k-1); 56 | } 57 | return 0; 58 | } 59 | 60 | int main() { 61 | int i; 62 | n = getString().parseInt(); 63 | a = new int[n]; 64 | 65 | for (i = 0; i < a.size(); i = i + 1) 66 | a[i] = i; 67 | makeHeap(); 68 | heapSort(); 69 | for (i = 0; i < a.size(); i = i + 1) 70 | print(toString(a[i]) + " "); 71 | print("\n"); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /testcase/final/horse-5100309153-yanghuan.in: -------------------------------------------------------------------------------- 1 | 101 2 | -------------------------------------------------------------------------------- /testcase/final/horse-5100309153-yanghuan.limit: -------------------------------------------------------------------------------- 1 | 25000000 2 | -------------------------------------------------------------------------------- /testcase/final/horse-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int head; 3 | int startx; 4 | int starty; 5 | int targetx; 6 | int targety; 7 | int x; 8 | int y; 9 | int[] xlist = new int[12000]; 10 | int[] ylist = new int[12000]; 11 | int tail; 12 | int ok; 13 | int now; 14 | int[][] step; 15 | int i; 16 | int j; 17 | 18 | void origin(int N) 19 | { 20 | step = new int[N][]; 21 | for (i = 0; i < N; i ++ ) { 22 | step[i] = new int[N]; 23 | for (j = 0; j < N; j ++ ) 24 | step[i][j] = 0; 25 | } 26 | } 27 | 28 | bool check(int a, int N) { 29 | return ((a < N) && (a >= 0)); 30 | } 31 | 32 | void addList(int x, int y) { 33 | if (check(x, N) && check(y, N) && step[x][y] == -1) { 34 | tail = tail + 1; 35 | xlist[tail] = x; 36 | ylist[tail] = y; 37 | step[x][y] = now + 1; 38 | if ((x == targetx) && (y == targety)) ok = 1; 39 | } 40 | } 41 | 42 | int main() { 43 | origin(106); 44 | N = getInt(); 45 | targetx = targety = N - 1; 46 | for (i = 0; i < N; i ++) 47 | for (j = 0; j < N; j ++) 48 | step[i][j] = -1; 49 | while (head <= tail) { 50 | x = xlist[head]; 51 | y = ylist[head]; 52 | now = step[x][y]; 53 | addList(x-1, y-2); 54 | addList(x-1, y+2); 55 | addList(x+1, y-2); 56 | addList(x+1, y+2); 57 | addList(x-2, y-1); 58 | addList(x-2, y+1); 59 | addList(x+2, y-1); 60 | addList(x+2, y+1); 61 | if (ok == 1) break; 62 | head = head + 1; 63 | } 64 | if (ok == 1) println(toString(step[targetx][targety])); 65 | else print("no solution!\n"); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /testcase/final/horse-5100309153-yanghuan.out: -------------------------------------------------------------------------------- 1 | 67 2 | -------------------------------------------------------------------------------- /testcase/final/horse2-5100309153-yanghuan.in: -------------------------------------------------------------------------------- 1 | 102 2 | 3 | -------------------------------------------------------------------------------- /testcase/final/horse2-5100309153-yanghuan.limit: -------------------------------------------------------------------------------- 1 | 15000000 2 | -------------------------------------------------------------------------------- /testcase/final/horse2-5100309153-yanghuan.out: -------------------------------------------------------------------------------- 1 | 67 2 | -------------------------------------------------------------------------------- /testcase/final/horse3-5100309153-yanghuan.in: -------------------------------------------------------------------------------- 1 | 103 2 | -------------------------------------------------------------------------------- /testcase/final/horse3-5100309153-yanghuan.limit: -------------------------------------------------------------------------------- 1 | 25000000 2 | -------------------------------------------------------------------------------- /testcase/final/horse3-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int head; 3 | int startx; 4 | int starty; 5 | int targetx; 6 | int targety; 7 | int x; 8 | int y; 9 | int[] xlist = new int[12000]; 10 | int[] ylist = new int[12000]; 11 | int tail; 12 | int ok; 13 | int now; 14 | int[] dx = new int[8]; 15 | int[] dy = new int[9]; 16 | int[][] step; 17 | int i; 18 | int j; 19 | 20 | void origin(int N) 21 | { 22 | head = 0; 23 | tail = 0; 24 | step = new int[N][]; 25 | for (i = 0; i < N; i ++ ) { 26 | step[i] = new int[N]; 27 | for (j = 0; j < N; j ++ ) 28 | step[i][j] = 0; 29 | } 30 | } 31 | 32 | bool check(int a) { 33 | return ((a < N) && (a >= 0)); 34 | } 35 | 36 | void addList(int x, int y) { 37 | if (check(x) && check(y) && step[x][y] == -1) { 38 | tail ++; 39 | xlist[tail] = x; 40 | ylist[tail] = y; 41 | step[x][y] = now + 1; 42 | if ((x == targetx) && (y == targety)) ok = 1; 43 | } 44 | } 45 | int main() { 46 | origin(106); 47 | N = getInt(); 48 | targetx = targety = N - 1; 49 | for (i = 0; i < N; i ++) 50 | for (j = 0; j < N; j ++) 51 | step[i][j] = -1; 52 | dx[0] = -2; dy[0] = -1; 53 | dx[1] = -2; dy[1] = 1; 54 | dx[2] = 2; dy[2] = -1; 55 | dx[3] = 2; dy[3] = 1; 56 | dx[4] = -1; dy[4] = -2; 57 | dx[5] = -1; dy[5] = 2; 58 | dx[6] = 1; dy[6] = -2; 59 | dx[7] = 1; dy[7] = 2; 60 | while (head <= tail) { 61 | x = xlist[head]; 62 | y = ylist[head]; 63 | now = step[x][y]; 64 | for (j = 0;j < 8;j ++) 65 | addList(x + dx[j], y + dy[j]); 66 | if (ok == 1) break; 67 | head ++; 68 | } 69 | if (ok == 1) println(toString(step[targetx][targety])); 70 | else print("no solution!\n"); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /testcase/final/horse3-5100309153-yanghuan.out: -------------------------------------------------------------------------------- 1 | 67 2 | -------------------------------------------------------------------------------- /testcase/final/lvalue2-5110379024-wuhang.mx: -------------------------------------------------------------------------------- 1 | int[] a = new int[4]; 2 | int main() 3 | { 4 | int[] b = new int[4]; 5 | b[2]=2; 6 | a=b; 7 | println(toString(a[2])); 8 | } 9 | -------------------------------------------------------------------------------- /testcase/final/lvalue2-5110379024-wuhang.out: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /testcase/final/magic-5100309153-yanghuan.limit: -------------------------------------------------------------------------------- 1 | 2000000 2 | -------------------------------------------------------------------------------- /testcase/final/magic-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int[][] make; 2 | int[] color = new int[10]; 3 | int[] count = new int[1]; 4 | int i; 5 | int j; 6 | 7 | void origin(int N) 8 | { 9 | make = new int[N][]; 10 | for (i = 0; i < N; i ++ ) { 11 | make[i] = new int[N]; 12 | for (j = 0; j < N; j ++ ) 13 | make[i][j] = 0; 14 | } 15 | } 16 | 17 | int search(int x, int y, int z) 18 | { 19 | int s; 20 | int i; 21 | int j; 22 | if ((y > 0 || y < 0) || x == 0 || make[x-1][0] + make[x-1][1] + make[x-1][2] == 15) 23 | { 24 | if (x == 2 && y == 2) { 25 | make[2][2] = 45 - z; 26 | s = make[0][0] + make[0][1] + make[0][2]; 27 | if (make[1][0] + make[1][1] + make[1][2] == s && 28 | make[2][0] + make[2][1] + make[2][2] == s && 29 | make[0][0] + make[1][0] + make[2][0] == s && 30 | make[0][1] + make[1][1] + make[2][1] == s && 31 | make[0][2] + make[1][2] + make[2][2] == s && 32 | make[0][0] + make[1][1] + make[2][2] == s && 33 | make[2][0] + make[1][1] + make[0][2] == s) 34 | { 35 | count[0] = count[0] + 1; 36 | for (i = 0;i <= 2;i ++) 37 | { 38 | for (j = 0;j <= 2;j ++) 39 | { 40 | print(toString(make[i][j])); 41 | print(" "); 42 | } 43 | print("\n"); 44 | } 45 | print("\n"); 46 | } 47 | } 48 | else { 49 | if (y == 2) { 50 | make[x][y] = 15 - make[x][0] - make[x][1]; 51 | if (make[x][y] > 0 && make[x][y] < 10 && color[make[x][y]] == 0) { 52 | color[make[x][y]] = 1; 53 | if (y == 2) 54 | search(x + 1, 0, z+make[x][y]); 55 | else 56 | search(x, y+1, z+make[x][y]); 57 | color[make[x][y]] = 0; 58 | } 59 | } 60 | else { 61 | for (i = 1;i <= 9;i ++) { 62 | if (color[i] == 0) { 63 | color[i] = 1; 64 | make[x][y] = i; 65 | if (y == 2) 66 | search(x + 1, 0, z+i); 67 | else 68 | search(x, y+1, z+i); 69 | make[x][y] = 0; 70 | color[i] = 0; 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | int main() 78 | { 79 | origin(3); 80 | search(0, 0, 0); 81 | println(toString(count[0])); 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /testcase/final/magic-5100309153-yanghuan.out: -------------------------------------------------------------------------------- 1 | 2 7 6 2 | 9 5 1 3 | 4 3 8 4 | 5 | 2 9 4 6 | 7 5 3 7 | 6 1 8 8 | 9 | 4 3 8 10 | 9 5 1 11 | 2 7 6 12 | 13 | 4 9 2 14 | 3 5 7 15 | 8 1 6 16 | 17 | 6 1 8 18 | 7 5 3 19 | 2 9 4 20 | 21 | 6 7 2 22 | 1 5 9 23 | 8 3 4 24 | 25 | 8 1 6 26 | 3 5 7 27 | 4 9 2 28 | 29 | 8 3 4 30 | 1 5 9 31 | 6 7 2 32 | 33 | 8 34 | -------------------------------------------------------------------------------- /testcase/final/manyarguments-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int a(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15) 2 | { 3 | return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15; 4 | } 5 | 6 | int main() 7 | { 8 | println(toString(a(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15))); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /testcase/final/manyarguments-5100379110-daibo.out: -------------------------------------------------------------------------------- 1 | 120 2 | -------------------------------------------------------------------------------- /testcase/final/maxflow-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 15000000 2 | -------------------------------------------------------------------------------- /testcase/final/maxflow-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int[][] c; 2 | int ans = 0; 3 | int[] visit = new int[110]; 4 | int[] pre = new int[110]; 5 | int[] f = new int[110]; 6 | int i; 7 | int j; 8 | int open; 9 | int closed; 10 | 11 | void origin(int N) 12 | { 13 | c = new int[N][]; 14 | for (i = 0; i < N; i ++ ) { 15 | c[i] = new int[N]; 16 | for (j = 0; j < N; j ++ ) 17 | c[i][j] = 0; 18 | } 19 | } 20 | 21 | int build(int start, int ending) { 22 | for (i = 1; i <= 49; i++) { 23 | for (j = 50; j <= 98-i+1; j++) { 24 | c[i][j] = 1; 25 | } 26 | } 27 | for (i = 1; i <= 49; i++) 28 | c[start][i] = 1; 29 | for (i = 50; i <= 98; i++) 30 | c[i][ending] = 1; 31 | return 0; 32 | } 33 | 34 | int find(int ending, int start, int flag) { 35 | open = 0; 36 | closed = 1; 37 | for (i = 1; i <= ending; i++) { 38 | visit[i] = 0; 39 | } 40 | f[1]=start; 41 | visit[start]=1; 42 | pre[start]=0; 43 | flag=0; 44 | while (open0 && visit[j]==0) { 49 | visit[j]=1; 50 | closed++; 51 | f[closed]=j; 52 | pre[j]=i; 53 | if (closed==ending) flag=1; 54 | } 55 | } 56 | return flag; 57 | } 58 | 59 | int improve(int ending) { 60 | i=ending; 61 | ans++; 62 | while (pre[i]>0) { 63 | j=pre[i]; 64 | c[j][i]--; 65 | c[i][j]++; 66 | i=j; 67 | } 68 | return 0; 69 | } 70 | 71 | int main() { 72 | origin(110); 73 | int k; 74 | int start; 75 | int ending; 76 | int flag; 77 | int i1; 78 | 79 | k = 0; 80 | start = 99; 81 | ending = 100; 82 | flag = 0; 83 | 84 | build(start, ending); 85 | while (find(ending, start, flag)>0) { 86 | improve(ending); 87 | } 88 | println(toString(ans)); 89 | return 0; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /testcase/final/maxflow-5100379110-daibo.out: -------------------------------------------------------------------------------- 1 | 49 2 | -------------------------------------------------------------------------------- /testcase/final/multiarray-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int[][] a = new int[4][]; 2 | int i; 3 | int j; 4 | 5 | class rec { 6 | int num; 7 | int c; 8 | } 9 | 10 | rec[] b = new rec[5]; 11 | 12 | int printNum(int num) { 13 | println(toString(num)); 14 | } 15 | int main() { 16 | for (i = 0; i < 4; i++) { 17 | a[i] = new int[11]; 18 | } 19 | 20 | 21 | for (i = 0; i < 4; i ++) { 22 | for (j = 0; j < 10; j ++) 23 | a[i][j] = 888; 24 | } 25 | for (i = 0; i < 5; i ++) { 26 | b[i] = new rec; 27 | b[i].num = -1; 28 | } 29 | 30 | printNum(a[3][9]); 31 | for (i = 0; i <= 3; i ++) 32 | for (j = 0; j <= 9; j ++) 33 | a[i][j] = i * 10 + j; 34 | 35 | for (i = 0; i <= 3; i ++) 36 | for (j = 0; j <= 9; j ++) 37 | printNum(a[i][j]); 38 | a[2][10]=0; 39 | printNum(a[2][10]); 40 | b[0].num = -2; 41 | b[a[2][10]].num = -10; 42 | printNum(b[0].num); 43 | printNum(b[1].num); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /testcase/final/multiarray-5100309153-yanghuan.out: -------------------------------------------------------------------------------- 1 | 888 2 | 0 3 | 1 4 | 2 5 | 3 6 | 4 7 | 5 8 | 6 9 | 7 10 | 8 11 | 9 12 | 10 13 | 11 14 | 12 15 | 13 16 | 14 17 | 15 18 | 16 19 | 17 20 | 18 21 | 19 22 | 20 23 | 21 24 | 22 25 | 23 26 | 24 27 | 25 28 | 26 29 | 27 30 | 28 31 | 29 32 | 30 33 | 31 34 | 32 35 | 33 36 | 34 37 | 35 38 | 36 39 | 37 40 | 38 41 | 39 42 | 0 43 | -10 44 | -1 45 | -------------------------------------------------------------------------------- /testcase/final/pi-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 10000; 3 | int b = 0; 4 | int c = 2800; 5 | int d = 0; 6 | int e = 0; 7 | int[] f = new int[2801]; 8 | int g = 0; 9 | 10 | for (;b-c!=0;) 11 | f[b++] = a/5; 12 | for (;; e = d%a){ 13 | d = 0; 14 | g = c*2; 15 | if (g==0) break; 16 | 17 | for (b=c;;d=d*b){ 18 | d=d+f[b]*a; 19 | f[b] = d%--g; 20 | d=d/g--; 21 | if (--b==0) break; 22 | } 23 | 24 | c = c-14; 25 | print(toString(e+d/a)); 26 | } 27 | 28 | print("\n"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /testcase/final/pi-5090379042-jiaxiao.out: -------------------------------------------------------------------------------- 1 | 3141592653589793238462643383279528841971693993751058209749445923078164062862089986280348253421170679821480865132823664709384469555822317253594081284811174502841270193852115559644622948954930381964428810975665933446128475648233786783165271201991456485669234634861045432664821339360726024914127372458706606315588174881520920962829254917153643678925903611330530548820466521384146951941511609433057273657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566438602139494639522473719070217986943702770539217176293176752384674818467669451320005681271452635608277857713427577896091736371787214684409012249534301465495853710579227968925892354201995611212902196864344181598136297747713099605187072113499999983729780499510597317328160963185 2 | -------------------------------------------------------------------------------- /testcase/final/prime-5100309153-yanghuan.in: -------------------------------------------------------------------------------- 1 | 168 2 | -------------------------------------------------------------------------------- /testcase/final/prime-5100309153-yanghuan.limit: -------------------------------------------------------------------------------- 1 | 6000000 2 | -------------------------------------------------------------------------------- /testcase/final/prime-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int M; 3 | int i; 4 | int j; 5 | int primeCount; 6 | int resultCount; 7 | int[] b = new int[1001]; 8 | int[] prime = new int[170]; 9 | int[] gps = new int[1001]; 10 | int[] tmp = new int[1]; 11 | int[][] result; 12 | 13 | void origin(int N) 14 | { 15 | result = new int[N][]; 16 | for (i = 0; i < N; i ++ ) { 17 | result[i] = new int[N]; 18 | for (j = 0; j < N; j ++ ) 19 | result[i][j] = 0; 20 | } 21 | } 22 | 23 | //int getPrime(int N, int* primeCount, int* b, int* gps, int* prime) { 24 | int getPrime(int N) { 25 | int count; 26 | int i; 27 | count = 2; 28 | for (i = 2; i <= N; i = i + 1) { 29 | if (b[i] == 1) { 30 | tmp[0] = tmp[0] + 1; 31 | prime[tmp[0]] = i; 32 | gps[i] = tmp[0]; 33 | } 34 | while (i * count <= N) { 35 | b[i * count] = 0; 36 | count = count + 1; 37 | } 38 | count = 2; 39 | } 40 | } 41 | 42 | //int getResult(int N, int k1, int k2, int* b, int* gps, int* prime, int result[170][170]) { 43 | int getResult(int N, int k1, int k2) { 44 | if (result[k1][k2] == -1) 45 | if (prime[k2] * 2 - prime[k1] <= N) 46 | if (b[prime[k2] * 2 - prime[k1]] != 0) 47 | result[k1][k2] = getResult(N, 48 | k2, 49 | gps[prime[k2] * 2 - prime[k1]] 50 | ) + 1; 51 | if (result[k1][k2] == -1) 52 | result[k1][k2] = 1; 53 | return result[k1][k2]; 54 | } 55 | 56 | int printF(int k1, int k2, int k3) { 57 | // printf("%d", k1); 58 | print(toString(k1)); 59 | while (k3 > 0) { 60 | // printf("%c", ' '); 61 | print(" "); 62 | // printf("%d", k2); 63 | print(toString(k2)); 64 | k2 = k2 * 2 - k1; 65 | k1 = (k1 + k2) / 2; 66 | k3 = k3 - 1; 67 | } 68 | // printf("%c", '\n'); 69 | print("\n"); 70 | } 71 | 72 | int main() { 73 | origin(170); 74 | N = 1000; 75 | // M = 100*(getchar()-'0')+10*(getchar()-'0')+getchar()-'0'; 76 | M = getInt(); 77 | primeCount = 0; 78 | resultCount = 0; 79 | tmp[0] = 0; 80 | for (i = 0; i < N + 1; i = i + 1) 81 | { 82 | b[i] = 1; 83 | gps[i] = 0; 84 | } 85 | for (i = 0; i < M + 1; i = i + 1) 86 | { 87 | prime[i] = 0; 88 | } 89 | for (i = 0; i <= M; i = i + 1) 90 | { 91 | for (j = 0; j <= M; j = j + 1) 92 | result[i][j] = -1; 93 | } 94 | // getPrime(N, tmp, b, gps, prime); 95 | getPrime(N); 96 | primeCount = tmp[0]; 97 | for (i = 1; i < primeCount; i = i + 1) 98 | for (j = i + 1; j <= primeCount; j = j + 1) 99 | if (result[i][j] == -1) { 100 | result[i][j] = getResult(N, i, j); 101 | if (result[i][j] > 1) { 102 | printF(prime[i], prime[j], result[i][j]); 103 | resultCount = resultCount + 1; 104 | } 105 | } 106 | //printf("Total: %d\n", resultCount); 107 | print("Total: "); 108 | println(toString(resultCount)); 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /testcase/final/qsort-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 5000000 2 | -------------------------------------------------------------------------------- /testcase/final/qsort-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | // Target: qsort 2 | // Possible optimization: Dead code elimination, common expression, strength reduction 3 | // REMARKS: nothing. 4 | // 5 | // 6 | 7 | //int a[10100]; 8 | int[] a = new int[10100]; 9 | int n = 10000; 10 | 11 | int qsrt(int l, int r) { 12 | int i = l; 13 | int j = r; 14 | int x = a[(l + r) / 2]; 15 | while (i <= j) { 16 | while (a[i] < x) i++; 17 | while (a[j] > x) j--; 18 | if (i <= j) { 19 | int temp = a[i]; 20 | a[i] = a[j]; 21 | a[j] = temp; 22 | i++; 23 | j--; 24 | } 25 | } 26 | if (l < j) qsrt(l, j); 27 | if (i < r) qsrt(i, r); 28 | return 0; 29 | } 30 | 31 | int main() { 32 | int i; 33 | for (i = 1; i <= n; i++) 34 | a[i] = n + 1 - i; 35 | qsrt(1, n); 36 | for (i = 1; i <= n; i++) { 37 | // printf("%d ", a[i]); 38 | print(toString(a[i])); 39 | print(" "); 40 | } 41 | print("\n"); 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /testcase/final/queens-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 1500000 2 | -------------------------------------------------------------------------------- /testcase/final/queens-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int N = 8; 2 | int[] row = new int[8]; 3 | int[] col = new int[8]; 4 | int[][] d = new int[2][]; 5 | 6 | void printBoard() { 7 | int i; 8 | int j; 9 | for (i = 0; i < N; i++) { 10 | for (j = 0; j < N; j++) { 11 | if (col[i] == j) 12 | print(" O"); 13 | else 14 | print(" ."); 15 | } 16 | println(""); 17 | } 18 | println(""); 19 | } 20 | 21 | void search(int c) { 22 | if (c == N) { 23 | printBoard(); 24 | } 25 | else { 26 | int r; 27 | for (r = 0; r < N; r++) { 28 | if (row[r] == 0 && d[0][r+c] == 0 && d[1][r+N-1-c] == 0) { 29 | row[r] = d[0][r+c] = d[1][r+N-1-c] = 1; 30 | col[c] = r; 31 | search(c+1); 32 | row[r] = d[0][r+c] = d[1][r+N-1-c] = 0; 33 | } 34 | } 35 | } 36 | } 37 | 38 | int main() { 39 | int i; 40 | for (i = 0; i < 2; i ++) 41 | d[i] = new int[8 + 8 - 1]; 42 | search(0); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /testcase/final/spill2-5100379110-daibo.limit: -------------------------------------------------------------------------------- 1 | 100000 2 | -------------------------------------------------------------------------------- /testcase/final/spill2-5100379110-daibo.out: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 3 | -------------------------------------------------------------------------------- /testcase/final/statement_test-huyuncong.in: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /testcase/final/statement_test-huyuncong.mx: -------------------------------------------------------------------------------- 1 | //考察点:section 8 语句,包括if,while,for,break,continue,return等 2 | //算法:线性筛法求欧拉函数 3 | //样例输入:10 4 | //样例输出: 5 | //1 6 | //2 7 | //2 8 | //4 9 | //2 10 | //6 11 | //4 12 | //6 13 | //4 14 | 15 | int N; 16 | int M = 0; 17 | bool[] check; 18 | 19 | int main() { 20 | N = getInt(); 21 | check = new bool[N+5]; 22 | int i = 0; 23 | while ( i <= N ) check[i++] = true; 24 | int[] phi = new int[N+5]; 25 | int[] P = new int[N+5]; 26 | phi[1] = 1; 27 | for (i = 2; ; ++i ) { 28 | if ( i > N ) break; 29 | if ( check[i] ) { 30 | P[++M] = i; 31 | phi[i] = i - 1; 32 | } 33 | int k = i; 34 | int i; 35 | for (i = 1; i <= M && (k * P[i] <= N); i++) { 36 | int tmp = k * P[i]; 37 | if ( tmp > N ) continue; 38 | check[tmp] = false; 39 | if ( k % P[i] == 0) { 40 | phi[tmp] = phi[k] * P[i]; 41 | break; 42 | } 43 | else { 44 | phi[k * P[i]] = phi[k] * (P[i] - 1); 45 | } 46 | } 47 | println(toString(phi[k])); 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /testcase/final/statement_test-huyuncong.out: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 2 4 | 4 5 | 2 6 | 6 7 | 4 8 | 6 9 | 4 10 | -------------------------------------------------------------------------------- /testcase/final/string_test-huyuncong.in: -------------------------------------------------------------------------------- 1 | DCBAE 2 | 4ssfsdf -------------------------------------------------------------------------------- /testcase/final/string_test-huyuncong.mx: -------------------------------------------------------------------------------- 1 | //考察点:section 10 字符串,包括字符串定义,运算符语义,字符串的内建方法 2 | //算法:递归模拟 3 | //样例输入: 4 | //DCBAE 5 | //4ssfsdf 6 | //样例输出: 7 | //ABCD 8 | 9 | string A; 10 | string B; 11 | string C; 12 | int N; 13 | 14 | string calc(string A) 15 | { 16 | int len = A.length(); 17 | if (1 == len) return A; 18 | int mid = len/2; 19 | string L = calc(A.substring(0,mid-1)); 20 | string R = calc(A.substring(mid,len-1)); 21 | if (L < R) return L + R; 22 | else if (L == R) { 23 | int l = L.ord(0); 24 | int r = R.ord(0); 25 | if (l < r) return L + R; 26 | return R + L; 27 | } 28 | else if (L > R) return R + L; 29 | println("Never Ever!"); 30 | } 31 | 32 | int main() 33 | { 34 | A = getString(); 35 | B = getString(); 36 | N = B.parseInt(); 37 | if (A.length() < N) { 38 | println("length error!"); 39 | return 0; 40 | } 41 | C = calc(A.substring(0,N-1)); 42 | println(C); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /testcase/final/string_test-huyuncong.out: -------------------------------------------------------------------------------- 1 | ABCD 2 | -------------------------------------------------------------------------------- /testcase/final/superloop-5090379042-jiaxiao.in: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /testcase/final/superloop-5090379042-jiaxiao.limit: -------------------------------------------------------------------------------- 1 | 5000000 2 | -------------------------------------------------------------------------------- /testcase/final/superloop-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | //Target: use loops to calculate calculator of 6! 2 | //@author yixi 3 | 4 | int N; 5 | int h = 99; 6 | int i = 100; 7 | int j = 101; 8 | int k = 102; 9 | int total = 0; 10 | 11 | int main() { 12 | int a; 13 | int b; 14 | int c; 15 | int d; 16 | int e; 17 | int f; 18 | N=getInt(); 19 | for ( a=1; a<=N; a++ ) 20 | for ( b=1; b<=N; b++ ) 21 | for ( c=1; c<=N; c++ ) 22 | for ( d=1; d<=N; d++ ) 23 | for ( e=1; e<=N; e++ ) 24 | for ( f=1; f<=N; f++ ) 25 | if (a!=b && a!=c && a!=d && a!=e && a!=f && a!=h && a!=i && a!=j && a!=k 26 | && b!=c && b!=d && b!=e && b!=f && b!=h && b!=i && b!=j && b!=k 27 | && c!=d && c!=e && c!=f && c!=h && c!=i && c!=j && c!=k 28 | && d!=e && d!=f && d!=h && d!=i && d!=j && d!=k 29 | && e!=f && e!=h && e!=i && e!=j && e!=k 30 | && f!=h && f!=i && f!=j && f!=k && i!=j && h!=k) 31 | { 32 | total++; 33 | } 34 | 35 | println(toString(total)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /testcase/final/superloop-5090379042-jiaxiao.out: -------------------------------------------------------------------------------- 1 | 720 2 | -------------------------------------------------------------------------------- /testcase/final/tak-5090379042-jiaxiao.in: -------------------------------------------------------------------------------- 1 | 18 2 | 12 3 | 6 4 | -------------------------------------------------------------------------------- /testcase/final/tak-5090379042-jiaxiao.limit: -------------------------------------------------------------------------------- 1 | 3500000 2 | -------------------------------------------------------------------------------- /testcase/final/tak-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | int tak(int x, int y, int z) { 2 | if ( y < x ) return 1 + tak( tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y) ); 3 | else return z; 4 | } 5 | 6 | int main(){ 7 | int a; 8 | int b; 9 | int c; 10 | a=getInt(); 11 | b=getInt(); 12 | c=getInt(); 13 | println(toString(tak(a,b,c))); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /testcase/final/tak-5090379042-jiaxiao.out: -------------------------------------------------------------------------------- 1 | 13 2 | -------------------------------------------------------------------------------- /testcase/final/twinprime-5090379042-jiaxiao.limit: -------------------------------------------------------------------------------- 1 | 2000000 2 | -------------------------------------------------------------------------------- /testcase/final/twinprime-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | 2 | int N = 15000; 3 | bool[] b = new bool[15001]; 4 | int resultCount = 0; 5 | 6 | int main() 7 | { 8 | int i; 9 | 10 | for (i = 1; i <= N; i++) b[i] = true; 11 | 12 | for (i = 2; i <= N; i++) if (b[i]) 13 | { 14 | int count = 2; 15 | 16 | if (i>3 && b[i-2]) 17 | { 18 | resultCount++; 19 | println(toString(i-2) + " " + toString(i)); 20 | } 21 | 22 | while (i*count <= N) 23 | { 24 | b[i*count] = false; 25 | count++; 26 | } 27 | } 28 | 29 | println("Total: " + toString(resultCount)); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /testcase/ir/array1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int[] a; 3 | int[] b = new int[10]; 4 | int[][] c = new int [2][]; 5 | return 0; 6 | } 7 | 8 | /*! assert: exitcode 9 | 0 10 | */ -------------------------------------------------------------------------------- /testcase/ir/array2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int[] a = new int[1]; 3 | a[0] = 10; 4 | return a[0] + 20; 5 | } 6 | 7 | /*! assert: exitcode 8 | 30 9 | */ -------------------------------------------------------------------------------- /testcase/ir/array3.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int la = 5; 3 | int[] a = new int[la]; 4 | 5 | int cnt = 0; 6 | for (int i = 0; i < la; ++i) 7 | a[i] = ++cnt; 8 | 9 | int sum = 0; 10 | for (int i = 0; i < la; ++i) sum = sum + a[i]; 11 | 12 | return sum; 13 | } 14 | 15 | /*! assert: exitcode 16 | 15 17 | */ -------------------------------------------------------------------------------- /testcase/ir/array4.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int la = 5; 3 | int lb = 10; 4 | int[] a; 5 | int[] b = new int[lb]; 6 | int[][] c = new int [2][]; 7 | a = new int[la]; 8 | c[0] = a; 9 | c[1] = b; 10 | 11 | int cnt = 0; 12 | for (int i = 0; i < 2; ++i) 13 | for (int j = 0; j < la; ++j) 14 | c[i][j] = ++cnt; 15 | for (int j = la; j < lb; ++j) 16 | c[1][j] = ++cnt; 17 | 18 | int sum = 0; 19 | for (int i = 0; i < la; ++i) sum = sum + a[i]; 20 | for (int i = 0; i < lb; ++i) sum = sum + b[i]; 21 | 22 | return sum; 23 | } 24 | 25 | /*! assert: exitcode 26 | 120 27 | */ -------------------------------------------------------------------------------- /testcase/ir/builtin1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int[] a = new int[20]; 3 | return a.size(); 4 | } 5 | /*! assert: exitcode 6 | 20 7 | */ -------------------------------------------------------------------------------- /testcase/ir/builtin2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | string text = "hello, world!\n"; 3 | print(text); 4 | return 0; 5 | } -------------------------------------------------------------------------------- /testcase/ir/builtin3.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | string text = "hello"; 3 | println(text); 4 | println("world"); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /testcase/ir/function1.mx: -------------------------------------------------------------------------------- 1 | int min(int a, int b) { 2 | if (a <= b) return a; 3 | return b; 4 | } 5 | 6 | int main() { 7 | int x = 10; 8 | int y = 20; 9 | return min(x, y); 10 | } 11 | 12 | /*! assert: exitcode 13 | 10 14 | */ -------------------------------------------------------------------------------- /testcase/ir/function2.mx: -------------------------------------------------------------------------------- 1 | int tak(int x, int y, int z) { 2 | if(y < x) { 3 | return 1 + tak( tak(x - 1, y , z), 4 | tak(y - 1, z , x), 5 | tak(z - 1, x , y) 6 | ); 7 | } else { 8 | return z; 9 | } 10 | } 11 | 12 | int main() { 13 | return tak(18,12,6); 14 | } 15 | 16 | /*! assert: exitcode 17 | 13 18 | */ -------------------------------------------------------------------------------- /testcase/ir/global1.mx: -------------------------------------------------------------------------------- 1 | int x; 2 | int y; 3 | int main() { 4 | x = 10; 5 | y = 20; 6 | int c = x+y; 7 | return c; 8 | } 9 | /*! assert: exitcode 10 | 30 11 | */ -------------------------------------------------------------------------------- /testcase/ir/loop1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 20; 3 | int sum = 0; 4 | int i = 1; 5 | while (i <= n) { 6 | sum = sum + i; 7 | i = i+1; 8 | } 9 | return sum; 10 | } 11 | /*! assert: exitcode 12 | 210 13 | */ -------------------------------------------------------------------------------- /testcase/ir/loop2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 10; 3 | int sum = 0; 4 | for (int i = 1; i <= n; ++i) sum = sum + i; 5 | int j; 6 | for (j = 1; j <= n; ++j) sum = sum + 10 + j; 7 | return sum; 8 | } 9 | /*! assert: exitcode 10 | 210 11 | */ -------------------------------------------------------------------------------- /testcase/ir/loop3.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 5; 3 | int sum = 0; 4 | for (int i = 1; i <= n; ++i) 5 | for (int j = 1; j <= n; ++j) 6 | sum = sum + i; 7 | return sum; 8 | } 9 | /*! assert: exitcode 10 | 75 11 | */ -------------------------------------------------------------------------------- /testcase/ir/loop4.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 5; 3 | int sum = 0; 4 | for (int i = 1; i <= n; ++i) { 5 | for (int j = 1; j <= n; ++j) 6 | sum = sum + i; 7 | sum = sum + 1; 8 | } 9 | return sum; 10 | } 11 | /*! assert: exitcode 12 | 80 13 | */ -------------------------------------------------------------------------------- /testcase/ir/simple1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5; 3 | int b; 4 | b = a; 5 | int c = a * 2 + b; 6 | return c; 7 | } 8 | /*! assert: exitcode 9 | 15 10 | */ -------------------------------------------------------------------------------- /testcase/ir/simple2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5, b = 0; 3 | int c; 4 | if (b != 0 && a/b != 1) { 5 | c = 10; 6 | } else { 7 | c = 20; 8 | } 9 | 10 | if (!(c == 10 && a/b == 0 && a == 5)) 11 | c = 30; 12 | 13 | return c; 14 | } 15 | /*! assert: exitcode 16 | 30 17 | */ -------------------------------------------------------------------------------- /testcase/ir/simple3.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5, b = 0; 3 | int c; 4 | if (a/b != 1) { 5 | c = 10; 6 | } else { 7 | c = 20; 8 | } 9 | return c; 10 | } 11 | /*! assert: exception 12 | */ -------------------------------------------------------------------------------- /testcase/ir/simple4.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5, b = 0; 3 | int c; 4 | bool d = b != 0 && a/b != 1; 5 | if (d) { 6 | c = 10; 7 | } else { 8 | c = 20; 9 | } 10 | 11 | bool e; 12 | e = !(c == 10 && a/b == 0 && a == 5); 13 | if (e) 14 | c = 30; 15 | 16 | return c; 17 | } 18 | /*! assert: exitcode 19 | 30 20 | */ -------------------------------------------------------------------------------- /testcase/ir/simple5.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5, b = 0; 3 | int c; 4 | if (b != 0 && a/b != 1) { 5 | c = 10; 6 | } else { 7 | c = 20; 8 | } 9 | 10 | if (!(c == 10 || a/b == 0 && a == 5)) 11 | c = 30; 12 | 13 | return c; 14 | } 15 | /*! assert: exception 16 | */ -------------------------------------------------------------------------------- /testcase/ir/simple6.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5; 3 | int b, c; 4 | b = c = a++; 5 | return a; 6 | } 7 | /*! assert: exitcode 8 | 6 9 | */ -------------------------------------------------------------------------------- /testcase/ir/simple7.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 5; 3 | int b, c; 4 | b = c = a++; 5 | return b+c+a; 6 | } 7 | /*! assert: exitcode 8 | 16 9 | */ -------------------------------------------------------------------------------- /testcase/ir/ssa1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int x = 10; 3 | if (x == 10) x = 20; 4 | else x = 30; 5 | return x; 6 | } 7 | /*! assert: exitcode 8 | 20 9 | */ -------------------------------------------------------------------------------- /testcase/ir/ssa2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int n = 10; 3 | int f0 = 0, f1 = 1, f2; 4 | for (int i = 1; i < n; ++i) { 5 | f2 = f0 + f1; 6 | f0 = f1; 7 | f1 = f2; 8 | } 9 | return f2; 10 | } 11 | /*! assert: exitcode 12 | 55 13 | */ -------------------------------------------------------------------------------- /testcase/ir/string1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | string s1 = "aaa"; 3 | string s2 = "bbbbb"; 4 | string s3 = s1 + s2; 5 | return s3.length() * 1000 + s3.ord(5); 6 | } 7 | /*! assert: exitcode 8 | 8098 9 | */ -------------------------------------------------------------------------------- /testcase/ir/string2.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | return "hahaha".substring(2, 3).length(); 3 | } 4 | /*! assert: exitcode 5 | 2 6 | */ -------------------------------------------------------------------------------- /testcase/ir/struct1.mx: -------------------------------------------------------------------------------- 1 | class C1 { 2 | int x; 3 | } 4 | 5 | int main() { 6 | C1 c1 = new C1; 7 | c1.x = 10; 8 | return 2 * c1.x; 9 | } 10 | 11 | /*! assert: exitcode 12 | 20 13 | */ -------------------------------------------------------------------------------- /testcase/ir/struct2.mx: -------------------------------------------------------------------------------- 1 | class C2 { 2 | int x; 3 | int y; 4 | int z; 5 | } 6 | 7 | int main() { 8 | C2 obj = new C2; 9 | obj.x = 10; 10 | obj.y = 20; 11 | obj.z = 40; 12 | return obj.x + obj.y + obj.z; 13 | } 14 | 15 | /*! assert: exitcode 16 | 70 17 | */ -------------------------------------------------------------------------------- /testcase/ir/struct3.mx: -------------------------------------------------------------------------------- 1 | class C2 { 2 | int x; 3 | int y; 4 | bool b; 5 | int z; 6 | } 7 | 8 | int main() { 9 | C2 obj = new C2; 10 | obj.x = 10; 11 | obj.y = 20; 12 | obj.b = obj.x > obj.y; 13 | if (obj.b) obj.z = 40; 14 | else obj.z = 80; 15 | return obj.x + obj.y + obj.z; 16 | } 17 | 18 | /*! assert: exitcode 19 | 110 20 | */ -------------------------------------------------------------------------------- /testcase/ir/symtable1.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int x = 10; 3 | int y = x; 4 | if (x == 10) { 5 | int x = 20; 6 | y = x; 7 | } 8 | return y; 9 | } 10 | 11 | /*! assert: exitcode 12 | 20 13 | */ -------------------------------------------------------------------------------- /testcase/ir/symtable2.mx: -------------------------------------------------------------------------------- 1 | int x = 10; 2 | int main() { 3 | x = 20; 4 | return x; 5 | } 6 | 7 | /*! assert: exitcode 8 | 20 9 | */ -------------------------------------------------------------------------------- /testcase/semantic/basic_test-yurongyou.mill: -------------------------------------------------------------------------------- 1 | //class delc 2 | class Mo{ 3 | int ang; 4 | int sec; 5 | string world; 6 | } 7 | 8 | class RedBoy{ 9 | int num; 10 | } 11 | 12 | //global delc 13 | int age = 1000000; 14 | string eng = "Four score and seven years ago"; 15 | 16 | 17 | //function delc 18 | int travel(){ 19 | int a = 10000000; 20 | return a; 21 | } 22 | 23 | void journalists(string location){ 24 | if (location == "HongKong"){ 25 | print("naive!\n"); 26 | print("fast!\n"); 27 | println("too young\\too simple."); 28 | } 29 | } 30 | 31 | Mo xu(Mo m0, RedBoy boys){ 32 | for (int i = 0; i < boys.num; ++i){ 33 | m0.sec++; 34 | } 35 | return m0; 36 | } 37 | 38 | int main(){ 39 | //test initialization 40 | int a; 41 | int b; 42 | int c; 43 | int year = 2016; 44 | string tell = "wukefengggao\n";; 45 | bool zici = true; 46 | bool nozici = false; 47 | RedBoy boy = new RedBoy;//default initializer is "()"-free 48 | RedBoy[] boys = new RedBoy[3]; 49 | RedBoy[][][] boyss = new RedBoy[1][2][3]; 50 | Mo haha = new Mo; 51 | 52 | //test for constant 53 | false; 54 | true; 55 | null; 56 | 123; 57 | "Four score and seven years ago\n\\\""; 58 | 59 | //test for opeartion 60 | a = 1; 61 | b = 2; 62 | a = b = c;//be aware of the right association 63 | 64 | a + b; 65 | a - b; 66 | a * b; 67 | a / b; 68 | a % b; 69 | 70 | a < 1; 71 | a > 2; 72 | a == b; 73 | a != b; 74 | a <= b; 75 | a >= b; 76 | 77 | bool g, h; 78 | g && h; 79 | g || h; 80 | !g; 81 | 82 | a << 1; 83 | a >> 2; 84 | ~a; 85 | a | b; 86 | a ^ b; 87 | a & b; 88 | 89 | ++a; 90 | b++; 91 | (++a)++; 92 | 93 | boy.num; 94 | 95 | boys[2].num; 96 | boyss[1][2][3].num; 97 | 98 | xu(new Mo, null); 99 | 100 | //test for iteration 101 | for (int i = 0; i < 3; ++i){ 102 | xu(haha, boys[i]); 103 | } 104 | int i = 0; 105 | for (i = 0; i < 3; ++i) xu(haha, boys[i]); 106 | for (;;); 107 | i = 0; 108 | while(i < year) print(toString(i)); 109 | while(true) ; 110 | 111 | //test for if 112 | if (i < year){ 113 | print(toString(travel())); 114 | } 115 | else{ 116 | print("naive!\n"); 117 | } 118 | 119 | if (i < year) print(toString(travel())); 120 | else if (i == year) print("naive!\n"); 121 | else print("too young!\\"); 122 | 123 | i = 0; 124 | while(true){ 125 | if (i == year) break; 126 | ++i; 127 | } 128 | 129 | i = 0; 130 | while(true){ 131 | if (i < year) { 132 | ++i; 133 | continue; 134 | } 135 | break; 136 | } 137 | 138 | string tmpstr = "test"; 139 | print(toString(tmpstr.length())); 140 | print(tmpstr.substring(1,2)); 141 | 142 | return 0; 143 | } -------------------------------------------------------------------------------- /testcase/semantic/compile_error/arrop-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a = 1; 3 | int x = a[1]; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | "a" = "b"; 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-2-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | int a; 3 | int b; 4 | a = 0 = b; 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-3-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | true = false; 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-4-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | null = 2016; 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-5-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int[] f() { 2 | return new int[20]; 3 | } 4 | 5 | int main() { 6 | f() = new int[16]; 7 | } 8 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-6-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | int y; 4 | int z; 5 | } 6 | 7 | int main() { 8 | int t; 9 | A x; 10 | t = x; 11 | } 12 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/asiop-7-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | string str = null; 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/biop-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int a; 3 | } 4 | 5 | int main() { 6 | A a = new A; 7 | A b = new A; 8 | A c = new A; 9 | c = a + b; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/bitop-1-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int mai() { 3 | string str = "120"; 4 | int x = str >> 3; 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/boop-1-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int main() { 3 | if ("20" && "16") { 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/class-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int f; 3 | bool f; 4 | } 5 | 6 | int main() { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/class-2-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | } 4 | 5 | class A { 6 | int x; 7 | int y; 8 | } 9 | 10 | int main() { 11 | } 12 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/ctrflow-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | while (1) { } 3 | break; 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/ctrflow-2-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | continue; 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/ctrflow-3-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { int a; } 2 | 3 | int main() { 4 | A a = new A; 5 | if (a) {} 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/ctrflow-4-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int main() { 3 | string institute = "zhiyuan"; 4 | while (institute) { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/ctrflow-5-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int main() { 3 | string institute = "zhiyuan"; 4 | for (; institute; ) { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/fldop-1-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | class Foo { 2 | int young; 3 | } 4 | 5 | int main() { 6 | Foo chairman = new Foo; 7 | chairman.naive = -2147483648; 8 | } 9 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-1-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | int y; 4 | int z; 5 | } 6 | 7 | B f() { 8 | int a; 9 | a = 1; 10 | } 11 | 12 | int main() { 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-2-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | int f(A x) { 2 | int year = 2016; 3 | return year; 4 | } 5 | 6 | int main() { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-3-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | int y; 4 | int z; 5 | } 6 | 7 | A f() { 8 | int a; 9 | a = 1; 10 | } 11 | 12 | int main() { 13 | A a; 14 | a(); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-4-5100379071-puyouer.mx: -------------------------------------------------------------------------------- 1 | int f(int a, int b) { 2 | return a + b; 3 | } 4 | 5 | int main() { 6 | int x; 7 | x = f(1, 2, 3); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-5-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | class AI { 2 | int a; 3 | } 4 | 5 | int f(int a, int b) 6 | { 7 | } 8 | 9 | int main() { 10 | int b; 11 | AI sa = new AI; 12 | f(b, sa); 13 | } 14 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-6-5130309059-lijiajun.mx: -------------------------------------------------------------------------------- 1 | int main(int main) { 2 | main(7); 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/func-7-5140309552-wancheng.mx: -------------------------------------------------------------------------------- 1 | void main() {} 2 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/incop-1-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int main() { 3 | string str = "sjtu"; 4 | str++; 5 | } 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/relop-1-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | } 4 | 5 | class B { 6 | int x; 7 | } 8 | 9 | int main() { 10 | A a = new A; 11 | a.x = 20; 12 | B b = new B; 13 | b.x = 16; 14 | if (a == b) {} 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/relop-2-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | class A { 2 | int x; 3 | } 4 | 5 | class B { 6 | int x; 7 | } 8 | 9 | int main() { 10 | A a = new A; 11 | a.x = 20; 12 | B b = new B; 13 | b.x = 16; 14 | if (a != b) {} 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/scope-1-5140519064-youyurong.mx: -------------------------------------------------------------------------------- 1 | int f(){ 2 | for (;;) int a = 1; 3 | return a; 4 | } 5 | 6 | int g(){ 7 | while(true) int b = 1; 8 | return b; 9 | } 10 | int main(){ 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/scope-2-5140519064-youyurong.mx: -------------------------------------------------------------------------------- 1 | int h(){ 2 | if (true) int c = 1; 3 | return c; 4 | } 5 | 6 | int k(){ 7 | if (true) ; 8 | else int d = 1; 9 | return d; 10 | } 11 | 12 | int l(){ 13 | if (true) ; 14 | else if (true) int e = 1; 15 | return e; 16 | } 17 | 18 | int main(){ 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/var-1-5110379024-wuhang.mx: -------------------------------------------------------------------------------- 1 | int main() { 2 | void a; 3 | } 4 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/var-2-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | int b; 2 | int c; 3 | int main() { 4 | } 5 | int c; 6 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/var-3-5120309049-liaochao.mx: -------------------------------------------------------------------------------- 1 | 2 | int main() { 3 | A x = new A; 4 | } 5 | -------------------------------------------------------------------------------- /testcase/semantic/compile_error/var-4-5140309552-wancheng.mx: -------------------------------------------------------------------------------- 1 | int a(int x) { 2 | int x = 0; 3 | return x; 4 | } 5 | 6 | int main() { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /testcase/semantic/grammar_patch_1.mill: -------------------------------------------------------------------------------- 1 | void func(int arg1, string arg2) { 2 | print("..."); 3 | } 4 | int x, y, z; 5 | int main() { 6 | int a, b = 3, c, d; 7 | a = c = d = b << 2; 8 | } -------------------------------------------------------------------------------- /testcase/semantic/grammar_patch_2.mill: -------------------------------------------------------------------------------- 1 | class experience { 2 | int naive; 3 | } 4 | int main() { 5 | if (true) 6 | if (true) 7 | print("hello"); 8 | else 9 | print("bye"); 10 | 11 | experience[][] exp = new experience[5][6]; 12 | string[] vec; 13 | int[][] mat = new int[5][6]; 14 | vec = new string[10]; 15 | } -------------------------------------------------------------------------------- /testcase/semantic/hello-5090379042-jiaxiao.mill: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | println("Hello, world!"); 4 | return 0; 5 | } -------------------------------------------------------------------------------- /testcase/semantic/language_manual.mill: -------------------------------------------------------------------------------- 1 | int Wallace = 1<<10; 2 | class sometimes { 3 | int naive; 4 | string haha; 5 | } 6 | class ArrayWrapper { 7 | sometimes[][] arr; 8 | } 9 | int get(int n) { 10 | if (n == 0) return 0; 11 | return 1 + get(n-1); 12 | } 13 | int main() { 14 | sometimes keep = new sometimes; 15 | int n = 3; 16 | keep.naive = 2 + get(n) * 4 / 5; 17 | bool fun = (keep.naive - 5 * 3) != (666 ^ 7); 18 | while (getInt() < Wallace) { 19 | keep.naive++; 20 | ++n; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /testcase/semantic/passed/basicopt1-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int[][] a = new int[100][]; 4 | int i; 5 | int j; 6 | for (i = 0; i < 100; i++) 7 | a[i] = new int[100]; 8 | int sum = 0; 9 | 10 | for (i = 0;i < 100;i++) 11 | for (j = 0;j < 100;j++) 12 | a[i][j] = 0; 13 | for (i = 0;i < 100;i++) 14 | if (i > 20 && i < 80) { 15 | for (j = 0;j < 100;j++) 16 | if (j > 5 || i < 90) 17 | a[i][j] = j + (100 - 1 + 1 - 1 + 1) / 2; 18 | } 19 | 20 | for (i = 0;i < 100;i++) 21 | for (j = 0;j < 100;j++) 22 | sum = sum + a[i][j]; 23 | println(toString(sum)); 24 | } 25 | -------------------------------------------------------------------------------- /testcase/semantic/passed/builtin-5140519064-youyurong.mx: -------------------------------------------------------------------------------- 1 | int main(){ 2 | int[] a = new int[3]; 3 | print("I have done " + toString(a.size()) + " little things"); 4 | println("too young!"); 5 | print(getString() + toString("\"hahaha\"".length())); 6 | println("four scores and seven years".substring(0, getInt())); 7 | println(toString("45 best songs in the world".parseInt())); 8 | return "45 best songs".ord(5); 9 | } 10 | -------------------------------------------------------------------------------- /testcase/semantic/passed/bulgarian-5110379024-wuhang.mx: -------------------------------------------------------------------------------- 1 | // Target: Simulate a Bulgarian-solitaire game. 2 | // Possible opitimization: Dead code elimination, common expression, inline function, loop unrolling, etc. 3 | // REMARKS: A funny game. If you like, you can try to prove that when n=1+2+..+i(i>0), the game will always stop 4 | // and converge to the only solution: {1,2,...i}. :) 5 | 6 | int n; 7 | int h; 8 | int now; 9 | int[] a; 10 | int A = 48271; 11 | int M = 2147483647; 12 | int Q; 13 | int R; 14 | int seed=1; 15 | int random() { 16 | int tempseed = A * (seed % Q) - R * (seed / Q); 17 | if (tempseed >= 0) 18 | return tempseed; 19 | else 20 | return tempseed + M; 21 | } 22 | void initialize(int val) { 23 | seed = val; 24 | } 25 | void swap(int x,int y) { 26 | int temp = a[x]; 27 | a[x] = a[y]; 28 | a[y] = temp; 29 | } 30 | bool pd(int x) { 31 | for (;h <= x; ++h) 32 | if (x == h * (h + 1) / 2) 33 | return true; 34 | return false; 35 | } 36 | void show() { 37 | int i; 38 | for (i = 0; i < now; ++i) 39 | print(toString(a[i]) + " "); 40 | println(""); 41 | } 42 | bool win() 43 | { 44 | int i; 45 | int j; 46 | int[] b = new int[100]; 47 | int temp; 48 | if (now != h) 49 | return false; 50 | for (j = 0; j < now; ++j) 51 | b[j] = a[j]; 52 | for (i = 0;i < now - 1; ++i) 53 | for (j = i + 1;j < now; ++j) 54 | if (b[i] > b[j]) { 55 | temp = b[i]; 56 | b[i] = b[j]; 57 | b[j] = temp; 58 | } 59 | for (i = 0; i < now; ++i) 60 | if (b[i] != i + 1) 61 | return false; 62 | return true; 63 | } 64 | void merge() 65 | { 66 | int i; 67 | for (i = 0;i < now; ++i) 68 | if (a[i] != 0) { 69 | int j; 70 | for (j = i+1; j < now; ++j) 71 | if (a[j] != 0) { 72 | swap(i,j); 73 | break; 74 | } 75 | } 76 | for (i=0;i n) 111 | a[i] = random() % 10 + 1; 112 | temp = temp + a[i]; 113 | } 114 | a[now - 1] = n - temp; 115 | show(); 116 | merge(); 117 | while (!win()) { 118 | println("step :" + toString(++count)); 119 | move(); 120 | merge(); 121 | show(); 122 | } 123 | println("Total: " + toString(count) + " step(s)"); 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /testcase/semantic/passed/hanoi-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int cd(int d, string a, string b, string c, int sum) { 2 | if (d == 1) { 3 | println("move " + a + " --> " + c); 4 | sum++; 5 | } else { 6 | sum = cd(d - 1, a, c, b, sum); 7 | println("move " + a + " --> " + c); 8 | sum = cd(d - 1, b, a, c, sum); 9 | sum++; 10 | } 11 | return sum; 12 | } 13 | 14 | int main() { 15 | string a = "A"; 16 | string b = "B"; 17 | string c = "C"; 18 | int d = getInt(); 19 | int sum = cd(d, a, b, c, 0); 20 | println(toString(sum)); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /testcase/semantic/passed/hashmap-5100309127-hetianxing.mx: -------------------------------------------------------------------------------- 1 | int hashsize = 100; 2 | class node { 3 | int key; 4 | int data; 5 | node next; 6 | } 7 | node[] table; 8 | int getHash(int n) { 9 | return (n * 237) % hashsize; 10 | } 11 | void put(int key, int data) { 12 | int p; 13 | node ptr = null; 14 | p = getHash(key); 15 | if (table[p] == null) { 16 | table[p] = new node; 17 | table[p].key = key; 18 | table[p].data = data; 19 | table[p].next = null; 20 | return; 21 | } 22 | ptr = table[p]; 23 | while (ptr.key != key) { 24 | if (ptr.next == null) { 25 | ptr.next = new node; 26 | ptr.next.key = key; 27 | ptr.next.next = null; 28 | } 29 | ptr = ptr.next; 30 | } 31 | ptr.data = data; 32 | } 33 | int get(int key) { 34 | node ptr = null; 35 | ptr = table[getHash(key)]; 36 | while (ptr.key != key) { 37 | ptr = ptr.next; 38 | } 39 | return ptr.data; 40 | } 41 | int main() { 42 | int i; 43 | table = new node[100]; 44 | for (i = 0;i < hashsize;i++) 45 | table[i] = null; 46 | for (i = 0;i < 1000;i++) 47 | put(i, i); 48 | for (i = 0;i < 1000;i++) 49 | println(toString(i) + " " + toString(get(i))); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /testcase/semantic/passed/heapsort-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int n; 2 | int[] a; 3 | 4 | void exchange(int x,int y) 5 | { 6 | int t=a[x]; 7 | a[x]=a[y]; 8 | a[y]=t; 9 | } 10 | 11 | int makeHeap() { 12 | int i; 13 | int t; 14 | int j; 15 | i = (n - 1) / 2; 16 | t = j = 0; 17 | while (i >= 0) { 18 | j = i * 2; 19 | if (i*2+1 a[j]) { 21 | exchange(i,j); 22 | } 23 | i = i - 1; 24 | } 25 | return 0; 26 | } 27 | 28 | int adjustHeap(int n) { 29 | int i; 30 | int j; 31 | int t; 32 | i = j = t = 0; 33 | while (i * 2 < n) { 34 | j = i*2; 35 | if (i*2+1 a[j]) { 37 | int t = a[i]; 38 | a[i] = a[j]; 39 | a[j] = t; 40 | i = j; 41 | } 42 | else break; 43 | } 44 | return 0; 45 | } 46 | 47 | int heapSort() { 48 | int t; 49 | int k; 50 | t = 0; 51 | for (k = 0; k < n; k = k + 1) { 52 | t = a[0]; 53 | a[0] = a[n-k-1]; 54 | a[n-k-1] = t; 55 | adjustHeap(n-k-1); 56 | } 57 | return 0; 58 | } 59 | 60 | int main() { 61 | int i; 62 | n = getString().parseInt(); 63 | a = new int[n]; 64 | 65 | for (i = 0; i < a.size(); i = i + 1) 66 | a[i] = i; 67 | makeHeap(); 68 | heapSort(); 69 | for (i = 0; i < a.size(); i = i + 1) 70 | print(toString(a[i]) + " "); 71 | print("\n"); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /testcase/semantic/passed/horse-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int head; 3 | int startx; 4 | int starty; 5 | int targetx; 6 | int targety; 7 | int x; 8 | int y; 9 | int[] xlist = new int[12000]; 10 | int[] ylist = new int[12000]; 11 | int tail; 12 | int ok; 13 | int now; 14 | int[][] step; 15 | int i; 16 | int j; 17 | 18 | void origin(int N) 19 | { 20 | step = new int[N][]; 21 | for (i = 0; i < N; i = i ++ ) { 22 | step[i] = new int[N]; 23 | for (j = 0; j < N; j = j ++ ) 24 | step[i][j] = 0; 25 | } 26 | } 27 | 28 | bool check(int a, int N) { 29 | return ((a < N) && (a >= 0)); 30 | } 31 | 32 | void addList(int x, int y) { 33 | if (check(x, N) && check(y, N) && step[x][y] == -1) { 34 | tail = tail + 1; 35 | xlist[tail] = x; 36 | ylist[tail] = y; 37 | step[x][y] = now + 1; 38 | if ((x == targetx) && (y == targety)) ok = 1; 39 | } 40 | } 41 | 42 | int main() { 43 | origin(106); 44 | N = getInt(); 45 | targetx = targety = N - 1; 46 | for (i = 0; i < N; i ++) 47 | for (j = 0; j < N; j ++) 48 | step[i][j] = -1; 49 | while (head <= tail) { 50 | x = xlist[head]; 51 | y = ylist[head]; 52 | now = step[x][y]; 53 | addList(x-1, y-2); 54 | addList(x-1, y+2); 55 | addList(x+1, y-2); 56 | addList(x+1, y+2); 57 | addList(x-2, y-1); 58 | addList(x-2, y+1); 59 | addList(x+2, y-1); 60 | addList(x+2, y+1); 61 | if (ok == 1) break; 62 | head = head + 1; 63 | } 64 | if (ok == 1) println(toString(step[targetx][targety])); 65 | else print("no solution!\n"); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /testcase/semantic/passed/horse3-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int head; 3 | int startx; 4 | int starty; 5 | int targetx; 6 | int targety; 7 | int x; 8 | int y; 9 | int[] xlist = new int[12000]; 10 | int[] ylist = new int[12000]; 11 | int tail; 12 | int ok; 13 | int now; 14 | int[] dx = new int[8]; 15 | int[] dy = new int[9]; 16 | int[][] step; 17 | int i; 18 | int j; 19 | 20 | void origin(int N) 21 | { 22 | step = new int[N][]; 23 | for (i = 0; i < N; i = i ++ ) { 24 | step[i] = new int[N]; 25 | for (j = 0; j < N; j = j ++ ) 26 | step[i][j] = 0; 27 | } 28 | } 29 | 30 | bool check(int a) { 31 | return ((a < N) && (a >= 0)); 32 | } 33 | 34 | void addList(int x, int y) { 35 | if (check(x) && check(y) && step[x][y] == -1) { 36 | tail ++; 37 | xlist[tail] = x; 38 | ylist[tail] = y; 39 | step[x][y] = now + 1; 40 | if ((x == targetx) && (y == targety)) ok = 1; 41 | } 42 | } 43 | int main() { 44 | origin(106); 45 | N = getInt(); 46 | targetx = targety = N - 1; 47 | for (i = 0; i < N; i ++) 48 | for (j = 0; j < N; j ++) 49 | step[i][j] = -1; 50 | dx[0] = -2; dy[0] = -1; 51 | dx[1] = -2; dy[1] = 1; 52 | dx[2] = 2; dy[2] = -1; 53 | dx[3] = 2; dy[3] = 1; 54 | dx[4] = -1; dy[4] = -2; 55 | dx[5] = -1; dy[5] = 2; 56 | dx[6] = 1; dy[6] = -2; 57 | dx[7] = 1; dy[7] = 2; 58 | while (head <= tail) { 59 | x = xlist[head]; 60 | y = ylist[head]; 61 | now = step[x][y]; 62 | for (j = 0;j < 8;j ++) 63 | addList(x + dx[j], y + dy[j]); 64 | if (ok == 1) break; 65 | head ++; 66 | } 67 | if (ok == 1) println(toString(step[targetx][targety])); 68 | else print("no solution!\n"); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /testcase/semantic/passed/lvalue2-5110379024-wuhang.mx: -------------------------------------------------------------------------------- 1 | int[] a = new int[4]; 2 | int main() 3 | { 4 | int[] b = new int[4]; 5 | a=b; 6 | } 7 | -------------------------------------------------------------------------------- /testcase/semantic/passed/magic-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int[][] make; 2 | int[] color = new int[10]; 3 | int[] count = new int[1]; 4 | int i; 5 | int j; 6 | 7 | void origin(int N) 8 | { 9 | make = new int[N][]; 10 | for (i = 0; i < N; i = i ++ ) { 11 | make[i] = new int[N]; 12 | for (j = 0; j < N; j = j ++ ) 13 | make[i][j] = 0; 14 | } 15 | } 16 | 17 | int search(int x, int y, int z) 18 | { 19 | int s; 20 | int i; 21 | int j; 22 | if ((y > 0 || y < 0) || x == 0 || make[x-1][0] + make[x-1][1] + make[x-1][2] == 15) 23 | { 24 | if (x == 2 && y == 2) { 25 | make[2][2] = 45 - z; 26 | s = make[0][0] + make[0][1] + make[0][2]; 27 | if (make[1][0] + make[1][1] + make[1][2] == s && 28 | make[2][0] + make[2][1] + make[2][2] == s && 29 | make[0][0] + make[1][0] + make[2][0] == s && 30 | make[0][1] + make[1][1] + make[2][1] == s && 31 | make[0][2] + make[1][2] + make[2][2] == s && 32 | make[0][0] + make[1][1] + make[2][2] == s && 33 | make[2][0] + make[1][1] + make[0][2] == s) 34 | { 35 | count[0] = count[0] + 1; 36 | for (i = 0;i <= 2;i ++) 37 | { 38 | for (j = 0;j <= 2;j ++) 39 | { 40 | print(toString(make[i][j])); 41 | print(" "); 42 | } 43 | print("\n"); 44 | } 45 | print("\n"); 46 | } 47 | } 48 | else { 49 | if (y == 2) { 50 | make[x][y] = 15 - make[x][0] - make[x][1]; 51 | if (make[x][y] > 0 && make[x][y] < 10 && color[make[x][y]] == 0) { 52 | color[make[x][y]] = 1; 53 | if (y == 2) 54 | search(x + 1, 0, z+make[x][y]); 55 | else 56 | search(x, y+1, z+make[x][y]); 57 | color[make[x][y]] = 0; 58 | } 59 | } 60 | else { 61 | for (i = 1;i <= 9;i ++) { 62 | if (color[i] == 0) { 63 | color[i] = 1; 64 | make[x][y] = i; 65 | if (y == 2) 66 | search(x + 1, 0, z+i); 67 | else 68 | search(x, y+1, z+i); 69 | make[x][y] = 0; 70 | color[i] = 0; 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | int main() 78 | { 79 | origin(3); 80 | search(0, 0, 0); 81 | println(toString(count[0])); 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /testcase/semantic/passed/maxflow-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int[][] c; 2 | int ans; 3 | int[] visit = new int[110]; 4 | int[] pre = new int[110]; 5 | int[] f = new int[110]; 6 | int i; 7 | int j; 8 | int open; 9 | int closed; 10 | 11 | void origin(int N) 12 | { 13 | c = new int[N][]; 14 | for (i = 0; i < N; i = i ++ ) { 15 | c[i] = new int[N]; 16 | for (j = 0; j < N; j = j ++ ) 17 | c[i][j] = 0; 18 | } 19 | } 20 | 21 | int build(int start, int ending) { 22 | for (i = 1; i <= 49; i++) { 23 | for (j = 50; j <= 98-i+1; j++) { 24 | c[i][j] = 1; 25 | } 26 | } 27 | for (i = 1; i <= 49; i++) 28 | c[start][i] = 1; 29 | for (i = 50; i <= 98; i++) 30 | c[i][ending] = 1; 31 | return 0; 32 | } 33 | 34 | int find(int ending, int start, int flag) { 35 | open = 0; 36 | closed = 1; 37 | for (i = 1; i <= ending; i++) { 38 | visit[i] = 0; 39 | } 40 | f[1]=start; 41 | visit[start]=1; 42 | pre[start]=0; 43 | flag=0; 44 | while (open0 && visit[j]==0) { 49 | visit[j]=1; 50 | closed++; 51 | f[closed]=j; 52 | pre[j]=i; 53 | if (closed==ending) flag=1; 54 | } 55 | } 56 | return flag; 57 | } 58 | 59 | int improve(int ending) { 60 | i=ending; 61 | ans++; 62 | while (pre[i]>0) { 63 | j=pre[i]; 64 | c[j][i]--; 65 | c[i][j]++; 66 | i=j; 67 | } 68 | return 0; 69 | } 70 | 71 | int main() { 72 | origin(110); 73 | int k; 74 | int start; 75 | int ending; 76 | int flag; 77 | int i1; 78 | 79 | k = 0; 80 | start = 99; 81 | ending = 100; 82 | flag = 0; 83 | 84 | build(start, ending); 85 | while (find(ending, start, flag)>0) { 86 | improve(ending); 87 | } 88 | println(toString(ans)); 89 | return 0; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /testcase/semantic/passed/prime-5100309153-yanghuan.mx: -------------------------------------------------------------------------------- 1 | int N; 2 | int M; 3 | int i; 4 | int j; 5 | int primeCount; 6 | int resultCount; 7 | int[] b = new int[1001]; 8 | int[] prime = new int[170]; 9 | int[] gps = new int[1001]; 10 | int[] tmp = new int[1]; 11 | int[][] result; 12 | 13 | void origin(int N) 14 | { 15 | result = new int[N][]; 16 | for (i = 0; i < N; i = i ++ ) { 17 | result[i] = new int[N]; 18 | for (j = 0; j < N; j = j ++ ) 19 | result[i][j] = 0; 20 | } 21 | } 22 | 23 | //int getPrime(int N, int* primeCount, int* b, int* gps, int* prime) { 24 | int getPrime(int N) { 25 | int count; 26 | int i; 27 | count = 2; 28 | for (i = 2; i <= N; i = i + 1) { 29 | if (b[i] == 1) { 30 | tmp[0] = tmp[0] + 1; 31 | prime[tmp[0]] = i; 32 | gps[i] = tmp[0]; 33 | } 34 | while (i * count <= N) { 35 | b[i * count] = 0; 36 | count = count + 1; 37 | } 38 | count = 2; 39 | } 40 | } 41 | 42 | //int getResult(int N, int k1, int k2, int* b, int* gps, int* prime, int result[170][170]) { 43 | int getResult(int N, int k1, int k2) { 44 | if (result[k1][k2] == -1) 45 | if (prime[k2] * 2 - prime[k1] <= N) 46 | if (b[prime[k2] * 2 - prime[k1]] != 0) 47 | result[k1][k2] = getResult(N, 48 | k2, 49 | gps[prime[k2] * 2 - prime[k1]] 50 | ) + 1; 51 | if (result[k1][k2] == -1) 52 | result[k1][k2] = 1; 53 | return result[k1][k2]; 54 | } 55 | 56 | int printF(int k1, int k2, int k3) { 57 | // printf("%d", k1); 58 | print(toString(k1)); 59 | while (k3 > 0) { 60 | // printf("%c", ' '); 61 | print(" "); 62 | // printf("%d", k2); 63 | print(toString(k2)); 64 | k2 = k2 * 2 - k1; 65 | k1 = (k1 + k2) / 2; 66 | k3 = k3 - 1; 67 | } 68 | // printf("%c", '\n'); 69 | print("\n"); 70 | } 71 | 72 | int main() { 73 | origin(170); 74 | N = 1000; 75 | // M = 100*(getchar()-'0')+10*(getchar()-'0')+getchar()-'0'; 76 | M = getInt(); 77 | primeCount = 0; 78 | resultCount = 0; 79 | tmp[0] = 0; 80 | for (i = 0; i < N + 1; i = i + 1) 81 | { 82 | b[i] = 1; 83 | gps[i] = 0; 84 | } 85 | for (i = 0; i < M + 1; i = i + 1) 86 | { 87 | prime[i] = 0; 88 | } 89 | for (i = 0; i <= M; i = i + 1) 90 | { 91 | for (j = 0; j <= M; j = j + 1) 92 | result[i][j] = -1; 93 | } 94 | // getPrime(N, tmp, b, gps, prime); 95 | getPrime(N); 96 | primeCount = tmp[0]; 97 | for (i = 1; i < primeCount; i = i + 1) 98 | for (j = i + 1; j <= primeCount; j = j + 1) 99 | if (result[i][j] == -1) { 100 | result[i][j] = getResult(N, i, j); 101 | if (result[i][j] > 1) { 102 | printF(prime[i], prime[j], result[i][j]); 103 | resultCount = resultCount + 1; 104 | } 105 | } 106 | //printf("Total: %d\n", resultCount); 107 | print("Total: "); 108 | println(toString(resultCount)); 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /testcase/semantic/passed/qsort-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | // Target: qsort 2 | // Possible optimization: Dead code elimination, common expression, strength reduction 3 | // REMARKS: nothing. 4 | // 5 | // 6 | 7 | //int a[10100]; 8 | int[] a = new int[10100]; 9 | int n = 10000; 10 | 11 | int qsrt(int l, int r) { 12 | int i = l; 13 | int j = r; 14 | int x = a[(l + r) / 2]; 15 | while (i <= j) { 16 | while (a[i] < x) i++; 17 | while (a[j] > x) j--; 18 | if (i <= j) { 19 | int temp = a[i]; 20 | a[i] = a[j]; 21 | a[j] = temp; 22 | i++; 23 | j--; 24 | } 25 | } 26 | if (l < j) qsrt(l, j); 27 | if (i < r) qsrt(i, r); 28 | return 0; 29 | } 30 | 31 | int main() { 32 | int i; 33 | for (i = 1; i <= n; i++) 34 | a[i] = n + 1 - i; 35 | qsrt(1, n); 36 | for (i = 1; i <= n; i++) { 37 | // printf("%d ", a[i]); 38 | print(toString(a[i])); 39 | print(" "); 40 | } 41 | print("\n"); 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /testcase/semantic/passed/queens-5100379110-daibo.mx: -------------------------------------------------------------------------------- 1 | int N = 8; 2 | int[] row = new int[8]; 3 | int[] col = new int[8]; 4 | int[][] d = new int[2][]; 5 | 6 | void printBoard() { 7 | int i; 8 | int j; 9 | for (i = 0; i < N; i++) { 10 | for (j = 0; j < N; j++) { 11 | if (col[i] == j) 12 | print(" O"); 13 | else 14 | print(" ."); 15 | } 16 | println(""); 17 | } 18 | println(""); 19 | } 20 | 21 | void search(int c) { 22 | if (c == N) { 23 | printBoard(); 24 | } 25 | else { 26 | int r; 27 | for (r = 0; r < N; r++) { 28 | if (row[r] == 0 && d[0][r+c] == 0 && d[1][r+N-1-c] == 0) { 29 | row[r] = d[0][r+c] = d[1][r+N-1-c] = 1; 30 | col[c] = r; 31 | search(c+1); 32 | row[r] = d[0][r+c] = d[1][r+N-1-c] = 0; 33 | } 34 | } 35 | } 36 | } 37 | 38 | int main() { 39 | int i; 40 | for (i = 0; i < 2; i ++) 41 | d[i] = new int[8 + 8 - 1]; 42 | search(0); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /testcase/semantic/passed/superloop-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | //Target: use loops to calculate calculator of 6! 2 | //@author yixi 3 | 4 | int N; 5 | int h = 99; 6 | int i = 100; 7 | int j = 101; 8 | int k = 102; 9 | int total = 0; 10 | 11 | int main() { 12 | int a; 13 | int b; 14 | int c; 15 | int d; 16 | int e; 17 | int f; 18 | N=getInt(); 19 | for ( a=1; a<=N; a++ ) 20 | for ( b=1; b<=N; b++ ) 21 | for ( c=1; c<=N; c++ ) 22 | for ( d=1; d<=N; d++ ) 23 | for ( e=1; e<=N; e++ ) 24 | for ( f=1; f<=N; f++ ) 25 | if (a!=b && a!=c && a!=d && a!=e && a!=f && a!=h && a!=i && a!=j && a!=k 26 | && b!=c && b!=d && b!=e && b!=f && b!=h && b!=i && b!=j && b!=k 27 | && c!=d && c!=e && c!=f && c!=h && c!=i && c!=j && c!=k 28 | && d!=e && d!=f && d!=h && d!=i && d!=j && d!=k 29 | && e!=f && e!=h && e!=i && e!=j && e!=k 30 | && f!=h && f!=i && f!=j && f!=k && i!=j && h!=k) 31 | { 32 | total++; 33 | } 34 | 35 | println(toString(total)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /testcase/semantic/passed/tak-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | int tak(int x, int y, int z) { 2 | if ( y < x ) return 1 + tak( tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y) ); 3 | else return z; 4 | } 5 | 6 | int main(){ 7 | int a; 8 | int b; 9 | int c; 10 | a=getInt(); 11 | b=getInt(); 12 | c=getInt(); 13 | println(toString(tak(a,b,c))); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /testcase/semantic/passed/twinprime-5090379042-jiaxiao.mx: -------------------------------------------------------------------------------- 1 | 2 | int N = 15000; 3 | bool[] b = new bool[15001]; 4 | int resultCount = 0; 5 | 6 | int main() 7 | { 8 | int i; 9 | 10 | for (i = 1; i <= N; i++) b[i] = true; 11 | 12 | for (i = 2; i <= N; i++) if (b[i]) 13 | { 14 | int count = 2; 15 | 16 | if (i>3 && b[i-2]) 17 | { 18 | resultCount++; 19 | println(toString(i-2) + " " + toString(i)); 20 | } 21 | 22 | while (i*count <= N) 23 | { 24 | b[i*count] = false; 25 | count++; 26 | } 27 | } 28 | 29 | println("Total: " + toString(resultCount)); 30 | return 0; 31 | } 32 | --------------------------------------------------------------------------------