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