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