-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_compiler.cpp
More file actions
140 lines (115 loc) · 3.63 KB
/
Copy pathmain_compiler.cpp
File metadata and controls
140 lines (115 loc) · 3.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "llvm/ADT/APFloat.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/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Target/TargetMachine.h"
#include "src/KaleidoscopeJIT.h"
#include <algorithm>
#include <cstdarg>
#include <cassert>
#include <cctype>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <memory>
#include <string>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>
#include <iomanip>
#include <math.h>
#include <fenv.h>
#include <tuple>
#include <chrono>
#include <thread>
#include <random>
#include <float.h>
#include <fstream>
#include <sstream>
#include <filesystem>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "src/include.h"
#include "src/pre_main.h"
using namespace llvm;
using namespace llvm::orc;
//===----------------------------------------------------------------------===//
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
void linkExecutable() {
// std::string out = R"(ld.lld-19 \
// /usr/lib/x86_64-linux-gnu/crt1.o \
// /usr/lib/x86_64-linux-gnu/crti.o \
// /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o \
// output.o \
// --start-group static/runtime.a --end-group \
// -L/usr/lib/gcc/x86_64-linux-gnu/9 -lstdc++ \
// -L/lib/x86_64-linux-gnu -lgcc_s -lgcc -lc -lm \
// /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o \
// /usr/lib/x86_64-linux-gnu/crtn.o \
// --dynamic-linker /lib64/ld-linux-x86-64.so.2 \
// -o output)";
// std::string out = "clang++-19 -no-pie output.o static/runtime.a -o output";
std::string out = "clang++-19 -static -static-libstdc++ -static-libgcc \
output.o static/runtime.a -o output";
std::cout << "" << out << "\n";
system(out.c_str());
}
static void Compile() {
for (auto &FuncAST : AllFunctions)
FuncAST->codegen();
std::error_code EC;
raw_fd_ostream dest("output.o", EC, sys::fs::OF_None);
legacy::PassManager pass;
if (CTM->addPassesToEmitFile(pass, dest, nullptr,
llvm::CodeGenFileType::ObjectFile)) {
errs() << "TargetMachine can't emit object file\n";
return;
}
pass.run(*TheModule);
dest.flush();
linkExecutable();
}
__attribute__((constructor))
void early_init() {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter(); // Prepare for target hardware
InitializeNativeTargetAsmParser();
// llvm::InitializeNVPTXTarget();
// llvm::InitializeNVPTXTargetInfo();
// llvm::InitializeNVPTXTargetMC();
// llvm::InitializeNVPTXAsmPrinter();
}
int main(int argc, char* argv[]) {
IsJIT = false;
for (int i = 1; i < argc; i++) {
// std::cout << "Argument " << i << ": " << argv[i] << "\n";
Sys_Arguments.push_back(argv[i]);
}
build_dicts();
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
InitializeModule();
InitializeTokenizer();
MainLoop();
Compile();
return 0;
}