Skip to content

Commit ead138a

Browse files
committed
Create LLVMTargetMachineC.cpp to implement the MC-emission related things in the C-API + linked any libraries that depends on MC file emission to CodeGen.
1 parent 9ba7913 commit ead138a

File tree

5 files changed

+105
-65
lines changed

5 files changed

+105
-65
lines changed

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ add_llvm_component_library(LLVMCodeGen
100100
LiveStacks.cpp
101101
LiveVariables.cpp
102102
LLVMTargetMachine.cpp
103+
LLVMTargetMachineC.cpp
103104
LocalStackSlotAllocation.cpp
104105
LoopTraversal.cpp
105106
LowLevelTypeUtils.cpp
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===-- LLVMTargetMachineC.cpp --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the LLVM-C part of TargetMachine.h that directly
10+
// depends on the CodeGen library.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "llvm-c/Core.h"
15+
#include "llvm-c/TargetMachine.h"
16+
#include "llvm/CodeGen/MachineModuleInfo.h"
17+
#include "llvm/IR/LegacyPassManager.h"
18+
#include "llvm/IR/Module.h"
19+
#include "llvm/Support/FileSystem.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
#include "llvm/Target/TargetMachine.h"
22+
23+
using namespace llvm;
24+
25+
static TargetMachine *unwrap(LLVMTargetMachineRef P) {
26+
return reinterpret_cast<TargetMachine *>(P);
27+
}
28+
29+
static Target *unwrap(LLVMTargetRef P) { return reinterpret_cast<Target *>(P); }
30+
31+
static LLVMTargetMachineRef wrap(const TargetMachine *P) {
32+
return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
33+
}
34+
35+
static LLVMTargetRef wrap(const Target *P) {
36+
return reinterpret_cast<LLVMTargetRef>(const_cast<Target *>(P));
37+
}
38+
39+
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
40+
raw_pwrite_stream &OS,
41+
LLVMCodeGenFileType codegen,
42+
char **ErrorMessage) {
43+
TargetMachine *TM = unwrap(T);
44+
Module *Mod = unwrap(M);
45+
46+
legacy::PassManager pass;
47+
MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(TM));
48+
49+
std::string error;
50+
51+
Mod->setDataLayout(TM->createDataLayout());
52+
53+
CodeGenFileType ft;
54+
switch (codegen) {
55+
case LLVMAssemblyFile:
56+
ft = CodeGenFileType::AssemblyFile;
57+
break;
58+
default:
59+
ft = CodeGenFileType::ObjectFile;
60+
break;
61+
}
62+
if (TM->addPassesToEmitFile(pass, MMI, OS, nullptr, ft)) {
63+
error = "TargetMachine can't emit a file of this type";
64+
*ErrorMessage = strdup(error.c_str());
65+
return true;
66+
}
67+
68+
pass.run(*Mod);
69+
70+
OS.flush();
71+
return false;
72+
}
73+
74+
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
75+
const char *Filename,
76+
LLVMCodeGenFileType codegen,
77+
char **ErrorMessage) {
78+
std::error_code EC;
79+
raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
80+
if (EC) {
81+
*ErrorMessage = strdup(EC.message().c_str());
82+
return true;
83+
}
84+
bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
85+
dest.flush();
86+
return Result;
87+
}
88+
89+
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
90+
LLVMModuleRef M,
91+
LLVMCodeGenFileType codegen,
92+
char **ErrorMessage,
93+
LLVMMemoryBufferRef *OutMemBuf) {
94+
SmallString<0> CodeString;
95+
raw_svector_ostream OStream(CodeString);
96+
bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
97+
98+
StringRef Data = OStream.str();
99+
*OutMemBuf =
100+
LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
101+
return Result;
102+
}

llvm/lib/ExecutionEngine/MCJIT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_llvm_component_library(LLVMMCJIT
55
intrinsics_gen
66

77
LINK_COMPONENTS
8+
CodeGen
89
Core
910
ExecutionEngine
1011
Object

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ add_llvm_component_library(LLVMOrcJIT
6363

6464
LINK_COMPONENTS
6565
BinaryFormat
66+
CodeGen
6667
Core
6768
ExecutionEngine
6869
JITLink

llvm/lib/Target/TargetMachineC.cpp

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
#include "llvm-c/Core.h"
1414
#include "llvm-c/TargetMachine.h"
1515
#include "llvm/Analysis/TargetTransformInfo.h"
16-
#include "llvm/CodeGen/MachineModuleInfo.h"
1716
#include "llvm/IR/DataLayout.h"
1817
#include "llvm/IR/LegacyPassManager.h"
1918
#include "llvm/IR/Module.h"
2019
#include "llvm/MC/TargetRegistry.h"
2120
#include "llvm/Support/CBindingWrapping.h"
22-
#include "llvm/Support/FileSystem.h"
2321
#include "llvm/Support/raw_ostream.h"
2422
#include "llvm/Target/CodeGenCWrappers.h"
2523
#include "llvm/Target/TargetMachine.h"
@@ -288,69 +286,6 @@ LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
288286
return wrap(new DataLayout(unwrap(T)->createDataLayout()));
289287
}
290288

291-
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
292-
raw_pwrite_stream &OS,
293-
LLVMCodeGenFileType codegen,
294-
char **ErrorMessage) {
295-
TargetMachine* TM = unwrap(T);
296-
Module* Mod = unwrap(M);
297-
298-
legacy::PassManager pass;
299-
MachineModuleInfo MMI(static_cast<LLVMTargetMachine *>(TM));
300-
301-
std::string error;
302-
303-
Mod->setDataLayout(TM->createDataLayout());
304-
305-
CodeGenFileType ft;
306-
switch (codegen) {
307-
case LLVMAssemblyFile:
308-
ft = CodeGenFileType::AssemblyFile;
309-
break;
310-
default:
311-
ft = CodeGenFileType::ObjectFile;
312-
break;
313-
}
314-
if (TM->addPassesToEmitFile(pass, MMI, OS, nullptr, ft)) {
315-
error = "TargetMachine can't emit a file of this type";
316-
*ErrorMessage = strdup(error.c_str());
317-
return true;
318-
}
319-
320-
pass.run(*Mod);
321-
322-
OS.flush();
323-
return false;
324-
}
325-
326-
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
327-
const char *Filename,
328-
LLVMCodeGenFileType codegen,
329-
char **ErrorMessage) {
330-
std::error_code EC;
331-
raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
332-
if (EC) {
333-
*ErrorMessage = strdup(EC.message().c_str());
334-
return true;
335-
}
336-
bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
337-
dest.flush();
338-
return Result;
339-
}
340-
341-
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
342-
LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
343-
LLVMMemoryBufferRef *OutMemBuf) {
344-
SmallString<0> CodeString;
345-
raw_svector_ostream OStream(CodeString);
346-
bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
347-
348-
StringRef Data = OStream.str();
349-
*OutMemBuf =
350-
LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
351-
return Result;
352-
}
353-
354289
char *LLVMGetDefaultTargetTriple(void) {
355290
return strdup(sys::getDefaultTargetTriple().c_str());
356291
}

0 commit comments

Comments
 (0)