-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjgen.cpp
More file actions
34 lines (27 loc) · 1.21 KB
/
objgen.cpp
File metadata and controls
34 lines (27 loc) · 1.21 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
#include "objgen.h"
int GenerateObject(CodegenContext &context, std::string objname) {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetAsmPrinter();
auto TargetTriple = llvm::sys::getDefaultTargetTriple();
context.module->setTargetTriple(TargetTriple);
auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, errmsg);
EXCEPT_RET(Target != nullptr, -1);
auto CPU = "generic";
auto Features = "";
llvm::TargetOptions opt;
auto RM = llvm::Optional<llvm::Reloc::Model>();
auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
context.module->setDataLayout(TargetMachine->createDataLayout());
context.module->setTargetTriple(TargetTriple);
std::error_code EC;
llvm::raw_fd_ostream dest(objname, EC, llvm::sys::fs::OF_None);
EXCEPT_MSG_RET(!EC, "Can not open file: " + EC.message(), -1);
llvm::legacy::PassManager pass;
auto FileType = llvm::CGFT_ObjectFile;
EXCEPT_MSG_RET(!TargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType),
"TargetMachine can't emit a file of this type", -1);
pass.run(*context.module);
dest.flush();
return 0;
}