-
Notifications
You must be signed in to change notification settings - Fork 2
/
cxx.cxx
96 lines (87 loc) · 3.14 KB
/
cxx.cxx
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
#include <llvm-c/TargetMachine.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Target/TargetMachine.h>
#include "cxx.h"
static llvm::Optional<llvm::Reloc::Model>
convert(LLVMRelocMode reloc_mode)
{
switch (reloc_mode) {
case LLVMRelocDefault:
return llvm::None;
case LLVMRelocStatic:
return llvm::Reloc::Static;
case LLVMRelocPIC:
return llvm::Reloc::PIC_;
case LLVMRelocDynamicNoPic:
return llvm::Reloc::DynamicNoPIC;
case LLVMRelocROPI:
return llvm::Reloc::ROPI;
case LLVMRelocRWPI:
return llvm::Reloc::RWPI;
case LLVMRelocROPI_RWPI:
return llvm::Reloc::ROPI_RWPI;
}
}
static llvm::CodeGenOpt::Level
convert(LLVMCodeGenOptLevel opt_level)
{
switch (opt_level) {
case LLVMCodeGenLevelNone:
return llvm::CodeGenOpt::None;
case LLVMCodeGenLevelLess:
return llvm::CodeGenOpt::Less;
case LLVMCodeGenLevelDefault:
return llvm::CodeGenOpt::Default;
case LLVMCodeGenLevelAggressive:
return llvm::CodeGenOpt::Aggressive;
}
}
static llvm::Optional<llvm::CodeModel::Model>
convert(LLVMCodeModel code_model, bool *jit)
{
*jit = false;
switch (code_model) {
case LLVMCodeModelDefault:
return llvm::None;
case LLVMCodeModelJITDefault:
*jit = true;
return llvm::None;
case LLVMCodeModelTiny:
return llvm::CodeModel::Tiny;
case LLVMCodeModelSmall:
return llvm::CodeModel::Small;
case LLVMCodeModelKernel:
return llvm::CodeModel::Kernel;
case LLVMCodeModelMedium:
return llvm::CodeModel::Medium;
case LLVMCodeModelLarge:
return llvm::CodeModel::Large;
}
}
LLVMTargetMachineRef
LLVMCreateTargetMachineWithOpts(LLVMTargetRef ctarget, const char *triple,
const char *cpu, const char *features,
LLVMCodeGenOptLevel opt_level,
LLVMRelocMode reloc_mode,
LLVMCodeModel code_model,
bool EmitStackSizeSection,
const char *StackUsageOutput)
{
llvm::TargetOptions opts;
// -fstack-size-section equiv
// emit it to ".stack_sizes" section in case of ELF
// you can read it with "llvm-readobj --stack-sizes"
opts.EmitStackSizeSection = EmitStackSizeSection;
// -fstack-usage equiv
if (StackUsageOutput != NULL) {
opts.StackUsageOutput = StackUsageOutput;
}
auto target = reinterpret_cast<llvm::Target *>(ctarget);
auto rm = convert(reloc_mode);
auto ol = convert(opt_level);
bool jit;
auto cm = convert(code_model, &jit);
auto targetmachine = target->createTargetMachine(
triple, cpu, features, opts, rm, cm, ol, jit);
return reinterpret_cast<LLVMTargetMachineRef>(targetmachine);
}