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