annotate tools/bugpoint/ToolRunner.cpp @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===-- ToolRunner.cpp ----------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 // This file implements the interfaces described in the ToolRunner.h file.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "ToolRunner.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
14 #include "llvm/Config/config.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "llvm/Support/CommandLine.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "llvm/Support/Debug.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Support/FileSystem.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "llvm/Support/FileUtilities.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/Support/Program.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/Support/raw_ostream.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include <fstream>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include <sstream>
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
23 #include <utility>
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
26 #define DEBUG_TYPE "toolrunner"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
27
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 namespace llvm {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
29 cl::opt<bool> SaveTemps("save-temps", cl::init(false),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
30 cl::desc("Save temporary files"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
34 cl::opt<std::string>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
35 RemoteClient("remote-client",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
36 cl::desc("Remote execution client (rsh/ssh)"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
38 cl::opt<std::string> RemoteHost("remote-host",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
39 cl::desc("Remote execution (rsh/ssh) host"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
41 cl::opt<std::string> RemotePort("remote-port",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
42 cl::desc("Remote execution (rsh/ssh) port"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
44 cl::opt<std::string> RemoteUser("remote-user",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
45 cl::desc("Remote execution (rsh/ssh) user id"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
47 cl::opt<std::string>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
48 RemoteExtra("remote-extra-options",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
49 cl::desc("Remote execution (rsh/ssh) extra options"));
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 /// RunProgramWithTimeout - This function provides an alternate interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 /// to the sys::Program::ExecuteAndWait interface.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 /// @see sys::Program::ExecuteAndWait
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
55 static int RunProgramWithTimeout(StringRef ProgramPath,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
56 ArrayRef<StringRef> Args, StringRef StdInFile,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
57 StringRef StdOutFile, StringRef StdErrFile,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
58 unsigned NumSeconds = 0,
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 unsigned MemoryLimit = 0,
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
60 std::string *ErrMsg = nullptr) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
61 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
62 return sys::ExecuteAndWait(ProgramPath, Args, None, Redirects, NumSeconds,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
63 MemoryLimit, ErrMsg);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 /// RunProgramRemotelyWithTimeout - This function runs the given program
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 /// Returns the remote program exit code or reports a remote client error if it
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 /// fails. Remote client is required to return 255 if it failed or program exit
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 /// code otherwise.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 /// @see sys::Program::ExecuteAndWait
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
72 static int RunProgramRemotelyWithTimeout(
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
73 StringRef RemoteClientPath, ArrayRef<StringRef> Args, StringRef StdInFile,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
74 StringRef StdOutFile, StringRef StdErrFile, unsigned NumSeconds = 0,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
75 unsigned MemoryLimit = 0) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
76 Optional<StringRef> Redirects[3] = {StdInFile, StdOutFile, StdErrFile};
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 // Run the program remotely with the remote client
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
79 int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, None, Redirects,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
80 NumSeconds, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 // Has the remote client fail?
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 if (255 == ReturnCode) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 std::ostringstream OS;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 OS << "\nError running remote client:\n ";
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
86 for (StringRef Arg : Args)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
87 OS << " " << Arg.str();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 OS << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 // The error message is in the output file, let's print it out from there.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 std::string StdOutFileName = StdOutFile.str();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 std::ifstream ErrorFile(StdOutFileName.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 if (ErrorFile) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 std::copy(std::istreambuf_iterator<char>(ErrorFile),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 std::istreambuf_iterator<char>(),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 std::ostreambuf_iterator<char>(OS));
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 ErrorFile.close();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 errs() << OS.str();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 return ReturnCode;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
106 static Error ProcessFailure(StringRef ProgPath, ArrayRef<StringRef> Args,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
107 unsigned Timeout = 0, unsigned MemoryLimit = 0) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 std::ostringstream OS;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 OS << "\nError running tool:\n ";
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
110 for (StringRef Arg : Args)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
111 OS << " " << Arg.str();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 OS << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 // Rerun the compiler, capturing any error messages to print them.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 SmallString<128> ErrorFilename;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
116 std::error_code EC = sys::fs::createTemporaryFile(
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
117 "bugpoint.program_error_messages", "", ErrorFilename);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 if (EC) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 errs() << "Error making unique filename: " << EC.message() << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 exit(1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
122
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 ErrorFilename.str(), Timeout, MemoryLimit);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 // FIXME: check return code ?
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
127 // Print out the error messages generated by CC if possible...
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 std::ifstream ErrorFile(ErrorFilename.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 if (ErrorFile) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 std::copy(std::istreambuf_iterator<char>(ErrorFile),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 std::istreambuf_iterator<char>(),
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 std::ostreambuf_iterator<char>(OS));
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 ErrorFile.close();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 sys::fs::remove(ErrorFilename.c_str());
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
137 return make_error<StringError>(OS.str(), inconvertibleErrorCode());
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 //===---------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 // LLI Implementation of AbstractIntepreter interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
144 class LLI : public AbstractInterpreter {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
145 std::string LLIPath; // The path to the LLI executable
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
146 std::vector<std::string> ToolArgs; // Args to pass to LLI
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
147 public:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
148 LLI(const std::string &Path, const std::vector<std::string> *Args)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 : LLIPath(Path) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
150 ToolArgs.clear();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
151 if (Args) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
152 ToolArgs = *Args;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
154 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
156 Expected<int> ExecuteProgram(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
157 const std::string &Bitcode, const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
158 const std::string &InputFile, const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
159 const std::vector<std::string> &CCArgs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
160 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
161 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
162 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
165 Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
166 const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
167 const std::string &InputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
168 const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
169 const std::vector<std::string> &CCArgs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
170 const std::vector<std::string> &SharedLibs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
171 unsigned Timeout, unsigned MemoryLimit) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
172 std::vector<StringRef> LLIArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 LLIArgs.push_back(LLIPath.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 LLIArgs.push_back("-force-interpreter=true");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
177 e = SharedLibs.end();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
178 i != e; ++i) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 LLIArgs.push_back("-load");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
180 LLIArgs.push_back(*i);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 // Add any extra LLI args.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
185 LLIArgs.push_back(ToolArgs[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
187 LLIArgs.push_back(Bitcode);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 // Add optional parameters to the running program from Argv
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
189 for (unsigned i = 0, e = Args.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
190 LLIArgs.push_back(Args[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
192 outs() << "<lli>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
193 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
194 LLVM_DEBUG(errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
195 for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
196 << " " << LLIArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
197 errs() << "\n";);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
198 return RunProgramWithTimeout(LLIPath, LLIArgs, InputFile, OutputFile,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
199 OutputFile, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
202 void AbstractInterpreter::anchor() {}
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
204 ErrorOr<std::string> llvm::FindProgramByName(const std::string &ExeName,
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 const char *Argv0,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 void *MainAddr) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 // Check the directory that the calling program is in. We can do
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 // this if ProgramPath contains at least one / character, indicating that it
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 // is a relative path to the executable itself.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 StringRef Result = sys::path::parent_path(Main);
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
212 if (ErrorOr<std::string> Path = sys::findProgramByName(ExeName, Result))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
213 return *Path;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
215 // Check the user PATH.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
216 return sys::findProgramByName(ExeName);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 // LLI create method - Try to find the LLI executable
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
220 AbstractInterpreter *
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
221 AbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
222 const std::vector<std::string> *ToolArgs) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
223 if (ErrorOr<std::string> LLIPath =
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
224 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createLLI)) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
225 Message = "Found lli: " + *LLIPath + "\n";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
226 return new LLI(*LLIPath, ToolArgs);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
227 } else {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
228 Message = LLIPath.getError().message() + "\n";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
229 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 //===---------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 // Custom compiler command implementation of AbstractIntepreter interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 // Allows using a custom command for compiling the bitcode, thus allows, for
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 // example, to compile a bitcode fragment without linking or executing, then
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 // using a custom wrapper script to check for compiler errors.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
240 class CustomCompiler : public AbstractInterpreter {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
241 std::string CompilerCommand;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
242 std::vector<std::string> CompilerArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
244 public:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
245 CustomCompiler(const std::string &CompilerCmd,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
246 std::vector<std::string> CompArgs)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
247 : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
248
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
249 Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
250 unsigned MemoryLimit = 0) override;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
252 Expected<int> ExecuteProgram(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
253 const std::string &Bitcode, const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
254 const std::string &InputFile, const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
255 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
256 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
257 unsigned Timeout = 0, unsigned MemoryLimit = 0) override {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
258 return make_error<StringError>(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
259 "Execution not supported with -compile-custom",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
260 inconvertibleErrorCode());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
261 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
262 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
265 Error CustomCompiler::compileProgram(const std::string &Bitcode,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
266 unsigned Timeout, unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
268 std::vector<StringRef> ProgramArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 ProgramArgs.push_back(CompilerCommand.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 ProgramArgs.push_back(CompilerArgs.at(i).c_str());
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
273 ProgramArgs.push_back(Bitcode);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 // Add optional parameters to the running program from Argv
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 ProgramArgs.push_back(CompilerArgs[i].c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
279 if (RunProgramWithTimeout(CompilerCommand, ProgramArgs, "", "", "", Timeout,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
280 MemoryLimit))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
281 return ProcessFailure(CompilerCommand, ProgramArgs, Timeout, MemoryLimit);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
282 return Error::success();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 //===---------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 // Custom execution command implementation of AbstractIntepreter interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 // Allows using a custom command for executing the bitcode, thus allows,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 // for example, to invoke a cross compiler for code generation followed by
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 // a simulator that executes the generated binary.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
292 class CustomExecutor : public AbstractInterpreter {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
293 std::string ExecutionCommand;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
294 std::vector<std::string> ExecutorArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
296 public:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
297 CustomExecutor(const std::string &ExecutionCmd,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
298 std::vector<std::string> ExecArgs)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
299 : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
300
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
301 Expected<int> ExecuteProgram(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
302 const std::string &Bitcode, const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
303 const std::string &InputFile, const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
304 const std::vector<std::string> &CCArgs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
305 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
306 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
307 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
310 Expected<int> CustomExecutor::ExecuteProgram(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
311 const std::string &Bitcode, const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
312 const std::string &InputFile, const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
313 const std::vector<std::string> &CCArgs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
314 const std::vector<std::string> &SharedLibs, unsigned Timeout,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
315 unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
317 std::vector<StringRef> ProgramArgs;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
318 ProgramArgs.push_back(ExecutionCommand);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
321 ProgramArgs.push_back(ExecutorArgs[i]);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
322 ProgramArgs.push_back(Bitcode);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 // Add optional parameters to the running program from Argv
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 for (unsigned i = 0, e = Args.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
326 ProgramArgs.push_back(Args[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
328 return RunProgramWithTimeout(ExecutionCommand, ProgramArgs, InputFile,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
329 OutputFile, OutputFile, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 // Tokenize the CommandLine to the command and the args to allow
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 // defining a full command line as the command instead of just the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 // executed program. We cannot just pass the whole string after the command
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
335 // as a single argument because then the program sees only a single
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 // command line argument (with spaces in it: "foo bar" instead
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 // of "foo" and "bar").
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 //
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
339 // Spaces are used as a delimiter; however repeated, leading, and trailing
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
340 // whitespace are ignored. Simple escaping is allowed via the '\'
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
341 // character, as seen below:
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
342 //
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
343 // Two consecutive '\' evaluate to a single '\'.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
344 // A space after a '\' evaluates to a space that is not interpreted as a
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
345 // delimiter.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
346 // Any other instances of the '\' character are removed.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
347 //
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
348 // Example:
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
349 // '\\' -> '\'
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
350 // '\ ' -> ' '
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
351 // 'exa\mple' -> 'example'
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
352 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
353 static void lexCommand(const char *Argv0, std::string &Message,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
354 const std::string &CommandLine, std::string &CmdPath,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
355 std::vector<std::string> &Args) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
357 std::string Token;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
358 std::string Command;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
359 bool FoundPath = false;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
361 // first argument is the PATH.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
362 // Skip repeated whitespace, leading whitespace and trailing whitespace.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
363 for (std::size_t Pos = 0u; Pos <= CommandLine.size(); ++Pos) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
364 if ('\\' == CommandLine[Pos]) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
365 if (Pos + 1 < CommandLine.size())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
366 Token.push_back(CommandLine[++Pos]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
368 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
369 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
370 if (' ' == CommandLine[Pos] || CommandLine.size() == Pos) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
371 if (Token.empty())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
372 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
373
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
374 if (!FoundPath) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
375 Command = Token;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
376 FoundPath = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
377 Token.clear();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
378 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
379 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
380
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
381 Args.push_back(Token);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
382 Token.clear();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
383 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
384 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
385 Token.push_back(CommandLine[Pos]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
387
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
388 auto Path = FindProgramByName(Command, Argv0, (void *)(intptr_t)&lexCommand);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
389 if (!Path) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
390 Message = std::string("Cannot find '") + Command +
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
391 "' in PATH: " + Path.getError().message() + "\n";
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 return;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 }
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
394 CmdPath = *Path;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 Message = "Found command in: " + CmdPath + "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 // Custom execution environment create method, takes the execution command
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 // as arguments
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
402 const char *Argv0, std::string &Message,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
403 const std::string &CompileCommandLine) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 std::string CmdPath;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 std::vector<std::string> Args;
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
407 lexCommand(Argv0, Message, CompileCommandLine, CmdPath, Args);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 if (CmdPath.empty())
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
409 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 return new CustomCompiler(CmdPath, Args);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 // Custom execution environment create method, takes the execution command
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 // as arguments
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
416 AbstractInterpreter *
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
417 AbstractInterpreter::createCustomExecutor(const char *Argv0,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
418 std::string &Message,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
419 const std::string &ExecCommandLine) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 std::string CmdPath;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 std::vector<std::string> Args;
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
423 lexCommand(Argv0, Message, ExecCommandLine, CmdPath, Args);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 if (CmdPath.empty())
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
425 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 return new CustomExecutor(CmdPath, Args);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 // LLC Implementation of AbstractIntepreter interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432 //
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
433 Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
434 std::string &OutputAsmFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
435 unsigned Timeout, unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438 SmallString<128> UniqueFile;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
439 std::error_code EC =
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 if (EC) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
442 errs() << "Error making unique filename: " << EC.message() << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 exit(1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
445 OutputAsmFile = UniqueFile.str();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
446 std::vector<StringRef> LLCArgs;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
447 LLCArgs.push_back(LLCPath);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
448
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 // Add any extra LLC args.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
451 LLCArgs.push_back(ToolArgs[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 LLCArgs.push_back("-o");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
454 LLCArgs.push_back(OutputAsmFile); // Output to the Asm file
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
455 LLCArgs.push_back(Bitcode); // This is the input bitcode
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 if (UseIntegratedAssembler)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 LLCArgs.push_back("-filetype=obj");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
462 LLVM_DEBUG(errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
463 for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
464 << " " << LLCArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
465 errs() << "\n";);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
466 if (RunProgramWithTimeout(LLCPath, LLCArgs, "", "", "", Timeout, MemoryLimit))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
467 return ProcessFailure(LLCPath, LLCArgs, Timeout, MemoryLimit);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
468 return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
471 Error LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
472 unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 std::string OutputAsmFile;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
474 Expected<CC::FileType> Result =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
475 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 sys::fs::remove(OutputAsmFile);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
477 if (Error E = Result.takeError())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
478 return E;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
479 return Error::success();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
482 Expected<int> LLC::ExecuteProgram(const std::string &Bitcode,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
483 const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
484 const std::string &InputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
485 const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
486 const std::vector<std::string> &ArgsForCC,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
487 const std::vector<std::string> &SharedLibs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
488 unsigned Timeout, unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
489
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 std::string OutputAsmFile;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
491 Expected<CC::FileType> FileKind =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
492 OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
494 if (Error E = FileKind.takeError())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
495 return std::move(E);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
497 std::vector<std::string> CCArgs(ArgsForCC);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
498 CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
499
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
500 // Assuming LLC worked, compile the result with CC and run it.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
501 return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
502 OutputFile, CCArgs, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 /// createLLC - Try to find the LLC executable
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
506 ///
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
507 LLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
508 const std::string &CCBinary,
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
509 const std::vector<std::string> *Args,
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
510 const std::vector<std::string> *CCArgs,
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 bool UseIntegratedAssembler) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
512 ErrorOr<std::string> LLCPath =
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
513 FindProgramByName("llc", Argv0, (void *)(intptr_t)&createLLC);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
514 if (!LLCPath) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
515 Message = LLCPath.getError().message() + "\n";
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
516 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
519 CC *cc = CC::create(Argv0, Message, CCBinary, CCArgs);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
520 if (!cc) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 errs() << Message << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 exit(1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 }
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
524 Message = "Found llc: " + *LLCPath + "\n";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
525 return new LLC(*LLCPath, cc, Args, UseIntegratedAssembler);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 //===---------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
529 // JIT Implementation of AbstractIntepreter interface
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
532 class JIT : public AbstractInterpreter {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
533 std::string LLIPath; // The path to the LLI executable
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
534 std::vector<std::string> ToolArgs; // Args to pass to LLI
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
535 public:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
536 JIT(const std::string &Path, const std::vector<std::string> *Args)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 : LLIPath(Path) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
538 ToolArgs.clear();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
539 if (Args) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
540 ToolArgs = *Args;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
542 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
543
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
544 Expected<int> ExecuteProgram(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
545 const std::string &Bitcode, const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
546 const std::string &InputFile, const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
547 const std::vector<std::string> &CCArgs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
548 const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
549 unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
550 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
552
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
553 Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
554 const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
555 const std::string &InputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
556 const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
557 const std::vector<std::string> &CCArgs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
558 const std::vector<std::string> &SharedLibs,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
559 unsigned Timeout, unsigned MemoryLimit) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
560 // Construct a vector of parameters, incorporating those from the command-line
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
561 std::vector<StringRef> JITArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
562 JITArgs.push_back(LLIPath.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
563 JITArgs.push_back("-force-interpreter=false");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
564
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
565 // Add any extra LLI args.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
567 JITArgs.push_back(ToolArgs[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
568
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 JITArgs.push_back("-load");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
571 JITArgs.push_back(SharedLibs[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
573 JITArgs.push_back(Bitcode.c_str());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
574 // Add optional parameters to the running program from Argv
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
575 for (unsigned i = 0, e = Args.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
576 JITArgs.push_back(Args[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
577
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
578 outs() << "<jit>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
579 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
580 LLVM_DEBUG(errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
581 for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
582 << " " << JITArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
583 errs() << "\n";);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
584 LLVM_DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
585 return RunProgramWithTimeout(LLIPath, JITArgs, InputFile, OutputFile,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
586 OutputFile, Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
587 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
588
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
589 /// createJIT - Try to find the LLI executable
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
590 ///
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
591 AbstractInterpreter *
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
592 AbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
593 const std::vector<std::string> *Args) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
594 if (ErrorOr<std::string> LLIPath =
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
595 FindProgramByName("lli", Argv0, (void *)(intptr_t)&createJIT)) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
596 Message = "Found lli: " + *LLIPath + "\n";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
597 return new JIT(*LLIPath, Args);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
598 } else {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
599 Message = LLIPath.getError().message() + "\n";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
600 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
601 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
602 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
603
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
604 //===---------------------------------------------------------------------===//
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
605 // CC abstraction
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
606 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
607
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
608 static bool IsARMArchitecture(std::vector<StringRef> Args) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
609 for (size_t I = 0; I < Args.size(); ++I) {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
610 if (!Args[I].equals_lower("-arch"))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
611 continue;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
612 ++I;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
613 if (I == Args.size())
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
614 break;
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
615 if (Args[I].startswith_lower("arm"))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
616 return true;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
617 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
618
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
619 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
620 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
621
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
622 Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
623 const std::vector<std::string> &Args,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
624 FileType fileType,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
625 const std::string &InputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
626 const std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
627 const std::vector<std::string> &ArgsForCC,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
628 unsigned Timeout, unsigned MemoryLimit) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
629 std::vector<StringRef> CCArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
630
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
631 CCArgs.push_back(CCPath);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
632
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
633 if (TargetTriple.getArch() == Triple::x86)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
634 CCArgs.push_back("-m32");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
635
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
636 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
637 E = ccArgs.end();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
638 I != E; ++I)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
639 CCArgs.push_back(*I);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
640
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
641 // Specify -x explicitly in case the extension is wonky
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
642 if (fileType != ObjectFile) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
643 CCArgs.push_back("-x");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
644 if (fileType == CFile) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
645 CCArgs.push_back("c");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
646 CCArgs.push_back("-fno-strict-aliasing");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
647 } else {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
648 CCArgs.push_back("assembler");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
649
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
650 // For ARM architectures we don't want this flag. bugpoint isn't
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
651 // explicitly told what architecture it is working on, so we get
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
652 // it from cc flags
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
653 if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
654 CCArgs.push_back("-force_cpusubtype_ALL");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
655 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
656 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
657
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
658 CCArgs.push_back(ProgramFile); // Specify the input filename.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
659
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
660 CCArgs.push_back("-x");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
661 CCArgs.push_back("none");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
662 CCArgs.push_back("-o");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
663
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
664 SmallString<128> OutputBinary;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
665 std::error_code EC =
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
666 sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
667 if (EC) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
668 errs() << "Error making unique filename: " << EC.message() << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
669 exit(1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
670 }
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
671 CCArgs.push_back(OutputBinary); // Output to the right file...
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
672
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
673 // Add any arguments intended for CC. We locate them here because this is
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
674 // most likely -L and -l options that need to come before other libraries but
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
675 // after the source. Other options won't be sensitive to placement on the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
676 // command line, so this should be safe.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
677 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
678 CCArgs.push_back(ArgsForCC[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
679
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
680 CCArgs.push_back("-lm"); // Hard-code the math library...
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
681 CCArgs.push_back("-O2"); // Optimize the program a bit...
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
682 if (TargetTriple.getArch() == Triple::sparc)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
683 CCArgs.push_back("-mcpu=v9");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
684
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
685 outs() << "<CC>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
686 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
687 LLVM_DEBUG(errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
688 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
689 << " " << CCArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
690 errs() << "\n";);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
691 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
692 return ProcessFailure(CCPath, CCArgs);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
694 std::vector<StringRef> ProgramArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 // Declared here so that the destructor only runs after
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
697 // ProgramArgs is used.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
698 std::string Exec;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700 if (RemoteClientPath.empty())
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
701 ProgramArgs.push_back(OutputBinary);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702 else {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
703 ProgramArgs.push_back(RemoteClientPath);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
704 ProgramArgs.push_back(RemoteHost);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705 if (!RemoteUser.empty()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
706 ProgramArgs.push_back("-l");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
707 ProgramArgs.push_back(RemoteUser);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
708 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 if (!RemotePort.empty()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 ProgramArgs.push_back("-p");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
711 ProgramArgs.push_back(RemotePort);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
712 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
713 if (!RemoteExtra.empty()) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
714 ProgramArgs.push_back(RemoteExtra);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
715 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
716
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
717 // Full path to the binary. We need to cd to the exec directory because
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
718 // there is a dylib there that the exec expects to find in the CWD
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
719 char *env_pwd = getenv("PWD");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
720 Exec = "cd ";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
721 Exec += env_pwd;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
722 Exec += "; ./";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
723 Exec += OutputBinary.c_str();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
724 ProgramArgs.push_back(Exec);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
725 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
727 // Add optional parameters to the running program from Argv
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 for (unsigned i = 0, e = Args.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
729 ProgramArgs.push_back(Args[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
730
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
731 // Now that we have a binary, run it!
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
732 outs() << "<program>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
733 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
734 LLVM_DEBUG(
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
735 errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
736 for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
737 << " " << ProgramArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
738 errs() << "\n";);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
739
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
740 FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
741
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
742 if (RemoteClientPath.empty()) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
743 LLVM_DEBUG(errs() << "<run locally>");
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
744 std::string Error;
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
745 int ExitCode = RunProgramWithTimeout(OutputBinary.str(), ProgramArgs,
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
746 InputFile, OutputFile, OutputFile,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
747 Timeout, MemoryLimit, &Error);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
748 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 // so that crash-causing miscompilation is handled seamlessly.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
750 if (ExitCode < -1) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
751 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
752 outFile << Error << '\n';
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
753 outFile.close();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
755 return ExitCode;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
756 } else {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
757 outs() << "<run remotely>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
758 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
759 return RunProgramRemotelyWithTimeout(RemoteClientPath, ProgramArgs,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
760 InputFile, OutputFile, OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
761 Timeout, MemoryLimit);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
763 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
764
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
765 Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
766 std::string &OutputFile,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
767 const std::vector<std::string> &ArgsForCC) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
768 SmallString<128> UniqueFilename;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
769 std::error_code EC = sys::fs::createUniqueFile(
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
770 InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
771 if (EC) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
772 errs() << "Error making unique filename: " << EC.message() << "\n";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
773 exit(1);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
774 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
775 OutputFile = UniqueFilename.str();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
776
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
777 std::vector<StringRef> CCArgs;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
778
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
779 CCArgs.push_back(CCPath);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
780
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 if (TargetTriple.getArch() == Triple::x86)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
782 CCArgs.push_back("-m32");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
783
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
784 for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
785 E = ccArgs.end();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
786 I != E; ++I)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
787 CCArgs.push_back(*I);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
788
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
789 // Compile the C/asm file into a shared object
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
790 if (fileType != ObjectFile) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
791 CCArgs.push_back("-x");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
792 CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
794 CCArgs.push_back("-fno-strict-aliasing");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
795 CCArgs.push_back(InputFile); // Specify the input filename.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
796 CCArgs.push_back("-x");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
797 CCArgs.push_back("none");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 if (TargetTriple.getArch() == Triple::sparc)
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
799 CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
800 else if (TargetTriple.isOSDarwin()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
801 // link all source files into a single module in data segment, rather than
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
802 // generating blocks. dynamic_lookup requires that you set
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
803 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
804 // bugpoint to just pass that in the environment of CC.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
805 CCArgs.push_back("-single_module");
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
806 CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
807 CCArgs.push_back("-undefined");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
808 CCArgs.push_back("dynamic_lookup");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 } else
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
810 CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
811
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
812 if (TargetTriple.getArch() == Triple::x86_64)
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
813 CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
814
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 if (TargetTriple.getArch() == Triple::sparc)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
816 CCArgs.push_back("-mcpu=v9");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
817
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
818 CCArgs.push_back("-o");
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
819 CCArgs.push_back(OutputFile); // Output to the right filename.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
820 CCArgs.push_back("-O2"); // Optimize the program a bit.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
821
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
822 // Add any arguments intended for CC. We locate them here because this is
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
823 // most likely -L and -l options that need to come before other libraries but
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
824 // after the source. Other options won't be sensitive to placement on the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
825 // command line, so this should be safe.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
826 for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
827 CCArgs.push_back(ArgsForCC[i]);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
828
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
829 outs() << "<CC>";
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
830 outs().flush();
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
831 LLVM_DEBUG(errs() << "\nAbout to run:\t";
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
832 for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
833 << " " << CCArgs[i];
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
834 errs() << "\n";);
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
835 if (RunProgramWithTimeout(CCPath, CCArgs, "", "", ""))
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
836 return ProcessFailure(CCPath, CCArgs);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
837 return Error::success();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
838 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
839
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
840 /// create - Try to find the CC executable
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
841 ///
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
842 CC *CC::create(const char *Argv0, std::string &Message,
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
843 const std::string &CCBinary,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
844 const std::vector<std::string> *Args) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
845 auto CCPath = FindProgramByName(CCBinary, Argv0, (void *)(intptr_t)&create);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
846 if (!CCPath) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
847 Message = "Cannot find `" + CCBinary + "' in PATH: " +
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
848 CCPath.getError().message() + "\n";
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
849 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
850 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
851
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
852 std::string RemoteClientPath;
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
853 if (!RemoteClient.empty()) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
854 auto Path = sys::findProgramByName(RemoteClient);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
855 if (!Path) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
856 Message = "Cannot find `" + RemoteClient + "' in PATH: " +
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
857 Path.getError().message() + "\n";
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
858 return nullptr;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
859 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
860 RemoteClientPath = *Path;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
861 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
862
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
863 Message = "Found CC: " + *CCPath + "\n";
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
864 return new CC(*CCPath, RemoteClientPath, Args);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
865 }