blob: d1a009b4118ad7790a428c1afeadb439551a67b7 [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"
John Criswelldc0de4f2003-09-18 16:22:26 +000025
Misha Brukman1c534052003-09-30 17:42:57 +000026/// GenerateBytecode - generates a bytecode file from the specified module.
27///
28/// Inputs:
29/// M - The module for which bytecode should be generated.
30/// Strip - Flags whether symbols should be stripped from the output.
31/// Internalize - Flags whether all symbols should be marked internal.
32/// Out - Pointer to file stream to which to write the output.
33///
34/// Outputs:
35/// None.
36///
37/// Returns non-zero value on error.
38///
John Criswelldc0de4f2003-09-18 16:22:26 +000039int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +000040GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
John Criswelldc0de4f2003-09-18 16:22:26 +000041 // In addition to just linking the input from GCC, we also want to spiff it up
42 // a little bit. Do this now.
43 PassManager Passes;
44
45 // Add an appropriate TargetData instance for this module...
46 Passes.add(new TargetData("gccld", M));
47
48 // Linking modules together can lead to duplicated global constants, only keep
49 // one copy of each constant...
John Criswelldc0de4f2003-09-18 16:22:26 +000050 Passes.add(createConstantMergePass());
51
52 // If the -s command line option was specified, strip the symbols out of the
53 // resulting program to make it smaller. -s is a GCC option that we are
54 // supporting.
John Criswelldc0de4f2003-09-18 16:22:26 +000055 if (Strip)
56 Passes.add(createSymbolStrippingPass());
57
58 // Often if the programmer does not specify proper prototypes for the
59 // functions they are calling, they end up calling a vararg version of the
60 // function that does not get a body filled in (the real function has typed
61 // arguments). This pass merges the two functions.
John Criswelldc0de4f2003-09-18 16:22:26 +000062 Passes.add(createFunctionResolvingPass());
63
64 if (Internalize) {
65 // Now that composite has been compiled, scan through the module, looking
66 // for a main function. If main is defined, mark all other functions
67 // internal.
John Criswelldc0de4f2003-09-18 16:22:26 +000068 Passes.add(createInternalizePass());
69 }
70
71 // Remove unused arguments from functions...
John Criswelldc0de4f2003-09-18 16:22:26 +000072 Passes.add(createDeadArgEliminationPass());
73
74 // The FuncResolve pass may leave cruft around if functions were prototyped
75 // differently than they were defined. Remove this cruft.
John Criswelldc0de4f2003-09-18 16:22:26 +000076 Passes.add(createInstructionCombiningPass());
77
78 // Delete basic blocks, which optimization passes may have killed...
John Criswelldc0de4f2003-09-18 16:22:26 +000079 Passes.add(createCFGSimplificationPass());
80
81 // Now that we have optimized the program, discard unreachable functions...
John Criswelldc0de4f2003-09-18 16:22:26 +000082 Passes.add(createGlobalDCEPass());
83
84 // Add the pass that writes bytecode to the output file...
85 Passes.add(new WriteBytecodePass(Out));
86
87 // Run our queue of passes all at once now, efficiently.
88 Passes.run(*M);
89
90 return 0;
91}
92
Misha Brukman1c534052003-09-30 17:42:57 +000093/// GenerateAssembly - generates a native assembly language source file from the
94/// specified bytecode file.
95///
96/// Inputs:
97/// InputFilename - The name of the output bytecode file.
98/// OutputFilename - The name of the file to generate.
99/// llc - The pathname to use for LLC.
100/// envp - The environment to use when running LLC.
101///
102/// Outputs:
103/// None.
104///
105/// Return non-zero value on error.
106///
John Criswelldc0de4f2003-09-18 16:22:26 +0000107int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000108GenerateAssembly(const std::string &OutputFilename,
109 const std::string &InputFilename,
110 const std::string &llc,
111 char ** const envp)
John Criswelldc0de4f2003-09-18 16:22:26 +0000112{
John Criswelldc0de4f2003-09-18 16:22:26 +0000113 // Run LLC to convert the bytecode file into assembly code.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000114 const char *cmd[8];
John Criswelldc0de4f2003-09-18 16:22:26 +0000115
Misha Brukmanb6b28432003-09-30 17:40:12 +0000116 cmd[0] = llc.c_str();
117 cmd[1] = "-f";
118 cmd[2] = "-o";
119 cmd[3] = OutputFilename.c_str();
120 cmd[4] = InputFilename.c_str();
121 cmd[5] = NULL;
John Criswelldc0de4f2003-09-18 16:22:26 +0000122
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000123 return ExecWait(cmd, envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000124}
125
Misha Brukman1c534052003-09-30 17:42:57 +0000126/// GenerateNative - generates a native assembly language source file from the
127/// specified assembly source file.
128///
129/// Inputs:
130/// InputFilename - The name of the output bytecode file.
131/// OutputFilename - The name of the file to generate.
132/// Libraries - The list of libraries with which to link.
133/// LibPaths - The list of directories in which to find libraries.
134/// gcc - The pathname to use for GGC.
135/// envp - A copy of the process's current environment.
136///
137/// Outputs:
138/// None.
139///
140/// Returns non-zero value on error.
141///
John Criswelldc0de4f2003-09-18 16:22:26 +0000142int
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000143GenerateNative(const std::string &OutputFilename,
144 const std::string &InputFilename,
145 const std::vector<std::string> &Libraries,
146 const std::vector<std::string> &LibPaths,
147 const std::string &gcc,
148 char ** const envp) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000149 // Remove these environment variables from the environment of the
150 // programs that we will execute. It appears that GCC sets these
151 // environment variables so that the programs it uses can configure
152 // themselves identically.
153 //
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000154 // However, when we invoke GCC below, we want it to use its normal
155 // configuration. Hence, we must sanitize its environment.
156 char ** clean_env = CopyEnv(envp);
John Criswelldc0de4f2003-09-18 16:22:26 +0000157 if (clean_env == NULL)
John Criswelldc0de4f2003-09-18 16:22:26 +0000158 return 1;
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000159 RemoveEnv("LIBRARY_PATH", clean_env);
160 RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
161 RemoveEnv("GCC_EXEC_PREFIX", clean_env);
162 RemoveEnv("COMPILER_PATH", clean_env);
163 RemoveEnv("COLLECT_GCC", clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000164
John Criswell71478b72003-09-19 20:24:23 +0000165 std::vector<const char *> cmd;
John Criswelldc0de4f2003-09-18 16:22:26 +0000166
John Criswelldc0de4f2003-09-18 16:22:26 +0000167 // Run GCC to assemble and link the program into native code.
168 //
169 // Note:
170 // We can't just assemble and link the file with the system assembler
171 // and linker because we don't know where to put the _start symbol.
172 // GCC mysteriously knows how to do it.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000173 cmd.push_back(gcc.c_str());
174 cmd.push_back("-o");
175 cmd.push_back(OutputFilename.c_str());
176 cmd.push_back(InputFilename.c_str());
John Criswelldc0de4f2003-09-18 16:22:26 +0000177
Misha Brukmanb6b28432003-09-30 17:40:12 +0000178 // Adding the library paths creates a problem for native generation. If we
179 // include the search paths from llvmgcc, then we'll be telling normal gcc
180 // to look inside of llvmgcc's library directories for libraries. This is
181 // bad because those libraries hold only bytecode files (not native object
182 // files). In the end, we attempt to link the bytecode libgcc into a native
183 // program.
Chris Lattner238cf3c2003-09-30 17:36:51 +0000184#if 0
John Criswell71478b72003-09-19 20:24:23 +0000185 // Add in the library path options.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000186 for (unsigned index=0; index < LibPaths.size(); index++) {
187 cmd.push_back("-L");
188 cmd.push_back(LibPaths[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000189 }
190#endif
191
John Criswell71478b72003-09-19 20:24:23 +0000192 // Add in the libraries to link.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000193 std::vector<std::string> Libs(Libraries);
194 for (unsigned index = 0; index < Libs.size(); index++) {
John Criswell71478b72003-09-19 20:24:23 +0000195 Libs[index] = "-l" + Libs[index];
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000196 cmd.push_back(Libs[index].c_str());
John Criswell71478b72003-09-19 20:24:23 +0000197 }
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000198 cmd.push_back(NULL);
John Criswell71478b72003-09-19 20:24:23 +0000199
John Criswell71478b72003-09-19 20:24:23 +0000200 // Run the compiler to assembly and link together the program.
Misha Brukmanbb5a4d02003-09-30 17:33:12 +0000201 return ExecWait(&(cmd[0]), clean_env);
John Criswelldc0de4f2003-09-18 16:22:26 +0000202}