├── .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