blob: fd6c16b563f7f63cbed9e9ca1ef12427c1d2f44f [file] [log] [blame]
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +00001//=- RemoteTargetExternal.inc - LLVM out-of-process JIT execution for Unix --=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of the Unix-specific parts of the RemoteTargetExternal class
11// which executes JITed code in a separate process from where it was built.
12//
13//===----------------------------------------------------------------------===//
14
15#include <unistd.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <sys/wait.h>
19
20namespace {
21
22struct ConnectionData_t {
23 int InputPipe;
24 int OutputPipe;
25
26 ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {}
27};
28
29} // namespace
30
31namespace llvm {
32
33void RemoteTargetExternal::create() {
34 int PipeFD[2][2];
35 pid_t ChildPID;
36
37 pipe(PipeFD[0]);
38 pipe(PipeFD[1]);
39
40 ChildPID = fork();
41
42 if (ChildPID == 0) {
43 // In the child...
44
45 // Close the parent ends of the pipes
46 close(PipeFD[0][1]);
47 close(PipeFD[1][0]);
48
49 // Use our pipes as stdin and stdout
50 if (PipeFD[0][0] != STDIN_FILENO) {
51 dup2(PipeFD[0][0], STDIN_FILENO);
52 close(PipeFD[0][0]);
53 }
54 if (PipeFD[1][1] != STDOUT_FILENO) {
55 dup2(PipeFD[1][1], STDOUT_FILENO);
56 close(PipeFD[1][1]);
57 }
58
59 // Execute the child process.
60 char *args[1] = { NULL };
61 int rc = execv(ChildName.c_str(), args);
62 if (rc != 0)
63 perror("Error executing child process: ");
64 }
65 else {
66 // In the parent...
67
68 // Close the child ends of the pipes
69 close(PipeFD[0][0]);
70 close(PipeFD[1][1]);
71
72 // Store the parent ends of the pipes
73 ConnectionData = (void*)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
74
75 Receive(LLI_ChildActive);
76 }
77}
78
79int RemoteTargetExternal::WriteBytes(const void *Data, size_t Size) {
80 return write(((ConnectionData_t*)ConnectionData)->OutputPipe, Data, Size);
81}
82
83int RemoteTargetExternal::ReadBytes(void *Data, size_t Size) {
84 return read(((ConnectionData_t*)ConnectionData)->InputPipe, Data, Size);
85}
86
87void RemoteTargetExternal::Wait() {
88 wait(NULL);
89}
90
91} // namespace llvm