C API: Add LLVMTargetMachineEmitToMemoryBuffer()

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179648 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Tom Stellard 2013-04-16 23:12:56 +00:00
parent edc93b356d
commit ad74f335ef
2 changed files with 36 additions and 12 deletions

View File

@ -114,6 +114,9 @@ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T);
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage); char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage);
/** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);

View File

@ -149,8 +149,8 @@ LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
return wrap(unwrap(T)->getDataLayout()); return wrap(unwrap(T)->getDataLayout());
} }
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { formatted_raw_ostream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage) {
TargetMachine* TM = unwrap(T); TargetMachine* TM = unwrap(T);
Module* Mod = unwrap(M); Module* Mod = unwrap(M);
@ -176,14 +176,7 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
ft = TargetMachine::CGFT_ObjectFile; ft = TargetMachine::CGFT_ObjectFile;
break; break;
} }
raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary); if (TM->addPassesToEmitFile(pass, OS, ft)) {
formatted_raw_ostream destf(dest);
if (!error.empty()) {
*ErrorMessage = strdup(error.c_str());
return true;
}
if (TM->addPassesToEmitFile(pass, destf, ft)) {
error = "TargetMachine can't emit a file of this type"; error = "TargetMachine can't emit a file of this type";
*ErrorMessage = strdup(error.c_str()); *ErrorMessage = strdup(error.c_str());
return true; return true;
@ -191,7 +184,35 @@ LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
pass.run(*Mod); pass.run(*Mod);
destf.flush(); OS.flush();
dest.flush();
return false; return false;
} }
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
std::string error;
raw_fd_ostream dest(Filename, error, raw_fd_ostream::F_Binary);
formatted_raw_ostream destf(dest);
if (!error.empty()) {
*ErrorMessage = strdup(error.c_str());
return true;
}
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;
}