2012-04-11 10:25:24 +00:00
|
|
|
//===-- TargetMachine.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM-C part of TargetMachine.h
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm-c/TargetMachine.h"
|
2012-04-11 10:25:24 +00:00
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/Target.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-04-11 10:25:24 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/CodeGen.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-04-11 10:25:24 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-22 22:47:22 +00:00
|
|
|
inline TargetMachine *unwrap(LLVMTargetMachineRef P) {
|
|
|
|
return reinterpret_cast<TargetMachine*>(P);
|
|
|
|
}
|
|
|
|
inline Target *unwrap(LLVMTargetRef P) {
|
|
|
|
return reinterpret_cast<Target*>(P);
|
|
|
|
}
|
|
|
|
inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
|
|
|
|
return
|
|
|
|
reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
|
|
|
|
}
|
|
|
|
inline LLVMTargetRef wrap(const Target * P) {
|
|
|
|
return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
|
|
|
|
}
|
2012-04-11 10:25:24 +00:00
|
|
|
|
|
|
|
LLVMTargetRef LLVMGetFirstTarget() {
|
2013-10-17 10:25:24 +00:00
|
|
|
if(TargetRegistry::begin() == TargetRegistry::end()) {
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2013-10-17 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Target* target = &*TargetRegistry::begin();
|
|
|
|
return wrap(target);
|
2012-04-11 10:25:24 +00:00
|
|
|
}
|
|
|
|
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
|
|
|
|
return wrap(unwrap(T)->getNext());
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:51:01 +00:00
|
|
|
LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
|
2013-11-15 02:51:18 +00:00
|
|
|
StringRef NameRef = Name;
|
2013-11-06 10:25:18 +00:00
|
|
|
for (TargetRegistry::iterator IT = TargetRegistry::begin(),
|
|
|
|
IE = TargetRegistry::end(); IT != IE; ++IT) {
|
2013-11-15 02:51:18 +00:00
|
|
|
if (IT->getName() == NameRef)
|
2013-11-15 02:51:01 +00:00
|
|
|
return wrap(&*IT);
|
2013-11-06 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2013-11-06 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
|
|
|
|
char **ErrorMessage) {
|
|
|
|
std::string Error;
|
|
|
|
|
|
|
|
*T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
|
|
|
|
|
|
|
|
if (!*T) {
|
|
|
|
if (ErrorMessage)
|
|
|
|
*ErrorMessage = strdup(Error.c_str());
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-11 10:25:24 +00:00
|
|
|
const char * LLVMGetTargetName(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * LLVMGetTargetDescription(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->getShortDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasJIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasTargetMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasMCAsmBackend();
|
|
|
|
}
|
|
|
|
|
2013-11-15 02:51:12 +00:00
|
|
|
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
|
|
|
|
const char* Triple, const char* CPU, const char* Features,
|
|
|
|
LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
|
|
|
|
LLVMCodeModel CodeModel) {
|
2012-04-11 10:25:24 +00:00
|
|
|
Reloc::Model RM;
|
|
|
|
switch (Reloc){
|
|
|
|
case LLVMRelocStatic:
|
|
|
|
RM = Reloc::Static;
|
|
|
|
break;
|
|
|
|
case LLVMRelocPIC:
|
|
|
|
RM = Reloc::PIC_;
|
|
|
|
break;
|
|
|
|
case LLVMRelocDynamicNoPic:
|
|
|
|
RM = Reloc::DynamicNoPIC;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
RM = Reloc::Default;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-01 22:58:00 +00:00
|
|
|
CodeModel::Model CM = unwrap(CodeModel);
|
2012-04-11 10:25:24 +00:00
|
|
|
|
2013-05-01 22:58:00 +00:00
|
|
|
CodeGenOpt::Level OL;
|
2012-04-11 10:25:24 +00:00
|
|
|
switch (Level) {
|
|
|
|
case LLVMCodeGenLevelNone:
|
|
|
|
OL = CodeGenOpt::None;
|
|
|
|
break;
|
|
|
|
case LLVMCodeGenLevelLess:
|
|
|
|
OL = CodeGenOpt::Less;
|
|
|
|
break;
|
|
|
|
case LLVMCodeGenLevelAggressive:
|
|
|
|
OL = CodeGenOpt::Aggressive;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OL = CodeGenOpt::Default;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetOptions opt;
|
|
|
|
return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM,
|
|
|
|
CM, OL));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
|
|
|
|
delete unwrap(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
|
|
|
|
const Target* target = &(unwrap(T)->getTarget());
|
|
|
|
return wrap(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
|
|
|
|
std::string StringRep = unwrap(T)->getTargetTriple();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
|
|
|
|
std::string StringRep = unwrap(T)->getTargetCPU();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
|
|
|
|
std::string StringRep = unwrap(T)->getTargetFeatureString();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
|
2012-10-08 16:38:25 +00:00
|
|
|
return wrap(unwrap(T)->getDataLayout());
|
2012-04-11 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 10:25:18 +00:00
|
|
|
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
|
|
|
|
LLVMBool VerboseAsm) {
|
|
|
|
unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:12:56 +00:00
|
|
|
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
|
|
|
|
formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
|
2012-04-11 10:25:24 +00:00
|
|
|
TargetMachine* TM = unwrap(T);
|
|
|
|
Module* Mod = unwrap(M);
|
|
|
|
|
|
|
|
PassManager pass;
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout* td = TM->getDataLayout();
|
2012-04-11 10:25:24 +00:00
|
|
|
|
|
|
|
if (!td) {
|
2012-10-08 16:38:25 +00:00
|
|
|
error = "No DataLayout in TargetMachine";
|
2012-04-11 10:25:24 +00:00
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-25 23:25:17 +00:00
|
|
|
Mod->setDataLayout(td);
|
|
|
|
pass.add(new DataLayoutPass(Mod));
|
2012-04-11 10:25:24 +00:00
|
|
|
|
|
|
|
TargetMachine::CodeGenFileType ft;
|
|
|
|
switch (codegen) {
|
|
|
|
case LLVMAssemblyFile:
|
|
|
|
ft = TargetMachine::CGFT_AssemblyFile;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ft = TargetMachine::CGFT_ObjectFile;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-16 23:12:56 +00:00
|
|
|
if (TM->addPassesToEmitFile(pass, OS, ft)) {
|
2013-03-10 22:01:44 +00:00
|
|
|
error = "TargetMachine can't emit a file of this type";
|
2012-04-11 10:25:24 +00:00
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pass.run(*Mod);
|
|
|
|
|
2013-04-16 23:12:56 +00:00
|
|
|
OS.flush();
|
2012-04-11 10:25:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-16 23:12:56 +00:00
|
|
|
|
|
|
|
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
|
|
|
|
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
|
|
|
|
std::string error;
|
2014-02-24 18:20:12 +00:00
|
|
|
raw_fd_ostream dest(Filename, error, sys::fs::F_None);
|
2013-04-16 23:12:56 +00:00
|
|
|
if (!error.empty()) {
|
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-17 10:39:35 +00:00
|
|
|
formatted_raw_ostream destf(dest);
|
2013-04-16 23:12:56 +00:00
|
|
|
bool Result = LLVMTargetMachineEmit(T, M, destf, codegen, ErrorMessage);
|
|
|
|
dest.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
|
|
|
|
LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
|
|
|
|
LLVMMemoryBufferRef *OutMemBuf) {
|
2014-06-26 00:00:48 +00:00
|
|
|
string_ostream Code;
|
|
|
|
formatted_raw_ostream Out(Code);
|
2013-04-16 23:12:56 +00:00
|
|
|
bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
|
|
|
|
|
2014-06-26 00:00:48 +00:00
|
|
|
StringRef Buffer = Code.str();
|
|
|
|
*OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Buffer.data(),
|
|
|
|
Buffer.size(), "");
|
2013-04-16 23:12:56 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2013-11-06 10:25:18 +00:00
|
|
|
|
|
|
|
char *LLVMGetDefaultTargetTriple(void) {
|
|
|
|
return strdup(sys::getDefaultTargetTriple().c_str());
|
|
|
|
}
|
2014-01-23 19:23:28 +00:00
|
|
|
|
|
|
|
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
|
|
|
|
unwrap(T)->addAnalysisPasses(*unwrap(PM));
|
|
|
|
}
|