-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMJCompiler.java
158 lines (127 loc) · 7.22 KB
/
MJCompiler.java
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
/*
* Copyright (C) 2018 Danijel Askov
*
* This file is part of MicroJava Compiler.
*
* MicroJava Compiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MicroJava Compiler is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package askov.schoolprojects.compilerconstruction.mjcompiler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java_cup.runtime.Symbol;
import askov.schoolprojects.compilerconstruction.mjcompiler.ast.Program;
import askov.schoolprojects.compilerconstruction.mjcompiler.inheritancetree.InheritanceTree;
import askov.schoolprojects.compilerconstruction.mjcompiler.inheritancetree.visitor.InheritanceTreePrinter;
import askov.schoolprojects.compilerconstruction.mjcompiler.mjsymboltable.MJTab;
import askov.schoolprojects.compilerconstruction.mjcompiler.util.Log4JUtils;
import askov.schoolprojects.compilerconstruction.mjcompiler.vmt.VMTCodeGenerator;
import askov.schoolprojects.compilerconstruction.mjcompiler.vmt.VMTCreator;
import askov.schoolprojects.compilerconstruction.mjcompiler.vmt.VMTStartAddressGenerator;
import rs.etf.pp1.mj.runtime.Code;
/**
*
* @author Danijel Askov
*/
public class MJCompiler {
static {
DOMConfigurator.configure(Log4JUtils.instance().findLoggerConfigFile());
Log4JUtils.instance().prepareLogFile(Logger.getRootLogger());
}
private static final Logger LOGGER = Logger.getLogger(MJCompiler.class);
public static void tsdump() {
MJTab.dump(LOGGER);
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
LOGGER.error("Too few arguments. Usage: MJCompiler <source-file> <obj-file>");
return;
}
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
LOGGER.error("Source file \"" + sourceFile.getAbsolutePath() + "\" has not been found!");
return;
}
LOGGER.info("Compiling source file \"" + sourceFile.getAbsolutePath() + "\"...");
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
MJLexer lexer = new MJLexer(br);
MJParser parser = new MJParser(lexer);
Symbol symbol = parser.parse();
if (!parser.lexicalErrorDetected() && !parser.syntaxErrorDetected()) {
LOGGER.info("No syntax errors have been detected in \"" + sourceFile.getAbsolutePath() + "\"");
Program program = (Program) symbol.value;
LOGGER.info("Abstract syntax tree:\n" + program.toString(""));
MJTab.init();
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
program.traverseBottomUp(semanticAnalyzer);
tsdump();
if (!semanticAnalyzer.semanticErrorDetected()) {
VMTCreator vmtCreator = new VMTCreator();
InheritanceTree.ROOT_NODE.accept(vmtCreator);
VMTStartAddressGenerator vmtStartAddressGenerator = new VMTStartAddressGenerator(
semanticAnalyzer.getStaticVarsCount());
InheritanceTree.ROOT_NODE.accept(vmtStartAddressGenerator);
Code.dataSize = semanticAnalyzer.getStaticVarsCount() + vmtStartAddressGenerator.getTotalVMTSize();
LOGGER.info("No semantic errors have been detected in \"" + sourceFile.getAbsolutePath() + "\"");
File objFile = new File(args[1]);
LOGGER.info("Generating bytecode file \"" + objFile.getAbsolutePath() + "\"...");
if (objFile.exists()) {
LOGGER.info("Deleting old bytecode file \"" + objFile.getAbsolutePath() + "\"...");
if (objFile.delete())
LOGGER.info("Old bytecode file \"" + objFile.getAbsolutePath() + "\" has been deleted.");
else
LOGGER.error("Old bytecode file \"" + objFile.getAbsolutePath() + "\" has not been deleted.");
}
CodeGenerator codeGenerator = new CodeGenerator();
if (semanticAnalyzer.printBoolMethodIsUsed()) CodeGenerator.generatePrintBoolMethod();
if (semanticAnalyzer.readBoolMethodIsUsed()) CodeGenerator.generateReadBoolMethod();
if (semanticAnalyzer.vecTimesVecMethodIsUsed()) CodeGenerator.generateVecTimesVecMethod();
if (semanticAnalyzer.vecPlusVecMethodIsUsed()) CodeGenerator.generateVecPlusVecMethod();
if (semanticAnalyzer.vecTimesScalarMethodIsUsed()) CodeGenerator.generateVecTimesScalarMethod();
if (semanticAnalyzer.scalarTimesVectorMethodIsUsed()) CodeGenerator.generateScalarTimesVectorMethod();
program.traverseBottomUp(codeGenerator);
InheritanceTreePrinter inheritanceTreeNodePrinter = new InheritanceTreePrinter();
InheritanceTree.ROOT_NODE.accept(inheritanceTreeNodePrinter);
Code.mainPc = Code.pc;
Code.put(Code.enter);
Code.put(0);
Code.put(0);
VMTCodeGenerator vmtCodeGenerator = new VMTCodeGenerator();
InheritanceTree.ROOT_NODE.accept(vmtCodeGenerator);
Code.put(Code.call);
Code.put2(codeGenerator.getMainPc() - Code.pc + 1);
Code.put(Code.exit);
Code.put(Code.return_);
Code.write(new FileOutputStream(objFile));
LOGGER.info("Bytecode file \"" + objFile.getAbsolutePath() + "\" has been generated.");
LOGGER.info("Compilation of source file \"" + sourceFile.getAbsolutePath() + "\" has finished successfully.");
} else {
LOGGER.error("Source file \"" + sourceFile.getAbsolutePath() + "\" contains semantic error(s)!");
LOGGER.error("Compilation of source file \"" + sourceFile.getAbsolutePath() + "\" has finished unsuccessfully.");
}
} else {
if (parser.lexicalErrorDetected()) {
LOGGER.error("Source file \"" + sourceFile.getAbsolutePath() + "\" contains lexical error(s)!");
}
if (parser.syntaxErrorDetected()) {
LOGGER.error("Source file \"" + sourceFile.getAbsolutePath() + "\" contains syntax error(s)!");
}
LOGGER.error("Compilation of source file \"" + sourceFile.getAbsolutePath() + "\" has finished unsuccessfully.");
}
}
}
}