-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (75 loc) · 2.03 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
#include <llvm/ADT/STLExtras.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Pass.h>
#include <llvm/PassManager.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/CallingConv.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/IRPrintingPasses.h>
#include <llvm/IR/InlineAsm.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/FormattedStream.h>
#include <cctype>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
#include "ast.hpp"
#include "parser.hpp"
#include "scan.hpp"
#include "global.hpp"
using namespace llvm;
llvm::IRBuilder<> Builder(llvm::getGlobalContext());
int state = 0;
std::map<char, int> BinopPrecedence;
int g_line = 1;
bool debug_on = false;
int main (int argc, char* argv[]) {
BinopPrecedence['+'] = 1;
BinopPrecedence['-'] = 1;
BinopPrecedence['*'] = 2;
BinopPrecedence['/'] = 2;
BinopPrecedence['('] = 3;
BinopPrecedence[')'] = 3;
std::string file_name;
if (argc < 2) {
std::cerr << "Invalid number of arguments. Please, insert the filename to be compiled" << std::endl;
exit(-1);
}
else if (argc == 2) {
file_name = argv[1];
}
else if (argc == 3) {
if (strcmp(argv[1],"-d") == 0) {
debug_on = true;
}
file_name = argv[2];
}
//lexer
Scan_file *scan = new Scan_file(file_name);
Main_block *module = new Main_block();
int i = 0;
while (scan->scan_tok() != T_EOF) {
//parser
parser(scan,module);
}
//codegen
if (debug_on)
std::cout << std::endl << std::endl << std::endl;
module->codegen();
//module->show_dump();
PassManager PM;
PM.add(createPrintModulePass(outs()));
Module* mod = module->get_module();
PM.run(*mod);
return 0;
}