2012-04-11 12:25:24 +02: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 17:50:05 +01:00
|
|
|
#include "llvm-c/TargetMachine.h"
|
2012-04-11 12:25:24 +02:00
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/Target.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-04-11 12:25:24 +02:00
|
|
|
#include "llvm/PassManager.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-04-11 12:25:24 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-23 00:47:22 +02:00
|
|
|
inline DataLayout *unwrap(LLVMTargetDataRef P) {
|
|
|
|
return reinterpret_cast<DataLayout*>(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMTargetDataRef wrap(const DataLayout *P) {
|
|
|
|
return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
|
|
|
|
}
|
2012-04-11 12:25:24 +02:00
|
|
|
|
2013-04-23 00:47:22 +02:00
|
|
|
inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
|
|
|
|
return reinterpret_cast<TargetLibraryInfo*>(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
|
|
|
|
TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
|
|
|
|
return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 12:25:24 +02:00
|
|
|
|
|
|
|
LLVMTargetRef LLVMGetFirstTarget() {
|
2013-10-17 12:25:24 +02:00
|
|
|
if(TargetRegistry::begin() == TargetRegistry::end()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Target* target = &*TargetRegistry::begin();
|
|
|
|
return wrap(target);
|
2012-04-11 12:25:24 +02:00
|
|
|
}
|
|
|
|
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
|
|
|
|
return wrap(unwrap(T)->getNext());
|
|
|
|
}
|
|
|
|
|
2013-11-15 03:51:01 +01:00
|
|
|
LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
|
2013-11-15 03:51:18 +01:00
|
|
|
StringRef NameRef = Name;
|
2013-11-06 11:25:18 +01:00
|
|
|
for (TargetRegistry::iterator IT = TargetRegistry::begin(),
|
|
|
|
IE = TargetRegistry::end(); IT != IE; ++IT) {
|
2013-11-15 03:51:18 +01:00
|
|
|
if (IT->getName() == NameRef)
|
2013-11-15 03:51:01 +01:00
|
|
|
return wrap(&*IT);
|
2013-11-06 11:25:18 +01:00
|
|
|
}
|
|
|
|
|
2013-11-15 03:51:01 +01:00
|
|
|
return NULL;
|
2013-11-06 11:25:18 +01: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 12:25:24 +02: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 03:51:12 +01:00
|
|
|
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
|
|
|
|
const char* Triple, const char* CPU, const char* Features,
|
|
|
|
LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
|
|
|
|
LLVMCodeModel CodeModel) {
|
2012-04-11 12:25:24 +02: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-02 00:58:00 +02:00
|
|
|
CodeModel::Model CM = unwrap(CodeModel);
|
2012-04-11 12:25:24 +02:00
|
|
|
|
2013-05-02 00:58:00 +02:00
|
|
|
CodeGenOpt::Level OL;
|
2012-04-11 12:25:24 +02: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 18:38:25 +02:00
|
|
|
return wrap(unwrap(T)->getDataLayout());
|
2012-04-11 12:25:24 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 11:25:18 +01:00
|
|
|
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
|
|
|
|
LLVMBool VerboseAsm) {
|
|
|
|
unwrap(T)->setAsmVerbosityDefault(VerboseAsm);
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:12:56 +02:00
|
|
|
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
|
|
|
|
formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
|
2012-04-11 12:25:24 +02:00
|
|
|
TargetMachine* TM = unwrap(T);
|
|
|
|
Module* Mod = unwrap(M);
|
|
|
|
|
|
|
|
PassManager pass;
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
|
2012-10-08 18:38:25 +02:00
|
|
|
const DataLayout* td = TM->getDataLayout();
|
2012-04-11 12:25:24 +02:00
|
|
|
|
|
|
|
if (!td) {
|
2012-10-08 18:38:25 +02:00
|
|
|
error = "No DataLayout in TargetMachine";
|
2012-04-11 12:25:24 +02:00
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-26 00:25:17 +01:00
|
|
|
Mod->setDataLayout(td);
|
|
|
|
pass.add(new DataLayoutPass(Mod));
|
2012-04-11 12:25:24 +02:00
|
|
|
|
|
|
|
TargetMachine::CodeGenFileType ft;
|
|
|
|
switch (codegen) {
|
|
|
|
case LLVMAssemblyFile:
|
|
|
|
ft = TargetMachine::CGFT_AssemblyFile;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ft = TargetMachine::CGFT_ObjectFile;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-17 01:12:56 +02:00
|
|
|
if (TM->addPassesToEmitFile(pass, OS, ft)) {
|
2013-03-10 23:01:44 +01:00
|
|
|
error = "TargetMachine can't emit a file of this type";
|
2012-04-11 12:25:24 +02:00
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pass.run(*Mod);
|
|
|
|
|
2013-04-17 01:12:56 +02:00
|
|
|
OS.flush();
|
2012-04-11 12:25:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-17 01:12:56 +02:00
|
|
|
|
|
|
|
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
|
|
|
|
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
|
|
|
|
std::string error;
|
2014-02-24 19:20:12 +01:00
|
|
|
raw_fd_ostream dest(Filename, error, sys::fs::F_None);
|
2013-04-17 01:12:56 +02:00
|
|
|
if (!error.empty()) {
|
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-17 12:39:35 +02:00
|
|
|
formatted_raw_ostream destf(dest);
|
2013-04-17 01:12:56 +02: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) {
|
|
|
|
std::string CodeString;
|
|
|
|
raw_string_ostream OStream(CodeString);
|
|
|
|
formatted_raw_ostream Out(OStream);
|
|
|
|
bool Result = LLVMTargetMachineEmit(T, M, Out, codegen, ErrorMessage);
|
|
|
|
OStream.flush();
|
|
|
|
|
|
|
|
std::string &Data = OStream.str();
|
|
|
|
*OutMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.c_str(),
|
|
|
|
Data.length(), "");
|
|
|
|
return Result;
|
|
|
|
}
|
2013-11-06 11:25:18 +01:00
|
|
|
|
|
|
|
char *LLVMGetDefaultTargetTriple(void) {
|
|
|
|
return strdup(sys::getDefaultTargetTriple().c_str());
|
|
|
|
}
|
2014-01-23 20:23:28 +01:00
|
|
|
|
|
|
|
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
|
|
|
|
unwrap(T)->addAnalysisPasses(*unwrap(PM));
|
|
|
|
}
|