-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbinary.cpp
More file actions
215 lines (192 loc) · 6.01 KB
/
binary.cpp
File metadata and controls
215 lines (192 loc) · 6.01 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "binary.h"
#include "expr.h"
#include "options.h"
#include "trace.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#include <llvm/CodeGen/CommandFlags.h>
#pragma clang diagnostic pop
#include <iostream>
#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Pass.h>
#include <llvm/Support/CodeGen.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/TargetParser/Host.h>
#include <llvm/TargetParser/SubtargetFeature.h>
#include <llvm/TargetParser/TargetParser.h>
#include <llvm/TargetParser/Triple.h>
#include <system_error>
static llvm::codegen::RegisterCodeGenFlags CGF;
std::string GetFeatureString()
{
std::vector<std::string> mattrs = llvm::codegen::getMAttrs();
llvm::SubtargetFeatures Features;
for (auto m : mattrs)
{
Features.AddFeature(m);
}
return Features.getString();
}
static llvm::ToolOutputFile* GetOutputStream(const std::string& filename)
{
// Open the file.
std::error_code error;
llvm::sys::fs::OpenFlags OpenFlags = llvm::sys::fs::OF_None;
llvm::ToolOutputFile* FDOut = new llvm::ToolOutputFile(filename, error, OpenFlags);
if (error)
{
std::cerr << error << '\n';
delete FDOut;
return 0;
}
return FDOut;
}
static void CreateObject(llvm::Module* module, const std::string& objname)
{
TIME_TRACE();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
std::string error;
llvm::Triple triple = llvm::Triple(module->getTargetTriple());
const llvm::Target* target = llvm::TargetRegistry::lookupTarget(triple, error);
if (!target)
{
std::cerr << "Error, could not find target: " << error << std::endl;
return;
}
std::string mcpu = llvm::codegen::getMCPU();
if (mcpu == "native")
{
mcpu = llvm::sys::getHostCPUName().str();
}
llvm::TargetOptions options;
std::string FeaturesStr = GetFeatureString();
std::unique_ptr<llvm::TargetMachine> tm(
target->createTargetMachine(triple, mcpu, FeaturesStr, options, llvm::Reloc::PIC_));
if (!tm)
{
std::cerr << "Error: Could not create targetmachine." << std::endl;
return;
}
llvm::legacy::PassManager PM;
llvm::TargetLibraryInfoWrapperPass* TLI = new llvm::TargetLibraryInfoWrapperPass(triple);
PM.add(TLI);
std::unique_ptr<llvm::ToolOutputFile> Out(GetOutputStream(objname));
if (!Out)
{
std::cerr << "Could not open file ... " << std::endl;
return;
}
llvm::raw_pwrite_stream* OS = &Out->os();
if (tm->addPassesToEmitFile(PM, *OS, nullptr, llvm::CodeGenFileType::ObjectFile, false))
{
std::cerr << objname
<< ": target does not support generation of this"
" file type!\n";
return;
}
PM.run(*module);
Out->keep();
}
std::string replace_ext(const std::string& origName, const std::string& expectedExt,
const std::string& newExt)
{
if (origName.substr(origName.size() - expectedExt.size()) != expectedExt)
{
std::cerr << "Could not find extension..." << std::endl;
exit(1);
}
return origName.substr(0, origName.size() - expectedExt.size()) + newExt;
}
bool CreateBinary(llvm::Module* module, const std::string& filename, EmitType emit)
{
TIME_TRACE();
if (emit == Exe)
{
std::string objname = replace_ext(filename, ".pas", ".o");
std::string exename = replace_ext(filename, ".pas", "");
std::string modelStr;
// Order matters here: clang, being gcc-compatible, will have __GNUC__ defined.
#ifdef __clang__
std::string compiler = "clang";
#elif defined(__GNUC__)
std::string compiler = "gcc";
#endif
if (model == m32)
{
modelStr = "-m32";
}
CreateObject(module, objname);
std::string verboseflags;
if (verbosity)
{
verboseflags = " -v";
}
std::string debugFlag;
if (debugInfo)
{
debugFlag = " -g";
}
std::string cmd = compiler + " " + modelStr + verboseflags + " " + objname + " -L\"" + libpath +
"\" -lruntime" + modelStr + debugFlag + " -lm -o " + exename;
if (verbosity)
{
std::cerr << "Executing final link command: " << cmd << std::endl;
}
int res = system(cmd.c_str());
if (res != 0)
{
std::cerr << "Error: " << res << std::endl;
return false;
}
return true;
}
ICE_IF(emit != LlvmIr, "Expect LLVM IR here..");
std::string irName = replace_ext(filename, ".pas", ".ll");
std::unique_ptr<llvm::ToolOutputFile> Out(GetOutputStream(irName));
llvm::formatted_raw_ostream FOS(Out->os());
module->print(FOS, 0);
Out->keep();
return true;
}
llvm::Module* CreateModule()
{
llvm::InitializeNativeTarget();
llvm::Module* module = new llvm::Module("TheModule", theContext);
llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
if (model == m32)
{
triple = triple.get32BitArchVariant();
}
else
{
triple = triple.get64BitArchVariant();
}
module->setTargetTriple(triple);
std::string error;
const llvm::Target* target = llvm::TargetRegistry::lookupTarget(triple, error);
if (!target)
{
std::cerr << "Error, could not find target: " << error << std::endl;
return 0;
}
std::string FeaturesStr = GetFeatureString();
llvm::TargetOptions options;
std::string mcpu = llvm::codegen::getMCPU();
std::unique_ptr<llvm::TargetMachine> tm(
target->createTargetMachine(triple, mcpu, FeaturesStr, options, llvm::Reloc::Static));
ICE_IF(!tm, "Could not create TargetMachine");
const llvm::DataLayout dl = tm->createDataLayout();
module->setDataLayout(dl);
return module;
}