-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathigniter.cpp
More file actions
124 lines (93 loc) · 3.05 KB
/
Copy pathigniter.cpp
File metadata and controls
124 lines (93 loc) · 3.05 KB
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
#include <iostream>
#include "lex.h"
#include "igniter.tab.hpp"
#include "ast/AstPackage.h"
#include <vector>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetRegistry.h"
#include "KaleidoscopeJIT.h"
#include "llvm/Target/TargetOptions.h"
#include <llvm/Support/FileSystem.h>
using namespace std;
extern FILE *yyin;
extern int yydebug;
int llvmdebug;
AstPackage *currentPackage;
int generateCode(Module *);
int main(int argc, char *argv[]) {
yydebug = 0; // 是否开启语法分析日志输出
llvmdebug = 0; // 是否开启LLVM-IR输出
if (argc <= 1) {
cerr << "filename not specified, reading from stdin." << endl;
} else {
char *filename = argv[1];
yyin = fopen(filename, "r");
}
AstContext context(nullptr);
currentPackage = new AstPackage(context);
try {
bool success = yyparse() == 0;
if (!success) {
cout << "parsing failed" << endl;
exit(1);
}
cerr << "\n====< 已完成语法分析, 准备生成代码. >=====\n" << endl;
// currentPackage->codegen(parent);
currentPackage->makeGen(&context);
currentPackage->generate();
if (llvmdebug)
context.module()->print(errs(), nullptr);
} catch (std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
auto module = context.module();
generateCode(module);
if (!module->getFunction("main")) {
cout << "还没有定义main函数!" << endl;
return 1;
}
// jitRun(module);
return 0;
}
int generateCode(Module *TheModule) {
// 这三个是用来生成代码的.
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeAllAsmParsers();
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::OF_None);
if (EC) {
errs() << "Could not open file: " << EC.message();
return 1;
}
legacy::PassManager pass;
auto FileType = CGFT_ObjectFile;
if (TheTargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) {
errs() << "TheTargetMachine can't emit a file of this type";
return 1;
}
pass.run(*TheModule);
dest.flush();
cerr << "Wrote to " << Filename << "\n";
return 0;
}