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" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/PassManager.h" |
Chris Lattner | cc650b6 | 2003-11-09 19:55:09 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoadValueNumbering.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 20 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Transforms/IPO.h" |
| 23 | #include "llvm/Transforms/Scalar.h" |
| 24 | #include "llvm/Transforms/Utils/Linker.h" |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 25 | #include "Support/SystemUtils.h" |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
| 27 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | cl::opt<bool> |
| 32 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| 33 | } |
| 34 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | namespace llvm { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 36 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 37 | /// GenerateBytecode - generates a bytecode file from the specified module. |
| 38 | /// |
| 39 | /// Inputs: |
| 40 | /// M - The module for which bytecode should be generated. |
| 41 | /// Strip - Flags whether symbols should be stripped from the output. |
| 42 | /// Internalize - Flags whether all symbols should be marked internal. |
| 43 | /// Out - Pointer to file stream to which to write the output. |
| 44 | /// |
| 45 | /// Outputs: |
| 46 | /// None. |
| 47 | /// |
| 48 | /// Returns non-zero value on error. |
| 49 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 50 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 51 | GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 52 | // In addition to just linking the input from GCC, we also want to spiff it up |
| 53 | // a little bit. Do this now. |
| 54 | PassManager Passes; |
| 55 | |
| 56 | // Add an appropriate TargetData instance for this module... |
| 57 | Passes.add(new TargetData("gccld", M)); |
| 58 | |
| 59 | // Linking modules together can lead to duplicated global constants, only keep |
| 60 | // one copy of each constant... |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 61 | Passes.add(createConstantMergePass()); |
| 62 | |
| 63 | // If the -s command line option was specified, strip the symbols out of the |
| 64 | // resulting program to make it smaller. -s is a GCC option that we are |
| 65 | // supporting. |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 66 | if (Strip) |
| 67 | Passes.add(createSymbolStrippingPass()); |
| 68 | |
| 69 | // Often if the programmer does not specify proper prototypes for the |
| 70 | // functions they are calling, they end up calling a vararg version of the |
| 71 | // function that does not get a body filled in (the real function has typed |
| 72 | // arguments). This pass merges the two functions. |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 73 | Passes.add(createFunctionResolvingPass()); |
| 74 | |
| 75 | if (Internalize) { |
| 76 | // Now that composite has been compiled, scan through the module, looking |
| 77 | // for a main function. If main is defined, mark all other functions |
| 78 | // internal. |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 79 | Passes.add(createInternalizePass()); |
| 80 | } |
| 81 | |
Chris Lattner | eaa35bb | 2003-10-23 18:25:57 +0000 | [diff] [blame] | 82 | // Propagate constants at call sites into the functions they call. |
| 83 | Passes.add(createIPConstantPropagationPass()); |
| 84 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 85 | // Remove unused arguments from functions... |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 86 | Passes.add(createDeadArgEliminationPass()); |
| 87 | |
Chris Lattner | 246ce3c | 2003-10-24 18:09:23 +0000 | [diff] [blame] | 88 | if (!DisableInline) |
| 89 | Passes.add(createFunctionInliningPass()); // Inline small functions |
| 90 | |
Chris Lattner | cc650b6 | 2003-11-09 19:55:09 +0000 | [diff] [blame] | 91 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 92 | // Eventually we should put an IP AA in place here. |
| 93 | |
| 94 | Passes.add(createLICMPass()); // Hoist loop invariants |
| 95 | Passes.add(createLoadValueNumberingPass()); // GVN for load instructions |
| 96 | Passes.add(createGCSEPass()); // Remove common subexprs |
| 97 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 98 | // The FuncResolve pass may leave cruft around if functions were prototyped |
| 99 | // differently than they were defined. Remove this cruft. |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 100 | Passes.add(createInstructionCombiningPass()); |
| 101 | |
| 102 | // Delete basic blocks, which optimization passes may have killed... |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 103 | Passes.add(createCFGSimplificationPass()); |
| 104 | |
| 105 | // Now that we have optimized the program, discard unreachable functions... |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 106 | Passes.add(createGlobalDCEPass()); |
| 107 | |
| 108 | // Add the pass that writes bytecode to the output file... |
| 109 | Passes.add(new WriteBytecodePass(Out)); |
| 110 | |
| 111 | // Run our queue of passes all at once now, efficiently. |
| 112 | Passes.run(*M); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 117 | /// GenerateAssembly - generates a native assembly language source file from the |
| 118 | /// specified bytecode file. |
| 119 | /// |
| 120 | /// Inputs: |
| 121 | /// InputFilename - The name of the output bytecode file. |
| 122 | /// OutputFilename - The name of the file to generate. |
| 123 | /// llc - The pathname to use for LLC. |
| 124 | /// envp - The environment to use when running LLC. |
| 125 | /// |
| 126 | /// Outputs: |
| 127 | /// None. |
| 128 | /// |
| 129 | /// Return non-zero value on error. |
| 130 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 131 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 132 | GenerateAssembly(const std::string &OutputFilename, |
| 133 | const std::string &InputFilename, |
| 134 | const std::string &llc, |
| 135 | char ** const envp) |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 136 | { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 137 | // Run LLC to convert the bytecode file into assembly code. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 138 | const char *cmd[8]; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 139 | |
Misha Brukman | b6b2843 | 2003-09-30 17:40:12 +0000 | [diff] [blame] | 140 | cmd[0] = llc.c_str(); |
| 141 | cmd[1] = "-f"; |
| 142 | cmd[2] = "-o"; |
| 143 | cmd[3] = OutputFilename.c_str(); |
| 144 | cmd[4] = InputFilename.c_str(); |
| 145 | cmd[5] = NULL; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 146 | |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 147 | return ExecWait(cmd, envp); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Misha Brukman | 1c53405 | 2003-09-30 17:42:57 +0000 | [diff] [blame] | 150 | /// GenerateNative - generates a native assembly language source file from the |
| 151 | /// specified assembly source file. |
| 152 | /// |
| 153 | /// Inputs: |
| 154 | /// InputFilename - The name of the output bytecode file. |
| 155 | /// OutputFilename - The name of the file to generate. |
| 156 | /// Libraries - The list of libraries with which to link. |
| 157 | /// LibPaths - The list of directories in which to find libraries. |
| 158 | /// gcc - The pathname to use for GGC. |
| 159 | /// envp - A copy of the process's current environment. |
| 160 | /// |
| 161 | /// Outputs: |
| 162 | /// None. |
| 163 | /// |
| 164 | /// Returns non-zero value on error. |
| 165 | /// |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 166 | int |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 167 | GenerateNative(const std::string &OutputFilename, |
| 168 | const std::string &InputFilename, |
| 169 | const std::vector<std::string> &Libraries, |
| 170 | const std::vector<std::string> &LibPaths, |
| 171 | const std::string &gcc, |
| 172 | char ** const envp) { |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 173 | // Remove these environment variables from the environment of the |
| 174 | // programs that we will execute. It appears that GCC sets these |
| 175 | // environment variables so that the programs it uses can configure |
| 176 | // themselves identically. |
| 177 | // |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 178 | // However, when we invoke GCC below, we want it to use its normal |
| 179 | // configuration. Hence, we must sanitize its environment. |
| 180 | char ** clean_env = CopyEnv(envp); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 181 | if (clean_env == NULL) |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 182 | return 1; |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 183 | RemoveEnv("LIBRARY_PATH", clean_env); |
| 184 | RemoveEnv("COLLECT_GCC_OPTIONS", clean_env); |
| 185 | RemoveEnv("GCC_EXEC_PREFIX", clean_env); |
| 186 | RemoveEnv("COMPILER_PATH", clean_env); |
| 187 | RemoveEnv("COLLECT_GCC", clean_env); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 188 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 189 | std::vector<const char *> cmd; |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 190 | |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 191 | // Run GCC to assemble and link the program into native code. |
| 192 | // |
| 193 | // Note: |
| 194 | // We can't just assemble and link the file with the system assembler |
| 195 | // and linker because we don't know where to put the _start symbol. |
| 196 | // GCC mysteriously knows how to do it. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 197 | cmd.push_back(gcc.c_str()); |
| 198 | cmd.push_back("-o"); |
| 199 | cmd.push_back(OutputFilename.c_str()); |
| 200 | cmd.push_back(InputFilename.c_str()); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 201 | |
Misha Brukman | b6b2843 | 2003-09-30 17:40:12 +0000 | [diff] [blame] | 202 | // Adding the library paths creates a problem for native generation. If we |
| 203 | // include the search paths from llvmgcc, then we'll be telling normal gcc |
| 204 | // to look inside of llvmgcc's library directories for libraries. This is |
| 205 | // bad because those libraries hold only bytecode files (not native object |
| 206 | // files). In the end, we attempt to link the bytecode libgcc into a native |
| 207 | // program. |
Chris Lattner | 238cf3c | 2003-09-30 17:36:51 +0000 | [diff] [blame] | 208 | #if 0 |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 209 | // Add in the library path options. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 210 | for (unsigned index=0; index < LibPaths.size(); index++) { |
| 211 | cmd.push_back("-L"); |
| 212 | cmd.push_back(LibPaths[index].c_str()); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 213 | } |
| 214 | #endif |
| 215 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 216 | // Add in the libraries to link. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 217 | std::vector<std::string> Libs(Libraries); |
| 218 | for (unsigned index = 0; index < Libs.size(); index++) { |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 219 | Libs[index] = "-l" + Libs[index]; |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 220 | cmd.push_back(Libs[index].c_str()); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 221 | } |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 222 | cmd.push_back(NULL); |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 223 | |
John Criswell | 71478b7 | 2003-09-19 20:24:23 +0000 | [diff] [blame] | 224 | // Run the compiler to assembly and link together the program. |
Misha Brukman | bb5a4d0 | 2003-09-30 17:33:12 +0000 | [diff] [blame] | 225 | return ExecWait(&(cmd[0]), clean_env); |
John Criswell | dc0de4f | 2003-09-18 16:22:26 +0000 | [diff] [blame] | 226 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 227 | |
| 228 | } // End llvm namespace |