1 #include "../RemoteJITUtils.h"
2 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
3 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h"
4 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
5 #include "llvm/Support/CommandLine.h"
6 #include "llvm/Support/DynamicLibrary.h"
7 #include "llvm/Support/Error.h"
8 #include "llvm/Support/raw_ostream.h"
9 #include "llvm/Support/TargetSelect.h"
14 #include <netinet/in.h>
15 #include <sys/socket.h>
18 using namespace llvm::orc
;
20 // Command line argument for TCP port.
21 cl::opt
<uint32_t> Port("port",
22 cl::desc("TCP port to listen on"),
25 ExitOnError ExitOnErr
;
27 using MainFun
= int (*)(int, const char*[]);
29 template <typename NativePtrT
>
30 NativePtrT
MakeNative(uint64_t P
) {
31 return reinterpret_cast<NativePtrT
>(static_cast<uintptr_t>(P
));
35 void printExprResult(double Val
) {
36 printf("Expression evaluated to: %f\n", Val
);
39 // --- LAZY COMPILE TEST ---
40 int main(int argc
, char* argv
[]) {
42 ExitOnErr
.setBanner("jit_server: ");
44 ExitOnErr
.setBanner(std::string(argv
[0]) + ": ");
46 // --- Initialize LLVM ---
47 cl::ParseCommandLineOptions(argc
, argv
, "LLVM lazy JIT example.\n");
49 InitializeNativeTarget();
50 InitializeNativeTargetAsmPrinter();
51 InitializeNativeTargetAsmParser();
53 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
54 errs() << "Error loading program symbols.\n";
58 // --- Initialize remote connection ---
60 int sockfd
= socket(PF_INET
, SOCK_STREAM
, 0);
61 sockaddr_in servAddr
, clientAddr
;
62 socklen_t clientAddrLen
= sizeof(clientAddr
);
63 memset(&servAddr
, 0, sizeof(servAddr
));
64 servAddr
.sin_family
= PF_INET
;
65 servAddr
.sin_family
= INADDR_ANY
;
66 servAddr
.sin_port
= htons(Port
);
69 // avoid "Address already in use" error.
71 if (setsockopt(sockfd
,SOL_SOCKET
,SO_REUSEADDR
,&yes
,sizeof(int)) == -1) {
72 errs() << "Error calling setsockopt.\n";
77 if (bind(sockfd
, reinterpret_cast<sockaddr
*>(&servAddr
),
78 sizeof(servAddr
)) < 0) {
79 errs() << "Error on binding.\n";
83 int newsockfd
= accept(sockfd
, reinterpret_cast<sockaddr
*>(&clientAddr
),
87 [](const std::string
&Name
) {
88 return RTDyldMemoryManager::getSymbolAddressInProcess(Name
);
91 auto RegisterEHFrames
=
92 [](uint8_t *Addr
, uint32_t Size
) {
93 RTDyldMemoryManager::registerEHFramesInProcess(Addr
, Size
);
96 auto DeregisterEHFrames
=
97 [](uint8_t *Addr
, uint32_t Size
) {
98 RTDyldMemoryManager::deregisterEHFramesInProcess(Addr
, Size
);
101 FDRPCChannel
TCPChannel(newsockfd
, newsockfd
);
103 using MyServerT
= remote::OrcRemoteTargetServer
<FDRPCChannel
, OrcX86_64_SysV
>;
105 MyServerT
Server(TCPChannel
, SymbolLookup
, RegisterEHFrames
, DeregisterEHFrames
);
107 while (!Server
.receivedTerminate())
108 ExitOnErr(Server
.handleOne());