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