-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
143 lines (102 loc) · 3.71 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
#include <iostream>
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "parser.h"
#include "ast.h"
using namespace llvm;
using namespace std;
// Very much inspired on "http://llvm.org/docs/tutorial"
int createObject(Module *module) {
// Initialize the target registry etc.
InitializeNativeTarget();
InitializeNativeTargetAsmParser();
InitializeNativeTargetAsmPrinter();
auto TargetTriple = sys::getDefaultTargetTriple();
module->setTargetTriple(TargetTriple);
std::string Error;
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
// Print an error and exit if we couldn't find the requested target.
// This generally occurs if we've forgotten to initialise the
// TargetRegistry or we have a bogus target triple.
if (!Target) {
errs() << Error;
return 1;
}
auto CPU = "generic";
auto Features = "";
TargetOptions opt;
auto RM = Optional<Reloc::Model>();
auto TheTargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
module->setDataLayout(TheTargetMachine->createDataLayout());
module->setTargetTriple(TargetTriple);
auto Filename = "output.o";
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
if (EC) {
errs() << "Could not open file: " << EC.message();
return 1;
}
//Print generated code
module->dump();
legacy::PassManager pass;
auto FileType = TargetMachine::CGFT_ObjectFile;
if (TheTargetMachine->addPassesToEmitFile(pass, dest, FileType)) {
errs() << "TheTargetMachine can't emit a file of this type";
return 1;
}
pass.run(*module);
dest.flush();
outs() << "Wrote " << Filename << "\n";
return 0;
}
int main() {
//string fileName = "../examples/global.p";
//string fileName = "../examples/readVar.p";
//string fileName = "../examples/if.p";
//string fileName = "../examples/while.p";
//string fileName = "../examples/whileBreak.p";
//string fileName = "../examples/forCycle.p";
//string fileName = "../examples/for.p";
string fileName = "../examples/arrayTest.p";
//Important objects!
LLVMContext context;
IRBuilder<> builder(context);
Module *module = new Module("mainTest", context);
map<string, Value *> namedValues;
BasicBlock * breakTarget;
// create -> MAIN()
Type *returnType = Type::getInt32Ty(context);
//vector<Type*> argTypes;
FunctionType *mainFunctionType = FunctionType::get(returnType, false);
// ExternalLinkage - accessible through the whole program
Function *mainFunction = Function::Create(mainFunctionType, Function::ExternalLinkage, "main", module);
BasicBlock *mainBlock = BasicBlock::Create(context, "entry", mainFunction);
builder.SetInsertPoint(mainBlock);
breakTarget = mainBlock;
Parser *parser = new Parser(fileName, context, module, builder, breakTarget);
Prog *program = parser->Program();
program->GenerateIR();
//Set Return value
builder.CreateRet(ConstantInt::get(builder.getInt32Ty(), 0));
createObject(module);
delete module;
llvm_shutdown();
system("g++ output.o");
return 0;
}