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