-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
139 lines (116 loc) · 3.54 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
#include <cstdio>
#include <cstdlib>
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#ifdef linux //linux
#include "KaleidoscopeJIT.h"
#else //windows
#include "../include/KaleidoscopeJIT.h"
#endif
static int emitIR = 0;
static int emitObj = 0; //如果添加了-obj选项,则只生成.o文件,不运行代码
static char *inputFileName;
#include "Lexer.h"
#include "AST.h"
#include "Parser.h"
void usage()
{
printf("usage: VSL inputFile [-r] [-h] [-obj]\n");
printf("-r: emit IR code to IRcode.ll file\n");
printf("-h: show help information\n");
printf("-obj: emit obj file of the input file\n");
exit(EXIT_FAILURE);
}
void getArgs(int argc, char *argv[])
{
for(int i = 1; i<argc; i++)
{
if(argv[i][0] == '-' && argv[i][1] == 'r')
{
emitIR = 1;
}else if (argv[i][0] == '-' && argv[i][1] == 'h')
{
usage();
}
else if (argv[i][0] == '-' && argv[i][1] == 'o')
{
if (strlen(argv[i]) > 3 &&
(argv[i][2] == 'b' && argv[i][3] == 'j'))
emitObj = 1;
else
usage();
}else
{
inputFileName = argv[i];
}
}
}
int main(int argc, char *argv[]) {
if(argc < 2)
usage();
getArgs(argc, argv);
inputFile = fopen(inputFileName, "r");
if(!inputFile){
printf("%s open error!\n", argv[0]);
exit(EXIT_FAILURE);
}
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
BinopPrecedence['+'] = 10;
BinopPrecedence['-'] = 10;
BinopPrecedence['*'] = 40;
BinopPrecedence['/'] = 40;
TheJIT = llvm::make_unique<KaleidoscopeJIT>();
InitializeModuleAndPassManager();
// Run the main "interpreter loop" now.
getNextToken();
MainLoop();
if(emitObj)
{
// Initialize the target registry etc.
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmParsers();
InitializeAllAsmPrinters();
auto TargetTriple = sys::getDefaultTargetTriple();
TheModule->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);
TheModule->setDataLayout(TheTargetMachine->createDataLayout());
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;
}
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(*TheModule);
dest.flush();
outs() << "Wrote " << Filename << "\n";
}
return 0;
}