2 // Copyright 2012-2016 Francisco Jerez
3 // Copyright 2012-2016 Advanced Micro Devices, Inc.
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 // OTHER DEALINGS IN THE SOFTWARE.
26 /// Generate code using an arbitrary LLVM back-end capable of emitting
27 /// executable code as an ELF object file.
30 #include "llvm/codegen.hpp"
31 #include "llvm/compat.hpp"
32 #include "llvm/util.hpp"
33 #include "core/error.hpp"
35 #include <llvm/Target/TargetMachine.h>
36 #include <llvm/Support/TargetRegistry.h>
37 #include <llvm/Transforms/Utils/Cloning.h>
42 using namespace clover
;
43 using namespace clover::llvm
;
44 using ::llvm::TargetMachine
;
48 std::unique_ptr
<Elf
, int (*)(Elf
*)>
49 get(const std::vector
<char> &code
) {
50 // One of the libelf implementations
51 // (http://www.mr511.de/software/english.htm) requires calling
52 // elf_version() before elf_memory().
53 elf_version(EV_CURRENT
);
54 return { elf_memory(const_cast<char *>(code
.data()), code
.size()),
59 get_symbol_table(Elf
*elf
) {
60 size_t section_str_index
;
61 elf_getshdrstrndx(elf
, §ion_str_index
);
63 for (Elf_Scn
*s
= elf_nextscn(elf
, NULL
); s
; s
= elf_nextscn(elf
, s
)) {
65 if (gelf_getshdr(s
, &header
) != &header
)
68 if (!std::strcmp(elf_strptr(elf
, section_str_index
, header
.sh_name
),
76 std::map
<std::string
, unsigned>
77 get_symbol_offsets(Elf
*elf
, Elf_Scn
*symtab
) {
78 Elf_Data
*const symtab_data
= elf_getdata(symtab
, NULL
);
80 if (gelf_getshdr(symtab
, &header
) != &header
)
83 std::map
<std::string
, unsigned> symbol_offsets
;
87 while (GElf_Sym
*s
= gelf_getsym(symtab_data
, i
++, &symbol
)) {
88 const char *name
= elf_strptr(elf
, header
.sh_link
, s
->st_name
);
89 symbol_offsets
[name
] = s
->st_value
;
92 return symbol_offsets
;
96 std::map
<std::string
, unsigned>
97 get_symbol_offsets(const std::vector
<char> &code
, std::string
&r_log
) {
98 const auto elf
= elf::get(code
);
99 const auto symtab
= elf::get_symbol_table(elf
.get());
101 fail(r_log
, build_error(), "Unable to find symbol table.");
103 return elf::get_symbol_offsets(elf
.get(), symtab
);
107 emit_code(::llvm::Module
&mod
, const target
&target
,
108 TargetMachine::CodeGenFileType ft
,
109 std::string
&r_log
) {
111 auto t
= ::llvm::TargetRegistry::lookupTarget(target
.triple
, err
);
113 fail(r_log
, build_error(), err
);
115 std::unique_ptr
<TargetMachine
> tm
{
116 t
->createTargetMachine(target
.triple
, target
.cpu
, "", {},
117 compat::default_reloc_model
,
118 ::llvm::CodeModel::Default
,
119 ::llvm::CodeGenOpt::Default
) };
121 fail(r_log
, build_error(),
122 "Could not create TargetMachine: " + target
.triple
);
124 ::llvm::SmallVector
<char, 1024> data
;
127 compat::pass_manager pm
;
128 ::llvm::raw_svector_ostream os
{ data
};
129 compat::raw_ostream_to_emit_file
fos(os
);
131 mod
.setDataLayout(compat::get_data_layout(*tm
));
132 tm
->Options
.MCOptions
.AsmVerbose
=
133 (ft
== TargetMachine::CGFT_AssemblyFile
);
135 if (tm
->addPassesToEmitFile(pm
, fos
, ft
))
136 fail(r_log
, build_error(), "TargetMachine can't emit this file");
141 return { data
.begin(), data
.end() };
146 clover::llvm::build_module_native(::llvm::Module
&mod
, const target
&target
,
147 const clang::CompilerInstance
&c
,
148 std::string
&r_log
) {
149 const auto code
= emit_code(mod
, target
,
150 TargetMachine::CGFT_ObjectFile
, r_log
);
151 return build_module_common(mod
, code
, get_symbol_offsets(code
, r_log
), c
);
155 clover::llvm::print_module_native(const ::llvm::Module
&mod
,
156 const target
&target
) {
159 std::unique_ptr
<::llvm::Module
> cmod
{ CloneModule(&mod
) };
160 return as_string(emit_code(*cmod
, target
,
161 TargetMachine::CGFT_AssemblyFile
, log
));
163 return "Couldn't output native disassembly: " + log
;