1 //===- genexec.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"
24 // Function: GenerateBytecode ()
27 // This function generates a bytecode file from the specified module.
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.
43 GenerateBytecode (Module * M,
48 // In addition to just linking the input from GCC, we also want to spiff it up
49 // a little bit. Do this now.
52 // Add an appropriate TargetData instance for this module...
53 Passes.add(new TargetData("gccld", M));
55 // Linking modules together can lead to duplicated global constants, only keep
56 // one copy of each constant...
58 Passes.add(createConstantMergePass());
60 // If the -s command line option was specified, strip the symbols out of the
61 // resulting program to make it smaller. -s is a GCC option that we are
65 Passes.add(createSymbolStrippingPass());
67 // Often if the programmer does not specify proper prototypes for the
68 // functions they are calling, they end up calling a vararg version of the
69 // function that does not get a body filled in (the real function has typed
70 // arguments). This pass merges the two functions.
72 Passes.add(createFunctionResolvingPass());
75 // Now that composite has been compiled, scan through the module, looking
76 // for a main function. If main is defined, mark all other functions
79 Passes.add(createInternalizePass());
82 // Remove unused arguments from functions...
84 Passes.add(createDeadArgEliminationPass());
86 // The FuncResolve pass may leave cruft around if functions were prototyped
87 // differently than they were defined. Remove this cruft.
89 Passes.add(createInstructionCombiningPass());
91 // Delete basic blocks, which optimization passes may have killed...
93 Passes.add(createCFGSimplificationPass());
95 // Now that we have optimized the program, discard unreachable functions...
97 Passes.add(createGlobalDCEPass());
99 // Add the pass that writes bytecode to the output file...
100 Passes.add(new WriteBytecodePass(Out));
102 // Run our queue of passes all at once now, efficiently.
109 // Function: generate_assembly ()
112 // This function generates a native assembly language source file from the
113 // specified bytecode file.
116 // InputFilename - The name of the output bytecode file.
117 // OutputFilename - The name of the file to generate.
118 // llc - The pathname to use for LLC.
119 // envp - The environment to use when running LLC.
129 generate_assembly (std::string OutputFilename,
130 std::string InputFilename,
135 // Run LLC to convert the bytecode file into assembly code.
139 cmd[0] = llc.c_str();
142 cmd[3] = OutputFilename.c_str();
143 cmd[4] = InputFilename.c_str();
145 if ((ExecWait (cmd, envp)) == -1)
154 // Function: generate_native ()
157 // This function generates a native assembly language source file from the
158 // specified assembly source file.
161 // InputFilename - The name of the output bytecode file.
162 // OutputFilename - The name of the file to generate.
163 // Libraries - The list of libraries with which to link.
164 // gcc - The pathname to use for GGC.
165 // envp - A copy of the process's current environment.
175 generate_native (std::string OutputFilename,
176 std::string InputFilename,
177 std::vector<std::string> Libraries,
182 // Remove these environment variables from the environment of the
183 // programs that we will execute. It appears that GCC sets these
184 // environment variables so that the programs it uses can configure
185 // themselves identically.
187 // However, when we invoke GCC below, we want it to use its normal
188 // configuration. Hence, we must sanitize it's environment.
190 char ** clean_env = copy_env (envp);
191 if (clean_env == NULL)
195 remove_env ("LIBRARY_PATH", clean_env);
196 remove_env ("COLLECT_GCC_OPTIONS", clean_env);
197 remove_env ("GCC_EXEC_PREFIX", clean_env);
198 remove_env ("COMPILER_PATH", clean_env);
199 remove_env ("COLLECT_GCC", clean_env);
201 const char * cmd[8 + Libraries.size()];
204 // Run GCC to assemble and link the program into native code.
207 // We can't just assemble and link the file with the system assembler
208 // and linker because we don't know where to put the _start symbol.
209 // GCC mysteriously knows how to do it.
211 unsigned int index=0;
212 cmd[index++] = gcc.c_str();
214 cmd[index++] = OutputFilename.c_str();
215 cmd[index++] = InputFilename.c_str();
216 for (; (index - 4) < Libraries.size(); index++)
218 Libraries[index - 4] = "-l" + Libraries[index - 4];
219 cmd[index] = Libraries[index-4].c_str();
222 if ((ExecWait (cmd, clean_env)) == -1)