1 //===- GenerateCode.cpp - Functions for generating executable files ------===//
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.
7 //===----------------------------------------------------------------------===//
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"
20 // Function: GenerateBytecode ()
23 // This function generates a bytecode file from the specified module.
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.
39 GenerateBytecode (Module * M,
44 // In addition to just linking the input from GCC, we also want to spiff it up
45 // a little bit. Do this now.
48 // Add an appropriate TargetData instance for this module...
49 Passes.add(new TargetData("gccld", M));
51 // Linking modules together can lead to duplicated global constants, only keep
52 // one copy of each constant...
54 Passes.add(createConstantMergePass());
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
61 Passes.add(createSymbolStrippingPass());
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.
68 Passes.add(createFunctionResolvingPass());
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
75 Passes.add(createInternalizePass());
78 // Remove unused arguments from functions...
80 Passes.add(createDeadArgEliminationPass());
82 // The FuncResolve pass may leave cruft around if functions were prototyped
83 // differently than they were defined. Remove this cruft.
85 Passes.add(createInstructionCombiningPass());
87 // Delete basic blocks, which optimization passes may have killed...
89 Passes.add(createCFGSimplificationPass());
91 // Now that we have optimized the program, discard unreachable functions...
93 Passes.add(createGlobalDCEPass());
95 // Add the pass that writes bytecode to the output file...
96 Passes.add(new WriteBytecodePass(Out));
98 // Run our queue of passes all at once now, efficiently.
105 // Function: GenerateAssembly ()
108 // This function generates a native assembly language source file from the
109 // specified bytecode file.
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.
125 GenerateAssembly (const std::string & OutputFilename,
126 const std::string & InputFilename,
127 const std::string & llc,
131 // Run LLC to convert the bytecode file into assembly code.
135 cmd[0] = llc.c_str();
138 cmd[3] = OutputFilename.c_str();
139 cmd[4] = InputFilename.c_str();
142 return (ExecWait (cmd, envp));
146 // Function: GenerateNative ()
149 // This function generates a native assembly language source file from the
150 // specified assembly source file.
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.
156 // LibPaths - The list of directories in which to find libraries.
157 // gcc - The pathname to use for GGC.
158 // envp - A copy of the process's current environment.
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,
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.
181 // However, when we invoke GCC below, we want it to use its normal
182 // configuration. Hence, we must sanitize it's environment.
184 char ** clean_env = CopyEnv (envp);
185 if (clean_env == NULL)
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);
195 std::vector<const char *> cmd;
198 // Run GCC to assemble and link the program into native code.
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.
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());
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
221 // Add in the library path options.
223 for (unsigned index=0; index < LibPaths.size(); index++)
225 cmd.push_back ("-L");
226 cmd.push_back (LibPaths[index].c_str());
231 // Add in the libraries to link.
233 std::vector<std::string> Libs (Libraries);
234 for (unsigned index = 0; index < Libs.size(); index++)
236 Libs[index] = "-l" + Libs[index];
237 cmd.push_back (Libs[index].c_str());
239 cmd.push_back (NULL);
242 // Run the compiler to assembly and link together the program.
244 return (ExecWait (&(cmd[0]), clean_env));