[ThinLTO] Add code comment. NFC
[llvm-complete.git] / examples / Kaleidoscope / BuildingAJIT / Chapter5 / Server / server.cpp
blobe50a7ecf96bcd7cb01be4c24a205b14166936e2d
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"
10 #include <cstdint>
11 #include <cstdio>
12 #include <cstring>
13 #include <string>
14 #include <netinet/in.h>
15 #include <sys/socket.h>
17 using namespace llvm;
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"),
23 cl::init(20000));
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));
34 extern "C"
35 void printExprResult(double Val) {
36 printf("Expression evaluated to: %f\n", Val);
39 // --- LAZY COMPILE TEST ---
40 int main(int argc, char* argv[]) {
41 if (argc == 0)
42 ExitOnErr.setBanner("jit_server: ");
43 else
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";
55 return 1;
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.
70 int yes = 1;
71 if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
72 errs() << "Error calling setsockopt.\n";
73 return 1;
77 if (bind(sockfd, reinterpret_cast<sockaddr*>(&servAddr),
78 sizeof(servAddr)) < 0) {
79 errs() << "Error on binding.\n";
80 return 1;
82 listen(sockfd, 1);
83 int newsockfd = accept(sockfd, reinterpret_cast<sockaddr*>(&clientAddr),
84 &clientAddrLen);
86 auto SymbolLookup =
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());
110 return 0;