Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // This utility is intended to be compatible with GCC, and follows standard |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 4 | // system 'ld' conventions. As such, the default output file is ./a.out. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 5 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 6 | // to execute the program. In this manner, the generated executable (a.out for |
| 7 | // example), is directly executable, whereas the bytecode file actually lives in |
| 8 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 9 | // |
| 10 | // Note that if someone (or a script) deletes the executable program generated, |
| 11 | // the .bc file will be left around. Considering that this is a temporary hack, |
Brian Gaeke | 69a7960 | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 12 | // I'm not too worried about this. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 16 | #include "gccld.h" |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
| 20 | #include "llvm/Bytecode/Reader.h" |
| 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
John Criswell | d35b5b5 | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 25 | #include "Support/FileUtilities.h" |
John Criswell | 621727c | 2003-09-17 15:20:51 +0000 | [diff] [blame] | 26 | #include "Support/SystemUtils.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 27 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 28 | #include "Support/Signals.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 29 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 30 | #include <fstream> |
| 31 | #include <memory> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 32 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | cl::list<std::string> |
| 35 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 36 | cl::OneOrMore); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 38 | cl::opt<std::string> |
| 39 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 40 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 42 | cl::opt<bool> |
| 43 | Verbose("v", cl::desc("Print information about actions taken")); |
| 44 | |
| 45 | cl::list<std::string> |
| 46 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 47 | cl::value_desc("directory")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 49 | cl::list<std::string> |
| 50 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 51 | cl::value_desc("library prefix")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 52 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 53 | cl::opt<bool> |
| 54 | Strip("s", cl::desc("Strip symbol info from executable")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 56 | cl::opt<bool> |
| 57 | NoInternalize("disable-internalize", |
| 58 | cl::desc("Do not mark all symbols as internal")); |
Chris Lattner | a2b2dc9 | 2003-08-22 19:18:45 +0000 | [diff] [blame] | 59 | static cl::alias |
| 60 | ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"), |
| 61 | cl::aliasopt(NoInternalize)); |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 63 | cl::opt<bool> |
| 64 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 65 | " library, not an executable")); |
| 66 | |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 67 | cl::opt<bool> |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 68 | Native("native", |
| 69 | cl::desc("Generate a native binary instead of a shell script")); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 70 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 71 | // Compatibility options that are ignored but supported by LD |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 72 | cl::opt<std::string> |
| 73 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 74 | cl::opt<std::string> |
| 75 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 76 | cl::opt<bool> |
| 77 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | 6ac79d1 | 2003-05-27 19:15:11 +0000 | [diff] [blame] | 78 | cl::opt<bool> |
| 79 | CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 82 | // |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 83 | // Function: PrintAndReturn () |
| 84 | // |
| 85 | // Description: |
| 86 | // Prints a message (usually error message) to standard error (stderr) and |
| 87 | // returns a value usable for an exit status. |
| 88 | // |
| 89 | // Inputs: |
| 90 | // progname - The name of the program (i.e. argv[0]). |
| 91 | // Message - The message to print to standard error. |
| 92 | // Extra - Extra information to print between the program name and thei |
| 93 | // message. It is optional. |
| 94 | // |
| 95 | // Outputs: |
| 96 | // None. |
| 97 | // |
| 98 | // Return value: |
| 99 | // Returns a value that can be used as the exit status (i.e. for exit()). |
| 100 | // |
| 101 | int |
| 102 | PrintAndReturn (const char *progname, |
| 103 | const std::string &Message, |
| 104 | const std::string &Extra) |
| 105 | { |
| 106 | std::cerr << progname << Extra << ": " << Message << "\n"; |
| 107 | return 1; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 110 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 111 | // |
| 112 | // |
| 113 | // Function: CopyEnv() |
| 114 | // |
| 115 | // Description: |
| 116 | // This function takes an array of environment variables and makes a |
| 117 | // copy of it. This copy can then be manipulated any way the caller likes |
| 118 | // without affecting the process's real environment. |
| 119 | // |
| 120 | // Inputs: |
| 121 | // envp - An array of C strings containing an environment. |
| 122 | // |
| 123 | // Outputs: |
| 124 | // None. |
| 125 | // |
| 126 | // Return value: |
| 127 | // NULL - An error occurred. |
| 128 | // |
| 129 | // Otherwise, a pointer to a new array of C strings is returned. Every string |
| 130 | // in the array is a duplicate of the one in the original array (i.e. we do |
| 131 | // not copy the char *'s from one array to another). |
| 132 | // |
| 133 | char ** |
| 134 | CopyEnv (char ** const envp) |
| 135 | { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 136 | // |
| 137 | // Count the number of entries in the old list; |
| 138 | // |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 139 | unsigned entries; // The number of entries in the old environment list |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 140 | for (entries = 0; envp[entries] != NULL; entries++) |
| 141 | { |
| 142 | ; |
Chris Lattner | 7cb77e1 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 143 | } |
| 144 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 145 | // |
| 146 | // Add one more entry for the NULL pointer that ends the list. |
| 147 | // |
| 148 | ++entries; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 149 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 150 | // |
| 151 | // If there are no entries at all, just return NULL. |
| 152 | // |
| 153 | if (entries == 0) |
| 154 | { |
| 155 | return NULL; |
| 156 | } |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 157 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 158 | // |
| 159 | // Allocate a new environment list. |
| 160 | // |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 161 | char **newenv; |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 162 | if ((newenv = new (char *) [entries]) == NULL) |
| 163 | { |
| 164 | return NULL; |
| 165 | } |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 166 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 167 | // |
| 168 | // Make a copy of the list. Don't forget the NULL that ends the list. |
| 169 | // |
| 170 | entries = 0; |
| 171 | while (envp[entries] != NULL) |
| 172 | { |
| 173 | newenv[entries] = new char[strlen (envp[entries]) + 1]; |
| 174 | strcpy (newenv[entries], envp[entries]); |
| 175 | ++entries; |
| 176 | } |
| 177 | newenv[entries] = NULL; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 178 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 179 | return newenv; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | // |
| 184 | // Function: RemoveEnv() |
| 185 | // |
| 186 | // Description: |
| 187 | // Remove the specified environment variable from the environment array. |
| 188 | // |
| 189 | // Inputs: |
| 190 | // name - The name of the variable to remove. It cannot be NULL. |
| 191 | // envp - The array of environment variables. It cannot be NULL. |
| 192 | // |
| 193 | // Outputs: |
| 194 | // envp - The pointer to the specified variable name is removed. |
| 195 | // |
| 196 | // Return value: |
| 197 | // None. |
| 198 | // |
| 199 | // Notes: |
| 200 | // This is mainly done because functions to remove items from the environment |
| 201 | // are not available across all platforms. In particular, Solaris does not |
| 202 | // seem to have an unsetenv() function or a setenv() function (or they are |
| 203 | // undocumented if they do exist). |
| 204 | // |
| 205 | void |
| 206 | RemoveEnv (const char * name, char ** const envp) |
| 207 | { |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 208 | for (unsigned index=0; envp[index] != NULL; index++) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 209 | // Find the first equals sign in the array and make it an EOS character. |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 210 | char *p = strchr (envp[index], '='); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 211 | if (p == NULL) |
| 212 | { |
| 213 | continue; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | *p = '\0'; |
| 218 | } |
| 219 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 220 | // Compare the two strings. If they are equal, zap this string. |
| 221 | // Otherwise, restore it. |
| 222 | // |
| 223 | if (!strcmp (name, envp[index])) |
| 224 | { |
| 225 | *envp[index] = '\0'; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | *p = '='; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 232 | |
| 233 | return; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 236 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 237 | int |
| 238 | main(int argc, char **argv, char ** envp) |
John Criswell | 621727c | 2003-09-17 15:20:51 +0000 | [diff] [blame] | 239 | { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 240 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 242 | std::string ErrorMessage; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 243 | std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage)); |
| 244 | if (Composite.get() == 0) |
| 245 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 246 | |
Brian Gaeke | 69a7960 | 2003-05-23 20:27:07 +0000 | [diff] [blame] | 247 | // We always look first in the current directory when searching for libraries. |
| 248 | LibPaths.insert(LibPaths.begin(), "."); |
| 249 | |
Chris Lattner | d34a51d | 2003-04-21 19:53:24 +0000 | [diff] [blame] | 250 | // If the user specied an extra search path in their environment, respect it. |
| 251 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) |
| 252 | LibPaths.push_back(SearchPath); |
| 253 | |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 254 | // Remove any consecutive duplicates of the same library... |
| 255 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 256 | Libraries.end()); |
| 257 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 258 | // Link in all of the files |
Brian Gaeke | e98ddfc | 2003-09-30 14:03:48 +0000 | [diff] [blame^] | 259 | if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose)) |
| 260 | return 1; // Error already printed |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 261 | LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths, Verbose, Native); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 263 | // Link in all of the libraries next... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 264 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 265 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 266 | // Create the output file. |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 267 | // |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 268 | std::string RealBytecodeOutput = OutputFilename; |
| 269 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 270 | std::ofstream Out(RealBytecodeOutput.c_str()); |
| 271 | if (!Out.good()) |
| 272 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 273 | "' for writing!"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 274 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 275 | // |
| 276 | // Ensure that the bytecode file gets removed from the disk if we get a |
| 277 | // SIGINT signal. |
| 278 | // |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 279 | RemoveFileOnSignal(RealBytecodeOutput); |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 280 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 281 | // |
| 282 | // Generate the bytecode file. |
| 283 | // |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 284 | if (GenerateBytecode (Composite.get(), Strip, !NoInternalize, &Out)) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 285 | Out.close(); |
| 286 | return PrintAndReturn(argv[0], "error generating bytcode"); |
| 287 | } |
| 288 | |
| 289 | // |
| 290 | // Close the bytecode file. |
| 291 | // |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 292 | Out.close(); |
| 293 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 294 | // |
| 295 | // If we are not linking a library, generate either a native executable |
| 296 | // or a JIT shell script, depending upon what the user wants. |
| 297 | // |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 298 | if (!LinkAsLibrary) { |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 299 | // |
| 300 | // If the user wants to generate a native executable, compile it from the |
| 301 | // bytecode file. |
| 302 | // |
| 303 | // Otherwise, create a script that will run the bytecode through the JIT. |
| 304 | // |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 305 | if (Native) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 306 | // Name of the Assembly Language output file |
| 307 | std::string AssemblyFile = OutputFilename + ".s"; |
| 308 | |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 309 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 310 | // Mark the output files for removal if we get an interrupt. |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 311 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 312 | RemoveFileOnSignal (AssemblyFile); |
| 313 | RemoveFileOnSignal (OutputFilename); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 314 | |
| 315 | // |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 316 | // Determine the locations of the llc and gcc programs. |
| 317 | // |
| 318 | std::string llc=FindExecutable ("llc", argv[0]); |
| 319 | std::string gcc=FindExecutable ("gcc", argv[0]); |
| 320 | if (llc.empty()) |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 321 | return PrintAndReturn (argv[0], "Failed to find llc"); |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 322 | |
| 323 | if (gcc.empty()) |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 324 | return PrintAndReturn (argv[0], "Failed to find gcc"); |
John Criswell | 83ca6ec | 2003-09-17 19:04:22 +0000 | [diff] [blame] | 325 | |
| 326 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 327 | // Generate an assembly language file for the bytecode. |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 328 | // |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 329 | if (Verbose) std::cout << "Generating Assembly Code\n"; |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 330 | GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 331 | if (Verbose) std::cout << "Generating Native Code\n"; |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 332 | GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths, |
| 333 | gcc, envp); |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 334 | |
| 335 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 336 | // Remove the assembly language file. |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 337 | // |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 338 | removeFile (AssemblyFile); |
Chris Lattner | 6e27123 | 2003-09-22 20:21:34 +0000 | [diff] [blame] | 339 | } else { |
John Criswell | dabaf7d | 2003-09-16 21:27:35 +0000 | [diff] [blame] | 340 | // Output the script to start the program... |
| 341 | std::ofstream Out2(OutputFilename.c_str()); |
| 342 | if (!Out2.good()) |
| 343 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 344 | "' for writing!"); |
| 345 | Out2 << "#!/bin/sh\nlli -q $0.bc $*\n"; |
| 346 | Out2.close(); |
| 347 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 349 | // Make the script executable... |
John Criswell | d35b5b5 | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 350 | MakeFileExecutable (OutputFilename); |
Misha Brukman | c1fdca8 | 2003-08-20 20:38:15 +0000 | [diff] [blame] | 351 | |
John Criswell | 22edc39 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 352 | // Make the bytecode file readable and directly executable in LLEE as well |
John Criswell | d35b5b5 | 2003-09-02 20:17:20 +0000 | [diff] [blame] | 353 | MakeFileExecutable (RealBytecodeOutput); |
John Criswell | 22edc39 | 2003-09-02 21:11:22 +0000 | [diff] [blame] | 354 | MakeFileReadable (RealBytecodeOutput); |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 355 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 356 | |
| 357 | return 0; |
| 358 | } |