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