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