-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
201 lines (182 loc) · 6.87 KB
/
main.cpp
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
#include <iostream>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/PassManager.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/CodeGen/CommandFlags.h>
using namespace std;
#include "SyntaxTree.h"
#include "SymbolTable.h"
#include "TypeChecking.h"
#include "MiddleWare.h"
#include "node.h"
#include "lex.yy.c"
extern void createSystemFunctions(CodeGenContext& context);
LLVMContext &context = getGlobalContext();
Module module("test",context);
IRBuilder<> builder(context);
Function *startFunc = NULL;
string errorMsg;
FunctionAST *program = NULL;
int main(int argc,char **argv){
bool runJit = false;
bool irOutput = true;
bool asmOutput = true;
bool objOutput = true;
TargetMachine::CodeGenFileType outputFileType = TargetMachine::CGFT_Null;
char *outputFileName = NULL;
int option;
/* ================================================= */
yyin = fopen(argv[1], "r");
fout = fopen(argv[2], "w");
lineNo = 1;
yyparse();
BuildSymTab();
TypeChecking(root);
//fprintf(fout, "root");
//if (!root) printf("null\n");
//print_tree(root, 1);
program = CreateFunctionAST(root);
// program->print(0);
/* ================================================= */
CodeGenContext astContext;
//astContext.addType("long",builder.getInt64Ty());
//astContext.addType("double",builder.getDoubleTy());
//astContext.addType("bool",builder.getInt1Ty());
createSystemFunctions(astContext);
program->Codegen(astContext);
//module.dump();cout<<endl;
InitializeNativeTarget();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();
char *inputFileName = NULL;
if(irOutput){
string opFileName;
if(outputFileName == NULL){
if(inputFileName == NULL){
opFileName = "temp.ir";
}else{
opFileName = string(basename(inputFileName)) + ".ir";
}
}else{
opFileName = outputFileName;
}
string errorMsg;
tool_output_file outputFile(opFileName.c_str(),errorMsg);
if(!errorMsg.empty()){
cout<<errorMsg<<endl;
return 1;
}
outputFile.os()<<module;
outputFile.keep();
}
if(outputFileType != TargetMachine::CGFT_Null){
Triple triple(sys::getDefaultTargetTriple());
string errorMsg;
const Target *target = TargetRegistry::lookupTarget(MArch,triple,errorMsg);
if(target == NULL){
cout<<errorMsg<<endl;
return 1;
}
TargetOptions targetOptions;
TargetMachine *targetMachine =
target->createTargetMachine(triple.getTriple(),MCPU,"",targetOptions);
string opFileName;
if(outputFileName == NULL){
if(inputFileName == NULL){
if(asmOutput){
opFileName = "temp.s";
}else{
opFileName = "temp.o";
}
}else{
if(asmOutput){
opFileName = string(basename(inputFileName)) + ".s";
}else{
opFileName = string(basename(inputFileName)) + ".o";
}
}
}else{
opFileName = outputFileName;
}
string errorMsg2;
tool_output_file *outputFile = new tool_output_file(opFileName.c_str(),errorMsg2);
if(!errorMsg2.empty()){
cout<<errorMsg2<<endl;
return 1;
}
PassManager passManager;
passManager.add(new DataLayout(&module));
formatted_raw_ostream fos(outputFile->os());
targetMachine->addPassesToEmitFile(passManager,fos,outputFileType);
passManager.run(module);
outputFile->keep();
}
if(runJit){
string errStr;
ExecutionEngine *execEngine = EngineBuilder(&module).setErrorStr(&errStr).setEngineKind(EngineKind::JIT).create();
if(execEngine == NULL){
cout<<"Could not create ExecutionEngine: "<<errStr<<endl;
exit(1);
}
vector<GenericValue> argValues;
execEngine->runFunction(startFunc,argValues);
}
return 0;
}
void createSystemFunctions(CodeGenContext& astContext){
//insert printf func decl
vector<Type*> printfFuncArgTypes;
printfFuncArgTypes.push_back(builder.getInt8PtrTy());
ArrayRef<Type*> printfFuncArgTypesRef(printfFuncArgTypes);
FunctionType *printfFuncType = FunctionType::get(builder.getInt32Ty(),printfFuncArgTypesRef,true);
Constant *printfFunc = module.getOrInsertFunction("printf",printfFuncType);
vector<Type*> emptyTypes;
//create print integer func
vector<Type*> printfLongFuncArgTypes;
printfLongFuncArgTypes.push_back(builder.getInt32Ty());
ArrayRef<Type*> printfLongFuncArgTypesRef(printfLongFuncArgTypes);
FunctionType *printfLongFuncType = FunctionType::get(builder.getVoidTy(),printfLongFuncArgTypesRef,false);
Function *printfLongFunc = Function::Create(printfLongFuncType,Function::ExternalLinkage,"writeI",&module);
builder.SetInsertPoint(BasicBlock::Create(context,"entry",printfLongFunc));
Value *longFormat = builder.CreateGlobalStringPtr("%d");
builder.CreateCall2(printfFunc,longFormat,printfLongFunc->arg_begin());
builder.CreateRetVoid();
//create print real func
vector<Type*> printfDoubleFuncArgTypes;
printfDoubleFuncArgTypes.push_back(builder.getDoubleTy());
ArrayRef<Type*> printfDoubleFuncArgTypesRef(printfDoubleFuncArgTypes);
FunctionType *printfDoubleFuncType = FunctionType::get(builder.getVoidTy(),printfDoubleFuncArgTypesRef,false);
Function *printfDoubleFunc = Function::Create(printfDoubleFuncType,Function::ExternalLinkage,"writeR",&module);
builder.SetInsertPoint(BasicBlock::Create(context,"entry",printfDoubleFunc));
Value *doubleFormat = builder.CreateGlobalStringPtr("%lf");
builder.CreateCall2(printfFunc,doubleFormat,printfDoubleFunc->arg_begin());
builder.CreateRetVoid();
//create print string func
vector<Type*> pirntfStringFuncArgTypes;
pirntfStringFuncArgTypes.push_back(builder.getInt8Ty()->getPointerTo());
ArrayRef<Type*> printfStringFuncArgTypesRef(pirntfStringFuncArgTypes);
FunctionType *printfStringFuncType = FunctionType::get(builder.getVoidTy(), printfStringFuncArgTypesRef, false);
Function *printfStringFunc = Function::Create(printfStringFuncType, Function::ExternalLinkage, "writeS", &module);
builder.SetInsertPoint(BasicBlock::Create(context, "entry", printfStringFunc));
Value *stringFormat = builder.CreateGlobalStringPtr("%s");
builder.CreateCall2(printfFunc,stringFormat,printfStringFunc->arg_begin());
builder.CreateRetVoid();
//create println func
FunctionType *printlnFuncType = FunctionType::get(builder.getVoidTy(),false);
Function *printlnFunc = Function::Create(printlnFuncType,Function::ExternalLinkage,"writeln",&module);
builder.SetInsertPoint(BasicBlock::Create(context,"entry",printlnFunc));
Value *lnFormat = builder.CreateGlobalStringPtr("\n");
builder.CreateCall(printfFunc,lnFormat);
builder.CreateRetVoid();
}