blob: 26b115f26290190991c301b2dfb6571a82c33677 [file] [log] [blame]
Chris Lattnera58d2be2003-09-30 03:24:28 +00001//===- GenerateCode.cpp - Functions for generating executable files ------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
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 Criswelldc0de4f2003-09-18 16:22:26 +00009//
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 Brukmanbb5a4d02003-09-30 17:33:12 +000016#include "gccld.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/Bytecode/WriteBytecodePass.h"
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000020#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 Criswelldc0de4f2003-09-18 16:22:26 +000024#include "Support/SystemUtils.h"
Chris Lattner246ce3c2003-10-24 18:09:23 +000025#include "Support/CommandLine.h"
26
27namespace {
28 cl::opt<bool>
29 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
30}
31
John Criswelldc0de4f2003-09-18 16:22:26 +000032
Misha Brukman1c534052003-09-30 17:42:57 +000033/// GenerateBytecode - generates a bytecode file from the specified module.
34///
35/// Inputs:
36/// M - The module for which bytecode should be generated.
37/// Strip - Flags whether symbols should be stripped from the output.
38/// Internalize - Flags whether all symbols should be marked internal.
39/// Out - Pointer to file stream to which to write the output.
40///
41/// Outputs:
42/// None.
43///
44/// Returns non-zero value on error.
45///
John Criswelldc0de4f2003-09-18 16:22:26 +000046int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000047GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +000048 // In addition to just linking the input from GCC, we also want to spiff it up
49 // a little bit. Do this now.
50 PassManager Passes;
51
52 // Add an appropriate TargetData instance for this module...
53 Passes.add(new TargetData("gccld", M));
54
55 // Linking modules together can lead to duplicated global constants, only keep
56 // one copy of each constant...
John Criswelldc0de4f2003-09-18 16:22:26 +000057 Passes.add(createConstantMergePass());
58
59 // If the -s command line option was specified, strip the symbols out of the
60 // resulting program to make it smaller. -s is a GCC option that we are
61 // supporting.
John Criswelldc0de4f2003-09-18 16:22:26 +000062 if (Strip)
63 Passes.add(createSymbolStrippingPass());
64
65 // Often if the programmer does not specify proper prototypes for the
66 // functions they are calling, they end up calling a vararg version of the
67 // function that does not get a body filled in (the real function has typed
68 // arguments). This pass merges the two functions.
John Criswelldc0de4f2003-09-18 16:22:26 +000069 Passes.add(createFunctionResolvingPass());
70
71 if (Internalize) {
72 // Now that composite has been compiled, scan through the module, looking
73 // for a main function. If main is defined, mark all other functions
74 // internal.
John Criswelldc0de4f2003-09-18 16:22:26 +000075 Passes.add(createInternalizePass());
76 }
77
Chris Lattnereaa35bb2003-10-23 18:25:57 +000078 // Propagate constants at call sites into the functions they call.
79 Passes.add(createIPConstantPropagationPass());
80
John Criswelldc0de4f2003-09-18 16:22:26 +000081 // Remove unused arguments from functions...
John Criswelldc0de4f2003-09-18 16:22:26 +000082 Passes.add(createDeadArgEliminationPass());
83
Chris Lattner246ce3c2003-10-24 18:09:23 +000084 if (!DisableInline)
85 Passes.add(createFunctionInliningPass()); // Inline small functions
86
John Criswelldc0de4f2003-09-18 16:22:26 +000087 // The FuncResolve pass may leave cruft around if functions were prototyped
88 // differently than they were defined. Remove this cruft.
John Criswelldc0de4f2003-09-18 16:22:26 +000089 Passes.add(createInstructionCombiningPass());
90
91 // Delete basic blocks, which optimization passes may have killed...
John Criswelldc0de4f2003-09-18 16:22:26 +000092 Passes.add(createCFGSimplificationPass());
93
94 // Now that we have optimized the program, discard unreachable functions...
John Criswelldc0de4f2003-09-18 16:22:26 +000095 Passes.add(createGlobalDCEPass());
96
97 // Add the pass that writes bytecode to the output file...
98 Passes.add(new WriteBytecodePass(Out));
99
100 // Run our queue of passes all at once now, efficiently.
101 Passes.run(*M);
102
103 return 0;
104}
105
Misha Brukman1c534052003-09-30 17:42:57 +0000106/// GenerateAssembly - generates a native assembly language source file from the
107/// specified bytecode file.
108///
109/// Inputs:
110/// InputFilename - The name of the output bytecode file.
111/// OutputFilename - The name of the file to generate.
112/// llc - The pathname to use for LLC.
113/// envp - The environment to use when running LLC.
114///
115/// Outputs:
116/// None.
117///
118/// Return non-zero value on error.
119///
John Criswelldc0de4f2003-09-18 16:22:26 +0000120int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000121GenerateAssembly(const std::string &OutputFilename,
122 const std::string &InputFilename,
123 const std::string &llc,
124 char ** const envp)
John Criswelldc0de4f2003-09-18 16:22:26 +0000125{
John Criswelldc0de4f2003-09-18 16:22:26 +0000126 // Run LLC to convert the bytecode file into assembly code.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000127 const char *cmd[8];
John Criswelldc0de4f2003-09-18 16:22:26 +0000128
Misha Brukmanb6b28432003-09-30 17:40:12 +0000129 cmd[0] = llc.c_str();
130 cmd[1] = "-f";
131 cmd[2] = "-o";
132 cmd[3] = OutputFilename.c_str();
133 cmd[4] = InputFilename.c_str();
134 cmd[5] = NULL;
John Criswelldc0de4f2003-09-18 16:22:26 +0000135
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000136 return ExecWait(cmd, envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000137}
138
Misha Brukman1c534052003-09-30 17:42:57 +0000139/// GenerateNative - generates a native assembly language source file from the
140/// specified assembly source file.
141///
142/// Inputs:
143/// InputFilename - The name of the output bytecode file.
144/// OutputFilename - The name of the file to generate.
145/// Libraries - The list of libraries with which to link.
146/// LibPaths - The list of directories in which to find libraries.
147/// gcc - The pathname to use for GGC.
148/// envp - A copy of the process's current environment.
149///
150/// Outputs:
151/// None.
152///
153/// Returns non-zero value on error.
154///
John Criswelldc0de4f2003-09-18 16:22:26 +0000155int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000156GenerateNative(const std::string &OutputFilename,
157 const std::string &InputFilename,
158 const std::vector<std::string> &Libraries,
159 const std::vector<std::string> &LibPaths,
160 const std::string &gcc,
161 char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000162 // Remove these environment variables from the environment of the
163 // programs that we will execute. It appears that GCC sets these
164 // environment variables so that the programs it uses can configure
165 // themselves identically.
166 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000167 // However, when we invoke GCC below, we want it to use its normal
168 // configuration. Hence, we must sanitize its environment.
169 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000170 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000171 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000172 RemoveEnv("LIBRARY_PATH", clean_env);
173 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
174 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
175 RemoveEnv("COMPILER_PATH", clean_env);
176 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000177
John Criswell71478b72003-09-19 20:24:23 +0000178 std::vector<const char *> cmd;
John Criswelldc0de4f2003-09-18 16:22:26 +0000179
John Criswelldc0de4f2003-09-18 16:22:26 +0000180 // Run GCC to assemble and link the program into native code.
181 //
182 // Note:
183 // We can't just assemble and link the file with the system assembler
184 // and linker because we don't know where to put the _start symbol.
185 // GCC mysteriously knows how to do it.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000186 cmd.push_back(gcc.c_str());
187 cmd.push_back("-o");
188 cmd.push_back(OutputFilename.c_str());
189 cmd.push_back(InputFilename.c_str());
John Criswelldc0de4f2003-09-18 16:22:26 +0000190
Misha Brukmanb6b28432003-09-30 17:40:12 +0000191 // Adding the library paths creates a problem for native generation. If we
192 // include the search paths from llvmgcc, then we'll be telling normal gcc
193 // to look inside of llvmgcc's library directories for libraries. This is
194 // bad because those libraries hold only bytecode files (not native object
195 // files). In the end, we attempt to link the bytecode libgcc into a native
196 // program.
Chris Lattner238cf3c2003-09-30 17:36:51 +0000197#if 0
John Criswell71478b72003-09-19 20:24:23 +0000198 // Add in the library path options.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000199 for (unsigned index=0; index < LibPaths.size(); index++) {
200 cmd.push_back("-L");
201 cmd.push_back(LibPaths[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000202 }
203#endif
204
John Criswell71478b72003-09-19 20:24:23 +0000205 // Add in the libraries to link.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000206 std::vector<std::string> Libs(Libraries);
207 for (unsigned index = 0; index < Libs.size(); index++) {
John Criswell71478b72003-09-19 20:24:23 +0000208 Libs[index] = "-l" + Libs[index];
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000209 cmd.push_back(Libs[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000210 }
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000211 cmd.push_back(NULL);
John Criswell71478b72003-09-19 20:24:23 +0000212
John Criswell71478b72003-09-19 20:24:23 +0000213 // Run the compiler to assembly and link together the program.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000214 return ExecWait(&(cmd[0]), clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000215}