Chris Lattner | a58d2be | 2003-09-30 03:24:28 +0000 | [diff] [blame] | 1 | //===- GenerateCode.cpp - Functions for generating executable files ------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains functions for generating executable files once linking |
| 11 | // has finished. This includes generating a shell script to run the JIT or |
| 12 | // a native executable derived from the bytecode. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 16 | #include "gccld.h" |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 17 | #include "llvm/System/Program.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/PassManager.h" |
Chris Lattner | cc650b6 | 2003-11-09 19:55:09 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoadValueNumbering.h" |
Chris Lattner | 2d26ffb | 2004-07-27 08:13:15 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Passes.h" |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 23 | #include "llvm/Bytecode/Archive.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 24 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
| 26 | #include "llvm/Transforms/IPO.h" |
| 27 | #include "llvm/Transforms/Scalar.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SystemUtils.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Streams.h" |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | cl::opt<bool> |
| 35 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 36 | |
| 37 | cl::opt<bool> |
| 38 | Verify("verify", cl::desc("Verify intermediate results of all passes")); |
| 39 | |
| 40 | cl::opt<bool> |
| 41 | DisableOptimizations("disable-opt", |
| 42 | cl::desc("Do not run any optimization passes")); |
John Criswell | d452599 | 2006-11-21 22:50:24 +0000 | [diff] [blame] | 43 | |
| 44 | cl::opt<bool> |
Chris Lattner | 17be679 | 2007-01-21 06:34:18 +0000 | [diff] [blame] | 45 | NoCompress("disable-compression", cl::init(true), |
John Criswell | d452599 | 2006-11-21 22:50:24 +0000 | [diff] [blame] | 46 | cl::desc("Don't compress the generated bytecode")); |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 49 | /// CopyEnv - This function takes an array of environment variables and makes a |
| 50 | /// copy of it. This copy can then be manipulated any way the caller likes |
| 51 | /// without affecting the process's real environment. |
| 52 | /// |
| 53 | /// Inputs: |
| 54 | /// envp - An array of C strings containing an environment. |
| 55 | /// |
| 56 | /// Return value: |
| 57 | /// NULL - An error occurred. |
| 58 | /// |
| 59 | /// Otherwise, a pointer to a new array of C strings is returned. Every string |
| 60 | /// in the array is a duplicate of the one in the original array (i.e. we do |
| 61 | /// not copy the char *'s from one array to another). |
| 62 | /// |
| 63 | static char ** CopyEnv(char ** const envp) { |
| 64 | // Count the number of entries in the old list; |
| 65 | unsigned entries; // The number of entries in the old environment list |
| 66 | for (entries = 0; envp[entries] != NULL; entries++) |
| 67 | /*empty*/; |
| 68 | |
| 69 | // Add one more entry for the NULL pointer that ends the list. |
| 70 | ++entries; |
| 71 | |
| 72 | // If there are no entries at all, just return NULL. |
| 73 | if (entries == 0) |
| 74 | return NULL; |
| 75 | |
| 76 | // Allocate a new environment list. |
| 77 | char **newenv = new char* [entries]; |
| 78 | if ((newenv = new char* [entries]) == NULL) |
| 79 | return NULL; |
| 80 | |
| 81 | // Make a copy of the list. Don't forget the NULL that ends the list. |
| 82 | entries = 0; |
| 83 | while (envp[entries] != NULL) { |
| 84 | newenv[entries] = new char[strlen (envp[entries]) + 1]; |
| 85 | strcpy (newenv[entries], envp[entries]); |
| 86 | ++entries; |
| 87 | } |
| 88 | newenv[entries] = NULL; |
| 89 | |
| 90 | return newenv; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /// RemoveEnv - Remove the specified environment variable from the environment |
| 95 | /// array. |
| 96 | /// |
| 97 | /// Inputs: |
| 98 | /// name - The name of the variable to remove. It cannot be NULL. |
| 99 | /// envp - The array of environment variables. It cannot be NULL. |
| 100 | /// |
| 101 | /// Notes: |
| 102 | /// This is mainly done because functions to remove items from the environment |
| 103 | /// are not available across all platforms. In particular, Solaris does not |
| 104 | /// seem to have an unsetenv() function or a setenv() function (or they are |
| 105 | /// undocumented if they do exist). |
| 106 | /// |
| 107 | static void RemoveEnv(const char * name, char ** const envp) { |
| 108 | for (unsigned index=0; envp[index] != NULL; index++) { |
| 109 | // Find the first equals sign in the array and make it an EOS character. |
| 110 | char *p = strchr (envp[index], '='); |
| 111 | if (p == NULL) |
| 112 | continue; |
| 113 | else |
| 114 | *p = '\0'; |
| 115 | |
| 116 | // Compare the two strings. If they are equal, zap this string. |
| 117 | // Otherwise, restore it. |
| 118 | if (!strcmp(name, envp[index])) |
| 119 | *envp[index] = '\0'; |
| 120 | else |
| 121 | *p = '='; |
| 122 | } |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 125 | static void dumpArgs(const char **args) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 126 | cerr << *args++; |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 127 | while (*args) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 128 | cerr << ' ' << *args++; |
| 129 | cerr << '\n' << std::flush; |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 132 | static inline void addPass(PassManager &PM, Pass *P) { |
| 133 | // Add the pass to the pass manager... |
| 134 | PM.add(P); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 135 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 136 | // If we are verifying all of the intermediate steps, add the verifier... |
| 137 | if (Verify) PM.add(createVerifierPass()); |
| 138 | } |
| 139 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 140 | static bool isBytecodeLibrary(const sys::Path &FullPath) { |
| 141 | // Check for a bytecode file |
| 142 | if (FullPath.isBytecodeFile()) return true; |
| 143 | // Check for a dynamic library file |
| 144 | if (FullPath.isDynamicLibrary()) return false; |
| 145 | // Check for a true bytecode archive file |
| 146 | if (FullPath.isArchive() ) { |
| 147 | std::string ErrorMessage; |
| 148 | Archive* ar = Archive::OpenAndLoadSymbols( FullPath, &ErrorMessage ); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 149 | return ar->isBytecodeArchive(); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 154 | static bool isBytecodeLPath(const std::string &LibPath) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 155 | sys::Path LPath(LibPath); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 157 | // Make sure it exists and is a directory |
Chris Lattner | 0b32d8b | 2006-08-01 18:04:01 +0000 | [diff] [blame] | 158 | sys::FileStatus Status; |
| 159 | if (LPath.getFileStatus(Status) || !Status.isDir) |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 160 | return false; |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 162 | // Grab the contents of the -L path |
| 163 | std::set<sys::Path> Files; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 164 | if (LPath.getDirectoryContents(Files, 0)) |
| 165 | return false; |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 166 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 167 | // Iterate over the contents one by one to determine |
| 168 | // if this -L path has any bytecode shared libraries |
| 169 | // or archives |
| 170 | std::set<sys::Path>::iterator File = Files.begin(); |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 171 | std::string dllsuffix = sys::Path::GetDLLSuffix(); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 172 | for (; File != Files.end(); ++File) { |
| 173 | |
Chris Lattner | 0b32d8b | 2006-08-01 18:04:01 +0000 | [diff] [blame] | 174 | // Not a file? |
| 175 | if (File->getFileStatus(Status) || Status.isDir) |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 176 | continue; |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 177 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 178 | std::string path = File->toString(); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 0b32d8b | 2006-08-01 18:04:01 +0000 | [diff] [blame] | 180 | // Check for an ending '.dll', '.so' or '.a' suffix as all |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 181 | // other files are not of interest to us here |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 182 | if (path.find(dllsuffix, path.size()-dllsuffix.size()) == std::string::npos |
| 183 | && path.find(".a", path.size()-2) == std::string::npos) |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 184 | continue; |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 186 | // Finally, check to see if the file is a true bytecode file |
| 187 | if (isBytecodeLibrary(*File)) |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 188 | return true; |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 7b5634d | 2005-09-23 06:11:24 +0000 | [diff] [blame] | 190 | return false; |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 193 | /// GenerateBytecode - generates a bytecode file from the specified module. |
| 194 | /// |
| 195 | /// Inputs: |
| 196 | /// M - The module for which bytecode should be generated. |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 197 | /// StripLevel - 2 if we should strip all symbols, 1 if we should strip |
| 198 | /// debug info. |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 199 | /// Internalize - Flags whether all symbols should be marked internal. |
| 200 | /// Out - Pointer to file stream to which to write the output. |
| 201 | /// |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 202 | /// Returns non-zero value on error. |
| 203 | /// |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 204 | int llvm::GenerateBytecode(Module *M, int StripLevel, bool Internalize, |
Chris Lattner | 27a9b27 | 2004-04-06 16:54:04 +0000 | [diff] [blame] | 205 | std::ostream *Out) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 206 | // In addition to just linking the input from GCC, we also want to spiff it up |
| 207 | // a little bit. Do this now. |
| 208 | PassManager Passes; |
| 209 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 210 | if (Verify) Passes.add(createVerifierPass()); |
| 211 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 212 | // Add an appropriate TargetData instance for this module... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 213 | addPass(Passes, new TargetData(M)); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 548e813 | 2003-11-28 09:44:03 +0000 | [diff] [blame] | 215 | // Often if the programmer does not specify proper prototypes for the |
| 216 | // functions they are calling, they end up calling a vararg version of the |
| 217 | // function that does not get a body filled in (the real function has typed |
| 218 | // arguments). This pass merges the two functions. |
| 219 | addPass(Passes, createFunctionResolvingPass()); |
| 220 | |
Chris Lattner | 429a9cb | 2004-11-16 18:59:20 +0000 | [diff] [blame] | 221 | if (!DisableOptimizations) { |
Chris Lattner | fbcd54f | 2005-10-18 06:29:43 +0000 | [diff] [blame] | 222 | // Now that composite has been compiled, scan through the module, looking |
| 223 | // for a main function. If main is defined, mark all other functions |
| 224 | // internal. |
| 225 | addPass(Passes, createInternalizePass(Internalize)); |
Chris Lattner | 86f42db | 2004-11-17 16:41:19 +0000 | [diff] [blame] | 226 | |
Chris Lattner | cf508bc | 2006-09-09 21:30:13 +0000 | [diff] [blame] | 227 | // Propagate constants at call sites into the functions they call. This |
| 228 | // opens opportunities for globalopt (and inlining) by substituting function |
| 229 | // pointers passed as arguments to direct uses of functions. |
| 230 | addPass(Passes, createIPSCCPPass()); |
| 231 | |
Chris Lattner | 93a00e4 | 2004-10-07 04:12:02 +0000 | [diff] [blame] | 232 | // Now that we internalized some globals, see if we can hack on them! |
| 233 | addPass(Passes, createGlobalOptimizerPass()); |
Chris Lattner | dd429c6 | 2004-02-25 21:35:13 +0000 | [diff] [blame] | 234 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 235 | // Linking modules together can lead to duplicated global constants, only |
| 236 | // keep one copy of each constant... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 237 | addPass(Passes, createConstantMergePass()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 238 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 239 | // Remove unused arguments from functions... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 240 | addPass(Passes, createDeadArgEliminationPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 241 | |
Chris Lattner | a49fd07 | 2007-02-03 23:06:03 +0000 | [diff] [blame] | 242 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 243 | // simplification opportunities, and both can propagate functions through |
| 244 | // function pointers. When this happens, we often have to resolve varargs |
| 245 | // calls, etc, so let instcombine do this. |
| 246 | addPass(Passes, createInstructionCombiningPass()); |
| 247 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 248 | if (!DisableInline) |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 249 | addPass(Passes, createFunctionInliningPass()); // Inline small functions |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 250 | |
Chris Lattner | f9c455d | 2004-04-12 05:38:15 +0000 | [diff] [blame] | 251 | addPass(Passes, createPruneEHPass()); // Remove dead EH info |
Chris Lattner | e3ef9b5 | 2004-10-11 04:47:18 +0000 | [diff] [blame] | 252 | addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again. |
Chris Lattner | f9c455d | 2004-04-12 05:38:15 +0000 | [diff] [blame] | 253 | addPass(Passes, createGlobalDCEPass()); // Remove dead functions |
| 254 | |
Chris Lattner | f8338c4 | 2004-03-07 22:12:40 +0000 | [diff] [blame] | 255 | // If we didn't decide to inline a function, check to see if we can |
| 256 | // transform it to pass arguments by value instead of by reference. |
| 257 | addPass(Passes, createArgumentPromotionPass()); |
| 258 | |
Chris Lattner | f74a401 | 2004-02-26 03:34:30 +0000 | [diff] [blame] | 259 | // The IPO passes may leave cruft around. Clean up after them. |
| 260 | addPass(Passes, createInstructionCombiningPass()); |
| 261 | |
| 262 | addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas |
| 263 | |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 264 | // Run a few AA driven optimizations here and now, to cleanup the code. |
Chris Lattner | 93127fb | 2004-08-02 10:10:08 +0000 | [diff] [blame] | 265 | addPass(Passes, createGlobalsModRefPass()); // IP alias analysis |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 266 | |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 267 | addPass(Passes, createLICMPass()); // Hoist loop invariants |
| 268 | addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs |
| 269 | addPass(Passes, createGCSEPass()); // Remove common subexprs |
Chris Lattner | 2d26ffb | 2004-07-27 08:13:15 +0000 | [diff] [blame] | 270 | addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 271 | |
Chris Lattner | f74a401 | 2004-02-26 03:34:30 +0000 | [diff] [blame] | 272 | // Cleanup and simplify the code after the scalar optimizations. |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 273 | addPass(Passes, createInstructionCombiningPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 274 | |
| 275 | // Delete basic blocks, which optimization passes may have killed... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 276 | addPass(Passes, createCFGSimplificationPass()); |
Brian Gaeke | 1ab90d4 | 2003-11-16 23:07:28 +0000 | [diff] [blame] | 277 | |
| 278 | // Now that we have optimized the program, discard unreachable functions... |
Misha Brukman | 438e364 | 2003-11-20 06:26:15 +0000 | [diff] [blame] | 279 | addPass(Passes, createGlobalDCEPass()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 3f14fb1 | 2004-12-02 21:26:10 +0000 | [diff] [blame] | 282 | // If the -s or -S command line options were specified, strip the symbols out |
| 283 | // of the resulting program to make it smaller. -s and -S are GLD options |
| 284 | // that we are supporting. |
| 285 | if (StripLevel) |
| 286 | addPass(Passes, createStripSymbolsPass(StripLevel == 1)); |
Chris Lattner | 429a9cb | 2004-11-16 18:59:20 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 0cccb18 | 2004-01-14 03:39:46 +0000 | [diff] [blame] | 288 | // Make sure everything is still good. |
| 289 | Passes.add(createVerifierPass()); |
| 290 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 291 | // Add the pass that writes bytecode to the output file... |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 292 | OStream L(*Out); |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 293 | addPass(Passes, new WriteBytecodePass(&L, false, !NoCompress)); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 294 | |
| 295 | // Run our queue of passes all at once now, efficiently. |
| 296 | Passes.run(*M); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 301 | /// GenerateAssembly - generates a native assembly language source file from the |
| 302 | /// specified bytecode file. |
| 303 | /// |
| 304 | /// Inputs: |
| 305 | /// InputFilename - The name of the output bytecode file. |
| 306 | /// OutputFilename - The name of the file to generate. |
| 307 | /// llc - The pathname to use for LLC. |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 308 | /// |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 309 | /// Return non-zero value on error. |
| 310 | /// |
Chris Lattner | 27a9b27 | 2004-04-06 16:54:04 +0000 | [diff] [blame] | 311 | int llvm::GenerateAssembly(const std::string &OutputFilename, |
| 312 | const std::string &InputFilename, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 313 | const sys::Path &llc, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 314 | std::string& ErrMsg, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 315 | bool Verbose) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 316 | // Run LLC to convert the bytecode file into assembly code. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 317 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 318 | args.push_back(llc.c_str()); |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 319 | args.push_back("-f"); |
| 320 | args.push_back("-o"); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 321 | args.push_back(OutputFilename.c_str()); |
| 322 | args.push_back(InputFilename.c_str()); |
Chris Lattner | ed5fa58 | 2005-02-13 23:02:34 +0000 | [diff] [blame] | 323 | args.push_back(0); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 324 | if (Verbose) dumpArgs(&args[0]); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 325 | return sys::Program::ExecuteAndWait(llc, &args[0],0,0,0,&ErrMsg); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Chris Lattner | 1d924f6 | 2005-08-02 22:07:38 +0000 | [diff] [blame] | 328 | /// GenerateCFile - generates a C source file from the specified bytecode file. |
Chris Lattner | 27a9b27 | 2004-04-06 16:54:04 +0000 | [diff] [blame] | 329 | int llvm::GenerateCFile(const std::string &OutputFile, |
| 330 | const std::string &InputFile, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 331 | const sys::Path &llc, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 332 | std::string& ErrMsg, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 333 | bool Verbose) { |
Chris Lattner | 69e8d28 | 2004-04-06 16:43:13 +0000 | [diff] [blame] | 334 | // Run LLC to convert the bytecode file into C. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 335 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 336 | args.push_back(llc.c_str()); |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 337 | args.push_back("-march=c"); |
| 338 | args.push_back("-f"); |
| 339 | args.push_back("-o"); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 340 | args.push_back(OutputFile.c_str()); |
| 341 | args.push_back(InputFile.c_str()); |
Chris Lattner | ed5fa58 | 2005-02-13 23:02:34 +0000 | [diff] [blame] | 342 | args.push_back(0); |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 343 | if (Verbose) dumpArgs(&args[0]); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 344 | return sys::Program::ExecuteAndWait(llc, &args[0],0,0,0,&ErrMsg); |
Chris Lattner | 69e8d28 | 2004-04-06 16:43:13 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | 1d924f6 | 2005-08-02 22:07:38 +0000 | [diff] [blame] | 347 | /// GenerateNative - generates a native executable file from the specified |
| 348 | /// assembly source file. |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 349 | /// |
| 350 | /// Inputs: |
| 351 | /// InputFilename - The name of the output bytecode file. |
| 352 | /// OutputFilename - The name of the file to generate. |
| 353 | /// Libraries - The list of libraries with which to link. |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 354 | /// gcc - The pathname to use for GGC. |
| 355 | /// envp - A copy of the process's current environment. |
| 356 | /// |
| 357 | /// Outputs: |
| 358 | /// None. |
| 359 | /// |
| 360 | /// Returns non-zero value on error. |
| 361 | /// |
Chris Lattner | 27a9b27 | 2004-04-06 16:54:04 +0000 | [diff] [blame] | 362 | int llvm::GenerateNative(const std::string &OutputFilename, |
| 363 | const std::string &InputFilename, |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 364 | const std::vector<std::string> &LibPaths, |
Chris Lattner | 27a9b27 | 2004-04-06 16:54:04 +0000 | [diff] [blame] | 365 | const std::vector<std::string> &Libraries, |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 366 | const sys::Path &gcc, char ** const envp, |
| 367 | bool Shared, |
Chris Lattner | 1d924f6 | 2005-08-02 22:07:38 +0000 | [diff] [blame] | 368 | bool ExportAllAsDynamic, |
Reid Spencer | 156aa35 | 2005-12-22 01:50:56 +0000 | [diff] [blame] | 369 | const std::vector<std::string> &RPaths, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 370 | const std::string &SOName, |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 371 | std::string& ErrMsg, |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 372 | bool Verbose) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 373 | // Remove these environment variables from the environment of the |
| 374 | // programs that we will execute. It appears that GCC sets these |
| 375 | // environment variables so that the programs it uses can configure |
| 376 | // themselves identically. |
| 377 | // |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 378 | // However, when we invoke GCC below, we want it to use its normal |
| 379 | // configuration. Hence, we must sanitize its environment. |
| 380 | char ** clean_env = CopyEnv(envp); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 381 | if (clean_env == NULL) |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 382 | return 1; |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 383 | RemoveEnv("LIBRARY_PATH", clean_env); |
| 384 | RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); |
| 385 | RemoveEnv("GCC_EXEC_PREFIX", clean_env); |
| 386 | RemoveEnv("COMPILER_PATH", clean_env); |
| 387 | RemoveEnv("COLLECT_GCC", clean_env); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 388 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 389 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 390 | // Run GCC to assemble and link the program into native code. |
| 391 | // |
| 392 | // Note: |
| 393 | // We can't just assemble and link the file with the system assembler |
| 394 | // and linker because we don't know where to put the _start symbol. |
| 395 | // GCC mysteriously knows how to do it. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 396 | std::vector<const char*> args; |
Chris Lattner | bf9add4 | 2005-04-10 20:59:38 +0000 | [diff] [blame] | 397 | args.push_back(gcc.c_str()); |
Reid Spencer | 6da1e0d | 2004-12-14 04:20:08 +0000 | [diff] [blame] | 398 | args.push_back("-fno-strict-aliasing"); |
| 399 | args.push_back("-O3"); |
| 400 | args.push_back("-o"); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 401 | args.push_back(OutputFilename.c_str()); |
| 402 | args.push_back(InputFilename.c_str()); |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 403 | |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 404 | // StringsToDelete - We don't want to call c_str() on temporary strings. |
| 405 | // If we need a temporary string, copy it here so that the memory is not |
| 406 | // reclaimed until after the exec call. All of these strings are allocated |
| 407 | // with strdup. |
| 408 | std::vector<char*> StringsToDelete; |
| 409 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 410 | if (Shared) args.push_back("-shared"); |
Chris Lattner | 1d924f6 | 2005-08-02 22:07:38 +0000 | [diff] [blame] | 411 | if (ExportAllAsDynamic) args.push_back("-export-dynamic"); |
Reid Spencer | 156aa35 | 2005-12-22 01:50:56 +0000 | [diff] [blame] | 412 | if (!RPaths.empty()) { |
| 413 | for (std::vector<std::string>::const_iterator I = RPaths.begin(), |
| 414 | E = RPaths.end(); I != E; I++) { |
| 415 | std::string rp = "-Wl,-rpath," + *I; |
| 416 | StringsToDelete.push_back(strdup(rp.c_str())); |
| 417 | args.push_back(StringsToDelete.back()); |
| 418 | } |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 419 | } |
| 420 | if (!SOName.empty()) { |
| 421 | std::string so = "-Wl,-soname," + SOName; |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 422 | StringsToDelete.push_back(strdup(so.c_str())); |
| 423 | args.push_back(StringsToDelete.back()); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 424 | } |
Misha Brukman | 704448f | 2005-04-20 04:08:35 +0000 | [diff] [blame] | 425 | |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 426 | // Add in the libpaths to find the libraries. |
| 427 | // |
| 428 | // Note: |
| 429 | // When gccld is called from the llvm-gxx frontends, the -L paths for |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 430 | // the LLVM cfrontend install paths are appended. We don't want the |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 431 | // native linker to use these -L paths as they contain bytecode files. |
| 432 | // Further, we don't want any -L paths that contain bytecode shared |
| 433 | // libraries or true bytecode archive files. We omit them in all such |
| 434 | // cases. |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 435 | for (unsigned index = 0; index < LibPaths.size(); index++) |
| 436 | if (!isBytecodeLPath(LibPaths[index])) { |
| 437 | std::string Tmp = "-L"+LibPaths[index]; |
| 438 | StringsToDelete.push_back(strdup(Tmp.c_str())); |
| 439 | args.push_back(StringsToDelete.back()); |
Reid Spencer | 837149c | 2005-02-28 08:45:35 +0000 | [diff] [blame] | 440 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 441 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 442 | // Add in the libraries to link. |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 443 | for (unsigned index = 0; index < Libraries.size(); index++) |
Chris Lattner | 2b11995 | 2005-11-03 07:17:51 +0000 | [diff] [blame] | 444 | // HACK: If this is libg, discard it. This gets added by the compiler |
| 445 | // driver when doing: 'llvm-gcc main.c -Wl,-native -o a.out -g'. Note that |
| 446 | // this should really be fixed by changing the llvm-gcc compiler driver. |
| 447 | if (Libraries[index] != "crtend" && Libraries[index] != "g") { |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 448 | std::string Tmp = "-l"+Libraries[index]; |
| 449 | StringsToDelete.push_back(strdup(Tmp.c_str())); |
| 450 | args.push_back(StringsToDelete.back()); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 452 | args.push_back(0); // Null terminate. |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 453 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 454 | // Run the compiler to assembly and link together the program. |
Misha Brukman | b0bafc5 | 2005-04-20 03:22:18 +0000 | [diff] [blame] | 455 | if (Verbose) dumpArgs(&args[0]); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 456 | int Res = sys::Program::ExecuteAndWait( |
| 457 | gcc, &args[0],(const char**)clean_env,0,0,&ErrMsg); |
Chris Lattner | c780982 | 2006-05-14 19:17:28 +0000 | [diff] [blame] | 458 | |
| 459 | delete [] clean_env; |
Chris Lattner | f394213 | 2005-09-23 06:05:46 +0000 | [diff] [blame] | 460 | |
| 461 | while (!StringsToDelete.empty()) { |
| 462 | free(StringsToDelete.back()); |
| 463 | StringsToDelete.pop_back(); |
| 464 | } |
| 465 | return Res; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | 0ebee74 | 2004-06-02 00:22:24 +0000 | [diff] [blame] | 467 | |